From 4e4554e97146f3144ee6d362645936cf08d4f332 Mon Sep 17 00:00:00 2001 From: Yuruzuu Date: Wed, 11 Nov 2026 00:27:16 +0800 Subject: [PATCH 1/3] perf(server): Load earlier turns is 2.0x faster for history written after this change (median 503 -> 247 ms) Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/ProjectionStore.test.ts | 107 ++++++++++++++++++ .../src/orchestration-v2/ProjectionStore.ts | 41 ++++--- apps/server/src/persistence/Migrations.ts | 2 + .../081_TurnItemPersistentOutput.ts | 8 ++ .../reconcileV2PreviewMigration.test.ts | 2 + 5 files changed, 143 insertions(+), 17 deletions(-) create mode 100644 apps/server/src/persistence/Migrations/081_TurnItemPersistentOutput.ts diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index de54a97ebebf8..6ffc9176c7c8a 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -52,6 +52,7 @@ import { decodeThreadHistoryCursor, encodeThreadHistoryCursor, selectHistoryPageFromCursor, + THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT, THREAD_HISTORY_PAGE_POLICY, THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT, } from "./threadHistoryPaging.ts"; @@ -2582,6 +2583,112 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }), ); + it.effect("stores completed inline-output flags that page like the payload predicate", () => + Effect.gen(function* () { + const store = yield* ProjectionStore.ProjectionStoreV2; + const sql = yield* SqlClient.SqlClient; + const threadId = yield* addRolledBackRecoveryCandidate("inline-output-flag"); + const run = (yield* store.getThreadProjection(threadId)).runs[0]!; + const now = yield* DateTime.now; + yield* store.apply({ + id: EventId.make("event:inline-output-flag:run-completed"), + type: "run.updated", + threadId, + runId: run.id, + providerInstanceId, + occurredAt: now, + payload: { ...run, status: "completed", completedAt: now }, + }); + const base = { + threadId, + runId: run.id, + nodeId: null, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + title: null, + startedAt: now, + completedAt: now, + updatedAt: now, + }; + const tool = (input: unknown, toolName = "lookup") => ({ + ...base, + type: "dynamic_tool" as const, + toolName, + input, + }); + const cases = [ + ["plain", tool({ q: "x" }), 0], + ["html", tool({ htmlRender: true }), 1], + ["html-case", tool({ HTMLRENDER: true }), 1], + ["app", tool({ t3McpApp: {} }), 1], + ["image", { ...tool({}), viewedImagePath: "/tmp/a.png" }, 1], + ["delegate", tool({}, "mcp__t3_code__delegate_task"), 1], + ["delegate-like", tool({}, "t3-code.delegate-task"), 1], + ["running", { ...tool({ htmlRender: true }), status: "running" as const }, null], + ["command", { ...base, type: "command_execution" as const, input: "htmlRender" }, 0], + ] as const; + const write = (name: string, item: (typeof cases)[number][1], ordinal: number) => + store.apply({ + id: EventId.make(`event:inline-output-flag:${name}:${ordinal}`), + type: "turn-item.updated", + threadId, + runId: run.id, + occurredAt: now, + payload: { + status: "completed", + ...item, + id: TurnItemId.make(`item:inline-output-flag:${name}`), + ordinal, + }, + }); + for (const [index, [name, item]] of cases.entries()) yield* write(name, item, index + 2); + const flags = () => + sql<{ readonly id: string; readonly flag: number | null }>` + SELECT turn_item_id AS id, persistent_output AS flag + FROM orchestration_v2_projection_turn_items + WHERE thread_id = ${threadId} AND ordinal > 1 + ORDER BY ordinal`; + assert.deepEqual( + yield* flags(), + cases.map(([name, , flag]) => ({ id: `item:inline-output-flag:${name}`, flag })), + ); + + yield* write("html", tool({ q: "x" }), 3); + yield* write("plain", { ...tool({ htmlRender: true }), status: "running" }, 2); + yield* write("running", tool({ htmlRender: true }), 9); + const updated = new Map((yield* flags()).map((row) => [row.id, row.flag])); + assert.strictEqual(updated.get("item:inline-output-flag:html"), 0); + assert.isNull(updated.get("item:inline-output-flag:plain")); + assert.strictEqual(updated.get("item:inline-output-flag:running"), 1); + for (let index = 0; index < THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT; index += 1) + yield* write(`bulk-${index}`, tool({ index }), 20 + index); + + const page = () => + store.getThreadHistoryPage( + threadId, + encodeThreadHistoryCursor({ + snapshotSequence: 0, + sourceThreadId: threadId, + sourceItemId: "item:inline-output-flag:end", + position: 1_000, + }), + undefined, + false, + "deferred", + ); + const stored = yield* page(); + assert.isTrue(stored.items.some((row) => row.item.deferredWork !== undefined)); + assert.isTrue( + stored.items.some((row) => String(row.sourceItemId) === "item:inline-output-flag:app"), + ); + yield* sql`UPDATE orchestration_v2_projection_turn_items SET persistent_output = NULL + WHERE thread_id = ${threadId}`; + assert.deepEqual(yield* page(), stored); + }), + ); + it.effect( "SQL recovery selects unfinished runless native subagents", () => runlessNativeSubagentNeedsRecovery, diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 7c7bbecafe5a0..e845f7b42cda3 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -2643,22 +2643,26 @@ export const layer: Layer.Layer = type, status, updated_at, - payload_json - ) - VALUES ( - ${event.payload.id}, - ${event.payload.threadId}, - ${event.payload.runId}, - ${event.payload.nodeId}, - ${event.payload.providerThreadId}, - ${event.payload.providerTurnId}, - ${event.payload.parentItemId}, - ${event.payload.ordinal}, - ${event.payload.type}, - ${event.payload.status}, - ${updatedAt}, - ${payloadJson} + payload_json, + persistent_output ) + SELECT item.*, CASE WHEN item.status = 'completed' + THEN CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END END + FROM (SELECT + ${event.payload.id} AS turn_item_id, + ${event.payload.threadId} AS thread_id, + ${event.payload.runId} AS run_id, + ${event.payload.nodeId} AS node_id, + ${event.payload.providerThreadId} AS provider_thread_id, + ${event.payload.providerTurnId} AS provider_turn_id, + ${event.payload.parentItemId} AS parent_item_id, + ${event.payload.ordinal} AS ordinal, + ${event.payload.type} AS type, + ${event.payload.status} AS status, + ${updatedAt} AS updated_at, + ${payloadJson} AS payload_json + ) AS item + WHERE true ON CONFLICT(turn_item_id) DO UPDATE SET thread_id = excluded.thread_id, @@ -2671,7 +2675,8 @@ export const layer: Layer.Layer = type = excluded.type, status = excluded.status, updated_at = excluded.updated_at, - payload_json = excluded.payload_json + payload_json = excluded.payload_json, + persistent_output = excluded.persistent_output `; break; } @@ -5802,7 +5807,9 @@ export const layer: Layer.Layer = ${includePersistentOutput ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id, ${includePersistentOutput ? sql`attempt.status` : sql`NULL`} AS attempt_status, ${ - includePersistentOutput ? sql`CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END` : sql`0` + includePersistentOutput + ? sql`COALESCE(item.persistent_output, CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END)` + : sql`0` } AS persistent_output, CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.inputIntent') END AS input_intent, CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.createdBy') END AS created_by, diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index 6a9c57e18cc43..14fae54a95c9b 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -83,6 +83,7 @@ import Migration0077 from "./Migrations/077_RepairBooleanSettledOverride.ts"; import Migration0078 from "./Migrations/078_DropOrchestrationEventCorrelationIndex.ts"; import Migration0079 from "./Migrations/079_DelegatedRecoveryIndexes.ts"; import Migration0080 from "./Migrations/080_ScheduledTaskThreadMode.ts"; +import Migration0081 from "./Migrations/081_TurnItemPersistentOutput.ts"; const migrationEntries = [ [1, "OrchestrationEvents", Migration0001], @@ -166,6 +167,7 @@ const migrationEntries = [ [78, "DropOrchestrationEventCorrelationIndex", Migration0078], [79, "DelegatedRecoveryIndexes", Migration0079], [80, "ScheduledTaskThreadMode", Migration0080], + [81, "TurnItemPersistentOutput", Migration0081], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/081_TurnItemPersistentOutput.ts b/apps/server/src/persistence/Migrations/081_TurnItemPersistentOutput.ts new file mode 100644 index 0000000000000..fee9cc5be3488 --- /dev/null +++ b/apps/server/src/persistence/Migrations/081_TurnItemPersistentOutput.ts @@ -0,0 +1,8 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +// Null means not yet derived; readers fall back to the payload predicate, so no backfill. +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + yield* sql`ALTER TABLE orchestration_v2_projection_turn_items ADD COLUMN persistent_output INTEGER`; +}); diff --git a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts index 84fa4ddb44c77..8b5da59c55b97 100644 --- a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts +++ b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts @@ -60,6 +60,7 @@ describe("V2 preview upgrade", () => { [78, "DropOrchestrationEventCorrelationIndex"], [79, "DelegatedRecoveryIndexes"], [80, "ScheduledTaskThreadMode"], + [81, "TurnItemPersistentOutput"], ]); assert.deepStrictEqual(yield* runMigrations(), []); assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports); @@ -163,6 +164,7 @@ describe("V2 preview upgrade", () => { [78, "DropOrchestrationEventCorrelationIndex"], [79, "DelegatedRecoveryIndexes"], [80, "ScheduledTaskThreadMode"], + [81, "TurnItemPersistentOutput"], ]); }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), ); From 580c968be397a1ca0bf11cdf7f81009928b326ef Mon Sep 17 00:00:00 2001 From: Yuruzuu Date: Wed, 11 Nov 2026 18:14:28 +0800 Subject: [PATCH 2/3] test(server): history paging reads the stored inline-output flag A stored flag of 0 on a row whose payload still matches the predicate now drops that row from the page, so a reader that ignores the flag fails. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/orchestration-v2/ProjectionStore.test.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index f4bea3e00b330..1c075a216949e 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -2799,6 +2799,13 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { assert.isTrue( stored.items.some((row) => String(row.sourceItemId) === "item:inline-output-flag:app"), ); + yield* sql`UPDATE orchestration_v2_projection_turn_items SET persistent_output = 0 + WHERE thread_id = ${threadId} AND turn_item_id = 'item:inline-output-flag:app'`; + assert.isFalse( + (yield* page()).items.some( + (row) => String(row.sourceItemId) === "item:inline-output-flag:app", + ), + ); yield* sql`UPDATE orchestration_v2_projection_turn_items SET persistent_output = NULL WHERE thread_id = ${threadId}`; assert.deepEqual(yield* page(), stored); From 3d786270e9f949e850c24a2967e6fd929b2c8581 Mon Sep 17 00:00:00 2001 From: Yuruzuu Date: Thu, 12 Nov 2026 19:28:09 +0800 Subject: [PATCH 3/3] test(server): record a thread event before each inline-output page read Main now caches timeline indexes until a thread event is written, so the raw SQL flag edits need an event to be seen. Co-Authored-By: Claude Opus 5.5 --- .../orchestration-v2/ProjectionStore.test.ts | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index db6f139b02ac8..35524726951bc 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -3024,18 +3024,30 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { for (let index = 0; index < THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT; index += 1) yield* write(`bulk-${index}`, tool({ index }), 20 + index); + // The raw SQL edits below stand in for committed writes, which always append a thread event. + let revision = 0; const page = () => - store.getThreadHistoryPage( - threadId, - encodeThreadHistoryCursor({ - snapshotSequence: 0, - sourceThreadId: threadId, - sourceItemId: "item:inline-output-flag:end", - position: 1_000, - }), - undefined, - false, - "deferred", + sql`INSERT INTO orchestration_events ( + event_id, aggregate_kind, stream_id, stream_version, event_type, + occurred_at, actor_kind, payload_json, metadata_json, application_event_version + ) VALUES ( + ${`event:inline-output-flag:revision:${++revision}`}, 'thread', ${threadId}, ${revision}, + 'turn-item.updated', ${DateTime.formatIso(now)}, 'user', '{}', '{}', 2 + )`.pipe( + Effect.andThen( + store.getThreadHistoryPage( + threadId, + encodeThreadHistoryCursor({ + snapshotSequence: 0, + sourceThreadId: threadId, + sourceItemId: "item:inline-output-flag:end", + position: 1_000, + }), + undefined, + false, + "deferred", + ), + ), ); const stored = yield* page(); assert.isTrue(stored.items.some((row) => row.item.deferredWork !== undefined)); @@ -3051,7 +3063,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { ); yield* sql`UPDATE orchestration_v2_projection_turn_items SET persistent_output = NULL WHERE thread_id = ${threadId}`; - assert.deepEqual(yield* page(), stored); + assert.deepEqual({ ...(yield* page()), snapshotSequence: stored.snapshotSequence }, stored); }), );