Skip to content

feat(cli): add --json output to kimi doctor#445

Closed
RTCartist wants to merge 3 commits into
MoonshotAI:mainfrom
RTCartist:feat/doctor-json-output
Closed

feat(cli): add --json output to kimi doctor#445
RTCartist wants to merge 3 commits into
MoonshotAI:mainfrom
RTCartist:feat/doctor-json-output

Conversation

@RTCartist
Copy link
Copy Markdown

@RTCartist RTCartist commented Jun 4, 2026

Related Issue

Resolve #53

Problem

kimi doctor currently prints a human-formatted block. Bug-report templates and CI pipelines want a stable machine-readable shape, and codex doctor --json (referenced in the issue) is the prior art.

What changed

Add --json to kimi doctor, kimi doctor config, and kimi doctor tui. The flag is declared once on the parent doctor command and read via cmd.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 synchronous process.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 of report_version: 1.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

Verification

  • pnpm -C apps/kimi-code exec vitest run test/cli/doctor.test.ts

RTCartist added 2 commits June 5, 2026 01:41
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-bot
Copy link
Copy Markdown

changeset-bot Bot commented Jun 4, 2026

🦋 Changeset detected

Latest commit: c24b341

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Minor

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

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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).
@RTCartist
Copy link
Copy Markdown
Author

Good catch. Added a setExitCode dep that defaults to process.exitCode = code and routed JSON-mode failures through it instead of the synchronous process.exit. The event loop now drains stdout naturally before the process terminates — no risk of truncated JSON when piped to jq.

Also added a regression test asserting the synchronous exit is never called for JSON-mode failures (only the deferred setExitCode is). Fixed in c24b341.

@RTCartist
Copy link
Copy Markdown
Author

@codex review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

ℹ️ 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".

@liruifengv
Copy link
Copy Markdown
Collaborator

Sorry, we currently do not accept PRs submitted in batches using AI

@liruifengv liruifengv closed this Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: 增加 kimi doctor --json 本地诊断命令

2 participants