Skip to content

Count OpenCode child sessions and Droid Workers in the parent's tokens - #2824

Merged
peyton-alt merged 5 commits into
mainfrom
peyton/child-session-tokens-in-totals
Oct 23, 2026
Merged

peyton-alt merged 5 commits into
mainfrom
peyton/child-session-tokens-in-totals

Conversation

@peyton-alt

@peyton-alt peyton-alt commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

https://entire.com/gh/entireio/-/trails/2340/changes/cli/1616

What was missing

OpenCode child sessions and Factory AI Droid Workers keep their token usage in their own sessions. The parent's transcript never includes it, so it was missing from every total: status, sessions, session tokens, attach and checkpoint metadata. The entire explain header total also left out subagents.

The fix

  • A child is credited exactly once, in the same locked state change that completes its task record.
  • The credit goes to a new summed field, ChildSessionTokens. It stays separate from TokenUsage.SubagentTokens, which each turn's snapshot replaces, so a later snapshot cannot erase a Worker.
  • A bounded ledger, CreditedChildCalls (last 256), survives condensation. It is keyed like completion-record keying: call ID, child and start. A stop repeated after condensation is not credited again. A reused call ID with a new start is still credited. An unknown start on either side matches any entry for the same child and call.
  • OpenCode declares agent.ChildSessionSubagents, so every completion of its children credits, including the SessionEnd sweep's completion of a child still running when the session ends. The sweep records the call's start as unknown, and an unknown start on either side matches in the ledger, so a later stop adds nothing. Droid Worker turns are credited on the existing per-turn path.
  • A child's task entry is not recounted from its transcript at condensation, so task.json shows the same figure the parent was credited. A child whose export failed keeps no usage in either place.
  • Checkpoints add the pending child share to their subagent total. Displays use SessionState.TotalTokenUsage().
  • entire explain now counts subagents in its header total.

Overlaps

Known limits

  • A child the SessionEnd sweep completes while it is still running counts only the usage its export held at that moment.
  • Tasks launched inside an OpenCode child are still skipped, both files and tokens.
  • Sessions that were already condensed are not backfilled.
  • After entire session attach, a child's tokens go into the attach checkpoint while its task.json goes into the next one. Nothing is counted twice.

Tests

New strategy, session, explain and integration tests. Each one fails on main, and the ledger tests also fail without the ledger. mise run test:integration and the canary pass, and lint reports 0 issues.

Size: +728/-39.

🤖 Generated with Claude Code

OpenCode child sessions and Factory AI Droid Workers keep their usage in
their own sessions, so the parent's transcript never carries it and no
total included it. The completion that claims a child's task record now
credits its usage once to a new summed field, ChildSessionTokens, kept
apart from the replace-on-snapshot SubagentTokens. A bounded ledger of
credited calls outlives condensation, so a repeated stop is not counted
again. Checkpoint metadata adds the pending share; status, sessions and
session tokens show it. OpenCode marks its stops with
SubagentUsageOutsideParent. The explain header total now includes
subagents.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Entire-Checkpoint: 01M5BHS1AQB3Z2RF41XC9A5BDG
@peyton-alt
peyton-alt requested a review from a team as a code owner October 20, 2026 02:21
Copilot AI balanced review requested due to automatic review settings October 20, 2026 02:21

Copilot AI 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.

🟡 Changes recommended

Finalization and recovery can omit child usage, while concurrent attach can count it twice.

3 open issues
What changed in this PR

Includes OpenCode child-session and Droid Worker usage in parent-session totals and checkpoint metadata.

Changes:

  • Adds separate child-token accounting and a bounded deduplication ledger.
  • Updates checkpoint, attach, adopt, and display paths.
  • Adds regression tests and documents accounting behavior.
File Description
docs/​development/​checkpoint-implementation.md Documents child-token accounting.
cmd/​entire/​cli/​strategy/​task_token_usage_test.go Tests credits and checkpoint windows.
cmd/​entire/​cli/​strategy/​manual_commit_git.go Credits completed child tasks.
cmd/​entire/​cli/​strategy/​manual_commit_condensation.go Includes pending child usage in checkpoints.
cmd/​entire/​cli/​status.go Displays combined totals.
cmd/​entire/​cli/​sessions.go Updates session token displays.
cmd/​entire/​cli/​session/​state.go Adds child totals, baselines, and ledger.
cmd/​entire/​cli/​session/​state_test.go Tests deduplication and totals.
cmd/​entire/​cli/​session_tokens.go Includes children in token reports.
cmd/​entire/​cli/​session_adopt.go Preserves child accounting during adoption.
cmd/​entire/​cli/​lifecycle.go Routes child completions to crediting.
cmd/​entire/​cli/​integration_test/​opencode_subagent_test.go Tests child totals across commits.
cmd/​entire/​cli/​integration_test/​factoryai_worker_session_test.go Checks Worker totals across checkpoints.
cmd/​entire/​cli/​explain.go Includes subagents in header totals.
cmd/​entire/​cli/​explain_test.go Tests subagent-inclusive headers.
cmd/​entire/​cli/​attach.go Detects concurrent child-token changes.
cmd/​entire/​cli/​agent/​opencode/​lifecycle.go Marks separately accounted child usage.
cmd/​entire/​cli/​agent/​event.go Defines the child-usage event flag.

Review effort: Balanced

Comment thread cmd/entire/cli/attach.go Resolved
Comment thread cmd/entire/cli/lifecycle.go Resolved
peyton-alt and others added 4 commits October 21, 2026 15:59
…tokens-in-totals

# Conflicts:
#	cmd/entire/cli/integration_test/opencode_subagent_test.go
…tokens-in-totals

# Conflicts:
#	docs/development/checkpoint-implementation.md
The SessionEnd sweep completed a still-running OpenCode child without
crediting its tokens, and a later stop then found the record completed,
so the child never counted. OpenCode now declares
agent.ChildSessionSubagents instead of flagging each stop event, so every
completion credits; the sweep records the call's start as unknown.

The credit ledger now matches an unknown start on either side, and a
child's task entry is no longer recounted at condensation, so it shows
the credited figure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Entire-Checkpoint: 01M5JWF8AHV4JM0J93SGX0A6CY
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Entire-Checkpoint: 01M5JWSMSDD6D93B7879RY10SV
@peyton-alt
peyton-alt merged commit 877dd61 into main Oct 23, 2026
21 checks passed
@peyton-alt
peyton-alt deleted the peyton/child-session-tokens-in-totals branch October 23, 2026 20:22
peyton-alt added a commit that referenced this pull request Oct 23, 2026
Reconciles with #2824: child sessions (Droid Workers, OpenCode children)
are counted only by its child-session credit. The Droid counter reads the
session's own files, and ledger checkpoints and attach add the pending
child-session tokens as the per-turn path does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Entire-Checkpoint: 01M5N9V8N0S99EC3S6Z52G2QA9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants