Repository navigation
Carry a comment sent before a Codex turn's first tool call quietly - #3460
Merged
Merged
Conversation
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]>
leaf-agent
approved these changes
Oct 21, 2026
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 queueroute (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 tocodex queue. Codex runs a queued pointer only once the running turn ends, as a turn of its own opening on aleaf-deliverymessage 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
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.CodexHarness.turn_takes_input, which the page's activity reads, useshooks_rantoo, 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.unloadstep. Oncodex-queuethe 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 barethread/resumereset the approval policy and the task'sleafMCP 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)
thread/unsubscribefrom a thread's last client closes the thread (thread/closed) and runs its SessionEnd hook, which retired the hook mark.thread/resumeruns no hook; the next turn's SessionStart and prompt hooks do.mainwith the new step,codex-queuefails 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 theleaf-deliverymessage (the comment sent before the first tool call started a turn of its own).codex-app-serverpasses it.codex-queuerun the comment entered the user's turn at itsprintfstep 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 onmain, 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 onmain, which queues at once.test_codex_serializes_later_input_behind_the_offered_delivery, still queue at once; they failed against a version that trusted every running turn.