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
@@ -1,6 +1,6 @@
"use client";
import { memo, useCallback, useEffect, useRef, useState, type ReactNode } from "react";
import { memo, useCallback, useEffect, useRef, useState } from "react";
import { useSettingsData } from "@/hooks/domains/settings/use-settings-data";
import { type ChatInputContainerHandle } from "@/components/task/chat/chat-input-container";
import { MessageList } from "@/components/task/chat/message-list";
@@ -21,8 +21,6 @@ type QuickChatContentProps = {
placeholderOverride?: string;
initialPrompt?: string;
onInitialPromptAttempted?: () => void;
recoveryContent?: ReactNode;
recoveryRevealKey?: string | null;
};
function useQuickChatState(sessionId: string) {
@@ -52,8 +50,6 @@ export const QuickChatContent = memo(function QuickChatContent({
placeholderOverride,
initialPrompt,
onInitialPromptAttempted,
recoveryContent,
recoveryRevealKey,
}: QuickChatContentProps) {
const [clarificationKey, setClarificationKey] = useState(0);
const shortcutScopeRef = useRef<HTMLDivElement>(null);
@@ -112,8 +108,6 @@ export const QuickChatContent = memo(function QuickChatContent({
sessionState={panelState.session?.state}
worktreePath={getSessionWorkspacePath(panelState.session)}
onOpenFile={undefined}
prependContent={recoveryContent}
recoveryRevealKey={recoveryRevealKey}
/>
</div>
<ClarificationPanelSection
@@ -1,4 +1,3 @@
import type { ReactNode } from "react";
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
@@ -17,7 +16,6 @@ const sessionRows = vi.hoisted(
const quickChatSessions = vi.hoisted(
() => [] as Array<{ sessionId: string; taskId?: string; agentProfileId?: string }>,
);
const quickChatContentProps = vi.hoisted(() => [] as Array<{ recoveryRevealKey?: string | null }>);
const useEnsureTaskSession = vi.hoisted(() => vi.fn());
const useTask = vi.hoisted(() => vi.fn());
const useSessionResumption = vi.hoisted(() =>
@@ -35,6 +33,10 @@ const useSessionResumption = vi.hoisted(() =>
})),
);
const useTaskStatusSummary = vi.hoisted(() => vi.fn());
const TEST_IDS = vi.hoisted(() => ({
sessionRecoveryCard: "session-bootstrap-recovery-card",
quickChatContent: "quick-chat-content",
}));
vi.mock("@/components/state-provider", () => ({
useAppStore: (selector: (state: unknown) => unknown) =>
@@ -53,34 +55,7 @@ vi.mock("@/components/task/passthrough-terminal", () => ({
PassthroughTerminal: () => <div data-testid="passthrough-terminal" />,
}));
vi.mock("./quick-chat-content", () => ({
QuickChatContent: ({
recoveryContent,
recoveryRevealKey,
}: {
recoveryContent?: ReactNode;
recoveryRevealKey?: string | null;
}) => {
quickChatContentProps.push({ recoveryRevealKey });
return <div data-testid="quick-chat-content">{recoveryContent}</div>;
},
}));
vi.mock("@/components/task/chat/session-bootstrap-recovery-card", () => ({
SessionBootstrapRecoveryCard: ({
error,
automaticRecovery,
}: {
error: { stamp: string };
automaticRecovery?: {
notice: string | null;
recoveryFailure: { outcome: string } | null;
};
}) => (
<div data-testid="session-bootstrap-recovery-card">
{error.stamp}
{automaticRecovery?.notice ?? ""}
{automaticRecovery?.recoveryFailure?.outcome ?? ""}
</div>
),
QuickChatContent: () => <div data-testid={TEST_IDS.quickChatContent} />,
}));
vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (key: string) => key }),
@@ -95,7 +70,6 @@ const BOOTSTRAP_OCCURRED_AT = "2026-09-11T10:00:00Z";
const BOOTSTRAP_PREVIEW = "The agent could not start.";
const WORKSPACE_READ_ONLY_NOTICE = "workspace restored read-only";
const RESUME_FAILURE = "resume failed";
const RECOVERY_CARD_TEST_ID = "session-bootstrap-recovery-card";
const session = {
kind: "chat" as const,
@@ -107,7 +81,6 @@ afterEach(() => {
cleanup();
delete sessionRows[session.sessionId];
quickChatSessions.length = 0;
quickChatContentProps.length = 0;
vi.clearAllMocks();
useTask.mockReset();
useTask.mockReturnValue(null);
@@ -190,7 +163,7 @@ describe("QuickChatSessionView session resumption", () => {
);
});
it("gives a matching bootstrap failure to the inline recovery card", () => {
it("keeps a session bootstrap failure in the transcript-owned path", () => {
sessionRows[session.sessionId] = { task_id: HYDRATED_TASK_ID };
useTask.mockReturnValue({ isArchived: false, workspaceId: WORKSPACE_ID });
useTaskStatusSummary.mockReturnValue({
@@ -205,11 +178,11 @@ describe("QuickChatSessionView session resumption", () => {
render(<QuickChatSessionView session={session} />);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).toBe("bootstrap-1");
expect(screen.getByTestId("quick-chat-content")).toBeTruthy();
expect(screen.queryByTestId(TEST_IDS.sessionRecoveryCard)).toBeNull();
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
});
it("passes automatic read-only recovery into the inline card", () => {
it("does not move session recovery feedback into the transcript", () => {
sessionRows[session.sessionId] = { task_id: HYDRATED_TASK_ID };
useTask.mockReturnValue({ isArchived: false, workspaceId: WORKSPACE_ID });
useTaskStatusSummary.mockReturnValue({
@@ -235,13 +208,11 @@ describe("QuickChatSessionView session resumption", () => {
render(<QuickChatSessionView session={session} />);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).toContain(
WORKSPACE_READ_ONLY_NOTICE,
);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).not.toContain(RESUME_FAILURE);
expect(screen.queryByTestId(TEST_IDS.sessionRecoveryCard)).toBeNull();
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
});
it("passes automatic recovery failure into the inline card", () => {
it("keeps session recovery failure out of the transcript", () => {
sessionRows[session.sessionId] = { task_id: HYDRATED_TASK_ID };
useTask.mockReturnValue({ isArchived: false, workspaceId: WORKSPACE_ID });
useTaskStatusSummary.mockReturnValue({
@@ -271,74 +242,19 @@ describe("QuickChatSessionView session resumption", () => {
render(<QuickChatSessionView session={session} />);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).toContain("recovery_failed");
expect(screen.queryByTestId(TEST_IDS.sessionRecoveryCard)).toBeNull();
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
});
it("keys unstamped recovery feedback by attempt and outcome while preserving duplicate state", () => {
const firstFailure = {
outcome: "recovery_failed" as const,
resumeError: "first resume failure",
restoreError: "first restore failure",
};
const firstAttempt = {
resumptionState: "error" as const,
sessionStatus: null,
error: "first translated failure",
notice: null,
recoveryFailure: firstFailure,
recoveryAttemptId: 4,
taskSessionState: "FAILED" as const,
worktreePath: null,
worktreeBranch: null,
resumeSession: vi.fn(),
};
useSessionResumption.mockReturnValue(firstAttempt as never);
it("does not pass a recovery reveal key to Quick Chat content", () => {
const view = render(<QuickChatSessionView session={session} />);
const firstKey = quickChatContentProps.at(-1)?.recoveryRevealKey;
expect(firstKey).toBe("session-1:recovery:4:recovery_failed");
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
useSessionResumption.mockReturnValue({
...firstAttempt,
error: "same attempt, different translated failure",
} as never);
view.rerender(<QuickChatSessionView session={session} />);
expect(quickChatContentProps.at(-1)?.recoveryRevealKey).toBe(firstKey);
useSessionResumption.mockReturnValue({
...firstAttempt,
recoveryFailure: {
outcome: "status_unavailable" as const,
kind: "timeout" as const,
statusError: "status request failed",
},
} as never);
view.rerender(<QuickChatSessionView session={session} />);
expect(quickChatContentProps.at(-1)?.recoveryRevealKey).toBe(
"session-1:recovery:4:status_unavailable",
);
useSessionResumption.mockReturnValue({
...firstAttempt,
error: null,
recoveryFailure: null,
recoveryAttemptId: 4,
} as never);
view.rerender(<QuickChatSessionView session={session} />);
expect(quickChatContentProps.at(-1)?.recoveryRevealKey).toBeNull();
useSessionResumption.mockReturnValue({
...firstAttempt,
error: "second translated failure",
recoveryAttemptId: 5,
} as never);
view.rerender(<QuickChatSessionView session={session} />);
expect(quickChatContentProps.at(-1)?.recoveryRevealKey).toBe(
"session-1:recovery:5:recovery_failed",
);
expect(screen.getByTestId(TEST_IDS.quickChatContent)).toBeTruthy();
});
it("keeps the bootstrap recovery card and automatic outcome in passthrough Quick Chat", () => {
it("keeps passthrough recovery feedback outside the terminal", () => {
sessionRows[session.sessionId] = { task_id: HYDRATED_TASK_ID, is_passthrough: true };
useTask.mockReturnValue({ isArchived: false, workspaceId: WORKSPACE_ID });
useTaskStatusSummary.mockReturnValue({
@@ -364,10 +280,11 @@ describe("QuickChatSessionView session resumption", () => {
render(<QuickChatSessionView session={session} />);
expect(screen.getByTestId(RECOVERY_CARD_TEST_ID).textContent).toContain(
expect(screen.queryByTestId(TEST_IDS.sessionRecoveryCard)).toBeNull();
expect(screen.getByTestId("session-recovery-notice").textContent).toContain(
WORKSPACE_READ_ONLY_NOTICE,
);
expect(screen.getByTestId("passthrough-terminal")).toBeTruthy();
expect(screen.queryByTestId("quick-chat-content")).toBeNull();
expect(screen.queryByTestId(TEST_IDS.quickChatContent)).toBeNull();
});
});
@@ -7,15 +7,12 @@ import { useSessionResumption } from "@/hooks/domains/session/use-session-resump
import { useTaskStatusSummary } from "@/hooks/domains/task/use-task-status-summary";
import { PassthroughTerminal } from "@/components/task/passthrough-terminal";
import { SessionRecoveryFeedback } from "@/components/task/ensure-session-error";
import { SessionBootstrapRecoveryCard } from "@/components/task/chat/session-bootstrap-recovery-card";
import { selectSessionRecoveryError } from "@/lib/session-recovery-presentation";
import { TaskLaunchErrorProvider } from "@/components/task/task-launch-error-context";
import { TaskSharedError } from "@/components/task/task-shared-error";
import type { QuickChatSession } from "@/lib/state/slices/ui/types";
import { QuickChatContent } from "./quick-chat-content";
import { useTranslation } from "react-i18next";
// i18n-exempt: internal recovery identity token, not user-facing copy.
const GENERIC_RECOVERY_OUTCOME = "feedback";
function useIsQuickChatPassthrough(sessionId: string) {
return useAppStore((state) => {
const session = state.taskSessions.items[sessionId];
@@ -45,33 +42,6 @@ function resolveTaskArchiveState(
return quickChatTaskId === taskId ? false : null;
}
function resolveRecoveryRevealKey(
sessionId: string,
bootstrapRecoveryError: { stamp: string; occurred_at: string } | null,
hasRecoveryFeedback: boolean,
recoveryAttemptId: number | undefined,
recoveryOutcome: string | null,
): string | null {
if (bootstrapRecoveryError) {
return `${sessionId}:${bootstrapRecoveryError.stamp || bootstrapRecoveryError.occurred_at}`;
}
if (!hasRecoveryFeedback) return null;
return `${sessionId}:recovery:${recoveryAttemptId ?? 0}:${recoveryOutcome ?? GENERIC_RECOVERY_OUTCOME}`;
}
function hasRecoveryFeedback(state: {
error: string | null;
notice: string | null;
recoveryFailure: unknown;
}): boolean {
return Boolean(state.error) || Boolean(state.notice) || state.recoveryFailure !== null;
}
function recoveryOutcome(failure: { outcome: string } | null): string | null {
if (!failure) return null;
return failure.outcome;
}
export function QuickChatSessionView({
session,
onInitialPromptAttempted,
@@ -91,13 +61,6 @@ export function QuickChatSessionView({
const resumption = useSessionResumption(taskId, session.sessionId, taskArchiveState);
const isPassthrough = useIsQuickChatPassthrough(session.sessionId);
const statusSummary = useTaskStatusSummary(taskId, task?.statusSummary);
const bootstrapRecoveryError = taskId
? selectSessionRecoveryError(
statusSummary?.active_error,
session.sessionId,
taskSession?.metadata,
)
: null;
const recoveryFeedback = (
<SessionRecoveryFeedback
error={resumption.error}
@@ -109,48 +72,60 @@ export function QuickChatSessionView({
}
/>
);
const recoverySurface = bootstrapRecoveryError ? (
<SessionBootstrapRecoveryCard
taskId={taskId!}
sessionId={session.sessionId}
workspaceId={task?.workspaceId ?? null}
error={bootstrapRecoveryError}
automaticRecovery={resumption}
/>
) : (
recoveryFeedback
);
const recoveryRevealKey = resolveRecoveryRevealKey(
session.sessionId,
bootstrapRecoveryError,
hasRecoveryFeedback(resumption),
resumption.recoveryAttemptId,
recoveryOutcome(resumption.recoveryFailure),
);
if (isPassthrough) {
return (
<div className="flex min-h-0 flex-1 flex-col overflow-hidden">
{recoverySurface}
<div className="min-h-0 flex-1">
<PassthroughTerminal key={session.sessionId} sessionId={session.sessionId} mode="agent" />
</div>
const sessionContent = isPassthrough ? (
<div className="flex min-h-0 flex-1 flex-col overflow-hidden">
{recoveryFeedback}
<div className="min-h-0 flex-1">
<PassthroughTerminal key={session.sessionId} sessionId={session.sessionId} mode="agent" />
</div>
);
}
const isConfig = session.kind === "config";
return (
</div>
) : (
<div className="flex min-h-0 flex-1 flex-col">
<div className="flex min-h-0 flex-1 flex-col">
<QuickChatContent
sessionId={session.sessionId}
minimalToolbar={isConfig}
placeholderOverride={isConfig ? t("chat:configChatPlaceholder") : undefined}
minimalToolbar={session.kind === "config"}
placeholderOverride={
session.kind === "config" ? t("chat:configChatPlaceholder") : undefined
}
initialPrompt={session.initialPrompt}
onInitialPromptAttempted={onInitialPromptAttempted}
recoveryContent={recoverySurface}
recoveryRevealKey={recoveryRevealKey}
/>
</div>
</div>
);
if (!taskId) return sessionContent;
if (isPassthrough) {
return (
<TaskLaunchErrorProvider
value={{
taskId,
workspaceId: task?.workspaceId ?? "",
statusSummary,
automaticRecovery: resumption,
}}
>
<div className="flex min-h-0 flex-1 flex-col">
<TaskSharedError />
{sessionContent}
</div>
</TaskLaunchErrorProvider>
);
}
return (
<TaskLaunchErrorProvider
value={{
taskId,
workspaceId: task?.workspaceId ?? "",
statusSummary,
automaticRecovery: resumption,
}}
>
<div className="flex min-h-0 flex-1 flex-col">
<TaskSharedError />
{sessionContent}
</div>
</TaskLaunchErrorProvider>
);
}