orca icon indicating copy to clipboard operation
orca copied to clipboard

fix(hooks): drain POSIX hook stdin without PATH

Open nolainjin opened this issue 4 months ago â€ĸ 1 comments

Summary

Codex/Grok/Claude POSIX managed hooks no longer depend on PATH just to read or drain hook stdin.

The shared hook prelude used cat in two places:

  • payload=$(cat) in every generated POSIX hook script
  • cat >/dev/null 2>&1 || : in missing-script/no-op launchers

When a tool launches a Codex worker with a stripped or minimal PATH, those reads can fail before stdin is consumed. That turns an otherwise harmless no-op into hook exit noise or a broken pipe while Codex is still writing the hook payload.

This changes the shared reader to prefer /bin/cat before falling back to cat, so normal macOS/Linux/WSL shells consume stdin even when PATH is empty.

Validation

  • corepack pnpm exec vitest run --config config/vitest.config.ts src/main/agent-hooks/managed-hook-stdin-lifecycle.test.ts
  • corepack pnpm exec oxlint src/main/agent-hooks/hook-stdin-contract.ts src/main/agent-hooks/managed-hook-stdin-lifecycle.test.ts
  • corepack pnpm run typecheck:node

Related: #8110

nolainjin avatar Jul 27 '26 07:07 nolainjin

Review Change Stack

No actionable comments were generated in the recent review. 🎉

â„šī¸ Recent review info
âš™ī¸ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 58b55bb8-7625-48b8-8418-bc446c77f429

đŸ“Ĩ Commits

Reviewing files that changed from the base of the PR and between 80b4b92c44380dbaf3cb21af58600e4365d4a039 and 6a2698846a6ea816375976de97969b9dd2151434.

📒 Files selected for processing (7)
  • src/main/agent-hooks/hook-stdin-contract.ts
  • src/main/agent-hooks/installer-utils.test.ts
  • src/main/agent-hooks/managed-hook-stdin-lifecycle.test.ts
  • src/main/antigravity/hook-service.test.ts
  • src/main/codex/hook-service-wsl-runtime.test.ts
  • src/main/cursor/hook-service.test.ts
  • src/main/grok/hook-service.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/agent-hooks/hook-stdin-contract.ts

📝 Walkthrough

Walkthrough

Introduces a reusable POSIX stdin reader used by stdin draining and generated payload-capture scripts. Updates lifecycle assertions to reference the shared reader contract and adds coverage for executing generated hooks and missing-script launchers with an empty PATH, verifying successful exits and the absence of stdin errors.

đŸšĨ Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check âš ī¸ Warning The description covers summary and validation, but it omits required template sections for screenshots, testing checklist, AI review report, security audit, and notes. Add the missing template sections: Screenshots, the full Testing checklist, AI Review Report, Security Audit, and Notes, even if some are brief.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and matches the main change: POSIX hook stdin draining no longer depends on PATH.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

â¤ī¸ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 27 '26 07:07 coderabbitai[bot]

Reviewed with an independent reproduction on both macOS and Ubuntu 20.04, driving the actual generated codex-hook.sh rather than a unit test alone.

Confirmed. Under a stripped PATH the old payload=$(cat) fails before consuming stdin, emitting cat: No such file or directory and giving the writer EPIPE — the broken pipe (os error 32) symptom in #8110. Your new test genuinely fails on revert, so it is a real regression guard.

One blocking defect, fixed and pushed. The change updated the shared constants but left five other test files asserting the literal old cat strings, breaking 10 pre-existing tests. CI did not catch this — the only check that ran was track-community-pr (7s); the vitest suite never ran. Those five now derive their assertions from the exported constants, so the contract has one source of truth.

One improvement, also pushed. I changed the reader to command -p cat instead of /bin/cat. command -p resolves from the shell's built-in default PATH, so it additionally survives hosts with no /bin/cat (NixOS) and ignores a worktree-local cat that could otherwise capture the payload. Verified directly: old form exits 127 under PATH="", new form reads the payload and exits 0.

I considered a pure-shell read loop, which needs no external binary, and rejected it with measurements: ~13x slower (272ms vs 21ms at 1MB) on a per-tool-call hot path, and it corrupts payloads — adds a trailing newline and mangles backslashes, disqualifying for JSON. The chosen shape also mirrors the file's own existing WINDOWS_HOOK_STDIN_READER precedent.

Honest gap worth stating: this stops the noise and the broken pipe, but does not restore delivery under a stripped PATH — curl is also unqualified, so the hook exits 0 having sent nothing. It converts a loud failure into a silent safe one. That matches your PR description, but it means #8110 is not fully closed.

Validation: 501 tests passing across the hook trees, typecheck exit 0.

OrcaWin avatar Jul 27 '26 22:07 OrcaWin