Skip to content

fix(#645): install relay hook + drift guard so messages reach the agent - #646

Merged
admarble merged 2 commits into
mainfrom
feature/645-bug-383-relay-messages-silently-lost-no-outbox
May 14, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/645-bug-383-relay-messages-silently-lost-no-outbox

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

Fixes #645. Interactive relay messages were silently consumed because .claude/hooks/post-tool.sh (the active PostToolUse hook) did not source relay-check.sh, and .claude/hooks/relay-check.sh did not exist in this repo at all. PR #638 added both to templates/hooks/ but never updated the installed copies.

  • Phase 1 (root cause): install relay-check.sh, insert the SEQUANT_RELAY sourcing block into post-tool.sh (surgical — leaves local-only test-coverage + post-merge-cleanup blocks intact)
  • Phase 2 (drift guard): fix DEFAULT_MIRROR_PAIRS source path (hooks → .claude/hooks); new Vitest asserts byte equality + sourcing block presence
  • Phase 3 (UX improvements from gaps 3 + 5): sequant watch exits cleanly when relay deactivates instead of polling forever; archive meta.json records inboxCount + outboxCount separately so unanswered queries are visible post-hoc

Out of scope: Gaps 4 (prompt UX polish), 6 (stale phase label), 7 (signal-based abort), and the broader .claude/hooks/ drift in pre-tool.sh. See issue comments for rationale.

Test plan

Unit tests (already passing in CI):

  • npm run build (tsc) — clean
  • npx vitest run src/lib/relay src/lib/merge-check src/commands/__tests__ — 7 new tests, 79 total touched, all passing
  • Adversarial: corrupting .claude/hooks/relay-check.sh makes hook-sync.test.ts fail; restoring it passes

Manual verification (AC-3 — please run before merge):

# Terminal 1 — start a small run
gh issue list --state open | head
npx sequant run <issue> --no-mcp --phases exec

# Terminal 2 — within 30s
npx sequant prompt <issue> "status?"
npx sequant watch <issue>

Expected:

  • Outbox reply appears in watch within ~30s
  • After the run completes, watch prints Run for #<issue> ended. Archive: … and exits 0
  • cat <archive>/meta.json shows both inboxCount and outboxCount fields

If the smoke fails (no outbox reply), Gap 2 (sub-agent hook isolation) remains a live hypothesis; file a follow-up issue with the sentinel-log diagnostic plan in the original issue body.

Files touched

  • .claude/hooks/post-tool.sh (+11) — insert SEQUANT_RELAY sourcing block
  • .claude/hooks/relay-check.sh (new, +107) — byte-identical to template
  • src/commands/watch.ts (+75 / −1) — dead-relay detection
  • src/lib/relay/archive.ts (+7) — inbox/outbox split counts
  • src/lib/relay/types.ts (+9) — schema additions
  • src/lib/merge-check/types.ts (+1 / −1) — mirror-pair source path
  • src/lib/merge-check/merge-check.test.ts (+1 / −1) — fixture update
  • src/lib/relay/__tests__/hook-sync.test.ts (new, +44) — drift guard
  • src/lib/relay/__tests__/archive-split-counts.test.ts (new, +91) — split-count coverage
  • src/commands/__tests__/watch-dead-relay.test.ts (new, +71) — watch behavior

Fixes #645

admarble added 2 commits May 14, 2026 02:18
Issue #645 reported that interactive relay messages disappear: inbox
entries pile up while `.cursor` is never written and no outbox reply
appears. Empirical evidence showed PostToolUse hooks were never
advancing the read cursor.

Root cause: PR #638 added `templates/hooks/relay-check.sh` and the
SEQUANT_RELAY sourcing block in `templates/hooks/post-tool.sh`, but
the installed copies in `.claude/hooks/` (referenced by
`.claude/settings.json`) were never updated. Worktrees inherit the
stale checked-in `post-tool.sh`, so the hook chain never sourced
relay-check.sh. The mirror-pair config that should have caught this
drift (`src/lib/merge-check/types.ts`) had `source: "hooks"` — a
directory that doesn't exist — so the check silently no-op'd.

Phase 1 — root-cause fix
- Add `.claude/hooks/relay-check.sh` (byte-identical to template)
- Surgically insert the 11-line SEQUANT_RELAY sourcing block into
  `.claude/hooks/post-tool.sh`. Wholesale sync is deferred because
  the installed file has two local-only features (test coverage,
  post-merge cleanup) that are not in the template.

Phase 2 — drift guard
- Fix DEFAULT_MIRROR_PAIRS source path: "hooks" -> ".claude/hooks"
- Add Vitest assertion that relay-check.sh stays byte-identical to
  its template and that post-tool.sh keeps sourcing it

Phase 3 — watch + archive metadata
- `sequant watch` now detects relay deactivation (pidfile removed
  or absent at startup) and exits with a useful pointer to the
  archive instead of polling forever (Gap 3)
- Archive `meta.json` records `inboxCount` + `outboxCount`
  separately so unanswered queries are visible post-hoc.
  `messageCount` preserved for back-compat; schema accepts older
  archives without the new fields (Gap 5)

Out of scope: Gap 4 (prompt UX polish), Gap 6 (stale phase label),
Gap 7 (signal-based abort fallback), broader `.claude/hooks/` drift
reconciliation — separate follow-ups if requested.
watch.ts now passes the resolved cwd to outboxPathFor() so the watch
command consistently uses the same root for pid + outbox + archive
lookups. Pre-existing prod behavior unchanged (defaults to
process.cwd()); enables clean test isolation.
@admarble
admarble merged commit 5124755 into main May 14, 2026
5 checks passed
@admarble
admarble deleted the feature/645-bug-383-relay-messages-silently-lost-no-outbox branch May 14, 2026 07:28
admarble added a commit that referenced this pull request May 14, 2026
…#648)

Three independent CLI UX improvements that complete the user-visible
gap list from #645 after the root-cause fix in #646. Each addresses
one of the deferred gaps:

Gap 7 — sequant abort <issue>
  Out-of-band escape hatch that bypasses the relay inbox entirely.
  Reads state.relay.pid (or the per-issue pidfile), sends SIGINT with
  a 10s grace, then escalates SIGTERM, then SIGKILL. Honors the
  existing ShutdownManager teardown so locks + relay archive run
  cleanly. Distinct from `prompt --type abort` by design: that is the
  polite path that needs the agent to read inbox; abort is the
  reliable kill that doesn't.

Gap 6 — accurate currentPhase in prompt confirmation
  prompt now reads state.json with a fresh StateManager immediately
  before printing, bypassing any cached snapshot from the start of
  the command. When no fresh phase can be resolved, the label is
  omitted entirely — a wrong phase is worse than no phase.

Gap 4 — sequant prompt --wait <seconds>
  Optional reply tailing: after appending to the inbox, poll the
  outbox for a response whose inReplyTo matches the new message ID.
  Print inline on hit, exit 1 with a "may be archived without
  response" message on timeout. Skipped for type=abort (no reply
  expected). Default behavior unchanged when --wait is absent.

