@devries-cameron20 / t3code-1 / commits / 52da4e5098
Fork 0

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

fix(server): reuse timeline indexes while paging long threads (#24422)

Julius Marminge committed Nov 11, 2026 at 15:57 UTC 52da4e50987714059e197d400e0c17f0913ae230
2 files changed +377 -49
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+296 -15
@@ -2929,6 +2929,16 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
2929 const now = yield* DateTime.now;
2930 const nowIso = DateTime.formatIso(now);
2931 const threadId = ThreadId.make("thread:bounded-sql-history");
2932 + // The raw SQL edits below stand in for committed writes, which always append a thread event.
2933 + let revision = 0;
2934 + const historyPage = (...args: Parameters<typeof projectionStore.getThreadHistoryPage>) =>
2935 + sql`INSERT INTO orchestration_events (
2936 + event_id, aggregate_kind, stream_id, stream_version, event_type,
2937 + occurred_at, actor_kind, payload_json, metadata_json, application_event_version
2938 + ) VALUES (
2939 + ${`event:bounded-sql-history:revision:${++revision}`}, 'thread', ${threadId}, ${revision},
2940 + 'turn-item.updated', ${nowIso}, 'user', '{}', '{}', 2
2941 + )`.pipe(Effect.andThen(projectionStore.getThreadHistoryPage(...args)));
2942 yield* projectionStore.apply({
2943 id: EventId.make("event:bounded-sql-history:thread"),
2944 type: "thread.created",
@@ -3152,7 +3162,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3162 sourceItemId: TurnItemId.make("turn-item:bounded-sql-history:71"),
3163 position: 70,
3164 });
3155 - const bytePage = yield* projectionStore.getThreadHistoryPage(threadId, byteCursor);
3165 + const bytePage = yield* historyPage(threadId, byteCursor);
3166 assert.isAbove(bytePage.items.length, 0);
3167 assert.isBelow(bytePage.items.length, 70);
3168 assert.isAtMost(
@@ -3163,7 +3173,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3173 let byteNext = bytePage.nextCursor;
3174 for (let pages = 0; byteNext !== null; pages += 1) {
3175 assert.isBelow(pages, 70);
3166 - const page = yield* projectionStore.getThreadHistoryPage(threadId, byteNext);
3176 + const page = yield* historyPage(threadId, byteNext);
3177 assert.isAbove(page.items.length, 0);
3178 byteIds.unshift(...page.items.map((row) => String(row.sourceItemId)));
3179 byteNext = page.nextCursor;
@@ -3269,7 +3279,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3279 '$.attachments', json('[]'))
3280 WHERE thread_id = ${threadId} AND ordinal BETWEEN 600 AND 624
3281 `;
3272 - const targetedPage = yield* projectionStore.getThreadHistoryPage(
3282 + const targetedPage = yield* historyPage(
3283 threadId,
3284 encodeThreadHistoryCursor({
3285 snapshotSequence: 0,
@@ -3299,7 +3309,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3309 '$.type', CASE ordinal WHEN 500 THEN 'run_interrupt_request' ELSE 'run_interrupt_result' END)
3310 WHERE thread_id = ${threadId} AND ordinal IN (500, 580)
3311 `;
3302 - const interruptPage = yield* projectionStore.getThreadHistoryPage(
3312 + const interruptPage = yield* historyPage(
3313 threadId,
3314 encodeThreadHistoryCursor({
3315 snapshotSequence: 0,
@@ -3446,7 +3456,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3456 let workCursor: string | null = anchor.item.deferredWork!.cursor;
3457 for (let pages = 0; workCursor !== null; pages += 1) {
3458 assert.isBelow(pages, 20);
3449 - const page: OrchestrationV2ThreadHistoryPage = yield* projectionStore.getThreadHistoryPage(
3459 + const page: OrchestrationV2ThreadHistoryPage = yield* historyPage(
3460 threadId,
3461 workCursor,
3462 anchor.sourceItemId,
@@ -3475,7 +3485,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3485 window.projection.visibleTurnItems.find((row) => row.item.deferredWork)?.item.ordinal,
3486 3,
3487 );
3478 - const page = yield* projectionStore.getThreadHistoryPage(
3488 + const page = yield* historyPage(
3489 threadId,
3490 encodeThreadHistoryCursor({
3491 snapshotSequence: 0,
@@ -3532,14 +3542,13 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3542 const loaded = new Set<TurnItemId>();
3543 for (let pages = 0; cursor !== null; pages += 1) {
3544 assert.isBelow(pages, 20);
3535 - const page: OrchestrationV2ThreadHistoryPage =
3536 - yield* projectionStore.getThreadHistoryPage(
3537 - threadId,
3538 - cursor,
3539 - oldWork!.sourceItemId,
3540 - false,
3541 - "work",
3542 - );
3545 + const page: OrchestrationV2ThreadHistoryPage = yield* historyPage(
3546 + threadId,
3547 + cursor,
3548 + oldWork!.sourceItemId,
3549 + false,
3550 + "work",
3551 + );
3552 for (const row of page.items) loaded.add(row.sourceItemId);
3553 cursor = page.nextCursor;
3554 }
@@ -4470,6 +4479,276 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4479 }),
4480 );
4481
4482 + it.effect("pages a whole thread without re-reading its full timeline for every page", () =>
4483 + Effect.gen(function* () {
4484 + const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
4485 + const sql = yield* SqlClient.SqlClient;
4486 + const now = yield* DateTime.now;
4487 + const nowIso = DateTime.formatIso(now);
4488 + const threadId = ThreadId.make("thread:paged-scrollback");
4489 + const runId = RunId.make("run:paged-scrollback");
4490 + const itemCount = 600;
4491 + yield* projectionStore.apply({
4492 + id: EventId.make("event:paged-scrollback:thread"),
4493 + type: "thread.created",
4494 + threadId,
4495 + occurredAt: now,
4496 + payload: {
4497 + createdBy: "user",
4498 + creationSource: "web",
4499 + id: threadId,
4500 + projectId: ProjectId.make("project:paged-scrollback"),
4501 + title: "Paged scrollback",
4502 + providerInstanceId,
4503 + modelSelection,
4504 + runtimeMode: "full-access",
4505 + interactionMode: "default",
4506 + branch: null,
4507 + worktreePath: null,
4508 + activeProviderThreadId: null,
4509 + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
4510 + forkedFrom: null,
4511 + createdAt: now,
4512 + updatedAt: now,
4513 + archivedAt: null,
4514 + settledOverride: null,
4515 + settledAt: null,
4516 + lastVisitedAt: null,
4517 + deletedAt: null,
4518 + },
4519 + });
4520 + yield* sql`
4521 + INSERT INTO orchestration_v2_projection_runs (
4522 + run_id, thread_id, ordinal, provider, provider_thread_id, status,
4523 + requested_at, completed_at, payload_json
4524 + ) VALUES (
4525 + ${runId}, ${threadId}, 1, 'codex', NULL, 'completed', ${nowIso}, ${nowIso},
4526 + ${encodeUnknownJsonString({
4527 + id: runId,
4528 + threadId,
4529 + ordinal: 1,
4530 + providerInstanceId,
4531 + modelSelection,
4532 + providerThreadId: null,
4533 + userMessageId: "message:paged-scrollback",
4534 + rootNodeId: "node:paged-scrollback",
4535 + activeAttemptId: null,
4536 + status: "completed",
4537 + requestedAt: nowIso,
4538 + startedAt: nowIso,
4539 + completedAt: nowIso,
4540 + checkpointId: null,
4541 + contextHandoffId: null,
4542 + })}
4543 + )
4544 + `;
4545 + for (let ordinal = 0; ordinal < itemCount; ordinal += 1) {
4546 + const id = `turn-item:paged-scrollback:${ordinal}`;
4547 + const message = ordinal % 9 === 0;
4548 + yield* sql`
4549 + INSERT INTO orchestration_v2_projection_turn_items (
4550 + turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
4551 + parent_item_id, ordinal, type, status, updated_at, payload_json
4552 + ) VALUES (
4553 + ${id}, ${threadId}, ${runId}, NULL, NULL, NULL, NULL, ${ordinal},
4554 + ${message ? "user_message" : "command_execution"}, 'completed', ${nowIso},
4555 + ${encodeUnknownJsonString({
4556 + id,
4557 + threadId,
4558 + runId,
4559 + nodeId: null,
4560 + providerThreadId: null,
4561 + providerTurnId: null,
4562 + nativeItemRef: null,
4563 + parentItemId: null,
4564 + ordinal,
4565 + status: "completed",
4566 + title: `item ${ordinal}`,
4567 + startedAt: nowIso,
4568 + completedAt: nowIso,
4569 + updatedAt: nowIso,
4570 + ...(message
4571 + ? {
4572 + type: "user_message",
4573 + createdBy: "user",
4574 + creationSource: "web",
4575 + messageId: `message:paged-scrollback:${ordinal}`,
4576 + inputIntent: "turn_start",
4577 + text: `Turn ${ordinal}`,
4578 + attachments: [],
4579 + }
4580 + : { type: "command_execution", input: "echo", output: "ok", exitCode: 0 }),
4581 + })}
4582 + )
4583 + `;
4584 + }
4585 + // An unbounded read of every turn item in the thread costs O(thread) per page, so a full
4586 + // scrollback that repeats it on each page is quadratic in thread length.
4587 + const wholeThreadReads = (statements: ReadonlyArray<string>) =>
4588 + statements.filter(
4589 + (statement) =>
4590 + /FROM orchestration_v2_projection_turn_items(\s+AS item)?\s+WHERE (item\.)?thread_id = \?\s*(ORDER BY|$)/.test(
4591 + statement,
4592 + ) && !/\bLIMIT\b/.test(statement),
4593 + ).length;
4594 + const snapshot = yield* projectionStore.getThreadSnapshot(threadId);
4595 +
4596 + const history = traceSqlStatements();
4597 + let cursor: string | null = encodeThreadHistoryCursor({
4598 + snapshotSequence: snapshot.snapshotSequence,
4599 + sourceThreadId: threadId,
4600 + sourceItemId: "absent",
4601 + position: itemCount,
4602 + });
4603 + const historyIds: Array<string> = [];
4604 + let historyPages = 0;
4605 + while (cursor !== null) {
4606 + const page: OrchestrationV2ThreadHistoryPage = yield* projectionStore
4607 + .getThreadHistoryPage(threadId, cursor)
4608 + .pipe(Effect.withTracer(history.tracer));
4609 + historyIds.push(...page.items.map((row) => String(row.sourceItemId)));
4610 + historyPages += 1;
4611 + cursor = page.hasMoreHistory ? page.nextCursor : null;
4612 + }
4613 + assert.strictEqual(new Set(historyIds).size, itemCount);
4614 + assert.isAtLeast(historyPages, 4);
4615 +
4616 + const timeline = traceSqlStatements();
4617 + const timelineIds: Array<string> = [];
4618 + let afterPosition = -1;
4619 + let timelinePages = 0;
4620 + for (let hasMore = true; hasMore;) {
4621 + const page = yield* projectionStore
4622 + .getTimelinePage(threadId, { view: "activity", afterPosition, limit: 100 })
4623 + .pipe(Effect.withTracer(timeline.tracer));
4624 + timelineIds.push(...page.items.map((row) => String(row.sourceItemId)));
4625 + afterPosition = page.items.at(-1)?.position ?? afterPosition;
4626 + timelinePages += 1;
4627 + hasMore = page.hasMore;
4628 + }
4629 + assert.strictEqual(new Set(timelineIds).size, itemCount);
4630 + assert.isAtLeast(timelinePages, 4);
4631 +
4632 + assert.isAtMost(
4633 + wholeThreadReads(history.statements),
4634 + 1,
4635 + `getThreadHistoryPage read the whole thread on each of ${historyPages} pages`,
4636 + );
4637 + assert.isAtMost(
4638 + wholeThreadReads(timeline.statements),
4639 + 1,
4640 + `getTimelinePage read the whole thread on each of ${timelinePages} pages`,
4641 + );
4642 +
4643 + // A write to the thread a fork inherits from must reach the fork's next page.
4644 + const forkThreadId = ThreadId.make("thread:paged-scrollback:fork");
4645 + yield* projectionStore.apply({
4646 + id: EventId.make("event:paged-scrollback:fork"),
4647 + type: "thread.created",
4648 + threadId: forkThreadId,
4649 + occurredAt: now,
4650 + payload: {
4651 + createdBy: "user",
4652 + creationSource: "web",
4653 + id: forkThreadId,
4654 + projectId: ProjectId.make("project:paged-scrollback"),
4655 + title: "Paged scrollback fork",
4656 + providerInstanceId,
4657 + modelSelection,
4658 + runtimeMode: "full-access",
4659 + interactionMode: "default",
4660 + branch: null,
4661 + worktreePath: null,
4662 + activeProviderThreadId: null,
4663 + lineage: {
4664 + parentThreadId: threadId,
4665 + relationshipToParent: "fork",
4666 + rootThreadId: threadId,
4667 + },
4668 + forkedFrom: { type: "run", threadId, runId },
4669 + createdAt: now,
4670 + updatedAt: now,
4671 + archivedAt: null,
4672 + settledOverride: null,
4673 + settledAt: null,
4674 + lastVisitedAt: null,
4675 + deletedAt: null,
4676 + },
4677 + });
4678 + const forkPage = () =>
4679 + projectionStore.getTimelinePage(forkThreadId, { view: "activity", limit: 1 });
4680 + const forkTotal = (yield* forkPage()).totalItems;
4681 + assert.strictEqual((yield* forkPage()).totalItems, forkTotal);
4682 + yield* projectionStore.apply({
4683 + id: EventId.make("event:paged-scrollback:late-item"),
4684 + type: "turn-item.updated",
4685 + threadId,
4686 + runId,
4687 + driver,
4688 + occurredAt: now,
4689 + payload: {
4690 + id: TurnItemId.make("turn-item:paged-scrollback:late"),
4691 + threadId,
4692 + runId,
4693 + nodeId: null,
4694 + providerThreadId: null,
4695 + providerTurnId: null,
4696 + nativeItemRef: null,
4697 + parentItemId: null,
4698 + ordinal: itemCount,
4699 + status: "completed",
4700 + title: "late",
4701 + startedAt: now,
4702 + completedAt: now,
4703 + updatedAt: now,
4704 + type: "command_execution",
4705 + input: "echo",
4706 + },
4707 + });
4708 + assert.strictEqual((yield* forkPage()).totalItems, forkTotal + 1);
4709 +
4710 + // Another store instance's write reaches this one only through the committed thread event.
4711 + const directId = "turn-item:paged-scrollback:direct";
4712 + yield* sql`
4713 + INSERT INTO orchestration_v2_projection_turn_items (
4714 + turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
4715 + parent_item_id, ordinal, type, status, updated_at, payload_json
4716 + ) VALUES (
4717 + ${directId}, ${threadId}, ${runId}, NULL, NULL, NULL, NULL, ${itemCount + 1},
4718 + 'command_execution', 'completed', ${nowIso},
4719 + ${encodeUnknownJsonString({
4720 + id: directId,
4721 + threadId,
4722 + runId,
4723 + nodeId: null,
4724 + providerThreadId: null,
4725 + providerTurnId: null,
4726 + nativeItemRef: null,
4727 + parentItemId: null,
4728 + ordinal: itemCount + 1,
4729 + status: "completed",
4730 + title: "direct",
4731 + startedAt: nowIso,
4732 + completedAt: nowIso,
4733 + updatedAt: nowIso,
4734 + type: "command_execution",
4735 + input: "echo",
4736 + })}
4737 + )
4738 + `;
4739 + yield* sql`
4740 + INSERT INTO orchestration_events (
4741 + event_id, aggregate_kind, stream_id, stream_version, event_type,
4742 + occurred_at, actor_kind, payload_json, metadata_json, application_event_version
4743 + ) VALUES (
4744 + 'event:paged-scrollback:direct', 'thread', ${threadId}, 1,
4745 + 'turn-item.updated', ${nowIso}, 'user', '{}', '{}', 2
4746 + )
4747 + `;
4748 + assert.strictEqual((yield* forkPage()).totalItems, forkTotal + 2);
4749 + }),
4750 + );
4751 +
4752 it.effect("scopes actionable provider state to the requested bounded thread", () =>
4753 Effect.gen(function* () {
4754 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
@@ -4781,6 +5060,8 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5060 number: 42,
5061 url: "https://github.com/acme/project/pull/42",
5062 })})) WHERE thread_id = ${readThreadId}`;
5063 + const [latest] = yield* sql<{ readonly sequence: number }>`
5064 + SELECT COALESCE(MAX(sequence), 0) AS sequence FROM orchestration_events`;
5065 const trace = traceSqlStatements();
5066 const discovery = yield* projectionStore
5067 .getPullRequestSnapshot({})
@@ -4791,7 +5072,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5072 .filter((id) => id === readThreadId || id === laterThreadId),
5073 [readThreadId],
5074 );
4794 - assert.strictEqual(discovery.snapshotSequence, 0);
5075 + assert.strictEqual(discovery.snapshotSequence, latest?.sequence);
5076 assert.strictEqual(trace.queryTransactions.length, 2);
5077 assert.isNotNull(trace.queryTransactions[0]);
5078 assert.strictEqual(new Set(trace.queryTransactions).size, 1);
apps/server/src/orchestration-v2/ProjectionStore.ts
+81 -34
@@ -2060,10 +2060,50 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2060 END
2061 `;
2062
2063 + type TimelineIndexItem = Pick<
2064 + OrchestrationV2TurnItem,
2065 + "id" | "threadId" | "runId" | "nodeId" | "parentItemId" | "type" | "status" | "ordinal"
2066 + > & {
2067 + readonly inputIntent?: Extract<
2068 + OrchestrationV2TurnItem,
2069 + { type: "user_message" }
2070 + >["inputIntent"];
2071 + readonly createdBy?: "user" | "agent" | "system";
2072 + readonly messageId?: MessageId;
2073 + readonly runStatus?: string | undefined;
2074 + readonly attemptId?: string | null;
2075 + readonly attemptStatus?: string | null;
2076 + readonly persistentOutput?: boolean;
2077 + readonly deferredWork?: OrchestrationV2TurnItem["deferredWork"];
2078 + };
2079 + type TimelineIndexRow = {
2080 + readonly sourceThreadId: ThreadId;
2081 + readonly sourceItemId: TurnItemId;
2082 + readonly visibility: OrchestrationV2ProjectedTurnItem["visibility"];
2083 + readonly item: TimelineIndexItem;
2084 + readonly synthetic?: OrchestrationV2TurnItem;
2085 + };
2086 + type TimelineIndex = {
2087 + readonly records: ProjectionRecords<"runs" | "attempts">;
2088 + /** The thread and every fork source the index read. */
2089 + readonly threadIds: ReadonlyArray<ThreadId>;
2090 + readonly local: ReadonlyArray<TimelineIndexRow>;
2091 + readonly visible: ReadonlyArray<TimelineIndexRow>;
2092 + };
2093 + // Least recently used first; a few cover clients scrolling side by side without pinning every thread.
2094 + const TIMELINE_INDEX_CACHE_LIMIT = 8;
2095 + const timelineIndexes = new Map<
2096 + string,
2097 + { readonly sequence: number; readonly index: TimelineIndex }
2098 + >();
2099 +
2100 const apply: ProjectionStoreV2Shape["apply"] =
2101 /* This helper runs for individual orchestration events; the enclosing operation owns the span. */
2102 Effect.fnUntraced(
2103 function* (event) {
2104 + for (const [key, cached] of timelineIndexes) {
2105 + if (cached.index.threadIds.includes(event.threadId)) timelineIndexes.delete(key);
2106 + }
2107 switch (event.type) {
2108 case "thread.pull-request-link-updated": {
2109 const thread = yield* getThread(event.threadId);
@@ -5902,41 +5942,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5942 ),
5943 );
5944
5905 - type TimelineIndexItem = Pick<
5906 - OrchestrationV2TurnItem,
5907 - "id" | "threadId" | "runId" | "nodeId" | "parentItemId" | "type" | "status" | "ordinal"
5908 - > & {
5909 - readonly inputIntent?: Extract<
5910 - OrchestrationV2TurnItem,
5911 - { type: "user_message" }
5912 - >["inputIntent"];
5913 - readonly createdBy?: "user" | "agent" | "system";
5914 - readonly messageId?: MessageId;
5915 - readonly runStatus?: string | undefined;
5916 - readonly attemptId?: string | null;
5917 - readonly attemptStatus?: string | null;
5918 - readonly persistentOutput?: boolean;
5919 - readonly deferredWork?: OrchestrationV2TurnItem["deferredWork"];
5920 - };
5921 - type TimelineIndexRow = {
5922 - readonly sourceThreadId: ThreadId;
5923 - readonly sourceItemId: TurnItemId;
5924 - readonly visibility: OrchestrationV2ProjectedTurnItem["visibility"];
5925 - readonly item: TimelineIndexItem;
5926 - readonly synthetic?: OrchestrationV2TurnItem;
5927 - };
5945 const readTimelineIndex: (
5946 threadId: ThreadId,
5947 seen: ReadonlySet<ThreadId>,
5948 includePersistentOutput?: boolean,
5932 - ) => Effect.Effect<
5933 - {
5934 - readonly records: ProjectionRecords<"runs" | "attempts">;
5935 - readonly local: ReadonlyArray<TimelineIndexRow>;
5936 - readonly visible: ReadonlyArray<TimelineIndexRow>;
5937 - },
5938 - ProjectionStoreV2Error
5939 - > = Effect.fn("ProjectionStore.readTimelineIndex")(
5949 + ) => Effect.Effect<TimelineIndex, ProjectionStoreV2Error> = Effect.fn(
5950 + "ProjectionStore.readTimelineIndex",
5951 + )(
5952 function* (threadId: ThreadId, seen: ReadonlySet<ThreadId>, includePersistentOutput = false) {
5953 const records = yield* getThreadRecords(threadId, ["runs", "attempts"]);
5954 const rows = yield* sql<{
@@ -6006,7 +6018,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6018 });
6019 const visible = local.filter((row) => isVisible(row.item));
6020 const fork = inheritedRunFork(records.thread);
6009 - if (fork === null || seen.has(fork.threadId)) return { records, local, visible };
6021 + if (fork === null || seen.has(fork.threadId))
6022 + return { records, threadIds: [threadId], local, visible };
6023 const source = yield* readTimelineIndex(
6024 fork.threadId,
6025 new Set([...seen, threadId]),
@@ -6044,6 +6057,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6057 });
6058 return {
6059 records,
6060 + threadIds: [threadId, ...source.threadIds],
6061 local,
6062 visible: [
6063 ...inherited,
@@ -6068,6 +6082,39 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6082 ),
6083 );
6084
6085 + // Every committed projection write appends a thread event, so the newest event across the
6086 + // index's threads identifies its snapshot; apply also drops entries for replayed writes. Call
6087 + // inside the read transaction. Without the index hint SQLite walks every newer V2 event.
6088 + const latestThreadEventSequence = (threadId: ThreadId, threadIds: ReadonlyArray<ThreadId>) =>
6089 + sql<{ sequence: number }>`SELECT COALESCE(MAX(sequence), 0) AS sequence
6090 + FROM orchestration_events INDEXED BY idx_orchestration_events_agent_stream_sequence
6091 + WHERE application_event_version = 2
6092 + AND aggregate_kind = 'thread' AND stream_id IN ${sql.in(threadIds)}`.pipe(
6093 + Effect.map((rows) => rows[0]?.sequence ?? 0),
6094 + Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })),
6095 + );
6096 + const readSnapshotTimelineIndex = Effect.fn("ProjectionStore.readSnapshotTimelineIndex")(
6097 + function* (threadId: ThreadId, includePersistentOutput: boolean) {
6098 + const key = `${includePersistentOutput ? "work" : "plain"}:${threadId}`;
6099 + const cached = timelineIndexes.get(key);
6100 + const index =
6101 + cached !== undefined &&
6102 + (yield* latestThreadEventSequence(threadId, cached.index.threadIds)) === cached.sequence
6103 + ? cached.index
6104 + : yield* readTimelineIndex(threadId, new Set(), includePersistentOutput);
6105 + const sequence =
6106 + index === cached?.index
6107 + ? cached.sequence
6108 + : yield* latestThreadEventSequence(threadId, index.threadIds);
6109 + timelineIndexes.delete(key);
6110 + timelineIndexes.set(key, { sequence, index });
6111 + const oldest = timelineIndexes.keys().next();
6112 + if (timelineIndexes.size > TIMELINE_INDEX_CACHE_LIMIT && oldest.done !== true)
6113 + timelineIndexes.delete(oldest.value);
6114 + return index;
6115 + },
6116 + );
6117 +
6118 const { searchThread, searchThreadStream } = yield* makeThreadFind((threadId) =>
6119 readTimelineIndex(threadId, new Set()).pipe(Effect.map((index) => index.visible)),
6120 );
@@ -6082,7 +6129,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6129 sql
6130 .withTransaction(
6131 Effect.gen(function* () {
6085 - const index = yield* readTimelineIndex(threadId, new Set(), workMode !== undefined);
6132 + const index = yield* readSnapshotTimelineIndex(threadId, workMode !== undefined);
6133 if (index.records.thread.deletedAt !== null)
6134 return yield* new ProjectionStoreThreadNotFoundError({ threadId });
6135 const revisions = yield* sql<{
@@ -6199,7 +6246,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6246 sql
6247 .withTransaction(
6248 Effect.gen(function* () {
6202 - const index = yield* readTimelineIndex(threadId, new Set());
6249 + const index = yield* readSnapshotTimelineIndex(threadId, false);
6250 const attachmentOwners =
6251 options.attachmentId === undefined
6252 ? undefined