Skip to content

harness-sdk: a fenced harness exits even while a task holds an event sender - #1826

Merged
nikhilunni merged 2 commits into
mainfrom
fix/harness-fenced-exit-hang
Nov 19, 2026
Merged

nikhilunni merged 2 commits into
mainfrom
fix/harness-fenced-exit-hang

Conversation

@nikhilunni

Copy link
Copy Markdown
Contributor

Summary

A session stopped taking prompts after a burst of prompts broke its harness connection.

  • The harness reconnected, and the host answered Superseded: it had already moved to a newer attach epoch.
  • No newer token arrived within SUPERSEDED_TOKEN_GRACE, so the harness took its "fenced exit". That exit is designed to end the process, so that agentd starts a fresh harness on the host's next attach.
  • The exit never finished. It dropped the command channel, then drained the event channel until every sender was gone.
  • The engine had stopped, but another task still held a sender. The hook and MCP socket servers give each connection task a clone, and aborting their listener does not stop a connection that is in flight (here, the PreToolUse hook of a running Bash call).
  • The drain waited forever, and the process ignored reconnect nudges. agentd kept "reattaching, not respawning" to it. Every prompt to the session became prompt_undeliverable until the harness process was killed by hand.

Fix: all three transport exits use close_out:

  1. drop the command sender;
  2. close the event receiver, then drain it, so the drain ends while other senders are still alive;
  3. wait at most ENGINE_EXIT_GRACE (10 s) for the engine, then abort it.

The start-up error path already closed the receiver; the fenced and normal exits did not.

Test plan

  • Two new tests in engram-harness-sdk/tests/attach_token.rs hold a stray event sender: one with an engine that finishes, one with an engine that never finishes. On the old code both hang until the test timeout. With the fix they exit at the expected grace periods.
  • cargo nextest -p engram-harness-sdk (46) and -p engram-harness-noop.
  • cargo clippy -D warnings on macOS, and for aarch64-unknown-linux-musl (sdk, harness-claude, harness-noop); cargo fmt.

🤖 Generated with Claude Code

nikhilunni and others added 2 commits November 19, 2026 13:11
…sender

A harness whose attach was superseded, and that saw no newer token in its
grace period, never exited. Its "fenced exit" dropped the command channel
and drained the event channel until every sender was gone, but a task
other than the engine still held a sender (the per-connection hook and
MCP socket tasks clone it, and aborting their listener does not stop a
connection in flight). The drain waited forever, the process ignored
reconnect nudges, and agentd kept reattaching to it instead of starting a
fresh harness, so the session took no prompts until someone killed it.

All three exits (token unavailable, fenced, end of transport) now go
through `close_out`: drop the command sender, close the event receiver
before draining it, and wait at most ENGINE_EXIT_GRACE (10 s) for the
engine, aborting it after that. Two tests hold a stray sender, one with an
engine that never finishes; both hang on the old code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nikhilunni
nikhilunni merged commit 3ad7e19 into main Nov 19, 2026
28 checks passed
@nikhilunni
nikhilunni deleted the fix/harness-fenced-exit-hang branch November 19, 2026 21:57
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.

1 participant