import { afterEach, expect, test } from "bun:test"; import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { createNotificationsExtension } from "../src/sdk/bus/index"; import { readEndpoint } from "../src/sdk/bus/telegram-reference"; import { cleanupFixtureRoots, createNotificationFixtureRoot, type FixtureRootCleanup, isolatedNotificationSettings, registerNotificationRuntime, } from "./helpers/notification-settings"; /** * Regression for the text-before-ask ordering bug: the assistant text that * precedes an ask must reach the remote BEFORE the ask's action_needed (it used * to arrive only at turn_end, after the ask resolved), must not be emitted twice * once turn_end fires, and must never mirror the user's own prompt back as turn * output (message_end fires for user messages too). */ const sleep = (ms: number) => new Promise(r => setTimeout(r, ms)); async function waitFor(pred: () => boolean, ms = 4000, label = "condition"): Promise { const start = Date.now(); while (!pred()) { if (Date.now() - start > ms) throw new Error(`timeout waiting for ${label}`); await sleep(10); } } type Handler = (event: unknown, ctx: unknown) => unknown; type Frame = { type: string; text?: string; verbosity?: "lean" | "verbose"; redact?: boolean; tokenUsage?: string; model?: string; cwd?: string; }; type TestContextUsage = { tokens: number | null; contextWindow: number; percent: number | null; source: "provider_anchor" | "heuristic" | "unknown"; }; type TestModel = { id?: string }; const cleanupRoots: FixtureRootCleanup[] = []; const openSockets: WebSocket[] = []; afterEach(async () => { for (const ws of openSockets.splice(0)) ws.close(); await cleanupFixtureRoots(cleanupRoots); }); /** Boot the notifications extension against a real NotificationServer + WS client. */ async function setup( options: { contextUsage?: TestContextUsage | false; model?: TestModel | false; readNotificationDiffStat?: (cwd: string) => Promise; } = {}, ): Promise<{ handlers: Map; ctx: unknown; frames: Frame[]; ws: WebSocket; token: string; sid: string; }> { const handlers = new Map(); const api = { on: (event: string, handler: Handler) => { handlers.set(event, handler); }, registerCommand: () => {}, sendUserMessage: () => {}, } as never; const cwd = fs.mkdtempSync(path.join(os.tmpdir(), "skc-notif-order-")); const agentDir = path.join(cwd, ".skc", "agent"); const cleanup = await createNotificationFixtureRoot(cwd, agentDir); cleanupRoots.push(cleanup); createNotificationsExtension(api, { settings: isolatedNotificationSettings(agentDir), readNotificationDiffStat: options.readNotificationDiffStat, }); const sid = `order-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`; const ctx = { cwd, sessionManager: { getSessionId: () => sid, getSessionName: () => "Ordering Test", getArtifactsDir: () => cwd, getCwd: () => cwd, }, getContextUsage: () => options.contextUsage === false ? undefined : (options.contextUsage ?? { tokens: 12, contextWindow: 100, percent: 12, source: "provider_anchor" }), getModel: () => (options.model === false ? undefined : (options.model ?? { id: "test-model" })), } as never; registerNotificationRuntime(cleanup, { key: `notification-session:${sid}`, shutdown: async () => { await handlers.get("session_shutdown")?.({ type: "session_shutdown" }, ctx); }, }); await handlers.get("session_start")!({ type: "session_start" }, ctx); const endpointFile = path.join(cwd, ".skc", "state", "sdk", `${sid}.json`); await waitFor(() => fs.existsSync(endpointFile), 4000, "endpoint file"); const { url, token } = readEndpoint(endpointFile); const frames: Frame[] = []; const ws = new WebSocket(`${url}/?token=${encodeURIComponent(token)}`); openSockets.push(ws); ws.addEventListener("message", ev => frames.push(JSON.parse(String((ev as MessageEvent).data)))); await new Promise((resolve, reject) => { ws.addEventListener("open", () => resolve()); ws.addEventListener("error", () => reject(new Error("ws error"))); }); // Let the server-side connection subscribe before any (unbuffered) broadcast. await sleep(250); return { handlers, ctx, frames, ws, token, sid }; } test("assistant text preceding an ask is flushed before the ask and not duplicated at turn_end", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames } = await setup(); const turnStreams = () => frames.filter(f => f.type === "turn_stream"); // The assistant message (lead-in text) completes, then the ask tool starts. await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Here are your options:" } }, ctx, ); await handlers.get("tool_execution_start")!( { type: "tool_execution_start", toolName: "ask", toolCallId: "t1", args: {} }, ctx, ); // The lead-in must be flushed now (before the ask), not at turn_end. await waitFor(() => turnStreams().length === 1, 3000, "pre-ask turn_stream"); expect(turnStreams()[0]!.text).toContain("Here are your options:"); // turn_end for the same message must NOT duplicate the lead-in. await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 0, message: { role: "assistant", content: "Here are your options:" } }, ctx, ); await sleep(150); expect(turnStreams().length).toBe(1); // A later turn with different text is held under lean until agent_end. await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "All done." } }, ctx, ); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 1, message: { role: "assistant", content: "All done." } }, ctx, ); await sleep(150); expect(turnStreams().length).toBe(1); await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await waitFor(() => turnStreams().length === 2, 3000, "settled turn_stream at idle"); expect(turnStreams()[1]!.text).toContain("All done."); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("a tool-only ask turn does not mirror the preceding user prompt as turn output", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames } = await setup(); const turnStreams = () => frames.filter(f => f.type === "turn_stream"); // The user's prompt fires message_end (role user) first. await handlers.get("message_end")!( { type: "message_end", message: { role: "user", content: "please ask me something" } }, ctx, ); // The assistant turn is tool-only: a message with NO text, just the ask tool_use. await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: [{ type: "tool_use", name: "ask" }] } }, ctx, ); await handlers.get("tool_execution_start")!( { type: "tool_execution_start", toolName: "ask", toolCallId: "t1", args: {} }, ctx, ); await sleep(250); // Nothing should have been streamed: the user's prompt must not be mirrored, // and the assistant turn had no text of its own. expect(turnStreams().length).toBe(0); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("inbound /verbose and /lean update runtime verbosity and confirmation policy", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames, ws, token, sid } = await setup(); const configUpdates = () => frames.filter(f => f.type === "config_update"); const contextUpdates = () => frames.filter(f => f.type === "context_update"); await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await sleep(200); expect(contextUpdates().length).toBe(0); ws.send(JSON.stringify({ type: "config_command", sessionId: sid, token, verbosity: "verbose" })); await waitFor(() => configUpdates().some(f => f.verbosity === "verbose"), 3000, "verbose config_update"); await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await waitFor( () => contextUpdates().some( f => f.tokenUsage === "12/100" && f.model === "test-model" && f.cwd === path.basename((ctx as { cwd: string }).cwd), ), 3000, "verbose context_update", ); ws.send(JSON.stringify({ type: "config_command", sessionId: sid, token, verbosity: "lean" })); await waitFor(() => configUpdates().some(f => f.verbosity === "lean"), 3000, "lean config_update"); const beforeLeanIdle = contextUpdates().length; await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await sleep(200); expect(contextUpdates().length).toBe(beforeLeanIdle); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("drops an asynchronous context update completed after redaction changes", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const diffEntered = Promise.withResolvers(); const releaseDiff = Promise.withResolvers(); const { handlers, ctx, frames, ws, token, sid } = await setup({ readNotificationDiffStat: async () => { diffEntered.resolve(); return await releaseDiff.promise; }, }); ws.send(JSON.stringify({ type: "config_command", sessionId: sid, token, verbosity: "verbose" })); await waitFor(() => frames.some(f => f.type === "config_update" && f.verbosity === "verbose")); await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await diffEntered.promise; ws.send(JSON.stringify({ type: "config_command", sessionId: sid, token, redact: true })); await waitFor(() => frames.some(f => f.type === "config_update" && f.redact === true)); releaseDiff.resolve("1 file changed"); await sleep(100); expect(frames.some(f => f.type === "context_update")).toBe(false); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("verbose idle context includes compact cwd without usage metadata", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames, ws, token, sid } = await setup({ contextUsage: false, model: false }); const configUpdates = () => frames.filter(f => f.type === "config_update"); const contextUpdates = () => frames.filter(f => f.type === "context_update"); ws.send(JSON.stringify({ type: "config_command", sessionId: sid, token, verbosity: "verbose" })); await waitFor(() => configUpdates().some(f => f.verbosity === "verbose"), 3000, "verbose config_update"); await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await waitFor( () => contextUpdates().some( f => f.cwd === path.basename((ctx as { cwd: string }).cwd) && f.tokenUsage === undefined && f.model === undefined, ), 3000, "cwd-only verbose context_update", ); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("session shutdown emits session_closed before stopping the endpoint", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames } = await setup(); await handlers.get("agent_start")!({ type: "agent_start" }, ctx); await waitFor(() => frames.some(f => f.type === "activity"), 3000, "activity frame"); frames.length = 0; await handlers.get("session_shutdown")!({ type: "session_shutdown" }, ctx); await waitFor(() => frames.some(f => f.type === "session_closed"), 3000, "session_closed frame"); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); // --- Turn-output streaming: observable ordering & dedup --------------------- // These assert the WS-observable turn_stream contract: the pre-ask lead-in is // flushed BEFORE the ask (not held until turn_end), identical text is deduped // within a turn, lean defers the settled answer until agent_end, and verbose // still streams per turn_end. All turn output arrives as a `finalized`-phase frame. // // The emit site tags each turn_stream with a `finalAnswer` bit (false for the // pre-ask lead-in, true at settled final). The Rust wire struct `TurnStream` // (crates/skc-sdk/src/protocol.rs) carries it as an optional // `final_answer` (serialized `finalAnswer`), so the bit is asserted here at the // WS-observable level; the `finalAnswer` -> `richMarkdown` mapping itself is // verified at the pure-renderer level in notifications-threaded-render.test.ts. /** Read the `phase` discriminator off a captured turn_stream frame (survives the wire). */ const phaseOf = (f: Frame): string | undefined => (f as { phase?: string }).phase; /** Read the `finalAnswer` bit off a captured turn_stream frame (survives the wire). */ const finalAnswerOf = (f: Frame): boolean | undefined => (f as { finalAnswer?: boolean }).finalAnswer; test("a pre-ask lead-in is flushed as a finalized turn_stream before the ask, and an identical turn_end is deduped", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames } = await setup(); const turnStreams = () => frames.filter(f => f.type === "turn_stream"); // Assistant lead-in completes, then the ask tool starts. await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Pick a branch to merge:" } }, ctx, ); await handlers.get("tool_execution_start")!( { type: "tool_execution_start", toolName: "ask", toolCallId: "t1", args: {} }, ctx, ); // The pre-ask lead-in is flushed now (before any turn_end), as a finalized frame. await waitFor(() => turnStreams().length === 1, 3000, "pre-ask turn_stream"); expect(turnStreams()[0]!.text).toContain("Pick a branch to merge:"); expect(phaseOf(turnStreams()[0]!)).toBe("finalized"); expect(finalAnswerOf(turnStreams()[0]!)).toBe(false); // turn_end with identical text is deduped: no second frame appears. await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 0, message: { role: "assistant", content: "Pick a branch to merge:" } }, ctx, ); await sleep(150); expect(turnStreams().length).toBe(1); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("a distinct lean answer after a pre-ask lead-in streams only at agent_end", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames } = await setup(); const turnStreams = () => frames.filter(f => f.type === "turn_stream"); await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Looking into it now." } }, ctx, ); await handlers.get("tool_execution_start")!( { type: "tool_execution_start", toolName: "ask", toolCallId: "t1", args: {} }, ctx, ); await waitFor(() => turnStreams().length === 1, 3000, "pre-ask turn_stream"); expect(phaseOf(turnStreams()[0]!)).toBe("finalized"); expect(finalAnswerOf(turnStreams()[0]!)).toBe(false); // Intermediate tool-turn narration must not flood under lean. await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Checking the merge base." } }, ctx, ); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 1, message: { role: "assistant", content: "Checking the merge base." }, }, ctx, ); await sleep(150); expect(turnStreams().length).toBe(1); // Latest settled answer overwrites the deferred text and flushes at idle. await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Done, merged the feature branch." } }, ctx, ); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 2, message: { role: "assistant", content: "Done, merged the feature branch." }, }, ctx, ); await sleep(150); expect(turnStreams().length).toBe(1); await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await waitFor(() => turnStreams().length === 2, 3000, "settled turn_stream"); expect(turnStreams()[1]!.text).toContain("Done, merged the feature branch."); expect(phaseOf(turnStreams()[1]!)).toBe("finalized"); expect(finalAnswerOf(turnStreams()[1]!)).toBe(true); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("lean does not re-emit intermediate narration after a later ask lead-in at idle", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames } = await setup(); const turnStreams = () => frames.filter(f => f.type === "turn_stream"); // Intermediate tool-turn narration is deferred under lean. await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Intermediate narration" } }, ctx, ); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 0, message: { role: "assistant", content: "Intermediate narration" } }, ctx, ); await sleep(100); expect(turnStreams().length).toBe(0); // Later ask lead-in flushes immediately and must supersede the deferred text. await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Choose one:" } }, ctx, ); await handlers.get("tool_execution_start")!( { type: "tool_execution_start", toolName: "ask", toolCallId: "ask-1", args: {} }, ctx, ); await waitFor(() => turnStreams().length === 1, 3000, "ask lead-in"); expect(turnStreams()[0]!.text).toContain("Choose one:"); expect(finalAnswerOf(turnStreams()[0]!)).toBe(false); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 1, message: { role: "assistant", content: "Choose one:" } }, ctx, ); await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await sleep(200); // Idle must not re-emit "Intermediate narration" as finalAnswer after the ask lead-in. const finals = turnStreams().filter(f => finalAnswerOf(f) === true); expect(finals.length).toBe(0); expect(turnStreams().some(f => f.text?.includes("Intermediate narration"))).toBe(false); expect(turnStreams().length).toBe(1); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("lean ask-free turns emit a single settled turn_stream only at agent_end", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames } = await setup(); const turnStreams = () => frames.filter(f => f.type === "turn_stream"); await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Working on it…" } }, ctx, ); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 0, message: { role: "assistant", content: "Working on it…" } }, ctx, ); await sleep(150); expect(turnStreams().length).toBe(0); await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "All finished." } }, ctx, ); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 1, message: { role: "assistant", content: "All finished." } }, ctx, ); await sleep(150); expect(turnStreams().length).toBe(0); await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await waitFor(() => turnStreams().length === 1, 3000, "settled turn_stream"); expect(turnStreams()[0]!.text).toContain("All finished."); expect(phaseOf(turnStreams()[0]!)).toBe("finalized"); expect(finalAnswerOf(turnStreams()[0]!)).toBe(true); // No second frame for a single agent settle. await sleep(150); expect(turnStreams().length).toBe(1); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); test("verbose still streams a finalized turn_stream at each turn_end", async () => { const prevEnv = process.env.SKC_NOTIFICATIONS; process.env.SKC_NOTIFICATIONS = "1"; try { const { handlers, ctx, frames, ws, token, sid } = await setup(); const turnStreams = () => frames.filter(f => f.type === "turn_stream"); ws.send(JSON.stringify({ type: "config_command", sessionId: sid, token, verbosity: "verbose" })); await waitFor(() => frames.some(f => f.type === "config_update" && f.verbosity === "verbose"), 3000, "verbose"); await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "Step one complete." } }, ctx, ); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 0, message: { role: "assistant", content: "Step one complete." } }, ctx, ); await waitFor(() => turnStreams().length === 1, 3000, "first verbose turn_stream"); expect(turnStreams()[0]!.text).toContain("Step one complete."); expect(finalAnswerOf(turnStreams()[0]!)).toBe(true); await handlers.get("message_end")!( { type: "message_end", message: { role: "assistant", content: "All finished." } }, ctx, ); await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 1, message: { role: "assistant", content: "All finished." } }, ctx, ); await waitFor(() => turnStreams().length === 2, 3000, "second verbose turn_stream"); expect(turnStreams()[1]!.text).toContain("All finished."); expect(finalAnswerOf(turnStreams()[1]!)).toBe(true); // agent_end must not re-emit already-streamed verbose turns. await handlers.get("agent_end")!({ type: "agent_end" }, ctx); await sleep(150); expect(turnStreams().length).toBe(2); } finally { if (prevEnv === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevEnv; } }, 30000); const messageRefOf = (f: Frame): string | undefined => (f as { messageRef?: string }).messageRef; // Decision A / Pro round-5 regression: a stream-enabled *verbose* turn must finalize // as an editable (messageRef-bearing) frame even when live frames were async and // none landed before turn_end — so the daemon keeps it on the HTML edit path and // never rich-promotes a streamed final — and a late message_update after turn_end // must be dropped so no stale live edit follows the final. Lean deliberately // suppresses live streaming and defers settled answers to agent_end. test("stream-enabled final always carries a messageRef and a late message_update is dropped", async () => { const prevN = process.env.SKC_NOTIFICATIONS; const prevS = process.env.SKC_NOTIFICATIONS_STREAM; process.env.SKC_NOTIFICATIONS = "1"; process.env.SKC_NOTIFICATIONS_STREAM = "1"; try { const { handlers, ctx, frames, ws, token, sid } = await setup(); const turnStreams = () => frames.filter(f => f.type === "turn_stream"); ws.send(JSON.stringify({ type: "config_command", sessionId: sid, token, verbosity: "verbose" })); await waitFor(() => frames.some(f => f.type === "config_update" && f.verbosity === "verbose"), 3000, "verbose"); await handlers.get("turn_start")!({ type: "turn_start", turnIndex: 0 }, ctx); // turn_end with NO preceding message_update (live frames were async / none landed). await handlers.get("turn_end")!( { type: "turn_end", turnIndex: 0, message: { role: "assistant", content: "Streamed final." } }, ctx, ); await waitFor(() => turnStreams().some(f => phaseOf(f) === "finalized"), 3000, "finalized frame"); const finalFrame = turnStreams().find(f => phaseOf(f) === "finalized")!; expect(finalAnswerOf(finalFrame)).toBe(true); // A stream-enabled final MUST be editable (carry a messageRef) so the daemon // keeps it on the HTML edit path (shouldPromoteRich rejects editable frames). expect(typeof messageRefOf(finalFrame)).toBe("string"); // A late async message_update after turn_end is dropped: no stale live frame. const before = turnStreams().length; await handlers.get("message_update")!( { type: "message_update", message: { role: "assistant", content: "late partial after turn_end" } }, ctx, ); await sleep(150); expect(turnStreams().length).toBe(before); expect(turnStreams().some(f => phaseOf(f) === "live")).toBe(false); } finally { if (prevN === undefined) delete process.env.SKC_NOTIFICATIONS; else process.env.SKC_NOTIFICATIONS = prevN; if (prevS === undefined) delete process.env.SKC_NOTIFICATIONS_STREAM; else process.env.SKC_NOTIFICATIONS_STREAM = prevS; } }, 30000);