Skip to content

opencode: hand over a session that has gone quiet - #230

Open
Have-Purple wants to merge 1 commit into
microsoft:mainfrom
Have-Purple:feat/opencode-session-lifecycle
Open

Have-Purple wants to merge 1 commit into
microsoft:mainfrom
Have-Purple:feat/opencode-session-lifecycle

Conversation

@Have-Purple

@Have-Purple Have-Purple commented Nov 14, 2026 •

Copy link
Copy Markdown

On 2.x opencode run and the TUI are the same background service, so mode() — which reads
process.argv for "run" — cannot tell a headless run from an interactive one. A headless run
therefore only folded at the next session start, through the recovery path.

A session quiet for fold.STALE_S (600 s) is now handed over on a timer, matching TE's own
recovery rule; TE_OPENCODE_QUIET_MS overrides the window (floor 10 s). Where mode() == "run"
can still be detected, the immediate hand-over is unchanged, and any prompt or tool call resets
the timer.

This is the first half of what was #230: the session-model change split out to #243 so it can be
discussed on its own, as review asked.

Tests

  • a quiet session ends with end_reason == "idle" after the window, while the process is still up.

Docs

plugin/README.md and docs/PLUGIN.md no longer say a TUI session is only folded at the next
session's start; they say the plugin hands it over itself after the quiet window.

Verification

opencode 2.3.0, Python 3.15, Windows. Offline suite: 1167 passed, 36 skipped, 0 failed. The
plugin's non-service tests (console windows, quiet hand-over) pass. The two opencode service
tests fail on this machine on main as well (baseline re-run at 583fff7), so they are not
evidence about this change; CI runs them on the pinned 2.2.5.

@Have-Purple

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@aaron-jx-li Aaron Li (aaron-jx-li) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Related to #229, this PR also needs a few changes before it can be merged:

  1. tokeneconomizer/plugin/fold.py:505 now reads session_usage before ctx.host.settle(transcript) (fold.py:520). Copilot's settle waits for session.shutdown, the only event that carries usage, which Copilot writes just after the SessionEnd hook returns (hosts.py:1176). With the read moved ahead of it, a Copilot fold that starts before that line is written records no tokens: when session.shutdown lands one second into the fold, main records 4 requests and 1500 input tokens, this branch requests: 0, input_tokens: None. Please keep the usage read after settle.
  2. The fallback to the log's model (fold.py:506) passes a bare model id, because the opencode reader keeps only modelID (trace/opencode.py:156,186): the side call becomes opencode run --model gpt-5.6-sol. opencode reads --model as provider/model, so that is a resolution error, not the session's model. On 1.x the server entry never sends model, so every paid proposer call would take this path and fail where it runs on the default model today. test_a_fold_takes_the_model_from_the_session_log (tests/test_live_hooks.py:1761) asserts the bare id. Please drop the log fallback, or build providerID/modelID from the log.
  3. Please split the two changes, as you offered. The quiet hand-over only changes the plugin. Following the session's model changes what side calls cost: they would run on the session's own model and variant (#high included, plugin/opencode/tokeneconomizer.ts:546), where docs/PLUGIN.md:98 describes the optimizer as the cheapest capable model. That needs its own discussion, and plugin/README.md:412 ("on your default opencode model") has to change with it.
  4. Please rebase onto main: since this was opened, #185 added a role= argument to sidecall.runner_for and the local judge's runner, and #160 changed _fold_microskills, so this conflicts in fold.py, judge.py and sidecall.py. The session's model has to go through runner_for alongside role= rather than replace it.
  5. For the quiet hand-over, please update plugin/README.md:403-410 and docs/PLUGIN.md:832, which still say a TUI session is only handed over at the next session's start.

On 2.x 'opencode run' and the TUI are the same background service, so mode() cannot tell them
apart: a headless run only folded at the next session start. A session quiet for TE's own
recovery rule (fold.STALE_S, 600 s; TE_OPENCODE_QUIET_MS overrides it) is now handed over on a
timer. Where mode()=='run' can still be detected, the immediate hand-over is unchanged, and
any prompt or tool call resets the timer.

plugin/README.md and docs/PLUGIN.md no longer say a TUI session waits for the next start.
@Have-Purple
Have-Purple force-pushed the feat/opencode-session-lifecycle branch from 0c56082 to 941a210 Compare November 16, 2026 14:14
@Have-Purple Have-Purple changed the title opencode: hand over a quiet session, and follow the session's model in side calls opencode: hand over a session that has gone quiet Nov 16, 2026
@Have-Purple

Copy link
Copy Markdown
Author

Split as offered: this PR is now the quiet hand-over only (one commit, 941a210, rebased onto
583fff7). The session-model change is #243 (one commit, 70a63b6).

Both review items that apply here are in:

  • plugin/README.md and docs/PLUGIN.md no longer say a TUI session waits for the next start;
    they say the plugin hands it over itself after the quiet window.

The model PR carries the other three fixes: the usage read is back after ctx.host.settle(), the
fold no longer takes a model from the session log (bare id, unusable as opencode run --model),
and the model travels through runner_for(role=..., session_model=...) on top of #185.

Evidence for each: offline suite green (1167 passed / 36 skipped here, 1171 / 36 on #243), the
plugin's non-service tests pass against opencode 2.3.0 on Windows. The two opencode service
tests fail on this machine on main as well (baseline re-run at 583fff7), so they are not
evidence about either change; CI runs them on the pinned 2.2.5.

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