Skip to content

Stop a disposed plugin from running the memory write gate - #367

Merged
abogartz merged 3 commits into
mainfrom
211-dispose-drain
Nov 9, 2026
Merged

abogartz merged 3 commits into
mainfrom
211-dispose-drain

Conversation

@abogartz

@abogartz abogartz commented Nov 9, 2026

Copy link
Copy Markdown
Owner

What this fixes

Auto-capture asks a model to read the conversation and propose memories, and that call takes tens of seconds. If the plugin is reloaded or shut down while the call is in flight, the call used to finish anyway and then run the entire memory write pipeline — against a store the host had already torn down. The review measured it: gate_start, ref_stale_scan, write_blocked, auto_capture_compaction_blocked and capture_lag, all stamped after disposal. A candidate that passed the gate would have written a row there, and a GATE-2 supersession would have retired one.

What the change does

A shutdown contract, written down where the code runs:

  1. Shutdown stops the event subscription, so no new capture starts.
  2. A capture that is already running asks "am I disposed?" as soon as the model call returns, and again before each candidate's write. If it is disposed, it stops before touching the store. A write already in progress is never cut in half: a half-applied write is worse than a late one.
  3. Shutdown then waits up to one second for the captures it is tracking to settle, so a host or a test that removes the store immediately afterwards sees nothing straggling. The wait is bounded on purpose — shutdown never blocks on a slow model, because step 2 already guarantees the straggler writes nothing.

One side effect: this gap was also the source of the hook failed (ignored): ENOENT .../evidence/<date>.logfmt line in the test logs. A shutdown is not a capture failure, so the failure receipt is skipped once disposed.

Tests

  • New: park the capture model call, dispose, then release the park. No row lands, and no evidence line carries a timestamp after the disposal instant. Without the fix this fails 2 of 3 — the compaction pass writes gate receipts and a row, the idle pass writes a failure receipt.
  • Control: the same pass with no disposal still promotes, so it is the new check that stops the write, not a harness artifact.
  • bunx tsc --noEmit clean; bun run lint identical to main's baseline; bun test 1141 pass / 19 skip / 0 fail.

Closes #211.

generate.text does not take the abort signal, so a capture parked mid-model-call
woke up after cleanup() and ran the whole gate against a store the caller had
already torn down: gate receipts, promoted rows, supersession, all stamped after
disposal. Same gap produced the ENOENT evidence noise in capture-crop teardown.

Contract, bounded and explicit: cleanup aborts the subscription so no new pass
starts; every pass re-checks the signal after the model call and between
candidates and stops before the next gate run (a gate already mutating is never
cut in half); cleanup then drains the tracked passes up to 1s, so a caller that
removes the store right after cleanup sees nothing straggling. Teardown never
waits on the model — a generate that outlives the bound is covered by the check,
not by the drain.

Regression test parks generate, disposes, releases, and asserts no row and no
evidence line after the disposal instant (RED 2/3 without the fix). The idle
path's failure receipt is skipped for the same reason: teardown is not a capture
failure.

@WhollyMindless WhollyMindless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Independent review of the lifecycle fix: the mid-model cancellation and its positive control pass. One earlier await still permits activity after cleanup, reproduced below.

Comment thread src/capture/runtime.ts
// caller may already have removed. Silent on purpose: a receipt here
// is the ENOENT teardown noise this check exists to eliminate, and
// the drain in cleanup means the pass is finished, not lost mid-air.
if (controller.signal.aborted) return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Check disposal after context reads, before skip receipts or generation

The first abort check is after generate.text, but captureSession also awaits contextFn earlier. I reproduced this at 49474b0 with the real plugin harness: park session.context, await cleanup() (the one-second drain expires), then release the context. Returning [] appends auto_capture_skipped after cleanup; returning the normal thick transcript starts a new generate.text call after cleanup. A control releasing the same context before disposal starts capture normally, and the three checked-in disposal tests pass.

Please check disposal after the context await and before starting generation (including the awaited known-rows read). This closes the pre-model path as well as the mid-model path: once cleanup returns, the host may have removed the store, and this path can still append evidence or spend a model call. Hermetic temporary-store probe only; no live-host occurrence claimed.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Reproduced and fixed in 9623129.

The rule the fix follows: every await that can outlive teardown is followed by the check, not just the model call. Two were missing — session.context and the known-rows read — and the second is now hoisted out of the prompt literal so the check runs before the call is made rather than after its arguments are built.

test/harness.ts gains a park seam on session.context, mirroring generate.park, so the interleave is deterministic (no sleep race).

New pin, pass disposed during the context read neither receipts nor generates (#211): park the context read, run cleanup() (the drain expires at its bound), release. RED before the fix — the model call started. GREEN now: generate.calls.length === 0 and no evidence line at or after the disposal instant, the same assertion style as the two existing disposal tests.

bun run gate green: 1112 unit + 30 terminal/BDD tests against a real host, 0 fail. bunx tsc --noEmit clean; lint at main's baseline (536 warnings, 147 infos).

…own (#367 review)

The first abort check sat after generate.text, but captureSession awaits more
before it gets there. Parking session.context, running cleanup() (whose drain
expires at its bound rather than hanging on a call that will never land) and
then releasing the context let the pass wake up post-disposal and append
auto_capture_skipped - or, with a thick transcript, start a model call against a
store the caller had already removed.

Checks now follow the context read and the known-rows read as well as the model
call and the candidate loop, so both the pre-model path and the mid-model path
stop at the same line. The known-rows read is hoisted out of the prompt literal
so the check runs before the call is made, not after its arguments are built.

test/harness.ts gains a park seam on session.context, mirroring generate.park.
@abogartz

abogartz commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

Review addressed in 9623129 — the abort check now follows the context read and the known-rows read as well as the model call, so the pre-model path closes with the mid-model one. New regression pins it through the real harness (RED before the fix). Gate green: 1112 unit + 30 e2e, tsc clean, lint unchanged.

@WhollyMindless

Copy link
Copy Markdown
Contributor

I checked the PR after the fix reply, but GitHub still reports head 49474b0, so 9623129 is not on the reviewable PR head yet. The post-context and pre-generation abort checks are still absent there. I will recheck the fix once that commit is pushed to this PR.

@WhollyMindless WhollyMindless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

9623129 is now on the PR branch, and I independently rechecked it. Both original parked-context probes pass: releasing thin context after cleanup appends no receipt, and releasing thick context starts no model call. Releasing before cleanup still captures normally. The new code also checks disposal after the known-rows await before generation. 20 focused disposal/compaction/probe tests pass and typecheck passes. My reported lifecycle finding is addressed; these are hermetic fake-host checks, not a claim of live-host validation.

@WhollyMindless WhollyMindless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 9623129; the previously reported parked-context failure is fixed. Astra found one different exception path, which I independently reproduced.

[P2] Suppress the compaction failure reporter after disposal.
At runtime.ts:556, compaction runs capture through generic guarded(). A rejection from the awaited model call bypasses the subsequent abort check and reaches that reporter.

Real-hook reproduction:

  1. Enable auto-capture and park a compaction-triggered generate.text call.
  2. Await cleanup() through its one-second drain deadline.
  3. Release the parked call with a synthetic rejection.
  4. Read evidence appended after cleanup returned.

Observed: one hook_error line is appended after cleanup. The identical idle-trigger control appends nothing because its catch already checks disposal.

This is an evidence-write failure path, not an observed memory-row mutation. It still leaves work touching a store the caller may remove or replace after cleanup, contrary to #211's lifecycle contract. Please consider checking disposal before the compaction exception reaches the generic reporter, while retaining the ordinary non-disposed error receipt.

Hermetic temporary store, fake generation, no live-host incident or model call. The earlier successful-call disposal fix remains intact.

#211 review)

The abort checks inside captureSession stop a disposed pass from starting the
next thing. They cannot stop a rejection that is already in flight: the model
call throws PAST every check that follows it, and on the compaction trigger the
error reaches the generic guarded() reporter, which appends a hook_error line to
a store cleanup() has already released. The idle trigger already checks the
signal in its own catch, which is why the same reproduction there writes nothing.

guarded() gains an optional reportable predicate for exactly this. The console
line — the primary trace under #138 — stays in both cases; only the store write
is gated, and only when the caller says it is disposed. A predicate that throws
reports, so a broken predicate cannot turn a swallowed failure into a throw.

Pinned RED-first, with the receipt's own control: a compaction pass whose parked
model call rejects after cleanup appends nothing at or after the disposal
instant, while the same rejection with a live plugin still records one
hook_error line naming the compaction hook.
@abogartz

abogartz commented Nov 9, 2026

Copy link
Copy Markdown
Owner Author

Round 3 (64b6d37): reproduced and fixed, RED-first.

Confirmed. The abort checks inside captureSession stop a disposed pass from starting the NEXT thing. They cannot stop a rejection that is already in flight: the awaited model call throws past every check that follows it, and on the compaction trigger the error lands in the generic guarded() reporter, which appends hook_error to a store cleanup() has already released. The idle trigger's own catch already checks the signal, which is exactly why the same reproduction there writes nothing — your control was right.

Fix. guarded() gains an optional reportable?: () => boolean and the compaction pass passes ()\ => !controller.signal.aborted. The console line — the primary trace under #138 — stays in both cases; only the store write is gated, and only when the caller says it is disposed. A predicate that throws reports, so a broken predicate can never turn a swallowed failure into a throw. The other guarded() call sites keep the #138 contract untouched, which is why the fix is a parameter rather than a change to the default.

Pins. New test replays your four steps through the real harness: park the compaction-triggered generate.text, await cleanup() past its drain bound, release with a synthetic rejection, then assert no evidence line at or after the disposal instant. It failed before the fix and passes now. The receipt's own control is checked in beside it: the same rejection with a live plugin still records exactly one hook_error naming the compaction hook.

Gates. typecheck clean; lint 536 warnings / 147 infos (main's baseline); 1114 unit tests; e2e 30/30 on a clean serial run.

@WhollyMindless WhollyMindless left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified 64b6d37; this supersedes my changes request for the post-disposal compaction exception receipt. I reported the finding; the correction is maintainer-authored.

Independently replayed the parked generation rejection after cleanup for both compaction and idle: neither appends evidence after cleanup. The new live-compaction rejection control still produces its hook_error receipt, and existing generic guarded-failure tests retain the default reporting contract. The earlier parked-context and successful-generation disposal regressions also pass.

73 focused capture/compaction/disposal tests, including two adapted original probes, pass; typecheck passes on Windows/Bun 1.4.4. No additional actionable finding in this revision. No live-host or full-suite certification.

@abogartz
abogartz merged commit 295dc06 into main Nov 9, 2026
4 checks passed
abogartz-opencode-self-improvement Bot pushed a commit that referenced this pull request Nov 9, 2026
# [0.21.0](v0.20.0...v0.21.0) (2026-11-09)

### Bug Fixes

* **capture:** a compaction rejection after teardown writes no evidence ([#211](#211) review) ([64b6d37](64b6d37)), closes [#138](#138)
* **capture:** check disposal after every await that can outlive teardown ([#367](#367) review) ([9623129](9623129))
* **capture:** lagSince reads time.created/time.completed — live capture_lag lines carried no lag= ([895237f](895237f))
* **capture:** no user turn also withholds a model-supplied guided quote ([#364](#364) review) ([2c1e07f](2c1e07f))
* **capture:** scan the quote, and quote one fact per row ([#364](#364) review) ([d0e1ee6](d0e1ee6)), closes [#205](#205)
* **capture:** stop a disposed plugin from running the write gate ([#211](#211)) ([49474b0](49474b0))
* **capture:** user provenance comes from structured turns, not labels ([#364](#364) review) ([0fdd8a6](0fdd8a6))
* **retrieval:** guided promotion recomputes tracked epochs (fix A, FINDINGS 2026-11-08 §4b) ([9ff014f](9ff014f))
* **store:** retire every copy; never file a ts outside its own day ([#373](#373) review) ([e67b12b](e67b12b))

### Features

* **capture:** deterministic guided-candidate pre-pass (captureGuidedRules, default off) ([de5ba0e](de5ba0e)), closes [#345](#345)
* **retrieval:** verbatim user quote on guided rows + stage telemetry for hint-miss attribution ([0af9fc4](0af9fc4))

### Performance Improvements

* **store:** a single-row mutation locks the day file its ts names ([#215](#215)) ([1a7e47b](1a7e47b))
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.21.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.

Capture lifecycle: define and stop work that continues after plugin cleanup

2 participants