Skip to content

fix(usage): subagent and completion-turn model calls go unattributed in usage reporting - #303

Merged
william-antoline-boon merged 10 commits into
boonfrom
william/eng-21618-subagent-usage-attribution
Oct 22, 2026
Merged

william-antoline-boon merged 10 commits into
boonfrom
william/eng-21618-subagent-usage-attribution

Conversation

@william-antoline-boon

@william-antoline-boon william-antoline-boon commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Fixes an issue where model calls made by a subagent, and by the parent session's turn that reports a finished subagent, were sent to the LLM gateway without the originating user's usage headers (x-boon-user-id / -name / -source). Per-user usage reporting showed that work as unattributed, on every channel. The user source header was also dropped on ordinary primary and queued turns, and the gateway-audience OBO token never reached subagent runs.

Why This Change Was Made

A new attribution-only field, usageAttribution: { userId, userName?, userSource? }, carries the requesting user to subagent runs and to completion/announce turns. It is never used as senderId, so sender-scoped tool policy and command auth are unchanged. withBoonUsageHeaders uses it only when senderId is absent and takes the whole user triple from one source.

  • Spawn fills it from the requester's sender (or its inherited attribution for nested subagents) and stores it on the child session entry, so later turns (including steer/send into the child) keep it. sessions_send passes the current turn's user for the run it starts. Resets and idle rollover keep it.
  • The completion request (SubagentCompletionRequest.usageAttribution) and MsgContext.UsageAttribution let channel plugins and core's own announce delivery attribute the parent's completion turn.
  • senderSource is now forwarded on primary and queued turns, and into the embedded attempt (the run-to-attempt copy dropped it, so the source header was always empty).
  • The gateway-audience OBO token is forwarded to a subagent's first run, in memory only. It is never persisted, and later turns of a long-lived child run without it.
  • Trust boundary: the agent method accepts usageAttribution and oboToken only from core-built in-process dispatch. Socket clients and the plugin SDK dispatchGatewayMethod path cannot set them (both stripped), and the trust flag is not inherited by nested dispatches that run inside a trusted request's async scope. The WebSocket fallback strips both fields, which also keeps older gateways (strict agent schema) from rejecting the spawn. Log redaction now covers oboToken and the x-boon-gateway-obo-token header.

Non-goals: compaction model calls, agent-harness task completions and background media-generation completions still send no user headers; they can adopt the field later.

User Impact

Operators get per-user usage that includes subagent work and completion turns, instead of an unattributed bucket. No behavior change for tools, permissions or replies. All new fields are optional; older session entries, plugins and clients keep working.

Evidence

  • ~1,350 vitest tests across spawn, announce/announce delivery, sessions send, subagent control, auto-reply session reset, gateway agent, server plugins, plugin-SDK gateway method runtime, model-call headers, harness tool policy and log redaction. Key cases:
    • sender wins over attribution; no identity is invented when neither exists
    • depth-2 inheritance of attribution and token
    • socket clients and the SDK dispatch path cannot set either field
    • neither field crosses the WebSocket fallback; the child entry still carries attribution
    • a sessions_send from another user attributes the run it starts to that user; a2a ping-pong and announce steps follow it
    • a nested dispatch inside a trusted request does not inherit the trust flag
    • senderSource and attribution reach the embedded attempt
    • /new and idle rollover keep attribution
    • child tool policy identical with and without the parent's attribution
    • the token is never in the session store or debug log output
  • tsgo core and core-test typecheck, oxlint/oxfmt, plugin-SDK API check, import-cycle checks: pass.
  • Not yet run end to end against a live gateway; to be verified on a canary host before rollout.

🤖 Generated with Claude Code


Summary by cubic

Fixes per-user usage reporting so model calls from subagents and from the parent session's subagent-completion turns carry the originating user's usage headers (x-boon-user-id / -name / -source) instead of landing in the unattributed bucket. senderSource now flows through primary, queued, and embedded-attempt turns, and the gateway-audience OBO token follows a subagent's first run in memory only.

The new billing-only usageAttribution field carries the requesting user to subagent runs, their persisted session entries, and completion turns. It is never used as senderId, so sender-scoped tool policy and command auth are unchanged. Steered and sent child runs keep the attribution persisted on the child session entry from spawn.

Metering effect (intended)

Subagent and completion-turn calls now carry the same user identity and, on a subagent's first run, the same OBO token as the turn that started them. The gateway's existing internal-test rule therefore applies to them: work triggered by a Boon staff member is no longer metered against the customer, matching how that staff member's own turns are already treated. Work triggered by customer users is metered exactly as before.

Trust boundary

  • usageAttribution and oboToken are honored only from core-built in-process agent dispatch; socket clients, the plugin SDK dispatchGatewayMethod path, and the WebSocket fallback strip both, so older strict gateways still accept spawns.
  • The trusted in-process flag is scoped to the dispatch that declares it; nested in-process dispatches no longer inherit it from an outer core-built request.
  • The OBO token is never persisted, and log redaction now covers it and the x-boon-gateway-obo-token header.

Out of scope

  • Compaction, agent-harness task completions, and background media-generation completions still send no user headers and can adopt usageAttribution later.

Written for commit efff95f. Summary will update on new commits.

View guided diff Turn on auto-fix

william-antoline-boon and others added 5 commits October 21, 2026 15:19
Subagent child runs reached the LLM gateway without X-Boon-User-* headers
because the child run has no inbound sender, so usage showed as Unattributed.

Add an optional usageAttribution { userId, userName?, userSource? } to the
agent gateway method. It is honored only for core-built in-process dispatch:
dispatchGatewayMethodInProcessRaw marks the client with
internal.inProcessDispatch only when a core caller opts in. Socket clients
cannot set it, and the plugin SDK dispatchGatewayMethod (which proxies
authenticated HTTP route bodies) neither opts in nor forwards the field. The
caller-supplied lane is not a trust signal. It is deliberately separate from
senderId, which drives sender-scoped tool policy.

- sessions_spawn fills it from the requester run's sender, or from the
  attribution that run inherited (nested subagents).
- Persisted on the child SessionEntry next to spawnedBy, so later turns of a
  spawnMode=session subagent stay attributed; preserved on /new, /reset and
  idle/daily rollover.
- sessions_send (including its agent-to-agent follow-up steps) and subagent
  steer/send pass the current turn's attribution, so a run another user drives
  is attributed to that user instead of the spawning user.
- withBoonUsageHeaders falls back to it only when the turn has no senderId,
  taking the user triple from one source so identities never mix.
- Plugin runtime subagent.run accepts usageAttribution too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
get-reply-run sets senderSource on the run, but neither the primary-turn
sender context nor the queued follow-up runner forwarded it into the embedded
run params, so X-Boon-User-Source was never emitted for ordinary user turns.
Same drop pattern as oboToken: declare it on FollowupRun.run and forward it
at both hops.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Subagent runs did not carry the gateway-audience OBO token of the requester
turn, so the child's model calls went out without it.

sessions_spawn now carries the requester run's oboToken and passes it as an
in-process-only `oboToken` param on the agent call that starts the child's
first run. The agent method honors it only for core-built in-process dispatch
(same gate as usageAttribution) and drops it for other clients. It is threaded
to the run params so the model-call diagnostic context emits
x-boon-gateway-obo-token. The token is a per-turn bearer: it is never written
to the session entry, and the existing debug line still reports presence only.

Later turns of a persisted session-mode child run without it and keep only the
user header from usage attribution.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
After a subagent finishes, the requester session runs a completion turn
with no inbound sender, so its model calls reached the LLM gateway without
X-Boon-User-* headers and showed as unattributed.

- The announce flow resolves a billing-only usageAttribution for the
  completion: the child session's persisted attribution (captured from the
  requester turn's sender at spawn), else the requester session's own
  inherited attribution. The subagent registry keeps no requester sender,
  so the child entry is the closest record of it.
- SubagentCompletionRequest carries it as optional `usageAttribution`, so
  plugin completion owners can set it on the turn they build.
- The built-in delivery path passes it on the in-process `agent` call
  (same in-process-only gate as spawn), covering Slack/Teams and other
  channels that core hands back to the requester agent.
