Repository navigation
opencode: deliver redirects on failed tools and clean up ended session state - #266
Open
Çağdaş Yürekli (cagdasyurekli) wants to merge 1 commit into
Conversation
Have-Purple
reviewed
Nov 18, 2026
Have-Purple
left a comment
Contributor
There was a problem hiding this comment.
Ran the two new offline tests in tests/test_plugin_package.py: both pass (Node 26, --experimental-strip-types).
Mutation checks in a scratch worktree:
- delivering only when
okmakestest_opencode_v2_delivers_redirect_on_failed_tool_and_holds_when_immutablered; - removing
cleanup(id)fromend()makestest_opencode_v2_exports_session_before_cleaning_state_on_endred.
So the tests pin the behaviour, not only the new code paths.
The change is correct for what it fixes: execute.after now carries redirects on failed calls; pendingNudges is cleared at prompt and turn close, so an undeliverable redirect cannot spill into the next turn; cleanup() covers records, steps, names, manuals, prompts, turns, unsettled and pending nudges, and the v2 exit listener is removed on teardown.
Minor, non-blocking:
deliverNudgestill falls through toresult.content = nudgewhencontentis a non-string, non-array object, replacing it. Pre-existing, not a regression, but an explicitelse if (result.content === undefined)would be safer.- v1's
process.once("exit")listener is still registered per setup and never removed; only v2's teardown is fixed. The PR description scopes it to v2, so noting rather than blocking. - The cleanup test checks the exported file and the manual disappearing, not the
steps/namesmap sizes; the memory claim is pinned indirectly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two issues in
plugin/opencode/tokeneconomizer.ts, particularly under opencode 2.x's long-lived background service:setupV2,ctx.tool.hook("execute.after")only appliedout.hookSpecificOutput.additionalContextwhene.status === "completed"andresultwas truthy. When a tool fails withstatus !== "completed"(which is when loop-guard redirects fire most often), the paid judge/redirect call ran and returned a redirect, then discarded it.setupV2(which stays resident across sessions in the 2.x daemon),records,steps,names,manuals,prompts,turns, andunsettledwere never cleaned up onsession.deleted/end(), andnameswas not deleted aftersession.tool.calledconsumed it. In v1,manuals,prompts, andturnsalso remained populated afterend().Fix
deliverNudge+pendingNudges): InsetupV2, redirects on completed calls continue appending toresult.content(withresult.outputfallback whencontentis undefined). On failed calls,deliverNudgeappends toe.error.message(ore.errorwhen it is a string). If an error object is frozen/missing so the redirect cannot be attached in place, it is held inpendingNudgesfor the remainder of that turn and attached to the next tool result, and cleared on turn close / new prompt / session end so redirects never bleed across turns.cleanup): Both v1 and v2 export the session JSON before removing per-session state. In v2,stepsandnamesstoresessionIDalongside each entry socleanup(id)removes every message, tool-call name, manual, prompt counter, turn, record list, and pending nudge for ended sessions;session.tool.calledalso deletes consumed entries fromnames; andsetupV2's teardown removes itsprocess.exitlistener.Verification
tests/test_plugin_package.pydrivingplugin/opencode/tokeneconomizer.tsvianode --experimental-strip-typeswith a mockte-hook/te_bootstrap.pydispatcher:test_opencode_v2_delivers_redirect_on_failed_tool_and_holds_when_immutabletest_opencode_v2_exports_session_before_cleaning_state_on_endpython -m pytest testslocally: 1246 passed, 30 skipped (Node v24.18.0, Python 3.12.13, macOS).python scripts/dead_code.py --json: no new unreferenced or test-only symbols.