Fleet spawn --task and relay messages injected into a PTY agent arrive truncated to their tail (silent), and spawn reports failure on success

Merged
AG
2mo ago
+2286-80

PTY injection integrity and fleet task confirmation

Long fleet tasks and relay messages were typed into Claude one character every
5 ms. A 6.7 KB brief occupied the PTY write drainer for roughly 33 seconds, delaying
terminal-query replies and startup auto-responses while the composer could change.
The existing verification timeout acknowledged even a tail-only echo.

This change sends one bracketed paste to harnesses that have advertised DECSET
2004, followed by the existing delayed Enter. The capability is latched across
mode resets; composer readiness is checked independently before writing. A missing
composer, oversized body, or tail-only echo produces an explicit non-retryable
failure. Failed deliveries retain their identity and are never replayed or
subsequently acknowledged as successful duplicates. CRLF becomes LF; bare CR and
ESC cannot submit or terminate the body early.

The verification ladder is shared with wrap: exact echo, normalized head/tail
anchors, then paste summary. Missing heads only count as loss when the observation
window is retained. Absent echoes preserve timeout_fallback, now with a warning
and process-local counter. Wrap retains its existing failed-throttle timeout policy.

Verified fleet PTY spawns with a task remain pending after readiness until the
matching task verdict arrives; explicit task failures fail the spawn action.
Native and argv-task startup paths retain their existing readiness contract.
fleet spawn, agent spawn, and node agent spawn accept --task-file, read on
the requester and forwarded through existing task transport. Fleet requires
exactly one task source. Message commands and task input validate UTF-8 byte size.

Limits are 16 KiB globally, including the formatted envelope at the PTY boundary,
and 1,536 bytes for the default paced fallback. A slower explicit pace lowers that
cap. Codex retains its chunked initial-task path and uses the smaller derived cap.
The ordinary typed-path budget closes inside 90 seconds:
60 s startup + 15 s prompt recheck + 7.68 s typing + 0.25 s submit + 5 s verification
= 87.93 s. Steer acknowledgements include the injection budget.

Validation

  • Full broker and relay-pty run: 1,627 passed, 0 failed, 7 ignored.
  • Integrity integration suite, including the ignored stress test: 6 passed.
    Twenty cold starts each receive a >10 KB task and a >10 KB follow-up message,
    verified byte-for-byte in the fake harness's input transcript with one paste
    pair and one submit. Other arms cover tail loss/no replay, explicit size
    rejection, disappearing composer, and absent-echo compatibility.
  • CLI regression suites: 188 passed; CLI TypeScript checking passed.
  • cargo fmt --all --check and cargo clippy --all-targets passed. Clippy reports
    five pre-existing sliced-string warnings in Codex tests.
  • Live Claude startup probe observed DECSET 2004; no disable appeared during the
    six-second observation. Parser regression covers enable and disable in one read.

Commands (debug symbols disabled to fit the workspace disk):

env -u GIT_CONFIG_COUNT CARGO_PROFILE_DEV_DEBUG=0 CARGO_PROFILE_TEST_DEBUG=0 CARGO_INCREMENTAL=0 cargo test -p agent-relay-broker -p relay-pty
CARGO_PROFILE_DEV_DEBUG=0 CARGO_PROFILE_TEST_DEBUG=0 CARGO_INCREMENTAL=0 cargo test -p agent-relay-broker --test pty_injection_integrity -- --include-ignored
CARGO_PROFILE_DEV_DEBUG=0 CARGO_PROFILE_TEST_DEBUG=0 CARGO_INCREMENTAL=0 cargo clippy --all-targets
cargo fmt --all --check
npx vitest run packages/cli/src/cli/lib/task-input.test.ts packages/cli/src/cli/commands/fleet.test.ts packages/cli/src/cli/commands/local-agent.test.ts packages/cli/src/cli/commands/relaycast-groups.test.ts
npx tsc --noEmit -p packages/cli/tsconfig.json

This workspace injects core.hooksPath=/dev/null through GIT_CONFIG_COUNT.
Removing that setting only for the Rust test process lets the existing hook-chain
regressions exercise their own temporary repositories; all four then pass.

