@devries-cameron20 / t3code-1 / commits / a909ea423c
Fork 0

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

perf(server): older history pages load without indexing the whole thread (#34573)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Theo Browne committed Nov 12, 2026 at 09:28 UTC a909ea423cbbea68db4b3eb4c6017e06ad6dd50d
4 files changed +1245 -139
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+478 -12
@@ -3651,14 +3651,16 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3651 const threadId = ThreadId.make("thread:bounded-sql-history");
3652 // The raw SQL edits below stand in for committed writes, which always append a thread event.
3653 let revision = 0;
3654 - const historyPage = (...args: Parameters<typeof projectionStore.getThreadHistoryPage>) =>
3654 + const committed = <A, E, R>(read: Effect.Effect<A, E, R>) =>
3655 sql`INSERT INTO orchestration_events (
3656 event_id, aggregate_kind, stream_id, stream_version, event_type,
3657 occurred_at, actor_kind, payload_json, metadata_json, application_event_version
3658 ) VALUES (
3659 ${`event:bounded-sql-history:revision:${++revision}`}, 'thread', ${threadId}, ${revision},
3660 'turn-item.updated', ${nowIso}, 'user', '{}', '{}', 2
3661 - )`.pipe(Effect.andThen(projectionStore.getThreadHistoryPage(...args)));
3661 + )`.pipe(Effect.andThen(read));
3662 + const historyPage = (...args: Parameters<typeof projectionStore.getThreadHistoryPage>) =>
3663 + committed(projectionStore.getThreadHistoryPage(...args));
3664 yield* projectionStore.apply({
3665 id: EventId.make("event:bounded-sql-history:thread"),
3666 type: "thread.created",
@@ -4050,6 +4052,134 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4052 WHERE thread_id = ${threadId} AND ordinal IN (500, 580)
4053 `;
4054
4055 + // Older pages and timeline pages read the rows near the page, not the whole history. A
4056 + // full index that another read left cached gives the same page.
4057 + const pageStatements: Array<readonly [string, ReadonlyArray<unknown>]> = [];
4058 + const recordPageStatements = <A, E, R>(read: Effect.Effect<A, E, R>) =>
4059 + committed(
4060 + read.pipe(
4061 + Effect.provideService(Statement.CurrentTransformer, (statement) => {
4062 + pageStatements.push(statement.compile());
4063 + return Effect.succeed(statement);
4064 + }),
4065 + ),
4066 + );
4067 + const olderCursor = encodeThreadHistoryCursor({
4068 + snapshotSequence: 0,
4069 + sourceThreadId: threadId,
4070 + sourceItemId: TurnItemId.make("turn-item:bounded-sql-history:990"),
4071 + position: 989,
4072 + });
4073 + const ordinalsFrom = (first: number, length: number) =>
4074 + Array.from({ length }, (_, index) => first + index);
4075 + for (const workMode of [undefined, "deferred"] as const) {
4076 + const read = (throughEntryId?: string) =>
4077 + projectionStore.getThreadHistoryPage(
4078 + threadId,
4079 + olderCursor,
4080 + throughEntryId,
4081 + false,
4082 + workMode,
4083 + );
4084 + const page = yield* recordPageStatements(read());
4085 + assert.deepEqual(
4086 + page.items.map((row) => row.item.ordinal),
4087 + ordinalsFrom(915, 75),
4088 + );
4089 + assert.strictEqual(decodeThreadHistoryCursor(page.nextCursor!).p, 914);
4090 + // A jump to an entry reads the full index and leaves it cached. With work, it scans
4091 + // payloads for inline output only in its page's runs, not in a run after its cursor.
4092 + const scannedRuns: Array<string> = [];
4093 + yield* read("turn-item:bounded-sql-history:950").pipe(
4094 + Effect.provideService(Statement.CurrentTransformer, (statement) => {
4095 + const [query, params] = statement.compile();
4096 + if (query.includes("LIKE '%htmlRender%'")) scannedRuns.push(String(params[0]));
4097 + return Effect.succeed(statement);
4098 + }),
4099 + );
4100 + assert.deepEqual(
4101 + [
4102 + scannedRuns.length,
4103 + scannedRuns.some((runs) => runs.includes('"run:bounded-sql-history:950"')),
4104 + scannedRuns.some((runs) => runs.includes('"run:bounded-sql-history:995"')),
4105 + ],
4106 + workMode === undefined ? [0, false, false] : [1, true, false],
4107 + );
4108 + assert.deepEqual(yield* read(), page);
4109 + }
4110 + for (const [view, ordinals] of [
4111 + ["activity", ordinalsFrom(902, 20)],
4112 + ["messages", [1000]],
4113 + ] as const) {
4114 + const read = projectionStore.getTimelinePage(threadId, {
4115 + afterPosition: 900,
4116 + limit: 20,
4117 + view,
4118 + });
4119 + const page = yield* recordPageStatements(read);
4120 + assert.deepEqual(
4121 + page.items.map((row) => [row.item.ordinal, row.position]),
4122 + ordinals.map((ordinal) => [ordinal, ordinal - 1]),
4123 + );
4124 + assert.strictEqual(page.totalItems, 1000);
4125 + // An attachment lookup reads the full index and leaves it cached.
4126 + yield* projectionStore.getTimelinePage(threadId, { attachmentId: "absent", limit: 1 });
4127 + assert.deepEqual(yield* read, page);
4128 + }
4129 + {
4130 + const pagePath = "file:/history-page-steps?vfs=memdb";
4131 + using copy = new NodeSqlite.DatabaseSync(pagePath);
4132 + yield* sql`VACUUM INTO ${pagePath}`;
4133 + // Only statements that count positions may grow with the history, and those read an
4134 + // index alone. Every other statement costs the same when the rows below the page are gone.
4135 + const countsPositions = ([query]: readonly [string, ReadonlyArray<unknown>]) =>
4136 + /COUNT\(\*\)|OFFSET/.test(query);
4137 + for (const [query, params] of pageStatements.filter(countsPositions)) {
4138 + const plan = copy
4139 + .prepare(`EXPLAIN QUERY PLAN ${query}`)
4140 + .all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>))
4141 + .map((row) => String(row.detail))
4142 + .filter((detail) => detail.includes("orchestration_v2_projection_turn_items"));
4143 + assert.isTrue(
4144 + plan.every((detail) => detail.includes("COVERING INDEX")),
4145 + `${query}\n${plan.join("\n")}`,
4146 + );
4147 + }
4148 + const rowReads = pageStatements.filter((statement) => !countsPositions(statement));
4149 + const rowReadSteps = () =>
4150 + rowReads.reduce((steps, [query, params]) => {
4151 + const statement = copy.prepare(query);
4152 + statement.all(...(params as ReadonlyArray<NodeSqlite.SQLInputValue>));
4153 + return steps + statement.stat("vmStep");
4154 + }, 0);
4155 + const fullSteps = rowReadSteps();
4156 + assert.isAbove(fullSteps, 0);
4157 + copy.exec(
4158 + "DELETE FROM orchestration_v2_projection_turn_items WHERE ordinal BETWEEN 2 AND 400",
4159 + );
4160 + assert.isAtMost(fullSteps, rowReadSteps() * 1.1);
4161 + }
4162 + // Rows that a rolled-back run hides below the page still shift every position above them.
4163 + const setOldRunStatus = (status: "rolled_back" | "completed") => sql`
4164 + UPDATE orchestration_v2_projection_runs
4165 + SET status = ${status}, payload_json = json_set(payload_json, '$.status', ${status})
4166 + WHERE thread_id = ${threadId} AND ordinal BETWEEN 100 AND 109`;
4167 + yield* setOldRunStatus("rolled_back");
4168 + const shiftedPage = yield* historyPage(threadId, olderCursor);
4169 + assert.strictEqual(decodeThreadHistoryCursor(shiftedPage.nextCursor!).p, 904);
4170 + const shiftedTimeline = yield* committed(
4171 + projectionStore.getTimelinePage(threadId, {
4172 + afterPosition: 900,
4173 + limit: 1,
4174 + view: "activity",
4175 + }),
4176 + );
4177 + assert.deepEqual(
4178 + [shiftedTimeline.totalItems, shiftedTimeline.items[0]?.item.ordinal],
4179 + [990, 912],
4180 + );
4181 + yield* setOldRunStatus("completed");
4182 +
4183 const retainedRequestPlan = yield* sql<{ readonly detail: string }>`
4184 EXPLAIN QUERY PLAN
4185 WITH selected AS (
@@ -4205,21 +4335,24 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4335 window.projection.visibleTurnItems.find((row) => row.item.deferredWork)?.item.ordinal,
4336 3,
4337 );
4338 + const newestCursor = encodeThreadHistoryCursor({
4339 + snapshotSequence: 0,
4340 + sourceThreadId: threadId,
4341 + sourceItemId: "turn-item:bounded-sql-history:1000",
4342 + position: 1000,
4343 + });
4344 + const page = yield* historyPage(threadId, newestCursor, undefined, false, "deferred");
4345 + assert.isUndefined(page.items.find((row) => row.item.ordinal === 2)?.item.deferredWork);
4346 + // Tool payloads hold megabytes, so a page that reads the full index, as a jump to an
4347 + // entry does, looks for inline output only in its own runs.
4348 const pageRead = traceSqlStatements();
4209 - const page = yield* historyPage(
4349 + yield* historyPage(
4350 threadId,
4211 - encodeThreadHistoryCursor({
4212 - snapshotSequence: 0,
4213 - sourceThreadId: threadId,
4214 - sourceItemId: "turn-item:bounded-sql-history:1000",
4215 - position: 1000,
4216 - }),
4217 - undefined,
4351 + newestCursor,
4352 + "turn-item:bounded-sql-history:2",
4353 false,
4354 "deferred",
4355 ).pipe(Effect.withTracer(pageRead.tracer));
4221 - assert.isUndefined(page.items.find((row) => row.item.ordinal === 2)?.item.deferredWork);
4222 - // Tool payloads hold megabytes, so a page looks for inline output only in its own runs.
4356 const payloadScans = pageRead.statements.filter((statement) =>
4357 statement.includes("LIKE '%htmlRender%'"),
4358 );
@@ -5558,6 +5691,339 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
5691 }),
5692 );
5693
5694 + it.effect("a page read from a window equals the page read from the full index", () =>
5695 + Effect.gen(function* () {
5696 + const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
5697 + const sql = yield* SqlClient.SqlClient;
5698 + const now = yield* DateTime.now;
5699 + const nowIso = DateTime.formatIso(now);
5700 + // Seeded, so every run builds the same threads.
5701 + let seed = 0x5eed;
5702 + const random = () => {
5703 + seed = (Math.imul(seed, 1_664_525) + 1_013_904_223) >>> 0;
5704 + return seed / 2 ** 32;
5705 + };
5706 + const chance = (probability: number) => random() < probability;
5707 + const spans: Array<string> = [];
5708 + const tracer = Tracer.make({
5709 + span(options) {
5710 + spans.push(options.name);
5711 + return new Tracer.NativeSpan(options);
5712 + },
5713 + });
5714 + let windowReads = 0;
5715 + let foldedGroups = 0;
5716 +
5717 + for (let round = 0; round < 4; round += 1) {
5718 + const threadId = ThreadId.make(`thread:window-equivalence:${round}`);
5719 + const created = {
5720 + id: EventId.make(`event:window-equivalence:${round}`),
5721 + type: "thread.created",
5722 + threadId,
5723 + occurredAt: now,
5724 + payload: {
5725 + createdBy: "user",
5726 + creationSource: "web",
5727 + id: threadId,
5728 + projectId: ProjectId.make("project:window-equivalence"),
5729 + title: "Window equivalence",
5730 + providerInstanceId,
5731 + modelSelection,
5732 + runtimeMode: "full-access",
5733 + interactionMode: "default",
5734 + branch: null,
5735 + worktreePath: null,
5736 + activeProviderThreadId: null,
5737 + lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
5738 + forkedFrom: null,
5739 + createdAt: now,
5740 + updatedAt: now,
5741 + archivedAt: null,
5742 + settledOverride: null,
5743 + settledAt: null,
5744 + lastVisitedAt: null,
5745 + deletedAt: null,
5746 + },
5747 + } satisfies Parameters<typeof projectionStore.apply>[0];
5748 + yield* projectionStore.apply(created);
5749 + // One run of each kind in a random order, then an active run. Every other thread has
5750 + // one user turn and then only wake turns.
5751 + // The last thread is a long run that a late notification wakes, then enough one-row
5752 + // runs that a page stops at the raw turn limit only if it counts that wake as a turn.
5753 + const kinds =
5754 + round === 3
5755 + ? ["late wake", ...Array.from({ length: 148 }, () => "one row")]
5756 + : [
5757 + "rolled back",
5758 + "cancelled queued turn",
5759 + "replaced attempt",
5760 + "stopped",
5761 + "stopped",
5762 + "folds work",
5763 + "folds work",
5764 + "short",
5765 + "short",
5766 + "short",
5767 + ]
5768 + .map((kind) => ({ kind, order: random() }))
5769 + .toSorted((left, right) => left.order - right.order)
5770 + .map(({ kind }) => kind);
5771 + const agentDriven = round === 1;
5772 + const runs: Array<unknown> = [];
5773 + const attempts: Array<unknown> = [];
5774 + const items: Array<unknown> = [];
5775 + const tiedWithHidden: Array<TurnItemId> = [];
5776 + const repliesInStoppedRuns: Array<string> = [];
5777 + let ordinal = 0;
5778 + for (const [index, kind] of [...kinds, "active"].entries()) {
5779 + const run = index + 1;
5780 + const runId = `run:window-equivalence:${round}:${run}`;
5781 + const nodeId = `node:window-equivalence:${round}:${run}`;
5782 + const status =
5783 + kind === "active"
5784 + ? "running"
5785 + : kind === "rolled back"
5786 + ? "rolled_back"
5787 + : kind === "cancelled queued turn"
5788 + ? "cancelled"
5789 + : kind === "short" && chance(0.5)
5790 + ? "interrupted"
5791 + : "completed";
5792 + runs.push({
5793 + id: runId,
5794 + threadId,
5795 + ordinal: run,
5796 + providerInstanceId,
5797 + modelSelection,
5798 + providerThreadId: null,
5799 + userMessageId: `message:window-equivalence:${round}:${run}`,
5800 + rootNodeId: nodeId,
5801 + activeAttemptId: null,
5802 + status,
5803 + requestedAt: nowIso,
5804 + startedAt: nowIso,
5805 + completedAt: nowIso,
5806 + checkpointId: null,
5807 + contextHandoffId: null,
5808 + });
5809 + attempts.push({
5810 + id: `attempt:window-equivalence:${round}:${run}`,
5811 + runId,
5812 + attemptOrdinal: 1,
5813 + rootNodeId: nodeId,
5814 + providerInstanceId,
5815 + providerThreadId: "provider-thread:window-equivalence",
5816 + providerTurnId: null,
5817 + reason: "initial",
5818 + status: kind === "replaced attempt" || chance(0.2) ? "superseded" : "completed",
5819 + startedAt: nowIso,
5820 + completedAt: nowIso,
5821 + });
5822 + // The first row after a rolled-back run shares the ordinal of that run's last row.
5823 + let tie = kinds[index - 1] === "rolled back";
5824 + if (tie)
5825 + tiedWithHidden.push(
5826 + TurnItemId.make(`turn-item:window-equivalence:${round}:${items.length}`),
5827 + );
5828 + const add = (type: string, fields: Record<string, unknown> = {}) => {
5829 + if (ordinal === 0 || !(tie || chance(0.04))) ordinal += 1;
5830 + tie = false;
5831 + items.push({
5832 + id: `turn-item:window-equivalence:${round}:${items.length}`,
5833 + threadId,
5834 + runId,
5835 + nodeId,
5836 + providerThreadId: null,
5837 + providerTurnId: null,
5838 + nativeItemRef: null,
5839 + parentItemId: null,
5840 + ordinal,
5841 + status: "completed",
5842 + title: null,
5843 + startedAt: nowIso,
5844 + completedAt: nowIso,
5845 + updatedAt: nowIso,
5846 + type,
5847 + ...fields,
5848 + });
5849 + };
5850 + const userMessage = (inputIntent: string) =>
5851 + add("user_message", {
5852 + createdBy: "user",
5853 + creationSource: "web",
5854 + messageId: `message:window-equivalence:${round}:${items.length}`,
5855 + inputIntent,
5856 + text: "Turn",
5857 + attachments: [],
5858 + });
5859 + const notification = () =>
5860 + add("notification", {
5861 + source: { kind: "system" },
5862 + outcome: "completed",
5863 + summary: "Wake",
5864 + });
5865 + const wakes =
5866 + kind === "one row"
5867 + ? run % 10 !== 0
5868 + : kind !== "late wake" && (agentDriven ? run > 1 : chance(0.2));
5869 + if (wakes) notification();
5870 + else userMessage(kind === "cancelled queued turn" ? "queued_turn" : "turn_start");
5871 + // A stopped run never folds, so it is long enough for page ends to fall inside it.
5872 + const work =
5873 + kind === "late wake"
5874 + ? 420 + Math.floor(random() * 40)
5875 + : kind === "stopped"
5876 + ? 320 + Math.floor(random() * 60)
5877 + : kind === "folds work"
5878 + ? 155 + Math.floor(random() * 60)
5879 + : kind === "one row"
5880 + ? 0
5881 + : Math.floor(random() * 30);
5882 + for (let row = 0; row < work; row += 1) {
5883 + if (chance(0.5)) add("command_execution", { input: "echo", output: "ok", exitCode: 0 });
5884 + else add("reasoning", { text: "", streaming: false });
5885 + // A reply deep in a stopped run: a page that ends on it has enough work below it
5886 + // to fold, and only the stop request above it says that it does not.
5887 + const reply = kind === "stopped" && row === 200;
5888 + if (reply)
5889 + repliesInStoppedRuns.push(`turn-item:window-equivalence:${round}:${items.length}`);
5890 + if (reply || chance(0.02))
5891 + add("assistant_message", {
5892 + messageId: `message:window-equivalence:${round}:${items.length}`,
5893 + text: "Reply",
5894 + streaming: false,
5895 + });
5896 + if (chance(0.01)) notification();
5897 + if (chance(0.004) && !agentDriven) userMessage("steer");
5898 + }
5899 + if (kind === "late wake") notification();
5900 + if (kind === "stopped") add("run_interrupt_request", { message: "Stop" });
5901 + if (kind === "replaced attempt" || (kind === "stopped" && chance(0.5)))
5902 + add("run_interrupt_result", { message: "Stopped" });
5903 + if (kind === "short" && chance(0.3))
5904 + add("error", {
5905 + status: "failed",
5906 + failure: { class: "unknown", message: "Failed", code: null, retryable: false },
5907 + });
5908 + }
5909 + yield* sql`
5910 + INSERT INTO orchestration_v2_projection_runs (
5911 + run_id, thread_id, ordinal, provider, provider_thread_id, status,
5912 + requested_at, completed_at, payload_json
5913 + )
5914 + SELECT json_extract(value, '$.id'), ${threadId}, json_extract(value, '$.ordinal'), 'codex',
5915 + NULL, json_extract(value, '$.status'), ${nowIso}, ${nowIso}, value
5916 + FROM json_each(${encodeUnknownJsonString(runs)})`;
5917 + yield* sql`
5918 + INSERT INTO orchestration_v2_projection_run_attempts (
5919 + attempt_id, thread_id, run_id, attempt_ordinal, root_node_id, provider,
5920 + provider_instance_id, provider_thread_id, provider_turn_id, status, payload_json
5921 + )
5922 + SELECT json_extract(value, '$.id'), ${threadId}, json_extract(value, '$.runId'), 1,
5923 + json_extract(value, '$.rootNodeId'), ${providerInstanceId}, ${providerInstanceId},
5924 + json_extract(value, '$.providerThreadId'), NULL, json_extract(value, '$.status'), value
5925 + FROM json_each(${encodeUnknownJsonString(attempts)})`;
5926 + yield* sql`
5927 + INSERT INTO orchestration_v2_projection_turn_items (
5928 + turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
5929 + parent_item_id, ordinal, type, status, updated_at, payload_json
5930 + )
5931 + SELECT json_extract(value, '$.id'), ${threadId}, json_extract(value, '$.runId'),
5932 + json_extract(value, '$.nodeId'), NULL, NULL, NULL, json_extract(value, '$.ordinal'),
5933 + json_extract(value, '$.type'), json_extract(value, '$.status'), ${nowIso}, value
5934 + FROM json_each(${encodeUnknownJsonString(items)})`;
5935 +
5936 + // Applying an event drops the thread's cached index, as each event of a run does, so
5937 + // each of these reads a window.
5938 + const requests: Array<{
5939 + readonly read: Effect.Effect<unknown, ProjectionStore.ProjectionStoreV2Error>;
5940 + readonly page: unknown;
5941 + }> = [];
5942 + const windowed = <A>(read: Effect.Effect<A, ProjectionStore.ProjectionStoreV2Error>) =>
5943 + Effect.gen(function* () {
5944 + yield* projectionStore.apply(created);
5945 + spans.length = 0;
5946 + const page = yield* read.pipe(Effect.withTracer(tracer));
5947 + if (spans.includes("ProjectionStore.readTimelineWindow")) windowReads += 1;
5948 + requests.push({ read, page });
5949 + return page;
5950 + });
5951 + const history = (
5952 + cursor: string,
5953 + throughEntryId?: string,
5954 + conversationOnly?: boolean,
5955 + workMode?: "deferred" | "work",
5956 + ) =>
5957 + projectionStore.getThreadHistoryPage(
5958 + threadId,
5959 + cursor,
5960 + throughEntryId,
5961 + conversationOnly,
5962 + workMode,
5963 + );
5964 + const timeline = (options: ProjectionStore.ProjectionTimelinePageOptions) =>
5965 + projectionStore.getTimelinePage(threadId, options);
5966 +
5967 + const { totalItems } = yield* windowed(timeline({ limit: 0 }));
5968 + const newest = (yield* windowed(
5969 + timeline({ view: "activity", afterPosition: totalItems - 2, limit: 1 }),
5970 + )).items[0]!;
5971 + const firstCursor = encodeThreadHistoryCursor({
5972 + snapshotSequence: 0,
5973 + sourceThreadId: threadId,
5974 + sourceItemId: newest.sourceItemId,
5975 + position: newest.position,
5976 + });
5977 + const groups = new Map<string, string>();
5978 + for (const [conversationOnly, workMode] of [
5979 + [false, "deferred"],
5980 + [false, undefined],
5981 + [true, undefined],
5982 + ] as const) {
5983 + for (let cursor: string | null = firstCursor; cursor !== null;) {
5984 + const page: OrchestrationV2ThreadHistoryPage = yield* windowed(
5985 + history(cursor, undefined, conversationOnly, workMode),
5986 + );
5987 + for (const { item } of page.items)
5988 + if (item.deferredWork !== undefined) groups.set(item.id, item.deferredWork.cursor);
5989 + cursor = page.nextCursor;
5990 + }
5991 + }
5992 + for (const sourceItemId of repliesInStoppedRuns) {
5993 + const cursor = encodeThreadHistoryCursor({
5994 + snapshotSequence: 0,
5995 + sourceThreadId: threadId,
5996 + sourceItemId,
5997 + position: 0,
5998 + });
5999 + yield* windowed(history(cursor, undefined, false, "deferred"));
6000 + }
6001 + foldedGroups += groups.size;
6002 + for (const [anchorId, groupCursor] of groups) {
6003 + for (let cursor: string | null = groupCursor; cursor !== null;)
6004 + cursor = (yield* windowed(history(cursor, anchorId, false, "work"))).nextCursor;
6005 + }
6006 + for (const view of ["messages", "activity"] as const) {
6007 + for (const afterPosition of [-1, Math.floor(totalItems / 3), totalItems - 5])
6008 + yield* windowed(timeline({ view, afterPosition, limit: 20 }));
6009 + }
6010 + for (const itemId of [newest.sourceItemId, ...tiedWithHidden])
6011 + yield* windowed(timeline({ itemId, limit: 1 }));
6012 +
6013 + // An attachment lookup and a jump to an entry read the full index and leave it cached,
6014 + // so the same requests now come from it.
6015 + yield* timeline({ attachmentId: "absent", limit: 1 });
6016 + yield* history(firstCursor, "absent", false, "deferred");
6017 + spans.length = 0;
6018 + for (const { read, page } of requests)
6019 + assert.deepEqual(yield* read.pipe(Effect.withTracer(tracer)), page);
6020 + assert.notInclude(spans, "ProjectionStore.readTimelineWindow");
6021 + }
6022 + assert.isAtLeast(foldedGroups, 2);
6023 + assert.isAbove(windowReads, 60);
6024 + }),
6025 + );
6026 +
6027 it.effect("scopes actionable provider state to the requested bounded thread", () =>
6028 Effect.gen(function* () {
6029 const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+693 -115
@@ -90,6 +90,7 @@ import * as Context from "effect/Context";
90 import * as DateTime from "effect/DateTime";
91 import * as Effect from "effect/Effect";
92 import * as Layer from "effect/Layer";
93 +import * as Result from "effect/Result";
94 import * as Schema from "effect/Schema";
95 import * as SqlClient from "effect/sql/SqlClient";
96 import type * as Statement from "effect/sql/Statement";
@@ -98,6 +99,7 @@ import { MCP_APP_OUTPUT_KEY } from "@t3tools/shared/mcpApp";
99 import { threadHtmlRenderAttachmentIds } from "../attachmentStore.ts";
100 import { readableSearchText } from "./messageSearchIndex.ts";
101 import {
102 + isFoldableWorkItem,
103 isThreadHistoryUserTurn,
104 isConversationHistoryItem,
105 THREAD_HISTORY_MAX_RAW_TURNS,
@@ -114,6 +116,8 @@ import {
116 THREAD_HISTORY_PAGE_POLICY,
117 THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT,
118 THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
119 + type SelectTimelinePageResult,
120 + type ThreadHistoryPrefix,
121 } from "./threadHistoryPaging.ts";
122
123 export class ProjectionStoreApplyEventError extends Schema.TaggedError<ProjectionStoreApplyEventError>()(
@@ -6451,6 +6455,459 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6455 readTimelineIndex(threadId, new Set()).pipe(Effect.map((index) => index.visible)),
6456 );
6457
6458 + /**
6459 + * Index columns of the thread's own turn items that `where` selects, in timeline order. The
6460 + * same columns as `readTimelineIndex`, which reads every row and stays as it is.
6461 + */
6462 + const readTimelineWindow = Effect.fn("ProjectionStore.readTimelineWindow")(function* (
6463 + threadId: ThreadId,
6464 + runs: ProjectionRecords<"runs">["runs"],
6465 + includePersistentOutput: boolean,
6466 + where: Statement.Fragment,
6467 + limit: Statement.Fragment = sql``,
6468 + ) {
6469 + const rows = yield* sql<{
6470 + turn_item_id: string;
6471 + run_id: string | null;
6472 + node_id: string | null;
6473 + parent_item_id: string | null;
6474 + type: OrchestrationV2TurnItem["type"];
6475 + input_intent: NonNullable<TimelineIndexItem["inputIntent"]> | null;
6476 + created_by: "user" | "agent" | "system" | null;
6477 + message_id: string | null;
6478 + status: OrchestrationV2TurnItem["status"];
6479 + ordinal: number;
6480 + persistent_output: number;
6481 + attempt_id: string | null;
6482 + attempt_status: string | null;
6483 + }>`SELECT item.turn_item_id, item.run_id, item.node_id, item.parent_item_id, item.type, item.status, item.ordinal,
6484 + ${includePersistentOutput ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id,
6485 + ${includePersistentOutput ? sql`attempt.status` : sql`NULL`} AS attempt_status,
6486 + ${
6487 + includePersistentOutput ? sql`CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END` : sql`0`
6488 + } AS persistent_output,
6489 + CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.inputIntent') END AS input_intent,
6490 + CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.createdBy') END AS created_by,
6491 + CASE WHEN item.type IN ('user_message', 'assistant_message') THEN json_extract(item.payload_json, '$.messageId') END AS message_id
6492 + FROM orchestration_v2_projection_turn_items AS item
6493 + ${
6494 + includePersistentOutput
6495 + ? sql`LEFT JOIN orchestration_v2_projection_nodes AS node ON node.node_id = item.node_id
6496 + LEFT JOIN orchestration_v2_projection_run_attempts AS attempt ON attempt.attempt_id = (
6497 + SELECT attempt_id FROM orchestration_v2_projection_run_attempts
6498 + WHERE run_id = item.run_id AND root_node_id = COALESCE(node.root_node_id, item.node_id)
6499 + ORDER BY attempt_ordinal DESC LIMIT 1
6500 + )`
6501 + : sql``
6502 + }
6503 + WHERE item.thread_id = ${threadId} ${where}
6504 + ORDER BY item.ordinal ASC, item.turn_item_id ASC ${limit}`;
6505 + const runStatuses = new Map(runs.map((run) => [String(run.id), run.status]));
6506 + return rows.map((row): TimelineIndexItem => ({
6507 + id: TurnItemId.make(row.turn_item_id),
6508 + threadId,
6509 + runId: row.run_id === null ? null : RunId.make(row.run_id),
6510 + nodeId: row.node_id === null ? null : NodeId.make(row.node_id),
6511 + parentItemId: row.parent_item_id === null ? null : TurnItemId.make(row.parent_item_id),
6512 + type: row.type,
6513 + status: row.status,
6514 + ordinal: row.ordinal,
6515 + runStatus: row.run_id === null ? undefined : runStatuses.get(row.run_id),
6516 + attemptId: row.attempt_id,
6517 + attemptStatus: row.attempt_status,
6518 + persistentOutput: row.persistent_output === 1,
6519 + ...(row.input_intent === null ? {} : { inputIntent: row.input_intent }),
6520 + ...(row.created_by === null ? {} : { createdBy: row.created_by }),
6521 + ...(row.message_id === null ? {} : { messageId: MessageId.make(row.message_id) }),
6522 + }));
6523 + });
6524 + const localTimelineRow = (item: TimelineIndexItem): TimelineIndexRow => ({
6525 + sourceThreadId: item.threadId,
6526 + sourceItemId: item.id,
6527 + visibility: "local",
6528 + item,
6529 + });
6530 +
6531 + /**
6532 + * Reads parts of a thread's own timeline without indexing all of it. Null when callers read
6533 + * the full index instead: the thread shows fork ancestry, so its rows come from other threads,
6534 + * or a full index of this snapshot is cached already.
6535 + */
6536 + const openLocalTimeline = Effect.fn("ProjectionStore.openLocalTimeline")(function* (
6537 + threadId: ThreadId,
6538 + includePersistentOutput = false,
6539 + ) {
6540 + // A read that only the full index can answer left it cached, and it answers faster than
6541 + // a window. The key is the one `readSnapshotTimelineIndex` uses.
6542 + const cached = timelineIndexes.get(
6543 + `${includePersistentOutput ? "work" : "plain"}:${threadId}`,
6544 + );
6545 + if (
6546 + cached !== undefined &&
6547 + (yield* latestThreadEventSequence(threadId, cached.index.threadIds)) === cached.sequence
6548 + )
6549 + return null;
6550 + // Checked in SQL, so a fork loads no runs and no thread here before the full index does.
6551 + const ownRowsOnly = yield* sql`
6552 + SELECT 1 FROM orchestration_v2_projection_threads
6553 + WHERE thread_id = ${threadId}
6554 + AND NOT (json_extract(payload_json, '$.forkedFrom.type') IS 'run'
6555 + AND json_extract(payload_json, '$.lineage.sideChat') IS NOT 1)`;
6556 + if (ownRowsOnly.length === 0) return null;
6557 + const records = yield* getThreadRecords(threadId, ["runs", "attempts"]);
6558 + if (inheritedRunFork(records.thread) !== null) return null;
6559 + const superseded = records.attempts.filter((attempt) => attempt.status === "superseded");
6560 + const stopRequests =
6561 + superseded.length === 0
6562 + ? []
6563 + : yield* sql<{ run_id: string | null }>`
6564 + SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
6565 + WHERE thread_id = ${threadId} AND type = 'run_interrupt_request'`;
6566 + const isVisible = createOrchestrationV2TurnItemVisibility({
6567 + runs: records.runs,
6568 + attempts: records.attempts,
6569 + items: stopRequests.map((row) => ({
6570 + type: "run_interrupt_request" as const,
6571 + runId: row.run_id === null ? null : RunId.make(row.run_id),
6572 + nodeId: null,
6573 + })),
6574 + });
6575 + // Only these runs can hide rows: every row of a rolled-back run, the queued turn of a
6576 + // cancelled run, and the stop result of a replaced attempt. So the hidden rows are found
6577 + // without reading the thread.
6578 + const stoppedRuns = new Set(stopRequests.map((row) => row.run_id));
6579 + const runsWithStatus = (status: "rolled_back" | "cancelled") =>
6580 + records.runs.flatMap((run) => (run.status === status ? [String(run.id)] : []));
6581 + const rolledBack = runsWithStatus("rolled_back");
6582 + const cancelled = runsWithStatus("cancelled");
6583 + const steered = [...new Set(superseded.map((attempt) => String(attempt.runId)))].filter(
6584 + (runId) => !stoppedRuns.has(runId) && !rolledBack.includes(runId),
6585 + );
6586 + type HiddenCandidate = {
6587 + turn_item_id: string;
6588 + run_id: string;
6589 + node_id: string | null;
6590 + type: OrchestrationV2TurnItem["type"];
6591 + ordinal: number;
6592 + input_intent: NonNullable<TimelineIndexItem["inputIntent"]> | null;
6593 + };
6594 + const candidateColumns = sql`item.turn_item_id, item.run_id, item.node_id, item.type,
6595 + item.ordinal, CASE WHEN item.type = 'user_message'
6596 + THEN json_extract(item.payload_json, '$.inputIntent') END AS input_intent`;
6597 + const [rolledBackRows, conversationRows] = yield* Effect.all([
6598 + rolledBack.length === 0
6599 + ? Effect.succeed([])
6600 + : sql<HiddenCandidate>`
6601 + SELECT ${candidateColumns}
6602 + FROM json_each(${encodeIdList(rolledBack)}) AS wanted
6603 + CROSS JOIN orchestration_v2_projection_turn_items AS item
6604 + INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
6605 + ON item.thread_id = ${threadId} AND item.run_id = wanted.value`,
6606 + // The conversation index holds the type and run of these rows, so only the matches are
6607 + // fetched. Through the run index, every row of each cancelled run would be.
6608 + cancelled.length + steered.length === 0
6609 + ? Effect.succeed([])
6610 + : sql<HiddenCandidate>`
6611 + SELECT ${candidateColumns}
6612 + FROM orchestration_v2_projection_turn_items AS item
6613 + INDEXED BY orchestration_v2_projection_turn_items_conversation_idx
6614 + WHERE item.thread_id = ${threadId}
6615 + AND item.type NOT IN ${CONVERSATION_INDEX_EXCLUDED_TYPES}
6616 + AND ((item.type = 'user_message'
6617 + AND item.run_id IN (SELECT value FROM json_each(${encodeIdList(cancelled)})))
6618 + OR (item.type = 'run_interrupt_result'
6619 + AND item.run_id IN (SELECT value FROM json_each(${encodeIdList(steered)}))))`,
6620 + ]);
6621 + const hidden = [...rolledBackRows, ...conversationRows].filter(
6622 + (row) =>
6623 + !isVisible({
6624 + type: row.type,
6625 + runId: RunId.make(row.run_id),
6626 + nodeId: row.node_id === null ? null : NodeId.make(row.node_id),
6627 + ...(row.input_intent === null ? {} : { inputIntent: row.input_intent }),
6628 + }),
6629 + );
6630 + /** Counts the visible rows that sort before the given ordinal and item id. */
6631 + const visibleBefore = Effect.fn("ProjectionStore.countVisibleTimelineRows")(function* (
6632 + ordinal: number,
6633 + itemId = "",
6634 + ) {
6635 + const rows = yield* sql<{ count: number }>`
6636 + SELECT COUNT(*) AS count FROM orchestration_v2_projection_turn_items
6637 + WHERE thread_id = ${threadId} AND (ordinal, turn_item_id) < (${ordinal}, ${itemId})`;
6638 + const hiddenBefore = hidden.filter(
6639 + (row) =>
6640 + row.ordinal < ordinal ||
6641 + (row.ordinal === ordinal &&
6642 + Buffer.compare(Buffer.from(row.turn_item_id), Buffer.from(itemId)) < 0),
6643 + );
6644 + return (rows[0]?.count ?? 0) - hiddenBefore.length;
6645 + });
6646 + return {
6647 + records,
6648 + isVisible,
6649 + hidden,
6650 + visibleBefore,
6651 + read: (where: Statement.Fragment, limit?: Statement.Fragment) =>
6652 + readTimelineWindow(threadId, records.runs, includePersistentOutput, where, limit),
6653 + };
6654 + });
6655 + type LocalTimeline = NonNullable<Effect.Success<ReturnType<typeof openLocalTimeline>>>;
6656 + type IndexedTimelineRow = TimelineIndexRow & {
6657 + readonly position: number;
6658 + readonly canonicalPosition: number;
6659 + };
6660 +
6661 + /** True when a visible user turn starts before the ordinal. Reads the nearest user messages. */
6662 + const olderUserTurn = (local: LocalTimeline, ordinal: number) =>
6663 + sql<{
6664 + run_id: string | null;
6665 + node_id: string | null;
6666 + input_intent: NonNullable<TimelineIndexItem["inputIntent"]>;
6667 + }>`
6668 + SELECT item.run_id, item.node_id,
6669 + json_extract(item.payload_json, '$.inputIntent') AS input_intent
6670 + FROM orchestration_v2_projection_turn_items AS item
6671 + INDEXED BY orchestration_v2_projection_turn_items_user_message_idx
6672 + WHERE item.thread_id = ${local.records.thread.id} AND item.type = 'user_message'
6673 + AND item.ordinal < ${ordinal}
6674 + AND json_extract(item.payload_json, '$.inputIntent') IN ('turn_start', 'queued_turn')
6675 + AND json_extract(item.payload_json, '$.createdBy') = 'user'
6676 + ORDER BY item.ordinal DESC, item.turn_item_id DESC
6677 + LIMIT ${local.hidden.length + 1}`.pipe(
6678 + Effect.map((rows) =>
6679 + rows.some((row) =>
6680 + local.isVisible({
6681 + type: "user_message",
6682 + runId: row.run_id === null ? null : RunId.make(row.run_id),
6683 + nodeId: row.node_id === null ? null : NodeId.make(row.node_id),
6684 + inputIntent: row.input_intent,
6685 + }),
6686 + ),
6687 + ),
6688 + );
6689 +
6690 + /**
6691 + * A notification starts a turn only as its run's first input. For each run whose first input
6692 + * in `rows` is a notification, this finds whether an input row sits before the ordinal.
6693 + */
6694 + const runsStartedBefore = Effect.fn("ProjectionStore.runsStartedBefore")(function* (
6695 + local: LocalTimeline,
6696 + ordinal: number,
6697 + rows: ReadonlyArray<TimelineIndexRow>,
6698 + ) {
6699 + const seen = new Set<string>();
6700 + const unknown = new Set<string>();
6701 + for (const { item } of rows) {
6702 + if (item.runId === null) continue;
6703 + if (item.type !== "user_message" && item.type !== "notification") continue;
6704 + if (item.type === "notification" && !seen.has(item.runId)) unknown.add(item.runId);
6705 + seen.add(item.runId);
6706 + }
6707 + if (unknown.size === 0) return new Set<string>();
6708 + const counts = yield* sql<{ run_id: string; inputs: number }>`
6709 + SELECT wanted.value AS run_id, (
6710 + SELECT COUNT(*) FROM orchestration_v2_projection_turn_items AS input
6711 + INDEXED BY orchestration_v2_projection_turn_items_conversation_idx
6712 + WHERE input.thread_id = ${local.records.thread.id}
6713 + AND input.ordinal >= (
6714 + SELECT MIN(ordinal) FROM orchestration_v2_projection_turn_items
6715 + WHERE run_id = wanted.value
6716 + )
6717 + AND input.ordinal < ${ordinal}
6718 + AND input.type NOT IN ${CONVERSATION_INDEX_EXCLUDED_TYPES}
6719 + AND input.type IN ('user_message', 'notification')
6720 + AND input.run_id = wanted.value
6721 + ) AS inputs
6722 + FROM json_each(${encodeIdList([...unknown])}) AS wanted`;
6723 + const isHiddenInput = (row: LocalTimeline["hidden"][number], runId: string) =>
6724 + row.run_id === runId &&
6725 + row.ordinal < ordinal &&
6726 + (row.type === "user_message" || row.type === "notification");
6727 + return new Set(
6728 + counts.flatMap((row) =>
6729 + row.inputs > local.hidden.filter((hidden) => isHiddenInput(hidden, row.run_id)).length
6730 + ? [row.run_id]
6731 + : [],
6732 + ),
6733 + );
6734 + });
6735 +
6736 + /**
6737 + * Selects an older history page from the rows near its cursor. The window grows until the
6738 + * page ends inside rows whose inputs are all known, so `select` gives the page it gives on
6739 + * the full index. Null when the cursor names no visible row, which needs the full index.
6740 + */
6741 + const selectHistoryPageInWindow = Effect.fn("ProjectionStore.selectHistoryPageInWindow")(
6742 + function* <
6743 + Selection extends {
6744 + readonly page: SelectTimelinePageResult<IndexedTimelineRow>;
6745 + readonly historyItems: ReadonlyArray<IndexedTimelineRow>;
6746 + },
6747 + >(
6748 + local: LocalTimeline,
6749 + input: {
6750 + readonly cursor: string;
6751 + readonly throughEntryId: string | undefined;
6752 + readonly workMode: "deferred" | "work" | undefined;
6753 + },
6754 + select: (
6755 + indexed: ReadonlyArray<IndexedTimelineRow>,
6756 + prefix?: ThreadHistoryPrefix,
6757 + ) => Effect.Effect<Selection, ProjectionStoreV2Error>,
6758 + ) {
6759 + const threadId = local.records.thread.id;
6760 + const decoded = Result.try(() => decodeThreadHistoryCursor(input.cursor));
6761 + if (Result.isFailure(decoded) || decoded.success.st !== threadId) return null;
6762 + const cursor = decoded.success;
6763 + // Looked up by key alone: with the thread in the query, SQLite scans the thread's index.
6764 + const ends = (yield* sql<{ turn_item_id: string; thread_id: string; ordinal: number }>`
6765 + SELECT turn_item_id, thread_id, ordinal FROM orchestration_v2_projection_turn_items
6766 + WHERE turn_item_id IN (${cursor.si}, ${input.throughEntryId ?? cursor.si})`).filter(
6767 + (row) => row.thread_id === threadId,
6768 + );
6769 + const upper = ends.find((row) => row.turn_item_id === cursor.si)?.ordinal;
6770 + if (upper === undefined) return null;
6771 + /* Runs for each window this operation tries; the operation owns the span. */
6772 + const indexFrom = Effect.fnUntraced(function* (
6773 + items: ReadonlyArray<TimelineIndexItem>,
6774 + from: number | null,
6775 + ) {
6776 + const base = from === null ? 0 : yield* local.visibleBefore(from);
6777 + return items.filter(local.isVisible).map((item, offset): IndexedTimelineRow => ({
6778 + ...localTimelineRow(item),
6779 + position: base + offset,
6780 + canonicalPosition: base + offset,
6781 + }));
6782 + });
6783 + const isCursorRow = (row: TimelineIndexRow) => row.sourceItemId === cursor.si;
6784 + // The ordinals come from the stop request index, so this does not walk the thread.
6785 + const interruptRequests = local
6786 + .read(
6787 + sql`AND item.type = 'run_interrupt_request' AND item.ordinal IN (
6788 + SELECT ordinal FROM orchestration_v2_projection_turn_items
6789 + INDEXED BY orchestration_v2_projection_turn_items_stop_request_idx
6790 + WHERE thread_id = ${threadId} AND type = 'run_interrupt_request')`,
6791 + )
6792 + .pipe(Effect.flatMap((items) => indexFrom(items, null)));
6793 +
6794 + if (input.workMode === "work") {
6795 + // A work group lies between its first row and the cursor, so that range is the read.
6796 + const lower = ends.find((row) => row.turn_item_id === input.throughEntryId)?.ordinal;
6797 + if (lower === undefined) return null;
6798 + const indexed = yield* indexFrom(
6799 + yield* local.read(sql`AND item.ordinal >= ${lower} AND item.ordinal <= ${upper}`),
6800 + lower,
6801 + );
6802 + if (!indexed.some(isCursorRow)) return null;
6803 + if (!indexed.some((row) => row.sourceItemId === input.throughEntryId)) return null;
6804 + return {
6805 + indexed,
6806 + selection: yield* select(indexed),
6807 + olderUserTurn: olderUserTurn(local, lower),
6808 + interruptRequests,
6809 + };
6810 + }
6811 +
6812 + const rowsBelow = (ordinal: number, count: number) =>
6813 + sql<{ ordinal: number }>`
6814 + SELECT ordinal FROM orchestration_v2_projection_turn_items
6815 + WHERE thread_id = ${threadId} AND ordinal < ${ordinal}
6816 + ORDER BY ordinal DESC LIMIT 1 OFFSET ${count - 1}`.pipe(
6817 + Effect.map((rows) => rows[0]?.ordinal ?? null),
6818 + );
6819 + // `trusted` is the lowest ordinal the page may reach. `lower` can sit below it, because
6820 + // a run that folds work is read whole.
6821 + let step = THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT;
6822 + let trusted = yield* rowsBelow(upper, step);
6823 + let lower = trusted;
6824 + let higher = upper;
6825 + let items = yield* local.read(
6826 + lower === null
6827 + ? sql`AND item.ordinal <= ${upper}`
6828 + : sql`AND item.ordinal >= ${lower} AND item.ordinal <= ${upper}`,
6829 + );
6830 + const wholeRuns = new Set<string>();
6831 + while (true) {
6832 + if (input.workMode === "deferred") {
6833 + // Folding needs every row of a run: its stop and error rows, and its group sizes.
6834 + const folding = new Set<string>();
6835 + for (const item of items) {
6836 + if (item.runId === null || wholeRuns.has(item.runId)) continue;
6837 + if (item.ordinal > upper || (trusted !== null && item.ordinal < trusted)) continue;
6838 + if (isFoldableWorkItem(item) && local.isVisible(item)) folding.add(item.runId);
6839 + }
6840 + if (folding.size > 0) {
6841 + const runs = encodeIdList([...folding]);
6842 + const [extent] = yield* sql<{ lower: number; higher: number }>`
6843 + SELECT MIN(run.lower) AS lower, MAX(run.higher) AS higher FROM (
6844 + SELECT
6845 + (SELECT MIN(ordinal) FROM orchestration_v2_projection_turn_items
6846 + WHERE run_id = wanted.value) AS lower,
6847 + (SELECT MAX(ordinal) FROM orchestration_v2_projection_turn_items
6848 + WHERE run_id = wanted.value) AS higher
6849 + FROM json_each(${runs}) AS wanted
6850 + ) AS run`;
6851 + if (extent !== undefined && lower !== null && extent.lower < lower) {
6852 + const below = yield* local.read(
6853 + sql`AND item.ordinal >= ${extent.lower} AND item.ordinal < ${lower}`,
6854 + );
6855 + items = below.concat(items);
6856 + lower = extent.lower;
6857 + }
6858 + if (extent !== undefined && extent.higher > higher) {
6859 + const newer = yield* local.read(
6860 + sql`AND item.ordinal > ${higher} AND item.ordinal <= ${extent.higher}`,
6861 + );
6862 + items = items.concat(newer);
6863 + higher = extent.higher;
6864 + }
6865 + for (const runId of folding) wholeRuns.add(runId);
6866 + }
6867 + }
6868 + const indexed = yield* indexFrom(items, lower);
6869 + const end = indexed.findIndex(isCursorRow);
6870 + if (end < 0) return null;
6871 + const older = lower === null ? Effect.succeed(false) : olderUserTurn(local, lower);
6872 + const prefix: ThreadHistoryPrefix | undefined =
6873 + lower === null
6874 + ? undefined
6875 + : {
6876 + hasUserTurn: indexed
6877 + .slice(0, end)
6878 + .some((row) => isThreadHistoryUserTurn(row.item))
6879 + ? false
6880 + : yield* older,
6881 + startedRuns: yield* runsStartedBefore(local, lower, indexed),
6882 + };
6883 + const selection = yield* select(indexed, prefix);
6884 + // Without its row the cursor falls back to a position, which only the full index has.
6885 + if (!selection.historyItems.some(isCursorRow)) return null;
6886 + const oldest = selection.page.oldestScanned;
6887 + if (
6888 + lower === null ||
6889 + (trusted !== null &&
6890 + oldest !== undefined &&
6891 + oldest !== selection.historyItems[0] &&
6892 + oldest.item.ordinal >= trusted)
6893 + ) {
6894 + return { indexed, selection, olderUserTurn: older, interruptRequests };
6895 + }
6896 + step *= 2;
6897 + trusted = trusted === null ? null : yield* rowsBelow(trusted, step);
6898 + if (trusted === null || trusted < lower) {
6899 + const below = yield* local.read(
6900 + trusted === null
6901 + ? sql`AND item.ordinal < ${lower}`
6902 + : sql`AND item.ordinal >= ${trusted} AND item.ordinal < ${lower}`,
6903 + );
6904 + items = below.concat(items);
6905 + lower = trusted;
6906 + }
6907 + }
6908 + },
6909 + );
6910 +
6911 const getThreadHistoryPage: ProjectionStoreV2Shape["getThreadHistoryPage"] = (
6912 threadId,
6913 cursor,
@@ -6461,8 +6918,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6918 sql
6919 .withTransaction(
6920 Effect.gen(function* () {
6464 - const index = yield* readSnapshotTimelineIndex(threadId, workMode !== undefined);
6465 - if (index.records.thread.deletedAt !== null)
6921 + const withWork = workMode !== undefined;
6922 + // A jump to a target entry selects from the whole index.
6923 + const local =
6924 + (workMode === "work") === (throughEntryId !== undefined)
6925 + ? yield* openLocalTimeline(threadId, withWork)
6926 + : null;
6927 + const full =
6928 + local === null ? yield* readSnapshotTimelineIndex(threadId, withWork) : null;
6929 + if ((local ?? full)?.records.thread.deletedAt !== null)
6930 return yield* new ProjectionStoreThreadNotFoundError({ threadId });
6931 const revisions = yield* sql<{
6932 sequence: number;
@@ -6470,100 +6934,132 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6934 FROM orchestration_events WHERE application_event_version = 2
6935 AND aggregate_kind = 'thread' AND stream_id = ${threadId}`;
6936 const snapshotSequence = revisions[0]?.sequence ?? 0;
6473 - const indexed = index.visible.map((row, position) => ({
6474 - ...row,
6475 - position,
6476 - canonicalPosition: position,
6477 - }));
6478 - const { olderPolicy, cursorPosition } = yield* Effect.try({
6479 - try: () => ({
6480 - olderPolicy: olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId }),
6481 - // The cursor's own row, or the row its stale position falls back to.
6482 - cursorPosition: Math.min(
6483 - findCursorEndIndex(indexed, decodeThreadHistoryCursor(cursor)),
6484 - indexed.length - 1,
6485 - ),
6486 - }),
6487 - catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
6488 - });
6489 - const selectPage = (rows: typeof indexed) =>
6937 + const select = (
6938 + indexed: ReadonlyArray<IndexedTimelineRow>,
6939 + prefix?: ThreadHistoryPrefix,
6940 + ) =>
6941 Effect.try({
6491 - try: () =>
6492 - workMode === "work"
6493 - ? selectDeferredWorkPage({
6494 - items: rows,
6495 - cursor,
6496 - throughEntryId,
6497 - snapshotSequence,
6498 - rowEncodedBytes: () => 0,
6499 - })
6500 - : selectHistoryPageFromCursor({
6501 - items: workMode === "deferred" ? deferWorkHistoryRows(rows) : rows,
6502 - cursor,
6503 - snapshotSequence,
6504 - rowEncodedBytes: () => 0,
6505 - policy: olderPolicy,
6506 - }),
6942 + try: () => {
6943 + const historyItems =
6944 + workMode === "deferred" ? deferWorkHistoryRows(indexed) : indexed;
6945 + const policy = olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId });
6946 + const page =
6947 + workMode === "work"
6948 + ? selectDeferredWorkPage({
6949 + items: indexed,
6950 + cursor,
6951 + throughEntryId,
6952 + snapshotSequence,
6953 + rowEncodedBytes: () => 0,
6954 + })
6955 + : selectHistoryPageFromCursor({
6956 + items: historyItems,
6957 + cursor,
6958 + snapshotSequence,
6959 + rowEncodedBytes: () => 0,
6960 + policy,
6961 + prefix,
6962 + });
6963 + return { policy, page, historyItems };
6964 + },
6965 catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
6966 });
6509 - let page = yield* selectPage(indexed);
6510 - // Inline output is found by scanning payloads, which costs a whole long thread several
6511 - // seconds. Scan only the runs the page spans, then select again with what they hold.
6512 - const scannedRuns = new Set<string>();
6513 - const persistentOutputs = new Set<string>();
6514 - while (workMode !== undefined) {
6515 - // Rows hidden in a work group move the page, so the scan always reaches the cursor.
6516 - const span = [...page.items.slice(0, 1), ...page.items.slice(-1)].map(
6517 - (row) => row.canonicalPosition,
6518 - );
6519 - if (cursorPosition >= 0) span.push(cursorPosition);
6520 - if (span.length === 0) break;
6521 - const runs = new Map(
6522 - // A work page holds rows of one run, which can be the rows that have no run.
6523 - (workMode === "work"
6524 - ? page.items.slice(0, 1)
6525 - : indexed.slice(Math.min(...span), Math.max(...span) + 1)
6526 - ).flatMap(({ sourceThreadId, item }) =>
6527 - (workMode === "work" ||
6528 - (item.runId !== null &&
6529 - (item.runStatus === "completed" || item.attemptStatus === "superseded"))) &&
6530 - !scannedRuns.has(`${sourceThreadId}:${item.runId}`)
6531 - ? [
6532 - [
6533 - `${sourceThreadId}:${item.runId}`,
6534 - { threadId: sourceThreadId, id: item.runId },
6535 - ] as const,
6536 - ]
6537 - : [],
6538 - ),
6539 - );
6540 - if (runs.size === 0) break;
6541 - for (const run of runs.keys()) scannedRuns.add(run);
6542 - const found = yield* sql<{ thread_id: string; turn_item_id: string }>`
6967 + const window =
6968 + local === null
6969 + ? null
6970 + : yield* selectHistoryPageInWindow(
6971 + local,
6972 + { cursor, throughEntryId, workMode },
6973 + select,
6974 + );
6975 + const whole = Effect.gen(function* () {
6976 + const index = full ?? (yield* readSnapshotTimelineIndex(threadId, withWork));
6977 + const indexed = index.visible.map((row, position): IndexedTimelineRow => ({
6978 + ...row,
6979 + position,
6980 + canonicalPosition: position,
6981 + }));
6982 + let selection = yield* select(indexed);
6983 + // The full index does not say which rows hold inline output: finding it scans
6984 + // payloads, which costs a whole long thread several seconds. Scan only the runs
6985 + // the page spans, then select again with what they hold.
6986 + const scannedRuns = new Set<string>();
6987 + const persistentOutputs = new Set<string>();
6988 + // Rows hidden in a work group move the page, so the scan always reaches the
6989 + // cursor's own row, or the row its stale position falls back to.
6990 + const cursorPosition =
6991 + workMode === undefined
6992 + ? -1
6993 + : Math.min(
6994 + findCursorEndIndex(indexed, decodeThreadHistoryCursor(cursor)),
6995 + indexed.length - 1,
6996 + );
6997 + while (workMode !== undefined) {
6998 + const { page } = selection;
6999 + const span = [...page.items.slice(0, 1), ...page.items.slice(-1)].map(
7000 + (row) => row.canonicalPosition,
7001 + );
7002 + if (cursorPosition >= 0) span.push(cursorPosition);
7003 + if (span.length === 0) break;
7004 + const runs = new Map(
7005 + // A work page holds rows of one run, which can be the rows that have no run.
7006 + (workMode === "work"
7007 + ? page.items.slice(0, 1)
7008 + : indexed.slice(Math.min(...span), Math.max(...span) + 1)
7009 + ).flatMap(({ sourceThreadId, item }) =>
7010 + (workMode === "work" ||
7011 + (item.runId !== null &&
7012 + (item.runStatus === "completed" || item.attemptStatus === "superseded"))) &&
7013 + !scannedRuns.has(`${sourceThreadId}:${item.runId}`)
7014 + ? [
7015 + [
7016 + `${sourceThreadId}:${item.runId}`,
7017 + { threadId: sourceThreadId, id: item.runId },
7018 + ] as const,
7019 + ]
7020 + : [],
7021 + ),
7022 + );
7023 + if (runs.size === 0) break;
7024 + for (const run of runs.keys()) scannedRuns.add(run);
7025 + const found = yield* sql<{ thread_id: string; turn_item_id: string }>`
7026 SELECT item.thread_id, item.turn_item_id
7027 FROM json_each(${yield* encodeTimelineSources([...runs.values()])}) AS wanted
7028 CROSS JOIN orchestration_v2_projection_turn_items AS item
7029 ON item.thread_id = json_extract(wanted.value, '$.threadId')
7030 AND item.run_id IS json_extract(wanted.value, '$.id')
7031 WHERE item.status = 'completed' AND ${inlineWorkOutput}`;
6549 - if (found.length === 0) break;
6550 - for (const row of found)
6551 - persistentOutputs.add(`${row.thread_id}:${row.turn_item_id}`);
6552 - page = yield* selectPage(
6553 - indexed.map((row) =>
6554 - persistentOutputs.has(`${row.sourceThreadId}:${row.sourceItemId}`)
6555 - ? { ...row, item: { ...row.item, persistentOutput: true } }
6556 - : row,
6557 - ),
6558 - );
6559 - }
6560 - const policy = workMode === "work" ? THREAD_HISTORY_PAGE_POLICY : olderPolicy;
7032 + if (found.length === 0) break;
7033 + for (const row of found)
7034 + persistentOutputs.add(`${row.thread_id}:${row.turn_item_id}`);
7035 + selection = yield* select(
7036 + indexed.map((row) =>
7037 + persistentOutputs.has(`${row.sourceThreadId}:${row.sourceItemId}`)
7038 + ? { ...row, item: { ...row.item, persistentOutput: true } }
7039 + : row,
7040 + ),
7041 + );
7042 + }
7043 + return {
7044 + indexed,
7045 + selection,
7046 + olderUserTurn: Effect.succeed(false),
7047 + interruptRequests: Effect.succeed(indexed),
7048 + };
7049 + });
7050 + const { indexed, selection, olderUserTurn, interruptRequests } =
7051 + window ?? (yield* whole);
7052 + const { page } = selection;
7053 + const policy = workMode === "work" ? THREAD_HISTORY_PAGE_POLICY : selection.policy;
7054 const end = (page.items.at(-1)?.canonicalPosition ?? -1) + 1;
6562 - const hasUserTurns = index.visible
6563 - .slice(0, end)
6564 - .some((row) => isThreadHistoryUserTurn(row.item));
7055 + const hasUserTurns =
7056 + conversationOnly === true &&
7057 + (indexed.some(
7058 + (row) => row.canonicalPosition < end && isThreadHistoryUserTurn(row.item),
7059 + ) ||
7060 + (yield* olderUserTurn));
7061 const materialize = Effect.fn("ProjectionStore.materializeHistoryRows")(function* (
6566 - rows: ReadonlyArray<(typeof indexed)[number]>,
7062 + rows: ReadonlyArray<IndexedTimelineRow>,
7063 ) {
7064 if (rows.length === 0) return [];
7065 const sources = yield* encodeTimelineSources(
@@ -6606,7 +7102,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7102 );
7103 });
7104 const items = yield* materialize(
6609 - conversationOnly && hasUserTurns
7105 + hasUserTurns
7106 ? page.items.filter((row) => isConversationHistoryItem(row.item))
7107 : page.items,
7108 );
@@ -6614,7 +7110,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7110 const requests =
7111 workMode === "work"
7112 ? []
6617 - : yield* materialize(missingInterruptRequests(indexed, bounded.items));
7113 + : yield* materialize(
7114 + missingInterruptRequests(
7115 + bounded.items.some((row) => row.item.type === "run_interrupt_result")
7116 + ? yield* interruptRequests
7117 + : [],
7118 + bounded.items,
7119 + ),
7120 + );
7121 const pageItems = [...requests, ...bounded.items].map((row, position) => ({
7122 ...row,
7123 position,
@@ -6631,39 +7134,118 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7134 )
7135 .pipe(Effect.mapError(controlReadError(threadId)));
7136
7137 + /**
7138 + * The rows `getTimelinePage` matches, cut after the first `limit + 1`, with their positions
7139 + * in the full visible index. Reads only the rows from the start position to the page end.
7140 + */
7141 + const matchLocalTimelineRows = Effect.fn("ProjectionStore.matchLocalTimelineRows")(function* (
7142 + local: LocalTimeline,
7143 + options: ProjectionTimelinePageOptions,
7144 + ) {
7145 + const threadId = local.records.thread.id;
7146 + const totals = yield* sql<{ count: number }>`
7147 + SELECT COUNT(*) AS count FROM orchestration_v2_projection_turn_items
7148 + WHERE thread_id = ${threadId}`;
7149 + const totalItems = (totals[0]?.count ?? 0) - local.hidden.length;
7150 + if (options.itemId !== undefined) {
7151 + const found = (yield* local.read(sql`AND item.turn_item_id = ${options.itemId}`)).filter(
7152 + local.isVisible,
7153 + );
7154 + const matching = yield* Effect.forEach(found, (item) =>
7155 + local
7156 + .visibleBefore(item.ordinal, item.id)
7157 + .pipe(Effect.map((position) => ({ ...localTimelineRow(item), position }))),
7158 + );
7159 + return { matching, totalItems };
7160 + }
7161 + const afterPosition = options.afterPosition ?? -1;
7162 + const firstPosition = Math.max(afterPosition + 1, 0);
7163 + // The row at this raw offset sorts at or before the first wanted row: hidden rows only
7164 + // push visible rows to later offsets.
7165 + const starts = yield* sql<{ ordinal: number }>`
7166 + SELECT ordinal FROM orchestration_v2_projection_turn_items
7167 + WHERE thread_id = ${threadId}
7168 + ORDER BY ordinal ASC, turn_item_id ASC LIMIT 1 OFFSET ${firstPosition}`;
7169 + const start = starts[0]?.ordinal;
7170 + if (start === undefined) return { matching: [], totalItems };
7171 + const base = yield* local.visibleBefore(start);
7172 + const hiddenAfter = local.hidden.filter((row) => row.ordinal >= start).length;
7173 + const messagesOnly = options.view !== "activity";
7174 + const candidates = (yield* local.read(
7175 + messagesOnly
7176 + ? sql`AND item.ordinal >= ${start}
7177 + AND item.type IN ('user_message', 'assistant_message', 'proposed_plan')`
7178 + : sql`AND item.ordinal >= ${start}`,
7179 + sql`LIMIT ${firstPosition - base + Math.min(options.limit, totalItems) + 1 + hiddenAfter}`,
7180 + )).filter(local.isVisible);
7181 + const positions = new Map<string, number>();
7182 + const last = candidates.at(-1);
7183 + if (messagesOnly && last !== undefined) {
7184 + const hiddenIds = new Set(local.hidden.map((row) => row.turn_item_id));
7185 + const span = yield* sql<{ turn_item_id: string }>`
7186 + SELECT turn_item_id FROM orchestration_v2_projection_turn_items
7187 + WHERE thread_id = ${threadId} AND ordinal >= ${start} AND ordinal <= ${last.ordinal}
7188 + ORDER BY ordinal ASC, turn_item_id ASC`;
7189 + for (const row of span) {
7190 + if (!hiddenIds.has(row.turn_item_id))
7191 + positions.set(row.turn_item_id, base + positions.size);
7192 + }
7193 + }
7194 + const matching = candidates
7195 + .map((item, offset) => ({
7196 + ...localTimelineRow(item),
7197 + position: messagesOnly ? (positions.get(item.id) ?? -1) : base + offset,
7198 + }))
7199 + .filter((row) => row.position > afterPosition);
7200 + return { matching, totalItems };
7201 + });
7202 +
7203 const getTimelinePage: ProjectionStoreV2Shape["getTimelinePage"] = (threadId, options) =>
7204 sql
7205 .withTransaction(
7206 Effect.gen(function* () {
6638 - const index = yield* readSnapshotTimelineIndex(threadId, false);
6639 - const attachmentOwners =
6640 - options.attachmentId === undefined
6641 - ? undefined
6642 - : new Set(
6643 - (yield* sql<{ thread_id: string; turn_item_id: string }>`
7207 + const local =
7208 + options.attachmentId === undefined &&
7209 + Number.isSafeInteger(options.limit) &&
7210 + options.limit >= 0 &&
7211 + Number.isSafeInteger(options.afterPosition ?? -1)
7212 + ? yield* openLocalTimeline(threadId)
7213 + : null;
7214 + const whole = Effect.gen(function* () {
7215 + const index = yield* readSnapshotTimelineIndex(threadId, false);
7216 + const attachmentOwners =
7217 + options.attachmentId === undefined
7218 + ? undefined
7219 + : new Set(
7220 + (yield* sql<{ thread_id: string; turn_item_id: string }>`
7221 SELECT item.thread_id, item.turn_item_id
7222 FROM orchestration_v2_projection_turn_items AS item,
7223 json_each(item.payload_json, '$.attachments') AS attachment
7224 WHERE item.thread_id IN ${sql.in([...new Set(index.visible.map((row) => row.sourceThreadId))])}
7225 AND item.type = 'user_message'
7226 AND json_extract(attachment.value, '$.id') = ${options.attachmentId}`).map(
6650 - (row) => `${row.thread_id}:${row.turn_item_id}`,
6651 - ),
6652 - );
6653 - const matching = index.visible
6654 - .map((row, position) => ({ ...row, position }))
6655 - .filter((row) =>
6656 - options.itemId === undefined
6657 - ? row.position > (options.afterPosition ?? -1) &&
6658 - (options.view === "activity" ||
6659 - ["user_message", "assistant_message", "proposed_plan"].includes(
6660 - row.item.type,
6661 - ))
6662 - : row.sourceItemId === options.itemId,
6663 - )
6664 - .filter(
6665 - (row) => attachmentOwners?.has(`${row.sourceThreadId}:${row.sourceItemId}`) ?? true,
6666 - );
7227 + (row) => `${row.thread_id}:${row.turn_item_id}`,
7228 + ),
7229 + );
7230 + const matching = index.visible
7231 + .map((row, position) => ({ ...row, position }))
7232 + .filter((row) =>
7233 + options.itemId === undefined
7234 + ? row.position > (options.afterPosition ?? -1) &&
7235 + (options.view === "activity" ||
7236 + ["user_message", "assistant_message", "proposed_plan"].includes(
7237 + row.item.type,
7238 + ))
7239 + : row.sourceItemId === options.itemId,
7240 + )
7241 + .filter(
7242 + (row) =>
7243 + attachmentOwners?.has(`${row.sourceThreadId}:${row.sourceItemId}`) ?? true,
7244 + );
7245 + return { matching, totalItems: index.visible.length };
7246 + });
7247 + const { matching, totalItems } =
7248 + local === null ? yield* whole : yield* matchLocalTimelineRows(local, options);
7249 const page = matching.slice(0, options.limit);
7250 const sources = yield* encodeTimelineSources(
7251 page
@@ -6713,11 +7295,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
7295 };
7296 }),
7297 );
6716 - return {
6717 - items,
6718 - totalItems: index.visible.length,
6719 - hasMore: matching.length > page.length,
6720 - };
7298 + return { items, totalItems, hasMore: matching.length > page.length };
7299 }),
7300 )
7301 .pipe(
apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
+32
@@ -396,6 +396,38 @@ describe("threadHistoryPaging", () => {
396 expect([...older.items, ...first.items].map((row) => row.sourceItemId)).toEqual(
397 items.map((row) => row.sourceItemId),
398 );
399 +
400 + // A caller that holds only the newest rows gets the same page when it says what it left
401 + // out: a user turn, and run 3, whose late notification is then not a new turn.
402 + const late = items.with(402, inRun(402, 3, notification("Late")));
403 + const cursor = encodeThreadHistoryCursor({
404 + snapshotSequence: 1,
405 + sourceThreadId: THREAD,
406 + sourceItemId: late.at(-1)!.sourceItemId,
407 + position: late.length - 1,
408 + });
409 + const prefix = { hasUserTurn: true, startedRuns: new Set(["run-3"]) };
410 + const whole = selectHistoryPageFromCursor({ items: late, cursor, snapshotSequence: 1, policy });
411 + expect(whole.items[0]?.sourceItemId).toBe(`item-${11 * 4 + 1}`);
412 + expect(
413 + selectHistoryPageFromCursor({
414 + items: late.slice(22),
415 + cursor,
416 + snapshotSequence: 1,
417 + policy,
418 + prefix,
419 + }),
420 + ).toEqual({ ...whole, oldestScanned: late[45] });
421 + // A part that ends above the page reports its first row, so the caller reads further back.
422 + expect(
423 + selectHistoryPageFromCursor({
424 + items: late.slice(60),
425 + cursor,
426 + snapshotSequence: 1,
427 + policy,
428 + prefix,
429 + }).oldestScanned,
430 + ).toBe(late[60]);
431 });
432
433 it("pages agent-only child transcripts instead of dropping their earlier activity", () => {
apps/server/src/orchestration-v2/threadHistoryPaging.ts
+42 -12
@@ -72,10 +72,22 @@ type HistoryRow = Pick<
72 readonly deferredWork?: OrchestrationV2TurnItem["deferredWork"];
73 };
74 };
75 +/** What a caller that passes only the newest part of a history knows about the rows it left out. */
76 +export type ThreadHistoryPrefix = {
77 + readonly hasUserTurn: boolean;
78 + /** Runs that have a user message or notification among the rows left out. */
79 + readonly startedRuns: ReadonlySet<string>;
80 +};
81 +
82 export type SelectTimelinePageResult<Row extends HistoryRow = OrchestrationV2ProjectedTurnItem> = {
83 readonly items: ReadonlyArray<Row>;
84 readonly nextCursor: string | null;
85 readonly hasMoreHistory: boolean;
86 + /**
87 + * Set with `prefix`: the oldest row the selection read, which can be older than `items[0]`.
88 + * The page is the same as on the full history only when a row lies before this one.
89 + */
90 + readonly oldestScanned?: Row | undefined;
91 };
92
93 export type BoundedProjectionResult = {
@@ -200,6 +212,15 @@ function isDeferrableWorkItem(item: HistoryRow["item"]): boolean {
212 );
213 }
214
215 +/** A row that folds when its run is not blocked and its group is large enough. */
216 +export function isFoldableWorkItem(item: HistoryRow["item"]): boolean {
217 + return (
218 + item.runId != null &&
219 + (item.runStatus === "completed" || item.attemptStatus === "superseded") &&
220 + isDeferrableWorkItem(item)
221 + );
222 +}
223 +
224 export function deferWorkHistoryRows<Row extends HistoryRow>(items: ReadonlyArray<Row>): Row[] {
225 const blocked = new Set(
226 items.flatMap(({ item }) =>
@@ -217,12 +238,7 @@ export function deferWorkHistoryRows<Row extends HistoryRow>(items: ReadonlyArra
238 const item = row.item;
239 if (item.runId == null) continue;
240 if (item.type === "user_message") segments.set(item.runId, (segments.get(item.runId) ?? 0) + 1);
220 - if (
221 - (item.runStatus !== "completed" && item.attemptStatus !== "superseded") ||
222 - blocked.has(item.runId) ||
223 - !isDeferrableWorkItem(item)
224 - )
225 - continue;
241 + if (blocked.has(item.runId) || !isFoldableWorkItem(item)) continue;
242 const key = `${row.sourceThreadId}:${item.runId}:${segments.get(item.runId) ?? 0}:${item.attemptId ?? ""}`;
243 const group = groups.get(key) ?? [];
244 group.push(row);
@@ -287,8 +303,11 @@ export function selectDeferredWorkPage<Row extends HistoryRow>(input: {
303 }
304
305 /** Notifications can start wake turns when they are the run's first input; match ProjectionStore run_starts. */
290 -function threadHistoryTurnStarts(items: ReadonlyArray<HistoryRow["item"]>): ReadonlyArray<boolean> {
291 - const startedRuns = new Set<string>();
306 +function threadHistoryTurnStarts(
307 + items: ReadonlyArray<HistoryRow["item"]>,
308 + earlierRuns?: ReadonlySet<string>,
309 +): ReadonlyArray<boolean> {
310 + const startedRuns = new Set<string>(earlierRuns);
311 return items.map((item) => {
312 if (item.type !== "user_message" && item.type !== "notification") return false;
313 const firstInput = item.runId != null && !startedRuns.has(item.runId);
@@ -312,6 +331,7 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
331 readonly snapshotSequence: number;
332 readonly policy?: ThreadHistoryPagePolicy | undefined;
333 readonly rowEncodedBytes?: ((row: Row) => number) | undefined;
334 + readonly prefix?: ThreadHistoryPrefix | undefined;
335 }): SelectTimelinePageResult<Row> {
336 const policy = input.policy ?? THREAD_HISTORY_PAGE_POLICY;
337 const rowCost = input.rowEncodedBytes ?? projectedRowEncodedBytes;
@@ -325,10 +345,15 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
345 let userTurns = 0;
346 let rawTurns = 0;
347 const candidates = input.items.slice(0, end);
328 - const turnStarts = threadHistoryTurnStarts(candidates.map((row) => row.item));
329 - const turnLimit = candidates.some((row) => isThreadHistoryUserTurn(row.item))
330 - ? policy.maxUserTurns
331 - : undefined;
348 + const turnStarts = threadHistoryTurnStarts(
349 + candidates.map((row) => row.item),
350 + input.prefix?.startedRuns,
351 + );
352 + const turnLimit =
353 + input.prefix?.hasUserTurn === true ||
354 + candidates.some((row) => isThreadHistoryUserTurn(row.item))
355 + ? policy.maxUserTurns
356 + : undefined;
357 const maxItems =
358 turnLimit === undefined ? policy.maxItems : (policy.maxTurnItems ?? policy.maxItems);
359 for (let index = end - 1; index >= 0; index -= 1) {
@@ -351,6 +376,7 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
376 // Inside a turn longer than the window, no selected row starts a user turn.
377 // Later pages and the snapshot query see only such rows and use `maxItems`,
378 // so this page uses it too.
379 + const oldestScanned = selected.at(-1);
380 if (userTurns === 0) selected.splice(policy.maxItems);
381 selected.reverse();
382
@@ -376,6 +402,7 @@ function selectOlderTimelinePage<Row extends HistoryRow>(input: {
402 items: renumberPositions(selected),
403 nextCursor,
404 hasMoreHistory,
405 + ...(input.prefix === undefined ? {} : { oldestScanned }),
406 };
407 }
408
@@ -458,6 +485,8 @@ export function selectHistoryPageFromCursor<Row extends HistoryRow>(input: {
485 readonly policy?: ThreadHistoryPagePolicy | undefined;
486 readonly rowEncodedBytes?: ((row: Row) => number) | undefined;
487 readonly throughEntryId?: string | undefined;
488 + /** Set when `items` holds only the newest part of the history. */
489 + readonly prefix?: ThreadHistoryPrefix | undefined;
490 }): SelectTimelinePageResult<Row> {
491 const decoded = decodeThreadHistoryCursor(input.cursor);
492 return selectOlderTimelinePage({
@@ -466,6 +495,7 @@ export function selectHistoryPageFromCursor<Row extends HistoryRow>(input: {
495 snapshotSequence: input.snapshotSequence,
496 ...(input.rowEncodedBytes === undefined ? {} : { rowEncodedBytes: input.rowEncodedBytes }),
497 policy: olderHistoryPagePolicy(input),
498 + prefix: input.prefix,
499 });
500 }
501