-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(core): implement fork subagent for context sharing #2936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wenshao
wants to merge
3
commits into
main
Choose a base branch
from
feat/fork-subagent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Fork Subagent Design | ||
|
|
||
| > Implicit fork subagent that inherits the parent's full conversation context and shares prompt cache for cost-efficient parallel task execution. | ||
|
|
||
| ## Overview | ||
|
|
||
| When the Agent tool is called without `subagent_type`, it triggers an implicit **fork** — a background subagent that inherits the parent's conversation history, system prompt, and tool definitions. The fork uses `CacheSafeParams` to ensure its API requests share the same prefix as the parent's, enabling DashScope prompt cache hits. | ||
|
|
||
| ## Architecture | ||
|
|
||
| ``` | ||
| Parent conversation: [SystemPrompt | Tools | Msg1 | Msg2 | ... | MsgN (model)] | ||
| ↑ identical prefix for all forks ↑ | ||
|
|
||
| Fork A: [...MsgN | placeholder results | "Research A"] ← shared cache | ||
| Fork B: [...MsgN | placeholder results | "Modify B"] ← shared cache | ||
| Fork C: [...MsgN | placeholder results | "Test C"] ← shared cache | ||
| ``` | ||
|
|
||
| ## Key Components | ||
|
|
||
| ### 1. FORK_AGENT (`forkSubagent.ts`) | ||
|
|
||
| Synthetic agent config, not registered in `builtInAgents`. Has a fallback `systemPrompt` but in practice uses the parent's rendered system prompt via `generationConfigOverride`. | ||
|
|
||
| ### 2. CacheSafeParams Integration (`agent.ts` + `forkedQuery.ts`) | ||
|
|
||
| ``` | ||
| agent.ts (fork path) | ||
| │ | ||
| ├── getCacheSafeParams() ← parent's generationConfig snapshot | ||
| │ ├── generationConfig ← systemInstruction + tools + temp/topP | ||
| │ └── history ← (not used — we build extraHistory instead) | ||
| │ | ||
| ├── forkGenerationConfig ← passed as generationConfigOverride | ||
| └── forkToolsOverride ← FunctionDeclaration[] extracted from tools | ||
| │ | ||
| ▼ | ||
| AgentHeadless.execute(context, signal, { | ||
| extraHistory, ← parent conversation history | ||
| generationConfigOverride, ← parent's exact systemInstruction + tools | ||
| toolsOverride, ← parent's exact tool declarations | ||
| }) | ||
| │ | ||
| ▼ | ||
| AgentCore.createChat(context, { | ||
| extraHistory, | ||
| generationConfigOverride, ← bypasses buildChatSystemPrompt() | ||
| }) AND skips getInitialChatHistory() | ||
| │ (extraHistory already has env context) | ||
| ▼ | ||
| new GeminiChat(config, generationConfig, startHistory) | ||
| ↑ byte-identical to parent's config | ||
| ``` | ||
|
|
||
| ### 3. History Construction (`agent.ts` + `forkSubagent.ts`) | ||
|
|
||
| The fork's `extraHistory` must end with a model message to maintain Gemini API's user/model alternation when `agent-headless` sends the `task_prompt`. | ||
|
|
||
| Three cases: | ||
|
|
||
| | Parent history ends with | extraHistory construction | task_prompt | | ||
| | ----------------------------- | ---------------------------------------------------------------------- | ------------------------------ | | ||
| | `model` (no function calls) | `[...rawHistory]` (unchanged) | `buildChildMessage(directive)` | | ||
| | `model` (with function calls) | `[...rawHistory, model(clone), user(responses+directive), model(ack)]` | `'Begin.'` | | ||
| | `user` (unusual) | `rawHistory.slice(0, -1)` (drop trailing user) | `buildChildMessage(directive)` | | ||
|
|
||
| ### 4. Recursive Fork Prevention (`forkSubagent.ts`) | ||
|
|
||
| `isInForkChild()` scans conversation history for the `<fork-boilerplate>` tag. If found, the fork attempt is rejected with an error message. | ||
|
|
||
| ### 5. Background Execution (`agent.ts`) | ||
|
|
||
| Fork uses `void executeSubagent()` (fire-and-forget) and returns `FORK_PLACEHOLDER_RESULT` immediately to the parent. Errors in the background task are caught, logged, and reflected in the display state. | ||
|
|
||
| ## Data Flow | ||
|
|
||
| ``` | ||
| 1. Model calls Agent tool (no subagent_type) | ||
| 2. agent.ts: import forkSubagent.js | ||
| 3. agent.ts: getCacheSafeParams() → forkGenerationConfig + forkToolsOverride | ||
| 4. agent.ts: build extraHistory from parent's getHistory(true) | ||
| 5. agent.ts: build forkTaskPrompt (directive or 'Begin.') | ||
| 6. agent.ts: createAgentHeadless(FORK_AGENT, ...) | ||
| 7. agent.ts: void executeSubagent() — background | ||
| 8. agent.ts: return FORK_PLACEHOLDER_RESULT to parent immediately | ||
| 9. Background: | ||
| a. AgentHeadless.execute(context, signal, {extraHistory, generationConfigOverride, toolsOverride}) | ||
| b. AgentCore.createChat() — uses parent's generationConfig (cache-shared) | ||
| c. runReasoningLoop() — uses parent's tool declarations | ||
| d. Fork executes tools, produces result | ||
| e. updateDisplay() with final status | ||
| ``` | ||
|
|
||
| ## Graceful Degradation | ||
|
|
||
| If `getCacheSafeParams()` returns null (first turn, no history yet), the fork falls back to: | ||
|
|
||
| - `FORK_AGENT.systemPrompt` for system instruction | ||
| - `prepareTools()` for tool declarations | ||
|
|
||
| This ensures the fork always works, even without cache sharing. | ||
|
|
||
| ## Files | ||
|
|
||
| | File | Role | | ||
| | ---------------------------------------------------- | ------------------------------------------------------------------------------------- | | ||
| | `packages/core/src/agents/runtime/forkSubagent.ts` | FORK_AGENT config, buildForkedMessages(), isInForkChild(), buildChildMessage() | | ||
| | `packages/core/src/tools/agent.ts` | Fork path: CacheSafeParams retrieval, extraHistory construction, background execution | | ||
| | `packages/core/src/agents/runtime/agent-headless.ts` | execute() options: generationConfigOverride, toolsOverride | | ||
| | `packages/core/src/agents/runtime/agent-core.ts` | CreateChatOptions.generationConfigOverride | | ||
| | `packages/core/src/followup/forkedQuery.ts` | CacheSafeParams infrastructure (existing, no changes) | |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import type { Content } from '@google/genai'; | ||
|
|
||
| export const FORK_SUBAGENT_TYPE = 'fork'; | ||
|
|
||
| export const FORK_BOILERPLATE_TAG = 'fork-boilerplate'; | ||
| export const FORK_DIRECTIVE_PREFIX = 'Directive: '; | ||
|
|
||
| export const FORK_AGENT = { | ||
| name: FORK_SUBAGENT_TYPE, | ||
| description: | ||
| 'Implicit fork — inherits full conversation context. Not selectable via subagent_type; triggered by omitting subagent_type.', | ||
| tools: ['*'], | ||
| systemPrompt: | ||
| 'You are a forked worker process. Follow the directive in the conversation history. Execute tasks directly using available tools. Do not spawn sub-agents.', | ||
| level: 'session' as const, | ||
| }; | ||
|
|
||
| export function isInForkChild(messages: Content[]): boolean { | ||
| return messages.some((m) => { | ||
| if (m.role !== 'user') return false; | ||
| return m.parts?.some( | ||
| (part) => part.text && part.text.includes(`<${FORK_BOILERPLATE_TAG}>`), | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| export const FORK_PLACEHOLDER_RESULT = | ||
| 'Fork started — processing in background'; | ||
|
|
||
| /** | ||
| * Build extra history messages for a forked subagent. | ||
| * | ||
| * When the last model message has function calls, we must include matching | ||
| * function responses in a user message (Gemini API requirement). The | ||
| * directive is embedded in this same user message to avoid consecutive | ||
| * user messages. | ||
| * | ||
| * When there are no function calls, we return [] — the parent history | ||
| * already ends with a model text message and the directive will be sent | ||
| * as the task_prompt by agent-headless (model → user alternation is OK). | ||
| * | ||
| * @param directive - The fork directive text (user's prompt) | ||
| * @param assistantMessage - The last model message from the parent history | ||
| * @returns Extra messages to append to history (may be empty) | ||
| */ | ||
| export function buildForkedMessages( | ||
| directive: string, | ||
| assistantMessage: Content, | ||
| ): Content[] { | ||
| const toolUseParts = | ||
| assistantMessage.parts?.filter((part) => part.functionCall) || []; | ||
|
|
||
| if (toolUseParts.length === 0) { | ||
| // No function calls — no extra messages needed. | ||
| // The parent history already ends with this model message. | ||
| return []; | ||
| } | ||
|
|
||
| // Clone the assistant message to avoid mutating the original | ||
| const fullAssistantMessage: Content = { | ||
| role: assistantMessage.role, | ||
| parts: [...(assistantMessage.parts || [])], | ||
| }; | ||
|
|
||
| // Build tool_result blocks for every tool_use, all with identical placeholder text. | ||
| // Include the directive text in the same user message to maintain | ||
| // proper user/model alternation. | ||
| const toolResultParts = toolUseParts.map((part) => ({ | ||
| functionResponse: { | ||
| id: part.functionCall!.id, | ||
| name: part.functionCall!.name, | ||
| response: { output: FORK_PLACEHOLDER_RESULT }, | ||
| }, | ||
| })); | ||
|
|
||
| const toolResultMessage: Content = { | ||
| role: 'user', | ||
| parts: [ | ||
| ...toolResultParts, | ||
| { | ||
| text: buildChildMessage(directive), | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| return [fullAssistantMessage, toolResultMessage]; | ||
| } | ||
|
|
||
| export function buildChildMessage(directive: string): string { | ||
| return `<${FORK_BOILERPLATE_TAG}> | ||
| STOP. READ THIS FIRST. | ||
|
|
||
| You are a forked worker process. You are NOT the main agent. | ||
|
|
||
| RULES (non-negotiable): | ||
| 1. You ARE the fork. Do NOT spawn sub-agents; execute directly. | ||
| 2. Do NOT converse, ask questions, or suggest next steps | ||
| 3. Do NOT editorialize or add meta-commentary | ||
| 4. USE your tools directly: Bash, Read, Write, etc. | ||
| 5. If you modify files, commit your changes before reporting. Include the commit hash in your report. | ||
| 6. Do NOT emit text between tool calls. Use tools silently, then report once at the end. | ||
| 7. Stay strictly within your directive's scope. If you discover related systems outside your scope, mention them in one sentence at most — other workers cover those areas. | ||
| 8. Keep your report under 500 words unless the directive specifies otherwise. Be factual and concise. | ||
| 9. Your response MUST begin with "Scope:". No preamble, no thinking-out-loud. | ||
| 10. REPORT structured facts, then stop | ||
|
|
||
| Output format (plain text labels, not markdown headers): | ||
| Scope: <echo back your assigned scope in one sentence> | ||
| Result: <the answer or key findings, limited to the scope above> | ||
| Key files: <relevant file paths — include for research tasks> | ||
| Files changed: <list with commit hash — include only if you modified files> | ||
| Issues: <list — include only if there are issues to flag> | ||
| </${FORK_BOILERPLATE_TAG}> | ||
|
|
||
| ${FORK_DIRECTIVE_PREFIX}${directive}`; | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.