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, 05:45 UTC
perf(server): bound streaming recovery reads by active runs (#30357)
Yash Singh committed
Nov 3, 2026 at 14:41 UTC
2c9991432f8a25740116c4d2aa8d9e52e6c27cec
2 files changed
+107
-7
apps/server/src/orchestration-v2/ProjectionRecovery.test.ts
+100
@@ -658,6 +658,106 @@ it.effect("includes shared sessions and provider-owned background rosters in rec
658
}).pipe(Effect.provide(layerTest)),
659
);
660
661
+it.effect("bounds streaming recovery reads by candidate runs and preserves message ordering", () =>
662
+ Effect.gen(function* () {
663
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
664
+ const sql = yield* SqlClient.SqlClient;
665
+ const now = yield* DateTime.now;
666
+ const threadId = yield* createThread("streaming");
667
+ const other = yield* createThread("streaming-other");
668
+ const empty = yield* createThread("streaming-empty");
669
+ const statements: Array<ReturnType<Statement.Statement<unknown>["compile"]>> = [];
670
+ const observe: Statement.Transformer = (statement) => {
671
+ const compiled = statement.compile();
672
+ if (compiled[0].includes("SELECT message.payload_json")) statements.push(compiled);
673
+ return Effect.succeed(statement);
674
+ };
675
+ const expected: MessageId[] = [];
676
+ const statuses = [
677
+ "queued",
678
+ "preparing",
679
+ "starting",
680
+ "running",
681
+ "waiting",
682
+ "completed",
683
+ "interrupted",
684
+ "rolled_back",
685
+ "failed",
686
+ "cancelled",
687
+ ] as const;
688
+ for (const [index, status] of statuses.entries()) {
689
+ const runId = yield* createRun(threadId, status, { ordinal: index + 1 });
690
+ for (const suffix of ["b", "a", "settled", "wrong-thread"]) {
691
+ const id = MessageId.make(`message:streaming:${index}:${suffix}`);
692
+ const streaming = suffix !== "settled";
693
+ const messageThread = suffix === "wrong-thread" ? other : threadId;
694
+ const createdAt = DateTime.add(now, { seconds: -index });
695
+ yield* projections.apply({
696
+ id: EventId.make(`event:${id}`),
697
+ type: "message.updated",
698
+ threadId: messageThread,
699
+ runId,
700
+ occurredAt: now,
701
+ payload: {
702
+ id,
703
+ threadId: messageThread,
704
+ runId,
705
+ nodeId: null,
706
+ role: "assistant",
707
+ text: id,
708
+ attachments: [],
709
+ streaming,
710
+ createdAt,
711
+ updatedAt: now,
712
+ createdBy: "agent",
713
+ creationSource: "provider",
714
+ },
715
+ });
716
+ }
717
+ if (index < 5)
718
+ expected.unshift(
719
+ MessageId.make(`message:streaming:${index}:a`),
720
+ MessageId.make(`message:streaming:${index}:b`),
721
+ );
722
+ }
723
+ const foreignRun = yield* createRun(other, "running");
724
+ yield* sql`
725
+ INSERT INTO orchestration_v2_projection_messages
726
+ (message_id, thread_id, run_id, role, streaming, created_at, updated_at, payload_json)
727
+ VALUES ('foreign-run', ${threadId}, ${foreignRun}, 'assistant', 1, '', '', '{}')
728
+ `;
729
+ yield* sql`
730
+ INSERT INTO orchestration_v2_projection_messages
731
+ (message_id, thread_id, run_id, role, streaming, created_at, updated_at, payload_json)
732
+ VALUES ('old-history', ${empty}, NULL, 'assistant', 1, '', '', '{}')
733
+ `;
734
+ const projection = yield* projections
735
+ .getRuntimeRecoveryProjection(threadId)
736
+ .pipe(Effect.provideService(Statement.CurrentTransformer, observe));
737
+ assert.deepEqual(
738
+ projection.messages.map((message) => message.id),
739
+ expected,
740
+ );
741
+ const emptyProjection = yield* projections
742
+ .getRuntimeRecoveryProjection(empty)
743
+ .pipe(Effect.provideService(Statement.CurrentTransformer, observe));
744
+ assert.deepEqual(emptyProjection.messages, []);
745
+ assert.equal(statements.length, 2);
746
+ for (const [query, params] of statements) {
747
+ const plan = yield* sql.unsafe<{ detail: string }>(`EXPLAIN QUERY PLAN ${query}`, params);
748
+ assert.isTrue(plan[0]?.detail.includes("SEARCH run "), JSON.stringify(plan));
749
+ assert.isTrue(
750
+ plan.some((row) => row.detail.includes("messages_run_idx")),
751
+ JSON.stringify(plan),
752
+ );
753
+ assert.isFalse(
754
+ plan.some((row) => row.detail.includes("messages_thread_created_idx")),
755
+ JSON.stringify(plan),
756
+ );
757
+ }
758
+ }).pipe(Effect.provide(layerTest)),
759
+);
760
+
761
it.effect("bounds runtime provider-thread reads by ownership and preserves recovery liveness", () =>
762
Effect.gen(function* () {
763
const projections = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+7
-7
@@ -4730,14 +4730,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
4730
WHERE thread_id = ${threadId} AND status = 'pending'
4731
ORDER BY created_at ASC, runtime_request_id ASC
4732
`,
4733
+ // CROSS JOIN keeps empty run cohorts out of message history; the run index bounds each lookup.
4734
sql<PayloadRow>`
4734
- SELECT message.payload_json FROM orchestration_v2_projection_messages AS message
4735
- WHERE message.thread_id = ${threadId} AND message.streaming = 1
4736
- AND message.run_id IN (
4737
- SELECT run_id FROM orchestration_v2_projection_runs
4738
- WHERE thread_id = ${threadId}
4739
- AND status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
4740
- )
4735
+ SELECT message.payload_json FROM orchestration_v2_projection_runs AS run
4736
+ CROSS JOIN orchestration_v2_projection_messages AS message
4737
+ INDEXED BY orchestration_v2_projection_messages_run_idx ON message.run_id = run.run_id
4738
+ WHERE run.thread_id = ${threadId}
4739
+ AND run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
4740
+ AND message.thread_id = ${threadId} AND message.streaming = 1
4741
ORDER BY message.created_at ASC, message.message_id ASC
4742
`,
4743
sql<PayloadRow>`