11 new tests across abort + prompt-wait surfaces; 353 tests passing
on the touched surface.
admarble added a commit that referenced this pull request May 14, 2026
Closes the broader hook-drift recurrence risk noted in #646. Before
this commit, `.claude/hooks/post-tool.sh` carried two local-only
features (test coverage analysis, post-merge worktree cleanup) that
were not in `templates/hooks/post-tool.sh`, and `templates/hooks/`
carried four improvements (CLAUDE_PLUGIN_DATA log dir, multi-PM test
detection, cross-shell marker discovery, $HOOK_LOG variable) that
were not in `.claude/hooks/`. PR #646 added a partial drift guard for
relay-check.sh only; this commit closes the gap for the whole
directory.

Changes
- templates/hooks/post-tool.sh: port the two local-only blocks
  (TEST COVERAGE ANALYSIS, POST-MERGE WORKTREE CLEANUP) into the
  template so it is a strict superset of every prior `.claude/hooks/`
  feature.
- .claude/hooks/post-tool.sh, .claude/hooks/pre-tool.sh: replace with
  byte-identical copies of the now-superset templates.
- src/lib/relay/__tests__/hook-sync.test.ts: widen drift guard from
  relay-check.sh-only to "every file in templates/hooks/ must be
  byte-identical in .claude/hooks/." Files local to .claude/hooks/
  (e.g. capture-tokens.sh) are still permitted.
