@@ -14,6 +14,7 @@ import {
1414 getToolTerminalPresentation ,
1515 setToolTerminalPresentation ,
1616} from "./tool-terminal-presentation.js" ;
17+ import { createSessionsYieldTool } from "./tools/sessions-yield-tool.js" ;
1718
1819type ExecuteMock = ReturnType < typeof vi . fn > ;
1920
@@ -84,6 +85,157 @@ describe("wrapToolWithAbortSignal", () => {
8485 await flushMicrotasks ( ) ;
8586 } ) ;
8687
88+ it ( "preserves the successful result when sessions_yield intentionally aborts its own run" , async ( ) => {
89+ const runAbort = new AbortController ( ) ;
90+ const handoffReason = { code : "sessions_yield" , turnHandoff : true } as const ;
91+ const beforeYield = vi . fn ( ) ;
92+ const wrapped = wrapToolWithAbortSignal (
93+ createSessionsYieldTool ( {
94+ sessionId : "requester" ,
95+ onBeforeYield : beforeYield ,
96+ onYield : ( ) => {
97+ runAbort . abort ( handoffReason ) ;
98+ } ,
99+ } ) ,
100+ runAbort . signal ,
101+ ) ;
102+
103+ await expect ( wrapped . execute ( "call-yield" , { } ) ) . resolves . toMatchObject ( {
104+ details : { status : "yielded" , message : "Turn yielded." } ,
105+ } ) ;
106+ expect ( beforeYield ) . toHaveBeenCalledOnce ( ) ;
107+ expect ( runAbort . signal . reason ) . toBe ( handoffReason ) ;
108+ } ) ;
109+
110+ it ( "still aborts a concurrent sibling when sessions_yield hands off the run" , async ( ) => {
111+ const runAbort = new AbortController ( ) ;
112+ const handoffReason = { code : "sessions_yield" , turnHandoff : true } as const ;
113+ const sibling = wrapToolWithAbortSignal (
114+ asAgentTool ( { name : "wedged" , execute : vi . fn ( ( ) => new Promise < never > ( ( ) => { } ) ) } ) ,
115+ runAbort . signal ,
116+ ) ;
117+ const siblingAborted = expect ( sibling . execute ( "call-sibling" , { } ) ) . rejects . toMatchObject ( {
118+ name : "AbortError" ,
119+ message : "Aborted" ,
120+ } ) ;
121+ const yieldTool = wrapToolWithAbortSignal (
122+ createSessionsYieldTool ( {
123+ sessionId : "requester" ,
124+ onYield : ( ) => {
125+ runAbort . abort ( handoffReason ) ;
126+ } ,
127+ } ) ,
128+ runAbort . signal ,
129+ ) ;
130+
131+ await expect ( yieldTool . execute ( "call-yield" , { } ) ) . resolves . toMatchObject ( {
132+ details : { status : "yielded" } ,
133+ } ) ;
134+ await siblingAborted ;
135+ } ) ;
136+
137+ it ( "preserves the handoff when distinct run and per-call signals both yield" , async ( ) => {
138+ const runAbort = new AbortController ( ) ;
139+ const callAbort = new AbortController ( ) ;
140+ const handoffReason = { code : "sessions_yield" , turnHandoff : true } as const ;
141+ const wrapped = wrapToolWithAbortSignal (
142+ createSessionsYieldTool ( {
143+ sessionId : "requester" ,
144+ onYield : ( ) => {
145+ runAbort . abort ( handoffReason ) ;
146+ callAbort . abort ( handoffReason ) ;
147+ } ,
148+ } ) ,
149+ runAbort . signal ,
150+ ) ;
151+
152+ await expect ( wrapped . execute ( "call-yield" , { } , callAbort . signal ) ) . resolves . toMatchObject ( {
153+ details : { status : "yielded" } ,
154+ } ) ;
155+ expect ( runAbort . signal . reason ) . toBe ( handoffReason ) ;
156+ expect ( callAbort . signal . reason ) . toBe ( handoffReason ) ;
157+ } ) ;
158+
159+ it . each ( [
160+ { name : "ordinary caller cancellation" , reason : new Error ( "operator cancelled" ) } ,
161+ {
162+ name : "a caller-authored lookalike handoff" ,
163+ reason : { code : "sessions_yield" , turnHandoff : true } ,
164+ } ,
165+ ] ) ( "rejects sessions_yield for $name without an owner-authored handoff" , async ( { reason } ) => {
166+ const runAbort = new AbortController ( ) ;
167+ const callAbort = new AbortController ( ) ;
168+ const execute = vi . fn ( ( ) => new Promise < never > ( ( ) => { } ) ) ;
169+ const wrapped = wrapToolWithAbortSignal (
170+ asAgentTool ( { name : "sessions_yield" , execute } ) ,
171+ runAbort . signal ,
172+ ) ;
173+
174+ const executePromise = wrapped . execute ( "call-yield" , { } , callAbort . signal ) ;
175+ callAbort . abort ( reason ) ;
176+
177+ await expect ( executePromise ) . rejects . toMatchObject ( {
178+ name : "AbortError" ,
179+ message : "Aborted" ,
180+ } ) ;
181+ expect ( runAbort . signal . aborted ) . toBe ( false ) ;
182+ } ) ;
183+
184+ it . each ( [
185+ { name : "ordinary cancellation" , reason : new Error ( "operator cancelled" ) } ,
186+ { name : "a missing handoff flag" , reason : { code : "sessions_yield" } } ,
187+ { name : "a disabled handoff flag" , reason : { code : "sessions_yield" , turnHandoff : false } } ,
188+ { name : "a different handoff owner" , reason : { code : "different" , turnHandoff : true } } ,
189+ ] ) ( "rejects sessions_yield when its run owner aborts with $name" , async ( { reason } ) => {
190+ const runAbort = new AbortController ( ) ;
191+ const execute = vi . fn ( async ( ) => {
192+ runAbort . abort ( reason ) ;
193+ return textResult ( "late" ) ;
194+ } ) ;
195+ const wrapped = wrapToolWithAbortSignal (
196+ asAgentTool ( { name : "sessions_yield" , execute } ) ,
197+ runAbort . signal ,
198+ ) ;
199+
200+ await expect ( wrapped . execute ( "call-yield" , { } ) ) . rejects . toMatchObject ( {
201+ name : "AbortError" ,
202+ message : "Aborted" ,
203+ } ) ;
204+ } ) ;
205+
206+ it ( "does not start sessions_yield when the run was already handed off" , async ( ) => {
207+ const runAbort = new AbortController ( ) ;
208+ runAbort . abort ( { code : "sessions_yield" , turnHandoff : true } ) ;
209+ const onYield = vi . fn ( ) ;
210+ const wrapped = wrapToolWithAbortSignal (
211+ createSessionsYieldTool ( { sessionId : "requester" , onYield } ) ,
212+ runAbort . signal ,
213+ ) ;
214+
215+ await expect ( wrapped . execute ( "call-yield" , { } ) ) . rejects . toMatchObject ( {
216+ name : "AbortError" ,
217+ message : "Aborted" ,
218+ } ) ;
219+ expect ( onYield ) . not . toHaveBeenCalled ( ) ;
220+ } ) ;
221+
222+ it ( "preserves an actual sessions_yield failure after its owner starts the handoff" , async ( ) => {
223+ const runAbort = new AbortController ( ) ;
224+ const yieldError = new Error ( "yield bookkeeping failed" ) ;
225+ const wrapped = wrapToolWithAbortSignal (
226+ createSessionsYieldTool ( {
227+ sessionId : "requester" ,
228+ onYield : ( ) => {
229+ runAbort . abort ( { code : "sessions_yield" , turnHandoff : true } ) ;
230+ throw yieldError ;
231+ } ,
232+ } ) ,
233+ runAbort . signal ,
234+ ) ;
235+
236+ await expect ( wrapped . execute ( "call-yield" , { } ) ) . rejects . toBe ( yieldError ) ;
237+ } ) ;
238+
87239 it ( "rejects with AbortError when the per-call signal aborts through the combined signal" , async ( ) => {
88240 const runAbort = new AbortController ( ) ;
89241 const callAbort = new AbortController ( ) ;
0 commit comments