← Back to Pull requests

fix(daemon): keep managed /compact short enough to run, send it only to an idle seat, and report it truthfully #1483

Closed
unknown closed this pull request without merging on Oct 19, 2026 05:17 UTC
View on GitHub
unknown unknown commented on Oct 18, 2026 20:52 UTC
Author

Refs #27

What went wrong

Since 0.6.10, managed compactions on Claude seats have arrived as pasted text and not run. Their attempts still read
compact-sent with delivery: delivered.

Root cause: the command got too long. A native measurement on the same Claude Code version (2.1.282), on an idle
seat, through the same OpenRig transport:

  • the 0.6.6 managed /compact, 792 characters, ran as a slash command;

  • the 0.6.10 one, 850 characters, arrived as pasted text.
  • So Claude Code stops treating a long bracketed paste as a command somewhere between 792 and 850 characters.

    The transport itself did not change between 0.6.6, 0.6.10 and main:

  • one bracketed paste (tmux paste-buffer -p) of the whole command;

  • no leading whitespace;

  • a separate Enter 200 ms later.
  • What changed is the text:

  • #928 added the prefix OpenRig managed compaction; wait for the restore request. (58 characters);

  • the map path is now in the seat's own workspace, so it's longer on real seats;

  • the fixed trust-channel sentence (about 380 characters) was already there.
  • Together they crossed the boundary.

    Two more defects in the same path:

  • The automatic path did not wait for an idle seat. It sent /compact on the first poll after the restore map was

  • ready. Since 0.6.10 the map counts as ready as soon as its .tmp file carries the marker, while the agent's
    preparation turn may still be running. The manual path already waited for idle.
  • delivery: delivered described the preparation prompt, not /compact. compact-sent only meant that tmux

  • accepted the paste and the Enter. Nothing said what happened to the command.

    The change

  • A hard length budget. MANAGED_COMPACT_COMMAND_MAX_CHARS = 600, with the native evidence in its comment.

  • composeManagedCompactCommand always keeps:
    - the #928 prefix at the start of the arguments. The precompact and refocus hooks recognise a managed compaction by
    custom_instructions.trimStart().startsWith(…), so that contract is unchanged;
    - a short trust note for the summary: "Keep in the summary: OpenRig's later restore request is
    operator-authorized; respond to it."

    Then it adds the map path, then the policy's compaction instruction, each only if it still fits. A path too long to
    fit becomes "Restore from this attempt's map."

  • What doesn't fit moves to the restore map, which the restore request has the seat read. The preparation prompt

  • asks the seat to copy into the map:
    - the full trust-channel note, word for word, always;
    - a policy instruction too long for the command, when there is one.
  • Bracketed paste is kept. Under the boundary it is proven to run as a command, so there's no reason to change the

  • transport. Typing the command unbracketed was considered and dropped: the length budget fixes the measured cause
    without a second input path.
  • Automatic /compact waits for an idle seat, as manual does, and "idle" comes from a live pane read (readinessFromPaneOnly), because the newest runtime hook can predate the rest of the preparation turn.

  • - It uses the same send-level idle wait (at most 1 s per poll).
    - A seat that isn't idle yet keeps the attempt armed (waiting_for_idle) for a later poll.
    - The existing preparation deadline (25 minutes from prep delivery) still disarms it. No new timer.
  • A truthful /compact result, with no renames.

  • - delivery is unchanged and now documented as the preparation prompt's delivery.
    - A new compact field: not_sent | submitted_unconfirmed | uncertain.
    - GET /api/compaction/state (and so rig compact --state) adds a compact sentence, for example "submitted,
    unconfirmed: OpenRig sent /compact and Enter to the seat; it does not check whether Claude Code then started
    compacting."
    - The trigger route returns the same sentence, and the CLI prints "Manual compaction for <seat>: submitted,
    unconfirmed (stage: compact-sent)".
    - stage values are unchanged.

    Changes after review (eaf26f22, 594b298f)

  • The restore request carries what the command couldn't: the exact map path when it didn't fit, and an

  • over-budget compaction instruction. That also covers --skip-map, with no new refusal.
  • The compaction instruction is captured when the attempt starts, so the preparation prompt and /compact always

  • use the same one.
  • Failed trigger responses include the compact result.

  • A busy seat's automatic attempt stays armed whenever the idle wait ran and nothing was sent, whatever the reason

  • string. The first CI failure was the new test's fake clock racing a real-time idle wait; the test now uses real
    timers.
  • /compact waits for a pane-confirmed idle seat (readinessFromPaneOnly).
  • Changes after the second review round (bd060cac)

  • An automatic retry only after a known pre-input idle deferral.

  • - SendResult gains an optional, additive inputPhase:
    - not_reached: set only on the idle-wait refusals;
    - attempted: the text write was issued and failed, and may have landed;
    - written: the text is in the input, and the send stopped at or before Enter.
    - An attempted or unknown write disarms the attempt as uncertain, with no replay.
  • No false "not sent" after a paste. A guard check that throws at the Enter stage now marks the text as written.

  • The guarded catch returns sent: true, outcome: failed, inputPhase: written, consistent with the existing
    submit_failed. compact reads uncertain ("possibly as text left in its input box without Enter").
  • --skip-map: the restore request names a map the seat completed at the attempt path anyway (the final file ends

  • with its marker). Otherwise it says that no restore map was required.
  • The budget's unit is named: UTF-16 code units (.length), not bytes or terminal cells. Pieces are included whole

  • or left out. The comment adds a second observation: a 624-character command ran on Claude Code 2.1.302, so the margin
    there is only 24. A non-ASCII test pins the bound.
  • The CLI's restore line now says "unless the daemon restarts first".
  • A changed result outside compaction (shared transport):

  • What changes: when a beforeWrite guard check throws after the text was written (at Enter), a guarded send used

  • to return sent: false. It now returns sent: true / outcome: failed / inputPhase: written.
  • Who it affects: every guarded send, not only compaction.

  • Why: it's the truthful reading, and it stops a caller from treating written text as unsent and pasting it again.
  • Main merged in, and the CI failure it explained (3ede0b30, 8657aea3)

  • 3ede0b30 merges main (48fe7827) into this branch, a normal merge with no rebase or force, so the branch is

  • tested the way CI tests it. This PR's own changes are identical, file by file, before and after the merge.
  • Why bd060cac's CI failed:

  • - CI tests the PR merged with main, and #1481 (the draft-aware typing guard) reads TmuxResult.code on a failed
    paste.
    - The new F1 test's fake failure had no code, so that read threw after the write was issued. The guarded catch
    then returned sent: false, and the attempt was recorded as not_sent.
    - The test's fake was the bug.
  • 8657aea3:

  • - The fake returns the real TmuxResult shape.
    - In the product, a throw after the text write is issued is tagged. The guarded catch returns
    inputPhase: attempted (no sent claim), and the attempt stops as uncertain.
    - *A draft_ refusal (the guard holding input before any write) is not an attempted write.
    -
    For --skip-map, the restore request checks the final map path, then <map>.tmp, and names whichever ends
    with the marker. Nothing publishes the .tmp under --skip-map.
  • A changed result to note:* on the automatic path, a draft_ refusal now stops the attempt as not_sent instead

  • of waiting.

    Changes after the third review round (d2c1fa55, 58551bc3)

  • One guard covers the whole span from a successful paste to Enter. That span is the 200 ms wait, the input-box

  • read, the binding check, the prompt-override capture and the Enter.
    - Any throw in that span reads written, so no caller reports it as unsent. A step added there later stays
    covered.
  • "Enter was not sent" is said only when the throw came before the Enter key reached the adapter. Otherwise the

  • message says the submission (Enter) outcome is unconfirmed.
  • A failed Enter receipt (submit_failed) now says the Enter reported a failure, so the submission outcome is

  • unconfirmed. It no longer says "not submitted". sent, outcome and inputPhase are unchanged.
  • The Enter stage checks typeof code === "string" before matching draft_, as the text stage does.

  • Held messages on draft-aware seats: a review concern was that busy polls on a draft-aware seat would pile up held

  • /compact messages.
    - They can't: managed compaction runs inside the delivery guard's lifecycle lease, and on an active
    draft-aware seat that lease refuses before any send
    (draft_aware_lifecycle, reported as
    guard_target_unknown).
    - So managed compaction does not run on an active draft-aware seat whose binding records its pane. That's an
    existing limit, not something this PR adds. (A correction from review: an earlier trace read the guard's "input
    absent" as an empty input box. It means no live input at all. So the refusal holds for seats with a recorded pane.)
    - A draft-aware seat whose binding has no recorded pane is now refused up front (pane_unknown_protected), on
    both paths, before any preparation prompt or /compact copy.
    - Why it's safe: the input layer already refuses every write to a managed seat without a known pane ("Managed
    pane identity unavailable or changed; no input written"). The fresh-lifecycle pane exception applies only to
    launch and restore. So this refuses nothing that works today. It makes the impossible case explicit, and
    stops it leaving held /compact copies.
    - Before this change (pinned in round 4): an automatic busy-then-idle attempt left the prep row
    indeterminate, 4× /compact retained and 1× /compact indeterminate, with 0 effects. Now: 0 outbox
    rows, 0 effects, and not_sent for a fresh attempt.
    - Detection reads the seat's binding (maybeTarget: no pane) and the guard's configured mode the way an
    automatic input lease chooses it: desired draft-aware, else effective. That's the lease's mode choice, not a full
    needsInputCheck, which also depends on the lease and lifecycle state.
    - It reads synchronously, so the manual path's in-progress guard (no await before an attempt is
    registered) is unchanged.
    - Stable modes: a stable hold keeps its existing refusal, and a seat with a recorded pane is unaffected.
    - During a mode switch, when the desired and effective modes differ and one is draft-aware, the manual path
    refuses pane_unknown_protected at once, where it used to meet the lease (and, under hold, could keep a held
    preparation copy). Nothing is written to the seat either way. This is read from the source, not a pending-mode
    test.
    - test/compaction-draft-aware.test.ts pins it: real SessionTransport, real draft-aware delivery, an outbox
    on a migrated database, the seat busy for 4 polls and then idle for 2. Every poll is refused, with zero writes
    and zero outbox rows
    (counted).

    Changed results (all disclosed for review)

    1. Shared transport:
    - Written input: a guarded send whose text was written before an exception returns
    sent: true / outcome: failed / inputPhase: written. It used to return sent: false.
    - An issued but unconfirmed write returns outcome: failed / inputPhase: attempted, with no sent claim.
    - What sent means: it is input-effect evidence, not delivery or consumption success.
    - Script-visible change: for an issued-write exception with no code, rig send now gets HTTP 409 and exits
    1
    , where it used to get HTTP 500 and exit 2.
    - Unchanged: queue classification.
    2.
    An established pre-input draft_ refusal ends automatic compaction as not_sent, rather than waiting. A
    refusal after a paste stays uncertain.
    3.
    An attempted or written /compact ends as uncertain, with no automatic replay. Only a positively known
    pre-input idle deferral re-arms, within the existing deadline.
    4.
    --skip-map: the restore request names a map completed at this attempt's path (the final file, then the .tmp,
    matched on the attempt's marker). Otherwise it says no map was required.
    5.
    Wording:
    - compact results say "possibly as text left in its input box without Enter".
    -
    A failed Enter receipt (submit_failed), on every send: "the Enter reported a failure, so the submission
    outcome is unconfirmed". It used to say "not submitted".
    -
    A written-input exception: "Enter was not sent" only when the throw came before the Enter key reached the
    adapter. Otherwise "the submission (Enter) outcome is unconfirmed".
    -
    The CLI notes the daemon-restart limit.
    6.
    Automatic /compact waits for a pane-confirmed idle seat, bounded by the existing preparation deadline.
    7.
    A compaction-only refusal (pane_unknown_protected) for a running draft-aware seat whose binding records no tmux
    pane, on both paths, before any preparation or /compact.
    -
    The manual trigger: HTTP 409 with "Refused: … its tmux pane is not recorded … no preparation or /compact was
    sent".
    Before: HTTP 502 (send_failed), with indeterminate rows.
    -
    Script-visible: rig compact exits 1, where it exited 2, and the response carries no preparation or
    compact field for this refusal.
    -
    The automatic path: it returns the reason and begins nothing; a fresh attempt leaves the state null, with no
    outbox rows or input effects.
    -
    An attempt already under way is stopped, and keeps its earlier compact evidence. Already stopped or
    /compact-sent attempts aren't rewritten, and no old outbox rows are cleared.
    -
    During a mode switch the manual path's reason can differ, as described above.
    -
    The input layer already refused this case, so no working compaction is lost.

    Tests (current head)

  • test/compaction-command-submission.test.ts (new) covers:

  • - the 600-code-unit budget (ASCII, non-ASCII, long paths and instructions);
    - the overflow carried in the map or the restore request;
    - the instruction captured at attempt start;
    - the idle wait (busy, then idle; never idle until the deadline);
    - truthful compact results;
    - --skip-map (final path, .tmp, none).
  • Through the real SessionTransport and a recording tmux, these cases each make no second /compact write:

  • -
    a preparation-deadline stop before the first paste: not_sent, 0 writes (a deadline control, not a transport
    refusal);
    -
    an issued text write that throws: uncertain, 1 write, 0 Enters;
    -
    a failed paste receipt after an idle wait: uncertain, and later polls don't write again;
    -
    a deadline or cancel between the paste and Enter;
    -
    a binding read throwing after the paste: written, "Enter was not sent";
    -
    the Enter key throwing: written, "outcome is unconfirmed";
    -
    a failed Enter receipt;
    -
    a no-code receipt.
  • test/compaction-draft-aware.test.ts (new):

  • -
    existing-behaviour controls, green before this PR's fixes:
    - automatic on a recorded-pane draft-aware seat: no sends and no outbox rows;
    - manual: the preparation prompt is delivered outside the lease, then the /compact lease refuses, with no
    /compact effect or row;
    -
    the pane-less cases, flipped in 2b0541cf: the automatic busy ×4 then idle case and the manual case both give
    pane_unknown_protected, 0 outbox rows (all counted) and 0 effects. Both were red at 34083df8;
    -
    a control: a draft-aware seat with a recorded pane isn't affected by the pane check.
  • A real zero-input refusal through the real SessionTransport: the pane stuck in a tmux mode gives

  • pane_in_mode, 0 writes, 0 Enters and not_sent, and clearing it sends nothing more. It's an existing-behaviour
    control.
  • The deadline cases are named as deadline cases, not transport refusals.

  • Every other behavioural test was shown failing first, at the head before its fix. The issued-write test also

  • fails when its exception tagging is removed.
  • Updated existing assertions: the enforcer, the route and session-transport.test.ts (the Enter-failure wording).

  • Run locally at 58551bc3:

  • - the related suites;
    - CLI send/compact;
    - lint, typecheck and build: green.
    -
    The full daemon suite: 17900 passed, 8 failed. Seven are known host-environment failures on the build machine.
    One is a tmux-control-native listing test that passes alone (load).
  • CI runs on every push. See the checks.
  • Native: NOT RUN on our box

  • The root-cause measurement above is the native evidence, taken on a separate seat. This PR has no native run of its

  • own, because this box has no Claude login.
  • The Claude Code version is a confound. Claude Code updates itself, and the threshold could move between

  • versions. The 600-character budget leaves about 190 characters of margin under the measured 792. If a later Claude
    Code lowers the boundary, this constant is the one place to change.

    Follow-ups (not in this PR)

  • Confirming that compaction ran. For example, Claude Code's compaction indicator in the pane, or the compact

  • boundary in the session transcript. Claude Code's strings are unverified, so compact stays at
    submitted_unconfirmed, with no confirmed value yet.
  • With --skip-map no map is required, so the full trust note is carried only if the seat completed a map anyway.

  • The restore request already states that it is operator-authorized, and the short note stays in the command.
  • A daemon restart loses the queued restore stage and the restore carry.* Both are held in memory only. After a

  • restart there may be no restore request at all. The attempt's restore map stays on disk at the attempt path.

    🤖 Generated with Claude Code

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    Bug Fixes
    Compaction status now distinguishes commands that were not sent, submitted without confirmation, or possibly delivered, without implying compaction has run.
    Automatic and manual compaction wait for an idle session before sending. Automatic attempts can retry after a confirmed idle deferral.
    Compaction requests preserve key trust instructions within the command length limit and include overflow details in preparation or restore prompts.
    Restore prompts identify the relevant map when available or state when no map is required.

    Documentation
    Updated /compact help text to explain prompt delivery, compaction status, and when restore and audit prompts follow.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: mvschwarz Review duration: 505m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.57) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.57 · 650 lines diff across 8 files
    0.57
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #1483"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 57% (MEDIUM) · Higher = larger blast radius
    57%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%