fix(cli): take Claude round tokens from the round's own usage #2583
[Minor] A session rotation can carry models into the new session
If an old-session background subagent emits an assistant message just before /clear binds a new native session, its model is already in answeredModels. Changing the session ID and consuming the suppressed /clear result leave the set populated. The first reply in the new session can then list the old subagent alongside its own model.
Suggested fix
Clear pending model names when the native session ID changes. Add a regression case that records an old-session assistant before the rotation and completes a reply after it.
[Minor] A held command result can consume a background round’s models
After /clear, a command result can be held until its replay echo. If a same-session background round emits an assistant message between the held result and that echo, forwarding the held result consumes the background round’s model set. hapi_skip_usage protects the cost baseline, but this code still assigns and clears the models. The command gets the background model, and the background result can lose it.
Suggested fix
Capture the command’s model list when its result first arrives, or have replayed command results leave the live model set untouched. Cover an echo that arrives after the background assistant message and before its result.
[Minor] Android drops subagent models from the new summaries
The CLI now emits every model entry with empty counters. Android’s RoundSummary.kt:89 discards the whole map when every entry lacks counters. In a reply whose Haiku messages are nested under an Agent tool call, Android falls back to the main response model and drops Haiku from the round details. The old summary could display both models when their counters were available.
Suggested fix
Keep empty model entries in Android’s round-summary decoder and retain its existing token fallback. Add a generated conformance case with a main model and a subagent model whose entries both lack counters.
[Minor] A prior plain-model entry masks the current 1M variant
After switching from plain Sonnet to Sonnet [1m] in a resumed session, the cumulative result map can contain both keys while the assistant message still reports bare Sonnet. This condition chooses the bare key whenever both exist, so the footer labels the current 1M reply as plain Sonnet. The converter already receives the suffixed model ID from system/init for this preset.
Suggested fix
Use the active init model to resolve the matching main assistant’s variant. Add a case where the current init model is suffixed and the result map contains both keys.
✅ All stages passed (4 Minor)
Review mode: initial
Requirement — Pass
Claude round footers should show the current round’s main-model token counts, the models that answered, and the round’s cost including subagent spend. The existing footer can show zero output despite positive round usage when the cumulative model entry contains input counters but omits output counters.
Evidence
Approach — Pass
Using the SDK result’s per-round usage removes the need to reconstruct token totals from cumulative counters. Assistant messages provide model identities, while cost and API time still use cumulative deltas.
Evidence
Code — Reviewed
Reviewed the full merge-base diff and relevant client consumers. The findings affect which models appear in the round footer.
cli/src/claude/utils/sdkToLogConverter.ts:160cli/src/claude/utils/sdkToLogConverter.ts:605cli/src/claude/utils/sdkToLogConverter.ts:613cli/src/claude/utils/sdkToLogConverter.ts:103Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:5363be15ecf684f4260e4439653ac7b0d2ea5b43d73d1023eb288c41218f3ae0 -->
HAPI Bot
[Minor] Android drops round model names when it decodes the timeline event
A Claude round with Sonnet and Haiku initially decodes both names from round_models into empty modelUsage entries. NormalizeAgent.kt:663 serializes those entries into the synthesized event without round_models. When the reducer reads TurnSummary.summary, the second decode applies the legacy rule and discards the empty entries. The footer falls back to the message’s model and omits Haiku.
Suggested fix
Preserve the explicit model list through event serialization and decoding while keeping the legacy wire fallback. Add a regression test that passes a two-model wire summary through the timeline reducer and verifies both names in the footer.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude round footers should show the current round’s main-model tokens, identify the models that responded, and include subagent spend in the round cost. The existing footer can show zero output tokens when a cumulative model entry has input counters but lacks an output counter.
Evidence
Approach — Pass
Forwarding the SDK’s round usage removes the need to reconstruct token totals from cumulative counters. Assistant messages supply the model names, and cumulative deltas still supply round cost and API time.
Evidence
Code — Reviewed
Reviewed the full merge-base diff. Android still loses the new model list between wire decoding and footer rendering.
android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/RoundSummary.kt:101Testing
Not run (automation; PR code execution is prohibited).
git diff --check.<!-- hapi-pr-review:v2:cc18ac559f261f996c239408b501e2f9085f10cb6fd1a243e3a519666cc8e3b2 -->
HAPI Bot
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude round footers should show the current round’s main-model token counts, identify the models that answered, and include subagent spend in the round cost.
Evidence
Approach — Pass
Using the SDK’s round usage removes the need to reconstruct token counts from cumulative counters. Assistant messages supply model names, and cumulative deltas still supply cost and API time.
Evidence
Code — Reviewed
Reviewed the full merge-base diff. Android now preserves model names through event serialization and timeline reduction. One CLI issue remains when a main model and a subagent use different variants of the same model.
cli/src/claude/utils/sdkToLogConverter.ts:500Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:953f32cc749ee732ae868507c7a2c999ef17721363cef25426e04676951b8e81 -->
HAPI Bot
[Minor] A subagent’s model can collapse into the main model’s [1m] variant
When the main model uses claude-sonnet-5[1m] and a subagent uses plain claude-sonnet-5, both assistant messages report the same bare ID. The set deduplicates them before variant resolution, and line 104 resolves the remaining entry to the main model’s [1m] ID. The footer omits the plain model even when the SDK result contains both variants.
Suggested fix
Keep the main and subagent observations distinct using parent_tool_use_id. Apply the active init variant only to the main model, then deduplicate the resolved names. Add a regression test with both variants in one round.
[Minor] A partial model snapshot can hide the plain subagent model
When a claude-sonnet-5[1m] main model and a plain claude-sonnet-5 subagent both answer, the converter initially records both IDs. If modelUsage contains only the [1m] key, the fallback promotes the subagent’s bare ID to that same key, and deduplication leaves the footer showing only the main model. The regression test at line 679 of sdkToLogConverter.test.ts supplies both keys, so it misses this case.
Suggested fix
Keep the bare subagent ID when the suffixed key is already attributed to the main thread. Extend the mixed-variant test to cover a result that omits the plain key.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude round footers should show the current round’s main-model tokens and participating models, with subagent spend included in the round cost. The current footer can undercount tokens when the SDK’s cumulative model counters are incomplete.
Evidence
Approach — Pass
Using the SDK’s per-round usage removes the token dependency on cumulative snapshots. Assistant messages supply the model list, while cumulative deltas supply cost and API time.
Evidence
Code — Reviewed
Reviewed the full merge-base diff. Android preserves the model list through timeline reduction. The CLI still drops a plain subagent model when the cumulative snapshot contains only the main model’s [1m] key.
cli/src/claude/utils/sdkToLogConverter.ts:103Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:2c4479a6e98462c7503f454d42f0aa095701101b77b08912a997e7ec81230ef4 -->
HAPI Bot
[Minor] Held command results reach accounting too late
If a native clear alias rotates the session but its result still carries the old ID, the new session has no baseline. A subsequent /help result can supply the first valid snapshot and be held for its input echo. If a background round finishes before that echo, its footer omits cost and the API-time-based token rate because accounting has yet to see the command snapshot. The integration test at cli/src/claude/claudeRemote.test.ts:811 seeds the baseline with the clear result and misses this case.
Suggested fix
Account for the first matching command result when it is captured, then skip accounting when it is replayed for display. Extend the integration test to cover a rotated session whose clear result carries the old ID.
[Minor] A command result can consume a background round’s model list
When the SDK echoes /help before returning its result, the result is forwarded without hapi_skip_usage. If a same-session background assistant message arrives before that result, the converter puts its model on the command summary and clears it from the pending list. The background round’s eventual footer can then omit the subagent model that participated.
Suggested fix
Let local command receipts that close no assistant round update accounting without consuming pending model IDs. Test the echo-first ordering alongside the existing held-result case.
✅ All stages passed (2 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude round footers should show the current round’s main-model tokens and participating models, with subagent spend included in the cost. The current footer can undercount output tokens when a cumulative model snapshot omits that counter.
Evidence
Approach — Pass
Use the SDK result’s round usage for tokens, assistant messages for model IDs, and cumulative deltas for cost and API time. Web and Android can render the explicit model list through their existing footer logic while retaining the fallback for stored summaries.
Evidence
Code — Reviewed
Reviewed the full fixed-head merge-base diff. The prior mixed-variant finding is resolved. Two command-result ordering cases still affect round footers.
cli/src/claude/claudeRemote.ts:861cli/src/claude/utils/sdkToLogConverter.ts:614Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:e1e0c96f4345406a3286e724201d704d2e40cc858e5887cf3d6126156aea89a0 -->
HAPI Bot
[Minor] A /compact receipt can consume a background round’s model list
The wrapper recognizes a manual /compact result by its input UUID even when local_command is absent, and forwards it with hapi_compact_turn (cli/src/claude/claudeRemote.ts:914). If a same-session background subagent emits an assistant message before that receipt, the converter attaches its model to the compaction summary and clears the pending list. The background round’s eventual footer can then omit the subagent model.
Suggested fix
Exclude results marked hapi_compact_turn from model-list consumption while still advancing cost and API-time accounting. Add a regression case with a background assistant message, a UUID-only /compact receipt, and then the background result.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude round footers should show the current round’s main-model token counts, the models that participated, and the round’s cost including subagent spend. The existing footer can show zero output tokens when a cumulative model entry omits that counter.
Evidence
Approach — Pass
Taking tokens directly from result usage removes the unreliable per-model deltas. Collecting model IDs from assistant messages and accounting for held command results on arrival lets the CLI provide the footer data while Web and Android preserve the rendering of stored summaries.
Evidence
Code — Reviewed
Reviewed the full fixed-head merge-base diff. The two prior findings are addressed. A manual compaction receipt can still take model IDs from a background round.
cli/src/claude/utils/sdkToLogConverter.ts:605Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:32fcc7093191760fb25b7da002e153882c4faf7abdda411351eec3812a67af07 -->
HAPI Bot
[Minor] A late command receipt can overwrite the background round footer
After /clear, a held /help result can be forwarded on its input echo after a same-session background reply has already published its summary. The wrapper consumes the echo at cli/src/claude/claudeRemote.ts:863, so it creates no transcript boundary. The converter still emits the receipt as a turn summary with no round_models or cost. The Web and Android reducers assign it to the same assistant group, replacing the background reply’s model list, usage, and cost. See web/src/chat/reducerTimeline.ts:607 and android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/ReducerTimeline.kt:610.
Suggested fix
Keep command accounting on arrival and route receipt display through an event that leaves assistant round summaries intact. Add a reducer-level regression with a background reply and result followed by the held command’s echo.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude round footers should show the round’s main-model token counts and participating models, with subagent spend included in the round cost. The current footer can show zero output tokens even when the SDK reports positive round usage.
Evidence
Approach — Pass
Using the SDK result’s round usage removes the dependency on cumulative token snapshots. Assistant messages supply model names, while cumulative deltas continue to supply cost and API time.
Evidence
Code — Reviewed
Reviewed the full fixed-head merge-base diff. The UUID-only /compact receipt now preserves pending model names. A late command receipt can still replace an already-rendered background round footer.
cli/src/claude/utils/sdkToLogConverter.ts:624Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:dcb0647820400e31ce5d9a1d0a4e80f65f68da1eb76794ad8023827de43b2a78 -->
HAPI Bot
[Minor] Sidechain results consume the enclosing round’s model list
When a subagent emits a same-session result with parent_tool_use_id before the main result, the converter puts the accumulated main and subagent models on that sidechain result and clears the set. The web client routes the summary to the subagent card, which can show the main model, while the enclosing reply loses the subagent model. The existing sidechain result path is covered at cli/src/claude/utils/sdkToLogConverter.test.ts:446 and web/src/chat/reducer.test.ts:683.
Suggested fix
Reserve consumption of the enclosing round’s model list for a top-level result. If the sidechain footer needs model names, derive them from messages with the matching parent_tool_use_id. Cover a sidechain result followed by the enclosing result with distinct model names.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude round footers should show the round’s main-model token counts, name the models that answered, and include subagent spend in the round cost. Incomplete cumulative counters can currently make a reply with positive output usage show zero output tokens.
Evidence
Approach — Pass
Using the SDK result’s own usage supplies per-round token counts directly. Assistant messages supply model names, while cumulative deltas still supply cost and API time. Accounting for held receipts on arrival preserves the baseline order.
Evidence
Code — Reviewed
Reviewed the full fixed-head diff. The previous late-command footer overwrite is addressed. A sidechain result can still consume the enclosing round’s model list.
cli/src/claude/utils/sdkToLogConverter.ts:297Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:85b717a526f9470d49d0488a57ad100ddc5c3bee8cf4e71a1d2e8b7f4a3a1ac4 -->
HAPI Bot
[Minor] Top-level results discard model names for unfinished subagents
When a background subagent answers before the enclosing result and finishes afterward, the enclosing result clears its per-tool model set. If the subagent switches to a fallback model after that boundary, its own summary lists only the fallback model and omits the model that answered earlier in the same run. Mid-run fallback is documented at web/src/components/ToolCard/ToolCard.tsx:233.
Suggested fix
Clear the enclosing round’s model set at the top-level result. Keep each subagent’s model set until its own result, a native session rotation, or a new process. Cover a background subagent that reports distinct models on either side of the enclosing result.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude round footers should show the current round’s main-model token counts, name the models that answered, and include subagent spend in the round cost. The existing calculation can show zero output tokens for a reply that generated output.
Evidence
Approach — Pass
The SDK result supplies round usage directly. Assistant messages supply model names, and cumulative deltas supply cost and API time. Accounting for held receipts on arrival preserves the order of the cost and time snapshots.
Evidence
Code — Reviewed
Reviewed the full fixed-head diff. The prior sidechain finding is addressed. An enclosing result can still erase the model history of a background subagent that is running.
cli/src/claude/utils/sdkToLogConverter.ts:314Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:a72e051cfa26c55078b8b092c70d976e8a129f07bf60924717ceb41ce5a1ba4c -->
HAPI Bot
[Minor] A first-input reset receipt can still replace a reply footer
When /reset is the first input and the clear hook identifies the new session, an untagged result carrying the new session ID leaves staleUsageSession false. The existing first-input alias test exercises that result shape at cli/src/claude/claudeRemote.test.ts:1715. The new predicate forwards it as a normal result, so the converter emits a round summary and the web reducer can overwrite the latest eligible reply’s footer with the reset’s duration and turn count.
Suggested fix
Mark the result recognized by the first-input clear-hook path as a command receipt regardless of which session ID it carries. Keep the usage skip conditional on the stale ID, and extend the first-input alias test to check that the reset emits no round summary.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the current round’s main-model token usage, name the models that answered, and include subagent spend in the round cost. The existing calculation can show zero output tokens for a reply that generated output.
Evidence
Approach — Pass
Using the SDK result’s round usage removes the unreliable token subtraction. Assistant messages provide model names, and accounting for held receipts on arrival preserves the order of cumulative cost and API-time snapshots.
Evidence
Code — Reviewed
Reviewed the full fixed-head diff. The follow-up preserves unfinished subagent model history across an enclosing result. One reset receipt still bypasses the new classification.
cli/src/claude/claudeRemote.ts:922Testing
Not run (automation; PR code execution is prohibited).
git diff --check. Review verification was read-only.ScratchlistStoreTest upload-progress test with UncompletedCoroutinesError. Android transcript layout checks on APIs 29 and 36 were still running. https://github.com/tiann/hapi/actions/runs/45365642012/job/138346537380<!-- hapi-pr-review:v2:316f82a9f56ff03cb2f0ebba55bbeed99127b7eef3e79cccbe262083310fbb22 -->
HAPI Bot
[Minor] Late subagent results replace the Android reply’s model list
When a background subagent finishes after the enclosing turn and before the next prompt, the CLI emits a summary containing only that subagent’s models, as covered at cli/src/claude/utils/sdkToLogConverter.test.ts:694. Android’s turn-summary normalizer drops the sidechain marker at android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/NormalizeAgent.kt:658. The root reducer then replaces the enclosing reply’s footer at android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/ReducerTimeline.kt:611, so the main model disappears from its own reply.
Suggested fix
Preserve the sidechain marker and parent tool-use ID when Android normalizes Claude turn summaries, then route them to the subagent’s group. Add a reduction test where a subagent result arrives after the enclosing result and verify that the enclosing footer keeps its original summary.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the current round’s main-model tokens and the models that answered, while retaining subagent spend in the round cost. The existing footer can show zero output tokens for a reply that generated output.
Evidence
Approach — Pass
Using the SDK result’s per-round usage removes cumulative token subtraction. Assistant messages supply the model names, and accounting for held command results on arrival preserves the order of cost and API-time snapshots.
Evidence
Code — Reviewed
Reviewed the full fixed-head diff. The previous first-input reset finding is resolved for both session IDs. Android can still attach a late subagent’s model list to the enclosing reply.
cli/src/claude/utils/sdkToLogConverter.ts:653Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:1ef328cc019caa4e14e4a76cc0913361e9182e18c00833808080c54443f78d75 -->
HAPI Bot
[Minor] A finished background subagent leaks into the next reply’s model list
If a background subagent emits its final assistant message and result after the enclosing reply has finished, both events can arrive before the next prompt. The assistant message adds its model to answeredModels, but the subagent result clears only its private list. The next reply then lists the finished subagent’s model even when only the main model answered. The converter test at line 694 currently expects this carryover.
Suggested fix
Associate subagent model entries with their parent round. Keep late output available for the subagent’s own summary and exclude it from later replies once the parent round closes. Update the test to place the subagent completion before the next prompt and expect only the main model on that reply.
[Minor] A subagent summary can overwrite the main footer when its parent call is outside the loaded page
A paginated transcript can contain the main reply and a late subagent result while omitting the older Agent tool call. Android’s tracer then leaves the summary ungrouped at Tracer.kt:127, and the root reducer replaces the main reply’s footer at ReducerTimeline.kt:611 with the subagent’s model list and usage. The web tracer has the same ungrouped fallback at web/src/chat/tracer.ts:142.
Suggested fix
Exclude unresolved sidechain summary events from the root timeline in both clients. Add a reducer test with the main reply and late subagent summary in the loaded page and the parent call outside it.
✅ All stages passed (2 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the round’s main-model token counts and identify the models that answered in that round. The base footer can show zero output tokens for a reply that generated output.
Evidence
Approach — Pass
Using the SDK result’s round-local usage removes token baseline errors. Assistant messages provide model names, and accounting for held command receipts on arrival preserves the order of cost and API-time snapshots.
Evidence
Code — Reviewed
Reviewed the full merge-base diff. Android now routes a late subagent summary to its card when the parent call is loaded. Two attribution errors remain.
cli/src/claude/utils/sdkToLogConverter.ts:515android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/Tracer.kt:104Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:2ef353bade1fc5faf3b306b72a0cdeeaf245371e5641e0f38a7f6a180bdf6257 -->
HAPI Bot
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the current round’s main-model token counts and identify the models that answered in that round. The existing footer can show zero output tokens for a reply that generated output.
Evidence
Approach — Pass
Using the SDK result’s own usage removes the dependency on cumulative token counters. Assistant messages provide model names, and accounting for held command receipts on arrival preserves the order of cost and API-time snapshots.
Evidence
Code — Reviewed
The full merge-base diff addresses the previous findings for direct subagents and summaries whose parent card is outside the loaded page. Late output from a nested subagent can still be attributed to the next reply.
cli/src/claude/utils/sdkToLogConverter.ts:540Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:efabddf8a15eb2f4e9e3a3ab3b8563aab426096640a21b1ba8b2cda30193268a -->
HAPI Bot
[Minor] A late nested subagent can appear in the next reply’s model list
If a subagent starts another Agent tool call before the main reply finishes and the nested agent first answers afterward, the converter has no record that the nested call belongs to the closed round. The registration guard only records tool calls from the main thread. When the nested answer arrives, its model enters answeredModels and can appear in the next reply’s footer. The existing tracer test at web/src/chat/tracer.test.ts:215 covers Agent calls inside sidechains.
Suggested fix
Record nested Agent calls and give each child the same round as its parent. Add a converter test where the nested agent first answers after the enclosing result and verify its model stays on its own summary.
[Minor] Completed subagents stay in the round tracker
When a Task or Agent invocation finishes with a regular tool_result and no separate result carrying parent_tool_use_id, its model set and tool ID survive until the process or native session changes. The successful Task completion at shared/fixtures/chat/sidechain-task-nested-children.json:378 illustrates that tool-result shape. Each later reply scans the retained model sets, so repeated subagent use in a long-running session increases memory use and the work needed to process each footer.
Suggested fix
Release tracking state on definitive subagent completion, including a synchronous tool_result, and preserve it when the tool result only acknowledges a background launch. Move only the current round’s IDs into the late set and cover tool-result-only completion in a converter test.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the current round’s main-model token counts and identify the models that answered. The existing footer can report zero output tokens when a per-model counter is missing, even when the round reports positive output.
Evidence
Approach — Pass
Use the SDK result’s round-local usage for tokens, collect model names from assistant messages, and account for held command receipts when they arrive. Web and Android can render the explicit model list while preserving the existing fallback for stored summaries.
Evidence
Code — Reviewed
The full merge-base diff addresses the previous nested-subagent attribution finding. The converter still retains state for subagents that complete through a tool result alone.
cli/src/claude/utils/sdkToLogConverter.ts:329Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:3be54ffe35261f0126c388ebcedf2909062fa005e68cbf87f9084cb90832d955 -->
HAPI Bot
[Minor] Background subagents can stay tracked after completion
A background Agent launch keeps its tool-use ID in the tracker, and the enclosing result moves it into lateSubagents. If the task then finishes through a terminal system event without a separate result carrying parent_tool_use_id, the converter forwards the system event without releasing the ID or its model set. Repeated background tasks therefore grow the CLI’s retained state until the process or native session changes. HAPI has observed task_notification events for background completions in production. https://github.com/tiann/hapi/pull/2102
Suggested fix
Release the tracked tool-use ID on terminal SDK task events. Use the event’s tool_use_id when present, or correlate its task_id with the task-start event. Add a converter test for a background launch that completes through a task notification alone.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the round’s main-model token counts, identify the models that answered, and retain per-round cost and API time. The current footer can show zero output tokens when a per-model counter is missing despite positive round usage.
Evidence
Approach — Pass
Use the SDK result’s round-local usage for tokens, collect model names from assistant messages, and account for held command results when they arrive. Web and Android can render the explicit model list while preserving the fallback for stored summaries.
Evidence
Code — Reviewed
The full merge-base diff now releases tracking after synchronous subagent tool results. Background subagents that complete through SDK task lifecycle events can still leave tracking state behind.
cli/src/claude/utils/sdkToLogConverter.ts:326Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:96eec3b36a18013585b0864deb7429f79a7d8afbb8f0a7e3fd59b66dc4f23716 -->
HAPI Bot
✅ All stages passed (2 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the round’s main-model token counts, identify the models that answered, and retain per-round cost and API time. The current footer can report zero output tokens even when the round reports positive output.
Evidence
Approach — Pass
Using the SDK result’s round-local usage removes the dependency on cumulative token snapshots. Collecting model names from assistant messages and accounting for held receipts on arrival addresses model attribution and preserves cost and API-time ordering.
Evidence
Code — Reviewed
The latest commit handles terminal task notifications by task ID or tool-use ID. The static review found two remaining issues with subagent summary routing and cleanup.
android/core/protocol/src/main/kotlin/app/hapi/protocol/chat/Tracer.kt:104cli/src/claude/utils/sdkToLogConverter.ts:498Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:584de0f24f20b86d5be5380c52bfc1883182a35a891ee5b611aad9d5013d4283 -->
HAPI Bot
[Minor] Sibling subagent summaries land on the first Android card
When one assistant record contains two Agent calls with different tool-use IDs, Tracer.kt:67 maps both IDs to the same message ID. The new summary routing puts both results in that shared group. ReducerTimeline.kt:1097 lets the first card consume the group, so the second agent’s model and usage can appear under the first card and the second card has no summary.
Suggested fix
Key explicitly identified sidechains by the spawning tool-use ID in the tracer and reducer. Cover two Agent calls in one assistant record and verify that each card receives its own summary. Apply the same keying in the web reference pipeline.
[Minor] Standalone tool results leave completed subagents tracked
A synchronous Agent completion delivered as a top-level tool_result bypasses this cleanup. The converter accepts that envelope in its standalone handler at line 732, and sdkToLogConverter.test.ts:2009 exercises the shape. When that is the only completion event, the next enclosing result moves the finished tool ID into lateSubagents and retains its model set until a session change or process restart.
Suggested fix
Share the completion check between the two supported tool-result envelopes. Add a converter case that completes an Agent through a standalone tool_result and verifies that its tracking entries are released.
[Minor] A first-prompt reset receipt can consume background round models
When /reset is the first prompt, the clear hook can report the rotated session and an init for that session can arrive before the untagged receipt. The init sets currentSessionId at line 998, so the equality check here prevents receipt classification. The converter then treats the receipt as a reply and drains any models already collected for a background round in the rotated session. That round’s eventual footer can omit those models.
Suggested fix
Classify the first result identified by the clear hook as a receipt even when init has already bound the rotated session. Keep the ID comparison for deciding whether to rebind, and extend the first-prompt alias test to cover init arriving before the receipt.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the current round’s main-model token counts, identify the models that answered, and report round-local cost and API time.
Evidence
Approach — Pass
Using the result’s own usage fixes token accounting. Collecting model names from assistant messages identifies which models answered, and accounting for held receipts on arrival preserves the order of cumulative cost and API-time snapshots.
Evidence
Code — Reviewed
The current head fixes both earlier findings by grouping sibling subagents by tool-use ID and releasing completed subagents for standalone tool results. One first-prompt reset sequence can still consume a background round’s model list.
cli/src/claude/claudeRemote.ts:905Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:5a0745461077a769e3a086fe3752aeb0570a5d2cd97976d63eb50f9829f5be01 -->
HAPI Bot
[Minor] Held command results lose their terminal output
When a post-/clear command result arrives before its echo and carries result text or an execution error, this guard drops the entire result. The launcher’s onMessage callback also feeds the terminal display, where cli/src/ui/messageFormatterInk.ts:93 renders result text and errors. The command can now finish without showing that output in the terminal.
Suggested fix
Keep arrival-time accounting and send the held result through terminal rendering on echo. Suppress transcript summary generation for that result.
[Minor] Replies lose token counts when SDK usage is absent
The SDK result type allows usage to be omitted at cli/src/claude/sdk/types.ts:83. If a result omits it but has comparable per-model counters, such as a single-model reply on a fresh process with a zero baseline, the base converter supplied token counts. Removing modelUsage leaves the footer with its timing and cost but no token line. The existing web test at web/src/chat/conciseView.roundSummary.test.tsx:224 covers rendering a Claude summary with only per-model counts.
Suggested fix
Prefer result usage when present. For a result without it, use trustworthy same-round token counts when available and cover the missing-usage case with a regression test.
✅ All stages passed (2 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the main model’s token usage for the current round and report that round’s cost and API time. A command result held after /clear should leave a background reply’s footer intact.
Evidence
Approach — Pass
Using the result’s round-local usage removes dependence on incomplete cumulative token counters. Accounting for a held command result on arrival preserves the order of cost and API-time snapshots, and suppressing its footer preserves the background reply’s metadata.
Evidence
Code — Reviewed
The full merge-base diff fixes the reported accounting and footer overwrite bugs. Two display regressions remain for held command output and results with no round-level usage.
cli/src/claude/claudeRemote.ts:921cli/src/claude/utils/sdkToLogConverter.ts:725Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:5a5bd7f61e07529453985baf6ca7c7b726ff7a745c360f50b94cd6d26952ddaa -->
HAPI Bot
[Minor] Round footers lose token counts when result usage is absent
The result type permits omitted usage at cli/src/claude/sdk/types.ts:83. For a fresh session’s first single-model reply with complete modelUsage counters and no usage, the base converter could report those counters against its zero baseline. The new summary still carries duration and cost, but the web footer omits the token line because it has neither round-local usage nor per-model counts.
Suggested fix
Keep result usage as the primary source. For a result that omits it, populate round usage only where the counters are known to cover that round, such as a complete single-model snapshot against a known zero baseline. Cover that case with a regression test.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the main model’s token usage for the current round. A command result held after /clear should preserve the background reply’s footer and the next reply’s cost and API time.
Evidence
Approach — Pass
The result’s round-local usage avoids incomplete cumulative token counters. Accounting for a held result on arrival preserves the order of cost and API-time snapshots. Forwarding it on echo preserves terminal output and turn completion while suppressing the misplaced footer.
Evidence
Code — Reviewed
The current head restores terminal output for held commands and adds coverage for their accounting order. One token display regression remains when a result omits usage.
cli/src/claude/utils/sdkToLogConverter.ts:725Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:ce0328d5abb24a5b5fc8a179c95e51c16ad3356a1cbced7deea004c676cb1cdb -->
HAPI Bot
[Minor] Fresh replies can lose available token counts
The SDK result type permits omitted usage at cli/src/claude/sdk/types.ts:83. If the first reply in a fresh single-model session supplies complete modelUsage counters but omits usage, those counters cover that round and the base converter emits them from its known zero baseline. The new summary drops them, so the web and Android footers omit the token line.
Suggested fix
Keep result usage as the primary source. When it is absent, populate round usage from a complete single-model snapshot only when the converter can establish that it covers the round, such as the first result from a known fresh process. Add a regression test for that case.
✅ All stages passed (1 Minor)
Review mode: follow-up after new commits
Requirement — Pass
Claude reply footers should show the main model’s tokens for the current round. A command result held after /clear should preserve the background reply’s footer and the next reply’s cost and API time.
Evidence
Approach — Pass
Using the result’s round-local usage avoids incomplete cumulative token counters. Accounting for a held command on arrival preserves snapshot order, and forwarding it on echo preserves terminal output and turn completion while suppressing its misplaced footer.
Evidence
Code — Reviewed
The full merge-base diff addresses the reported accounting and footer overwrite bugs. The latest commit documents the missing-usage behavior, and one token display regression remains.
cli/src/claude/utils/sdkToLogConverter.ts:725Testing
Not run (automation; PR code execution is prohibited).
<!-- hapi-pr-review:v2:19eb4155866feaeb6353d3f9026c71cd95c1be48cf3d407511224f450d5f5c53 -->
HAPI Bot
github-actions[bot]
junmo-kim
Problem
A Claude round footer took its token counts from
modelUsage, which Claude Code reports as cumulative per-model counters that survive--resume. The CLI diffed those counters against the previous result to get per-round values. The diff has to cope with results that omitmodelUsage, return an empty map, drop a counter, or list a model whose counters never include output tokens, and some of these still produced a wrong footer. For example, a model whose counters report input but no output tokens made the footer show 0 output tokens althoughusage.output_tokenswas positive.Separately, after
/clear, a command result such as/helpcan arrive before its input echo and is held until the echo. When a background round of the same session finished in between, two things went wrong once the held result was forwarded:Solution
modelUsage. Token counts come from the result's ownusage, which is already per round, so the converter keeps no per-model token baselines. Web and Android already render a summary without per-model token counters as the message's model plus theusagetokens, so no client change is needed.usagecovers the main model, so a round that ran a subagent on another model shows only the main model's tokens; its cost still includes the subagent's spend.total_cost_usdandduration_api_msagainst a baseline tied to the native session id, omitted when there is no baseline or the value went backwards.Tests
bun run typecheck && (cd shared && bun test) && (cd cli && bunx vitest run src/claude) && bun run test:parity && (cd web && bunx vitest run src/chat src/components/AssistantChat/messages)sdkToLogConverter.test.ts: a round whose per-model counters lack output tokens now reports the result'susageand nomodelUsage. This fails before the change. Tests that only covered the removed per-model diffing are deleted; the cost and API time cases are kept.claudeRemote.test.ts: a/helpresult with its own cost, held until its echo while a background round completes. Before the change, the next reply's cost is 1.5 instead of 0.5 and the held result emits a summary. After the change, the background round reports only its share after the held result (0.75), the reply reports 0.5, the held result's text reaches the terminal, and it writes no transcript entry.