From aa12dc026c2733f8ccf9d6dd2df04634106f2085 Mon Sep 17 00:00:00 2001 From: Taras Mankovski Date: Tue, 17 Nov 2026 10:49:27 -0500 Subject: [PATCH] Show screened reconnect reasons in Sidekick chat --- packages/agent/host.ts | 1 + packages/cli/src/repl/sidekick/model.ts | 13 +- packages/cli/src/repl/sidekick/owner.ts | 23 ++- packages/cli/tests/repl-sidekick-acp.test.ts | 147 +++++++++++++++++- .../cli/tests/repl-sidekick-recovery.test.ts | 7 +- specs/repl-spec.md | 6 + 6 files changed, 186 insertions(+), 11 deletions(-) diff --git a/packages/agent/host.ts b/packages/agent/host.ts index a31cff18e..34581ee93 100644 --- a/packages/agent/host.ts +++ b/packages/agent/host.ts @@ -105,3 +105,4 @@ export type { */ export { AGENT_PROMPT, parsePromptRecord } from "./src/journal.ts"; export type { PromptRecord } from "./src/journal.ts"; +export { readPromptDiagnostic } from "./src/errors.ts"; diff --git a/packages/cli/src/repl/sidekick/model.ts b/packages/cli/src/repl/sidekick/model.ts index 4c1d5e438..43ac875db 100644 --- a/packages/cli/src/repl/sidekick/model.ts +++ b/packages/cli/src/repl/sidekick/model.ts @@ -853,8 +853,8 @@ function established( * is chat, and what belongs in chat is a sentence. */ function incompleteFailure(record: PromptRecord): string { - if (record.error?.message !== undefined) { - return record.error.message; + if (record.error !== undefined) { + return sidekickFailureText(record.error); } if (record.stopReason !== undefined) { return `the agent stopped early (${record.stopReason}).`; @@ -862,6 +862,15 @@ function incompleteFailure(record: PromptRecord): string { return `the agent's turn ${record.status}.`; } +export function sidekickFailureText(failure: NonNullable): string { + const detail = failure.diagnostic?.acp?.details; + return failure.name === "SessionResumeRequiredError" && + detail !== undefined && + detail.trim() !== "" + ? `${detail}\n${failure.message}` + : failure.message; +} + /** * What a saved turn said, as the literal Output its source projects. * diff --git a/packages/cli/src/repl/sidekick/owner.ts b/packages/cli/src/repl/sidekick/owner.ts index 3be526537..d7a6d89af 100644 --- a/packages/cli/src/repl/sidekick/owner.ts +++ b/packages/cli/src/repl/sidekick/owner.ts @@ -110,10 +110,15 @@ import { AGENT_PROMPT, AgentReplyWithheldError, parsePromptRecord, + readPromptDiagnostic, useAgentPromptPublisher, } from "@executablemd/agent/host"; import type { ExecutionInstallation, GeneratedXmdRootResult } from "@executablemd/core/host"; -import type { AgentPromptHandle, AgentPromptPublisher } from "@executablemd/agent/host"; +import type { + AgentPromptHandle, + AgentPromptPublisher, + PromptRecord, +} from "@executablemd/agent/host"; import { serializeError } from "@executablemd/durable-streams"; import { useAgentConversation } from "@executablemd/agent/host"; import type { AgentConversation } from "@executablemd/agent/host"; @@ -136,6 +141,7 @@ import { projectSidekick, recoveryRequired, requestsFromHistory, + sidekickFailureText, } from "./model.ts"; import type { ProjectedHistory, SidekickReading } from "./model.ts"; import { controlJournal } from "./admission.ts"; @@ -2855,6 +2861,13 @@ export function useSidekickOwner(input: SidekickOwnerInput): Operation { @@ -3287,6 +3299,7 @@ function* turnForRequest( text: saved.text, withheld: saved.withheld === true, resumeRefused: held.resumeRefused === true, + error: saved.error, }; } } diff --git a/packages/cli/tests/repl-sidekick-acp.test.ts b/packages/cli/tests/repl-sidekick-acp.test.ts index 70f34353b..609865f24 100644 --- a/packages/cli/tests/repl-sidekick-acp.test.ts +++ b/packages/cli/tests/repl-sidekick-acp.test.ts @@ -81,11 +81,13 @@ import { } from "./support/screen-journeys.ts"; import { useReplPage } from "./support/repl-page.ts"; import { settledReading } from "./support/mounted-history.ts"; -import { awaitedFact } from "./support/mounted-repl.ts"; +import { awaited, awaitedFact } from "./support/mounted-repl.ts"; import { runReplProgram } from "../src/repl/program.ts"; import { ReplHost } from "../src/repl/host.ts"; import { PAIRED_DIRECTORY, parseEntryRecords } from "../src/repl/sidekick/history.ts"; -import { AGENT_PROMPT } from "@executablemd/agent/host"; +import { AGENT_PROMPT, parsePromptRecord } from "@executablemd/agent/host"; +import { encodeLocation } from "../src/repl/route.ts"; +import { initialState } from "../src/repl/application.ts"; import { Descriptor, Request, @@ -3650,6 +3652,147 @@ describe("ACP82: the file this publication keeps open", () => { }); }); +describe("resume presentation in mounted Sidekick chat", () => { + const actionable = "Release first."; + const credential = ["ghp", "_", "abcdefghijklmnopqrstuvwxyz0123456789"].join(""); + const remedy = "could not be resumed"; + + for (const [mode, detail] of [ + ["actionable", actionable], + ["absent", undefined], + ["blank", " "], + ["screened", credential], + ] as const) { + it(`shows the same ${mode} refusal live and after passive reopen`, function* () { + yield* usePosixSidekickTransport(process.platform); + const home = yield* publishingHome("xmd-resume-presentation-"); + const path = join(home, "attachment.json"); + const host = yield* useMountedHost(); + host.fake.script({ reply: "Established chat." }); + const original = yield* scoped(function* () { + const page = yield* useReplPage({ host, sidekickAcp: path }); + const descriptor = Descriptor.parse(JSON.parse(yield* readTextFile(path))); + const baseline = yield* settledReading(page.repl); + page.repl.terminal.end(); + yield* awaited("the first REPL joining", () => page.repl.outcome() !== undefined); + return { descriptor, baseline }; + }); + const opening = host.fake.turns[0]?.handle; + if (opening === undefined) { + throw new Error("The original provider session was not established"); + } + const route = initialState(original.descriptor.execution).route; + const location = encodeLocation({ ...route, view: "sidekick", right: "sidekick" }); + host.fake.refuseSessionResume(opening.sessionKey, detail); + const beforeEnsures = host.fake.ensured.length; + const after = yield* scoped(function* () { + const page = yield* useReplPage({ host, location, sidekickAcp: path }); + yield* page.resize({ columns: 145, rows: 36 }); + const descriptor = Descriptor.parse(JSON.parse(yield* readTextFile(path))); + expect(descriptor.conversation).toBe(original.descriptor.conversation); + expect(host.fake.ensured).toHaveLength(beforeEnsures); + expect((yield* settledReading(page.repl)).chat).toEqual(original.baseline.chat); + const attached = attachResult.parse( + yield* requestAttachment(path, { method: "attach", cwd: descriptor.cwd }), + ); + const result = exchangeSchema.parse( + yield* requestAttachment(path, { + method: "prompt", + attachment: attached.attachment, + text: "Cold reconnect attempt.", + }), + ); + const visible = yield* page.until( + "one failed chat attempt and its recovery message", + (one) => + one.occurrences("(unfinished):") === 1 && + one.shows(remedy) && + (mode !== "actionable" || one.shows(`(unfinished): ${actionable}`)), + ); + expect(visible.shows(actionable)).toBe(mode === "actionable"); + expect(visible.shows(credential)).toBe(false); + expect(visible.shows("Internal error")).toBe(false); + expect(visible.shows("unrelated synthetic RPC payload")).toBe(false); + expect(host.fake.prompts).toHaveLength(1); + const retained = yield* settledReading(page.repl); + const delta = retained.chat.slice(original.baseline.chat.length); + expect( + delta.some( + (one) => one.kind === "response" || one.kind === "control" || one.kind === "feedback", + ), + ).toBe(false); + const attempts = delta.flatMap((one) => + one.kind === "event" + ? parseEntryRecords(one.event).flatMap((event) => + event.type === "yield" && + event.description.type === AGENT_PROMPT && + event.result.status === "ok" + ? [parsePromptRecord(event.result.value)] + : [], + ) + : [], + ); + expect(attempts).toHaveLength(1); + const saved = attempts[0]?.error; + if (saved === undefined) { + throw new Error("Expected a canonical resume failure"); + } + expect(saved).toEqual({ + name: "SessionResumeRequiredError", + message: expect.stringContaining(remedy), + diagnostic: { + provider: "acp", + operation: "session/prompt", + stage: "held-session-ensure", + code: "RUNTIME", + detailCode: "SESSION_RESUME_REQUIRED", + origin: "runtime", + acp: { + code: -32041, + message: "Internal error", + ...(mode === "actionable" || mode === "blank" ? { details: detail } : {}), + }, + }, + }); + expect(JSON.stringify(delta)).not.toContain(credential); + expect(JSON.stringify(delta)).not.toContain("unrelated synthetic RPC payload"); + expect(result.ending).toMatchObject({ + joined: true, + outcome: "failed", + error: mode === "actionable" ? actionable + "\n" + saved.message : saved.message, + }); + expect(result.output).toBe(""); + page.repl.terminal.end(); + yield* awaited("the failed REPL joining", () => page.repl.outcome() !== undefined); + return retained; + }); + const settledEnsures = host.fake.ensured.length; + const page = yield* useReplPage({ host, location }); + yield* page.resize({ columns: 145, rows: 36 }); + const reopened = yield* page.until( + "one reopened failure and its recovery message", + (one) => + one.occurrences("(unfinished):") === 1 && + one.shows(remedy) && + (mode !== "actionable" || one.shows(`(unfinished): ${actionable}`)), + ); + expect(reopened.shows(actionable)).toBe(mode === "actionable"); + expect(reopened.shows(credential)).toBe(false); + expect(reopened.shows("unrelated synthetic RPC payload")).toBe(false); + expect(host.fake.ensured).toHaveLength(settledEnsures); + expect(host.fake.prompts).toHaveLength(1); + expect((yield* settledReading(page.repl)).chat).toEqual(after.chat); + host.fake.script({ reply: "Later explicit Send succeeded." }); + yield* sendMessage(page, "Fresh request after release."); + yield* page.until("the new reply", (one) => one.shows("Later explicit Send succeeded.")); + expect(host.fake.prompts).toHaveLength(2); + expect(host.fake.prompts[1]).toContain("Fresh request after release."); + expect(host.fake.prompts[1]).not.toContain("Cold reconnect attempt."); + expect(host.fake.turns.at(-1)?.handle.sessionKey).toBe(opening.sessionKey); + }); + } +}); + describe("Follow: joined mixed-origin queue handoff", () => { it("Follow: mounted UI and delivered ACP stay FIFO behind entry and generated-control cleanup", function* () { yield* usePosixSidekickTransport(process.platform); diff --git a/packages/cli/tests/repl-sidekick-recovery.test.ts b/packages/cli/tests/repl-sidekick-recovery.test.ts index ccc4f4af7..bb670c4b8 100644 --- a/packages/cli/tests/repl-sidekick-recovery.test.ts +++ b/packages/cli/tests/repl-sidekick-recovery.test.ts @@ -5098,7 +5098,7 @@ describe("resume retention and mounted retained recovery corrections", () => { "the saved fallback and new chat action", (one) => one.shows("could not be resumed") && one.shows("[Start new chat]"), ); - expect(page.screen().shows("Release the active writer.")).toBe(false); + expect(page.screen().shows("Release the active writer.")).toBe(true); expect(page.screen().shows("[Retry]")).toBe(false); page.repl.terminal.end(); yield* awaited("the REPL joining", () => page.repl.outcome() !== undefined); @@ -5125,7 +5125,10 @@ describe("resume retention and mounted retained recovery corrections", () => { host: { ...held.host, profile: held.profile }, location: held.location, }); - yield* reopened.until("the retained local failure", (one) => one.shows("could not be resumed")); + yield* reopened.until( + "the retained local failure", + (one) => one.shows("Release the active writer.") && one.shows("could not be resumed"), + ); expect(reopened.screen().shows("[Retry]")).toBe(false); expect(yield* readSidekickRecords(held.chat.location.sidekick)).toEqual(after); }); diff --git a/specs/repl-spec.md b/specs/repl-spec.md index f3db3930b..328e49fef 100644 --- a/specs/repl-spec.md +++ b/specs/repl-spec.md @@ -1068,6 +1068,12 @@ no established request and appears beside the preceding attempt on its own. It creates no feedback intent or acknowledgement. A second ordinary outcome for the same request still refuses the history. +For a classified resume refusal, chat displays the screened ACP details as +literal text followed by the saved recovery message. A missing or blank detail +leaves the recovery message alone. The live exchange, including an original +continuation refusal, uses the same text as the saved attempt. The entry +Journal returns the canonical saved fields with their original values. + Activating a Sidekick turn opens the chat that contains it and brings that turn into view. It sets `view=sidekick`, `right=sidekick` and that chat's own identifier, and preserves the conversation filter, the saved surface, the