feat(cli): add --json output to kimi doctor#445
Conversation
Add a `--json` flag to `kimi doctor`, `kimi doctor config`, and
`kimi doctor tui` that emits a stable machine-readable report
instead of the human-formatted text. Useful for embedding doctor
output in bug-report templates and CI pipelines.
The flag is declared once on the parent `doctor` command and read
via `cmd.optsWithGlobals()` from every subcommand action so users
can write either `doctor --json` or `doctor config --json`.
Report shape:
{
"report_version": 1,
"ok": boolean,
"issue_count": number,
"results": [{ "label", "path", "status", "message"? }, ...]
}
JSON mode always writes to stdout (even on validation failure) so
callers can pipe through `jq` without juggling stderr; the exit
code conveys success/failure.
Closes MoonshotAI#53
🦋 Changeset detectedLatest commit: c24b341 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 710141613e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (options.json === true) { | ||
| // JSON mode always writes to stdout regardless of exit code so callers can | ||
| // pipe through `jq` without juggling stderr — the exit code conveys failure. | ||
| resolved.stdout.write(formatJson(results, issueCount)); |
There was a problem hiding this comment.
Avoid truncating JSON before exiting
When a JSON doctor run finds an error, this writes the report to stdout and returns 1, after which runDoctorCommand immediately calls resolved.exit(code) (the default is process.exit). On Linux/CI when stdout is a pipe, process.stdout.write() can still be pending, so kimi doctor --json | jq may receive empty or truncated JSON exactly in the failure case this mode is meant to support. Consider setting process.exitCode or waiting for the write/drain before exiting nonzero.
Useful? React with 👍 / 👎.
`process.exit()` called immediately after `stdout.write(...)` can truncate the pending pipe write on Linux/CI — exactly when the JSON report describes a failure. Add a `setExitCode` dep that defaults to `process.exitCode = code` and use it instead of synchronous exit when `--json` is set, so the event loop drains stdout naturally before termination. Adds a regression test that asserts the synchronous `exit` is never called for JSON-mode failures (only `setExitCode` is).
|
Good catch. Added a Also added a regression test asserting the synchronous |
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Sorry, we currently do not accept PRs submitted in batches using AI |
Related Issue
Resolve #53
Problem
kimi doctorcurrently prints a human-formatted block. Bug-report templates and CI pipelines want a stable machine-readable shape, andcodex doctor --json(referenced in the issue) is the prior art.What changed
Add
--jsontokimi doctor,kimi doctor config, andkimi doctor tui. The flag is declared once on the parentdoctorcommand and read viacmd.optsWithGlobals()from every subcommand action so users can pass it after the root or after a subcommand. Output shape:{ "report_version": 1, "ok": true, "issue_count": 0, "results": [{ "label": "config.toml", "path": "...", "status": "OK" }] }JSON-mode failures defer exit via
process.exitCode(not synchronousprocess.exit) so the event loop drains stdout before termination — addresses a codex review concern that pipe writes could truncate the report. Scope note: this implements the foundation (config validation as JSON); the broader diagnostic surface area listed in the issue (env, MCP, terminal, network) can land incrementally on top ofreport_version: 1.Checklist
gen-changesetsskill, or this PR needs no changeset.gen-docsskill, or this PR needs no doc update.Verification
pnpm -C apps/kimi-code exec vitest run test/cli/doctor.test.ts