Skip to content

Add TTY detection for interactive prompts to prevent agent hangs#1547

Merged
garrytrinder merged 5 commits intomainfrom
copilot/add-tty-detection
Feb 26, 2026
Merged

Add TTY detection for interactive prompts to prevent agent hangs#1547
garrytrinder merged 5 commits intomainfrom
copilot/add-tty-detection

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

cert remove and first-run setup use Console.ReadLine() which hangs indefinitely in non-interactive contexts (CI, piped stdin, AI agents). The --force flag exists but nothing prevents the prompt from blocking.

Changes

  • CertCommand.cs: Check Console.IsInputRedirected and CI env var before prompting. If non-interactive and --force not set, fail immediately with exit code 1 and actionable error:
    Confirmation required but running in non-interactive mode. Use --force to skip confirmation.
    
  • CertCommand.cs: RemoveCert now returns int exit code (1 for non-interactive failure or exceptions, 0 for success) so System.CommandLine propagates failure correctly.
  • ProxyEngine.cs: Skip first-run cert trust prompt when stdin is redirected or CI env var is set; default to trusting (matches the Y/n default). Logs the decision.
  • --force help text: Updated to note it's required for non-interactive use.

Non-interactive detection consistently uses both Console.IsInputRedirected and the CI environment variable, matching the existing pattern in ProxyEngine.ExecuteAsync to handle CI environments that provide a pseudo-TTY.

Original prompt

This section details on the original issue you should resolve

<issue_title>Add TTY detection for interactive prompts to prevent agent hangs</issue_title>
<issue_description>Parent: #1534

Problem

cert remove uses Console.ReadLine() in a while(true) loop to prompt for confirmation. When called by an AI agent or in any non-interactive context (CI, piped stdin, headless), the command hangs indefinitely waiting for input that will never come.

The --force flag exists to skip the prompt, but:

  • The help text doesn't mention that --force is required for non-interactive use
  • There's no TTY detection — the prompt runs regardless of whether stdin is a terminal

Rationale

AI agents and CI pipelines don't have interactive terminals. A CLI that hangs waiting for input is a blocker — the agent can't recover from it, can't detect it's happening, and can't time out gracefully. This is the most common way CLIs break in automated contexts.

Proposed solution

  1. Add TTY detection before prompting. If stdin is not a terminal and --force is not set, fail immediately with:

    Error: Confirmation required but running in non-interactive mode. Use --force to skip confirmation.
    

    In .NET: Console.IsInputRedirected detects non-TTY stdin.

  2. Consider a global --no-input flag that disables all interactive prompts across all commands. Any command requiring confirmation without --force should fail with a clear error.

  3. Update help text for cert remove to note that --force is required for non-interactive use.

  4. Audit all commands for other Console.ReadLine() calls that could hang.

Affected code

  • DevProxy/Commands/CertCommand.csPromptConfirmation() method
  • Any other code using Console.ReadLine() or similar interactive patterns
    </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

- CertCommand: detect non-interactive stdin before prompting, fail with
  clear error directing users to --force flag
- ProxyEngine: skip first-run cert trust prompt in non-interactive mode,
  default to trusting the certificate
- Update --force option help text to note it's required for non-interactive use

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copilot AI changed the title [WIP] Add TTY detection for interactive prompts to prevent agent hangs Add TTY detection for interactive prompts to prevent agent hangs Feb 25, 2026
@waldekmastykarz waldekmastykarz marked this pull request as ready for review February 25, 2026 13:07
@waldekmastykarz waldekmastykarz requested a review from a team as a code owner February 25, 2026 13:07
Copilot AI review requested due to automatic review settings February 25, 2026 13:07
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to prevent Dev Proxy CLI commands from hanging indefinitely in non-interactive environments (CI, piped stdin, automation/agents) by adding stdin/TTY detection around interactive prompts.

Changes:

  • Add Console.IsInputRedirected checks to avoid Console.ReadLine() prompts in non-interactive mode.
  • Update cert remove’s --force help text to clarify non-interactive requirements.
  • Default first-run macOS certificate trust prompt to “yes” when stdin is redirected, and log that decision.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
DevProxy/Proxy/ProxyEngine.cs Avoids first-run certificate trust prompt when stdin is redirected by auto-selecting the default choice.
DevProxy/Commands/CertCommand.cs Prevents cert remove from prompting in non-interactive mode unless --force is set; updates --force help text.

waldekmastykarz and others added 2 commits February 26, 2026 08:45
…t code

- CertCommand and ProxyEngine: check both Console.IsInputRedirected and
  CI env var for non-interactive detection, matching ExecuteAsync pattern
- RemoveCert returns int: exit code 1 for non-interactive failure or
  exceptions, 0 for success

Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 26, 2026

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: /home/REDACTED/work/dev-proxy/dev-proxy/DevProxy/bin/Debug/net10.0/devproxy /home/REDACTED/work/dev-proxy/dev-proxy/DevProxy/bin/Debug/net10.0/devproxy cert remove (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copy link
Contributor

@garrytrinder garrytrinder left a comment

Choose a reason for hiding this comment

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

Tested: non-interactive detection works correctly via piped stdin and CI env var. cert remove fails immediately with exit code 1 and actionable message. --force bypasses prompt. ProxyEngine first-run guard consistent. No hangs observed.

@garrytrinder garrytrinder enabled auto-merge (squash) February 26, 2026 11:05
@garrytrinder garrytrinder merged commit 98de506 into main Feb 26, 2026
4 checks passed
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.

Add TTY detection for interactive prompts to prevent agent hangs

4 participants