On overflow the owner retires a viewer's subscription and sends one
session.replay_gap while the turn runs on. Every Python subscriber treated
that frame as a failed turn: the classic CLI reported "outcome unknown" and
then hung on every later turn, GatewayChatView (stream-json, -z,
--safe-mode) failed, and ACP stopped its event pump, failing the waiting
prompts of every session. #140292 made gaps much rarer by raising the frame
cap, but kept the overflow path.
A gap now means "this subscription ended; re-attach". The client resumes
that session only, adopts the new subscription, wakes whatever waits on its
pending rows or controls, and answers the controls the snapshot lists. The
snapshot is taken under the event-stream lock: a queued or started
admission it lists publishes its completion on the new subscription; an
unknown one owes none and settles as unknown now, in every client; one it
no longer lists ended in the hole and its receipt settles it, read once and
only against a snapshot that covers it (one whose resume left after the
submit). ACP's pump is the one recovery owner, so a reply is delivered at
most once. After a gap a control blocks only an admission the snapshot
still lists.
Live deltas stop at the gap and the settled reply shows once (the rest of
an exact prefix, else "[output resumed]" and the whole reply), to the
terminal and to the voice stream separately. An interim stays whole, as it
can be the reply's only copy. stream-json emits no further text and flags
its result with replay_gap. After a gap or from a receipt, a reused reply
is suppressed only when all of it was shown; without a gap main's rule
holds. A refused re-attach still ends the wait with today's error; an
unreadable receipt fails only that admission. A CLI turn owns its turn
token and its session from the pre-submit re-attach through _result and
painting, bounded by the interrupt settle deadline, and any received
boundary closes the reply segment.
The real-rig tests share one real owner that pins the fanout frame cap at
256, as #140292's own overflow probe does, so the paused viewer still
overflows.
Refs #134889Fixes#140388
(cherry picked from commit 0d51f10640c560e0cb0b2205e2dbad18d06c7c5c)
17 conflicts, all from main's lint sweep (ruff UP007/UP017/UP022/UP028/UP031/UP037/UP041,
FURB188/FURB192); no other main commit touches a conflicted file. Resolution: the branch side
of each file plus main's exact autofix (ruff 0.15.10, main's ruff.strict.toml, --fix
--unsafe-fixes). ruff.strict.toml takes main's copy. The pool-retirement live fixture stays
deleted with the pooled backend this branch retires.
After the cutover every non-TUI chat launches through the gateway client, so
main's stream-json wiring (1657a1ce, aa75d372) — which lived on the in-process
`cli.main` single-query path — was unreachable: `cmd_chat` consulted the TUI
gate first and the gateway view printed human text.
- `cmd_chat` decides stream-json before the TUI gate (structured stdout is a
non-interactive protocol) and always takes the gateway transport.
- `launch_from_args` builds the emitter before validation/connection so a
refused launch, connection failure or Ctrl-C still closes the protocol
(init + `result` with the exit code, diagnostics on stderr); setup
guidance is redirected off stdout.
- `GatewayChatView` routes message.delta / tool.start / tool.complete /
message.complete into the emitter; the oneshot terminal record carries
exit 0/1/130 from the authority's outcome.
- `StreamJsonEmitter.bind_session` records the gateway-assigned id after
`init` so the terminal record names the session holding accepted work.
The regression test drives parser -> cmd_chat -> gateway transport against a
fake authority peer instead of a fake `HermesCLI`.
- on_text_delta dropped whitespace-only deltas, so concatenating the `text`
events no longer reproduced the answer (a newline between paragraphs was
lost). Only None/"" (the turn-end sentinel) is skipped now.
- The emitter was attached only after credentials + agent init succeeded, so a
missing key or unknown provider exited 1 with an EMPTY stdout and the
provider error rendered through ChatConsole (stdout). The emitter is now
built before _ensure_runtime_credentials/_init_agent; that path closes the
protocol with init + a failed `result` (exit_code 1, error) and the
credential error goes to stderr whenever stdout is machine-readable
(tool_progress_mode == "off", i.e. -Q and stream-json).
- _tool_started was keyed by tool name, so concurrent same-name calls
clobbered each other's start time; key on tool_call_id when the caller
passes one and surface it on tool_use/tool_result.
Live: `hermes chat -q … --format stream-json` with no provider and with a dead
custom base_url both yield pure JSONL (`system` + `result`, exit 1).
Adds a --format flag to hermes chat single-query mode. stream-json
emits newline-delimited JSON events (init, text, tool_use, tool_result,
result envelope with token stats + exit code) to stdout for CI
pipelines and external tooling. Session ID stays on stderr.
Salvaged from PR #12278 by @ProDrifterDK onto current main, including
the follow-up commit enforcing the single-query contract (implies
quiet, rejects --tui, emits a final result record with exit code 130
on interrupt).