Skip to content

fix(cli): send ctrl+c and signals through one cancel path - #1351

Draft
gewenyu99 wants to merge 1 commit into
mainfrom
posthog/one-cancel-path
Draft

gewenyu99 wants to merge 1 commit into
mainfrom
posthog/one-cancel-path

Conversation

@gewenyu99

Copy link
Copy Markdown
Collaborator

Problem

Ctrl-C in the TUI skipped every registered cleanup. Ink runs the terminal in raw mode, so Ctrl-C arrives as a key, not a SIGINT. Ink's exitOnCtrlC exited on that key, and run-wizard's SIGINT handler never ran. So a cancelled run:

Headless and --ci runs had no signal handler at all. A SIGTERM from CI killed the process before any cleanup ran.

Changes

There's now one cancel path. Every way of quitting goes through it.

  • wizardCancel in wizard-abort.ts. Runs the cleanups first, then the cancel hooks and the analytics flush, each capped at 2 s. Then it exits with 128 plus the signal number. A second cancel while the first is settling exits right away.
  • The Ctrl-C key. startTUI renders with exitOnCtrlC: false. ScreenContainer's global useInput sends Ctrl-C to wizardCancel.
  • Signals. installCancelSignals sends SIGINT, SIGTERM and SIGHUP to wizardCancel. It's installed in startTUI and runNonInteractive.
  • Task streams. run-wizard and runNonInteractive register a cancel hook that ends their stream as an error. run-wizard's own signal handler is gone.

Exit codes: 130 for Ctrl-C or SIGINT, 143 for SIGTERM, 129 for SIGHUP. docs/error-catalog.md lists them.

Prior art. We copied these patterns rather than invent one:

  • Vim. In raw mode Ctrl-C is a byte, not a signal, so Vim reads it as a key. SIGTERM and SIGHUP go to one handler that restores the terminal before exiting.
  • Ink. Its README says to set exitOnCtrlC: false and handle Ctrl-C yourself in useInput.
  • Shells, git, npm. A cancelled process exits 128 plus the signal number.
  • docker compose. The first Ctrl-C stops gracefully, a second one forces the exit.

Test plan

  • Real TUI. Ran the built dist/bin.js audit in a pty (node-pty) and sent a real \x03 or signal:

    Input Exit Log
    Ctrl-C 130 [wizard-cancel] ctrl+c, cancelling
    SIGTERM 143 [wizard-cancel] SIGTERM, cancelling
    Ctrl-C twice, 30 ms apart 130 ctrl+c again, exiting now

    Each run left the alternate screen and printed the exit line.

  • Unit and integration tests. They cover wizardCancel's order, exit codes, the second-cancel force, a hanging or throwing hook, the Ctrl-C key in ScreenContainer, startTUI's render options, a TUI run cancelled mid-agent (stream settled as an error before exit), and a headless run cancelled by SIGTERM.

  • Mutations. Breaking each wiring point (the key, exitOnCtrlC, the headless signals, the run-wizard hook, cleanups-first) fails one test.

  • pnpm typecheck, ESLint on the changed files (0 errors), and vitest run (203 files, 3368 tests) pass.

To try it, run any TUI command, press Ctrl-C, then check echo $? prints 130.

LLM context

Written by Claude Code while fixing the audit ledger cleanup in #1336.

🤖 Generated with Claude Code

Ink runs the terminal in raw mode, so ctrl+c reaches the TUI as a key,
not a SIGINT. Ink's exitOnCtrlC exited on it without running the
registered cleanups, without settling the task stream, and with exit
code 0. Only a real SIGINT or SIGTERM from outside reached run-wizard's
handler, and headless runs had no handler at all.

wizardCancel in wizard-abort.ts is now the one exit for a cancel. It
runs the cleanups, then the registered cancel hooks and the analytics
flush, each bounded to 2 s, then exits 128 plus the signal number. A
second cancel while the first settles exits at once. The ctrl+c key in
ScreenContainer, SIGINT, SIGTERM and SIGHUP in the TUI, and the same
signals in headless runs all call it. run-wizard and runNonInteractive
register a cancel hook that settles their task stream as an error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

🧙 Wizard CI

Run the Wizard CI and test your changes against wizard-workbench example apps by replying with a GitHub comment using one of the following commands:

Test all apps:

  • /wizard-ci all

Test all apps in a directory:

  • /wizard-ci ai-observability
  • /wizard-ci basic-integration
  • /wizard-ci feature-flags
  • /wizard-ci mcp-analytics
  • /wizard-ci replay-vision
  • /wizard-ci revenue
  • /wizard-ci self-driving
  • /wizard-ci warehouse
  • /wizard-ci warehouse-seeded

Test an individual app:

  • /wizard-ci ai-observability/anthropic
  • /wizard-ci ai-observability/google-adk
  • /wizard-ci ai-observability/groq
Show more apps
  • /wizard-ci ai-observability/manual-capture
  • /wizard-ci ai-observability/openai
  • /wizard-ci ai-observability/openai-agents
  • /wizard-ci ai-observability/opentelemetry
  • /wizard-ci ai-observability/vercel-ai
  • /wizard-ci basic-integration/android
  • /wizard-ci basic-integration/angular
  • /wizard-ci basic-integration/astro
  • /wizard-ci basic-integration/django
  • /wizard-ci basic-integration/fastapi
  • /wizard-ci basic-integration/flask
  • /wizard-ci basic-integration/flutter
  • /wizard-ci basic-integration/javascript-node
  • /wizard-ci basic-integration/javascript-web
  • /wizard-ci basic-integration/laravel
  • /wizard-ci basic-integration/next-js
  • /wizard-ci basic-integration/nuxt
  • /wizard-ci basic-integration/python
  • /wizard-ci basic-integration/rails
  • /wizard-ci basic-integration/react-native
  • /wizard-ci basic-integration/react-router
  • /wizard-ci basic-integration/sveltekit
  • /wizard-ci basic-integration/swift
  • /wizard-ci basic-integration/tanstack-router
  • /wizard-ci basic-integration/tanstack-start
  • /wizard-ci basic-integration/vue
  • /wizard-ci feature-flags/django
  • /wizard-ci feature-flags/next-js
  • /wizard-ci mcp-analytics/custom-dispatcher
  • /wizard-ci mcp-analytics/typescript-sdk
  • /wizard-ci replay-vision/javascript-node
  • /wizard-ci replay-vision/next-js
  • /wizard-ci replay-vision/react-vite
  • /wizard-ci revenue/stripe
  • /wizard-ci self-driving/astro
  • /wizard-ci self-driving/fastapi
  • /wizard-ci self-driving/nuxt
  • /wizard-ci self-driving/react-router
  • /wizard-ci self-driving/sveltekit
  • /wizard-ci warehouse/monorepo-env
  • /wizard-ci warehouse/multi-source-next
  • /wizard-ci warehouse/stripe-node
  • /wizard-ci warehouse/zero-source
  • /wizard-ci warehouse-seeded/next-stripe
  • /wizard-ci warehouse-seeded/next-stripe-declined

Test against a Context Mill branch:

  • /wizard-ci all context-mill:my-branch

Add context-mill:<branch> to any command above to pin the Context Mill branch. It defaults to main.

Results will be posted here when complete.

gewenyu99 added a commit that referenced this pull request Sep 24, 2026
#1351 routes ctrl+c, SIGINT, SIGTERM and SIGHUP through one cancel path
for both the TUI and headless runs, so this PR keeps only the ledger
removal it registers as a cleanup.

This reverts commit f9fc462.

Part of #1326

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant