@@ -7,10 +7,12 @@ import * as NetService from "@t3tools/shared/Net";
77import * as ConfigProvider from "effect/ConfigProvider" ;
88import * as DateTime from "effect/DateTime" ;
99import * as Effect from "effect/Effect" ;
10+ import * as Fiber from "effect/Fiber" ;
1011import * as FileSystem from "effect/FileSystem" ;
1112import * as Layer from "effect/Layer" ;
1213import * as Path from "effect/Path" ;
1314import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
15+ import * as TestClock from "effect/testing/TestClock" ;
1416import * as TestConsole from "effect/testing/TestConsole" ;
1517import { Command } from "effect/cli" ;
1618import { vi } from "vite-plus/test" ;
@@ -210,6 +212,133 @@ it.effect("--wait follows the sent run to its continuation's terminal status", (
210212 } ) ,
211213) ;
212214
215+ /** The reads that ask whether a run after `runId` holds a message someone sent. */
216+ const laterRunReads = (
217+ laterRun : RunId ,
218+ items : ReadonlyArray < { readonly runId : RunId ; readonly type : string } > ,
219+ ) => [
220+ {
221+ recentRuns : [
222+ { runId, ordinal : 1 } ,
223+ { runId : laterRun , ordinal : 2 } ,
224+ ] ,
225+ } ,
226+ { thread : { } , items, nextPosition : null , hasMore : false } ,
227+ ] ;
228+
229+ it . effect ( "--wait lasts until background work the agent waits on wakes it and that run ends" , ( ) =>
230+ Effect . gen ( function * ( ) {
231+ const wakeRun = RunId . make ( "run-2" ) ;
232+ const { tools, calls } = scriptedTools ( {
233+ t3_thread_wait : [
234+ { threadId, runId, status : "completed" , timedOut : false } ,
235+ {
236+ threadId,
237+ runId,
238+ status : "completed" ,
239+ timedOut : false ,
240+ resultRun : { runId : wakeRun , status : "completed" } ,
241+ } ,
242+ ] ,
243+ // The wake run starts from a notification, which the messages view leaves out.
244+ t3_thread_read : laterRunReads ( wakeRun , [ ] ) ,
245+ } ) ;
246+
247+ const outcome = yield * waitForRun ( tools , threadId , runId , true ) ;
248+
249+ assert . deepStrictEqual (
250+ calls . filter ( ( entry ) => entry . tool === "t3_thread_wait" ) . map ( ( entry ) => entry . args ) ,
251+ [
252+ { threadId, runId, timeoutMs : 15_000 } ,
253+ { threadId, runId, timeoutMs : 15_000 , untilDelegatedWorkSettled : true } ,
254+ ] ,
255+ ) ;
256+ assert . deepStrictEqual ( outcome , { type : "ended" , runId : wakeRun , status : "completed" } ) ;
257+ } ) ,
258+ ) ;
259+
260+ it . effect (
261+ "--wait waits out the moment between background work ending and its wake run starting" ,
262+ ( ) =>
263+ Effect . gen ( function * ( ) {
264+ const wakeRun = RunId . make ( "run-2" ) ;
265+ const ownOnly = { recentRuns : [ { runId, ordinal : 1 , status : "completed" } ] } ;
266+ const withWake = ( status : string ) => [
267+ {
268+ recentRuns : [
269+ { runId, ordinal : 1 , status : "completed" } ,
270+ { runId : wakeRun , ordinal : 2 , status } ,
271+ ] ,
272+ } ,
273+ { thread : { } , items : [ ] , nextPosition : null , hasMore : false } ,
274+ ] ;
275+ const { tools } = scriptedTools ( {
276+ t3_thread_wait : [
277+ { threadId, runId, status : "completed" , timedOut : false } ,
278+ { threadId, runId, status : "completed" , timedOut : true } ,
279+ // The work ended, and the run it wakes is not recorded yet.
280+ {
281+ threadId,
282+ runId,
283+ status : "completed" ,
284+ timedOut : false ,
285+ resultRun : { runId, status : "completed" } ,
286+ } ,
287+ {
288+ threadId,
289+ runId,
290+ status : "completed" ,
291+ timedOut : false ,
292+ resultRun : { runId : wakeRun , status : "completed" } ,
293+ } ,
294+ ] ,
295+ t3_thread_read : [
296+ ownOnly ,
297+ { thread : { pendingRequestCount : 0 } } ,
298+ ownOnly ,
299+ ...withWake ( "running" ) ,
300+ ...withWake ( "completed" ) ,
301+ ] ,
302+ } ) ;
303+
304+ const fiber = yield * Effect . forkChild ( waitForRun ( tools , threadId , runId , true ) ) ;
305+ yield * TestClock . adjust ( "500 millis" ) ;
306+
307+ assert . deepStrictEqual ( yield * Fiber . join ( fiber ) , {
308+ type : "ended" ,
309+ runId : wakeRun ,
310+ status : "completed" ,
311+ } ) ;
312+ } ) ,
313+ ) ;
314+
315+ it . effect (
316+ "--wait keeps its own run's result when another client's later run settles the thread" ,
317+ ( ) =>
318+ Effect . gen ( function * ( ) {
319+ const sentRun = RunId . make ( "run-2" ) ;
320+ const { tools } = scriptedTools ( {
321+ t3_thread_wait : [
322+ { threadId, runId, status : "completed" , timedOut : false } ,
323+ {
324+ threadId,
325+ runId,
326+ status : "completed" ,
327+ timedOut : false ,
328+ resultRun : { runId : sentRun , status : "failed" } ,
329+ } ,
330+ ] ,
331+ t3_thread_read : laterRunReads ( sentRun , [ { runId : sentRun , type : "user_message" } ] ) ,
332+ } ) ;
333+
334+ assert . deepStrictEqual ( yield * waitForRun ( tools , threadId , runId , true ) , {
335+ type : "ended" ,
336+ runId,
337+ status : "completed" ,
338+ } ) ;
339+ } ) ,
340+ ) ;
341+
213342it . effect ( "--wait stops when the thread waits for an answer instead of polling forever" , ( ) =>
214343 Effect . gen ( function * ( ) {
215344 const { tools } = scriptedTools ( {
0 commit comments