Skip to content

Commit 13efe8d

Browse files
braulioboclaude
andcommitted
orchestration-v2: stop tracking below-window background work once it finishes
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
1 parent 55aa62d commit 13efe8d

2 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎packages/client-runtime/src/state/orchestrationV2Projection.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,22 @@ describe("applyOrchestrationV2ProjectionEvent", () => {
469469
expect(next?.visibleTurnItems).toEqual(oldSubagentWindow().visibleTurnItems);
470470
});
471471

472+
it("stops tracking a below-window subagent once it finishes", () => {
473+
const running = applyOldSubagent("running")!;
474+
const next = applyOrchestrationV2ProjectionEvent(
475+
running,
476+
itemEvent(subagentItem("completed")),
477+
{
478+
partialTimeline: true,
479+
latestLocalTurnOrdinal: 3_000_003,
480+
},
481+
);
482+
483+
expect(next?.turnItems.map((item) => item.id)).toEqual(
484+
oldSubagentWindow().turnItems.map((item) => item.id),
485+
);
486+
});
487+
472488
it("still drops a finished subagent below the window", () => {
473489
const projection = oldSubagentWindow();
474490
expect(applyOldSubagent("completed")).toEqual(projection);

‎packages/client-runtime/src/state/orchestrationV2Projection.ts‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import type {
55
OrchestrationV2ThreadProjection,
66
OrchestrationV2TurnItem,
77
} from "@t3tools/contracts";
8-
import { isActiveBackgroundTurnItem } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
8+
import {
9+
isActiveBackgroundTurnItem,
10+
turnItemUpdateCanEndBackgroundWork,
11+
} from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
912
import {
1013
createOrchestrationV2TurnItemVisibility,
1114
isOrchestrationV2TurnItemVisible,
@@ -294,14 +297,22 @@ export function applyOrchestrationV2ProjectionEvent(
294297
case "plan.updated":
295298
return { ...base, plans: upsertEntity(base.plans, event.payload) };
296299
case "turn-item.updated": {
297-
const unloaded =
300+
const loaded = projection.turnItems.some((candidate) => candidate.id === event.payload.id);
301+
const belowWindow =
298302
partialTimeline &&
299-
!projection.turnItems.some((candidate) => candidate.id === event.payload.id) &&
303+
(!loaded || turnItemUpdateCanEndBackgroundWork(event.payload)) &&
300304
shouldDropMissingPartialTurnItem(projection, event.payload, latestLocalTurnOrdinal);
301-
if (unloaded && !isActiveBackgroundTurnItem(event.payload)) return projection;
305+
// Running work from before the window still counts toward the waiting state, but is not
306+
// timeline, so it is kept only while it runs.
307+
if (belowWindow) {
308+
if (isActiveBackgroundTurnItem(event.payload)) {
309+
return { ...base, turnItems: upsertEntity(base.turnItems, event.payload) };
310+
}
311+
return loaded
312+
? { ...base, turnItems: base.turnItems.filter((item) => item.id !== event.payload.id) }
313+
: projection;
314+
}
302315
const next = { ...base, turnItems: upsertEntity(base.turnItems, event.payload) };
303-
// Running work from before the window still counts toward the waiting state, but is not timeline.
304-
if (unloaded) return next;
305316
// Only interrupt requests can change another item's visibility. Streaming
306317
// text/tool updates must not recheck every row against every run.
307318
const previous = projection.turnItems.find((item) => item.id === event.payload.id);

0 commit comments

Comments
 (0)