Skip to content

feat(#383): interactive relay — bidirectional communication with headless runs - #638

Merged
admarble merged 2 commits into
mainfrom
feature/383-interactive-relay-bidirectional-communication-with
May 13, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/383-interactive-relay-bidirectional-communication-with

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

Closes #383. File-based IPC channel that lets users send query/directive/abort messages into a running headless sequant run session via a new sequant prompt CLI command, with responses tailed via sequant watch. The PostToolUse hook surfaces inbox messages back to Claude wrapped in an anti-derailment framing block.

  • sequant prompt <issue> "<msg>" — write to <worktree>/.sequant/relay/inbox.jsonl; refuses on dead PIDs.
  • sequant watch <issue> — tail outbox via fs.watch() + polling fallback.
  • sequant status — gains a Relay column for active runs.
  • --no-relay flag on sequant run for opt-out; settings.run.relay defaults to true.

How it works

  1. phase-executor sets SEQUANT_RELAY=true + SEQUANT_RELAY_FRAME=<path> in the agent env when relay is enabled.
  2. post-tool.sh sources relay-check.sh on every PostToolUse when the env var is set.
  3. The hook fast-paths (test -s on inbox.jsonl) when nothing is pending. On non-empty inbox, it renders the framing template (with the 6 verbatim rules) and emits all unread messages as a single block to stdout — Claude Code surfaces this as additional context.
  4. A cursor file (.cursor) is advanced atomically (temp + rename) so each message is shown exactly once.
  5. On phase teardown, batch-executor archives inbox.jsonl + outbox.jsonl + meta.json to .sequant/logs/relay/<issue>-<phase>-<ts>/ before worktree teardown, preserving transcripts.

AC Coverage (25 ACs + 4 derived)

Group ACs Status
Relay protocol (dir, JSONL, schemas, atomic writes, cursor, archive) AC-1..6, AC-11 MET
PostToolUse hook (sourcing, fast path, framing, opt-in, perf) AC-7..10, AC-12, AC-13 MET
Framing (template + 6 verbatim rules + no SKILL.md leakage) AC-14, AC-15, AC-16 MET
CLI commands (prompt, watch, status) AC-17..19, AC-17a, AC-17b MET
PID tracking (pidfile, LockManager reuse, stale cleanup, concurrency) AC-20..23 MET
State schema (optional relay field, lifecycle) AC-24, AC-25 MET
Derived (perf, archive ordering, SIGKILL recovery, meta.json) AC-D1..D4 MET

Bugs found and fixed during testgen→exec

  • relay-check.sh short-circuit bug: the initial guard cond && return 0 2>/dev/null || exit 0 exits prematurely when cond is false (the relay-enabled branch) due to bash precedence. Switched to an explicit if block. Without this fix the hook would always exit before reading the inbox.
  • archiveRelayDir idempotency: repeated deactivation calls now return archived=false when both inbox and outbox are absent, instead of creating empty archive directories.
  • Commander --no-relay wiring: RunOptions.noRelay was inconsistent with how Commander surfaces --no-X flags. Renamed to relay?: boolean (matches chain pattern) and the resolver checks relay === false.

Test plan

  • npm run build — passes
  • npm test — 3297 tests pass across 147 files (111 new tests across 12 new files)
  • npm run lint:skill-calls — no violations
  • templates/hooks/relay-check.sh smoke-tested manually with a multi-message inbox; produces correct frame with --- between messages and single [SEQUANT RELAY] header
  • AC-16: SKILL.md isolation grep across all three skill directories returns zero matches
  • Live integration: sequant run 1 in one terminal, sequant prompt 1 "status?" in another — recommend during human review

Out of scope (follow-ups)

…less runs

Adds a file-based IPC channel between the user terminal and a running
`sequant run` session. Users can now send query/directive/abort messages
into a headless Claude session via `sequant prompt <issue> "<msg>"`, with
responses tailed via `sequant watch <issue>`.

Implementation:
- src/lib/relay/: protocol (types, paths, writer, reader, frame, archive,
  pid, activation) — 9 modules, all I/O guarded for filesystem failures.
- templates/hooks/relay-check.sh: sourced by post-tool.sh on every
  PostToolUse when SEQUANT_RELAY=true. Fast path is a single env-var test
  + `test -s` check; slow path renders one framing block per invocation.
- templates/relay/frame.txt: framing template with the 6 verbatim
  anti-derailment rules from the issue body.
- bin/cli.ts: new `prompt` and `watch` commands; `--no-relay` flag on
  `run` to opt out.
- src/lib/workflow/: relay lifecycle wired into batch-executor (activate
  on run start, deactivate before worktree teardown so archives land in
  .sequant/logs/relay/<issue>-<phase>-<ts>/). phase-executor injects
  SEQUANT_RELAY + SEQUANT_RELAY_FRAME env vars.
- state-schema.ts/state-manager.ts: optional `relay` field on IssueState
  (legacy state still parses); `setRelayState` / `incrementRelayMessageCount`.

Tests: 12 new files, 111 new tests (72 unit + 39 integration). All ACs
are covered, including the AC-16 SKILL.md isolation guard that scans
all three skill directories.

Bug fixes discovered during testing:
- relay-check.sh used `cond && return 0 || exit 0`; bash precedence makes
  this exit prematurely when cond is false. Switched to `if [[...]]; then
  return ...; fi`. Without this fix, the hook would short-circuit on the
  very env-var test it was supposed to gate on.
- archiveRelayDir now returns archived=false when both inbox and outbox
  are absent, so repeated deactivation calls are idempotent rather than
  creating empty archive dirs.
@admarble
admarble merged commit 515b616 into main May 13, 2026
5 checks passed
@admarble
admarble deleted the feature/383-interactive-relay-bidirectional-communication-with branch May 13, 2026 23:14
admarble added a commit that referenced this pull request May 14, 2026
admarble added a commit that referenced this pull request May 14, 2026
…nt (#646)

* fix(#645): install relay hook so messages reach the agent

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.

* fix(watch): honor cwd override when resolving outbox path

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.
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.

Interactive relay: bidirectional communication with headless runs

1 participant