- scripts/sync-hooks.sh + npm run sync:hooks: regenerate
  .claude/hooks/ from templates after editing a template hook.

Adversarial verification: appending "# drift" to any hook file
causes the test to fail; removing the line passes again.

This removes the recurrence surface that originally caused #645.
admarble added a commit that referenced this pull request Sep 9, 2026
Three independent CLI UX improvements that complete the user-visible
gap list from #645 after the root-cause fix in #646. Each addresses
one of the deferred gaps:

Gap 7 — sequant abort <issue>
  Out-of-band escape hatch that bypasses the relay inbox entirely.
  Reads state.relay.pid (or the per-issue pidfile), sends SIGINT with
  a 10s grace, then escalates SIGTERM, then SIGKILL. Honors the
  existing ShutdownManager teardown so locks + relay archive run
  cleanly. Distinct from `prompt --type abort` by design: that is the
  polite path that needs the agent to read inbox; abort is the
  reliable kill that doesn't.

Gap 6 — accurate currentPhase in prompt confirmation
  prompt now reads state.json with a fresh StateManager immediately
  before printing, bypassing any cached snapshot from the start of
  the command. When no fresh phase can be resolved, the label is
  omitted entirely — a wrong phase is worse than no phase.

Gap 4 — sequant prompt --wait <seconds>
  Optional reply tailing: after appending to the inbox, poll the
  outbox for a response whose inReplyTo matches the new message ID.
  Print inline on hit, exit 1 with a "may be archived without
  response" message on timeout. Skipped for type=abort (no reply
  expected). Default behavior unchanged when --wait is absent.

11 new tests across abort + prompt-wait surfaces; 353 tests passing
on the touched surface.
admarble added a commit that referenced this pull request Sep 9, 2026
Closes the broader hook-drift recurrence risk noted in #646. Before
this commit, `.claude/hooks/post-tool.sh` carried two local-only
features (test coverage analysis, post-merge worktree cleanup) that
were not in `templates/hooks/post-tool.sh`, and `templates/hooks/`
carried four improvements (CLAUDE_PLUGIN_DATA log dir, multi-PM test
detection, cross-shell marker discovery, $HOOK_LOG variable) that
were not in `.claude/hooks/`. PR #646 added a partial drift guard for
relay-check.sh only; this commit closes the gap for the whole
directory.

Changes
- templates/hooks/post-tool.sh: port the two local-only blocks
  (TEST COVERAGE ANALYSIS, POST-MERGE WORKTREE CLEANUP) into the
  template so it is a strict superset of every prior `.claude/hooks/`
  feature.
- .claude/hooks/post-tool.sh, .claude/hooks/pre-tool.sh: replace with
  byte-identical copies of the now-superset templates.
- src/lib/relay/__tests__/hook-sync.test.ts: widen drift guard from
  relay-check.sh-only to "every file in templates/hooks/ must be
  byte-identical in .claude/hooks/." Files local to .claude/hooks/
  (e.g. capture-tokens.sh) are still permitted.
- scripts/sync-hooks.sh + npm run sync:hooks: regenerate
  .claude/hooks/ from templates after editing a template hook.

Adversarial verification: appending "# drift" to any hook file
causes the test to fail; removing the line passes again.

This removes the recurrence surface that originally caused #645.
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.

bug(#383): relay messages silently lost — no outbox, phase ends, watch keeps tailing dead session

1 participant