The best way to control Claude Code, Codex and more from any computer or phone
fix: keep older pending questions answerable (#26435)
Co-authored-by: PR Batch Tester <agent@local.test> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
shivam committed
Nov 5, 2026 at 07:01 UTC
c15e49a77cbbc071f68f18cbf65e1b1e3481d036
8 files changed
+522
-24
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+220
@@ -2012,6 +2012,226 @@ it.effect("SQL list older settled threads below a cursor", () =>
2012
}).pipe(Effect.provide(layerTest)),
2013
);
2014
2015
+// regression: #25806 pending request cards outlive the history window that drops their turn
2016
+it.effect("SQL snapshot windows keep cards for requests still pending", () =>
2017
+ Effect.gen(function* () {
2018
+ const store = yield* ProjectionStore.ProjectionStoreV2;
2019
+ const now = yield* DateTime.now;
2020
+ const threadId = ThreadId.make("thread:pending-request-window");
2021
+ yield* store.apply({
2022
+ id: EventId.make("event:pending-request-window:thread"),
2023
+ type: "thread.created",
2024
+ threadId,
2025
+ occurredAt: now,
2026
+ payload: {
2027
+ createdBy: "user",
2028
+ creationSource: "web",
2029
+ id: threadId,
2030
+ projectId: ProjectId.make("project:pending-request-window"),
2031
+ title: "Pending request window",
2032
+ providerInstanceId,
2033
+ modelSelection,
2034
+ runtimeMode: "full-access",
2035
+ interactionMode: "default",
2036
+ branch: null,
2037
+ worktreePath: null,
2038
+ activeProviderThreadId: null,
2039
+ lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2040
+ forkedFrom: null,
2041
+ createdAt: now,
2042
+ updatedAt: now,
2043
+ archivedAt: null,
2044
+ settledOverride: null,
2045
+ settledAt: null,
2046
+ lastVisitedAt: null,
2047
+ deletedAt: null,
2048
+ },
2049
+ });
2050
+ let ordinal = 0;
2051
+ const itemBase = (runId: RunId, nodeId: NodeId, id: string) => ({
2052
+ id: TurnItemId.make(`item:pending-request-window:${id}`),
2053
+ threadId,
2054
+ runId,
2055
+ nodeId,
2056
+ providerThreadId: null,
2057
+ providerTurnId: null,
2058
+ nativeItemRef: null,
2059
+ parentItemId: null,
2060
+ ordinal: ++ordinal,
2061
+ status: "completed" as const,
2062
+ title: null,
2063
+ startedAt: now,
2064
+ completedAt: now,
2065
+ updatedAt: now,
2066
+ });
2067
+ const request = (id: string, nodeId: NodeId, kind: "user_input" | "command") => ({
2068
+ id: RuntimeRequestId.make(`request:pending-request-window:${id}`),
2069
+ nodeId,
2070
+ providerTurnId: null,
2071
+ nativeRequestRef: null,
2072
+ kind,
2073
+ responseCapability: {
2074
+ type: "live" as const,
2075
+ providerSessionId: ProviderSessionId.make("session:pending-request-window"),
2076
+ },
2077
+ createdAt: now,
2078
+ });
2079
+ const turnCount = THREAD_HISTORY_PAGE_POLICY.maxUserTurns + 4;
2080
+ for (let turn = 1; turn <= turnCount; turn += 1) {
2081
+ const runId = RunId.make(`run:pending-request-window:${turn}`);
2082
+ const nodeId = NodeId.make(`node:pending-request-window:${turn}`);
2083
+ yield* store.apply({
2084
+ id: EventId.make(`event:pending-request-window:run:${turn}`),
2085
+ type: "run.created",
2086
+ threadId,
2087
+ runId,
2088
+ nodeId,
2089
+ driver,
2090
+ occurredAt: now,
2091
+ payload: {
2092
+ id: runId,
2093
+ threadId,
2094
+ ordinal: turn,
2095
+ providerInstanceId,
2096
+ modelSelection,
2097
+ providerThreadId: null,
2098
+ userMessageId: MessageId.make(`message:pending-request-window:${turn}`),
2099
+ rootNodeId: nodeId,
2100
+ activeAttemptId: null,
2101
+ status: "completed",
2102
+ requestedAt: now,
2103
+ startedAt: now,
2104
+ completedAt: now,
2105
+ checkpointId: null,
2106
+ contextHandoffId: null,
2107
+ },
2108
+ });
2109
+ const items: Array<OrchestrationV2TurnItem> = [
2110
+ {
2111
+ ...itemBase(runId, nodeId, `message:${turn}`),
2112
+ type: "user_message",
2113
+ createdBy: "user",
2114
+ creationSource: "web",
2115
+ messageId: MessageId.make(`message:pending-request-window:${turn}`),
2116
+ inputIntent: "turn_start",
2117
+ text: `Turn ${turn}`,
2118
+ attachments: [],
2119
+ },
2120
+ ];
2121
+ if (turn === 1) {
2122
+ const question = {
2123
+ id: "scope",
2124
+ header: "Scope",
2125
+ question: "Which scope?",
2126
+ options: [{ label: "All", description: "Everything" }],
2127
+ };
2128
+ items.push(
2129
+ {
2130
+ ...itemBase(runId, nodeId, "question:pending"),
2131
+ status: "running",
2132
+ type: "user_input_request",
2133
+ requestId: request("question:pending", nodeId, "user_input").id,
2134
+ questions: [question],
2135
+ },
2136
+ {
2137
+ ...itemBase(runId, nodeId, "approval:pending"),
2138
+ status: "running",
2139
+ type: "approval_request",
2140
+ requestId: request("approval:pending", nodeId, "command").id,
2141
+ requestKind: "command",
2142
+ prompt: "rm -rf build",
2143
+ },
2144
+ {
2145
+ ...itemBase(runId, nodeId, "question:resolved"),
2146
+ type: "user_input_request",
2147
+ requestId: request("question:resolved", nodeId, "user_input").id,
2148
+ questions: [question],
2149
+ },
2150
+ );
2151
+ for (const [id, kind, status] of [
2152
+ ["question:pending", "user_input", "pending"],
2153
+ ["approval:pending", "command", "pending"],
2154
+ ["question:resolved", "user_input", "resolved"],
2155
+ ] as const) {
2156
+ yield* store.apply({
2157
+ id: EventId.make(`event:pending-request-window:request:${id}`),
2158
+ type: "runtime-request.updated",
2159
+ threadId,
2160
+ nodeId,
2161
+ driver,
2162
+ occurredAt: now,
2163
+ payload: {
2164
+ ...request(id, nodeId, kind),
2165
+ status,
2166
+ resolvedAt: status === "pending" ? null : now,
2167
+ },
2168
+ });
2169
+ }
2170
+ }
2171
+ for (const item of items) {
2172
+ yield* store.apply({
2173
+ id: EventId.make(`event:pending-request-window:${item.id}`),
2174
+ type: "turn-item.updated",
2175
+ threadId,
2176
+ runId,
2177
+ nodeId,
2178
+ driver,
2179
+ occurredAt: now,
2180
+ payload: item,
2181
+ });
2182
+ }
2183
+ }
2184
+
2185
+ const pendingCardIds = [
2186
+ TurnItemId.make("item:pending-request-window:question:pending"),
2187
+ TurnItemId.make("item:pending-request-window:approval:pending"),
2188
+ ];
2189
+ const { statements, tracer } = traceSqlStatements();
2190
+ const snapshot = yield* store
2191
+ .getThreadSnapshotWindow(threadId, {
2192
+ rowLimit: THREAD_HISTORY_PAGE_POLICY.maxItems + 2,
2193
+ userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
2194
+ })
2195
+ .pipe(Effect.withTracer(tracer));
2196
+ const sql = yield* SqlClient.SqlClient;
2197
+ const query = statements.find(
2198
+ (statement) =>
2199
+ statement.includes("FROM orchestration_v2_projection_runtime_requests AS request") &&
2200
+ statement.includes("SELECT item.payload_json"),
2201
+ );
2202
+ assert.isDefined(query);
2203
+ const plan = (yield* sql.unsafe<{ readonly detail: string }>(
2204
+ `EXPLAIN QUERY PLAN ${query}`,
2205
+ )).map((row) => row.detail);
2206
+ assert.include(
2207
+ plan[0],
2208
+ "SEARCH request USING INDEX orchestration_v2_projection_runtime_requests_thread_status_idx",
2209
+ );
2210
+ assert.include(
2211
+ plan[1],
2212
+ "SEARCH item USING INDEX orchestration_v2_projection_turn_items_node_ordinal_idx",
2213
+ );
2214
+ const bounded = buildBoundedThreadProjection({
2215
+ projection: snapshot.projection,
2216
+ snapshotSequence: snapshot.snapshotSequence,
2217
+ });
2218
+ assert.isTrue(bounded.hasMoreHistory);
2219
+ assert.notInclude(
2220
+ bounded.projection.turnItems.map((item) => item.id),
2221
+ TurnItemId.make("item:pending-request-window:question:resolved"),
2222
+ );
2223
+ for (const projection of [snapshot.projection, bounded.projection]) {
2224
+ const visibleIds = projection.visibleTurnItems.map((row) => row.sourceItemId);
2225
+ assert.notInclude(visibleIds, TurnItemId.make("item:pending-request-window:message:1"));
2226
+ assert.includeMembers(
2227
+ projection.turnItems.map((item) => item.id),
2228
+ pendingCardIds,
2229
+ );
2230
+ for (const id of pendingCardIds) assert.notInclude(visibleIds, id);
2231
+ }
2232
+ }).pipe(Effect.provide(layerTest)),
2233
+);
2234
+
2235
it.effect("lists only active subagent IDs without loading shell data", () =>
2236
Effect.gen(function* () {
2237
const store = yield* ProjectionStore.ProjectionStoreV2;
apps/server/src/orchestration-v2/ProjectionStore.ts
+28
-16
@@ -3075,21 +3075,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
3075
)
3076
AND request.type = 'run_interrupt_request'
3077
UNION
3078
- -- An open question or approval stays in the window: clients
3079
- -- build the answer panel from its row, and a run that kept
3080
- -- working pushes the row out of the newest turns.
3081
- SELECT question.rowid AS item_rowid, question.ordinal, question.turn_item_id
3082
- FROM orchestration_v2_projection_runtime_requests AS pending
3083
- JOIN orchestration_v2_projection_turn_items AS question
3084
- ON question.node_id = pending.node_id
3085
- WHERE pending.thread_id = ${threadId}
3086
- AND pending.status = 'pending'
3087
- AND question.thread_id = ${threadId}
3088
- AND question.type IN ('user_input_request', 'approval_request')
3089
- AND ${window.rowLimit} > 0
3090
- AND ${window.anchorItemId ?? null} IS NULL
3091
- AND ${window.requiredRunId ?? null} IS NULL
3092
- UNION
3078
SELECT latest.item_rowid, latest.ordinal, latest.turn_item_id
3079
FROM (
3080
SELECT rowid AS item_rowid, ordinal, turn_item_id
@@ -6051,6 +6036,33 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6036
: {}),
6037
...(historyAnchor === undefined ? {} : { historyAnchor }),
6038
});
6039
+ // Pending requests outlive their turn's window; clients read their prompt from the card.
6040
+ const retainedIds = new Set(projection.turnItems.map((item) => item.id));
6041
+ const pendingCards = (yield* decodeRows(
6042
+ decodeTurnItemPayload,
6043
+ threadId,
6044
+ )(
6045
+ yield* sql<PayloadRow>`
6046
+ SELECT item.payload_json
6047
+ FROM orchestration_v2_projection_runtime_requests AS request
6048
+ CROSS JOIN orchestration_v2_projection_turn_items AS item
6049
+ ON item.node_id = request.node_id
6050
+ AND item.type IN ('approval_request', 'user_input_request')
6051
+ AND json_extract(item.payload_json, '$.requestId') = request.runtime_request_id
6052
+ WHERE request.thread_id = ${threadId} AND request.status = 'pending'
6053
+ AND item.thread_id = ${threadId}
6054
+ `,
6055
+ )).filter((item) => !retainedIds.has(item.id));
6056
+ const windowProjection =
6057
+ pendingCards.length === 0
6058
+ ? projection
6059
+ : {
6060
+ ...projection,
6061
+ turnItems: [...projection.turnItems, ...pendingCards].toSorted(
6062
+ (left, right) =>
6063
+ left.ordinal - right.ordinal || left.id.localeCompare(right.id),
6064
+ ),
6065
+ };
6066
const rows = yield* sql<{ readonly snapshot_sequence: number | null }>`
6067
SELECT MAX(sequence) AS snapshot_sequence
6068
FROM orchestration_events
@@ -6061,7 +6073,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6073
return {
6074
schemaVersion: ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION,
6075
snapshotSequence: rows[0]?.snapshot_sequence ?? 0,
6064
- projection,
6076
+ projection: windowProjection,
6077
};
6078
}),
6079
)
apps/server/src/orchestration-v2/threadHistoryPaging.test.ts
+64
@@ -2,6 +2,7 @@ import {
2
MessageId,
3
NodeId,
4
RunId,
5
+ RuntimeRequestId,
6
ThreadId,
7
TurnItemId,
8
type OrchestrationV2ProjectedTurnItem,
@@ -817,6 +818,69 @@ describe("threadHistoryPaging", () => {
818
).toBe(2_097_152);
819
});
820
821
+ it.each([40_000, 80_000])(
822
+ "reserves pending question cards before visible history: %s bytes",
823
+ (questionBytes) => {
824
+ const rows = Array.from({ length: 20 }, (_, index) =>
825
+ makeRow(index + 1, { outputBytes: 1_500 }),
826
+ );
827
+ const requestId = RuntimeRequestId.make("pending-question");
828
+ const card: OrchestrationV2TurnItem = {
829
+ id: TurnItemId.make("pending-question-card"),
830
+ type: "user_input_request",
831
+ threadId: THREAD,
832
+ runId: null,
833
+ nodeId: NODE,
834
+ providerThreadId: null,
835
+ providerTurnId: null,
836
+ nativeItemRef: null,
837
+ parentItemId: null,
838
+ ordinal: 1,
839
+ status: "completed",
840
+ title: null,
841
+ requestId,
842
+ responseMode: "message",
843
+ questions: [
844
+ { id: "scope", header: "Scope", question: "q".repeat(questionBytes), options: [] },
845
+ ],
846
+ startedAt: NOW,
847
+ completedAt: NOW,
848
+ updatedAt: NOW,
849
+ };
850
+ const projection = {
851
+ ...makeProjection(rows),
852
+ turnItems: [card, ...rows.map((row) => row.item)],
853
+ runtimeRequests: [
854
+ {
855
+ id: requestId,
856
+ nodeId: NODE,
857
+ providerTurnId: null,
858
+ nativeRequestRef: null,
859
+ kind: "user_input" as const,
860
+ status: "pending" as const,
861
+ responseCapability: { type: "message" as const },
862
+ createdAt: NOW,
863
+ resolvedAt: null,
864
+ },
865
+ ],
866
+ };
867
+ const policy = { ...THREAD_HISTORY_PAGE_POLICY, maxEncodedBytes: 64_000 };
868
+
869
+ const bounded = buildBoundedThreadProjection({ projection, snapshotSequence: 9, policy });
870
+
871
+ expect(bounded.projection.turnItems).toContain(card);
872
+ expect(bounded.projection.visibleTurnItems.length).toBeGreaterThan(0);
873
+ expect(bounded.hasMoreHistory).toBe(true);
874
+ if (questionBytes < policy.maxEncodedBytes) {
875
+ expect(Buffer.byteLength(JSON.stringify(bounded.projection), "utf8")).toBeLessThanOrEqual(
876
+ policy.maxEncodedBytes,
877
+ );
878
+ } else {
879
+ expect(bounded.projection.visibleTurnItems).toHaveLength(1);
880
+ }
881
+ },
882
+ );
883
+
884
it("preserves oversized actionable state", () => {
885
const projection = makeProjection([makeRow(0)]);
886
const actionable = {
apps/server/src/orchestration-v2/threadHistoryPaging.ts
+2
-3
@@ -673,10 +673,9 @@ export function buildBoundedThreadProjection(input: {
673
turnItems: [],
674
visibleTurnItems: [],
675
});
676
- // Control state (checkpoint file lists, the latest task list) cannot be paged, so it may claim at most
677
- // half the budget; past that the window would shrink to the one row every page admits.
676
+ // Keep the control-only window minimum, but required dependency cards claim their reserve first.
677
const windowBudget = Math.max(
679
- policy.maxEncodedBytes / 2,
678
+ dependencyReserve === 0 ? policy.maxEncodedBytes / 2 : 0,
679
policy.maxEncodedBytes - dependencyReserve - controlBytes - 1_024,
680
);
681
const windowPolicy: ThreadHistoryPagePolicy = { ...policy, maxEncodedBytes: windowBudget };
packages/client-runtime/src/state/threadHistoryMerge.test.ts
+92
@@ -5,6 +5,7 @@ import * as DateTime from "effect/DateTime";
5
import { v2Projection, v2ThreadId } from "./orchestrationV2TestFixtures.ts";
6
import { mergeOlderHistoryIntoProjection } from "./threadHistoryMerge.ts";
7
import { applyOrchestrationV2ProjectionEvent } from "./orchestrationV2Projection.ts";
8
+import { derivePendingThreadRequests } from "./threadRequests.ts";
9
10
const NOW = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
11
@@ -222,6 +223,97 @@ describe("threadHistoryMerge", () => {
223
expect(next?.visibleTurnItems[3]?.item).toEqual(liveItem);
224
});
225
226
+ // regression: #25806 a pending question card retained outside the window shows when paged in
227
+ it.each(["completed", "rolled_back"] as const)(
228
+ "keeps a pending question actionable when paging a %s run",
229
+ (status) => {
230
+ const recent = row(5);
231
+ const runId = "question-run" as never;
232
+ const nodeId = "question-node" as never;
233
+ const card = {
234
+ id: "item-question" as never,
235
+ type: "user_input_request" as const,
236
+ threadId: v2ThreadId,
237
+ runId,
238
+ nodeId,
239
+ providerThreadId: null,
240
+ providerTurnId: null,
241
+ nativeItemRef: null,
242
+ parentItemId: null,
243
+ ordinal: 1,
244
+ status: "running" as const,
245
+ title: null,
246
+ requestId: "request-question" as never,
247
+ responseMode: "message" as const,
248
+ questions: [{ id: "scope", header: "Scope", question: "Which scope?", options: [] }],
249
+ startedAt: NOW,
250
+ completedAt: null,
251
+ updatedAt: NOW,
252
+ };
253
+ const coldOpen = {
254
+ ...v2Projection,
255
+ runs: [
256
+ {
257
+ id: runId,
258
+ threadId: v2ThreadId,
259
+ ordinal: 1,
260
+ providerInstanceId: v2Projection.thread.providerInstanceId,
261
+ modelSelection: v2Projection.thread.modelSelection,
262
+ providerThreadId: null,
263
+ userMessageId: "question-message" as never,
264
+ rootNodeId: nodeId,
265
+ activeAttemptId: null,
266
+ status,
267
+ requestedAt: NOW,
268
+ startedAt: NOW,
269
+ completedAt: NOW,
270
+ checkpointId: null,
271
+ contextHandoffId: null,
272
+ },
273
+ ],
274
+ runtimeRequests: [
275
+ {
276
+ id: card.requestId,
277
+ nodeId,
278
+ providerTurnId: null,
279
+ nativeRequestRef: null,
280
+ kind: "user_input" as const,
281
+ status: "pending" as const,
282
+ responseCapability: { type: "message" as const },
283
+ createdAt: NOW,
284
+ resolvedAt: null,
285
+ },
286
+ ],
287
+ turnItems: [card, recent.item],
288
+ visibleTurnItems: [{ ...recent, position: 0 }],
289
+ };
290
+
291
+ const merged = mergeOlderHistoryIntoProjection(coldOpen, [
292
+ { ...row(0), position: 0 },
293
+ {
294
+ position: 1,
295
+ visibility: "local",
296
+ sourceThreadId: v2ThreadId,
297
+ sourceItemId: card.id,
298
+ item: card,
299
+ },
300
+ ]);
301
+
302
+ expect(merged.visibleTurnItems.map((entry) => String(entry.sourceItemId))).toEqual(
303
+ status === "rolled_back" ? ["item-0", "item-5"] : ["item-0", "item-question", "item-5"],
304
+ );
305
+ expect(merged.turnItems).toContain(card);
306
+ const pending = derivePendingThreadRequests(merged).userInputs;
307
+ expect(pending).toEqual(derivePendingThreadRequests(coldOpen).userInputs);
308
+ expect(pending).toHaveLength(1);
309
+ expect(pending[0]).toMatchObject({
310
+ requestId: card.requestId,
311
+ dismissible: true,
312
+ questions: card.questions.map((question) => ({ ...question, multiSelect: false })),
313
+ });
314
+ },
315
+ );
316
+
317
it("keeps a history-page interrupt result visible when its request was retained at cold open", () => {
318
const runId = "run-interrupt-cold" as never;
319
const nodeId = "node-interrupt-cold" as never;
packages/client-runtime/src/state/threadHistoryMerge.ts
+5
-1
@@ -89,9 +89,13 @@ export function mergeOlderHistoryIntoProjection(
89
(row.visibility === "local" || row.sourceThreadId === projection.thread.id)
90
) {
91
const currentItem = turnItemById.get(String(row.sourceItemId));
92
+ // Hidden live items stay hidden. Snapshots also retain interrupt requests and
93
+ // pending request cards outside the visible window; their history page shows them.
94
if (currentItem !== undefined) {
95
if (
94
- currentItem.type !== "run_interrupt_request" ||
96
+ (currentItem.type !== "run_interrupt_request" &&
97
+ currentItem.type !== "approval_request" &&
98
+ currentItem.type !== "user_input_request") ||
99
!isOrchestrationV2TurnItemVisible({
100
item: currentItem,
101
runs: projection.runs,
packages/client-runtime/src/state/threads-sync.test.ts
+84
-2
@@ -317,6 +317,32 @@ const PENDING_QUESTION = {
317
resolvedAt: null,
318
};
319
320
+const QUESTION_CARD: OrchestrationV2TurnItem = {
321
+ id: TurnItemId.make("question-card"),
322
+ threadId: THREAD_ID,
323
+ runId: null,
324
+ nodeId: PENDING_QUESTION.nodeId,
325
+ providerThreadId: null,
326
+ providerTurnId: null,
327
+ nativeItemRef: null,
328
+ parentItemId: null,
329
+ ordinal: 1,
330
+ status: "running",
331
+ title: null,
332
+ startedAt: v2Now,
333
+ completedAt: null,
334
+ updatedAt: v2Now,
335
+ type: "user_input_request",
336
+ requestId: PENDING_QUESTION.id,
337
+ questions: [{ id: "scope", header: "Scope", question: "Which scope?", options: [] }],
338
+};
339
+
340
+const PROJECTION_WITH_QUESTION = {
341
+ ...BASE_PROJECTION,
342
+ runtimeRequests: [PENDING_QUESTION],
343
+ turnItems: [QUESTION_CARD],
344
+};
345
+
346
const questionResolved = (sequence: number): OrchestrationV2ThreadStreamItem => ({
347
kind: "event",
348
sequence,
@@ -360,6 +386,62 @@ const deleted = (sequence = 3): OrchestrationV2ThreadStreamItem => {
386
};
387
388
describe("EnvironmentThreads", () => {
389
+ it.effect.each([
390
+ ["disk", false],
391
+ ["retained", false],
392
+ ["disk", true],
393
+ ["retained", true],
394
+ ] as const)(
395
+ "recovers a %s cache only when its pending question card is missing: %s",
396
+ ([cacheKind, hasCard]) =>
397
+ Effect.gen(function* () {
398
+ const projection = {
399
+ ...PROJECTION_WITH_QUESTION,
400
+ turnItems: hasCard ? [QUESTION_CARD] : [],
401
+ };
402
+ const schema = Schema.fromJsonString(StoredOrchestrationThreadSnapshot);
403
+ const encoded = yield* Schema.encodeEffect(schema)({
404
+ schemaVersion: ORCHESTRATION_THREAD_CACHE_SCHEMA_VERSION,
405
+ environmentId: TARGET.environmentId,
406
+ threadId: THREAD_ID,
407
+ snapshot: { snapshotSequence: CACHED_SNAPSHOT_SEQUENCE, projection },
408
+ });
409
+ const stored = yield* Schema.decodeEffect(schema)(encoded);
410
+ const h = yield* makeHarness({
411
+ loadCached: Effect.succeedSome(stored.snapshot),
412
+ ...(cacheKind === "retained"
413
+ ? {
414
+ resumeCache: {
415
+ owner: undefined,
416
+ snapshot: {
417
+ state: {
418
+ ...EMPTY_ENVIRONMENT_THREAD_STATE,
419
+ data: Option.some(projection),
420
+ status: "live",
421
+ },
422
+ sequence: CACHED_SNAPSHOT_SEQUENCE,
423
+ persisted: true,
424
+ },
425
+ },
426
+ }
427
+ : {}),
428
+ httpSnapshot: {
429
+ _tag: "present",
430
+ snapshot: {
431
+ snapshotSequence: CACHED_SNAPSHOT_SEQUENCE,
432
+ projection: PROJECTION_WITH_QUESTION,
433
+ },
434
+ },
435
+ completionMarker: true,
436
+ });
437
+ yield* Queue.offer(h.inputs, synchronized());
438
+ const state = yield* awaitThreadState(h.observed, (value) => value.status === "live");
439
+ expect(yield* Ref.get(h.loaderCalls)).toBe(hasCard ? 0 : 1);
440
+ expect(Option.getOrThrow(state.data)).toEqual(PROJECTION_WITH_QUESTION);
441
+ expect(yield* Ref.get(h.lastSubscribeAfterSequence)).toBe(CACHED_SNAPSHOT_SEQUENCE);
442
+ }),
443
+ );
444
+
445
it.effect("discards an oversized version-4 cache and requests a fresh bounded snapshot", () =>
446
Effect.gen(function* () {
447
const item: OrchestrationV2TurnItem = {
@@ -559,7 +641,7 @@ describe("EnvironmentThreads", () => {
641
Scope.close(scope, Exit.void),
642
);
643
const old = yield* makeHarness({
562
- cached: { ...BASE_PROJECTION, runtimeRequests: [PENDING_QUESTION] },
644
+ cached: PROJECTION_WITH_QUESTION,
645
resumeCache,
646
}).pipe(Effect.provideService(Scope.Scope, oldScope));
647
yield* awaitThreadState(old.observed, (value) => value.status === "live");
@@ -2146,7 +2228,7 @@ describe("EnvironmentThreads", () => {
2228
(unsubscribe) => Effect.sync(unsubscribe),
2229
);
2230
const harness = yield* makeHarness({
2149
- cached: { ...BASE_PROJECTION, runtimeRequests: [PENDING_QUESTION] },
2231
+ cached: PROJECTION_WITH_QUESTION,
2232
completionMarker: true,
2233
});
2234
yield* awaitThreadState(
packages/client-runtime/src/state/threads.ts
+27
-2
@@ -188,6 +188,20 @@ function cachedThreadState(value: EnvironmentThreadState): EnvironmentThreadStat
188
};
189
}
190
191
+function hasMissingPendingQuestionCard(projection: OrchestrationV2ThreadProjection): boolean {
192
+ const pending = new Set(
193
+ projection.runtimeRequests
194
+ .filter((request) => request.kind === "user_input" && request.status === "pending")
195
+ .map((request) => request.id),
196
+ );
197
+ if (pending.size === 0) return false;
198
+ for (const item of projection.turnItems) {
199
+ if (item.type === "user_input_request") pending.delete(item.requestId);
200
+ if (pending.size === 0) return false;
201
+ }
202
+ return true;
203
+}
204
+
205
export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make")(function* (
206
threadId: ThreadIdType,
207
resumeCache?: ThreadResumeCache,
@@ -205,10 +219,16 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
219
);
220
const wakeups = yield* Effect.serviceOption(ConnectionWakeups.ConnectionWakeups);
221
const environmentId = supervisor.target.environmentId;
208
- const retained = resumeCache?.snapshot;
222
+ const candidate = resumeCache?.snapshot;
223
+ const retained =
224
+ candidate &&
225
+ Option.isSome(candidate.state.data) &&
226
+ hasMissingPendingQuestionCard(candidate.state.data.value)
227
+ ? undefined
228
+ : candidate;
229
const owner = {};
230
if (resumeCache) resumeCache.owner = owner;
211
- const cached =
231
+ const loaded =
232
retained === undefined
233
? yield* cache.loadThread(environmentId, threadId).pipe(
234
Effect.catch((error) =>
@@ -223,6 +243,11 @@ export const makeEnvironmentThreadState = Effect.fn("EnvironmentThreadState.make
243
),
244
)
245
: Option.none<OrchestrationV2ThreadDetailSnapshot>();
246
+ // Old bounded caches can omit a pending question's card; replay alone cannot restore it.
247
+ const cached = Option.filter(
248
+ loaded,
249
+ (snapshot) => !hasMissingPendingQuestionCard(snapshot.projection),
250
+ );
251
const cachedThread = Option.map(cached, (snapshot) => snapshot.projection);
252
// Seed the resume cursor from the cached snapshot so a warm cache can catch up
253
// via `afterSequence` instead of re-downloading the full thread body.