Skip to content

fix(hooks): avoid blocking on empty stdin - #360

Merged
taosu0216 merged 1 commit into
mainfrom
fix/issue-356-hook-stdin
Jun 24, 2026
Merged

taosu0216 merged 1 commit into
mainfrom
fix/issue-356-hook-stdin

Conversation

@taosu0216

@taosu0216 taosu0216 commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Root Cause

json.load(sys.stdin) waits for EOF. Kiro IDE runCommand and similar hook runners can leave stdin open while sending no payload, so the hook process blocks before it can emit workflow state.

Tests

  • pnpm --filter @mindfoldhq/trellis-core build
  • pnpm --filter @mindfoldhq/trellis typecheck
  • pnpm --filter @mindfoldhq/trellis lint
  • pnpm --filter @mindfoldhq/trellis exec vitest run test/regression.test.ts -t "#356|inject-workflow-state.py emits BeforeAgent|silent exit 0 when not a Trellis project"
  • pnpm --filter @mindfoldhq/trellis exec vitest run test/templates/shared-hooks.test.ts
  • python3 -m py_compile packages/cli/src/templates/shared-hooks/inject-workflow-state.py

Fixes #356

Summary by CodeRabbit

  • Bug Fixes
    • Improved hook handling so it no longer hangs when input is left open without data.
    • Added safer fallback behavior for missing, delayed, or invalid input, allowing the process to exit cleanly.
    • Verified the hook still produces the expected workflow state output under open-stdin conditions.

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

inject-workflow-state.py replaces its blocking json.load(sys.stdin) call with a new _load_hook_input() helper that reads stdin on a daemon thread with a 0.2 s timeout, returning {} on timeout, error, or invalid JSON. A regression test is added that spawns the hook with an open, empty stdin and asserts it exits cleanly within 1.5 s.

Changes

Stdin hang fix and regression coverage

Layer / File(s) Summary
Non-blocking stdin reader
packages/cli/src/templates/shared-hooks/inject-workflow-state.py
Adds queue and threading imports. Introduces _load_hook_input(), which spawns a daemon thread to call sys.stdin.read(), waits up to 0.2 s on a bounded queue, and returns {} on timeout, exception, empty/whitespace input, or JSON parse failure. main() calls this helper instead of the former direct json.load(sys.stdin).
Regression test for open-stdin hang
packages/cli/test/regression.test.ts
Adds spawn to the node:child_process import. Inserts async test [#356] that spawns the hook as a Python subprocess with piped stdio, accumulates output, applies a 1.5 s SIGKILL watchdog, and asserts exit code 0, no timeout, and <workflow-state> in stdout.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 A rabbit once sat by a pipe,
Waiting for data that ne'er would arrive.
Now a thread taps its paw — just 0.2 ticks —
Then hops along merrily, returning {}.
No more stuck hooks, no more frozen IDE!
✨ Stdin shall block us no longer, yippee!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preventing blocking on empty stdin.
Linked Issues check ✅ Passed The code and regression test address #356 by handling open, empty stdin without hanging while preserving normal behavior.
Out of Scope Changes check ✅ Passed The changes are focused on the stdin hang fix and its regression test, with no obvious unrelated additions.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/issue-356-hook-stdin

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

@taosu0216
taosu0216 marked this pull request as ready for review June 24, 2026 12:46
@taosu0216
taosu0216 merged commit 64fcae9 into main Jun 24, 2026
1 of 2 checks passed
@taosu0216
taosu0216 deleted the fix/issue-356-hook-stdin branch June 24, 2026 12:48
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.

inject-workflow-state.py hook hangs on platforms that don't pipe stdin (Kiro/IDE runCommand)

1 participant