Skip to content

Revert "Queue each Codex App Server delivery instead of starting its turn" (#3357) - #3362

Merged
max-sixty merged 2 commits into
mainfrom
agent-a529870d7b75e2218
Oct 20, 2026
Merged

max-sixty merged 2 commits into
mainfrom
agent-a529870d7b75e2218

Conversation

@max-sixty

@max-sixty max-sixty commented Oct 20, 2026 •

Copy link
Copy Markdown
Owner

Reverts #3357. That change put every Codex 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, 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.

This reopens the race #3357 closed: a follow-up the user queued in Codex starts just 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. Reproduced on this branch with the note's script (codex-cli 0.160.0): Leaf's turn/start landed in the turn holding Reply: USER FOLLOW-UP. notes/codex-integration.md, "Delivery-start race", and its TODO entry are back, saying why the queue route was reverted. The note also records a new measurement: the Codex TUI keeps a follow-up queued with Tab in its own state, not in App Server's queue (thread/queue/list returned no items while the TUI showed one queued, and the TUI sent its turn/start 17.5 ms after the turn completed), so reading App Server's queue before starting can't close the race. The TODO entry is a decision for Max again.

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

Checks: the focused Codex tests (test_codex_app_server, test_codex_adapter, test_hooks, test_responses, the layer contract, tests/process/website_server) pass, and the codex-app-server and website-adapter journeys passed.

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

…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]>

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The restored turn/start path can mark a Leaf comment answered with text written for a different Codex prompt. TaskConnection._start_delivery reads the task as idle, then start_app_server_delivery sends turn/start; a user follow-up queued behind the prior turn can start between those steps. App Server then returns the follow-up's turn ID after steering Leaf's delivery into it, and the fold binds that ID to the comment and commits the turn's final answer as its reply. The PR's Delivery-start race reproduction confirms this sequence. Please prevent a joined turn's final answer from settling the Leaf reply before this lands; checking the queue before the start alone still leaves a gap if its state changes after the check.

… 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]>
@max-sixty
max-sixty merged commit 14b77a1 into main Oct 20, 2026
2 checks passed
@max-sixty
max-sixty deleted the agent-a529870d7b75e2218 branch October 20, 2026 20:47
max-sixty added a commit that referenced this pull request Oct 20, 2026
…waiting and working (#3387)

Max asked that the reply benchmark record both Leaf's share and the
whole reply, and cover two cases: a comment that reaches a waiting
harness and one that reaches a working harness. This change makes
`leaf-dev journey` and `leaf-dev response-times` report both numbers for
every reply. The whole reply is broken into its model calls. The note
`notes/user-feedback-responsiveness.md` gets budgets for each number and
case, a measured table on current main, and the model calls a small page
change should take.

## The benchmark

- **One home for a reply's breakdown.** `dev/leaf_dev/reply_phases.py`
splits the span from a comment's admission to its reply into phases.
`busy` is the harness on other work before it takes the comment,
`delivery` runs until the comment is in the agent's context, and the
rest is `model`, `tool` and `idle` time between turns. `reply_breakdown`
sums the phases into the whole reply and Leaf's share (delivery plus
tool phases that run `leaf`). It also groups model phases into the model
calls the harness made, each with the tool calls it issued. The
journey's old `turn_phases` moved here.
- **Harness records carry turns and responses.** Each harness's
normalized record marks where a model response ended and where a turn
started and ended, using the harness's own signals. Claude Code's
transcript gives the last record of each message id, `turnPosition`,
`turn_duration` and `interruptedMessageId`. Codex gives
`thread/tokenUsage/updated`, `turn/started` and `turn/completed`. Pi
gives `turn_end`, `agent_start` and `agent_settled`.
- **Two cases in every harness journey.** `journey.CASES` names them.
The release ask reaches a waiting harness. The `mid-turn` step now asks
for the same small page change, naming a release of its own, while the
harness runs the user's 25 s command. Each step requires a revision that
names its release. Each sample says which comment asked for which case.
- **The table.** `uv run leaf-dev journey-chart --replies` tabulates the
kept samples by target and case.
- **Real pages.** `leaf-dev response-times` adds the breakdown for
inputs that Claude Code answered, reading the transcript it finds by
session id. On real pages Leaf's share is an upper bound, because a
shell command that runs `leaf` among slower work counts whole.

## Readings on current main

Four Claude Code journeys and three each on Codex App Server and Pi, at
this branch on main `23acce2b1`. Seconds:

| Harness | Case | Whole reply | Leaf's share | After the boundary |
Model calls |
| --- | --- | --: | --: | --: | --: |
| Claude Code | waiting | 21.9–45.2 | 1.1–8.7 | 21.9–45.2 | 4–8 |
| Claude Code | working | 35.5–41.9 | 0.9–1.2 | 10.8–17.0 | 2–3 |
| Codex App Server | waiting | 14.1–19.4 | 0.5–0.9 | 14.1–19.4 | 4–5 |
| Codex App Server | working | 38.6–43.1 | 0.7–0.8 | 13.9–18.4 | 3 |
| Pi | waiting | 31.0–33.0 | 1.0–1.5 | 31.0–33.0 | 8–10 |
| Pi | working | 40.0–41.7 | 0.7–0.8 | 14.8–16.5 | 6 |

In the working case the harness was busy with the user's command for
24.5–25.2 s first, so the working budget counts from that boundary.
Leaf's share was 0.5–1.5 s in 19 of 20 replies; the exception ran `leaf
page check --render`. The model takes the rest. The proposed budgets are
Leaf's share under 2 s in both cases, and a whole reply of Leaf's share
plus two model calls. The note says which calls the replies spent beyond
two: widget and registry lookups, and reference reads on a session's
first comment. It also says why the two cases differ in more than the
harness's state.

Codex App Server ran main's delivery after the revert of #3357 (#3362)
and #3368: Leaf starts a turn for an idle task, and the tool hook
delivers inline to a working one. Earlier batches on #3357's queue path,
before #3368 and #3372 landed, showed the two calls those changes
removed.

## Review

An independent Codex review ran over three rounds against real
transcripts and the kept runs. It found idle gaps counted as model time,
an idle harness read as busy, local commands and compaction summaries
starting turns, and `end_turn` blocks ending turns early. All of these
are fixed and checked on the page it used. The remaining limit is on
real pages: Leaf's share counts a whole shell command that runs `leaf`
among other work. The note proposes that each `leaf` command record its
own duration.

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

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Oct 21, 2026
…turn (#3380)

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_

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[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