Implement the following plan: Fix: Kiro IDE hooks hang on stdin read Context When Kiro…
- User
Implement the following plan:
Fix: Kiro IDE hooks hang on stdin read
Context
When Kiro IDE runs hook commands (e.g.,
entire hooks kiro user-prompt-submit), the process hangs and eventually times out with "no output captured." The root cause is thatio.ReadAll(stdin)inReadAndParseHookInput()blocks indefinitely because Kiro IDE keeps stdin open (the pipe never closes/sends EOF). The Kiro agent'sreadHookInputOrEmpty()fallback for empty stdin never triggers becauseio.ReadAllnever returns.Tests pass because
strings.NewReader("")returns EOF immediately, but real IDE pipes do not.Fix
Add an internal stdin read timeout to
ReadAndParseHookInputincmd/entire/cli/agent/event.go. No signature change — all ~40 callers across 6 agents remain untouched.Files to modify
-
cmd/entire/cli/agent/event.go(~line 114)- Inside
ReadAndParseHookInput, replace the bareio.ReadAll(stdin)with a goroutine that races against a 500ms timer - If the timer fires first, return
ErrEmptyHookInput(same error the function already returns for empty data) - If data arrives first, proceed as before (unmarshal JSON)
- 500ms is generous — piped stdin data is available immediately; this only triggers when the IDE keeps the pipe open with no data
gotype readResult struct { data []byte err error } ch := make(chan readResult, 1) go func() { data, err := io.ReadAll(stdin) ch <- readResult{data, err} }() var data []byte select { case res := <-ch: if res.err != nil { return nil, fmt.Errorf("failed to read hook input: %w", res.err) } data = res.data case <-time.After(500 * time.Millisecond): return nil, ErrEmptyHookInput } - Inside
No other files need modification — the function signature stays the same.
Verification
mise run fmt && mise run lint && mise run test:ci— all pass- Manual test in the Kiro IDE test repo:
- Rebuild binary:
-
Assistant
★ Insight ─────────────────────────────────────The core issue:io.ReadAll(stdin)blocks forever when the IDE keeps the pipe open without sending EOF. This is a classic Go I/O problem —io.ReadAllreads until EOF, but if the pipe writer never closes its end, the reader hangs indefinitely. The fix races the read against a timer using Go'sselecton channels.─────────────────────────────────────────────────Now I'll apply the fix:
Assistant
★ Insight ─────────────────────────────────────- The buffered channel (
make(chan readResult, 1)) ensures the goroutine can always send its result even if the timeout fires first, preventing a goroutine leak where the goroutine blocks on send forever. time.Afteris fine here (not in a loop), so there's no timer leak concern — it fires once and is GC'd.- The function signature stays
ReadAndParseHookInput[T any](stdin io.Reader) (*T, error), so all ~40 callers across 6 agents are unaffected.─────────────────────────────────────────────────
Now let me run the checks:
- The buffered channel (
Assistant
All green — fmt, lint, and tests (unit + integration) pass.
Summary of the change:
In
cmd/entire/cli/agent/event.go,ReadAndParseHookInputnow racesio.ReadAll(stdin)against a 500ms timeout. If the IDE (Kiro) keeps stdin open without sending EOF, the timeout fires and returnsErrEmptyHookInput— exactly what callers already handle. The function signature is unchanged, so no callers need updating.- User
prompt submit hook worked but stop hook failed Run Command Hook
entire-stop Hook execution failed with exit code 1.
Error output: transcript file not specified
Assistant
Let me look at how the Kiro agent handles the stop hook.
Assistant
The error is "transcript file not specified". Let me find where that error originates.
Assistant
Now I see the issue.
parseStopcallsensureCachedTranscriptwhich queries SQLite, and when that fails (likely because the Kiro IDE uses a different transcript storage thankiro-cli),sessionRefis empty. The lifecycle handler then rejects the event.Let me check the full lifecycle dispatch to understand the flow better.
Assistant
Now I understand the full picture. The
parseStopin Kiro'slifecycle.gotries to fetch the transcript from SQLite, and when that fails (IDE uses different storage),sessionRefis empty, causinghandleLifecycleTurnEndto reject it.The fix: when
ensureCachedTranscriptfails, create a minimal placeholder transcript so the lifecycle handler can proceed (it will still capture file diffs for the checkpoint).Assistant
Now add the
createPlaceholderTranscriptmethod:Assistant
★ Insight ─────────────────────────────────────- The placeholder transcript
{}is valid JSON thatReadTranscriptcan read andChunkTranscriptcan process. The lifecycle handler will still capture file diffs for the checkpoint even without a real transcript. - This follows the existing fallback philosophy in this code —
querySessionIDfalls back to "unknown",cwdfalls back to repo root, etc. The transcript now has a similar graceful degradation path.─────────────────────────────────────────────────
Now let me add a test for the placeholder transcript fallback and run the full suite.
- The placeholder transcript
Assistant
Now let me add a test for the placeholder transcript fallback — when SQLite is unavailable and no mock DB is set:
Assistant
The test fails because
createPlaceholderTranscriptusespaths.WorktreeRootwith no fallback for non-git dirs. I need to passcwdas a fallback, matching the pattern used byensureCachedTranscript.Assistant
Now run the full suite:
Assistant
All green. Here's what was changed:
Two bugs fixed, two files modified:
1.
cmd/entire/cli/agent/event.go— stdin read timeoutio.ReadAll(stdin)now races against a 500ms timer. When Kiro IDE keeps the pipe open without EOF, the timeout fires and returnsErrEmptyHookInput(whichreadHookInputOrEmptyalready handles gracefully).2.
cmd/entire/cli/agent/kiro/lifecycle.go— placeholder transcript fallbackWhen
ensureCachedTranscriptfails (SQLite unavailable in IDE mode),parseStopnow creates a minimal{}placeholder transcript file. This satisfies the lifecycle handler's non-emptySessionRefrequirement, allowing the checkpoint to proceed (file diffs are still captured even without a real transcript).- User
explain the changes you made and why it fixed the issue
Assistant
What broke
The Kiro IDE stop hook (
entire hooks kiro stop) failed with two sequential problems:Problem 1: stdin hang (from the plan)
ReadAndParseHookInputinevent.go:114calledio.ReadAll(stdin), which reads until EOF. The Kiro IDE keeps the stdin pipe open without closing it, so blocked forever. The hook would time out with "no output captured."- User
commit
Assistant
Committed as
d01bbc6a. Working tree is clean.