Skip to content

Refuse a Codex delivery turn's answer when a user message shares the turn - #3380

Merged
max-sixty merged 8 commits into
mainfrom
agent-a529870d7b75e2218
Oct 21, 2026
Merged

max-sixty merged 8 commits into
mainfrom
agent-a529870d7b75e2218

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Follows #3362, which reverted #3357's queue route. That revert reopened a race. App Server's turn/start on a running turn steers into it and answers with that turn's id. A follow-up the user queued in the Codex TUI starts just after the turn before it ends, while App Server still reads idle, so the adapter's idle check can pass and its start join the follow-up's turn. Leaf then posted that turn's final message as its reply to the page comment. A user can also steer into Leaf's own delivery turn by pressing Enter in Codex. This PR meets the auto-reviewer's condition on #3362 and keeps deliveries quiet: a leaf_delivery tool output, never a user message.

What changes

  • A delivery turn's items decide whether its answer is the delivery's (codex.carries_user_message). A turn Leaf starts opens with its tool output and holds no user message. A turn that holds one, ahead of the delivery or after it, is joined. A leaf-delivery pointer is Leaf's input and doesn't count, and App Server's schema records a hook's inline delivery as a hookPrompt item, not a userMessage (read from the 0.160.0 schema, not run).
  • From the moment the fold sees the user's message, it posts no more of the reply, neither opening nor draft. It marks the draft withheld while keeping the reservation. The Stop hook reads a withheld draft as the turn being done with the update (activity._turn_wrote), so it lets the user's turn end on its own answer rather than continuing it. A withheld draft survives a dropped connection and ages only by its lease.
  • When the turn ends, the fold releases the reservation. A completed joined turn's final message is refused (JoinedTurn), and TurnFold.commit offers the update again through the existing correction route, answered in a turn of its own. An interrupted or failed joined turn leaves the update owed with its condition shown, as any unanswered turn does.
  • The contract lives in codex.py's header. The note's "Delivery-start race" section and the two TODO entries (the race, and the user steering into a delivery turn) are retired. harness-codex.md, "Replies", names the new correction reason, and the correction instruction is generalized to both reasons a final goes unposted.

Evidence (codex-cli 0.160.0)

  • Item order: a turn Leaf starts opens with functionCallOutput leaf_delivery and holds no userMessage (4 of 4 runs). A steered turn holds the user's message after the delivery, and a joined turn holds it ahead.
  • The TUI's queue: the TUI keeps a Tab-queued follow-up in its own state and sends its own turn/start 17.5 ms after turn/completed. With the TUI showing the follow-up queued, thread/queue/list returned no items. No queue read can close the gap.
  • Race, before and after: with a real App Server, the user's turn/start lands between Leaf's idle check and its start, and both starts get the same turn id.
    • Before: Leaf posted the joined turn's answer as its reply.
    • After: it posted nothing and offered the correction. In one run the joined turn's answer addressed only the user's follow-up, which is what the old path would have posted.

Tests

  • A joined turn and a steered turn, each completed and interrupted, run over a task connection. The test checks that the reservation is held mid-turn, that the Stop hook owes nothing mid-turn (including after a dropped connection and an aged draft), and that a correction follows only a completed turn.
  • A joined turn read back from history.
  • Control runs: without the fix, without the terminal-items clause, without the withheld rule, and with the withheld draft aging; each fails the test that targets it.
  • Focused suites pass (497).
  • The codex-app-server and codex-queue journeys passed on this change.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 8 commits October 20, 2026 13:20
