fix: improve state detection accuracy and notification UX

- Hook reads JSON from stdin (not env vars) matching Claude Code protocol
- end_turn = Idle (not NeedsInput); real questions come from hooks
- Permission prompt (stale tool_use) never becomes Idle
- Notifications auto-expire after 10s (--expire-time)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pierre Martin
2026-03-24 11:29:26 +01:00
parent 0e4ced5b1f
commit 001c453462
5 changed files with 41 additions and 23 deletions

View File

@@ -20,34 +20,35 @@ func DetectState(messages []JSONLMessage, now time.Time) SessionState {
last := messages[len(messages)-1]
// Check idle threshold first
age := time.Duration(0)
if ts, err := time.Parse(time.RFC3339, last.Timestamp); err == nil {
if now.Sub(ts) > IdleThreshold {
return Idle
}
age = now.Sub(ts)
}
if last.Type == "assistant" && last.Message != nil {
switch last.Message.StopReason {
case "end_turn":
return NeedsInput
return Idle
case "tool_use":
for _, block := range last.Message.Content {
if block.Type == "tool_use" && block.Name == "AskUserQuestion" {
return NeedsInput
}
}
// If tool_use is stale (no new JSONL activity), Claude is likely
// waiting for a permission prompt approval.
if ts, err := time.Parse(time.RFC3339, last.Timestamp); err == nil {
if now.Sub(ts) > PermissionStallThreshold {
return NeedsInput
}
// Stale tool_use = permission prompt waiting for approval.
// Never becomes Idle: a pending permission is always NeedsInput.
if age > PermissionStallThreshold {
return NeedsInput
}
return Working
}
}
// Non-assistant messages (progress, user): idle if old enough
if age > IdleThreshold {
return Idle
}
if last.Type == "progress" {
return Working
}