Skip to content

fix(cli): keep stdout parseable in JSON mode - #257

Merged
dbfx merged 2 commits into
AdventDevInc:mainfrom
evolv3ai:fix/cli-json-stdout
Jul 27, 2026
Merged

fix(cli): keep stdout parseable in JSON mode#257
dbfx merged 2 commits into
AdventDevInc:mainfrom
evolv3ai:fix/cli-json-stdout

Conversation

@evolv3ai

Copy link
Copy Markdown
Contributor

What does this PR do?

Routes CLI progress output to stderr when --json is passed, so stdout contains nothing but the JSON document.

Why?

--json is documented as "Output results as JSON instead of human-readable text", but several commands print a progress line to stdout ahead of the JSON, which breaks any consumer that pipes stdout into a parser:

$ kudu --cli programs list --json
Loading installed programs...
{
  "programs": [ ...

$ kudu --cli network scan --json
Scanning network...
{
  "items": [ ...

The behaviour was inconsistent, which is what suggested an oversight rather than a deliberate choice — perf info and startup list already emitted clean JSON, while programs list and network scan did not.

cliLog() and cliVerbose() are fixed at the source instead of at their ~116 call sites, so every command benefits and no future call site can reintroduce it. Progress isn't lost — it moves to stderr, so it still shows in a terminal and can be captured (2>progress.log) or dropped (2>/dev/null).

showProgress() already suppressed carriage-return progress in JSON mode, so that path was unaffected.

How to test

npm install && npm run build

# Before: fails to parse. After: prints a count.
npx electron . --cli programs list --json | jq '.count'
npx electron . --cli network scan --json | jq '.count'

# Progress is preserved on stderr:
npx electron . --cli programs list --json 2>&1 >/dev/null
# → Loading installed programs...

# Human mode is unchanged:
npx electron . --cli programs list

Unit tests cover both streams across JSON/human/quiet modes in src/main/cli.test.ts.

Checklist

  • Tested on my platform (Windows / macOS / Linux) — verified on Windows 11
  • npm test passes — 2167 tests, 93 files
  • npm run build succeeds
  • Commit messages follow Conventional Commits

🤖 Generated with Claude Code

`--json` is documented as machine-readable output, but some commands wrote
human-facing progress text to stdout ahead of the JSON document, so piping
the output into a parser failed:

    $ kudu --cli programs list --json
    Loading installed programs...
    { "programs": [ ...

    $ kudu --cli network scan --json
    Scanning network...
    { "items": [ ...

The behaviour was inconsistent — `perf info` and `startup list` emitted clean
JSON while `programs list` and `network scan` did not.

Route `cliLog()` and `cliVerbose()` to stderr when `ctx.json` is set. Progress
is preserved (visible on stderr, or redirectable with `2>/dev/null`) while
stdout becomes a single parseable JSON document. Both helpers are fixed at the
source rather than at their ~116 call sites, so every command benefits.

`showProgress()` already suppressed carriage-return progress in JSON mode, so
that path was unaffected.

Adds tests covering both streams in JSON and human modes, and documents the
stdout/stderr split in CLI.md.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
evolv3ai added a commit to evolv3ai/kudu that referenced this pull request Jul 26, 2026
CLI.md documented three commands (scan, clean, metrics) while the CLI
dispatcher exposes twenty command groups. `kudu --cli --help` was already
accurate; only the Markdown had gone stale.

Rewrites CLI.md against the actual command surface, verified empirically on
Windows 11:

- All twenty command groups with subcommands, grouped by purpose
  (inventory/diagnostics, maintenance, security, config).
- Per-command annotations for what works without elevation, and which
  commands are long-running filesystem walks.
- The graceful-degradation contract: privileged operations return
  `needsAdmin: true` with exit code 0, so scripts must check the flag rather
  than infer from exit status.
- Elevation behaviour, including that packaged Windows builds force UAC via
  `requestedExecutionLevel: requireAdministrator` while running from source
  does not — the code path for unelevated operation is fully supported.
- That `network scan` reports entry counts rather than entries, and the
  platform layer's `getEstablishedConnections()` is not CLI-reachable.
- That `metrics-server` binds 0.0.0.0 and needs no cloud API key, unlike
  `--daemon`.

Fork-specific: the unelevated verification tables and source-route invocation
notes reflect this fork's use as a system-data source for local agent
tooling. Upstream received only the narrow stdout/stderr fix (AdventDevInc#257); this
broader rewrite is deliberately kept here.

Claims that could not be verified are marked as such rather than asserted:
`malware scan` was not run to completion, and `disk analyze`/`disk file-types`
exceeded a 180s timeout on a 2TB volume.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

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

Copy link
Copy Markdown

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: 37b7db819c

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

Comment thread CLI.md
The --json stdout-purity guarantee didn't hold for `malware scan --json`:
_initYaraEngine() and the engine's platform-skip line logged `[yara] ...`
via console.log (stdout) before the JSON result, breaking `| jq`. Route
both to stderr (console.error), matching the other [yara] diagnostics.
@dbfx

dbfx commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Thank you @evolv3ai !

@dbfx
dbfx merged commit 0af251b into AdventDevInc:main Jul 27, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants