Skip to content

Studio: an OpenCode session shows its context size (#719) - #787

Merged
danielrispler merged 2 commits into
mainfrom
issue-719-studio-an-opencode-session
Nov 28, 2026
Merged

danielrispler merged 2 commits into
mainfrom
issue-719-studio-an-opencode-session

Conversation

@danielrispler

Copy link
Copy Markdown
Owner

Closes #719.

Summary

An OpenCode session now shows its context size. The plugin keeps the latest step's input tokens on each top-level turn and sends them on the turn's end. The core puts them where Claude Code's transcript puts its count.

 plugin-opencode/plotkeeper.js  recordEvents
+  session.step.ended   turn.context = input + cache.read + cache.write   (zero or no tokens keep the last count)
   turn end (top-level) { model, last_assistant_message,
+                         context_tokens: turn.context ?? null }
   turn end (subagent)  { last_assistant_message, subagent_session_id }   # no count: a child's steps land on its own turn

 core/src/opencode.rs   ended(raw)
+  Happened::Context(tokens)   unless the line is a subagent's or late (an older count can't overwrite a newer one)
 core/src/studio/take.rs follow_process
+  session.transcript.context = Context { tokens }
 OpenCode CAPABILITIES
-  context: false
+  context: true

An end without context_tokens (the turn ended before any step did, or a log from before this change) leaves the session's last size as it was. The UI needed no change: the row mark reads transcript.context whatever the agent is, and the "reports no context size" note goes away because it follows capabilities.context.

Evidence

  • New real fixture core/tests/fixtures/opencode/context.jsonl: OpenCode 2.4.2's service, 2026-11-28. Turn 1 reads, writes and runs a command; turn 2 starts a background subagent, whose result wakes the session. A scratch capture plugin beside it logged every session.step.ended (the raw files are in local-evidence/2026-11-28-issue-719-opencode-context/):

    turn end context_tokens sent the session's latest step, input + cache.read + cache.write
    turn 1 11,215 11,215
    turn 2 11,516 11,516
    subagent's end none the subagent's own steps were 10,180 and 10,239, and neither was sent
    wake-up 11,689 11,689
  • Before: a_turn_ends_count_is_the_sessions_context_size_and_an_end_without_one_keeps_it failed with a later end's count: left: None, right: Some(13000).
    After: all 10 opencode tests pass:

    replay context.jsonl, check the context size after each end   → None, 11215, 11516, 11516 (subagent's end), 11516 (wake-up start), 11689
    table: a later end's count → 13000 · an end without a count → kept · a subagent's end with a count → kept · a late end → kept
    run.jsonl (the plugin before #719) → context None, as before
    
  • npm run check passes.

Tests: two new ones, the fixture replay and the table, and one assertion added to the_recorded_run_replays_into_its_sessions_turns_and_tools. None were removed or merged.

The recording had a catch: with Daniel's installed user-level plugin present, a project-level copy with the same id: "plotkeeper" recorded nothing. The scratch copy was renamed to plotkeeper-rec-719. This only matters for recording fixtures; a real install has one copy.

Correctness pass

/code-review high ran a standards axis and a spec axis, with state, ordering and stored-data tracing, and both tests run.

  • Fixed: a step ending with zero tokens would have set the count to 0. It is now || turn.context. This was unverified that OpenCode ever sends one.
  • Fixed: docs/studio/development.md, docs/studio/CONTEXT.md ("Context size") and follow_process's doc comment didn't name the new source. The new test also reuses server_line instead of copying it.
  • Left as is: in ended, the subagent and late conditionals could be one branch per case; Happened::Context could carry transcript::Context rather than u64. Both are judgement calls.
  • Stored data, both ways: capabilities are read back by agent name, so an old saved state gets context: true. An old build reading new state shows the count next to its stale note. No new stored fields.

Outside review

gemini-4-neon-medium: 0 findings. cost: 2.6 min, 273480 tokens; Gemini quota left: weekly 55% to 54%, 5h 93% to 89%. It reviewed the first commit; the review-fix commit was made while it ran, so it printed warning: the reviewed checkout changed during the review.

Merge Danger

Door: two-way

The new line field is optional on both sides. Old logs replay as before, and a new log read by an old build ignores the field.

Blast Radius: OpenCode-only

Only OpenCode sessions change: their header gains a token count. Daniel's installed plugin picks up the change on plotkeeper's next start (the rewire on start, #438).

🤖 Generated with Claude Code

https://claude.ai/code/session_01CVqACdAVieHSJNHrmEc2Xd

danielrispler and others added 2 commits November 28, 2026 04:13
OpenCode's plugin keeps the latest step's input tokens (session.step.ended's
input + cache.read + cache.write) on each top-level turn and sends them on the
turn's end as context_tokens. The adapter reads them into the session's
context; a subagent's or a late end, and an end without a count, keep it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CVqACdAVieHSJNHrmEc2Xd
A step with zero or no tokens keeps the turn's last count; the dev and
vocabulary docs and follow_process's comment name the new source; the
new test reuses server_line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CVqACdAVieHSJNHrmEc2Xd
@danielrispler
danielrispler merged commit 207bf0d into main Nov 28, 2026
11 checks passed
@danielrispler
danielrispler deleted the issue-719-studio-an-opencode-session branch November 28, 2026 02:24
danielrispler added a commit that referenced this pull request Nov 28, 2026
Resolves #719's OpenCode context size (#787) with #779's held wake-up: a
top-level turn's end goes through the recorder's send, after its held
wake-up, and carries context_tokens.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CVqACdVnKiSXskgVojtEiA
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.

Studio: an OpenCode session shows its context size

1 participant