Skip to content

fix(session): match the terminal's shutdown signals in headless reviews - #1353

Merged
benvinegar merged 1 commit into
mainfrom
fix/web-closed-stdout
Nov 22, 2026
Merged

benvinegar merged 1 commit into
mainfrom
fix/web-closed-stdout

Conversation

@benvinegar

@benvinegar benvinegar commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Problem

  • hunk diff --web --no-open | head -1 keeps the review running after head exits.
  • The browser review docs said "Standard output is exactly the URL, so a script can capture it". But URL=$(hunk diff --web --no-open) never returns, because the review keeps running and never closes stdout.
  • A headless review also used a different shutdown-signal list from the terminal UI: on Unix it did not stop on SIGQUIT (Ctrl+).

Approach

This mirrors the terminal UI instead of adding new detection. The TUI does not poll for a closed reader. It stops on signals and on EPIPE from a failed write, and the headless runner already handles EPIPE that way.

  • New packages/hunk/src/core/process/shutdownSignals.ts holds REVIEW_SHUTDOWN_SIGNALS, the TUI's list:
    • Unix: SIGINT, SIGTERM, SIGHUP, SIGQUIT, SIGPIPE.
    • Windows: SIGINT, SIGTERM, SIGBREAK.
  • runInteractiveApp.tsx and headlessReviewRunner.ts both use it, so the two lists cannot drift apart again. The only behavior change is that headless now also stops on SIGQUIT.
  • Exit codes already match: neither surface maps signals to exit codes, so both exit 0. No change there.
  • Docs (browser-review.md):
    • Explain why $(...) never returns.
    • Show a background-plus-file pattern, stopped with kill (a background job in a non-interactive shell ignores SIGINT, so kill -INT would not stop it).
    • Point to hunk session open --repo . --no-open for a review that is already running.
    • Say that in | head -1 the review ends on a signal or at its next write to the closed pipe, as with the terminal UI.

Non-goals: detecting a closed reader without a write. An earlier version of this PR polled with bun:ffi; that was dropped to match the TUI.

Tests and checks (Linux only)

  • New unit test: SIGQUIT shuts the headless review down in order.
  • bun test packages/hunk/src/app/session/headlessReviewRunner.test.ts packages/hunk/src/ui/runInteractiveApp*: 25 pass
  • bun test test/cli/web-entry-points.test.ts test/session/headless.test.ts: 14 pass
  • bun run typecheck, bun run lint, bun run format:check, bun run deps:check: clean
  • By hand: I ran the documented script snippet against a private daemon. It read the URL, and kill ended the review with exit code 0.

Known limitations

https://claude.ai/code/session_01Nn5HpgDBQrjicbK5ZoZnMG


Generated by Claude Code

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects headless browser reviews and the scripts that read their output.

The filename requirement must be satisfied before merging; the script example also needs non-blocking fixes.

Findings

  1. P2 New filenames break repository rules ▶
  2. P2 Script shares an unreliable filename ▶
  3. P2 Script hangs after startup fails ▶
Fix with agent prompt
### Issue 1
packages/hunk/src/core/process/outputClosed.ts:14
The new `outputClosed.ts` and `outputClosed.test.ts` files violate the repository directive to use dash-case for TypeScript filenames. Rename them to `output-closed.ts` and `output-closed.test.ts`, and update the imports. This repository requirement must be satisfied before merging.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

### Issue 2
website/src/content/docs/docs/workflows/browser-review.md:34
If `TMPDIR` is unset, this redirects to `/hunk-url`. An ordinary Linux user cannot write there, so the review never starts and the following loop waits forever. Concurrent runs also share this filename and can read each other's URL. Create a unique file with `mktemp`, using `${TMPDIR:-/tmp}` if needed, and remove it when done.

### Issue 3
website/src/content/docs/docs/workflows/browser-review.md:36
Even with a writable temporary file, this loop never ends if Hunk fails before printing its URL. A daemon port conflict is one documented example: Hunk exits, but the file stays empty. Check whether `review` has exited while waiting, then use `wait` to return its failure instead of sleeping forever.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

The PR checks for closed output readers every 500 ms and sends headless reviews through the existing shutdown path. It also adds a real-process regression test and replaces the command-substitution advice with a background script example.

  • Headless reviews stop when their output reader closes.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Headless review prints URL] --> B[Check stdout and stderr every 500 ms]
  B --> C{Pipe or socket reader gone?}
  C -->|No| B
  C -->|Yes| D[Stop output checks]
  D --> E[Stop reloads and input watch]
  E --> F[Shut down extensions]
  F --> G[Stop broker client and exit]
