Skip to content

fix(init): fail when the settings.json prompt goes unanswered - #4141

Open
Wilson421 wants to merge 2 commits into
rtk-ai:developfrom
ewc3labs:fix/init-non-interactive-exit-code
Open

Wilson421 wants to merge 2 commits into
rtk-ai:developfrom
ewc3labs:fix/init-non-interactive-exit-code

Conversation

@Wilson421

Copy link
Copy Markdown

Summary

  • rtk init in the default Ask mode prints the manual instructions and exits 0 when stdin is not a terminal, so the hook is not installed and the caller is told it succeeded.
  • Split that case out as its own outcome (PatchResult::DeclinedNonInteractive) instead of reusing Declined, and end the run with an error naming --auto-patch. Everything else init writes still completes first.
  • --no-patch is untouched and still exits 0: a deliberate skip is not a failure.

Why it matters

prompt_user_confirmation already defaults to No without a terminal, which is the right default. The caller just cannot tell that apart from a human typing n. An installer, a CI job, or a devcontainer postCreateCommand sees a zero exit for a hook that was never registered.

That is not hypothetical: the ai-token-optimize-vscode extension runs rtk init -g --copilot from its installer and then tells the user RTK is wired. I hit it by hand on Windows first, which is how I found it.

Measured, on 0924356, Windows

$ CLAUDE_CONFIG_DIR=$T/claude rtk init -g --agent claude </dev/null
...
  MANUAL STEP: Add this to .../settings.json:
EXIT=0            <- reports success
hook present: 0   <- and the hook is not there

After this change, same command:

rtk: settings.json was not patched: stdin is not a terminal, so the confirmation
prompt could not be answered. Re-run with --auto-patch to patch without prompting,
or with --no-patch if you are adding the hook by hand.
EXIT=1

--auto-patch on the same file: exit 0, hook installed, pre-existing keys intact. --no-patch: exit 0.

Test plan

  • cargo fmt --check clean, cargo clippy --all-targets clean
  • cargo test --bin rtk: 3644 passed. The 17 failures are all in core::stream ("program not found") and reproduce identically on a stock checkout of develop on this Windows box, so they are environmental and predate this change.
  • Three new tests in hooks::init::tests: the unanswered prompt is its own outcome and leaves settings.json untouched; --no-patch still reports Skipped; the error message names both flags.
  • Negative control: with the new guard disabled, test_ask_mode_without_a_terminal_is_its_own_outcome fails with left: Declined, right: DeclinedNonInteractive. The test fails against the old behavior.
  • Manual, end to end, with CLAUDE_CONFIG_DIR pointed at a scratch dir: the three exit codes above.

Note on scope

Only the Claude settings.json path returns PatchResult, so that is all this touches. Other prompts (pi/omp extension overwrite, Trae) call prompt_user_confirmation directly and take the same silent default; happy to follow up separately if you want that pattern closed too.

`rtk init` (Ask mode, the default) prints the manual instructions and
exits 0 when stdin is not a terminal. `prompt_user_confirmation` already
defaults to No there, which is the right default, but the caller cannot
tell that apart from a human typing `n` -- so an installer, a CI job or a
devcontainer postCreateCommand sees success for a hook that was never
installed.

Measured on 0924356, Windows, with CLAUDE_CONFIG_DIR pointed at a temp
dir holding a settings.json:

    rtk init -g --agent claude </dev/null
    EXIT=0, hook absent, settings.json untouched

The VS Code extension ai-token-optimize-vscode hits this: its post-install
runs `rtk init -g --copilot` and then tells the user RTK is wired.

Split the unanswered prompt out as its own outcome
(PatchResult::DeclinedNonInteractive) rather than reusing Declined, and
end the run with an error naming --auto-patch. The rest of init still
completes first: the hook is the only piece that did not land.

--no-patch is untouched and still exits 0 -- a deliberate skip is not a
failure.

Co-Authored-By: Claude Opus 5 <[email protected]>
Copilot AI lite review requested due to automatic review settings September 20, 2026 09:03

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Address the critical non-interactive TTY handling and missing caller-level regression coverage.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity

Open (1)
What changed in this PR

This PR makes unanswered non-interactive Claude settings prompts fail explicitly instead of silently succeeding.

Changes:

  • Adds DeclinedNonInteractive with actionable guidance.
  • Preserves --auto-patch and --no-patch behavior.
  • Adds regression tests for prompt outcomes.
File Summary
src/​hooks/​init.rs Implements outcome handling and error reporting. Review findings remain for caller-level coverage, pseudo-TTY handling, and stronger no-write assertions.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/hooks/init.rs Outdated
Comment on lines +714 to +716
fn stdin_can_answer_prompt() -> bool {
use std::io::IsTerminal;
std::io::stdin().is_terminal()

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Conceded, and fixed in 6276ae7 — the helper claimed more than it proved.

You are right that a pseudo-TTY defeats is_terminal(), and this file already says so: prompt_telemetry_consent documents devcontainer postCreateCommand and CI agents handing rtk a pseudo-TTY, and short-circuits on RTK_TELEMETRY_DISABLED for exactly that reason.

What I changed:

  • Renamed to stdin_is_terminal() and documented both halves. No terminal at all is the half this settles: the No in prompt_user_confirmation is then nobody's decision, so reporting it as one is wrong regardless of what the pseudo-TTY case does. The pseudo-TTY hang is real, predates this change, and is untouched here.
  • Extracted finish_patch_outcome so both callers share one outcome→exit mapping, with a test at that level (the missing caller-level coverage in your overview).

On the hang itself, I did not want to invent policy in your tool. Two shapes that would close it, and I will send whichever you prefer as its own PR:

  1. An explicit signal, matching the precedent already in this file: RTK_NONINTERACTIVE=1 (and/or CI) treated as "do not prompt", the way RTK_TELEMETRY_DISABLED is handled today.
  2. A bounded read: prompt, wait N seconds, then take the same DeclinedNonInteractive path instead of blocking forever.

Option 1 is the smaller change and matches what is already here; option 2 needs no environment cooperation. Your call.

@CLAassistant

CLAassistant commented Sep 20, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working cli init windows labels Sep 20, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes rtk init in default Ask mode so it fails (exit 1) instead of silently succeeding when stdin is not a terminal and the settings.json patch prompt can't be answered. Introduces a new PatchResult::DeclinedNonInteractive outcome distinct from a human declining, printing manual instructions and returning an error that points to --auto-patch/--no-patch, while --no-patch remains a deliberate zero-exit skip.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

…exit decision

Copilot review, PR rtk-ai#4141. `is_terminal()` does not establish that a human is
there to answer: this file already records that some non-interactive
environments hand rtk a pseudo-TTY, where the prompt waits forever. The helper
claimed more than it proves.

- Rename to `stdin_is_terminal` and document both halves: no terminal means the
  No belongs to nobody, while a pseudo-TTY still hangs and wants its own fix.
- Extract `finish_patch_outcome` so both callers share one mapping from outcome
  to exit code, and cover it with a test at that level.

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

Labels

bug Something isn't working cli init windows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants