Skip to content

auto-capture loses its pass to host teardown: 3 of 12 real CLI passes reach the write gate #404

Closed

Description

@abogartz

Summary

A real opencode run turn does fire an auto-capture pass. The pass then loses its own result: opencode run disposes the plugin the moment the turn ends, captureCleanup aborts, and the pass stops between the model answer and the write gate.

Measured on 12 identical runs on 2026-11-13, macOS, host pinned by package.json, scripted instant model:

passes started (ev=capture_cost) 12 / 12
passes that reached the gate and recorded (ev=capture_lag) 3 / 12

The model answers instantly in these runs. A real model takes seconds, so on a normal CLI session the host is gone before the answer arrives. This is the most likely explanation for the README's standing note that auto-capture has never been observed firing on a live store: the trigger works, the write does not survive.

Mechanism

src/capture/runtime.ts captureCleanup:

captureCleanup = async () => {
  controller.abort();
  await drainInFlight();
};

and inside a pass, after the model call and between candidates, if (controller.signal.aborted) return;.

That is the #211 contract, stated at src/capture/runtime.ts:232-247, and its purpose is sound: a pass must not run the write gate against a store its caller has already torn down, and a gate already mutating is never cut in half. The drain is 1000 ms and the comment is explicit that "teardown never waits on the model".

What the contract does not distinguish is the case where nothing is being torn down. On opencode run the store is not removed and no caller waits for cleanup — the process simply exits. The abort therefore fires on the healthy path, and it fires in the one window where capture is allowed to run, because capture is triggered by the turn-end event.

The evidence trail shows the cut point exactly. A lost run ends here:

plugin_load, epoch_recompute, tool_result, capture_cost

A won run continues:

..., capture_cost, gate_start, graph_current, ref_stale_scan, dedup_clean,
write_promoted, auto_capture_promoted, auto_capture_scan, capture_lag

Why no existing test saw this

Every capture test constructs the event envelope and the session.context by hand and keeps the process alive, so the abort never fires during the pass. The fabricated envelope is precisely what let this feature pass its tests while dead (#402 records the same failure mode from the other direction). e2e/ drove the real host for loading, the store, the ledger, compaction and the viewer, but never capture — and could not: the fake provider only served scenario steps to requests carrying a tools array, and the capture call is a plain generation with none, so the scripted capture reply was unreachable. Both gaps are closed by the e2e/capture.test.ts added alongside this issue (capture lane in e2e/support/fake-model.ts).

Reproduction

e2e/capture.test.ts asserts the parts that hold under the race and prints [capture-race] when the pass is cut; running it repeatedly shows the rate. To measure it directly, run the scenario in a loop and count ev=capture_cost against ev=capture_lag.

Candidate fixes, with the risk each carries

  1. Let a pass that already holds its model answer finish, bounded by the drain. Distinguish "no new passes" (abort the subscription, unchanged) from "stop writing" (do not abort a pass past the model call). The drain still protects the store-removal case, because it waits for the pass. Risk: it weakens Capture lifecycle: define and stop work that continues after plugin cleanup #211 exactly where Capture lifecycle: define and stop work that continues after plugin cleanup #211 was written to be strong, so it needs the disposal test to be re-derived rather than kept.
  2. Give the host a reason to stay. Out of this repo's control.
  3. Accept it and say so. If capture cannot survive CLI teardown, the switch should report that rather than burn a model call per session and store nothing. capture_cost already records the call; a outcome=aborted line would make the loss visible in the trail instead of silent.

1 is the fix; 3 is the minimum honesty step if 1 is judged too risky. Either way the trail should distinguish "the pass found nothing" from "the pass was cut", which today it cannot: a cut pass records nothing at all after capture_cost.

Notes

Activity

  1. abogartz commented on Nov 13, 2026

    @abogartz
    OwnerAuthor

    Fix landed on the branch carrying #403 (e12c8a3, PR #403), per the maintainer's instruction to keep the fix with the change that surfaced it rather than open a second PR.\n\nTwo-phase teardown: captureCleanup abandons only a pass still parked in generate.text and drains one that already holds its answer. All of #211's disposal assertions pass unchanged. Option 3 (an outcome=aborted line) was implemented and then removed: it breaks #211's tested "nothing at all after cleanup" contract in all four of its cases, because appendLog calls ensureDir and the receipt resurrects a directory the caller deleted. The gap in the existing lines carries the same information — capture_cost with no later capture_lag for that session is a pass that was cut.\n\nMeasured A/B/A, 12 runs each: 12/12, 6/12 reverted, 12/12. The scenario in e2e/capture.test.ts now asserts the row unconditionally. Leaving this issue open until #403 merges.

  2. added
    bug
    capture
    severity:high
    on Nov 14, 2026
  3. abogartz commented on Nov 21, 2026

    @abogartz
    OwnerAuthor

    Verified fixed on main — closing.

    Two-phase teardown landed in e12c8a3 (fix(capture): stop teardown abandoning a capture pass that already answered), merged with PR #403 on 2026-11-13. captureCleanup in src/capture/runtime.ts now aborts only NEW passes and calls drainInFlight(), which abandons a pass still parked in generate.text and drains one that already holds an answer.

    The 3-of-12 figure no longer holds: README now reports that a capture pass starts in 12 of 12 opencode run runs (e2e/capture.test.ts), and e2e/capture.test.ts asserts outcome=candidates on the last capture_lag line.

    Checked against origin/main @ c48aea6 (v0.33.7). The separate open question — no live store has ever been observed completing a pass — is tracked by the README caveat and #386, not this ticket.

  4. added
    verified-on-main
    on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcaptureseverity:highverified-on-main

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions