Skip to content

fix(doctor): report an unparseable roles manifest, and keep --json stdout parseable - #1898

Merged
SaulMoro merged 1 commit into
Tencent:mainfrom
Walternater:fix/roles-manifest-json
Nov 15, 2026
Merged

SaulMoro merged 1 commit into
Tencent:mainfrom
Walternater:fix/roles-manifest-json

Conversation

@Walternater

Copy link
Copy Markdown
Collaborator

Summary

Fixes both bugs in #1885. They share a trigger — a manifest/roles.yaml that does not parse — and have separate causes, so fixing one alone still leaves the other broken.

  1. doctor stopped with the manifest's error, and doctor --json printed nothing. buildRulesDeliveryChecks and buildInstructionDeliveryChecks called buildRolePullContext unguarded; the throw escaped buildChecks before the report was written, so the --json caller got 0 bytes. Both now return the unresolvableCheck the skills and agents builders already use, so the manifest's error lands in a failed check's fix and doctor still exits 1. pull is unchanged — it must fail closed rather than deliver namespaces the manifest gates.
  2. Every other --json command printed the load warning on stdout ahead of its JSON. list, digest, recall maintenance --update-quality and codebase --lint each set stderr-only themselves, if at all, and a log.warn added to shared code later would leak through another command. The program's preAction hook now sets it once for any command whose --json is true — first in the hook, so the config load and the auto-migration under it are covered. --json <payload> takes a string, not true, so workflow claim/report/answer are untouched.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Test plan

Proof is a failing test first in both places (AGENTS.md "Proof for a bug fix is a failing test first").

Bug 1 — extended the existing doctor-delivery.test.ts, which already owns the delivered-resources checks:

  • On main: npx vitest run src/__tests__/doctor-delivery.test.ts -t "roles manifest that does not parse" → Error: Invalid roles manifest: roles must be a non-empty array thrown from doctor.ts:996.
  • Guarding only the rules builder is not enough: the test then throws from buildInstructionDeliveryChecks, exactly as the issue predicted. With both guards it passes, and the three … to deliver can be resolved checks each carry the manifest's error.

