Skip to content

fix(patchy): a flag error under --json leaves stdout empty - #1073

Merged
allisonmahmood merged 4 commits into
mainfrom
fix/1072-json-parse-error-stdout
Oct 30, 2026
Merged

allisonmahmood merged 4 commits into
mainfrom
fix/1072-json-parse-error-stdout

Conversation

@allisonmahmood

@allisonmahmood allisonmahmood commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

What changed

When an agent passed a bad flag with --json, patchy printed the command's help to stdout before the JSON failure on stderr, so stdout held usage text where an agent expects nothing or a result. A parse error under --json now leaves stdout empty: one failure document on stderr, exit 1. Help that is asked for still prints, with or without --json.

Closes #1072

Look at

Where the help came from. On a parse error, Effect 4.0.5's Command.runWith logs the help doc with Console.log, then the errors with Console.error (showHelp in effect/cli/Command.ts). An explicit --help goes through the same formatHelpDoc + Console.log, so the formatter alone can't tell the two apart. formatErrors has one caller, the invalid-invocation branch, which makes it the signal.

The fix, in packages/patchy/src/index.ts. Under --json only:

  • formatHelpDoc holds the help and returns an empty string. A console wrapper skips that lone empty line.
  • formatErrors drops the held help.
  • When the run ends, any help still held is printed. That covers --help, -h, and commands without a handler such as bare patchy or patchy auth. The output is byte-identical to before.

Without --json, a command's own parse errors still print usage, then the one line.

Two more paths review found.

  • A global flag's own bad value. For example --log-level loud or --completions nope. These fail with a raw InvalidValue outside Effect's usage path, so the runtime printed an error and stack on stdout, with or without --json. The entrypoint now catches that tag and writes it through the same renderer: one line, or one document under --json. It exits 1 through Output.Failed, which is now exported.
  • How --json is read before parsing. It was argv.includes("--json"). Now jsonRequested, beside JsonFlag in Output.ts, reads it the way Effect does: the first --json, --json=<b>, -json=<b>, --json <b> or --no-json before -- decides, reading past a --no-json <b> that Effect refuses.

Docs. ADR-0004's "Parse errors" bullet now states the contract instead of the exception. packages/patchy/README.md says the same in one line. The global patchy skill drops its "usage on stdout" warning, since its general failure contract now covers parse errors, and skills-lock.json carries the new hash.

