@@ -52,7 +52,7 @@ import {
5252 type UnavailableToolDescriber ,
5353} from './toolExecutor' ;
5454import { ToolCallStarted , ToolProgress , ToolResultEvent } from './toolExecutorEvents' ;
55- import { ToolScheduler } from './toolScheduler' ;
55+ import { ToolScheduler , type OutstandingEffect } from './toolScheduler' ;
5656
5757const ABORT_GRACE_MS = 2_000 ;
5858const TOOL_OUTPUT_EMPTY = 'Tool output is empty.' ;
@@ -71,12 +71,15 @@ export interface ToolExecutionTask {
7171export interface ToolExecutionRunResult {
7272 readonly result : ToolResult ;
7373 readonly outcome : ToolExecutionOutcome ;
74+ readonly cancelled ?: boolean ;
75+ readonly effectsSettled ?: Promise < void > ;
7476}
7577
7678interface TimedToolResult {
7779 readonly index : number ;
7880 readonly result : ToolResult ;
7981 readonly outcome : ToolExecutionOutcome ;
82+ readonly cancelled : boolean ;
8083 readonly durationMs : number ;
8184}
8285
@@ -111,6 +114,7 @@ export const toolExecutorDupTypeTurnIdKey = defineState<number | undefined>(
111114export class AgentToolExecutorService implements IAgentToolExecutorService {
112115 declare readonly _serviceBrand : undefined ;
113116
117+ private readonly outstandingEffects = new Set < OutstandingEffect > ( ) ;
114118 private readonly beforeExecuteEmitter = new BeforeToolExecuteEmitter ( ) ;
115119 readonly onBeforeExecuteTool : Event < BeforeToolExecuteEvent > = this . beforeExecuteEmitter . event ;
116120 private readonly willExecuteEmitter = new AsyncEmitter < WillExecuteToolEvent > ( ) ;
@@ -302,7 +306,13 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
302306 ) ;
303307
304308 this . dispatchToolResult ( call , finalized , options ) ;
305- this . trackToolCall ( call , finalized , timedResult . durationMs , options ) ;
309+ this . trackToolCall (
310+ call ,
311+ finalized ,
312+ timedResult . durationMs ,
313+ options ,
314+ timedResult . cancelled || timedResult . outcome === 'aborted' ,
315+ ) ;
306316
307317 return {
308318 toolCallId : call . toolCall . id ,
@@ -316,8 +326,9 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
316326 result : ToolResult ,
317327 durationMs : number ,
318328 options : ToolExecutorExecuteOptions ,
329+ cancelled : boolean ,
319330 ) : void {
320- const outcome = toolTelemetryOutcome ( result ) ;
331+ const outcome = toolTelemetryOutcome ( result , cancelled ) ;
321332 const toolCallId = call . toolCall . id ;
322333 const dupType = this . toolCallDupTypes . get ( toolCallId ) ?? 'normal' ;
323334 this . toolCallDupTypes . delete ( toolCallId ) ;
@@ -385,6 +396,10 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
385396 return settleError ( call . args , call . output , 'preflight-rejected' ) ;
386397 }
387398
399+ if ( options . signal . aborted ) {
400+ return settleError ( call . args , abortedToolOutput ( call . toolName , options . signal ) , 'aborted' ) ;
401+ }
402+
388403 let execution : ToolExecution ;
389404 try {
390405 execution = await call . tool . resolveExecution ( call . args ) ;
@@ -458,7 +473,7 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
458473 tasks : ToolExecutionTask [ ] ,
459474 signal : AbortSignal ,
460475 ) : AsyncIterable < TimedToolResult > {
461- const scheduler = new ToolScheduler < TimedToolResult > ( ) ;
476+ const scheduler = new ToolScheduler < TimedToolResult > ( this . outstandingEffects ) ;
462477 const allResults : Array < Promise < TimedToolResult > > = [ ] ;
463478 const pendingResults = new Map < number , Promise < SettledTimedToolResult > > ( ) ;
464479
@@ -468,13 +483,24 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
468483 accesses : task . accesses ,
469484 start : async ( ) => {
470485 const startedAt = Date . now ( ) ;
486+ const run = task . execute ( signal ) ;
471487 return {
472- result : task . execute ( signal ) . then ( ( { result, outcome } ) => ( {
473- index,
474- result,
475- outcome,
476- durationMs : Math . max ( 0 , Date . now ( ) - startedAt ) ,
477- } ) ) ,
488+ result : run . then ( ( { result, outcome, cancelled, effectsSettled } ) => {
489+ if ( effectsSettled !== undefined ) {
490+ this . trackOutstandingEffect ( task . accesses , effectsSettled ) ;
491+ }
492+ return {
493+ index,
494+ result,
495+ outcome,
496+ cancelled : cancelled === true ,
497+ durationMs : Math . max ( 0 , Date . now ( ) - startedAt ) ,
498+ } ;
499+ } ) ,
500+ effectsSettled : run . then (
501+ ( value ) => value . effectsSettled ,
502+ ( ) => undefined ,
503+ ) ,
478504 } ;
479505 } ,
480506 } ) ;
@@ -501,6 +527,14 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
501527 }
502528 }
503529
530+ private trackOutstandingEffect ( accesses : ToolAccesses , settled : Promise < void > ) : void {
531+ const effect : OutstandingEffect = { accesses, settled } ;
532+ this . outstandingEffects . add ( effect ) ;
533+ void settled . finally ( ( ) => {
534+ this . outstandingEffects . delete ( effect ) ;
535+ } ) ;
536+ }
537+
504538 private async runSingleExecution (
505539 call : RunnableToolCall ,
506540 execution : RunnableToolExecution ,
@@ -516,12 +550,14 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
516550 abortedToolOutput ( call . toolName , signal ) ,
517551 ) . result ,
518552 outcome : 'aborted' ,
553+ cancelled : true ,
519554 } ;
520555 }
521556
522557 let rawResult : ExecutableToolResult ;
558+ let executePromise : Promise < ExecutableToolResult > ;
523559 try {
524- const executePromise = execution . execute ( {
560+ executePromise = execution . execute ( {
525561 turnId : options . turnId ,
526562 toolCallId : call . toolCall . id ,
527563 trace : options . trace ,
@@ -532,7 +568,20 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
532568 this . dispatchToolProgress ( call , update , options ) ;
533569 } ,
534570 } ) ;
535- rawResult = await raceWithAbortGrace ( executePromise , signal , call . toolName ) ;
571+ const raced = await raceWithAbortGrace ( executePromise , signal ) ;
572+ if ( raced . graceExpired ) {
573+ return {
574+ result : makeErrorToolResult ( call , call . args , abortedToolOutput ( call . toolName , signal ) )
575+ . result ,
576+ outcome : 'executed' ,
577+ cancelled : true ,
578+ effectsSettled : executePromise . then (
579+ ( ) => undefined ,
580+ ( ) => undefined ,
581+ ) ,
582+ } ;
583+ }
584+ rawResult = raced . value ;
536585 } catch ( error ) {
537586 const aborted = isAbortError ( error ) || signal . aborted ;
538587 const output = aborted
@@ -541,6 +590,7 @@ export class AgentToolExecutorService implements IAgentToolExecutorService {
541590 return {
542591 result : makeErrorToolResult ( call , call . args , output ) . result ,
543592 outcome : 'executed' ,
593+ cancelled : aborted ,
544594 } ;
545595 }
546596
@@ -904,28 +954,19 @@ function normalizeToolResult(result: ExecutableToolResult): ToolResult {
904954 return base ;
905955}
906956
907- function toolTelemetryOutcome ( result : ToolResult ) : 'success' | 'error' | 'cancelled' {
908- if ( result . isError !== true ) return 'success' ;
909- const text = toolOutputText ( result . output ) . toLowerCase ( ) ;
910- return text . includes ( 'aborted' ) ||
911- text . includes ( 'cancelled' ) ||
912- text . includes ( 'manually interrupted' )
913- ? 'cancelled'
914- : 'error' ;
957+ function toolTelemetryOutcome (
958+ result : ToolResult ,
959+ cancelled : boolean ,
960+ ) : 'success' | 'error' | 'cancelled' {
961+ if ( cancelled ) return 'cancelled' ;
962+ return result . isError === true ? 'error' : 'success' ;
915963}
916964
917965function toolTelemetryErrorType ( outcome : 'success' | 'error' | 'cancelled' ) : 'cancelled' | 'error' {
918966 if ( outcome === 'cancelled' ) return 'cancelled' ;
919967 return 'error' ;
920968}
921969
922- function toolOutputText ( output : ToolResult [ 'output' ] ) : string {
923- if ( typeof output === 'string' ) return output ;
924- return output
925- . filter ( ( part ) : part is Extract < ContentPart , { type : 'text' } > => part . type === 'text' )
926- . map ( ( part ) => part . text )
927- . join ( '' ) ;
928- }
929970
930971function isMediaContentPart ( part : ContentPart ) : boolean {
931972 return part . type === 'image_url' || part . type === 'audio_url' || part . type === 'video_url' ;
@@ -938,21 +979,21 @@ function abortedToolOutput(toolName: string, signal: AbortSignal): string {
938979 return `Tool "${ toolName } " was aborted` ;
939980}
940981
982+ type AbortGraceOutcome < Result > =
983+ | { readonly graceExpired : false ; readonly value : Result }
984+ | { readonly graceExpired : true } ;
985+
941986async function raceWithAbortGrace < Result > (
942987 executePromise : Promise < Result > ,
943988 signal : AbortSignal ,
944- toolName : string ,
945- ) : Promise < Result > {
989+ ) : Promise < AbortGraceOutcome < Result > > {
946990 let graceTimer : ReturnType < typeof setTimeout > | undefined ;
947991 let onAbort : ( ( ) => void ) | undefined ;
948992
949- const graceSentinel : Promise < Result > = new Promise ( ( resolve ) => {
993+ const graceSentinel : Promise < AbortGraceOutcome < Result > > = new Promise ( ( resolve ) => {
950994 const armTimer = ( ) : void => {
951995 graceTimer = setTimeout ( ( ) => {
952- resolve ( {
953- output : abortedToolOutput ( toolName , signal ) ,
954- isError : true ,
955- } as unknown as Result ) ;
996+ resolve ( { graceExpired : true } ) ;
956997 } , ABORT_GRACE_MS ) ;
957998 } ;
958999 if ( signal . aborted ) {
@@ -964,7 +1005,10 @@ async function raceWithAbortGrace<Result>(
9641005 } ) ;
9651006
9661007 try {
967- return await Promise . race ( [ executePromise , graceSentinel ] ) ;
1008+ return await Promise . race ( [
1009+ executePromise . then ( ( value ) => ( { graceExpired : false , value } ) as const ) ,
1010+ graceSentinel ,
1011+ ] ) ;
9681012 } finally {
9691013 if ( graceTimer !== undefined ) clearTimeout ( graceTimer ) ;
9701014 if ( onAbort !== undefined ) {
0 commit comments