fix(broker): verify PTY turn submission

Merged
KJ
2mo ago
+3256-509

Summary

  • keep broker custody of PTY messages until the harness accepts the turn; terminal/editor echo alone no longer acknowledges delivery
  • recover visibly parked composers with bounded submit-key-only retries, never by replaying the body
  • protect attached humans by pausing during interactive hold and cancelling automatic recovery when human PTY input takes ownership
  • expose unconfirmed, resubmitted, verified, and failed lifecycle events through broker status and typed SDK contracts
  • document per-harness submit strategies and the broker restart requirement

Root cause and real-PTY evidence

The previous worker treated the formatted body appearing in PTY output as successful delivery. Codex can echo a pasted multiline body while leaving it parked in the composer, so the broker released custody even though no turn started. The old timeout fallback could also acknowledge an unverified delivery.

Authenticated Codex CLI 0.160.0 probes established the mechanism:

  • idle delivery and a follow-up queued until idle both submit and receive the expected response
  • body plus CR in one burst reproduces the parked composer
  • End followed by a distinct delayed CR advances the current Codex composer; LF remains a guarded final fallback for the older field state
  • recovery is attempted only while the exact message tail remains in the live composer, so an accepted turn is never submitted twice

No running fleet node, broker, or agent service was restarted or reconfigured during reproduction.

Behavior

Initial submit strategy stays harness-specific:

  • Codex, Claude, Muse, and Devin use a distinct delayed CR; Devin keeps bracketed paste
  • OpenCode, Gemini, Cursor, and other PTY harnesses retain their established typed body plus CR path
  • verification requires post-echo activity or a proven cleared idle composer
  • parked bodies receive at most two submit-only recovery gestures; inconclusive or exhausted delivery becomes explicit delivery_failed and blocked_on_send
  • human input cancels recovery and transfers composer ownership without pressing another key

The published macOS Codex app and standalone Connect app-server/IPC paths do not use broker PTY injection and are not affected by this bug.

Validation

  • cargo test -p agent-relay-broker --lib -- --test-threads=1: 1338 passed, 0 failed, 7 ignored
  • cargo test -p relay-pty: 256 passed, 0 failed, 1 ignored doctest
  • cargo clippy -p agent-relay-broker --all-targets -- -D warnings
  • npm --prefix packages/sdk run build
  • npm --prefix packages/harness-driver run check
  • swift build --target AgentRelayBrokerSDK
  • real ignored Codex probes:
    • live_codex_idle_and_queued_to_idle_delivery
    • live_codex_burst_parks_then_submit_only_recovery_accepts
  • git diff --check

The local Swift toolchain has no XCTest module, so swift test cannot load the test target; the changed AgentRelayBrokerSDK target builds successfully.

Rollout

Planned for broker 13.2.0. Existing brokers and PTY workers must be upgraded and restarted to pick up the acceptance verifier and recovery behavior.

Founder approved merge after exact-head CI is green and all review feedback is resolved.

Latest verification (41a41a1ad)

  • Targeted parked-composer RelayFlow: fixed signature at the exact PR head.
  • Focused broker delivery-verification suite: 35 passed, 0 failed.
  • Authenticated native Codex CLI 0.159.0 end-to-end: one Relay body injection, one submit-only recovery, broker delivery_verified, and exactly one visible RELAY_1891_REAL_CODEX_ACK response. The disposable Codex home was removed after the run.
  • Fresh CI and review gate are tracked on this PR.

[!NOTE]
High Risk
Changes core PTY injection, fleet withheld-ack timing, and delivery failure semantics; misclassification could drop or duplicate turns until brokers and PTY agents are restarted.

Overview
PTY message delivery no longer treats terminal echo as success. Broker custody stays until the harness actually accepts a turn—via activity markers, a cleared idle composer, or (for cat) process echo—instead of acknowledging on timeout when echo parsing fails.

Parked composers get bounded submit-only recovery (up to three attempts, body never replayed): Codex uses End plus distinct CR and guarded LF; other harnesses use distinct CR/LF per harness docs. Failed or inconclusive acceptance emits delivery_failed and blocks further sends until resolved. Human PTY input cancels automatic recovery and fails pending verifications; interactive hold pauses recovery without burning retry budget.

Observability and contracts add delivery_unconfirmed / delivery_resubmitted, richer delivery_verified (harness_acceptance, evidence, attempts), worker blocked_on_send during recovery, and deferred broker retries while recovery is in flight. Snapshot/composer heuristics and activity detection (e.g. Codex busy line) support the new gate. Docs, changelog (13.2.0 rollout), SDK types, and a RelayFlow case cover the behavior.

Reviewed by Cursor Bugbot for commit 45f0cf758ff93487ff0b04b116d9956103c93977. Bugbot is set up for automated code reviews on this repo. Configure here.


Agent Relay sessions

  • claude session 02580a3c · opened via gh pr create · last active 2026-10-04
  • claude session 5fa1b339 · contributor · ran gh pr commands · last active 2026-10-08