Skip to content

fix(init): --guided reads piped answers, aborts non-zero on EOF; EOF-safe y/N prompts (#375) - #376

Merged
stackbilt-admin merged 4 commits into
mainfrom
fix/375-guided-non-tty
Oct 17, 2026
Merged

stackbilt-admin merged 4 commits into
mainfrom
fix/375-guided-non-tty

Conversation

@stackbilt-admin

@stackbilt-admin stackbilt-admin commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Closes #375

Problem

charter init --guided with piped / non-TTY stdin (echo y | charter init --guided, CI) stopped partway through the questions, wrote nothing, and exited 0. It did this even when every answer was piped (printf 'typescript\ny\ny\n' | ...).

Root causes (two)

  1. Dropped lines. With piped stdin readline emits all buffered lines at once. rl.question only receives a line if a question is pending at that moment, so lines 2..n were dropped and the run stalled after Q2.
  2. Never-settling promise. On EOF readline emits close and never calls a pending rl.question callback. run()'s promise never settled, bin.ts never set process.exitCode, and Node exited 0 once the event loop drained.

Fix

packages/cli/src/commands/init.ts

  • GuidedLineReader registers rl.on('line') once and queues lines. Each question takes the next queued line or waits for one, so piped answers behave like typed ones and write the same files. Non-TTY stdin is not refused: Git Bash/mintty without ConPTY presents typed input to node.exe as a pipe (review finding 1 on the first revision).
  • If stdin closes before the last answer (short pipe, </dev/null, Ctrl-D on an empty line), it prints Aborted; nothing was written. on stderr and exits 1. Ctrl-C at a terminal (readline SIGINT) exits 130. On a pipe, Ctrl-C is a process signal that readline never sees. The CLI does not handle it, so the signal ends the process; the exact exit status has not been verified on Windows/mintty.
  • No project file is written before the last answer. The CLI's usage log .charter/telemetry/events.ndjson is still appended, as after every command. In a fresh dir with no .charter/.gitignore it shows up as untracked; the docs say so.
  • --format json is still refused up front (GUIDED_REQUIRES_TEXT): one JSON document { error, errorCode, guided: true, writesPerformed: 0 }, exit 1. GUIDED_REQUIRES_TTY is gone.
  • --guided --preset <name> now seeds the fallback preset (previously silently ignored). It is used when the language answer names no known stack, and Q1 shows blank keeps --preset <name>. A recognized language or a Cloudflare y still decides. I picked this over a warning because it makes the flag useful and is a one-line change in the existing decision chain.

packages/cli/src/commands/adf-scaffold-write.ts: promptYesNo

  • It had the same never-settling rl.question. A close before the answer now resolves N, with No answer (input closed); treating it as N. on stderr. The non-TTY guard is unchanged.
    • For --reset-adf "Replace them?", this is the existing decline path: exit 1, nothing written.
    • For bootstrap's "Register these modules now?", it means don't register (same as N); the existing hint warning is pushed.
  • bootstrap.ts call sites are untouched (bootstrap rewrites .mcp.json formatting and overwrites a hand-edited charter-governance.yml with no backup #374 is in flight there).

packages/cli/src/index.ts: the root --help lists all six presets for init and setup.

Tests

init-guided-tty.test.ts (19 tests) runs the real node:readline over a PassThrough:

  • typed TTY happy path;
  • fully piped answers produce the same file tree as a typed run;
  • CRLF piped answers with no final newline;
  • defaults;
  • --preset seeding, and language overriding the seed;
  • </dev/null, echo y | and two-answer pipes all exit 1 with nothing written;
  • a short pipe leaves an existing .charter/ untouched;
  • TTY Ctrl-D after 0, 1 and 2 answers exits 1;
  • Ctrl-C (SIGINT) exits 130;
  • the json refusal, on a TTY and off one;
  • promptYesNo: EOF gives N plus a note, y gives yes, non-TTY gives N without creating an interface;
  • the help text lists 6 presets.

Other test files: the no-silent-overwrite.test.ts readline mock is now an EventEmitter that answers prompt() with a line event, and the bootstrap-force-merge.test.ts mock gains once.

Full suite: 1620 passed; the 3 failures are the known pre-existing Windows path-separator ones (bootstrap #307, context-refresh, serve-context).

Docs

docs/cli-reference.md (charter init -> --guided) and CHANGELOG [Unreleased] -> ### Fixed (guided, y/N prompts, --preset seeding, help text).

Known cosmetic

With piped answers, the echo of each answer in the stdout transcript is not uniform. An answer that arrives while its question is waiting is not echoed; answers already queued are. Echoing unconditionally would double-echo mintty typists.

Update (d429987, second review round)

  • promptYesNo, Ctrl-C: readline SIGINT now prints Aborted. and calls process.exit(130) inside the helper. Bootstrap no longer goes on to migrate configs or install packages after an interrupt. bootstrap.ts is untouched. EOF and Ctrl-D on an empty line still mean N, with the note.
  • init --guided --ci is refused up front: exit 1, with a stderr message naming charter init --preset. When --format json is also passed, the JSON refusal applies instead.
  • Terminal-mode tests: readline runs with terminal: true over a fake TTY PassThrough. 10 consecutive runs passed. They cover:
    • guided input: typed Enter, \x03 (exit 130), \x04 on an empty line (exit 1), and \x04 mid-line deleting a character;
    • promptYesNo: \x03 with process.exit mocked (asserts 130), and \x04 (resolves N).
  • Six-preset lists in the root --help --preset option, charter adf --help, packages/cli/README.md (two places) and docs-snippets/charter-cli-reference.md. bootstrap.ts:145, the invalid-preset error message, still lists five; I left it because bootstrap rewrites .mcp.json formatting and overwrites a hand-edited charter-governance.yml with no backup #374 is changing that file.
  • CHANGELOG: the --guided --format json removal and the --ci refusal are now under ### Changed.
  • Tests: removed the leftover setTTY() calls from no-silent-overwrite.test.ts.

Update (5dee01b)

  • promptYesNo on Ctrl-C: it now rejects with CLIError('Aborted.', 130) instead of calling process.exit(130). Nothing between the prompt and the dispatcher catches it, so:
    • the command still stops;
    • run() records the usage-log event with exit code 130;
    • bin.ts prints charter: Aborted. and sets the exit code.
  • New test: drives bootstrap's orphan prompt in terminal mode, sends Ctrl-C, and asserts both the CLIError 130 and the 130 telemetry event.
  • Docs: the Global Flags --preset rows in docs/cli-reference.md and docs-snippets/charter-cli-reference.md now list all six presets.
  • init.ts comment: now says 130 is what POSIX shells report and that Windows reports STATUS_CONTROL_C_EXIT.

🤖 Generated with Claude Code

kovermier and others added 2 commits October 17, 2026 17:27
…ts with exit 1 (#375)

`charter init --guided` with piped stdin stopped mid-questionnaire, wrote
nothing and exited 0. Root cause: a pending rl.question callback never fires
when readline closes on EOF, so run()'s promise never settled and Node exited
0 once the event loop drained.

- Check up front, before any prompt or write: non-TTY stdin or a non-text
  format exits 1 with a message (stderr, or one JSON error document) naming
  `charter init --preset <...>` / `--force`. Answers are never read from a pipe.
- Questions reject on readline 'close', so Ctrl-D / Ctrl-C mid-questionnaire
  prints "Aborted; nothing was written." and exits 1.
- Tests run the real readline over a PassThrough for EOF and the happy path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…mptYesNo (#375)

Rework after review: refusing every non-TTY stdin would lock out Git Bash /
mintty users, whose typed input reaches node.exe as a pipe.

- Drop the non-TTY refusal (GUIDED_REQUIRES_TTY). Guided answers come from a
  queue fed by rl.on('line'), so lines that arrive while no question is
  pending are no longer dropped: fully piped answers work like typed ones.
- stdin closing before the last answer (short pipe, </dev/null, Ctrl-D on an
  empty line) prints "Aborted; nothing was written." and exits 1; Ctrl-C at
  a terminal exits 130. Nothing is written before the last answer.
- --format json refusal (GUIDED_REQUIRES_TEXT) kept.
- promptYesNo: a close before the answer resolves N with a note on stderr
  instead of leaving the promise pending (exit 0 mid-run).
- --guided --preset <name> seeds the fallback preset instead of being ignored.
- Root --help lists all six presets for init and setup.
- Docs/CHANGELOG: both root causes, usage-log caveat, Ctrl-D wording.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stackbilt-admin stackbilt-admin changed the title fix(init): --guided refuses non-TTY stdin and --format json; EOF exits 1 (#375) fix(init): --guided reads piped answers, aborts non-zero on EOF; EOF-safe y/N prompts (#375) Oct 17, 2026
kovermier and others added 2 commits October 17, 2026 18:05
… preset lists (#375)

Second review round on PR #376:
- promptYesNo: Ctrl-C (readline SIGINT) prints "Aborted." and exits 130 from
  the helper, so bootstrap no longer carries on to migrate/install after an
  interrupt. EOF / Ctrl-D on an empty line still resolve N with the note.
- init --guided --ci refuses up front (exit 1); a CI stdin that never closes
  would otherwise wait forever.
- Terminal-mode readline tests over a fake TTY: typed Enter, Ctrl-C (130),
  Ctrl-D on an empty line (1), Ctrl-D mid-line deletes; promptYesNo Ctrl-C
  (mocked process.exit) and Ctrl-D.
- All preset lists show six presets (root --help option, adf --help, CLI
  README, docs snippet).
- CHANGELOG: --guided --format json removal and --ci refusal under Changed;
  pipe Ctrl-C wording limited to what is verified. Drop leftover setTTY().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ecorded; preset tables (#375)

- promptYesNo: Ctrl-C rejects with CLIError('Aborted.', 130) instead of
  process.exit(130). No caller catches it, so the command still stops, run()
  records the usage-log event with exit 130, and bin.ts prints
  `charter: Aborted.` and sets the exit code. Test drives bootstrap's orphan
  prompt in terminal mode and checks the 130 telemetry event.
- Global Flags --preset rows in docs/cli-reference.md and
  docs-snippets/charter-cli-reference.md list all six presets.
- init.ts comment: 130 is what POSIX shells report; Windows reports
  STATUS_CONTROL_C_EXIT.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stackbilt-admin
stackbilt-admin merged commit d1d5209 into main Oct 17, 2026
7 checks passed
@stackbilt-admin
stackbilt-admin deleted the fix/375-guided-non-tty branch October 17, 2026 23:20
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.

init --guided with non-TTY stdin exits 0 mid-questionnaire without writing anything

2 participants