Skip to content

Queue each Codex App Server delivery instead of starting its turn - #3357

Merged
max-sixty merged 4 commits into
mainfrom
agent-af1737630c736d03b
Oct 20, 2026
Merged

max-sixty merged 4 commits into
mainfrom
agent-af1737630c736d03b

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

App Server's turn/start steers its input into a turn that is already running and answers with that turn's id. When the user queued a follow-up in Codex, App Server started it about 0.4 s after the running turn ended; the adapter's idle check read idle in that gap, its turn/start joined the follow-up's turn, and Leaf posted the follow-up's answer as its reply to the user's comment. Reproduced before this change with the note's script on codex-cli 0.160.0: Leaf's turn/start landed in the turn holding Reply: USER FOLLOW-UP, and that turn's answer was the one Leaf bound.

Each delivery now goes on the task's App Server queue (thread/queue/add, under the experimentalApi capability) as the user message of a turn of its own, named by the delivery id as its clientUserMessageId. App Server orders it with the user's queued input and never steers it. Leaf starts the queue (thread/queue/start) only when its delivery heads it, since a task whose last turn was interrupted holds its queue until a client starts it. With the same reproduction after the change, the follow-up and Leaf's delivery ran as separate turns, and Leaf adopted the one whose user message carried its delivery.

Behavior changes:

  • The agent receives a delivery as a user message, Leaf's opening line then the envelope as JSON (delivery.inline_delivery, the form a hook hands over inline), instead of a leaf_delivery tool output. Leaf's submissions show in the user's Codex queue while they wait. harness-codex-app-server.md and the skill's input list say so.
  • The adapter binds a delivery to the turn whose user message carries its id (clientId), when that turn starts. It no longer resumes the task for a fresh idle reading first; a running turn still holds the delivery back, so the tool hook can hand it to that turn between steps. A submission whose answer was lost waits while App Server's queue holds it, and is abandoned only once neither the queue nor the complete history holds it on an idle task.
  • leaf codex start lists the task's queue once before starting the adapter, so a Codex without the experimental queue is refused in front of the agent rather than refusing every delivery into the adapter's log.
  • leaf.page's container queues its deliveries the same way and names the turn at the next turn/started on its thread, since it is the thread's only client. On any failure after a submission it takes the delivery back out of the queue before interrupting, so a held queue can't strand it at the head.

The decision and its measurements move from notes/codex-integration.md into codex.py's header, and its TODO entry is retired. A new TODO entry records a pre-existing case for Max: a user who presses Enter in Codex while Leaf's delivery turn runs steers into it, and that turn's final answer still posts as the page reply.

Tests: the steering scenario in the measured order (the user's follow-up queued ahead, Leaf's delivery answering in its own turn), a held queue Leaf starts from its head, a delivery held behind the user's input that waits rather than being abandoned, a Codex without the queue refused at start, and the existing start, uncertainty and recovery cases moved onto the queue. The route walkthrough and the website's wire snapshot show the new input. Removed: the test that a systemError task raises before a start, and the one that a status read cannot resend an accepted delivery; both guarded the idle check this deletes. Journeys codex-app-server, codex-queue and website-adapter passed.

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

max-sixty and others added 4 commits October 20, 2026 11:20
App Server's `turn/start` steers its input into a turn that is already
running and answers with that turn's id. When the user queued a follow-up
in Codex, App Server started it about 0.4 s after the running turn ended;
the adapter's idle check read idle in that gap, its `turn/start` joined the
follow-up's turn, and Leaf posted the follow-up's answer as its reply to
the user's comment.

Each delivery now goes on the task's App Server queue
(`thread/queue/add`, under the `experimentalApi` capability) as the user
message of a turn of its own, named by the delivery id as its
`clientUserMessageId`. App Server orders it with the user's queued input and
never steers it. Leaf starts the queue (`thread/queue/start`) only when its
delivery heads it, since a task whose last turn was interrupted holds its
queue until a client starts it.

Behavior changes:

- The agent receives a delivery as a user message: Leaf's opening line,
  then the envelope as JSON (`delivery.inline_delivery`, the form a hook
  hands over inline), instead of a `leaf_delivery` tool output. Leaf's
  submissions show in the user's Codex queue while they wait.
- The adapter binds a delivery to the turn whose user message carries its
  id, when that turn starts, rather than to the turn a start request named.
  It no longer resumes the task for a fresh idle reading before a delivery;
  a running turn still holds the delivery back, so the tool hook can hand it
  to that turn between steps. A submission whose answer was lost waits while
  App Server's queue holds it, and is abandoned once neither the queue nor
  the complete history holds it on an idle task.
- leaf.page's container queues its deliveries the same way and names the
  turn at the next `turn/started` on its thread, since it is the thread's
  only client.

The decision and its measurements move out of `notes/codex-integration.md`
into `codex.py`'s header, and the TODO entry is retired.

Tests: the steering scenario as measured (user follow-up queued ahead,
Leaf's delivery answering in its own turn), a held queue Leaf starts, and
the existing start, uncertainty and recovery cases moved onto the queue.
The route walkthrough and the website's wire snapshot show the new input.
Removed: the test that a `systemError` task raises before a start, and the
one that a status read cannot resend an accepted delivery; both guarded the
idle check this deletes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…eue withdrawn hosted deliveries, test the held queue

- `leaf codex start` lists the task's App Server queue once before it starts the
  adapter, so a Codex without the experimental queue is refused in front of the
  agent instead of refusing every delivery into the adapter's log.
- The adapter's delivery start returns whether a turn has taken the delivery, so
  the loop waits for news after a submission instead of reconciling the full
  history while App Server's answer outruns the turn's first notification.
- leaf.page's container withdraws a delivery on any failure after its
  submission, and takes it out of App Server's queue, where after an interrupt
  it would hold back every later delivery on the thread.
- A test holds that a submission the queue still holds behind the user's input
  waits rather than being abandoned; prose now matches the measured order.
- TODO records the open decision on a user's steer into Leaf's delivery turn.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
`check_delivery_queue` let a connection failure escape as a traceback; it is a
Refused again, worded as the adapter's own connection failure. The request
docstring no longer claims a turn's notifications precede the answer to the
request that started it, which the probes contradict.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
# Conflicts:
#	skills/leaf/scripts/leaf/codex_adapter.py
#	tests/model/interact_contract/_regtest_outputs/test_layer_contract.test_each_route_hands_the_agent_what_the_snapshot_shows__0/snapshot.yaml
#	tests/process/website_server/_regtest_outputs/test_app_server.test_hosted_agent_receives_the_response_instructions_and_delivery__0/snapshot.yaml
@max-sixty
max-sixty merged commit ef47d1a into main Oct 20, 2026
3 checks passed
@max-sixty
max-sixty deleted the agent-af1737630c736d03b branch October 20, 2026 18:56
max-sixty added a commit that referenced this pull request Oct 20, 2026
…turn" (#3357) (#3362)

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_

---------

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