99 ThreadId ,
1010 TrimmedNonEmptyString ,
1111 type OrchestratorMcpThreadListItem ,
12+ type OrchestratorMcpThreadRun ,
1213 type OrchestratorMcpThreadStatus ,
1314 type OrchestratorMcpThreadTimelineItem ,
1415} from "@t3tools/contracts" ;
@@ -39,6 +40,15 @@ import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
3940 * soon, since `t3_thread_wait` only returns on a terminal run.
4041 */
4142const WAIT_SLICE_MS = 15_000 ;
43+ const WAKE_GRACE_MS = 10_000 ;
44+ const WAKE_POLL_MS = 500 ;
45+ const TERMINAL_RUN_STATUSES = new Set < OrchestratorMcpThreadRun [ "status" ] > ( [
46+ "completed" ,
47+ "failed" ,
48+ "cancelled" ,
49+ "interrupted" ,
50+ "rolled_back" ,
51+ ] ) ;
4252const READ_PAGE_LIMIT = 100 ;
4353const TITLE_MAX_CHARS = 80 ;
4454
@@ -498,35 +508,131 @@ type WaitOutcome =
498508 }
499509 | { readonly type : "needs_input" } ;
500510
511+ /**
512+ * Whether the thread has nothing left to do: no active run and no delegated
513+ * or background work. Only then is the work a later wait settles on this
514+ * send's own.
515+ */
516+ const threadIsIdle = Effect . fn ( "cli.thread.threadIsIdle" ) ( function * (
517+ tools : ThreadTools ,
518+ threadId : ThreadId ,
519+ ) {
520+ const waited = yield * tools . call (
521+ "t3_thread_wait" ,
522+ { threadId, timeoutMs : 1 , untilDelegatedWorkSettled : true } ,
523+ OrchestratorMcpThreadWaitResult ,
524+ ) ;
525+ return ! waited . timedOut ;
526+ } ) ;
527+
528+ /**
529+ * The runs after `runId`, and whether one holds a message someone sent.
530+ * Background work wakes a run with a notification, never a user message, so
531+ * such a run is another request and its reply is not this one's.
532+ */
533+ const runsAfter = Effect . fn ( "cli.thread.runsAfter" ) ( function * (
534+ tools : ThreadTools ,
535+ threadId : ThreadId ,
536+ runId : RunId ,
537+ ) {
538+ const { recentRuns } = yield * tools . call (
539+ "t3_thread_read" ,
540+ { threadId, limit : 1 , runLimit : 50 } ,
541+ OrchestratorMcpThreadReadResult ,
542+ ) ;
543+ const own = recentRuns . find ( ( run ) => run . runId === runId ) ;
544+ if ( own === undefined ) return { later : [ ] , sent : true } ;
545+ const later = recentRuns . filter ( ( run ) => run . ordinal > own . ordinal ) ;
546+ if ( later . length === 0 ) return { later, sent : false } ;
547+ const { items } = yield * readTimeline ( tools , threadId , "messages" ) ;
548+ const sent = items . some (
549+ ( item ) => item . type === "user_message" && later . some ( ( run ) => run . runId === item . runId ) ,
550+ ) ;
551+ return { later, sent } ;
552+ } ) ;
553+
501554/**
502555 * Waits for this exact run, following a restart to its continuation, so a
503556 * message another client sends meanwhile cannot stand in for its result.
557+ * With `followWakes`, a completed run is followed further to the runs its
558+ * background work wakes, as long as no one else sends to the thread.
504559 */
505560export const waitForRun = Effect . fn ( "cli.thread.waitForRun" ) ( function * (
506561 tools : ThreadTools ,
507562 threadId : ThreadId ,
508563 runId : RunId ,
564+ followWakes = false ,
509565) {
510- const waitSlice = ( timeoutMs : number ) =>
511- tools . call ( "t3_thread_wait" , { threadId, runId, timeoutMs } , OrchestratorMcpThreadWaitResult ) ;
566+ const waitSlice = ( timeoutMs : number , untilDelegatedWorkSettled ?: true ) =>
567+ tools . call (
568+ "t3_thread_wait" ,
569+ {
570+ threadId,
571+ runId,
572+ timeoutMs,
573+ ...( untilDelegatedWorkSettled ? { untilDelegatedWorkSettled } : { } ) ,
574+ } ,
575+ OrchestratorMcpThreadWaitResult ,
576+ ) ;
512577 const ended = ( waited : OrchestratorMcpThreadWaitResult ) =>
513578 ( { type : "ended" , runId : waited . runId , status : waited . status } ) satisfies WaitOutcome ;
579+ const ownRun = Effect . gen ( function * ( ) {
580+ while ( true ) {
581+ const waited = yield * waitSlice ( WAIT_SLICE_MS ) ;
582+ if ( ! waited . timedOut ) return ended ( waited ) ;
583+ // A pending request on this run, or on an earlier run this one is queued
584+ // behind, blocks it until someone answers.
585+ const { thread } = yield * tools . call (
586+ "t3_thread_read" ,
587+ { threadId, limit : 1 , runLimit : 1 } ,
588+ OrchestratorMcpThreadReadResult ,
589+ ) ;
590+ if ( thread . pendingRequestCount > 0 ) {
591+ // The count is thread-wide: if this run ended since the slice, the
592+ // request belongs to a later run.
593+ const recheck = yield * waitSlice ( 1 ) ;
594+ return recheck . timedOut ? ( { type : "needs_input" } satisfies WaitOutcome ) : ended ( recheck ) ;
595+ }
596+ }
597+ } ) ;
598+ const outcome = yield * ownRun ;
599+ if ( ! followWakes || outcome . type !== "ended" || outcome . status !== "completed" ) return outcome ;
600+ const finalRunId = outcome . runId ?? runId ;
601+ // A provider records the run that background work wakes only after that
602+ // work has ended, so the thread briefly looks settled in between.
603+ const wakeRunRecorded = Effect . gen ( function * ( ) {
604+ for ( let waited = 0 ; waited < WAKE_GRACE_MS ; waited += WAKE_POLL_MS ) {
605+ yield * Effect . sleep ( WAKE_POLL_MS ) ;
606+ const after = yield * runsAfter ( tools , threadId , finalRunId ) ;
607+ if ( after . sent ) return false ;
608+ if ( after . later . length > 0 ) return true ;
609+ }
610+ return false ;
611+ } ) ;
612+ let workOutlivedRun = false ;
514613 while ( true ) {
515- const waited = yield * waitSlice ( WAIT_SLICE_MS ) ;
516- if ( ! waited . timedOut ) return ended ( waited ) ;
517- // A pending request on this run, or on an earlier run this one is queued
518- // behind, blocks it until someone answers.
614+ const waited = yield * waitSlice ( WAIT_SLICE_MS , true ) ;
615+ // `resultRun` is the thread's latest finished run, whoever sent it.
616+ const result = waited . timedOut ? undefined : waited . resultRun ;
617+ const after = yield * runsAfter ( tools , threadId , finalRunId ) ;
618+ if ( after . sent ) return outcome ;
619+ if ( ! waited . timedOut ) {
620+ if ( result !== undefined && after . later . some ( ( run ) => run . runId === result . runId ) ) {
621+ return { type : "ended" , runId : result . runId , status : result . status } satisfies WaitOutcome ;
622+ }
623+ if ( ! workOutlivedRun ) return outcome ;
624+ if ( after . later . some ( ( run ) => ! TERMINAL_RUN_STATUSES . has ( run . status ) ) ) continue ;
625+ if ( after . later . length === 0 && ( yield * wakeRunRecorded ) ) continue ;
626+ return outcome ;
627+ }
628+ workOutlivedRun = true ;
519629 const { thread } = yield * tools . call (
520630 "t3_thread_read" ,
521631 { threadId, limit : 1 , runLimit : 1 } ,
522632 OrchestratorMcpThreadReadResult ,
523633 ) ;
524- if ( thread . pendingRequestCount > 0 ) {
525- // The count is thread-wide: if this run ended since the slice, the
526- // request belongs to a later run.
527- const recheck = yield * waitSlice ( 1 ) ;
528- return recheck . timedOut ? ( { type : "needs_input" } satisfies WaitOutcome ) : ended ( recheck ) ;
529- }
634+ // Earlier runs have ended, so the request is on a run this one's work woke.
635+ if ( thread . pendingRequestCount > 0 ) return { type : "needs_input" } satisfies WaitOutcome ;
530636 }
531637} ) ;
532638
@@ -540,9 +646,10 @@ const finishRun = Effect.fn("cli.thread.finishRun")(function* (
540646 readonly threadId : ThreadId ;
541647 readonly runId : RunId ;
542648 readonly json : boolean ;
649+ readonly followWakes : boolean ;
543650 } ,
544651) {
545- const outcome = yield * waitForRun ( tools , input . threadId , input . runId ) ;
652+ const outcome = yield * waitForRun ( tools , input . threadId , input . runId , input . followWakes ) ;
546653 const runId = outcome . type === "ended" ? ( outcome . runId ?? input . runId ) : input . runId ;
547654 const status = outcome . type === "ended" ? outcome . status : "waiting" ;
548655 const reply =
@@ -727,6 +834,7 @@ const threadStartCommand = Command.make("start", {
727834 threadId : launched . threadId ,
728835 runId : launched . runId ,
729836 json : flags . json ,
837+ followWakes : true ,
730838 } ) ;
731839 }
732840 yield * flags . json ? printJson ( launched ) : Console . log ( launched . threadId ) ;
@@ -753,13 +861,20 @@ const threadSendCommand = Command.make("send", {
753861 yield * withThreadTools (
754862 flags ,
755863 Effect . fn ( "cli.thread.send" ) ( function * ( tools ) {
864+ // Work already running when this is sent is not this message's to wait for.
865+ const followWakes = flags . wait ? yield * threadIsIdle ( tools , threadId ) : false ;
756866 const sent = yield * tools . call (
757867 "t3_thread_send" ,
758868 { threadId, message, mode : "queue" } ,
759869 OrchestratorMcpThreadSendResult ,
760870 ) ;
761871 if ( flags . wait ) {
762- return yield * finishRun ( tools , { threadId, runId : sent . runId , json : flags . json } ) ;
872+ return yield * finishRun ( tools , {
873+ threadId,
874+ runId : sent . runId ,
875+ json : flags . json ,
876+ followWakes,
877+ } ) ;
763878 }
764879 yield * flags . json
765880 ? printJson ( sent )
0 commit comments