@@ -2,6 +2,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
22
33const hoisted = vi . hoisted ( ( ) => ( {
44 runAgentEndSideEffects : vi . fn ( ) ,
5+ shouldWaitForCompletionRequiredAsyncTasks : vi . fn ( ( ) : boolean => false ) ,
6+ waitForCompletionRequiredAsyncTasks : vi . fn ( ) ,
57} ) ) ;
68
79vi . mock ( "../../harness/agent-end-side-effects.js" , ( ) => ( {
@@ -10,13 +12,19 @@ vi.mock("../../harness/agent-end-side-effects.js", () => ({
1012vi . mock ( "./agent-end-context.js" , ( ) => ( {
1113 buildEmbeddedAgentEndContext : ( ) => ( { } ) ,
1214} ) ) ;
15+ vi . mock ( "./attempt.async-tasks.js" , ( ) => ( {
16+ shouldWaitForCompletionRequiredAsyncTasks : hoisted . shouldWaitForCompletionRequiredAsyncTasks ,
17+ waitForCompletionRequiredAsyncTasks : hoisted . waitForCompletionRequiredAsyncTasks ,
18+ } ) ) ;
1319
1420import { completeEmbeddedAttemptAfterTurn } from "./attempt-after-turn.js" ;
1521import { settleEmbeddedAttemptStream } from "./attempt-stream-settle.js" ;
1622
1723describe ( "embedded attempt phase lifecycle state" , ( ) => {
1824 beforeEach ( ( ) => {
1925 hoisted . runAgentEndSideEffects . mockReset ( ) ;
26+ hoisted . shouldWaitForCompletionRequiredAsyncTasks . mockReset ( ) . mockReturnValue ( false ) ;
27+ hoisted . waitForCompletionRequiredAsyncTasks . mockReset ( ) ;
2028 } ) ;
2129
2230 it ( "re-reads compaction timeout state after the retry wait" , async ( ) => {
@@ -95,6 +103,135 @@ describe("embedded attempt phase lifecycle state", () => {
95103 expect ( removeTrailingEntries ) . toHaveBeenCalledOnce ( ) ;
96104 } ) ;
97105
106+ it ( "settles a user-aborted run whose async-task wait throws AbortError" , async ( ) => {
107+ const abortError = Object . assign ( new Error ( "This operation was aborted" ) , {
108+ name : "AbortError" ,
109+ } ) ;
110+ hoisted . shouldWaitForCompletionRequiredAsyncTasks . mockReturnValue ( true ) ;
111+ hoisted . waitForCompletionRequiredAsyncTasks
112+ . mockRejectedValueOnce ( abortError )
113+ . mockResolvedValueOnce ( { timedOutRunIds : [ "exec-run-1" ] } ) ;
114+ const messages : never [ ] = [ ] ;
115+ const sessionManager = {
116+ appendCustomEntry : vi . fn ( ) ,
117+ buildSessionContext : ( ) => ( { messages } ) ,
118+ getEntries : ( ) => [ ] ,
119+ removeTrailingEntries : vi . fn ( ( ) => 0 ) ,
120+ } ;
121+ const activeSession = {
122+ agent : { state : { messages } } ,
123+ isCompacting : false ,
124+ isStreaming : false ,
125+ messages,
126+ sessionId : "session-1" ,
127+ } ;
128+
129+ const result = await settleEmbeddedAttemptStream ( {
130+ attempt : {
131+ runId : "run-1" ,
132+ sessionId : "session-1" ,
133+ sessionKey : "agent:main:main" ,
134+ sessionFile : "/tmp/session.jsonl" ,
135+ provider : "test" ,
136+ modelId : "model" ,
137+ model : { api : "openai-responses" } ,
138+ } as never ,
139+ activeSession : activeSession as never ,
140+ sessionManager : sessionManager as never ,
141+ sessionLockController : { } as never ,
142+ withOwnedSessionWriteLock : async ( operation ) => await operation ( ) ,
143+ subscription : {
144+ toolMetas : [ { toolName : "exec" , asyncStarted : true } ] ,
145+ waitForCompactionRetry : async ( ) => { } ,
146+ isCompactionInFlight : ( ) => false ,
147+ getCompactionCount : ( ) => 0 ,
148+ getCurrentAttemptAssistant : ( ) => undefined ,
149+ getUsageTotals : ( ) => undefined ,
150+ getLastAssistantUsage : ( ) => undefined ,
151+ } as never ,
152+ state : {
153+ promptError : null ,
154+ promptErrorSource : null ,
155+ yieldAborted : false ,
156+ sessionIdUsed : "session-1" ,
157+ } ,
158+ readLifecycleState : ( ) => ( {
159+ aborted : true ,
160+ timedOut : false ,
161+ timedOutDuringCompaction : false ,
162+ } ) ,
163+ markTimedOutDuringCompaction : ( ) => { } ,
164+ runAbortDeadlineAtMs : Date . now ( ) + 60_000 ,
165+ runAbortSignal : AbortSignal . abort ( ) ,
166+ isProbeSession : true ,
167+ abortable : async ( promise ) => await promise ,
168+ prePromptMessageCount : 0 ,
169+ toolSearchTargetTranscriptProjections : [ ] ,
170+ cache : {
171+ observabilityEnabled : false ,
172+ changesForTurn : null ,
173+ retention : undefined ,
174+ } ,
175+ shouldFlushForContextEngine : false ,
176+ } ) ;
177+
178+ // The aborted run settles instead of unwinding the lane task, and its
179+ // unfinished async tasks are not reclassified as a timeout failure.
180+ expect ( result . promptError ) . toBeNull ( ) ;
181+ expect ( hoisted . waitForCompletionRequiredAsyncTasks ) . toHaveBeenCalledTimes ( 2 ) ;
182+ } ) ;
183+
184+ it ( "emits an abort-classified agent_end event when a teardown error races the abort" , async ( ) => {
185+ const abortError = Object . assign ( new Error ( "This operation was aborted" ) , {
186+ name : "AbortError" ,
187+ } ) ;
188+ await completeEmbeddedAttemptAfterTurn ( {
189+ attempt : {
190+ runId : "run-1" ,
191+ sessionId : "session-1" ,
192+ sessionFile : "/tmp/session.jsonl" ,
193+ } as never ,
194+ activeSession : { } as never ,
195+ sessionManager : { appendCustomEntry : vi . fn ( ) } as never ,
196+ sessionLockController : { } as never ,
197+ withOwnedSessionWriteLock : async ( operation ) => await operation ( ) ,
198+ state : {
199+ promptError : abortError ,
200+ yieldAborted : false ,
201+ sessionIdUsed : "session-1" ,
202+ messagesSnapshot : [ ] ,
203+ prePromptMessageCount : 0 ,
204+ contextEngineAfterTurnCheckpoint : null ,
205+ compactionOccurredThisAttempt : false ,
206+ } ,
207+ readLifecycleState : ( ) => ( {
208+ aborted : true ,
209+ timedOut : false ,
210+ idleTimedOut : false ,
211+ timedOutDuringCompaction : false ,
212+ } ) ,
213+ runtime : {
214+ effectiveWorkspace : "/tmp/workspace" ,
215+ agentDir : "/tmp/agent" ,
216+ sessionAgentId : "main" ,
217+ resolveActiveContextEnginePluginId : ( ) => undefined ,
218+ shouldRecordCompletedBootstrapTurn : false ,
219+ cacheTrace : null ,
220+ anthropicPayloadLogger : null ,
221+ hookAgentId : "main" ,
222+ diagnosticTrace : { traceId : "trace-1" , spanId : "span-1" } as never ,
223+ skillWorkshopAvailable : true ,
224+ hookRunner : null ,
225+ promptStartedAt : Date . now ( ) ,
226+ } ,
227+ } ) ;
228+
229+ expect ( hoisted . runAgentEndSideEffects ) . toHaveBeenCalledTimes ( 1 ) ;
230+ const event = hoisted . runAgentEndSideEffects . mock . calls [ 0 ] ?. [ 0 ] ?. event ;
231+ expect ( event ) . toMatchObject ( { success : false } ) ;
232+ expect ( event ?. error ) . toBeUndefined ( ) ;
233+ } ) ;
234+
98235 it ( "re-reads abort state inside the post-turn session write" , async ( ) => {
99236 let aborted = false ;
100237 await completeEmbeddedAttemptAfterTurn ( {
0 commit comments