Repository navigation
feat(supervision-host): compact idle sessions while the prompt cache is warm - #7807
Merged
Merged
Conversation
…le, match resume predicate
…ecause the two new warm-compact tests had no timing hint. The runner packed them at the 45000 ms default, pushing the largest serial shard to 1204066 ms, over the 1200000 ms limit. I added measured hints in bin/fm-test-run.sh (fm-warm-compact.test.sh 1500, fm-warm-compact-herdr-live-e2e.test.sh 170). `bin/fm-test-run.sh --check-coverage` now passes (largest serial shard 1194253 ms), and tests/fm-supervision-instructions.test.sh passes. A full run of tests/fm-test-run.test.sh was killed at its time limit and did not finish, so it is unverified. The serial-shard failures in the logs were watcher/timing tests unrelated to warm-compact. I did not reproduce them locally. main also has a failing run. They need a rerun, not a code change
Owner
|
Speaking as Kun's firstmate: this is merged. Thank you @Azd325 — really appreciate you taking the time on this. It stays off unless a home adds |
prishsu0730
pushed a commit
to prishsu0730/firstmate
that referenced
this pull request
Oct 29, 2026
…is warm (kunchenguid#7807) * feat(bin): add opt-in warm-cache compaction for the Claude primary and host engine Refs kunchenguid#7755 * no-mistakes(review): Persist engine compaction marker, run it abortable, match resume predicate * no-mistakes(review): Run engine compaction in host shell; skip marker on abandon * no-mistakes(review): Test abandoned engine compaction is not marked done * no-mistakes(document): Clarify warm-compact episode and engine resume rules * no-mistakes(lint): Fix ShellCheck SC2015 in engine compaction marker * no-mistakes(ci): Test coverage guard and portable parallel 2 failed because the two new warm-compact tests had no timing hint. The runner packed them at the 45000 ms default, pushing the largest serial shard to 1204066 ms, over the 1200000 ms limit. I added measured hints in bin/fm-test-run.sh (fm-warm-compact.test.sh 1500, fm-warm-compact-herdr-live-e2e.test.sh 170). `bin/fm-test-run.sh --check-coverage` now passes (largest serial shard 1194253 ms), and tests/fm-supervision-instructions.test.sh passes. A full run of tests/fm-test-run.test.sh was killed at its time limit and did not finish, so it is unverified. The serial-shard failures in the logs were watcher/timing tests unrelated to warm-compact. I did not reproduce them locally. main also has a failing run. They need a rerun, not a code change (cherry picked from commit c1669d9)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What Changed
bin/fm-warm-compact-lib.sh, gated by an optionalconfig/warm-compactfile (idle-seconds, default 2700, andmin-context-tokens, default 80000). It decides when a session is due for compaction. For a Claude primary, it sends/compactthrough the backend's verified submit primitive, and only when the agent is idle and the composer is empty. For the supervision host's engine conversation, it runs/compactas a bounded headless turn.ended=andcompacted=fields. The resumable-record check is factored intoengine_record_resumable, shared withchoose_conversation. Warm-compact state files are cleared on activation.docs/configuration.mdand add a row todocs/supervision-host.md. Add tests:tests/fm-warm-compact.test.sh, additions totests/fm-supervision-host.test.sh, and a live herdr end-to-end test.Risk Assessment
Testing
The library suite passed. The live Herdr e2e passed on the rerun after a fix to the test's transcript lookup. The live run showed an idle Claude primary compacted once through the backend submit path, and the engine conversation compacted once by a resumed /compact, with no wake queued for the captain. The primary then drained and acknowledged a wake queued afterward. The host-suite cases for engine marker persistence, the non-resumable-session skip, and abandon-at-park-boundary passed in a subset run against stub engines (not live). The full host suite aborted on an unrelated, load-sensitive case; that case passed when rerun alone.
Evidence: live e2e pass log
Source: live e2e pass log
Evidence: first live run (test-lookup failure)
Source: first live run (test-lookup failure)
Evidence: host suite subset
Source: host suite subset
Evidence: full host suite run (aborted on unrelated case)
Source: full host suite run (aborted on unrelated case)
Pipeline
Updates from git push no-mistakes
✅ **Intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 3 issues found → auto-fixed (3) ✅
bin/fm-supervision-host.sh:569- The engine's once-per-idle-episode marker (WARM_COMPACT_ENGINE_DONE) lives only in host process memory, but the engine episode spans host processes. choose_conversation (bin/fm-supervision-host.sh:845-855) resumes the recorded engine session across parks, andended=in .supervision-host-engine changes only when a turn is handled. Failing sequence: (1) the engine handles a wake at T0. (2) The primary keeps working, and each Stop starts a fresh host with WARM_COMPACT_ENGINE_DONE empty. (3) After T0+idle-seconds, every new host's first tick finds idle>=2700 and compacted=0. (4) Each host then resumes the engine with /compact again. The size guard does not stop this. A /compact adds a compact_boundary and a summary user entry but no new assistant usage line, so fm_warm_compact_context_tokens (bin/fm-warm-compact-lib.sh:64-72) still reads the pre-compaction size. Result: one headless Claude run per primary turn end. Once the cache is cold, each run re-writes the context at the cache-write price, which is the cost Claude primary and supervision host are never compacted before cache expiry, so idle wakes re-write ~400k tokens #7755 (warm-compact) sets out to avoid. This breaks the intent's 'at most once per idle episode'. The primary has the same weakness, because WARM_COMPACT_PRIMARY_DONE (line 568) and the stale transcript size reset whenever the autoarm loop restarts the host in the same idle episode (bin/fm-claude-stop-autoarm.sh:446-451). Fix: persist the marker with the record it guards, for examplecompacted=<ended>in ENGINE_RECORD, written atomically like write_engine_record, and read it in warm_compact_tick. Also add a host-level test: two host runs over one engine record must produce one /compact. tests/fm-warm-compact.test.sh:118 only passes compacted=1 into the library, so it cannot catch this.bin/fm-supervision-host.sh:592- The engine compaction runs synchronously inside await_close, through a command substitution. That blocks the park loop for up to FM_WARM_COMPACT_ENGINE_TIMEOUT (300s) plus the engine grace. During that time the host does not see the arm close, so a real wake waits up to about 5 minutes. boundary_reached is also not checked. Bash defers the host's TERM/INT/HUP traps until the foreground command substitution returns, so a take-over or stop also waits that long. The compaction also bypasses ENGINE_RUNNING and ENGINE_PID_FILE, so cleanup/stop_engine_turn (lines 426-437, 448-450) cannot stop the engine. A host killed outright leaves the resumed claude process running on the session the successor resumes next. Its .warm-compact-msg/result/err temp files are also left behind, because the startup sweep only removes .supervision-host-* files (line 400). Fix: run the compaction like handle's engine turn (lines 1073-1090): background the subshell and wait on it, set ENGINE_RUNNING/ENGINE_SUBSHELL, pass ENGINE_PID_FILE to fm_supervision_engine_turn, and name the temp files under the swept .supervision-host-* prefix.bin/fm-supervision-host.sh:585- The engine check matches onlyengine=against the record. choose_conversation resumes a recorded session only when key, engine, and model all match and turns < ROTATE_TURNS (bin/fm-supervision-host.sh:845-848). The host can therefore compact a conversation that the next turn will never resume: after a model change, a prompt-key change, or a conversation at its 20-turn rotation limit. Each such compaction is a paid headless run of up to 300s with no benefit. Fix: apply the same model, key, and turns<ROTATE_TURNS predicate before calling fm_warm_compact_engine.🔧 Fix applied.
4 issues (1 error, 3 warnings) still open:
bin/fm-supervision-host.sh:569- The engine's once-per-idle-episode marker (WARM_COMPACT_ENGINE_DONE) lives only in host process memory, but the engine episode spans host processes. choose_conversation (bin/fm-supervision-host.sh:845-855) resumes the recorded engine session across parks, andended=in .supervision-host-engine changes only when a turn is handled. Failing sequence: (1) the engine handles a wake at T0. (2) The primary keeps working, and each Stop starts a fresh host with WARM_COMPACT_ENGINE_DONE empty. (3) After T0+idle-seconds, every new host's first tick finds idle>=2700 and compacted=0. (4) Each host then resumes the engine with /compact again. The size guard does not stop this. A /compact adds a compact_boundary and a summary user entry but no new assistant usage line, so fm_warm_compact_context_tokens (bin/fm-warm-compact-lib.sh:64-72) still reads the pre-compaction size. Result: one headless Claude run per primary turn end. Once the cache is cold, each run re-writes the context at the cache-write price, which is the cost Claude primary and supervision host are never compacted before cache expiry, so idle wakes re-write ~400k tokens #7755 (warm-compact) sets out to avoid. This breaks the intent's 'at most once per idle episode'. The primary has the same weakness, because WARM_COMPACT_PRIMARY_DONE (line 568) and the stale transcript size reset whenever the autoarm loop restarts the host in the same idle episode (bin/fm-claude-stop-autoarm.sh:446-451). Fix: persist the marker with the record it guards, for examplecompacted=<ended>in ENGINE_RECORD, written atomically like write_engine_record, and read it in warm_compact_tick. Also add a host-level test: two host runs over one engine record must produce one /compact. tests/fm-warm-compact.test.sh:118 only passes compacted=1 into the library, so it cannot catch this.bin/fm-supervision-host.sh:592- The engine compaction runs synchronously inside await_close, through a command substitution. That blocks the park loop for up to FM_WARM_COMPACT_ENGINE_TIMEOUT (300s) plus the engine grace. During that time the host does not see the arm close, so a real wake waits up to about 5 minutes. boundary_reached is also not checked. Bash defers the host's TERM/INT/HUP traps until the foreground command substitution returns, so a take-over or stop also waits that long. The compaction also bypasses ENGINE_RUNNING and ENGINE_PID_FILE, so cleanup/stop_engine_turn (lines 426-437, 448-450) cannot stop the engine. A host killed outright leaves the resumed claude process running on the session the successor resumes next. Its .warm-compact-msg/result/err temp files are also left behind, because the startup sweep only removes .supervision-host-* files (line 400). Fix: run the compaction like handle's engine turn (lines 1073-1090): background the subshell and wait on it, set ENGINE_RUNNING/ENGINE_SUBSHELL, pass ENGINE_PID_FILE to fm_supervision_engine_turn, and name the temp files under the swept .supervision-host-* prefix.bin/fm-supervision-host.sh:585- The engine check matches onlyengine=against the record. choose_conversation resumes a recorded session only when key, engine, and model all match and turns < ROTATE_TURNS (bin/fm-supervision-host.sh:845-848). The host can therefore compact a conversation that the next turn will never resume: after a model change, a prompt-key change, or a conversation at its 20-turn rotation limit. Each such compaction is a paid headless run of up to 300s with no benefit. Fix: apply the same model, key, and turns<ROTATE_TURNS predicate before calling fm_warm_compact_engine.bin/fm-supervision-host.sh:590- Fix round 1 did not close review-2 (abortable engine compaction). warm_compact_tick still calls the new runner through a command substitution:verdict=$(run_warm_compact_engine ...). SoENGINE_RUNNING=1andENGINE_SUBSHELL(lines 607, 615) are set only in the substitution's subshell, never in the host. The host also waits on a foreground command substitution, and bash defers the host'strap 'exit 143' TERM(line 1218) until it returns. Failing sequence: (1) an engine compaction is in flight, for up to FM_WARM_COMPACT_ENGINE_TIMEOUT (300s). (2) A take-over calls stop_recorded on the host (lines 343-355): TERM, wait 10s, then KILL. (3) The TERM is deferred, so the KILL lands. cleanup never runs: no grant release or deactivate, no lease release, no arm retire, and no HOST_RECORD removal. The orphaned substitution subshell keeps polling and keeps the resumed claude alive on the session that the successor resumes. The arm-close and boundary checks inside the runner do fix the wake-latency half. Sibling defect in the same runner: on abandonment, stop_engine_turn TERMs the engine. fm_warm_compact_engine then printsfailedinto verdict_file before the subshell exits, so the runner returnsfailed, not the documented nothing (comment at lines 600-602). warm_compact_tick then runs mark_engine_compacted. A compaction abandoned at the park boundary therefore marks the episode done although no compaction happened, and the next host never retries it. Fix: call run_warm_compact_engine directly, without$(...). Return the verdict in a global, for example WARM_COMPACT_VERDICT. Clear the verdict when the loop abandoned the turn. The handle path does the same at lines 1118-1132: it uses a barewait, so the trap fires at once. Also remove the now-unusedWARM_COMPACT_ENGINE_DONE=at line 570. No test covers a signal or an abandonment during a compaction.🔧 Fix applied.
5 issues (1 error, 3 warnings, 1 info) still open:
bin/fm-supervision-host.sh:569- The engine's once-per-idle-episode marker (WARM_COMPACT_ENGINE_DONE) lives only in host process memory, but the engine episode spans host processes. choose_conversation (bin/fm-supervision-host.sh:845-855) resumes the recorded engine session across parks, andended=in .supervision-host-engine changes only when a turn is handled. Failing sequence: (1) the engine handles a wake at T0. (2) The primary keeps working, and each Stop starts a fresh host with WARM_COMPACT_ENGINE_DONE empty. (3) After T0+idle-seconds, every new host's first tick finds idle>=2700 and compacted=0. (4) Each host then resumes the engine with /compact again. The size guard does not stop this. A /compact adds a compact_boundary and a summary user entry but no new assistant usage line, so fm_warm_compact_context_tokens (bin/fm-warm-compact-lib.sh:64-72) still reads the pre-compaction size. Result: one headless Claude run per primary turn end. Once the cache is cold, each run re-writes the context at the cache-write price, which is the cost Claude primary and supervision host are never compacted before cache expiry, so idle wakes re-write ~400k tokens #7755 (warm-compact) sets out to avoid. This breaks the intent's 'at most once per idle episode'. The primary has the same weakness, because WARM_COMPACT_PRIMARY_DONE (line 568) and the stale transcript size reset whenever the autoarm loop restarts the host in the same idle episode (bin/fm-claude-stop-autoarm.sh:446-451). Fix: persist the marker with the record it guards, for examplecompacted=<ended>in ENGINE_RECORD, written atomically like write_engine_record, and read it in warm_compact_tick. Also add a host-level test: two host runs over one engine record must produce one /compact. tests/fm-warm-compact.test.sh:118 only passes compacted=1 into the library, so it cannot catch this.bin/fm-supervision-host.sh:592- The engine compaction runs synchronously inside await_close, through a command substitution. That blocks the park loop for up to FM_WARM_COMPACT_ENGINE_TIMEOUT (300s) plus the engine grace. During that time the host does not see the arm close, so a real wake waits up to about 5 minutes. boundary_reached is also not checked. Bash defers the host's TERM/INT/HUP traps until the foreground command substitution returns, so a take-over or stop also waits that long. The compaction also bypasses ENGINE_RUNNING and ENGINE_PID_FILE, so cleanup/stop_engine_turn (lines 426-437, 448-450) cannot stop the engine. A host killed outright leaves the resumed claude process running on the session the successor resumes next. Its .warm-compact-msg/result/err temp files are also left behind, because the startup sweep only removes .supervision-host-* files (line 400). Fix: run the compaction like handle's engine turn (lines 1073-1090): background the subshell and wait on it, set ENGINE_RUNNING/ENGINE_SUBSHELL, pass ENGINE_PID_FILE to fm_supervision_engine_turn, and name the temp files under the swept .supervision-host-* prefix.bin/fm-supervision-host.sh:585- The engine check matches onlyengine=against the record. choose_conversation resumes a recorded session only when key, engine, and model all match and turns < ROTATE_TURNS (bin/fm-supervision-host.sh:845-848). The host can therefore compact a conversation that the next turn will never resume: after a model change, a prompt-key change, or a conversation at its 20-turn rotation limit. Each such compaction is a paid headless run of up to 300s with no benefit. Fix: apply the same model, key, and turns<ROTATE_TURNS predicate before calling fm_warm_compact_engine.bin/fm-supervision-host.sh:590- Fix round 1 did not close review-2 (abortable engine compaction). warm_compact_tick still calls the new runner through a command substitution:verdict=$(run_warm_compact_engine ...). SoENGINE_RUNNING=1andENGINE_SUBSHELL(lines 607, 615) are set only in the substitution's subshell, never in the host. The host also waits on a foreground command substitution, and bash defers the host'strap 'exit 143' TERM(line 1218) until it returns. Failing sequence: (1) an engine compaction is in flight, for up to FM_WARM_COMPACT_ENGINE_TIMEOUT (300s). (2) A take-over calls stop_recorded on the host (lines 343-355): TERM, wait 10s, then KILL. (3) The TERM is deferred, so the KILL lands. cleanup never runs: no grant release or deactivate, no lease release, no arm retire, and no HOST_RECORD removal. The orphaned substitution subshell keeps polling and keeps the resumed claude alive on the session that the successor resumes. The arm-close and boundary checks inside the runner do fix the wake-latency half. Sibling defect in the same runner: on abandonment, stop_engine_turn TERMs the engine. fm_warm_compact_engine then printsfailedinto verdict_file before the subshell exits, so the runner returnsfailed, not the documented nothing (comment at lines 600-602). warm_compact_tick then runs mark_engine_compacted. A compaction abandoned at the park boundary therefore marks the episode done although no compaction happened, and the next host never retries it. Fix: call run_warm_compact_engine directly, without$(...). Return the verdict in a global, for example WARM_COMPACT_VERDICT. Clear the verdict when the loop abandoned the turn. The handle path does the same at lines 1118-1132: it uses a barewait, so the trap fires at once. Also remove the now-unusedWARM_COMPACT_ENGINE_DONE=at line 570. No test covers a signal or an abandonment during a compaction.bin/fm-supervision-host.sh:619- The round-2 fix (r2-1) is correct: the runner now runs in the host shell, so ENGINE_RUNNING and ENGINE_SUBSHELL are visible to cleanup, the TERM trap fires within one 0.5s sleep, and an abandoned compaction leaves WARM_COMPACT_VERDICT empty, so it is not marked. But no test covers the new abandon branch. Failing change a test should reject: drop theabandoned=1guard at line 629, so the runner reads thefailedverdict that fm_warm_compact_engine writes after it is TERMed. The episode is then marked although no compaction happened, and every current test still passes. Add a host test: a slow stub engine, close the arm during the compaction, then assert that the record has nocompacted=line and that a later tick compacts. The test's oracle is the documented contract (an abandoned compaction is not marked).🔧 Fix applied.
✅ Re-checked - no issues remain.
tests/fm-warm-compact-herdr-live-e2e.test.sh:84- The live e2e helper transcript() took the first jsonl in the project directory. That directory also holds the engine conversation's transcript, so the pending-wake check could read the wrong file and fail. It now selects the pane's own agent session file. This is a test-only change.bash tests/fm-warm-compact.test.shFM_WARM_COMPACT_LIVE_E2E=1 tests/fm-warm-compact-herdr-live-e2e.test.sh(run 1 failed at the last step because of the test's transcript lookup; run 2 passed after the fix)bash tests/fm-supervision-host.test.sh(full suite; aborted on an unrelated timing case)subset oftests/fm-supervision-host.test.sh: owner-only dialog files, engine conversation compacted once across hosts, conversation the next turn would not resume is not compacted, compaction abandoned at the park boundary is not marked done✅ **Document** - passed
✅ No issues found.
🔧 **Lint** - 1 issue found → auto-fixed ✅
🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Push** - passed
✅ No issues found.