Skip to content

Commit 6093e34

Browse files
authored
fix(cli): make doctor --json imply read-only lint mode (#122662)
* fix(cli): make doctor json imply lint * fix(cli): preserve doctor session selector errors
1 parent 44335ac commit 6093e34

5 files changed

Lines changed: 104 additions & 22 deletions

File tree

‎docs/cli/doctor.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,18 @@ Doctor has five postures:
3232
| ------------------------- | ----------------------------------------- | ------------------------------------------------------------------------------- |
3333
| Inspect | `openclaw doctor` | Human-oriented checks and guided prompts. |
3434
| Repair | `openclaw doctor --fix` | Applies supported repairs, using prompts unless non-interactive repair is safe. |
35-
| Lint | `openclaw doctor --lint` | Read-only structured findings for CI, preflight, and review gates. |
35+
| Lint | `openclaw doctor --json` | Read-only JSON findings for deployment preflight and CI gates. |
3636
| Shared SQLite maintenance | `openclaw doctor --state-sqlite compact` | Explicitly checkpoints, compacts, and verifies the canonical shared state DB. |
3737
| Session SQLite migration | `openclaw doctor --session-sqlite <mode>` | Inspects, imports, validates, compacts, recovers, or restores session state. |
3838

39-
Prefer `--lint` when automation needs a stable result. Prefer `--fix` when a human operator wants doctor to edit config or state.
39+
Use `openclaw doctor --json` as the machine-readable deployment preflight. It runs the same read-only checks, JSON output, and exit codes as `openclaw doctor --lint --json`. Prefer `--fix` when a human operator wants doctor to edit config or state.
4040

4141
## Examples
4242

4343
```bash
4444
openclaw doctor
4545
openclaw doctor --lint
46+
openclaw doctor --json
4647
openclaw doctor --lint --json
4748
openclaw doctor --lint --severity-min warning
4849
openclaw doctor --lint --all
@@ -93,19 +94,20 @@ openclaw channels status --probe
9394
| `--session-sqlite-agent <id>` | With `--session-sqlite`: select one configured agent. |
9495
| `--session-sqlite-all-agents` | With `--session-sqlite`: select configured and discovered agent stores. |
9596
| `--github-issue` | With `--session-sqlite recover`: prepare a sanitized openclaw/openclaw issue report; doctor creates it with `gh` after `--yes` or interactive confirmation. |
96-
| `--json` | With `--lint`: JSON findings. With `--post-upgrade`: `{ probesRun, findings }`. With `--state-sqlite` or `--session-sqlite`: the maintenance report as JSON. |
97+
| `--json` | Run lint checks in read-only mode and emit JSON. With another machine mode, emit that mode's existing JSON report. |
9798
| `--severity-min <level>` | With `--lint`: drop findings below `info`, `warning`, or `error`. |
9899
| `--all` | With `--lint`: run all registered checks, including opt-in checks excluded from the default set. |
99100
| `--skip <id>` | With `--lint`: skip a check id. Repeatable. |
100101
| `--only <id>` | With `--lint`: run only the given check id(s). Repeatable. |
101102

102-
`--severity-min`, `--all`, `--only`, and `--skip` are only accepted together with `--lint`; `--json` is accepted with `--lint`, `--post-upgrade`, `--state-sqlite`, and `--session-sqlite`.
103+
`--severity-min`, `--all`, `--only`, and `--skip` are only accepted together with `--lint`. Bare `--json` implies lint mode. It cannot be combined with `--repair`, `--fix`, or `--force` unless another machine mode owns the command.
103104

104105
## Lint mode
105106

106-
`openclaw doctor --lint` is read-only: no prompts, no repair, no config/state rewrites.
107+
`openclaw doctor --json` is the deployment-preflight form of lint mode. It is read-only and non-interactive: no prompts, repairs, or config/state rewrites. `openclaw doctor --lint --json` remains an equivalent explicit spelling.
107108

108109
```bash
110+
openclaw doctor --json
109111
openclaw doctor --lint
110112
openclaw doctor --lint --severity-min warning
111113
openclaw doctor --lint --json

‎src/cli/doctor-output-mode.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import type { MachineOutputResolverParams } from "./machine-output-argv.js";
22
import { hasMachineOutputOption } from "./machine-output-argv.js";
33

4-
/** Doctor lint follows Unix convention and emits JSON when stdout is not a terminal. */
4+
/** Bare doctor JSON and non-TTY lint runs own machine-readable stdout. */
55
export function isDoctorMachineOutput(params: MachineOutputResolverParams): boolean {
6-
return (
7-
hasMachineOutputOption(params.argv, "--lint") &&
8-
(hasMachineOutputOption(params.argv, "--json") || !params.stdoutIsTTY)
9-
);
6+
const lint = hasMachineOutputOption(params.argv, "--lint");
7+
if (lint) {
8+
return hasMachineOutputOption(params.argv, "--json") || !params.stdoutIsTTY;
9+
}
10+
const existingMachineMode =
11+
hasMachineOutputOption(params.argv, "--post-upgrade") ||
12+
hasMachineOutputOption(params.argv, "--state-sqlite") ||
13+
hasMachineOutputOption(params.argv, "--session-sqlite");
14+
return hasMachineOutputOption(params.argv, "--json") && !existingMachineMode;
1015
}

‎src/cli/machine-output-modes.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ describe("built-in machine-output resolvers", () => {
3030
expect(isDoctorMachineOutput({ argv, stdoutIsTTY: true })).toBe(false);
3131
});
3232

33+
it("reserves doctor JSON output with or without explicit lint mode", () => {
34+
for (const argv of [
35+
["node", "openclaw", "doctor", "--json"],
36+
["node", "openclaw", "doctor", "--lint", "--json"],
37+
]) {
38+
expect(isDoctorMachineOutput({ argv, stdoutIsTTY: true })).toBe(true);
39+
}
40+
});
41+
42+
it.each(["--post-upgrade", "--state-sqlite=compact", "--session-sqlite=dry-run"])(
43+
"preserves registered-command JSON handling for doctor %s",
44+
(mode) => {
45+
const argv = ["node", "openclaw", "doctor", mode, "--json"];
46+
expect(isDoctorMachineOutput({ argv, stdoutIsTTY: true })).toBe(false);
47+
},
48+
);
49+
3350
it.each(["blob", "coverage", "purge", "query", "sessions"])(
3451
"detects proxy %s output",
3552
(command) => {

‎src/cli/program/register.maintenance.test.ts‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,14 @@ describe("registerMaintenanceCommands doctor action", () => {
231231
expect(runtime.exit).toHaveBeenCalledWith(2);
232232
});
233233

234-
it("rejects session sqlite selectors without session sqlite mode", async () => {
235-
await runMaintenanceCli(["doctor", "--session-sqlite-agent", "main"]);
234+
it.each([
235+
["without JSON", ["--session-sqlite-agent", "main"]],
236+
["with JSON", ["--json", "--session-sqlite-agent", "main"]],
237+
])("rejects session sqlite selectors without session sqlite mode %s", async (_label, args) => {
238+
await runMaintenanceCli(["doctor", ...args]);
236239

237240
expect(doctorCommand).not.toHaveBeenCalled();
241+
expect(runDoctorLintCli).not.toHaveBeenCalled();
238242
expect(runtime.error).toHaveBeenCalledWith(
239243
"doctor session SQLite options require --session-sqlite. Use `openclaw doctor --session-sqlite dry-run ...`.",
240244
);
@@ -271,6 +275,54 @@ describe("registerMaintenanceCommands doctor action", () => {
271275
expect(runtime.exit).toHaveBeenCalledWith(1);
272276
});
273277

278+
it("treats bare --json as lint mode and emits machine-readable output", async () => {
279+
const output: string[] = [];
280+
const writeSpy = vi.spyOn(process.stdout, "write").mockImplementation((chunk) => {
281+
output.push(String(chunk));
282+
return true;
283+
});
284+
runDoctorLintCli.mockImplementationOnce(async () => {
285+
process.stdout.write('{"ok":true,"checksRun":1,"checksSkipped":0,"findings":[]}\n');
286+
return 0;
287+
});
288+
289+
try {
290+
await runMaintenanceCli(["doctor", "--json"]);
291+
292+
expect(doctorCommand).not.toHaveBeenCalled();
293+
expect(runDoctorLintCli).toHaveBeenCalledWith(runtime, {
294+
json: true,
295+
severityMin: undefined,
296+
includeAllChecks: false,
297+
skipIds: [],
298+
onlyIds: [],
299+
allowExec: false,
300+
deep: false,
301+
});
302+
expect(JSON.parse(output.join(""))).toEqual({
303+
ok: true,
304+
checksRun: 1,
305+
checksSkipped: 0,
306+
findings: [],
307+
});
308+
expect(runtime.exit).toHaveBeenCalledWith(0);
309+
} finally {
310+
writeSpy.mockRestore();
311+
runDoctorLintCli.mockReset();
312+
}
313+
});
314+
315+
it("rejects JSON repair mode before running doctor", async () => {
316+
await runMaintenanceCli(["doctor", "--json", "--repair"]);
317+
318+
expect(doctorCommand).not.toHaveBeenCalled();
319+
expect(runDoctorLintCli).not.toHaveBeenCalled();
320+
expect(runtime.error).toHaveBeenCalledWith(
321+
"doctor --json runs read-only lint checks and cannot be combined with --repair, --fix, or --force.",
322+
);
323+
expect(runtime.exit).toHaveBeenCalledWith(2);
324+
});
325+
274326
it("rejects lint selectors outside doctor lint mode", async () => {
275327
await runMaintenanceCli(["doctor", "--fix", "--only", "policy/channels-denied-provider"]);
276328

‎src/cli/program/register.maintenance.ts‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export function registerMaintenanceCommands(program: Command) {
7979
)
8080
.option(
8181
"--json",
82-
"With --lint, --post-upgrade, --state-sqlite, or --session-sqlite: emit machine-readable JSON output",
82+
"Run read-only lint checks as JSON (or emit JSON for another machine mode)",
8383
false,
8484
)
8585
.option(
@@ -110,7 +110,21 @@ export function registerMaintenanceCommands(program: Command) {
110110
defaultRuntime.exit(2);
111111
return;
112112
}
113-
if (opts.lint === true) {
113+
const jsonImpliesLint =
114+
opts.json === true &&
115+
opts.lint !== true &&
116+
opts.postUpgrade !== true &&
117+
typeof opts.stateSqlite !== "string" &&
118+
typeof opts.sessionSqlite !== "string" &&
119+
!hasSessionSqliteOnlyDoctorOptions(opts);
120+
if (jsonImpliesLint && (opts.repair === true || opts.fix === true || opts.force === true)) {
121+
defaultRuntime.error(
122+
"doctor --json runs read-only lint checks and cannot be combined with --repair, --fix, or --force.",
123+
);
124+
defaultRuntime.exit(2);
125+
return;
126+
}
127+
if (opts.lint === true || jsonImpliesLint) {
114128
await runCommandWithRuntime(
115129
defaultRuntime,
116130
async () => {
@@ -258,20 +272,12 @@ export function registerMaintenanceCommands(program: Command) {
258272
}
259273

260274
function hasLintOnlyDoctorOptions(opts: {
261-
readonly json?: boolean;
262-
readonly postUpgrade?: boolean;
263-
readonly stateSqlite?: unknown;
264-
readonly sessionSqlite?: unknown;
265275
readonly severityMin?: unknown;
266276
readonly all?: boolean;
267277
readonly skip?: unknown;
268278
readonly only?: unknown;
269279
}): boolean {
270280
return (
271-
(opts.json === true &&
272-
opts.postUpgrade !== true &&
273-
typeof opts.stateSqlite !== "string" &&
274-
typeof opts.sessionSqlite !== "string") ||
275281
typeof opts.severityMin === "string" ||
276282
opts.all === true ||
277283
(Array.isArray(opts.skip) && opts.skip.length > 0) ||

0 commit comments

Comments
 (0)