Skip to content

Commit c15e49a

Browse files
shivamhwpPR Batch Testerclaude
authored
fix: keep older pending questions answerable (pingdotgg#26435)
Co-authored-by: PR Batch Tester <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent d520d5b commit c15e49a

8 files changed

Lines changed: 522 additions & 24 deletions

File tree

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

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2012,6 +2012,226 @@ it.effect("SQL list older settled threads below a cursor", () =>
20122012
}).pipe(Effect.provide(layerTest)),
20132013
);
20142014

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+
20152235
it.effect("lists only active subagent IDs without loading shell data", () =>
20162236
Effect.gen(function* () {
20172237
const store = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,21 +3075,6 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
30753075
)
30763076
AND request.type = 'run_interrupt_request'
30773077
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
30933078
SELECT latest.item_rowid, latest.ordinal, latest.turn_item_id
30943079
FROM (
30953080
SELECT rowid AS item_rowid, ordinal, turn_item_id
@@ -6051,6 +6036,33 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60516036
: {}),
60526037
...(historyAnchor === undefined ? {} : { historyAnchor }),
60536038
});
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+
};
60546066
const rows = yield* sql<{ readonly snapshot_sequence: number | null }>`
60556067
SELECT MAX(sequence) AS snapshot_sequence
60566068
FROM orchestration_events
@@ -6061,7 +6073,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60616073
return {
60626074
schemaVersion: ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION,
60636075
snapshotSequence: rows[0]?.snapshot_sequence ?? 0,
6064-
projection,
6076+
projection: windowProjection,
60656077
};
60666078
}),
60676079
)

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
MessageId,
33
NodeId,
44
RunId,
5+
RuntimeRequestId,
56
ThreadId,
67
TurnItemId,
78
type OrchestrationV2ProjectedTurnItem,
@@ -817,6 +818,69 @@ describe("threadHistoryPaging", () => {
817818
).toBe(2_097_152);
818819
});
819820

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+
820884
it("preserves oversized actionable state", () => {
821885
const projection = makeProjection([makeRow(0)]);
822886
const actionable = {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,9 @@ export function buildBoundedThreadProjection(input: {
673673
turnItems: [],
674674
visibleTurnItems: [],
675675
});
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.
678677
const windowBudget = Math.max(
679-
policy.maxEncodedBytes / 2,
678+
dependencyReserve === 0 ? policy.maxEncodedBytes / 2 : 0,
680679
policy.maxEncodedBytes - dependencyReserve - controlBytes - 1_024,
681680
);
682681
const windowPolicy: ThreadHistoryPagePolicy = { ...policy, maxEncodedBytes: windowBudget };

0 commit comments

Comments
 (0)