You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Question: Should auto-capture apply to single prompt cli requests? #571
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.
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.
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.
Timing has been removed. It tried to implement with a timer and token limit despite opencode not doing any of that. But the auto-capture on -p invocation feels heavy. But it's your call and we can try to tune later.
________________________________
From: abog ***@***.***>
Sent: Saturday, November 28, 2026 11:06 AM
To: abogartz/opencode-self-improvement
Cc: WhollyMindless; Author
Subject: Re: [abogartz/opencode-self-improvement] Question: Should auto-capture apply to single prompt cli requests? (Issue #571)
abogartz left a comment (abogartz/opencode-self-improvement#571)
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.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS and Android. Download it today!
You are receiving this because you authored the thread.Message ID: ***@***.***>
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
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.
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.
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.
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.
Activity
abogartz commentedon Nov 28, 2026
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.
abogartz commentedon Nov 28, 2026
Quick google:
Yes. Even in print mode (
pi -p), the full extension lifecycle fires. The two most relevant hooks are:agent_endsession_shutdownBoth fire in
-pmode. You register them in a TypeScript extension: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_settlealso fires (before the run settles). Extensions likepi-subagentsuse it in print mode to wait for background agents/workflows before the process exits.Stophooks (via the@hsingjui/pi-hooksor@fyeeme/pi-hookspackages) map tosession_shutdownand 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.session_shutdownhandlers return, pi callsprocess.exit, so nothing after anawaitin your handler is guaranteed to run.WhollyMindless commentedon Nov 28, 2026
abogartz commentedon Nov 28, 2026
What do you mean by heavy? Have we quantified the effect?
ocsi-architect commentedon Nov 28, 2026
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).session_shutdownin-p?reason=quitagent_end's 1500 ms sleep honored on every passreason=quitExtension error (…/observer.ts): handler timed out after 2000ms; neither registered handler reached its own completionThe source agrees: print mode's
finallyawaitsruntimeHost.dispose(), which awaitsemitSessionShutdownEvent→extensionRunner.emit(), andemit()doesawait handler(event, ctx)with noPromise.raceand no timer in that path.Every number in our path is ours, and all of them are smaller than the grant
src/capture/runtime.ts:268—const DRAIN_LIMIT_MS = 1000, raced indrainInFlight.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/0005phase split — never abandon a pass that already holds its model answer, always abandon one parked ingenerate.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 onopencode 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_costwith 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
-pcapture work as-is, because the host awaits the handler.pi -pforever, 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.capture_cost-with-no-capture_laggap heuristic can go, since nothing is abandoned anymore.Two things that block the fix today
close()(src/adapters/oh-my-pi/capture.ts:32-36) resolves pending snapshots'donepromises without aborting thegenerateTextsignal, 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.integration/oh-my-pi/capture-shutdown-runner.tsasserts shutdown finishes under 1900 ms and asserts theunansweredscenario 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.