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, 03:15 UTC
perf(server): batch projection recovery reads into one query (#31842)
Combine the ten runtime recovery collections into one SQLite statement, leaving the initial thread lookup separate. Preserve collection filters, ordering, decoding and ordered reconciliation after prior commits. Extend startup/shutdown idempotency coverage and guard against full message and run history scans. Synthetic stress measurements show fewer database requests; overall desktop quit latency remains unmeasured. Implemented with GPT 6.1 Sol through the Codex harness in T3 Code.
Yash Singh committed
Nov 6, 2026 at 01:01 UTC
c22251163132534c4f2048ac35bad8caad0708db
3 files changed
+123
-82
apps/server/src/orchestration-v2/ProjectionRecovery.test.ts
+82
-43
@@ -213,55 +213,87 @@ it.effect.each(["startup", "shutdown"] as const)(
213
}).pipe(Effect.provide(layerRecoveryTest)),
214
);
215
216
-it.effect("stops a session its thread detached, which kept its live status", () =>
217
- Effect.gen(function* () {
218
- const projections = yield* ProjectionStore.ProjectionStoreV2;
219
- const recovery = yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService;
220
- const now = yield* DateTime.now;
221
- const threadId = yield* createThread("detached-session");
222
- const providerSessionId = ProviderSessionId.make("session:recovery:detached");
223
- yield* projections.apply({
224
- id: EventId.make("event:recovery:detached-session:attached"),
225
- type: "provider-session.attached",
226
- threadId,
227
- driver,
228
- providerInstanceId,
229
- occurredAt: now,
230
- payload: {
231
- id: providerSessionId,
216
+it.effect.each(["startup", "shutdown"] as const)(
217
+ "stops a detached session and its orphaned active provider thread on %s",
218
+ (trigger) =>
219
+ Effect.gen(function* () {
220
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
221
+ const recovery = yield* ProviderRuntimeRecovery.ProviderRuntimeRecoveryService;
222
+ const now = yield* DateTime.now;
223
+ const threadId = yield* createThread("detached-session");
224
+ const providerSessionId = ProviderSessionId.make("session:recovery:detached");
225
+ yield* projections.apply({
226
+ id: EventId.make("event:recovery:detached-session:attached"),
227
+ type: "provider-session.attached",
228
+ threadId,
229
driver,
230
providerInstanceId,
234
- status: "ready",
235
- cwd: "/workspace",
236
- model: modelSelection.model,
237
- capabilities: CodexProviderCapabilitiesV2,
238
- createdAt: now,
239
- updatedAt: now,
240
- lastError: null,
241
- },
242
- });
243
- yield* projections.apply({
244
- id: EventId.make("event:recovery:detached-session:detached"),
245
- type: "provider-session.detached",
246
- threadId,
247
- driver,
248
- providerInstanceId,
249
- occurredAt: now,
250
- payload: { providerSessionId, detachedAt: now, reason: "Thread settled." },
251
- });
252
- assert.deepEqual(yield* projections.getRecoveryThreadIds("runtime"), [threadId]);
231
+ occurredAt: now,
232
+ payload: {
233
+ id: providerSessionId,
234
+ driver,
235
+ providerInstanceId,
236
+ status: "ready",
237
+ cwd: "/workspace",
238
+ model: modelSelection.model,
239
+ capabilities: CodexProviderCapabilitiesV2,
240
+ createdAt: now,
241
+ updatedAt: now,
242
+ lastError: null,
243
+ },
244
+ });
245
+ yield* projections.apply({
246
+ id: EventId.make("event:recovery:detached-session:detached"),
247
+ type: "provider-session.detached",
248
+ threadId,
249
+ driver,
250
+ providerInstanceId,
251
+ occurredAt: now,
252
+ payload: { providerSessionId, detachedAt: now, reason: "Thread settled." },
253
+ });
254
+ const providerThreadId = ProviderThreadId.make("provider-thread:recovery:detached");
255
+ yield* projections.apply({
256
+ id: EventId.make("event:recovery:detached-provider-thread"),
257
+ type: "provider-thread.updated",
258
+ threadId,
259
+ occurredAt: now,
260
+ payload: {
261
+ id: providerThreadId,
262
+ appThreadId: threadId,
263
+ ownerNodeId: null,
264
+ driver,
265
+ providerInstanceId,
266
+ providerSessionId: null,
267
+ nativeThreadRef: null,
268
+ nativeConversationHeadRef: null,
269
+ status: "active",
270
+ firstRunOrdinal: null,
271
+ lastRunOrdinal: null,
272
+ handoffIds: [],
273
+ forkedFrom: null,
274
+ createdAt: now,
275
+ updatedAt: now,
276
+ },
277
+ });
278
+ assert.deepEqual(yield* projections.getRecoveryThreadIds("runtime"), [threadId]);
279
254
- const summary = yield* recovery.reconcile("startup");
280
+ const summary = yield* recovery.reconcile(trigger);
281
256
- assert.equal(summary.stoppedSessions, 1);
257
- const sql = yield* SqlClient.SqlClient;
258
- const rows = yield* sql<{ readonly status: string }>`
282
+ assert.equal(summary.stoppedSessions, 1);
283
+ const sql = yield* SqlClient.SqlClient;
284
+ const rows = yield* sql<{ readonly status: string }>`
285
SELECT status FROM orchestration_v2_projection_provider_sessions
286
WHERE provider_session_id = ${providerSessionId}
287
`;
262
- assert.deepEqual(rows, [{ status: "stopped" }]);
263
- assert.deepEqual(yield* projections.getRecoveryThreadIds("runtime"), []);
264
- }).pipe(Effect.provide(layerRecoveryTest)),
288
+ assert.deepEqual(rows, [{ status: "stopped" }]);
289
+ const projection = yield* projections.getThreadProjection(threadId);
290
+ assert.equal(projection.providerThreads[0]?.status, "idle");
291
+ const eventStore = yield* EventStore.EventStoreV2;
292
+ const sequence = yield* eventStore.latestSequence();
293
+ assert.deepEqual(yield* projections.getRecoveryThreadIds("runtime"), []);
294
+ yield* recovery.reconcile(trigger);
295
+ assert.equal(yield* eventStore.latestSequence(), sequence);
296
+ }).pipe(Effect.provide(layerRecoveryTest)),
297
);
298
299
it.effect.each(["attached", "detached", "replaced"] as const)(
@@ -964,11 +996,18 @@ it.effect("bounds streaming recovery reads by candidate runs and preserves messa
996
assert.equal(statements.length, 2);
997
for (const [query, params] of statements) {
998
const plan = yield* sql.unsafe<{ detail: string }>(`EXPLAIN QUERY PLAN ${query}`, params);
967
- assert.isTrue(plan[0]?.detail.includes("SEARCH run "), JSON.stringify(plan));
999
+ assert.isTrue(
1000
+ plan.some((row) => row.detail.includes("SEARCH message_run ")),
1001
+ JSON.stringify(plan),
1002
+ );
1003
assert.isTrue(
1004
plan.some((row) => row.detail.includes("messages_run_idx")),
1005
JSON.stringify(plan),
1006
);
1007
+ assert.isFalse(
1008
+ plan.some((row) => /\bSCAN message\b/.test(row.detail)),
1009
+ JSON.stringify(plan),
1010
+ );
1011
assert.isFalse(
1012
plan.some((row) => row.detail.includes("messages_thread_created_idx")),
1013
JSON.stringify(plan),
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+1
@@ -8439,6 +8439,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
8439
.pipe(Effect.withTracer(tracer))).runs.map((run) => run.id),
8440
[runId, newerRunId],
8441
);
8442
+ assert.lengthOf(statements, 2);
8443
// Live-item filters must hit the active partial indexes, not walk the thread's history.
8444
const sql = yield* SqlClient.SqlClient;
8445
for (const statement of statements.filter((query) => query.includes("turn_items"))) {
apps/server/src/orchestration-v2/ProjectionStore.ts
+40
-39
@@ -4608,21 +4608,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4608
if (threadRows[0] === undefined) {
4609
return yield* new ProjectionStoreThreadNotFoundError({ threadId });
4610
}
4611
- const [
4612
- thread,
4613
- runRows,
4614
- attemptRows,
4615
- nodeRows,
4616
- subagentRows,
4617
- providerSessionRows,
4618
- providerThreadRows,
4619
- providerTurnRows,
4620
- runtimeRequestRows,
4621
- messageRows,
4622
- turnItemRows,
4623
- ] = yield* Effect.all([
4624
- decodeThreadPayload(threadRows[0].payload_json),
4625
- sql<PayloadRow>`
4611
+ const thread = yield* decodeThreadPayload(threadRows[0].payload_json);
4612
+ // One worker round trip and one SQLite snapshot; decisions still read after the prior commit.
4613
+ const queries = {
4614
+ runs: sql<PayloadRow>`
4615
SELECT payload_json FROM orchestration_v2_projection_runs AS run
4616
WHERE run.thread_id = ${threadId}
4617
AND (
@@ -4665,7 +4654,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4654
)
4655
ORDER BY run.ordinal ASC
4656
`,
4668
- sql<PayloadRow>`
4657
+ attempts: sql<PayloadRow>`
4658
SELECT attempt.payload_json
4659
FROM orchestration_v2_projection_run_attempts AS attempt
4660
WHERE attempt.thread_id = ${threadId}
@@ -4676,7 +4665,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4665
)
4666
ORDER BY attempt.run_id ASC, attempt.attempt_ordinal ASC
4667
`,
4679
- sql<PayloadRow>`
4668
+ nodes: sql<PayloadRow>`
4669
SELECT node.payload_json FROM orchestration_v2_projection_nodes AS node
4670
WHERE node.thread_id = ${threadId}
4671
AND node.status IN ('pending', 'starting', 'running', 'waiting')
@@ -4711,7 +4700,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4700
)
4701
ORDER BY COALESCE(node.started_at, ''), node.node_id ASC
4702
`,
4714
- sql<PayloadRow>`
4703
+ subagents: sql<PayloadRow>`
4704
SELECT subagent.payload_json FROM orchestration_v2_projection_subagents AS subagent
4705
WHERE subagent.thread_id = ${threadId}
4706
AND subagent.status IN ('pending', 'starting', 'running', 'waiting')
@@ -4731,7 +4720,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4720
)
4721
ORDER BY COALESCE(subagent.started_at, ''), subagent.subagent_id ASC
4722
`,
4734
- sql<PayloadRow>`
4723
+ providerSessions: sql<PayloadRow>`
4724
SELECT session.payload_json
4725
FROM orchestration_v2_projection_provider_sessions AS session
4726
WHERE (
@@ -4769,7 +4758,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4758
)
4759
ORDER BY session.updated_at ASC, session.provider_session_id ASC
4760
`,
4772
- sql<PayloadRow>`
4761
+ providerThreads: sql<PayloadRow>`
4762
SELECT provider_thread.payload_json
4763
FROM orchestration_v2_projection_provider_threads AS provider_thread
4764
WHERE provider_thread.provider_thread_id IN (
@@ -4815,7 +4804,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4804
)
4805
ORDER BY COALESCE(provider_thread.first_run_ordinal, 0), provider_thread.provider_thread_id ASC
4806
`,
4818
- sql<PayloadRow>`
4807
+ providerTurns: sql<PayloadRow>`
4808
SELECT provider_turn.payload_json
4809
FROM orchestration_v2_projection_provider_turns AS provider_turn
4810
WHERE provider_turn.thread_id = ${threadId}
@@ -4843,22 +4832,22 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4832
)
4833
ORDER BY provider_turn.provider_thread_id ASC, provider_turn.ordinal ASC
4834
`,
4846
- sql<PayloadRow>`
4835
+ runtimeRequests: sql<PayloadRow>`
4836
SELECT payload_json FROM orchestration_v2_projection_runtime_requests
4837
WHERE thread_id = ${threadId} AND status = 'pending'
4838
ORDER BY created_at ASC, runtime_request_id ASC
4839
`,
4840
// CROSS JOIN keeps empty run cohorts out of message history; the run index bounds each lookup.
4852
- sql<PayloadRow>`
4853
- SELECT message.payload_json FROM orchestration_v2_projection_runs AS run
4841
+ messages: sql<PayloadRow>`
4842
+ SELECT message.payload_json FROM orchestration_v2_projection_runs AS message_run
4843
CROSS JOIN orchestration_v2_projection_messages AS message
4855
- INDEXED BY orchestration_v2_projection_messages_run_idx ON message.run_id = run.run_id
4856
- WHERE run.thread_id = ${threadId}
4857
- AND run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
4844
+ INDEXED BY orchestration_v2_projection_messages_run_idx ON message.run_id = message_run.run_id
4845
+ WHERE message_run.thread_id = ${threadId}
4846
+ AND message_run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
4847
AND message.thread_id = ${threadId} AND message.streaming = 1
4848
ORDER BY message.created_at ASC, message.message_id ASC
4849
`,
4861
- sql<PayloadRow>`
4850
+ turnItems: sql<PayloadRow>`
4851
SELECT item.payload_json FROM orchestration_v2_projection_turn_items AS item
4852
WHERE item.thread_id = ${threadId}
4853
AND item.status IN ('pending', 'running', 'waiting')
@@ -4889,7 +4878,19 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4878
)
4879
ORDER BY item.ordinal ASC, item.turn_item_id ASC
4880
`,
4892
- ]);
4881
+ };
4882
+ const sections = Object.entries(queries).map(
4883
+ ([section, query]) => sql`
4884
+ SELECT ${section} AS section, payload_json, row_number() OVER () AS position
4885
+ FROM (${query})
4886
+ `,
4887
+ );
4888
+ const rows = yield* sql<PayloadRow & { readonly section: string }>`
4889
+ ${sql.join(" UNION ALL ", false)(sections)}
4890
+ ORDER BY section, position
4891
+ `;
4892
+ const readRows = (section: keyof typeof queries) =>
4893
+ rows.filter((row) => row.section === section);
4894
const [
4895
runs,
4896
attempts,
@@ -4902,16 +4903,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4903
messages,
4904
turnItems,
4905
] = yield* Effect.all([
4905
- decodeRows(decodeRunPayload, threadId)(runRows),
4906
- decodeRows(decodeRunAttemptPayload, threadId)(attemptRows),
4907
- decodeRows(decodeNodePayload, threadId)(nodeRows),
4908
- decodeRows(decodeSubagentPayload, threadId)(subagentRows),
4909
- decodeRows(decodeProviderSessionPayload, threadId)(providerSessionRows),
4910
- decodeRows(decodeProviderThreadPayload, threadId)(providerThreadRows),
4911
- decodeRows(decodeProviderTurnPayload, threadId)(providerTurnRows),
4912
- decodeRows(decodeRuntimeRequestPayload, threadId)(runtimeRequestRows),
4913
- decodeRows(decodeMessagePayload, threadId)(messageRows),
4914
- decodeRows(decodeTurnItemPayload, threadId)(turnItemRows),
4906
+ decodeRows(decodeRunPayload, threadId)(readRows("runs")),
4907
+ decodeRows(decodeRunAttemptPayload, threadId)(readRows("attempts")),
4908
+ decodeRows(decodeNodePayload, threadId)(readRows("nodes")),
4909
+ decodeRows(decodeSubagentPayload, threadId)(readRows("subagents")),
4910
+ decodeRows(decodeProviderSessionPayload, threadId)(readRows("providerSessions")),
4911
+ decodeRows(decodeProviderThreadPayload, threadId)(readRows("providerThreads")),
4912
+ decodeRows(decodeProviderTurnPayload, threadId)(readRows("providerTurns")),
4913
+ decodeRows(decodeRuntimeRequestPayload, threadId)(readRows("runtimeRequests")),
4914
+ decodeRows(decodeMessagePayload, threadId)(readRows("messages")),
4915
+ decodeRows(decodeTurnItemPayload, threadId)(readRows("turnItems")),
4916
]);
4917
return {
4918
thread,