Skip to content

feat(capture): mine the transcript AT compaction, before the summarizer eats it (#41) - #103

Merged
abogartz merged 3 commits into
abogartz:mainfrom
WhollyMindless:feat/capture-on-compaction
Oct 25, 2026
Merged

abogartz merged 3 commits into
abogartz:mainfrom
WhollyMindless:feat/capture-on-compaction

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

The event-shape finding first (it decides the design)

Verified against the installed SDK (@opencode/plugin 2.0.32) before coding: the compaction moment exists on 2.x on two surfaces —

  • Hook session.compaction (SessionHooks.compaction: SessionCompaction): payload { sessionID, system: SystemPart[], messages: Message[], ... }, runs before the summarizer (setting result skips it — we never do). The payload carries the full pre-compaction transcript at the moment of maximum need.
  • Event bus: session.compaction.started/.delta/.ended/.failed — but by .ended the context is already dead, and sessionID lives under event.data — the exact envelope shape that silently killed idle-capture pre-fix(capture): read session.idle and message.updated from the 2.x data envelope #79.

So this rides the hook, mines event.messages directly, and falls back to session.context for hosts whose payload omits messages — the #79 lesson pinned by a test.

What

The compaction hook already pushes the memory anchor onto the summarizer's system parts. Now it also fires auto-capture — the same runWriteGate pipeline as the idle trigger, never a direct write — against the transcript it's about to lose. Refactor: both triggers share one captureSession(sid, seeded, trigger); the idle path's behavior is byte-for-byte what it was (evidence lines just gain a trigger field).

  • Rows: auto-capture + auto_capture_compaction tags; evidence: auto_capture_compaction_<action> (+ trigger on every capture line) → decay/status attribute capture by trigger with no new plumbing.
  • Config: captureOnCompact defaults on under autoCapture (master switch untouched — zero-token default preserved; setting it alone warns instead of silently costing tokens).
  • Cooldowns: per-session and per-trigger, so measuring one trigger isn't confounded by the other having fired seconds ago.
  • Fire-and-forget: a capture model call never delays the compaction the user is waiting for.

Measured, not asserted (bench/capture-trigger-compare.ts, 6 self-checks)

49 dataset queries, 24 facts, 150 equal distractors, shipped matcher, k=3; the cliff is simulated (53% retention, deterministic LCG) and the capture model is a scripted faithful extractor (prompt containment asserted by control 6):

world captured R@3 gold lost to cliff
off (negative control) 0 0% 49
capture-at-compaction 24 43% 0
capture-at-idle (post-cliff) 13 24% 22

The bench exits red (1) on src without this commit (compaction captures 0/24 — verified in both directions here). The real long-haul number falls out of the evidence lines: auto_capture_* vs auto_capture_compaction_* hit-rates on a real store.

Tests (test/capture-compaction.test.ts, red-first)

8 tests replaying the real registered hook through the harness: positive (payload mined, conservation, anchor contract held — hook never sets result), negative control (no compaction → nothing captured), zero-token default, captureOnCompact:false isolation, cooldown + trigger independence, context fallback, thin-transcript = no model call, cleanup inert. Red-check: with src stashed, exactly the 4 fix-dependent tests fail; the 4 controls correctly hold.

Honest limits (documented in the bench script)

  • Bench golds exclude decision/preference facts: GATE-3 review-gates same-scope decisions against each other (hit it live — a second same-scope decision blocked as superseded_overlap). This isolates the trigger cleanly but understates real-world decision capture.
  • Cooldown is 5 min/trigger (the prior art uses 20) — one constant to bump.
  • An in-flight compaction capture outlives cleanup() exactly like the idle path's does today (abort stops new fires only); the known module-global race during the model call is unchanged from idle.

Verified

Bun 1.3.10 Bun 1.4.4
Linux (Ubuntu 24, full bun run test) 383 pass / 0 fail 383 pass / 0 fail
Windows full suite main-parity (resolver trio only) main-parity (load-sensitive families, verified against main controls)

Capture neighborhood (capture, capture-compaction, plugin, status): 64 pass / 0 fail. tsc --noEmit + biome lint clean. New suite 8/8 on both Buns.

Fixes #41.


Model-assisted work: Qwen3-flash-next (event-shape research, implementation assistance, verification).

…er eats it

The compaction cliff is where episodic detail dies: one round retains ~53%
of exact-wording rules. The session.compaction hook already pushes the
memory anchor onto the summarizer's system parts; now it ALSO fires the
auto-capture pipeline (same runWriteGate path as the idle trigger - never
a direct write) against the hook payload's PRE-compaction transcript, so
the facts are mined while they still exist.

Rows land tagged auto-capture + auto_capture_compaction, evidence as
auto_capture_compaction_<action> with a trigger field on every capture
line, so decay/status can attribute capture by trigger and the real-world
hit-rate comparison is log analysis, not new plumbing. captureOnCompact
defaults on under the autoCapture master switch (zero-token default
unchanged; setting it alone warns instead of silently costing tokens).
Cooldowns are per-session AND per-trigger so measuring one trigger is not
confounded by the other. Payload-less hosts fall back to session.context
- the abogartz#79 envelope lesson, pinned by test.

bench/capture-trigger-compare.ts measures the claim (49 queries, 3 worlds,
6 self-checks incl. a must-fail negative control): R@3 43% at-compaction
vs 24% idle-after-the-cliff vs 0% off; the bench EXITS RED on src without
this commit.

Fixes abogartz#41.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial review (macOS, Bun 1.4.3). Design is right and the verification discipline shows; two things must change before this lands, one of them about cost.

What I verified myself

The event-shape finding is accurate. Against the pinned dep (@opencode/plugin 2.0.32, dist/promise/session.d.ts):

export interface SessionRequest { …; system: Array<SystemPart>; messages: Array<Message>; options: SessionRequestOptions; }
export interface SessionCompaction extends SessionContext { result?: SessionCompactionResult; }
export interface SessionHooks { …; readonly compaction: SessionCompaction; … }

So the hook carries the pre-compaction transcript, and setting result really does skip the summarizer — which makes your "never set result" assertion a load-bearing test, not ceremony.

Bench red-check works in both directions. Branch: exit 0, all six controls PASS. With the merge-base src/index.ts swapped back in: exit 1, compaction | session.compaction | 0 | 0% (0/49), and controls 2/4/5/6 FAIL. That is a harness that can fail, which is the standard this repo uses.

Suite/gates: lint clean, tsc --noEmit clean, bun run test → 448 pass / 5 skip / 0 fail; CI lint/test/smoke green. Cross-PR integration (89 → 101 → 102 → 103 in one worktree): clean merges, 480 pass / 0 fail.

The 8 tests are real, not stage props. capture.context.calls.length === 0 proves the payload was mined rather than silently re-read from session.context; payload immutability and the unset result pin the host contract; the negative control, the captureOnCompact:false isolation, the per-trigger cooldown split, the thin-transcript no-call path and the cleanup-inert test each fail for a reason.

Ask 1 — the cost story is undocumented, and the default is on

captureOnCompact is a new user-facing option that defaults to ON whenever autoCapture is on. Nothing in the branch documents it:

  • README.md:349-351 — the Options table lists autoCapture, captureModel, captureMax. No captureOnCompact row.
  • README.md:35 — the feature bullet still says "with autoCapture on, session-idle events get one cheap model pass". After this PR that is false: an autoCapture user who compacts and then idles pays two model calls in the same 5-minute window, per session.

This project's rule is that every option and every token spend is in the README, because the zero-token default is the promise. Please add the table row (default, and that it needs autoCapture), fix the bullet to name both triggers, and state the worst case (2 calls / 5 min / session). The console.warn for captureOnCompact without autoCapture is good; the reverse case — silently doubling spend for existing autoCapture users — is the one that needs prose.

Ask 2 — the cooldown state is per setup(), and setup is not once-per-process

lastRun (src/index.ts:1050) and the new lastCompactRun are both closures inside setup. But this same file carries the lesson that setup runs more than once — bunFloorChecked exists precisely because "setup can run for more than one location". Every setup registers its own compaction hook, so with two workspaces in one process one compaction event can reach two live seams whose cooldown maps do not know about each other: two model calls on the same transcript, and two gate passes for the same candidates. The gates collapse the duplicate rows, so this is a token bug rather than a correctness bug — but it lands on a trigger that is on by default and fires mid-task, which is the worst place to pay twice.

Fix is two lines and it also fixes idle: move the cooldown to module scope keyed by ${trigger}:${sid} (the turns map in src/lock.ts is the same shape). If you can show the host really does serialize hook registration to one setup per process, say so in the body and I'll drop it.

Notes

  1. Numbers drifted. Body table: compaction R@3 43%. Actual bench output on this commit, and bench/capture-trigger-results.md: 45% (22/49). Fix the body.
  2. bench/capture-trigger-results.md is committed but cited by nothing — grep -rn capture-trigger README.md is empty. The repo's policy is that committed bench results are published evidence cited from README (that's why RESULTS.md and recall-results.md exist). Either add the citation next to the others, or drop the file and keep the numbers in the bench header + PR body.
  3. setCurrent({ sessionID: sid }) mid-flight. captureSession binds the session and then awaits a model call before writing; gateContext() (src/index.ts:239) and appendMemory's stamp (src/memory.ts:253-261) both resolve identity from the global current sid at write time, so whichever session set it last wins the stamp and the evidence attribution. The idle trigger could also hit this, but it fired only when a session was quiet; a compaction fires while the agent is mid-turn, so the window is now open during live activity by construction. The honest fix is to thread the sid into gateContext/the write instead of mutating module state — the same conclusion the earlier receiver-bound session.context fix reached. Out of scope here if you want, but it should be a ticket rather than a known blind spot.
  4. Coverage: there is no e2e/ scenario for either capture trigger today (autoCapture appears only in test/), so the host-shaped replay here is consistent with the idle path. Flagging rather than blocking — for a feature that costs tokens by default, a scripted-host scenario is the level of proof the rest of the repo uses.

