@@ -10,12 +10,15 @@ export interface ThreadActivitySnapshot {
1010
1111export interface ThreadAnnouncementMemory {
1212 readonly snapshot : ThreadActivitySnapshot ;
13- /** Work started that has not been announced as ended yet. */
14- readonly outcomePending : boolean ;
13+ /**
14+ * The turn under way: the run it belongs to, "unknown" until that run shows
15+ * up, or null between turns. Its ending is announced once, then it clears.
16+ */
17+ readonly turnRunId : RunId | "unknown" | null ;
1518 /** The run whose ending is already known, so a stale run is never announced as this turn's. */
1619 readonly settledRunId : RunId | null ;
17- /** The run seen doing this turn's work; another one taking over is a new turn . */
18- readonly activeRunId : RunId | null ;
20+ /** The run shown while idle belongs to the last turn, even if it is still settling . */
21+ readonly idleRunId : RunId | null ;
1922}
2023
2124const ACTIVE_RUN_STATUSES : ReadonlySet < OrchestrationV2RunStatus | null > = new Set ( [
@@ -48,71 +51,69 @@ export function nextThreadAnnouncement(
4851 previous : ThreadAnnouncementMemory | null ,
4952 next : ThreadActivitySnapshot ,
5053) : { readonly memory : ThreadAnnouncementMemory ; readonly announcement : string | null } {
54+ const idle = next . status === "idle" ;
55+ const idleRunId = idle ? next . runId : ( previous ?. idleRunId ?? null ) ;
56+ // A run doing new work; the run left over from the last turn doesn't count.
57+ const newRunId =
58+ ! idle && ACTIVE_RUN_STATUSES . has ( next . runStatus ) && next . runId !== idleRunId
59+ ? next . runId
60+ : null ;
61+
5162 if ( previous === null ) {
5263 return {
5364 memory : {
5465 snapshot : next ,
55- outcomePending : next . status !== "idle" ,
66+ turnRunId : idle ? null : ( newRunId ?? "unknown" ) ,
5667 settledRunId : runOutcomeText ( next . runStatus ) === null ? null : next . runId ,
57- activeRunId : ACTIVE_RUN_STATUSES . has ( next . runStatus ) ? next . runId : null ,
68+ idleRunId ,
5869 } ,
5970 announcement : null ,
6071 } ;
6172 }
62- const before = previous . snapshot . status ;
63- const memory = {
64- ...previous ,
65- snapshot : next ,
66- activeRunId : next . status === "idle" ? null : previous . activeRunId ,
67- } ;
73+ const memory = { ...previous , snapshot : next , idleRunId } ;
74+ const { turnRunId } = previous ;
6875
69- if ( next . status !== " idle" ) {
70- const nextActiveRunId = ACTIVE_RUN_STATUSES . has ( next . runStatus )
71- ? next . runId
72- : previous . activeRunId ;
73- // A queued message can start as the last turn ends, without an idle moment.
74- const queuedTurnStarted =
75- next . status === "working " &&
76- previous . activeRunId !== null &&
77- nextActiveRunId !== previous . activeRunId ;
76+ if ( ! idle ) {
77+ // Sending can look idle for a moment before the server reports the run,
78+ // so a turn is tied to its run as soon as one appears, and only another
79+ // run taking over (a queued message starting) is a new turn.
80+ const turnStarted =
81+ turnRunId === null ||
82+ ( newRunId !== null && turnRunId !== "unknown " && newRunId !== turnRunId ) ;
83+ const nextTurnRunId =
84+ turnStarted || turnRunId === "unknown" ? ( newRunId ?? "unknown" ) : turnRunId ;
7885 const announcement =
79- next . status === before && ! queuedTurnStarted
80- ? null
81- : next . status === "approval"
82- ? "Needs approval"
83- : next . status === "input"
84- ? "Needs your input"
85- : // Sending can look idle for a moment before the server reports the
86- // run; that flicker is still the same turn.
87- queuedTurnStarted || ( before === "idle" && ! previous . outcomePending )
88- ? "Working"
89- : null ;
90- return {
91- memory : { ...memory , outcomePending : true , activeRunId : nextActiveRunId } ,
92- announcement,
93- } ;
86+ next . status === "approval"
87+ ? previous . snapshot . status === "approval"
88+ ? null
89+ : "Needs approval"
90+ : next . status === "input"
91+ ? previous . snapshot . status === "input"
92+ ? null
93+ : "Needs your input"
94+ : turnStarted
95+ ? "Working"
96+ : null ;
97+ return { memory : { ...memory , turnRunId : nextTurnRunId } , announcement } ;
9498 }
9599
96100 // Work can stop a moment before the run settles; the ending waits for the run.
97101 const outcome = runOutcomeText ( next . runStatus ) ;
98- if ( ! previous . outcomePending ) {
102+ if ( turnRunId === null ) {
99103 // A run that settled unseen is history too, or the next failed send would claim it.
100104 const settledRunId = outcome === null ? previous . settledRunId : next . runId ;
101105 return { memory : { ...memory , settledRunId } , announcement : null } ;
102106 }
103107 if ( next . runStatus === "rolled_back" ) {
104- return {
105- memory : { ...memory , outcomePending : false , settledRunId : next . runId } ,
106- announcement : null ,
107- } ;
108+ return { memory : { ...memory , turnRunId : null , settledRunId : next . runId } , announcement : null } ;
108109 }
109110 if ( outcome === null ) return { memory, announcement : null } ;
110111 // Work stopped without a new run, e.g. a send that failed; the retry is a new turn.
111112 if ( next . runId === previous . settledRunId ) {
112- return { memory : { ...memory , outcomePending : false } , announcement : null } ;
113+ return { memory : { ...memory , turnRunId : null } , announcement : null } ;
113114 }
114115 return {
115- memory : { ...memory , outcomePending : false , settledRunId : next . runId } ,
116+ memory : { ...memory , turnRunId : null , settledRunId : next . runId } ,
116117 announcement : outcome ,
117118 } ;
118119}
0 commit comments