11import { beforeEach , describe , expect , it , vi } from "vitest" ;
2+ import {
3+ createReplyOperation ,
4+ type ReplyOperation ,
5+ } from "../../../auto-reply/reply/reply-run-registry.js" ;
6+ import { isAgentRunRestartAbortReason } from "../../run-termination.js" ;
27import {
38 projectToolSearchTargetTranscriptMessages ,
49 type ToolSearchTargetTranscriptProjection ,
@@ -45,6 +50,10 @@ function prepareCatalogExecutor(
4550 runAbortController ?: AbortController ;
4651 sandboxSessionKey ?: string ;
4752 sessionKey ?: string ;
53+ replyOperation ?: ReplyOperation ;
54+ onAttemptAbort ?: ( ) => void ;
55+ abortRun ?: ( isTimeout ?: boolean , reason ?: unknown ) => void ;
56+ markExternalAbort ?: ( ) => void ;
4857 } ,
4958) {
5059 const runAbortController = options ?. runAbortController ?? new AbortController ( ) ;
@@ -53,6 +62,8 @@ function prepareCatalogExecutor(
5362 runId : "run-output-schema" ,
5463 sessionId : "session-output-schema" ,
5564 sessionKey : options ?. sessionKey ?? "agent:main:main" ,
65+ replyOperation : options ?. replyOperation ,
66+ onAttemptAbort : options ?. onAttemptAbort ,
5667 } as never ,
5768 activeSession : { agent : { } , isStreaming : false } as never ,
5869 hookRunner : undefined as never ,
@@ -62,8 +73,8 @@ function prepareCatalogExecutor(
6273 toolSearchTargetTranscriptProjections : projections ,
6374 isReplaySafeTool : ( ) => false ,
6475 runAbortController,
65- abortRun : vi . fn ( ) ,
66- markExternalAbort : vi . fn ( ) ,
76+ abortRun : options ?. abortRun ?? vi . fn ( ) ,
77+ markExternalAbort : options ?. markExternalAbort ?? vi . fn ( ) ,
6778 getRunState :
6879 options ?. getRunState ??
6980 ( ( ) => ( {
@@ -443,4 +454,56 @@ describe("prepareEmbeddedAttemptStream", () => {
443454 aborted = true ;
444455 expect ( prepared . queueHandle . isAborted ?.( ) ) . toBe ( true ) ;
445456 } ) ;
457+
458+ it ( "processes aliased cancel and abort through one external-abort sequence" , ( ) => {
459+ const markExternalAbort = vi . fn ( ) ;
460+ const onAttemptAbort = vi . fn ( ) ;
461+ const abortRun = vi . fn ( ) ;
462+ const prepared = prepareCatalogExecutor ( [ ] , {
463+ markExternalAbort,
464+ onAttemptAbort,
465+ abortRun,
466+ } ) ;
467+
468+ prepared . queueHandle . abort ( "restart" ) ;
469+ prepared . queueHandle . cancel ( "user_abort" ) ;
470+
471+ expect ( markExternalAbort ) . toHaveBeenCalledOnce ( ) ;
472+ expect ( onAttemptAbort ) . toHaveBeenCalledOnce ( ) ;
473+ expect ( abortRun ) . toHaveBeenCalledOnce ( ) ;
474+ expect ( abortRun . mock . calls [ 0 ] ?. [ 0 ] ) . toBe ( false ) ;
475+ expect ( isAgentRunRestartAbortReason ( abortRun . mock . calls [ 0 ] ?. [ 1 ] ) ) . toBe ( true ) ;
476+ } ) ;
477+
478+ it ( "latches external abort before reply cancellation can synchronously re-enter" , ( ) => {
479+ const operation = createReplyOperation ( {
480+ sessionKey : "agent:main:main" ,
481+ sessionId : "session-output-schema" ,
482+ resetTriggered : false ,
483+ } ) ;
484+ const markExternalAbort = vi . fn ( ) ;
485+ const abortRun = vi . fn ( ) ;
486+ const onAttemptAbort = vi . fn ( ( ) => {
487+ operation . abortByUser ( ) ;
488+ } ) ;
489+
490+ try {
491+ const prepared = prepareCatalogExecutor ( [ ] , {
492+ replyOperation : operation ,
493+ markExternalAbort,
494+ onAttemptAbort,
495+ abortRun,
496+ } ) ;
497+
498+ prepared . queueHandle . cancel ( "superseded" ) ;
499+ prepared . queueHandle . abort ( "restart" ) ;
500+
501+ expect ( markExternalAbort ) . toHaveBeenCalledOnce ( ) ;
502+ expect ( onAttemptAbort ) . toHaveBeenCalledOnce ( ) ;
503+ expect ( abortRun ) . toHaveBeenCalledOnce ( ) ;
504+ expect ( abortRun ) . toHaveBeenCalledWith ( false , undefined ) ;
505+ } finally {
506+ operation . complete ( ) ;
507+ }
508+ } ) ;
446509} ) ;
0 commit comments