MCPcopy
hub / github.com/claude-code-best/claude-code / createSubagentContext

Function createSubagentContext

src/utils/forkedAgent.ts:351–472  ·  view source on GitHub ↗
(
  parentContext: ToolUseContext,
  overrides?: SubagentContextOverrides,
)

Source from the content-addressed store, hash-verified

349 * })
350 */
351export function createSubagentContext(
352 parentContext: ToolUseContext,
353 overrides?: SubagentContextOverrides,
354): ToolUseContext {
355 // Determine abortController: explicit override > share parent's > new child
356 const abortController =
357 overrides?.abortController ??
358 (overrides?.shareAbortController
359 ? parentContext.abortController
360 : createChildAbortController(parentContext.abortController))
361
362 // Determine getAppState - wrap to set shouldAvoidPermissionPrompts unless sharing abortController
363 // (if sharing abortController, it's an interactive agent that CAN show UI)
364 const getAppState: ToolUseContext['getAppState'] = overrides?.getAppState
365 ? overrides.getAppState
366 : overrides?.shareAbortController
367 ? parentContext.getAppState
368 : () => {
369 const state = parentContext.getAppState()
370 if (state.toolPermissionContext.shouldAvoidPermissionPrompts) {
371 return state
372 }
373 return {
374 ...state,
375 toolPermissionContext: {
376 ...state.toolPermissionContext,
377 shouldAvoidPermissionPrompts: true,
378 },
379 }
380 }
381
382 return {
383 // Preserve the parent Langfuse trace separately so nested side queries
384 // like auto_mode can attach to the main agent trace instead of the
385 // subagent's own trace.
386 langfuseRootTrace: parentContext.langfuseTrace,
387 // Mutable state - cloned by default to maintain isolation
388 // Clone overrides.readFileState if provided, otherwise clone from parent
389 readFileState: cloneFileStateCache(
390 overrides?.readFileState ?? parentContext.readFileState,
391 ),
392 nestedMemoryAttachmentTriggers: new Set<string>(),
393 loadedNestedMemoryPaths: new Set<string>(),
394 dynamicSkillDirTriggers: new Set<string>(),
395 // Per-subagent: tracks skills surfaced by discovery for was_discovered telemetry (SkillTool.ts:116)
396 discoveredSkillNames: new Set<string>(),
397 toolDecisions: undefined,
398 // Budget decisions: override > clone of parent > undefined (feature off).
399 //
400 // Clone by default (not fresh): cache-sharing forks process parent
401 // messages containing parent tool_use_ids. A fresh state would see
402 // them as unseen and make divergent replacement decisions → wire
403 // prefix differs → cache miss. A clone makes identical decisions →
404 // cache hit. For non-forking subagents the parent UUIDs never match
405 // — clone is a harmless no-op.
406 //
407 // Override: AgentTool resume (reconstructed from sidechain records)
408 // and inProcessRunner (per-teammate persistent loop state).

Callers 5

runAgentFunction · 0.85
runForkedAgentFunction · 0.85
sessionMemory.tsFile · 0.85

Calls 5

cloneFileStateCacheFunction · 0.85
createAgentIdFunction · 0.85

Tested by

no test coverage detected