Skip to content

Exit llm chat cleanly on Ctrl+D#1457

Open
ATOM00blue wants to merge 1 commit into
simonw:mainfrom
ATOM00blue:exit-chat-on-eof
Open

Exit llm chat cleanly on Ctrl+D#1457
ATOM00blue wants to merge 1 commit into
simonw:mainfrom
ATOM00blue:exit-chat-on-eof

Conversation

@ATOM00blue
Copy link
Copy Markdown

Fixes #1214

Pressing Ctrl+D (EOF) at the llm chat prompt currently prints Aborted! and exits with a non-zero status, instead of exiting cleanly the way most interactive REPLs (Python, etc.) do:

> ^DAborted!

Root cause

The chat loop reads input with click.prompt(...). On EOF, click.prompt catches the underlying EOFError and re-raises it as click.exceptions.Abort. Nothing in the loop handles that, so it bubbles up to Click's top level, which prints Aborted! and exits with status 1.

Fix

Wrap the click.prompt call in the loop and treat an Abort as a clean exit:

while True:
    try:
        prompt = click.prompt("", prompt_suffix="> " if not in_multi else "")
    except click.exceptions.Abort:
        # Ctrl+D (EOF) should exit the chat cleanly
        break

Testing

Added test_chat_eof_exits_cleanly, which patches click.prompt to raise click.Abort (the same thing a real terminal EOF produces) and asserts the command exits with status 0 and never prints Aborted!. The test fails on main and passes with this change. The rest of tests/test_chat.py still passes.

Pressing Ctrl+D (EOF) at the chat prompt let click.prompt's Abort
propagate, printing "Aborted!" and exiting with a non-zero status.
Catch the Abort and break out of the loop so EOF ends the session
cleanly, matching the Python REPL and other interactive tools.

Refs simonw#1214
Copilot AI review requested due to automatic review settings May 22, 2026 01:46
Copy link
Copy Markdown

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 fixes llm chat so pressing Ctrl+D (EOF) exits the chat loop cleanly (status 0) instead of bubbling up to Click’s top-level abort handler that prints Aborted! and exits non-zero.

Changes:

  • Catch click.exceptions.Abort around the click.prompt(...) call in the chat loop and exit the loop cleanly.
  • Add a regression test that simulates EOF by patching click.prompt to raise click.Abort and asserting exit code 0 with no Aborted! output.

Reviewed changes

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

File Description
llm/cli.py Wraps the interactive prompt read to treat click.exceptions.Abort as a clean exit from llm chat.
tests/test_chat.py Adds a test covering EOF/Abort behavior to prevent regression and verify clean exit semantics.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

ctrl-d (EOF) should exit cleanly instead of printing "Aborted!"

2 participants