Compatibility and remaining work

Per reviewed-plan.md, absent-echo fallback still counts as spawn success. It is
not cryptographic or byte-for-byte receipt confirmation; the stronger ticket
acceptance needs a harness receipt protocol. The 20-run proof uses a raw-mode fake
Claude TUI through the real broker, not 20 authenticated Claude transcripts.

Wrap already bulk-wrote Claude input before this change. Fleet gains that behavior
plus explicit paste framing and readiness checks, rather than simply reverting
the 10.4.0 pacing fix. Large Claude inputs can now appear as pasted-content blocks;
the attribution envelope is unchanged.

Duplicate desktop/broker registrations remain a separate investigation requiring
the reporter's ambient MCP configuration. A node-side brief-file writer and
integration-command --task-file support are follow-ups. No workflow files were
changed, and no release or merge was performed.

Checks

Relayflow ran this repository's checks (.relayflow/check.sh) and they passed.

What ran (.relayflow/check.sh)
#!/bin/sh
# Fresh-machine equivalent of this repo's CI gate.
#
# Mirrors the jobs that gate a pull request on Linux:
#   .github/workflows/test.yml     -> test, lint
#   .github/workflows/codegen-models.yml -> codegen (generated files in sync)
#   .github/workflows/rust-ci.yml  -> rust-test, rust-clippy, rust-fmt
#
# Requires Node >= 22 (see .nvmrc / package.json engines).
set -e

cd "$(dirname "$0")/.."

# CI sets this on every workflow; keep local runs from emitting telemetry.
export AGENT_RELAY_TELEMETRY_DISABLED=1

# rust-ci.yml provisions cargo with dtolnay/rust-toolchain, which puts it on
# PATH. A toolchain is installed here but not exported by the login shell, so
# put it on PATH explicitly instead of silently skipping the Rust jobs below.
if [ -d "$HOME/.cargo/bin" ]; then
  PATH="$HOME/.cargo/bin:$PATH"
  export PATH
fi

# No CI workflow sets RELAY_BASE_URL. This sandbox exports it, and the CLI
# suites that assert "no ambient gateway is inherited" read the real
# environment (packages/cli/src/cli/lib/sdk-client.test.ts,
# commands/fleet-lifecycle-integration.test.ts), so leaving it set fails them.
unset RELAY_BASE_URL

# This sandbox has a 10 GB disk. A full-debug `cargo test` build of the
# workspace needs ~6 GB of debug info on top of the node_modules and dist
# trees, and runs the disk out mid-link. Dropping debug info keeps the same
# tests running; CI has the headroom and does not need this.
export CARGO_PROFILE_DEV_DEBUG=0
export CARGO_PROFILE_TEST_DEBUG=0
export CARGO_INCREMENTAL=0

# --- Dependencies -----------------------------------------------------------
npm ci

# test.yml installs rollup's platform-specific optional dependency explicitly,
# because npm sometimes skips it; failure here is non-fatal in CI too.
npm install --no-save rollup || true

# --- Code generation --------------------------------------------------------
# codegen-models.yml regenerates these from packages/utils/cli-registry.yaml and
# fails the PR if the committed output drifts.
npm run codegen:models
git diff --quiet packages/config/src/cli-registry.generated.ts packages/sdk-py/src/agent_relay/models.py || {
  echo "::error::Generated models are out of sync with cli-registry.yaml. Run 'npm run codegen:models' and commit the changes."
  exit 1
}

# --- Build ------------------------------------------------------------------
# Builds the Rust broker (skipped automatically when cargo is absent) and every
# workspace package. This is the same build `npm test` runs via its pretest
# hook, so the test step below invokes vitest directly instead of `npm test` to
# avoid building twice.
npm run build

# --- Lint / formatting ------------------------------------------------------
npm run lint
npm run format:check

# --- Tests ------------------------------------------------------------------
# Guard suites that test.yml runs ahead of the main suite.
node --test scripts/pr-proof/broker-transfer.test.mjs scripts/pr-proof/run-cloud.test.mjs scripts/flows/deploy-listeners.test.mjs scripts/pr-proof/event-from-input.test.mjs
npm run test:subscriptions:proof

# Main vitest suite (equivalent to `npm test`, whose pretest build already ran).
npx vitest run

# --- Rust (crates/broker, crates/relay-pty) ---------------------------------
# rust-ci.yml provisions a toolchain with dtolnay/rust-toolchain. Run these only
# when cargo is already present; this script does not install a toolchain.
if command -v cargo >/dev/null 2>&1; then
  cargo fmt -- --check
  cargo clippy -- -D warnings
  # This sandbox injects `core.hooksPath=/dev/null` through GIT_CONFIG_COUNT,
  # which defeats the spawner hook-chain tests that build their own temporary
  # repositories. CI has no GIT_CONFIG_* overrides; drop them for the tests.
  env -u GIT_CONFIG_COUNT cargo test
else
  echo "cargo not found - skipping Rust tests, clippy and fmt (rust-ci.yml equivalents)"
fi

# --- Deliberately omitted ---------------------------------------------------
# e2e-tests.yml, fleet-e2e.yml, relay-evals.yml, stress-tests.yml: need
#   ANTHROPIC_API_KEY and real agent CLIs driving live PTY sessions.
# test.yml coverage job: `npm run test:coverage` duplicates the suite above and
#   its only extra step is the Codecov upload, which needs a Codecov token.
# test.yml windows-credential-acl and swift-test jobs: need Windows and a Swift
#   toolchain on macOS respectively.
# prod-smoke.yml, publish.yml, verify-publish*.yml, deploy-relayflows.yml,
#   relay-*-qualification*.yml: deployment, npm/GitHub publishing and
#   cleanroom/Daytona cloud runs, all gated on registry and cloud credentials.
# test-build.yml / test-install.yml: build and install the released binaries
#   with Bun and a network install of agent-relay; not part of the source gate.

What the repair agent found

Repair notes — PTY injection integrity branch

Gate failures and what caused them

.relayflow/check.sh initially stopped at npm run format:check. Everything
after it (the guard suites, the vitest suite and the whole Rust section) never
ran. Findings, in the order the gate hits them:

1. format:check — untracked relayflow working files (fixed in place)

prettier --check . flagged plan.md and reviewed-plan.md. Both are
relayflow scratch documents listed in .git/info/exclude, so they are not part
of the repository and do not exist in CI; prettier reads .prettierignore, not
.git/info/exclude, so it checked them anyway. Formatted them in place
(npx prettier --write plan.md reviewed-plan.md). No repository file changed.

2. Rust jobs were silently skipped — toolchain not on PATH (fixed in check.sh)

check.sh guards the rust-ci.yml equivalents with command -v cargo. A
toolchain is installed at ~/.cargo/bin (cargo 1.99.0) but the login shell does
not export it, so cargo fmt, cargo clippy and cargo test were all skipped —
which hides the bulk of this branch, since the fix is in Rust. check.sh now
puts $HOME/.cargo/bin on PATH when it exists, the way
dtolnay/rust-toolchain does in CI.

3. Two CLI suites fail on an ambient RELAY_BASE_URL (fixed in check.sh)

  • packages/cli/src/cli/lib/sdk-client.test.ts › does not inherit a persisted
    gateway when an explicit agent token is supplied
  • packages/cli/src/cli/commands/fleet-lifecycle-integration.test.ts › reuses
    the persisted Cloud target across spawn, attach, message, list, and release
    without --base-url

Both assert that no ambient gateway is inherited, and both reach
createAgentRelay without an env override, so they read the real process
environment. This sandbox exports RELAY_BASE_URL=https://cast.agentrelay.com;
no workflow in .github/workflows/ sets it. env -u RELAY_BASE_URL npx vitest run <both files> passes 36/36, so check.sh now unsets it. The tests are
correct as written and were not touched.

4. Four Rust hook-chain tests fail on the sandbox git config (fixed in check.sh)

