Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/cli.ts
Comment thread
rohitg00 marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { generateId } from "./state/schema.js";
import {
buildDiagnostics,
canAutoFix,
doctorPromptsAllowed,
dryRunPlan,
parseEnvFile,
type Diagnostic,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/cli/doctor-diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 10 additions & 2 deletions test/cli-doctor-env-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ afterEach(() => {
rmSync(home, { recursive: true, force: true });
});

function doctorDryRun(extraEnv: Record<string, string>): Promise<string> {
function doctorDryRun(extraEnv: Record<string, string>, args: string[] = ["--dry-run"]): Promise<string> {
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,
Expand Down Expand Up @@ -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");
Expand Down
11 changes: 11 additions & 0 deletions test/cli-doctor-fixes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { III_PINNED_VERSION } from "../src/version.js";
import {
buildDiagnostics,
canAutoFix,
doctorPromptsAllowed,
DIAGNOSTIC_IDS,
dryRunPlan,
parseEnvFile,
Expand Down Expand Up @@ -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);
});
});
Loading