feat(FN-1152): add retry flows for failed AI planning sessions

- Keep errored AI sessions retryable in the session store and add backend retry handlers for planning, subtask breakdown, and mission interview flows
- Add retry API routes and dashboard API client helpers for planning, subtask, and mission interview session retries
- Update PlanningModeModal, SubtaskBreakdownModal, MissionInterviewModal, and background session handling to show error states with retry/cancel UX
- Expand unit and integration tests across store, services, routes, and modal components to cover retry success and failure paths
This commit is contained in:
gsxdsm
2026-04-08 15:10:46 -07:00
parent d0578a589b
commit 0ff6d42c0f
20 changed files with 1368 additions and 234 deletions

View File

@@ -2,7 +2,16 @@
import { EventEmitter } from "node:events";
import ts from "typescript";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const { mockCreateKbAgent } = vi.hoisted(() => ({
mockCreateKbAgent: vi.fn(),
}));
vi.mock("@fusion/engine", () => ({
createKbAgent: mockCreateKbAgent,
}));
import type { AiSessionRow } from "./ai-session-store.js";
// @ts-expect-error Vite raw loader import for source-level utility tests
import subtaskBreakdownSource from "./subtask-breakdown.ts?raw";
@@ -11,9 +20,11 @@ import {
cancelSubtaskSession,
cleanupSubtaskSession,
createSubtaskSession,
retrySubtaskSession,
getSubtaskSession,
rehydrateFromStore,
SessionNotFoundError,
InvalidSessionStateError,
setAiSessionStore,
SubtaskStreamManager,
} from "./subtask-breakdown.js";
@@ -111,16 +122,66 @@ async function loadInternalSubtaskFunctions(): Promise<InternalSubtaskFns> {
let internalFns: InternalSubtaskFns;
function createMockSubtaskAgent(responseText?: string) {
const messages: Array<{ role: string; content: string }> = [];
const response =
responseText ??
JSON.stringify({
subtasks: [
{
id: "subtask-1",
title: "Define implementation approach",
description: "Plan the implementation details",
suggestedSize: "S",
dependsOn: [],
},
],
});
return {
session: {
state: { messages },
prompt: vi.fn(async (message: string) => {
messages.push({ role: "user", content: message });
messages.push({ role: "assistant", content: response });
}),
dispose: vi.fn(),
},
};
}
class MockAiSessionStore extends EventEmitter {
rows = new Map<string, AiSessionRow>();
upsert(row: AiSessionRow): void {
this.rows.set(row.id, row);
}
updateThinking(id: string, thinkingOutput: string): void {
const row = this.rows.get(id);
if (!row) {
return;
}
this.rows.set(id, {
...row,
thinkingOutput,
updatedAt: new Date().toISOString(),
});
}
delete(id: string): void {
this.rows.delete(id);
this.emit("ai_session:deleted", id);
}
get(id: string): AiSessionRow | null {
return this.rows.get(id) ?? null;
}
listRecoverable(): AiSessionRow[] {
return [...this.rows.values()].filter(
(row) => row.status === "awaiting_input" || row.status === "generating",
(row) => row.status === "awaiting_input" || row.status === "generating" || row.status === "error",
);
}
@@ -169,6 +230,11 @@ beforeAll(async () => {
internalFns = await loadInternalSubtaskFunctions();
});
beforeEach(() => {
mockCreateKbAgent.mockReset();
mockCreateKbAgent.mockImplementation(async () => createMockSubtaskAgent());
});
afterEach(() => {
__resetSubtaskBreakdownState();
vi.restoreAllMocks();
@@ -394,6 +460,38 @@ describe("subtask session lifecycle", () => {
expect(session).not.toHaveProperty("thinkingOutput");
});
it("retrySubtaskSession retries errored sessions restored from SQLite", async () => {
const store = new MockAiSessionStore();
const row = buildSubtaskRow({
id: "subtask-retry-1",
status: "error",
error: "Transient failure",
result: null,
});
store.rows.set(row.id, row);
setAiSessionStore(store as any);
await retrySubtaskSession(row.id, "/tmp/project");
const session = getSubtaskSession(row.id);
expect(session).toBeDefined();
expect(session?.status).toBe("complete");
expect(session?.subtasks.length).toBeGreaterThan(0);
expect(store.get(row.id)?.status).toBe("complete");
expect(store.get(row.id)?.error).toBeNull();
});
it("retrySubtaskSession rejects non-error sessions", async () => {
const store = new MockAiSessionStore();
const row = buildSubtaskRow({ id: "subtask-retry-2", status: "generating" });
store.rows.set(row.id, row);
setAiSessionStore(store as any);
await expect(retrySubtaskSession(row.id, "/tmp/project")).rejects.toBeInstanceOf(
InvalidSessionStateError,
);
});
it("cancelSubtaskSession throws SessionNotFoundError for unknown session", async () => {
await expect(cancelSubtaskSession("missing-session")).rejects.toMatchObject({
name: "SessionNotFoundError",