Skip to content
TypeScript

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

Yash Singhfd1063ba7c45
diff --git a/apps/server/src/orchestration-v2/LiveStreamBudget.test.ts b/apps/server/src/orchestration-v2/LiveStreamBudget.test.ts
index be612837c6e..65914ec0b72 100644
--- a/apps/server/src/orchestration-v2/LiveStreamBudget.test.ts
+++ b/apps/server/src/orchestration-v2/LiveStreamBudget.test.ts
@@ -163,6 +163,7 @@ describe("replayAndBufferLiveEvents", () => {
const highWaterStarted = yield* Deferred.make<void>();
const highWater = yield* Deferred.make<number>();
const first = { sequence: 1, text: "first", threadId: "selected" };
+ const unrelatedReplay = { sequence: 0, text: "unrelated replay", threadId: "other" };
const overlap = { sequence: 2, text: "overlap", threadId: "selected" };
const last = { sequence: 10, text: "last", threadId: "selected" };
const reader = yield* replayAndBufferLiveEvents(
@@ -171,7 +172,7 @@ describe("replayAndBufferLiveEvents", () => {
latestSequence: Deferred.succeed(highWaterStarted, undefined).pipe(
Effect.andThen(Deferred.await(highWater)),
),
- replay: () => Stream.make(first, overlap),
+ replay: () => Stream.make(unrelatedReplay, first, overlap),
filter: (event) => event.threadId === "selected",
},
{ maxItems: 4 },
diff --git a/apps/server/src/orchestration-v2/LiveStreamBudget.ts b/apps/server/src/orchestration-v2/LiveStreamBudget.ts
index 3960d9982bf..c4ace088c57 100644
--- a/apps/server/src/orchestration-v2/LiveStreamBudget.ts
+++ b/apps/server/src/orchestration-v2/LiveStreamBudget.ts
@@ -385,6 +385,7 @@ export const replayAndBufferProjectedLiveEvents = <
// The finite database page is independent of the live tail.
// Charge only the item being delivered so a fast reader can
// consume a page larger than the budget without overflowing.
+ Stream.filter((event) => input.filter?.(event) ?? true),
Stream.rechunk(1),
Stream.mapEffect((event) => budget.retain(input.project(event))),
);
diff --git a/apps/server/src/orchestration-v2/ShellStream.test.ts b/apps/server/src/orchestration-v2/ShellStream.test.ts
index 3ecfe50f1af..fcfaf8298f5 100644
--- a/apps/server/src/orchestration-v2/ShellStream.test.ts
+++ b/apps/server/src/orchestration-v2/ShellStream.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from "@effect/vitest";
import type {
+ ApplicationStoredEvent,
OrchestrationV2ShellSnapshot,
OrchestrationV2ShellStreamItem,
OrchestrationV2ThreadShell,
@@ -21,6 +22,7 @@ import {
dedupeShellEnrichment,
shellStreamItemFromEnrichmentRefresh,
shellStreamItemsFromInitialSnapshot,
+ shouldPublishShellApplicationEvent,
skipUnchangedThreadShells,
} from "./ShellStream.ts";
@@ -32,6 +34,49 @@ const emptyShellSnapshot = {
archivedThreads: [],
} as OrchestrationV2ShellSnapshot;
+describe("subagent shell events", () => {
+ const stored = (threadId: string, type: string) =>
+ ({ event: { threadId, type } }) as ApplicationStoredEvent;
+
+ it("keeps regular threads and subagent state changes while omitting subagent transcript updates", () => {
+ const subagents = new Set(["child"]);
+ expect(
+ shouldPublishShellApplicationEvent(stored("parent", "turn-item.updated"), subagents),
+ ).toBe(true);
+ expect(
+ shouldPublishShellApplicationEvent(stored("child", "turn-item.updated"), subagents),
+ ).toBe(false);
+ expect(shouldPublishShellApplicationEvent(stored("child", "node.updated"), subagents)).toBe(
+ false,
+ );
+ expect(
+ shouldPublishShellApplicationEvent(stored("child", "runtime-request.updated"), subagents),
+ ).toBe(true);
+ expect(shouldPublishShellApplicationEvent(stored("child", "run.updated"), subagents)).toBe(
+ true,
+ );
+ });
+
+ it("tracks subagents created after the subscription starts", () => {
+ const subagents = new Set<string>();
+ const created = {
+ event: {
+ threadId: "new-child",
+ type: "thread.created",
+ payload: { lineage: { relationshipToParent: "subagent" } },
+ },
+ } as ApplicationStoredEvent;
+ expect(shouldPublishShellApplicationEvent(created, subagents)).toBe(true);
+ expect(
+ shouldPublishShellApplicationEvent(stored("new-child", "message.updated"), subagents),
+ ).toBe(false);
+ expect(
+ shouldPublishShellApplicationEvent(stored("new-child", "thread.deleted"), subagents),
+ ).toBe(true);
+ expect(subagents.has("new-child")).toBe(true);
+ });
+});
+
it.effect("coalesces a slow shell subscriber by identity and location in sequence order", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/server/src/orchestration-v2/ShellStream.ts b/apps/server/src/orchestration-v2/ShellStream.ts
index 057fd77d809..7a1367aa679 100644
--- a/apps/server/src/orchestration-v2/ShellStream.ts
+++ b/apps/server/src/orchestration-v2/ShellStream.ts
@@ -106,6 +106,32 @@ export type ShellApplicationEvent =
readonly event: Pick<OrchestrationV2StoredEvent["event"], "threadId">;
};
+/** Focused subagents receive these updates through their thread subscription. */
+export function shouldPublishShellApplicationEvent(
+ stored: ApplicationStoredEvent,
+ subagentThreadIds: Set<string>,
+): boolean {
+ if ("aggregateKind" in stored) return true;
+ const { event } = stored;
+ if (event.type === "thread.created") {
+ if (event.payload.lineage.relationshipToParent === "subagent") {
+ subagentThreadIds.add(event.threadId);
+ }
+ return true;
+ }
+ if (event.type === "thread.deleted") {
+ // Replay and the live tail overlap; keep known IDs so late events stay filtered.
+ return true;
+ }
+ return !(
+ subagentThreadIds.has(event.threadId) &&
+ (event.type === "node.updated" ||
+ event.type === "turn-item.updated" ||
+ event.type === "provider-turn.updated" ||
+ event.type === "message.updated")
+ );
+}
+
/** Shell updates refetch an aggregate; drop transcript bodies before retaining an event. */
export function toShellApplicationEvent(stored: ApplicationStoredEvent): ShellApplicationEvent {
return "aggregateKind" in stored
diff --git a/apps/server/src/persistence/OrchestrationEventStore.ts b/apps/server/src/persistence/OrchestrationEventStore.ts
index a7940746116..394f627a4e4 100644
--- a/apps/server/src/persistence/OrchestrationEventStore.ts
+++ b/apps/server/src/persistence/OrchestrationEventStore.ts
@@ -118,6 +118,7 @@ export class OrchestrationEventStore extends Context.Service<
readonly streamProjectedApplicationEvents: <A extends { readonly sequence: number }>(input: {
readonly afterSequence?: number;
readonly project: (event: ApplicationStoredEvent) => A;
+ readonly filter?: (event: ApplicationStoredEvent) => boolean;
}) => Stream.Stream<A, OrchestrationEventStoreError>;
}
>()("t3/persistence/OrchestrationEventStore") {}
@@ -716,11 +717,17 @@ const makeEventStore = Effect.gen(function* () {
const streamProjectedApplicationEvents: OrchestrationEventStore["Service"]["streamProjectedApplicationEvents"] =
(input) =>
- replayAndBufferProjectedLiveEvents({
+ replayAndBufferProjectedLiveEvents<
+ ApplicationStoredEvent,
+ ReturnType<typeof input.project>,
+ OrchestrationEventStoreError,
+ never
+ >({
subscribe: PubSub.subscribe(committedEvents),
latestSequence: latestApplicationSequence,
afterSequence: input.afterSequence ?? 0,
project: input.project,
+ ...(input.filter === undefined ? {} : { filter: input.filter }),
replay: (throughSequence) =>
catchUpApplicationEvents({
afterSequence: input?.afterSequence ?? 0,
diff --git a/apps/server/src/ws.test.ts b/apps/server/src/ws.test.ts
index a9a028ec94b..75da37ad3b6 100644
--- a/apps/server/src/ws.test.ts
+++ b/apps/server/src/ws.test.ts
@@ -252,6 +252,7 @@ it.live.each(["thread", "shell"] as const)(
const items = yield* subscription.pipe(
Effect.provideService(ThreadManagement.ThreadManagementService, {
ensureLegacyTranscript: () => Effect.void,
+ getShellSnapshot: () => Effect.succeed({ threads: [] }),
streamStoredEventsFrom: (
input: Parameters<
ThreadManagement.ThreadManagementService["Service"]["streamStoredEventsFrom"]
diff --git a/apps/server/src/ws.ts b/apps/server/src/ws.ts
index 1ba4ff99ff6..7051fcce28a 100644
--- a/apps/server/src/ws.ts
+++ b/apps/server/src/ws.ts
@@ -62,6 +62,7 @@ import {
OrchestrationV2ThreadLaunchError,
type OrchestrationProjectShell,
type OrchestrationV2ShellSnapshot,
+ type ApplicationStoredEvent,
type ProjectEntriesFailure,
type ProjectFileFailure,
type ProjectFileOperation,
@@ -131,6 +132,7 @@ import {
shellStreamItemFromThreadShell,
shellStreamItemsFromInitialSnapshot,
shellStreamItemsFromResumeSnapshot,
+ shouldPublishShellApplicationEvent,
skipUnchangedThreadShells,
toShellApplicationEvent,
type ShellApplicationEvent,
@@ -964,6 +966,9 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
const projects = yield* ProjectStore.ProjectStoreV2;
const projectService = yield* ProjectService.ProjectService;
const projectEnrichment = yield* ProjectEnrichmentService.ProjectEnrichmentService;
+ let subagentThreadIds = new Set<string>();
+ const filterSubagentTranscript = (stored: ApplicationStoredEvent) =>
+ shouldPublishShellApplicationEvent(stored, subagentThreadIds);
const enrichmentChanges = yield* projectEnrichment.subscribeChanges;
const loadProjectMetadataSnapshot = Effect.fn("ws.orchestrationV2.loadProjectMetadataSnapshot")(
@@ -990,6 +995,11 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
latestSequence: applicationEvents.latestApplicationSequence,
}),
);
+ subagentThreadIds = new Set(
+ base.threads
+ .filter((thread) => thread.lineage.relationshipToParent === "subagent")
+ .map((thread) => thread.id),
+ );
const enriched = yield* enrichProjectShells(base.projects);
return {
snapshot: { ...base, projects: enriched.projects } as OrchestrationV2ShellSnapshot,
@@ -1052,6 +1062,7 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
applicationEvents.streamProjectedApplicationEvents({
afterSequence,
project: toShellApplicationEvent,
+ filter: filterSubagentTranscript,
}),
),
);
@@ -1142,16 +1153,25 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
});
}
+ const currentShell = yield* threadManagement.getShellSnapshot({ location: "active" });
+ subagentThreadIds = new Set(
+ currentShell.threads
+ .filter((thread) => thread.lineage.relationshipToParent === "subagent")
+ .map((thread) => thread.id),
+ );
+
const loaded = yield* loadProjectMetadataSnapshot(highWater);
const replay = toShellStream(
- applicationEvents.readApplicationEvents({
- afterSequence: input.afterSequence,
- throughSequence: highWater,
- // A row a newer build wrote (e.g. after a downgrade) must not fail
- // every shell resubscribe. The live tail below resumes from
- // highWater, so a dropped row never hides later events.
- skipUnknownEventTypes: true,
- }),
+ applicationEvents
+ .readApplicationEvents({
+ afterSequence: input.afterSequence,
+ throughSequence: highWater,
+ // A row a newer build wrote (e.g. after a downgrade) must not fail
+ // every shell resubscribe. The live tail below resumes from
+ // highWater, so a dropped row never hides later events.
+ skipUnknownEventTypes: true,
+ })
+ .pipe(Stream.filter(filterSubagentTranscript)),
);
return composeShellStreamWithEnrichment({
initial: initialEnrichmentItems(loaded),