fix: retain session failures across recovery surfaces (#3669)

This commit is contained in:
Carlos Florêncio
2026-09-15 09:36:47 +01:00
committed by GitHub
parent 54e2e21f32
commit eeeb6f5586
101 changed files with 4057 additions and 1704 deletions
@@ -241,6 +241,53 @@ describe("filterVisibleMessages empty-turn notice supersession", () => {
});
});
describe("filterVisibleMessages recovery history", () => {
const RECOVERY_MESSAGE = "Could not resume the saved session.";
const FIRST_RECOVERY_ID = "recovery-1";
it("keeps a session failure after later agent output arrives", () => {
const failure = baseMessage({
id: FIRST_RECOVERY_ID,
type: "status",
content: RECOVERY_MESSAGE,
metadata: { recovery_actions: true, recovery_stamp: "failure-1" },
});
const output = baseMessage({
id: "agent-1",
type: "message",
content: "The resumed session is ready.",
created_at: AFTER,
});
expect(
filterVisibleMessages([failure, output], new Set(), new Set()).map((message) => message.id),
).toEqual([FIRST_RECOVERY_ID, "agent-1"]);
});
it("collapses duplicate deliveries of one failure while keeping later failures", () => {
const first = baseMessage({
id: FIRST_RECOVERY_ID,
type: "status",
content: RECOVERY_MESSAGE,
metadata: { recovery_actions: true, recovery_stamp: "failure-1" },
});
const duplicate = { ...first, id: `${FIRST_RECOVERY_ID}-duplicate` };
const second = baseMessage({
id: "recovery-2",
type: "status",
content: RECOVERY_MESSAGE,
metadata: { recovery_actions: true, recovery_stamp: "failure-2" },
created_at: AFTER,
});
expect(
filterVisibleMessages([first, duplicate, second], new Set(), new Set()).map(
(message) => message.id,
),
).toEqual([FIRST_RECOVERY_ID, "recovery-2"]);
});
});
describe("hasFailedAgentBootAfter", () => {
it("returns true when a boot after the failure reports status failed", () => {
expect(hasFailedAgentBootAfter([bootMessage(AFTER, { status: "failed" })], ERROR_AT)).toBe(
+15 -14
View File
@@ -91,22 +91,23 @@ function isRecoveryMessage(message: Message): boolean {
return metadata?.recovery_actions === true;
}
function deduplicateRecoveryMessages(messages: Message[]): Message[] {
let lastRecoveryIndex = -1;
for (let index = messages.length - 1; index >= 0; index--) {
if (isRecoveryMessage(messages[index])) {
lastRecoveryIndex = index;
break;
}
function recoveryMessageStamp(message: Message): string | null {
const metadata = message.metadata as Record<string, unknown> | undefined;
for (const key of ["recovery_stamp", "error_stamp", "failure_stamp"]) {
const value = metadata?.[key];
if (typeof value === "string" && value !== "") return value;
}
if (lastRecoveryIndex === -1) return messages;
const hasLaterActivity = messages
.slice(lastRecoveryIndex + 1)
.some((message) => message.type === "message" || message.type === "content");
return messages.filter((message, index) => {
return null;
}
function deduplicateRecoveryMessages(messages: Message[]): Message[] {
const seenStamps = new Set<string>();
return messages.filter((message) => {
if (!isRecoveryMessage(message)) return true;
if (hasLaterActivity) return false;
return index === lastRecoveryIndex;
const stamp = recoveryMessageStamp(message);
if (!stamp || seenStamps.has(stamp)) return !stamp;
seenStamps.add(stamp);
return true;
});
}
@@ -0,0 +1,35 @@
import { describe, expect, it } from "vitest";
import { sessionId as toSessionId, taskId as toTaskId, type Message } from "@/lib/types/http";
import { buildGroupedRenderItems, insertLastAgentErrorItem } from "./use-processed-messages";
const ERROR_MESSAGE = "peer disconnected before response";
const ERROR_OCCURRED_AT = "2026-06-14T10:05:00Z";
function legacyRecoveryMessage(): Message {
return {
id: "legacy-recovery",
session_id: toSessionId("s1"),
task_id: toTaskId("t1"),
author_type: "agent",
content: `Agent startup failed: ${ERROR_MESSAGE}`,
type: "status",
metadata: { recovery_actions: true },
created_at: ERROR_OCCURRED_AT,
};
}
describe("legacy recovery message deduplication", () => {
it("uses a matching unstamped row instead of adding a provisional notice", () => {
const legacy = legacyRecoveryMessage();
const items = buildGroupedRenderItems([legacy], "s1");
const result = insertLastAgentErrorItem(
items,
"s1",
{ message: ERROR_MESSAGE, occurredAt: ERROR_OCCURRED_AT },
[legacy],
);
expect(result).toBe(items);
});
});
@@ -0,0 +1,85 @@
import { describe, expect, it } from "vitest";
import {
sessionId as toSessionId,
taskId as toTaskId,
type Message,
type MessageType,
} from "@/lib/types/http";
import { buildGroupedRenderItems, insertLastAgentErrorItem } from "./use-processed-messages";
const SESSION_ID = "s1";
const ERROR_OCCURRED_AT = "2026-06-14T10:05:00Z";
function makeMessage(
id: string,
type: MessageType,
metadata?: Record<string, unknown>,
session = SESSION_ID,
): Message {
return {
id,
session_id: toSessionId(session),
task_id: toTaskId("t1"),
author_type: "agent",
content: "The agent encountered an error.",
type,
metadata,
created_at: ERROR_OCCURRED_AT,
};
}
describe("insertLastAgentErrorItem recovery identity", () => {
it("keeps one representation when the persisted recovery message is present", () => {
const persisted = makeMessage("recovery-1", "status", {
recovery_actions: true,
error_stamp: "failure-1",
});
const items = buildGroupedRenderItems([persisted], SESSION_ID, {
canAnchorPrepareProgress: false,
});
const result = insertLastAgentErrorItem(
items,
SESSION_ID,
{ message: persisted.content, occurredAt: ERROR_OCCURRED_AT, stamp: "failure-1" },
[persisted],
);
expect(result.map((item) => item.type)).toEqual(["message"]);
expect(result.filter((item) => item.type === "agent_error_notice")).toHaveLength(0);
});
it.each([
{
name: "a different failure stamp",
messageSession: SESSION_ID,
messageStamp: "failure-1",
errorStamp: "failure-2",
},
{
name: "a different session",
messageSession: "s2",
messageStamp: "failure-1",
errorStamp: "failure-1",
},
])("keeps the provisional notice for $name", ({ messageSession, messageStamp, errorStamp }) => {
const persisted = makeMessage(
"recovery-1",
"status",
{ recovery_actions: true, error_stamp: messageStamp },
messageSession,
);
const items = buildGroupedRenderItems([], SESSION_ID, {
canAnchorPrepareProgress: false,
});
const result = insertLastAgentErrorItem(
items,
SESSION_ID,
{ message: "new failure", occurredAt: ERROR_OCCURRED_AT, stamp: errorStamp },
[persisted],
);
expect(result.map((item) => item.type)).toEqual(["agent_error_notice"]);
});
});
@@ -23,6 +23,8 @@ import {
} from "./use-processed-messages";
const CURRENT_TURN_ID = "turn-current";
const PROVISIONAL_ERROR_MESSAGE = "peer disconnected before response";
const ERROR_OCCURRED_AT = "2026-06-14T10:05:00Z";
function makeMessage(
id: string,
@@ -416,8 +418,8 @@ describe("insertLastAgentErrorItem", () => {
});
const result = insertLastAgentErrorItem(items, "s1", {
message: "peer disconnected before response",
occurredAt: "2026-06-14T10:05:00Z",
message: PROVISIONAL_ERROR_MESSAGE,
occurredAt: ERROR_OCCURRED_AT,
});
expect(result.map((item) => item.type)).toEqual(["message", "agent_error_notice", "message"]);
@@ -425,8 +427,8 @@ describe("insertLastAgentErrorItem", () => {
it("uses the notice as the only item when there are no messages", () => {
const result = insertLastAgentErrorItem([], "s1", {
message: "peer disconnected before response",
occurredAt: "2026-06-14T10:05:00Z",
message: PROVISIONAL_ERROR_MESSAGE,
occurredAt: ERROR_OCCURRED_AT,
});
expect(result).toEqual([
@@ -444,8 +446,8 @@ describe("insertLastAgentErrorItem", () => {
});
const result = insertLastAgentErrorItem(items, "s1", {
message: "peer disconnected before response",
occurredAt: "2026-06-14T10:05:00Z",
message: PROVISIONAL_ERROR_MESSAGE,
occurredAt: ERROR_OCCURRED_AT,
});
expect(result.map((item) => item.type)).toEqual(["message", "agent_error_notice"]);
+26 -1
View File
@@ -15,7 +15,8 @@ import {
type PendingClarificationScope,
} from "@/lib/utils/pending-clarification";
import { createDebugLogger, isDebug } from "@/lib/debug/log";
import type { LastAgentError } from "@/lib/session-last-agent-error";
import { lastAgentErrorStamp, type LastAgentError } from "@/lib/session-last-agent-error";
import { legacyRecoveryMessageMatchesError } from "@/lib/session-recovery-presentation";
import {
buildChildrenByParentToolCallId,
buildPermissionsByToolCallId,
@@ -364,6 +365,7 @@ function buildGroupedItemsForHook(args: {
}),
args.resolvedSessionId,
args.lastAgentError,
args.allSessionMessages,
);
}
@@ -394,8 +396,16 @@ export function insertLastAgentErrorItem(
items: RenderItem[],
resolvedSessionId: string | null,
error?: LastAgentError | null,
persistedMessages: Message[] = [],
): RenderItem[] {
if (!resolvedSessionId || !error) return items;
if (
persistedMessages.some((message) =>
isPersistedRecoveryForSession(message, resolvedSessionId, error),
)
) {
return items;
}
const notice: AgentErrorNoticeItem = {
type: "agent_error_notice",
id: `last-agent-error-${resolvedSessionId}-${error.occurredAt ?? "unknown"}`,
@@ -406,6 +416,21 @@ export function insertLastAgentErrorItem(
return [...items.slice(0, insertAt), notice, ...items.slice(insertAt)];
}
function isPersistedRecoveryForSession(
message: Message,
sessionId: string,
error: LastAgentError,
): boolean {
if (message.session_id !== sessionId) return false;
const metadata = message.metadata as Record<string, unknown> | undefined;
if (metadata?.recovery_actions !== true) return false;
const messageStamp = metadata.error_stamp ?? metadata.recovery_stamp ?? metadata.failure_stamp;
if (typeof messageStamp === "string" && messageStamp !== "") {
return messageStamp === lastAgentErrorStamp(error);
}
return legacyRecoveryMessageMatchesError(message.content, message.created_at, error);
}
/** Builds the todo checklist from the latest persisted `todo`-type message,
* accepting string and `{ text, done }` entries. Total by construction:
* malformed metadata yields an empty list, never throws. */