Skip to content

Await capture completion as an event: remove every teardown timer, bound the request instead #578

Done

Description

@ocsi-architect

Problem

Capture completion at teardown is raced against hardcoded timers instead of awaited as an event, and the timers are shorter than what the hosts grant. Passes are abandoned while the host is still waiting for us.

Current bounds, all ours:

  • src/capture/runtime.ts — DRAIN_LIMIT_MS = 1000, raced in drainInFlight().
  • src/adapters/oh-my-pi/extension.ts — if (!config.capture) await capture.settle(500), with settle() implemented as Promise.race against a timer.

Measured ceilings on the installed hosts:

Host session_shutdown in print mode Handler budget the host enforces
Pi 1.7.0 yes (reason=quit) none — an observer handler sleeping 3000 ms was awaited to completion
Pi 0.74.2 yes (reason=quit) none
oh-my-pi 19.2.1 yes 2000 ms per handler, host-reported: handler timed out after 2000ms

A pass that mines a transcript measured 3.4 s to save in a long-running session. The cost of the current bounds is already recorded in this repository: passes that logged capture_cost with no row, 9 of 12 in one sample and 6 of 12 in a controlled A/B.

Required shape

  1. Cleanup awaits the capture pass. No Promise.race against a timer on the teardown path, in core or in an adapter.
  2. The model request owns its own timeout and abort signal, so the completion event fires on answer, error, or request timeout. This bound is a property of the request and must not be expressed as a teardown duration.
  3. Abandoning a pass must cancel it. Resolving a bookkeeping promise while the request continues is not cancellation.
  4. A host that enforces a handler budget declares it; the core verifies the declaration at install time and uses it to decide whether to start a pass at all. Deriving the value at runtime from a hardcoded per-harness number is not acceptable.
  5. A pass that does not complete reports a reason. The inference-from-missing-telemetry path can be retired once nothing is abandoned.

Acceptance criteria

  • Both teardown bounds are gone; a mutation that reintroduces a numeric bound on the cleanup path fails a test that names it.
  • With a provider that answers slower than any previous bound, a print-mode run on a host that awaits handlers saves the row. Prove it on a real host, not only a fake.
  • With a provider that never answers, the run terminates because the request's own timeout fires; the conformance fake includes that host.
  • An abandoned or cancelled pass aborts its in-flight request; a test observes the abort.
  • A declared handler budget that the host does not honor fails at install time, alongside the verification required for other declarations.
  • On a host whose declared grant is shorter than the work, the run states that capture did not run and why, rather than starting work that cannot land.

Existing assertions that must change

integration/oh-my-pi/capture-shutdown-runner.ts asserts teardown finishes under 1900 ms and that the unanswered scenario saves nothing. Those assertions pin the current abandonment, so any correct implementation fails them; the expectations must be rewritten with the behavior, and the rewrite has to stay falsifiable.

Out of scope

Provider billing behavior for aborted streams, and any per-harness timing constant that merely replaces the existing ones.

Activity

  1. added
    capture
    severity:high
    on Nov 28, 2026
  2. ocsi-architect commented on Nov 28, 2026

    @ocsi-architect
    Author

    Sequencing: this ticket is not startable yet, and that is deliberate.

    It rewrites teardown in core, while the core/adapter seam is still moving through the shared-interface work. Doing it before that seam lands means writing the change twice — once against code that is being relocated, once against its new home — and the second write is where mistakes hide.

    Start after the adapter isolation lands. At that point the work has a stable shape:

    1. the drain bounds in core and in the adapter are both gone, replaced by awaiting the pass;
    2. the model request owns its timeout and abort, which is what keeps a hung provider from hanging a print-mode run once no teardown bound remains;
    3. the host's own handler budget is declared data, verified at install time, and is what decides whether a pass is started on that host at all;
    4. the assertion that currently pins abandonment — teardown finishing under a bound, and the slow-provider scenario expecting no saved row — is rewritten with the behavior rather than around it.

    Host budgets measured on real installs, so nobody has to re-derive them: one host awaits extension handlers to completion in print mode with no timeout; the other enforces 2000 ms per handler. A pass that mines a transcript measured 3.4 s, which is why a per-harness constant is the wrong instrument here.

  3. added
    verified-on-main
    on Nov 28, 2026
  4. ocsi-architect commented on Nov 29, 2026

    @ocsi-architect
    Author

    Satisfied by the shared capture fix on the open host-contract pull request, and I verified the structural claims rather than reading the commit message: the drain timer is absent from src at that head, completion is joined as an event, the request owns its deadline and abort signal with typed reasons, and the host handler deadline is read from the installed host and probe-verified against its running dispatcher instead of being hardcoded. One item still open in reporting, tracked separately under #603 (capture stop reasons that never reach the human report). Closing this ticket is reasonable at merge time; the core teardown timers are gone.

  5. ocsi-architect commented on Nov 29, 2026

    @ocsi-architect
    Author

    Closed — shipped in 0.40.0 and verified on main at db2c6f3, not from the diff.

    • DRAIN_LIMIT_MS: zero occurrences anywhere under src/.
    • settle() in src/adapters/oh-my-pi/capture.ts takes no argument, so the adapter joins completion instead of racing a timer, and the core drain awaits the in-flight set.
    • Both remaining Promise.race calls in src/ are the shape the design keeps, named here because the second one will otherwise read like a violation:
      • src/capture/request.ts:61 races the provider response against the request's own abort signal. That is the single bound, owned by the model request.
      • src/adapters/oh-my-pi/lifetime.ts:36 is the startup capability probe, not a cleanup path. Its watchdog is derived from the grant the host declares — max(10_000, declared * 2) — and its failure mode is a loud installation error rather than a silent drop.
    • The grant is declared and then verified: the probe fails when the host dispatcher does not honour its own exported budget.

    test/capture/request-lifecycle.test.ts on main: 5 pass, 0 fail, including "a request deadline starts at request creation, independently of teardown".

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

    captureseverity:highverified-on-main

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions