fix(ai): keep subagent run ids out of tool results #1623
main fromrefs/pull/1623/head·merged by AlemTuzlak View on GitHubA subagent that the parent model starts as a tool sent its internal child id (subagentRunId) back to the parent model inside the tool result. This PR removes that id from the result content. The tool result is now only the child's output or error. The child id stays in the subagent tracking events.
🎯 Changes#
- Remove
subagentRunIdfrom the synthetic subagent tool result, for success and for error. - Document that child run tracking stays in subagent events.
- Add unit tests for a successful and a failed child run.
- Add an E2E check that the parent model request has only
{"result":"..."}in the tool message.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested code changes locally with
pnpm run test:pr, or these tests do not apply to this pull request. - I fully understand the code in this pull request, including any code generated with AI assistance.
- Docs: I updated
docs/for this change, or this change is not user-facing. - Changeset: I added a changeset (
pnpm changeset), or this PR does not change a published package.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Root cause#
Issue. In the no-router flow, the parent model calls a subagent as a tool. The tool result contains the internal child id subagentRunId. The parent model and the TOOL_CALL_RESULT event both get it.
Cause. executeServerTool in packages/ai/src/activities/chat/tools/tool-calls.ts builds the result as { subagentRunId, result } or { subagentRunId, error }.
Fix. The result is now { result } or { error }. The SUBAGENT_* events and the child chunks still carry the id. Resume and reconstruct read the id from events and parts, not from the tool result.
Possible alternatives#
- Move the id to another result field. The subagent events already carry the child id, and the tool call id links the result to its call. Another field only duplicates tracking data.
- Keep the id in the content. This sends internal tracking data to the model and mixes it with the child output.
Testing#
- Commands run.
@tanstack/aiunit tests (vitest run --maxWorkers=2): 121 files, 2052 tests passed.@tanstack/aitest:types,test:oxlint,build,test:build: pass.oxfmt --checkon the changed files: pass.testing/e2e/tests/subagents.spec.tstype check: no errors in the spec. The E2E suite was not run locally. CI runs it.pnpm test:prwas not run locally. CI runs it.
- Manual test.
- On
main, make a parentchat()withsubagents: { agents: [child] }and no router. Let the model call the child tool. - Look at the tool message in the next parent model request. On
mainit is{"subagentRunId":"...","result":"..."}. - On this branch, do the same steps. The tool message is
{"result":"..."}.
- On
- How this PR makes testing easy.
packages/ai/tests/subagent-tool-result.test.tsfails onmainsource (2 of 2) and passes on this branch.testing/e2e/tests/subagents.spec.tschecks the real parent request through the aimock journal.
Maintainer repro (a separate test, not the one in this PR). On main source (cf90e418):
TOOL_CALL_RESULT content: {"subagentRunId":"subagent-1791382430104-49yi3uy","result":"child says hi"}
parent model tool message: {"subagentRunId":"subagent-1791382430104-49yi3uy","result":"child says hi"}
AssertionError: expected { …(2) } to not have property "subagentRunId"
Tests 1 failed (1)
On this branch:
TOOL_CALL_RESULT content: {"result":"child says hi"}
parent model tool message: {"result":"child says hi"}
✓ repro #1623: parent model does not see subagentRunId in the child tool result
Tests 1 passed (1)
Risk / rollback#
Low risk. This removes an internal field from the synthetic tool result content. Code that read subagentRunId from that content must read it from the SUBAGENT_* events. Revert this PR to restore the old content.
Public API change#
Tool result content for a subagent tool, as the parent model and TOOL_CALL_RESULT see it.
Before
{ "subagentRunId": "...", "result": "..." }
After
{ "result": "..." }- 1
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
- Configuration used: Repository: TanStack/ai/.coderabbit.yaml
- Review profile: CHILL
- Plan: Advanced
- Run ID:
3c53c2a7-7811-4df5-9181-90e1a17bd7b5
📥 Commits
Reviewing files that changed from the base of the PR and between 7fb4a5f7f827636304243d68c515e5f08ecff8ac and 09306fed8869fef86467e03f99a4489b9c35a845.
📒 Files selected for processing (5)
.changeset/proud-keys-result.mddocs/chat/subagents.mdpackages/ai/src/activities/chat/tools/tool-calls.tspackages/ai/tests/subagent-tool-result.test.tstesting/e2e/tests/subagents.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
📝 Walkthrough
Walkthrough#
Subagent tool results now contain only the child’s output or error. Tests check the emitted tool result and model tool message. Documentation distinguishes this content from the
subagentRunIdin tracking events.Changes#
Subagent tool result content
Layer / File(s) Summary Set and verify subagent tool result content
packages/ai/src/activities/chat/tools/tool-calls.ts,packages/ai/tests/subagent-tool-result.test.ts,testing/e2e/tests/subagents.spec.ts,docs/chat/subagents.md,.changeset/proud-keys-result.mdSubagent results contain only the child output or error. Unit and end-to-end tests check the emitted result and model tool message. Documentation describes the separation from subagentRunId, and a patch changeset records the update.Priority: ⬇️ Low
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix
Suggested reviewers:
alemtuzlakMerge Risk: ⚪ Minimal · up to
09306Subagent tool results now omit internal run IDs while tracking events retain them. No actionable merge risk is identified beyond normal checks.
Security Architecture Review#
Security architecture risk: ⚪ Minimal · up to
09306This reduces exposure of internal identifiers without changing permissions, approvals, or recovery behavior. No material security risk was identified in the changed behavior.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
- inferred — The effective change affects consumers of synthetic subagent result envelopes through the existing chat execution path. Inspection found no accompanying expansion of invocation authority or production reachability. The two path-specific public-entrypoint signals resolve to test-only helpers and request-journal assertions.
Trust Boundaries and Controls
- observed — Approval bindings use approvalId, toolCallId, and interrupt metadata rather than subagentRunId in result content. Child interrupts retain their child ID, and tool-result events and messages retain toolCallId. These controls remain independent of the removed payload field.
Resilience and Maintainability Implications
- observed — An interrupted child still leaves its tool call open without emitting a completion result. Resume selects suspended state by parentToolCallId and reuses the stored child ID. Abort linkage and child iterator cleanup remain in finally blocks. These recovery paths are unchanged by the result-envelope projection.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
Check name Status Explanation Resolution Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. (2 skipped: 2 … Write docstrings for the functions missing them to satisfy the coverage threshold. ✅ Passed checks (4 passed)
Check name Status Explanation Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request. Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request. Title check ✅ Passed The title clearly and concisely describes the main change: removing subagent run IDs from tool results. Description check ✅ Passed The description is complete and relevant. It explains the change, root cause, alternatives, testing status, risk, rollback, public API impact, documentation, and changeset. Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 3 files. (2 skipped: 2 unsupported.)
- Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
- Create a new PR
- Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment `@coderabbitai help` to get the list of available commands.github-actions[bot](GitHub profile, opens in a new tab) assignedAlemTuzlak
Thanks for the PR, @duohelingdukele! 🙌 @AlemTuzlak will take a look.
Automated pre-review checks
- ✅ CI passing
- ✅ No merge conflicts
- ✅ Changeset present
- ✅ E2E test changes included
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
View your CI Pipeline Execution ↗ for commit ec11d1e56c81dac0673be3b0b1fa8713d773ae1d
Command Status Duration Result nx run-many --targets=build --exclude=examples/...✅ Succeeded 1m 29s View ↗
☁️ Nx Cloud last updated this comment at
2026-10-07 14:49:31UTC@tanstack/ai
npm i https://pkg.pr.new/@tanstack/ai@1623@tanstack/ai-acp
npm i https://pkg.pr.new/@tanstack/ai-acp@1623@tanstack/ai-angular
npm i https://pkg.pr.new/@tanstack/ai-angular@1623@tanstack/ai-anthropic
npm i https://pkg.pr.new/@tanstack/ai-anthropic@1623@tanstack/ai-bedrock
npm i https://pkg.pr.new/@tanstack/ai-bedrock@1623@tanstack/ai-byteplus
npm i https://pkg.pr.new/@tanstack/ai-byteplus@1623@tanstack/ai-claude-code
npm i https://pkg.pr.new/@tanstack/ai-claude-code@1623@tanstack/ai-client
npm i https://pkg.pr.new/@tanstack/ai-client@1623@tanstack/ai-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-cloudflare@1623@tanstack/ai-code-mode
npm i https://pkg.pr.new/@tanstack/ai-code-mode@1623@tanstack/ai-code-mode-snippets
npm i https://pkg.pr.new/@tanstack/ai-code-mode-snippets@1623@tanstack/ai-codex
npm i https://pkg.pr.new/@tanstack/ai-codex@1623@tanstack/ai-cohere
npm i https://pkg.pr.new/@tanstack/ai-cohere@1623@tanstack/ai-compaction
npm i https://pkg.pr.new/@tanstack/ai-compaction@1623@tanstack/ai-devtools-core
npm i https://pkg.pr.new/@tanstack/ai-devtools-core@1623@tanstack/ai-durable-stream
npm i https://pkg.pr.new/@tanstack/ai-durable-stream@1623@tanstack/ai-elevenlabs
npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@1623@tanstack/ai-event-client
npm i https://pkg.pr.new/@tanstack/ai-event-client@1623@tanstack/ai-fal
npm i https://pkg.pr.new/@tanstack/ai-fal@1623@tanstack/ai-gemini
npm i https://pkg.pr.new/@tanstack/ai-gemini@1623@tanstack/ai-grok
npm i https://pkg.pr.new/@tanstack/ai-grok@1623@tanstack/ai-grok-build
npm i https://pkg.pr.new/@tanstack/ai-grok-build@1623@tanstack/ai-groq
npm i https://pkg.pr.new/@tanstack/ai-groq@1623@tanstack/ai-isolate-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@1623@tanstack/ai-isolate-daytona
npm i https://pkg.pr.new/@tanstack/ai-isolate-daytona@1623@tanstack/ai-isolate-e2b
npm i https://pkg.pr.new/@tanstack/ai-isolate-e2b@1623@tanstack/ai-isolate-node
npm i https://pkg.pr.new/@tanstack/ai-isolate-node@1623@tanstack/ai-isolate-quickjs
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@1623@tanstack/ai-isolate-quickjs-bun
npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs-bun@1623@tanstack/ai-llmgateway
npm i https://pkg.pr.new/@tanstack/ai-llmgateway@1623@tanstack/ai-lovable
npm i https://pkg.pr.new/@tanstack/ai-lovable@1623@tanstack/ai-mcp
npm i https://pkg.pr.new/@tanstack/ai-mcp@1623@tanstack/ai-memory
npm i https://pkg.pr.new/@tanstack/ai-memory@1623@tanstack/ai-mistral
npm i https://pkg.pr.new/@tanstack/ai-mistral@1623@tanstack/ai-octane
npm i https://pkg.pr.new/@tanstack/ai-octane@1623@tanstack/ai-ollama
npm i https://pkg.pr.new/@tanstack/ai-ollama@1623@tanstack/ai-ollaya
npm i https://pkg.pr.new/@tanstack/ai-ollaya@1623@tanstack/ai-openai
npm i https://pkg.pr.new/@tanstack/ai-openai@1623@tanstack/ai-opencode
npm i https://pkg.pr.new/@tanstack/ai-opencode@1623@tanstack/ai-openrouter
npm i https://pkg.pr.new/@tanstack/ai-openrouter@1623@tanstack/ai-perplexity
npm i https://pkg.pr.new/@tanstack/ai-perplexity@1623@tanstack/ai-persistence
npm i https://pkg.pr.new/@tanstack/ai-persistence@1623@tanstack/ai-preact
npm i https://pkg.pr.new/@tanstack/ai-preact@1623@tanstack/ai-react
npm i https://pkg.pr.new/@tanstack/ai-react@1623@tanstack/ai-react-ui
npm i https://pkg.pr.new/@tanstack/ai-react-ui@1623@tanstack/ai-reactor
npm i https://pkg.pr.new/@tanstack/ai-reactor@1623@tanstack/ai-remix
npm i https://pkg.pr.new/@tanstack/ai-remix@1623@tanstack/ai-sandbox
npm i https://pkg.pr.new/@tanstack/ai-sandbox@1623@tanstack/ai-sandbox-blaxel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-blaxel@1623@tanstack/ai-sandbox-boxd
npm i https://pkg.pr.new/@tanstack/ai-sandbox-boxd@1623@tanstack/ai-sandbox-cloudflare
npm i https://pkg.pr.new/@tanstack/ai-sandbox-cloudflare@1623@tanstack/ai-sandbox-daytona
npm i https://pkg.pr.new/@tanstack/ai-sandbox-daytona@1623@tanstack/ai-sandbox-docker
npm i https://pkg.pr.new/@tanstack/ai-sandbox-docker@1623@tanstack/ai-sandbox-e2b
npm i https://pkg.pr.new/@tanstack/ai-sandbox-e2b@1623@tanstack/ai-sandbox-local-process
npm i https://pkg.pr.new/@tanstack/ai-sandbox-local-process@1623@tanstack/ai-sandbox-railway
npm i https://pkg.pr.new/@tanstack/ai-sandbox-railway@1623@tanstack/ai-sandbox-sprites
npm i https://pkg.pr.new/@tanstack/ai-sandbox-sprites@1623@tanstack/ai-sandbox-upstash-box
npm i https://pkg.pr.new/@tanstack/ai-sandbox-upstash-box@1623@tanstack/ai-sandbox-vercel
npm i https://pkg.pr.new/@tanstack/ai-sandbox-vercel@1623@tanstack/ai-skills
npm i https://pkg.pr.new/@tanstack/ai-skills@1623@tanstack/ai-solid
npm i https://pkg.pr.new/@tanstack/ai-solid@1623@tanstack/ai-solid-ui
npm i https://pkg.pr.new/@tanstack/ai-solid-ui@1623@tanstack/ai-svelte
npm i https://pkg.pr.new/@tanstack/ai-svelte@1623@tanstack/ai-typesafe
npm i https://pkg.pr.new/@tanstack/ai-typesafe@1623@tanstack/ai-utils
npm i https://pkg.pr.new/@tanstack/ai-utils@1623@tanstack/ai-vercel-gateway
npm i https://pkg.pr.new/@tanstack/ai-vercel-gateway@1623@tanstack/ai-vertex
npm i https://pkg.pr.new/@tanstack/ai-vertex@1623@tanstack/ai-vue
npm i https://pkg.pr.new/@tanstack/ai-vue@1623@tanstack/ai-vue-ui
npm i https://pkg.pr.new/@tanstack/ai-vue-ui@1623@tanstack/ai-worldlabs
npm i https://pkg.pr.new/@tanstack/ai-worldlabs@1623@tanstack/openai-base
npm i https://pkg.pr.new/@tanstack/openai-base@1623@tanstack/preact-ai-devtools
npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@1623@tanstack/react-ai-devtools
npm i https://pkg.pr.new/@tanstack/react-ai-devtools@1623@tanstack/solid-ai-devtools
npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@1623@tanstack/svelte-ai-devtools
npm i https://pkg.pr.new/@tanstack/svelte-ai-devtools@1623commit:
ec11d1eReview findings#
Thank you for this fix, @duohelingdukele. I checked it with our bug-fix review steps. The result: the bug is real, the fix works, and the diff is the smallest fix. I found nothing to change in the code.
1. Security#
Clean. The diff changes one source file, one unit test, one E2E spec, one docs sentence, and one changeset. It does not change
package.json, the lockfile, scripts, or CI.2. Root cause#
- Issue. A subagent that the parent model starts as a tool (the no-router flow) returns its tool result to the parent model. That result contains the internal child id
subagentRunId. The model and theTOOL_CALL_RESULTevent both get this id. - Cause.
executeServerToolinpackages/ai/src/activities/chat/tools/tool-calls.tsbuilds the result as{ subagentRunId, result }or{ subagentRunId, error }. - Fix. The result is now only
{ result }or{ error }. The child id stays on theSUBAGENT_*events and on the child chunks. I checked the resume and reconstruct paths (turn.ts,spawn.ts,processor.ts,chat-client.ts). They read the id from events and parts, not from the tool result content.
3. Repro (my own test, not the test in this PR)#
I wrote a separate test. A child agent yields text directly, and the parent model calls it as a tool. The test checks the tool message in the second parent model request.
On
mainsource (cf90e418), it fails:TOOL_CALL_RESULT content: {"subagentRunId":"subagent-1791382430104-49yi3uy","result":"child says hi"} parent model tool message: {"subagentRunId":"subagent-1791382430104-49yi3uy","result":"child says hi"} AssertionError: expected { …(2) } to not have property "subagentRunId" Tests 1 failed (1)On this PR, it passes:
TOOL_CALL_RESULT content: {"result":"child says hi"} parent model tool message: {"result":"child says hi"} ✓ repro #1623: parent model does not see subagentRunId in the child tool result Tests 1 passed (1)The PR test
tests/subagent-tool-result.test.tsalso fails onmainsource (2 of 2 failed) and passes on this branch. It is a good regression test, so I keep it as it is.4. Keep check (is every change needed?)#
tool-calls.ts: the fix. It is two lines. I do not see a smaller fix.subagent-tool-result.test.ts: the regression test. It covers success and error.subagents.spec.ts: E2E check on the real parent request through aimock. It uses the same journal pattern as other specs.docs/chat/subagents.md: one sentence that describes the correct result content. That is allowed for a behavior fix..changeset/proud-keys-result.md: patch for@tanstack/ai.
No extra changes.
5. CodeRabbit and reviewers#
- CodeRabbit: no findings about defects. The "docstring coverage" warning is a style nit. I will not apply it.
- No human review comments.
6. Checks I ran on this branch#
@tanstack/aiunit tests: 121 files, 2052 tests passed.@tanstack/aitest:types,test:oxlint,build,test:build: all pass.oxfmt --checkon the changed files: pass.subagents.spec.tstype check: no errors in that file. I did not run the E2E suite locally. CI runs it.
7. What I will change#
I merged the latest
maininto this branch and pushed that merge (ec11d1e5). I will not change the code. I will update the PR description with the root cause and both repro results.- Issue. A subagent that the parent model starts as a tool (the no-router flow) returns its tool result to the parent model. That result contains the internal child id
What I pushed#
ec11d1e5: merge of the latestmain(cf90e418) into this branch. There were no conflicts.
I did not push code changes. The fix, the unit test, the E2E check, the docs sentence, and the changeset are all correct as they are.
I also updated the PR description with the root cause and both repro results. CI must run again on the merge commit. Then a maintainer can review for merge.
AlemTuzlak approved these changes
AlemTuzlak merged thisfb55bcb
AlemTuzlak closed this
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: ec11d1e56c81dac0673be3b0b1fa8713d773ae1d
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR