@devries-cameron20 / t3code-1 / commits / 4f829644f6
Fork 0

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

perf(server): long threads open about 2.5x faster (#34014)

Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Valtrizt Khalifah Warri committed Nov 15, 2026 at 15:57 UTC 4f829644f668f7e3ac2ed0351003c7de722ca09d
7 files changed +185 -38
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+79 -21
@@ -44,6 +44,7 @@ import * as Tracer from "effect/Tracer";
44 import * as SqlClient from "effect/sql/SqlClient";
45 import * as Statement from "effect/sql/Statement";
46 import { projectThreadAwarenessV2 } from "@t3tools/shared/agentAwareness";
47 +import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp";
48
49 import * as SqlitePersistence from "../persistence/Sqlite.ts";
50 import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
@@ -4327,13 +4328,13 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4328 ordinalsFrom(915, 75),
4329 );
4330 assert.strictEqual(decodeThreadHistoryCursor(page.nextCursor!).p, 914);
4330 - // A jump to an entry reads the full index and leaves it cached. With work, it scans
4331 - // payloads for inline output only in its page's runs, not in a run after its cursor.
4331 + // A jump to an entry reads the full index and leaves it cached. With work, it looks for
4332 + // inline output only in its page's runs, not in a run after its cursor.
4333 const scannedRuns: Array<string> = [];
4334 yield* read("turn-item:bounded-sql-history:950").pipe(
4335 Effect.provideService(Statement.CurrentTransformer, (statement) => {
4336 const [query, params] = statement.compile();
4336 - if (query.includes("LIKE '%htmlRender%'")) scannedRuns.push(String(params[0]));
4337 + if (query.includes("item.inline_work_output = 1")) scannedRuns.push(String(params[0]));
4338 return Effect.succeed(statement);
4339 }),
4340 );
@@ -4558,17 +4559,49 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4559 workCursor = page.nextCursor;
4560 }
4561 assert.strictEqual(workIds.size, 999);
4561 - for (const [toolName, output] of [
4562 - ["t3-code.delegate_task", null],
4563 - ["mcp__t3_code__delegate_task", null],
4564 - ["mcp__t3-code__send_to_user", null],
4562 + const secondItemId = TurnItemId.make("turn-item:bounded-sql-history:2");
4563 + const secondItem = {
4564 + id: secondItemId,
4565 + threadId,
4566 + runId: (yield* projectionStore.getTurnItem({ threadId, itemId: secondItemId }))!.runId,
4567 + nodeId: null,
4568 + providerThreadId: null,
4569 + providerTurnId: null,
4570 + nativeItemRef: null,
4571 + parentItemId: null,
4572 + ordinal: 2,
4573 + status: "completed" as const,
4574 + title: "command 2",
4575 + startedAt: now,
4576 + completedAt: now,
4577 + updatedAt: now,
4578 + };
4579 + const writeSecondItem = (item: OrchestrationV2TurnItem) =>
4580 + projectionStore.apply({
4581 + id: EventId.make(`event:bounded-sql-history:item-2:${item.type}`),
4582 + type: "turn-item.updated",
4583 + threadId,
4584 + runId: secondItem.runId ?? undefined,
4585 + driver,
4586 + occurredAt: now,
4587 + payload: item,
4588 + });
4589 + for (const inline of [
4590 + { toolName: "t3-code.delegate_task" },
4591 + { toolName: "mcp__t3_code__delegate_task" },
4592 + { toolName: "mcp__docs__search", output: { next: "Delegate-Task" } },
4593 + { toolName: "view_image", viewedImagePath: "/repo/screenshot.png" },
4594 + { toolName: "mcp__docs__chart", output: { [MCP_APP_OUTPUT_KEY]: { uri: "ui://chart" } } },
4595 + { toolName: "mcp__t3-code__send_to_user" },
4596 // OpenCode Code Mode reports a nested send_to_user call as one `execute` item.
4566 - ["execute", JSON.stringify({ sentMessage: "Draft" })],
4567 - ] as const) {
4568 - yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
4569 - payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', ${toolName},
4570 - '$.output', ${output})
4571 - WHERE thread_id = ${threadId} AND ordinal = 2`;
4597 + { toolName: "execute", output: JSON.stringify({ sentMessage: "Draft" }) },
4598 + ]) {
4599 + yield* writeSecondItem({
4600 + ...secondItem,
4601 + type: "dynamic_tool",
4602 + input: {},
4603 + ...inline,
4604 + });
4605 const window = yield* projectionStore.getThreadSnapshotWindow(threadId, {
4606 rowLimit: sqlPageLimit,
4607 userTurnLimit: 10,
@@ -4590,8 +4623,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4623 });
4624 const page = yield* historyPage(threadId, newestCursor, undefined, false, "deferred");
4625 assert.isUndefined(page.items.find((row) => row.item.ordinal === 2)?.item.deferredWork);
4593 - // Tool payloads hold megabytes, so a page that reads the full index, as a jump to an
4594 - // entry does, looks for inline output only in its own runs.
4626 + // A page that reads the full index, as a jump to an entry does, looks for inline output
4627 + // only in its own runs.
4628 const pageRead = traceSqlStatements();
4629 yield* historyPage(
4630 threadId,
@@ -4601,15 +4634,15 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4634 "deferred",
4635 ).pipe(Effect.withTracer(pageRead.tracer));
4636 const payloadScans = pageRead.statements.filter((statement) =>
4604 - statement.includes("LIKE '%htmlRender%'"),
4637 + statement.includes("item.inline_work_output = 1"),
4638 );
4639 assert.lengthOf(payloadScans, 1);
4640 assert.include(payloadScans[0], "item.run_id IS json_extract(wanted.value, '$.id')");
4641 }
4642 // Until its run is scanned, a cursor on inline output is hidden inside a work group.
4643 yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
4611 - payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task')
4612 - WHERE thread_id = ${threadId} AND ordinal = 4`;
4644 + payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task'),
4645 + inline_work_output = 1 WHERE thread_id = ${threadId} AND ordinal = 4`;
4646 const sharedRunId = "run:bounded-sql-history:1";
4647 // A work page of rows that have no run leaves their inline output out too.
4648 yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = NULL
@@ -4670,8 +4703,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4703 '$.runId', CASE WHEN ordinal <= 700 THEN 'run:bounded-sql-history:300' ELSE ${sharedRunId} END)
4704 WHERE thread_id = ${threadId} AND ordinal >= 100`;
4705 yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
4673 - payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task')
4674 - WHERE thread_id = ${threadId} AND ordinal = 720`;
4706 + payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', 't3-code.delegate_task'),
4707 + inline_work_output = 1 WHERE thread_id = ${threadId} AND ordinal = 720`;
4708 const beforeMissingCursor = yield* historyPage(
4709 threadId,
4710 encodeThreadHistoryCursor({
@@ -4690,7 +4723,15 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4723 payload_json = json_set(payload_json, '$.status', 'completed', '$.runId', ${sharedRunId})
4724 WHERE thread_id = ${threadId} AND ordinal >= 100`;
4725 yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
4693 - payload_json = json_set(payload_json, '$.type', 'command_execution') WHERE thread_id = ${threadId} AND ordinal IN (2, 4, 720)`;
4726 + payload_json = json_set(payload_json, '$.type', 'command_execution'), inline_work_output = 0
4727 + WHERE thread_id = ${threadId} AND ordinal IN (4, 720)`;
4728 + yield* writeSecondItem({
4729 + ...secondItem,
4730 + type: "command_execution",
4731 + input: "echo 2",
4732 + output: "ok",
4733 + exitCode: 0,
4734 + });
4735 for (const [ordinal, status] of [
4736 [0, "completed"],
4737 [1, "superseded"],
@@ -4727,6 +4768,23 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4768 });
4769 const oldWork = window.projection.visibleTurnItems.find((row) => row.item.deferredWork);
4770 assert.strictEqual(oldWork?.item.deferredWork?.itemCount, 987);
4771 + // A jump to an entry reads the full index, which folds the same group.
4772 + const jump = yield* historyPage(
4773 + threadId,
4774 + encodeThreadHistoryCursor({
4775 + snapshotSequence: 0,
4776 + sourceThreadId: threadId,
4777 + sourceItemId: "turn-item:bounded-sql-history:1000",
4778 + position: 1000,
4779 + }),
4780 + oldWork!.sourceItemId,
4781 + false,
4782 + "deferred",
4783 + );
4784 + assert.strictEqual(
4785 + jump.items.find((row) => row.item.deferredWork)?.item.deferredWork?.itemCount,
4786 + 987,
4787 + );
4788 let cursor: string | null = oldWork!.item.deferredWork!.cursor;
4789 const loaded = new Set<TurnItemId>();
4790 for (let pages = 0; cursor !== null; pages += 1) {
apps/server/src/orchestration-v2/ProjectionStore.ts
+31 -16
@@ -2178,18 +2178,31 @@ function shellFromState(input: {
2178 };
2179 }
2180
2181 +// Dynamic tool output the client renders inline (HTML, MCP apps, viewed images, delegated
2182 +// tasks, messages sent to the user), so its work group never defers. Mirrors the LIKE backfill
2183 +// in migration 095, where a tool name counts anywhere after the first toolName. Searching from
2184 +// that first one once avoids rescanning the payload for every nested toolName.
2185 +const INLINE_WORK_OUTPUT = new RegExp(
2186 + `htmlRender|${MCP_APP_OUTPUT_KEY}|"viewedImagePath"|sentMessage`,
2187 + "i",
2188 +);
2189 +const TOOL_NAME_PREFIX = /"toolName":"/i;
2190 +const INLINE_TOOL_NAME = /(?:delegate.task|send.to.user)"/is;
2191 +
2192 +const isInlineWorkOutput = (payloadJson: string) => {
2193 + if (INLINE_WORK_OUTPUT.test(payloadJson)) return true;
2194 + const toolName = TOOL_NAME_PREFIX.exec(payloadJson);
2195 + return (
2196 + toolName !== null &&
2197 + INLINE_TOOL_NAME.test(payloadJson.slice(toolName.index + toolName[0].length))
2198 + );
2199 +};
2200 +
2201 export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = Layer.effect(
2202 ProjectionStoreV2,
2203 Effect.gen(function* () {
2204 const sql = yield* SqlClient.SqlClient;
2205
2186 - const inlineWorkOutput = sql`item.type = 'dynamic_tool' AND (
2187 - item.payload_json LIKE '%htmlRender%' OR item.payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`}
2188 - OR item.payload_json LIKE '%"viewedImagePath"%'
2189 - OR item.payload_json LIKE '%"toolName":"%delegate_task"%'
2190 - OR item.payload_json LIKE '%"toolName":"%send_to_user"%'
2191 - OR item.payload_json LIKE '%sentMessage%')`;
2192 -
2206 // SQLite uses the partial conversation index of migration 082 only for this exact predicate.
2207 const CONVERSATION_INDEX_EXCLUDED_TYPES = sql.literal(
2208 `(${DEFERRED_WORK_ITEM_TYPES.map((type) => `'${type}'`).join(", ")})`,
@@ -2939,7 +2952,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2952 type,
2953 status,
2954 updated_at,
2942 - payload_json
2955 + payload_json,
2956 + inline_work_output
2957 )
2958 VALUES (
2959 ${event.payload.id},
@@ -2953,7 +2967,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2967 ${event.payload.type},
2968 ${event.payload.status},
2969 ${updatedAt},
2956 - ${payloadJson}
2970 + ${payloadJson},
2971 + ${event.payload.type === "dynamic_tool" && isInlineWorkOutput(payloadJson) ? 1 : 0}
2972 )
2973 ON CONFLICT(turn_item_id)
2974 DO UPDATE SET
@@ -2967,7 +2982,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2982 type = excluded.type,
2983 status = excluded.status,
2984 updated_at = excluded.updated_at,
2970 - payload_json = excluded.payload_json
2985 + payload_json = excluded.payload_json,
2986 + inline_work_output = excluded.inline_work_output
2987 `;
2988 break;
2989 }
@@ -3189,7 +3205,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3205 const eligible = sql`
3206 WITH eligible AS NOT MATERIALIZED (
3207 SELECT item.rowid AS item_rowid, item.payload_json, item.ordinal, item.turn_item_id,
3192 - item.run_id, item.node_id, item.type, item.status, run.status AS run_status,
3208 + item.run_id, item.node_id, item.type, item.status, item.inline_work_output,
3209 + run.status AS run_status,
3210 attempt.attempt_id, attempt.status AS attempt_status
3211 FROM orchestration_v2_projection_turn_items AS item
3212 LEFT JOIN orchestration_v2_projection_runs AS run
@@ -3480,7 +3497,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3497 OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
3498 CASE WHEN status = 'completed' AND (run_status = 'completed' OR attempt_status = 'superseded')
3499 AND type IN ${sql.in(DEFERRED_WORK_ITEM_TYPES)}
3483 - AND NOT (${inlineWorkOutput})
3500 + AND inline_work_output = 0
3501 AND run_id NOT IN (SELECT run_id FROM blocked_runs)
3502 THEN 1 ELSE 0 END AS can_defer
3503 FROM eligible AS item
@@ -6757,9 +6774,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6774 }>`SELECT item.turn_item_id, item.run_id, item.node_id, item.parent_item_id, item.type, item.status, item.ordinal,
6775 ${includePersistentOutput ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id,
6776 ${includePersistentOutput ? sql`attempt.status` : sql`NULL`} AS attempt_status,
6760 - ${
6761 - includePersistentOutput ? sql`CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END` : sql`0`
6762 - } AS persistent_output,
6777 + ${includePersistentOutput ? sql`item.inline_work_output` : sql`0`} AS persistent_output,
6778 CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.inputIntent') END AS input_intent,
6779 CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.createdBy') END AS created_by,
6780 CASE WHEN item.type IN ('user_message', 'assistant_message') THEN json_extract(item.payload_json, '$.messageId') END AS message_id
@@ -7313,7 +7328,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7328 CROSS JOIN orchestration_v2_projection_turn_items AS item
7329 ON item.thread_id = json_extract(wanted.value, '$.threadId')
7330 AND item.run_id IS json_extract(wanted.value, '$.id')
7316 - WHERE item.status = 'completed' AND ${inlineWorkOutput}`;
7331 + WHERE item.status = 'completed' AND item.inline_work_output = 1`;
7332 if (found.length === 0) break;
7333 for (const row of found)
7334 persistentOutputs.add(`${row.thread_id}:${row.turn_item_id}`);
apps/server/src/persistence/Migrations.ts
+2
@@ -102,6 +102,7 @@ import Migration0091 from "./Migrations/091_AuthConnectDeniedRequests.ts";
102 import Migration0092 from "./Migrations/092_QueuedTurnInputIndexJsonValid.ts";
103 import Migration0093 from "./Migrations/093_ProviderTurnAttemptIndex.ts";
104 import Migration0094 from "./Migrations/094_TurnItemMessageOrdinalIndex.ts";
105 +import Migration0095 from "./Migrations/095_TurnItemInlineWorkOutput.ts";
106
107 const migrationEntries = [
108 [1, "OrchestrationEvents", Migration0001],
@@ -199,6 +200,7 @@ const migrationEntries = [
200 [92, "QueuedTurnInputIndexJsonValid", Migration0092],
201 [93, "ProviderTurnAttemptIndex", Migration0093],
202 [94, "TurnItemMessageOrdinalIndex", Migration0094],
203 + [95, "TurnItemInlineWorkOutput", Migration0095],
204 ] as const;
205
206 export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const);
apps/server/src/persistence/Migrations/094_TurnItemMessageOrdinalIndex.test.ts
+1 -1
@@ -43,7 +43,7 @@ it.effect("migrates over a message whose payload is not JSON, and takes one afte
43 yield* insertTurnItem("ok-1", "thread-a", 2, "user_message", '{"messageId":"m1"}');
44
45 assert.deepEqual(
46 - (yield* runMigrations()).map(([id]) => id),
46 + (yield* runMigrations({ toMigrationInclusive: 94 })).map(([id]) => id),
47 [94],
48 );
49 yield* insertTurnItem("bad-2", "thread-a", 3, "user_message", "{not json");
apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.test.ts new
+47
@@ -0,0 +1,47 @@
1 +import { assert, it } from "@effect/vitest";
2 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 +import * as Effect from "effect/Effect";
4 +import * as SqlClient from "effect/sql/SqlClient";
5 +
6 +import { runMigrations } from "../Migrations.ts";
7 +
8 +const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" }));
9 +
10 +layer("095_TurnItemInlineWorkOutput", (it) => {
11 + it.effect("flags existing dynamic tools whose output renders inline", () =>
12 + Effect.gen(function* () {
13 + const sql = yield* SqlClient.SqlClient;
14 + yield* runMigrations({ toMigrationInclusive: 94 });
15 + const rows: ReadonlyArray<readonly [string, string, object, number]> = [
16 + ["delegate", "dynamic_tool", { toolName: "mcp__t3_code__delegate_task" }, 1],
17 + ["html", "dynamic_tool", { toolName: "x", output: { htmlRender: {} } }, 1],
18 + ["mcp-app", "dynamic_tool", { toolName: "x", output: { t3McpApp: {} } }, 1],
19 + ["image", "dynamic_tool", { toolName: "view_image", viewedImagePath: "/a.png" }, 1],
20 + ["send", "dynamic_tool", { toolName: "mcp__t3-code__send_to_user" }, 1],
21 + ["sent", "dynamic_tool", { toolName: "execute", output: '{"sentMessage":"Hi"}' }, 1],
22 + ["plain", "dynamic_tool", { toolName: "mcp__docs__search", output: "ok" }, 0],
23 + ["command", "command_execution", { output: "htmlRender" }, 0],
24 + ];
25 + for (const [id, type, payload] of rows) {
26 + yield* sql`
27 + INSERT INTO orchestration_v2_projection_turn_items (
28 + turn_item_id, thread_id, ordinal, type, status, updated_at, payload_json
29 + ) VALUES (
30 + ${id}, 'thread-1', 0, ${type}, 'completed', '2026-11-01T00:00:00.000Z',
31 + ${JSON.stringify({ type, ...payload })}
32 + )
33 + `;
34 + }
35 +
36 + yield* runMigrations({ toMigrationInclusive: 95 });
37 + const flagged = yield* sql<{ readonly turn_item_id: string; readonly inline: number }>`
38 + SELECT turn_item_id, inline_work_output AS inline
39 + FROM orchestration_v2_projection_turn_items
40 + `;
41 + assert.deepStrictEqual(
42 + new Map(flagged.map((row) => [row.turn_item_id, row.inline])),
43 + new Map(rows.map(([id, , , inline]) => [id, inline])),
44 + );
45 + }),
46 + );
47 +});
apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.ts new
+23
@@ -0,0 +1,23 @@
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 + // Thread windows read this flag for every row; scanning payloads on each read
8 + // dominated thread open. Keep the predicate in step with ProjectionStore.
9 + yield* sql`
10 + ALTER TABLE orchestration_v2_projection_turn_items
11 + ADD COLUMN inline_work_output INTEGER NOT NULL DEFAULT 0
12 + `;
13 + yield* sql`
14 + UPDATE orchestration_v2_projection_turn_items
15 + SET inline_work_output = 1
16 + WHERE type = 'dynamic_tool' AND (
17 + payload_json LIKE '%htmlRender%' OR payload_json LIKE '%t3McpApp%'
18 + OR payload_json LIKE '%"viewedImagePath"%'
19 + OR payload_json LIKE '%"toolName":"%delegate_task"%'
20 + OR payload_json LIKE '%"toolName":"%send_to_user"%'
21 + OR payload_json LIKE '%sentMessage%')
22 + `;
23 +});
apps/server/src/persistence/reconcileV2PreviewMigration.test.ts
+2
@@ -74,6 +74,7 @@ describe("V2 preview upgrade", () => {
74 [92, "QueuedTurnInputIndexJsonValid"],
75 [93, "ProviderTurnAttemptIndex"],
76 [94, "TurnItemMessageOrdinalIndex"],
77 + [95, "TurnItemInlineWorkOutput"],
78 ]);
79 assert.deepStrictEqual(yield* runMigrations(), []);
80 assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports);
@@ -191,6 +192,7 @@ describe("V2 preview upgrade", () => {
192 [92, "QueuedTurnInputIndexJsonValid"],
193 [93, "ProviderTurnAttemptIndex"],
194 [94, "TurnItemMessageOrdinalIndex"],
195 + [95, "TurnItemInlineWorkOutput"],
196 ]);
197 }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
198 );