@@ -8,6 +8,7 @@ import { TRANSCRIPT_NOT_CONTINUABLE_ERROR_CODE, TranscriptNotContinuableError }
88import {
99 attachInternalSyncSteeringGetter ,
1010 attachInternalToolBatchLifecycle ,
11+ attachInternalToolExecutionPreparer ,
1112 setInternalBeforeToolBatch ,
1213 takeInternalToolBatchLifecycle ,
1314} from "./internal-hooks.js" ;
@@ -1123,7 +1124,9 @@ describe("agentLoop tool termination", () => {
11231124
11241125 expect ( firstExecute ) . toHaveBeenCalledOnce ( ) ;
11251126 expect ( secondExecute ) . not . toHaveBeenCalled ( ) ;
1126- expect ( commitReadyCalls ) . toHaveBeenCalledExactlyOnceWith ( [ "call-first" ] ) ;
1127+ expect ( commitReadyCalls ) . toHaveBeenCalledExactlyOnceWith ( [
1128+ { toolCallId : "call-first" , args : { } } ,
1129+ ] ) ;
11271130 expect ( releaseSkippedCalls ) . toHaveBeenCalledWith ( [ "call-second" ] ) ;
11281131 expect ( requestMessages ) . toHaveLength ( 3 ) ;
11291132 expect ( agent . state . messages . slice ( 1 , 5 ) ) . toMatchObject ( [
@@ -1241,6 +1244,183 @@ describe("agentLoop tool termination", () => {
12411244 expect ( publicGetter ) . not . toHaveBeenCalled ( ) ;
12421245 } ) ;
12431246
1247+ it ( "suppresses a tool when steering arrives during private execution preflight" , async ( ) => {
1248+ const preflightStarted = createDeferred ( ) ;
1249+ const releasePreflight = createDeferred ( ) ;
1250+ const execute = vi . fn ( async ( ) => ( { content : [ ] , details : { executed : true } } ) ) ;
1251+ const dispose = vi . fn ( ) ;
1252+ const tool = attachInternalToolExecutionPreparer (
1253+ { ...makeTool ( "delayed" , [ ] ) , execute } ,
1254+ async ( ) => {
1255+ preflightStarted . resolve ( ) ;
1256+ await releasePreflight . promise ;
1257+ const finalArgs = { rewritten : true } ;
1258+ return {
1259+ kind : "ready" ,
1260+ args : finalArgs ,
1261+ execute : async ( onImplementationStart ) => {
1262+ onImplementationStart ?.( ) ;
1263+ return await execute ( ) ;
1264+ } ,
1265+ dispose,
1266+ } ;
1267+ } ,
1268+ ) ;
1269+ const requestMessages : Message [ ] [ ] = [ ] ;
1270+ const afterToolOutcome = vi . fn ( async ( ) => undefined ) ;
1271+ const commitReadyCalls = vi . fn ( ) ;
1272+ const releaseSkippedCalls = vi . fn ( ) ;
1273+ const agent = new Agent ( {
1274+ initialState : { model, tools : [ tool ] } ,
1275+ streamFn : createTurnSequenceStream (
1276+ [
1277+ [ { type : "toolCall" , id : "delayed-call" , name : "delayed" , arguments : { } } ] ,
1278+ [ { type : "text" , text : "redirected" } ] ,
1279+ ] ,
1280+ requestMessages ,
1281+ ) ,
1282+ toolExecution : "sequential" ,
1283+ afterToolOutcome,
1284+ } ) ;
1285+ setInternalBeforeToolBatch ( agent , async ( ) =>
1286+ attachInternalToolBatchLifecycle ( { } , { commitReadyCalls, releaseSkippedCalls } ) ,
1287+ ) ;
1288+ const steer = { role : "user" as const , content : "redirect" , timestamp : 2 } ;
1289+
1290+ const run = agent . prompt ( "start" ) ;
1291+ await preflightStarted . promise ;
1292+ agent . steer ( steer ) ;
1293+ releasePreflight . resolve ( ) ;
1294+ await run ;
1295+
1296+ expect ( execute ) . not . toHaveBeenCalled ( ) ;
1297+ expect ( commitReadyCalls ) . not . toHaveBeenCalled ( ) ;
1298+ expect ( dispose ) . toHaveBeenCalledOnce ( ) ;
1299+ expect ( requestMessages [ 1 ] ?. slice ( - 3 ) ) . toMatchObject ( [
1300+ { role : "assistant" , stopReason : "toolUse" } ,
1301+ {
1302+ role : "toolResult" ,
1303+ toolCallId : "delayed-call" ,
1304+ isError : true ,
1305+ details : { status : "skipped" , deniedReason : "steering" } ,
1306+ } ,
1307+ steer ,
1308+ ] ) ;
1309+ expect ( afterToolOutcome ) . toHaveBeenCalledWith (
1310+ expect . objectContaining ( {
1311+ toolCall : expect . objectContaining ( { id : "delayed-call" } ) ,
1312+ args : { rewritten : true } ,
1313+ executionStarted : false ,
1314+ } ) ,
1315+ expect . any ( AbortSignal ) ,
1316+ ) ;
1317+ } ) ;
1318+
1319+ it . each ( [ "sequential" , "parallel" ] as const ) (
1320+ "uses private final args for %s launch facts and hooks" ,
1321+ async ( toolExecution ) => {
1322+ const finalArgs = { rewritten : true } ;
1323+ const execute = vi . fn ( async ( ) => ( { content : [ ] , details : { executed : true } } ) ) ;
1324+ const tool = attachInternalToolExecutionPreparer (
1325+ { ...makeTool ( "rewritten" , [ ] ) , execute } ,
1326+ async ( ) => ( {
1327+ kind : "ready" ,
1328+ args : finalArgs ,
1329+ execute : async ( start ) => {
1330+ start ?.( ) ;
1331+ return await execute ( ) ;
1332+ } ,
1333+ dispose : vi . fn ( ) ,
1334+ } ) ,
1335+ ) ;
1336+ const afterToolCall = vi . fn ( async ( ) => undefined ) ;
1337+ const afterToolOutcome = vi . fn ( async ( ) => undefined ) ;
1338+ const commitReadyCalls = vi . fn ( ) ;
1339+ const agent = new Agent ( {
1340+ initialState : { model, tools : [ tool ] } ,
1341+ streamFn : createTurnSequenceStream (
1342+ [
1343+ [ { type : "toolCall" , id : "rewritten-call" , name : "rewritten" , arguments : { } } ] ,
1344+ [ { type : "text" , text : "done" } ] ,
1345+ ] ,
1346+ [ ] ,
1347+ ) ,
1348+ toolExecution,
1349+ afterToolCall,
1350+ afterToolOutcome,
1351+ } ) ;
1352+ setInternalBeforeToolBatch ( agent , async ( ) =>
1353+ attachInternalToolBatchLifecycle (
1354+ { } ,
1355+ {
1356+ commitReadyCalls,
1357+ releaseSkippedCalls : vi . fn ( ) ,
1358+ } ,
1359+ ) ,
1360+ ) ;
1361+
1362+ await agent . prompt ( "start" ) ;
1363+
1364+ expect ( commitReadyCalls ) . toHaveBeenCalledExactlyOnceWith ( [
1365+ { toolCallId : "rewritten-call" , args : finalArgs } ,
1366+ ] ) ;
1367+ expect ( afterToolCall ) . toHaveBeenCalledWith (
1368+ expect . objectContaining ( { args : finalArgs } ) ,
1369+ expect . any ( AbortSignal ) ,
1370+ ) ;
1371+ expect ( afterToolOutcome ) . toHaveBeenCalledWith (
1372+ expect . objectContaining ( { args : finalArgs , executionStarted : true } ) ,
1373+ expect . any ( AbortSignal ) ,
1374+ ) ;
1375+ expect ( execute ) . toHaveBeenCalledOnce ( ) ;
1376+ expect (
1377+ agent . state . messages . find (
1378+ ( message ) => message . role === "assistant" && message . stopReason === "toolUse" ,
1379+ ) ,
1380+ ) . toMatchObject ( {
1381+ content : [ expect . objectContaining ( { id : "rewritten-call" , arguments : { } } ) ] ,
1382+ } ) ;
1383+ } ,
1384+ ) ;
1385+
1386+ it ( "disposes private preflight when the steering checkpoint throws" , async ( ) => {
1387+ const execute = vi . fn ( async ( ) => ( { content : [ ] , details : { } } ) ) ;
1388+ const dispose = vi . fn ( ) ;
1389+ const tool = attachInternalToolExecutionPreparer (
1390+ { ...makeTool ( "cleanup" , [ ] ) , execute } ,
1391+ async ( { args } ) => ( {
1392+ kind : "ready" ,
1393+ args,
1394+ execute : async ( onImplementationStart ) => {
1395+ onImplementationStart ?.( ) ;
1396+ return await execute ( ) ;
1397+ } ,
1398+ dispose,
1399+ } ) ,
1400+ ) ;
1401+ const getSteeringMessages = vi
1402+ . fn < ( ) => Promise < AgentMessage [ ] > > ( )
1403+ . mockResolvedValueOnce ( [ ] )
1404+ . mockResolvedValueOnce ( [ ] )
1405+ . mockRejectedValueOnce ( new Error ( "steering checkpoint failed" ) ) ;
1406+
1407+ await expect (
1408+ runAgentLoop (
1409+ [ { role : "user" , content : "start" , timestamp : 1 } ] ,
1410+ { systemPrompt : "" , messages : [ ] , tools : [ tool ] } ,
1411+ { ...config , toolExecution : "sequential" , getSteeringMessages } ,
1412+ ( ) => { } ,
1413+ undefined ,
1414+ createTurnSequenceStream (
1415+ [ [ { type : "toolCall" , id : "cleanup-call" , name : "cleanup" , arguments : { } } ] ] ,
1416+ [ ] ,
1417+ ) ,
1418+ ) ,
1419+ ) . rejects . toThrow ( "steering checkpoint failed" ) ;
1420+ expect ( execute ) . not . toHaveBeenCalled ( ) ;
1421+ expect ( dispose ) . toHaveBeenCalledOnce ( ) ;
1422+ } ) ;
1423+
12441424 it ( "delivers async steering between tools before shouldStopAfterTurn" , async ( ) => {
12451425 const steer = { role : "user" as const , content : "keep going" , timestamp : 2 } ;
12461426 const queued : AgentMessage [ ] = [ ] ;
@@ -1491,7 +1671,7 @@ describe("agentLoop tool termination", () => {
14911671 ] ) ;
14921672 } ) ;
14931673
1494- it ( "commits a prepared parallel batch in assistant order before launch" , async ( ) => {
1674+ it ( "commits prepared parallel calls in assistant order at launch" , async ( ) => {
14951675 const order : string [ ] = [ ] ;
14961676 const requestMessages : Message [ ] [ ] = [ ] ;
14971677 const streamFn = createTurnSequenceStream (
@@ -1504,8 +1684,8 @@ describe("agentLoop tool termination", () => {
15041684 ] ,
15051685 requestMessages ,
15061686 ) ;
1507- const commitReadyCalls = vi . fn ( ( ids : readonly string [ ] ) => {
1508- order . push ( `commit:${ ids . join ( "," ) } ` ) ;
1687+ const commitReadyCalls = vi . fn ( ( calls : readonly { toolCallId : string ; args : unknown } [ ] ) => {
1688+ order . push ( `commit:${ calls . map ( ( call ) => call . toolCallId ) . join ( "," ) } ` ) ;
15091689 } ) ;
15101690 const releaseSkippedCalls = vi . fn ( ) ;
15111691
@@ -1519,13 +1699,17 @@ describe("agentLoop tool termination", () => {
15191699 ...makeTool ( "first" , [ ] ) ,
15201700 execute : async ( ) => {
15211701 order . push ( "execute:parallel-first" ) ;
1702+ await Promise . resolve ( ) ;
1703+ order . push ( "gap:parallel-first" ) ;
15221704 return { content : [ ] , details : { } } ;
15231705 } ,
15241706 } ,
15251707 {
15261708 ...makeTool ( "second" , [ ] ) ,
15271709 execute : async ( ) => {
15281710 order . push ( "execute:parallel-second" ) ;
1711+ await Promise . resolve ( ) ;
1712+ order . push ( "gap:parallel-second" ) ;
15291713 return { content : [ ] , details : { } } ;
15301714 } ,
15311715 } ,
@@ -1543,9 +1727,12 @@ describe("agentLoop tool termination", () => {
15431727 ) ;
15441728
15451729 expect ( order ) . toEqual ( [
1546- "commit:parallel-first,parallel-second " ,
1730+ "commit:parallel-first" ,
15471731 "execute:parallel-first" ,
1732+ "commit:parallel-second" ,
15481733 "execute:parallel-second" ,
1734+ "gap:parallel-first" ,
1735+ "gap:parallel-second" ,
15491736 ] ) ;
15501737 expect ( releaseSkippedCalls ) . not . toHaveBeenCalled ( ) ;
15511738 } ) ;
0 commit comments