Repository navigation
Conversation
li-xiu-qi
requested review from
MelodyVAR,
Uking-xxx,
ZouR-Ma and
liuedcson
as code owners
October 24, 2026 17:45
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.
Fixes #180
Problem
After a subagent or workflow finishes, its own result can display token usage and cost, but
/sessionomits that spending from the parent session's totals. Users therefore cannot reconcile a task's actual cost without manually adding every delegated result. Issue #180 reports the same gap for workflows.The usage was already collected, but the handoff into the parent session ledger was incomplete: foreground results did not populate
ToolResultMessage.usage, workflow terminal errors discarded it, and background lanes had no notification-independent accounting path. Adding parent-side records also required an Overview deduplication rule so parent and child transcripts would not be counted twice.Changes
Usageshape and persist it on foreground tool results, including cache and total cost.childSessionIdsthrough workflow terminal errors and the generic agent-loop error-result path. Keep the original error type and message whenever the error object is extensible.step-subagent-usageentry per background lane turn. Queued replies, interrupts, stops, and shutdowns now settle usage without depending on a terminal notification. Runner and worktree-cleanup failures retain previously observed usage.Behavior and compatibility
/session, the footer, and RPC session totals now include subagent and workflow token usage and cost. Commands, tool arguments, provider requests, model prompts, and notification timing are unchanged.WorkflowRunResultgains optionalusageandchildSessionIds;WorkflowAgentRunResultgains optionalsessionId. These are additive fields. Existing results and transcripts without them remain valid and need no migration.cost.totalis authoritative and zero component fields mean that no component split was provided.Validation
npm run checkfrom the repository root: passed on Windows with Node v24.21.0.cd packages/agent-core && pnpm exec vitest --run test/agent-loop.test.ts: 38/38 passed.cd packages/coding-agent && pnpm exec vitest --run test/step-usage.test.ts test/suite/agent-session-delegated-usage.test.ts: 30/30 passed. This covers the real AgentSession consumer path, including workflow failure, budget exhaustion, top-level script failure, and synchronous background progress.cd packages/coding-agent && pnpm exec vitest --run test/step-subagent-events.test.ts -t "later preflight|rejected later turn": 2/2 passed, proving that a later turn cannot reuse the previous turn's usage.cd packages/coding-agent && pnpm exec vitest --run test/subagent-chain-result.test.ts test/step-subagent-events.test.ts -t "usage|worktree cleanup": 3/3 passed, including runner-failure and cleanup-failure usage preservation.cd packages/coding-agent && pnpm exec vitest --run test/subagent-child-env.test.ts: 8/8 passed.upstream/main(baseline: 207/210 passed). This run also reported one Windows temporary-directory cleanup error, while the baseline reported two. The 15 additional tests in this branch passed.d52521b: run 40116834458 completed withconclusion: success, covering build, check, and test on Ubuntu.conclusion: success; the Build, Check, and Test steps all completed successfully. Attempt 1 failed duringcoding-agenttests without a failure summary and was rerun without code changes.git diff --check: passed. Public-content review found no private paths, credentials, AI attribution, debug residue, or unrelated files.Limitations
subagent-child-env.test.tstiming tests can still be flaky under heavily parallel Windows runs: the 400 ms idle timeout may fire before queued child output is processed or race the 700 ms wall-clock assertion. This PR leaves those tests and the underlying timeout behavior unchanged because stabilizing them is outside the scope of subagent 的 token 花费不计入/session,两个视图数字不一致 #180.step-subagent-events.test.ts(oversized-output notification),subagent-exit-drain.test.ts(quiet-descendant exit), andworkflow-runtime.test.ts(transient progress-write failure) are pre-existing. They fail identically on unmodifiedupstream/main, so they are not introduced by this change and are not fixed by this PR.