@@ -168,7 +168,7 @@ describe("terminal tool", () => {
168168 spawn : async ( ) => backends . shift ( ) ?? makeBackend ( ) ,
169169 } ) ;
170170 const agentSessionKey = "agent:main:shared-task-session" ;
171- const lookupTaskByRunId = vi . fn ( async ( runId : string ) =>
171+ const lookupTaskByRunIdForChildSession = vi . fn ( async ( runId : string ) =>
172172 runId === "conversation-run"
173173 ? undefined
174174 : {
@@ -182,22 +182,62 @@ describe("terminal tool", () => {
182182 agentId : "main" ,
183183 agentSessionKey,
184184 runId,
185- lookupTaskByRunId ,
185+ lookupTaskByRunIdForChildSession ,
186186 getGatewayContext : ( ) => makeContext ( manager ) ,
187187 } ) ;
188188
189189 await createTaskTool ( "run-1" ) . execute ( "open" , { action : "open" , show : false } ) ;
190190 await createTaskTool ( "run-2" ) . execute ( "open" , { action : "open" , show : false } ) ;
191191 await createTaskTool ( "conversation-run" ) . execute ( "open" , { action : "open" , show : false } ) ;
192192
193- expect ( lookupTaskByRunId . mock . calls ) . toEqual ( [ [ "run-1" ] , [ "run-2" ] , [ "conversation-run" ] ] ) ;
193+ expect ( lookupTaskByRunIdForChildSession . mock . calls ) . toEqual ( [
194+ [ "run-1" , agentSessionKey ] ,
195+ [ "run-2" , agentSessionKey ] ,
196+ [ "conversation-run" , agentSessionKey ] ,
197+ ] ) ;
194198 expect ( manager . closeAgentSessions ( "task-1" ) ) . toBe ( 1 ) ;
195199 expect ( firstBackend . killed ) . toBe ( true ) ;
196200 expect ( secondBackend . killed ) . toBe ( false ) ;
197201 expect ( persistentBackend . killed ) . toBe ( false ) ;
198202 expect ( manager . listAgent ( agentSessionKey , "main" ) ) . toHaveLength ( 2 ) ;
199203 } ) ;
200204
205+ it ( "binds the matching child session when task run ids collide" , async ( ) => {
206+ const backend = makeBackend ( ) ;
207+ const manager = new TerminalSessionManager ( { emit : vi . fn ( ) , spawn : async ( ) => backend } ) ;
208+ const agentSessionKey = "agent:main:shared-run-task-2" ;
209+ const tasks = [
210+ {
211+ taskId : "task-1" ,
212+ status : "running" as const ,
213+ childSessionKey : "agent:main:shared-run-task-1" ,
214+ } ,
215+ {
216+ taskId : "task-2" ,
217+ status : "running" as const ,
218+ childSessionKey : agentSessionKey ,
219+ } ,
220+ ] ;
221+ const lookupTaskByRunIdForChildSession = vi . fn (
222+ async ( _runId : string , childSessionKey : string ) =>
223+ tasks . find ( ( task ) => task . childSessionKey === childSessionKey ) ,
224+ ) ;
225+ const tool = createTerminalTool ( {
226+ agentId : "main" ,
227+ agentSessionKey,
228+ runId : "shared-run" ,
229+ lookupTaskByRunIdForChildSession,
230+ getGatewayContext : ( ) => makeContext ( manager ) ,
231+ } ) ;
232+
233+ await tool . execute ( "open" , { action : "open" , show : false } ) ;
234+
235+ expect ( lookupTaskByRunIdForChildSession ) . toHaveBeenCalledWith ( "shared-run" , agentSessionKey ) ;
236+ expect ( manager . closeAgentSessions ( "task-2" ) ) . toBe ( 1 ) ;
237+ expect ( manager . closeAgentSessions ( "task-1" ) ) . toBe ( 0 ) ;
238+ expect ( backend . killed ) . toBe ( true ) ;
239+ } ) ;
240+
201241 it ( "maps a cron agent run to its detached task before terminal lookup" , async ( ) => {
202242 const backend = makeBackend ( ) ;
203243 const manager = new TerminalSessionManager ( { emit : vi . fn ( ) , spawn : async ( ) => backend } ) ;
@@ -212,7 +252,7 @@ describe("terminal tool", () => {
212252 throw new Error ( "expected cron agent run claim" ) ;
213253 }
214254 expect ( bindAgentRunTaskRunId ( "cron-agent-run" , claimId , "detached-task-run" ) ) . toBe ( true ) ;
215- const lookupTaskByRunId = vi . fn ( async ( ) => ( {
255+ const lookupTaskByRunIdForChildSession = vi . fn ( async ( ) => ( {
216256 taskId : "cron-task" ,
217257 status : "running" as const ,
218258 childSessionKey : agentSessionKey ,
@@ -221,14 +261,17 @@ describe("terminal tool", () => {
221261 agentId : "main" ,
222262 agentSessionKey,
223263 runId : "cron-agent-run" ,
224- lookupTaskByRunId ,
264+ lookupTaskByRunIdForChildSession ,
225265 getGatewayContext : ( ) => makeContext ( manager ) ,
226266 } ) ;
227267
228268 try {
229269 await tool . execute ( "open" , { action : "open" , show : false } ) ;
230270
231- expect ( lookupTaskByRunId ) . toHaveBeenCalledWith ( "detached-task-run" ) ;
271+ expect ( lookupTaskByRunIdForChildSession ) . toHaveBeenCalledWith (
272+ "detached-task-run" ,
273+ agentSessionKey ,
274+ ) ;
232275 expect ( manager . closeAgentSessions ( "cron-task" ) ) . toBe ( 1 ) ;
233276 expect ( backend . killed ) . toBe ( true ) ;
234277 } finally {
@@ -246,7 +289,7 @@ describe("terminal tool", () => {
246289 agentId : "main" ,
247290 agentSessionKey : "agent:main:completed-task" ,
248291 runId : "completed-run" ,
249- lookupTaskByRunId : vi . fn ( async ( ) => ( {
292+ lookupTaskByRunIdForChildSession : vi . fn ( async ( ) => ( {
250293 taskId : "task-completed" ,
251294 status : "succeeded" as const ,
252295 childSessionKey : "agent:main:completed-task" ,
0 commit comments