Bug 2 — new src/__tests__/e2e/json-stdout-contract-1885.test.ts, real CLI in a self-mode project with roles: 5:

  • list --json, digest --json, codebase --lint --json: stdout starts with { and parses; the warning is on stderr.
  • doctor --json: writes a report with ok: false and a failed check naming the manifest, exit 1.
  • Negative control: removing the preAction line and rebuilding turns 3 of 4 red (doctor stays green because it sets stderr-only itself — which is the point: each command had to remember). Restoring it returns 4/4.

Real CLI in a verify world (20261115T172253-4b1848, build 43be4ea7):

doctor --json    → exit 1, stdout starts {"ok": false, "scope": "project", …}, stderr holds the warning
list --json      → first char { , warning on stderr (1 line)
digest --json    → first char { , warning on stderr (1 line)
codebase --lint --json → first char { , warning on stderr (2 lines)

npx tsc --noEmit passes. npm run lint passes (oxlint type-aware + documentation checks). doctor-delivery + doctor suites: 164/164.

The ledger on that world reports one LEAK, and it is not this change: ~/.claude/plugins/claude-hud/transcript-cache/… (a plugin of the shell I ran in) names the world, which is the host-file check doing its job, not teamai writing outside the world. Everything else under (e) is the known #1808 set.

Hit every surface

  • Tools: none of these checks are per-tool; the roles manifest gates skills, rules, agents, instructions and docs regardless of tool.
  • Scopes: user and project. The guard is in buildChecks, so both scopes take the same path; the world ran project scope.
  • Team repo modes: git in the world. http builds no roles context through these two builders, and self mode uses the same git loaders.
  • OS: macOS local + Linux and Windows CI. No path arithmetic changed.
  • Reverse states: nothing is delivered or removed differently; only what doctor reports and where warnings go.

Notes for reviewers

  • I did not remove the now-redundant per-command setStderrOnly(true) calls in doctor, roles list, session migrate, session list and session show. They are harmless and the issue suggests removing them separately, which keeps this diff to the two causes.
  • The class does not close here: a third builder could repeat the unguarded call, and a third --json path could bypass the hook. buildChecks catching any builder's throw as one failed check named for that builder is the structural fix the issue points at; that is a larger change than this bug.

Disclosure

Implemented with AI assistance (Claude Code); every line reviewed and tested by the author.

…dout parseable

Two bugs, one trigger, and either alone still leaves the other broken (Tencent#1885).

`doctor` stopped with the manifest's error. `buildRulesDeliveryChecks` and
`buildInstructionDeliveryChecks` called `buildRolePullContext` unguarded, and
the throw escaped `buildChecks` before the report was written, so
`doctor --json` printed nothing at all. Both now return the
`unresolvableCheck` the skills and agents builders already use, so the
manifest's error lands in a failed check's `fix` and the command still exits 1.
`pull` is unchanged: it must fail closed rather than deliver namespaces the
manifest gates.

Every other `--json` command printed the load warning on stdout ahead of its
JSON. `list`, `digest`, `recall maintenance --update-quality` and
`codebase --lint` each set stderr-only themselves, if at all, and a later
`log.warn` in shared code adds another leaking command. The program's
`preAction` hook now sets it once for any command whose `--json` is `true` —
first, so the config load and the auto-migration under it are covered.
`--json <payload>` takes a string, so `workflow claim/report/answer` are
untouched.

Co-Authored-By: Claude Code <[email protected]>
@github-actions

Copy link
Copy Markdown
  • [P1 blocking] The new E2E fixture inherits tool-home overrides outside its sandbox — src/__tests__/e2e/json-stdout-contract-1885.test.ts:41 spreads process.env but only replaces HOME/USERPROFILE. If CLAUDE_CONFIG_DIR points at the real Claude configuration, the fixture’s init --self --agent claude can write there instead of under the temporary home—the exact isolation hazard documented in AGENTS.md. Start from a sanitized environment and explicitly sandbox tool homes.

The PR description includes a representative real-CLI verify-world record, so the runtime-testing requirement is satisfied. No other confident findings in the scoped diff.

@SaulMoro SaulMoro left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This was generated by AI during triage.

Approving at 43be4ea.

What I checked

  • Red on main, green here. With main's doctor-delivery.ts and index.ts, the new doctor-delivery.test.ts case throws Invalid roles manifest: roles must be a non-empty array, and all four cases of json-stdout-contract-1885.test.ts fail against a build of main. With this head and a fresh build, doctor-delivery.test.ts passes (71), the E2E file passes (4), and typecheck passes.
  • Both causes are fixed where they start. The rules and instructions builders now return unresolvableCheck as the skills and agents builders already do, so doctor --json writes a report and pull still fails closed. The preAction hook sets stderr-only before the config load for any boolean --json, and the --json <payload> commands are left alone because their value is a string.

CI review: we disagree with its P1. The E2E file spreads process.env, but the E2E setup has already removed every tool-root variable from it by then. vitest.e2e.config.ts seeds CLAUDE_CONFIG_DIR, CODEX_HOME and the Cursor ones, and clear-agent-session-env.ts deletes each of toolRootVariables() before any test file runs. isolate-e2e-env.ts points HOME at a sandbox as well. So init --self --agent claude can't reach a real Claude config from this fixture, and the many other E2E files that spread process.env rely on the same setup.

[P3 nit]

  • The new E2E file is named for the issue (json-stdout-contract-1885). A name for the contract, such as json-stdout-contract.test.ts, would let the next --json case join it instead of starting another file.

Merge risk. Low. Doctor reports two more builders' errors as failed checks instead of stopping, and every boolean --json command now logs to stderr. Pull and the payload-taking --json commands are unchanged.

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.

3 participants