@devries-cameron20 / t3code Mirror · read-only / commits / a4d0a06e7f
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 21, 19:45 UTC

perf(server): sending to a very long thread reads about 5x less of its history (#35130)

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>

Theo Browne committed Nov 12, 2026 at 02:25 UTC a4d0a06e7f1a062b2fcbc93e5052db017483297e
7 files changed +195 -37
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+20
@@ -827,6 +827,23 @@ it.effect(
827 yield* sql`INSERT INTO orchestration_v2_projection_context_handoffs
828 (context_handoff_id, thread_id, target_run_id, to_provider_thread_id, strategy, status, updated_at, payload_json)
829 VALUES ('obsolete-handoff', ${threadId}, 'old-run', 'old-provider-thread', 'full_thread_summary', 'ready', ${DateTime.formatIso(now)}, '{"obsolete":true}')`;
830 + for (const status of [
831 + "resolved_native",
832 + "resolved_portable",
833 + "failed",
834 + "consumed",
835 + "superseded",
836 + ]) {
837 + yield* sql`INSERT INTO orchestration_v2_projection_context_transfers
838 + (context_transfer_id, source_thread_id, target_thread_id, target_run_id,
839 + type, status, updated_at, payload_json)
840 + VALUES (${`obsolete-transfer:${status}`}, 'old-source', ${threadId}, NULL,
841 + 'merge_back', ${status}, ${DateTime.formatIso(now)}, '{"obsolete":true}')`;
842 + }
843 + assert.equal(
844 + (yield* Effect.exit(projections.getThreadRecords(threadId, ["contextTransfers"])))._tag,
845 + "Failure",
846 + );
847 yield* orchestrator.dispatch({
848 type: "message.dispatch",
849 commandId: CommandId.make("dispatch-with-old-history"),
@@ -842,6 +859,9 @@ it.effect(
859 turnItemTypes: ["user_message"],
860 });
861 assert.isAbove(fresh.turnItems.at(-1)!.ordinal, 900);
862 + // Deletion reads transfer history separately to find side chats.
863 + yield* sql`DELETE FROM orchestration_v2_projection_context_transfers
864 + WHERE target_thread_id = ${threadId}`;
865 yield* orchestrator.dispatch({
866 type: "thread.archive",
867 commandId: CommandId.make("archive-with-old-history"),
apps/server/src/orchestration-v2/Orchestrator.ts
+2
@@ -1489,6 +1489,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1489 // Commands here read only undelivered or active tasks; a dispatcher thread keeps
1490 // thousands of delivered ones, each carrying its full prompt.
1491 omitSettledSubagents: true,
1492 + // Commands act on pending forks and merge-backs only; consumed ones stay unread.
1493 + contextTransferStatuses: ["pending"],
1494 },
1495 )
1496 .pipe(
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+41
@@ -741,6 +741,37 @@ it.effect("idle compaction filters keep the latest run, live work and the newest
741 .join("\n");
742 assert.include(plan, `USING INDEX sqlite_autoindex_orchestration_v2_projection_${table}_1`);
743 }
744 + // The filters every command reads with seek the rows they keep, not every row of the thread.
745 + const command = traceSqlStatements();
746 + yield* store
747 + .getThreadRecords(threadId, ["nodes", "subagents", "turnItems", "contextTransfers"], {
748 + turnItemTypes: ["user_message", "error"],
749 + omitSettledLeafNodes: true,
750 + omitSettledSubagents: true,
751 + contextTransferStatuses: ["pending"],
752 + })
753 + .pipe(Effect.withTracer(command.tracer));
754 + for (const [table, seeks] of [
755 + ["turn_items", ["USING INDEX orchestration_v2_projection_turn_items_conversation_idx"]],
756 + ["nodes", ["nodes_settled_leaf_idx (thread_id=? AND <expr>=?)"]],
757 + ["subagents", ["USING COVERING INDEX orchestration_v2_projection_subagents_settled_idx"]],
758 + [
759 + "context_transfers",
760 + [
761 + "source_thread_status_idx (source_thread_id=? AND status=?)",
762 + "target_thread_idx (target_thread_id=? AND status=?)",
763 + ],
764 + ],
765 + ] as const) {
766 + const query = command.statements.find((statement) =>
767 + statement.includes(`FROM orchestration_v2_projection_${table}`),
768 + );
769 + assert.isDefined(query);
770 + const plan = (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`))
771 + .map((row) => row.detail)
772 + .join("\n");
773 + for (const seek of seeks) assert.include(plan, seek);
774 + }
775 assert.deepEqual(
776 records.providerTurns.map((turn) => turn.id),
777 ["turn:idle-newest"],
@@ -828,6 +859,16 @@ it.effect("record reads filter notification sources without dropping matching me
859 plan.some((detail) => detail.includes("turn_items_user_message_idx")),
860 plan.join("\n"),
861 );
862 + // With every user item loaded, the items give the order and the lookup does not run.
863 + const withItems = traceSqlStatements();
864 + const ordered = yield* store
865 + .getThreadRecords(threadId, ["messages", "turnItems"], {
866 + messageRoles: ["user"],
867 + turnItemTypes: ["user_message"],
868 + })
869 + .pipe(Effect.withTracer(withItems.tracer));
870 + assert.deepEqual(ordered.messages, userMessages.messages);
871 + assert.isFalse(withItems.statements.some((statement) => statement.includes("MIN(ordinal)")));
872 assert.lengthOf((yield* store.getThreadRecords(threadId, ["messages"])).messages, 4);
873 assert.lengthOf(
874 (yield* store.getThreadRecords(threadId, ["messages"], {
apps/server/src/orchestration-v2/ProjectionStore.ts
+101 -37
@@ -1116,6 +1116,15 @@ const encodeIdList = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema
1116 const matchesNothing = (...filters: ReadonlyArray<ReadonlyArray<unknown> | undefined>) =>
1117 filters.some((values) => values?.length === 0);
1118
1119 +// A typed item read that names no tool type fits in migration 082's conversation index. A run or
1120 +// node filter is left alone: it already reads through a narrower index.
1121 +const readsConversationItemsOnly = (filter: ProjectionRecordFilter | undefined) =>
1122 + filter?.turnItemTypes !== undefined &&
1123 + filter.turnItemRunId === undefined &&
1124 + filter.turnItemRunIds === undefined &&
1125 + filter.turnItemNodeIds === undefined &&
1126 + filter.turnItemTypes.every((type) => DEFERRED_WORK_ITEM_TYPES.every((tool) => tool !== type));
1127 +
1128 const encodeThreadPayload = Schema.encodeEffect(
1129 Schema.fromJsonString(OrchestrationV2AppThreadJsonSchema),
1130 );
@@ -2072,6 +2081,17 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2081 `(${DEFERRED_WORK_ITEM_TYPES.map((type) => `'${type}'`).join(", ")})`,
2082 );
2083
2084 + // An ended task whose completion was acknowledged, delivered or disposed, outside an active run.
2085 + const settledSubagent = (row: Statement.Fragment) => sql`(
2086 + ${row}.status IN ('completed', 'failed', 'cancelled', 'interrupted')
2087 + AND COALESCE(${row}.completion_delivery_state, '') IN ('acknowledged', 'delivered', 'disposed')
2088 + AND NOT EXISTS (
2089 + SELECT 1 FROM orchestration_v2_projection_runs AS run
2090 + WHERE run.run_id = ${row}.run_id
2091 + AND run.status IN ('preparing', 'queued', 'starting', 'running', 'waiting')
2092 + )
2093 + )`;
2094 +
2095 // A todo list is never closed when its run ends, so a long thread holds an active one per
2096 // old run. Counting those as live work drags each run's nodes and subagents into every window.
2097 const todoListOfSettledRun = (row: Statement.Fragment) => sql`(
@@ -3429,7 +3449,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3449 ? yield* sql<PayloadRow>`
3450 SELECT payload_json
3451 FROM orchestration_v2_projection_turn_items
3432 - WHERE thread_id = ${threadId}
3452 + ${
3453 + // A type list of conversation items reads them from the conversation index.
3454 + // Any other plan fetches every tool call of the thread to check its type.
3455 + readsConversationItemsOnly(filter)
3456 + ? sql`INDEXED BY orchestration_v2_projection_turn_items_conversation_idx
3457 + WHERE thread_id = ${threadId} AND type NOT IN ${CONVERSATION_INDEX_EXCLUDED_TYPES}`
3458 + : sql`WHERE thread_id = ${threadId}`
3459 + }
3460 ${filter?.turnItemRequestIds === undefined ? sql`` : sql`AND json_extract(payload_json, '$.requestId') IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemRequestIds)}))`}
3461 ${filter?.turnItemTypes === undefined ? sql`` : sql`AND type IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemTypes)}))`}
3462 ${filter?.turnItemStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.turnItemStatuses)}))`}
@@ -3694,22 +3721,31 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3721 ? sql<PayloadRow>`
3722 SELECT payload_json
3723 FROM orchestration_v2_projection_nodes AS node
3697 - WHERE ${threadColumn(filter?.nodeIds, "node.thread_id")} = ${threadId}
3698 - ${filter?.nodeIds === undefined ? sql`` : sql`AND node.node_id IN (SELECT value FROM json_each(${encodeIdList(filter.nodeIds)}))`}
3699 - ${
3700 - filter?.omitSettledLeafNodes === true
3701 - ? sql`AND NOT (
3702 - node.kind IN ('tool_call', 'assistant_message', 'reasoning')
3703 - AND node.status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3704 - AND node.run_id IS NOT NULL
3705 - AND NOT EXISTS (
3706 - SELECT 1 FROM orchestration_v2_projection_runs AS run
3707 - WHERE run.run_id = node.run_id
3708 - AND run.status IN ('preparing', 'queued', 'starting', 'running', 'waiting')
3724 + WHERE ${
3725 + filter?.omitSettledLeafNodes === true
3726 + ? // Seeks the kept nodes. One filter over the thread reads every settled leaf to
3727 + // drop it. The first predicate must match migration 083's index expression. A
3728 + // node's run is always a run of its own thread.
3729 + sql`node.rowid IN (
3730 + SELECT rowid FROM orchestration_v2_projection_nodes
3731 + WHERE thread_id = ${threadId}
3732 + AND (kind IN ('tool_call', 'assistant_message', 'reasoning')
3733 + AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3734 + AND run_id IS NOT NULL) = 0
3735 + UNION ALL
3736 + SELECT rowid FROM orchestration_v2_projection_nodes
3737 + WHERE thread_id = ${threadId}
3738 + AND run_id IN (
3739 + SELECT run_id FROM orchestration_v2_projection_runs
3740 + WHERE thread_id = ${threadId}
3741 + AND status IN ('preparing', 'queued', 'starting', 'running', 'waiting')
3742 )
3710 - )`
3711 - : sql``
3712 - }
3743 + AND kind IN ('tool_call', 'assistant_message', 'reasoning')
3744 + AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
3745 + )`
3746 + : sql`${threadColumn(filter?.nodeIds, "node.thread_id")} = ${threadId}`
3747 + }
3748 + ${filter?.nodeIds === undefined ? sql`` : sql`AND node.node_id IN (SELECT value FROM json_each(${encodeIdList(filter.nodeIds)}))`}
3749 ORDER BY COALESCE(node.started_at, ''), node.node_id ASC
3750 `
3751 : sql<PayloadRow & { readonly position: number }>`
@@ -3737,22 +3773,22 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3773 ${filter?.subagentIds === undefined ? sql`` : sql`AND subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentIds)}))`}
3774 ${filter?.subagentRunIds === undefined ? sql`` : sql`AND run_id IN (SELECT value FROM json_each(${encodeIdList(filter.subagentRunIds)}))`}
3775 ${
3740 - filter?.omitSettledSubagents !== undefined
3741 - ? sql`AND (${
3742 - filter.omitSettledSubagents === true
3743 - ? sql`0`
3744 - : sql`subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.omitSettledSubagents.except)}))`
3745 - } OR NOT (
3746 - subagent.status IN ('completed', 'failed', 'cancelled', 'interrupted')
3747 - AND COALESCE(subagent.completion_delivery_state, '')
3748 - IN ('acknowledged', 'delivered', 'disposed')
3749 - AND NOT EXISTS (
3750 - SELECT 1 FROM orchestration_v2_projection_runs AS run
3751 - WHERE run.run_id = subagent.run_id
3752 - AND run.status IN ('preparing', 'queued', 'starting', 'running', 'waiting')
3753 - )
3754 - ))`
3755 - : sql``
3776 + filter?.omitSettledSubagents === true
3777 + ? // The settled index holds every column this check reads, so the settled
3778 + // tasks are dropped there and their rows, each with a full prompt, stay unread.
3779 + sql`AND subagent.rowid IN (
3780 + SELECT candidate.rowid
3781 + FROM orchestration_v2_projection_subagents AS candidate
3782 + INDEXED BY orchestration_v2_projection_subagents_settled_idx
3783 + WHERE candidate.thread_id = ${threadId}
3784 + AND NOT ${settledSubagent(sql`candidate`)}
3785 + )`
3786 + : filter?.omitSettledSubagents !== undefined
3787 + ? sql`AND (
3788 + subagent_id IN (SELECT value FROM json_each(${encodeIdList(filter.omitSettledSubagents.except)}))
3789 + OR NOT ${settledSubagent(sql`subagent`)}
3790 + )`
3791 + : sql``
3792 }
3793 ORDER BY COALESCE(started_at, ''), subagent_id ASC
3794 `
@@ -3984,8 +4020,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4020 ? sql<PayloadRow>`
4021 SELECT payload_json
4022 FROM orchestration_v2_projection_context_transfers
3987 - WHERE (source_thread_id = ${threadId} OR target_thread_id = ${threadId})
3988 - ${filter?.contextTransferStatuses === undefined ? sql`` : sql`AND status IN (SELECT value FROM json_each(${encodeIdList(filter.contextTransferStatuses)}))`}
4023 + WHERE ${
4024 + filter?.contextTransferStatuses === undefined
4025 + ? sql`(source_thread_id = ${threadId} OR target_thread_id = ${threadId})`
4026 + : // Each side seeks its (thread, status) index. One OR over both sides checks the
4027 + // status on every row the thread ever sent or received.
4028 + sql`rowid IN (
4029 + SELECT rowid FROM orchestration_v2_projection_context_transfers
4030 + WHERE source_thread_id = ${threadId}
4031 + AND status IN (SELECT value FROM json_each(${encodeIdList(filter.contextTransferStatuses)}))
4032 + UNION
4033 + SELECT rowid FROM orchestration_v2_projection_context_transfers
4034 + WHERE target_thread_id = ${threadId}
4035 + AND status IN (SELECT value FROM json_each(${encodeIdList(filter.contextTransferStatuses)}))
4036 + )`
4037 + }
4038 ORDER BY rowid ASC
4039 `
4040 : sql<PayloadRow>`
@@ -4031,7 +4080,24 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4080 decodeRows(decodeContextTransferPayload, threadId)(contextTransferRows),
4081 ]);
4082 const messageOrdinals = new Map<string, number>();
4034 - if (fields !== undefined && fields.includes("messages") && messages.length > 1) {
4083 + // The literal `type = 'user_message'` lets SQLite pick the partial user-message index.
4084 + const userOnly = messages.every((message) => message.role === "user");
4085 + // The loaded items already order the messages when they hold every item the lookup reads.
4086 + const loadsEveryItemOf = (type: OrchestrationV2TurnItem["type"]) =>
4087 + window === undefined &&
4088 + (fields === undefined || fields.includes("turnItems")) &&
4089 + filter?.turnItemRequestIds === undefined &&
4090 + filter?.turnItemStatuses === undefined &&
4091 + filter?.turnItemRunId === undefined &&
4092 + filter?.turnItemRunIds === undefined &&
4093 + filter?.turnItemNodeIds === undefined &&
4094 + (filter?.turnItemTypes === undefined || filter.turnItemTypes.includes(type));
4095 + if (
4096 + fields !== undefined &&
4097 + fields.includes("messages") &&
4098 + messages.length > 1 &&
4099 + !(loadsEveryItemOf("user_message") && (userOnly || loadsEveryItemOf("assistant_message")))
4100 + ) {
4101 // Broad role reads keep the original query to avoid per-ID lookup overhead.
4102 const loadedMessageIds =
4103 filter?.messageIds === undefined &&
@@ -4040,8 +4106,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4106 (filter.messageRoles.includes("user") && filter.messageRoles.includes("assistant")))
4107 ? undefined
4108 : messages.map((message) => message.id);
4043 - // The literal `type = 'user_message'` lets SQLite pick the partial user-message index.
4044 - const userOnly = messages.every((message) => message.role === "user");
4109 const rows = yield* sql<{ message_id: string; ordinal: number }>`
4110 SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal
4111 FROM orchestration_v2_projection_turn_items
apps/server/src/persistence/Migrations.ts
+2
@@ -85,6 +85,7 @@ import Migration0079 from "./Migrations/079_DelegatedRecoveryIndexes.ts";
85 import Migration0080 from "./Migrations/080_ScheduledTaskThreadMode.ts";
86 import Migration0081 from "./Migrations/081_ProjectionMessageSearchText.ts";
87 import Migration0082 from "./Migrations/082_TurnItemConversationIndex.ts";
88 +import Migration0083 from "./Migrations/083_CommandReadIndexes.ts";
89
90 const migrationEntries = [
91 [1, "OrchestrationEvents", Migration0001],
@@ -170,6 +171,7 @@ const migrationEntries = [
171 [80, "ScheduledTaskThreadMode", Migration0080],
172 [81, "ProjectionMessageSearchText", Migration0081],
173 [82, "TurnItemConversationIndex", Migration0082],
174 + [83, "CommandReadIndexes", Migration0083],
175 ] as const;
176
177 export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/083_CommandReadIndexes.ts new
+27
@@ -0,0 +1,27 @@
1 +import * as Effect from "effect/Effect";
2 +import * as SqlClient from "effect/sql/SqlClient";
3 +
4 +export default Effect.gen(function* () {
5 + const sql = yield* SqlClient.SqlClient;
6 +
7 + // Every command reads its thread's nodes without the settled leaves. This index lets that read
8 + // seek the nodes it keeps instead of fetching each finished tool call to drop it. The
9 + // omitSettledLeafNodes read in ProjectionStore must spell this expression the same way.
10 + yield* sql`
11 + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_nodes_settled_leaf_idx
12 + ON orchestration_v2_projection_nodes(
13 + thread_id,
14 + (kind IN ('tool_call', 'assistant_message', 'reasoning')
15 + AND status IN ('completed', 'interrupted', 'failed', 'cancelled', 'rolled_back')
16 + AND run_id IS NOT NULL)
17 + )
18 + `;
19 +
20 + // A dispatcher thread is the source of thousands of consumed transfers. With status in the
21 + // index, a read of the pending ones no longer fetches each consumed row to check it.
22 + yield* sql`DROP INDEX IF EXISTS orchestration_v2_projection_context_transfers_source_thread_idx`;
23 + yield* sql`
24 + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_context_transfers_source_thread_status_idx
25 + ON orchestration_v2_projection_context_transfers(source_thread_id, status)
26 + `;
27 +});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -62,6 +62,7 @@ describe("V2 preview upgrade", () => {
62 [80, "ScheduledTaskThreadMode"],
63 [81, "ProjectionMessageSearchText"],
64 [82, "TurnItemConversationIndex"],
65 + [83, "CommandReadIndexes"],
66 ]);
67 assert.deepStrictEqual(yield* runMigrations(), []);
68 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -167,6 +168,7 @@ describe("V2 preview upgrade", () => {
168 [80, "ScheduledTaskThreadMode"],
169 [81, "ProjectionMessageSearchText"],
170 [82, "TurnItemConversationIndex"],
171 + [83, "CommandReadIndexes"],
172 ]);
173 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
174 );