@devries-cameron20 / t3code-1 / commits / 016cfad012
Fork 0

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

perf(server): starting a turn on a long thread no longer scans its messages, tasks and transfers (#39103)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Bráulio Oliveira committed Nov 19, 2026 at 12:02 UTC 016cfad01223b61d4bd028ed3979dad88483b919
6 files changed +253 -13
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+187
@@ -7334,6 +7334,193 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
7334 }),
7335 );
7336
7337 + it.effect(
7338 + "a turn start reads a long thread's deferred commands, routable tasks and fork by index",
7339 + () =>
7340 + Effect.gen(function* () {
7341 + const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
7342 + const sql = yield* SqlClient.SqlClient;
7343 + const threadId = yield* addRolledBackRecoveryCandidate("turn-start-commands");
7344 + const runId = RunId.make("run:turn-start-commands:rolled-back");
7345 + const created = yield* DateTime.now;
7346 + const nowIso = DateTime.formatIso(created);
7347 + const message = (id: string, role: "user" | "assistant", text: string, minutes: number) =>
7348 + projectionStore.apply({
7349 + id: EventId.make(`event:${id}`),
7350 + type: "message.updated",
7351 + threadId,
7352 + occurredAt: created,
7353 + payload: {
7354 + id: MessageId.make(id),
7355 + threadId,
7356 + runId: null,
7357 + nodeId: null,
7358 + role,
7359 + text,
7360 + attachments: [],
7361 + streaming: false,
7362 + createdBy: "user",
7363 + creationSource: "web",
7364 + createdAt: DateTime.add(created, { minutes }),
7365 + updatedAt: DateTime.add(created, { minutes }),
7366 + },
7367 + });
7368 + yield* message("message:turn-start-commands:rolled-back", "user", "Own prompt", 5);
7369 + yield* message("message:turn-start-commands:compact", "user", "\u00a0 /Compact\n", 1);
7370 + yield* message("message:turn-start-commands:goal", "user", "/goal ship it", 2);
7371 + yield* message("message:turn-start-commands:plain", "user", "Plain prompt", 3);
7372 + yield* message("message:turn-start-commands:later", "user", "/compact later", 4);
7373 + yield* message("message:turn-start-commands:assistant", "assistant", "/compact", 6);
7374 +
7375 + const otherThreadId = ThreadId.make("thread:other");
7376 + const transfer = (id: string, type: "fork" | "merge_back") =>
7377 + projectionStore.apply({
7378 + id: EventId.make(`event:${id}`),
7379 + type: "context-transfer.created",
7380 + threadId,
7381 + occurredAt: created,
7382 + payload: {
7383 + id: ContextTransferId.make(id),
7384 + type,
7385 + sourceThreadId: otherThreadId,
7386 + targetThreadId: threadId,
7387 + sourcePoint: { threadId: otherThreadId },
7388 + basePoint: null,
7389 + sourceProviderInstanceId: providerInstanceId,
7390 + targetProviderInstanceId: providerInstanceId,
7391 + targetRunId: null,
7392 + status: "pending",
7393 + resolution: null,
7394 + createdBy: "user",
7395 + error: null,
7396 + createdAt: created,
7397 + updatedAt: created,
7398 + consumedAt: null,
7399 + },
7400 + });
7401 + yield* transfer("transfer:turn-start-commands:fork", "fork");
7402 + yield* transfer("transfer:turn-start-commands:referenced", "merge_back");
7403 + yield* projectionStore.apply({
7404 + id: EventId.make("event:turn-start-commands:handoff"),
7405 + type: "context-handoff.updated",
7406 + threadId,
7407 + occurredAt: created,
7408 + payload: {
7409 + id: ContextHandoffId.make("handoff:turn-start-commands"),
7410 + transferId: ContextTransferId.make("transfer:turn-start-commands:referenced"),
7411 + threadId,
7412 + targetRunId: runId,
7413 + fromProviderThreadIds: [],
7414 + toProviderThreadId: ProviderThreadId.make("provider-thread:turn-start-commands"),
7415 + coveredRunOrdinals: { from: 1, to: 1 },
7416 + strategy: "full_thread_summary",
7417 + status: "ready",
7418 + summaryMessageId: null,
7419 + summaryText: "Summary",
7420 + createdByProviderInstanceId: providerInstanceId,
7421 + createdAt: created,
7422 + updatedAt: created,
7423 + },
7424 + });
7425 + const nativeId = NodeId.make("subagent:turn-start-commands:native");
7426 + yield* projectionStore.apply({
7427 + id: EventId.make("event:turn-start-commands:native"),
7428 + type: "subagent.updated",
7429 + threadId,
7430 + runId,
7431 + nodeId: nativeId,
7432 + driver,
7433 + providerInstanceId,
7434 + occurredAt: created,
7435 + payload: {
7436 + id: nativeId,
7437 + threadId,
7438 + runId,
7439 + parentNodeId: NodeId.make("node:turn-start-commands:rolled-back"),
7440 + status: "completed",
7441 + startedAt: created,
7442 + completedAt: created,
7443 + origin: "provider_native",
7444 + createdBy: "agent",
7445 + driver,
7446 + providerInstanceId,
7447 + providerThreadId: null,
7448 + childThreadId: null,
7449 + nativeTaskRef: null,
7450 + prompt: "Do the work",
7451 + title: "native",
7452 + model: null,
7453 + completionDelivery: { state: "delivered", observedByRunId: null },
7454 + result: null,
7455 + updatedAt: created,
7456 + },
7457 + });
7458 + yield* sql`
7459 + INSERT INTO orchestration_v2_projection_subagents (
7460 + subagent_id, thread_id, run_id, parent_node_id, provider, origin, status, updated_at,
7461 + payload_json
7462 + ) VALUES
7463 + ('subagent:turn-start-commands:delegated', ${threadId}, NULL, 'node', 'codex',
7464 + 'app_owned', 'completed', ${nowIso}, '{}'),
7465 + ('subagent:turn-start-commands:running', ${threadId}, NULL, 'node', 'codex',
7466 + 'provider_native', 'running', ${nowIso}, '{}')
7467 + `;
7468 + yield* sql`
7469 + INSERT INTO orchestration_v2_projection_context_transfers (
7470 + context_transfer_id, source_thread_id, target_thread_id, type, status, updated_at,
7471 + payload_json
7472 + ) VALUES
7473 + ('transfer:turn-start-commands:result', 'thread:other', ${threadId},
7474 + 'subagent_result', 'consumed', ${nowIso}, '{}'),
7475 + ('transfer:turn-start-commands:other-fork', ${threadId}, 'thread:other',
7476 + 'fork', 'pending', ${nowIso}, '{}')
7477 + `;
7478 +
7479 + const { statements, tracer } = traceSqlStatements();
7480 + const context = yield* projectionStore
7481 + .getTurnStartContext(threadId, runId)
7482 + .pipe(Effect.withTracer(tracer));
7483 + assert.deepEqual(
7484 + context.messages.map((item) => item.id),
7485 + [
7486 + "message:turn-start-commands:compact",
7487 + "message:turn-start-commands:goal",
7488 + "message:turn-start-commands:rolled-back",
7489 + ],
7490 + );
7491 + assert.deepEqual(
7492 + context.subagents.map((task) => task.id),
7493 + [nativeId],
7494 + );
7495 + assert.deepEqual(
7496 + context.contextTransfers.map((item) => item.id),
7497 + ["transfer:turn-start-commands:fork", "transfer:turn-start-commands:referenced"],
7498 + );
7499 +
7500 + const planOf = Effect.fn("planOf")(function* (table: string) {
7501 + const statement = statements.find((query) =>
7502 + query.includes(`FROM orchestration_v2_projection_${table}`),
7503 + )!;
7504 + const params = Array.from({ length: statement.split("?").length - 1 }, () => threadId);
7505 + return (yield* sql.unsafe<{ readonly detail: string }>(
7506 + `EXPLAIN QUERY PLAN ${statement}`,
7507 + params,
7508 + )).map((row) => row.detail);
7509 + });
7510 + for (const [table, index] of [
7511 + ["messages", "orchestration_v2_projection_messages_deferred_command_idx"],
7512 + ["subagents", "orchestration_v2_projection_subagents_routable_idx"],
7513 + ["context_transfers", "orchestration_v2_projection_context_transfers_fork_idx"],
7514 + ] as const) {
7515 + const plan = yield* planOf(table);
7516 + assert.isTrue(
7517 + plan.some((detail) => detail.includes(index)),
7518 + `${table}\n${plan.join("\n")}`,
7519 + );
7520 + }
7521 + }),
7522 + );
7523 +
7524 it.effect(
7525 "counts each shell's own runless and live-run items, skipping rolled back and missing runs",
7526 () =>
apps/server/src/orchestration-v2/ProjectionStore.ts
+21 -13
@@ -103,6 +103,10 @@ import type * as Statement from "effect/sql/Statement";
103
104 import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp";
105 import { threadHtmlRenderAttachmentIds } from "../attachmentStore.ts";
106 +import {
107 + DEFERRED_COMMAND_MESSAGE_CONDITION,
108 + JAVASCRIPT_TRIM_WHITESPACE,
109 +} from "./deferredCommandMessage.ts";
110 import { readableSearchText } from "./messageSearchIndex.ts";
111 import {
112 isFoldableWorkItem,
@@ -5384,10 +5388,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5388 Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })),
5389 );
5390
5387 - // SQLite trim defaults to ASCII spaces; compact recognition uses JavaScript trim.
5388 - const javascriptTrimWhitespace =
5389 - " \t\n\r\v\f\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff";
5390 -
5391 const getTurnStartContext: ProjectionStoreV2Shape["getTurnStartContext"] = (threadId, runId) =>
5392 sql
5393 .withTransaction(
@@ -5407,9 +5407,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5407 INDEXED BY sqlite_autoindex_orchestration_v2_projection_nodes_1
5408 WHERE thread_id = ${threadId} AND node_id IN (SELECT json_extract(payload_json, '$.rootNodeId') FROM orchestration_v2_projection_runs WHERE thread_id = ${threadId} AND run_id = ${runId}) ORDER BY node_id ASC
5409 `.pipe(Effect.flatMap(decodeRows(decodeNodePayload, threadId)));
5410 + // Only the completed provider-native ones are routed. A long thread holds thousands of delegated ones.
5411 const subagents = yield* sql<PayloadRow>`
5412 SELECT payload_json FROM orchestration_v2_projection_subagents
5412 - WHERE thread_id = ${threadId} AND status NOT IN ('interrupted','failed','cancelled') ORDER BY subagent_id ASC
5413 + WHERE thread_id = ${threadId} AND origin = 'provider_native' AND status = 'completed' ORDER BY subagent_id ASC
5414 `.pipe(Effect.flatMap(decodeRows(decodeSubagentPayload, threadId)));
5415 const providerThreads = yield* sql<PayloadRow>`
5416 SELECT payload_json FROM orchestration_v2_projection_provider_threads
@@ -5427,9 +5428,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5428 SELECT payload_json FROM orchestration_v2_projection_context_handoffs
5429 WHERE thread_id = ${threadId} ORDER BY rowid ASC
5430 `.pipe(Effect.flatMap(decodeRows(decodeContextHandoffPayload, threadId)));
5431 + // The fork into this thread and the transfers its handoffs came from, not the thousands of delegated results.
5432 const contextTransfers = yield* sql<PayloadRow>`
5433 SELECT payload_json FROM orchestration_v2_projection_context_transfers
5432 - WHERE source_thread_id = ${threadId} OR target_thread_id = ${threadId} ORDER BY rowid ASC
5434 + WHERE (target_thread_id = ${threadId} AND type = 'fork')
5435 + OR context_transfer_id IN (
5436 + SELECT json_extract(payload_json, '$.transferId')
5437 + FROM orchestration_v2_projection_context_handoffs WHERE thread_id = ${threadId}
5438 + )
5439 + ORDER BY rowid ASC
5440 `.pipe(Effect.flatMap(decodeRows(decodeContextTransferPayload, threadId)));
5441 const turnItems = yield* sql<PayloadRow>`
5442 SELECT payload_json FROM orchestration_v2_projection_turn_items
@@ -5453,20 +5460,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5460 const messageIds = [run, unsentSource].flatMap((source) =>
5461 source === undefined ? [] : [source.userMessageId],
5462 );
5463 + // Earlier /compact and /goal turns defer context too; parseGoalCommand narrows the /goal prefix match.
5464 const messages = yield* sql<PayloadRow>`
5457 - SELECT payload_json FROM orchestration_v2_projection_messages
5458 - WHERE thread_id = ${threadId} AND (
5459 - message_id IN ${sql.in(messageIds)}
5460 - OR (role = 'user' AND lower(trim(json_extract(payload_json, '$.text'), ${javascriptTrimWhitespace})) = '/compact')
5461 - -- Earlier /goal turns defer context too; parseGoalCommand narrows this prefix match.
5462 - OR (role = 'user' AND trim(json_extract(payload_json, '$.text'), ${javascriptTrimWhitespace}) GLOB '/goal*')
5465 + SELECT payload_json FROM (
5466 + SELECT payload_json, created_at, message_id FROM orchestration_v2_projection_messages
5467 + WHERE thread_id = ${threadId} AND message_id IN ${sql.in(messageIds)}
5468 + UNION
5469 + SELECT payload_json, created_at, message_id FROM orchestration_v2_projection_messages
5470 + WHERE thread_id = ${threadId} AND ${sql.literal(DEFERRED_COMMAND_MESSAGE_CONDITION)}
5471 ) ORDER BY created_at ASC, message_id ASC
5472 `.pipe(Effect.flatMap(decodeRows(decodeMessagePayload, threadId)));
5473 const conversation = yield* sql<{ present: number }>`
5474 SELECT EXISTS(
5475 SELECT 1 FROM orchestration_v2_projection_messages
5476 WHERE thread_id = ${threadId} AND role = 'user'
5469 - AND (lower(trim(json_extract(payload_json, '$.text'), ${javascriptTrimWhitespace})) <> '/compact'
5477 + AND (lower(trim(json_extract(payload_json, '$.text'), ${JAVASCRIPT_TRIM_WHITESPACE})) <> '/compact'
5478 OR json_array_length(payload_json, '$.attachments') > 0)
5479 ) AS present
5480 `;
apps/server/src/orchestration-v2/deferredCommandMessage.ts new
+15
@@ -0,0 +1,15 @@
1 +// SQLite's trim needs the characters JavaScript's String.prototype.trim removes spelled out.
2 +export const JAVASCRIPT_TRIM_WHITESPACE =
3 + " \t\n\r\v\f\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff";
4 +
5 +const text = `json_extract(payload_json, '$.text')`;
6 +const whitespace = `'${JAVASCRIPT_TRIM_WHITESPACE}'`;
7 +
8 +/**
9 + * A user message that is a `/compact` or `/goal` command, whose turns defer context to the next
10 + * one. The partial index on it is only used when the read spells this text the same way.
11 + */
12 +export const DEFERRED_COMMAND_MESSAGE_CONDITION = `role = 'user' AND (
13 + lower(trim(${text}, ${whitespace})) = '/compact'
14 + OR trim(${text}, ${whitespace}) GLOB '/goal*'
15 +)`;
apps/server/src/persistence/Migrations.ts
+2
@@ -107,6 +107,7 @@ import Migration0096 from "./Migrations/096_RepairStaleTitleRegeneration.ts";
107 import Migration0097 from "./Migrations/097_IssueTrackerBindings.ts";
108 import Migration0098 from "./Migrations/098_ActiveShellIndexIndexes.ts";
109 import Migration0099 from "./Migrations/099_BackgroundRecoveryIndexes.ts";
110 +import Migration0100 from "./Migrations/100_TurnStartReadIndexes.ts";
111
112 const migrationEntries = [
113 [1, "OrchestrationEvents", Migration0001],
@@ -209,6 +210,7 @@ const migrationEntries = [
210 [97, "IssueTrackerBindings", Migration0097],
211 [98, "ActiveShellIndexIndexes", Migration0098],
212 [99, "BackgroundRecoveryIndexes", Migration0099],
213 + [100, "TurnStartReadIndexes", Migration0100],
214 ] as const;
215
216 export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/100_TurnStartReadIndexes.ts new
+26
@@ -0,0 +1,26 @@
1 +import * as Effect from "effect/Effect";
2 +import * as SqlClient from "effect/sql/SqlClient";
3 +
4 +import { DEFERRED_COMMAND_MESSAGE_CONDITION } from "../../orchestration-v2/deferredCommandMessage.ts";
5 +
6 +export default Effect.gen(function* () {
7 + const sql = yield* SqlClient.SqlClient;
8 +
9 + // A turn start finds these few rows of a long thread without reading the rest of its history.
10 + // The reads in ProjectionStore.getTurnStartContext must spell the same predicates.
11 + yield* sql.unsafe(`
12 + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_messages_deferred_command_idx
13 + ON orchestration_v2_projection_messages(thread_id, created_at, message_id)
14 + WHERE ${DEFERRED_COMMAND_MESSAGE_CONDITION}
15 + `);
16 + yield* sql`
17 + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_subagents_routable_idx
18 + ON orchestration_v2_projection_subagents(thread_id, subagent_id)
19 + WHERE origin = 'provider_native' AND status = 'completed'
20 + `;
21 + yield* sql`
22 + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_context_transfers_fork_idx
23 + ON orchestration_v2_projection_context_transfers(target_thread_id)
24 + WHERE type = 'fork'
25 + `;
26 +});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -79,6 +79,7 @@ describe("V2 preview upgrade", () => {
79 [97, "IssueTrackerBindings"],
80 [98, "ActiveShellIndexIndexes"],
81 [99, "BackgroundRecoveryIndexes"],
82 + [100, "TurnStartReadIndexes"],
83 ]);
84 assert.deepStrictEqual(yield* runMigrations(), []);
85 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -201,6 +202,7 @@ describe("V2 preview upgrade", () => {
202 [97, "IssueTrackerBindings"],
203 [98, "ActiveShellIndexIndexes"],
204 [99, "BackgroundRecoveryIndexes"],
205 + [100, "TurnStartReadIndexes"],
206 ]);
207 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
208 );