Handle stale pending requests and reused Claude text block indices
- Map unknown pending approval/user-input errors to explicit stale-request details - Clear stale pending approvals and user-input prompts in session derivation logic - Treat reused Claude text block indexes as new assistant messages to avoid item ID reuse - Persist latest runtime metadata before provider stop-all and expand regression coverage
This commit is contained in:
1 parent
ef843d400e
commit
754efdbebe
8 files changed
+473
-36
No files matched your search
@@ -142,6 +142,36 @@ describe("derivePendingApprovals", () => {
|
||||
|
||||
expect(derivePendingApprovals(activities)).toEqual([]);
|
||||
});
|
||||
|
||||
it("clears stale pending approvals when the backend marks them stale after restart", () => {
|
||||
const activities: OrchestrationThreadActivity[] = [
|
||||
makeActivity({
|
||||
id: "approval-open-stale-restart",
|
||||
createdAt: "2026-02-23T00:00:01.000Z",
|
||||
kind: "approval.requested",
|
||||
summary: "Command approval requested",
|
||||
tone: "approval",
|
||||
payload: {
|
||||
requestId: "req-stale-restart-1",
|
||||
requestKind: "command",
|
||||
},
|
||||
}),
|
||||
makeActivity({
|
||||
id: "approval-failed-stale-restart",
|
||||
createdAt: "2026-02-23T00:00:02.000Z",
|
||||
kind: "provider.approval.respond.failed",
|
||||
summary: "Provider approval response failed",
|
||||
tone: "error",
|
||||
payload: {
|
||||
requestId: "req-stale-restart-1",
|
||||
detail:
|
||||
"Stale pending approval request: req-stale-restart-1. Provider callback state does not survive app restarts or recovered sessions. Restart the turn to continue.",
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
expect(derivePendingApprovals(activities)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("derivePendingUserInputs", () => {
|
||||
@@ -228,6 +258,48 @@ describe("derivePendingUserInputs", () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("clears stale pending user-input prompts when the provider reports an orphaned request", () => {
|
||||
const activities: OrchestrationThreadActivity[] = [
|
||||
makeActivity({
|
||||
id: "user-input-open-stale",
|
||||
createdAt: "2026-02-23T00:00:01.000Z",
|
||||
kind: "user-input.requested",
|
||||
summary: "User input requested",
|
||||
tone: "info",
|
||||
payload: {
|
||||
requestId: "req-user-input-stale-1",
|
||||
questions: [
|
||||
{
|
||||
id: "sandbox_mode",
|
||||
header: "Sandbox",
|
||||
question: "Which mode should be used?",
|
||||
options: [
|
||||
{
|
||||
label: "workspace-write",
|
||||
description: "Allow workspace writes only",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
makeActivity({
|
||||
id: "user-input-failed-stale",
|
||||
createdAt: "2026-02-23T00:00:02.000Z",
|
||||
kind: "provider.user-input.respond.failed",
|
||||
summary: "Provider user input response failed",
|
||||
tone: "error",
|
||||
payload: {
|
||||
requestId: "req-user-input-stale-1",
|
||||
detail:
|
||||
"Stale pending user-input request: req-user-input-stale-1. Provider callback state does not survive app restarts or recovered sessions. Restart the turn to continue.",
|
||||
},
|
||||
}),
|
||||
];
|
||||
|
||||
expect(derivePendingUserInputs(activities)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("deriveActivePlanState", () => {
|
||||
|
||||
@@ -160,6 +160,20 @@ function requestKindFromRequestType(requestType: unknown): PendingApproval["requ
|
||||
}
|
||||
}
|
||||
|
||||
function isStalePendingRequestFailureDetail(detail: string | undefined): boolean {
|
||||
const normalized = detail?.toLowerCase();
|
||||
if (!normalized) {
|
||||
return false;
|
||||
}
|
||||
return (
|
||||
normalized.includes("stale pending approval request") ||
|
||||
normalized.includes("stale pending user-input request") ||
|
||||
normalized.includes("unknown pending approval request") ||
|
||||
normalized.includes("unknown pending permission request") ||
|
||||
normalized.includes("unknown pending user-input request")
|
||||
);
|
||||
}
|
||||
|
||||
export function derivePendingApprovals(
|
||||
activities: ReadonlyArray<OrchestrationThreadActivity>,
|
||||
): PendingApproval[] {
|
||||
@@ -204,7 +218,7 @@ export function derivePendingApprovals(
|
||||
if (
|
||||
activity.kind === "provider.approval.respond.failed" &&
|
||||
requestId &&
|
||||
detail?.includes("Unknown pending permission request")
|
||||
isStalePendingRequestFailureDetail(detail)
|
||||
) {
|
||||
openByRequestId.delete(requestId);
|
||||
continue;
|
||||
@@ -280,6 +294,7 @@ export function derivePendingUserInputs(
|
||||
payload && typeof payload.requestId === "string"
|
||||
? ApprovalRequestId.makeUnsafe(payload.requestId)
|
||||
: null;
|
||||
const detail = payload && typeof payload.detail === "string" ? payload.detail : undefined;
|
||||
|
||||
if (activity.kind === "user-input.requested" && requestId) {
|
||||
const questions = parseUserInputQuestions(payload);
|
||||
@@ -296,6 +311,15 @@ export function derivePendingUserInputs(
|
||||
|
||||
if (activity.kind === "user-input.resolved" && requestId) {
|
||||
openByRequestId.delete(requestId);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (
|
||||
activity.kind === "provider.user-input.respond.failed" &&
|
||||
requestId &&
|
||||
isStalePendingRequestFailureDetail(detail)
|
||||
) {
|
||||
openByRequestId.delete(requestId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user