Skip to content

Commit 5d822a4

Browse files
committed
fix(server): large archive resync snapshots no longer overflow the live buffer
A thread archived in a project the subscriber had not seen made the archive stream send a replacement snapshot through the live buffer. A snapshot over the buffer's 8 MiB limit failed the subscription instead of delivering it. The live segment now ends at that point and a new one starts from the fresh snapshot, which goes out like the initial snapshot, outside the live budget.
1 parent d41d345 commit 5d822a4

3 files changed

Lines changed: 126 additions & 36 deletions

File tree

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from "@effect/vitest";
22
import type {
33
ApplicationStoredEvent,
4+
OrchestrationV2ArchivedShellSnapshot,
45
OrchestrationV2ShellSnapshot,
56
OrchestrationV2ShellStreamItem,
67
OrchestrationV2StoredEvent,
@@ -18,6 +19,7 @@ import * as TestClock from "effect/testing/TestClock";
1819

1920
import {
2021
archivedShellItemsNeedSnapshot,
22+
archivedShellStream,
2123
archivedShellStreamItemFromThreadShell,
2224
buildActiveShellSnapshot,
2325
coalesceShellApplicationEvents,
@@ -292,6 +294,60 @@ describe("archivedShellItemsNeedSnapshot", () => {
292294
});
293295
});
294296

