@devries-cameron20 / t3code Mirror · read-only / commits / d5b921ce4b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 17:00 UTC

fix(server): keep history discovery off the writer and pace compaction (#20877)

Co-authored-by: GPT-6.1 Sol <noreply@openai.com> Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Yash Singh <saiansh2525@gmail.com>

Yannic Charlon committed Oct 26, 2026 at 06:40 UTC d5b921ce4bf0d687e5cedbf956839dd5ebabeaa2
8 files changed +338 -38
apps/server/src/orchestration-v2/FoundationPersistence.test.ts
+212 -16
@@ -1,6 +1,5 @@
1 +import * as NodeSqlite from "node:sqlite";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
2 -import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 -import * as ServerSettings from "../serverSettings.ts";
3 import { assert, it } from "@effect/vitest";
4 import {
5 CheckpointId,
@@ -27,6 +26,8 @@ import {
26 ThreadId,
27 TurnItemId,
28 } from "@t3tools/contracts";
29 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
30 +import * as Clock from "effect/Clock";
31 import * as DateTime from "effect/DateTime";
32 import * as Deferred from "effect/Deferred";
33 import * as Duration from "effect/Duration";
@@ -40,10 +41,16 @@ import * as Ref from "effect/Ref";
41 import * as Scheduler from "effect/Scheduler";
42 import * as Schema from "effect/Schema";
43 import * as Stream from "effect/Stream";
44 +import * as TestClock from "effect/testing/TestClock";
45 import * as Tracer from "effect/Tracer";
46 import * as SqlClient from "effect/sql/SqlClient";
47 import * as Statement from "effect/sql/Statement";
48
49 +import { vi } from "vite-plus/test";
50 +
51 +import * as ServerConfig from "../config.ts";
52 +import * as DatabaseStorage from "../persistence/DatabaseStorage.ts";
53 +import * as ServerSettings from "../serverSettings.ts";
54 import { LIVE_STREAM_MAX_ITEMS, LiveStreamBufferError } from "./LiveStreamBudget.ts";
55 import * as SqlitePersistence from "../persistence/Sqlite.ts";
56 import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
@@ -219,6 +226,173 @@ it.effect("rebuilds event history one bounded page at a time", () =>
226 }).pipe(Effect.provide(layerTest)),
227 );
228
229 +const seedCompaction = Effect.fnUntraced(function* () {
230 + const sql = yield* SqlClient.SqlClient;
231 + yield* sql`INSERT INTO orchestration_v2_legacy_imports (
232 + thread_id, source_updated_at, shell_imported_at, transcript_imported_at, imported_message_count
233 + ) VALUES ('imported', '2026-10-17', '2026-10-17', '2026-10-17', 0)`;
234 + yield* sql`WITH RECURSIVE history(n) AS (
235 + SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 151
236 + ) INSERT INTO orchestration_events (
237 + event_id, aggregate_kind, stream_id, stream_version, event_type,
238 + occurred_at, actor_kind, payload_json, metadata_json, application_event_version
239 + ) SELECT 'event-' || n, 'thread', 'thread', n,
240 + CASE WHEN n IN (1, 51, 101) THEN 'message.updated' ELSE 'thread.visited' END,
241 + '2026-10-17', 'server', '{"id":"message"}', '{}', 2 FROM history`;
242 + yield* sql`WITH RECURSIVE history(n) AS (
243 + SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 100
244 + ) INSERT INTO orchestration_command_receipts (
245 + rowid, command_id, aggregate_kind, aggregate_id, accepted_at, result_sequence, status, command_type
246 + ) SELECT n * 2, 'command-' || n, 'thread', 'imported', '2026-10-17', 0, 'accepted', 'legacy' FROM history`;
247 +});
248 +
249 +it.effect("discovers on the reader worker and deletes on the writer", () =>
250 + Effect.gen(function* () {
251 + const fs = yield* FileSystem.FileSystem;
252 + const path = yield* Path.Path;
253 + const filename = path.join(yield* fs.makeTempDirectoryScoped(), "fixture.sqlite");
254 + yield* Effect.gen(function* () {
255 + const sql = yield* SqlClient.SqlClient;
256 + yield* seedCompaction();
257 + // Establish the reader before recording main-thread statements.
258 + yield* sql`SELECT 1`.pipe(NodeSqliteClient.readOnly);
259 + const reads: string[] = [];
260 + const writes: string[] = [];
261 + const all = NodeSqlite.StatementSync.prototype.all;
262 + const run = NodeSqlite.StatementSync.prototype.run;
263 + yield* Effect.acquireRelease(
264 + Effect.sync(() => {
265 + const readSpy = vi
266 + .spyOn(NodeSqlite.StatementSync.prototype, "all")
267 + .mockImplementation(function (this: NodeSqlite.StatementSync, ...params) {
268 + reads.push(this.sourceSQL);
269 + return all.apply(this, params);
270 + });
271 + const writeSpy = vi
272 + .spyOn(NodeSqlite.StatementSync.prototype, "run")
273 + .mockImplementation(function (this: NodeSqlite.StatementSync, ...params) {
274 + writes.push(this.sourceSQL);
275 + return run.apply(this, params);
276 + });
277 + return { readSpy, writeSpy };
278 + }),
279 + ({ readSpy, writeSpy }) =>
280 + Effect.sync(() => {
281 + readSpy.mockRestore();
282 + writeSpy.mockRestore();
283 + }),
284 + );
285 + const maintenance = yield* ProjectionMaintenance.ProjectionMaintenanceV2;
286 + const result = yield* maintenance.compactEventStore();
287 + assert.equal(result.deletedEventCount, 148);
288 + assert.equal(result.deletedReceiptCount, 100);
289 + assert.deepEqual(
290 + reads.filter((query) => /\bSELECT\b/.test(query)),
291 + [],
292 + );
293 + assert(writes.some((query) => query.includes("DELETE FROM orchestration_events")));
294 + assert(writes.some((query) => query.includes("DELETE FROM orchestration_command_receipts")));
295 + }).pipe(
296 + Effect.provide(
297 + ProjectionMaintenance.layer.pipe(
298 + Layer.provide(Layer.merge(EventStore.layer, ProjectionStore.layer)),
299 + Layer.provideMerge(SqlitePersistence.layerFromPath(filename)),
300 + ),
301 + ),
302 + );
303 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
304 +);
305 +
306 +it.effect("paces scheduled compaction while preserving retention state and fixed scan bounds", () =>
307 + Effect.gen(function* () {
308 + const sql = yield* SqlClient.SqlClient;
309 + const clock = yield* Clock.Clock;
310 + yield* seedCompaction();
311 + const completed = yield* Deferred.make<void>();
312 + const pauseStarted = yield* Deferred.make<void>();
313 + let activeNanos = 0n;
314 + let eventPages = 0;
315 + const pauses: number[] = [];
316 + const compactionSpans: Tracer.Span[] = [];
317 + const tracer = Tracer.make({
318 + span(options) {
319 + const span = new Tracer.NativeSpan(options);
320 + if (span.name === "ProjectionMaintenance.compactEventStore") compactionSpans.push(span);
321 + const end = span.end.bind(span);
322 + span.end = (time, exit) => {
323 + end(time, exit);
324 + const query = span.attributes.get("db.query.text");
325 + if (typeof query !== "string" || !query.includes("LIMIT")) return;
326 + if (query.includes("FROM orchestration_events AS event")) {
327 + eventPages++;
328 + } else if (!query.includes("FROM orchestration_command_receipts AS receipt")) return;
329 + // Model a page exceeding the active budget, without wall-clock sleeps.
330 + activeNanos += 30_000_000n;
331 + };
332 + return span;
333 + },
334 + });
335 + yield* Effect.gen(function* () {
336 + yield* DatabaseStorage.DatabaseStorage;
337 + yield* TestClock.adjust("1 minute");
338 + yield* Effect.raceFirst(Deferred.await(pauseStarted), Deferred.await(completed));
339 + yield* TestClock.adjust("269 millis");
340 + assert.equal(eventPages, 1);
341 + assert.deepEqual(pauses, [270]);
342 + assert.deepEqual(
343 + compactionSpans.map((span) => span.status._tag),
344 + ["Started"],
345 + );
346 + // Foreground commits must not extend the compactor's initial scan bounds.
347 + yield* sql`INSERT INTO orchestration_events (
348 + event_id, aggregate_kind, stream_id, stream_version, event_type,
349 + occurred_at, actor_kind, payload_json, metadata_json, application_event_version
350 + ) VALUES ('after-start', 'thread', 'thread', 152, 'thread.visited', '2026-10-17', 'server', '{}', '{}', 2)`;
351 + yield* sql`INSERT INTO orchestration_command_receipts (
352 + command_id, aggregate_kind, aggregate_id, accepted_at, result_sequence, status, command_type
353 + ) VALUES ('after-start', 'thread', 'imported', '2026-10-17', 0, 'accepted', 'legacy')`;
354 + yield* TestClock.adjust("10 seconds");
355 + yield* Deferred.await(completed);
356 + }).pipe(
357 + Effect.provide(
358 + DatabaseStorage.layer.pipe(
359 + Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "compaction-" })),
360 + Layer.provide(NodeServices.layer),
361 + ),
362 + ),
363 + Effect.withTracer(tracer),
364 + Effect.provideService(Clock.Clock, {
365 + ...clock,
366 + monotonicTimeNanos: Effect.sync(() => activeNanos),
367 + sleep: (duration) => {
368 + const millis = Duration.toMillis(duration);
369 + if (millis < 60_000) {
370 + pauses.push(millis);
371 + Deferred.doneUnsafe(pauseStarted, Effect.void);
372 + } else if (millis === 6 * 60 * 60_000) {
373 + Deferred.doneUnsafe(completed, Effect.void);
374 + }
375 + return clock.sleep(duration);
376 + },
377 + }),
378 + );
379 + assert.deepEqual(pauses, Array(5).fill(270));
380 + assert.deepEqual(
381 + compactionSpans.map((span) => span.status._tag),
382 + ["Ended"],
383 + );
384 + assert.deepEqual(yield* sql`SELECT sequence FROM orchestration_events ORDER BY sequence`, [
385 + { sequence: 1 },
386 + { sequence: 101 },
387 + { sequence: 151 },
388 + { sequence: 152 },
389 + ]);
390 + assert.deepEqual(yield* sql`SELECT command_id FROM orchestration_command_receipts`, [
391 + { command_id: "after-start" },
392 + ]);
393 + }).pipe(Effect.scoped, Effect.provide(layerTest)),
394 +);
395 +
396 it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
397 it.effect("projects oversized tool bodies before both replay and live RPC retention", () =>
398 Effect.scoped(
@@ -803,7 +977,7 @@ it.layer(layerTest)("orchestration V2 foundation persistence", (it) => {
977 ON CONFLICT(command_id) DO NOTHING
978 `;
979
806 - const summary = yield* maintenance.compactEventStore;
980 + const summary = yield* maintenance.compactEventStore();
981 assert.isAtLeast(summary.deletedEventCount, 504);
982 assert.isAtLeast(summary.deletedReceiptCount, 1);
983
@@ -3173,34 +3347,56 @@ it.effect.each(["events", "receipts"] as const)(
3347 yield* sql`CREATE TABLE concurrent_writes (value TEXT)`;
3348 const readerHeld = yield* Deferred.make<void>();
3349 const releaseReader = yield* Deferred.make<void>();
3176 - const reader = yield* sql
3177 - .withTransaction(
3178 - Deferred.succeed(readerHeld, undefined).pipe(
3179 - Effect.andThen(Deferred.await(releaseReader)),
3180 - ),
3181 - )
3182 - .pipe(NodeSqliteClient.readOnly, Effect.forkChild);
3183 - yield* Deferred.await(readerHeld);
3350 const discoveryStarted = yield* Deferred.make<Tracer.Span>();
3351 const observeDiscovery: Statement.Transformer = (statement, _sql, _fiber, span) => {
3352 const [query] = statement.compile();
3353 const pageQuery =
3354 kind === "events" ? /^\s*SELECT\s+event\.sequence/ : /^\s*SELECT\s+receipt\.rowid/;
3355 return pageQuery.test(query)
3190 - ? Deferred.succeed(discoveryStarted, span).pipe(Effect.as(statement))
3356 + ? Deferred.succeed(discoveryStarted, span).pipe(
3357 + Effect.andThen(Deferred.await(readerHeld)),
3358 + Effect.as(statement),
3359 + )
3360 : Effect.succeed(statement);
3361 };
3362 const compacted = yield* Deferred.make<void>();
3194 - const compaction = yield* maintenance.compactEventStore.pipe(
3363 + const compaction = yield* maintenance.compactEventStore().pipe(
3364 Effect.provideService(Statement.CurrentTransformer, observeDiscovery),
3365 Effect.provideService(Statement.SpanPropagationEnabled, true),
3366 Effect.tap(() => Deferred.succeed(compacted, undefined)),
3367 Effect.forkChild,
3368 );
3369 const discoverySpan = yield* Deferred.await(discoveryStarted);
3370 + // Let the initial bounds read finish before holding the reader for this page.
3371 + const reader = yield* sql
3372 + .withTransaction(
3373 + Deferred.succeed(readerHeld, undefined).pipe(
3374 + Effect.andThen(Deferred.await(releaseReader)),
3375 + ),
3376 + )
3377 + .pipe(NodeSqliteClient.readOnly, Effect.forkChild);
3378 + yield* Deferred.await(readerHeld);
3379 yield* sql`INSERT INTO concurrent_writes VALUES ('committed')`;
3380 assert.isFalse(yield* Deferred.isDone(compacted));
3381 assert.deepEqual(yield* sql`SELECT value FROM concurrent_writes`, [{ value: "committed" }]);
3382 + if (kind === "events") {
3383 + yield* sql`
3384 + INSERT INTO orchestration_events (
3385 + event_id, aggregate_kind, stream_id, stream_version, event_type,
3386 + occurred_at, actor_kind, payload_json, metadata_json, application_event_version
3387 + ) VALUES (
3388 + 'after-bound', 'thread', 'imported', 2, 'thread.message-appended',
3389 + ${now}, 'user', '{}', '{}', 1
3390 + )
3391 + `;
3392 + } else {
3393 + yield* sql`
3394 + INSERT INTO orchestration_command_receipts (
3395 + command_id, aggregate_kind, aggregate_id, accepted_at, result_sequence,
3396 + status, command_type
3397 + ) VALUES ('after-bound', 'thread', 'imported', ${now}, 1, 'accepted', 'legacy')
3398 + `;
3399 + }
3400 yield* Deferred.succeed(releaseReader, undefined);
3401 yield* Fiber.join(reader);
3402 const result = yield* Fiber.join(compaction);
@@ -3209,9 +3405,9 @@ it.effect.each(["events", "receipts"] as const)(
3405 assert.equal(result.deletedReceiptCount, kind === "receipts" ? 1 : 0);
3406 const retained =
3407 kind === "events"
3212 - ? yield* sql`SELECT stream_id AS id FROM orchestration_events`
3213 - : yield* sql`SELECT aggregate_id AS id FROM orchestration_command_receipts`;
3214 - assert.deepEqual(retained, [{ id: "pending" }]);
3408 + ? yield* sql`SELECT event_id AS id FROM orchestration_events ORDER BY sequence`
3409 + : yield* sql`SELECT command_id AS id FROM orchestration_command_receipts ORDER BY rowid`;
3410 + assert.deepEqual(retained, [{ id: "pending" }, { id: "after-bound" }]);
3411 }).pipe(Effect.provide(maintenanceLayer));
3412 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
3413 );
apps/server/src/orchestration-v2/ProjectionMaintenance.ts
+22 -15
@@ -32,7 +32,7 @@ export class ProjectionMaintenanceError extends Schema.TaggedError<ProjectionMai
32 export interface ProjectionMaintenanceV2Shape {
33 readonly verify: Effect.Effect<ProjectionVerificationV2, ProjectionMaintenanceError>;
34 readonly rebuild: Effect.Effect<ProjectionVerificationV2, ProjectionMaintenanceError>;
35 - readonly compactEventStore: Effect.Effect<
35 + readonly compactEventStore: (betweenPages?: Effect.Effect<void>) => Effect.Effect<
36 {
37 readonly deletedEventCount: number;
38 readonly deletedReceiptCount: number;
@@ -223,12 +223,15 @@ export const layer: Layer.Layer<
223 const supersedableThreadEventTypes = new Set(SUPERSEDABLE_THREAD_EVENT_TYPES);
224 const readStateThreadEventTypes = new Set(READ_STATE_THREAD_EVENT_TYPES);
225 // Keep subagent.updated and turn-item.updated rows: slimmed payloads reference their prompts.
226 - // Small pages bound work on the reader worker and synchronous fallback.
226 + // Bound each reader request and writer delete, including when the reader
227 + // worker is unavailable and discovery falls back to the writer.
228 const COMPACTION_PAGE_SIZE = 50;
229
230 /** Retain newest and oldest keyed events to preserve client insertion order; page all events to bound sparse scans.
231 * Keep turn-item.updated because replay assigns positions on first write. */
231 - const compactEventStore = Effect.gen(function* () {
232 + const compactEventStore = Effect.fn("ProjectionMaintenance.compactEventStore")(function* (
233 + betweenPages: Effect.Effect<void> = Effect.void,
234 + ) {
235 const bounds = yield* sql<{
236 readonly event_sequence: number;
237 readonly receipt_row_id: number;
@@ -236,7 +239,7 @@ export const layer: Layer.Layer<
239 SELECT
240 COALESCE((SELECT MAX(sequence) FROM orchestration_events), 0) AS event_sequence,
241 COALESCE((SELECT MAX(rowid) FROM orchestration_command_receipts), 0) AS receipt_row_id
239 - `;
242 + `.pipe(NodeSqliteClient.readOnly);
243 let throughSequence = bounds[0]?.event_sequence ?? 0;
244 let throughReceiptRowId = bounds[0]?.receipt_row_id ?? 0;
245 const retainedThreadIds = new Set<string>();
@@ -285,10 +288,11 @@ export const layer: Layer.Layer<
288 FROM orchestration_events AS event
289 WHERE event.sequence <= ${throughSequence}
290 ORDER BY event.sequence DESC
288 - LIMIT ${COMPACTION_PAGE_SIZE}
291 + LIMIT ${COMPACTION_PAGE_SIZE + 1}
292 `.pipe(NodeSqliteClient.readOnly);
293 + const page = rows.slice(0, COMPACTION_PAGE_SIZE);
294 const obsolete: number[] = [];
291 - for (const row of rows) {
295 + for (const row of page) {
296 if (row.imported_legacy_thread === 1) {
297 obsolete.push(row.sequence);
298 } else if (row.application_event_version === 2) {
@@ -329,15 +333,16 @@ export const layer: Layer.Layer<
333 }
334 }
335 }
332 - // Yield even without deletions for clients using the synchronous fallback.
336 + // Never hold a transaction or the reader lock across a yield or a rest.
337 yield* Effect.yieldNow;
338 if (obsolete.length > 0) {
339 yield* sql`DELETE FROM orchestration_events WHERE sequence IN ${sql.in(obsolete)}`;
340 deletedEventCount += obsolete.length;
341 yield* Effect.yieldNow;
342 }
339 - throughSequence = (rows.at(-1)?.sequence ?? 1) - 1;
340 - if (rows.length < COMPACTION_PAGE_SIZE) break;
343 + throughSequence =
344 + rows.length <= COMPACTION_PAGE_SIZE ? 0 : (page.at(-1)?.sequence ?? 1) - 1;
345 + if (throughSequence > 0 || throughReceiptRowId > 0) yield* betweenPages;
346 }
347
348 while (throughReceiptRowId > 0) {
@@ -361,9 +366,10 @@ export const layer: Layer.Layer<
366 FROM orchestration_command_receipts AS receipt
367 WHERE receipt.rowid <= ${throughReceiptRowId}
368 ORDER BY receipt.rowid DESC
364 - LIMIT ${COMPACTION_PAGE_SIZE}
369 + LIMIT ${COMPACTION_PAGE_SIZE + 1}
370 `.pipe(NodeSqliteClient.readOnly);
366 - const obsolete = rows
371 + const page = rows.slice(0, COMPACTION_PAGE_SIZE);
372 + const obsolete = page
373 .filter((row) => row.imported_legacy_thread === 1)
374 .map((row) => row.command_id);
375 yield* Effect.yieldNow;
@@ -372,8 +378,9 @@ export const layer: Layer.Layer<
378 deletedReceiptCount += obsolete.length;
379 yield* Effect.yieldNow;
380 }
375 - throughReceiptRowId = (rows.at(-1)?.row_id ?? 1) - 1;
376 - if (rows.length < COMPACTION_PAGE_SIZE) break;
381 + throughReceiptRowId = (page.at(-1)?.row_id ?? 1) - 1;
382 + if (rows.length <= COMPACTION_PAGE_SIZE) break;
383 + yield* betweenPages;
384 }
385
386 const freelistRows = yield* sql<{ readonly freelist_count: number }>`PRAGMA freelist_count`;
@@ -382,12 +389,12 @@ export const layer: Layer.Layer<
389 (freelistRows[0]?.freelist_count ?? 0) * (pageSizeRows[0]?.page_size ?? 0);
390
391 return { deletedEventCount, deletedReceiptCount, reclaimableBytes };
385 - });
392 + }, mapError("compact event store"));
393
394 return ProjectionMaintenanceV2.of({
395 verify: mapError("verify")(verify),
396 rebuild: mapError("rebuild")(rebuild),
390 - compactEventStore: mapError("compact event store")(compactEventStore),
397 + compactEventStore,
398 });
399 }),
400 );
apps/server/src/orchestration-v2/runtimeLayer.test.ts
+3 -3
@@ -5889,7 +5889,7 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events",
5889 });
5890 }
5891 const latest = yield* orchestrator.getThreadShell(threadId);
5892 - yield* maintenance.compactEventStore;
5892 + yield* maintenance.compactEventStore();
5893 const linkEvents = yield* sql<{ command_id: string }>`
5894 SELECT command_id FROM orchestration_events
5895 WHERE stream_id = ${threadId} AND event_type = 'thread.pull-request-link-updated'
@@ -5929,7 +5929,7 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events",
5929 visitedAt,
5930 });
5931 }
5932 - yield* maintenance.compactEventStore;
5932 + yield* maintenance.compactEventStore();
5933 assert.deepEqual(
5934 (yield* threadEvents()).map((row) => row.event_type),
5935 ["thread.metadata-updated", "thread.visited"],
@@ -5940,7 +5940,7 @@ it.layer(Layer.fresh(layerSharedApplicationDataPlaneTest))("compact PR events",
5940 threadId,
5941 title: "Renamed after visit",
5942 });
5943 - yield* maintenance.compactEventStore;
5943 + yield* maintenance.compactEventStore();
5944 assert.deepEqual(
5945 (yield* threadEvents()).map((row) => row.event_type),
5946 ["thread.metadata-updated"],
apps/server/src/persistence/DatabaseMaintenance.test.ts
+26 -1
@@ -1,6 +1,8 @@
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, it } from "@effect/vitest";
3 import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
4 +import * as Clock from "effect/Clock";
5 +import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import * as FileSystem from "effect/FileSystem";
8 import * as Layer from "effect/Layer";
@@ -111,6 +113,14 @@ it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))
113 VALUES ('delete-all')
114 `;
115 yield* sql`PRAGMA wal_checkpoint(TRUNCATE)`;
116 + yield* sql`INSERT INTO orchestration_v2_legacy_imports (
117 + thread_id, source_updated_at, shell_imported_at, transcript_imported_at, imported_message_count
118 + ) VALUES ('imported', '2026-10-17', '2026-10-17', '2026-10-17', 0)`;
119 + yield* sql`WITH RECURSIVE history(n) AS (
120 + SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 51
121 + ) INSERT INTO orchestration_command_receipts (
122 + command_id, aggregate_kind, aggregate_id, accepted_at, result_sequence, status, command_type
123 + ) SELECT 'legacy-' || n, 'thread', 'imported', '2026-10-17', 0, 'accepted', 'legacy' FROM history`;
124 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: dbPath })));
125
126 const sizeBefore = (yield* fs.stat(dbPath)).size;
@@ -122,7 +132,22 @@ it.layer(DatabaseMaintenance.layer.pipe(Layer.provideMerge(NodeServices.layer)))
132 assert.equal(refused._tag, "DatabaseInUseError");
133 assert.equal((yield* fs.stat(dbPath)).size, sizeBefore);
134
125 - const result = yield* maintenance.compact(dbPath);
135 + const clock = yield* Clock.Clock;
136 + const pauses: number[] = [];
137 + let activeNanos = 0n;
138 + const result = yield* maintenance.compact(dbPath).pipe(
139 + Effect.provideService(Clock.Clock, {
140 + ...clock,
141 + // Exceed the background budget whenever the compactor checks elapsed time.
142 + monotonicTimeNanos: Effect.sync(() => (activeNanos += 30_000_000n)),
143 + sleep: (duration) =>
144 + Effect.sync(() => {
145 + pauses.push(Duration.toMillis(duration));
146 + }),
147 + }),
148 + );
149 + assert.deepEqual(pauses, [], "offline doctor must not throttle compaction");
150 + assert.equal(result.deletedReceiptCount, 51);
151 assert.isBelow(result.afterBytes, result.beforeBytes - 3 * 1024 * 1024);
152 yield* Effect.gen(function* () {
153 const sql = yield* SqlClient.SqlClient;
apps/server/src/persistence/DatabaseMaintenance.ts
+1 -1
@@ -182,7 +182,7 @@ const make = Effect.gen(function* () {
182 if (!(yield* maintenance.verify).valid) {
183 return yield* new DatabaseProjectionsInvalidError({ path: databasePath });
184 }
185 - const summary = yield* maintenance.compactEventStore;
185 + const summary = yield* maintenance.compactEventStore();
186 if (!(yield* maintenance.verify).valid) {
187 return yield* new DatabaseProjectionsInvalidError({ path: databasePath });
188 }
apps/server/src/persistence/DatabaseStorage.test.ts
+64
@@ -1,9 +1,11 @@
1 import * as NodeServices from "@effect/platform-node/NodeServices";
2 import { assert, it } from "@effect/vitest";
3 +import * as Deferred from "effect/Deferred";
4 import * as Effect from "effect/Effect";
5 import * as FileSystem from "effect/FileSystem";
6 import * as Layer from "effect/Layer";
7 import * as Path from "effect/Path";
8 +import * as TestClock from "effect/testing/TestClock";
9 import * as SqlClient from "effect/sql/SqlClient";
10
11 import * as ServerConfig from "../config.ts";
@@ -50,3 +52,65 @@ it.effect("a new database returns freed pages online without losing data", () =>
52 assert.deepEqual(yield* sql`SELECT value FROM retained`, [{ value: "keep" }]);
53 }).pipe(Effect.provide(storageLayer(testConfig))),
54 );
55 +
56 +const compactionLayer = (
57 + compactEventStore: ProjectionMaintenance.ProjectionMaintenanceV2["Service"]["compactEventStore"],
58 +) =>
59 + DatabaseStorage.layer.pipe(
60 + Layer.provide(
61 + Layer.succeed(ProjectionMaintenance.ProjectionMaintenanceV2, {
62 + compactEventStore,
63 + verify: Effect.die("unexpected verification"),
64 + rebuild: Effect.die("unexpected rebuild"),
65 + }),
66 + ),
67 + Layer.provide(Sqlite.layerMemory),
68 + );
69 +
70 +const minute = 60_000;
71 +const startupTime = Date.UTC(2026, 9, 19);
72 +const emptyCompaction = { deletedEventCount: 0, deletedReceiptCount: 0, reclaimableBytes: 0 };
73 +
74 +it.effect("remembers completion only after the paced pass finishes", () =>
75 + Effect.gen(function* () {
76 + yield* TestClock.setTime(startupTime);
77 + const fs = yield* FileSystem.FileSystem;
78 + const path = yield* Path.Path;
79 + const config = yield* ServerConfig.ServerConfig;
80 + const timestampPath = path.join(path.dirname(config.dbPath), "compacted-at");
81 + const betweenPagesReached = yield* Deferred.make<void>();
82 + const completed = yield* Deferred.make<void>();
83 + const timestampWritten = yield* Deferred.make<void>();
84 + const compact = (betweenPages: Effect.Effect<void> = Effect.void) =>
85 + Effect.gen(function* () {
86 + yield* Effect.sleep(30);
87 + yield* Deferred.succeed(betweenPagesReached, undefined);
88 + yield* betweenPages;
89 + yield* Deferred.succeed(completed, undefined);
90 + return emptyCompaction;
91 + });
92 + yield* Effect.gen(function* () {
93 + yield* TestClock.adjust(minute);
94 + yield* TestClock.adjust(30);
95 + yield* Deferred.await(betweenPagesReached);
96 + assert.isFalse(yield* Deferred.isDone(completed));
97 + assert.isFalse(yield* fs.exists(timestampPath));
98 + yield* TestClock.adjust(269);
99 + assert.isFalse(yield* Deferred.isDone(completed));
100 + assert.isFalse(yield* fs.exists(timestampPath));
101 + yield* TestClock.adjust(1);
102 + yield* Deferred.await(timestampWritten);
103 + assert.isTrue(yield* Deferred.isDone(completed));
104 + assert.equal(Number(yield* fs.readFileString(timestampPath)), startupTime + minute + 300);
105 + }).pipe(
106 + Effect.provide(compactionLayer(compact)),
107 + Effect.provideService(FileSystem.FileSystem, {
108 + ...fs,
109 + writeFileString: (file, content, options) =>
110 + fs
111 + .writeFileString(file, content, options)
112 + .pipe(Effect.ensuring(Deferred.succeed(timestampWritten, undefined))),
113 + }),
114 + );
115 + }).pipe(Effect.provide(testConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
116 +);
apps/server/src/persistence/DatabaseStorage.ts
+9 -1
@@ -88,7 +88,15 @@ const make = Effect.gen(function* () {
88 // so restarting does not trigger another scan before the six-hour interval.
89 const compactedAtPath = path.join(path.dirname(config.dbPath), "compacted-at");
90 const compact = Effect.gen(function* () {
91 - const result = yield* maintenance.compactEventStore;
91 + let sliceStarted = yield* Clock.monotonicTimeNanos;
92 + const betweenPages = Effect.gen(function* () {
93 + const elapsed = (yield* Clock.monotonicTimeNanos) - sliceStarted;
94 + if (elapsed < 25_000_000n) return;
95 + // Keep background maintenance at 10% duty, including pages that exceed the budget.
96 + yield* Effect.sleep((Number(elapsed) / 1_000_000) * 9);
97 + sliceStarted = yield* Clock.monotonicTimeNanos;
98 + });
99 + const result = yield* maintenance.compactEventStore(betweenPages);
100 const completedAt = yield* Clock.currentTimeMillis;
101 yield* fs.writeFileString(compactedAtPath, String(completedAt)).pipe(Effect.ignore);
102 if (result.deletedEventCount > 0 || result.deletedReceiptCount > 0) {
apps/server/src/ws.test.ts
+1 -1
@@ -843,7 +843,7 @@ it.effect("thread resume across compacted history keeps message order and final
843 )
844 `;
845 }
846 - assert.equal((yield* maintenance.compactEventStore).deletedEventCount, 1);
846 + assert.equal((yield* maintenance.compactEventStore()).deletedEventCount, 1);
847
848 const items = yield* subscribeOrchestrationV2Thread({
849 threadId,