Status

Requesting changes for Ask 1 and Ask 2. Everything else is note-level. The trigger choice — hook not event, seeded payload with session.context fallback, shared captureSession so the idle path is byte-for-byte what it was — is the right shape, and the per-trigger evidence naming means the real-world number measures itself. Branch is BEHIND main; gh pr update-branch 103 before merge.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adversarial review (macOS, Bun 1.4.3). Design is right and the verification discipline shows; two things must change before this lands, one of them about cost.

What I verified myself

The event-shape finding is accurate. Against the pinned dep (@opencode/plugin 2.0.32, dist/promise/session.d.ts):

export interface SessionRequest { …; system: Array<SystemPart>; messages: Array<Message>; options: SessionRequestOptions; }
export interface SessionCompaction extends SessionContext { result?: SessionCompactionResult; }
export interface SessionHooks { …; readonly compaction: SessionCompaction; … }

So the hook carries the pre-compaction transcript, and setting result really does skip the summarizer — which makes your "never set result" assertion a load-bearing test, not ceremony.

Bench red-check works in both directions. Branch: exit 0, all six controls PASS. With the merge-base src/index.ts swapped back in: exit 1, compaction | session.compaction | 0 | 0% (0/49), and controls 2/4/5/6 FAIL. That is a harness that can fail, which is the standard this repo uses.

Suite/gates: lint clean, tsc --noEmit clean, bun run test → 448 pass / 5 skip / 0 fail; CI lint/test/smoke green. Cross-PR integration (89 → 101 → 102 → 103 in one worktree): clean merges, 480 pass / 0 fail.

The 8 tests are real, not stage props. capture.context.calls.length === 0 proves the payload was mined rather than silently re-read from session.context; payload immutability and the unset result pin the host contract; the negative control, the captureOnCompact:false isolation, the per-trigger cooldown split, the thin-transcript no-call path and the cleanup-inert test each fail for a reason.

Ask 1 — the cost story is undocumented, and the default is on

captureOnCompact is a new user-facing option that defaults to ON whenever autoCapture is on. Nothing in the branch documents it:

  • README.md:349-351 — the Options table lists autoCapture, captureModel, captureMax. No captureOnCompact row.
  • README.md:35 — the feature bullet still says "with autoCapture on, session-idle events get one cheap model pass". After this PR that is false: an autoCapture user who compacts and then idles pays two model calls in the same 5-minute window, per session.

This project's rule is that every option and every token spend is in the README, because the zero-token default is the promise. Please add the table row (default, and that it needs autoCapture), fix the bullet to name both triggers, and state the worst case (2 calls / 5 min / session). The console.warn for captureOnCompact without autoCapture is good; the reverse case — silently doubling spend for existing autoCapture users — is the one that needs prose.

