Skip to content

Commit 7422b42

Browse files
fix(server): an open question stays in the thread window, so its answer panel shows
Clients build the answer panel for an agent's question (and the approval panel) from the question's row, but a thread opens with only its newest turns. When the run kept working after asking, the row fell outside the window: the sidebar said "Input" while the thread showed no question. The snapshot window now keeps the rows of pending questions and approvals, as it already keeps run interrupt requests, both in the SQL window and in the bounded projection sent to clients. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d2fb83a commit 7422b42

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
@@ -2907,6 +2907,176 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
29072907
}),
29082908
);
29092909

2910+
it.effect("keeps an open question's row in the window after the run kept working", () =>
2911+
Effect.gen(function* () {
2912+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
2913+
const sql = yield* SqlClient.SqlClient;
2914+
const now = yield* DateTime.now;
2915+
const nowIso = DateTime.formatIso(now);
2916+
const threadId = ThreadId.make("thread:open-question-window");
2917+
yield* projectionStore.apply({
2918+
id: EventId.make("event:open-question-window:thread"),
2919+
type: "thread.created",
2920+
threadId,
2921+
occurredAt: now,
2922+
payload: {
2923+
createdBy: "user",
2924+
creationSource: "web",
2925+
id: threadId,
2926+
projectId: ProjectId.make("project:open-question-window"),
2927+
title: "Open question window",
2928+
providerInstanceId,
2929+
modelSelection,
2930+
runtimeMode: "full-access",
2931+
interactionMode: "default",
2932+
branch: null,
2933+
worktreePath: null,
2934+
activeProviderThreadId: null,
2935+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
2936+
forkedFrom: null,
2937+
createdAt: now,
2938+
updatedAt: now,
2939+
archivedAt: null,
2940+
settledOverride: null,
2941+
settledAt: null,
2942+
lastVisitedAt: null,
2943+
deletedAt: null,
2944+
},
2945+
});
2946+
for (let ordinal = 10; ordinal <= 300; ordinal += 1) {
2947+
const id = `turn-item:open-question-window:${ordinal}`;
2948+
yield* sql`
2949+
INSERT INTO orchestration_v2_projection_turn_items (
2950+
turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
2951+
parent_item_id, ordinal, type, status, updated_at, payload_json
2952+
) VALUES (
2953+
${id}, ${threadId}, NULL, NULL, NULL, NULL, NULL, ${ordinal},
2954+
'command_execution', 'completed', ${nowIso},
2955+
${encodeUnknownJsonString({
2956+
id,
2957+
threadId,
2958+
runId: null,
2959+
nodeId: null,
2960+
providerThreadId: null,
2961+
providerTurnId: null,
2962+
nativeItemRef: null,
2963+
parentItemId: null,
2964+
ordinal,
2965+
status: "completed",
2966+
title: `command ${ordinal}`,
2967+
input: `echo ${ordinal}`,
2968+
output: "ok",
2969+
exitCode: 0,
2970+
startedAt: nowIso,
2971+
completedAt: nowIso,
2972+
updatedAt: nowIso,
2973+
type: "command_execution",
2974+
})}
2975+
)
2976+
`;
2977+
}
2978+
const requestId = RuntimeRequestId.make("request:open-question-window");
2979+
const nodeId = NodeId.make("node:open-question-window");
2980+
const itemId = TurnItemId.make("turn-item:open-question-window:question");
2981+
yield* projectionStore.apply({
2982+
id: EventId.make("event:open-question-window:node"),
2983+
type: "node.updated",
2984+
threadId,
2985+
nodeId,
2986+
occurredAt: now,
2987+
payload: {
2988+
id: nodeId,
2989+
threadId,
2990+
runId: null,
2991+
parentNodeId: null,
2992+
rootNodeId: nodeId,
2993+
kind: "user_input_request",
2994+
status: "waiting",
2995+
countsForRun: false,
2996+
providerThreadId: null,
2997+
providerTurnId: null,
2998+
nativeItemRef: null,
2999+
runtimeRequestId: requestId,
3000+
checkpointScopeId: null,
3001+
startedAt: now,
3002+
completedAt: null,
3003+
},
3004+
});
3005+
const request = {
3006+
id: requestId,
3007+
nodeId,
3008+
providerTurnId: null,
3009+
nativeRequestRef: null,
3010+
kind: "user_input" as const,
3011+
status: "pending" as const,
3012+
responseCapability: { type: "message" as const },
3013+
createdAt: now,
3014+
resolvedAt: null,
3015+
};
3016+
yield* projectionStore.apply({
3017+
id: EventId.make("event:open-question-window:request"),
3018+
type: "runtime-request.updated",
3019+
threadId,
3020+
nodeId,
3021+
occurredAt: now,
3022+
payload: request,
3023+
});
3024+
yield* projectionStore.apply({
3025+
id: EventId.make("event:open-question-window:item"),
3026+
type: "turn-item.updated",
3027+
threadId,
3028+
nodeId,
3029+
occurredAt: now,
3030+
payload: {
3031+
id: itemId,
3032+
type: "user_input_request",
3033+
threadId,
3034+
runId: null,
3035+
nodeId,
3036+
providerThreadId: null,
3037+
providerTurnId: null,
3038+
nativeItemRef: null,
3039+
parentItemId: null,
3040+
ordinal: 5,
3041+
status: "waiting",
3042+
title: null,
3043+
startedAt: now,
3044+
completedAt: null,
3045+
updatedAt: now,
3046+
requestId,
3047+
responseMode: "message",
3048+
questions: [{ id: "choice", header: "Choice", question: "Continue?", options: [] }],
3049+
},
3050+
});
3051+
3052+
const questionRows = (snapshot: {
3053+
readonly projection: { readonly turnItems: ReadonlyArray<{ readonly id: string }> };
3054+
}) => snapshot.projection.turnItems.filter((item) => item.id === itemId).length;
3055+
const open = yield* projectionStore.getThreadSnapshotWindow(threadId, { rowLimit: 76 });
3056+
assert.strictEqual(questionRows(open), 1);
3057+
const bounded = buildBoundedThreadProjection({
3058+
policy: { ...THREAD_HISTORY_PAGE_POLICY, maxUserTurns: undefined, maxItems: 50 },
3059+
projection: open.projection,
3060+
snapshotSequence: open.snapshotSequence,
3061+
});
3062+
assert.strictEqual(questionRows(bounded), 1);
3063+
assert.isFalse(
3064+
bounded.projection.visibleTurnItems.some((row) => row.sourceItemId === itemId),
3065+
);
3066+
3067+
yield* projectionStore.apply({
3068+
id: EventId.make("event:open-question-window:resolved"),
3069+
type: "runtime-request.updated",
3070+
threadId,
3071+
nodeId,
3072+
occurredAt: now,
3073+
payload: { ...request, status: "resolved", resolvedAt: now },
3074+
});
3075+
const answered = yield* projectionStore.getThreadSnapshotWindow(threadId, { rowLimit: 76 });
3076+
assert.strictEqual(questionRows(answered), 0);
3077+
}),
3078+
);
3079+
29103080
it.effect("scopes actionable provider state to the requested bounded thread", () =>
29113081
Effect.gen(function* () {
29123082
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
@@ -2959,6 +2959,21 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
29592959
)
29602960
AND request.type = 'run_interrupt_request'
29612961
UNION
2962+
-- An open question or approval stays in the window: clients
2963+
-- build the answer panel from its row, and a run that kept
2964+
-- working pushes the row out of the newest turns.
2965+
SELECT question.rowid AS item_rowid, question.ordinal, question.turn_item_id
2966+
FROM orchestration_v2_projection_runtime_requests AS pending
2967+
JOIN orchestration_v2_projection_turn_items AS question
2968+
ON question.node_id = pending.node_id
2969+
WHERE pending.thread_id = ${threadId}
2970+
AND pending.status = 'pending'
2971+
AND question.thread_id = ${threadId}
2972+
AND question.type IN ('user_input_request', 'approval_request')
2973+
AND ${window.rowLimit} > 0
2974+
AND ${window.anchorItemId ?? null} IS NULL
2975+
AND ${window.requiredRunId ?? null} IS NULL
2976+
UNION
29622977
SELECT latest.item_rowid, latest.ordinal, latest.turn_item_id
29632978
FROM (
29642979
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
@@ -358,6 +358,29 @@ function isLocalProjectedRow(
358358
return row.visibility === "local" || String(row.sourceThreadId) === String(projection.thread.id);
359359
}
360360

361+
function pendingRequestIdsOf(
362+
projection: Pick<OrchestrationV2ThreadProjection, "runtimeRequests">,
363+
): ReadonlySet<string> {
364+
return new Set(
365+
projection.runtimeRequests
366+
.filter((request) => request.status === "pending")
367+
.map((request) => String(request.id)),
368+
);
369+
}
370+
371+
// Clients build the answer panel of an open question or approval from its row,
372+
// and a run that kept working pushes that row out of the newest turns.
373+
function isRetainedDependency(
374+
item: OrchestrationV2TurnItem,
375+
pendingRequestIds: ReadonlySet<string>,
376+
): boolean {
377+
return (
378+
item.type === "run_interrupt_request" ||
379+
((item.type === "user_input_request" || item.type === "approval_request") &&
380+
pendingRequestIds.has(String(item.requestId)))
381+
);
382+
}
383+
361384
/**
362385
* Keep every small request item from the full projection even when it sits
363386
* outside the recent visible window, so a later history page that introduces
@@ -375,9 +398,10 @@ function retainedInterruptRequestTurnItems(
375398
}
376399
}
377400

401+
const pendingRequestIds = pendingRequestIdsOf(projection);
378402
const retained: OrchestrationV2TurnItem[] = [];
379403
for (const item of projection.turnItems) {
380-
if (item.type !== "run_interrupt_request") {
404+
if (!isRetainedDependency(item, pendingRequestIds)) {
381405
continue;
382406
}
383407
if (visibleLocalIds.has(String(item.id))) {
@@ -510,8 +534,9 @@ export function buildBoundedThreadProjection(input: {
510534

511535
const dependencyReserve = (() => {
512536
let reserve = 0;
537+
const pendingRequestIds = pendingRequestIdsOf(controlProjection);
513538
for (const item of controlProjection.turnItems) {
514-
if (item.type === "run_interrupt_request") {
539+
if (isRetainedDependency(item, pendingRequestIds)) {
515540
reserve += bytesOfJson(item);
516541
}
517542
}

0 commit comments

Comments
 (0)