The best way to control Claude Code, Codex and more from any computer or phone
perf(server): prune events the projections already cover (#29806)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 3, 2026 at 12:13 UTC
550c5fa34b6b4f287496f3d725a300347274fbcf
22 files changed
+1144
-1204
apps/server/src/orchestration-v2/FoundationPersistence.test.ts
+31
-113
@@ -62,6 +62,7 @@ import * as EventSink from "./EventSink.ts";
62
import * as EventStore from "./EventStore.ts";
63
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
64
import * as ProjectionMaintenance from "./ProjectionMaintenance.ts";
65
+import * as ProjectionReplay from "./testkit/ProjectionReplay.ts";
66
import * as ProjectionStore from "./ProjectionStore.ts";
67
import * as ProjectStore from "./ProjectStore.ts";
68
import * as ProviderRuntimeRecovery from "./ProviderRuntimeRecoveryService.ts";
@@ -85,6 +86,9 @@ const layerCommandReceiptStoreProvided = CommandReceiptStore.layer.pipe(
86
);
87
const layerIsolatedOutbox = Layer.fresh(EffectOutbox.layer.pipe(Layer.provideMerge(layerDatabase)));
88
const layerProjectionMaintenanceProvided = ProjectionMaintenance.layer.pipe(
89
+ Layer.provide(layerDatabase),
90
+);
91
+const layerProjectionReplayProvided = ProjectionReplay.layer.pipe(
92
Layer.provide(layerStoresProvided),
93
);
94
const layerTest = Layer.mergeAll(
@@ -94,6 +98,7 @@ const layerTest = Layer.mergeAll(
98
layerCommandReceiptStoreProvided,
99
IdAllocator.layer,
100
layerProjectionMaintenanceProvided,
101
+ layerProjectionReplayProvided,
102
);
103
104
const providerInstanceId = ProviderInstanceId.make("codex");
@@ -148,85 +153,6 @@ function threadCreatedEvent(input: {
153
};
154
}
155
151
-it.effect("rebuilds event history one bounded page at a time", () =>
152
- Effect.gen(function* () {
153
- const eventStore = yield* EventStore.EventStoreV2;
154
- const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
155
- const now = yield* DateTime.now;
156
- const threadId = ThreadId.make("thread:foundation-paged-rebuild");
157
- const thread = makeThread(threadId, now);
158
- const eventCount = 1_005;
159
- yield* eventStore.append({
160
- events: [
161
- threadCreatedEvent({ id: "event:paged-rebuild:0", thread, now }),
162
- ...Array.from({ length: eventCount - 1 }, (_, index) => ({
163
- id: EventId.make(`event:paged-rebuild:${index + 1}`),
164
- type: "thread.metadata-updated" as const,
165
- threadId,
166
- occurredAt: now,
167
- payload: { ...thread, title: `Rebuilt update ${index + 1}` },
168
- })),
169
- ],
170
- });
171
- let applied = 0;
172
- let failAfterFirstPage = false;
173
- const appliedAtRead: Array<number> = [];
174
- const layerObservedStores = Layer.mergeAll(
175
- Layer.succeed(EventStore.EventStoreV2, {
176
- ...eventStore,
177
- read: (input) =>
178
- Stream.suspend(() => {
179
- appliedAtRead.push(applied);
180
- if (failAfterFirstPage && applied >= 500) {
181
- return Stream.fail(
182
- new EventStore.EventStoreReadEventsError({ afterSequence: input?.afterSequence }),
183
- );
184
- }
185
- return eventStore.read(input);
186
- }),
187
- }),
188
- Layer.succeed(ProjectionStore.ProjectionStoreV2, {
189
- ...projectionStore,
190
- apply: (event) =>
191
- projectionStore.apply(event).pipe(
192
- Effect.tap(() =>
193
- Effect.sync(() => {
194
- applied += 1;
195
- }),
196
- ),
197
- ),
198
- }),
199
- );
200
- const rebuild = Effect.gen(function* () {
201
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
202
- return yield* maintenance.rebuild;
203
- }).pipe(
204
- Effect.provide(
205
- Layer.fresh(ProjectionMaintenance.layer.pipe(Layer.provide(layerObservedStores))),
206
- ),
207
- );
208
- const rebuilt = yield* rebuild;
209
- assert.isTrue(rebuilt.valid);
210
- assert.equal(applied, eventCount);
211
- assert.deepEqual(appliedAtRead, [0, 500, 1_000]);
212
- assert.equal(
213
- (yield* projectionStore.getThreadProjection(threadId)).thread.title,
214
- "Rebuilt update 1004",
215
- );
216
- applied = 0;
217
- failAfterFirstPage = true;
218
- const failed = yield* Effect.exit(rebuild);
219
- assert.equal(failed._tag, "Failure");
220
- assert.equal(applied, 500);
221
- assert.equal(
222
- (yield* projectionStore.getThreadProjection(threadId)).thread.title,
223
- "Rebuilt update 1004",
224
- );
225
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
226
- assert.isTrue((yield* maintenance.verify).valid);
227
- }).pipe(Effect.provide(layerTest)),
228
-);
229
-
156
const seedCompaction = Effect.fnUntraced(function* () {
157
const sql = yield* SqlClient.SqlClient;
158
yield* sql`INSERT INTO orchestration_v2_legacy_imports (
@@ -240,6 +166,13 @@ const seedCompaction = Effect.fnUntraced(function* () {
166
) SELECT 'event-' || n, 'thread', 'thread', n,
167
CASE WHEN n IN (1, 51, 101) THEN 'message.updated' ELSE 'thread.visited' END,
168
'2026-10-17', 'server', '{"id":"message"}', '{}', 2 FROM history`;
169
+ // A pass a day ago marked the whole log; projections cover it.
170
+ yield* sql`INSERT INTO orchestration_v2_projection_metadata (
171
+ projection_name, schema_version, last_sequence, updated_at
172
+ ) VALUES
173
+ ('event-log-prune-mark', 2, 151, '1969-12-30T00:00:00.000Z'),
174
+ ('thread-projections', 2, 151, '1969-12-30T00:00:00.000Z')
175
+ ON CONFLICT(projection_name) DO UPDATE SET last_sequence = excluded.last_sequence`;
176
yield* sql`WITH RECURSIVE history(n) AS (
177
SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 100
178
) INSERT INTO orchestration_command_receipts (
@@ -284,11 +217,12 @@ it.effect("discovers on the reader worker and deletes on the writer", () =>
217
}),
218
);
219
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
287
- const result = yield* maintenance.compactEventStore();
288
- assert.equal(result.deletedEventCount, 148);
220
+ const result = yield* maintenance.pruneEventStore();
221
+ assert.equal(result.deletedEventCount, 150);
222
assert.equal(result.deletedReceiptCount, 100);
223
+ // Only the in-transaction check for rows appended during the pass reads on the writer.
224
assert.deepEqual(
291
- reads.filter((query) => /\bSELECT\b/.test(query)),
225
+ reads.filter((query) => /\bSELECT\b/.test(query) && !query.includes("AS prompt_sequence")),
226
[],
227
);
228
assert(writes.some((query) => query.includes("DELETE FROM orchestration_events")));
@@ -296,7 +230,6 @@ it.effect("discovers on the reader worker and deletes on the writer", () =>
230
}).pipe(
231
Effect.provide(
232
ProjectionMaintenance.layer.pipe(
299
- Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
233
Layer.provideMerge(SqlitePersistence.layerFromPath(filename)),
234
),
235
),
@@ -318,7 +251,7 @@ it.effect("paces scheduled compaction while preserving retention state and fixed
251
const tracer = Tracer.make({
252
span(options) {
253
const span = new Tracer.NativeSpan(options);
321
- if (span.name === "ProjectionMaintenance.compactEventStore") compactionSpans.push(span);
254
+ if (span.name === "ProjectionMaintenance.pruneEventStore") compactionSpans.push(span);
255
const end = span.end.bind(span);
256
span.end = (time, exit) => {
257
end(time, exit);
@@ -377,15 +310,13 @@ it.effect("paces scheduled compaction while preserving retention state and fixed
310
},
311
}),
312
);
380
- assert.deepEqual(pauses, Array(5).fill(270));
313
+ assert.deepEqual(pauses, Array(8).fill(270));
314
assert.deepEqual(
315
compactionSpans.map((span) => span.status._tag),
316
["Ended"],
317
);
318
+ // 151 stopped being the stream head when 152 committed mid-pass; 152 is past the bound.
319
assert.deepEqual(yield* sql`SELECT sequence FROM orchestration_events ORDER BY sequence`, [
386
- { sequence: 1 },
387
- { sequence: 101 },
388
- { sequence: 151 },
320
{ sequence: 152 },
321
]);
322
assert.deepEqual(yield* sql`SELECT command_id FROM orchestration_command_receipts`, [
@@ -768,7 +699,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
699
Effect.gen(function* () {
700
const eventSink = yield* EventSink.EventSinkV2;
701
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
771
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
702
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
703
const now = yield* DateTime.now;
704
const firstThreadId = ThreadId.make("thread:foundation-shared-session:first");
705
const secondThreadId = ThreadId.make("thread:foundation-shared-session:second");
@@ -829,7 +760,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
760
}),
761
);
762
832
- it.effect("compacts superseded state events, imported v1 events, and legacy receipts", () =>
763
+ it.effect("prunes covered history, imported v1 events, and legacy receipts", () =>
764
Effect.gen(function* () {
765
const eventSink = yield* EventSink.EventSinkV2;
766
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
@@ -983,9 +914,12 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
914
ON CONFLICT(command_id) DO NOTHING
915
`;
916
986
- const summary = yield* maintenance.compactEventStore();
917
+ // The first pass only marks the head and drops imported legacy rows.
918
+ const marked = yield* maintenance.pruneEventStore();
919
+ yield* TestClock.adjust("1 day");
920
+ const summary = yield* maintenance.pruneEventStore();
921
assert.isAtLeast(summary.deletedEventCount, 504);
988
- assert.isAtLeast(summary.deletedReceiptCount, 1);
922
+ assert.equal(marked.deletedReceiptCount + summary.deletedReceiptCount, 1);
923
924
const remaining = yield* sql<{ readonly event_id: string }>`
925
SELECT event_id
@@ -996,18 +930,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
930
`;
931
assert.deepEqual(
932
remaining.map((row) => row.event_id),
999
- [
1000
- "event:foundation-compact:create",
1001
- "event:foundation-compact:meta",
1002
- "event:foundation-compact:message-1",
1003
- "event:foundation-compact:node-1",
1004
- "event:foundation-compact:item-1",
1005
- "event:foundation-compact:visit-2",
1006
- "event:foundation-compact:message-2",
1007
- "event:foundation-compact:node-2",
1008
- "event:foundation-compact:item-2",
1009
- "event:foundation-compact:tags-2",
1010
- ],
933
+ ["event:foundation-compact:create", "event:foundation-compact:tags-2"],
934
);
935
936
const remainingV1 = yield* sql<{ readonly stream_id: string }>`
@@ -1031,8 +954,6 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
954
["command:foundation-compact:pending"],
955
);
956
1034
- assert.isTrue((yield* maintenance.verify).valid);
1035
- assert.isTrue((yield* maintenance.rebuild).valid);
957
assert.deepEqual(yield* projections.getThreadProjection(threadId), beforeCompaction);
958
}),
959
);
@@ -1041,7 +962,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
962
Effect.gen(function* () {
963
const eventSink = yield* EventSink.EventSinkV2;
964
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
1044
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
965
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
966
const sql = yield* SqlClient.SqlClient;
967
const now = yield* DateTime.now;
968
const parentThreadId = ThreadId.make("thread:foundation-cross-thread:parent");
@@ -3152,7 +3073,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
3073
Effect.gen(function* () {
3074
const eventSink = yield* EventSink.EventSinkV2;
3075
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
3155
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
3076
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
3077
const sql = yield* SqlClient.SqlClient;
3078
const now = yield* DateTime.now;
3079
const threadId = ThreadId.make("thread:foundation-many-items");
@@ -3384,10 +3305,7 @@ it.effect.each(["events", "receipts"] as const)(
3305
const path = yield* Path.Path;
3306
const directory = yield* fs.makeTempDirectoryScoped({ prefix: "compaction-reader-" });
3307
const database = SqlitePersistence.layerFromPath(path.join(directory, "statev2.sqlite"));
3387
- const maintenanceLayer = ProjectionMaintenance.layer.pipe(
3388
- Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
3389
- Layer.provideMerge(database),
3390
- );
3308
+ const maintenanceLayer = ProjectionMaintenance.layer.pipe(Layer.provideMerge(database));
3309
yield* Effect.gen(function* () {
3310
const sql = yield* SqlClient.SqlClient;
3311
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
@@ -3434,7 +3352,7 @@ it.effect.each(["events", "receipts"] as const)(
3352
: Effect.succeed(statement);
3353
};
3354
const compacted = yield* Deferred.make<void>();
3437
- const compaction = yield* maintenance.compactEventStore().pipe(
3355
+ const compaction = yield* maintenance.pruneEventStore().pipe(
3356
Effect.provideService(Statement.CurrentTransformer, observeDiscovery),
3357
Effect.provideService(Statement.SpanPropagationEnabled, true),
3358
Effect.tap(() => Deferred.succeed(compacted, undefined)),
apps/server/src/orchestration-v2/ProjectionMaintenance.test.ts
new
+385
@@ -0,0 +1,385 @@
1
+import { assert, it } from "@effect/vitest";
2
+import {
3
+ EventId,
4
+ NodeId,
5
+ PlanId,
6
+ type OrchestrationV2AppThread,
7
+ type OrchestrationV2DomainEvent,
8
+ type OrchestrationV2TurnItem,
9
+ ProjectId,
10
+ ProviderDriverKind,
11
+ ProviderInstanceId,
12
+ ThreadId,
13
+ TurnItemId,
14
+} from "@t3tools/contracts";
15
+import * as DateTime from "effect/DateTime";
16
+import * as Deferred from "effect/Deferred";
17
+import * as Effect from "effect/Effect";
18
+import * as Fiber from "effect/Fiber";
19
+import * as Layer from "effect/Layer";
20
+import * as Stream from "effect/Stream";
21
+import * as TestClock from "effect/testing/TestClock";
22
+import * as SqlClient from "effect/sql/SqlClient";
23
+import * as Statement from "effect/sql/Statement";
24
+
25
+import * as SqlitePersistence from "../persistence/Sqlite.ts";
26
+import * as EventSink from "./EventSink.ts";
27
+import * as EventStore from "./EventStore.ts";
28
+import * as ProjectionMaintenance from "./ProjectionMaintenance.ts";
29
+import * as ProjectionStore from "./ProjectionStore.ts";
30
+import * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts";
31
+
32
+const database = SqlitePersistence.layerMemory;
33
+const eventStoreProvided = EventStore.layer.pipe(Layer.provideMerge(database));
34
+const projectionStoreProvided = ProjectionStore.layer.pipe(Layer.provideMerge(database));
35
+const stores = Layer.mergeAll(database, eventStoreProvided, projectionStoreProvided);
36
+const layerTest = Layer.mergeAll(
37
+ stores,
38
+ OrchestrationEventStore.layer.pipe(Layer.provide(database)),
39
+ EventSink.layer.pipe(Layer.provide(stores)),
40
+ ProjectionMaintenance.layer.pipe(Layer.provide(stores)),
41
+);
42
+
43
+const providerInstanceId = ProviderInstanceId.make("codex");
44
+const providerDriver = ProviderDriverKind.make("codex");
45
+const modelSelection = { instanceId: providerInstanceId, model: "gpt-5.4" };
46
+
47
+function makeThread(threadId: ThreadId, now: DateTime.Utc): OrchestrationV2AppThread {
48
+ return {
49
+ id: threadId,
50
+ projectId: ProjectId.make(`project:${threadId}`),
51
+ title: "Turn item compaction",
52
+ providerInstanceId,
53
+ modelSelection,
54
+ runtimeMode: "full-access",
55
+ interactionMode: "default",
56
+ branch: null,
57
+ worktreePath: null,
58
+ activeProviderThreadId: null,
59
+ lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null },
60
+ forkedFrom: null,
61
+ createdBy: "user",
62
+ creationSource: "web",
63
+ createdAt: now,
64
+ updatedAt: now,
65
+ archivedAt: null,
66
+ settledOverride: null,
67
+ settledAt: null,
68
+ lastVisitedAt: null,
69
+ deletedAt: null,
70
+ };
71
+}
72
+
73
+function threadCreated(thread: OrchestrationV2AppThread, now: DateTime.Utc) {
74
+ return {
75
+ id: EventId.make("event:turn-item-compaction:thread"),
76
+ type: "thread.created",
77
+ threadId: thread.id,
78
+ providerInstanceId,
79
+ occurredAt: now,
80
+ payload: thread,
81
+ } satisfies OrchestrationV2DomainEvent;
82
+}
83
+
84
+const itemSpecs = [
85
+ { key: "command", type: "command_execution", ordinal: 10 },
86
+ { key: "reasoning", type: "reasoning", ordinal: 20 },
87
+ { key: "todo", type: "todo_list", ordinal: 30 },
88
+ { key: "commanded", type: "command_execution", ordinal: 40 },
89
+] as const;
90
+
91
+function itemEvent(
92
+ threadId: ThreadId,
93
+ now: DateTime.Utc,
94
+ spec: (typeof itemSpecs)[number],
95
+ revision: number,
96
+ eventId = `event:turn-item-compaction:${spec.key}:${revision}`,
97
+): OrchestrationV2DomainEvent {
98
+ const id = TurnItemId.make(`turn-item:compaction:${spec.key}`);
99
+ const growing = `${spec.key}:${revision}:` + "x".repeat((revision + 1) * 192);
100
+ const completed = revision === 69;
101
+ const base = {
102
+ id,
103
+ threadId,
104
+ runId: null,
105
+ nodeId: null,
106
+ providerThreadId: null,
107
+ providerTurnId: null,
108
+ nativeItemRef: null,
109
+ parentItemId: null,
110
+ ordinal: spec.ordinal,
111
+ status: completed ? ("completed" as const) : ("running" as const),
112
+ title: spec.key,
113
+ startedAt: now,
114
+ completedAt: completed ? now : null,
115
+ updatedAt: now,
116
+ };
117
+ let payload: OrchestrationV2TurnItem;
118
+ switch (spec.type) {
119
+ case "command_execution":
120
+ payload = {
121
+ ...base,
122
+ type: "command_execution",
123
+ input: `run ${spec.key}`,
124
+ output: growing,
125
+ };
126
+ break;
127
+ case "reasoning":
128
+ payload = { ...base, type: "reasoning", text: growing, streaming: !completed };
129
+ break;
130
+ case "todo_list":
131
+ payload = {
132
+ ...base,
133
+ type: "todo_list",
134
+ planId: PlanId.make("plan:turn-item-compaction"),
135
+ steps: [{ id: "step", text: growing, status: completed ? "completed" : "running" }],
136
+ explanation: growing,
137
+ };
138
+ break;
139
+ }
140
+ return {
141
+ id: EventId.make(eventId),
142
+ type: "turn-item.updated",
143
+ threadId,
144
+ occurredAt: now,
145
+ payload,
146
+ };
147
+}
148
+
149
+function subagentEvents(
150
+ threadId: ThreadId,
151
+ now: DateTime.Utc,
152
+ revision: number,
153
+ eventSuffix = String(revision),
154
+): ReadonlyArray<OrchestrationV2DomainEvent> {
155
+ const id = NodeId.make("subagent:turn-item-compaction");
156
+ const prompt = revision === 0 ? "Review prompt A. ".repeat(40) : "Review prompt B. ".repeat(40);
157
+ const progress = `Review progress ${revision}`;
158
+ return [
159
+ {
160
+ id: EventId.make(`event:turn-item-compaction:subagent:${eventSuffix}`),
161
+ type: "subagent.updated",
162
+ threadId,
163
+ occurredAt: now,
164
+ payload: {
165
+ id,
166
+ threadId,
167
+ runId: null,
168
+ parentNodeId: NodeId.make("node:turn-item-compaction-parent"),
169
+ origin: "provider_native",
170
+ createdBy: "agent",
171
+ driver: providerDriver,
172
+ providerInstanceId,
173
+ providerThreadId: null,
174
+ childThreadId: null,
175
+ nativeTaskRef: {
176
+ driver: providerDriver,
177
+ nativeId: "compaction-review",
178
+ strength: "strong",
179
+ },
180
+ prompt,
181
+ progress,
182
+ title: "Review",
183
+ model: null,
184
+ status: "running",
185
+ result: null,
186
+ startedAt: now,
187
+ completedAt: null,
188
+ updatedAt: now,
189
+ },
190
+ },
191
+ {
192
+ id: EventId.make(`event:turn-item-compaction:subagent-item:${eventSuffix}`),
193
+ type: "turn-item.updated",
194
+ threadId,
195
+ occurredAt: now,
196
+ payload: {
197
+ id: TurnItemId.make("turn-item:compaction:subagent"),
198
+ threadId,
199
+ runId: null,
200
+ nodeId: null,
201
+ providerThreadId: null,
202
+ providerTurnId: null,
203
+ nativeItemRef: null,
204
+ parentItemId: null,
205
+ ordinal: 50,
206
+ status: "running",
207
+ title: "Review",
208
+ startedAt: now,
209
+ completedAt: null,
210
+ updatedAt: now,
211
+ type: "subagent",
212
+ subagentId: id,
213
+ origin: "provider_native",
214
+ driver: providerDriver,
215
+ providerInstanceId,
216
+ childThreadId: null,
217
+ prompt,
218
+ progress,
219
+ result: null,
220
+ },
221
+ },
222
+ ] satisfies ReadonlyArray<OrchestrationV2DomainEvent>;
223
+}
224
+
225
+const insertRunningRunUpdate = (threadId: ThreadId) =>
226
+ Effect.flatMap(
227
+ SqlClient.SqlClient,
228
+ (sql) => sql<{ readonly sequence: number }>`
229
+ INSERT INTO orchestration_events (
230
+ event_id, aggregate_kind, stream_id, stream_version, event_type,
231
+ occurred_at, actor_kind, payload_json, metadata_json, application_event_version
232
+ ) VALUES (
233
+ 'event:prune:run-running', 'thread', ${threadId},
234
+ (SELECT MAX(stream_version) + 1 FROM orchestration_events WHERE stream_id = ${threadId}),
235
+ 'run.updated', '2026-11-01T00:00:00.000Z', 'server', '{"status":"running"}', '{}', 2
236
+ ) RETURNING sequence
237
+ `,
238
+ );
239
+
240
+const remainingEventIds = (threadId: ThreadId) =>
241
+ Effect.flatMap(
242
+ SqlClient.SqlClient,
243
+ (sql) => sql<{ readonly event_id: string }>`
244
+ SELECT event_id FROM orchestration_events WHERE stream_id = ${threadId} ORDER BY sequence
245
+ `,
246
+ ).pipe(Effect.map((rows) => rows.map((row) => row.event_id)));
247
+
248
+it.effect("prunes events the projections cover once the retained day has passed", () =>
249
+ Effect.gen(function* () {
250
+ const sink = yield* EventSink.EventSinkV2;
251
+ const eventStore = yield* EventStore.EventStoreV2;
252
+ const events = yield* OrchestrationEventStore.OrchestrationEventStore;
253
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
254
+ const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
255
+ const now = yield* DateTime.now;
256
+ const threadId = ThreadId.make("thread:prune");
257
+ yield* sink.write({ events: [threadCreated(makeThread(threadId, now), now)] });
258
+ for (const revision of [0, 1, 2]) {
259
+ yield* sink.write({ events: [itemEvent(threadId, now, itemSpecs[0], revision)] });
260
+ }
261
+ yield* sink.write({ events: subagentEvents(threadId, now, 0) });
262
+ yield* insertRunningRunUpdate(threadId);
263
+ yield* sink.write({ events: [itemEvent(threadId, now, itemSpecs[1], 0)] });
264
+
265
+ // The first pass only marks the head; nothing is a day old yet.
266
+ assert.equal((yield* maintenance.pruneEventStore()).deletedEventCount, 0);
267
+ const markedThrough = yield* eventStore.latestSequence();
268
+
269
+ // Repeating prompt A borrows it from the anchors written before the mark.
270
+ yield* sink.write({ events: subagentEvents(threadId, now, 0, "repeat") });
271
+ yield* sink.write({ events: [itemEvent(threadId, now, itemSpecs[0], 3)] });
272
+ const highWater = yield* eventStore.latestSequence();
273
+ const projection = yield* projections.getThreadProjection(threadId);
274
+
275
+ yield* TestClock.adjust("23 hours");
276
+ assert.equal((yield* maintenance.pruneEventStore()).deletedEventCount, 0);
277
+ yield* TestClock.adjust("1 hour");
278
+ assert.equal((yield* maintenance.pruneEventStore()).deletedEventCount, 4);
279
+
280
+ assert.deepEqual(yield* remainingEventIds(threadId), [
281
+ "event:turn-item-compaction:thread",
282
+ "event:turn-item-compaction:subagent:0",
283
+ "event:turn-item-compaction:subagent-item:0",
284
+ "event:prune:run-running",
285
+ "event:turn-item-compaction:subagent:repeat",
286
+ "event:turn-item-compaction:subagent-item:repeat",
287
+ "event:turn-item-compaction:command:3",
288
+ ]);
289
+ assert.equal(yield* events.prunedThroughSequence, markedThrough);
290
+ assert.equal(yield* eventStore.latestSequence(), highWater);
291
+ assert.equal(yield* eventStore.latestSequence({ threadId }), highWater);
292
+ assert.deepEqual(yield* projections.getThreadProjection(threadId), projection);
293
+ const repeated = yield* eventStore
294
+ .read({ threadId, eventType: "subagent.updated", afterSequence: markedThrough })
295
+ .pipe(Stream.runCollect);
296
+ assert.deepEqual(
297
+ Array.from(repeated, (stored) =>
298
+ stored.event.type === "subagent.updated" ? stored.event.payload.prompt : null,
299
+ ),
300
+ ["Review prompt A. ".repeat(40)],
301
+ );
302
+ }).pipe(Effect.provide(layerTest)),
303
+);
304
+
305
+it.effect("keeps the anchor a retained stream head borrows from", () =>
306
+ Effect.gen(function* () {
307
+ const sink = yield* EventSink.EventSinkV2;
308
+ const eventStore = yield* EventStore.EventStoreV2;
309
+ const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
310
+ const now = yield* DateTime.now;
311
+ const threadId = ThreadId.make("thread:prune-head");
312
+ yield* sink.write({ events: [threadCreated(makeThread(threadId, now), now)] });
313
+ yield* sink.write({ events: subagentEvents(threadId, now, 0) });
314
+ // The stream's newest row borrows prompt A, so it is kept below the boundary.
315
+ yield* sink.write({ events: subagentEvents(threadId, now, 0, "repeat") });
316
+ yield* maintenance.pruneEventStore();
317
+ yield* TestClock.adjust("1 day");
318
+ assert.isAbove((yield* maintenance.pruneEventStore()).deletedEventCount, 0);
319
+
320
+ assert.include(
321
+ yield* remainingEventIds(threadId),
322
+ "event:turn-item-compaction:subagent-item:0",
323
+ );
324
+ const stored = yield* eventStore.read({ threadId }).pipe(Stream.runCollect);
325
+ const head = Array.from(stored).at(-1)?.event;
326
+ assert.deepEqual(
327
+ head?.type === "turn-item.updated" && head.payload.type === "subagent"
328
+ ? head.payload.prompt
329
+ : null,
330
+ "Review prompt A. ".repeat(40),
331
+ );
332
+ }).pipe(Effect.provide(layerTest)),
333
+);
334
+
335
+it.effect("keeps an anchor that a row appended during the pass borrows from", () =>
336
+ Effect.scoped(
337
+ Effect.gen(function* () {
338
+ const sink = yield* EventSink.EventSinkV2;
339
+ const eventStore = yield* EventStore.EventStoreV2;
340
+ const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
341
+ const now = yield* DateTime.now;
342
+ const threadId = ThreadId.make("thread:prune-race");
343
+ yield* sink.write({ events: [threadCreated(makeThread(threadId, now), now)] });
344
+ yield* sink.write({ events: subagentEvents(threadId, now, 0) });
345
+ yield* maintenance.pruneEventStore();
346
+ yield* sink.write({ events: [itemEvent(threadId, now, itemSpecs[0], 0)] });
347
+ yield* TestClock.adjust("1 day");
348
+
349
+ const scanStarted = yield* Deferred.make<void>();
350
+ const releaseScan = yield* Deferred.make<void>();
351
+ let blocked = false;
352
+ const blockFirstScanPage: Statement.Transformer = (statement) => {
353
+ const [query] = statement.compile();
354
+ if (!blocked && query.includes("END AS prunable")) {
355
+ blocked = true;
356
+ return Deferred.succeed(scanStarted, undefined).pipe(
357
+ Effect.andThen(Deferred.await(releaseScan)),
358
+ Effect.as(statement),
359
+ );
360
+ }
361
+ return Effect.succeed(statement);
362
+ };
363
+ const pass = yield* maintenance
364
+ .pruneEventStore()
365
+ .pipe(
366
+ Effect.provideService(Statement.CurrentTransformer, blockFirstScanPage),
367
+ Effect.forkScoped,
368
+ );
369
+ yield* Deferred.await(scanStarted);
370
+ yield* sink.write({ events: subagentEvents(threadId, now, 0, "late") });
371
+ yield* Deferred.succeed(releaseScan, undefined);
372
+ assert.equal((yield* Fiber.join(pass)).deletedEventCount, 0);
373
+
374
+ const late = yield* eventStore
375
+ .read({ threadId, eventType: "subagent.updated" })
376
+ .pipe(Stream.runCollect);
377
+ assert.deepEqual(
378
+ Array.from(late, (stored) =>
379
+ stored.event.type === "subagent.updated" ? stored.event.payload.prompt : null,
380
+ ),
381
+ ["Review prompt A. ".repeat(40), "Review prompt A. ".repeat(40)],
382
+ );
383
+ }),
384
+ ).pipe(Effect.provide(layerTest)),
385
+);
apps/server/src/orchestration-v2/ProjectionMaintenance.ts
+238
-334
@@ -1,26 +1,18 @@
1
-import { ThreadId } from "@t3tools/contracts";
1
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
2
import * as Context from "effect/Context";
3
import * as DateTime from "effect/DateTime";
4
+import * as Duration from "effect/Duration";
5
import * as Effect from "effect/Effect";
6
import * as Layer from "effect/Layer";
7
import * as Schema from "effect/Schema";
8
-import * as Stream from "effect/Stream";
8
import * as SqlClient from "effect/sql/SqlClient";
9
11
-import * as EventStore from "./EventStore.ts";
10
+import {
11
+ EVENT_LOG_PRUNE_MARK,
12
+ EVENT_LOG_PRUNED_THROUGH,
13
+} from "../persistence/OrchestrationEventStore.ts";
14
import * as ProjectionStore from "./ProjectionStore.ts";
15
14
-export interface ProjectionVerificationV2 {
15
- readonly valid: boolean;
16
- readonly schemaVersion: number;
17
- readonly expectedSequence: number;
18
- readonly projectionSequence: number;
19
- readonly unreadableThreadIds: ReadonlyArray<ThreadId>;
20
- readonly missingThreadIds: ReadonlyArray<ThreadId>;
21
- readonly unexpectedThreadIds: ReadonlyArray<ThreadId>;
22
-}
23
-
16
export class ProjectionMaintenanceError extends Schema.TaggedError<ProjectionMaintenanceError>()(
17
"ProjectionMaintenanceError",
18
{
@@ -30,9 +22,8 @@ export class ProjectionMaintenanceError extends Schema.TaggedError<ProjectionMai
22
) {}
23
24
export interface ProjectionMaintenanceV2Shape {
33
- readonly verify: Effect.Effect<ProjectionVerificationV2, ProjectionMaintenanceError>;
34
- readonly rebuild: Effect.Effect<ProjectionVerificationV2, ProjectionMaintenanceError>;
35
- readonly compactEventStore: (betweenPages?: Effect.Effect<void>) => Effect.Effect<
25
+ /** Deletes events the projections already cover once they are older than the retained history. */
26
+ readonly pruneEventStore: (betweenPages?: Effect.Effect<void>) => Effect.Effect<
27
{
28
readonly deletedEventCount: number;
29
readonly deletedReceiptCount: number;
@@ -47,369 +38,282 @@ export class ProjectionMaintenanceV2 extends Context.Service<
38
ProjectionMaintenanceV2Shape
39
>()("t3/orchestration-v2/ProjectionMaintenance/ProjectionMaintenanceV2") {}
40
50
-type ProjectionMetadataRow = {
51
- readonly schema_version: number;
41
+// Reconnect replay and command-id retries read this tail; older cursors resync from a snapshot.
42
+const RETAINED_EVENT_HISTORY = Duration.days(1);
43
+// Small pages bound work on the reader worker and synchronous fallback.
44
+const PAGE_SIZE = 50;
45
+const REFERENCE_SCAN_WINDOW = 1000;
46
+
47
+type MetadataRow = {
48
+ readonly projection_name: string;
49
readonly last_sequence: number;
50
+ readonly updated_at: string;
51
};
52
55
-const encodeEntityKey = Schema.encodeSync(
56
- Schema.fromJsonString(Schema.Tuple([Schema.String, Schema.String, Schema.NullOr(Schema.String)])),
57
-);
58
-
59
-export const layer: Layer.Layer<
60
- ProjectionMaintenanceV2,
61
- never,
62
- EventStore.EventStoreV2 | ProjectionStore.ProjectionStoreV2 | SqlClient.SqlClient
63
-> = Layer.effect(
53
+export const layer: Layer.Layer<ProjectionMaintenanceV2, never, SqlClient.SqlClient> = Layer.effect(
54
ProjectionMaintenanceV2,
55
Effect.gen(function* () {
56
const sql = yield* SqlClient.SqlClient;
67
- const eventStore = yield* EventStore.EventStoreV2;
68
- const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
69
-
70
- const verify = Effect.gen(function* () {
71
- const expectedThreadRows = yield* sql<{ readonly thread_id: string }>`
72
- SELECT DISTINCT stream_id AS thread_id
73
- FROM orchestration_events INDEXED BY orchestration_events_v2_created_threads_idx
74
- WHERE application_event_version = 2
75
- AND aggregate_kind = 'thread'
76
- AND event_type = 'thread.created'
77
- ORDER BY stream_id ASC
78
- `;
79
- const projectionRows = yield* sql<{ readonly thread_id: string }>`
80
- SELECT thread_id
81
- FROM orchestration_v2_projection_threads
82
- ORDER BY thread_id ASC
83
- `;
84
- const actualIds = projectionRows.map((row) => ThreadId.make(row.thread_id));
85
- const expectedIds = expectedThreadRows.map((row) => ThreadId.make(row.thread_id));
86
- const actualSet = new Set(actualIds);
87
- const expectedSet = new Set(expectedIds);
88
- const missingThreadIds = expectedIds.filter((threadId) => !actualSet.has(threadId));
89
- const unexpectedThreadIds = actualIds.filter((threadId) => !expectedSet.has(threadId));
90
- const unreadableThreadIds = yield* projectionStore.getUnreadableThreadIds();
91
- const metadata = yield* sql<ProjectionMetadataRow>`
92
- SELECT schema_version, last_sequence
93
- FROM orchestration_v2_projection_metadata
94
- WHERE projection_name = 'thread-projections'
95
- LIMIT 1
96
- `;
97
- const expectedSequence = yield* eventStore.latestSequence();
98
- const schemaVersion = metadata[0]?.schema_version ?? 0;
99
- const projectionSequence = metadata[0]?.last_sequence ?? 0;
100
- return {
101
- valid:
102
- schemaVersion === ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION &&
103
- projectionSequence === expectedSequence &&
104
- missingThreadIds.length === 0 &&
105
- unexpectedThreadIds.length === 0 &&
106
- unreadableThreadIds.length === 0,
107
- schemaVersion,
108
- expectedSequence,
109
- projectionSequence,
110
- unreadableThreadIds,
111
- missingThreadIds,
112
- unexpectedThreadIds,
113
- } satisfies ProjectionVerificationV2;
114
- }).pipe(sql.withTransaction);
57
116
- const rebuild = Effect.gen(function* () {
117
- yield* sql.withTransaction(
118
- Effect.gen(function* () {
119
- const throughSequence = yield* eventStore.latestSequence();
120
- yield* sql`DELETE FROM orchestration_v2_projection_context_transfers`;
121
- yield* sql`DELETE FROM orchestration_v2_projection_context_handoffs`;
122
- yield* sql`DELETE FROM orchestration_v2_projection_checkpoints`;
123
- yield* sql`DELETE FROM orchestration_v2_projection_checkpoint_scopes`;
124
- yield* sql`DELETE FROM orchestration_v2_projection_turn_items`;
125
- yield* sql`DELETE FROM orchestration_v2_projection_plans`;
126
- yield* sql`DELETE FROM orchestration_v2_projection_messages`;
127
- yield* sql`DELETE FROM orchestration_v2_projection_runtime_requests`;
128
- yield* sql`DELETE FROM orchestration_v2_projection_provider_turns`;
129
- yield* sql`DELETE FROM orchestration_v2_projection_provider_threads`;
130
- yield* sql`DELETE FROM orchestration_v2_projection_provider_session_bindings`;
131
- yield* sql`DELETE FROM orchestration_v2_projection_provider_sessions`;
132
- yield* sql`DELETE FROM orchestration_v2_projection_subagents`;
133
- yield* sql`DELETE FROM orchestration_v2_projection_nodes`;
134
- yield* sql`DELETE FROM orchestration_v2_projection_run_attempts`;
135
- yield* sql`DELETE FROM orchestration_v2_projection_runs`;
136
- yield* sql`DELETE FROM orchestration_v2_projection_threads`;
137
- yield* sql`DELETE FROM orchestration_v2_turn_item_positions`;
58
+ const writeMetadata = (name: string, sequence: number, at: DateTime.Utc) => sql`
59
+ INSERT INTO orchestration_v2_projection_metadata (
60
+ projection_name, schema_version, last_sequence, updated_at
61
+ )
62
+ VALUES (
63
+ ${name},
64
+ ${ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION},
65
+ ${sequence},
66
+ ${DateTime.formatIso(at)}
67
+ )
68
+ ON CONFLICT(projection_name) DO UPDATE SET
69
+ last_sequence = excluded.last_sequence,
70
+ updated_at = excluded.updated_at
71
+ `;
72
139
- const pageSize = 500;
140
- let lastSequence = 0;
141
- while (true) {
142
- const page = yield* eventStore
143
- .read({ afterSequence: lastSequence, throughSequence, limit: pageSize })
144
- .pipe(Stream.runCollect);
145
- for (const stored of page) {
146
- yield* projectionStore.apply(stored.event);
147
- if (stored.event.type === "turn-item.updated") {
148
- yield* sql`
149
- INSERT INTO orchestration_v2_turn_item_positions (
150
- thread_id,
151
- turn_item_id,
152
- ordinal
153
- )
154
- VALUES (
155
- ${stored.event.threadId},
156
- ${stored.event.payload.id},
157
- ${stored.event.payload.ordinal}
158
- )
159
- ON CONFLICT(thread_id, turn_item_id) DO UPDATE SET
160
- ordinal = excluded.ordinal
161
- `;
162
- }
163
- lastSequence = stored.sequence;
164
- }
165
- if (page.length < pageSize) break;
166
- yield* Effect.yieldNow;
167
- }
168
- const now = DateTime.formatIso(yield* DateTime.now);
169
- yield* sql`
170
- INSERT INTO orchestration_v2_projection_metadata (
171
- projection_name,
172
- schema_version,
173
- last_sequence,
174
- updated_at
175
- )
176
- VALUES (
177
- 'thread-projections',
178
- ${ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION},
179
- ${lastSequence},
180
- ${now}
181
- )
182
- ON CONFLICT(projection_name) DO UPDATE SET
183
- schema_version = excluded.schema_version,
184
- last_sequence = excluded.last_sequence,
185
- updated_at = excluded.updated_at
186
- `;
187
- }),
188
- );
189
- return yield* verify;
73
+ // Collects anchors whose prompt later rows borrow through promptSequence.
74
+ const readPromptReferences = Effect.fnUntraced(function* (
75
+ afterSequence: number,
76
+ throughSequence: number,
77
+ betweenPages: Effect.Effect<void>,
78
+ ) {
79
+ const referenced = new Set<number>();
80
+ for (let cursor = afterSequence; cursor < throughSequence;) {
81
+ const windowEnd = Math.min(cursor + REFERENCE_SCAN_WINDOW, throughSequence);
82
+ const rows = yield* sql<{ readonly prompt_sequence: number }>`
83
+ SELECT json_extract(payload_json, '$.promptSequence') AS prompt_sequence
84
+ FROM orchestration_events
85
+ WHERE sequence > ${cursor}
86
+ AND sequence <= ${windowEnd}
87
+ AND event_type IN ('subagent.updated', 'turn-item.updated')
88
+ AND json_type(payload_json, '$.promptSequence') = 'integer'
89
+ `.pipe(NodeSqliteClient.readOnly);
90
+ for (const row of rows) referenced.add(row.prompt_sequence);
91
+ cursor = windowEnd;
92
+ yield* betweenPages;
93
+ }
94
+ return referenced;
95
});
96
192
- const mapError =
193
- (operation: string) =>
194
- <A, E, R>(effect: Effect.Effect<A, E, R>) =>
195
- effect.pipe(
196
- Effect.mapError((cause) => new ProjectionMaintenanceError({ operation, cause })),
197
- );
198
-
199
- // Keep thread.created as the verification and replay anchor; clients recognize deletion by thread.deleted event type.
200
- const SUPERSEDABLE_THREAD_EVENT_TYPES = [
201
- "thread.archived",
202
- "thread.unarchived",
203
- "thread.settled",
204
- "thread.unsettled",
205
- "thread.snoozed",
206
- "thread.unsnoozed",
207
- "thread.pinned",
208
- "thread.auto-settle-set",
209
- "thread.unpinned",
210
- "thread.pin-reordered",
211
- "thread.active-reordered",
212
- "thread.metadata-updated",
213
- "thread.pull-request-synced",
214
- "thread.runtime-mode-updated",
215
- "thread.interaction-mode-updated",
216
- "thread.model-selection-updated",
217
- "thread.provider-switched",
218
- "thread.guest-share-updated",
219
- ];
220
- // Read-state events preserve updatedAt and supersede only each other. Activity events supersede both groups.
221
- const READ_STATE_THREAD_EVENT_TYPES = ["thread.visited", "thread.marked-unread"];
222
- // Older clients skip tag events, so these supersede only each other.
223
- const TAG_THREAD_EVENT_TYPE = "thread.tags-updated";
224
-
225
- const supersedableThreadEventTypes = new Set(SUPERSEDABLE_THREAD_EVENT_TYPES);
226
- const readStateThreadEventTypes = new Set(READ_STATE_THREAD_EVENT_TYPES);
227
- // Subagent snapshots stay intact because later rows can reference an earlier prompt.
228
- // Commanded turn items stay for command-result replay.
229
- // Small pages bound work on the reader worker and synchronous fallback.
230
- const COMPACTION_PAGE_SIZE = 50;
231
-
232
- // Keep first/latest keyed events for replay order; page every event to bound sparse scans.
233
- const compactEventStore = Effect.fn("ProjectionMaintenance.compactEventStore")(function* (
234
- betweenPages: Effect.Effect<void> = Effect.void,
97
+ const pruneEvents = Effect.fnUntraced(function* (
98
+ throughSequence: number,
99
+ head: number,
100
+ betweenPages: Effect.Effect<void>,
101
) {
236
- const bounds = yield* sql<{
237
- readonly event_sequence: number;
238
- readonly receipt_row_id: number;
239
- }>`
240
- SELECT
241
- COALESCE((SELECT MAX(sequence) FROM orchestration_events), 0) AS event_sequence,
242
- COALESCE((SELECT MAX(rowid) FROM orchestration_command_receipts), 0) AS receipt_row_id
102
+ const referenced = yield* readPromptReferences(throughSequence, head, betweenPages);
103
+ // Stream heads at or below the boundary are kept, so the anchors they borrow must stay too.
104
+ const keptHeadReferences = yield* sql<{ readonly prompt_sequence: number }>`
105
+ SELECT json_extract(event.payload_json, '$.promptSequence') AS prompt_sequence
106
+ FROM (
107
+ SELECT MAX(sequence) AS sequence FROM orchestration_events
108
+ GROUP BY aggregate_kind, stream_id
109
+ ) AS heads
110
+ JOIN orchestration_events AS event ON event.sequence = heads.sequence
111
+ WHERE heads.sequence <= ${throughSequence}
112
+ AND event.event_type IN ('subagent.updated', 'turn-item.updated')
113
+ AND json_type(event.payload_json, '$.promptSequence') = 'integer'
114
`.pipe(NodeSqliteClient.readOnly);
244
- let throughSequence = bounds[0]?.event_sequence ?? 0;
245
- let throughReceiptRowId = bounds[0]?.receipt_row_id ?? 0;
246
- const retainedThreadIds = new Set<string>();
247
- const retainedReadStateThreadIds = new Set<string>();
248
- const retainedTagThreadIds = new Set<string>();
249
- // Retain each key's oldest candidate, deleting it only when an older event appears.
250
- const oldestCandidateByEntityKey = new Map<string, number | null>();
251
- let deletedEventCount = 0;
252
- let deletedReceiptCount = 0;
253
-
254
- while (throughSequence > 0) {
255
- const rows = yield* sql<{
256
- readonly sequence: number;
257
- readonly application_event_version: number;
258
- readonly aggregate_kind: string;
259
- readonly stream_id: string;
260
- readonly event_type: string;
261
- readonly entity_id: string | null;
262
- readonly imported_legacy_thread: number;
263
- }>`
115
+ for (const row of keptHeadReferences) referenced.add(row.prompt_sequence);
116
+ yield* betweenPages;
117
+ let referencesThrough = head;
118
+ let deleted = 0;
119
+ let cursor = 0;
120
+ while (cursor < throughSequence) {
121
+ const rows = yield* sql<{ readonly sequence: number; readonly prunable: number }>`
122
SELECT
123
event.sequence,
266
- event.application_event_version,
267
- event.aggregate_kind,
268
- event.stream_id,
269
- event.event_type,
270
- CASE
271
- WHEN event.application_event_version = 2
272
- AND event.event_type IN ('message.updated', 'node.updated')
273
- THEN json_extract(event.payload_json, '$.id')
274
- WHEN event.application_event_version = 2
275
- AND event.event_type = 'turn-item.updated'
276
- AND event.command_id IS NULL
277
- AND json_extract(event.payload_json, '$.type') <> 'subagent'
278
- THEN json_extract(event.payload_json, '$.id')
279
- WHEN event.application_event_version = 2
280
- AND event.event_type = 'thread.pull-request-link-updated'
281
- THEN json_array(json_extract(event.payload_json, '$.host'),
282
- json_extract(event.payload_json, '$.repository'),
283
- json_extract(event.payload_json, '$.number'))
284
- ELSE NULL
285
- END AS entity_id,
124
CASE
287
- WHEN event.application_event_version = 1 AND event.aggregate_kind = 'thread'
288
- THEN EXISTS (
289
- SELECT 1 FROM orchestration_v2_legacy_imports AS legacy_import
290
- WHERE legacy_import.thread_id = event.stream_id
291
- AND legacy_import.transcript_imported_at IS NOT NULL
292
- )
293
- ELSE 0
294
- END AS imported_legacy_thread
125
+ WHEN event.application_event_version = 1 THEN 0
126
+ -- The legacy importer treats a thread without thread.created as not yet imported.
127
+ WHEN event.event_type = 'thread.created' THEN 0
128
+ -- Codex usage history reads the selection a run attempt started with.
129
+ WHEN event.event_type = 'run.updated'
130
+ AND json_extract(event.payload_json, '$.status') = 'running' THEN 0
131
+ -- The newest row keeps each stream's high water, stream_version, and snapshot sequence.
132
+ WHEN event.sequence = (
133
+ SELECT MAX(newer.sequence) FROM orchestration_events AS newer
134
+ WHERE newer.aggregate_kind = event.aggregate_kind
135
+ AND newer.stream_id = event.stream_id
136
+ ) THEN 0
137
+ ELSE 1
138
+ END AS prunable
139
FROM orchestration_events AS event
296
- WHERE event.sequence <= ${throughSequence}
297
- ORDER BY event.sequence DESC
298
- LIMIT ${COMPACTION_PAGE_SIZE + 1}
140
+ WHERE event.sequence > ${cursor}
141
+ AND event.sequence <= ${throughSequence}
142
+ ORDER BY event.sequence ASC
143
+ LIMIT ${PAGE_SIZE}
144
`.pipe(NodeSqliteClient.readOnly);
300
- const page = rows.slice(0, COMPACTION_PAGE_SIZE);
301
- const obsolete: number[] = [];
302
- for (const row of page) {
303
- if (row.imported_legacy_thread === 1) {
304
- obsolete.push(row.sequence);
305
- } else if (row.application_event_version === 2) {
306
- if (
307
- row.aggregate_kind === "thread" &&
308
- supersedableThreadEventTypes.has(row.event_type)
309
- ) {
310
- if (retainedThreadIds.has(row.stream_id)) obsolete.push(row.sequence);
311
- else retainedThreadIds.add(row.stream_id);
312
- } else if (
313
- row.aggregate_kind === "thread" &&
314
- readStateThreadEventTypes.has(row.event_type)
315
- ) {
316
- if (
317
- retainedThreadIds.has(row.stream_id) ||
318
- retainedReadStateThreadIds.has(row.stream_id)
319
- ) {
320
- obsolete.push(row.sequence);
321
- } else retainedReadStateThreadIds.add(row.stream_id);
322
- } else if (
323
- row.aggregate_kind === "thread" &&
324
- row.event_type === TAG_THREAD_EVENT_TYPE
325
- ) {
326
- if (retainedThreadIds.has(row.stream_id) || retainedTagThreadIds.has(row.stream_id)) {
327
- obsolete.push(row.sequence);
328
- } else retainedTagThreadIds.add(row.stream_id);
329
- } else if (
330
- row.event_type === "thread.pull-request-link-updated" &&
331
- retainedThreadIds.has(row.stream_id)
332
- ) {
333
- // A newer whole-thread payload already carries this link's state.
334
- obsolete.push(row.sequence);
335
- } else if (
336
- row.event_type === "thread.pull-request-link-updated" ||
337
- row.event_type === "message.updated" ||
338
- row.event_type === "node.updated" ||
339
- (row.event_type === "turn-item.updated" && row.entity_id !== null)
340
- ) {
341
- const key = encodeEntityKey([row.event_type, row.stream_id, row.entity_id]);
342
- const candidate = oldestCandidateByEntityKey.get(key);
343
- if (candidate === undefined) oldestCandidateByEntityKey.set(key, null);
344
- else {
345
- if (candidate !== null) obsolete.push(candidate);
346
- oldestCandidateByEntityKey.set(key, row.sequence);
145
+ const candidates = rows.filter((row) => row.prunable === 1).map((row) => row.sequence);
146
+ // Never hold a transaction or the reader lock across a yield or a rest.
147
+ yield* Effect.yieldNow;
148
+ if (candidates.length > 0) {
149
+ deleted += yield* sql.withTransaction(
150
+ Effect.gen(function* () {
151
+ // Rows appended since the reference scan may borrow a prompt from a candidate.
152
+ const appended = yield* sql<{
153
+ readonly sequence: number;
154
+ readonly prompt_sequence: number | null;
155
+ }>`
156
+ SELECT
157
+ sequence,
158
+ CASE
159
+ WHEN event_type IN ('subagent.updated', 'turn-item.updated')
160
+ AND json_type(payload_json, '$.promptSequence') = 'integer'
161
+ THEN json_extract(payload_json, '$.promptSequence')
162
+ END AS prompt_sequence
163
+ FROM orchestration_events
164
+ WHERE sequence > ${referencesThrough}
165
+ `;
166
+ for (const row of appended) {
167
+ if (row.prompt_sequence !== null) referenced.add(row.prompt_sequence);
168
+ referencesThrough = Math.max(referencesThrough, row.sequence);
169
}
348
- }
349
- }
170
+ const obsolete = candidates.filter((sequence) => !referenced.has(sequence));
171
+ if (obsolete.length === 0) return 0;
172
+ yield* sql`DELETE FROM orchestration_events WHERE sequence IN ${sql.in(obsolete)}`;
173
+ return obsolete.length;
174
+ }),
175
+ );
176
+ yield* Effect.yieldNow;
177
}
351
- // Never hold a transaction or the reader lock across a yield or a rest.
178
+ // Rest after every page, including the last, so the next loop starts a fresh slice.
179
+ yield* betweenPages;
180
+ if (rows.length < PAGE_SIZE) break;
181
+ cursor = rows.at(-1)?.sequence ?? throughSequence;
182
+ }
183
+ return deleted;
184
+ });
185
+
186
+ // Imported legacy threads are served from v2 rows; their v1 events and receipts have no reader.
187
+ const pruneImportedLegacyHistory = Effect.fnUntraced(function* (
188
+ throughSequence: number,
189
+ throughReceiptRowId: number,
190
+ betweenPages: Effect.Effect<void>,
191
+ ) {
192
+ let deletedEventCount = 0;
193
+ let deletedReceiptCount = 0;
194
+ for (let cursor = 0; ;) {
195
+ const rows = yield* sql<{ readonly sequence: number; readonly imported: number }>`
196
+ SELECT
197
+ event.sequence,
198
+ event.aggregate_kind = 'thread' AND EXISTS (
199
+ SELECT 1 FROM orchestration_v2_legacy_imports AS legacy_import
200
+ WHERE legacy_import.thread_id = event.stream_id
201
+ AND legacy_import.transcript_imported_at IS NOT NULL
202
+ ) AS imported
203
+ FROM orchestration_events AS event
204
+ INDEXED BY idx_orchestration_events_application_sequence
205
+ WHERE event.application_event_version = 1
206
+ AND event.sequence > ${cursor}
207
+ AND event.sequence <= ${throughSequence}
208
+ ORDER BY event.sequence ASC
209
+ LIMIT ${PAGE_SIZE}
210
+ `.pipe(NodeSqliteClient.readOnly);
211
+ const obsolete = rows.filter((row) => row.imported === 1).map((row) => row.sequence);
212
yield* Effect.yieldNow;
213
if (obsolete.length > 0) {
214
yield* sql`DELETE FROM orchestration_events WHERE sequence IN ${sql.in(obsolete)}`;
215
deletedEventCount += obsolete.length;
216
yield* Effect.yieldNow;
217
}
358
- throughSequence =
359
- rows.length <= COMPACTION_PAGE_SIZE ? 0 : (page.at(-1)?.sequence ?? 1) - 1;
360
- if (throughSequence > 0 || throughReceiptRowId > 0) yield* betweenPages;
218
+ yield* betweenPages;
219
+ if (rows.length < PAGE_SIZE) break;
220
+ cursor = rows.at(-1)?.sequence ?? cursor;
221
}
362
-
363
- while (throughReceiptRowId > 0) {
222
+ for (let cursor = 0; ;) {
223
const rows = yield* sql<{
224
readonly row_id: number;
225
readonly command_id: string;
367
- readonly imported_legacy_thread: number;
226
+ readonly imported: number;
227
}>`
228
SELECT
229
receipt.rowid AS row_id,
230
receipt.command_id,
372
- CASE
373
- WHEN receipt.command_type = 'legacy' AND receipt.aggregate_kind = 'thread'
374
- THEN EXISTS (
375
- SELECT 1 FROM orchestration_v2_legacy_imports AS legacy_import
376
- WHERE legacy_import.thread_id = receipt.aggregate_id
377
- AND legacy_import.transcript_imported_at IS NOT NULL
378
- )
379
- ELSE 0
380
- END AS imported_legacy_thread
231
+ receipt.command_type = 'legacy' AND receipt.aggregate_kind = 'thread' AND EXISTS (
232
+ SELECT 1 FROM orchestration_v2_legacy_imports AS legacy_import
233
+ WHERE legacy_import.thread_id = receipt.aggregate_id
234
+ AND legacy_import.transcript_imported_at IS NOT NULL
235
+ ) AS imported
236
FROM orchestration_command_receipts AS receipt
382
- WHERE receipt.rowid <= ${throughReceiptRowId}
383
- ORDER BY receipt.rowid DESC
384
- LIMIT ${COMPACTION_PAGE_SIZE + 1}
237
+ WHERE receipt.rowid > ${cursor} AND receipt.rowid <= ${throughReceiptRowId}
238
+ ORDER BY receipt.rowid ASC
239
+ LIMIT ${PAGE_SIZE}
240
`.pipe(NodeSqliteClient.readOnly);
386
- const page = rows.slice(0, COMPACTION_PAGE_SIZE);
387
- const obsolete = page
388
- .filter((row) => row.imported_legacy_thread === 1)
389
- .map((row) => row.command_id);
241
+ const obsolete = rows.filter((row) => row.imported === 1).map((row) => row.command_id);
242
yield* Effect.yieldNow;
243
if (obsolete.length > 0) {
244
yield* sql`DELETE FROM orchestration_command_receipts WHERE command_id IN ${sql.in(obsolete)}`;
245
deletedReceiptCount += obsolete.length;
246
yield* Effect.yieldNow;
247
}
396
- throughReceiptRowId = (page.at(-1)?.row_id ?? 1) - 1;
397
- if (rows.length <= COMPACTION_PAGE_SIZE) break;
248
yield* betweenPages;
249
+ if (rows.length < PAGE_SIZE) break;
250
+ cursor = rows.at(-1)?.row_id ?? cursor;
251
}
252
+ return { deletedEventCount, deletedReceiptCount };
253
+ });
254
401
- const freelistRows = yield* sql<{ readonly freelist_count: number }>`PRAGMA freelist_count`;
402
- const pageSizeRows = yield* sql<{ readonly page_size: number }>`PRAGMA page_size`;
403
- const reclaimableBytes =
404
- (freelistRows[0]?.freelist_count ?? 0) * (pageSizeRows[0]?.page_size ?? 0);
255
+ // A pass prunes through the mark the previous pass left once that mark is a day old, so
256
+ // at least RETAINED_EVENT_HISTORY of events always remains. Receipts stay for idempotency.
257
+ const pruneEventStore = Effect.fn("ProjectionMaintenance.pruneEventStore")(
258
+ function* (betweenPages: Effect.Effect<void> = Effect.void) {
259
+ const now = yield* DateTime.now;
260
+ const metadata = yield* sql<MetadataRow>`
261
+ SELECT projection_name, last_sequence, updated_at
262
+ FROM orchestration_v2_projection_metadata
263
+ WHERE projection_name IN (
264
+ 'thread-projections', ${EVENT_LOG_PRUNE_MARK}, ${EVENT_LOG_PRUNED_THROUGH}
265
+ )
266
+ `.pipe(NodeSqliteClient.readOnly);
267
+ const read = (name: string) => metadata.find((row) => row.projection_name === name);
268
+ const projected = read("thread-projections")?.last_sequence ?? 0;
269
+ const mark = read(EVENT_LOG_PRUNE_MARK);
270
+ const prunedThrough = read(EVENT_LOG_PRUNED_THROUGH)?.last_sequence ?? 0;
271
+ // Fixed bounds: foreground commits during the pass wait for the next one.
272
+ const bounds = yield* sql<{ readonly sequence: number; readonly receipt_row_id: number }>`
273
+ SELECT
274
+ COALESCE((SELECT MAX(sequence) FROM orchestration_events), 0) AS sequence,
275
+ COALESCE((SELECT MAX(rowid) FROM orchestration_command_receipts), 0) AS receipt_row_id
276
+ `.pipe(NodeSqliteClient.readOnly);
277
+ const head = bounds[0]?.sequence ?? 0;
278
406
- return { deletedEventCount, deletedReceiptCount, reclaimableBytes };
407
- }, mapError("compact event store"));
279
+ let deletedEventCount = 0;
280
+ const markIsDue =
281
+ mark !== undefined &&
282
+ DateTime.toEpochMillis(now) - Date.parse(mark.updated_at) >=
283
+ Duration.toMillis(RETAINED_EVENT_HISTORY);
284
+ if (mark !== undefined && markIsDue) {
285
+ const throughSequence = Math.max(Math.min(mark.last_sequence, projected), prunedThrough);
286
+ if (throughSequence > prunedThrough) {
287
+ // Publish the boundary first so resuming clients take a snapshot, never a partial replay.
288
+ yield* writeMetadata(EVENT_LOG_PRUNED_THROUGH, throughSequence, now);
289
+ }
290
+ deletedEventCount += yield* pruneEvents(throughSequence, head, betweenPages);
291
+ }
292
+ if (mark === undefined || markIsDue) yield* writeMetadata(EVENT_LOG_PRUNE_MARK, head, now);
293
409
- return ProjectionMaintenanceV2.of({
410
- verify: mapError("verify")(verify),
411
- rebuild: mapError("rebuild")(rebuild),
412
- compactEventStore,
413
- });
294
+ const legacy = yield* pruneImportedLegacyHistory(
295
+ head,
296
+ bounds[0]?.receipt_row_id ?? 0,
297
+ betweenPages,
298
+ );
299
+ deletedEventCount += legacy.deletedEventCount;
300
+
301
+ const freelistRows = yield* sql<{ readonly freelist_count: number }>`PRAGMA freelist_count`;
302
+ const pageSizeRows = yield* sql<{ readonly page_size: number }>`PRAGMA page_size`;
303
+ const reclaimableBytes =
304
+ (freelistRows[0]?.freelist_count ?? 0) * (pageSizeRows[0]?.page_size ?? 0);
305
+
306
+ return {
307
+ deletedEventCount,
308
+ deletedReceiptCount: legacy.deletedReceiptCount,
309
+ reclaimableBytes,
310
+ };
311
+ },
312
+ Effect.mapError(
313
+ (cause) => new ProjectionMaintenanceError({ operation: "prune event store", cause }),
314
+ ),
315
+ );
316
+
317
+ return ProjectionMaintenanceV2.of({ pruneEventStore });
318
}),
319
);
apps/server/src/orchestration-v2/TurnItemCompaction.test.ts
deleted
-488
@@ -1,488 +0,0 @@
1
-import { applyOrchestrationV2ProjectionEvent } from "../../../../packages/client-runtime/src/state/orchestrationV2Projection.ts";
2
-import { assert, it } from "@effect/vitest";
3
-import {
4
- CommandId,
5
- EventId,
6
- NodeId,
7
- PlanId,
8
- type OrchestrationV2AppThread,
9
- type OrchestrationV2DomainEvent,
10
- type OrchestrationV2TurnItem,
11
- ProjectId,
12
- ProviderDriverKind,
13
- ProviderInstanceId,
14
- ThreadId,
15
- TurnItemId,
16
-} from "@t3tools/contracts";
17
-import * as DateTime from "effect/DateTime";
18
-import * as Deferred from "effect/Deferred";
19
-import * as Effect from "effect/Effect";
20
-import * as Fiber from "effect/Fiber";
21
-import * as Layer from "effect/Layer";
22
-import * as Stream from "effect/Stream";
23
-import * as Tracer from "effect/Tracer";
24
-import * as SqlClient from "effect/sql/SqlClient";
25
-import * as Statement from "effect/sql/Statement";
26
-
27
-import * as SqlitePersistence from "../persistence/Sqlite.ts";
28
-import * as EventSink from "./EventSink.ts";
29
-import * as EventStore from "./EventStore.ts";
30
-import * as ProjectionMaintenance from "./ProjectionMaintenance.ts";
31
-import * as ProjectionStore from "./ProjectionStore.ts";
32
-
33
-const database = SqlitePersistence.layerMemory;
34
-const eventStoreProvided = EventStore.layer.pipe(Layer.provideMerge(database));
35
-const projectionStoreProvided = ProjectionStore.layer.pipe(Layer.provideMerge(database));
36
-const stores = Layer.mergeAll(database, eventStoreProvided, projectionStoreProvided);
37
-const layerTest = Layer.mergeAll(
38
- stores,
39
- EventSink.layer.pipe(Layer.provide(stores)),
40
- ProjectionMaintenance.layer.pipe(Layer.provide(stores)),
41
-);
42
-
43
-const providerInstanceId = ProviderInstanceId.make("codex");
44
-const providerDriver = ProviderDriverKind.make("codex");
45
-const modelSelection = { instanceId: providerInstanceId, model: "gpt-5.4" };
46
-
47
-function makeThread(threadId: ThreadId, now: DateTime.Utc): OrchestrationV2AppThread {
48
- return {
49
- id: threadId,
50
- projectId: ProjectId.make(`project:${threadId}`),
51
- title: "Turn item compaction",
52
- providerInstanceId,
53
- modelSelection,
54
- runtimeMode: "full-access",
55
- interactionMode: "default",
56
- branch: null,
57
- worktreePath: null,
58
- activeProviderThreadId: null,
59
- lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null },
60
- forkedFrom: null,
61
- createdBy: "user",
62
- creationSource: "web",
63
- createdAt: now,
64
- updatedAt: now,
65
- archivedAt: null,
66
- settledOverride: null,
67
- settledAt: null,
68
- lastVisitedAt: null,
69
- deletedAt: null,
70
- };
71
-}
72
-
73
-function threadCreated(thread: OrchestrationV2AppThread, now: DateTime.Utc) {
74
- return {
75
- id: EventId.make("event:turn-item-compaction:thread"),
76
- type: "thread.created",
77
- threadId: thread.id,
78
- providerInstanceId,
79
- occurredAt: now,
80
- payload: thread,
81
- } satisfies OrchestrationV2DomainEvent;
82
-}
83
-
84
-const itemSpecs = [
85
- { key: "command", type: "command_execution", ordinal: 10 },
86
- { key: "reasoning", type: "reasoning", ordinal: 20 },
87
- { key: "todo", type: "todo_list", ordinal: 30 },
88
- { key: "commanded", type: "command_execution", ordinal: 40 },
89
-] as const;
90
-
91
-function itemEvent(
92
- threadId: ThreadId,
93
- now: DateTime.Utc,
94
- spec: (typeof itemSpecs)[number],
95
- revision: number,
96
- eventId = `event:turn-item-compaction:${spec.key}:${revision}`,
97
-): OrchestrationV2DomainEvent {
98
- const id = TurnItemId.make(`turn-item:compaction:${spec.key}`);
99
- const growing = `${spec.key}:${revision}:` + "x".repeat((revision + 1) * 192);
100
- const completed = revision === 69;
101
- const base = {
102
- id,
103
- threadId,
104
- runId: null,
105
- nodeId: null,
106
- providerThreadId: null,
107
- providerTurnId: null,
108
- nativeItemRef: null,
109
- parentItemId: null,
110
- ordinal: spec.ordinal,
111
- status: completed ? ("completed" as const) : ("running" as const),
112
- title: spec.key,
113
- startedAt: now,
114
- completedAt: completed ? now : null,
115
- updatedAt: now,
116
- };
117
- let payload: OrchestrationV2TurnItem;
118
- switch (spec.type) {
119
- case "command_execution":
120
- payload = {
121
- ...base,
122
- type: "command_execution",
123
- input: `run ${spec.key}`,
124
- output: growing,
125
- };
126
- break;
127
- case "reasoning":
128
- payload = { ...base, type: "reasoning", text: growing, streaming: !completed };
129
- break;
130
- case "todo_list":
131
- payload = {
132
- ...base,
133
- type: "todo_list",
134
- planId: PlanId.make("plan:turn-item-compaction"),
135
- steps: [{ id: "step", text: growing, status: completed ? "completed" : "running" }],
136
- explanation: growing,
137
- };
138
- break;
139
- }
140
- return {
141
- id: EventId.make(eventId),
142
- type: "turn-item.updated",
143
- threadId,
144
- occurredAt: now,
145
- payload,
146
- };
147
-}
148
-
149
-function subagentEvents(
150
- threadId: ThreadId,
151
- now: DateTime.Utc,
152
- revision: number,
153
-): ReadonlyArray<OrchestrationV2DomainEvent> {
154
- const id = NodeId.make("subagent:turn-item-compaction");
155
- const prompt = revision === 0 ? "Review prompt A. ".repeat(40) : "Review prompt B. ".repeat(40);
156
- const progress = `Review progress ${revision}`;
157
- return [
158
- {
159
- id: EventId.make(`event:turn-item-compaction:subagent:${revision}`),
160
- type: "subagent.updated",
161
- threadId,
162
- occurredAt: now,
163
- payload: {
164
- id,
165
- threadId,
166
- runId: null,
167
- parentNodeId: NodeId.make("node:turn-item-compaction-parent"),
168
- origin: "provider_native",
169
- createdBy: "agent",
170
- driver: providerDriver,
171
- providerInstanceId,
172
- providerThreadId: null,
173
- childThreadId: null,
174
- nativeTaskRef: {
175
- driver: providerDriver,
176
- nativeId: "compaction-review",
177
- strength: "strong",
178
- },
179
- prompt,
180
- progress,
181
- title: "Review",
182
- model: null,
183
- status: "running",
184
- result: null,
185
- startedAt: now,
186
- completedAt: null,
187
- updatedAt: now,
188
- },
189
- },
190
- {
191
- id: EventId.make(`event:turn-item-compaction:subagent-item:${revision}`),
192
- type: "turn-item.updated",
193
- threadId,
194
- occurredAt: now,
195
- payload: {
196
- id: TurnItemId.make("turn-item:compaction:subagent"),
197
- threadId,
198
- runId: null,
199
- nodeId: null,
200
- providerThreadId: null,
201
- providerTurnId: null,
202
- nativeItemRef: null,
203
- parentItemId: null,
204
- ordinal: 50,
205
- status: "running",
206
- title: "Review",
207
- startedAt: now,
208
- completedAt: null,
209
- updatedAt: now,
210
- type: "subagent",
211
- subagentId: id,
212
- origin: "provider_native",
213
- driver: providerDriver,
214
- providerInstanceId,
215
- childThreadId: null,
216
- prompt,
217
- progress,
218
- result: null,
219
- },
220
- },
221
- ] satisfies ReadonlyArray<OrchestrationV2DomainEvent>;
222
-}
223
-
224
-it.effect(
225
- "compacts ordinary snapshots while preserving replay, prompts, command lookup, and positions",
226
- () =>
227
- Effect.gen(function* () {
228
- const sink = yield* EventSink.EventSinkV2;
229
- const eventStore = yield* EventStore.EventStoreV2;
230
- const projections = yield* ProjectionStore.ProjectionStoreV2;
231
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
232
- const sql = yield* SqlClient.SqlClient;
233
- const now = yield* DateTime.now;
234
- const threadId = ThreadId.make("thread:turn-item-compaction");
235
- const thread = makeThread(threadId, now);
236
- const writes = [...(yield* sink.write({ events: [threadCreated(thread, now)] }))];
237
- const commandId = CommandId.make("command:turn-item-compaction:middle");
238
- const snapshotsPerItem = 70;
239
- for (let revision = 0; revision < snapshotsPerItem; revision += 1) {
240
- const order =
241
- revision === 0
242
- ? [itemSpecs[0], itemSpecs[1], itemSpecs[2], itemSpecs[3]]
243
- : [itemSpecs[1], itemSpecs[0], itemSpecs[2], itemSpecs[3]];
244
- const events = order
245
- .filter((spec) => spec.key !== "commanded" || revision !== 35)
246
- .map((spec) => itemEvent(threadId, now, spec, revision));
247
- if (revision < 3) events.push(...subagentEvents(threadId, now, revision));
248
- const stored = yield* sink.write({ events });
249
- writes.push(...stored);
250
- if (revision === 35) {
251
- const middle = itemEvent(
252
- threadId,
253
- now,
254
- itemSpecs[3],
255
- revision,
256
- "event:turn-item-compaction:commanded:middle",
257
- );
258
- writes.push(...(yield* sink.write({ commandId, events: [middle] })));
259
- }
260
- }
261
- const beforeProjection = yield* projections.getThreadProjection(threadId);
262
- const highWater = yield* eventStore.latestSequence();
263
- const bytes = yield* sql<{ readonly rows: number; readonly payload_bytes: number }>`
264
- SELECT COUNT(*) AS rows, COALESCE(SUM(octet_length(payload_json)), 0) AS payload_bytes
265
- FROM orchestration_events WHERE stream_id = ${threadId}
266
- `;
267
- const summary = yield* maintenance.compactEventStore();
268
- const replay = yield* eventStore.read({ threadId }).pipe(Stream.runCollect);
269
- const expectedIds = new Set([
270
- "event:turn-item-compaction:thread",
271
- ...itemSpecs.flatMap((spec) => [
272
- `event:turn-item-compaction:${spec.key}:0`,
273
- `event:turn-item-compaction:${spec.key}:${snapshotsPerItem - 1}`,
274
- ]),
275
- "event:turn-item-compaction:commanded:middle",
276
- ...Array.from({ length: 3 }, (_, revision) => [
277
- `event:turn-item-compaction:subagent:${revision}`,
278
- `event:turn-item-compaction:subagent-item:${revision}`,
279
- ]).flat(),
280
- ]);
281
- assert.deepEqual(
282
- replay,
283
- writes.filter((stored) => expectedIds.has(stored.event.id)),
284
- );
285
- assert.lengthOf(replay, 16);
286
- assert.equal(summary.deletedEventCount, bytes[0]!.rows - replay.length);
287
- assert.isBelow(
288
- (yield* sql<{ readonly payload_bytes: number }>`
289
- SELECT COALESCE(SUM(octet_length(payload_json)), 0) AS payload_bytes
290
- FROM orchestration_events WHERE stream_id = ${threadId}
291
- `)[0]!.payload_bytes,
292
- bytes[0]!.payload_bytes,
293
- );
294
- assert.equal(yield* eventStore.latestSequence(), highWater);
295
-
296
- const commanded = yield* sink.readByCommandId({ commandId }).pipe(Stream.runCollect);
297
- assert.deepEqual(
298
- commanded.map((stored) => stored.event.id),
299
- [EventId.make("event:turn-item-compaction:commanded:middle")],
300
- );
301
- const storedPrompts = yield* sql<{
302
- readonly event_id: string;
303
- readonly sequence: number;
304
- readonly prompt: string;
305
- readonly prompt_sequence: number | null;
306
- }>`
307
- SELECT event_id, sequence,
308
- json_extract(payload_json, '$.prompt') AS prompt,
309
- json_extract(payload_json, '$.promptSequence') AS prompt_sequence
310
- FROM orchestration_events
311
- WHERE stream_id = ${threadId} AND event_type = 'subagent.updated'
312
- ORDER BY sequence ASC
313
- `;
314
- assert.lengthOf(storedPrompts, 3);
315
- assert.equal(storedPrompts[0]!.prompt_sequence, null);
316
- assert.equal(storedPrompts[1]!.prompt_sequence, null);
317
- assert.equal(storedPrompts[2]!.prompt, "");
318
- assert.equal(storedPrompts[2]!.prompt_sequence, storedPrompts[1]!.sequence);
319
-
320
- const deletedCursor = writes.find(
321
- (stored) => stored.event.id === "event:turn-item-compaction:command:35",
322
- )!.sequence;
323
- assert.isFalse(replay.some((stored) => stored.sequence === deletedCursor));
324
- const cursors = [0, deletedCursor, highWater];
325
- for (const cursor of cursors) {
326
- const cursorReplay = yield* eventStore
327
- .read({ threadId, afterSequence: cursor, throughSequence: highWater })
328
- .pipe(Stream.runCollect);
329
- const expected = replay.filter((stored) => stored.sequence > cursor);
330
- assert.deepEqual(
331
- cursorReplay.map((stored) => stored.sequence),
332
- expected.map((stored) => stored.sequence),
333
- );
334
-
335
- let clientProjection: typeof beforeProjection | null = {
336
- ...beforeProjection,
337
- subagents: [],
338
- turnItems: [],
339
- visibleTurnItems: [],
340
- };
341
- let latestLocalTurnOrdinal: number | null = null;
342
- for (const stored of [
343
- ...writes.filter((entry) => entry.sequence <= cursor),
344
- ...cursorReplay,
345
- ]) {
346
- clientProjection = applyOrchestrationV2ProjectionEvent(clientProjection, stored.event, {
347
- partialTimeline: true,
348
- latestLocalTurnOrdinal,
349
- });
350
- if (stored.event.type === "turn-item.updated") {
351
- latestLocalTurnOrdinal = Math.max(
352
- latestLocalTurnOrdinal ?? 0,
353
- stored.event.payload.ordinal,
354
- );
355
- }
356
- }
357
- assert.deepEqual(clientProjection?.turnItems, beforeProjection.turnItems);
358
- assert.deepEqual(clientProjection?.visibleTurnItems, beforeProjection.visibleTurnItems);
359
- }
360
- assert.isTrue((yield* maintenance.verify).valid);
361
- assert.isTrue((yield* maintenance.rebuild).valid);
362
- assert.deepEqual(yield* projections.getThreadProjection(threadId), beforeProjection);
363
- const positions = yield* sql<{
364
- readonly turn_item_id: string;
365
- readonly ordinal: number;
366
- }>`
367
- SELECT turn_item_id, ordinal
368
- FROM orchestration_v2_turn_item_positions
369
- WHERE thread_id = ${threadId}
370
- ORDER BY ordinal ASC
371
- `;
372
- assert.deepEqual(
373
- positions.map((row) => [row.turn_item_id, row.ordinal]),
374
- [
375
- ...itemSpecs.map((spec, index) => [`turn-item:compaction:${spec.key}`, index + 1]),
376
- ["turn-item:compaction:subagent", 5],
377
- ],
378
- );
379
- }).pipe(Effect.provide(layerTest)),
380
-);
381
-
382
-it.effect("keeps post-bound appends visible during compaction", () =>
383
- Effect.scoped(
384
- Effect.gen(function* () {
385
- const sink = yield* EventSink.EventSinkV2;
386
- const eventStore = yield* EventStore.EventStoreV2;
387
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
388
- const now = yield* DateTime.now;
389
- const threadId = ThreadId.make("thread:turn-item-compaction-race");
390
- const thread = makeThread(threadId, now);
391
- const initial = yield* sink.write({
392
- events: [
393
- threadCreated(thread, now),
394
- ...[0, 1, 2].map((revision) => itemEvent(threadId, now, itemSpecs[0], revision)),
395
- ],
396
- });
397
- const beforeSequence = initial.at(-1)!.sequence;
398
-
399
- const streamReady = yield* Deferred.make<void>();
400
- const streamTracer = Tracer.make({
401
- span(options) {
402
- const span = new Tracer.NativeSpan(options);
403
- const end = span.end.bind(span);
404
- span.end = (endTime, exit) => {
405
- end(endTime, exit);
406
- const query = span.attributes.get("db.query.text");
407
- if (typeof query === "string" && query.includes("MAX(sequence)")) {
408
- Deferred.doneUnsafe(streamReady, Effect.void);
409
- }
410
- };
411
- return span;
412
- },
413
- });
414
- const liveEvents = yield* sink
415
- .stream({ threadId, afterSequence: beforeSequence })
416
- .pipe(
417
- Stream.take(3),
418
- Stream.runCollect,
419
- Effect.withTracer(streamTracer),
420
- Effect.forkScoped,
421
- );
422
- yield* Deferred.await(streamReady);
423
-
424
- const scanStarted = yield* Deferred.make<void>();
425
- const releaseScan = yield* Deferred.make<void>();
426
- let blocked = false;
427
- const blockFirstScanPage: Statement.Transformer = (statement) => {
428
- const [query] = statement.compile();
429
- if (
430
- !blocked &&
431
- query.includes("FROM orchestration_events AS event") &&
432
- query.includes("ORDER BY event.sequence DESC")
433
- ) {
434
- blocked = true;
435
- return Deferred.succeed(scanStarted, undefined).pipe(
436
- Effect.andThen(Deferred.await(releaseScan)),
437
- Effect.as(statement),
438
- );
439
- }
440
- return Effect.succeed(statement);
441
- };
442
- const compaction = yield* maintenance
443
- .compactEventStore()
444
- .pipe(
445
- Effect.provideService(Statement.CurrentTransformer, blockFirstScanPage),
446
- Effect.forkScoped,
447
- );
448
- yield* Deferred.await(scanStarted);
449
- const snapshots = [3, 4, 5].map((revision) =>
450
- itemEvent(
451
- threadId,
452
- now,
453
- itemSpecs[0],
454
- revision,
455
- `event:turn-item-compaction-race:${revision}`,
456
- ),
457
- );
458
- const appended = yield* sink.write({ events: snapshots });
459
- yield* Deferred.succeed(releaseScan, undefined);
460
- assert.equal((yield* Fiber.join(compaction)).deletedEventCount, 1);
461
- const streamed = yield* Fiber.join(liveEvents);
462
- assert.deepEqual(
463
- streamed.map((stored) => stored.sequence),
464
- appended.map((stored) => stored.sequence),
465
- );
466
- assert.deepEqual(
467
- streamed.map((stored) => stored.event.id),
468
- snapshots.map((event) => event.id),
469
- );
470
- assert.equal(yield* eventStore.latestSequence(), appended.at(-1)!.sequence);
471
- const firstPassRows = yield* eventStore
472
- .read({ threadId, eventType: "turn-item.updated" })
473
- .pipe(Stream.runCollect);
474
- assert.deepEqual(
475
- firstPassRows.map((stored) => stored.sequence),
476
- [initial[1]!.sequence, beforeSequence, ...appended.map((stored) => stored.sequence)],
477
- );
478
- yield* maintenance.compactEventStore();
479
- const secondPassRows = yield* eventStore
480
- .read({ threadId, eventType: "turn-item.updated" })
481
- .pipe(Stream.runCollect);
482
- assert.deepEqual(
483
- secondPassRows.map((stored) => stored.sequence),
484
- [initial[1]!.sequence, appended.at(-1)!.sequence],
485
- );
486
- }),
487
- ).pipe(Effect.provide(layerTest)),
488
-);
apps/server/src/orchestration-v2/legacy/LegacyV1Cutover.integration.test.ts
+3
-5
@@ -42,7 +42,7 @@ import * as EventSink from "../EventSink.ts";
42
import * as EventStore from "../EventStore.ts";
43
import * as LegacyV1ThreadImporter from "./LegacyV1ThreadImporter.ts";
44
import * as Orchestrator from "../Orchestrator.ts";
45
-import * as ProjectionMaintenance from "../ProjectionMaintenance.ts";
45
+import * as ProjectionReplay from "../testkit/ProjectionReplay.ts";
46
import * as ProjectionStore from "../ProjectionStore.ts";
47
import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
48
import * as ProviderReplayHarness from "../testkit/ProviderReplayHarness.ts";
@@ -600,9 +600,7 @@ const layerBoot = (input: {
600
const layerImporterProvided = LegacyV1ThreadImporter.layer.pipe(
601
Layer.provide(Layer.mergeAll(layerStoresProvided, layerEventSinkProvided)),
602
);
603
- const layerMaintenanceProvided = ProjectionMaintenance.layer.pipe(
604
- Layer.provide(layerStoresProvided),
605
- );
603
+ const layerMaintenanceProvided = ProjectionReplay.layer.pipe(Layer.provide(layerStoresProvided));
604
const layerOrchestratorProvided = ProviderReplayHarness.layerWithRegistry(
605
{
606
name: input.name,
@@ -676,7 +674,7 @@ describe("orchestration v2 legacy v1 cutover", () => {
674
Effect.gen(function* () {
675
const sql = yield* SqlClient.SqlClient;
676
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
679
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
677
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
678
const projections = yield* ProjectionStore.ProjectionStoreV2;
679
const orchestrator = yield* Orchestrator.OrchestratorV2;
680
apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.test.ts
+5
-5
@@ -19,7 +19,7 @@ import { listLinkedPullRequestThreads } from "../../pullRequest/linkedThreads.ts
19
import * as EventSink from "../EventSink.ts";
20
import * as EventStore from "../EventStore.ts";
21
import * as LegacyV1ThreadImporter from "./LegacyV1ThreadImporter.ts";
22
-import * as ProjectionMaintenance from "../ProjectionMaintenance.ts";
22
+import * as ProjectionReplay from "../testkit/ProjectionReplay.ts";
23
import * as ProjectionStore from "../ProjectionStore.ts";
24
25
const layerDatabase = SqlitePersistence.layerMemory;
@@ -34,14 +34,14 @@ const layerEventSinkProvided = EventSink.layer.pipe(Layer.provide(layerStoresPro
34
const layerImporterProvided = LegacyV1ThreadImporter.layer.pipe(
35
Layer.provide(Layer.mergeAll(layerStoresProvided, layerEventSinkProvided)),
36
);
37
-const layerProjectionMaintenanceProvided = ProjectionMaintenance.layer.pipe(
37
+const layerProjectionReplayProvided = ProjectionReplay.layer.pipe(
38
Layer.provide(layerStoresProvided),
39
);
40
const layerTest = Layer.mergeAll(
41
layerStoresProvided,
42
layerEventSinkProvided,
43
layerImporterProvided,
44
- layerProjectionMaintenanceProvided,
44
+ layerProjectionReplayProvided,
45
);
46
47
const seedPendingTranscripts = Effect.gen(function* () {
@@ -220,7 +220,7 @@ it.layer(layerTest)("LegacyV1ThreadImporter", (it) => {
220
Effect.gen(function* () {
221
const sql = yield* SqlClient.SqlClient;
222
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
223
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
223
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
224
const projections = yield* ProjectionStore.ProjectionStoreV2;
225
const eventSink = yield* EventSink.EventSinkV2;
226
const threadId = ThreadId.make("thread:legacy-import");
@@ -605,7 +605,7 @@ it.layer(layerTest)("LegacyV1ThreadImporter", (it) => {
605
Effect.gen(function* () {
606
const sql = yield* SqlClient.SqlClient;
607
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
608
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
608
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
609
const projections = yield* ProjectionStore.ProjectionStoreV2;
610
const eventSink = yield* EventSink.EventSinkV2;
611
const threadId = ThreadId.make("thread:legacy-metadata-upgrade");
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+26
-90
@@ -68,7 +68,8 @@ import * as EffectWorker from "./EffectWorker.ts";
68
import * as EffectOutbox from "./EffectOutbox.ts";
69
import * as EventSink from "./EventSink.ts";
70
import * as ProviderRuntimeRecoveryService from "./ProviderRuntimeRecoveryService.ts";
71
-import * as ProjectionMaintenance from "./ProjectionMaintenance.ts";
71
+import * as EventStore from "./EventStore.ts";
72
+import * as ProjectionReplay from "./testkit/ProjectionReplay.ts";
73
import * as ProjectionStore from "./ProjectionStore.ts";
74
import * as PullRequestWatchReactor from "./PullRequestWatchReactor.ts";
75
import { PULL_REQUEST_WATCH_WAKE_LIMIT } from "./pullRequestWatch.ts";
@@ -277,8 +278,21 @@ const moveProject = (projectId: ProjectId, workspaceRoot: string, updatedAt: str
278
}),
279
);
280
281
+// Read-only oracle: projections still equal a replay of these unpruned test logs.
282
+const layerProjectionReplayTest = ProjectionReplay.layer.pipe(
283
+ Layer.provide(
284
+ Layer.merge(
285
+ EventStore.layerFromOrchestrationEventStore.pipe(
286
+ Layer.provide(RuntimeLayer.layerEventInfrastructure),
287
+ ),
288
+ ProjectionStore.layer,
289
+ ),
290
+ ),
291
+);
292
+
293
const layerTest = Layer.mergeAll(
294
RuntimeLayer.layer,
295
+ layerProjectionReplayTest,
296
RuntimeLayer.layerEventSink,
297
ProjectStore.layer,
298
ProjectionStore.layer,
@@ -297,7 +311,7 @@ const layerTest = Layer.mergeAll(
311
Layer.provide(layerPlatformTest),
312
);
313
300
-const layerLegacyImportTest = RuntimeLayer.layer.pipe(
314
+const layerLegacyImportTest = Layer.merge(RuntimeLayer.layer, layerProjectionReplayTest).pipe(
315
Layer.provide(McpSessionRegistryTestkit.layer),
316
Layer.provide(McpProviderSessions.layer),
317
Layer.provideMerge(SqlitePersistence.layerMemory),
@@ -817,6 +831,7 @@ it.layer(layerProjectDeletionTest)("project deletion during thread commands", (i
831
});
832
833
const layerSharedApplicationDataPlaneTest = Layer.mergeAll(
834
+ layerProjectionReplayTest,
835
RuntimeLayer.layer.pipe(Layer.provide(RuntimeLayer.layerProjectService)),
836
RuntimeLayer.layerProjectService,
837
RuntimeLayer.layerEventSink,
@@ -2210,7 +2225,7 @@ it.layer(layerLegacyImportTest)("OrchestrationV2 legacy import", (it) => {
2225
Effect.gen(function* () {
2226
const sql = yield* SqlClient.SqlClient;
2227
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
2213
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
2228
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
2229
const orchestrator = yield* Orchestrator.OrchestratorV2;
2230
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
2231
const metadataThreadId = ThreadId.make("runtime-layer-legacy-metadata-thread");
@@ -2725,7 +2740,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
2740
it.effect("keeps the branch pull request when linking another pull request", () =>
2741
Effect.gen(function* () {
2742
const orchestrator = yield* Orchestrator.OrchestratorV2;
2728
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
2743
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
2744
const threadId = ThreadId.make("branch-pr-link");
2745
const projectId = ProjectId.make("branch-pr-project");
2746
yield* seedProject({
@@ -2834,7 +2849,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
2849
it.effect("retains multiple pull requests and dismissed stack members through rebuilds", () =>
2850
Effect.gen(function* () {
2851
const orchestrator = yield* Orchestrator.OrchestratorV2;
2837
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
2852
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
2853
const threadId = ThreadId.make("runtime-multiple-pull-requests");
2854
yield* orchestrator.dispatch({
2855
type: "thread.create",
@@ -2925,7 +2940,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
2940
it.effect("starts, records, and stops a pull request watch", () =>
2941
Effect.gen(function* () {
2942
const orchestrator = yield* Orchestrator.OrchestratorV2;
2928
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
2943
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
2944
const threadId = ThreadId.make("runtime-pull-request-watch");
2945
yield* orchestrator.dispatch({
2946
type: "thread.create",
@@ -3017,7 +3032,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
3032
it.effect.each(["manual", "automatic"])("settling ends every pull request watch: %s", (mode) =>
3033
Effect.gen(function* () {
3034
const orchestrator = yield* Orchestrator.OrchestratorV2;
3020
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
3035
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
3036
const threadId = ThreadId.make(`pr-watch-settle-${mode}`);
3037
yield* orchestrator.dispatch({
3038
type: "thread.create",
@@ -4753,7 +4768,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
4768
it.effect("cancels preparing work on archive and keeps it cancelled through replay", () =>
4769
Effect.gen(function* () {
4770
const orchestrator = yield* Orchestrator.OrchestratorV2;
4756
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
4771
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
4772
const threadId = ThreadId.make("runtime-layer-archive-preparing-thread");
4773
yield* orchestrator.dispatch({
4774
type: "thread.create",
@@ -6321,7 +6336,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
6336
assert.equal(queued.length, 2);
6337
yield* recovery.reconcile(trigger);
6338
yield* recovery.reconcile("startup");
6324
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
6339
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
6340
assert.isTrue((yield* maintenance.rebuild).valid);
6341
assert.equal(yield* orchestrator.resumeQueuedRuns, 0);
6342
const held = yield* orchestrator.getThreadProjection(threadId);
@@ -7547,11 +7562,11 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("thread opens", (it)
7562
);
7563
});
7564
7550
-it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events", (it) => {
7565
+it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("PR link events", (it) => {
7566
it.effect("persists only the changed PR link and replays it without losing other links", () =>
7567
Effect.gen(function* () {
7568
const orchestrator = yield* Orchestrator.OrchestratorV2;
7554
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
7569
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
7570
const sql = yield* SqlClient.SqlClient;
7571
const threadId = ThreadId.make("compact-pr-sync");
7572
yield* orchestrator.dispatch({
@@ -7650,85 +7665,6 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events",
7665
(yield* orchestrator.getThreadShell(threadId))?.pullRequests,
7666
after?.pullRequests,
7667
);
7653
- for (const [commandId, title] of [
7654
- ["compact-pr-sync-again", "Still open PR"],
7655
- ["compact-pr-sync-merged", "Merged PR"],
7656
- ] as const) {
7657
- yield* orchestrator.dispatch({
7658
- type: "thread.pull-request-link.sync",
7659
- commandId: CommandId.make(commandId),
7660
- threadId,
7661
- ...key,
7662
- number: 2,
7663
- snapshot: {
7664
- state: title === "Merged PR" ? "merged" : "open",
7665
- title,
7666
- headBranch: "feature/pr",
7667
- baseBranch: "main",
7668
- isDraft: false,
7669
- updatedAt: null,
7670
- syncedAt: "2026-10-13T13:00:00.000Z",
7671
- },
7672
- stack: null,
7673
- });
7674
- }
7675
- const latest = yield* orchestrator.getThreadShell(threadId);
7676
- yield* maintenance.compactEventStore();
7677
- const linkEvents = yield* sql<{ command_id: string }>`
7678
- SELECT command_id FROM orchestration_events
7679
- WHERE stream_id = ${threadId} AND event_type = 'thread.pull-request-link-updated'
7680
- ORDER BY sequence
7681
- `;
7682
- assert.deepEqual(
7683
- linkEvents.map((row) => row.command_id),
7684
- ["compact-pr-sync", "compact-pr-sync-merged"],
7685
- );
7686
- assert.isTrue((yield* maintenance.rebuild).valid);
7687
- assert.deepEqual(
7688
- (yield* orchestrator.getThreadShell(threadId))?.pullRequests,
7689
- latest?.pullRequests,
7690
- );
7691
-
7692
- // Visits preserve updatedAt, so compaction must retain earlier renames; later renames supersede visits.
7693
- const threadEvents = () => sql<{ event_type: string }>`
7694
- SELECT event_type FROM orchestration_events
7695
- WHERE stream_id = ${threadId}
7696
- AND event_type IN ('thread.metadata-updated', 'thread.visited')
7697
- ORDER BY sequence
7698
- `;
7699
- yield* orchestrator.dispatch({
7700
- type: "thread.metadata.update",
7701
- commandId: CommandId.make("compact-pr-rename"),
7702
- threadId,
7703
- title: "Renamed before visit",
7704
- });
7705
- for (const [commandId, visitedAt] of [
7706
- ["compact-pr-visit", "2026-10-13T14:00:00.000Z"],
7707
- ["compact-pr-visit-again", "2026-10-13T15:00:00.000Z"],
7708
- ] as const) {
7709
- yield* orchestrator.dispatch({
7710
- type: "thread.visit",
7711
- commandId: CommandId.make(commandId),
7712
- threadId,
7713
- visitedAt,
7714
- });
7715
- }
7716
- yield* maintenance.compactEventStore();
7717
- assert.deepEqual(
7718
- (yield* threadEvents()).map((row) => row.event_type),
7719
- ["thread.metadata-updated", "thread.visited"],
7720
- );
7721
- yield* orchestrator.dispatch({
7722
- type: "thread.metadata.update",
7723
- commandId: CommandId.make("compact-pr-rename-again"),
7724
- threadId,
7725
- title: "Renamed after visit",
7726
- });
7727
- yield* maintenance.compactEventStore();
7728
- assert.deepEqual(
7729
- (yield* threadEvents()).map((row) => row.event_type),
7730
- ["thread.metadata-updated"],
7731
- );
7668
}),
7669
);
7670
});
apps/server/src/orchestration-v2/testkit/ProjectionReplay.ts
new
+182
@@ -0,0 +1,182 @@
1
+import { ThreadId } from "@t3tools/contracts";
2
+import * as Context from "effect/Context";
3
+import * as DateTime from "effect/DateTime";
4
+import * as Effect from "effect/Effect";
5
+import * as Layer from "effect/Layer";
6
+import * as Schema from "effect/Schema";
7
+import * as Stream from "effect/Stream";
8
+import * as SqlClient from "effect/sql/SqlClient";
9
+
10
+import * as EventStore from "../EventStore.ts";
11
+import * as ProjectionStore from "../ProjectionStore.ts";
12
+
13
+export interface ProjectionReplayVerification {
14
+ readonly valid: boolean;
15
+ readonly schemaVersion: number;
16
+ readonly expectedSequence: number;
17
+ readonly projectionSequence: number;
18
+ readonly unreadableThreadIds: ReadonlyArray<ThreadId>;
19
+ readonly missingThreadIds: ReadonlyArray<ThreadId>;
20
+ readonly unexpectedThreadIds: ReadonlyArray<ThreadId>;
21
+}
22
+
23
+export class ProjectionReplayError extends Schema.TaggedError<ProjectionReplayError>()(
24
+ "ProjectionReplayError",
25
+ { cause: Schema.Defect() },
26
+) {}
27
+
28
+/**
29
+ * Test oracle: projections must equal a replay of an unpruned event log. Production prunes
30
+ * events the projections cover, so it never replays (see ProjectionMaintenance).
31
+ */
32
+export class ProjectionReplay extends Context.Service<
33
+ ProjectionReplay,
34
+ {
35
+ readonly verify: Effect.Effect<ProjectionReplayVerification, ProjectionReplayError>;
36
+ readonly rebuild: Effect.Effect<ProjectionReplayVerification, ProjectionReplayError>;
37
+ }
38
+>()("t3/orchestration-v2/testkit/ProjectionReplay") {}
39
+
40
+type ProjectionMetadataRow = {
41
+ readonly schema_version: number;
42
+ readonly last_sequence: number;
43
+};
44
+
45
+export const layer: Layer.Layer<
46
+ ProjectionReplay,
47
+ never,
48
+ EventStore.EventStoreV2 | ProjectionStore.ProjectionStoreV2 | SqlClient.SqlClient
49
+> = Layer.effect(
50
+ ProjectionReplay,
51
+ Effect.gen(function* () {
52
+ const sql = yield* SqlClient.SqlClient;
53
+ const eventStore = yield* EventStore.EventStoreV2;
54
+ const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
55
+
56
+ const verify = Effect.gen(function* () {
57
+ const expectedThreadRows = yield* sql<{ readonly thread_id: string }>`
58
+ SELECT DISTINCT stream_id AS thread_id
59
+ FROM orchestration_events INDEXED BY orchestration_events_v2_created_threads_idx
60
+ WHERE application_event_version = 2
61
+ AND aggregate_kind = 'thread'
62
+ AND event_type = 'thread.created'
63
+ ORDER BY stream_id ASC
64
+ `;
65
+ const projectionRows = yield* sql<{ readonly thread_id: string }>`
66
+ SELECT thread_id
67
+ FROM orchestration_v2_projection_threads
68
+ ORDER BY thread_id ASC
69
+ `;
70
+ const actualIds = projectionRows.map((row) => ThreadId.make(row.thread_id));
71
+ const expectedIds = expectedThreadRows.map((row) => ThreadId.make(row.thread_id));
72
+ const actualSet = new Set(actualIds);
73
+ const expectedSet = new Set(expectedIds);
74
+ const missingThreadIds = expectedIds.filter((threadId) => !actualSet.has(threadId));
75
+ const unexpectedThreadIds = actualIds.filter((threadId) => !expectedSet.has(threadId));
76
+ const unreadableThreadIds = yield* projectionStore.getUnreadableThreadIds();
77
+ const metadata = yield* sql<ProjectionMetadataRow>`
78
+ SELECT schema_version, last_sequence
79
+ FROM orchestration_v2_projection_metadata
80
+ WHERE projection_name = 'thread-projections'
81
+ LIMIT 1
82
+ `;
83
+ const expectedSequence = yield* eventStore.latestSequence();
84
+ const schemaVersion = metadata[0]?.schema_version ?? 0;
85
+ const projectionSequence = metadata[0]?.last_sequence ?? 0;
86
+ return {
87
+ valid:
88
+ schemaVersion === ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION &&
89
+ projectionSequence === expectedSequence &&
90
+ missingThreadIds.length === 0 &&
91
+ unexpectedThreadIds.length === 0 &&
92
+ unreadableThreadIds.length === 0,
93
+ schemaVersion,
94
+ expectedSequence,
95
+ projectionSequence,
96
+ unreadableThreadIds,
97
+ missingThreadIds,
98
+ unexpectedThreadIds,
99
+ } satisfies ProjectionReplayVerification;
100
+ }).pipe(sql.withTransaction);
101
+
102
+ const rebuild = Effect.gen(function* () {
103
+ yield* sql.withTransaction(
104
+ Effect.gen(function* () {
105
+ const throughSequence = yield* eventStore.latestSequence();
106
+ yield* sql`DELETE FROM orchestration_v2_projection_context_transfers`;
107
+ yield* sql`DELETE FROM orchestration_v2_projection_context_handoffs`;
108
+ yield* sql`DELETE FROM orchestration_v2_projection_checkpoints`;
109
+ yield* sql`DELETE FROM orchestration_v2_projection_checkpoint_scopes`;
110
+ yield* sql`DELETE FROM orchestration_v2_projection_turn_items`;
111
+ yield* sql`DELETE FROM orchestration_v2_projection_plans`;
112
+ yield* sql`DELETE FROM orchestration_v2_projection_messages`;
113
+ yield* sql`DELETE FROM orchestration_v2_projection_runtime_requests`;
114
+ yield* sql`DELETE FROM orchestration_v2_projection_provider_turns`;
115
+ yield* sql`DELETE FROM orchestration_v2_projection_provider_threads`;
116
+ yield* sql`DELETE FROM orchestration_v2_projection_provider_session_bindings`;
117
+ yield* sql`DELETE FROM orchestration_v2_projection_provider_sessions`;
118
+ yield* sql`DELETE FROM orchestration_v2_projection_subagents`;
119
+ yield* sql`DELETE FROM orchestration_v2_projection_nodes`;
120
+ yield* sql`DELETE FROM orchestration_v2_projection_run_attempts`;
121
+ yield* sql`DELETE FROM orchestration_v2_projection_runs`;
122
+ yield* sql`DELETE FROM orchestration_v2_projection_threads`;
123
+ yield* sql`DELETE FROM orchestration_v2_turn_item_positions`;
124
+
125
+ const pageSize = 500;
126
+ let lastSequence = 0;
127
+ while (true) {
128
+ const page = yield* eventStore
129
+ .read({ afterSequence: lastSequence, throughSequence, limit: pageSize })
130
+ .pipe(Stream.runCollect);
131
+ for (const stored of page) {
132
+ yield* projectionStore.apply(stored.event);
133
+ if (stored.event.type === "turn-item.updated") {
134
+ yield* sql`
135
+ INSERT INTO orchestration_v2_turn_item_positions (
136
+ thread_id,
137
+ turn_item_id,
138
+ ordinal
139
+ )
140
+ VALUES (
141
+ ${stored.event.threadId},
142
+ ${stored.event.payload.id},
143
+ ${stored.event.payload.ordinal}
144
+ )
145
+ ON CONFLICT(thread_id, turn_item_id) DO UPDATE SET
146
+ ordinal = excluded.ordinal
147
+ `;
148
+ }
149
+ lastSequence = stored.sequence;
150
+ }
151
+ if (page.length < pageSize) break;
152
+ yield* Effect.yieldNow;
153
+ }
154
+ const now = DateTime.formatIso(yield* DateTime.now);
155
+ yield* sql`
156
+ INSERT INTO orchestration_v2_projection_metadata (
157
+ projection_name,
158
+ schema_version,
159
+ last_sequence,
160
+ updated_at
161
+ )
162
+ VALUES (
163
+ 'thread-projections',
164
+ ${ProjectionStore.ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION},
165
+ ${lastSequence},
166
+ ${now}
167
+ )
168
+ ON CONFLICT(projection_name) DO UPDATE SET
169
+ schema_version = excluded.schema_version,
170
+ last_sequence = excluded.last_sequence,
171
+ updated_at = excluded.updated_at
172
+ `;
173
+ }),
174
+ );
175
+ return yield* verify;
176
+ });
177
+
178
+ const mapError = <A, E>(effect: Effect.Effect<A, E>) =>
179
+ effect.pipe(Effect.mapError((cause) => new ProjectionReplayError({ cause })));
180
+ return ProjectionReplay.of({ verify: mapError(verify), rebuild: mapError(rebuild) });
181
+ }),
182
+);
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+3
-3
@@ -52,7 +52,7 @@ import * as LegacyV1ThreadImporter from "../legacy/LegacyV1ThreadImporter.ts";
52
import * as Orchestrator from "../Orchestrator.ts";
53
import * as EffectWorker from "../EffectWorker.ts";
54
import * as EffectOutbox from "../EffectOutbox.ts";
55
-import * as ProjectionMaintenance from "../ProjectionMaintenance.ts";
55
+import * as ProjectionReplay from "./ProjectionReplay.ts";
56
import * as ProjectionStore from "../ProjectionStore.ts";
57
import * as ProviderAdapterRegistry from "../ProviderAdapterRegistry.ts";
58
import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
@@ -3148,7 +3148,7 @@ describe("orchestration v2 provider switching", () => {
3148
const layerImporterProvided = LegacyV1ThreadImporter.layer.pipe(
3149
Layer.provide(Layer.mergeAll(layerStoresProvided, layerEventSinkProvided)),
3150
);
3151
- const layerMaintenanceProvided = ProjectionMaintenance.layer.pipe(
3151
+ const layerMaintenanceProvided = ProjectionReplay.layer.pipe(
3152
Layer.provide(layerStoresProvided),
3153
);
3154
const layerOrchestratorProvided = ProviderReplayHarness.layerWithRegistry(
@@ -3177,7 +3177,7 @@ describe("orchestration v2 provider switching", () => {
3177
const projection = yield* Effect.gen(function* () {
3178
const sql = yield* SqlClient.SqlClient;
3179
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
3180
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
3180
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
3181
const orchestrator = yield* Orchestrator.OrchestratorV2;
3182
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
3183
apps/server/src/persistence/DatabaseMaintenance.test.ts
+2
-14
@@ -12,9 +12,6 @@ import * as SqlClient from "effect/sql/SqlClient";
12
13
import * as DatabaseMaintenance from "./DatabaseMaintenance.ts";
14
import * as Sqlite from "./Sqlite.ts";
15
-import * as EventStore from "../orchestration-v2/EventStore.ts";
16
-import * as ProjectionMaintenance from "../orchestration-v2/ProjectionMaintenance.ts";
17
-import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
15
16
it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))(
17
"DatabaseMaintenance",
@@ -111,17 +108,8 @@ it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))
108
const maintenance = yield* DatabaseMaintenance.DatabaseMaintenance;
109
const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-compact-" });
110
const dbPath = path.join(baseDir, "userdata", "statev2.sqlite");
114
- yield* Effect.flatMap(
115
- ProjectionMaintenance.ProjectionMaintenanceV2,
116
- (projections) => projections.rebuild,
117
- ).pipe(
118
- Effect.provide(
119
- ProjectionMaintenance.layer.pipe(
120
- Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
121
- Layer.provideMerge(Sqlite.layerFromPath(dbPath)),
122
- ),
123
- ),
124
- );
111
+ // Opening the database migrates it.
112
+ yield* Effect.void.pipe(Effect.provide(Sqlite.layerFromPath(dbPath)));
113
yield* Effect.gen(function* () {
114
const sql = yield* SqlClient.SqlClient;
115
yield* sql`PRAGMA auto_vacuum = NONE`;
apps/server/src/persistence/DatabaseMaintenance.ts
+3
-26
@@ -9,9 +9,7 @@ import * as Schema from "effect/Schema";
9
import * as SqlClient from "effect/sql/SqlClient";
10
import { isSqlError } from "effect/sql/SqlError";
11
12
-import * as EventStore from "../orchestration-v2/EventStore.ts";
12
import * as ProjectionMaintenance from "../orchestration-v2/ProjectionMaintenance.ts";
14
-import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
13
import { rebuildMessageSearch } from "../orchestration-v2/messageSearchIndex.ts";
14
15
export class DatabaseMissingError extends Schema.TaggedError<DatabaseMissingError>()(
@@ -32,15 +30,6 @@ export class DatabaseInUseError extends Schema.TaggedError<DatabaseInUseError>()
30
}
31
}
32
35
-export class DatabaseProjectionsInvalidError extends Schema.TaggedError<DatabaseProjectionsInvalidError>()(
36
- "DatabaseProjectionsInvalidError",
37
- { path: Schema.String },
38
-) {
39
- override get message() {
40
- return "Database projections need repair before compaction. Start T3 Code once so it rebuilds them, then run doctor again. If this CLI is older than the one that wrote the database, update T3 Code first.";
41
- }
42
-}
43
-
33
export class DatabaseInspectError extends Schema.TaggedError<DatabaseInspectError>()(
34
"DatabaseInspectError",
35
{ path: Schema.String, cause: Schema.Defect() },
@@ -89,10 +78,7 @@ export class DatabaseMaintenance extends Context.Service<
78
readonly deletedEventCount: number;
79
readonly deletedReceiptCount: number;
80
},
92
- | DatabaseMissingError
93
- | DatabaseInUseError
94
- | DatabaseProjectionsInvalidError
95
- | DatabaseCompactError
81
+ DatabaseMissingError | DatabaseInUseError | DatabaseCompactError
82
>;
83
/** Deletes only the pre-0.1.0 database; earlier versions still use it. */
84
readonly removeLegacyDatabase: (
@@ -101,9 +87,7 @@ export class DatabaseMaintenance extends Context.Service<
87
}
88
>()("t3/persistence/DatabaseMaintenance") {}
89
104
-const isDomainError = Schema.is(
105
- Schema.Union([DatabaseMissingError, DatabaseInUseError, DatabaseProjectionsInvalidError]),
106
-);
90
+const isDomainError = Schema.is(Schema.Union([DatabaseMissingError, DatabaseInUseError]));
91
92
const readStorage = Effect.gen(function* () {
93
const sql = yield* SqlClient.SqlClient;
@@ -180,13 +164,7 @@ const make = Effect.gen(function* () {
164
const sql = yield* SqlClient.SqlClient;
165
yield* lockExclusively(databasePath);
166
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
183
- if (!(yield* maintenance.verify).valid) {
184
- return yield* new DatabaseProjectionsInvalidError({ path: databasePath });
185
- }
186
- const summary = yield* maintenance.compactEventStore();
187
- if (!(yield* maintenance.verify).valid) {
188
- return yield* new DatabaseProjectionsInvalidError({ path: databasePath });
189
- }
167
+ const summary = yield* maintenance.pruneEventStore();
168
// VACUUM is required to enable incremental auto_vacuum on existing databases.
169
yield* sql`PRAGMA auto_vacuum = INCREMENTAL`;
170
yield* sql`VACUUM`;
@@ -196,7 +174,6 @@ const make = Effect.gen(function* () {
174
}).pipe(
175
Effect.provide(
176
ProjectionMaintenance.layer.pipe(
199
- Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
177
Layer.provideMerge(NodeSqliteClient.layer({ filename: databasePath })),
178
),
179
),
apps/server/src/persistence/DatabaseStorage.test.ts
+2
-7
@@ -9,9 +9,7 @@ import * as TestClock from "effect/testing/TestClock";
9
import * as SqlClient from "effect/sql/SqlClient";
10
11
import * as ServerConfig from "../config.ts";
12
-import * as EventStore from "../orchestration-v2/EventStore.ts";
12
import * as ProjectionMaintenance from "../orchestration-v2/ProjectionMaintenance.ts";
14
-import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
13
import * as DatabaseStorage from "./DatabaseStorage.ts";
14
import * as Sqlite from "./Sqlite.ts";
15
@@ -28,7 +26,6 @@ const storageLayer = <E>(
26
) =>
27
DatabaseStorage.layer.pipe(
28
Layer.provide(ProjectionMaintenance.layer),
31
- Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
29
Layer.provideMerge(Sqlite.layerMemory),
30
Layer.provideMerge(config),
31
Layer.provide(NodeServices.layer),
@@ -54,14 +51,12 @@ it.effect("a new database returns freed pages online without losing data", () =>
51
);
52
53
const compactionLayer = (
57
- compactEventStore: ProjectionMaintenance.ProjectionMaintenanceV2["Service"]["compactEventStore"],
54
+ pruneEventStore: ProjectionMaintenance.ProjectionMaintenanceV2["Service"]["pruneEventStore"],
55
) =>
56
DatabaseStorage.layer.pipe(
57
Layer.provide(
58
Layer.succeed(ProjectionMaintenance.ProjectionMaintenanceV2, {
62
- compactEventStore,
63
- verify: Effect.die("unexpected verification"),
64
- rebuild: Effect.die("unexpected rebuild"),
59
+ pruneEventStore,
60
}),
61
),
62
Layer.provide(Sqlite.layerMemory),
apps/server/src/persistence/DatabaseStorage.ts
+1
-2
@@ -31,7 +31,6 @@ export class DatabaseStorage extends Context.Service<
31
}
32
>()("t3/persistence/DatabaseStorage") {}
33
34
-// Deleted events have newer retained replacements; old cursors replay to the same state.
34
const make = Effect.gen(function* () {
35
const sql = yield* SqlClient.SqlClient;
36
const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
@@ -108,7 +107,7 @@ const make = Effect.gen(function* () {
107
yield* Effect.sleep((Number(elapsed) / 1_000_000) * 9);
108
sliceStarted = yield* Clock.monotonicTimeNanos;
109
});
111
- const result = yield* maintenance.compactEventStore(betweenPages);
110
+ const result = yield* maintenance.pruneEventStore(betweenPages);
111
const completedAt = yield* Clock.currentTimeMillis;
112
yield* fs.writeFileString(compactedAtPath, String(completedAt)).pipe(Effect.ignore);
113
if (result.deletedEventCount > 0 || result.deletedReceiptCount > 0) {
apps/server/src/persistence/Migrations/064_CanonicalizeThreadPullRequestLinks.test.ts
+3
-3
@@ -9,7 +9,7 @@ import * as SqlClient from "effect/sql/SqlClient";
9
10
import * as EventStore from "../../orchestration-v2/EventStore.ts";
11
import * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts";
12
-import * as ProjectionMaintenance from "../../orchestration-v2/ProjectionMaintenance.ts";
12
+import * as ProjectionReplay from "../../orchestration-v2/testkit/ProjectionReplay.ts";
13
import { listLinkedPullRequestThreads } from "../../pullRequest/linkedThreads.ts";
14
import { runMigrations } from "../Migrations.ts";
15
@@ -53,7 +53,7 @@ const thread = (id: string) => ({
53
});
54
55
const layer = it.layer(
56
- ProjectionMaintenance.layer.pipe(
56
+ ProjectionReplay.layer.pipe(
57
Layer.provideMerge(
58
Layer.mergeAll(EventStore.layer, ProjectionStore.layer).pipe(
59
Layer.provideMerge(NodeSqliteClient.layer({ filename: ":memory:" })),
@@ -156,7 +156,7 @@ layer("064_CanonicalizeThreadPullRequestLinks", (it) => {
156
`;
157
assert.equal(v1?.payload_json, v1Payload);
158
159
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
159
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
160
assert.isTrue((yield* maintenance.rebuild).valid);
161
for (const [host, repository, id] of [
162
["github.com", "acme/web", "github"],
apps/server/src/persistence/Migrations/066_RepairClientSettlementTimestamps.test.ts
+3
-3
@@ -6,13 +6,13 @@ import * as Layer from "effect/Layer";
6
import * as SqlClient from "effect/sql/SqlClient";
7
8
import * as EventStore from "../../orchestration-v2/EventStore.ts";
9
-import * as ProjectionMaintenance from "../../orchestration-v2/ProjectionMaintenance.ts";
9
+import * as ProjectionReplay from "../../orchestration-v2/testkit/ProjectionReplay.ts";
10
import * as ProjectionStore from "../../orchestration-v2/ProjectionStore.ts";
11
import { runMigrations } from "../Migrations.ts";
12
import Migration0066 from "./066_RepairClientSettlementTimestamps.ts";
13
14
const layer = it.layer(
15
- ProjectionMaintenance.layer.pipe(
15
+ ProjectionReplay.layer.pipe(
16
Layer.provideMerge(
17
Layer.mergeAll(EventStore.layer, ProjectionStore.layer).pipe(
18
Layer.provideMerge(NodeSqliteClient.layer({ filename: ":memory:" })),
@@ -363,7 +363,7 @@ layer("066_RepairClientSettlementTimestamps", (it) => {
363
yield* Migration0066;
364
assert.deepEqual(yield* snapshot, repaired);
365
366
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
366
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
367
assert.isTrue((yield* maintenance.rebuild).valid);
368
assert.deepEqual(yield* imported, expectedImported);
369
}),
apps/server/src/persistence/OrchestrationEventStore.ts
+18
-1
@@ -33,6 +33,10 @@ import {
33
type OrchestrationEventStoreError,
34
} from "./Errors.ts";
35
36
+/** Projection metadata rows: events at or below the pruned-through sequence may be gone. */
37
+export const EVENT_LOG_PRUNED_THROUGH = "event-log-pruned-through";
38
+export const EVENT_LOG_PRUNE_MARK = "event-log-prune-mark";
39
+
40
export type UnsequencedProjectEvent = ApplicationProjectEvent extends infer Event
41
? Event extends ApplicationProjectEvent
42
? Omit<Event, "sequence">
@@ -93,6 +97,9 @@ export class OrchestrationEventStore extends Context.Service<
97
98
readonly latestApplicationSequence: Effect.Effect<number, OrchestrationEventStoreError>;
99
100
+ /** Replay from a cursor below this misses pruned events; resync from a snapshot instead. */
101
+ readonly prunedThroughSequence: Effect.Effect<number, OrchestrationEventStoreError>;
102
+
103
/** Unknown event types fail unless skipUnknownEventTypes logs and drops them; only client replay may opt in. */
104
readonly readApplicationEvents: (input: {
105
readonly afterSequence: number;
@@ -450,7 +457,7 @@ const makeEventStore = Effect.gen(function* () {
457
const promptKey = subagent === undefined ? undefined : `${event.type}:${subagent.id}`;
458
const hint = promptKey === undefined ? undefined : promptHints.get(promptKey);
459
const payloadJson = yield* encodeJson(encoded.payload);
453
- // Deduplicate repeated progress prompts via anchor rows that ProjectionMaintenance never deletes.
460
+ // Deduplicate repeated progress prompts via anchor rows; pruning keeps anchors that retained rows reference.
461
const storedPayload =
462
hint !== undefined && subagent !== undefined
463
? sql`CASE WHEN EXISTS (
@@ -672,6 +679,15 @@ const makeEventStore = Effect.gen(function* () {
679
),
680
);
681
682
+ const prunedThroughSequence = sql<{ readonly last_sequence: number }>`
683
+ SELECT last_sequence FROM orchestration_v2_projection_metadata
684
+ WHERE projection_name = ${EVENT_LOG_PRUNED_THROUGH}
685
+ `.pipe(
686
+ NodeSqliteClient.readOnly,
687
+ Effect.map((rows) => rows[0]?.last_sequence ?? 0),
688
+ Effect.mapError(toPersistenceSqlError("OrchestrationEventStore.prunedThroughSequence:query")),
689
+ );
690
+
691
// Advance over raw rows so even a full page of unknown types cannot end replay early.
692
const catchUpApplicationEvents = (input: {
693
readonly afterSequence: number;
@@ -748,6 +764,7 @@ const makeEventStore = Effect.gen(function* () {
764
getReplayStats,
765
latestAgentSequence,
766
latestApplicationSequence,
767
+ prunedThroughSequence,
768
readApplicationEvents: catchUpApplicationEvents,
769
publishCommitted: (events) => PubSub.publishAll(committedEvents, events).pipe(Effect.asVoid),
770
streamApplicationEvents,
apps/server/src/project/ProjectService.deletion.test.ts
+3
-3
@@ -22,7 +22,7 @@ import * as EventSink from "../orchestration-v2/EventSink.ts";
22
import * as EventStore from "../orchestration-v2/EventStore.ts";
23
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
24
import * as LegacyV1ThreadImporter from "../orchestration-v2/legacy/LegacyV1ThreadImporter.ts";
25
-import * as ProjectionMaintenance from "../orchestration-v2/ProjectionMaintenance.ts";
25
+import * as ProjectionReplay from "../orchestration-v2/testkit/ProjectionReplay.ts";
26
import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
27
import * as ProjectStore from "../orchestration-v2/ProjectStore.ts";
28
import * as ThreadCommandExecutor from "../orchestration-v2/ThreadCommandExecutor.ts";
@@ -40,7 +40,7 @@ const layerEventPersistence = EventSink.layer.pipe(
40
);
41
const layerServices = Layer.mergeAll(
42
LegacyV1ThreadImporter.layer.pipe(Layer.provideMerge(layerEventPersistence)),
43
- ProjectionMaintenance.layer.pipe(Layer.provide(layerEventPersistence)),
43
+ ProjectionReplay.layer.pipe(Layer.provide(layerEventPersistence)),
44
ProjectStore.layer,
45
IdAllocator.layer,
46
ThreadCommandExecutor.layer,
@@ -310,7 +310,7 @@ it.effect(
310
311
yield* Effect.gen(function* () {
312
const importer = yield* LegacyV1ThreadImporter.LegacyV1ThreadImporter;
313
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
313
+ const maintenance = yield* ProjectionReplay.ProjectionReplay;
314
const projections = yield* ProjectionStore.ProjectionStoreV2;
315
const service = yield* ProjectService.make;
316
assert.deepEqual(yield* importer.reconcileShells, {
apps/server/src/server.ts
+1
-7
@@ -198,7 +198,6 @@ import * as ThreadSettlementService from "./orchestration-v2/ThreadSettlementSer
198
import * as IdleCompactionService from "./orchestration-v2/IdleCompactionService.ts";
199
import * as ThreadPullRequestService from "./orchestration-v2/ThreadPullRequestService.ts";
200
import * as RunFinalizationService from "./orchestration-v2/RunFinalizationService.ts";
201
-import * as EventStoreV2 from "./orchestration-v2/EventStore.ts";
201
import * as ProjectionMaintenance from "./orchestration-v2/ProjectionMaintenance.ts";
202
import * as ProjectionStoreV2 from "./orchestration-v2/ProjectionStore.ts";
203
import { makePersistedServerRuntimeState } from "./serverRuntimeState.ts";
@@ -629,12 +628,7 @@ const layerRuntimeCoreDependenciesBase = Layer.mergeAll(
628
layerThreadSettlementWorker,
629
layerIdleCompactionWorker,
630
StorageCleanup.layer.pipe(Layer.provide(ProjectionStoreV2.layer)),
632
- DatabaseStorage.layer.pipe(
633
- Layer.provide(ProjectionMaintenance.layer),
634
- Layer.provide(
635
- Layer.merge(EventStoreV2.layerFromOrchestrationEventStore, ProjectionStoreV2.layer),
636
- ),
637
- ),
631
+ DatabaseStorage.layer.pipe(Layer.provide(ProjectionMaintenance.layer)),
632
layerThreadPullRequestWorker,
633
DelegatedTaskAnalytics.layer,
634
Layer.effectDiscard(
apps/server/src/ws.test.ts
+160
-77
@@ -22,6 +22,8 @@ import {
22
import * as Deferred from "effect/Deferred";
23
import * as DateTime from "effect/DateTime";
24
import * as Effect from "effect/Effect";
25
+import * as Exit from "effect/Exit";
26
+import * as Cause from "effect/Cause";
27
import * as Fiber from "effect/Fiber";
28
import * as Layer from "effect/Layer";
29
import * as PubSub from "effect/PubSub";
@@ -31,6 +33,7 @@ import * as TestClock from "effect/testing/TestClock";
33
import { Headers } from "effect/http";
34
import { RpcGroup, RpcMessage, RpcServer } from "effect/rpc";
35
import * as SqlClient from "effect/sql/SqlClient";
36
+import * as Statement from "effect/sql/Statement";
37
38
import * as PreviewAutomationBroker from "./mcp/PreviewAutomationBroker.ts";
39
import * as ServerBrowser from "./preview/ServerBrowser.ts";
@@ -115,9 +118,6 @@ import * as Keybindings from "./keybindings.ts";
118
import * as ServerSettings from "./serverSettings.ts";
119
import * as ManagedPolicy from "./policy/ManagedPolicy.ts";
120
118
-import * as EventStore from "./orchestration-v2/EventStore.ts";
119
-import * as ProjectionMaintenance from "./orchestration-v2/ProjectionMaintenance.ts";
120
-import * as ProjectionStore from "./orchestration-v2/ProjectionStore.ts";
121
import * as ThreadManagement from "./orchestration-v2/ThreadManagementService.ts";
122
import * as ProjectStore from "./orchestration-v2/ProjectStore.ts";
123
import * as OrchestrationEventStore from "./persistence/OrchestrationEventStore.ts";
@@ -1161,93 +1161,176 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
1161
),
1162
);
1163
1164
-// Compaction keeps the oldest and newest event per message, so a cursor older
1165
-// than the deleted rows still replays each message where the client would
1166
-// first have seen it (reducers append on first sight) and reaches the same
1167
-// final state. No snapshot fallback is needed.
1168
-it.effect("thread resume across compacted history keeps message order and final state", () =>
1164
+// Pruned history cannot be replayed: a cursor below the boundary must resync from a snapshot.
1165
+it.effect.each(["thread", "shell"] as const)(
1166
+ "%s resume from a cursor below the pruned boundary loads a snapshot",
1167
+ (scope) =>
1168
+ Effect.gen(function* () {
1169
+ const sql = yield* SqlClient.SqlClient;
1170
+ const now = "2026-10-13T00:00:00.000Z";
1171
+ const threadId = ThreadId.make("thread:pruned");
1172
+ const insert = (version: number) => sql<{ sequence: number }>`
1173
+ INSERT INTO orchestration_events (
1174
+ event_id, aggregate_kind, stream_id, stream_version, event_type,
1175
+ occurred_at, actor_kind, payload_json, metadata_json, application_event_version
1176
+ ) VALUES (
1177
+ ${`pruned:${version}`}, 'thread', ${threadId}, ${version}, 'provider-session.detached',
1178
+ ${now}, 'server', ${JSON.stringify({ providerSessionId: "session:pruned", detachedAt: now })},
1179
+ '{}', 2
1180
+ ) RETURNING sequence
1181
+ `;
1182
+ const [first] = yield* insert(0);
1183
+ const [second] = yield* insert(1);
1184
+ yield* insert(2);
1185
+ yield* sql`
1186
+ INSERT INTO orchestration_v2_projection_metadata (
1187
+ projection_name, schema_version, last_sequence, updated_at
1188
+ ) VALUES (${OrchestrationEventStore.EVENT_LOG_PRUNED_THROUGH}, 2, ${second!.sequence}, ${now})
1189
+ `;
1190
+ const subscription = Effect.gen(function* () {
1191
+ const stream =
1192
+ scope === "thread"
1193
+ ? yield* subscribeOrchestrationV2Thread({ threadId, afterSequence: first!.sequence })
1194
+ : yield* subscribeOrchestrationV2Shell({ afterSequence: first!.sequence });
1195
+ return stream;
1196
+ });
1197
+ const exit = yield* subscription.pipe(
1198
+ Effect.provideService(ThreadManagement.ThreadManagementService, {
1199
+ ensureLegacyTranscript: () => Effect.void,
1200
+ getThreadSnapshot: () => Effect.die("snapshot"),
1201
+ listActiveShellIndex: () => Effect.die("snapshot"),
1202
+ readShellSnapshot: () => Effect.die("snapshot"),
1203
+ listActiveSubagentThreadIds: () => Effect.die("replay"),
1204
+ } as unknown as ThreadManagement.ThreadManagementService["Service"]),
1205
+ Effect.provideService(ProjectStore.ProjectStoreV2, {
1206
+ listShells: () => Effect.succeed([]),
1207
+ } as unknown as ProjectStore.ProjectStoreV2["Service"]),
1208
+ Effect.provideService(
1209
+ ProjectService.ProjectService,
1210
+ {} as ProjectService.ProjectService["Service"],
1211
+ ),
1212
+ Effect.provideService(ProjectEnrichment.ProjectEnrichmentService, {
1213
+ subscribeChanges: PubSub.subscribe(yield* PubSub.unbounded()),
1214
+ } as ProjectEnrichment.ProjectEnrichmentService["Service"]),
1215
+ Effect.exit,
1216
+ );
1217
+ assert.deepEqual(Exit.isFailure(exit) ? Cause.squash(exit.cause) : exit, "snapshot");
1218
+ }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
1219
+);
1220
+
1221
+// A prune pass can delete replayed rows after the boundary check; the read must not be trusted.
1222
+it.effect.each(["thread", "shell"] as const)(
1223
+ "%s resume loads a snapshot when pruning advances during the replay read",
1224
+ (scope) =>
1225
+ Effect.gen(function* () {
1226
+ const sql = yield* SqlClient.SqlClient;
1227
+ const now = "2026-10-13T00:00:00.000Z";
1228
+ const threadId = ThreadId.make("thread:prune-race");
1229
+ const insert = (version: number) => sql<{ sequence: number }>`
1230
+ INSERT INTO orchestration_events (
1231
+ event_id, aggregate_kind, stream_id, stream_version, event_type,
1232
+ occurred_at, actor_kind, payload_json, metadata_json, application_event_version
1233
+ ) VALUES (
1234
+ ${`prune-race:${version}`}, 'thread', ${threadId}, ${version}, 'provider-session.detached',
1235
+ ${now}, 'server', ${JSON.stringify({ providerSessionId: "session:race", detachedAt: now })},
1236
+ '{}', 2
1237
+ ) RETURNING sequence
1238
+ `;
1239
+ const [first] = yield* insert(0);
1240
+ const [second] = yield* insert(1);
1241
+ yield* insert(2);
1242
+
1243
+ const readStarted = yield* Deferred.make<void>();
1244
+ const releaseRead = yield* Deferred.make<void>();
1245
+ let gated = false;
1246
+ const gateReplayRead: Statement.Transformer = (statement) => {
1247
+ const [query] = statement.compile();
1248
+ // The paged replay read (readApplicationRows), not the stats or high-water queries.
1249
+ if (!gated && /^\s*SELECT\s+sequence,\s+event_id,/.test(query)) {
1250
+ gated = true;
1251
+ return Deferred.succeed(readStarted, undefined).pipe(
1252
+ Effect.andThen(Deferred.await(releaseRead)),
1253
+ Effect.as(statement),
1254
+ );
1255
+ }
1256
+ return Effect.succeed(statement);
1257
+ };
1258
+ const subscription = Effect.gen(function* () {
1259
+ const stream =
1260
+ scope === "thread"
1261
+ ? yield* subscribeOrchestrationV2Thread({ threadId, afterSequence: first!.sequence })
1262
+ : yield* subscribeOrchestrationV2Shell({ afterSequence: first!.sequence });
1263
+ return stream;
1264
+ }).pipe(
1265
+ Effect.provideService(Statement.CurrentTransformer, gateReplayRead),
1266
+ Effect.provideService(ThreadManagement.ThreadManagementService, {
1267
+ ensureLegacyTranscript: () => Effect.void,
1268
+ getThreadSnapshot: () => Effect.die("snapshot"),
1269
+ listActiveShellIndex: () => Effect.die("snapshot"),
1270
+ readShellSnapshot: () => Effect.die("snapshot"),
1271
+ listActiveSubagentThreadIds: () => Effect.succeed([]),
1272
+ } as unknown as ThreadManagement.ThreadManagementService["Service"]),
1273
+ Effect.provideService(ProjectStore.ProjectStoreV2, {
1274
+ listShells: () => Effect.succeed([]),
1275
+ } as unknown as ProjectStore.ProjectStoreV2["Service"]),
1276
+ Effect.provideService(
1277
+ ProjectService.ProjectService,
1278
+ {} as ProjectService.ProjectService["Service"],
1279
+ ),
1280
+ Effect.provideService(ProjectEnrichment.ProjectEnrichmentService, {
1281
+ subscribeChanges: PubSub.subscribe(yield* PubSub.unbounded()),
1282
+ } as ProjectEnrichment.ProjectEnrichmentService["Service"]),
1283
+ Effect.exit,
1284
+ Effect.forkChild,
1285
+ );
1286
+ const fiber = yield* subscription;
1287
+ yield* Deferred.await(readStarted);
1288
+ // A pass publishes its boundary, then deletes the rows it covers.
1289
+ yield* sql`
1290
+ INSERT INTO orchestration_v2_projection_metadata (
1291
+ projection_name, schema_version, last_sequence, updated_at
1292
+ ) VALUES (${OrchestrationEventStore.EVENT_LOG_PRUNED_THROUGH}, 2, ${second!.sequence}, ${now})
1293
+ `;
1294
+ yield* sql`DELETE FROM orchestration_events WHERE sequence = ${second!.sequence}`;
1295
+ yield* Deferred.succeed(releaseRead, undefined);
1296
+ const exit = yield* Fiber.join(fiber);
1297
+ assert.deepEqual(Exit.isFailure(exit) ? Cause.squash(exit.cause) : exit, "snapshot");
1298
+ }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
1299
+);
1300
+
1301
+// A thread idle since before the boundary missed nothing; resuming must not resend its body.
1302
+it.effect("thread resume at its own high-water below the pruned boundary skips the snapshot", () =>
1303
Effect.gen(function* () {
1304
const sql = yield* SqlClient.SqlClient;
1171
- const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
1305
const now = "2026-10-13T00:00:00.000Z";
1173
- const threadId = ThreadId.make("thread:compacted");
1174
- const insertMessage = (version: number, id: string, text: string) => sql<{
1175
- sequence: number;
1176
- }>`
1306
+ const threadId = ThreadId.make("thread:idle");
1307
+ const [last] = yield* sql<{ sequence: number }>`
1308
INSERT INTO orchestration_events (
1309
event_id, aggregate_kind, stream_id, stream_version, event_type,
1310
occurred_at, actor_kind, payload_json, metadata_json, application_event_version
1311
) VALUES (
1181
- ${`compacted:${version}`}, 'thread', ${threadId}, ${version}, 'message.updated',
1182
- ${now}, 'server', ${JSON.stringify({
1183
- createdBy: "user",
1184
- creationSource: "web",
1185
- id,
1186
- threadId,
1187
- runId: null,
1188
- nodeId: null,
1189
- role: "user",
1190
- text,
1191
- attachments: [],
1192
- streaming: false,
1193
- createdAt: now,
1194
- updatedAt: now,
1195
- })}, '{}', 2
1312
+ 'idle:0', 'thread', ${threadId}, 0, 'provider-session.detached',
1313
+ ${now}, 'server', ${JSON.stringify({ providerSessionId: "session:idle", detachedAt: now })},
1314
+ '{}', 2
1315
) RETURNING sequence
1316
`;
1198
- const [first] = yield* insertMessage(0, "message:a", "a created");
1199
- const [second] = yield* insertMessage(1, "message:b", "b created");
1200
- yield* insertMessage(2, "message:a", "a superseded");
1201
- const [head] = yield* insertMessage(3, "message:a", "a final");
1202
- // thread.deleted is never superseded: clients switch on the event type.
1203
- for (const version of [0, 1]) {
1204
- yield* sql`
1205
- INSERT INTO orchestration_events (
1206
- event_id, aggregate_kind, stream_id, stream_version, event_type,
1207
- occurred_at, actor_kind, payload_json, metadata_json, application_event_version
1208
- ) VALUES (
1209
- ${`deleted:${version}`}, 'thread', 'thread:deleted-twice', ${version}, 'thread.deleted',
1210
- ${now}, 'server', '{}', '{}', 2
1211
- )
1212
- `;
1213
- }
1214
- assert.equal((yield* maintenance.compactEventStore()).deletedEventCount, 1);
1215
-
1216
- const items = yield* subscribeOrchestrationV2Thread({
1317
+ yield* sql`
1318
+ INSERT INTO orchestration_v2_projection_metadata (
1319
+ projection_name, schema_version, last_sequence, updated_at
1320
+ ) VALUES (${OrchestrationEventStore.EVENT_LOG_PRUNED_THROUGH}, 2, ${last!.sequence + 10}, ${now})
1321
+ `;
1322
+ const exit = yield* subscribeOrchestrationV2Thread({
1323
threadId,
1218
- afterSequence: first!.sequence - 1,
1219
- requestCompletionMarker: true,
1324
+ afterSequence: last!.sequence,
1325
}).pipe(
1221
- Effect.flatMap((stream) =>
1222
- stream.pipe(
1223
- Stream.takeUntil((item) => item.kind === "synchronized"),
1224
- Stream.runCollect,
1225
- ),
1226
- ),
1326
Effect.provideService(ThreadManagement.ThreadManagementService, {
1327
ensureLegacyTranscript: () => Effect.void,
1229
- streamStoredEventsFrom: () => Stream.never,
1328
+ getThreadSnapshot: () => Effect.die("snapshot"),
1329
+ streamStoredEventsFrom: () => Stream.empty,
1330
} as unknown as ThreadManagement.ThreadManagementService["Service"]),
1331
+ Effect.exit,
1332
);
1232
- assert.deepEqual(
1233
- Array.from(items, (item) =>
1234
- item.kind === "event"
1235
- ? [item.sequence, item.event.type === "message.updated" ? item.event.payload.text : null]
1236
- : [item.kind],
1237
- ),
1238
- [
1239
- [first!.sequence, "a created"],
1240
- [second!.sequence, "b created"],
1241
- [head!.sequence, "a final"],
1242
- ["synchronized"],
1243
- ],
1244
- );
1245
- }).pipe(
1246
- Effect.provide(
1247
- ProjectionMaintenance.layer.pipe(
1248
- Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
1249
- Layer.provideMerge(ReplayTestLayer),
1250
- ),
1251
- ),
1252
- ),
1333
+ // A snapshot load dies with "snapshot", so success means the resume replayed nothing.
1334
+ assert.isTrue(Exit.isSuccess(exit));
1335
+ }).pipe(Effect.scoped, Effect.provide(ReplayTestLayer)),
1336
);
apps/server/src/ws.ts
+56
-18
@@ -780,6 +780,7 @@ const canReplayPersistedRange = Effect.fnUntraced(function* (
780
if (replayGap < 0 || replayGap > maxGap) {
781
return false;
782
}
783
+ if (afterSequence < (yield* applicationEvents.prunedThroughSequence)) return false;
784
const stats = yield* applicationEvents.getReplayStats({
785
afterSequence,
786
throughSequence: headSequence,
@@ -968,7 +969,21 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
969
}),
970
),
971
);
971
- if (input.afterSequence > highWater) {
972
+ const prunedThrough = yield* applicationEvents.prunedThroughSequence.pipe(
973
+ Effect.mapError(
974
+ (cause) =>
975
+ new OrchestrationV2GetThreadProjectionError({
976
+ threadId: input.threadId,
977
+ message: `Failed to prepare orchestration V2 thread ${input.threadId} replay`,
978
+ cause,
979
+ }),
980
+ ),
981
+ );
982
+ // An idle thread's cursor sits at its own high-water: nothing was missed, so replay nothing.
983
+ if (
984
+ input.afterSequence > highWater ||
985
+ (input.afterSequence < highWater && input.afterSequence < prunedThrough)
986
+ ) {
987
return yield* snapshotThenLive();
988
}
989
const stats = yield* applicationEvents
@@ -1009,9 +1024,25 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
1024
// Retained creation events may describe deleted threads; replace replay only if a snapshot exists.
1025
if (shell !== null) return yield* snapshotThenLive();
1026
}
1012
- const replay = coalesceLiveUpdatedEvents(
1013
- yield* loadReplayThrough(input.afterSequence, highWater),
1014
- );
1027
+ const replayed = yield* loadReplayThrough(input.afterSequence, highWater);
1028
+ // Pruning publishes its boundary before deleting, so a pass during the read shows up here.
1029
+ if (
1030
+ input.afterSequence < highWater &&
1031
+ input.afterSequence <
1032
+ (yield* applicationEvents.prunedThroughSequence.pipe(
1033
+ Effect.mapError(
1034
+ (cause) =>
1035
+ new OrchestrationV2GetThreadProjectionError({
1036
+ threadId: input.threadId,
1037
+ message: `Failed to prepare orchestration V2 thread ${input.threadId} replay`,
1038
+ cause,
1039
+ }),
1040
+ ),
1041
+ ))
1042
+ ) {
1043
+ return yield* snapshotThenLive();
1044
+ }
1045
+ const replay = coalesceLiveUpdatedEvents(replayed);
1046
const plan = decideThreadResume({
1047
afterSequence: input.afterSequence,
1048
highWater,
@@ -1245,31 +1276,38 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
1276
});
1277
}
1278
1248
- const highWater = yield* applicationEvents.latestApplicationSequence;
1249
- if (!(yield* canReplayPersistedRange(input.afterSequence, highWater, SHELL_RESUME_MAX_GAP))) {
1279
+ const fromSnapshot = Effect.gen(function* () {
1280
const loaded = yield* loadSnapshot();
1281
return composeShellStreamWithEnrichment({
1282
initial: initialSnapshotItems(loaded),
1283
tail: completionThenLive(loaded.snapshot.snapshotSequence),
1284
enrichment: enrichmentRefreshes,
1285
});
1286
+ });
1287
+ const highWater = yield* applicationEvents.latestApplicationSequence;
1288
+ if (!(yield* canReplayPersistedRange(input.afterSequence, highWater, SHELL_RESUME_MAX_GAP))) {
1289
+ return yield* fromSnapshot;
1290
}
1291
1292
subagentThreadIds = new Set(yield* threadManagement.listActiveSubagentThreadIds());
1293
1294
const loaded = yield* loadProjectMetadataSnapshot(highWater);
1261
- const replay = toShellStream(
1262
- applicationEvents
1263
- .readApplicationEvents({
1264
- afterSequence: input.afterSequence,
1265
- throughSequence: highWater,
1266
- // A row a newer build wrote (e.g. after a downgrade) must not fail
1267
- // every shell resubscribe. The live tail below resumes from
1268
- // highWater, so a dropped row never hides later events.
1269
- skipUnknownEventTypes: true,
1270
- })
1271
- .pipe(Stream.filter(filterSubagentTranscript)),
1272
- );
1295
+ // Read before replying: the range is budgeted above, and a prune pass during the read
1296
+ // publishes its boundary first, so the recheck below catches deleted rows.
1297
+ const replayed = yield* applicationEvents
1298
+ .readApplicationEvents({
1299
+ afterSequence: input.afterSequence,
1300
+ throughSequence: highWater,
1301
+ // A row a newer build wrote (e.g. after a downgrade) must not fail
1302
+ // every shell resubscribe. The live tail below resumes from
1303
+ // highWater, so a dropped row never hides later events.
1304
+ skipUnknownEventTypes: true,
1305
+ })
1306
+ .pipe(Stream.filter(filterSubagentTranscript), Stream.runCollect);
1307
+ if (input.afterSequence < (yield* applicationEvents.prunedThroughSequence)) {
1308
+ return yield* fromSnapshot;
1309
+ }
1310
+ const replay = toShellStream(Stream.fromIterable(replayed));
1311
return composeShellStreamWithEnrichment({
1312
initial: initialEnrichmentItems(loaded),
1313
tail: Stream.concat(Stream.concat(replay, completionMarker), liveFrom(highWater)),
docs/internals/overview.md
+17
-3
@@ -44,8 +44,8 @@ select known environment-local checkouts; the group itself does not store inheri
44
45
## Durable intent and side effects
46
47
-The event log is the source of truth for orchestration state. The
48
-[v2 orchestrator](../../apps/server/src/orchestration-v2/Orchestrator.ts) serializes commands and
47
+Orchestration state is decided as events, and projections are the source of truth once they apply
48
+them. The [v2 orchestrator](../../apps/server/src/orchestration-v2/Orchestrator.ts) serializes commands and
49
decides events without performing provider or filesystem work.
50
[EventSink](../../apps/server/src/orchestration-v2/EventSink.ts) commits events, persisted projections,
51
the accepted command receipt, and outbox effects in one database transaction. Subscribers receive
@@ -58,7 +58,21 @@ therefore means the intent committed, not that the provider, checkpoint, or othe
58
finished. Keep external I/O out of command decisions and the database transaction. Effects tied to
59
a lost provider process cannot simply replay; recovery retires them before admitting new work.
60
61
-Persisted events must remain decodable on replay. Changing a schema affects old environments at
61
+The event log only keeps what live readers need: about a day of history for reconnect replay and
62
+command-id retries.
63
+[ProjectionMaintenance](../../apps/server/src/orchestration-v2/ProjectionMaintenance.ts) deletes
64
+older events the projections cover, so projections cannot be rebuilt from the log. It keeps each
65
+stream's newest event (high-water marks, stream versions, snapshot sequences), `thread.created`
66
+(the legacy importer's marker), every `running` run update (Codex usage history reads the first
67
+of each attempt), and prompt anchors that retained subagent rows reference. A client whose cursor
68
+is below both its thread's newest event and the recorded pruned-through sequence missed pruned
69
+events, so it resumes from a snapshot; an idle thread's cursor resumes without one. Resumes
70
+recheck that sequence after reading the replay, since a pass can prune during the read. V2 command
71
+receipts stay for idempotency: a retry older than the retained history is still deduplicated,
72
+but its replay comes back without the pruned events. Receipts of imported legacy commands are
73
+removed once their threads are imported.
74
+
75
+Retained events must remain decodable on replay. Changing a schema affects old environments at
76
startup as well as live RPC traffic. Compatibility work must account for stored history, not just
77
what the newest client sends.
78
docs/user/background-service.md
+2
-2
@@ -161,8 +161,8 @@ For failures after signing in to T3 Connect, see
161
162
## Reclaim database space
163
164
-T3 Code removes superseded internal history on its own while it runs; your
165
-conversations are kept. Databases created before this shrank automatically
164
+T3 Code removes internal history older than a day on its own while it runs;
165
+your conversations are kept. Databases created before this shrank automatically
166
still hold the freed space on disk until a one-time offline repair returns it
167
and turns on automatic shrinking. Settings > Maintenance (web and desktop) or
168
Settings > Maintenance (mobile) suggests the repair when at least 1 GiB can be