Skip to content

Commit 1cc05e0

Browse files
committed
perf(server): replay only the selected thread
1 parent c4353bc commit 1cc05e0

12 files changed

Lines changed: 781 additions & 94 deletions

‎apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ describe("OrchestrationEngine", () => {
325325
}),
326326
),
327327
hasEventAfter: () => Effect.succeed(false),
328+
readAggregateRange: () => Stream.die("unused aggregate replay"),
329+
getAggregateReplayStats: () => Effect.die("unused aggregate replay stats"),
328330
};
329331

330332
const projectionSnapshot = {
@@ -1235,6 +1237,8 @@ describe("OrchestrationEngine", () => {
12351237
return Stream.fromIterable(events);
12361238
},
12371239
hasEventAfter: () => Effect.succeed(false),
1240+
readAggregateRange: () => Stream.die("unused aggregate replay"),
1241+
getAggregateReplayStats: () => Effect.die("unused aggregate replay stats"),
12381242
};
12391243

12401244
const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), {
@@ -1473,6 +1477,8 @@ describe("OrchestrationEngine", () => {
14731477
return Stream.fromIterable(events);
14741478
},
14751479
hasEventAfter: () => Effect.succeed(false),
1480+
readAggregateRange: () => Stream.die("unused aggregate replay"),
1481+
getAggregateReplayStats: () => Effect.die("unused aggregate replay stats"),
14761482
};
14771483

14781484
let shouldFailProjection = true;

‎apps/server/src/orchestration/Layers/OrchestrationEngine.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,19 @@ const makeOrchestrationEngine = Effect.gen(function* () {
381381
const readEvents: OrchestrationEngineShape["readEvents"] = (fromSequenceExclusive, limit) =>
382382
eventStore.readFromSequence(fromSequenceExclusive, limit);
383383

384+
const readThreadEvents: OrchestrationEngineShape["readThreadEvents"] = ({ threadId, ...range }) =>
385+
eventStore.readAggregateRange({ ...range, aggregateKind: "thread", aggregateId: threadId });
386+
387+
const getThreadReplayStats: OrchestrationEngineShape["getThreadReplayStats"] = ({
388+
threadId,
389+
...range
390+
}) =>
391+
eventStore.getAggregateReplayStats({
392+
...range,
393+
aggregateKind: "thread",
394+
aggregateId: threadId,
395+
});
396+
384397
const dispatch: OrchestrationEngineShape["dispatch"] = (command, options) =>
385398
Effect.gen(function* () {
386399
const result = yield* Deferred.make<{ sequence: number }, OrchestrationDispatchError>();
@@ -395,6 +408,8 @@ const makeOrchestrationEngine = Effect.gen(function* () {
395408

396409
return {
397410
readEvents,
411+
readThreadEvents,
412+
getThreadReplayStats,
398413
dispatch,
399414
subscribeDomainEvents: PubSub.subscribe(eventPubSub).pipe(Effect.map(Stream.fromSubscription)),
400415
// Each access creates a fresh PubSub subscription so that multiple

‎apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ describe("ProviderCommandReactor", () => {
418418
const engine = yield* OrchestrationEngineService;
419419
return {
420420
readEvents: engine.readEvents,
421+
readThreadEvents: engine.readThreadEvents,
422+
getThreadReplayStats: engine.getThreadReplayStats,
421423
dispatch: (command) => {
422424
if (command.type === "thread.title.regeneration.complete") {
423425
titleRegenerationCompletionDispatchAttempts += 1;

‎apps/server/src/orchestration/Services/OrchestrationEngine.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {
1414
OrchestrationClientOrigin,
1515
OrchestrationCommand,
1616
OrchestrationEvent,
17+
ThreadId,
1718
} from "@t3tools/contracts";
1819
import * as Context from "effect/Context";
1920
import type * as Effect from "effect/Effect";
@@ -22,6 +23,13 @@ import type * as Stream from "effect/Stream";
2223

2324
import type { OrchestrationDispatchError } from "../Errors.ts";
2425
import type { OrchestrationEventStoreError } from "../../persistence/Errors.ts";
26+
import type { OrchestrationAggregateReplayStats } from "../../persistence/Services/OrchestrationEventStore.ts";
27+
28+
export interface OrchestrationThreadReplayRange {
29+
readonly threadId: ThreadId;
30+
readonly fromSequenceExclusive: number;
31+
readonly toSequenceInclusive: number;
32+
}
2533

2634
/**
2735
* OrchestrationEngineShape - Service API for orchestration command and event flow.
@@ -33,15 +41,24 @@ export interface OrchestrationEngineShape {
3341
* @param fromSequenceExclusive - Sequence cursor (exclusive).
3442
* @param limit - Maximum number of events to read. Defaults to the event
3543
* store's page-bounded default; pass a higher value when the caller must
36-
* read every event after the cursor (e.g. per-thread catch-up that filters
37-
* a small subset out of a potentially larger global range).
44+
* read a wider global range. Thread subscriptions use readThreadEvents.
3845
* @returns Stream containing ordered events.
3946
*/
4047
readonly readEvents: (
4148
fromSequenceExclusive: number,
4249
limit?: number,
4350
) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError, never>;
4451

52+
/** Read only this thread's events through a captured authoritative head. */
53+
readonly readThreadEvents: (
54+
input: OrchestrationThreadReplayRange & { readonly limit?: number },
55+
) => Stream.Stream<OrchestrationEvent, OrchestrationEventStoreError>;
56+
57+
/** Measure a bounded thread replay without decoding its event bodies. */
58+
readonly getThreadReplayStats: (
59+
input: OrchestrationThreadReplayRange & { readonly maxEvents: number },
60+
) => Effect.Effect<OrchestrationAggregateReplayStats, OrchestrationEventStoreError>;
61+
4562
/**
4663
* Dispatch a validated orchestration command.
4764
*

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

Lines changed: 183 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { CommandId, EventId, ProjectId } from "@t3tools/contracts";
1+
import {
2+
CommandId,
3+
EventId,
4+
MessageId,
5+
ProjectId,
6+
ThreadId,
7+
type OrchestrationEvent,
8+
} from "@t3tools/contracts";
29
import { assert, it } from "@effect/vitest";
310
import * as Effect from "effect/Effect";
411
import * as Layer from "effect/Layer";
@@ -12,6 +19,31 @@ import { OrchestrationEventStoreLive } from "./OrchestrationEventStore.ts";
1219
import { SqlitePersistenceMemory } from "./Sqlite.ts";
1320
const isPersistenceDecodeError = Schema.is(PersistenceDecodeError);
1421

22+
function messageEvent(threadId: ThreadId, id: string): Omit<OrchestrationEvent, "sequence"> {
23+
const now = "2026-01-01T00:00:00.000Z";
24+
return {
25+
type: "thread.message-sent",
26+
eventId: EventId.make(id),
27+
aggregateKind: "thread",
28+
aggregateId: threadId,
29+
occurredAt: now,
30+
commandId: null,
31+
causationEventId: null,
32+
correlationId: null,
33+
metadata: {},
34+
payload: {
35+
threadId,
36+
messageId: MessageId.make(id),
37+
role: "assistant",
38+
text: id,
39+
turnId: null,
40+
streaming: false,
41+
createdAt: now,
42+
updatedAt: now,
43+
},
44+
};
45+
}
46+
1547
const layer = it.layer(
1648
OrchestrationEventStoreLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
1749
);
@@ -79,7 +111,7 @@ layer("OrchestrationEventStore", (it) => {
79111
const sql = yield* SqlClient.SqlClient;
80112
const now = "2026-01-01T00:00:00.000Z";
81113

82-
yield* sql`
114+
const invalidRows = yield* sql<{ readonly sequence: number }>`
83115
INSERT INTO orchestration_events (
84116
event_id,
85117
aggregate_kind,
@@ -108,6 +140,7 @@ layer("OrchestrationEventStore", (it) => {
108140
${"{"},
109141
${"{}"}
110142
)
143+
RETURNING sequence
111144
`;
112145

113146
const replayResult = yield* Effect.result(
@@ -122,6 +155,154 @@ layer("OrchestrationEventStore", (it) => {
122155
),
123156
);
124157
}
158+
const scopedResult = yield* eventStore
159+
.readAggregateRange({
160+
aggregateKind: "project",
161+
aggregateId: "project-invalid-json",
162+
fromSequenceExclusive: 0,
163+
toSequenceInclusive: invalidRows[0]!.sequence,
164+
})
165+
.pipe(Stream.runCollect, Effect.result);
166+
assert.equal(scopedResult._tag, "Failure");
167+
if (scopedResult._tag === "Failure") {
168+
assert.ok(isPersistenceDecodeError(scopedResult.failure));
169+
assert.ok(
170+
scopedResult.failure.operation.includes(
171+
"OrchestrationEventStore.readAggregateRange:decodeRows",
172+
),
173+
);
174+
}
175+
}),
176+
);
177+
178+
it.effect("reads one aggregate through the captured head across pruned global gaps", () =>
179+
Effect.gen(function* () {
180+
const store = yield* OrchestrationEventStore;
181+
const sql = yield* SqlClient.SqlClient;
182+
const threadId = ThreadId.make("shared-stream-id");
183+
const first = yield* store.append(messageEvent(threadId, "scoped-first"));
184+
const pruned = yield* store.append(
185+
messageEvent(ThreadId.make("pruned-thread"), "pruned-event"),
186+
);
187+
const second = yield* store.append(messageEvent(threadId, "scoped-second"));
188+
// The same stream ID in a different aggregate is not part of this thread.
189+
// Its invalid JSON must never reach the event decoder.
190+
yield* sql`
191+
INSERT INTO orchestration_events (
192+
event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at,
193+
actor_kind, payload_json, metadata_json
194+
) VALUES (
195+
'same-id-project', 'project', ${threadId}, 0, 'project.created',
196+
'2026-01-01T00:00:00.000Z', 'server', '{', '{'
197+
), (
198+
'unrelated-invalid', 'thread', 'unrelated-invalid-thread', 0, 'thread.activity-appended',
199+
'2026-01-01T00:00:00.000Z', 'server', '{', '{'
200+
)
201+
`;
202+
const last = yield* store.append(messageEvent(threadId, "scoped-last"));
203+
yield* sql`DELETE FROM orchestration_events WHERE sequence = ${pruned.sequence}`;
204+
yield* store.append(messageEvent(threadId, "after-captured-head"));
205+
206+
const events = yield* store
207+
.readAggregateRange({
208+
aggregateKind: "thread",
209+
aggregateId: threadId,
210+
fromSequenceExclusive: first.sequence,
211+
toSequenceInclusive: last.sequence,
212+
limit: 100,
213+
})
214+
.pipe(Stream.runCollect);
215+
assert.deepEqual(
216+
events.map((event) => event.sequence),
217+
[second.sequence, last.sequence],
218+
);
219+
}),
220+
);
221+
222+
it.effect("bounds thread replay metadata and counts UTF-8 bytes without decoding payloads", () =>
223+
Effect.gen(function* () {
224+
const store = yield* OrchestrationEventStore;
225+
const sql = yield* SqlClient.SqlClient;
226+
const rows = yield* sql<{ readonly sequence: number }>`
227+
INSERT INTO orchestration_events (
228+
event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at,
229+
actor_kind, payload_json, metadata_json
230+
) VALUES
231+
('stats-1', 'thread', 'stats-thread', 0, 'thread.message-sent',
232+
'2026-01-01T00:00:00.000Z', 'provider', '{"output":"😀"}', '{}'),
233+
('stats-unrelated', 'thread', 'another-thread', 0, 'thread.created',
234+
'2026-01-01T00:00:00.000Z', 'provider', printf('%.*c', 10000, 'x'), '{}'),
235+
('stats-2', 'thread', 'stats-thread', 1, 'thread.activity-appended',
236+
'2026-01-01T00:00:00.000Z', 'provider', '{', '{}'),
237+
('stats-other-kind', 'project', 'stats-thread', 0, 'project.deleted',
238+
'2026-01-01T00:00:00.000Z', 'provider', printf('%.*c', 20000, 'x'), '{}'),
239+
('stats-3', 'thread', 'stats-thread', 2, 'thread.deleted',
240+
'2026-01-01T00:00:00.000Z', 'provider', '{"output":"é"}', '{}'),
241+
('stats-4', 'thread', 'stats-thread', 3, 'thread.created',
242+
'2026-01-01T00:00:00.000Z', 'provider', printf('%.*c', 2000, 'x'), '{}')
243+
RETURNING sequence
244+
`;
245+
const range = {
246+
aggregateKind: "thread" as const,
247+
aggregateId: "stats-thread",
248+
fromSequenceExclusive: 0,
249+
toSequenceInclusive: rows.at(-1)!.sequence,
250+
};
251+
assert.deepEqual(yield* store.getAggregateReplayStats({ ...range, maxEvents: 2 }), {
252+
eventCount: 3,
253+
payloadBytes: 33,
254+
hasCreateEvent: false,
255+
});
256+
assert.deepEqual(yield* store.getAggregateReplayStats({ ...range, maxEvents: 10 }), {
257+
eventCount: 4,
258+
payloadBytes: 2033,
259+
hasCreateEvent: true,
260+
});
261+
assert.deepEqual(
262+
yield* store.getAggregateReplayStats({
263+
...range,
264+
toSequenceInclusive: rows[2]!.sequence,
265+
maxEvents: 10,
266+
}),
267+
{
268+
eventCount: 2,
269+
payloadBytes: 18,
270+
hasCreateEvent: false,
271+
},
272+
);
273+
}),
274+
);
275+
276+
it.effect("keeps later pages below the captured head when new events are appended", () =>
277+
Effect.gen(function* () {
278+
const store = yield* OrchestrationEventStore;
279+
const threadId = ThreadId.make("paged-thread");
280+
const persisted = yield* Effect.forEach(
281+
Array.from({ length: 502 }, (_, index) => index),
282+
(index) => store.append(messageEvent(threadId, `paged-${index}`)),
283+
);
284+
const head = persisted.at(-1)!.sequence;
285+
let appendedDuringReplay = false;
286+
const replayed = yield* store
287+
.readAggregateRange({
288+
aggregateKind: "thread",
289+
aggregateId: threadId,
290+
fromSequenceExclusive: 0,
291+
toSequenceInclusive: head,
292+
limit: 1_000,
293+
})
294+
.pipe(
295+
Stream.tap(() => {
296+
if (appendedDuringReplay) return Effect.void;
297+
appendedDuringReplay = true;
298+
return store.append(messageEvent(threadId, "appended-during-replay"));
299+
}),
300+
Stream.runCollect,
301+
);
302+
assert.deepEqual(
303+
replayed.map((event) => event.sequence),
304+
persisted.map((event) => event.sequence),
305+
);
125306
}),
126307
);
127308
});

0 commit comments

Comments
 (0)