@@ -127,6 +127,7 @@ export type SpawnSubagentParams = {
127127 model ?: string ;
128128 taskName ?: string ;
129129 thinking ?: string ;
130+ cwd ?: string ;
130131 runTimeoutSeconds ?: number ;
131132 thread ?: boolean ;
132133 mode ?: SpawnSubagentMode ;
@@ -809,6 +810,7 @@ export async function spawnSubagentDirect(
809810 } ;
810811 }
811812 const targetAgentId = requestedAgentId ? normalizeAgentId ( requestedAgentId ) : requesterAgentId ;
813+ const explicitWorkspaceDir = normalizeOptionalString ( params . cwd ) ;
812814 const requesterOrigin = normalizeDeliveryContext ( {
813815 channel : ctx . agentChannel ,
814816 accountId : ctx . agentAccountId ,
@@ -1035,9 +1037,24 @@ export async function spawnSubagentDirect(
10351037 | undefined ;
10361038 let attachmentAbsDir : string | undefined ;
10371039 let attachmentRootDir : string | undefined ;
1040+ const toolSpawnMetadata = mapToolContextToSpawnedRunMetadata ( {
1041+ agentGroupId : ctx . agentGroupId ,
1042+ agentGroupChannel : ctx . agentGroupChannel ,
1043+ agentGroupSpace : ctx . agentGroupSpace ,
1044+ workspaceDir : ctx . workspaceDir ,
1045+ } ) ;
1046+ const inheritedWorkspaceDir =
1047+ targetAgentId !== requesterAgentId ? undefined : toolSpawnMetadata . workspaceDir ;
1048+ const spawnedWorkspaceDir = resolveSpawnedWorkspaceInheritance ( {
1049+ config : cfg ,
1050+ targetAgentId,
1051+ explicitWorkspaceDir : explicitWorkspaceDir ?? inheritedWorkspaceDir ,
1052+ } ) ;
1053+
10381054 const materializedAttachments = await materializeSubagentAttachments ( {
10391055 config : cfg ,
10401056 targetAgentId,
1057+ workspaceDir : spawnedWorkspaceDir ,
10411058 attachments : params . attachments ,
10421059 mountPathHint,
10431060 } ) ;
@@ -1070,23 +1087,10 @@ export async function spawnSubagentDirect(
10701087 task,
10711088 } ) ;
10721089
1073- const toolSpawnMetadata = mapToolContextToSpawnedRunMetadata ( {
1074- agentGroupId : ctx . agentGroupId ,
1075- agentGroupChannel : ctx . agentGroupChannel ,
1076- agentGroupSpace : ctx . agentGroupSpace ,
1077- workspaceDir : ctx . workspaceDir ,
1078- } ) ;
10791090 const spawnedMetadata = normalizeSpawnedRunMetadata ( {
10801091 spawnedBy : spawnedByKey ,
10811092 ...toolSpawnMetadata ,
1082- workspaceDir : resolveSpawnedWorkspaceInheritance ( {
1083- config : cfg ,
1084- targetAgentId,
1085- // For cross-agent spawns, ignore the caller's inherited workspace;
1086- // let targetAgentId resolve the correct workspace instead.
1087- explicitWorkspaceDir :
1088- targetAgentId !== requesterAgentId ? undefined : toolSpawnMetadata . workspaceDir ,
1089- } ) ,
1093+ workspaceDir : spawnedWorkspaceDir ,
10901094 } ) ;
10911095 const spawnLineagePatchError = await patchChildSession ( {
10921096 spawnedBy : spawnedByKey ,
0 commit comments