Skip to content

Carry a comment sent before a Codex turn's first tool call quietly - #3460

Merged
max-sixty merged 2 commits into
mainfrom
agent-a5d927f6f0a7aa487
Oct 21, 2026
Merged

max-sixty merged 2 commits into
mainfrom
agent-a5d927f6f0a7aa487

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Builds Max's decision #42 on the overnight report, "Carry it quietly": trust a Codex turn's hooks from the turn's start.

On the codex queue route (the desktop app and IDE), the adapter left a running turn's input to its hooks only once a tool step had run in the task's current instance (step_hook_ran). The desktop app unloads an idle chat, and the instance's end retires that mark, so in the next turn a comment that arrived before the first tool call went to codex queue. Codex runs a queued pointer only once the running turn ends, as a turn of its own opening on a leaf-delivery message in the user's name, which is the noise Max ruled out ("In codex, adding a message to the user turn is very noisy to the user, so we only want to do it when we need to").

What changes

  • The queue adapter leaves a running turn's input to it from the turn's start, wherever Leaf will see that turn end (codex_adapter._offer_queued_delivery). The comment then enters quietly at the turn's first tool step, or at Stop in a turn that runs no tool. Leaf sees the end through the hooks, where any Leaf hook has run in the task's instance (leases.hooks_ran), as the turn's prompt hook does at its start, or through Codex's transcript, which the task's hooks named in its session record and which the adapter reads before each offer. The transcript matters for the first second of a turn after an unload, when it shows the turn before the new instance's first hook has run.
  • No path loses a comment. Waiting costs nothing on this route, since a queued pointer would wait for the turn to end anyway. A turn whose tool and Stop hooks don't run still ends in Codex's transcript or by Interrupt, and the adapter then queues the comment, or a pointer the turn left unread. A task with neither a hook run nor a known transcript has nothing that would show its turn end before the claim's turn ages out after 15 minutes, so there the adapter queues at once, as before.
  • One hook proof. CodexHarness.turn_takes_input, which the page's activity reads, uses hooks_ran too, so the step-hook mark (mark_step_hook, step_hook_ran, STEP_HOOK_SUFFIX) is deleted, along with the adapter's special case for a Stop offer made before any tool step, which the new condition covers.
  • A journey step covers it. The Codex journeys gain an unload step. On codex-queue the user leaves the task (thread/unsubscribe), which closes it and runs its SessionEnd hook as the desktop app's unload does, and resumes it. On both routes a comment is then sent once the claim names the user's next turn and before its first tool call, and the step requires the comment to enter that turn and start no turn of its own. The journey now resumes a task with the cwd, approval policy and sandbox it started with, since a bare thread/resume reset the approval policy and the task's leaf MCP tool then asked for approval.

The App Server route is unchanged: its adapter already waits for any running turn, and its Stop hands nothing in.

Evidence (codex-cli 0.160.0)

  • An App Server probe showed that thread/unsubscribe from a thread's last client closes the thread (thread/closed) and runs its SessionEnd hook, which retired the hook mark. thread/resume runs no hook; the next turn's SessionStart and prompt hooks do.
  • On main with the new step, codex-queue fails it: the comment was queued while the turn wrote its opening sentences, the turn's Stop handed it in, and Codex then opened another turn on the leaf-delivery message (the comment sent before the first tool call started a turn of its own). codex-app-server passes it.
  • A first version that trusted only a hook run failed the same way: the claim named the new turn from the transcript, the comment arrived 120 ms later, and the instance's first hook ran about a second after that, so the adapter queued the comment. Reading the transcript as well closes that gap.
  • With this change both journeys pass every step. In the codex-queue run the comment entered the user's turn at its printf step and was answered there, and no other turn started.

Tests

  • test_a_comment_before_a_turns_first_tool_step_enters_that_turn: with the turn known from its prompt hook (entering at the first tool step, and at Stop) or only from the transcript after an instance ended, the adapter queues nothing and the comment opens in the user's turn. All three fail on main, where the adapter queues it.
  • test_a_comment_left_to_a_turn_whose_hooks_never_run_is_queued_as_it_ends: the adapter holds the comment while the turn runs, and queues it once the transcript records the turn's end, with no hook run. It fails on main, which queues at once.
  • The adapter tests whose tasks run no hook and name no transcript, such as test_codex_serializes_later_input_behind_the_offered_delivery, still queue at once; they failed against a version that trusted every running turn.
  • Tests that marked a tool step mark a hook run instead.

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

max-sixty and others added 2 commits October 21, 2026 01:20
On the `codex queue` route the adapter left a running turn's input to its
hooks only after a tool step had run in the task's current instance. After the
desktop app unloads an idle chat, a comment sent as the next turn starts went
to `codex queue`, and Codex opened a turn of its own on a `leaf-delivery`
message in the user's name.

The adapter now leaves a running turn's input to it from the turn's start
wherever Leaf will see the turn end: a Leaf hook has run in the instance, or
the session record names Codex's transcript, which the adapter reads. The
comment enters at the first tool step or at Stop, and a turn whose hooks never
run still ends in the transcript, when the queue takes it. A task with neither
queues at once, as before. `CodexHarness.turn_takes_input` uses the same hook
proof, so the step-hook mark is deleted.

The Codex journeys gain an `unload` step that ends the task's instance on the
queue route and sends a comment before the next turn's first tool call, and
resume a task with the settings it started with.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit 9f01f31 into main Oct 21, 2026
2 of 3 checks passed
@max-sixty
max-sixty deleted the agent-a5d927f6f0a7aa487 branch October 21, 2026 08:33
max-sixty added a commit that referenced this pull request Oct 21, 2026
A page server now installs its `exit_serving` signal handlers as its
service commits, before its URL leaves the process. Before this change
it installed them only after the URL was printed. This is a follow-up to
#3452 (Max's decision #34 on the overnight report).

**The bug.** #3452 installed the handlers just before the server started
its threads, which comes after `server run` prints the URL. Ctrl-C sent
as soon as the URL appeared could land in between. It then took the old
unwinding path and exited 1 with `{"error": "interrupted"}` instead of
130.
`test_server_run_persistent_declines_the_claim_a_harness_session_offers`
sends SIGINT as soon as it reads the URL line, and it failed this way on
CI in #3455, #3459 and #3460
([run](https://github.com/max-sixty/leaf/actions/runs/39568649930/job/119117041536)).
A user pressing Ctrl-C right after the URL appears hits the same window.

**The fix.** `cmd_serve`'s `commit()` installs the handlers once the
service is committed and before it returns. Both paths that hand the URL
out run after `commit()`: `server run` prints it, and `_serve` sends the
handshake confirmation that `server start` prints. A signal during the
commit still unwinds, so a failed commit still rolls the service back.

**Checked.** A temporary test (not committed) started `leaf server run
--persistent`, read the URL line and signaled at once. With a 0.05 s
sleep injected right after the URL is printed:

| Version | SIGINT trials | Exited 130 | SIGTERM trials | Exited 143 |
|---|---|---|---|---|
| main | 100 | 0 (all exited 1, "interrupted") | 100 | 100 |
| this change | 100 | 100 | 100 | 100 |

SIGTERM's status doesn't show which path it took, since the unwinding
path also exits 143. Without the injected sleep, 100 trials of each
signal passed on both versions on a lightly loaded Mac; the failure
needs CI's scheduling delay. The focused server lifetime, server
process, title and preview handoff tests pass (193).

> _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