acpmux: a write fence for one message to a direct agent (#23322 a3 fence, part 1 of 5)
What (#23322 a3 fence, part 1 of 5: the fence for a direct agent)
It builds on the decision on #23322, quoted word for word:
"#23322 (sessions.allowFullAccess under MDM), decided: an MDM-managed value is an upper bound the user can't override. If MDM sets allowFullAccess=false, Full access is unavailable and the mode menu says "Managed by your organization". If MDM is unset, nothing changes. This only narrows access."
#25538 (#23322 a3) landed as 3e72956265. Review 40 on it found that its family gate was released before the write (3) and that a blocked transport could stall an MDM off push forever (4). This stack of five PRs closes both. Each one lands on its own, in order:
- the fence for a direct agent;
- the fence for a hosted agent;
- host protocol 2 receipts;
- family messages;
- ancestor answers.
This PR adds Fence (agent.rs). A fence follows one message to a directly spawned agent's stdin. Its states are queued, being written, written, cut while queued (never written after), and failed. Nothing fences a message until part 4, so on its own this PR changes no behaviour.
- The direct writer settles it. The stdin writer marks the message written after its write.
- If the writer stops, it cuts what it still has queued, even if it was stopped before it ever ran.
- If the agent ended, its writer stops and the message it was writing fails. A descendant that kept stdin and never reads can't hold it forever (review 87).
- Only the writer says written. An answer to a request proves nothing for a direct agent, because the Codex bridge makes up answers once its app-server exits (review 87).
- A message never handed on settles. If a send errors, or the sender is dropped while it waits for room, the message is cut while queued, or failed once a translator took it (review 87).
- A translated message is taken before translation. Claude's translator and the Codex bridge consume their own state (a permission ask's control id) when they translate. So a cut never drops a message they already translated; it ends the agent instead (review 88).
Fence::cut_or_end(budget)cuts the message.- If the writer already has it, the cut stops that writer first, so nothing more reaches the pipe.
- It then signals the agent's process group only while the leader is unreaped and its slot is locked.
- A reaped leader's group is never signalled, since its pid may be reused. A failed
try_waitproves nothing and is tried again. - Each step is bounded, and the cut never goes through the chat's agent slot (reviews 81, 82, 87).
- An empty Claude or Codex batch queues nothing, as on base (reviews 81, 82).
- A hosted agent takes no fenced message until part 2. It is refused and cut, never sent unfenced.
With MDM unset, nothing changes. No caller passes a fence yet; request_with_hook and agent_prompt_hook take None.
Risk: this changes how the direct stdin writer and the reader's exit path settle state. Without a fence, both behave as before.
Tests
agent::fence_tests, red first:
an_empty_batch_of_server_lines_queues_nothing(reviews 81, 82);a_cut_stops_the_writer_of_a_stdin_a_descendant_kept: a setsid descendant that holds stdin gets less than the 1 MiB line after a cut. Red: run 40466670878 (review 87);a_message_a_descendant_never_reads_settles_once_its_agent_ended. Red: run 40475262602 (review 87);an_answer_a_translator_took_is_not_cut_while_queued. Red: run 40476304828 (review 88);only_its_writer_settles_a_direct_agents_messageanda_translated_message_its_closed_writer_never_took_settles. Red: run 40481938596 (review 87);a_writer_stopped_before_it_ran_settles_its_queue. Red: run 40490555881 (review 87).
Green at 3d6df8c023 (run 40490555878):
- fmt and clippy
-D warningsclean; - lib 1387/1388, and fence_tests 12/12 four times;
- agent_host 11/11, durable_agents 61/61 and hub_integration 508/508; host_deadlines, host_lifetime, host_scope, send_steer and codex_app_server_live pass.
The only failures are base reds:
agent_tools own_servers_run_under_the_daemon_prefix;session_pool pooled_sessions_end_with_the_daemon_and_after_a_crash.
Review
An independent Codex reviewer (gpt-6-astra, xhigh) gave SHIP at 3d6df8c023 (review 87, round 5). Rounds 1 to 4 blocked; each finding was fixed red-first, as listed above:
- the writer of a stdin a descendant kept;
- a request settled by this side's own "closed";
- an answer made up by the Codex bridge;
- a translated message never handed on;
- a writer stopped before its first poll.
No capture: no UI changes.
Agent session: agent session
š¤ Generated with Claude Code