Skip to content

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
microsoft:mainfrom
cagdasyurekli:fix/opencode-v2-error-nudge-and-memory-cleanup
Open

Çağdaş Yürekli (cagdasyurekli) wants to merge 1 commit into
microsoft:mainfrom
cagdasyurekli:fix/opencode-v2-error-nudge-and-memory-cleanup

Conversation

@cagdasyurekli

Copy link
Copy Markdown
Contributor

Problem

Two issues in plugin/opencode/tokeneconomizer.ts, particularly under opencode 2.x's long-lived background service:

  1. Loop-guard redirects dropped on failed tool executions: In setupV2, ctx.tool.hook("execute.after") only applied out.hookSpecificOutput.additionalContext when e.status === "completed" and result was truthy. When a tool fails with status !== "completed" (which is when loop-guard redirects fire most often), the paid judge/redirect call ran and returned a redirect, then discarded it.
  2. Unbounded per-session state growth: In setupV2 (which stays resident across sessions in the 2.x daemon), records, steps, names, manuals, prompts, turns, and unsettled were never cleaned up on session.deleted / end(), and names was not deleted after session.tool.called consumed it. In v1, manuals, prompts, and turns also remained populated after end().

Fix

  • Failed-tool redirect delivery (deliverNudge + pendingNudges): In setupV2, redirects on completed calls continue appending to result.content (with result.output fallback when content is undefined). On failed calls, deliverNudge appends to e.error.message (or e.error when it is a string). If an error object is frozen/missing so the redirect cannot be attached in place, it is held in pendingNudges for 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.
  • Session cleanup (cleanup): Both v1 and v2 export the session JSON before removing per-session state. In v2, steps and names store sessionID alongside each entry so cleanup(id) removes every message, tool-call name, manual, prompt counter, turn, record list, and pending nudge for ended sessions; session.tool.called also deletes consumed entries from names; and setupV2's teardown removes its process.exit listener.

Verification

  • Added two offline Node tests in tests/test_plugin_package.py driving plugin/opencode/tokeneconomizer.ts via node --experimental-strip-types with a mock te-hook/te_bootstrap.py dispatcher:
    • test_opencode_v2_delivers_redirect_on_failed_tool_and_holds_when_immutable
    • test_opencode_v2_exports_session_before_cleaning_state_on_end
  • Ran python -m pytest tests locally: 1246 passed, 30 skipped (Node v24.18.0, Python 3.12.13, macOS).
  • Ran python scripts/dead_code.py --json: no new unreferenced or test-only symbols.

@Have-Purple Have-Purple left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ok makes test_opencode_v2_delivers_redirect_on_failed_tool_and_holds_when_immutable red;
  • removing cleanup(id) from end() makes test_opencode_v2_exports_session_before_cleaning_state_on_end red.

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:

  • deliverNudge still falls through to result.content = nudge when content is a non-string, non-array object, replacing it. Pre-existing, not a regression, but an explicit else 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/names map sizes; the memory claim is pinned indirectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants