Compare commits

...
4 changed files with 110 additions and 160 deletions

No files matched your search

+19 -122
View File
@@ -1,31 +1,15 @@
name: Tests
# Builds, type-checks and runs every test suite on a clean GitHub runner, and records the file
# list each published package would contain. Each suite is its own job so one failure does not
# hide the others. No provider credentials or operator configuration are supplied.
# Package suites deny external networking; scenario runtime has no external route.
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
on: workflow_dispatch
permissions:
contents: read
concurrency:
group: tests-${{ github.event_name }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build-and-package:
compact-audit:
runs-on: ubuntu-latest
timeout-minutes: 30
timeout-minutes: 15
steps:
- uses: actions/checkout@v4
with:
ref: e1eb5818b88b7d817096e5fa367c90f0cf68af52
persist-credentials: false
- uses: actions/setup-node@v4
with:
@@ -33,110 +17,23 @@ jobs:
cache: npm
- run: npm ci
- run: npm run build
- name: Record the published file list of each package
- name: Pending compaction stage progress regression
shell: bash
run: |
set -euo pipefail
mkdir -p pack-lists
for p in cli daemon tui; do
(cd packages/$p && npm pack --dry-run --json --ignore-scripts) \
| node -e 'let s="";process.stdin.on("data",d=>s+=d).on("end",()=>{for(const f of JSON.parse(s)[0].files)console.log(f.path)})' \
| sort > pack-lists/$p.txt
echo "$p: $(wc -l < pack-lists/$p.txt) files"
done
- uses: actions/upload-artifact@v4
with:
name: pack-lists
path: pack-lists/
typecheck:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run lint
repo-checks:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- run: npm run test:repo
package-tests:
if: ${{ github.event_name != 'push' }}
# macOS, the platform OpenRig runs on: several daemon and cli tests read macOS ps columns,
# file-inode behaviour and release tags.
runs-on: macos-latest
timeout-minutes: 45
strategy:
fail-fast: false
matrix:
package: [daemon, cli, tui, ui]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
persist-credentials: false
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: brew install tmux
- run: npm ci
- name: Build the packages the tests import
run: npm run build
- name: Run the suite without ambient credentials or external network
run: |
suite_root=$(mktemp -d "$RUNNER_TEMP/opr-test.XXXXXX")
suite_root=$(cd "$suite_root" && pwd -P)
mkdir -p "$suite_root/home" "$suite_root/bin"
# Same system bytes, nonprivileged mode; no system file is changed.
/usr/bin/install -m 0755 /bin/ps "$suite_root/bin/ps"
# Only this job's temporary sockets (including tmux) are reachable.
env -i HOME="$suite_root/home" PATH="$PATH" TMPDIR="$suite_root" TMUX_TMPDIR="$suite_root" \
CI=true TERM=xterm-256color \
sandbox-exec -D IPC_ROOT="$suite_root" -p '(version 1)(allow default)(deny network-outbound)(allow network-outbound (remote ip "localhost:*"))(allow network-outbound (subpath (param "IPC_ROOT")))' \
/bin/bash -s "${{ matrix.package }}" <<'SUITE'
set -euo pipefail
node scripts/ci-macos-process-probe.mjs
# The production readers use bare ps. Select real system bytes only after
# self/owned-child observation and network/IPC confinement are proved.
export PATH="$TMPDIR/bin:$PATH"
test "$(command -v ps)" = "$TMPDIR/bin/ps"
exec npm run test -w "packages/$1"
SUITE
installed-scenario:
runs-on: ubuntu-latest
timeout-minutes: 35
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-node@v4
with:
node-version: 22
cache: npm
- run: npm ci
- name: Installed package, daemon restart, and seeded durability failure
run: bash scripts/run-pr-scenarios.sh
set +e
set -uo pipefail
mkdir -p proof
git rev-parse HEAD HEAD^{tree} > proof/source.txt
npm --workspace @openrig/daemon test -- test/claude-compaction-enforcer.test.ts test/context-monitor.test.ts test/compaction-drain-readiness.test.ts test/context-monitor-compaction-race.test.ts test/compaction-generation.test.ts test/compaction-preparation.test.ts test/managed-compaction-effect.test.ts test/compaction-routes.test.ts --reporter=verbose 2>&1 | tee proof/daemon.log
result=${PIPESTATUS[0]}
npm run lint 2>&1 | tee proof/typecheck.log
lint_result=${PIPESTATUS[0]}
git diff --exit-code > proof/source-unchanged.txt
printf 'daemon=%s\ntypecheck=%s\n' "$result" "$lint_result" > proof/exits.txt
test "$result" -eq 0 && test "$lint_result" -eq 0
- uses: actions/upload-artifact@v4
if: always()
with:
name: installed-scenario-evidence
path: dist/pr-scenarios/
name: compact-audit-green
path: proof/
if-no-files-found: error
@@ -38,9 +38,8 @@ import * as path from "node:path";
* write an attempt-bound restore map. Existing polls send /compact only after
* atomic publication of that completed artifact, within the preparation deadline.
* - Post-compact restore: after a successful auto-compact, the enforcer
* first sends a turn-boundary handshake once context usage drops below
* threshold, or when the latest sample is stale or unknown (a seat that
* takes no turn after /compact never refreshes its sample), then sends the
* first sends a turn-boundary handshake when the pane is idle, independently
* of trigger usage (including stale or unknown samples), then sends the
* restore prompt on a later polling tick.
* This is intentionally active because Claude hooks can provide
* context, but they do not create a new assistant turn by themselves.
@@ -687,7 +686,10 @@ export class ClaudeCompactionEnforcer {
input.sessionName,
Date.now() + this.postCompactRestoreCooldownMs,
);
this.triggeredAboveThreshold.delete(input.sessionName);
// A high sample may now finish the audit, but must not re-arm automatic compaction.
if (input.usedPercentage == null || input.usedPercentage < policy.thresholdPercent) {
this.triggeredAboveThreshold.delete(input.sessionName);
}
// OPR.0.4.3.14 — terminal manual-trigger stage (no-op for auto seats).
this.advanceManualStage(input.sessionName, "restore-sent", "audit-sent");
return { triggered: true };
@@ -714,9 +716,11 @@ export class ClaudeCompactionEnforcer {
if (!isValidThresholdPercent(policy.thresholdPercent)) {
return { triggered: false, reason: "invalid_policy" };
}
// A trigger sample must not block work already owed to this occupant. Restore reads can
// themselves exceed a low threshold; keep the same stage, authorization and idle gates.
const drained = await this.drainPendingStage(input, policy);
if (drained) return drained;
if (input.usedPercentage < policy.thresholdPercent) {
const drained = await this.drainPendingStage(input, policy);
if (drained) return drained;
this.triggeredAboveThreshold.delete(input.sessionName);
const preparation = this.pendingPreCompactPrep.get(input.sessionName);
if (preparation?.status === "compact-sent") this.pendingPreCompactPrep.delete(input.sessionName);
@@ -725,15 +729,8 @@ export class ClaudeCompactionEnforcer {
return { triggered: false, reason: "below_threshold" };
}
// OPR.0.4.3.14 — the `enabled` gate moved here (from the top of the method)
// so it guards only the auto TRIGGER (this above-threshold path). The
// below-threshold back-half above now drains regardless of `enabled`,
// because it only advances an ALREADY-INITIATED guided sequence
// (`pendingPostCompactRestore` is set only after a /compact was sent — by
// auto above OR by the manual trigger). A disabled policy therefore still
// never STARTS a compaction (unchanged observable auto behavior for a
// constant policy), while a manual trigger's restore/audit half can finish
// via this single shared path even when auto-compaction is disabled.
// No new automatic compaction while disabled. An already-owed stage above uses
// drainPendingStage's existing operator-initiated manual exemption.
if (!policy.enabled) {
return { triggered: false, reason: "disabled" };
}
@@ -227,14 +227,6 @@ describe("ClaudeCompactionEnforcer", () => {
expect(second).toEqual({ triggered: true });
expect(send.mock.calls[1]![1]).toContain("/compact");
now += 61_000;
const third = await enforcer.maybeAutoCompact({
sessionName: "claude-seat@rig",
runtime: "claude-code",
usedPercentage: 95,
});
expect(third).toEqual({ triggered: false, reason: "already_triggered_above_threshold" });
const belowBoundary = await enforcer.maybeAutoCompact({
sessionName: "claude-seat@rig",
runtime: "claude-code",
@@ -441,6 +433,52 @@ describe("ClaudeCompactionEnforcer", () => {
expect(await belowTick(enforcer)).toEqual({ triggered: true }); // no resolver -> gate inert -> drains
});
it.each(["busy", "retained"])("a fresh high sample retains the exact owed stage after %s", async kind => {
const { enforcer, send } = await queueStageWithGen("same-generation");
const highTick = () => enforcer.maybeAutoCompact({ sessionName: "claude-seat@rig", runtime: "claude-code",
usedPercentage: 95, transcriptPath: "/tmp/c.jsonl" });
const deliveredBefore = send.mock.calls.length;
// The initial boundary advances while high; the restore must still respect delivery admission.
expect(await highTick()).toEqual({ triggered: true });
const held = kind === "busy" ? { ok: false, reason: "wait_for_idle_timeout" } : { ok: true, outcome: "retained" };
send.mockResolvedValueOnce(held);
expect(await highTick()).toEqual({ triggered: false, reason: "send_failed" });
expect(send.mock.calls.at(-1)?.[1]).toContain("restoring this Claude session after compaction");
expect(send.mock.calls.at(-1)?.[2]).toEqual({ waitForIdleMs: expect.any(Number) });
expect(await highTick()).toEqual({ triggered: true });
expect(send.mock.calls.at(-1)?.[1]).toContain("restoring this Claude session after compaction");
expect(await highTick()).toEqual({ triggered: true });
expect(send.mock.calls.at(-1)?.[1]).toContain("Now audit your compaction restore");
expect(send.mock.calls.slice(deliveredBefore).some(call => String(call[1]).includes("preparation is now required") || String(call[1]).startsWith("/compact"))).toBe(false);
});
it.each(["disabled", "replacement"])("a fresh high sample preserves the pending-stage %s gate", async kind => {
const policy = { ...POLICY_ENABLED_AT_80 };
const { transport, send } = makeSessionTransport();
let generation = "original";
const enforcer = new ClaudeCompactionEnforcer(makeSettingsStore(policy), transport, {
resolveOccupantGeneration: () => generation,
});
const tick = () => enforcer.maybeAutoCompact({ sessionName: "claude-seat@rig", runtime: "claude-code", usedPercentage: 95 });
await tick(); await tick();
const count = send.mock.calls.length;
if (kind === "disabled") policy.enabled = false;
else generation = "replacement";
expect(await tick()).toEqual({ triggered: false, reason: kind === "disabled" ? "disabled" : "stale_generation" });
expect(send).toHaveBeenCalledTimes(count);
});
it.each([true, false])("high-usage manual drain keeps the operator exemption actor-gated: %s", async operatorInitiated => {
const { transport, send } = makeSessionTransport();
const enforcer = new ClaudeCompactionEnforcer(makeSettingsStore(POLICY_DISABLED), transport);
expect(await enforcer.triggerManualCompact({ sessionName: "manual@rig", runtime: "claude-code", usedPercentage: 95 },
{ operatorInitiated })).toEqual({ triggered: true, stage: "compact-sent" });
const before = send.mock.calls.length;
expect(await enforcer.maybeAutoCompact({ sessionName: "manual@rig", runtime: "claude-code", usedPercentage: 95 }))
.toEqual(operatorInitiated ? { triggered: true } : { triggered: false, reason: "disabled" });
expect(send).toHaveBeenCalledTimes(before + (operatorInitiated ? 1 : 0));
});
// A pending post-compact stage drains without a usage sample (ContextMonitor calls this when the
// latest sample is stale or unknown). A sample-less call never starts a compaction.
const drainTick = (enforcer: ClaudeCompactionEnforcer) =>
@@ -1352,7 +1390,7 @@ describe("ClaudeCompactionEnforcer", () => {
expect(enforcer.getManualCompactionState("b@rig")).toBeNull();
});
it("forward-fix B1: after a manual trigger, an above-threshold auto tick past the dedup window does NOT start a second prep (dedups); the manual back-half still drains", async () => {
it("after a manual trigger, a fresh high tick advances the owed stage without a second preparation", async () => {
const settings = makeSettingsStore(POLICY_ENABLED_AT_80);
const { transport, send } = makeSessionTransport();
const enforcer = new ClaudeCompactionEnforcer(settings, transport, {
@@ -1370,18 +1408,12 @@ describe("ClaudeCompactionEnforcer", () => {
})).toEqual({ triggered: true, stage: "compact-sent" });
expect(send).toHaveBeenCalledTimes(2);
// Advance PAST the short dedup window, then an ABOVE-threshold auto tick.
// It must dedup on triggeredAboveThreshold — NOT start a second prep.
// Advancing a pending stage is not a new trigger, even past the short dedup window.
now += 61_000;
expect(await enforcer.maybeAutoCompact({
sessionName: SEAT, runtime: "claude-code", usedPercentage: 95,
})).toEqual({ triggered: false, reason: "already_triggered_above_threshold" });
expect(send).toHaveBeenCalledTimes(2); // no third message
// The manual restore/audit back-half still drains normally once below threshold.
expect(await enforcer.maybeAutoCompact({
sessionName: SEAT, runtime: "claude-code", usedPercentage: 20, transcriptPath: "/tmp/claude.jsonl",
})).toEqual({ triggered: true }); // turn boundary
})).toEqual({ triggered: true }); // owed turn boundary, not another prep
expect(send).toHaveBeenCalledTimes(3);
expect(send).toHaveBeenLastCalledWith(SEAT, expect.stringContaining("OpenRig post-compaction turn boundary"), { waitForIdleMs: expect.any(Number) });
expect(await enforcer.maybeAutoCompact({
sessionName: SEAT, runtime: "claude-code", usedPercentage: 20, transcriptPath: "/tmp/claude.jsonl",
+29 -5
View File
@@ -150,10 +150,10 @@ describe("ContextMonitor", () => {
sampled_at: new Date().toISOString(),
};
function installRealCompactionEnforcer() {
function installRealCompactionEnforcer(threshold = 80) {
const settings = new SettingsStore(join(tmpDir, "settings.json"));
settings.set("policies.claude_compaction.enabled", "true");
settings.set("policies.claude_compaction.threshold_percent", "80");
settings.set("policies.claude_compaction.threshold_percent", String(threshold));
const send = vi.fn(async (_session: string, text: string) => {
const marker = text.match(/<!-- openrig-compaction-complete .*? -->/)?.[0];
const target = text.match(/Write this attempt's complete restore map to ("(?:[^"\\]|\\.)*")/);
@@ -186,14 +186,14 @@ describe("ContextMonitor", () => {
// Bring a seat to the point just after /compact: fresh 80% usage sends the preparation prompt,
// then the next tick sends /compact, leaving the post-compact turn boundary pending.
async function compactFreshSeat(sessionName: string, now: number) {
const send = installRealCompactionEnforcer();
async function compactFreshSeat(sessionName: string, now: number, threshold = 80) {
const send = installRealCompactionEnforcer(threshold);
const writeUsage = (percentage: number, sampledAt: number) => writeSidecar(sessionName, {
...VALID_SIDECAR,
sampled_at: new Date(sampledAt).toISOString(),
context_window: { ...VALID_SIDECAR.context_window, used_percentage: percentage, remaining_percentage: 100 - percentage },
});
writeUsage(80, now);
writeUsage(threshold, now);
await monitor.pollOnce();
expect(send).toHaveBeenCalledTimes(1);
expect(send.mock.calls[0]?.[1]).toContain("OpenRig automatic compaction preparation is now required");
@@ -251,6 +251,30 @@ describe("ContextMonitor", () => {
expect(texts.some((text) => text.includes("preparation is now required") || text.startsWith("/compact"))).toBe(false);
});
it("drains the owed restore and audit on fresh usage above a low threshold, without a second compact", async () => {
let now = Date.parse("2026-10-02T12:00:00Z");
vi.spyOn(Date, "now").mockImplementation(() => now);
const { node, sessionName } = seedClaudeNode();
const { send, writeUsage } = await compactFreshSeat(sessionName, now, 9);
writeUsage(15, now);
for (let i = 0; i < 3; i++) await monitor.pollOnce();
expect(store.getForNode(node.id, sessionName).fresh).toBe(true);
const texts = send.mock.calls.slice(2).map(call => call[1]);
expect(texts).toHaveLength(3);
expect(texts[0]).toContain("OpenRig post-compaction turn boundary.");
expect(texts[1]).toContain("restoring this Claude session after compaction");
expect(texts[2]).toContain("Now audit your compaction restore");
// Even after the cooldown, sustained high usage must not re-arm the trigger.
now += 11 * 60_000;
writeUsage(15, now);
await monitor.pollOnce();
expect(send).toHaveBeenCalledTimes(5);
writeUsage(1, now); await monitor.pollOnce();
writeUsage(15, now); await monitor.pollOnce();
expect(send).toHaveBeenCalledTimes(6);
expect(send.mock.calls[5]?.[1]).toContain("preparation is now required");
});
it("an unknown sample with nothing pending triggers nothing", async () => {
seedClaudeNode();
const send = installRealCompactionEnforcer();