spawner::tests::broker_hook_chain_* (4 tests) build their own temporary
repositories and assert the repository's own hooks still run while the broker's
core.hooksPath is active. This sandbox injects core.hooksPath=/dev/null
through GIT_CONFIG_COUNT=4, which defeats them. CI sets no GIT_CONFIG_*
overrides, so check.sh now runs env -u GIT_CONFIG_COUNT cargo test. With
that, cargo test is 1347 + 254 + 25 passing, 0 failing.

5. cargo test runs the 10 GB disk out with full debug info (fixed in check.sh)

Once item 2 let the Rust section actually run, cargo test built the workspace
with the default debug = true and filled the disk mid-link (5.9 GB of debug
info in target/ on top of node_modules and the dist trees, leaving under
30 MB free). check.sh now exports CARGO_PROFILE_DEV_DEBUG=0,
CARGO_PROFILE_TEST_DEBUG=0 and CARGO_INCREMENTAL=0. The same tests run; only
the debug info is dropped. CI has the headroom and needs none of this.

None of these five are regressions from this branch; items 2–5 are environment
differences and are fixed only in .relayflow/check.sh, which is not committed.

Outside the gate, left alone

cargo clippy --all-targets -- -D warnings reports five
sliced_string_as_bytes errors in crates/broker/src/pty_worker.rs test code
(lines 2722/2733/2801/2851/3040 region). All five are present in the branch
point (238b522) and are test-only. rust-ci.yml and check.sh run
cargo clippy -- -D warnings without --all-targets, which passes, so this is
pre-existing and out of scope here.

Branch fixes committed in this pass

  1. crates/relay-pty/src/pty.rs — the bracketed-paste capability latch parsed
    every PTY output byte with its own processor.advance() call until DECSET
    2004 was observed. For a harness that never enables 2004 that is permanent,
    and it sits in the single reader thread that has to keep up with the child.
    Per-byte stepping is now reserved for chunks containing the literal 2004
    (a complete enable sequence must contain it, so enable+disable inside one
    read is still caught); every other chunk is bulk-parsed with one mode check
    afterwards, which still latches a DECSET split across reads. Covered by a new
    paste_capability_latches_when_decset_spans_two_reads test, verified to fail
    without the post-chunk check.
  2. Same file — bracketed_paste_enabled() had taken over consumed_offset()'s
    doc block, including its intra-doc link definition. Each has its own docs
    again.
  3. crates/broker/src/protocol.rs and packages/harness-driver/src/protocol.ts
    — the delivery_verified.verification and delivery_failed.reason doc
    comments still listed only echo / timeout_fallback. They now enumerate
    echo_anchors, paste_summary, prompt_unproven, echo_head_missing and
    injection_too_large, keeping the Rust and TypeScript protocol definitions in
    sync as .claude/rules/rust.md requires.
  4. CHANGELOG.md — added the missing Changed entry for the new 16 KiB body
    cap. It is user-visible (message send|reply|dm and the broker now reject
    larger bodies, including to native-runtime workers) and was only documented
    in docs/harnesses/injection.md.
  5. summary.md was committed into the repository in 924171f even though
    .git/info/exclude lists it as a relayflow working file. Untracked with
    git rm --cached so it stays on disk but out of the repository.

Fixes #1890


[!NOTE]
High Risk
Changes core PTY injection, delivery verification, and verified fleet spawn success criteria—incorrect behavior could silently lose tasks or block legitimate spawns.

Overview
Hardens PTY task and message delivery so large briefs use bracketed paste when DECSET 2004 is latched (with typed fallback and size gates), and echo verification only counts whole-payload receipt—not head/tail anchors or collapsed-paste markers.

Verified fleet spawns with a task no longer report spawned:true, ready:true unless delivery is confirmed (echo / echo_normalized); unconfirmed acks become spawn_task_unconfirmed (live agent, do not retry), terminal failures use spawn_task_failed / echo_head_missing, and oversized tasks are rejected before launch. Shared injection_wire enforces 16 KiB envelope / 14 KiB body caps; wrap delivers a pointer when over limit.

CLI adds --task-file (fleet requires exactly one of --task / --task-file); generic message commands stay uncapped at the Relaycast layer. relay-pty latches paste capability with efficient DECSET 2004 scanning. New integration tests and expanded runtime tests cover adversarial echo modes.

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