From 019c6df7e0c3c444a72664ed4d8d5533dbc95448 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Wed, 4 Nov 2026 12:40:53 +0530 Subject: [PATCH] fix(doctor): do not prompt Fix/Skip in a non-interactive shell agentmemory doctor showed the Fix/Skip/More/Quit select for every auto-fixable failure even with CI set or without a terminal. It only avoided hanging because clack gave up on a closed stdin. Doctor now prompts only when stdin and stdout are terminals and CI is unset. Otherwise it reports each failure, skips the fix, and prints one hint to run `agentmemory doctor --all`. --- src/cli.ts | 12 ++++++++++++ src/cli/doctor-diagnostics.ts | 8 ++++++++ test/cli-doctor-env-file.test.ts | 12 ++++++++++-- test/cli-doctor-fixes.test.ts | 11 +++++++++++ 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index b9e512e3d8..1c599b1e02 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -41,6 +41,7 @@ import { generateId } from "./state/schema.js"; import { buildDiagnostics, canAutoFix, + doctorPromptsAllowed, dryRunPlan, parseEnvFile, type Diagnostic, @@ -2366,6 +2367,8 @@ async function runDoctor() { let fixed = 0; let skipped = 0; let quit = false; + const promptsAllowed = doctorPromptsAllowed(process.env, process.stdin.isTTY, process.stdout.isTTY); + let autoFixHintShown = false; for (const d of diagnostics) { if (quit) { @@ -2397,6 +2400,15 @@ async function runDoctor() { continue; } + if (!promptsAllowed) { + skipped++; + if (!autoFixHintShown) { + autoFixHintShown = true; + p.log.info("Not prompting in a non-interactive shell. Run `agentmemory doctor --all` to apply the fixes."); + } + continue; + } + // Interactive prompt loop — allow [?] More info without leaving the check. while (true) { const action = await askFixAction(d); diff --git a/src/cli/doctor-diagnostics.ts b/src/cli/doctor-diagnostics.ts index d6b7e77241..9dca3ce75a 100644 --- a/src/cli/doctor-diagnostics.ts +++ b/src/cli/doctor-diagnostics.ts @@ -66,6 +66,14 @@ export function canAutoFix(d: Diagnostic, status?: DiagnosticStatus): boolean { return !d.manualOnly && !status?.manualFix; } +export function doctorPromptsAllowed( + env: NodeJS.ProcessEnv, + stdinIsTTY: boolean | undefined, + stdoutIsTTY: boolean | undefined, +): boolean { + return Boolean(stdinIsTTY) && Boolean(stdoutIsTTY) && !env["CI"]; +} + // Diagnostic ids are stable for testing and machine-readable doctor output. export const DIAGNOSTIC_IDS = [ "env-missing", diff --git a/test/cli-doctor-env-file.test.ts b/test/cli-doctor-env-file.test.ts index c2656123ea..0b1eee033b 100644 --- a/test/cli-doctor-env-file.test.ts +++ b/test/cli-doctor-env-file.test.ts @@ -17,9 +17,9 @@ afterEach(() => { rmSync(home, { recursive: true, force: true }); }); -function doctorDryRun(extraEnv: Record): Promise { +function doctorDryRun(extraEnv: Record, args: string[] = ["--dry-run"]): Promise { return new Promise((done, reject) => { - const child = spawn(process.execPath, [resolve("dist/cli.mjs"), "doctor", "--dry-run"], { + const child = spawn(process.execPath, [resolve("dist/cli.mjs"), "doctor", ...args], { env: { PATH: process.env.PATH, HOME: home, @@ -48,6 +48,14 @@ it("reads provider keys from AGENTMEMORY_ENV_FILE, the file the daemon loads", a expect(output).not.toContain("[no-llm-provider-key]"); }); +it("never shows the Fix/Skip prompt when CI is set", async () => { + rmSync(join(home, ".agentmemory", ".env")); + const output = await doctorDryRun({}, []); + expect(output).toContain("env-missing"); + expect(output).not.toContain("Fix"); + expect(output).toContain("agentmemory doctor --all"); +}); + it("reports no provider key when only the default env file is read", async () => { const output = await doctorDryRun({}); expect(output).toContain("Dry-run complete"); diff --git a/test/cli-doctor-fixes.test.ts b/test/cli-doctor-fixes.test.ts index 333d19859c..ab0ff59c13 100644 --- a/test/cli-doctor-fixes.test.ts +++ b/test/cli-doctor-fixes.test.ts @@ -10,6 +10,7 @@ import { III_PINNED_VERSION } from "../src/version.js"; import { buildDiagnostics, canAutoFix, + doctorPromptsAllowed, DIAGNOSTIC_IDS, dryRunPlan, parseEnvFile, @@ -508,3 +509,13 @@ describe("realProviderKeys / placeholderProviderKeys", () => { ]); }); }); + +describe("doctor prompts", () => { + it("prompts only in an interactive terminal without CI", () => { + expect(doctorPromptsAllowed({}, true, true)).toBe(true); + expect(doctorPromptsAllowed({ CI: "1" }, true, true)).toBe(false); + expect(doctorPromptsAllowed({}, false, true)).toBe(false); + expect(doctorPromptsAllowed({}, true, false)).toBe(false); + expect(doctorPromptsAllowed({}, undefined, undefined)).toBe(false); + }); +});