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

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

fix(server): load long thread history in bounded pages (#26240)

Yash Singh committed Oct 28, 2026 at 05:53 UTC d2429f733c30772ab5ef8818685615e656ec432f
30 files changed +1607 -254
apps/mobile/src/features/threads/ThreadFeed.tsx
+95 -24
@@ -1208,6 +1208,92 @@ function compactionLabel(label: string, title: string | undefined) {
1208 : label;
1209 }
1210
1211 +function RunFoldButton({
1212 + entry,
1213 + environmentId,
1214 + threadId,
1215 + onToggle,
1216 + rowSizing,
1217 + iconColor,
1218 +}: {
1219 + entry: Extract<ThreadFeedEntry, { type: "run-fold" }>;
1220 + environmentId: EnvironmentId;
1221 + threadId: ThreadId;
1222 + onToggle: (runId: RunId) => void;
1223 + rowSizing: ReturnType<typeof deriveThreadWorkLogSizing>;
1224 + iconColor: ColorValue;
1225 +}) {
1226 + const loadWork = useAtomCommand(threadEnvironment.loadEarlierHistory, { reportFailure: false });
1227 + const anchorsKey = entry.deferredWorkItemIds?.join("\n") ?? "";
1228 + const [failed, setFailed] = useState(false);
1229 + const load = () => {
1230 + if (!anchorsKey) return;
1231 + void Promise.all(
1232 + anchorsKey
1233 + .split("\n")
1234 + .map((workAnchorId) => loadWork({ environmentId, input: { threadId, workAnchorId } })),
1235 + ).then((results) =>
1236 + setFailed(
1237 + results.some((result) => result._tag === "Failure" || result.value._tag === "error"),
1238 + ),
1239 + );
1240 + };
1241 + const loadOnExpand = useEffectEvent(load);
1242 + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Restored expanded folds load their deferred server history on mount.
1243 + useEffect(() => {
1244 + if (entry.expanded && anchorsKey && environmentId && threadId) loadOnExpand();
1245 + }, [entry.expanded, anchorsKey, environmentId, threadId]);
1246 + const error = anchorsKey && failed ? "Couldn't load work. Tap to retry." : null;
1247 + const loading = entry.expanded && anchorsKey !== "" && !failed;
1248 + const toggle = () => {
1249 + setFailed(false);
1250 + if (error && entry.expanded) load();
1251 + else onToggle(entry.runId);
1252 + };
1253 +
1254 + return (
1255 + <View className="min-w-0 flex-1">
1256 + <Pressable
1257 + accessibilityRole="button"
1258 + accessibilityState={{ expanded: entry.expanded, busy: loading }}
1259 + onPress={toggle}
1260 + hitSlop={4}
1261 + className="min-h-11 min-w-0 flex-1 flex-row items-center gap-2"
1262 + style={{ minHeight: Math.max(TURN_FOLD_HEIGHT - 3.5, rowSizing.estimatedRowHeight) }}
1263 + >
1264 + <Text
1265 + key={rowSizing.textSizeKey}
1266 + numberOfLines={1}
1267 + className="shrink font-t3-medium text-sm tabular-nums text-foreground-muted"
1268 + >
1269 + {entry.label}
1270 + {error ? ` · ${error}` : ""}
1271 + </Text>
1272 + <ThreadDisclosureChevron
1273 + expanded={entry.expanded}
1274 + collapsedDirection="right"
1275 + size={15}
1276 + tintColor={iconColor}
1277 + />
1278 + </Pressable>
1279 + {loading ? (
1280 + <View
1281 + accessible={false}
1282 + importantForAccessibility="no-hide-descendants"
1283 + className="gap-2 px-1 pb-2"
1284 + >
1285 + {(["40%", "60%", "33%"] as const).map((width) => (
1286 + <View key={width} className="flex-row items-center gap-2">
1287 + <View className="size-3 rounded bg-subtle-strong" />
1288 + <View className="h-2.5 rounded-full bg-subtle-strong" style={{ width }} />
1289 + </View>
1290 + ))}
1291 + </View>
1292 + ) : null}
1293 + </View>
1294 + );
1295 +}
1296 +
1297 function renderFeedEntry(
1298 info: { item: ThreadFeedRow; index: number },
1299 props: Pick<
@@ -1279,30 +1365,15 @@ function renderFeedEntry(
1365 if (entry.type === "run-fold") {
1366 return (
1367 <View className="mb-1 flex-row items-center border-b border-border-subtle px-2">
1282 - <Pressable
1283 - accessibilityRole="button"
1284 - accessibilityState={{ expanded: entry.expanded }}
1285 - onPress={() => props.onToggleTurnFold(entry.runId)}
1286 - hitSlop={4}
1287 - className="min-h-11 min-w-0 flex-1 flex-row items-center gap-2"
1288 - style={{
1289 - minHeight: Math.max(TURN_FOLD_HEIGHT - 3.5, props.workRowSizing.estimatedRowHeight),
1290 - }}
1291 - >
1292 - <Text
1293 - key={props.workRowSizing.textSizeKey}
1294 - numberOfLines={1}
1295 - className="shrink font-t3-medium text-sm tabular-nums text-foreground-muted"
1296 - >
1297 - {entry.label}
1298 - </Text>
1299 - <ThreadDisclosureChevron
1300 - expanded={entry.expanded}
1301 - collapsedDirection="right"
1302 - size={15}
1303 - tintColor={iconSubtleColor}
1304 - />
1305 - </Pressable>
1368 + <RunFoldButton
1369 + key={entry.id}
1370 + entry={entry}
1371 + environmentId={props.environmentId}
1372 + threadId={props.threadId}
1373 + onToggle={props.onToggleTurnFold}
1374 + rowSizing={props.workRowSizing}
1375 + iconColor={iconSubtleColor}
1376 + />
1377 {entry.checkpointSummary ? (
1378 <TurnDiffStat
1379 checkpoint={entry.checkpointSummary}
apps/mobile/src/lib/threadActivity.test.ts
+15
@@ -809,6 +809,21 @@ describe("buildThreadFeed", () => {
809 "message",
810 "message",
811 ]);
812 + const anchor = {
813 + ...command(),
814 + deferredWork: { cursor: "old-work", itemCount: 100, lastOrdinal: 100 },
815 + };
816 + const deferredFeed = buildThreadFeed([projected(userMessage(), 0), projected(anchor, 1)]);
817 + const running = { ...latestRun, status: "running" as const, completedAt: null };
818 + const deferredRows = deriveThreadFeedPresentation(deferredFeed, running, new Set());
819 + const disclosure = deferredRows.find((entry) => entry.type === "run-fold");
820 + expect(disclosure).toMatchObject({ deferredWorkItemIds: [anchor.id], expanded: false });
821 + if (disclosure?.type !== "run-fold") throw new Error("Expected deferred work disclosure");
822 + expect(
823 + deriveThreadFeedPresentation(deferredFeed, running, new Set([disclosure.runId])).some(
824 + (entry) => entry.type === "activity-group",
825 + ),
826 + ).toBe(true);
827 expect(expanded[4]).toMatchObject({ message: { id: middle.messageId, text: middle.text } });
828
829 const active = deriveThreadFeedPresentation(
apps/mobile/src/lib/threadActivity.ts
+33 -2
@@ -231,6 +231,7 @@ type ThreadFeedEntryContent =
231 }
232 | {
233 readonly type: "run-fold";
234 + readonly deferredWorkItemIds?: ReadonlyArray<string>;
235 readonly id: string;
236 readonly createdAt: string;
237 readonly runId: RunId;
@@ -1082,6 +1083,7 @@ function threadFeedActivityIsVisible(
1083 }
1084
1085 interface ThreadFeedRunFold {
1086 + readonly deferredWorkItemIds?: ReadonlyArray<string>;
1087 readonly runId: RunId;
1088 readonly createdAt: string;
1089 readonly hiddenEntryIds: ReadonlySet<string>;
@@ -1273,7 +1275,17 @@ function deriveThreadFeedRunFolds(
1275 const duration = elapsedMs === null ? null : formatDuration(elapsedMs);
1276 const interrupted =
1277 latestRunMatches && (latestRun.status === "interrupted" || latestRun.status === "cancelled");
1278 + const deferredWorkItemIds = group.entries.flatMap((entry) =>
1279 + entry.type === "activity-group"
1280 + ? entry.activities.flatMap((activity) =>
1281 + activity.projectedItem.item.deferredWork
1282 + ? [String(activity.projectedItem.sourceItemId)]
1283 + : [],
1284 + )
1285 + : [],
1286 + );
1287 foldsByAnchorId.set(firstHiddenEntry.id, {
1288 + ...(deferredWorkItemIds.length > 0 ? { deferredWorkItemIds } : {}),
1289 runId,
1290 createdAt: firstHiddenEntry.createdAt,
1291 hiddenEntryIds,
@@ -1286,6 +1298,23 @@ function deriveThreadFeedRunFolds(
1298 : "Worked",
1299 });
1300 }
1301 + const covered = new Set(
1302 + [...foldsByAnchorId.values()].flatMap((fold) => [...fold.hiddenEntryIds]),
1303 + );
1304 + for (const entry of feed) {
1305 + if (entry.type !== "activity-group" || covered.has(entry.id)) continue;
1306 + const deferredWorkItemIds = entry.activities.flatMap((activity) =>
1307 + activity.projectedItem.item.deferredWork ? [String(activity.projectedItem.sourceItemId)] : [],
1308 + );
1309 + if (deferredWorkItemIds.length === 0) continue;
1310 + foldsByAnchorId.set(entry.id, {
1311 + runId: RunId.make(`deferred-work:${entry.id}`),
1312 + createdAt: entry.createdAt,
1313 + hiddenEntryIds: new Set([entry.id]),
1314 + label: "Earlier work",
1315 + deferredWorkItemIds,
1316 + });
1317 + }
1318 return foldsByAnchorId;
1319 }
1320
@@ -1501,11 +1530,13 @@ export function deriveThreadFeedPresentation(
1530 row.runId !== fold.runId ||
1531 row.createdAt !== fold.createdAt ||
1532 row.label !== fold.label ||
1504 - row.expanded !== expanded
1533 + row.expanded !== expanded ||
1534 + row.deferredWorkItemIds?.join("|") !== fold.deferredWorkItemIds?.join("|")
1535 ) {
1536 row = {
1537 type: "run-fold",
1538 id: `run-fold:${fold.runId}`,
1539 + ...(fold.deferredWorkItemIds ? { deferredWorkItemIds: fold.deferredWorkItemIds } : {}),
1540 createdAt: fold.createdAt,
1541 runId: fold.runId,
1542 label: fold.label,
@@ -2189,7 +2220,7 @@ export function buildThreadFeed(
2220 for (let index = restart?.row ?? 0; index < visibleTurnItems.length; index += 1) {
2221 const row = visibleTurnItems[index]!;
2222 const item = row.item;
2192 - if (turnItemIsWorkspacePreparation(item)) continue;
2223 + if (turnItemIsWorkspacePreparation(item) && !item.deferredWork) continue;
2224 if (item.type === "todo_list" || item.type === "checkpoint") continue;
2225 if (
2226 item.type === "user_message" &&
apps/server/src/orchestration-v2/Orchestrator.ts
+3 -2
@@ -344,6 +344,7 @@ export interface OrchestratorV2Shape {
344 cursor: string,
345 throughEntryId?: string,
346 conversationOnly?: boolean,
347 + workMode?: "deferred" | "work",
348 ) => Effect.Effect<OrchestrationV2ThreadHistoryPage, OrchestratorV2Error>;
349 readonly getTimelinePage: (
350 threadId: ThreadId,
@@ -13281,9 +13282,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13282 );
13283 }),
13284 getThreadHistoryPage: Effect.fn("OrchestratorV2.getThreadHistoryPage")(
13284 - function (threadId, cursor, throughEntryId, conversationOnly) {
13285 + function (threadId, cursor, throughEntryId, conversationOnly, workMode) {
13286 return projectionStore
13286 - .getThreadHistoryPage(threadId, cursor, throughEntryId, conversationOnly)
13287 + .getThreadHistoryPage(threadId, cursor, throughEntryId, conversationOnly, workMode)
13288 .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause })));
13289 },
13290 ),
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+201
@@ -10,6 +10,7 @@ import {
10 type ModelSelection,
11 type OrchestrationV2ProviderThread,
12 type OrchestrationV2TurnItem,
13 + type OrchestrationV2ThreadHistoryPage,
14 type OrchestrationV2ThreadShellSnapshot,
15 NodeId,
16 PlanId,
@@ -1972,6 +1973,206 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
1973 Array.from({ length: 1_000 }, (_, index) => `turn-item:bounded-sql-history:${index + 1}`),
1974 );
1975
1976 + yield* setNewestTurnType("command_execution");
1977 + yield* sql`
1978 + UPDATE orchestration_v2_projection_turn_items
1979 + SET run_id = 'run:bounded-sql-history:1',
1980 + payload_json = json_set(payload_json, '$.runId', 'run:bounded-sql-history:1')
1981 + WHERE thread_id = ${threadId}
1982 + `;
1983 + yield* sql`UPDATE orchestration_v2_projection_turn_items SET ordinal = 1000,
1984 + payload_json = json_set(payload_json, '$.ordinal', 1000)
1985 + WHERE thread_id = ${threadId} AND ordinal = 999`;
1986 + const promptId = TurnItemId.make("turn-item:bounded-sql-history:1");
1987 + const { statements: deferredStatements, tracer: deferredTracer } = traceSqlStatements();
1988 + const deferred = yield* projectionStore
1989 + .getThreadSnapshotWindow(threadId, {
1990 + rowLimit: sqlPageLimit,
1991 + userTurnLimit: 10,
1992 + deferWork: true,
1993 + })
1994 + .pipe(Effect.withTracer(deferredTracer));
1995 + const deferredQuery = deferredStatements.find((statement) =>
1996 + statement.includes("blocked_runs AS MATERIALIZED"),
1997 + );
1998 + assert.isDefined(deferredQuery);
1999 + const deferredPlan = yield* sql.unsafe<{
2000 + readonly id: number;
2001 + readonly parent: number;
2002 + readonly detail: string;
2003 + }>(`EXPLAIN QUERY PLAN ${deferredQuery}`);
2004 + const blockedRunsPlan = deferredPlan.find((row) => row.detail === "MATERIALIZE blocked_runs");
2005 + assert.isDefined(blockedRunsPlan);
2006 + const blockedLookup = deferredPlan.find(
2007 + (row) => row.parent === blockedRunsPlan!.id && row.detail.startsWith("SEARCH "),
2008 + );
2009 + assert.include(blockedLookup?.detail ?? "", "run_id=?");
2010 + assert.lengthOf(deferred.projection.visibleTurnItems, 3);
2011 + assert.strictEqual(deferred.projection.visibleTurnItems[0]?.sourceItemId, promptId);
2012 + const anchor = deferred.projection.visibleTurnItems[1]!;
2013 + assert.strictEqual(anchor.item.deferredWork?.itemCount, 999);
2014 + const workIds = new Set<TurnItemId>();
2015 + let workCursor: string | null = anchor.item.deferredWork!.cursor;
2016 + for (let pages = 0; workCursor !== null; pages += 1) {
2017 + assert.isBelow(pages, 20);
2018 + const page: OrchestrationV2ThreadHistoryPage = yield* projectionStore.getThreadHistoryPage(
2019 + threadId,
2020 + workCursor,
2021 + anchor.sourceItemId,
2022 + false,
2023 + "work",
2024 + );
2025 + assert.isAtMost(page.items.length, 75);
2026 + for (const row of page.items) workIds.add(row.sourceItemId);
2027 + workCursor = page.nextCursor;
2028 + }
2029 + assert.strictEqual(workIds.size, 999);
2030 + for (const toolName of ["t3-code.delegate_task", "mcp__t3_code__delegate_task"]) {
2031 + yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'dynamic_tool',
2032 + payload_json = json_set(payload_json, '$.type', 'dynamic_tool', '$.toolName', ${toolName})
2033 + WHERE thread_id = ${threadId} AND ordinal = 2`;
2034 + const window = yield* projectionStore.getThreadSnapshotWindow(threadId, {
2035 + rowLimit: sqlPageLimit,
2036 + userTurnLimit: 10,
2037 + deferWork: true,
2038 + });
2039 + assert.isUndefined(
2040 + window.projection.visibleTurnItems.find((row) => row.item.ordinal === 2)?.item
2041 + .deferredWork,
2042 + );
2043 + assert.strictEqual(
2044 + window.projection.visibleTurnItems.find((row) => row.item.deferredWork)?.item.ordinal,
2045 + 3,
2046 + );
2047 + const page = yield* projectionStore.getThreadHistoryPage(
2048 + threadId,
2049 + encodeThreadHistoryCursor({
2050 + snapshotSequence: 0,
2051 + sourceThreadId: threadId,
2052 + sourceItemId: "turn-item:bounded-sql-history:1000",
2053 + position: 1000,
2054 + }),
2055 + undefined,
2056 + false,
2057 + "deferred",
2058 + );
2059 + assert.isUndefined(page.items.find((row) => row.item.ordinal === 2)?.item.deferredWork);
2060 + }
2061 + yield* sql`UPDATE orchestration_v2_projection_turn_items SET type = 'command_execution',
2062 + payload_json = json_set(payload_json, '$.type', 'command_execution') WHERE thread_id = ${threadId} AND ordinal = 2`;
2063 + const sharedRunId = "run:bounded-sql-history:1";
2064 + for (const [ordinal, status] of [
2065 + [0, "completed"],
2066 + [1, "superseded"],
2067 + [2, "running"],
2068 + ] as const) {
2069 + const attempt = {
2070 + id: `attempt:bounded-sql-history:${ordinal}`,
2071 + runId: sharedRunId,
2072 + attemptOrdinal: ordinal + 1,
2073 + rootNodeId: `node:bounded-sql-history:attempt-${ordinal === 0 ? 1 : ordinal}`,
2074 + providerInstanceId,
2075 + providerThreadId: "provider-thread:bounded-sql-history",
2076 + providerTurnId: null,
2077 + reason: ordinal === 1 ? "initial" : "steering_restart",
2078 + status,
2079 + startedAt: nowIso,
2080 + completedAt: status === "superseded" ? nowIso : null,
2081 + };
2082 + yield* sql`INSERT INTO orchestration_v2_projection_run_attempts
2083 + (attempt_id, thread_id, run_id, attempt_ordinal, root_node_id, provider, provider_instance_id, provider_thread_id, provider_turn_id, status, payload_json)
2084 + VALUES (${attempt.id}, ${threadId}, ${sharedRunId}, ${attempt.attemptOrdinal}, ${attempt.rootNodeId}, ${providerInstanceId}, ${providerInstanceId}, ${attempt.providerThreadId}, NULL, ${status}, ${encodeUnknownJsonString(attempt)})`;
2085 + }
2086 + yield* sql`UPDATE orchestration_v2_projection_turn_items
2087 + SET node_id = CASE WHEN ordinal < 990 AND ordinal <> 10 THEN 'node:bounded-sql-history:attempt-1' ELSE 'node:bounded-sql-history:attempt-2' END,
2088 + payload_json = json_set(payload_json, '$.nodeId', CASE WHEN ordinal < 990 AND ordinal <> 10 THEN 'node:bounded-sql-history:attempt-1' ELSE 'node:bounded-sql-history:attempt-2' END)
2089 + WHERE thread_id = ${threadId}`;
2090 + for (const status of ["completed", "running"]) {
2091 + yield* sql`UPDATE orchestration_v2_projection_runs SET status = ${status},
2092 + payload_json = json_set(payload_json, '$.status', ${status}) WHERE run_id = ${sharedRunId}`;
2093 + const window = yield* projectionStore.getThreadSnapshotWindow(threadId, {
2094 + rowLimit: sqlPageLimit,
2095 + userTurnLimit: 10,
2096 + deferWork: true,
2097 + });
2098 + const oldWork = window.projection.visibleTurnItems.find((row) => row.item.deferredWork);
2099 + assert.strictEqual(oldWork?.item.deferredWork?.itemCount, 987);
2100 + let cursor: string | null = oldWork!.item.deferredWork!.cursor;
2101 + const loaded = new Set<TurnItemId>();
2102 + for (let pages = 0; cursor !== null; pages += 1) {
2103 + assert.isBelow(pages, 20);
2104 + const page: OrchestrationV2ThreadHistoryPage =
2105 + yield* projectionStore.getThreadHistoryPage(
2106 + threadId,
2107 + cursor,
2108 + oldWork!.sourceItemId,
2109 + false,
2110 + "work",
2111 + );
2112 + for (const row of page.items) loaded.add(row.sourceItemId);
2113 + cursor = page.nextCursor;
2114 + }
2115 + assert.strictEqual(loaded.size, 987);
2116 + assert.isFalse(loaded.has(TurnItemId.make("turn-item:bounded-sql-history:10")));
2117 + }
2118 + yield* sql`DELETE FROM orchestration_v2_projection_run_attempts WHERE thread_id = ${threadId}`;
2119 + yield* sql`UPDATE orchestration_v2_projection_runs SET status = 'completed',
2120 + payload_json = json_set(payload_json, '$.status', 'completed') WHERE run_id = ${sharedRunId}`;
2121 + yield* sql`UPDATE orchestration_v2_projection_turn_items SET node_id = NULL,
2122 + payload_json = json_set(payload_json, '$.nodeId', NULL) WHERE thread_id = ${threadId}`;
2123 + yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = NULL,
2124 + payload_json = json_set(payload_json, '$.runId', NULL)
2125 + WHERE thread_id = ${threadId} AND ordinal > 1 AND ordinal < 851`;
2126 + const inlineWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
2127 + rowLimit: sqlPageLimit,
2128 + userTurnLimit: 10,
2129 + deferWork: true,
2130 + });
2131 + assert.isFalse(inlineWindow.projection.visibleTurnItems.some((row) => row.item.deferredWork));
2132 + yield* sql`UPDATE orchestration_v2_projection_turn_items SET run_id = ${sharedRunId},
2133 + payload_json = json_set(payload_json, '$.runId', ${sharedRunId})
2134 + WHERE thread_id = ${threadId} AND ordinal = 850`;
2135 + const deferredWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
2136 + rowLimit: sqlPageLimit,
2137 + userTurnLimit: 10,
2138 + deferWork: true,
2139 + });
2140 + assert.isTrue(
2141 + deferredWindow.projection.visibleTurnItems.some(
2142 + (row) => row.item.deferredWork?.itemCount === 151,
2143 + ),
2144 + );
2145 + yield* sql`UPDATE orchestration_v2_projection_turn_items
2146 + SET run_id = 'run:bounded-sql-history:1',
2147 + payload_json = json_set(payload_json, '$.runId', 'run:bounded-sql-history:1')
2148 + WHERE thread_id = ${threadId}`;
2149 + yield* sql`UPDATE orchestration_v2_projection_turn_items SET ordinal = 999,
2150 + payload_json = json_set(payload_json, '$.ordinal', 999)
2151 + WHERE turn_item_id = 'turn-item:bounded-sql-history:999'`;
2152 + yield* sql`
2153 + UPDATE orchestration_v2_projection_turn_items
2154 + SET type = 'command_execution', payload_json = json_set(payload_json, '$.type', 'command_execution')
2155 + WHERE thread_id = ${threadId} AND ordinal = 1
2156 + `;
2157 + const backgroundWindow = yield* projectionStore.getThreadSnapshotWindow(threadId, {
2158 + rowLimit: sqlPageLimit,
2159 + userTurnLimit: 10,
2160 + deferWork: true,
2161 + });
2162 + assert.lengthOf(backgroundWindow.projection.visibleTurnItems, sqlPageLimit);
2163 + yield* sql`
2164 + UPDATE orchestration_v2_projection_turn_items
2165 + SET type = 'user_message', payload_json = json_set(payload_json, '$.type', 'user_message')
2166 + WHERE thread_id = ${threadId} AND ordinal = 1
2167 + `;
2168 + yield* sql`
2169 + UPDATE orchestration_v2_projection_turn_items
2170 + SET run_id = 'run:bounded-sql-history:' || ordinal,
2171 + payload_json = json_set(payload_json, '$.runId', 'run:bounded-sql-history:' || ordinal)
2172 + WHERE thread_id = ${threadId}
2173 + `;
2174 + yield* setNewestTurnType("user_message");
2175 +
2176 const tiedIds = [
2177 TurnItemId.make("turn-item:bounded-sql-history:tie:b"),
2178 TurnItemId.make("turn-item:bounded-sql-history:tie:a"),
apps/server/src/orchestration-v2/ProjectionStore.ts
+212 -66
@@ -97,10 +97,14 @@ import {
97 isConversationHistoryItem,
98 THREAD_HISTORY_MAX_RAW_TURNS,
99 missingInterruptRequests,
100 + deferWorkHistoryRows,
101 + selectDeferredWorkPage,
102 + encodeThreadHistoryCursor,
103 olderHistoryPagePolicy,
104 selectHistoryPageFromCursor,
105 selectRecentTimelineWindow,
106 THREAD_HISTORY_PAGE_POLICY,
107 + THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT,
108 THREAD_HISTORY_TURN_SNAPSHOT_ROW_LIMIT,
109 } from "./threadHistoryPaging.ts";
110
@@ -416,6 +420,7 @@ export interface ProjectionStoreV2Shape {
420 cursor: string,
421 throughEntryId?: string,
422 conversationOnly?: boolean,
423 + workMode?: "deferred" | "work",
424 ) => Effect.Effect<OrchestrationV2ThreadHistoryPage, ProjectionStoreV2Error>;
425 readonly getTimelinePage: (
426 threadId: ThreadId,
@@ -611,6 +616,7 @@ export interface ProjectionStoreV2Shape {
616 options: {
617 readonly rowLimit: number;
618 readonly userTurnLimit?: number | undefined;
619 + readonly deferWork?: boolean | undefined;
620 readonly anchorItemId?: TurnItemId | undefined;
621 readonly anchorThreadId?: ThreadId | undefined;
622 readonly requiredRunId?: RunId | undefined;
@@ -625,6 +631,14 @@ export interface ProjectionStoreV2Shape {
631 >;
632 }
633
634 +type ProjectionWindow = Omit<
635 + Parameters<ProjectionStoreV2Shape["getThreadSnapshotWindow"]>[1],
636 + "anchorThreadId"
637 +> & {
638 + readonly suppressLocal?: boolean | undefined;
639 + readonly historyAnchor?: { readonly threadId: ThreadId; readonly itemId: TurnItemId } | undefined;
640 +};
641 +
642 export class ProjectionStoreV2 extends Context.Service<ProjectionStoreV2, ProjectionStoreV2Shape>()(
643 "t3/orchestration-v2/ProjectionStore/ProjectionStoreV2",
644 ) {}
@@ -1796,6 +1810,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
1810 Effect.gen(function* () {
1811 const sql = yield* SqlClient.SqlClient;
1812
1813 + const inlineWorkOutput = sql`item.type = 'dynamic_tool' AND (
1814 + item.payload_json LIKE '%htmlRender%' OR item.payload_json LIKE ${`%${MCP_APP_OUTPUT_KEY}%`}
1815 + OR item.payload_json LIKE '%"viewedImagePath"%'
1816 + OR item.payload_json LIKE '%"toolName":"%delegate_task"%')`;
1817 +
1818 const keepRecordedRunField = (payload: Statement.Fragment, path: string) => sql`
1819 CASE
1820 WHEN json_type(excluded.payload_json, ${path}) IS NULL
@@ -2676,21 +2695,26 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2695 );
2696
2697 // Share selection so counting discarded ancestry preserves payload-read positions.
2679 - const retainedWindowRows = (
2698 + const retainedWindowRows = Effect.fn("ProjectionStore.retainedWindowRows")(function* (
2699 threadId: ThreadId,
2681 - window: {
2682 - readonly rowLimit: number;
2683 - readonly userTurnLimit?: number | undefined;
2684 - readonly anchorItemId?: TurnItemId | undefined;
2685 - readonly requiredRunId?: RunId | undefined;
2686 - },
2687 - ) => sql`
2700 + window: ProjectionWindow,
2701 + ) {
2702 + const eligible = sql`
2703 WITH eligible AS NOT MATERIALIZED (
2704 SELECT item.rowid AS item_rowid, item.payload_json, item.ordinal, item.turn_item_id,
2690 - item.run_id, item.node_id, item.type
2705 + item.run_id, item.node_id, item.type, item.status, run.status AS run_status,
2706 + attempt.attempt_id, attempt.status AS attempt_status
2707 FROM orchestration_v2_projection_turn_items AS item
2708 LEFT JOIN orchestration_v2_projection_runs AS run
2709 ON run.run_id = item.run_id
2710 + LEFT JOIN orchestration_v2_projection_nodes AS node
2711 + ON ${window.deferWork ? 1 : 0} = 1 AND node.node_id = item.node_id
2712 + LEFT JOIN orchestration_v2_projection_run_attempts AS attempt
2713 + ON ${window.deferWork ? 1 : 0} = 1 AND attempt.attempt_id = (
2714 + SELECT attempt_id FROM orchestration_v2_projection_run_attempts
2715 + WHERE run_id = item.run_id AND root_node_id = COALESCE(node.root_node_id, item.node_id)
2716 + ORDER BY attempt_ordinal DESC LIMIT 1
2717 + )
2718 WHERE item.thread_id = ${threadId}
2719 AND item.ordinal <= COALESCE(
2720 (
@@ -2749,7 +2773,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2773 AND request.type = 'run_interrupt_request'
2774 )
2775 )
2752 - ), run_starts AS (
2776 + )`;
2777 + // Resolve anchors separately: repeated CTE expansion exceeds SQLite's subquery limit.
2778 + const anchors = yield* sql<{ readonly ordinal: number; readonly latest: number | null }>`
2779 + ${eligible}, run_starts AS (
2780 -- A wake (PR watch, task completion) starts its run with a
2781 -- notification instead of a user message. Read the first
2782 -- input of each run, newest run first, and add the
@@ -2813,6 +2840,49 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2840 ELSE 0
2841 END AS ordinal
2842 FROM user_anchors
2843 + )
2844 + SELECT ordinal, (SELECT MAX(ordinal) FROM user_anchors) AS latest FROM boundary
2845 + `;
2846 + const boundary = anchors[0]!;
2847 + return sql`
2848 + ${eligible}, blocked_runs AS MATERIALIZED (
2849 + SELECT DISTINCT run_id FROM orchestration_v2_projection_turn_items
2850 + WHERE thread_id = ${threadId} AND run_id IS NOT NULL
2851 + AND ${window.deferWork ? 1 : 0} = 1
2852 + AND run_id IN (
2853 + SELECT run_id FROM eligible
2854 + WHERE ordinal >= ${boundary.ordinal} AND run_id IS NOT NULL
2855 + )
2856 + AND (type IN ('run_interrupt_request', 'run_interrupt_result')
2857 + OR (type = 'error' AND status = 'failed' AND parent_item_id IS NULL))
2858 + ), work_segments AS (
2859 + SELECT item_rowid, ordinal, turn_item_id, run_id, type, attempt_id,
2860 + MAX(CASE WHEN type = 'user_message' THEN ordinal ELSE 0 END)
2861 + OVER (PARTITION BY run_id ORDER BY ordinal) AS segment,
2862 + CASE WHEN status = 'completed' AND (run_status = 'completed' OR attempt_status = 'superseded')
2863 + AND type IN ('command_execution', 'reasoning', 'file_change', 'file_search', 'web_search', 'dynamic_tool')
2864 + AND NOT (${inlineWorkOutput})
2865 + AND run_id NOT IN (SELECT run_id FROM blocked_runs)
2866 + THEN 1 ELSE 0 END AS can_defer
2867 + FROM eligible AS item
2868 + WHERE ${window.deferWork ? 1 : 0} = 1
2869 + AND ${boundary.latest === null ? 0 : 1} = 1
2870 + AND ordinal >= ${boundary.ordinal}
2871 + ), deferred_work AS (
2872 + SELECT run_id, segment, attempt_id, MIN(ordinal) AS first_ordinal,
2873 + MAX(ordinal) AS last_ordinal, COUNT(*) AS item_count
2874 + FROM work_segments WHERE can_defer = 1
2875 + GROUP BY run_id, segment, attempt_id HAVING COUNT(*) > ${THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT}
2876 + ), window_items AS NOT MATERIALIZED (
2877 + SELECT item_rowid, ordinal, turn_item_id, run_id, type
2878 + FROM eligible WHERE ${window.deferWork ? 1 : 0} = 0
2879 + OR ${boundary.latest === null ? 1 : 0} = 1
2880 + UNION ALL
2881 + SELECT item.item_rowid, item.ordinal, item.turn_item_id, item.run_id, item.type
2882 + FROM work_segments item LEFT JOIN deferred_work deferred
2883 + ON deferred.run_id = item.run_id AND deferred.segment = item.segment
2884 + AND deferred.attempt_id IS item.attempt_id
2885 + WHERE item.can_defer = 0 OR deferred.run_id IS NULL OR item.ordinal = deferred.first_ordinal
2886 ), latest_user_turn AS (
2887 -- Mirrors selectOlderTimelinePage: whole turns while the
2888 -- newest user turn fits the turn row budget, otherwise the
@@ -2820,8 +2890,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2890 SELECT COUNT(*) AS row_count
2891 FROM (
2892 SELECT 1
2823 - FROM eligible
2824 - WHERE ordinal >= (SELECT MAX(ordinal) FROM user_anchors)
2893 + FROM window_items
2894 + WHERE ordinal >= ${boundary.latest}
2895 LIMIT ${THREAD_HISTORY_PAGE_POLICY.maxTurnItems + 1}
2896 )
2897 ), selected AS (
@@ -2829,8 +2899,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2899 -- order. A turn window can hold megabytes of tool output, and
2900 -- carrying it through the union and sort cost about a second.
2901 SELECT item_rowid, ordinal, turn_item_id, run_id, type
2832 - FROM eligible
2833 - WHERE ordinal >= (SELECT ordinal FROM boundary)
2902 + FROM window_items
2903 + WHERE ordinal >= ${boundary.ordinal}
2904 ORDER BY ordinal DESC, turn_item_id DESC
2905 LIMIT CASE
2906 WHEN ${window.rowLimit} = 0 THEN 0
@@ -2863,17 +2933,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2933 ORDER BY ordinal ASC, turn_item_id ASC
2934 )
2935 `;
2936 + });
2937
2938 const readCanonicalProjection = Effect.fn("ProjectionStore.readCanonicalProjection")(
2939 function* (
2940 threadId: ThreadId,
2870 - window?: {
2871 - readonly rowLimit: number;
2872 - readonly userTurnLimit?: number | undefined;
2873 - readonly anchorItemId?: TurnItemId | undefined;
2874 - readonly requiredRunId?: RunId | undefined;
2875 - readonly suppressLocal?: boolean | undefined;
2876 - },
2941 + window?: ProjectionWindow,
2942 fields?: ReadonlyArray<ProjectionRecordField>,
2943 filter?: ProjectionRecordFilter,
2944 ) {
@@ -2888,7 +2953,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2953 return yield* new ProjectionStoreThreadNotFoundError({ threadId });
2954 }
2955
2891 - const boundedTurnItemRows =
2956 + type WindowPayloadRow = PayloadRow & {
2957 + readonly work_last_id?: string | null;
2958 + readonly work_last_ordinal?: number | null;
2959 + readonly work_item_count?: number | null;
2960 + };
2961 + const boundedTurnItemRows: ReadonlyArray<WindowPayloadRow> =
2962 (fields !== undefined && !fields.includes("turnItems")) ||
2963 (window === undefined &&
2964 matchesNothing(filter?.turnItemTypes, filter?.turnItemStatuses, filter?.turnItemRunIds))
@@ -2905,18 +2975,51 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
2975 ORDER BY ordinal ASC, turn_item_id ASC
2976 `
2977 : yield* sql<PayloadRow>`
2908 - ${retainedWindowRows(threadId, window)}
2978 + ${yield* retainedWindowRows(threadId, window)}
2979 -- CROSS JOIN keeps the sorted IDs as the outer loop, so SQLite
2980 -- skips sorting again once the payloads are attached. Carry the
2981 -- rowid through selection to avoid a second string-key index lookup.
2912 - SELECT item.payload_json
2982 + SELECT item.payload_json, tail.turn_item_id AS work_last_id,
2983 + deferred.last_ordinal AS work_last_ordinal, deferred.item_count AS work_item_count
2984 FROM retained
2985 CROSS JOIN orchestration_v2_projection_turn_items AS item
2986 ON item.rowid = retained.item_rowid
2987 + LEFT JOIN deferred_work deferred ON deferred.run_id = item.run_id
2988 + AND deferred.first_ordinal = item.ordinal
2989 + LEFT JOIN orchestration_v2_projection_turn_items tail
2990 + ON tail.turn_item_id = (
2991 + SELECT turn_item_id FROM orchestration_v2_projection_turn_items
2992 + WHERE thread_id = item.thread_id AND ordinal = deferred.last_ordinal
2993 + ORDER BY turn_item_id DESC LIMIT 1
2994 + )
2995 ORDER BY retained.ordinal ASC, retained.turn_item_id ASC
2996 `;
2997 // Decode items once for cohort ids and projection; repeated parsing duplicates retained tool output.
2919 - const turnItems = yield* decodeRows(decodeTurnItemPayload, threadId)(boundedTurnItemRows);
2998 + const decodedTurnItems = yield* decodeRows(
2999 + decodeTurnItemPayload,
3000 + threadId,
3001 + )(boundedTurnItemRows);
3002 + const turnItems = decodedTurnItems.map((item, index) => {
3003 + const row = boundedTurnItemRows[index]!;
3004 + return row.work_last_id == null ||
3005 + row.work_item_count == null ||
3006 + row.work_last_ordinal == null
3007 + ? item
3008 + : {
3009 + ...item,
3010 + deferredWork: {
3011 + cursor: encodeThreadHistoryCursor({
3012 + snapshotSequence: 0,
3013 + sourceThreadId: threadId,
3014 + sourceItemId: TurnItemId.make(row.work_last_id),
3015 + position: row.work_last_ordinal,
3016 + inclusive: true,
3017 + }),
3018 + itemCount: row.work_item_count,
3019 + lastOrdinal: row.work_last_ordinal,
3020 + },
3021 + };
3022 + });
3023 const cohortJson = (field: string) =>
3024 JSON.stringify(
3025 turnItems.flatMap((payload) => {
@@ -3438,32 +3541,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3541 const readProjection: (
3542 threadId: ThreadId,
3543 seenThreadIds: ReadonlySet<ThreadId>,
3441 - window?: {
3442 - readonly rowLimit: number;
3443 - readonly userTurnLimit?: number | undefined;
3444 - readonly anchorItemId?: TurnItemId | undefined;
3445 - readonly requiredRunId?: RunId | undefined;
3446 - readonly suppressLocal?: boolean | undefined;
3447 - readonly historyAnchor?:
3448 - | { readonly threadId: ThreadId; readonly itemId: TurnItemId }
3449 - | undefined;
3450 - },
3544 + window?: ProjectionWindow,
3545 ) => Effect.Effect<OrchestrationV2ThreadProjection, ProjectionStoreV2Error> = Effect.fn(
3546 "ProjectionStore.readProjection",
3547 )(
3548 function* (
3549 threadId: ThreadId,
3550 seenThreadIds: ReadonlySet<ThreadId>,
3457 - window?: {
3458 - readonly rowLimit: number;
3459 - readonly userTurnLimit?: number | undefined;
3460 - readonly anchorItemId?: TurnItemId | undefined;
3461 - readonly requiredRunId?: RunId | undefined;
3462 - readonly suppressLocal?: boolean | undefined;
3463 - readonly historyAnchor?:
3464 - | { readonly threadId: ThreadId; readonly itemId: TurnItemId }
3465 - | undefined;
3466 - },
3551 + window?: ProjectionWindow,
3552 ) {
3553 const localWindow =
3554 window?.suppressLocal === true ||
@@ -3569,6 +3654,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3654 return {
3655 rowLimit: window.rowLimit,
3656 userTurnLimit: window.userTurnLimit,
3657 + deferWork: window.deferWork,
3658 requiredRunId: forkedFrom.runId,
3659 suppressLocal: anchor === undefined || anchorIsInDescendant,
3660 ...(anchor === undefined || anchorIsInDescendant
@@ -3591,7 +3677,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3677 .length >= Math.max(window.userTurnLimit, THREAD_HISTORY_PAGE_POLICY.maxUserTurns)
3678 ) {
3679 const counted = yield* sql<{ readonly inherited_rows: number | null }>`
3594 - ${retainedWindowRows(forkedFrom.threadId, {
3680 + ${yield* retainedWindowRows(forkedFrom.threadId, {
3681 ...sourceWindow,
3682 rowLimit: sourceWindow.suppressLocal ? 0 : sourceWindow.rowLimit,
3683 })}
@@ -5196,7 +5282,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5282
5283 type TimelineIndexItem = Pick<
5284 OrchestrationV2TurnItem,
5199 - "id" | "threadId" | "runId" | "nodeId" | "type"
5285 + "id" | "threadId" | "runId" | "nodeId" | "parentItemId" | "type" | "status" | "ordinal"
5286 > & {
5287 readonly inputIntent?: Extract<
5288 OrchestrationV2TurnItem,
@@ -5204,6 +5290,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5290 >["inputIntent"];
5291 readonly createdBy?: "user" | "agent" | "system";
5292 readonly messageId?: MessageId;
5293 + readonly runStatus?: string | undefined;
5294 + readonly attemptId?: string | null;
5295 + readonly attemptStatus?: string | null;
5296 + readonly persistentOutput?: boolean;
5297 + readonly deferredWork?: OrchestrationV2TurnItem["deferredWork"];
5298 };
5299 type TimelineIndexRow = {
5300 readonly sourceThreadId: ThreadId;
@@ -5215,6 +5306,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5306 const readTimelineIndex: (
5307 threadId: ThreadId,
5308 seen: ReadonlySet<ThreadId>,
5309 + includePersistentOutput?: boolean,
5310 ) => Effect.Effect<
5311 {
5312 readonly records: ProjectionRecords<"runs" | "attempts">;
@@ -5223,28 +5315,58 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5315 },
5316 ProjectionStoreV2Error
5317 > = Effect.fn("ProjectionStore.readTimelineIndex")(
5226 - function* (threadId: ThreadId, seen: ReadonlySet<ThreadId>) {
5318 + function* (threadId: ThreadId, seen: ReadonlySet<ThreadId>, includePersistentOutput = false) {
5319 const records = yield* getThreadRecords(threadId, ["runs", "attempts"]);
5320 const rows = yield* sql<{
5321 turn_item_id: string;
5322 run_id: string | null;
5323 node_id: string | null;
5324 + parent_item_id: string | null;
5325 type: OrchestrationV2TurnItem["type"];
5326 input_intent: NonNullable<TimelineIndexItem["inputIntent"]> | null;
5327 created_by: "user" | "agent" | "system" | null;
5328 message_id: string | null;
5236 - }>`SELECT turn_item_id, run_id, node_id, type,
5237 - CASE WHEN type = 'user_message' THEN json_extract(payload_json, '$.inputIntent') END AS input_intent,
5238 - CASE WHEN type = 'user_message' THEN json_extract(payload_json, '$.createdBy') END AS created_by,
5239 - CASE WHEN type IN ('user_message', 'assistant_message') THEN json_extract(payload_json, '$.messageId') END AS message_id
5240 - FROM orchestration_v2_projection_turn_items WHERE thread_id = ${threadId}
5241 - ORDER BY ordinal ASC, turn_item_id ASC`;
5329 + status: OrchestrationV2TurnItem["status"];
5330 + ordinal: number;
5331 + persistent_output: number;
5332 + attempt_id: string | null;
5333 + attempt_status: string | null;
5334 + }>`SELECT item.turn_item_id, item.run_id, item.node_id, item.parent_item_id, item.type, item.status, item.ordinal,
5335 + ${includePersistentOutput ? sql`attempt.attempt_id` : sql`NULL`} AS attempt_id,
5336 + ${includePersistentOutput ? sql`attempt.status` : sql`NULL`} AS attempt_status,
5337 + ${
5338 + includePersistentOutput ? sql`CASE WHEN ${inlineWorkOutput} THEN 1 ELSE 0 END` : sql`0`
5339 + } AS persistent_output,
5340 + CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.inputIntent') END AS input_intent,
5341 + CASE WHEN item.type = 'user_message' THEN json_extract(item.payload_json, '$.createdBy') END AS created_by,
5342 + CASE WHEN item.type IN ('user_message', 'assistant_message') THEN json_extract(item.payload_json, '$.messageId') END AS message_id
5343 + FROM orchestration_v2_projection_turn_items AS item
5344 + ${
5345 + includePersistentOutput
5346 + ? sql`LEFT JOIN orchestration_v2_projection_nodes AS node ON node.node_id = item.node_id
5347 + LEFT JOIN orchestration_v2_projection_run_attempts AS attempt ON attempt.attempt_id = (
5348 + SELECT attempt_id FROM orchestration_v2_projection_run_attempts
5349 + WHERE run_id = item.run_id AND root_node_id = COALESCE(node.root_node_id, item.node_id)
5350 + ORDER BY attempt_ordinal DESC LIMIT 1
5351 + )`
5352 + : sql``
5353 + }
5354 + WHERE item.thread_id = ${threadId}
5355 + ORDER BY item.ordinal ASC, item.turn_item_id ASC`;
5356 + const runStatuses = new Map(records.runs.map((run) => [String(run.id), run.status]));
5357 const items: Array<TimelineIndexItem> = rows.map((row) => ({
5358 id: TurnItemId.make(row.turn_item_id),
5359 threadId,
5360 runId: row.run_id === null ? null : RunId.make(row.run_id),
5361 nodeId: row.node_id === null ? null : NodeId.make(row.node_id),
5362 + parentItemId: row.parent_item_id === null ? null : TurnItemId.make(row.parent_item_id),
5363 type: row.type,
5364 + status: row.status,
5365 + ordinal: row.ordinal,
5366 + runStatus: row.run_id === null ? undefined : runStatuses.get(row.run_id),
5367 + attemptId: row.attempt_id,
5368 + attemptStatus: row.attempt_status,
5369 + persistentOutput: row.persistent_output === 1,
5370 ...(row.input_intent === null ? {} : { inputIntent: row.input_intent }),
5371 ...(row.created_by === null ? {} : { createdBy: row.created_by }),
5372 ...(row.message_id === null ? {} : { messageId: MessageId.make(row.message_id) }),
@@ -5263,7 +5385,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5385 const visible = local.filter((row) => isVisible(row.item));
5386 const fork = records.thread.forkedFrom;
5387 if (fork?.type !== "run" || seen.has(fork.threadId)) return { records, local, visible };
5266 - const source = yield* readTimelineIndex(fork.threadId, new Set([...seen, threadId]));
5388 + const source = yield* readTimelineIndex(
5389 + fork.threadId,
5390 + new Set([...seen, threadId]),
5391 + includePersistentOutput,
5392 + );
5393 const sourceRun = source.records.runs.find((run) => run.id === fork.runId);
5394 let inherited: Array<TimelineIndexRow> = [];
5395 if (sourceRun !== undefined) {
@@ -5329,11 +5455,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5455 cursor,
5456 throughEntryId,
5457 conversationOnly,
5458 + workMode,
5459 ) =>
5460 sql
5461 .withTransaction(
5462 Effect.gen(function* () {
5336 - const index = yield* readTimelineIndex(threadId, new Set());
5463 + const index = yield* readTimelineIndex(threadId, new Set(), workMode !== undefined);
5464 if (index.records.thread.deletedAt !== null)
5465 return yield* new ProjectionStoreThreadNotFoundError({ threadId });
5466 const revisions = yield* sql<{
@@ -5347,21 +5474,32 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5474 position,
5475 canonicalPosition: position,
5476 }));
5477 + const historyItems = workMode === "deferred" ? deferWorkHistoryRows(indexed) : indexed;
5478 const selection = yield* Effect.try({
5479 try: () => {
5480 const policy = olderHistoryPagePolicy({ items: indexed, cursor, throughEntryId });
5353 - const page = selectHistoryPageFromCursor({
5354 - items: indexed,
5355 - cursor,
5356 - snapshotSequence,
5357 - rowEncodedBytes: () => 0,
5358 - policy,
5359 - });
5481 + const page =
5482 + workMode === "work"
5483 + ? selectDeferredWorkPage({
5484 + items: indexed,
5485 + cursor,
5486 + throughEntryId,
5487 + snapshotSequence,
5488 + rowEncodedBytes: () => 0,
5489 + })
5490 + : selectHistoryPageFromCursor({
5491 + items: historyItems,
5492 + cursor,
5493 + snapshotSequence,
5494 + rowEncodedBytes: () => 0,
5495 + policy,
5496 + });
5497 return { policy, page };
5498 },
5499 catch: (cause) => new ProjectionStoreReadError({ threadId, cause }),
5500 });
5364 - const { policy, page } = selection;
5501 + const { page } = selection;
5502 + const policy = workMode === "work" ? THREAD_HISTORY_PAGE_POLICY : selection.policy;
5503 const end = (page.items.at(-1)?.canonicalPosition ?? -1) + 1;
5504 const hasUserTurns = index.visible
5505 .slice(0, end)
@@ -5400,7 +5538,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5538 visibility: row.visibility,
5539 sourceThreadId: row.sourceThreadId,
5540 sourceItemId: row.sourceItemId,
5403 - item: projectTurnItemForWire(item),
5541 + item: projectTurnItemForWire(
5542 + row.item.deferredWork === undefined
5543 + ? item
5544 + : { ...item, deferredWork: row.item.deferredWork },
5545 + ),
5546 };
5547 }),
5548 );
@@ -5411,7 +5553,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5553 : page.items,
5554 );
5555 const bounded = selectRecentTimelineWindow({ items, snapshotSequence, policy });
5414 - const requests = yield* materialize(missingInterruptRequests(indexed, bounded.items));
5556 + const requests =
5557 + workMode === "work"
5558 + ? []
5559 + : yield* materialize(missingInterruptRequests(indexed, bounded.items));
5560 const pageItems = [...requests, ...bounded.items].map((row, position) => ({
5561 ...row,
5562 position,
@@ -5517,6 +5662,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
5662 const projection = yield* readProjection(threadId, new Set(), {
5663 rowLimit: options.rowLimit,
5664 userTurnLimit: options.userTurnLimit,
5665 + deferWork: options.deferWork,
5666 ...(historyAnchor?.threadId === threadId
5667 ? { anchorItemId: historyAnchor.itemId }
5668 : {}),
apps/server/src/orchestration-v2/ThreadManagementService.ts
+8 -2
@@ -1278,10 +1278,16 @@ const make = Effect.gen(function* () {
1278 }),
1279 dispatch,
1280 getThreadHistoryPage: Effect.fn("ThreadManagementService.getThreadHistoryPage")(
1281 - function (threadId, cursor, throughEntryId, conversationOnly) {
1281 + function (threadId, cursor, throughEntryId, conversationOnly, workMode) {
1282 return ensureProjectionTranscript(threadId).pipe(
1283 Effect.andThen(
1284 - orchestrator.getThreadHistoryPage(threadId, cursor, throughEntryId, conversationOnly),
1284 + orchestrator.getThreadHistoryPage(
1285 + threadId,
1286 + cursor,
1287 + throughEntryId,
1288 + conversationOnly,
1289 + workMode,
1290 + ),
1291 ),
1292 );
1293 },
apps/server/src/orchestration-v2/http.ts
+13 -3
@@ -133,10 +133,12 @@ export const layer = HttpApiBuilder.group(
133 anchorThreadId?: Parameters<
134 typeof threadManagement.getThreadSnapshotWindow
135 >[1]["anchorThreadId"],
136 + deferWork = false,
137 ) {
138 return yield* threadManagement
139 .getThreadSnapshotWindow(threadId, {
140 rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
141 + deferWork,
142 userTurnLimit:
143 anchorItemId === undefined
144 ? THREAD_HISTORY_PAGE_POLICY.maxUserTurns
@@ -223,9 +225,12 @@ export const layer = HttpApiBuilder.group(
225 Effect.fn("environment.orchestration.threadBoundedSnapshot")(function* (args) {
226 yield* annotateEnvironmentRequest(args.endpoint.name);
227 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
226 - const snapshot = yield* loadThreadSnapshotWindow(args.params.threadId).pipe(
227 - traceLocalHandlerWork,
228 - );
228 + const snapshot = yield* loadThreadSnapshotWindow(
229 + args.params.threadId,
230 + undefined,
231 + undefined,
232 + args.query.deferWork === "1",
233 + ).pipe(traceLocalHandlerWork);
234 const bounded = buildBoundedThreadProjection({
235 projection: snapshot.projection,
236 snapshotSequence: snapshot.snapshotSequence,
@@ -261,6 +266,11 @@ export const layer = HttpApiBuilder.group(
266 args.query.cursor,
267 args.query.throughEntryId,
268 args.query.view === "conversation",
269 + args.query.view === "work"
270 + ? "work"
271 + : args.query.deferWork === "1"
272 + ? "deferred"
273 + : undefined,
274 )
275 .pipe(
276 Effect.map((page) =>
apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
+106
@@ -12,12 +12,15 @@ import * as DateTime from "effect/DateTime";
12
13 import {
14 buildBoundedThreadProjection,
15 + deferWorkHistoryRows,
16 boundedTimelineEncodedBytes,
17 computeLatestLocalTurnOrdinal,
18 + decodeThreadHistoryCursor,
19 encodeThreadHistoryCursor,
20 projectedRowBoundedSnapshotEncodedBytes,
21 projectedRowEncodedBytes,
22 selectHistoryPageFromCursor,
23 + selectDeferredWorkPage,
24 selectRecentTimelineWindow,
25 THREAD_HISTORY_PAGE_POLICY,
26 } from "./threadHistoryPaging.ts";
@@ -155,6 +158,76 @@ function makeProjection(visibleTurnItems: OrchestrationV2ProjectedTurnItem[]) {
158 }
159
160 describe("threadHistoryPaging", () => {
161 + it("defers completed work above 150 items and keeps conversation and persistent rows", () => {
162 + const work = Array.from({ length: 151 }, (_, index) => {
163 + const row = makeRow(index);
164 + return { ...row, item: { ...row.item, runId: RUN, runStatus: "completed" } };
165 + });
166 + expect(deferWorkHistoryRows(work.slice(0, 150))).toHaveLength(150);
167 + const deferredWork = deferWorkHistoryRows(work)[0]!.item.deferredWork!;
168 + expect(deferredWork.itemCount).toBe(151);
169 + expect(decodeThreadHistoryCursor(deferredWork.cursor).p).toBe(151);
170 + const superseded = work.map((row) => ({
171 + ...row,
172 + item: { ...row.item, runStatus: "running", attemptId: "old", attemptStatus: "superseded" },
173 + }));
174 + const active = work.map((row, index) => ({
175 + ...row,
176 + sourceItemId: TurnItemId.make(`active-${index}`),
177 + position: row.position + work.length,
178 + item: { ...row.item, runStatus: "running", attemptId: "active", attemptStatus: "running" },
179 + }));
180 + const attempts = deferWorkHistoryRows([...superseded, ...active]);
181 + expect(attempts[0]?.item.deferredWork?.itemCount).toBe(151);
182 + expect(attempts.slice(1)).toEqual(active);
183 + const compaction = {
184 + ...work[0]!,
185 + sourceItemId: TurnItemId.make("compaction"),
186 + item: {
187 + ...work[0]!.item,
188 + type: "compaction" as const,
189 + },
190 + };
191 + const persistent = {
192 + ...work[0]!,
193 + sourceItemId: TurnItemId.make("render"),
194 + item: {
195 + ...work[0]!.item,
196 + type: "dynamic_tool" as const,
197 + persistentOutput: true,
198 + },
199 + };
200 + const deferred = deferWorkHistoryRows([
201 + ...work.slice(0, 30),
202 + compaction,
203 + persistent,
204 + ...work.slice(30),
205 + ]);
206 + expect(deferred.map((row) => row.sourceItemId)).toEqual(["item-0", "compaction", "render"]);
207 + expect(deferred[0]?.item.deferredWork?.itemCount).toBe(151);
208 + for (const runStatus of ["running", "failed", "interrupted", "cancelled"]) {
209 + expect(
210 + deferWorkHistoryRows(work.map((row) => ({ ...row, item: { ...row.item, runStatus } }))),
211 + ).toHaveLength(151);
212 + }
213 + const interrupted = {
214 + ...compaction,
215 + item: { ...compaction.item, type: "run_interrupt_result" as const },
216 + };
217 + expect(deferWorkHistoryRows([...work, interrupted])).toHaveLength(152);
218 + const failed = {
219 + ...compaction,
220 + item: {
221 + ...compaction.item,
222 + type: "error" as const,
223 + status: "failed" as const,
224 + nodeId: "root-node",
225 + parentItemId: null,
226 + },
227 + };
228 + expect(deferWorkHistoryRows([...work, failed])).toHaveLength(152);
229 + });
230 +
231 it("keeps a human turn whole up to the turn row budget and pages a longer one from inside", () => {
232 const humanTurn = (length: number) => {
233 const items = Array.from({ length }, (_, index) => makeRow(index));
@@ -394,6 +467,39 @@ describe("threadHistoryPaging", () => {
467 expect(page.items.map((row) => row.sourceItemId)).toEqual(["item-3", "item-4"]);
468 expect(page.hasMoreHistory).toBe(true);
469 expect(page.nextCursor).not.toBeNull();
470 + const sparse = [makeRow(0), makeRow(10), makeRow(20)];
471 + const middle = selectHistoryPageFromCursor({
472 + items: sparse,
473 + cursor: encodeThreadHistoryCursor({
474 + snapshotSequence: 3,
475 + sourceThreadId: THREAD,
476 + sourceItemId: "item-hidden",
477 + position: 15,
478 + }),
479 + snapshotSequence: 3,
480 + });
481 + expect(middle.items.map((row) => row.sourceItemId)).toEqual(["item-0", "item-10"]);
482 + const work = Array.from({ length: 80 }, (_, index) => {
483 + const row = makeRow(index);
484 + return { ...row, item: { ...row.item, runId: RUN, ordinal: index + 100 } };
485 + });
486 + const tail = work[75]!;
487 + const deferred = selectDeferredWorkPage({
488 + items: work.filter((row) => row !== tail).map((row, position) => ({ ...row, position })),
489 + cursor: encodeThreadHistoryCursor({
490 + snapshotSequence: 3,
491 + sourceThreadId: THREAD,
492 + sourceItemId: tail.sourceItemId,
493 + position: tail.item.ordinal,
494 + inclusive: true,
495 + }),
496 + throughEntryId: work[0]!.sourceItemId,
497 + snapshotSequence: 3,
498 + });
499 + expect(deferred.items.map((row) => row.sourceItemId)).toEqual(
500 + work.slice(0, 75).map((row) => row.sourceItemId),
501 + );
502 + expect(deferred.nextCursor).toBeNull();
503 });
504
505 it("keeps cursor resolution stable when newer items append", () => {
apps/server/src/orchestration-v2/threadHistoryPaging.ts
+132 -18
@@ -19,6 +19,8 @@ export const THREAD_HISTORY_PAGE_POLICY = {
19 maxEncodedBytes: 1_048_576,
20 } as const;
21
22 +export const THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT = 150;
23 +
24 export const OLDER_THREAD_USER_TURN_LIMIT = 20;
25
26 export function isConversationHistoryItem(item: Pick<OrchestrationV2TurnItem, "type">): boolean {
@@ -46,6 +48,7 @@ export type ThreadHistoryCursorPayload = {
48 readonly st: string;
49 readonly si: string;
50 readonly p: number;
51 + readonly inclusive?: boolean;
52 };
53
54 type HistoryRow = Pick<
@@ -58,6 +61,14 @@ type HistoryRow = Pick<
61 readonly inputIntent?: string;
62 readonly createdBy?: string;
63 readonly runId?: string | null;
64 + readonly status?: OrchestrationV2TurnItem["status"];
65 + readonly parentItemId?: string | null;
66 + readonly ordinal?: number;
67 + readonly runStatus?: string | undefined;
68 + readonly attemptId?: string | null | undefined;
69 + readonly attemptStatus?: string | null | undefined;
70 + readonly persistentOutput?: boolean;
71 + readonly deferredWork?: OrchestrationV2TurnItem["deferredWork"];
72 };
73 };
74 export type SelectTimelinePageResult<Row extends HistoryRow = OrchestrationV2ProjectedTurnItem> = {
@@ -114,6 +125,7 @@ export function encodeThreadHistoryCursor(input: {
125 readonly sourceThreadId: ThreadId | string;
126 readonly sourceItemId: TurnItemId | string;
127 readonly position: number;
128 + readonly inclusive?: boolean;
129 }): string {
130 return encodeCursorPayload({
131 v: 1,
@@ -121,6 +133,7 @@ export function encodeThreadHistoryCursor(input: {
133 st: String(input.sourceThreadId),
134 si: String(input.sourceItemId),
135 p: input.position,
136 + ...(input.inclusive ? { inclusive: true } : {}),
137 });
138 }
139
@@ -153,7 +166,8 @@ export function decodeThreadHistoryCursor(cursor: string): ThreadHistoryCursorPa
166 !("p" in parsed) ||
167 typeof parsed.p !== "number" ||
168 !Number.isInteger(parsed.p) ||
156 - parsed.p < 0
169 + parsed.p < 0 ||
170 + ("inclusive" in parsed && typeof parsed.inclusive !== "boolean")
171 ) {
172 throw new InvalidThreadHistoryCursorError();
173 }
@@ -167,6 +181,109 @@ function isThreadHistoryTurnStart(item: HistoryRow["item"]): boolean {
181 );
182 }
183
184 +const DEFERRED_WORK_ITEM_TYPES = [
185 + "command_execution",
186 + "reasoning",
187 + "file_change",
188 + "file_search",
189 + "web_search",
190 + "dynamic_tool",
191 +] as const;
192 +
193 +function isDeferrableWorkItem(item: HistoryRow["item"]): boolean {
194 + return (
195 + item.status === "completed" &&
196 + item.persistentOutput !== true &&
197 + DEFERRED_WORK_ITEM_TYPES.some((type) => type === item.type)
198 + );
199 +}
200 +
201 +export function deferWorkHistoryRows<Row extends HistoryRow>(items: ReadonlyArray<Row>): Row[] {
202 + const blocked = new Set(
203 + items.flatMap(({ item }) =>
204 + item.runId != null &&
205 + (item.type === "run_interrupt_request" ||
206 + item.type === "run_interrupt_result" ||
207 + (item.type === "error" && item.status === "failed" && item.parentItemId == null))
208 + ? [item.runId]
209 + : [],
210 + ),
211 + );
212 + const segments = new Map<string, number>();
213 + const groups = new Map<string, Row[]>();
214 + for (const row of items) {
215 + const item = row.item;
216 + if (item.runId == null) continue;
217 + if (item.type === "user_message") segments.set(item.runId, (segments.get(item.runId) ?? 0) + 1);
218 + if (
219 + (item.runStatus !== "completed" && item.attemptStatus !== "superseded") ||
220 + blocked.has(item.runId) ||
221 + !isDeferrableWorkItem(item)
222 + )
223 + continue;
224 + const key = `${row.sourceThreadId}:${item.runId}:${segments.get(item.runId) ?? 0}:${item.attemptId ?? ""}`;
225 + const group = groups.get(key) ?? [];
226 + group.push(row);
227 + groups.set(key, group);
228 + }
229 + const omitted = new Set<Row>();
230 + const anchors = new Map<Row, Row>();
231 + for (const group of groups.values()) {
232 + if (group.length <= THREAD_HISTORY_INLINE_WORK_ITEM_LIMIT) continue;
233 + const first = group[0]!;
234 + const last = group.at(-1)!;
235 + for (const row of group.slice(1)) omitted.add(row);
236 + anchors.set(first, {
237 + ...first,
238 + item: {
239 + ...first.item,
240 + deferredWork: {
241 + cursor: encodeThreadHistoryCursor({
242 + snapshotSequence: 0,
243 + sourceThreadId: last.sourceThreadId,
244 + sourceItemId: last.sourceItemId,
245 + position: last.item.ordinal ?? last.position,
246 + inclusive: true,
247 + }),
248 + itemCount: group.length,
249 + lastOrdinal: last.item.ordinal ?? last.position,
250 + },
251 + },
252 + });
253 + }
254 + return items.flatMap((row) => (omitted.has(row) ? [] : [anchors.get(row) ?? row]));
255 +}
256 +
257 +export function selectDeferredWorkPage<Row extends HistoryRow>(input: {
258 + readonly items: ReadonlyArray<Row>;
259 + readonly cursor: string;
260 + readonly throughEntryId?: string | undefined;
261 + readonly snapshotSequence: number;
262 + readonly rowEncodedBytes?: ((row: Row) => number) | undefined;
263 +}): SelectTimelinePageResult<Row> {
264 + const cursor = decodeThreadHistoryCursor(input.cursor);
265 + const end = findCursorEndIndex(input.items, cursor);
266 + const start = input.items.findIndex((row) => row.sourceItemId === input.throughEntryId);
267 + if (start < 0 || start >= end) throw new InvalidThreadHistoryCursorError();
268 + const anchor = input.items[start]!;
269 + const items = input.items
270 + .slice(start, end)
271 + .filter(
272 + (row) =>
273 + row.sourceThreadId === anchor.sourceThreadId &&
274 + row.item.runId === anchor.item.runId &&
275 + row.item.attemptId === anchor.item.attemptId &&
276 + isDeferrableWorkItem(row.item),
277 + );
278 + return selectOlderTimelinePage({
279 + items,
280 + exclusiveEndIndex: items.length,
281 + snapshotSequence: input.snapshotSequence,
282 + rowEncodedBytes: input.rowEncodedBytes,
283 + policy: THREAD_HISTORY_PAGE_POLICY,
284 + });
285 +}
286 +
287 /** Notifications can start wake turns when they are the run's first input; match ProjectionStore run_starts. */
288 function threadHistoryTurnStarts(items: ReadonlyArray<HistoryRow["item"]>): ReadonlyArray<boolean> {
289 const startedRuns = new Set<string>();
@@ -275,17 +392,23 @@ export function selectRecentTimelineWindow(input: {
392 });
393 }
394
278 -function findCursorIndex(
279 - items: ReadonlyArray<Pick<HistoryRow, "sourceThreadId" | "sourceItemId">>,
395 +function findCursorEndIndex(
396 + items: ReadonlyArray<HistoryRow>,
397 cursor: ThreadHistoryCursorPayload,
398 ): number {
399 const byIdentity = items.findIndex(
400 (row) => String(row.sourceThreadId) === cursor.st && String(row.sourceItemId) === cursor.si,
401 );
402 if (byIdentity !== -1) {
286 - return byIdentity;
403 + return byIdentity + (cursor.inclusive ? 1 : 0);
404 }
288 - return Math.min(cursor.p, items.length);
405 + // Inclusive deferred cursors carry source ordinals; ordinary cursors carry visible positions.
406 + const byPosition = items.findIndex((row) =>
407 + cursor.inclusive
408 + ? String(row.sourceThreadId) === cursor.st && (row.item.ordinal ?? row.position) > cursor.p
409 + : row.position >= cursor.p,
410 + );
411 + return byPosition === -1 ? items.length : byPosition;
412 }
413
414 /**
@@ -304,7 +427,7 @@ export function olderHistoryPagePolicy<Row extends HistoryRow>(input: {
427 maxUserTurns: OLDER_THREAD_USER_TURN_LIMIT,
428 };
429 if (input.throughEntryId === undefined) return policy;
307 - const anchorIndex = findCursorIndex(input.items, decodeThreadHistoryCursor(input.cursor));
430 + const anchorIndex = findCursorEndIndex(input.items, decodeThreadHistoryCursor(input.cursor));
431 const targetIndex = input.items.findIndex(
432 (row) => (row.item.messageId ?? row.item.id) === input.throughEntryId,
433 );
@@ -337,7 +460,7 @@ export function selectHistoryPageFromCursor<Row extends HistoryRow>(input: {
460 const decoded = decodeThreadHistoryCursor(input.cursor);
461 return selectOlderTimelinePage({
462 items: input.items,
340 - exclusiveEndIndex: findCursorIndex(input.items, decoded),
463 + exclusiveEndIndex: findCursorEndIndex(input.items, decoded),
464 snapshotSequence: input.snapshotSequence,
465 ...(input.rowEncodedBytes === undefined ? {} : { rowEncodedBytes: input.rowEncodedBytes }),
466 policy: olderHistoryPagePolicy(input),
@@ -351,7 +474,7 @@ export function selectHistoryPageFromCursor<Row extends HistoryRow>(input: {
474 */
475 export function missingInterruptRequests<Row extends HistoryRow>(
476 items: ReadonlyArray<Row>,
354 - page: ReadonlyArray<Pick<HistoryRow, "item" | "sourceThreadId" | "sourceItemId">>,
477 + page: ReadonlyArray<Pick<HistoryRow, "item" | "sourceThreadId" | "sourceItemId" | "position">>,
478 ): Row[] {
479 const runs = new Set<string>();
480 for (const row of page) {
@@ -407,16 +530,7 @@ function localTurnItemsForVisibleWindow(
530 projection: OrchestrationV2ThreadProjection,
531 visible: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
532 ): OrchestrationV2TurnItem[] {
410 - const localIds = new Set<string>();
411 - for (const row of visible) {
412 - if (isLocalProjectedRow(projection, row)) {
413 - localIds.add(String(row.sourceItemId));
414 - }
415 - }
416 - if (localIds.size === 0) {
417 - return [];
418 - }
419 - return projection.turnItems.filter((item) => localIds.has(String(item.id)));
533 + return visible.filter((row) => isLocalProjectedRow(projection, row)).map((row) => row.item);
534 }
535
536 function messagesForBoundedProjection(
apps/server/src/ws.ts
+3
@@ -640,6 +640,7 @@ function hasCompatibleOrchestrationProtocol(url: URL): boolean {
640
641 function shouldUseBoundedThreadSnapshot(input: {
642 readonly acceptBoundedSnapshot?: boolean;
643 + readonly acceptDeferredWork?: boolean;
644 }): boolean {
645 return input.acceptBoundedSnapshot === true;
646 }
@@ -773,6 +774,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
774 readonly afterSequence?: number;
775 readonly requestCompletionMarker?: boolean;
776 readonly acceptBoundedSnapshot?: boolean;
777 + readonly acceptDeferredWork?: boolean;
778 readonly acceptCompactTurnItems?: boolean;
779 readonly acceptDedupedBodies?: boolean;
780 readonly acceptCompactIds?: boolean;
@@ -860,6 +862,7 @@ export const subscribeOrchestrationV2Thread = Effect.fn("ws.orchestrationV2.subs
862 useBoundedSnapshot
863 ? threadManagement.getThreadSnapshotWindow(input.threadId, {
864 rowLimit: THREAD_HISTORY_SNAPSHOT_ROW_LIMIT,
865 + deferWork: input.acceptDeferredWork === true,
866 userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
867 })
868 : threadManagement.getThreadSnapshot(input.threadId)
apps/web/src/components/chat/MessagesTimeline.logic.test.ts
+83
@@ -2693,6 +2693,13 @@ describe("v2 run and attempt history", () => {
2693 runId,
2694 label: "Old command",
2695 tone: "tool" as const,
2696 + projectedItem: {
2697 + sourceItemId: "old-work-anchor",
2698 + item: {
2699 + type: "command_execution",
2700 + deferredWork: { cursor: "old-work", itemCount: 200, lastOrdinal: 200 },
2701 + },
2702 + } as never,
2703 },
2704 },
2705 {
@@ -2765,6 +2772,7 @@ describe("v2 run and attempt history", () => {
2772 runId,
2773 label: "Superseded attempt",
2774 expanded: false,
2775 + deferredWorkItemIds: ["old-work-anchor"],
2776 });
2777
2778 const expandedRows = deriveMessagesTimelineRows({
@@ -2781,6 +2789,52 @@ describe("v2 run and attempt history", () => {
2789 "steer-user-entry",
2790 "active-assistant-entry",
2791 ]);
2792 + const settledRows = deriveMessagesTimelineRows({
2793 + ...common,
2794 + latestRun: { ...common.latestRun, status: "completed", completedAt: "2026-01-01T00:00:08Z" },
2795 + timelineEntries: timelineEntries.map((entry) =>
2796 + entry.kind === "message" && entry.id === "active-assistant-entry"
2797 + ? { ...entry, message: { ...entry.message, streaming: false } }
2798 + : entry,
2799 + ),
2800 + expandedRunIds: new Set([runId]),
2801 + });
2802 + expect(settledRows.find((row) => row.kind === "attempt-fold")).toMatchObject({
2803 + deferredWorkItemIds: ["old-work-anchor"],
2804 + });
2805 + expect(
2806 + settledRows
2807 + .filter((row) => row.kind === "turn-fold")
2808 + .flatMap((row) => row.deferredWorkItemIds ?? []),
2809 + ).not.toContain("old-work-anchor");
2810 + const deferredTailRows = deriveMessagesTimelineRows({
2811 + ...common,
2812 + timelineEntries: [
2813 + timelineEntries[0]!,
2814 + timelineEntries[2]!,
2815 + timelineEntries[1]!,
2816 + ...timelineEntries.slice(3),
2817 + ].map((entry) => ({
2818 + ...entry,
2819 + attempt:
2820 + entry.attempt.id === supersededAttemptId
2821 + ? { ...entry.attempt, completedAt: DateTime.makeUnsafe("2026-01-01T00:00:08Z") }
2822 + : entry.attempt,
2823 + ...(entry.id === "superseded-assistant-entry"
2824 + ? {
2825 + projectedItem: {
2826 + item: {
2827 + type: "assistant_message",
2828 + ordinal: 2,
2829 + status: "completed",
2830 + completedAt: DateTime.makeUnsafe("2026-01-01T00:00:03Z"),
2831 + },
2832 + } as never,
2833 + }
2834 + : {}),
2835 + })),
2836 + });
2837 + expect(deferredTailRows.some((row) => row.kind === "attempt-fold")).toBe(true);
2838 });
2839
2840 describe("a steer that supersedes an attempt", () => {
@@ -3015,6 +3069,35 @@ describe("v2 run and attempt history", () => {
3069 });
3070 });
3071
3072 +it.each([
3073 + [undefined, ["first"]],
3074 + [["first"], ["second"]],
3075 + [["first"], []],
3076 + [
3077 + ["first", "second"],
3078 + ["second", "first"],
3079 + ],
3080 + [undefined, []],
3081 +] as const)("keeps deferred fold anchors current from %j to %j", (before, after) => {
3082 + const row: Extract<MessagesTimelineRow, { kind: "turn-fold" }> = {
3083 + kind: "turn-fold",
3084 + id: "fold",
3085 + runId: RunId.make("run"),
3086 + createdAt: "2026-01-01T00:00:00Z",
3087 + label: "Worked for 1m",
3088 + expanded: true,
3089 + ...(before === undefined ? {} : { deferredWorkItemIds: before }),
3090 + };
3091 + const previous = computeStableMessagesTimelineRows([row], { result: [], byId: new Map() });
3092 + const nextRow = { ...row, deferredWorkItemIds: after };
3093 + const next = computeStableMessagesTimelineRows([nextRow], previous);
3094 + const fold = next.result[0];
3095 + expect(fold?.kind === "turn-fold" ? (fold.deferredWorkItemIds ?? []) : null).toEqual(after);
3096 + expect(
3097 + computeStableMessagesTimelineRows([{ ...nextRow, deferredWorkItemIds: [...after] }], next),
3098 + ).toBe(next);
3099 +});
3100 +
3101 describe("streaming v2 row projection", () => {
3102 function fixture(text = "") {
3103 const source = makeStreamingTimelineFixture(text);
apps/web/src/components/chat/MessagesTimeline.logic.ts
+43 -6
@@ -553,6 +553,7 @@ type MessagesTimelineRowContent =
553 }
554 | {
555 kind: "turn-fold";
556 + deferredWorkItemIds?: ReadonlyArray<string>;
557 id: string;
558 createdAt: string;
559 runId: RunId;
@@ -561,6 +562,7 @@ type MessagesTimelineRowContent =
562 }
563 | {
564 kind: "attempt-fold";
565 + deferredWorkItemIds?: ReadonlyArray<string>;
566 id: string;
567 createdAt: string;
568 runId: RunId;
@@ -709,6 +711,7 @@ interface TurnFold {
711 }
712
713 interface SupersededAttemptFold {
714 + readonly deferredWorkItemIds: ReadonlyArray<string>;
715 readonly runId: RunId;
716 readonly attemptId: RunAttemptId;
717 readonly anchorEntryId: string;
@@ -731,6 +734,18 @@ function completeSupersededReplyEntryIds(
734 ) {
735 continue;
736 }
737 + const previous = lastOutputByAttemptId.get(entry.attempt.id);
738 + const deferredEnd =
739 + previous?.kind === "work"
740 + ? previous.entry.projectedItem?.item.deferredWork?.lastOrdinal
741 + : undefined;
742 + const ordinal =
743 + entry.kind === "work"
744 + ? entry.entry.projectedItem?.item.ordinal
745 + : entry.kind === "message"
746 + ? entry.projectedItem?.item.ordinal
747 + : undefined;
748 + if (deferredEnd !== undefined && ordinal !== undefined && deferredEnd > ordinal) continue;
749 lastOutputByAttemptId.set(entry.attempt.id, entry);
750 }
751 const replyEntryIds = new Set<string>();
@@ -790,6 +805,11 @@ function deriveSupersededAttemptFolds(
805 const attempt = firstEntry?.attempt;
806 if (firstEntry === undefined || attempt === undefined) continue;
807 foldsByAnchorEntryId.set(firstEntry.id, {
808 + deferredWorkItemIds: entries.flatMap((entry) =>
809 + entry.kind === "work" && entry.entry.projectedItem?.item.deferredWork
810 + ? [String(entry.entry.projectedItem.sourceItemId)]
811 + : [],
812 + ),
813 runId: attempt.runId,
814 attemptId: attempt.id,
815 anchorEntryId: firstEntry.id,
@@ -1398,6 +1418,9 @@ export function deriveMessagesTimelineRows(input: {
1418 liveSubagentEntryIds,
1419 completeSupersededReplyIds,
1420 );
1421 + const supersededWorkItemIds = new Set(
1422 + [...supersededFoldsByAnchorEntryId.values()].flatMap((fold) => fold.deferredWorkItemIds),
1423 + );
1424 const activeVisualResponseRunIds = deriveActiveVisualResponseRunIds({
1425 timelineEntries: timelineEntries,
1426 unsettledRunId,
@@ -1658,9 +1681,18 @@ export function deriveMessagesTimelineRows(input: {
1681
1682 const turnFold = foldsByAnchorEntryId.get(timelineEntry.id);
1683 if (turnFold) {
1684 + const deferredWorkItemIds = timelineEntries.flatMap((entry) =>
1685 + turnFold.entryIds.has(entry.id) &&
1686 + entry.kind === "work" &&
1687 + entry.entry.projectedItem?.item.deferredWork &&
1688 + !supersededWorkItemIds.has(String(entry.entry.projectedItem.sourceItemId))
1689 + ? [String(entry.entry.projectedItem.sourceItemId)]
1690 + : [],
1691 + );
1692 nextRows.push({
1693 kind: "turn-fold",
1694 id: `turn-fold:${turnFold.runId}`,
1695 + ...(deferredWorkItemIds.length > 0 ? { deferredWorkItemIds } : {}),
1696 createdAt: turnFold.createdAt,
1697 runId: turnFold.runId,
1698 label: turnFold.label,
@@ -1676,6 +1708,7 @@ export function deriveMessagesTimelineRows(input: {
1708 if (supersededFold) {
1709 nextRows.push({
1710 kind: "attempt-fold",
1711 + deferredWorkItemIds: supersededFold.deferredWorkItemIds,
1712 id: `attempt-fold:${supersededFold.attemptId}`,
1713 createdAt: supersededFold.createdAt,
1714 runId: supersededFold.runId,
@@ -2326,14 +2359,18 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
2359 );
2360 }
2361
2362 + case "attempt-fold":
2363 case "turn-fold": {
2364 const bf = b as typeof a;
2331 - return a.createdAt === bf.createdAt && a.label === bf.label && a.expanded === bf.expanded;
2332 - }
2333 -
2334 - case "attempt-fold": {
2335 - const bf = b as typeof a;
2336 - return a.createdAt === bf.createdAt && a.label === bf.label && a.expanded === bf.expanded;
2365 + const anchors = a.deferredWorkItemIds ?? [];
2366 + const nextAnchors = bf.deferredWorkItemIds ?? [];
2367 + return (
2368 + a.createdAt === bf.createdAt &&
2369 + a.label === bf.label &&
2370 + a.expanded === bf.expanded &&
2371 + anchors.length === nextAnchors.length &&
2372 + anchors.every((id, index) => id === nextAnchors[index])
2373 + );
2374 }
2375
2376 case "context-compaction": {
apps/web/src/components/chat/MessagesTimeline.tsx
+112 -32
@@ -1,3 +1,5 @@
1 +import { threadEnvironment } from "../../state/threads";
2 +import { useAtomCommand } from "../../state/use-atom-command";
3 import type { ToolCallDiffTarget } from "../../diffPanelStore";
4 import { AudioPreviewUnmountContext } from "../files/AudioPreview";
5 import { ThreadFindTimelineContext } from "./ThreadFindProvider";
@@ -36,6 +38,8 @@ import {
38 use,
39 useContext,
40 useLayoutEffect,
41 + useEffect,
42 + useEffectEvent,
43 useRef,
44 useState,
45 type ReactNode,
@@ -52,6 +56,7 @@ import { type ChatFileAttachment, type TurnDiffSummary } from "../../types";
56 import { ArrowRightIcon, BotIcon, CircleAlertIcon, Minimize2Icon } from "lucide-react";
57 import { ChevronDown, ChevronRight } from "lucide";
58 import { MorphIcon } from "~/components/MorphIcon";
59 +import { Skeleton } from "../ui/skeleton";
60 import { ExpandedImagePreview } from "./ExpandedImagePreview";
61 import {
62 CHAT_TIMELINE_ANCHOR_OFFSET,
@@ -1517,7 +1522,7 @@ const TimelineRowContent = memo(function TimelineRowContent({ row }: { row: Time
1522 {row.kind === "thinking" ? <ThinkingTimelineRow row={row} /> : null}
1523 </WorkLogBlock>
1524 ) : null}
1520 - {row.kind === "turn-fold" ? <TurnFoldTimelineRow row={row} /> : null}
1525 + {row.kind === "turn-fold" ? <TurnFoldTimelineRow key={row.id} row={row} /> : null}
1526 {row.kind === "attempt-fold" ? <AttemptFoldTimelineRow row={row} /> : null}
1527 {row.kind === "context-compaction" ? <ContextCompactionTimelineRow row={row} /> : null}
1528 {row.kind === "message" && row.message.role === "user" ? <UserMessageRow row={row} /> : null}
@@ -1609,48 +1614,123 @@ function ContextCompactionTimelineRow({
1614 );
1615 }
1616
1617 +function useDeferredWorkLoading(
1618 + row: { expanded: boolean; deferredWorkItemIds?: ReadonlyArray<string> },
1619 + onToggle: () => void,
1620 +) {
1621 + const ctx = use(TimelineRowCtx);
1622 + const loadWork = useAtomCommand(threadEnvironment.loadEarlierHistory, { reportFailure: false });
1623 + const ref = parseScopedThreadKey(ctx.routeThreadKey);
1624 + const environmentId = ref?.environmentId;
1625 + const threadId = ref?.threadId;
1626 + const anchorsKey = row.deferredWorkItemIds?.join("\n") ?? "";
1627 + const [failed, setFailed] = useState(false);
1628 + const load = () => {
1629 + if (!anchorsKey || environmentId === undefined || threadId === undefined) return;
1630 + void Promise.all(
1631 + anchorsKey
1632 + .split("\n")
1633 + .map((workAnchorId) => loadWork({ environmentId, input: { threadId, workAnchorId } })),
1634 + ).then((results) =>
1635 + setFailed(
1636 + results.some((result) => result._tag === "Failure" || result.value._tag === "error"),
1637 + ),
1638 + );
1639 + };
1640 + const loadOnExpand = useEffectEvent(load);
1641 + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Restored expanded folds load their deferred server history on mount.
1642 + useEffect(() => {
1643 + if (row.expanded && anchorsKey && environmentId && threadId) loadOnExpand();
1644 + }, [row.expanded, anchorsKey, environmentId, threadId]);
1645 + const error = anchorsKey && failed ? "Couldn't load work. Click to retry." : null;
1646 + const loading = ref !== null && row.expanded && anchorsKey !== "" && !failed;
1647 + const toggle = () => {
1648 + setFailed(false);
1649 + if (error && row.expanded) load();
1650 + else onToggle();
1651 + };
1652 +
1653 + return { loading, error, toggle };
1654 +}
1655 +
1656 +function WorkHistorySkeleton() {
1657 + return (
1658 + <div role="status" aria-label="Loading work history" className="space-y-2 px-1 pb-1 pt-3">
1659 + {[0, 1, 2].map((index) => (
1660 + <div key={index} className="flex items-center gap-2" aria-hidden="true">
1661 + <Skeleton className="size-3 shrink-0" />
1662 + <Skeleton
1663 + shape="pill"
1664 + className={index === 0 ? "h-2.5 w-2/5" : index === 1 ? "h-2.5 w-3/5" : "h-2.5 w-1/3"}
1665 + />
1666 + </div>
1667 + ))}
1668 + </div>
1669 + );
1670 +}
1671 +
1672 function TurnFoldTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "turn-fold" }> }) {
1673 const ctx = use(TimelineRowCtx);
1674 + const { loading, error, toggle } = useDeferredWorkLoading(row, () =>
1675 + ctx.onToggleTurnFold(row.runId),
1676 + );
1677
1678 return (
1616 - <div className="group/timeline-row relative flex items-center gap-1 border-b border-border/60 pb-2 pe-0.5 pt-1">
1617 - <button
1618 - type="button"
1619 - aria-expanded={row.expanded}
1620 - data-scroll-anchor-ignore
1621 - onClick={() => ctx.onToggleTurnFold(row.runId)}
1622 - className="flex cursor-pointer select-none items-center gap-1 rounded-md px-1 text-sm leading-relaxed text-muted-foreground tabular-nums transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
1623 - >
1624 - <span>{row.label}</span>
1625 - <MorphIcon className="size-3.5" icon={row.expanded ? ChevronDown : ChevronRight} />
1626 - </button>
1627 - <TimelineRowTimestamp
1628 - createdAt={row.createdAt}
1629 - timestampFormat={ctx.timestampFormat}
1630 - className="ms-auto"
1631 - />
1679 + <div className="group/timeline-row border-b border-border/60 pb-2 pe-0.5 pt-1">
1680 + <div className="relative flex items-center gap-1">
1681 + <button
1682 + type="button"
1683 + aria-expanded={row.expanded}
1684 + data-scroll-anchor-ignore
1685 + onClick={toggle}
1686 + aria-busy={loading}
1687 + className="flex cursor-pointer select-none items-center gap-1 rounded-md px-1 text-sm leading-relaxed text-muted-foreground tabular-nums transition-colors hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
1688 + >
1689 + <span>
1690 + {row.label}
1691 + {error ? ` · ${error}` : ""}
1692 + </span>
1693 + <MorphIcon className="size-3.5" icon={row.expanded ? ChevronDown : ChevronRight} />
1694 + </button>
1695 + <TimelineRowTimestamp
1696 + createdAt={row.createdAt}
1697 + timestampFormat={ctx.timestampFormat}
1698 + className="ms-auto"
1699 + />
1700 + </div>
1701 + {loading ? <WorkHistorySkeleton /> : null}
1702 </div>
1703 );
1704 }
1705
1706 function AttemptFoldTimelineRow({ row }: { row: Extract<TimelineRow, { kind: "attempt-fold" }> }) {
1707 const ctx = use(TimelineRowCtx);
1708 + const { loading, error, toggle } = useDeferredWorkLoading(row, () =>
1709 + ctx.onToggleAttemptFold(row.attemptId),
1710 + );
1711
1712 return (
1640 - <button
1641 - type="button"
1642 - aria-expanded={row.expanded}
1643 - data-scroll-anchor-ignore
1644 - data-superseded-attempt-id={row.attemptId}
1645 - onClick={() => ctx.onToggleAttemptFold(row.attemptId)}
1646 - className="flex w-full cursor-pointer select-none items-center gap-2 rounded-md border border-border/60 bg-muted/20 px-2.5 py-2 text-left transition-colors hover:bg-muted/35 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
1647 - >
1648 - <MorphIcon
1649 - className="size-3.5 shrink-0 text-muted-foreground"
1650 - icon={row.expanded ? ChevronDown : ChevronRight}
1651 - />
1652 - <span className="text-xs font-medium text-foreground/80">{row.label}</span>
1653 - <span className="text-2xs text-muted-foreground">Cut off by a steer</span>
1654 - </button>
1713 + <div>
1714 + <button
1715 + type="button"
1716 + aria-expanded={row.expanded}
1717 + data-scroll-anchor-ignore
1718 + data-superseded-attempt-id={row.attemptId}
1719 + onClick={toggle}
1720 + aria-busy={loading}
1721 + className="flex w-full cursor-pointer select-none items-center gap-2 rounded-md border border-border/60 bg-muted/20 px-2.5 py-2 text-left transition-colors hover:bg-muted/35 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring/70"
1722 + >
1723 + <MorphIcon
1724 + className="size-3.5 shrink-0 text-muted-foreground"
1725 + icon={row.expanded ? ChevronDown : ChevronRight}
1726 + />
1727 + <span className="text-xs font-medium text-foreground/80">
1728 + {row.label}
1729 + {error ? ` · ${error}` : ""}
1730 + </span>
1731 + <span className="text-2xs text-muted-foreground">Cut off by a steer</span>
1732 + </button>
1733 + {loading ? <WorkHistorySkeleton /> : null}
1734 + </div>
1735 );
1736 }
apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+1 -1
@@ -1284,7 +1284,7 @@ export function PullRequestDetailPanel({
1284 openOnHostText={openOnHostText}
1285 onBack={onBack}
1286 />
1287 - <div className="mr-4 flex h-7 shrink-0 items-center justify-end gap-1">
1287 + <div className="mr-4 flex h-7 shrink-0 select-none items-center justify-end gap-1">
1288 {detail ? (
1289 <TooltipProvider delay={150} closeDelay={150} timeout={400}>
1290 {!nativeStack && supportsStackActions && nativeStackQuery.error ? (
apps/web/src/session-logic.test.ts
+14
@@ -557,6 +557,20 @@ describe("V2 session presentation", () => {
557 if (threadCreatedEntry?.kind === "work") {
558 expect(threadCreatedEntry.entry.projectedItem?.item.type).toBe("thread_created");
559 }
560 + expect(
561 + deriveTimelineEntriesFromVisibleTurnItems({
562 + visibleTurnItems: [
563 + {
564 + ...visibleTurnItems.at(-1)!,
565 + item: {
566 + ...workspacePreparationItem,
567 + deferredWork: { cursor: "work-page", itemCount: 100, lastOrdinal: 107 },
568 + },
569 + },
570 + ],
571 + optimisticMessages: [],
572 + }).map((entry) => entry.id),
573 + ).toEqual([workspacePreparationItem.id]);
574 });
575
576 it.each(["pending", "running", "completed"] as const)(
apps/web/src/session-logic.ts
+1 -1
@@ -601,7 +601,7 @@ export function deriveTimelineEntriesFromVisibleTurnItems(
601 );
602 for (const row of input.visibleTurnItems) {
603 const { item } = row;
604 - if (turnItemIsWorkspacePreparation(item)) continue;
604 + if (turnItemIsWorkspacePreparation(item) && !item.deferredWork) continue;
605 if (item.type === "checkpoint") continue;
606 if (
607 item.type === "user_message" &&
docs/internals/performance-regressions.md
+6 -3
@@ -15,7 +15,9 @@ The v2 checks pin these invariants:
15 - Cold opens and history pages stop at about 1 MiB of encoded rows. A window that holds a user
16 turn also stops at 10 user turns (20 on older pages) or 500 rows, so ordinary turns stay whole.
17 Any other window, including the inside of one turn longer than 500 rows, stops at 75 rows. The
18 - snapshot query applies the same rule before it decodes payloads. Required control records and
18 + snapshot query applies the same rule before it decodes payloads. Clients can opt into deferring
19 + large work groups until expansion. Messages and items outside those groups remain in normal
20 + history paging. Required control records and
21 the at-least-one-row rule can exceed the byte target; it is not a hard cap.
22 - The bounded snapshot does not retain the full duplicate conversation-message table.
23 - Older activity is fetched in bounded HTTP pages and merged without disturbing the live scroll
@@ -32,8 +34,9 @@ The v2 checks pin these invariants:
34 projects small can increase server allocations on repeated large tool updates.
35 - Raw command output, dynamic-tool result bodies, and inline file-change bodies are omitted at
36 the wire boundary, including small outputs. Explicit failure flags and bounded result IDs
35 - preserve status and grouped action counts without shipping those bodies. Subagent prompts and
36 - results, reasoning text, command lines, search results, and checkpoint file lists still ship.
37 + preserve status and grouped action counts without shipping those bodies. Reasoning text,
38 + command lines, and search results ship when their work group loads. Subagent prompts and results
39 + and checkpoint control records still accompany the initial window.
40 Persisted events remain complete; the existing diff endpoints still provide file content when
41 requested.
42 Explicit cross-environment transcript exports preserve full tool bodies so the destination
packages/client-runtime/src/state/boundedThreadSnapshotHttp.ts
+6 -1
@@ -30,7 +30,12 @@ export const fetchEnvironmentBoundedThreadSnapshot = Effect.fn(
30 }) {
31 const endpoint = {
32 params: { threadId: input.threadId },
33 - query: { compactTurnItems: "1", dedupeBodies: "1", compactIds: "1" },
33 + query: {
34 + compactTurnItems: "1",
35 + dedupeBodies: "1",
36 + compactIds: "1",
37 + deferWork: "1",
38 + },
39 };
40 return yield* executeAuthenticatedEnvironmentHttpRequest({
41 ...input,
packages/client-runtime/src/state/orchestrationV2Projection.test.ts
+5 -3
@@ -219,8 +219,9 @@ describe("applyOrchestrationV2ProjectionEvent", () => {
219 expect(next?.visibleTurnItems[0]).toBe(visibleTurnItems[0]);
220 });
221
222 - it("replaces only the updated visible item when membership is unchanged", () => {
223 - const first = commandItem("item-first", "first");
222 + it("replaces only the updated visible item and retains its deferred work cursor", () => {
223 + const deferredWork = { cursor: "remaining-work", itemCount: 100, lastOrdinal: 101 };
224 + const first = { ...commandItem("item-first", "first"), deferredWork };
225 const second = commandItem("item-second", "second");
226 const firstRow = {
227 position: 0,
@@ -253,9 +254,10 @@ describe("applyOrchestrationV2ProjectionEvent", () => {
254 } as OrchestrationV2DomainEvent;
255
256 const next = applyOrchestrationV2ProjectionEvent(projection, event);
257 + expect(next?.visibleTurnItems[0]?.item.deferredWork).toEqual(deferredWork);
258 expect(next?.visibleTurnItems).not.toBe(projection.visibleTurnItems);
259 expect(next?.visibleTurnItems[0]).not.toBe(firstRow);
258 - expect(next?.visibleTurnItems[0]?.item).toBe(updated);
260 + expect(next?.visibleTurnItems[0]?.item).toEqual({ ...updated, deferredWork });
261 expect(next?.visibleTurnItems[1]).toBe(secondRow);
262 });
263
packages/client-runtime/src/state/orchestrationV2Projection.ts
+4 -1
@@ -370,12 +370,15 @@ export function applyOrchestrationV2ProjectionEvent(
370 case "plan.updated":
371 return { ...base, plans: upsertEntity(base.plans, event.payload) };
372 case "turn-item.updated": {
373 - const payload = restoreTurnItemBodies(event.payload, latestBodies(projection));
373 + let payload = restoreTurnItemBodies(event.payload, latestBodies(projection));
374 // Streamed updates land on the newest items, so search from the end.
375 let itemIndex = projection.turnItems.length - 1;
376 while (itemIndex >= 0 && projection.turnItems[itemIndex]!.id !== payload.id) {
377 itemIndex -= 1;
378 }
379 + const deferredWork =
380 + itemIndex === -1 ? undefined : projection.turnItems[itemIndex]?.deferredWork;
381 + if (deferredWork !== undefined) payload = { ...payload, deferredWork };
382 if (
383 partialTimeline &&
384 itemIndex === -1 &&
packages/client-runtime/src/state/threadCommands.ts
+2
@@ -64,6 +64,7 @@ import * as ThreadHistoryController from "./threadHistoryController.ts";
64 export type LoadEarlierThreadHistoryInput = {
65 readonly threadId: ThreadId;
66 readonly throughEntryId?: string;
67 + readonly workAnchorId?: string;
68 };
69
70 export function createThreadEnvironmentAtoms<R, E>(
@@ -90,6 +91,7 @@ export function createThreadEnvironmentAtoms<R, E>(
91 supervisor.target.environmentId,
92 input.threadId,
93 input.throughEntryId,
94 + input.workAnchorId,
95 );
96 });
97
packages/client-runtime/src/state/threadHistoryController.ts
+7 -3
@@ -13,7 +13,10 @@ export type ThreadHistoryLoadEarlierResult =
13 | { readonly _tag: "error"; readonly message: string };
14
15 export type ThreadHistoryHandler = {
16 - readonly loadEarlier: (throughEntryId?: string) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
16 + readonly loadEarlier: (
17 + throughEntryId?: string,
18 + workAnchorId?: string,
19 + ) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
20 readonly reconcilePendingWork: () => Effect.Effect<void>;
21 };
22
@@ -36,6 +39,7 @@ export class ThreadHistoryController extends Context.Service<
39 environmentId: EnvironmentId,
40 threadId: ThreadId,
41 throughEntryId?: string,
42 + workAnchorId?: string,
43 ) => Effect.Effect<ThreadHistoryLoadEarlierResult>;
44 readonly reconcilePendingWork: (
45 environmentId: EnvironmentId,
@@ -81,14 +85,14 @@ export const layer: Layer.Layer<ThreadHistoryController> = Layer.effect(
85 });
86 }),
87 loadEarlier: Effect.fn("ThreadHistoryController.loadEarlier")(
84 - function (environmentId, threadId, throughEntryId) {
88 + function (environmentId, threadId, throughEntryId, workAnchorId) {
89 return Ref.get(handlers).pipe(
90 Effect.flatMap((current) => {
91 const handler = current.get(threadKey({ environmentId, threadId }));
92 if (handler === undefined) {
93 return Effect.succeed({ _tag: "noop" } satisfies ThreadHistoryLoadEarlierResult);
94 }
91 - return handler.loadEarlier(throughEntryId);
95 + return handler.loadEarlier(throughEntryId, workAnchorId);
96 }),
97 );
98 },
packages/client-runtime/src/state/threadHistoryHttp.ts
+2 -1
@@ -19,7 +19,7 @@ export const fetchEnvironmentThreadHistoryPage = Effect.fn(
19 readonly threadId: ThreadId;
20 readonly cursor: string;
21 readonly throughEntryId?: string | undefined;
22 - readonly view?: "conversation" | "activity" | undefined;
22 + readonly view?: "conversation" | "activity" | "work" | undefined;
23 readonly signer: Option.Option<ManagedRelay.ManagedRelayDpopSigner["Service"]>;
24 readonly remoteAuthorization?: Option.Option<RemoteEnvironmentAuthorization["Service"]>;
25 readonly timeoutMs?: number;
@@ -29,6 +29,7 @@ export const fetchEnvironmentThreadHistoryPage = Effect.fn(
29 query: {
30 cursor: input.cursor,
31 compactIds: "1",
32 + ...(input.throughEntryId === undefined ? { deferWork: "1" } : {}),
33 ...(input.view === undefined ? {} : { view: input.view }),
34 ...(input.throughEntryId === undefined ? {} : { throughEntryId: input.throughEntryId }),
35 },
packages/client-runtime/src/state/threadHistoryMerge.test.ts
+23
@@ -64,6 +64,29 @@ describe("threadHistoryMerge", () => {
64 expect(complete.visibleTurnItems[1]?.item).toBe(updated.item);
65 expect(complete.visibleTurnItems.map((entry) => entry.position)).toEqual([0, 1, 2, 3, 4]);
66 expect(new Set(complete.turnItems.map((item) => item.id)).size).toBe(5);
67 + const anchorItem = {
68 + ...updated.item,
69 + deferredWork: { cursor: "work", itemCount: 4, lastOrdinal: 5 },
70 + };
71 + const deferred = {
72 + ...live,
73 + turnItems: live.turnItems.map((item) => (item.id === anchorItem.id ? anchorItem : item)),
74 + visibleTurnItems: live.visibleTurnItems.map((entry) =>
75 + entry.item.id === anchorItem.id ? { ...entry, item: anchorItem } : entry,
76 + ),
77 + };
78 + const loadedWork = mergeOlderHistoryIntoProjection(deferred, [row(1), row(2), row(3)], {
79 + anchorId: anchorItem.id,
80 + nextCursor: null,
81 + });
82 + expect(loadedWork.visibleTurnItems.map((entry) => entry.sourceItemId)).toEqual([
83 + "item-1",
84 + "item-2",
85 + "item-3",
86 + "item-4",
87 + ]);
88 + expect(loadedWork.visibleTurnItems[0]?.item.title).toBe("newer live title");
89 + expect(loadedWork.visibleTurnItems[0]?.item.deferredWork).toBeUndefined();
90 const hidden = {
91 ...partial,
92 visibleTurnItems: partial.visibleTurnItems.filter(
packages/client-runtime/src/state/threadHistoryMerge.ts
+52 -3
@@ -52,11 +52,19 @@ function renumberVisible(
52 export function mergeOlderHistoryIntoProjection(
53 projection: OrchestrationV2ThreadProjection,
54 olderItems: ReadonlyArray<OrchestrationV2ProjectedTurnItem>,
55 + work?: { readonly anchorId: string; readonly nextCursor: string | null },
56 ): OrchestrationV2ThreadProjection {
56 - if (olderItems.length === 0) {
57 + if (olderItems.length === 0 && work === undefined) {
58 return projection;
59 }
60
61 + const start =
62 + work === undefined
63 + ? -1
64 + : projection.visibleTurnItems.findIndex((row) => row.sourceItemId === work.anchorId);
65 + const anchor = projection.visibleTurnItems[start];
66 + const deferred = anchor?.item.deferredWork;
67 + if (work !== undefined && deferred === undefined) return projection;
68 const currentByKey = new Map(
69 projection.visibleTurnItems.map((row) => [projectedItemKey(row), row]),
70 );
@@ -64,6 +72,15 @@ export function mergeOlderHistoryIntoProjection(
72 const pageKeys = new Set<string>();
73 const page: OrchestrationV2ProjectedTurnItem[] = [];
74 for (const pageRow of olderItems) {
75 + if (
76 + work &&
77 + anchor &&
78 + deferred &&
79 + (pageRow.sourceThreadId !== anchor.sourceThreadId ||
80 + pageRow.item.ordinal < anchor.item.ordinal ||
81 + pageRow.item.ordinal > deferred.lastOrdinal)
82 + )
83 + continue;
84 const key = projectedItemKey(pageRow);
85 if (pageKeys.has(key)) continue;
86 let row = currentByKey.get(key) ?? pageRow;
@@ -94,11 +111,43 @@ export function mergeOlderHistoryIntoProjection(
111 )
112 turnItemById.set(String(row.sourceItemId), row.item);
113 }
97 - if (page.length === 0) return projection;
98 - const visible = [
114 + if (page.length === 0 && work === undefined) return projection;
115 + let visible = [
116 ...page,
117 ...projection.visibleTurnItems.filter((row) => !pageKeys.has(projectedItemKey(row))),
118 ];
119 + if (work && anchor && deferred) {
120 + let end = start + 1;
121 + while (end < projection.visibleTurnItems.length) {
122 + const row = projection.visibleTurnItems[end]!;
123 + if (
124 + row.sourceThreadId !== anchor.sourceThreadId ||
125 + row.visibility === "synthetic" ||
126 + row.item.ordinal > deferred.lastOrdinal
127 + )
128 + break;
129 + end += 1;
130 + }
131 + const item = { ...anchor.item };
132 + if (work.nextCursor === null) delete item.deferredWork;
133 + else item.deferredWork = { ...deferred, cursor: work.nextCursor };
134 + const updated = { ...anchor, item };
135 + const section = [
136 + ...page,
137 + ...projection.visibleTurnItems
138 + .slice(start, end)
139 + .filter((row) => !pageKeys.has(projectedItemKey(row))),
140 + ];
141 + const ordered = section
142 + .map((row) => (row.sourceItemId === anchor.sourceItemId ? updated : row))
143 + .sort((left, right) => left.item.ordinal - right.item.ordinal);
144 + visible = [
145 + ...projection.visibleTurnItems.slice(0, start),
146 + ...ordered,
147 + ...projection.visibleTurnItems.slice(end),
148 + ];
149 + if (anchor.visibility === "local") turnItemById.set(String(updated.item.id), updated.item);
150 + }
151 if (
152 visible.length === projection.visibleTurnItems.length &&
153 visible.every((row, index) => row === projection.visibleTurnItems[index])
packages/client-runtime/src/state/threads-sync.test.ts
+286 -5
@@ -22,6 +22,7 @@ import * as Option from "effect/Option";
22 import * as Queue from "effect/Queue";
23 import * as Ref from "effect/Ref";
24 import * as Scope from "effect/Scope";
25 +import * as Scheduler from "effect/Scheduler";
26 import * as Schema from "effect/Schema";
27 import * as Stream from "effect/Stream";
28 import * as SubscriptionRef from "effect/SubscriptionRef";
@@ -269,6 +270,8 @@ const makeHarness = Effect.fn("TestEnvironmentThreads.makeHarness")(function* (o
270
271 return {
272 threadState,
273 + loadWork: (anchorId: string) =>
274 + historyController.loadEarlier(TARGET.environmentId, THREAD_ID, undefined, anchorId),
275 loadEarlier: (throughEntryId?: string) =>
276 historyController.loadEarlier(TARGET.environmentId, THREAD_ID, throughEntryId),
277 reconcilePendingWork: historyController.reconcilePendingWork(TARGET.environmentId, THREAD_ID),
@@ -688,7 +691,7 @@ describe("EnvironmentThreads", () => {
691 }),
692 );
693
691 - describe("Stop on cached work the server already finished (#18188)", () => {
694 + describe("cached work (#18188)", () => {
695 const at = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
696 const parentThreadId = ThreadId.make("thread:parent");
697 const command = (
@@ -696,7 +699,7 @@ describe("EnvironmentThreads", () => {
699 status: "running" | "completed",
700 exitCode?: number,
701 ordinal = id === "older-command" ? 1 : 2,
699 - ): OrchestrationV2TurnItem => ({
702 + ): Extract<OrchestrationV2TurnItem, { type: "command_execution" }> => ({
703 id: TurnItemId.make(id),
704 threadId: THREAD_ID,
705 runId: null,
@@ -723,6 +726,261 @@ describe("EnvironmentThreads", () => {
726 sourceItemId: item.id,
727 item,
728 });
729 + const cachedItem = (item: OrchestrationV2TurnItem) => ({
730 + ...BASE_PROJECTION,
731 + turnItems: [item],
732 + visibleTurnItems: [localRow(item, 0)],
733 + });
734 + const historyPage = (
735 + request: Parameters<typeof HttpClientResponse.fromWeb>[0],
736 + items: ReadonlyArray<ReturnType<typeof localRow>>,
737 + nextCursor: string | null,
738 + ) =>
739 + HttpClientResponse.fromWeb(
740 + request,
741 + Response.json({
742 + snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
743 + items,
744 + nextCursor,
745 + hasMoreHistory: nextCursor !== null,
746 + }),
747 + );
748 + it.effect("loads a long unwrapped turn through its user message and resumes failed pages", () =>
749 + Effect.gen(function* () {
750 + const prompt = {
751 + ...command("prompt", "completed", 0, 0),
752 + type: "user_message" as const,
753 + messageId: MessageId.make("prompt"),
754 + inputIntent: "turn_start" as const,
755 + createdBy: "user" as const,
756 + creationSource: "web" as const,
757 + text: "Do the work",
758 + attachments: [],
759 + };
760 + const work = Array.from({ length: 1_000 }, (_, index) =>
761 + command(`work-${index + 1}`, "completed", 0, index + 1),
762 + );
763 + const older = [prompt, ...work.slice(0, -1)];
764 + const tail = work.at(-1)!;
765 + const requests: string[] = [];
766 + let failed = false;
767 + const harness = yield* makeHarness({
768 + cached: cachedItem(tail),
769 + cachedHistory: { historyCursor: "1000", hasMoreHistory: true },
770 + historyHttpClient: HttpClient.make((request, url) => {
771 + const cursor = url.searchParams.get("cursor")!;
772 + requests.push(cursor);
773 + if (cursor === "before-user") return Effect.succeed(historyPage(request, [], cursor));
774 + if (cursor === "925" && !failed) {
775 + failed = true;
776 + return Effect.succeed(
777 + HttpClientResponse.fromWeb(request, new Response(null, { status: 503 })),
778 + );
779 + }
780 + const end = Number(cursor);
781 + expect(Number.isFinite(end)).toBe(true);
782 + const start = Math.max(0, end - 75);
783 + return Effect.succeed(
784 + historyPage(
785 + request,
786 + older.slice(start, end).map(localRow),
787 + start === 0 ? "before-user" : String(start),
788 + ),
789 + );
790 + }),
791 + });
792 + expect((yield* harness.loadEarlier())._tag).toBe("error");
793 + expect(requests).toEqual(["1000", "925"]);
794 + expect(yield* harness.loadEarlier()).toEqual({ _tag: "loaded" });
795 + const state = yield* SubscriptionRef.get(harness.threadState);
796 + expect(
797 + Option.getOrThrow(state.data).visibleTurnItems.map((row) => row.sourceItemId),
798 + ).toEqual([prompt, ...work].map((item) => item.id));
799 + expect(state.history).toMatchObject({
800 + historyCursor: "before-user",
801 + hasMoreHistory: true,
802 + loading: false,
803 + });
804 + expect(requests.slice(1, 3)).toEqual(["925", "925"]);
805 + expect(yield* harness.loadEarlier()).toEqual({
806 + _tag: "error",
807 + message: "History did not advance.",
808 + });
809 + expect((yield* SubscriptionRef.get(harness.threadState)).history.loading).toBe(false);
810 + }),
811 + );
812 + it.effect("loads deferred work on demand and resumes a failed page", () =>
813 + Effect.gen(function* () {
814 + const anchor = {
815 + ...command("anchor", "completed", 0, 1),
816 + deferredWork: { cursor: "first", itemCount: 3, lastOrdinal: 3 },
817 + };
818 + const requests: string[] = [];
819 + let failed = false;
820 + const harness = yield* makeHarness({
821 + cached: cachedItem(anchor),
822 + cachedHistory: { historyCursor: "older", hasMoreHistory: true },
823 + historyHttpClient: HttpClient.make((request, url) => {
824 + expect(url.searchParams.get("view")).toBe("work");
825 + const cursor = url.searchParams.get("cursor")!;
826 + requests.push(cursor);
827 + if (cursor === "second" && !failed) {
828 + failed = true;
829 + return Effect.succeed(
830 + HttpClientResponse.fromWeb(request, new Response("Unavailable", { status: 503 })),
831 + );
832 + }
833 + return Effect.succeed(
834 + historyPage(
835 + request,
836 + [
837 + localRow(
838 + command(`work-${cursor}`, "completed", 0, cursor === "first" ? 3 : 2),
839 + 0,
840 + ),
841 + ],
842 + cursor === "first" ? "second" : null,
843 + ),
844 + );
845 + }),
846 + });
847 + expect(requests).toEqual([]);
848 + expect((yield* harness.loadWork(anchor.id))._tag).toBe("error");
849 + const partial = Option.getOrThrow((yield* SubscriptionRef.get(harness.threadState)).data);
850 + expect(partial.visibleTurnItems[0]?.item.deferredWork?.cursor).toBe("second");
851 + expect(yield* harness.loadWork(anchor.id)).toEqual({ _tag: "loaded" });
852 + const state = yield* SubscriptionRef.get(harness.threadState);
853 + expect(
854 + Option.getOrThrow(state.data).visibleTurnItems.map((row) => row.item.ordinal),
855 + ).toEqual([1, 2, 3]);
856 + expect(state.history.historyCursor).toBe("older");
857 + expect(requests).toEqual(["first", "second", "second"]);
858 + }),
859 + );
860 +
861 + it.effect.each(["deferred", "complete"] as const)(
862 + "settles a replaced work request when the new snapshot is %s",
863 + (mode) =>
864 + Effect.gen(function* () {
865 + const anchor = {
866 + ...command("anchor", "completed"),
867 + deferredWork: { cursor: "before", itemCount: 80, lastOrdinal: 80 },
868 + };
869 + const asked = yield* Deferred.make<void>();
870 + const release = yield* Deferred.make<void>();
871 + const harness = yield* makeHarness({
872 + cached: cachedItem(anchor),
873 + historyHttpClient: HttpClient.make((request) =>
874 + Effect.gen(function* () {
875 + yield* Deferred.succeed(asked, undefined);
876 + yield* Deferred.await(release);
877 + return historyPage(request, [], null);
878 + }),
879 + ),
880 + });
881 + const loading = yield* harness.loadWork(anchor.id).pipe(Effect.forkChild);
882 + yield* Deferred.await(asked);
883 + const replacement =
884 + mode === "deferred"
885 + ? { ...anchor, deferredWork: { ...anchor.deferredWork, cursor: "after" } }
886 + : command("anchor", "completed");
887 + yield* Queue.offer(
888 + harness.inputs,
889 + snapshot(
890 + {
891 + ...BASE_PROJECTION,
892 + thread: { ...BASE_PROJECTION.thread, title: "Replacement" },
893 + turnItems: [replacement],
894 + visibleTurnItems: [localRow(replacement, 0)],
895 + },
896 + CACHED_SNAPSHOT_SEQUENCE + 1,
897 + ),
898 + );
899 + yield* awaitThreadState(
900 + harness.observed,
901 + (value) => Option.getOrNull(value.data)?.thread.title === "Replacement",
902 + );
903 + yield* Deferred.succeed(release, undefined);
904 + expect((yield* Fiber.join(loading))._tag).toBe(mode === "deferred" ? "error" : "noop");
905 + }),
906 + );
907 +
908 + it.effect("does not mark replacement history loading when a queued request becomes stale", () =>
909 + Effect.gen(function* () {
910 + const harness = yield* makeHarness({
911 + cached: BASE_PROJECTION,
912 + cachedHistory: { historyCursor: "older", hasMoreHistory: true },
913 + historyHttpClient: HttpClient.make((request) =>
914 + Effect.succeed(historyPage(request, [], null)),
915 + ),
916 + });
917 + const held = yield* Deferred.make<void>();
918 + const release = yield* Deferred.make<void>();
919 + const replacement = yield* SubscriptionRef.updateEffect(harness.threadState, (latest) =>
920 + Deferred.succeed(held, undefined).pipe(
921 + Effect.andThen(Deferred.await(release)),
922 + Effect.as({ ...latest, history: { ...latest.history, historyCursor: "replacement" } }),
923 + ),
924 + ).pipe(Effect.forkChild({ startImmediately: true }));
925 + yield* Deferred.await(held);
926 + const dispatcher = new Scheduler.MixedScheduler("sync").makeDispatcher();
927 + const loading = yield* harness.loadEarlier().pipe(
928 + Effect.forkChild({ startImmediately: true }),
929 + Effect.provideService(Scheduler.Scheduler, {
930 + executionMode: "sync",
931 + shouldYield: () => false,
932 + makeDispatcher: () => dispatcher,
933 + }),
934 + );
935 + dispatcher.flush();
936 + yield* Deferred.succeed(release, undefined);
937 + yield* Fiber.join(replacement);
938 + dispatcher.flush();
939 + expect(yield* Fiber.join(loading)).toEqual({ _tag: "noop" });
940 + expect((yield* SubscriptionRef.get(harness.threadState)).history).toMatchObject({
941 + historyCursor: "replacement",
942 + loading: false,
943 + });
944 + }),
945 + );
946 +
947 + it.effect("loads targeted messages without fetching unrelated deferred work", () =>
948 + Effect.gen(function* () {
949 + const anchor = {
950 + ...command("anchor", "completed"),
951 + deferredWork: { cursor: "work", itemCount: 80, lastOrdinal: 80 },
952 + };
953 + const message = {
954 + ...command("older-message", "completed"),
955 + type: "assistant_message" as const,
956 + messageId: MessageId.make("older-message"),
957 + text: "Older response",
958 + streaming: false,
959 + };
960 + const views: Array<string | null> = [];
961 + const harness = yield* makeHarness({
962 + cached: cachedItem(anchor),
963 + cachedHistory: { historyCursor: "older", hasMoreHistory: true },
964 + historyHttpClient: HttpClient.make((request, url) => {
965 + views.push(url.searchParams.get("view"));
966 + return Effect.succeed(
967 + url.searchParams.get("view") === "work"
968 + ? HttpClientResponse.fromWeb(request, new Response("Unavailable", { status: 503 }))
969 + : historyPage(request, [localRow(message, 0)], null),
970 + );
971 + }),
972 + });
973 + expect(yield* harness.loadEarlier(message.messageId)).toEqual({ _tag: "loaded" });
974 + const state = yield* SubscriptionRef.get(harness.threadState);
975 + expect(
976 + Option.getOrThrow(state.data).visibleTurnItems.some(
977 + (row) => row.sourceItemId === message.id,
978 + ),
979 + ).toBe(true);
980 + expect(views).toEqual(["conversation", null]);
981 + }),
982 + );
983 +
984 // A fork's parent row keeps its own source; the child's Stop must not touch it.
985 const inheritedRow = {
986 position: 0,
@@ -1248,6 +1506,17 @@ describe("EnvironmentThreads", () => {
1506 text: "COD4 result",
1507 streaming: false,
1508 };
1509 + const prompt = {
1510 + ...message,
1511 + id: "item:older-prompt",
1512 + messageId: "message:older-prompt",
1513 + ordinal: 0,
1514 + type: "user_message",
1515 + inputIntent: "turn_start",
1516 + createdBy: "user",
1517 + creationSource: "web",
1518 + attachments: [],
1519 + };
1520 const activity = {
1521 ...common,
1522 id: "item:older-command",
@@ -1270,6 +1539,16 @@ describe("EnvironmentThreads", () => {
1539 const harness = yield* makeHarness({
1540 historyHttpClient: HttpClient.make((request, url) =>
1541 Effect.gen(function* () {
1542 + if (url.searchParams.get("cursor") === "next-cursor")
1543 + return HttpClientResponse.fromWeb(
1544 + request,
1545 + Response.json({
1546 + snapshotSequence: 14,
1547 + items: [projected(prompt, 0)],
1548 + nextCursor: "before-prompt",
1549 + hasMoreHistory: true,
1550 + }),
1551 + );
1552 expect(url.searchParams.get("cursor")).toBe("progressive-cursor");
1553 const conversationOnly = url.searchParams.get("view") === "conversation";
1554 if (!conversationOnly && activityRequests++ === 0) {
@@ -1341,14 +1620,16 @@ describe("EnvironmentThreads", () => {
1620 const complete = yield* SubscriptionRef.get(harness.threadState);
1621 expect(complete.history).toMatchObject({
1622 loading: false,
1344 - historyCursor: "next-cursor",
1623 + historyCursor: mode === "retry" ? "before-prompt" : "next-cursor",
1624 error: null,
1625 latestLocalTurnOrdinal: 47,
1626 });
1627 expect(
1628 Option.getOrThrow(complete.data).visibleTurnItems.map((row) => row.sourceItemId),
1350 - ).toEqual([message.id, activity.id]);
1351 - expect(Option.getOrThrow(complete.data).turnItems).toHaveLength(2);
1629 + ).toEqual(
1630 + mode === "retry" ? [prompt.id, message.id, activity.id] : [message.id, activity.id],
1631 + );
1632 + expect(Option.getOrThrow(complete.data).turnItems).toHaveLength(mode === "retry" ? 3 : 2);
1633 }),
1634 );
1635
packages/client-runtime/src/state/threads.ts
+127 -76
@@ -658,29 +658,40 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
658
659 const loadEarlier = Effect.fn("EnvironmentThreadState.loadEarlier")(function* (
660 throughEntryId?: string,
661 - ) {
661 + workAnchorId?: string,
662 + ): Effect.fn.Return<ThreadHistoryController.ThreadHistoryLoadEarlierResult> {
663 + const workCursor = (latest: EnvironmentThreadState) =>
664 + Option.getOrNull(latest.data)?.visibleTurnItems.find(
665 + (row) => row.sourceItemId === workAnchorId,
666 + )?.item.deferredWork?.cursor ?? null;
667 const current = yield* SubscriptionRef.get(state);
668 + const initialCursor =
669 + workAnchorId === undefined ? current.history.historyCursor : workCursor(current);
670 if (
671 current.status === "deleted" ||
672 Option.isNone(current.data) ||
666 - !current.history.hasMoreHistory ||
667 - current.history.historyCursor === null
668 - ) {
669 - return { _tag: "noop" } satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult;
670 - }
671 - if (current.history.loading) {
672 - return { _tag: "busy" } satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult;
673 - }
674 -
675 - // Capture the cursor that initiated this request. Completions/failures must
676 - // no-op if a socket or new bounded snapshot replaced progressive meta mid-flight.
677 - const requestCursor = current.history.historyCursor;
678 - yield* patchHistoryMeta((history) =>
679 - isActiveHistoryRequestCursor(requestCursor, history)
680 - ? { ...history, loading: true, error: null }
681 - : history,
682 - );
683 -
673 + initialCursor === null ||
674 + (workAnchorId === undefined && !current.history.hasMoreHistory)
675 + )
676 + return { _tag: "noop" };
677 + if (workAnchorId === undefined && current.history.loading) return { _tag: "busy" };
678 + let requestCursor = initialCursor;
679 + const isCurrent = (latest: EnvironmentThreadState) =>
680 + workAnchorId === undefined
681 + ? isActiveHistoryRequestCursor(requestCursor, latest.history)
682 + : workCursor(latest) === requestCursor;
683 + const staleResult = (
684 + latest: EnvironmentThreadState,
685 + ): ThreadHistoryController.ThreadHistoryLoadEarlierResult =>
686 + workAnchorId !== undefined && workCursor(latest) !== null
687 + ? { _tag: "error", message: "Work history changed. Retry loading it." }
688 + : { _tag: "noop" };
689 + if (workAnchorId === undefined)
690 + yield* patchHistoryMeta((history) =>
691 + isActiveHistoryRequestCursor(requestCursor, history)
692 + ? { ...history, loading: true, error: null }
693 + : history,
694 + );
695 const failLoad = (message: string) =>
696 SubscriptionRef.modify(
697 state,
@@ -690,72 +701,108 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
701 ThreadHistoryController.ThreadHistoryLoadEarlierResult,
702 EnvironmentThreadState,
703 ] =>
693 - isActiveHistoryRequestCursor(requestCursor, latest.history)
704 + isCurrent(latest)
705 ? [
706 { _tag: "error", message },
707 {
708 ...latest,
698 - history: { ...latest.history, loading: false, error: message },
709 + history:
710 + workAnchorId === undefined
711 + ? { ...latest.history, loading: false, error: message }
712 + : latest.history,
713 },
714 ]
701 - : [{ _tag: "noop" }, latest],
715 + : [staleResult(latest), latest],
716 );
717
718 const runLoad = Effect.gen(function* () {
705 - const preparedOption = yield* SubscriptionRef.get(supervisor.prepared);
706 - if (Option.isNone(preparedOption) || Option.isNone(httpClient))
707 - return yield* failLoad("Environment is not connected.");
708 -
709 - const views =
710 - throughEntryId === undefined ? [undefined] : ["conversation" as const, undefined];
711 - for (const view of views) {
712 - const pageResult = yield* fetchEnvironmentThreadHistoryPage({
713 - prepared: preparedOption.value,
714 - threadId,
715 - cursor: requestCursor,
716 - throughEntryId,
717 - view,
718 - signer: dpopSigner,
719 - remoteAuthorization,
720 - }).pipe(Effect.provideService(HttpClient.HttpClient, httpClient.value), Effect.result);
721 - if (Result.isFailure(pageResult))
722 - return yield* failLoad(formatHistoryError(pageResult.failure));
723 - const page = pageResult.success;
724 - const complete = view === undefined;
725 - const waiting = yield* Ref.get(awaitingCompletion);
726 - const result = yield* applyLock.withPermits(1)(
727 - SubscriptionRef.modify(
728 - state,
729 - (
730 - latest,
731 - ): readonly [
732 - ThreadHistoryController.ThreadHistoryLoadEarlierResult,
733 - EnvironmentThreadState,
734 - ] => {
735 - if (!isActiveHistoryRequestCursor(requestCursor, latest.history))
736 - return [{ _tag: "noop" }, latest];
737 - if (Option.isNone(latest.data) || latest.status === "deleted")
738 - return [{ _tag: "noop" }, { ...latest, history: EMPTY_THREAD_HISTORY_META }];
739 - return [
740 - { _tag: "loaded" },
741 - {
742 - ...latest,
743 - data: Option.some(mergeOlderHistoryIntoProjection(latest.data.value, page.items)),
744 - status: complete && waiting ? "synchronizing" : latest.status,
745 - history: complete
746 - ? applyHistoryPageMeta(latest.history, page)
747 - : { ...latest.history, expanded: true },
748 - },
749 - ];
750 - },
751 - ),
752 - );
753 - if (result._tag !== "loaded") return result;
754 - if (complete) yield* remember;
719 + while (true) {
720 + const preparedOption = yield* SubscriptionRef.get(supervisor.prepared);
721 + if (Option.isNone(preparedOption) || Option.isNone(httpClient))
722 + return yield* failLoad("Environment is not connected.");
723 + const views =
724 + workAnchorId !== undefined
725 + ? ["work" as const]
726 + : throughEntryId === undefined
727 + ? [undefined]
728 + : ["conversation" as const, undefined];
729 + let nextCursor: string | null = null;
730 + for (const view of views) {
731 + const pageResult = yield* fetchEnvironmentThreadHistoryPage({
732 + prepared: preparedOption.value,
733 + threadId,
734 + cursor: requestCursor,
735 + throughEntryId: workAnchorId ?? throughEntryId,
736 + view,
737 + signer: dpopSigner,
738 + remoteAuthorization,
739 + }).pipe(Effect.provideService(HttpClient.HttpClient, httpClient.value), Effect.result);
740 + if (Result.isFailure(pageResult))
741 + return yield* failLoad(formatHistoryError(pageResult.failure));
742 + const page = pageResult.success;
743 + const complete = view !== "conversation";
744 + const keepPaging =
745 + complete &&
746 + (workAnchorId !== undefined
747 + ? page.nextCursor !== null
748 + : throughEntryId === undefined &&
749 + page.hasMoreHistory &&
750 + !page.items.some(
751 + ({ item }) => item.type === "user_message" && item.createdBy === "user",
752 + ));
753 + if (keepPaging && (page.nextCursor === null || page.nextCursor === requestCursor))
754 + return yield* failLoad(
755 + workAnchorId === undefined
756 + ? "History did not advance."
757 + : "Work history did not advance.",
758 + );
759 + const waiting = yield* Ref.get(awaitingCompletion);
760 + const result = yield* applyLock.withPermits(1)(
761 + SubscriptionRef.modify(
762 + state,
763 + (
764 + latest,
765 + ): readonly [
766 + ThreadHistoryController.ThreadHistoryLoadEarlierResult,
767 + EnvironmentThreadState,
768 + ] => {
769 + if (!isCurrent(latest)) return [staleResult(latest), latest];
770 + if (Option.isNone(latest.data) || latest.status === "deleted")
771 + return [{ _tag: "noop" }, { ...latest, history: EMPTY_THREAD_HISTORY_META }];
772 + return [
773 + { _tag: "loaded" },
774 + {
775 + ...latest,
776 + data: Option.some(
777 + mergeOlderHistoryIntoProjection(
778 + latest.data.value,
779 + page.items,
780 + workAnchorId === undefined
781 + ? undefined
782 + : { anchorId: workAnchorId, nextCursor: page.nextCursor },
783 + ),
784 + ),
785 + status:
786 + workAnchorId === undefined && complete && waiting
787 + ? "synchronizing"
788 + : latest.status,
789 + history:
790 + workAnchorId === undefined && complete
791 + ? { ...applyHistoryPageMeta(latest.history, page), loading: keepPaging }
792 + : { ...latest.history, expanded: true },
793 + },
794 + ];
795 + },
796 + ),
797 + );
798 + if (result._tag !== "loaded") return result;
799 + if (complete) yield* remember;
800 + nextCursor = keepPaging ? page.nextCursor : null;
801 + }
802 + if (nextCursor === null) return { _tag: "loaded" } as const;
803 + requestCursor = nextCursor;
804 }
756 - return { _tag: "loaded" } satisfies ThreadHistoryController.ThreadHistoryLoadEarlierResult;
805 });
758 -
806 return yield* runLoad.pipe(
807 Effect.onInterrupt(() =>
808 SubscriptionRef.update(state, (latest) => ({
@@ -832,8 +879,11 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
879 if (Option.isSome(historyController)) {
880 const scope = yield* Scope.Scope;
881 const registration = yield* historyController.value.register(environmentId, threadId, {
835 - loadEarlier: (throughEntryId) =>
836 - loadEarlier(throughEntryId).pipe(Effect.forkIn(scope), Effect.flatMap(Fiber.join)),
882 + loadEarlier: (throughEntryId, workAnchorId) =>
883 + loadEarlier(throughEntryId, workAnchorId).pipe(
884 + Effect.forkIn(scope),
885 + Effect.flatMap(Fiber.join),
886 + ),
887 reconcilePendingWork: () =>
888 reconcilePendingWork().pipe(
889 Effect.provideService(EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
@@ -962,6 +1012,7 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
1012 ...(acceptBoundedSnapshot
1013 ? {
1014 acceptBoundedSnapshot: true as const,
1015 + acceptDeferredWork: true as const,
1016 acceptCompactTurnItems: true as const,
1017 acceptCompactIds: true as const,
1018 }
packages/contracts/src/environmentHttp.ts
+3 -1
@@ -657,6 +657,7 @@ const EnvironmentOrchestrationShellSnapshotQuery = Schema.Struct({
657 });
658
659 const EnvironmentOrchestrationThreadBoundedSnapshotQuery = Schema.Struct({
660 + deferWork: Schema.optionalKey(Schema.String),
661 compactTurnItems: Schema.optionalKey(Schema.String),
662 dedupeBodies: Schema.optionalKey(Schema.String),
663 compactIds: Schema.optionalKey(Schema.String),
@@ -665,7 +666,8 @@ const EnvironmentOrchestrationThreadBoundedSnapshotQuery = Schema.Struct({
666 const EnvironmentOrchestrationThreadHistoryQuery = Schema.Struct({
667 cursor: TrimmedNonEmptyString,
668 throughEntryId: Schema.optional(TrimmedNonEmptyString),
668 - view: Schema.optional(Schema.Literals(["conversation", "activity"])),
669 + view: Schema.optional(Schema.Literals(["conversation", "activity", "work"])),
670 + deferWork: Schema.optionalKey(Schema.String),
671 compactIds: Schema.optionalKey(Schema.String),
672 });
673
packages/contracts/src/orchestrationV2.ts
+9
@@ -1447,6 +1447,14 @@ export type OrchestrationV2UserMessageInputIntent =
1447 typeof OrchestrationV2UserMessageInputIntent.Type;
1448
1449 const OrchestrationV2TurnItemBaseFields = {
1450 + /** Wire-only cursor for collapsed work; persisted items keep their complete history. */
1451 + deferredWork: Schema.optionalKey(
1452 + Schema.Struct({
1453 + cursor: TrimmedNonEmptyString,
1454 + itemCount: PositiveInt,
1455 + lastOrdinal: NonNegativeInt,
1456 + }),
1457 + ),
1458 toolNonExecutionKind: Schema.optional(Schema.String),
1459 toolSurface: Schema.optional(ToolActivitySurface),
1460 toolIcon: Schema.optional(ToolActivityIcon),
@@ -3680,6 +3688,7 @@ export const OrchestrationV2SubscribeThreadInput = Schema.Struct({
3688 afterSequence: Schema.optionalKey(NonNegativeInt),
3689 requestCompletionMarker: Schema.optionalKey(Schema.Boolean),
3690 acceptBoundedSnapshot: Schema.optionalKey(Schema.Boolean),
3691 + acceptDeferredWork: Schema.optionalKey(Schema.Boolean),
3692 acceptCompactTurnItems: Schema.optionalKey(Schema.Boolean),
3693 /** The client restores `*Omitted` bodies from records it already holds. */
3694 acceptDedupedBodies: Schema.optionalKey(Schema.Boolean),