Repository navigation
Cheaper auto-capture: signal gate + delta window, with a hint-channel guard - #366
Conversation
Ported from ocsi-plugin-freq@442c694 (#365), re-resolved against the #343 lineage. Two deliberate changes from the POC: 1. The signal gate moved AFTER the context read. The POC ran it before to save the read, but the read is a local host call and detecting a user-stated spec needs the messages; the expensive artifact the gate saves — the capture model call — is still saved. 2. New fourth signal: guidedStatementInWindow. The POC's three signals are all tool-ledger reads, so a session where the USER states a rule with no tool activity ('TAX_RATE must be 0.0825') would skip the capture and lose the hint channel (#339) when the session ends. A declarative marker in a windowed user message now fires the capture; questions still skip. Pinned by unit + e2e tests. Refs #365 #334
WhollyMindless
left a comment
There was a problem hiding this comment.
Independent review of 8af9d27. Two opt-in-path regressions are reproducible: a shorter context stops subsequent delta capture, and the guidance guard misses supported message shapes. Existing focused suites: 78 pass; typecheck passes. Delta-disabled and recorded 2.2.5 user/text controls pass. No live benchmark results claimed.
| */ | ||
| export function deltaMessages(msgs: unknown, since: number): unknown { | ||
| if (!Array.isArray(msgs) || since <= 0) return msgs; | ||
| return since >= msgs.length ? [] : msgs.slice(since); |
There was a problem hiding this comment.
[P2] Reset the delta window when session context shrinks
A completed capture advances the cursor to the old message count. After context compaction/replacement yields fewer messages, this returns [] even if those messages contain new user guidance. The thin-transcript check then skips capture without advancing/resetting the cursor, so subsequent turns stay unmined until the context exceeds the old count.
Real-hook probe: capture six messages successfully (model returns []), then clear only the cooldown and supply one new, >200-character user message. With captureDelta:true, generation stays at one call and the new pass is skipped; with captureDelta:false it reaches two calls. captureOnCompact:false is a supported configuration in this probe. Please account for context shrinkage/compaction instead of treating an invalid old offset as an empty new window.
There was a problem hiding this comment.
Both findings reproduced and are fixed in e7e1125.
The stuck cursor. A completed capture advanced the cursor to the count it had seen. After compaction replaced the transcript with a shorter array, deltaMessages returned an empty window, the thin-transcript check skipped the pass, and a skip leaves the cursor alone — so every later turn was skipped too, until the array grew past the stale count. An offset pointing past the end now means a replaced transcript: mine what is there, and the runtime marks the pass deltaRestart so the cursor is rewritten to the new length instead of stale + delta. "Nothing new was appended" (since === length) is still an empty window; the two are told apart by > versus ===, and both are pinned.
The message shapes. The guard had its own reader for one narrow shape, so it reported "no user rule here" for {role:"user", content:"…"} and {info:{role:"user"}, parts:[…]} — both accepted by the capture transcript parser. It now calls that parser's decode, exported as conversationTurns(), so there is one definition of who said a line. A window that decodes to no turns at all (a shape this build has never seen, or one holding only non-conversation records like 2.2.5's idle) fires rather than assumes; the comment says why the two cannot be told apart from there.
Tests:
test/capture-policy.test.ts—a fully consumed window is empty; a SHRUNK context is mined whole,shrink is told apart from 'nothing new',every shape the transcript parser accepts is read the same way(four accepted user shapes fire, three assistant shapes do not),a window the decoder cannot read at all fires rather than assumes. The first, third and fourth were RED before the fix.test/capture.test.ts, real hook, fake host —a shrunken context is mined, not skipped forever: six messages mined, cooldown cleared, context replaced by two messages holding new guidance. Before the fix the second generation call never arrived (the test timed out in the wait); now the pass runs and its prompt carries the new turn and not the mined one.
bun run gate green: 1135 unit + 30 terminal/BDD tests against a real host, 0 fail. bunx tsc --noEmit clean; lint at main's baseline (536 warnings, 147 infos).
| text?: unknown; | ||
| content?: unknown; | ||
| }; | ||
| const isUser = msg.role === "user" || msg.type === "user"; |
There was a problem hiding this comment.
[P2] Use the same accepted user-message shapes as transcript parsing
The no-tool guidance guard returns false for both {role:"user",content:"The constant TAX_RATE must be 0.0825."} and {info:{role:"user"},parts:[{type:"text",text:"The constant TAX_RATE must be 0.0825."}]}. Both shapes are supported by the existing capture transcript parser. The first loses the string content; the second loses the role and parts. The recorded {type:"user",text:...} control returns true.
With turn tracking and no tool signal, these genuine user rules get a no-signal skip before generation, contrary to the stated no-lost-guidance exception. Please share the existing role/text decoding or conservatively capture when the shape cannot be understood; tests should cover these accepted forms as well as the live 2.2.5 shape.
There was a problem hiding this comment.
Both findings reproduced and are fixed in e7e1125.
The stuck cursor. A completed capture advanced the cursor to the count it had seen. After compaction replaced the transcript with a shorter array, deltaMessages returned an empty window, the thin-transcript check skipped the pass, and a skip leaves the cursor alone — so every later turn was skipped too, until the array grew past the stale count. An offset pointing past the end now means a replaced transcript: mine what is there, and the runtime marks the pass deltaRestart so the cursor is rewritten to the new length instead of stale + delta. "Nothing new was appended" (since === length) is still an empty window; the two are told apart by > versus ===, and both are pinned.
The message shapes. The guard had its own reader for one narrow shape, so it reported "no user rule here" for {role:"user", content:"…"} and {info:{role:"user"}, parts:[…]} — both accepted by the capture transcript parser. It now calls that parser's decode, exported as conversationTurns(), so there is one definition of who said a line. A window that decodes to no turns at all (a shape this build has never seen, or one holding only non-conversation records like 2.2.5's idle) fires rather than assumes; the comment says why the two cannot be told apart from there.
Tests:
test/capture-policy.test.ts—a fully consumed window is empty; a SHRUNK context is mined whole,shrink is told apart from 'nothing new',every shape the transcript parser accepts is read the same way(four accepted user shapes fire, three assistant shapes do not),a window the decoder cannot read at all fires rather than assumes. The first, third and fourth were RED before the fix.test/capture.test.ts, real hook, fake host —a shrunken context is mined, not skipped forever: six messages mined, cooldown cleared, context replaced by two messages holding new guidance. Before the fix the second generation call never arrived (the test timed out in the wait); now the pass runs and its prompt carries the new turn and not the mined one.
bun run gate green: 1135 unit + 30 terminal/BDD tests against a real host, 0 fail. bunx tsc --noEmit clean; lint at main's baseline (536 warnings, 147 infos).
…nce guard shares the transcript decoder (#366 review) Two opt-in-path regressions from review: The cursor could not recover from compaction. A completed capture advanced it to the count it had seen; when the context came back shorter, deltaMessages returned an empty window, the thin-check skipped the pass, and a skip leaves the cursor alone - so every later turn was skipped too, until the array grew past the stale count. An offset past the end now means a REPLACED transcript: mine what is there and rewrite the cursor to the new length. 'Nothing new was appended' (since == length) is still an empty window. The guidance guard read one narrow message shape and reported no user rule for shapes the capture parser accepts ({role, content: string} and {info.role, parts}), which is the loss the guard exists to prevent. It now calls the parser's own decode (conversationTurns), and a window that decodes to no turns fires rather than assumes.
|
Review addressed in e7e1125 — see the inline replies. Both regressions reproduced; the stuck-cursor case is now pinned through the real hook (it timed out before the fix) and the shape guard shares the transcript decoder instead of its own reader. Gate green: 1135 unit + 30 e2e, tsc clean, lint unchanged. |
WhollyMindless
left a comment
There was a problem hiding this comment.
Independently rechecked e7e1125 against both findings. The original real-hook context-shrink probe now captures the replacement transcript, and all three message-shape controls pass. The changed path resets the cursor after shrink and shares structured transcript decoding for the signal guard. 87 focused capture/policy/probe tests pass, and typecheck passes. Both findings from my previous review are addressed; no live benchmark result is inferred from these local tests.
WhollyMindless
left a comment
There was a problem hiding this comment.
Follow-up from a user-requested adversarial review using GPT-6 Astra, Claude Opus 5.5, and Qwen 3.8 Thinking. I consolidated the independent reports and replayed the reproducers locally. Findings below identify their specific sources; participation in the batch does not imply all three models found every case.
Rechecked e7e1125; the previously reported delta-shrink and message-shape probes pass. Three additional cursor cases remain:
[P2] Recognize transcript replacement in both capture modes. Astra and Opus independently reproduced the signal-only case; Opus also reproduced replacement followed by regrowth.
At runtime.ts:339:
- With
captureOnSignal:true, captureDelta:false, capture six messages, then replace the context with two messages containing a new explicit “must” rule and no tool activity. The stale offset slices away the guidance window, recordsno-signal, and makes no second generation call. Enabling both options is a passing control. - With
captureDelta:true, replace that six-message context and let the replacement reach eight messages before capture observes it. The new user rule at index 0 is omitted; only indices 6–7 reach generation. Checking only whether the current array is shorter cannot distinguish this replacement from an append.
[P2] Advance the signal cursor only through turns actually captured. Astra identified this; independently replayed.
At runtime.ts:502, completion reads the current session(sid).turns.
With captureOnSignal:true, park generation after its turn-1 snapshot, deliver turn 2 and an edit through the real hooks, then release generation. The cursor records turn 2 although its work was not in the prompt. The next capture skips no-signal. Delivering turn 2 after cursor advancement captures normally. A later qualifying signal could recover the work, but ending here leaves it unmined.
[P2, ordering-dependent] Do not add the old offset to a full seeded compaction payload. Astra and Qwen independently reproduced the arithmetic; Opus also identified it in source.
At runtime.ts:499, seeded payloads are mined whole, but completion still adds cursor.msgs. A prior cursor of 2 plus a full four-message payload becomes 6 rather than 4. In a replay, the replacement then contained a two-message summary plus four new messages: the next capture treated all six as already consumed and skipped transcript_too_thin.
The reviewers differed on this last case's impact. If a shorter context is observed first, the restart self-corrects; the loss reproduces when replacement regrows before the next read. Live-host ordering/frequency is unverified.
These options are off by default. All observations above use synthetic stores and fake generation; no live benchmark claim. Please consider capturing a stable transcript/turn boundary and accounting separately for whole seeded payloads.
…iew) Three cursor cases from the adversarial round, all RED-first. A message count cannot express which messages were consumed. A transcript replaced by compaction and then grown past the old count presents an array LONGER than the cursor that shares nothing with it, so the length check called it an append and sliced the new user rule at index 0 away. The cursor now also carries a fingerprint of the last message the pass mined (FNV-1a over its flattened text), and transcriptReplaced says the array is no longer the one being counted when it is shorter OR when the boundary message differs. A cursor with no fingerprint falls back to the length check, so an upgrade never restarts a window on its own. The same replacement is now recognized with the delta OFF. There the pass mines the whole context anyway, but the guidance window was still cut at the stale count, so a replaced context sliced the user's new rule out of the only check that could open the gate without tool activity, and the pass recorded no-signal. Completion advanced the turn cursor to whatever the session had reached, so a turn delivered while the model call was in flight was marked consumed although its work was never in the prompt. The pass now snapshots the turn it describes and advances to that. A seeded compaction payload is mined whole, so the cursor advances TO its length rather than past it by the old count: a prior cursor of 2 plus a four-message payload claimed six messages consumed, and the next window came out empty. Gates: typecheck clean, lint 536 warnings / 147 infos (branch baseline), 1140 unit tests, e2e 30/30.
|
Round 3 ( 1. A count cannot say WHICH messages were consumed. Confirmed in both shapes. The cursor now also carries a fingerprint of the last message the pass mined (FNV-1a over its flattened text — an equality test on one message, never a security decision), and The same replacement is now recognized with the delta off. There the pass mines the whole context anyway, but the guidance window was still cut at the stale count, so a replaced context sliced the user's new rule out of the only check that can open the gate without tool activity, and the pass recorded 2. The turn cursor advances to the turn the pass describes. Confirmed: completion read 3. A seeded payload is the whole window. Confirmed: a prior cursor of 2 plus a four-message payload stored 6. The cursor now advances to the payload's length when the pass mined a seeded payload or a replaced transcript, and adds only for an ordinary delta window. Pinned through the compaction hook: after the seeded pass, a six-message post-compaction transcript mines exactly the two new messages and never re-sees the four it already did.
Gates. typecheck clean; lint 536 warnings / 147 infos (branch baseline); 1140 unit tests; e2e 30/30 on a clean serial run. All three options stay off by default. |
WhollyMindless
left a comment
There was a problem hiding this comment.
Rechecked a96bd49. The signal-only shrink, turn-during-generation and seeded-count arithmetic corrections pass their controls. One replacement case remains.
[P2] Do not use one repeated message as proof that the consumed prefix is unchanged.
At policy.ts:113, transcriptReplaced compares only the flattened text at cursor.msgs - 1. Different conversations can have the same assistant reply at that position; this does not require an FNV hash collision.
Real-hook reproduction with autoCapture:true, captureDelta:true:
- Capture six alternating user/assistant messages; the cursor becomes 6. Use the same ordinary assistant response text after each user message.
- Replace the context with eight messages. Index 0 contains new
GAMMA shipping must be free at 100.00guidance; the assistant text at index 5 equals the old boundary text. The last pair containsDELTAwork. - Trigger capture again. The generation prompt contains DELTA but not GAMMA, and completion advances the cursor to 8, marking the omitted prefix consumed.
- Control: change only the assistant text at index 5. Replacement is detected and GAMMA reaches the prompt.
Both transcripts are short enough to avoid prompt cropping. The earlier seeded-compaction reproducer also retains a failure with this repeated-boundary shape, although its cursor arithmetic now correctly becomes 4 rather than 6. Independent compact controls: five pass, two fail on assertions for this same residual; typecheck passes. Other focused repository tests pass.
Please consider validating consumed message identities or the consumed prefix, or tracking context replacement explicitly, rather than one boundary message alone. Options remain off by default; live-host frequency is unmeasured. The other reviewed fixes remain intact.
…ge (#366 review, round 4) A single message cannot prove the consumed prefix is the same prefix. Ordinary assistant replies repeat: a replaced transcript wearing the same sentence at the boundary index looked unchanged, so the new guidance below the cursor was sliced out of the window AND marked consumed when the pass advanced the cursor to 8. No hash collision required. The invariant the cursor actually needs is the first messages, message for message: if those are the ones the last pass mined, mining from is correct whatever was appended after. prefixFingerprint digests the flattened consumed prefix (length plus two 32-bit accumulators, so two different-length prefixes cannot agree), and the runtime fingerprints the array AS READ up to the total it is recording, since the window variable no longer holds the prefix. Cost is one flatten of text the pass already flattens once, in-process. What the delta exists to avoid paying is the capture model call, not this. Pinned RED-first at both altitudes: the policy case (same reply at the boundary, different prefix, plus an append on the same prefix staying an append) and the real-hook reproduction (six alternating messages with one repeated reply, then a replaced-then-regrown eight where index 5 repeats the old boundary text). The seeded-compaction test now uses the repeated-reply shape too.
…ge (#366 review, round 4) A single message cannot prove the consumed prefix is the same prefix. Ordinary assistant replies repeat: a replaced transcript wearing the same sentence at the boundary index looked unchanged, so the new guidance below the cursor was sliced out of the window AND marked consumed when the pass advanced the cursor to 8. No hash collision required. The invariant the cursor actually needs is the first N messages, message for message: if those are the ones the last pass mined, mining from N is correct whatever was appended after. prefixFingerprint digests the flattened consumed prefix (length plus two 32-bit accumulators, so two different-length prefixes cannot agree), and the runtime fingerprints the array AS READ up to the total it is recording, since the window variable no longer holds the prefix. Cost is one flatten of text the pass already flattens once, in-process. What the delta exists to avoid paying is the capture model call, not this. Pinned RED-first at both altitudes: the policy case (same reply at the boundary, different prefix, plus an append on the same prefix staying an append) and the real-hook reproduction (six alternating messages with one repeated reply, then a replaced-then-regrown eight where index 5 repeats the old boundary text). The seeded-compaction test now uses the repeated-reply shape too.
324ffae to
01cc0a7
Compare
|
Round 4 ( What the cursor actually needs. The first N messages, message for message. If those are the ones the last pass mined, then mining from N is correct whatever was appended after; if any of them differs, the array is a different transcript. One message at index N-1 proves neither. So Where it is taken. The runtime now fingerprints the array AS READ up to the total it records, not the Cost. One flatten of text the pass has already flattened once, in-process, plus a hash over it. The delta exists to stop paying the capture MODEL call for an answered window; it was never about local string work. Say the word if you want a measured number on a long transcript instead. Pins. Policy altitude: same reply text at the boundary, different prefix → replaced; an append on top of the same prefix, same reply or not → still an append. Real-hook altitude: your four steps verbatim — six alternating messages with one repeated assistant reply, then a replaced-then-regrown eight with the old boundary text at index 5 and new guidance at index 0 — asserting GAMMA reaches the prompt and ALPHA does not. The seeded-compaction test now uses the repeated-reply shape too, so the case you noted it retains is covered there as well. Gates. typecheck clean; lint 536 warnings / 147 infos (branch baseline); 1142 unit tests; e2e 30/30 serial. Note the branch head moved by force-with-lease once after the first push: the tree is identical, only the commit message changed (a shell ate two backticked words). Options stay off by default. |
WhollyMindless
left a comment
There was a problem hiding this comment.
Verified 01cc0a7df307805b7a974b335a7b4cd9fb9d8643; this supersedes my previous changes request for the repeated-boundary replacement case. I reported that case; the correction is maintainer-authored.
Replayed the original six-to-eight-message replacement with identical assistant text at the old boundary. GAMMA now reaches the generation prompt alongside DELTA. The control changing only the boundary text also passes. The seeded-compaction/replaced-prefix case now captures its previously omitted guidance as well.
Checked that the stored fingerprint is taken from the consumed prefix of the full array as read, not the sliced delta. The signal-only shrink, turn arriving during parked generation, both-options and after-completion controls remain green.
Validation on Windows/Bun 1.4.4: 96 focused capture/policy tests, including seven adapted independent controls, pass; typecheck passes. No additional actionable finding in this revision. No live model, full-suite, cost benchmark or rollout certification.
…elta # Conflicts: # src/capture/runtime.ts
|
Merged into The merge touched |
# [0.22.0](v0.21.0...v0.22.0) (2026-11-09) ### Bug Fixes * **capture:** a replaced context restarts the delta window; the guidance guard shares the transcript decoder ([#366](#366) review) ([e7e1125](e7e1125)) * **capture:** fingerprint the consumed prefix, not one boundary message ([#366](#366) review, round 4) ([01cc0a7](01cc0a7)) * **capture:** the cursor names a boundary, not just a count ([#366](#366) review) ([a96bd49](a96bd49)) ### Features * **capture:** signal-gate and delta-window capture (both opt-in) ([8af9d27](8af9d27)), closes [#343](#343) [#339](#339) [#365](#365) [#334](#334)
|
🎉 This PR is included in version 0.22.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What this does
Auto-capture currently runs at the end of every session and re-reads the whole conversation each time. Each run costs roughly 2,000 prompt tokens and 20-35 seconds of model time. This PR adds two switches to make that cheaper. Both are off by default.
captureOnSignal— capture only when the session did something worth remembering: edited a file, turned a failing build or test into a pass, ran a command that passed for the first time, or heard the user state a rule. A turn that only read files skips the model call.captureDelta— remember what capture already read for this session and send only the new messages, instead of re-mining the full transcript every pass.The important exception
If the user states a rule in their own words ("The constant TAX_RATE in src/pricing.ts must be 0.0825.") and nothing else happened, capture still runs. We look for rule-like wording (must, never, always, rule, spec, ...) in the user's recent messages. A false alarm costs one capture we would have paid for anyway; missing a stated rule would silently break the feature that carries user guidance into the next session. When unsure, capture.
How it works
src/capture/policy.tsholds the decision logic and a per-session cursor; the capture path consults both.Tests
bunx tsc --noEmitclean,bun run lintclean,bun test1180 pass / 19 skip / 0 fail.The cost measurement (the table on #365) comes next; until it lands, both switches stay off by default.