Loading

Reviews (1) · Last reviewed commit: "fix(session): end a headless review when..." · Reviewed by Greptile

* `/dev/null` has no reader to lose, so those are never reported gone. Windows has no `poll`
* for pipe handles, so there a closed reader is still noticed by the next failed write only.
*/
import { fstatSync } from "node:fs";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 New filenames break repository rules

The new outputClosed.ts and outputClosed.test.ts files violate the repository directive to use dash-case for TypeScript filenames. Rename them to output-closed.ts and output-closed.test.ts, and update the imports. This repository requirement must be satisfied before merging.

Context Used: guidelines.mdc Cursor rule (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/hunk/src/core/process/outputClosed.ts
Line: 14

Comment:
**New filenames break repository rules**

The new `outputClosed.ts` and `outputClosed.test.ts` files violate the repository directive to use dash-case for TypeScript filenames. Rename them to `output-closed.ts` and `output-closed.test.ts`, and update the imports. This repository requirement must be satisfied before merging.

**Context Used:** guidelines.mdc Cursor rule ([source](https://github.com/modem-dev/modem/blob/main/.cursor/rules/guidelines.mdc))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Start the review in the background, send its output to a file, and read the first line once it appears. Stop the review when the script is done:

```bash
hunk diff --web --no-open > "$TMPDIR/hunk-url" &

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Script shares an unreliable filename

If TMPDIR is unset, this redirects to /hunk-url. An ordinary Linux user cannot write there, so the review never starts and the following loop waits forever. Concurrent runs also share this filename and can read each other's URL. Create a unique file with mktemp, using ${TMPDIR:-/tmp} if needed, and remove it when done.

Prompt To Fix With AI
This is a comment left during a code review.
Path: website/src/content/docs/docs/workflows/browser-review.md
Line: 34

Comment:
**Script shares an unreliable filename**

If `TMPDIR` is unset, this redirects to `/hunk-url`. An ordinary Linux user cannot write there, so the review never starts and the following loop waits forever. Concurrent runs also share this filename and can read each other's URL. Create a unique file with `mktemp`, using `${TMPDIR:-/tmp}` if needed, and remove it when done.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

```bash
hunk diff --web --no-open > "$TMPDIR/hunk-url" &
review=$!
until [ -s "$TMPDIR/hunk-url" ]; do sleep 0.1; done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Script hangs after startup fails

Even with a writable temporary file, this loop never ends if Hunk fails before printing its URL. A daemon port conflict is one documented example: Hunk exits, but the file stays empty. Check whether review has exited while waiting, then use wait to return its failure instead of sleeping forever.

Prompt To Fix With AI
This is a comment left during a code review.
Path: website/src/content/docs/docs/workflows/browser-review.md
Line: 36

Comment:
**Script hangs after startup fails**

Even with a writable temporary file, this loop never ends if Hunk fails before printing its URL. A daemon port conflict is one documented example: Hunk exits, but the file stays empty. Check whether `review` has exited while waiting, then use `wait` to return its failure instead of sleeping forever.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

A `--web` review now stops on the same signals as the terminal UI, SIGQUIT included,
from one shared list. The browser review docs now explain why `URL=$(hunk diff --web)`
never returns and show how a script reads the URL instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Nn5HpgDBQrjicbK5ZoZnMG
@benvinegar
benvinegar force-pushed the fix/web-closed-stdout branch from 2f1043d to 646bd6d Compare November 22, 2026 03:55
@benvinegar benvinegar changed the title fix(session): end a headless review when its output reader goes away fix(session): match the terminal's shutdown signals in headless reviews Nov 22, 2026

Copy link
Copy Markdown
Member Author

Reworked after review, to mirror the terminal UI. I dropped the bun:ffi poll for a closed reader along with its test and the real-process test. The headless review now shares the TUI's shutdown-signal list (core/process/shutdownSignals.ts), which adds SIGQUIT on Unix. Closing the pipe ends the review only at its next write or on a signal. The docs now explain why $(...) never returns and show a background-plus-file pattern and hunk session open --no-open. The changeset and PR description are updated.


Generated by Claude Code

@benvinegar
benvinegar merged commit 555be5a into main Nov 22, 2026
15 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.

2 participants