297+
describe("archivedShellStream", () => {
298+
const archivedAt = "2026-07-30T00:00:00.000Z" as never;
299+
const archivedSnapshot = (
300+
snapshotSequence: number,
301+
projectIds: ReadonlyArray<string>,
302+
title = "Project",
303+
): OrchestrationV2ArchivedShellSnapshot =>
304+
({
305+
schemaVersion: 1,
306+
snapshotSequence,
307+
projects: projectIds.map((id) => ({ id: ProjectId.make(id), title })),
308+
threads: [],
309+
}) as unknown as OrchestrationV2ArchivedShellSnapshot;
310+
const archived = (sequence: number, id: string, projectId: string) => ({
311+
kind: "thread.updated" as const,
312+
sequence,
313+
thread: shellFixture({
314+
id: ThreadId.make(id),
315+
projectId: ProjectId.make(projectId),
316+
archivedAt,
317+
}),
318+
});
319+
320+
it.effect("restarts from a fresh snapshot, outside the live budget, for an unseen project", () =>
321+
Effect.gen(function* () {
322+
// Larger than the live budget's byte limit.
323+
const fresh = archivedSnapshot(7, ["project-known", "project-new"], "x".repeat(9 << 20));
324+
const segments: Array<number> = [];
325+
const items = yield* archivedShellStream({
326+
snapshot: archivedSnapshot(1, ["project-known"]),
327+
loadSnapshot: Effect.succeed(fresh),
328+
batchesFrom: (afterSequence) => {
329+
segments.push(afterSequence);
330+
return afterSequence === 1
331+
? Stream.make(
332+
[archived(2, "thread-a", "project-known")],
333+
[archived(3, "thread-b", "project-new")],
334+
// Covered by the fresh snapshot, so the old segment must stop.
335+
[archived(4, "thread-c", "project-known")],
336+
)
337+
: Stream.make([archived(8, "thread-d", "project-new")]);
338+
},
339+
}).pipe(Stream.runCollect);
340+
341+
expect(segments).toEqual([1, 7]);
342+
expect(
343+
Array.from(items, (item) =>
344+
item.kind === "snapshot" ? item.snapshot.snapshotSequence : item.sequence,
345+
),
346+
).toEqual([1, 2, 7, 8]);
347+
}),
348+
);
349+
});
350+
295351
describe("shellStreamItemFromEnrichmentRefresh", () => {
296352
it("batches nearby completion roots onto one snapshot item", () => {
297353
expect(

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
ApplicationStoredEvent,
33
OrchestrationProjectShell,
4+
OrchestrationV2ArchivedShellSnapshot,
45
OrchestrationV2ArchivedShellStreamItem,
56
OrchestrationV2ShellSnapshot,
67
OrchestrationV2ThreadShell,
@@ -19,6 +20,9 @@ import * as Schema from "effect/Schema";
1920
import type * as SqlClient from "effect/sql/SqlClient";
2021
import * as Stream from "effect/Stream";
2122

23+
import { rpcInitialItems } from "../rpcInitialItems.ts";
24+
import { bufferLiveStream, type LiveStreamBufferError } from "./LiveStreamBudget.ts";
25+
2226
/** Build the regular navigation shell without duplicating the archive dataset. */
2327
export function buildActiveShellSnapshot(input: {
2428
readonly projects: ReadonlyArray<OrchestrationProjectShell>;
@@ -332,6 +336,52 @@ export function archivedShellItemsNeedSnapshot(
332336
);
333337
}
334338

339+
/**
340+
* Streams the archive from `snapshot`. When a batch archives a thread in a
341+
* project the subscriber has not seen, the live segment ends and a new one
342+
* starts from a fresh snapshot. Snapshots go out like the initial one, outside
343+
* the live budget, so a large archive cannot overflow it.
344+
*/
345+
export function archivedShellStream<E, R, E2, R2>(input: {
346+
readonly snapshot: OrchestrationV2ArchivedShellSnapshot;
347+
readonly loadSnapshot: Effect.Effect<OrchestrationV2ArchivedShellSnapshot, E, R>;
348+
readonly batchesFrom: (
349+
afterSequence: number,
350+
) => Stream.Stream<ReadonlyArray<OrchestrationV2ArchivedShellStreamItem>, E2, R2>;
351+
}): Stream.Stream<OrchestrationV2ArchivedShellStreamItem, E | E2 | LiveStreamBufferError, R | R2> {
352+
// Closures below must not reference `input`, or a delivered snapshot would
353+
// stay alive for the whole segment.
354+
const { loadSnapshot, batchesFrom } = input;
355+
const knownProjectIds = new Set(input.snapshot.projects.map((project) => project.id));
356+
let resync: OrchestrationV2ArchivedShellSnapshot | undefined;
357+
const live = batchesFrom(input.snapshot.snapshotSequence).pipe(
358+
Stream.mapEffect((items) =>
359+
archivedShellItemsNeedSnapshot(items, knownProjectIds)
360+
? Effect.map(loadSnapshot, (fresh) => {
361+
resync = fresh;
362+
return [];
363+
})
364+
: Effect.succeed(items),
365+
),
366+
Stream.takeUntil(() => resync !== undefined),
367+
Stream.flatMap(Stream.fromIterable),
368+
(stream) => bufferLiveStream(stream),
369+
);
370+
return Stream.concat(
371+
rpcInitialItems([{ kind: "snapshot" as const, snapshot: input.snapshot }]),
372+
Stream.concat(
373+
live,
374+
Stream.suspend(() => {
375+
const snapshot = resync;
376+
resync = undefined;
377+
return snapshot === undefined
378+
? Stream.empty
379+
: archivedShellStream({ snapshot, loadSnapshot, batchesFrom });
380+
}),
381+
),
382+
);
383+
}
384+
335385
/** Converts a committed event into an archive-only delta when it changes archive membership. */
336386
export function archivedShellStreamItemFromThreadShell(input: {
337387
readonly stored: OrchestrationV2StoredEvent;

‎apps/server/src/ws.ts‎

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ import {
6262
OrchestrationV2GetThreadProjectionError,
6363
OrchestrationV2ThreadLaunchError,
6464
type OrchestrationProjectShell,
65-
type OrchestrationV2ArchivedShellStreamItem,
6665
type OrchestrationV2ShellSnapshot,
6766
type ProjectEntriesFailure,
6867
type ProjectFileFailure,
@@ -126,7 +125,7 @@ import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
126125
import * as ScheduledTasks from "./scheduledTasks/ScheduledTaskService.ts";
127126
import * as SecretRequests from "./secrets/SecretRequests.ts";
128127
import {
129-
archivedShellItemsNeedSnapshot,
128+
archivedShellStream,
130129
archivedShellStreamItemFromThreadShell,
131130
buildActiveShellSnapshot,
132131
coalesceShellApplicationEvents,
@@ -1780,16 +1779,14 @@ const layerWsRpc = (
17801779
"ws.orchestrationV2.subscribeArchivedShell",
17811780
)(function* () {
17821781
const snapshot = yield* getOrchestrationV2ArchivedShellSnapshot;
1783-
const knownProjectIds = yield* Ref.make(
1784-
new Set(snapshot.projects.map((project) => project.id)),
1785-
);
1786-
const live = threadManagement
1787-
.streamStoredEventsFrom({ afterSequence: snapshot.snapshotSequence })
1788-
.pipe(
1789-
Stream.groupedWithin(512, Duration.millis(50)),
1790-
Stream.mapEffect((events) =>
1791-
Effect.gen(function* () {
1792-
const items = (yield* Effect.forEach(
1782+
return archivedShellStream({
1783+
snapshot,
1784+
loadSnapshot: getOrchestrationV2ArchivedShellSnapshot,
1785+
batchesFrom: (afterSequence) =>
1786+
threadManagement.streamStoredEventsFrom({ afterSequence }).pipe(
1787+
Stream.groupedWithin(512, Duration.millis(50)),
1788+
Stream.mapEffect((events) =>
1789+
Effect.forEach(
17931790
coalesceStoredThreadEvents(Array.from(events)),
17941791
(stored) =>
17951792
threadManagement
@@ -1800,31 +1797,18 @@ const layerWsRpc = (
18001797
),
18011798
),
18021799
{ concurrency: 8 },
1803-
)).filter((item) => item !== null);
1804-
if (!archivedShellItemsNeedSnapshot(items, yield* Ref.get(knownProjectIds))) {
1805-
return items;
1806-
}
1807-
const fresh = yield* getOrchestrationV2ArchivedShellSnapshot;
1808-
yield* Ref.set(
1809-
knownProjectIds,
1810-
new Set(fresh.projects.map((project) => project.id)),
1811-
);
1812-
return [{ kind: "snapshot" as const, snapshot: fresh }];
1813-
}),
1814-
),
1815-
Stream.flatMap((items) =>
1816-
Stream.fromIterable<OrchestrationV2ArchivedShellStreamItem>(items),
1817-
),
1818-
(stream) => bufferLiveStream(stream),
1819-
Stream.mapError(
1820-
(cause) =>
1821-
new OrchestrationV2GetShellSnapshotError({
1822-
message: "Failed while streaming archived threads",
1823-
cause,
1824-
}),
1800+
).pipe(Effect.map((items) => items.filter((item) => item !== null))),
1801+
),
18251802
),
1826-
);
1827-
return Stream.concat(rpcInitialItems([{ kind: "snapshot" as const, snapshot }]), live);
1803+
}).pipe(
1804+
Stream.mapError(
1805+
(cause) =>
1806+
new OrchestrationV2GetShellSnapshotError({
1807+
message: "Failed while streaming archived threads",
1808+
cause,
1809+
}),
1810+
),
1811+
);
18281812
});
18291813

18301814
const mutateProject = Effect.fn("ws.projects.mutate")(function* (mutation: ProjectMutation) {

0 commit comments

Comments
 (0)