Skip to content

Commit de44c6e

Browse files
Merge main into fix/issue-22364
2 parents ba593c8 + 22840ab commit de44c6e

9 files changed

Lines changed: 177 additions & 32 deletions

File tree

‎apps/server/src/orchestration-v2/LiveStreamBudget.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ describe("replayAndBufferLiveEvents", () => {
163163
const highWaterStarted = yield* Deferred.make<void>();
164164
const highWater = yield* Deferred.make<number>();
165165
const first = { sequence: 1, text: "first", threadId: "selected" };
166+
const unrelatedReplay = { sequence: 0, text: "unrelated replay", threadId: "other" };
166167
const overlap = { sequence: 2, text: "overlap", threadId: "selected" };
167168
const last = { sequence: 10, text: "last", threadId: "selected" };
168169
const reader = yield* replayAndBufferLiveEvents(
@@ -171,7 +172,7 @@ describe("replayAndBufferLiveEvents", () => {
171172
latestSequence: Deferred.succeed(highWaterStarted, undefined).pipe(
172173
Effect.andThen(Deferred.await(highWater)),
173174
),
174-
replay: () => Stream.make(first, overlap),
175+
replay: () => Stream.make(unrelatedReplay, first, overlap),
175176
filter: (event) => event.threadId === "selected",
176177
},
177178
{ maxItems: 4 },

‎apps/server/src/orchestration-v2/LiveStreamBudget.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ export const replayAndBufferProjectedLiveEvents = <
385385
// The finite database page is independent of the live tail.
386386
// Charge only the item being delivered so a fast reader can
387387
// consume a page larger than the budget without overflowing.
388+
Stream.filter((event) => input.filter?.(event) ?? true),
388389
Stream.rechunk(1),
389390
Stream.mapEffect((event) => budget.retain(input.project(event))),
390391
);

‎apps/server/src/orchestration-v2/ShellStream.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import type {
3+
ApplicationStoredEvent,
34
OrchestrationV2ShellSnapshot,
45
OrchestrationV2ShellStreamItem,
56
OrchestrationV2ThreadShell,
@@ -21,6 +22,7 @@ import {
2122
dedupeShellEnrichment,
2223
shellStreamItemFromEnrichmentRefresh,
2324
shellStreamItemsFromInitialSnapshot,
25+
shouldPublishShellApplicationEvent,
2426
skipUnchangedThreadShells,
2527
} from "./ShellStream.ts";
2628

@@ -32,6 +34,49 @@ const emptyShellSnapshot = {
3234
archivedThreads: [],
3335
} as OrchestrationV2ShellSnapshot;
3436

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+
3580
it.effect("coalesces a slow shell subscriber by identity and location in sequence order", () =>
3681
Effect.scoped(
3782
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/ShellStream.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,32 @@ export type ShellApplicationEvent =
106106
readonly event: Pick<OrchestrationV2StoredEvent["event"], "threadId">;
107107
};
108108

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+
109135
/** Shell updates refetch an aggregate; drop transcript bodies before retaining an event. */
110136
export function toShellApplicationEvent(stored: ApplicationStoredEvent): ShellApplicationEvent {
111137
return "aggregateKind" in stored

‎apps/server/src/persistence/OrchestrationEventStore.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export class OrchestrationEventStore extends Context.Service<
118118
readonly streamProjectedApplicationEvents: <A extends { readonly sequence: number }>(input: {
119119
readonly afterSequence?: number;
120120
readonly project: (event: ApplicationStoredEvent) => A;
121+
readonly filter?: (event: ApplicationStoredEvent) => boolean;
121122
}) => Stream.Stream<A, OrchestrationEventStoreError>;
122123
}
123124
>()("t3/persistence/OrchestrationEventStore") {}
@@ -716,11 +717,17 @@ const makeEventStore = Effect.gen(function* () {
716717

717718
const streamProjectedApplicationEvents: OrchestrationEventStore["Service"]["streamProjectedApplicationEvents"] =
718719
(input) =>
719-
replayAndBufferProjectedLiveEvents({
720+
replayAndBufferProjectedLiveEvents<
721+
ApplicationStoredEvent,
722+
ReturnType<typeof input.project>,
723+
OrchestrationEventStoreError,
724+
never
725+
>({
720726
subscribe: PubSub.subscribe(committedEvents),
721727
latestSequence: latestApplicationSequence,
722728
afterSequence: input.afterSequence ?? 0,
723729
project: input.project,
730+
...(input.filter === undefined ? {} : { filter: input.filter }),
724731
replay: (throughSequence) =>
725732
catchUpApplicationEvents({
726733
afterSequence: input?.afterSequence ?? 0,

‎apps/server/src/ws.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ it.live.each(["thread", "shell"] as const)(
252252
const items = yield* subscription.pipe(
253253
Effect.provideService(ThreadManagement.ThreadManagementService, {
254254
ensureLegacyTranscript: () => Effect.void,
255+
getShellSnapshot: () => Effect.succeed({ threads: [] }),
255256
streamStoredEventsFrom: (
256257
input: Parameters<
257258
ThreadManagement.ThreadManagementService["Service"]["streamStoredEventsFrom"]

‎apps/server/src/ws.ts‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
OrchestrationV2ThreadLaunchError,
6464
type OrchestrationProjectShell,
6565
type OrchestrationV2ShellSnapshot,
66+
type ApplicationStoredEvent,
6667
type ProjectEntriesFailure,
6768
type ProjectFileFailure,
6869
type ProjectFileOperation,
@@ -132,6 +133,7 @@ import {
132133
shellStreamItemFromThreadShell,
133134
shellStreamItemsFromInitialSnapshot,
134135
shellStreamItemsFromResumeSnapshot,
136+
shouldPublishShellApplicationEvent,
135137
skipUnchangedThreadShells,
136138
toShellApplicationEvent,
137139
type ShellApplicationEvent,
@@ -983,6 +985,9 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
983985
const projects = yield* ProjectStore.ProjectStoreV2;
984986
const projectService = yield* ProjectService.ProjectService;
985987
const projectEnrichment = yield* ProjectEnrichmentService.ProjectEnrichmentService;
988+
let subagentThreadIds = new Set<string>();
989+
const filterSubagentTranscript = (stored: ApplicationStoredEvent) =>
990+
shouldPublishShellApplicationEvent(stored, subagentThreadIds);
986991

987992
const enrichmentChanges = yield* projectEnrichment.subscribeChanges;
988993
const loadProjectMetadataSnapshot = Effect.fn("ws.orchestrationV2.loadProjectMetadataSnapshot")(
@@ -1009,6 +1014,11 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
10091014
latestSequence: applicationEvents.latestApplicationSequence,
10101015
}),
10111016
);
1017+
subagentThreadIds = new Set(
1018+
base.threads
1019+
.filter((thread) => thread.lineage.relationshipToParent === "subagent")
1020+
.map((thread) => thread.id),
1021+
);
10121022
const enriched = yield* enrichProjectShells(base.projects);
10131023
return {
10141024
snapshot: { ...base, projects: enriched.projects } as OrchestrationV2ShellSnapshot,
@@ -1071,6 +1081,7 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
10711081
applicationEvents.streamProjectedApplicationEvents({
10721082
afterSequence,
10731083
project: toShellApplicationEvent,
1084+
filter: filterSubagentTranscript,
10741085
}),
10751086
),
10761087
);
@@ -1161,16 +1172,25 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
11611172
});
11621173
}
11631174

1175+
const currentShell = yield* threadManagement.getShellSnapshot({ location: "active" });
1176+
subagentThreadIds = new Set(
1177+
currentShell.threads
1178+
.filter((thread) => thread.lineage.relationshipToParent === "subagent")
1179+
.map((thread) => thread.id),
1180+
);
1181+
11641182
const loaded = yield* loadProjectMetadataSnapshot(highWater);
11651183
const replay = toShellStream(
1166-
applicationEvents.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-
}),
1184+
applicationEvents
1185+
.readApplicationEvents({
1186+
afterSequence: input.afterSequence,
1187+
throughSequence: highWater,
1188+
// A row a newer build wrote (e.g. after a downgrade) must not fail
1189+
// every shell resubscribe. The live tail below resumes from
1190+
// highWater, so a dropped row never hides later events.
1191+
skipUnknownEventTypes: true,
1192+
})
1193+
.pipe(Stream.filter(filterSubagentTranscript)),
11741194
);
11751195
return composeShellStreamWithEnrichment({
11761196
initial: initialEnrichmentItems(loaded),

0 commit comments

Comments
 (0)