@devries-cameron20 / t3code-1 / commits / fd1063ba7c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): keep subagent transcript updates off shell stream (#23783)

Yash Singh committed Oct 22, 2026 at 14:40 UTC fd1063ba7c452e2a938484277235afebf0639119
7 files changed +111 -10
apps/server/src/orchestration-v2/LiveStreamBudget.test.ts
+2 -1
@@ -163,6 +163,7 @@ describe("replayAndBufferLiveEvents", () => {
163 const highWaterStarted = yield* Deferred.make<void>();
164 const highWater = yield* Deferred.make<number>();
165 const first = { sequence: 1, text: "first", threadId: "selected" };
166 + const unrelatedReplay = { sequence: 0, text: "unrelated replay", threadId: "other" };
167 const overlap = { sequence: 2, text: "overlap", threadId: "selected" };
168 const last = { sequence: 10, text: "last", threadId: "selected" };
169 const reader = yield* replayAndBufferLiveEvents(
@@ -171,7 +172,7 @@ describe("replayAndBufferLiveEvents", () => {
172 latestSequence: Deferred.succeed(highWaterStarted, undefined).pipe(
173 Effect.andThen(Deferred.await(highWater)),
174 ),
174 - replay: () => Stream.make(first, overlap),
175 + replay: () => Stream.make(unrelatedReplay, first, overlap),
176 filter: (event) => event.threadId === "selected",
177 },
178 { maxItems: 4 },
apps/server/src/orchestration-v2/LiveStreamBudget.ts
+1
@@ -385,6 +385,7 @@ export const replayAndBufferProjectedLiveEvents = <
385 // The finite database page is independent of the live tail.
386 // Charge only the item being delivered so a fast reader can
387 // consume a page larger than the budget without overflowing.
388 + Stream.filter((event) => input.filter?.(event) ?? true),
389 Stream.rechunk(1),
390 Stream.mapEffect((event) => budget.retain(input.project(event))),
391 );
apps/server/src/orchestration-v2/ShellStream.test.ts
+45
@@ -1,5 +1,6 @@
1 import { describe, expect, it } from "@effect/vitest";
2 import type {
3 + ApplicationStoredEvent,
4 OrchestrationV2ShellSnapshot,
5 OrchestrationV2ShellStreamItem,
6 OrchestrationV2ThreadShell,
@@ -21,6 +22,7 @@ import {
22 dedupeShellEnrichment,
23 shellStreamItemFromEnrichmentRefresh,
24 shellStreamItemsFromInitialSnapshot,
25 + shouldPublishShellApplicationEvent,
26 skipUnchangedThreadShells,
27 } from "./ShellStream.ts";
28
@@ -32,6 +34,49 @@ const emptyShellSnapshot = {
34 archivedThreads: [],
35 } as OrchestrationV2ShellSnapshot;
36
37 +describe("subagent shell events", () => {
38 + const stored = (threadId: string, type: string) =>
39 + ({ event: { threadId, type } }) as ApplicationStoredEvent;
40 +
41 + it("keeps regular threads and subagent state changes while omitting subagent transcript updates", () => {
42 + const subagents = new Set(["child"]);
43 + expect(
44 + shouldPublishShellApplicationEvent(stored("parent", "turn-item.updated"), subagents),
45 + ).toBe(true);
46 + expect(
47 + shouldPublishShellApplicationEvent(stored("child", "turn-item.updated"), subagents),
48 + ).toBe(false);
49 + expect(shouldPublishShellApplicationEvent(stored("child", "node.updated"), subagents)).toBe(
50 + false,
51 + );
52 + expect(
53 + shouldPublishShellApplicationEvent(stored("child", "runtime-request.updated"), subagents),
54 + ).toBe(true);
55 + expect(shouldPublishShellApplicationEvent(stored("child", "run.updated"), subagents)).toBe(
56 + true,
57 + );
58 + });
59 +
60 + it("tracks subagents created after the subscription starts", () => {
61 + const subagents = new Set<string>();
62 + const created = {
63 + event: {
64 + threadId: "new-child",
65 + type: "thread.created",
66 + payload: { lineage: { relationshipToParent: "subagent" } },
67 + },
68 + } as ApplicationStoredEvent;
69 + expect(shouldPublishShellApplicationEvent(created, subagents)).toBe(true);
70 + expect(
71 + shouldPublishShellApplicationEvent(stored("new-child", "message.updated"), subagents),
72 + ).toBe(false);
73 + expect(
74 + shouldPublishShellApplicationEvent(stored("new-child", "thread.deleted"), subagents),
75 + ).toBe(true);
76 + expect(subagents.has("new-child")).toBe(true);
77 + });
78 +});
79 +
80 it.effect("coalesces a slow shell subscriber by identity and location in sequence order", () =>
81 Effect.scoped(
82 Effect.gen(function* () {
apps/server/src/orchestration-v2/ShellStream.ts
+26
@@ -106,6 +106,32 @@ export type ShellApplicationEvent =
106 readonly event: Pick<OrchestrationV2StoredEvent["event"], "threadId">;
107 };
108
109 +/** Focused subagents receive these updates through their thread subscription. */
110 +export function shouldPublishShellApplicationEvent(
111 + stored: ApplicationStoredEvent,
112 + subagentThreadIds: Set<string>,
113 +): boolean {
114 + if ("aggregateKind" in stored) return true;
115 + const { event } = stored;
116 + if (event.type === "thread.created") {
117 + if (event.payload.lineage.relationshipToParent === "subagent") {
118 + subagentThreadIds.add(event.threadId);
119 + }
120 + return true;
121 + }
122 + if (event.type === "thread.deleted") {
123 + // Replay and the live tail overlap; keep known IDs so late events stay filtered.
124 + return true;
125 + }
126 + return !(
127 + subagentThreadIds.has(event.threadId) &&
128 + (event.type === "node.updated" ||
129 + event.type === "turn-item.updated" ||
130 + event.type === "provider-turn.updated" ||
131 + event.type === "message.updated")
132 + );
133 +}
134 +
135 /** Shell updates refetch an aggregate; drop transcript bodies before retaining an event. */
136 export function toShellApplicationEvent(stored: ApplicationStoredEvent): ShellApplicationEvent {
137 return "aggregateKind" in stored
apps/server/src/persistence/OrchestrationEventStore.ts
+8 -1
@@ -118,6 +118,7 @@ export class OrchestrationEventStore extends Context.Service<
118 readonly streamProjectedApplicationEvents: <A extends { readonly sequence: number }>(input: {
119 readonly afterSequence?: number;
120 readonly project: (event: ApplicationStoredEvent) => A;
121 + readonly filter?: (event: ApplicationStoredEvent) => boolean;
122 }) => Stream.Stream<A, OrchestrationEventStoreError>;
123 }
124 >()("t3/persistence/OrchestrationEventStore") {}
@@ -716,11 +717,17 @@ const makeEventStore = Effect.gen(function* () {
717
718 const streamProjectedApplicationEvents: OrchestrationEventStore["Service"]["streamProjectedApplicationEvents"] =
719 (input) =>
719 - replayAndBufferProjectedLiveEvents({
720 + replayAndBufferProjectedLiveEvents<
721 + ApplicationStoredEvent,
722 + ReturnType<typeof input.project>,
723 + OrchestrationEventStoreError,
724 + never
725 + >({
726 subscribe: PubSub.subscribe(committedEvents),
727 latestSequence: latestApplicationSequence,
728 afterSequence: input.afterSequence ?? 0,
729 project: input.project,
730 + ...(input.filter === undefined ? {} : { filter: input.filter }),
731 replay: (throughSequence) =>
732 catchUpApplicationEvents({
733 afterSequence: input?.afterSequence ?? 0,
apps/server/src/ws.test.ts
+1
@@ -252,6 +252,7 @@ it.live.each(["thread", "shell"] as const)(
252 const items = yield* subscription.pipe(
253 Effect.provideService(ThreadManagement.ThreadManagementService, {
254 ensureLegacyTranscript: () => Effect.void,
255 + getShellSnapshot: () => Effect.succeed({ threads: [] }),
256 streamStoredEventsFrom: (
257 input: Parameters<
258 ThreadManagement.ThreadManagementService["Service"]["streamStoredEventsFrom"]
apps/server/src/ws.ts
+28 -8
@@ -62,6 +62,7 @@ import {
62 OrchestrationV2ThreadLaunchError,
63 type OrchestrationProjectShell,
64 type OrchestrationV2ShellSnapshot,
65 + type ApplicationStoredEvent,
66 type ProjectEntriesFailure,
67 type ProjectFileFailure,
68 type ProjectFileOperation,
@@ -131,6 +132,7 @@ import {
132 shellStreamItemFromThreadShell,
133 shellStreamItemsFromInitialSnapshot,
134 shellStreamItemsFromResumeSnapshot,
135 + shouldPublishShellApplicationEvent,
136 skipUnchangedThreadShells,
137 toShellApplicationEvent,
138 type ShellApplicationEvent,
@@ -964,6 +966,9 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
966 const projects = yield* ProjectStore.ProjectStoreV2;
967 const projectService = yield* ProjectService.ProjectService;
968 const projectEnrichment = yield* ProjectEnrichmentService.ProjectEnrichmentService;
969 + let subagentThreadIds = new Set<string>();
970 + const filterSubagentTranscript = (stored: ApplicationStoredEvent) =>
971 + shouldPublishShellApplicationEvent(stored, subagentThreadIds);
972
973 const enrichmentChanges = yield* projectEnrichment.subscribeChanges;
974 const loadProjectMetadataSnapshot = Effect.fn("ws.orchestrationV2.loadProjectMetadataSnapshot")(
@@ -990,6 +995,11 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
995 latestSequence: applicationEvents.latestApplicationSequence,
996 }),
997 );
998 + subagentThreadIds = new Set(
999 + base.threads
1000 + .filter((thread) => thread.lineage.relationshipToParent === "subagent")
1001 + .map((thread) => thread.id),
1002 + );
1003 const enriched = yield* enrichProjectShells(base.projects);
1004 return {
1005 snapshot: { ...base, projects: enriched.projects } as OrchestrationV2ShellSnapshot,
@@ -1052,6 +1062,7 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
1062 applicationEvents.streamProjectedApplicationEvents({
1063 afterSequence,
1064 project: toShellApplicationEvent,
1065 + filter: filterSubagentTranscript,
1066 }),
1067 ),
1068 );
@@ -1142,16 +1153,25 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
1153 });
1154 }
1155
1156 + const currentShell = yield* threadManagement.getShellSnapshot({ location: "active" });
1157 + subagentThreadIds = new Set(
1158 + currentShell.threads
1159 + .filter((thread) => thread.lineage.relationshipToParent === "subagent")
1160 + .map((thread) => thread.id),
1161 + );
1162 +
1163 const loaded = yield* loadProjectMetadataSnapshot(highWater);
1164 const replay = toShellStream(
1147 - applicationEvents.readApplicationEvents({
1148 - afterSequence: input.afterSequence,
1149 - throughSequence: highWater,
1150 - // A row a newer build wrote (e.g. after a downgrade) must not fail
1151 - // every shell resubscribe. The live tail below resumes from
1152 - // highWater, so a dropped row never hides later events.
1153 - skipUnknownEventTypes: true,
1154 - }),
1165 + applicationEvents
1166 + .readApplicationEvents({
1167 + afterSequence: input.afterSequence,
1168 + throughSequence: highWater,
1169 + // A row a newer build wrote (e.g. after a downgrade) must not fail
1170 + // every shell resubscribe. The live tail below resumes from
1171 + // highWater, so a dropped row never hides later events.
1172 + skipUnknownEventTypes: true,
1173 + })
1174 + .pipe(Stream.filter(filterSubagentTranscript)),
1175 );
1176 return composeShellStreamWithEnrichment({
1177 initial: initialEnrichmentItems(loaded),