Skip to content

Commit 73b4ab6

Browse files
Alb11747codexclaudePR Batch Tester
committed
fix(client): keep older pending questions answerable
Carry upstream PR 26435 so pending question and approval cards survive bounded snapshots, remain reachable through history paging, and recover from incomplete disk or retained caches. The parent banner previously counted a pending request after its card left loaded history, leaving the child composer with no question to show. Upstream-PR: pingdotgg#26435 Co-Authored-By: GPT-6.1 Sol at high effort <[email protected]> Co-Authored-By: GPT-6.1 Sol at xhigh effort, review subagent <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Co-Authored-By: PR Batch Tester <[email protected]>
1 parent fdfcf46 commit 73b4ab6

9 files changed

Lines changed: 549 additions & 14 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
@@ -1490,6 +1490,226 @@ it.effect("SQL list older settled threads below a cursor", () =>
14901490
}).pipe(Effect.provide(layerTest)),
14911491
);
14921492

1493+
// regression: #25806 pending request cards outlive the history window that drops their turn
1494+
it.effect("SQL snapshot windows keep cards for requests still pending", () =>
1495+
Effect.gen(function* () {
1496+
const store = yield* ProjectionStore.ProjectionStoreV2;
1497+
const now = yield* DateTime.now;
1498+
const threadId = ThreadId.make("thread:pending-request-window");
1499+
yield* store.apply({
1500+
id: EventId.make("event:pending-request-window:thread"),
1501+
type: "thread.created",
1502+
threadId,
1503+
occurredAt: now,
1504+
payload: {
1505+
createdBy: "user",
1506+
creationSource: "web",
1507+
id: threadId,
1508+
projectId: ProjectId.make("project:pending-request-window"),
1509+
title: "Pending request window",
1510+
providerInstanceId,
1511+
modelSelection,
1512+
runtimeMode: "full-access",
1513+
interactionMode: "default",
1514+
branch: null,
1515+
worktreePath: null,
1516+
activeProviderThreadId: null,
1517+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
1518+
forkedFrom: null,
1519+
createdAt: now,
1520+
updatedAt: now,
1521+
archivedAt: null,
1522+
settledOverride: null,
1523+
settledAt: null,
1524+
lastVisitedAt: null,
1525+
deletedAt: null,
1526+
},
1527+
});
1528+
let ordinal = 0;
1529+
const itemBase = (runId: RunId, nodeId: NodeId, id: string) => ({
1530+
id: TurnItemId.make(`item:pending-request-window:${id}`),
1531+
threadId,
1532+
runId,
1533+
nodeId,
1534+
providerThreadId: null,
1535+
providerTurnId: null,
1536+
nativeItemRef: null,
1537+
parentItemId: null,
1538+
ordinal: ++ordinal,
1539+
status: "completed" as const,
1540+
title: null,
1541+
startedAt: now,
1542+
completedAt: now,
1543+
updatedAt: now,
1544+
});
1545+
const request = (id: string, nodeId: NodeId, kind: "user_input" | "command") => ({
1546+
id: RuntimeRequestId.make(`request:pending-request-window:${id}`),
1547+
nodeId,
1548+
providerTurnId: null,
1549+
nativeRequestRef: null,
1550+
kind,
1551+
responseCapability: {
1552+
type: "live" as const,
1553+
providerSessionId: ProviderSessionId.make("session:pending-request-window"),
1554+
},
1555+
createdAt: now,
1556+
});
1557+
const turnCount = THREAD_HISTORY_PAGE_POLICY.maxUserTurns + 4;
1558+
for (let turn = 1; turn <= turnCount; turn += 1) {
1559+
const runId = RunId.make(`run:pending-request-window:${turn}`);
1560+
const nodeId = NodeId.make(`node:pending-request-window:${turn}`);
1561+
yield* store.apply({
1562+
id: EventId.make(`event:pending-request-window:run:${turn}`),
1563+
type: "run.created",
1564+
threadId,
1565+
runId,
1566+
nodeId,
1567+
driver,
1568+
occurredAt: now,
1569+
payload: {
1570+
id: runId,
1571+
threadId,
1572+
ordinal: turn,
1573+
providerInstanceId,
1574+
modelSelection,
1575+
providerThreadId: null,
1576+
userMessageId: MessageId.make(`message:pending-request-window:${turn}`),
1577+
rootNodeId: nodeId,
1578+
activeAttemptId: null,
1579+
status: "completed",
1580+
requestedAt: now,
1581+
startedAt: now,
1582+
completedAt: now,
1583+
checkpointId: null,
1584+
contextHandoffId: null,
1585+
},
1586+
});
1587+
const items: Array<OrchestrationV2TurnItem> = [
1588+
{
1589+
...itemBase(runId, nodeId, `message:${turn}`),
1590+
type: "user_message",
1591+
createdBy: "user",
1592+
creationSource: "web",
1593+
messageId: MessageId.make(`message:pending-request-window:${turn}`),
1594+
inputIntent: "turn_start",
1595+
text: `Turn ${turn}`,
1596+
attachments: [],
1597+
},
1598+
];
1599+
if (turn === 1) {
1600+
const question = {
1601+
id: "scope",
1602+
header: "Scope",
1603+
question: "Which scope?",
1604+
options: [{ label: "All", description: "Everything" }],
1605+
};
1606+
items.push(
1607+
{
1608+
...itemBase(runId, nodeId, "question:pending"),
1609+
status: "running",
1610+
type: "user_input_request",
1611+
requestId: request("question:pending", nodeId, "user_input").id,
1612+
questions: [question],
1613+
},
1614+
{
1615+
...itemBase(runId, nodeId, "approval:pending"),
1616+
status: "running",
1617+
type: "approval_request",
1618+
requestId: request("approval:pending", nodeId, "command").id,
1619+
requestKind: "command",
1620+
prompt: "rm -rf build",
1621+
},
1622+
{
1623+
...itemBase(runId, nodeId, "question:resolved"),
1624+
type: "user_input_request",
1625+
requestId: request("question:resolved", nodeId, "user_input").id,
1626+
questions: [question],
1627+
},
1628+
);
1629+
for (const [id, kind, status] of [
1630+
["question:pending", "user_input", "pending"],
1631+
["approval:pending", "command", "pending"],
1632+
["question:resolved", "user_input", "resolved"],
1633+
] as const) {
1634+
yield* store.apply({
1635+
id: EventId.make(`event:pending-request-window:request:${id}`),
1636+
type: "runtime-request.updated",
1637+
threadId,
1638+
nodeId,
1639+
driver,
1640+
occurredAt: now,
1641+
payload: {
1642+
...request(id, nodeId, kind),
1643+
status,
1644+
resolvedAt: status === "pending" ? null : now,
1645+
},
1646+
});
1647+
}
1648+
}
1649+
for (const item of items) {
1650+
yield* store.apply({
1651+
id: EventId.make(`event:pending-request-window:${item.id}`),
1652+
type: "turn-item.updated",
1653+
threadId,
1654+
runId,
1655+
nodeId,
1656+
driver,
1657+
occurredAt: now,
1658+
payload: item,
1659+
});
1660+
}
1661+
}
1662+
1663+
const pendingCardIds = [
1664+
TurnItemId.make("item:pending-request-window:question:pending"),
1665+
TurnItemId.make("item:pending-request-window:approval:pending"),
1666+
];
1667+
const { statements, tracer } = traceSqlStatements();
1668+
const snapshot = yield* store
1669+
.getThreadSnapshotWindow(threadId, {
1670+
rowLimit: THREAD_HISTORY_PAGE_POLICY.maxItems + 2,
1671+
userTurnLimit: THREAD_HISTORY_PAGE_POLICY.maxUserTurns,
1672+
})
1673+
.pipe(Effect.withTracer(tracer));
1674+
const sql = yield* SqlClient.SqlClient;
1675+
const query = statements.find(
1676+
(statement) =>
1677+
statement.includes("FROM orchestration_v2_projection_runtime_requests AS request") &&
1678+
statement.includes("SELECT item.payload_json"),
1679+
);
1680+
assert.isDefined(query);
1681+
const plan = (yield* sql.unsafe<{ readonly detail: string }>(
1682+
`EXPLAIN QUERY PLAN ${query}`,
1683+
)).map((row) => row.detail);
1684+
assert.include(
1685+
plan[0],
1686+
"SEARCH request USING INDEX orchestration_v2_projection_runtime_requests_thread_status_idx",
1687+
);
1688+
assert.include(
1689+
plan[1],
1690+
"SEARCH item USING INDEX orchestration_v2_projection_turn_items_node_ordinal_idx",
1691+
);
1692+
const bounded = buildBoundedThreadProjection({
1693+
projection: snapshot.projection,
1694+
snapshotSequence: snapshot.snapshotSequence,
1695+
});
1696+
assert.isTrue(bounded.hasMoreHistory);
1697+
assert.notInclude(
1698+
bounded.projection.turnItems.map((item) => item.id),
1699+
TurnItemId.make("item:pending-request-window:question:resolved"),
1700+
);
1701+
for (const projection of [snapshot.projection, bounded.projection]) {
1702+
const visibleIds = projection.visibleTurnItems.map((row) => row.sourceItemId);
1703+
assert.notInclude(visibleIds, TurnItemId.make("item:pending-request-window:message:1"));
1704+
assert.includeMembers(
1705+
projection.turnItems.map((item) => item.id),
1706+
pendingCardIds,
1707+
);
1708+
for (const id of pendingCardIds) assert.notInclude(visibleIds, id);
1709+
}
1710+
}).pipe(Effect.provide(layerTest)),
1711+
);
1712+
14931713
it.effect("lists only active subagent IDs without loading shell data", () =>
14941714
Effect.gen(function* () {
14951715
const store = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5853,6 +5853,33 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
58535853
: {}),
58545854
...(historyAnchor === undefined ? {} : { historyAnchor }),
58555855
});
5856+
// Pending requests outlive their turn's window; clients read their prompt from the card.
5857+
const retainedIds = new Set(projection.turnItems.map((item) => item.id));
5858+
const pendingCards = (yield* decodeRows(
5859+
decodeTurnItemPayload,
5860+
threadId,
5861+
)(
5862+
yield* sql<PayloadRow>`
5863+
SELECT item.payload_json
5864+
FROM orchestration_v2_projection_runtime_requests AS request
5865+
CROSS JOIN orchestration_v2_projection_turn_items AS item
5866+
ON item.node_id = request.node_id
5867+
AND item.type IN ('approval_request', 'user_input_request')
5868+
AND json_extract(item.payload_json, '$.requestId') = request.runtime_request_id
5869+
WHERE request.thread_id = ${threadId} AND request.status = 'pending'
5870+
AND item.thread_id = ${threadId}
5871+
`,
5872+
)).filter((item) => !retainedIds.has(item.id));
5873+
const windowProjection =
5874+
pendingCards.length === 0
5875+
? projection
5876+
: {
5877+
...projection,
5878+
turnItems: [...projection.turnItems, ...pendingCards].toSorted(
5879+
(left, right) =>
5880+
left.ordinal - right.ordinal || left.id.localeCompare(right.id),
5881+
),
5882+
};
58565883
const rows = yield* sql<{ readonly snapshot_sequence: number | null }>`
58575884
SELECT MAX(sequence) AS snapshot_sequence
58585885
FROM orchestration_events
@@ -5863,7 +5890,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
58635890
return {
58645891
schemaVersion: ORCHESTRATION_V2_PROJECTION_SCHEMA_VERSION,
58655892
snapshotSequence: rows[0]?.snapshot_sequence ?? 0,
5866-
projection,
5893+
projection: windowProjection,
58675894
};
58685895
}),
58695896
)

‎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,
@@ -738,6 +739,69 @@ describe("threadHistoryPaging", () => {
738739
).toBe(2_097_152);
739740
});
740741

742+
it.each([40_000, 80_000])(
743+
"reserves pending question cards before visible history: %s bytes",
744+
(questionBytes) => {
745+
const rows = Array.from({ length: 20 }, (_, index) =>
746+
makeRow(index + 1, { outputBytes: 1_500 }),
747+
);
748+
const requestId = RuntimeRequestId.make("pending-question");
749+
const card: OrchestrationV2TurnItem = {
750+
id: TurnItemId.make("pending-question-card"),
751+
type: "user_input_request",
752+
threadId: THREAD,
753+
runId: null,
754+
nodeId: NODE,
755+
providerThreadId: null,
756+
providerTurnId: null,
757+
nativeItemRef: null,
758+
parentItemId: null,
759+
ordinal: 1,
760+
status: "completed",
761+
title: null,
762+
requestId,
763+
responseMode: "message",
764+
questions: [
765+
{ id: "scope", header: "Scope", question: "q".repeat(questionBytes), options: [] },
766+
],
767+
startedAt: NOW,
768+
completedAt: NOW,
769+
updatedAt: NOW,
770+
};
771+
const projection = {
772+
...makeProjection(rows),
773+
turnItems: [card, ...rows.map((row) => row.item)],
774+
runtimeRequests: [
775+
{
776+
id: requestId,
777+
nodeId: NODE,
778+
providerTurnId: null,
779+
nativeRequestRef: null,
780+
kind: "user_input" as const,
781+
status: "pending" as const,
782+
responseCapability: { type: "message" as const },
783+
createdAt: NOW,
784+
resolvedAt: null,
785+
},
786+
],
787+
};
788+
const policy = { ...THREAD_HISTORY_PAGE_POLICY, maxEncodedBytes: 64_000 };
789+
790+
const bounded = buildBoundedThreadProjection({ projection, snapshotSequence: 9, policy });
791+
792+
expect(bounded.projection.turnItems).toContain(card);
793+
expect(bounded.projection.visibleTurnItems.length).toBeGreaterThan(0);
794+
expect(bounded.hasMoreHistory).toBe(true);
795+
if (questionBytes < policy.maxEncodedBytes) {
796+
expect(Buffer.byteLength(JSON.stringify(bounded.projection), "utf8")).toBeLessThanOrEqual(
797+
policy.maxEncodedBytes,
798+
);
799+
} else {
800+
expect(bounded.projection.visibleTurnItems).toHaveLength(1);
801+
}
802+
},
803+
);
804+
741805
it("preserves oversized actionable state", () => {
742806
const projection = makeProjection([makeRow(0)]);
743807
const actionable = {

0 commit comments

Comments
 (0)