- MsgContext gains `UsageAttribution`. get-reply-run maps it (else the
  session entry's inherited value) onto the queued run, and the primary
  and follow-up runners forward it to the embedded run, where it feeds the
  user headers only when the turn has no senderId.

It never sets senderId, so command auth, sender-scoped tool policy and OBO
handling are unchanged; a completion with no resolvable user still sends
no user header.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The OBO bearer and usageAttribution are honored only for in-process dispatch,
but a subagent spawn that fell back to the WebSocket gateway client still
serialized them in the `agent` params. Strip both on that fallback: the
credential no longer crosses the socket, and an older gateway whose closed
`agent` schema rejects unknown params no longer fails the spawn. The child
stays attributed through its session-entry patch.

Log redaction now treats `oboToken` / `obo_token` / `obo-token` fields and the
`x-boon-gateway-obo-token` header as secrets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@william-antoline-boon
william-antoline-boon requested a review from a team October 21, 2026 08:20

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cubic analysis

All reported issues were addressed across 55 files

Linked issue analysis

Linked issue: ENG-21618: [Builders Inc] Usage attribution missing for Dan; tokens falling under unattributed usage

Status Acceptance criteria Notes
✅ Attribute subagent and completion usage to the originating user Usage attribution is propagated to those turns and applied to model-call headers when no sender is present.
⚠️ Show Dan’s attributed usage in the Builders Inc dashboard The change supplies user headers for the gateway, but live dashboard behavior has not been verified end to end.

Heads up: you’re close to your included review allowance. Set a flex budget so reviews don’t pause.

View guided diff | Turn on auto-fix | Re-trigger cubic

Comment thread src/gateway/session-reset-service.ts Resolved
Comment thread src/agents/subagent-control.ts Resolved
Comment thread src/agents/tools/sessions-spawn-tool.ts Resolved
Comment thread src/agents/tools/sessions.test.ts Resolved
Comment thread src/agents/tools/agent-step.ts Resolved
william-antoline-boon and others added 4 commits October 21, 2026 16:25
…ts it

Request scopes flow through AsyncLocalStorage into the runs a handler
starts, so a nested in-process dispatch inherited an outer core-built
request's inProcessDispatch flag. Strip the inherited flag from the scoped
client so only the dispatch that declares core-built params is trusted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The run-to-attempt params are an explicit copy and omitted senderSource,
so the user-source usage header and the source of the attribution a run
hands to its children were always empty.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
No production caller steers or sends through subagent control, so the
per-call override was dead code. Steered and sent child runs keep the
attribution persisted on the child session entry at spawn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Assert attribution reaches sessions_spawn's spawn context, a2a ping-pong
and announce steps, both agent-step execution paths, and survives a
session reset. Make the blank-token and token-persistence assertions
non-vacuous, and keep the plugin loader out of tool unit tests by mocking
the in-process dispatch module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@boon-hermes-bot boon-hermes-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Reviewed the full source diff (35 non-test files) at 498d7d3.

Verified

  • Trust boundary holds. usageAttribution and oboToken are read in the agent handler only when client.internal.inProcessDispatch === true. That flag is set only by core-built dispatch (spawn, announce, callAgentWithUsageAttribution, plugin subagent runtime). The SDK dispatchGatewayMethod strips both fields. The socket fallback in callSubagentGateway strips both. Nested dispatches drop an inherited flag via withoutInheritedInProcessDispatch.
  • Attribution is billing-only. It is never copied into senderId. withBoonUsageHeaders takes the whole user triple from one source (sender if present, otherwise attribution), so identities can't mix.
  • The OBO token is per-run only. It is read from opts, not from the session entry. It is not in buildDirectChildSessionPatch and the child patch is a direct store write. Redaction covers oboToken and x-boon-gateway-obo-token.
  • The senderSource drop in the run to attempt copy (run.ts) is fixed. The same field is forwarded through the followup runner and template context.
  • Attribution survives /new and idle rollover (session.ts, session-reset-service.ts). Depth-2 children inherit it via resolveRequesterUsageAttribution.
  • Optional fields only, so older session entries, plugins and clients are unaffected.
  • CI is green except security-fast. It fails at "Audit production dependencies" on advisories in @modelcontextprotocol/sdk, follow-redirects, markdown-it and vite. This PR has no dependency changes, and a separate fix is already open (ridho/fix-security-fast-pnpm-audit-advisories). That check will probably need a rebase onto boon after the fix lands.
  • cubic's threads are answered with reasons, or fixed in 498d7d3.

Non-blocking notes

  • callAgentWithUsageAttribution moves attributed sessions_send and a2a steps from the socket path to in-process dispatch with a synthetic WRITE_SCOPE client. Spawn already does this, and the comment says the client shape is the same. Check on canary that provenance and inputProvenance handling match the socket path.
  • If there is no in-process gateway context, or the message is queued into an active run, a sessions_send from user B falls back to the target's persisted attribution (the spawn-time user). This is documented in the code, but usage reports can still mis-attribute that case.
  • Not yet run against a live gateway, per the PR description. Please verify on canary that the child's x-boon-user-id, -name, -source and the OBO header reach the LLM gateway for spawn, announce and completion turns before wide rollout.

@william-antoline-boon
william-antoline-boon merged commit 9be13f4 into boon Oct 22, 2026
144 of 145 checks passed
@william-antoline-boon
william-antoline-boon deleted the william/eng-21618-subagent-usage-attribution branch October 22, 2026 05:41
convyusboy added a commit that referenced this pull request Oct 26, 2026
* chore(release): bump version 2026.6.35-boon.1 -> 2026.6.35-boon.2

Cuts the fork release covering the 9 PRs merged to boon since v2026.6.35-boon.1:
#298 (ENG-21265) stuck-generation cut-off retry + sub-agent spawn backstop,
#299 workspaceDir threading through outbound channel media adapters,
#304 (ENG-19208) real inbound media path in the system prompt,
#301 (ENG-21585) forced sessions_yield on narrated sub-agent turns,
#305 message-tool source reply observed outside message_tool_only,
#306 (ENG-19103) reasoning-stream callbacks honor message_tool_only,
#303 sub-agent/completion-turn usage attribution,
#302 clears 7 high-severity pnpm-audit-prod advisories,
#300 (ENG-21588) boon-estimation trajectory label detail.

Bump root package.json/npm-shrinkwrap.json, all bundled plugin manifests, and
the version + openclaw peer floor in every extension npm-shrinkwrap.json. Add
the 2026.6.35-boon.2 CHANGELOG.md section. No additional dependency or source
changes beyond what already shipped in #299 and #302.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* docs(changelog): fold PR #309's advisory fixes into the 2026.6.35-boon.2 entry

---------

Co-authored-by: Claude Sonnet 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.

2 participants