Skip to content

fix(grok): count spontaneous root follow-up usage - #327

Merged
xxchan merged 2 commits into
mainfrom
fix/grok-background-usage
Oct 10, 2026
Merged

xxchan merged 2 commits into
mainfrom
fix/grok-background-usage

Conversation

@xxchan

@xxchan xxchan commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Grok starts a root follow-up after a background subagent finishes, without an ACP session/prompt reply. Its usage was therefore omitted. Count the independent subagent-completed-* root terminal ledger once per native event id, using the same accumulator as ordinary prompt replies so later replies retain it. Duplicate native frames remain available.

Opening history stays native-only. Track notification arrival against the matching successful session/new, session/resume or session/load answer on the decoded wire, before the SDK can delay notification callbacks. Remember historical identities, including explicit replay reports, so late duplicates cannot become new bills. The messages and their recorded native payloads are unchanged.

Child accounting remains partial. The native probe demonstrates that will_wake: true can accompany either an inclusive or exclusive parent ledger; source inspection also rules out inferring inclusion from completion-notification order. Keep child ledgers native-only and document the remaining late-child undercount instead of risking double counting.

Evidence and a repeatable probe: Grok background usage report. Includes three recorded completion orders, duplicate delivery and JSON replay, opening-history regressions, ownership and partial-ledger boundaries. All provider calls use isolated local mocks; no account or quota.

Validation passed: CI=1 pnpm run check (1,622 tests plus mock behavior) and build on the final code, based on main e5793f9. Claude/Codex/Pi/Pi Durable behavior suites passed; Pi was rechecked after incorporating the new main changes. Nine applicable Grok vendor tests and the three-scenario native Grok 1.0.50 probe passed after the wire-boundary revision. Twenty-seven regressions cover accounting and opening boundaries. The first callback-based gate failed on repeat; the report records why it was replaced with wire-order tracking.

Fixes #325 under the agreed scope: count independent root follow-ups and document the child ledger gap. This is a separate follow-up to #320, outside the 0.54.0 release batch.

@xxchan
xxchan force-pushed the fix/grok-background-usage branch from 0465460 to 3805905 Compare October 10, 2026 09:16
@xxchan
xxchan merged commit 4765958 into main Oct 10, 2026
17 checks passed
@xxchan
xxchan deleted the fix/grok-background-usage branch October 10, 2026 09:24
@xxchan xxchan mentioned this pull request Oct 10, 2026
xxchan added a commit that referenced this pull request Oct 10, 2026
Release OAR and CLI 0.54.2. This PR changes only the two package version
fields.

**Fixed: grok's own follow-up turn counts in `usage()`** (#325, #327)
- When a background subagent finishes, grok starts a root turn on its
own (`prompt_id: "subagent-completed-<child>"`). It has no
`session/prompt` answer, which is where OAR read the root's ledger, so
its spend was missing from `usage().total`. OAR now adds that turn's
`turn_completed.usage` to the same running total as prompt answers, once
per native `_meta.eventId`; later prompt answers keep it. The original
frames stay in the record stream.
- History is never billed again. grok replays old notifications when a
session is opened with `session/load`. OAR records, on the wire, whether
each notification arrived before or after the session-open answer, even
when the ACP SDK delivers it later, and counts only those after it that
are not marked as a replay. A resumed or loaded session's total starts
empty, as before.

**Still not counted, now documented:** a background subagent's own spend
when it finishes after the parent's turn ends. grok includes a child's
spend in the parent's prompt ledger only when the child finishes during
that prompt, and neither `subagent_finished.will_wake`, the model's
`background` argument nor the order of the notifications tells OAR which
happened (grok can apply the child's usage before it schedules the
finished notification on another worker). Grok child ledgers therefore
stay native-only and `withChildren` stays absent for grok, so
`usage().total` can undercount such a child. `docs/runtimes/grok.md` has
the evidence and a repeatable probe
(`experiments/grok-background-usage-2026-10-10.md`).

**Behavior changes**
- On grok, `usage().total` grows when grok runs a follow-up turn after a
background subagent, with a `usage` event on that notification's frame.

Validation: #327 passed all 17 checks on its final head, and its merged
tree equals the tree verified locally on top of 0.54.1 (typecheck, lint,
full tests). Lookout verified it against real grok 1.0.50 with a mock
provider, with no account: background, foreground and resumed sessions,
and a shim that forced `session/load` and renamed grok's replayed
history to the live notification name, so replays looked exactly like
live reports. No replayed ledger was counted. After this PR passes CI
and merges, tag its exact squash commit as `v0.54.2` to trigger npm
trusted publishing.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: exe.dev user <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

grok: a background subagent's spend and the root's follow-up turn are missing from usage()

2 participants