Skip to content

Commit 08a40a9

Browse files
fix(server): an open question stays in the thread window, so its answer panel shows (pingdotgg#22291)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 43d445b commit 08a40a9

3 files changed

Lines changed: 212 additions & 2 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,6 +2885,176 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
28852885
}),
28862886
);
28872887

2888+
it.effect("keeps an open question's row in the window after the run kept working", () =>
2889+
Effect.gen(function* () {
2890+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2891+
const sql = yield* SqlClient.SqlClient;
2892+
const now = yield* DateTime.now;
2893+
const nowIso = DateTime.formatIso(now);
2894+
const threadId = ThreadId.make("thread:open-question-window");
2895+
yield* projectionStore.apply({
2896+
id: EventId.make("event:open-question-window:thread"),
2897+
type: "thread.created",
2898+
threadId,
2899+
occurredAt: now,
2900+
payload: {
2901+
createdBy: "user",
2902+
creationSource: "web",
2903+
id: threadId,
2904+
projectId: ProjectId.make("project:open-question-window"),
2905+
title: "Open question window",
2906+
providerInstanceId,
2907+
modelSelection,
2908+
runtimeMode: "full-access",
2909+
interactionMode: "default",
2910+
branch: null,
2911+
worktreePath: null,
2912+
activeProviderThreadId: null,
2913+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2914+
forkedFrom: null,
2915+
createdAt: now,
2916+
updatedAt: now,
2917+
archivedAt: null,
2918+
settledOverride: null,
2919+
settledAt: null,
2920+
lastVisitedAt: null,
2921+
deletedAt: null,
2922+
},
2923+
});
2924+
for (let ordinal = 10; ordinal <= 300; ordinal += 1) {
2925+
const id = `turn-item:open-question-window:${ordinal}`;
2926+
yield* sql`
2927+
INSERT INTO orchestration_v2_projection_turn_items (
2928+
turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
2929+
parent_item_id, ordinal, type, status, updated_at, payload_json
2930+
) VALUES (
2931+
${id}, ${threadId}, NULL, NULL, NULL, NULL, NULL, ${ordinal},
2932+
'command_execution', 'completed', ${nowIso},
2933+
${encodeUnknownJsonString({
2934+
id,
2935+
threadId,
2936+
runId: null,
2937+
nodeId: null,
2938+
providerThreadId: null,
2939+
providerTurnId: null,
2940+
nativeItemRef: null,
2941+
parentItemId: null,
2942+
ordinal,
2943+
status: "completed",
2944+
title: `command ${ordinal}`,
2945+
input: `echo ${ordinal}`,
2946+
output: "ok",
2947+
exitCode: 0,
2948+
startedAt: nowIso,
2949+
completedAt: nowIso,
2950+
updatedAt: nowIso,
2951+
type: "command_execution",
2952+
})}
2953+
)
2954+
`;
2955+
}
2956+
const requestId = RuntimeRequestId.make("request:open-question-window");
2957+
const nodeId = NodeId.make("node:open-question-window");
2958+
const itemId = TurnItemId.make("turn-item:open-question-window:question");
2959+
yield* projectionStore.apply({
2960+
id: EventId.make("event:open-question-window:node"),
2961+
type: "node.updated",
2962+
threadId,
2963+
nodeId,
2964+
occurredAt: now,
2965+
payload: {
2966+
id: nodeId,
2967+
threadId,
2968+
runId: null,
2969+
parentNodeId: null,
2970+
rootNodeId: nodeId,
2971+
kind: "user_input_request",
2972+
status: "waiting",
2973+
countsForRun: false,
2974+
providerThreadId: null,
2975+
providerTurnId: null,
2976+
nativeItemRef: null,
2977+
runtimeRequestId: requestId,
2978+
checkpointScopeId: null,
2979+
startedAt: now,
2980+
completedAt: null,
2981+
},
2982+
});
2983+
const request = {
2984+
id: requestId,
2985+
nodeId,
2986+
providerTurnId: null,
2987+
nativeRequestRef: null,
2988+
kind: "user_input" as const,
2989+
status: "pending" as const,
2990+
responseCapability: { type: "message" as const },
2991+
createdAt: now,
2992+
resolvedAt: null,
2993+
};
2994+
yield* projectionStore.apply({
2995+
id: EventId.make("event:open-question-window:request"),
2996+
type: "runtime-request.updated",
2997+
threadId,
2998+
nodeId,
2999+
occurredAt: now,
3000+
payload: request,
3001+
});
3002+
yield* projectionStore.apply({
3003+
id: EventId.make("event:open-question-window:item"),
3004+
type: "turn-item.updated",
3005+
threadId,
3006+
nodeId,
3007+
occurredAt: now,
3008+
payload: {
3009+
id: itemId,
3010+
type: "user_input_request",
3011+
threadId,
3012+
runId: null,
3013+
nodeId,
3014+
providerThreadId: null,
3015+
providerTurnId: null,
3016+
nativeItemRef: null,
3017+
parentItemId: null,
3018+
ordinal: 5,
3019+
status: "waiting",
3020+
title: null,
3021+
startedAt: now,
3022+
completedAt: null,
3023+
updatedAt: now,
3024+
requestId,
3025+
responseMode: "message",
3026+
questions: [{ id: "choice", header: "Choice", question: "Continue?", options: [] }],
3027+
},
3028+
});
3029+
3030+
const questionRows = (snapshot: {
3031+
readonly projection: { readonly turnItems: ReadonlyArray<{ readonly id: string }> };
3032+
}) => snapshot.projection.turnItems.filter((item) => item.id === itemId).length;
3033+
const open = yield* projectionStore.getThreadSnapshotWindow(threadId, { rowLimit: 76 });
3034+
assert.strictEqual(questionRows(open), 1);
3035+
const bounded = buildBoundedThreadProjection({
3036+
policy: { ...THREAD_HISTORY_PAGE_POLICY, maxUserTurns: undefined, maxItems: 50 },
3037+
projection: open.projection,
3038+
snapshotSequence: open.snapshotSequence,
3039+
});
3040+
assert.strictEqual(questionRows(bounded), 1);
3041+
assert.isFalse(
3042+
bounded.projection.visibleTurnItems.some((row) => row.sourceItemId === itemId),
3043+
);
3044+
3045+
yield* projectionStore.apply({
3046+
id: EventId.make("event:open-question-window:resolved"),
3047+
type: "runtime-request.updated",
3048+
threadId,
3049+
nodeId,
3050+
occurredAt: now,
3051+
payload: { ...request, status: "resolved", resolvedAt: now },
3052+
});
3053+
const answered = yield* projectionStore.getThreadSnapshotWindow(threadId, { rowLimit: 76 });
3054+
assert.strictEqual(questionRows(answered), 0);
3055+
}),
3056+
);
3057+
28883058
it.effect("scopes actionable provider state to the requested bounded thread", () =>
28893059
Effect.gen(function* () {
28903060
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,6 +2967,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
29672967
)
29682968
AND request.type = 'run_interrupt_request'
29692969
UNION
2970+
-- An open question or approval stays in the window: clients
2971+
-- build the answer panel from its row, and a run that kept
2972+
-- working pushes the row out of the newest turns.
2973+
SELECT question.rowid AS item_rowid, question.ordinal, question.turn_item_id
2974+
FROM orchestration_v2_projection_runtime_requests AS pending
2975+
JOIN orchestration_v2_projection_turn_items AS question
2976+
ON question.node_id = pending.node_id
2977+
WHERE pending.thread_id = ${threadId}
2978+
AND pending.status = 'pending'
2979+
AND question.thread_id = ${threadId}
2980+
AND question.type IN ('user_input_request', 'approval_request')
2981+
AND ${window.rowLimit} > 0
2982+
AND ${window.anchorItemId ?? null} IS NULL
2983+
AND ${window.requiredRunId ?? null} IS NULL
2984+
UNION
29702985
SELECT latest.item_rowid, latest.ordinal, latest.turn_item_id
29712986
FROM (
29722987
SELECT rowid AS item_rowid, ordinal, turn_item_id

‎apps/server/src/orchestration-v2/threadHistoryPaging.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,29 @@ function isLocalProjectedRow(
501501
return row.visibility === "local" || String(row.sourceThreadId) === String(projection.thread.id);
502502
}
503503

504+
function pendingRequestIdsOf(
505+
projection: Pick<OrchestrationV2ThreadProjection, "runtimeRequests">,
506+
): ReadonlySet<string> {
507+
return new Set(
508+
projection.runtimeRequests
509+
.filter((request) => request.status === "pending")
510+
.map((request) => String(request.id)),
511+
);
512+
}
513+
514+
// Clients build the answer panel of an open question or approval from its row,
515+
// and a run that kept working pushes that row out of the newest turns.
516+
function isRetainedDependency(
517+
item: OrchestrationV2TurnItem,
518+
pendingRequestIds: ReadonlySet<string>,
519+
): boolean {
520+
return (
521+
item.type === "run_interrupt_request" ||
522+
((item.type === "user_input_request" || item.type === "approval_request") &&
523+
pendingRequestIds.has(String(item.requestId)))
524+
);
525+
}
526+
504527
/** Retain small requests outside the visible window; later result pages still need them for attempt/run reducers. */
505528
function retainedInterruptRequestTurnItems(
506529
projection: OrchestrationV2ThreadProjection,
@@ -513,9 +536,10 @@ function retainedInterruptRequestTurnItems(
513536
}
514537
}
515538

539+
const pendingRequestIds = pendingRequestIdsOf(projection);
516540
const retained: OrchestrationV2TurnItem[] = [];
517541
for (const item of projection.turnItems) {
518-
if (item.type !== "run_interrupt_request") {
542+
if (!isRetainedDependency(item, pendingRequestIds)) {
519543
continue;
520544
}
521545
if (visibleLocalIds.has(String(item.id))) {
@@ -635,8 +659,9 @@ export function buildBoundedThreadProjection(input: {
635659

636660
const dependencyReserve = (() => {
637661
let reserve = 0;
662+
const pendingRequestIds = pendingRequestIdsOf(controlProjection);
638663
for (const item of controlProjection.turnItems) {
639-
if (item.type === "run_interrupt_request") {
664+
if (isRetainedDependency(item, pendingRequestIds)) {
640665
reserve += bytesOfJson(item);
641666
}
642667
}

0 commit comments

Comments
 (0)