Skip to content

Question: Should auto-capture apply to single prompt cli requests? #571

Backlog

Description

@WhollyMindless

In testing our pi-adapter, the tester found this:

Auto-capture never finishes in pi -p mode. In 2 of 2 runs the capture started, then Pi exited before the model replied, and nothing was saved. The identical prompt in a long-running Pi session saved a memory about 3.4 s later. This fits the PR's stated limit (only 500 ms is allowed at shutdown), but README.pi.md doesn't mention -p mode. I couldn't tell whether the abandoned model call still reaches, and gets billed by, the provider.

I don't think that -p (prompt) mode should bother with auto-capture. Unless you know something I don't.

I suspect this applies to any harness with a similar option.

Comments?

Activity

  1. abogartz commented on Nov 28, 2026

    @abogartz
    Owner

    Maybe the problem is in using timings instead of hooks. Does PI expose any events we can listen for on shutdown? I'm thinking of how you can capture sigterm events in other tools like ExpressJS.

  2. abogartz commented on Nov 28, 2026

    @abogartz
    Owner

    Quick google:

    Yes. Even in print mode (pi -p), the full extension lifecycle fires. The two most relevant hooks are:

    Event When Use
    agent_end After the agent finishes its final turn Completion logic (log, notify, post-process)
    session_shutdown Before the session runtime is torn down Cleanup (close sockets, flush state, kill child processes)

    Both fire in -p mode. You register them in a TypeScript extension:

    pi.on("agent_end", async (event, ctx) => {
      // agent finished its turn(s)
    });
    
    pi.on("session_shutdown", async (event, ctx) => {
      // event.reason — "quit" | "reload" | "new" | "resume" | "fork"
      // cleanup here
    });

    A few caveats specific to print mode:

    • ctx.shutdown() is a no-op in print mode — the process exits automatically once all prompts are processed, so you can't request a deferred shutdown.
    • agent_before_settle also fires (before the run settles). Extensions like pi-subagents use it in print mode to wait for background agents/workflows before the process exits.
    • Stop hooks (via the @hsingjui/pi-hooks or @fyeeme/pi-hooks packages) map to session_shutdown and are awaited, so a slow hook will delay exit up to its timeout (default 60 s). decision: "block" is not honored in this context — pi cannot prevent exit.
    • Once session_shutdown handlers return, pi calls process.exit, so nothing after an await in your handler is guaranteed to run.
  3. WhollyMindless commented on Nov 28, 2026

    @WhollyMindless
    ContributorAuthor
  4. abogartz commented on Nov 28, 2026

    @abogartz
    Owner

    What do you mean by heavy? Have we quantified the effect?

  5. ocsi-architect commented on Nov 28, 2026

    @ocsi-architect

    Design call, and it settles the question: there should be no teardown constant at all. Completion becomes an event the host waits on, not a race against a number we picked. Every timer currently in this path is a workaround, and the measurements below say the workaround is costing captures rather than protecting anything.

    Measured on real hosts, both now installed on this mac: Pi 1.7.0 (@earendil-works/pi-coding-agent) and oh-my-pi 19.2.1 (@oh-my-pi/pi-coding-agent).

    Host session_shutdown in -p? Handler budget the host enforces Evidence
    Pi 1.7.0 yes, reason=quit none — handler awaited to completion observer handler sleeping 3000 ms honored exactly: ENTER 20239 ms, EXIT 23240 ms; agent_end's 1500 ms sleep honored on every pass
    Pi 0.74.2 (previous) yes, reason=quit none same probe: 3000 ms honored, process wall 5 s
    oh-my-pi 19.2.1 yes 2000 ms per handler host emitted verbatim: Extension error (…/observer.ts): handler timed out after 2000ms; neither registered handler reached its own completion

    The source agrees: print mode's finally awaits runtimeHost.dispose(), which awaits emitSessionShutdownEvent → extensionRunner.emit(), and emit() does await handler(event, ctx) with no Promise.race and no timer in that path.

    Every number in our path is ours, and all of them are smaller than the grant

    • Core: src/capture/runtime.ts:268 — const DRAIN_LIMIT_MS = 1000, raced in drainInFlight.
    • oh-my-pi adapter: src/adapters/oh-my-pi/extension.ts:103 — if (!config.capture) await capture.settle(500), commented "Leave room for the shared write drain within the host's two-second cap."

    On oh-my-pi that spends 500 ms of a 2000 ms grant. On Pi it spends 500 ms of an unbounded grant. Neither is derived from anything, and the comment asserts a host limit that is true for one harness and false for the other. Two constants in two layers, unverified, re-invented per adapter.

    Why the event form is strictly simpler, not more complex

    The docs/adr/0005 phase split — never abandon a pass that already holds its model answer, always abandon one parked in generate.text — exists because of the bound. Its stated reason for not waiting is "the caller may remove the store the moment cleanup resolves", and the same comment block concedes that on opencode run "nothing is being removed at all — the process simply exits". Remove the bound and both halves of that machinery lose their purpose: cleanup does not return until the pass is done, so there is no store to remove underneath it and no parked/past-model distinction to maintain.

    The repo's own numbers show what the bound costs: 9 of 12 in one sample and 6 of 12 in a controlled A/B recorded capture_cost with no row. The tester's 3.4 s to save in a long session is the same event. We are abandoning work the host was willing to wait for.

    What replaces the timers

    1. Turn completion and teardown are events; cleanup awaits the pass. No sleep, no race, no constant. On Pi this makes -p capture work as-is, because the host awaits the handler.
    2. The only bound lives on the request, not on teardown. A hung provider must not be able to hang pi -p forever, so the model call owns its own timeout and abort, and the completion event fires on answer, error, or that timeout. Every wait then has an owner, and the fake host in the conformance suite that never answers is what proves it. This is not a teardown constant and should not be reintroduced as one.
    3. A host's own cap becomes declared data, not our timer. oh-my-pi declares 2000 ms per handler; Pi declares none. Declarations get verified at install time, so a false grant fails there rather than losing captures silently later.
    4. Where the grant cannot fit the work, don't start the work. oh-my-pi's 2 s is shorter than a 3.4 s pass, so on that harness a capture started at teardown cannot succeed by construction — that is now a fact about the host rather than a preference, and it is the honest basis for deciding whether auto-capture runs there at all. Reporting a non-save beats a silent one either way, and the capture_cost-with-no-capture_lag gap heuristic can go, since nothing is abandoned anymore.

    Two things that block the fix today

    • Abandoning is not cancelling. The adapter's close() (src/adapters/oh-my-pi/capture.ts:32-36) resolves pending snapshots' done promises without aborting the generateText signal, so the request stays in flight with nobody awaiting it. Whether a provider bills a stream killed mid-flight I did not measure; that we send it and never cancel it is in the code.
    • The suite currently fails any real fix. integration/oh-my-pi/capture-shutdown-runner.ts asserts shutdown finishes under 1900 ms and asserts the unanswered scenario saves nothing. Any change that actually waits gets reverted by that green test, so the assertion has to change with the behavior.

    Not measured

    Pi's handler behavior in a fully successful multi-turn run under load; whether providers bill aborted streams; and OpenCode's own teardown grant, which has never been measured and is not cited as the basis for the 1000 ms constant. No number is claimed for those three.

    Filing the ticket with the shape above.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions