3 Commits
Author SHA1 Message Date
Bastien Chanot 8d5d154c28 chore(memory): LRN-149 — background_tasks gates the turn-end signal 2026-09-10 03:03:22 +02:00
Bastien Chanot 0e8018ae7b fix(hooks): no turn-end signal while background work runs
Ending a turn right after spawning a subagent fired the bell and a toast
saying the response was finished, while the work continued. The Stop
payload carries background_tasks, so skip the signal when it is not
empty; the next turn end signals once the work is really done.

Interaction requests still signal during background work. A missing
field still signals, so an older client loses nothing. Also drop the
message suffix when it merely restates the label.
2026-09-10 03:03:22 +02:00
Bastien Chanot 12d7fc1483 fix(hooks): stay silent on events that need no attention
Only turn end and the moments needing the user should signal. Any other
event reaching the hook, such as agent_completed or auth_success, now
exits without emitting, so a subagent finishing rings nothing even if the
Notification matcher is ignored.
2026-09-03 02:56:49 +02:00
3 changed files with 32 additions and 3 deletions
+1
View File
@@ -459,3 +459,4 @@ rules:
- BDR-087 + LRN-146 + BLK-020 capitalized. Branch feature/notify-stop-event, merged to develop (f90ee74). - BDR-087 + LRN-146 + BLK-020 capitalized. Branch feature/notify-stop-event, merged to develop (f90ee74).
- Post-merge regression: toast dead again after re-attach from a RESTORED terminal, bell fine. Root cause [[LRN-147]]: ext hooks only terminals born after its activation; `enablePersistentSessions` restores terminals before it. Fix = disable persistent sessions, or fresh terminal + `dtach -a`. Verified: 3/3 toasts on fresh pty. - Post-merge regression: toast dead again after re-attach from a RESTORED terminal, bell fine. Root cause [[LRN-147]]: ext hooks only terminals born after its activation; `enablePersistentSessions` restores terminals before it. Fix = disable persistent sessions, or fresh terminal + `dtach -a`. Verified: 3/3 toasts on fresh pty.
- Same-day counter-example broke that cause: second session's terminal deaf though created LATER, same window, ext global, shells identical. Trigger unknown; [[LRN-148]] adds the 5s pre-flight test + demotes LRN-147's mechanism claim. - Same-day counter-example broke that cause: second session's terminal deaf though created LATER, same window, ext global, shells identical. Trigger unknown; [[LRN-148]] adds the 5s pre-flight test + demotes LRN-147's mechanism claim.
- Attention signal refined: per-event labels (BDR-087 follow-on), silence on non-attention events, and no turn-end signal while `background_tasks` non-empty ([[LRN-149]]). Payload dump beat the docs: `background_tasks` undocumented for Stop but present on the wire. Branch bugfix/notify-subagent-spawn.
+7
View File
@@ -1414,3 +1414,10 @@ Rule: when editing a doctrine file under structure locks, grep the test's lock s
- **Recovery**: deaf terminal never repairs. Open fresh terminal, pre-flight it, `dtach -a ~/.dtach/<session>`. dtach broadcasts, so old client may stay attached; session never at risk. - **Recovery**: deaf terminal never repairs. Open fresh terminal, pre-flight it, `dtach -a ~/.dtach/<session>`. dtach broadcasts, so old client may stay attached; session never at risk.
- **Diagnostic split (holds)**: bell alive + toast dead = terminal instrumentation. Toast alive + bell dead = client audio ([[BLK-020]]). Neither = bytes never arrive. - **Diagnostic split (holds)**: bell alive + toast dead = terminal instrumentation. Toast alive + bell dead = client audio ([[BLK-020]]). Neither = bytes never arrive.
- **Future**: do NOT assert the born-before-activation cause as established — it fits the first incident, not the second. Unknown trigger is the honest state. - **Future**: do NOT assert the born-before-activation cause as established — it fits the first incident, not the second. Unknown trigger is the honest state.
## LRN-149 — Stop hook payload carries background_tasks; use it to skip premature signals
- **Context**: 2026-09-03. User: "notif à la création d'un sous-agent alors qu'il faudrait pas". Instrumented hook, ran probe subagents: NEITHER subagent creation NOR completion calls the hook. Only event = `Stop`, fired when the turn ends right after spawning. Signal was real but LIED ("Finished responding" while work continued).
- **Pattern**: dump the real payload (`printf '%s' "$payload" >> file.jsonl`) instead of trusting docs — docs list Stop fields without `background_tasks`, the wire has it: `[{"id","type":"subagent","status":"running","description","agent_type"}]`. Rule: on Stop, `(.background_tasks // []) | length` > 0 → exit 0 silent. Next turn end signals for real. Interaction events (permission/question) always signal, background or not.
- **Fail-open**: field absent (older client) → still signal. Missed notification worse than extra one.
- **Cross-session gotcha**: hook is user-scope, so EVERY session runs it. A single-file dump (`> file`) gets overwritten by another project's session — append JSONL and filter on `.cwd`. That accident proved `permission_prompt` fires with `message="Claude needs your permission"` (unexercisable in this session under `defaultMode: auto`).
- **Future**: any hook needing turn-completion semantics must check background_tasks; "turn ended" ≠ "work done". Verified live: Stop with 0 tasks signals, Stop with 1 running subagent silent.
+24 -3
View File
@@ -24,20 +24,41 @@ read_field() {
| tr -d '\000-\037' | cut -c1-160 | tr -d '\000-\037' | cut -c1-160
} }
# How many background tasks are still running as the hook fires.
background_count() {
count=$(printf '%s' "$payload" | jq -r '(.background_tasks // []) | length' 2>/dev/null)
case "$count" in ''|*[!0-9]*) echo 0 ;; *) echo "$count" ;; esac
}
event=$(read_field '.notification_type') event=$(read_field '.notification_type')
[ -n "$event" ] || event=$(read_field '.hook_event_name') [ -n "$event" ] || event=$(read_field '.hook_event_name')
case "$event" in case "$event" in
Stop) label="Finished responding" ;; # Turn end while a subagent still runs is not the real end: stay silent,
# the next turn end will signal once the work is actually done.
Stop) [ "$(background_count)" -eq 0 ] || exit 0
label="Finished responding" ;;
permission_prompt) label="Needs your permission" ;; permission_prompt) label="Needs your permission" ;;
agent_needs_input) label="Asks you a question" ;; agent_needs_input) label="Asks you a question" ;;
idle_prompt) label="Waiting for you" ;; idle_prompt) label="Waiting for you" ;;
elicitation_dialog|elicitation_url_dialog) label="Needs your input" ;; elicitation_dialog|elicitation_url_dialog) label="Needs your input" ;;
*) label="Needs your attention" ;; # anything else (agent_completed, auth_success, quota_*) stays silent:
# signal only for turn end and moments needing the user.
*) exit 0 ;;
esac esac
detail=$(read_field '.message') detail=$(read_field '.message')
[ -z "$detail" ] || label="${label}: ${detail}" if [ -n "$detail" ]; then
# Claude Code's own wording often restates the label ("Claude needs your
# permission"). Append it only when it actually adds something.
short=$(printf '%s' "$detail" | tr '[:upper:]' '[:lower:]' | sed 's/^claude //')
case "$(printf '%s' "$label" | tr '[:upper:]' '[:lower:]')" in
*"$short"*) : ;;
*) label="${label}: ${detail}" ;;
esac
fi
bell=$(printf '\a') bell=$(printf '\a')
esc=$(printf '\033') esc=$(printf '\033')