Later. Effect 4.0.7 adds CliOutput.Presenter, which receives an InvalidInvocation separately from Help. Once the Effect bump (#974, then 4.0.7) lands, about ten lines of Presenter replace the held-help state. The comment in index.ts says so.

Transcript json-parse-errors

Before is e778152 (where this branch left main) and after is f1841dd (fix/1072-json-parse-error-stdout), both on the team scenario, from pnpm dev transcript.

Each line ran in its own bash -c from the side's agent workspace, as the scenario's signed-in CLI, with stdin closed. Each side's origin reads <origin>, its environment folder <env>, its checkout <checkout> and the home folder ~; nothing else is normalized. Nothing in it matched a collected secret (each side's CLI state, machine-token and login-code shapes, email addresses outside the scenarios' example domains, secret-named variables, and any accounts the run acts as).

7 of 10 lines changed.

Under --json, a bad flag value: stdout

Changed. Before, exit 1 in 0.2 s; after, exit 1 in 0.2 s.

$ patchy init notes --tier 7 --json 2>/dev/null
-DESCRIPTION
-  Create a patch repo at tier 1 by default, or tier 2 with server handlers.
-
-USAGE
-  patchy init [flags] [<dir>]
-
-ARGUMENTS
-  dir string    Directory for the new repo; defaults to the current directory (optional)
-
-FLAGS
-  --tier choice       Runtime tier for the new repo; defaults to 1 (choices: 1, 2)
-  --purpose string    What this patch is for; required for agent, --json and non-terminal calls; otherwise prompted
-
-GLOBAL FLAGS
-  --help, -h                                                          Show help information
-  --version, -v                                                       Show version information
-  --wizard                                                            Start wizard mode for a command
-  --completions <bash|zsh|fish|powershell|sh|pwsh>                    Print shell completion script (choices: bash, zsh, fish, powershell, sh, pwsh)
-  --log-level <all|trace|debug|info|warn|warning|error|fatal|none>    Sets the minimum log level (choices: all, trace, debug, info, warn, warning, error, fatal, none)
-  --json                                                              Print the result as one JSON document on stdout
-  --api-url string                                                    The Patchy Cloud instance to talk to (its API base URL)
...and stderr, with its exit code
$ patchy init notes --tier 7 --json 2>&1 >/dev/null: no change, exit 1 in 0.2 s
$ patchy init notes --tier 7 --json 2>&1 >/dev/null
{"ok":false,"error":"Invalid value for flag --tier: \"7\". Expected: \"1\" | \"2\"","kind":"local"}
An unknown flag and a missing required flag: stdout

Changed. Before, exit 1 in 0.2 s; after, exit 1 in 0.2 s.

$ patchy publish --bogus --json 2>/dev/null
-DESCRIPTION
-  Publish the current patch repo, or a single self-contained HTML file.
-
-USAGE
-  patchy publish [flags] [<file>]
-
-ARGUMENTS
-  file string    HTML file path (optional)
-
-FLAGS
-  --name string           Set the patch's name in its company
-  --patch string          Update an existing patch only; never creates a patch
-  --share choice          Who can open the patch: your company or anyone with the link (choices: company, public)
-  --description string    Set the description when publishing an HTML file
-  --force                 Accept the reported dependant or source breakage; ask the person first
-  --new                   Always create a new patch
-
-GLOBAL FLAGS
-  --help, -h                                                          Show help information
-  --version, -v                                                       Show version information
-  --wizard                                                            Start wizard mode for a command
-  --completions <bash|zsh|fish|powershell|sh|pwsh>                    Print shell completion script (choices: bash, zsh, fish, powershell, sh, pwsh)
-  --log-level <all|trace|debug|info|warn|warning|error|fatal|none>    Sets the minimum log level (choices: all, trace, debug, info, warn, warning, error, fatal, none)
-  --json                                                              Print the result as one JSON document on stdout
-  --api-url string                                                    The Patchy Cloud instance to talk to (its API base URL)

Changed. Before, exit 1 in 0.2 s; after, exit 1 in 0.2 s.

$ patchy look publish ./x --json 2>/dev/null
-DESCRIPTION
-  Admins: publish a look folder (look.css, LOOK.md, optional logo.svg) as the company's next revision.
-
-USAGE
-  patchy look publish [flags] <dir>
-
-ARGUMENTS
-  dir string    The look folder to publish
-
-FLAGS
-  --note string    Why this revision, such as darker green (required)
-
-GLOBAL FLAGS
-  --help, -h                                                          Show help information
-  --version, -v                                                       Show version information
-  --wizard                                                            Start wizard mode for a command
-  --completions <bash|zsh|fish|powershell|sh|pwsh>                    Print shell completion script (choices: bash, zsh, fish, powershell, sh, pwsh)
-  --log-level <all|trace|debug|info|warn|warning|error|fatal|none>    Sets the minimum log level (choices: all, trace, debug, info, warn, warning, error, fatal, none)
-  --json                                                              Print the result as one JSON document on stdout
-  --api-url string                                                    The Patchy Cloud instance to talk to (its API base URL)
Effect's other spellings of the flag count too

Changed. Before, exit 1 in 0.2 s; after, exit 1 in 0.2 s.

$ patchy init notes --tier 7 --json=true 2>/dev/null
-DESCRIPTION
-  Create a patch repo at tier 1 by default, or tier 2 with server handlers.
-
-USAGE
-  patchy init [flags] [<dir>]
-
-ARGUMENTS
-  dir string    Directory for the new repo; defaults to the current directory (optional)
-
-FLAGS
-  --tier choice       Runtime tier for the new repo; defaults to 1 (choices: 1, 2)
-  --purpose string    What this patch is for; required for agent, --json and non-terminal calls; otherwise prompted
-
-GLOBAL FLAGS
-  --help, -h                                                          Show help information
-  --version, -v                                                       Show version information
-  --wizard                                                            Start wizard mode for a command
-  --completions <bash|zsh|fish|powershell|sh|pwsh>                    Print shell completion script (choices: bash, zsh, fish, powershell, sh, pwsh)
-  --log-level <all|trace|debug|info|warn|warning|error|fatal|none>    Sets the minimum log level (choices: all, trace, debug, info, warn, warning, error, fatal, none)
-  --json                                                              Print the result as one JSON document on stdout
-  --api-url string                                                    The Patchy Cloud instance to talk to (its API base URL)

Changed. Before, exit 1 in 0.2 s; after, exit 1 in 0.2 s.

$ patchy init notes --tier 7 -json=true 2>/dev/null
-DESCRIPTION
-  Create a patch repo at tier 1 by default, or tier 2 with server handlers.
-
-USAGE
-  patchy init [flags] [<dir>]
-
-ARGUMENTS
-  dir string    Directory for the new repo; defaults to the current directory (optional)
-
-FLAGS
-  --tier choice       Runtime tier for the new repo; defaults to 1 (choices: 1, 2)
-  --purpose string    What this patch is for; required for agent, --json and non-terminal calls; otherwise prompted
-
-GLOBAL FLAGS
-  --help, -h                                                          Show help information
-  --version, -v                                                       Show version information
-  --wizard                                                            Start wizard mode for a command
-  --completions <bash|zsh|fish|powershell|sh|pwsh>                    Print shell completion script (choices: bash, zsh, fish, powershell, sh, pwsh)
-  --log-level <all|trace|debug|info|warn|warning|error|fatal|none>    Sets the minimum log level (choices: all, trace, debug, info, warn, warning, error, fatal, none)
-  --json                                                              Print the result as one JSON document on stdout
-  --api-url string                                                    The Patchy Cloud instance to talk to (its API base URL)
A global flag's bad value, with and without --json: stdout, then stderr

Changed. Before, exit 1 in 0.2 s; after, exit 1 in 0.2 s.

$ patchy whoami --log-level loud --json 2>/dev/null
-[00:51:45.914] ERROR (#1): ~effect/cli/CliError/InvalidValue: Invalid value for flag --log-level: "loud". Expected: "all" | "trace" | "debug" | "info" | "warn" | "warning" | "error" | "fatal" | "none"
-    at file://<env>/cli/node_modules/patchy/dist/index.js:63778:73
-    at file://<env>/cli/node_modules/patchy/dist/index.js:4003:95

Changed. Before, exit 1 in 0.2 s; after, exit 1 in 0.2 s.

$ patchy whoami --log-level loud 2>&1 >/dev/null
Invalid value for flag --log-level: "loud". Expected: "all" | "trace" | "debug" | "info" | "warn" | "warning" | "error" | "fatal" | "none"
+Invalid value for flag --log-level: "loud". Expected: "all" | "trace" | "debug" | "info" | "warn" | "warning" | "error" | "fatal" | "none"
Help asked for under --json still prints
$ patchy init --help --json | head -4: no change, exit 0 in 0.2 s
$ patchy init --help --json | head -4
DESCRIPTION
  Create a patch repo at tier 1 by default, or tier 2 with server handlers.

USAGE
Without --json, usage still prints before the one-line error
$ patchy init notes --tier 7 2>&1 | tail -4: no change, exit 0 in 0.2 s
$ patchy init notes --tier 7 2>&1 | tail -4
  --log-level <all|trace|debug|info|warn|warning|error|fatal|none>    Sets the minimum log level (choices: all, trace, debug, info, warn, warning, error, fatal, none)
  --json                                                              Print the result as one JSON document on stdout
  --api-url string                                                    The Patchy Cloud instance to talk to (its API base URL)
Invalid value for flag --tier: "7". Expected: "1" | "2"

How it was verified

  • The focused test. cli.test.ts › help › "under --json, a parse error is one document on stderr, and help still prints" covers:

    • a bad value, under --json, --json=true and -json=true
    • an unknown flag, a missing flag and a missing argument
    • --log-level loud

    Each must have empty stdout and exactly one document line on stderr. With --no-json, the text error and usage must be back, and --log-level loud without --json must be one stderr line. init --help --json and auth --json must print the same stdout as without --json. It fails against main's index.ts (21 lines of help on stdout) and passes with the fix.

  • The old assertion. cliRepoPublish.test.ts asserted the old help on stdout for init --tier 0 --json. It now asserts empty stdout.

  • Review. Three GPT 6.1 Sol (xhigh) passes. Every finding is fixed in the later commits: global flag values, the --json=true and -json=true spellings, a refused --no-json false before -json=true, and catchTag → catchTags.

  • Checks. pnpm check format, lint, typecheck and synth pass, and pnpm typecheck is 24/24. On this shared dev machine, offline CLI stub-server tests failed in a rotating set from the host's stray GET / probes. Each either passes when run alone or fails the same way with main's index.ts. CI decides.

Release impact

  • Breaking — requires action from anyone already running this
  • Not breaking — no action required

Under --json, a parse error's stdout loses the help text that nothing read as a result. Without --json, a global flag's bad value is one stderr line instead of a stack on stdout. Exit codes are unchanged.

  • What does a revert leave behind? Nothing: no schema, data or wire change.
  • Does the release before it still work with that? Yes.

Built by Claude Opus 5.5 (claude-opus-5-5) in Claude Code, through T3 Code.

On a parse error Effect logs the command's usage to stdout before the
errors, so an agent reading stdout under --json got help text where it
expected nothing. Under --json the entrypoint now holds help back: the
errors discard it, and the run's end prints the help that --help or a
command without a handler shows. ADR-0004, the CLI README and the patchy
skill drop the exception.

Closes #1072

Co-authored-by: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added area:ci CI, shared testing infrastructure, development scripts and tooling area:cli Touches the cli area area:docs Touches the docs area size:m 100-500 changed lines labels Oct 29, 2026
allisonmahmood and others added 3 commits October 30, 2026 00:34
…tract

Review found two parse errors that still broke the --json contract. A
global flag's own bad value, such as --log-level loud, fails outside
Effect's usage path, so the runtime printed a stack on stdout in either
mode; the entrypoint now renders it as the one line or the document.
And the early --json check missed --json=true and --json yes, which
Effect accepts; it now reads the flag as Effect does, beside JsonFlag.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Re-check found that Effect's lexer reads -json=true as the json flag
with a value, so the early check missed it and printed usage. The
entrypoint's catch also moves to catchTags, per the service conventions.

Co-authored-by: Claude Opus 5.5 <[email protected]>
…ecide

Effect refuses --no-json followed by a boolean literal without recording
a value, so a later -json=true is the flag's first value. The early
reading stopped at --no-json and rendered text; it now reads past it.

Co-authored-by: Claude Opus 5.5 <[email protected]>
@allisonmahmood

Copy link
Copy Markdown
Owner Author

Acceptance: PASS

Checked f1841ddc2db98a652cf8e445db071c1aa01ec332 (base e7781523993785f29c38965a9a8664606575f6a5) on fresh team and brightline data and isolated CLI state, by GPT 6.1 Sol in Codex, via T3 Code. This run checked the whole PR; the earlier verdict was not used as evidence.

Done means

  • Parse errors under --json: passed. Bad flag values, unknown flags, missing required flags and missing arguments each exited 1, left stdout empty and wrote exactly one local failure document to stderr. Unknown commands and invalid global --log-level/--completions values passed the same checks.
  • Without --json: passed. The four requested error cases retain usage on stdout and one error line on stderr. Invalid global values produce one stderr line with empty stdout; missing global values retain usage.
  • Explicit help with and without --json: passed. --help and -h were byte-identical across root, init, auth, look, publish and login: exit 0, empty stderr. Bare patchy and patchy auth also retain their help.
  • Documentation mirrors: passed. ADR-0004 and the CLI README now state empty stdout for JSON parse failures, and the global skill's old exception is gone. Its SHA-256 matches skills-lock.json. No stale exception remains in those surfaces. The applicable surface change is the CLI contract and its documentation; no API schema, migration or served-page code changes.
Observed error output at this head
$ patchy init notes --tier 7 --json
exit: 1; stdout: 0 bytes
stderr: {"ok":false,"error":"Invalid value for flag --tier: \"7\". Expected: \"1\" | \"2\"","kind":"local"}

$ patchy publish --bogus --json
exit: 1; stdout: 0 bytes
stderr: {"ok":false,"error":"Unrecognized flag: --bogus in command patchy publish","kind":"local"}

$ patchy look publish ./x --json
exit: 1; stdout: 0 bytes
stderr: {"ok":false,"error":"Missing required flag: --note","kind":"local"}

$ patchy look publish --note darker --json
exit: 1; stdout: 0 bytes
stderr: {"ok":false,"error":"Missing required argument: dir","kind":"local"}

$ patchy init notes --tier 7 --no-json false -json=true
exit: 1; stdout: 0 bytes
stderr: {"ok":false,"error":"Invalid value for flag --json: \"false\". Expected: omit the value and use --no-json by itself to set --json to false","kind":"local"}

Risks beyond Done means

  • Passed: 116 independent checks against the built executable. These cover the ten boolean literals in separated and inline JSON flags, -json=…, first-occurrence precedence, negation, the -- delimiter, text/JSON errors, explicit/implicit help, version and bash completions. Forty cases exercise refused --no-json <boolean> followed by another JSON or negation flag, including the regression fixed in f1841dd. Version and completion output remain byte-identical with and without --json.
  • Passed: both changed test suites, without changing their code:
$ pnpm exec vitest run packages/patchy/src/cli.test.ts packages/patchy/src/cliRepoPublish.test.ts
Test Files  2 passed (2)
     Tests  31 passed (31)
  • Passed: head CI. The CI run for f1841dd passed all three offline test jobs (Node 22, 24 and 26), browser, lint/typecheck, release packaging, CLI smoke on both Node versions, all three installer platforms, tier 2 smoke, Postgres concurrency, live Clerk and rollback rehearsal. No check on the head failed.

Golden journeys

  • Log a machine in — passed (team, Ada). Login returned one JSON handoff with URL and code; Ada confirmed the device form. Completion identified Ada's scenario email and “Acceptance laptop”; whoami identified Ada Park and that machine in JSON and text. Logout returned revoked=true; subsequent whoami --json exited 1 with empty stdout and one local failure document.
  • Publish a page — passed (team, Ada and Ben). Ada published Acceptance notes; Ben found its link on Home and fetched both its live frame and page content. Signed-out access returned 401 with the sign-in URL header. Retire and delete each replaced the frame with the appropriate notice; restore brought the content back at the original address after each. Each CLI success was one JSON document with empty stderr.
  • Share and unshare — passed (brightline, Allison and a signed-out reader). harbor-pine-kickoff began behind the company door. Sharing it publicly returned one JSON result; its address and content then answered 200 without a session, with public cache headers and no session cookie. Switching it back to company returned one JSON result and restored 401, the sign-in URL header and private cache headers.

All 22 fresh-instance checks passed, including the latest flag regression against the environment's installed CLI. Both environments were removed with pnpm dev down.

Rollback answers: match the diff. The output contract changes, but creates no persistent state that an older release cannot read. There are no schema, data or infrastructure changes. A revert restores the previous CLI output behavior and leaves no incompatible state; one-way is not needed.

Not run

  • None of the checks selected for this change were left unrun. Full offline and browser coverage above comes from CI on the checked head.

Notes

  • None.

@allisonmahmood
allisonmahmood merged commit d285816 into main Oct 30, 2026
23 checks passed
@allisonmahmood
allisonmahmood deleted the fix/1072-json-parse-error-stdout branch October 30, 2026 00:11
allisonmahmood added a commit that referenced this pull request Oct 30, 2026
…mmand (#1075)

## What changed

This adds release 27's What's new entry, written from `d285816`. It
covers everything merged since release 26's `through` (`eabf2e6`), and
sets the `patchy` release to `0.27.0` in `packages/patchy/package.json`
and `CURRENT_RELEASE`.

| Change | PRs |
| --- | --- |
| Fixed: your agent gets just the error when it mistypes a command |
#1073 |

#1071 is release 26's own entry.

## Look at

- **The entry**, at the top of `releases` in
`packages/core/src/whatsNew.ts`.
- **Rollback safety, from the live commit `e778152` (release 26) to
`d285816`:** the only PR is #1073, a CLI-only change to how parse errors
print, plus its tests and docs. It has no migration, no stored data and
no wire change, and it isn't labelled `one-way`. Release 26 works with
everything it leaves behind.

**Rollback line:** If this release misbehaves, roll back to `e778152`
(release 26).

## How it was verified

- The What's new, release and package-pin tests pass
(`whatsNew.test.ts`, `release.test.ts`, `packagePin.test.ts` and the
release-aware suites: 68 tests).
- #1073 itself had green CI and an acceptance PASS on its head before
merging.

## Release impact

- [ ] **Breaking** — requires action from anyone already running this
- [x] **Not breaking** — no action required

A changelog entry and the release version. CLIs older than `0.27.0` are
told to upgrade, as every release does.

- **What does a revert leave behind?** Nothing.
- **Does the release before it still work with that?** Yes.

Built by Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, through T3
Code.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:ci CI, shared testing infrastructure, development scripts and tooling area:cli Touches the cli area area:docs Touches the docs area size:m 100-500 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(cli): a flag error under --json prints help to stdout before the JSON error

1 participant