@devries-cameron20 / t3code Mirror · read-only / commits / 8a942c2121
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 22, 03:15 UTC

perf(server): provider-thread reads start from owned rows, not every node of the thread (#31012)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 6, 2026 at 11:54 UTC 8a942c2121c7a2d5b8a164c1ffc247334c62137b
2 files changed +70 -8
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+56
@@ -472,6 +472,62 @@ it.effect("record reads restrict provider histories to delivery identities", ()
472 })).providerThreads,
473 [],
474 );
475 + const ownerNodeId = NodeId.make("node:delivery-identities:owner");
476 + yield* store.apply({
477 + id: EventId.make("event:delivery-identities:owner-node"),
478 + type: "node.updated",
479 + threadId,
480 + runId: RunId.make("run:delivery-identities:rolled-back"),
481 + nodeId: ownerNodeId,
482 + driver,
483 + occurredAt: now,
484 + payload: {
485 + id: ownerNodeId,
486 + threadId,
487 + runId: RunId.make("run:delivery-identities:rolled-back"),
488 + rootNodeId: NodeId.make("node:delivery-identities:rolled-back"),
489 + parentNodeId: NodeId.make("node:delivery-identities:rolled-back"),
490 + kind: "subagent",
491 + status: "completed",
492 + countsForRun: false,
493 + providerThreadId: null,
494 + providerTurnId: null,
495 + nativeItemRef: null,
496 + runtimeRequestId: null,
497 + checkpointScopeId: null,
498 + startedAt: now,
499 + completedAt: now,
500 + },
501 + });
502 + yield* store.apply({
503 + id: EventId.make("event:delivery-identities:owned"),
504 + type: "provider-thread.updated",
505 + threadId,
506 + occurredAt: now,
507 + payload: {
508 + id: ProviderThreadId.make("provider-thread:owned"),
509 + appThreadId: ThreadId.make("thread:delivery-identities:child"),
510 + driver,
511 + providerInstanceId,
512 + providerSessionId: null,
513 + ownerNodeId,
514 + nativeThreadRef: null,
515 + nativeConversationHeadRef: null,
516 + lastRunOrdinal: null,
517 + handoffIds: [],
518 + forkedFrom: null,
519 + status: "idle",
520 + firstRunOrdinal: 2,
521 + createdAt: now,
522 + updatedAt: now,
523 + },
524 + });
525 + assert.deepEqual(
526 + (yield* store.getThreadRecords(threadId, ["providerThreads"])).providerThreads.map(
527 + (row) => row.id,
528 + ),
529 + ["provider-thread:history", "provider-thread:selected", "provider-thread:owned"],
530 + );
531 // Unselected history must not be decoded, even if its payload is unreadable.
532 yield* sql`UPDATE orchestration_v2_projection_provider_threads SET payload_json = '{}'
533 WHERE provider_thread_id = 'provider-thread:history'`;
apps/server/src/orchestration-v2/ProjectionStore.ts
+14 -8
@@ -1979,6 +1979,16 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
1979 OR item.payload_json LIKE '%"viewedImagePath"%'
1980 OR item.payload_json LIKE '%"toolName":"%delegate_task"%')`;
1981
1982 + // Start from the few owned provider threads; a thread can have a large settled node history.
1983 + const nodesOwningProviderThreads = (threadId: ThreadId) => sql`
1984 + SELECT owner.node_id
1985 + FROM orchestration_v2_projection_provider_threads AS owned
1986 + INDEXED BY orchestration_v2_projection_provider_threads_owner_idx
1987 + CROSS JOIN orchestration_v2_projection_nodes AS owner
1988 + ON owner.node_id = owned.owner_node_id
1989 + WHERE owned.owner_node_id IS NOT NULL AND owner.thread_id = ${threadId}
1990 + `;
1991 +
1992 const keepRecordedRunField = (payload: Statement.Fragment, path: string) => sql`
1993 CASE
1994 WHEN json_type(excluded.payload_json, ${path}) IS NULL
@@ -3500,11 +3510,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3510 SELECT payload_json
3511 FROM orchestration_v2_projection_provider_threads
3512 WHERE (thread_id = ${threadId}
3503 - OR owner_node_id IN (
3504 - SELECT node_id
3505 - FROM orchestration_v2_projection_nodes
3506 - WHERE thread_id = ${threadId}
3507 - )
3513 + OR owner_node_id IN (${nodesOwningProviderThreads(threadId)})
3514 OR provider_thread_id IN (
3515 SELECT provider_thread_id
3516 FROM orchestration_v2_projection_subagents
@@ -5085,7 +5091,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5091 sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_provider_threads
5092 WHERE provider_thread_id = ${target.providerThreadId} AND (
5093 thread_id = ${threadId}
5088 - OR owner_node_id IN (SELECT node_id FROM orchestration_v2_projection_nodes WHERE thread_id = ${threadId})
5094 + OR owner_node_id IN (${nodesOwningProviderThreads(threadId)})
5095 OR provider_thread_id IN (SELECT provider_thread_id FROM orchestration_v2_projection_subagents WHERE thread_id = ${threadId})
5096 )`,
5097 sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_provider_turns
@@ -5325,7 +5331,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5331 sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_provider_threads
5332 WHERE provider_thread_id = ${run?.providerThreadId ?? null}
5333 AND (thread_id = ${threadId}
5328 - OR owner_node_id IN (SELECT node_id FROM orchestration_v2_projection_nodes WHERE thread_id = ${threadId})
5334 + OR owner_node_id IN (${nodesOwningProviderThreads(threadId)})
5335 OR provider_thread_id IN (SELECT provider_thread_id FROM orchestration_v2_projection_subagents WHERE thread_id = ${threadId}))`,
5336 sql<{ readonly ordinal: number }>`SELECT ordinal_within_scope AS ordinal
5337 FROM orchestration_v2_projection_checkpoints
@@ -5364,7 +5370,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5370 sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_provider_threads
5371 WHERE provider_thread_id = ${target.providerThreadId}
5372 AND (thread_id = ${threadId}
5367 - OR owner_node_id IN (SELECT node_id FROM orchestration_v2_projection_nodes WHERE thread_id = ${threadId})
5373 + OR owner_node_id IN (${nodesOwningProviderThreads(threadId)})
5374 OR provider_thread_id IN (SELECT provider_thread_id FROM orchestration_v2_projection_subagents WHERE thread_id = ${threadId}))`,
5375 sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_checkpoints
5376 WHERE thread_id = ${threadId} AND checkpoint_id = ${target.checkpointId}`,