The best way to control Claude Code, Codex and more from any computer or phone
perf(server): bootstrap engine from persisted projections (#1648)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Apr 1, 2026 at 15:07 UTC
823d69f6215d80ce4e75ac4fb9a8f763d5d27b87
8 files changed
+141
-17
apps/server/integration/OrchestrationEngineHarness.integration.ts
+6
-3
@@ -289,9 +289,10 @@ export const makeOrchestrationIntegrationHarness = (
289
);
290
291
const checkpointStoreLayer = CheckpointStoreLive.pipe(Layer.provide(GitCoreLive));
292
+ const projectionSnapshotQueryLayer = OrchestrationProjectionSnapshotQueryLive;
293
const runtimeServicesLayer = Layer.mergeAll(
293
- orchestrationLayer,
294
- OrchestrationProjectionSnapshotQueryLive,
294
+ projectionSnapshotQueryLayer,
295
+ orchestrationLayer.pipe(Layer.provide(projectionSnapshotQueryLayer)),
296
ProjectionCheckpointRepositoryLive,
297
ProjectionPendingApprovalRepositoryLive,
298
checkpointStoreLayer,
@@ -333,7 +334,9 @@ export const makeOrchestrationIntegrationHarness = (
334
Layer.provideMerge(providerCommandReactorLayer),
335
Layer.provideMerge(checkpointReactorLayer),
336
);
336
- const layer = orchestrationReactorLayer.pipe(
337
+ const layer = Layer.empty.pipe(
338
+ Layer.provideMerge(runtimeServicesLayer),
339
+ Layer.provideMerge(orchestrationReactorLayer),
340
Layer.provide(persistenceLayer),
341
Layer.provideMerge(ServerSettingsService.layerTest()),
342
Layer.provideMerge(ServerConfig.layerTest(workspaceDir, rootDir)),
apps/server/src/orchestration/Layers/CheckpointReactor.test.ts
+2
@@ -23,6 +23,7 @@ import { GitCoreLive } from "../../git/Layers/GitCore.ts";
23
import { CheckpointReactorLive } from "./CheckpointReactor.ts";
24
import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
25
import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
26
+import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
27
import { RuntimeReceiptBusLive } from "./RuntimeReceiptBus.ts";
28
import { OrchestrationEventStoreLive } from "../../persistence/Layers/OrchestrationEventStore.ts";
29
import { OrchestrationCommandReceiptRepositoryLive } from "../../persistence/Layers/OrchestrationCommandReceipts.ts";
@@ -249,6 +250,7 @@ describe("CheckpointReactor", () => {
250
options?.providerName ?? "codex",
251
);
252
const orchestrationLayer = OrchestrationEngineLive.pipe(
253
+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),
254
Layer.provide(OrchestrationProjectionPipelineLive),
255
Layer.provide(OrchestrationEventStoreLive),
256
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
apps/server/src/orchestration/Layers/OrchestrationEngine.test.ts
+106
-1
@@ -8,7 +8,7 @@ import {
8
TurnId,
9
type OrchestrationEvent,
10
} from "@t3tools/contracts";
11
-import { Effect, Layer, ManagedRuntime, Queue, Stream } from "effect";
11
+import { Effect, Layer, ManagedRuntime, Option, Queue, Stream } from "effect";
12
import { describe, expect, it } from "vitest";
13
14
import { PersistenceSqlError } from "../../persistence/Errors.ts";
@@ -21,11 +21,13 @@ import {
21
} from "../../persistence/Services/OrchestrationEventStore.ts";
22
import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
23
import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
24
+import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
25
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
26
import {
27
OrchestrationProjectionPipeline,
28
type OrchestrationProjectionPipelineShape,
29
} from "../Services/ProjectionPipeline.ts";
30
+import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
31
import { ServerConfig } from "../../config.ts";
32
import * as NodeServices from "@effect/platform-node/NodeServices";
33
@@ -39,6 +41,7 @@ async function createOrchestrationSystem() {
41
prefix: "t3-orchestration-engine-test-",
42
});
43
const orchestrationLayer = OrchestrationEngineLive.pipe(
44
+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),
45
Layer.provide(OrchestrationProjectionPipelineLive),
46
Layer.provide(OrchestrationEventStoreLive),
47
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
@@ -60,6 +63,105 @@ function now() {
63
}
64
65
describe("OrchestrationEngine", () => {
66
+ it("bootstraps the in-memory read model from persisted projections", async () => {
67
+ const failOnHistoricalReplayStore: OrchestrationEventStoreShape = {
68
+ append: () =>
69
+ Effect.fail(
70
+ new PersistenceSqlError({
71
+ operation: "test.append",
72
+ detail: "append should not be called during bootstrap",
73
+ }),
74
+ ),
75
+ readFromSequence: () => Stream.empty,
76
+ readAll: () =>
77
+ Stream.fail(
78
+ new PersistenceSqlError({
79
+ operation: "test.readAll",
80
+ detail: "historical replay should not be used during bootstrap",
81
+ }),
82
+ ),
83
+ };
84
+
85
+ const projectionSnapshot = {
86
+ snapshotSequence: 7,
87
+ updatedAt: "2026-03-03T00:00:04.000Z",
88
+ projects: [
89
+ {
90
+ id: asProjectId("project-bootstrap"),
91
+ title: "Bootstrap Project",
92
+ workspaceRoot: "/tmp/project-bootstrap",
93
+ defaultModelSelection: {
94
+ provider: "codex" as const,
95
+ model: "gpt-5-codex",
96
+ },
97
+ scripts: [],
98
+ createdAt: "2026-03-03T00:00:00.000Z",
99
+ updatedAt: "2026-03-03T00:00:01.000Z",
100
+ deletedAt: null,
101
+ },
102
+ ],
103
+ threads: [
104
+ {
105
+ id: ThreadId.makeUnsafe("thread-bootstrap"),
106
+ projectId: asProjectId("project-bootstrap"),
107
+ title: "Bootstrap Thread",
108
+ modelSelection: {
109
+ provider: "codex" as const,
110
+ model: "gpt-5-codex",
111
+ },
112
+ interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
113
+ runtimeMode: "full-access" as const,
114
+ branch: null,
115
+ worktreePath: null,
116
+ latestTurn: null,
117
+ createdAt: "2026-03-03T00:00:02.000Z",
118
+ updatedAt: "2026-03-03T00:00:03.000Z",
119
+ archivedAt: null,
120
+ deletedAt: null,
121
+ messages: [],
122
+ proposedPlans: [],
123
+ activities: [],
124
+ checkpoints: [],
125
+ session: null,
126
+ },
127
+ ],
128
+ };
129
+
130
+ const layer = OrchestrationEngineLive.pipe(
131
+ Layer.provide(
132
+ Layer.succeed(ProjectionSnapshotQuery, {
133
+ getSnapshot: () => Effect.succeed(projectionSnapshot),
134
+ getCounts: () => Effect.succeed({ projectCount: 1, threadCount: 1 }),
135
+ getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
136
+ getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
137
+ getThreadCheckpointContext: () => Effect.succeed(Option.none()),
138
+ }),
139
+ ),
140
+ Layer.provide(
141
+ Layer.succeed(OrchestrationProjectionPipeline, {
142
+ bootstrap: Effect.void,
143
+ projectEvent: () => Effect.void,
144
+ } satisfies OrchestrationProjectionPipelineShape),
145
+ ),
146
+ Layer.provide(Layer.succeed(OrchestrationEventStore, failOnHistoricalReplayStore)),
147
+ Layer.provide(OrchestrationCommandReceiptRepositoryLive),
148
+ Layer.provide(SqlitePersistenceMemory),
149
+ );
150
+
151
+ const runtime = ManagedRuntime.make(layer);
152
+
153
+ const engine = await runtime.runPromise(Effect.service(OrchestrationEngineService));
154
+ const readModel = await runtime.runPromise(engine.getReadModel());
155
+
156
+ expect(readModel.snapshotSequence).toBe(7);
157
+ expect(readModel.projects).toHaveLength(1);
158
+ expect(readModel.projects[0]?.title).toBe("Bootstrap Project");
159
+ expect(readModel.threads).toHaveLength(1);
160
+ expect(readModel.threads[0]?.title).toBe("Bootstrap Thread");
161
+
162
+ await runtime.dispose();
163
+ });
164
+
165
it("returns deterministic read models for repeated reads", async () => {
166
const createdAt = now();
167
const system = await createOrchestrationSystem();
@@ -417,6 +519,7 @@ describe("OrchestrationEngine", () => {
519
520
const runtime = ManagedRuntime.make(
521
OrchestrationEngineLive.pipe(
522
+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),
523
Layer.provide(OrchestrationProjectionPipelineLive),
524
Layer.provide(Layer.succeed(OrchestrationEventStore, flakyStore)),
525
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
@@ -512,6 +615,7 @@ describe("OrchestrationEngine", () => {
615
616
const runtime = ManagedRuntime.make(
617
OrchestrationEngineLive.pipe(
618
+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),
619
Layer.provide(Layer.succeed(OrchestrationProjectionPipeline, flakyProjectionPipeline)),
620
Layer.provide(OrchestrationEventStoreLive),
621
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
@@ -653,6 +757,7 @@ describe("OrchestrationEngine", () => {
757
758
const runtime = ManagedRuntime.make(
759
OrchestrationEngineLive.pipe(
760
+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),
761
Layer.provide(Layer.succeed(OrchestrationProjectionPipeline, flakyProjectionPipeline)),
762
Layer.provide(Layer.succeed(OrchestrationEventStore, nonTransactionalStore)),
763
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
apps/server/src/orchestration/Layers/OrchestrationEngine.ts
+3
-7
@@ -19,6 +19,7 @@ import {
19
import { decideOrchestrationCommand } from "../decider.ts";
20
import { createEmptyReadModel, projectEvent } from "../projector.ts";
21
import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts";
22
+import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
23
import {
24
OrchestrationEngineService,
25
type OrchestrationEngineShape,
@@ -54,6 +55,7 @@ const makeOrchestrationEngine = Effect.gen(function* () {
55
const eventStore = yield* OrchestrationEventStore;
56
const commandReceiptRepository = yield* OrchestrationCommandReceiptRepository;
57
const projectionPipeline = yield* OrchestrationProjectionPipeline;
58
+ const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
59
60
let readModel = createEmptyReadModel(new Date().toISOString());
61
@@ -195,13 +197,7 @@ const makeOrchestrationEngine = Effect.gen(function* () {
197
};
198
199
yield* projectionPipeline.bootstrap;
198
-
199
- // bootstrap in-memory read model from event store
200
- yield* Stream.runForEach(eventStore.readAll(), (event) =>
201
- Effect.gen(function* () {
202
- readModel = yield* projectEvent(readModel, event);
203
- }),
204
- );
200
+ readModel = yield* projectionSnapshotQuery.getSnapshot();
201
202
const worker = Effect.forever(Queue.take(commandQueue).pipe(Effect.flatMap(processEnvelope)));
203
yield* Effect.forkScoped(worker);
apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts
+2
@@ -25,6 +25,7 @@ import {
25
ORCHESTRATION_PROJECTOR_NAMES,
26
OrchestrationProjectionPipelineLive,
27
} from "./ProjectionPipeline.ts";
28
+import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
29
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
30
import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts";
31
import { ServerConfig } from "../../config.ts";
@@ -1841,6 +1842,7 @@ it.effect("restores pending turn-start metadata across projection pipeline resta
1842
1843
const engineLayer = it.layer(
1844
OrchestrationEngineLive.pipe(
1845
+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),
1846
Layer.provide(OrchestrationProjectionPipelineLive),
1847
Layer.provide(OrchestrationEventStoreLive),
1848
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
apps/server/src/orchestration/Layers/ProviderCommandReactor.test.ts
+2
@@ -30,6 +30,7 @@ import { GitCore, type GitCoreShape } from "../../git/Services/GitCore.ts";
30
import { TextGeneration, type TextGenerationShape } from "../../git/Services/TextGeneration.ts";
31
import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
32
import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
33
+import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
34
import { ProviderCommandReactorLive } from "./ProviderCommandReactor.ts";
35
import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts";
36
import { ProviderCommandReactor } from "../Services/ProviderCommandReactor.ts";
@@ -211,6 +212,7 @@ describe("ProviderCommandReactor", () => {
212
};
213
214
const orchestrationLayer = OrchestrationEngineLive.pipe(
215
+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),
216
Layer.provide(OrchestrationProjectionPipelineLive),
217
Layer.provide(OrchestrationEventStoreLive),
218
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts
+2
@@ -31,6 +31,7 @@ import {
31
} from "../../provider/Services/ProviderService.ts";
32
import { OrchestrationEngineLive } from "./OrchestrationEngine.ts";
33
import { OrchestrationProjectionPipelineLive } from "./ProjectionPipeline.ts";
34
+import { OrchestrationProjectionSnapshotQueryLive } from "./ProjectionSnapshotQuery.ts";
35
import { ProviderRuntimeIngestionLive } from "./ProviderRuntimeIngestion.ts";
36
import {
37
OrchestrationEngineService,
@@ -197,6 +198,7 @@ describe("ProviderRuntimeIngestion", () => {
198
fs.mkdirSync(path.join(workspaceRoot, ".git"));
199
const provider = createProviderServiceHarness();
200
const orchestrationLayer = OrchestrationEngineLive.pipe(
201
+ Layer.provide(OrchestrationProjectionSnapshotQueryLive),
202
Layer.provide(OrchestrationProjectionPipelineLive),
203
Layer.provide(OrchestrationEventStoreLive),
204
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
apps/server/src/server.ts
+18
-6
@@ -99,12 +99,24 @@ const ReactorLayerLive = Layer.empty.pipe(
99
Layer.provideMerge(RuntimeReceiptBusLive),
100
);
101
102
-const OrchestrationLayerLive = Layer.empty.pipe(
103
- Layer.provideMerge(OrchestrationProjectionSnapshotQueryLive),
104
- Layer.provideMerge(OrchestrationEngineLive),
105
- Layer.provideMerge(OrchestrationProjectionPipelineLive),
106
- Layer.provideMerge(OrchestrationEventStoreLive),
107
- Layer.provideMerge(OrchestrationCommandReceiptRepositoryLive),
102
+const OrchestrationEventInfrastructureLayerLive = Layer.mergeAll(
103
+ OrchestrationEventStoreLive,
104
+ OrchestrationCommandReceiptRepositoryLive,
105
+);
106
+
107
+const OrchestrationProjectionPipelineLayerLive = OrchestrationProjectionPipelineLive.pipe(
108
+ Layer.provide(OrchestrationEventStoreLive),
109
+);
110
+
111
+const OrchestrationInfrastructureLayerLive = Layer.mergeAll(
112
+ OrchestrationProjectionSnapshotQueryLive,
113
+ OrchestrationEventInfrastructureLayerLive,
114
+ OrchestrationProjectionPipelineLayerLive,
115
+);
116
+
117
+const OrchestrationLayerLive = Layer.mergeAll(
118
+ OrchestrationInfrastructureLayerLive,
119
+ OrchestrationEngineLive.pipe(Layer.provide(OrchestrationInfrastructureLayerLive)),
120
);
121
122
const CheckpointingLayerLive = Layer.empty.pipe(