…turn (#3357)"

This reverts commit ef47d1a.

#3357 put every App Server delivery on the task's Codex queue, so each one
became the user message of its turn: Leaf's opening line and the envelope
as JSON, shown in the user's Codex transcript and queue. In Codex a message
added to the user turn is noisy, and Leaf should add one only when it must.
Deliveries go back to `turn/start` with a `leaf_delivery` tool output
(`turnTrigger: "leaf"`), which adds no user message.

That reopens the race #3357 closed: a follow-up the user queued in Codex
starts about 0.4 s after the running turn ends, a Leaf `turn/start` in that
gap steers into the follow-up's turn, and Leaf posts its answer as the
reply. `notes/codex-integration.md`, "Delivery-start race", and its TODO
entry are back, updated to say why the queue route was reverted; a fix
that keeps `turn/start` and reads the task's queue first follows.

Kept from #3357: the TODO entry for a user who steers into Leaf's delivery
turn, which predates it. Kept from #3358: the opening posted as the turn's
progress line; the two regenerated snapshots carry its wording on the
restored `turn/start` input.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
… queue

With the TUI on `codex --remote` showing a follow-up queued with Tab,
`thread/queue/list` on the same task returned no items, and the TUI sent
its own `turn/start` 17.5 ms after the running turn completed. Reading
App Server's queue before a delivery's `turn/start` therefore can't close
the delivery-start race, so the TODO entry goes back to a decision for Max.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…turn

App Server's `turn/start` on a running turn steers into it and answers with
that turn's id. A follow-up the user queued in the Codex TUI starts just
after the turn before it ends, while App Server still reads idle, so the
adapter's idle check can pass and its start join the follow-up's turn; Leaf
then posted that turn's final message as its reply to the page comment. A
user can also steer into Leaf's own delivery turn with Enter.

A delivery turn now decides from its items whether its answer is the
delivery's (`codex.carries_user_message`). Once its fold sees a user
message it posts no more of the reply, neither opening nor draft, and marks
the draft `withheld`; the Stop hook reads a withheld draft as the turn being
done with the update (`activity._turn_wrote`), so it lets the user's turn
end on its own answer. A withheld draft survives a dropped connection and
ages only by its lease, since nothing renews it. As the turn ends the fold
releases the reservation; a
completed joined turn's final message is refused (`JoinedTurn`), and
`TurnFold.commit` offers the update again through the existing correction
route, answered in a turn of its own. An interrupted or failed joined turn
leaves the update owed and its condition shown, as any turn that ends
without an answer does. A
`leaf-delivery` pointer is Leaf's input and doesn't count. The correction
instruction is generalized to both reasons a final goes unposted.

Measured with codex-cli 0.160.0 on 2026-10-20:

- A turn Leaf starts opens with its `leaf_delivery` tool output and holds no
  user message (4 of 4 runs); a steered turn holds the user's message after
  the delivery, and a joined one holds it ahead of the delivery.
- The TUI keeps a Tab-queued follow-up in its own state
  (`queued_user_messages`, `codex-rs/tui/src/chatwidget/input_queue.rs`) and
  sends its own `turn/start` 17.5 ms after `turn/completed`. With the TUI on
  `codex --remote` showing the follow-up queued, `thread/queue/list` on the
  task returned no items, so no queue read can close the gap.
- Against a real App Server, with the user's `turn/start` landing between
  Leaf's idle check and its start: before, Leaf posted the joined turn's
  answer as the reply; after, it posted nothing and offered the correction.

The race and the steer case were two TODO entries and a section of
`notes/codex-integration.md`; the contract now lives in `codex.py`'s header
and those are retired. `harness-codex-app-server.md` tells the agent about
the new correction reason.

Tests: a joined and a steered turn, each completed and interrupted, over a
task connection, and a joined turn read back from history.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	TODO.md
#	skills/leaf/references/harness-codex-app-server.md
#	skills/leaf/scripts/leaf/activity.py
#	skills/leaf/scripts/leaf/codex.py
# Conflicts:
#	skills/leaf/references/harness-codex-app-server.md
@max-sixty
max-sixty merged commit 0376e03 into main Oct 21, 2026
3 checks passed
@max-sixty
max-sixty deleted the agent-a529870d7b75e2218 branch October 21, 2026 04:08
max-sixty added a commit that referenced this pull request Oct 21, 2026
Applies #3256's Stop rule to Codex. #3256 made Claude Code's and Pi's
Stop hook hand no late page input into a finishing turn where a watch
follows it, so the turn ends on its answer and the input gets a turn of
its own. Codex's Stop hook still blocked the turn's end and handed a
late comment in, so the agent wrote one more message after its answer,
and that reply became the turn's final message.

### What changes

- **One predicate says whether a later turn takes late input**
(`Harness.later_turn_takes_input`). It is the watch between turns for
Claude Code and Pi (`watches_between_turns`), and the live delivery
adapter for Codex (`adapter_is_live`). `hook_transport.carry_turn` and
`CodexHarness.offer_at_step` both read it, so Codex's Stop offers
nothing while the adapter runs. The turn closes, and the adapter offers
the input in a turn of its own: a `turn/start` with a `leaf_delivery`
tool output over App Server, or the queue's next turn through `codex
queue`. A Codex task that waits with `leaf wait` has no adapter, so its
Stop still hands input over, as under plain `claude -p`.
- **An owed update this owner holds no usable reference for counts as
input, not as a debt** (`hook_transport.read_plans`). Such an update,
received by another session before the page changed hands, is already
deliverable again (`delivery.unreferenced`). Stop used to remind the
agent about it as well, which with Codex's adapter running continued the
turn with a reminder naming the `leaf wait` that would deliver its
reference. Now Stop hands it over with the other input or leaves it to
the later turn. The same applies to Claude Code and Pi with a watch.
- Prose in the `codex.py` and `hook_transport.py` headers,
`session-lifetime.md`, `harness-codex.md` ("Delivery"), the site's "Stop
hook checks" and `TODO.md` now says Stop hands input over only where
nothing opens a later turn for it.

### Codex's TUI shows every message of a turn

The question was whether replacing the final message matters in Codex
the way it does in Claude Code's focus view. In codex-cli 0.160.0's TUI,
a turn asked to send a progress message, run a command and then answer
showed all three in the transcript (`• ALPHAONE`, `• Ran echo hi`, `•
OMEGATWO`). So the TUI never hid the answer. What this change keeps in
Codex is that a turn's final message is its answer, and that late input
gets a turn of its own; surfaces that show a turn's final message alone
were not checked.

### Tests

- New `test_a_codex_stop_leaves_late_input_to_the_adapters_own_turn`:
with the adapter live, a comment collected as the turn ends gets no Stop
output, the turn closes, and the adapter queues it once. It fails with
the Stop early return removed (Stop blocks with the envelope).
- `test_a_stop_reminder_recovers_the_exact_reply_address`: the handoff
cases now reach the new owner through the adapter's offer after Stop.
They fail without the `read_plans` change (Stop continues with the
reminder).
-
`test_codex_stop_leaves_quiet_input_but_delivers_the_whole_batch_with_owed_input`
is deleted: it asserted that an owed comment keeps the turn open with
the adapter running, the behavior this PR reverses.
`test_a_codex_stop_keeps_the_revision_returned_by_its_offer` now runs
without an adapter, where Stop still offers.
- The `codex-app-server` and `codex-queue` journeys pass. Their `resume`
step now runs on both transports and requires that a comment sent during
an empty-input resumed turn, whose first boundary is its Stop, is
answered in a turn of its own rather than handed into the resumed turn.
- Focused suites pass, except
`test_codex_app_server.py::test_a_turn_that_shares_a_user_message_leaves_the_delivery_owed`,
which fails the same way on `main` (`KeyError: 'status.json'`, from
#3380 meeting #3384).

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
leaf-agent added a commit that referenced this pull request Oct 21, 2026
The [main CI
run](https://github.com/max-sixty/leaf/actions/runs/39542395592) failed
four new joined-turn cases from #3380 because their age check still read
the retired `status.json` record. Its mock `turn/started` notification
also omitted the schema-required `status`, and Ruff found the test's
`write_record` call unimported. This PR reads and ages the draft in
`stream.json`, imports its writer, and sends a valid turn notification.
The delivery behavior and the test's assertions stay the same.

All four affected cases pass locally, as does the Ruff hook. The run's
separate detached-touch failure is covered by merged #3419; that exact
browser case passes on current `main`.

Co-authored-by: leaf-agent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants