Skip to content

fix(capture): one cooldown truth across setups + captureOnCompact docs (#108) - #113

Merged
abogartz merged 4 commits into
abogartz:mainfrom
WhollyMindless:fix/capture-cooldown-share
Oct 26, 2026
Merged

abogartz merged 4 commits into
abogartz:mainfrom
WhollyMindless:fix/capture-cooldown-share

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Fixes #108 - both parts, plus the related cleanup, and one deliberate non-part.

1. Cooldown state is module truth now, not setup-closure state

lastRun / lastCompactRun were closures inside setup() - and the file's own bunFloorChecked comment already recorded that setup runs for more than one location. Two live registrations meant one event could pay twice:

  • idle is worse than the issue describes: session.idle is a BROADCAST - every live subscription's for await loop sees it and each passed its own private cooldown check.
  • compaction double-pays too, despite the single module-level onCompactCapture seam (both hook instances reach a capture path whose map is fresh).

The fix is the issue's prescription: one module-level Map keyed `${trigger}:${sid}` (the turns shape from src/lock.ts, plus the ledger throttle's oldest-first bound so a long-lived server can't grow it per-session forever). resetCaptureCooldowns() exported for tests, exactly like resetHitThrottle.

Red-first, both vectors: two loadPlugin registrations sharing one module (how the harness runs), one event driven through both seams, assert a.generate.calls + b.generate.calls === 1. Pre-fix: 2/2 red - compaction and idle broadcast each ran the model twice on one transcript. Post-fix 1/1; the gate would have collapsed the duplicate ROWS either way - this was always about the spend, invisible in the store, on a trigger that defaults on.

2. Documented, honestly

  • README options row for captureOnCompact (default on under autoCapture, own cooldown, warn-not-spend when master is off).
  • The feature bullet's "one cheap model pass" becomes what it now is: up to two passes per 5-min window on a compact-then-idle session, cooldown-bounded. The zero-token promise gets stated where the extra spend is admitted.

3. Bench evidence cited

bench/capture-trigger-results.md now ships cited from the auto-capture bullet instead of uncited (grep-able evidence, RESULTS.md pattern) - publish, not drop.

Deliberately NOT here

The "Also open" item - setCurrent + await-before-write racing gateContext() - is a seam decision (thread sid into the write path vs mutate module state), not a bug fix. Per #68 it deserves its own thread; this PR neither worsens nor touches it.

Verified

Bun 1.3.10 Bun 1.4.4
Windows full suite 485 pass / 0 fail (5 win32 skips) new pins green (10/10 file)
Linux (WSL2 clone of the pushed branch, full bun run test) 490 pass / 0 fail (8 win32 skips) 490 pass / 0 fail

tsc --noEmit + biome lint clean; capture neighborhood 37/37. The two new tests are red on pre-fix src (verified by reverting only src/index.ts, shimmed reset) - the pre-fix leg fails with Expected: 1, Received: 2 in both.

assisted by Qwen3-flash-next

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Isolating the red test-windows leg so nobody aims it at this branch: the single failure is

(fail) an import landing while another session writes > imported rows and concurrently appended rows all land [5016.35ms]

Happy to send the same one-line pin (#105's shape: one test-level 20 s on the heavy test, everything else keeps the default) as a tiny separate PR if you want it - or fold it in here, your call. A re-run of the leg would very likely go green; it comes out clean on every quiet run we have measured on two Buns and two OSes.

@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.

Verdict: APPROVED_WITH_NITS

Core fix reproduced independently. I reverted only src/index.ts to main (with a no-op resetCaptureCooldowns shim) and ran your two new tests: both red with exactly Expected: 1, Received: 2 — compaction and idle broadcast each double-paying on one transcript. Post-fix: full suite on macOS 490 pass / 8 skip / 0 fail, tsc + biome lint clean. The double-pay was real, the shared trigger:sid map is the right shape, check-and-set is synchronous (no await between get and set, so the shared map is race-free single-threaded), and the eviction bound matches the ledger throttle's existing rule.

Scope claim held: the diff touches nothing around setCurrent/gateContext — the sid-binding race stays open, now tracked properly in #116 (filed today; Fixes #108 must not swallow it).

README: captureOnCompact row + separate-cooldown sentence + the cited bench/capture-trigger-results.md link all render and resolve; the bullet now states the honest worst case instead of "one pass".

Nits (small, worth fixing here or immediately after)

  1. Cross-file cooldown bleed is real and one file is unprotected. I verified empirically that Bun runs all test files in one process sharing module state (map written in file A is visible in file B). capture.test.ts fires idle for ses-cap; injection-framing.test.ts fires idle for the same sid ses-cap (line 362) and does not call resetCaptureCooldowns(). It passes today only because capture.test.ts's last cooldown-firing test uses a different sid and each subsequent beforeEach clears the map. One reordered/added test in capture.test.ts turns injection-framing red with a mystifying symptom. One line in its beforeEach removes the order dependency.
  2. The reset comment — "a fresh test is a fresh process" — is factually wrong (module state demonstrably survives file boundaries in a bun test run; the reset is needed precisely because of that). Fix the sentence so the next person doesn't reason off it.
  3. Style: the rewritten Auto-capture bullet uses - while every sibling bullet uses — (em-dash).

CI note (not your fault, but relevant): test-windows failed twice on this branch — attempt 1 on the import-concurrency race (5016 ms timeout), my rerun on supersede racing appends > a concurrent reader never sees a half-written file, a different member of the known Windows torn-read family (#110's own baseline names it). #114's identical-family failure passed a clean rerun. Treat as the documented suite flake, not this PR — but it reinforces that the deterministic pins #110 is trying to build are the real fix.

…n-sharing file, honest reset comment, README on main's dash
@WhollyMindless

Copy link
Copy Markdown
Contributor Author

All three nits taken, in acd265c:

  1. injection-framing.test.ts now calls resetCaptureCooldowns() in its beforeEach — with a comment naming the ses-cap overlap as the reason, so nobody "optimizes" the line away.
  2. The wrong sentence is gone from both files: the comment now states the truth you demonstrated — module state survives file boundaries in a bun test run, which is precisely why the explicit clear is needed.
  3. The README bullet is back on main's exact text with only the two facts re-added (cooldown bound, bench cite), so the dash is main's em-dash by construction, not by find-and-replace. This also makes any future main-side README edit conflict-free with this branch.

Re-verified after the changes: the three files that share the cooldown map (capture, capture-compaction, injection-framing) run 55/55 in one process, tsc clean.

@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.

Re-verified at acd265c (macOS, Bun 1.4.3) — all three nits confirmed fixed, APPROVED.

  1. Nit 1 (cooldown reset seam): injection-framing.test.ts:39-42 now calls resetCaptureCooldowns() in beforeEach, and the comment names the ses-cap overlap explicitly — the order-luck dependency is gone by construction, not just papered over.
  2. Nit 2 (honest comment): the false 'a fresh test is a fresh process' sentence is replaced in both capture.test.ts:41 and capture-compaction.test.ts:48 with the true statement (module state survives file boundaries, hence the explicit clear).
  3. Nit 3 (README): diffed the branch's bullet against origin/main — line 36 differs from main's text only by the two added facts ('per trigger', the cooldown bound + bench cite); the em-dash is main's. The table row adds the separate-cooldown fact. Claim verified mechanically, not by eye.

Re-runs after the changes: the three cooldown-sharing files in one process → 55 pass / 0 fail; full suite → 490 pass / 8 skip / 0 fail; bun run typecheck clean.

@abogartz
abogartz merged commit 3589658 into abogartz:main Oct 26, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.3 🎉

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.

capture: captureOnCompact is undocumented, and its cooldown state is per-setup

2 participants