Repository navigation
Rebuild the hosted turn follower around its delivery - #779
Conversation
Three of the nine hosted turns on leaf.page in a six-hour window today ended `turn_stream_completed status=failed error=AppServerRequestRejected`, about 290 ms after the follower started, and none of the three readers got a reply. The record named the exception class and nothing else, so the three are one line repeated and the App Server's own account of what it refused is nowhere. `fault_fields` puts that account on every record that names a failure: `error` stays the class, `detail` carries the sentence. `terminal_fault` does the same for a turn App Server itself reports as failed, whose reason previously reached only the container's stderr. The calls behind these records are Leaf's own, so the sentence is the provider's about the call rather than a reader's words; `worker/README.md` says so where it describes the record contract. The delivery writes a "Starting" reading before its turn binds, keyed by the delivery because there is no turn id yet. The fault path cleared by turn id alone, so a turn that never bound left that reading standing and the page told the reader the agent was starting for the whole fifteen-minute working grace, on a move it would never answer. Clearing it by the same key it was written with is `starting_turn_key`, now spelled once. Both halves reproduced first: with either change reverted the new test fails, and without them it emits exactly the production signature, `turn_delivery_unbound` followed by `turn_stream_completed status=failed`. What the reader sees is still not enough, and the remaining gap is structural rather than a missing string: `_finish_turn` is reached only for a turn that bound, so an unbound failure writes no receipt, leaves its claim's turn open, and never says the move went unanswered. Closing that claim needs the adapter to know whether this follower's delivery owns the claim's turn, which `codex.py` does not currently expose — a queued follower running beside an opened delivery must not close it. Checks: `test_website_server.py` and `test_interact_session.py` (259), the everyday suite (1,139), and pre-commit. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`_follow_turn` was 408 lines — a quarter of the adapter in one function, with 38 locals, 21 of them reassigned, three nested loops, five `try` blocks and twelve levels of indentation. Thirteen commits shaped it, and the last four each answered the failure in front of it: #491 settled lost streams, #633 and #677 moved reply authority, #729 closed a turn whose follower faulted. Three of nine hosted turns on leaf.page in a six-hour window today ended with a reader who got nothing, which is what a fourth patch would have been for. Three names existed for one turn: the delivery, known from the moment it is prepared; App Server's turn id, which arrives by notification or resume and may never arrive; and Leaf's turn id, which arrives when the delivery is accepted. The code keyed its consequential steps on the last of those, so a turn that never bound skipped its receipt, its claim close and its reply-seat release — three symptoms of one mistake. The delivery is now the identity, and binding is something that happens to a turn rather than a condition for having one. `TurnStream` owns the socket, the buffered notifications behind a request, and the resume; nothing that arrives on it means anything to it. `HostedTurn` binds, folds, records and commits. `_follow_turn` is the 25-line loop between them: 69 lines, 11 locals, one reassigned, three levels of indentation. The same two binding steps run whether the turn id came from a notification or a resumed thread, where they were near-copies before, and four `first_*` flags became one set of recorded milestones. The restructure is behaviour-preserving, and the 73 website tests pinning the old follower pass unchanged against it. What changes is the account a turn gives of itself. A delivery no provider turn ever took now gives up its reserved reply address — `release_delivery_reply` completes the reserve/reserved pair — and settles each of its moves with a `turn_failed` receipt, so the reader is told their message did not arrive rather than watching an agent that reads as listening. The address is the delivery's own: `delivery_reply_targets` reads both halves of each response contract, because a widget frozen into thread markup is answered on the conversation holding it and not at the gesture. `stream_reply_target` is now one line over it. A move another turn has picked up is that turn's to answer, which `only_if_unclaimed` already knew, and `abandon_codex_delivery` retires only a delivery still offering. A queued follower beside a running turn therefore leaves both alone, which is what `test_an_unbound_queued_website_turn_leaves_the_direct_turn_running` has always said. Codex reviewed the diff and found the receipt addressed at the gesture rather than at its conversation; `test_an_unanswered_widget_gesture_is_receipted_on_its_conversation` now pins it, and without the fix it fails with the `unknown comment id` exit Codex described. Both other changes are bug-back checked the same way. Checks: the everyday suite (1,140) and pre-commit. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Keeping `_follow_turn`'s nine-parameter signature through the rewrite was a way to hold the tests still while the body changed underneath them, and it did that. As a design it is the tests choosing the interface: the parameters were the fields of the `HostedTurn` the function went on to build from them. `_start_turn` now returns that turn, so it is constructed once, by the code that knows how. The seven-tuple it used to return was written out three times in one function and annotated twice in nine lines each, then read back by position — `follow[1]` for the thread, `follow[4]` for the events, `follow[-1]` for the delivery — at fifteen sites. Those are named attributes now. `_follow_turn(turn, socket, initial_messages)` takes three parameters, and its two remaining callers pass one object. With the tuple gone the two `except` arms in `_start_turn` turned out to be the same arm: a refused start and a lost one both leave the offer standing and the delivery still this turn's, and the follower reconciles against the thread to find out which. `_resume_and_start` builds a turn the same way; it deliberately does not announce a start, because a turn already running owns the page's activity reading and this delivery is queued behind it, and that comment now sits where the difference is. `HostedTurn` takes what a turn always has first — its host, page, thread, delivery and moves — and what it acquires as keywords, which is the same ordering the rewrite already used to explain itself. worker/server.py loses 39 lines net. The tests gain 29 and read better for it: a constructor whose arguments are named beats eight positional ones, whichever side of the call they are on. Checks: the everyday suite (1,140) and pre-commit. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
leaf-agent
left a comment
There was a problem hiding this comment.
The restructure reads well — TurnStream/HostedTurn/_follow_turn is a real decomposition rather than a reshuffle, and each of the three behavioural changes is pinned by a test that fails when reverted. Two things to decide before it lands.
detail is not always a sentence. _ensure_server raises RuntimeError(detail.strip() or "Codex App Server exited"), where detail is self.log_path.read_text(...) — the whole App Server log, appended across the container's life. fault_fields now feeds str(error) into app_server_prewarm_failed and container_start_failed, so an App Server that exits during spawn writes its entire log into a single JSON record. That is the one call site where the new field is neither bounded nor "the sentence the refusing boundary wrote" that worker/README.md now promises, and it is also the one place the message is Codex's own output rather than its account of a call Leaf made. Capping it in fault_fields is the in-diff fix (suggested inline); bounding the message where _ensure_server raises it is the other half of the same choice — say which and I'll push it.
The receipt names a cause the follower cannot read. _report_unanswered fires on leaf_turn is None, which establishes that this follower never bound the delivery — not that no provider turn took it. In the incident this PR is built on, turn/start was acknowledged and Codex went on to complete six model requests, and on that path nothing writes a pickup, so only_if_unclaimed does not hold the receipt back either: the reader is told "This message did not reach the agent" about a turn that was running, and "send it again" duplicates work already in flight. Writing the receipt is the right call — the reader was getting nothing — but the sentence asserts the one thing this failure path cannot establish. Naming the observable instead holds on both paths.
Review found both of these. `_report_unanswered` fires when no Leaf turn holds the delivery, which proves this follower never bound it and nothing about whether a provider turn ran. The receipt said the message never reached the agent, and the incident this whole path was written for is the one where it had: `turn/start` was acknowledged and Codex completed six model requests with no observer. So the sentence was wrong exactly where it matters most. It now says the turn ended without an answer, which holds on both paths, and the docstring and `worker/README.md`'s note on `turn_delivery_unbound` stop claiming more than the absence of an answer too. An exception message is not always a sentence. `_wait_for_app_server` raises with App Server's entire log when a spawn never becomes ready, and `fault_fields` would have written all of it into one record — not the bounded sentence the README promises. `FAULT_DETAIL_LIMIT` keeps the last 500 characters, marked with a leading ellipsis, because the reason a process exited is at the end of its log rather than the start. Checks: the everyday suite (1,141) and pre-commit. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Review found this. `FAULT_DETAIL_LIMIT` bounded `fault_fields`, but `terminal_fault` is the other writer of a record's `detail` — the path taken when App Server reports its own turn failed rather than raising — and it returned the provider's message verbatim. So `worker/README.md`'s size bound held for one of the two producers, which is the same as not holding. `bounded_detail` is the one place the limit lives and both call it. A record's size is the record's contract, not something each writer decides. Checks: the everyday suite (1,141) and pre-commit. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`fallback_reply` was the name for the thing that answers a reader when no agent answer is coming — the Worker's rate limiter, a dispatch that threw — and #779 added a second implementation of the same idea for a turn the container followed to nothing. Neither was a fallback: they are how a failure reaches the reader instead of being swallowed. But the page never drew one. `failure` was set on the reply and schema-validated, and read by nothing outside the deploy gate, so "I couldn't generate a reply just now" arrived in the agent's own first person, in the same bubble, under the thread's own agent name — indistinguishable from an answer, which is the one thing the field exists to prevent. ## What changed **One writer, one vocabulary.** `write_failure_receipt` is now the only code that appends a reply carrying `failure`, and `FAILURE_RECEIPTS` declares each of the three codes beside the words the reader gets for it. The Worker sends `{event, failure}` and no longer carries a copy of that wording across the HTTP hop; `turn_failed` is derived out of the door's accepted set, because a receipt claiming a turn was observed should come only from the code that observed it. Removing `text` from that request left `_agent_event`'s text half with no callers, so it goes too. **The writer names only the move.** A reply's address is not always the move — a gesture on a widget frozen into thread markup is answered on the conversation holding it — but `cmd_reply` resolves that from the same reading, so working it out beforehand was a second answer to a question the writer already answers, and the wrong one on a repeat: the Worker retries, and once the first receipt has settled the move, nothing outside the log can say where its reply went. Doing it early got the gesture back and refused its own earlier receipt as another event's, turning the retry into a 400. The durable `attempt` is what makes the repeat idempotent, and the writer reads it before the address. **The head says the rest.** A reply carrying `failure` wears one mark, `Not answered`, in the panel and inline both — the only thing in a message head that is not muted metadata, because it is the only thing there a reader has to act on. The three texts now speak in the host's voice rather than the agent's first person. A failure receipt deliberately does *not* enter the panel's "On you" list: `awaits` means the agent's reply left a question and the banner counts it as an Ask, which is a package-owned semantic, so the chip and "Send it again to retry" carry it instead. ## Checks Everyday suite (1,142), `test_render_conversations.py` + `test_chrome_contracts.py` under `--run-nightly` (105), worker typecheck and vitest (62), pre-commit, and one real hosted Codex turn through `scripts/verify_site.py local` — published revision 2 and replied in 17.2 s through the changed reply door. The new render test is bug-back checked against both the runtime's read of `failure` and the chip's styling; the retry regression is bug-back checked against resolving the address ahead of the write. 🤖 Generated with [Claude Code](https://claude.com/claude-code) > _This was written by Claude Code on behalf of max-sixty_ Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Three of the nine hosted turns on leaf.page in a six-hour window yesterday ended
turn_stream_completed status=failed error=AppServerRequestRejectedabout 290 ms after the follower started, and none of those three readers got a reply. In the one I traced, Codex went on to complete six model requests over the next thirty seconds with nobody listening. The record named an exception class and nothing else, and the page held its "Starting" reading for the full fifteen-minute working grace before quietly reading as an agent that is listening — so the reader was told nothing, twice over.The cause is structural rather than a missing string, which is why this ends up being mostly a rewrite.
Reading the diff
worker/server.pycarries the change.skills/leaf/scripts/leaf/conversation.pyandcodex.pyeach gain one primitive the adapter needed;worker/README.mdandpage-storage.mdare the contracts those touch.One turn, three names. The delivery is known from the moment it is prepared. App Server's turn id arrives by notification or resume and may never arrive. Leaf's turn id arrives later still, when the delivery is accepted. The code keyed its consequential steps on the last of those, so a turn that never bound skipped its receipt, its claim close and its reply-seat release — three symptoms of one mistake. The delivery is now the identity, and binding is something that happens to a turn rather than a condition for having one.
The follower is three units.
TurnStreamowns the socket, the notifications buffered behind a request, and the resume; nothing arriving on it means anything to it.HostedTurnbinds, folds, records and commits._follow_turnis the loop between them. It was 408 lines with 38 locals, 21 of them reassigned, three nested loops, fivetryblocks and twelve levels of indentation; it is 54 lines with 11 locals, one reassigned, and three levels. Thirteen commits had shaped it, the last four each answering the failure in front of it, which is what a fourth patch would have been.The turn is the unit callers pass.
_start_turnreturns aHostedTurnrather than a seven-tuple that was written out three times in one function, annotated twice at nine lines each, and read back by position at fifteen sites._follow_turn(turn, socket, initial_messages)takes three parameters where it took nine.What a reader sees now
A delivery no Leaf turn holds gives up its reserved reply address and settles each of its moves with a
turn_failedreceipt. The receipt says only that the turn ended without an answer: that a follower never bound a delivery proves nothing about whether a provider turn ran, and in the incident behind this PR one was still running. Releasing the address is the point: until it is given up it also refuses the receipt that would say no answer is coming, so the reader was left with neither. The address comes from the delivery's own response contract, because a widget frozen into thread markup is answered on the conversation holding it and not at the gesture.A move another turn has picked up stays that turn's to answer —
only_if_unclaimedalready knew, andabandon_codex_deliveryretires only a delivery still offering — so a queued follower beside a running turn leaves both alone.What the log says now
Every record naming a failure carries
detail, the sentence the refusing boundary wrote, besideerror, its class — bounded to its last 500 characters, because an exception message is not always a sentence and a spawn that never became ready raises with App Server's whole log, whose reason is at its end. Without it every rejection from one class reads alike, which is the difference between knowing that App Server refused and knowing what it refused. That question is still open: this is what will answer it on the next occurrence, and nothing here guesses at it.The stale "Starting" reading is cleared with the key it was written under, which is the delivery's, not the turn id the follower never learned.
Checks
The everyday suite (1,141) and pre-commit are green on the merge with main.
scripts/verify_site.py localran a real hosted agent turn through the rewritten follower twice — it claimed the delivery, edited the page, published revision 2 and replied in 17.3 s. The 73 website tests that pinned the old follower passed unchanged against the restructure before their call sites were updated, which is the evidence that it preserves behaviour; each of the three behavioural changes fails its own test when reverted.Codex reviewed the diff before it was pushed and found the receipt addressed at the gesture rather than at its conversation; that is fixed, and
test_an_unanswered_widget_gesture_is_receipted_on_its_conversationpins it. The tend reviewer then found the receipt's wording overclaiming andfault_fieldsunbounded against a spawn failure, both taken in 74ad015.No TypeScript changed, so neither the worker nor the browser-framework gate is in scope.