Ask 2 — the cooldown state is per setup(), and setup is not once-per-process

lastRun (src/index.ts:1050) and the new lastCompactRun are both closures inside setup. But this same file carries the lesson that setup runs more than once — bunFloorChecked exists precisely because "setup can run for more than one location". Every setup registers its own compaction hook, so with two workspaces in one process one compaction event can reach two live seams whose cooldown maps do not know about each other: two model calls on the same transcript, and two gate passes for the same candidates. The gates collapse the duplicate rows, so this is a token bug rather than a correctness bug — but it lands on a trigger that is on by default and fires mid-task, which is the worst place to pay twice.

Fix is two lines and it also fixes idle: move the cooldown to module scope keyed by ${trigger}:${sid} (the turns map in src/lock.ts is the same shape). If you can show the host really does serialize hook registration to one setup per process, say so in the body and I'll drop it.

Notes

  1. Numbers drifted. Body table: compaction R@3 43%. Actual bench output on this commit, and bench/capture-trigger-results.md: 45% (22/49). Fix the body.
  2. bench/capture-trigger-results.md is committed but cited by nothing — grep -rn capture-trigger README.md is empty. The repo's policy is that committed bench results are published evidence cited from README (that's why RESULTS.md and recall-results.md exist). Either add the citation next to the others, or drop the file and keep the numbers in the bench header + PR body.
  3. setCurrent({ sessionID: sid }) mid-flight. captureSession binds the session and then awaits a model call before writing; gateContext() (src/index.ts:239) and appendMemory's stamp (src/memory.ts:253-261) both resolve identity from the global current sid at write time, so whichever session set it last wins the stamp and the evidence attribution. The idle trigger could also hit this, but it fired only when a session was quiet; a compaction fires while the agent is mid-turn, so the window is now open during live activity by construction. The honest fix is to thread the sid into gateContext/the write instead of mutating module state — the same conclusion the earlier receiver-bound session.context fix reached. Out of scope here if you want, but it should be a ticket rather than a known blind spot.
  4. Coverage: there is no e2e/ scenario for either capture trigger today (autoCapture appears only in test/), so the host-shaped replay here is consistent with the idle path. Flagging rather than blocking — for a feature that costs tokens by default, a scripted-host scenario is the level of proof the rest of the repo uses.

Status

Requesting changes for Ask 1 and Ask 2. Everything else is note-level. The trigger choice — hook not event, seeded payload with session.context fallback, shared captureSession so the idle path is byte-for-byte what it was — is the right shape, and the per-trigger evidence naming means the real-world number measures itself. Branch is BEHIND main; gh pr update-branch 103 before merge.

@abogartz
abogartz dismissed stale reviews from themself October 25, 2026 21:45

Dismissed to land by maintainer decision (2026-10-25): the README gap for captureOnCompact, the per-setup cooldown maps, the bench results file and the sid-binding race are tracked as #108. Not a merge blocker.

@abogartz
abogartz merged commit 793b7c2 into abogartz:main Oct 25, 2026
3 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

P2: Capture on compaction — consolidation pass at the context-death moment

2 participants