GeezerVison is showing github.com — perf(server): provider-thread reads no longer walk a thread's node hi… · pingdotgg/t3code@908a520
File treeExpand file treeCollapse file tree Open diff view settings apps/server/src/orchestration-v2
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.test.ts +170Lines changed: 170 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -484,6 +484,176 @@ it.effect("record reads restrict provider histories to delivery identities", () | 484 | 484 | }).pipe(Effect.provide(layerTest)), | 485 | 485 | ); | 486 | 486 |
| | 487 | +it.effect("provider-thread reads find owned threads without walking the thread's nodes", () => | | 488 | + Effect.gen(function* () { | | 489 | + const store = yield* ProjectionStore.ProjectionStoreV2; | | 490 | + const sql = yield* SqlClient.SqlClient; | | 491 | + const now = yield* DateTime.now; | | 492 | + const threadId = yield* addRolledBackRecoveryCandidate("provider-ownership"); | | 493 | + const otherThreadId = yield* addRolledBackRecoveryCandidate("provider-ownership-other"); | | 494 | + const ownerNodeId = NodeId.make("node:provider-ownership:owner"); | | 495 | + const foreignNodeId = NodeId.make("node:provider-ownership:foreign"); | | 496 | + for (const [nodeId, nodeThreadId] of [ | | 497 | + [ownerNodeId, threadId], | | 498 | + [foreignNodeId, otherThreadId], | | 499 | + ] as const) { | | 500 | + yield* store.apply({ | | 501 | + id: EventId.make(`event:${nodeId}`), | | 502 | + type: "node.updated", | | 503 | + threadId: nodeThreadId, | | 504 | + nodeId, | | 505 | + driver, | | 506 | + occurredAt: now, | | 507 | + payload: { | | 508 | + id: nodeId, | | 509 | + threadId: nodeThreadId, | | 510 | + runId: null, | | 511 | + parentNodeId: null, | | 512 | + rootNodeId: nodeId, | | 513 | + kind: "root_turn", | | 514 | + status: "completed", | | 515 | + countsForRun: false, | | 516 | + providerThreadId: null, | | 517 | + providerTurnId: null, | | 518 | + nativeItemRef: null, | | 519 | + runtimeRequestId: null, | | 520 | + checkpointScopeId: null, | | 521 | + startedAt: now, | | 522 | + completedAt: now, | | 523 | + }, | | 524 | + }); | | 525 | + } | | 526 | + const cases = [ | | 527 | + { name: "direct", appThreadId: threadId, ownerNodeId: null, subagent: false, ordinal: 2 }, | | 528 | + { name: "owner", appThreadId: otherThreadId, ownerNodeId, subagent: false, ordinal: null }, | | 529 | + { name: "owner-only", appThreadId: null, ownerNodeId, subagent: false, ordinal: 1 }, | | 530 | + { | | 531 | + name: "subagent", | | 532 | + appThreadId: otherThreadId, | | 533 | + ownerNodeId: null, | | 534 | + subagent: true, | | 535 | + ordinal: 1, | | 536 | + }, | | 537 | + { name: "duplicate", appThreadId: threadId, ownerNodeId, subagent: true, ordinal: 3 }, | | 538 | + { | | 539 | + name: "foreign-owner", | | 540 | + appThreadId: otherThreadId, | | 541 | + ownerNodeId: foreignNodeId, | | 542 | + subagent: false, | | 543 | + ordinal: 4, | | 544 | + }, | | 545 | + { | | 546 | + name: "unrelated", | | 547 | + appThreadId: otherThreadId, | | 548 | + ownerNodeId: null, | | 549 | + subagent: false, | | 550 | + ordinal: 5, | | 551 | + }, | | 552 | + ] as const; | | 553 | + const idOf = (name: string) => ProviderThreadId.make(`provider-thread:ownership:${name}`); | | 554 | + for (const entry of cases) { | | 555 | + yield* store.apply({ | | 556 | + id: EventId.make(`event:provider-ownership:${entry.name}`), | | 557 | + type: "provider-thread.updated", | | 558 | + threadId, | | 559 | + occurredAt: now, | | 560 | + payload: { | | 561 | + id: idOf(entry.name), | | 562 | + appThreadId: entry.appThreadId, | | 563 | + ownerNodeId: entry.ownerNodeId, | | 564 | + driver, | | 565 | + providerInstanceId, | | 566 | + providerSessionId: null, | | 567 | + nativeThreadRef: null, | | 568 | + nativeConversationHeadRef: null, | | 569 | + status: "idle", | | 570 | + firstRunOrdinal: entry.ordinal, | | 571 | + lastRunOrdinal: entry.ordinal, | | 572 | + handoffIds: [], | | 573 | + forkedFrom: null, | | 574 | + createdAt: now, | | 575 | + updatedAt: now, | | 576 | + }, | | 577 | + }); | | 578 | + if (entry.subagent) { | | 579 | + yield* sql` | | 580 | + INSERT INTO orchestration_v2_projection_subagents | | 581 | + (subagent_id, thread_id, parent_node_id, provider, provider_thread_id, | | 582 | + origin, status, updated_at, payload_json) | | 583 | + VALUES (${`subagent:${entry.name}`}, ${threadId}, ${ownerNodeId}, 'codex', | | 584 | + ${idOf(entry.name)}, 'provider_native', 'completed', '', '{}') | | 585 | + `; | | 586 | + } | | 587 | + } | | 588 | + | | 589 | + const statements: Array<ReturnType<Statement.Statement<unknown>["compile"]>> = []; | | 590 | + const observe: Statement.Transformer = (statement) => { | | 591 | + const compiled = statement.compile(); | | 592 | + if (compiled[0].includes("FROM orchestration_v2_projection_provider_threads")) { | | 593 | + statements.push(compiled); | | 594 | + } | | 595 | + return Effect.succeed(statement); | | 596 | + }; | | 597 | + const observed = <A, E, R>(effect: Effect.Effect<A, E, R>) => | | 598 | + effect.pipe(Effect.provideService(Statement.CurrentTransformer, observe)); | | 599 | + | | 600 | + const records = yield* observed(store.getThreadRecords(threadId, ["providerThreads"])); | | 601 | + assert.deepEqual( | | 602 | + records.providerThreads.map((row) => row.id), | | 603 | + ["owner", "owner-only", "subagent", "direct", "duplicate"].map(idOf), | | 604 | + ); | | 605 | + const filtered = yield* observed( | | 606 | + store.getThreadRecords(threadId, ["providerThreads"], { | | 607 | + providerThreadIds: [idOf("subagent"), idOf("foreign-owner"), idOf("unrelated")], | | 608 | + }), | | 609 | + ); | | 610 | + assert.deepEqual( | | 611 | + filtered.providerThreads.map((row) => row.id), | | 612 | + [idOf("subagent")], | | 613 | + ); | | 614 | + const providerTurnId = ProviderTurnId.make("provider-turn:provider-ownership:missing"); | | 615 | + for (const [name, owned] of [ | | 616 | + ["owner-only", true], | | 617 | + ["foreign-owner", false], | | 618 | + ] as const) { | | 619 | + const control = yield* observed( | | 620 | + store.getProviderControlContext(threadId, { providerThreadId: idOf(name), providerTurnId }), | | 621 | + ); | | 622 | + assert.equal(control.providerThread?.id, owned ? idOf(name) : undefined, name); | | 623 | + } | | 624 | + const rollback = yield* observed( | | 625 | + store.getCheckpointRollbackContext(threadId, { | | 626 | + providerThreadId: idOf("subagent"), | | 627 | + checkpointId: CheckpointId.make("checkpoint:provider-ownership:missing"), | | 628 | + scopeId: CheckpointScopeId.make("scope:provider-ownership:missing"), | | 629 | + }), | | 630 | + ); | | 631 | + assert.equal(rollback.providerThread?.id, idOf("subagent")); | | 632 | + yield* observed( | | 633 | + store.getCheckpointCaptureContext(threadId, { | | 634 | + runId: RunId.make("run:provider-ownership:rolled-back"), | | 635 | + scopeId: CheckpointScopeId.make("scope:provider-ownership:missing"), | | 636 | + }), | | 637 | + ); | | 638 | + | | 639 | + // The record read, provider control, checkpoint rollback, and checkpoint capture. | | 640 | + const ownershipReads = statements.filter(([query]) => query.includes("owner_node_id")); | | 641 | + assert.lengthOf(ownershipReads, 6); | | 642 | + for (const [query, params] of ownershipReads) { | | 643 | + const plan = yield* sql.unsafe<{ readonly detail: string }>( | | 644 | + `EXPLAIN QUERY PLAN ${query}`, | | 645 | + params, | | 646 | + ); | | 647 | + assert.isFalse( | | 648 | + plan.some((row) => | | 649 | + /USING (?:COVERING )?INDEX orchestration_v2_projection_nodes_thread/.test(row.detail), | | 650 | + ), | | 651 | + JSON.stringify(plan), | | 652 | + ); | | 653 | + } | | 654 | + }).pipe(Effect.provide(layerTest)), | | 655 | +); | | 656 | + | 487 | 657 | it.effect("idle compaction filters keep the latest run, live work and the newest usage", () => | 488 | 658 | Effect.gen(function* () { | 489 | 659 | const store = yield* ProjectionStore.ProjectionStoreV2; | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration-v2/ProjectionStore.ts +23-38Lines changed: 23 additions & 38 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3108,6 +3108,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 3108 | 3108 | `; | 3109 | 3109 | }); | 3110 | 3110 |
| | 3111 | + // Provider threads a thread owns directly, through one of its nodes, or through a subagent. | | 3112 | + const ownedProviderThreadIds = (threadId: ThreadId) => sql` | | 3113 | + SELECT provider_thread_id FROM orchestration_v2_projection_provider_threads | | 3114 | + WHERE thread_id = ${threadId} | | 3115 | + UNION | | 3116 | + -- Start with non-null owners; a thread can have a large settled node history. | | 3117 | + SELECT owned.provider_thread_id | | 3118 | + FROM orchestration_v2_projection_provider_threads AS owned | | 3119 | + INDEXED BY orchestration_v2_projection_provider_threads_owner_idx | | 3120 | + CROSS JOIN orchestration_v2_projection_nodes AS owner | | 3121 | + ON owner.node_id = owned.owner_node_id | | 3122 | + WHERE owned.owner_node_id IS NOT NULL AND owner.thread_id = ${threadId} | | 3123 | + UNION | | 3124 | + SELECT provider_thread_id FROM orchestration_v2_projection_subagents | | 3125 | + WHERE thread_id = ${threadId} AND provider_thread_id IS NOT NULL | | 3126 | + `; | | 3127 | + | 3111 | 3128 | const readCanonicalProjection = Effect.fn("ProjectionStore.readCanonicalProjection")( | 3112 | 3129 | function* ( | 3113 | 3130 | threadId: ThreadId, | @@ -3499,18 +3516,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 3499 | 3516 | ? sql<PayloadRow>` | 3500 | 3517 | SELECT payload_json | 3501 | 3518 | FROM orchestration_v2_projection_provider_threads | 3502 | | - 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 | | - ) | 3508 | | - OR provider_thread_id IN ( | 3509 | | - SELECT provider_thread_id | 3510 | | - FROM orchestration_v2_projection_subagents | 3511 | | - WHERE thread_id = ${threadId} | 3512 | | - AND provider_thread_id IS NOT NULL | 3513 | | - )) | | 3519 | + WHERE provider_thread_id IN (${ownedProviderThreadIds(threadId)}) | 3514 | 3520 | ${filter?.providerThreadIds === undefined ? sql`` : sql`AND provider_thread_id IN (SELECT value FROM json_each(${encodeIdList(filter.providerThreadIds)}))`} | 3515 | 3521 | ORDER BY COALESCE(first_run_ordinal, 0), provider_thread_id ASC | 3516 | 3522 | ` | @@ -4772,21 +4778,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 4772 | 4778 | sql<PayloadRow>` | 4773 | 4779 | SELECT provider_thread.payload_json | 4774 | 4780 | FROM orchestration_v2_projection_provider_threads AS provider_thread | 4775 | | - WHERE provider_thread.provider_thread_id IN ( | 4776 | | - SELECT provider_thread_id FROM orchestration_v2_projection_provider_threads | 4777 | | - WHERE thread_id = ${threadId} | 4778 | | - UNION | 4779 | | - -- Start with non-null owners; a thread can have a large settled node history. | 4780 | | - SELECT owned.provider_thread_id | 4781 | | - FROM orchestration_v2_projection_provider_threads AS owned | 4782 | | - INDEXED BY orchestration_v2_projection_provider_threads_owner_idx | 4783 | | - CROSS JOIN orchestration_v2_projection_nodes AS owner | 4784 | | - ON owner.node_id = owned.owner_node_id | 4785 | | - WHERE owned.owner_node_id IS NOT NULL AND owner.thread_id = ${threadId} | 4786 | | - UNION | 4787 | | - SELECT provider_thread_id FROM orchestration_v2_projection_subagents | 4788 | | - WHERE thread_id = ${threadId} AND provider_thread_id IS NOT NULL | 4789 | | - ) | | 4781 | + WHERE provider_thread.provider_thread_id IN (${ownedProviderThreadIds(threadId)}) | 4790 | 4782 | AND ( | 4791 | 4783 | provider_thread.status = 'active' | 4792 | 4784 | OR CASE WHEN json_valid(provider_thread.payload_json) | @@ -5082,11 +5074,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 5082 | 5074 | yield* requireThread(threadId); | 5083 | 5075 | const [threadRows, turnRows, attemptRows, messageRows] = yield* Effect.all([ | 5084 | 5076 | sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_provider_threads | 5085 | | - WHERE provider_thread_id = ${target.providerThreadId} AND ( | 5086 | | - thread_id = ${threadId} | 5087 | | - OR owner_node_id IN (SELECT node_id FROM orchestration_v2_projection_nodes WHERE thread_id = ${threadId}) | 5088 | | - OR provider_thread_id IN (SELECT provider_thread_id FROM orchestration_v2_projection_subagents WHERE thread_id = ${threadId}) | 5089 | | - )`, | | 5077 | + WHERE provider_thread_id = ${target.providerThreadId} | | 5078 | + AND provider_thread_id IN (${ownedProviderThreadIds(threadId)})`, | 5090 | 5079 | sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_provider_turns | 5091 | 5080 | WHERE thread_id = ${threadId} AND provider_turn_id = ${target.providerTurnId}`, | 5092 | 5081 | target.attemptId === undefined | @@ -5323,9 +5312,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 5323 | 5312 | WHERE thread_id = ${threadId} AND scope_id = ${target.scopeId}`, | 5324 | 5313 | sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_provider_threads | 5325 | 5314 | WHERE provider_thread_id = ${run?.providerThreadId ?? null} | 5326 | | - AND (thread_id = ${threadId} | 5327 | | - OR owner_node_id IN (SELECT node_id FROM orchestration_v2_projection_nodes WHERE thread_id = ${threadId}) | 5328 | | - OR provider_thread_id IN (SELECT provider_thread_id FROM orchestration_v2_projection_subagents WHERE thread_id = ${threadId}))`, | | 5315 | + AND provider_thread_id IN (${ownedProviderThreadIds(threadId)})`, | 5329 | 5316 | sql<{ readonly ordinal: number }>`SELECT ordinal_within_scope AS ordinal | 5330 | 5317 | FROM orchestration_v2_projection_checkpoints | 5331 | 5318 | WHERE thread_id = ${threadId} AND scope_id = ${target.scopeId} AND status = 'ready'`, | @@ -5362,9 +5349,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> = | 5362 | 5349 | const [providerRows, checkpointRows, scopeRows] = yield* Effect.all([ | 5363 | 5350 | sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_provider_threads | 5364 | 5351 | WHERE provider_thread_id = ${target.providerThreadId} | 5365 | | - AND (thread_id = ${threadId} | 5366 | | - OR owner_node_id IN (SELECT node_id FROM orchestration_v2_projection_nodes WHERE thread_id = ${threadId}) | 5367 | | - OR provider_thread_id IN (SELECT provider_thread_id FROM orchestration_v2_projection_subagents WHERE thread_id = ${threadId}))`, | | 5352 | + AND provider_thread_id IN (${ownedProviderThreadIds(threadId)})`, | 5368 | 5353 | sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_checkpoints | 5369 | 5354 | WHERE thread_id = ${threadId} AND checkpoint_id = ${target.checkpointId}`, | 5370 | 5355 | sql<PayloadRow>`SELECT payload_json FROM orchestration_v2_projection_checkpoint_scopes | |
|