|
| 1 | +/** |
| 2 | + * Real-surface QA driver (manual-qa; not part of the default suite). |
| 3 | + * |
| 4 | + * Drives the REAL builtin goal extension across a simulated process restart to |
| 5 | + * prove that a `blocked` goal produces the restart resume prompt, that accepting |
| 6 | + * reactivates it and queues a continuation, and that declining leaves it blocked. |
| 7 | + * |
| 8 | + * Run: npx vitest run test/manual-qa/goal-blocked-resume-restart.test.ts |
| 9 | + */ |
| 10 | +import { mkdtemp, rm } from "node:fs/promises"; |
| 11 | +import { tmpdir } from "node:os"; |
| 12 | +import { join } from "node:path"; |
| 13 | +import { expect, it } from "vitest"; |
| 14 | +import goalExtension from "../../src/core/extensions/builtin/goal/index.ts"; |
| 15 | +import { readGoal } from "../../src/core/extensions/builtin/goal/store.ts"; |
| 16 | +import type { ExtensionAPI, ExtensionContext, ToolDefinition } from "../../src/core/extensions/types.ts"; |
| 17 | + |
| 18 | +type AnyTool = ToolDefinition<any, any, any>; |
| 19 | +type Handler = (event: unknown, ctx: ExtensionContext) => Promise<unknown> | unknown; |
| 20 | + |
| 21 | +const THREAD = "qa-thread-blocked"; |
| 22 | +const transcript: string[] = []; |
| 23 | +const say = (line: string): void => { |
| 24 | + transcript.push(line); |
| 25 | + console.log(line); |
| 26 | +}; |
| 27 | + |
| 28 | +function makeSession(dir: string, onSelect: (options: string[]) => string | undefined) { |
| 29 | + const tools = new Map<string, AnyTool>(); |
| 30 | + const handlers = new Map<string, Handler[]>(); |
| 31 | + const sent: Array<{ customType: string }> = []; |
| 32 | + const prompts: Array<{ prompt: string; options: string[] }> = []; |
| 33 | + const pi = { |
| 34 | + registerTool: (tool: AnyTool) => tools.set(tool.name, tool), |
| 35 | + registerCommand: () => {}, |
| 36 | + on: (event: string, handler: Handler) => handlers.set(event, [...(handlers.get(event) ?? []), handler]), |
| 37 | + sendMessage: (message: { customType: string }) => sent.push(message), |
| 38 | + registerEntryRenderer: () => {}, |
| 39 | + appendEntry: () => {}, |
| 40 | + } as unknown as ExtensionAPI; |
| 41 | + goalExtension(pi); |
| 42 | + const ctx = { |
| 43 | + hasUI: true, |
| 44 | + cwd: dir, |
| 45 | + isIdle: () => true, |
| 46 | + hasPendingMessages: () => false, |
| 47 | + ui: { |
| 48 | + notify: () => {}, |
| 49 | + setStatus: () => {}, |
| 50 | + select: async (prompt: string, options: string[]) => { |
| 51 | + prompts.push({ prompt, options }); |
| 52 | + return onSelect(options); |
| 53 | + }, |
| 54 | + }, |
| 55 | + sessionManager: { |
| 56 | + getSessionFile: () => join(dir, "session.jsonl"), |
| 57 | + getSessionDir: () => dir, |
| 58 | + getSessionId: () => THREAD, |
| 59 | + getBranch: () => [], |
| 60 | + }, |
| 61 | + } as unknown as ExtensionContext; |
| 62 | + const fire = async (event: string, payload: unknown): Promise<void> => { |
| 63 | + for (const handler of handlers.get(event) ?? []) await handler(payload, ctx); |
| 64 | + }; |
| 65 | + return { tools, ctx, sent, prompts, fire }; |
| 66 | +} |
| 67 | + |
| 68 | +it("prompts to resume a blocked goal after a process restart", async () => { |
| 69 | + const dir = await mkdtemp(join(tmpdir(), "senpi-qa-goal-resume-")); |
| 70 | + const storeRef = { baseDir: join(dir, "extensions", "goal"), threadId: THREAD }; |
| 71 | + |
| 72 | + // Session A: the goal gets blocked, then the process goes away. |
| 73 | + const a = makeSession(dir, () => undefined); |
| 74 | + await a.tools |
| 75 | + .get("create_goal") |
| 76 | + ?.execute("c1", { objective: "Finish the release checklist" }, undefined, undefined, a.ctx); |
| 77 | + await a.tools |
| 78 | + .get("update_goal") |
| 79 | + ?.execute("u1", { status: "blocked", reason: "user interrupted the turn" }, undefined, undefined, a.ctx); |
| 80 | + await a.fire("session_shutdown", { type: "session_shutdown" }); |
| 81 | + const afterA = await readGoal(storeRef); |
| 82 | + say(`[session A] persisted status after shutdown: ${afterA?.status} (reason: ${afterA?.blockedReason})`); |
| 83 | + expect(afterA?.status).toBe("blocked"); |
| 84 | + |
| 85 | + // Session B: fresh process over the same store, resumed -> must prompt. |
| 86 | + const b = makeSession(dir, (options) => options[0]); |
| 87 | + await b.fire("session_start", { type: "session_start", reason: "resume" }); |
| 88 | + say(`[session B] resume prompts shown: ${b.prompts.length}`); |
| 89 | + for (const entry of b.prompts) { |
| 90 | + say(`[session B] prompt body: ${JSON.stringify(entry.prompt)}`); |
| 91 | + say(`[session B] prompt options: ${JSON.stringify(entry.options)}`); |
| 92 | + } |
| 93 | + const afterB = await readGoal(storeRef); |
| 94 | + say(`[session B] status after accepting "Resume goal": ${afterB?.status}`); |
| 95 | + say(`[session B] continuation queued: ${JSON.stringify(b.sent.map((message) => message.customType))}`); |
| 96 | + |
| 97 | + expect(b.prompts).toHaveLength(1); |
| 98 | + expect(b.prompts[0]?.prompt).toContain("Resume blocked goal?"); |
| 99 | + expect(b.prompts[0]?.prompt).toContain("Finish the release checklist"); |
| 100 | + expect(b.prompts[0]?.options).toEqual(["Resume goal", "Leave stopped"]); |
| 101 | + expect(afterB?.status).toBe("active"); |
| 102 | + expect(b.sent.map((message) => message.customType)).toEqual(["goal-continuation"]); |
| 103 | + |
| 104 | + // Session C: blocked again, resumed, declined -> stays blocked. |
| 105 | + await b.tools |
| 106 | + .get("update_goal") |
| 107 | + ?.execute("u2", { status: "blocked", reason: "user interrupted the turn" }, undefined, undefined, b.ctx); |
| 108 | + const c = makeSession(dir, (options) => options[1]); |
| 109 | + await c.fire("session_start", { type: "session_start", reason: "resume" }); |
| 110 | + const afterC = await readGoal(storeRef); |
| 111 | + say(`[session C] declined -> status stays: ${afterC?.status}; continuations queued: ${c.sent.length}`); |
| 112 | + expect(afterC?.status).toBe("blocked"); |
| 113 | + expect(c.sent).toHaveLength(0); |
| 114 | + |
| 115 | + await rm(dir, { recursive: true, force: true }); |
| 116 | + say(`cleanup: rm -rf ${dir}`); |
| 117 | +}); |
0 commit comments