Repository navigation
Move the Codex adapter's process beside the watch it runs, and the wake rule beside delivery, ending agents' last cycle - #7222
Merged
Conversation
…selection beside delivery Two `agents` modules reached up into `commands.session`. Both edges came from code sitting a layer away from what it uses or what uses it. The Codex adapter has no watch of its own: `run_adapter` builds a `session.Watch` and drives `read_watch_pass`, as `leaf wait` and a hook harness's watch between turns do. The watch revives a dead page server through `hosting.start_server`, so it stands above `pages`, and its three runners belong with it. `run_adapter` and `_hand_over`, the adapter's process and its handover to a successor copy, move to `commands/codex_run.py`, named for `leaf codex run`, the command they implement. What a claim, the hooks and the loop read of the adapter stays in `agents/codex_adapter.py`: its transports, `TaskConnection`, its lease record and its start. `_offer_queued_delivery` and `_start_adapter`, now called across the two modules, lose their underscores. Each header keeps the paragraphs about its own half. `presence.nudge_interrupted` imported `session.wake_inputs` and `delivery.deliverable` from inside the function. Its one caller is the page server's row publisher, whose header already describes it, so it moves to `serving/server_rows.py`. `wake_inputs`, which picks out of a deliverable batch what can wake a session, moves beside `deliverable` in `agents/delivery.py`; the between-turns watch, `session.nudge_pending` and `nudge_interrupted` all call it there. `presence` now imports neither `delivery` nor `commands`, which takes it out of the cycle among `codex`, `codex_state`, `delivery` and `thread`. The allowances `agents.codex_adapter -> commands.session` and `agents.presence -> commands.session` go. `wake_inputs` keeps its deferred `read_page`, now under delivery's existing allowance `agents.delivery -> serving.served_state.context`. A refactor: behaviour is unchanged. Tests that patched the loop's collaborators on `codex_adapter` patch them on `codex_run`.
`codex_adapter.capture_batch` read three fields of `session.PageTick`, the command layer's watch result, so `agents` still leaned on the shape of a `commands` value. It now takes the page's transaction and the batch, and the runner that holds the tick (`codex_run.run_adapter`) unpacks it. The tests that built a tick only to hand it over pass the transaction and batch directly. `wake_inputs`'s docstring said receipted input is absent from the batch; it now names the exception, an update still owed whose owner holds no usable `answer.ref`, which `deliverable` hands over again. `codex.py`'s header names `codex_adapter.py` as the header that says how `TaskConnection` folds notifications and handles failures, and the agent-journey reference sends a change to `delivery.wake_inputs` through the Claude Code and Pi journeys. A refactor: behaviour is unchanged.
Merges #7216 and #7217. #7217 took delivery's reads of the page's work to `presence.read_work`; this branch had already taken `nudge_interrupted` out of `presence` and `wake_inputs` out of `commands.session`, so the conflicts resolve to this branch's homes for both, `read_work` beside presence's readings, and neither `commands.session` allowance. `wake_inputs` read and folded the page again for a batch `deliverable` had just read by the same fold. It now takes that work reading, as `batch_data` does, and reads no page itself. A watch pass reads each page's work once, hands it to `deliverable` and keeps it on the tick (`PageTick.work`), so the between-turns watch selects its wake input by it; `nudge_pending` and `server_rows.nudge_interrupted` read the work once and pass it to both. A nudge, and a pass over a page with a batch, folds the page once instead of twice. The layers contract keeps three allowances, none of them a `commands.session` one, and `agents` holds no import cycle. Behaviour is unchanged.
A watch pass already reads each page's work to find its batch and keeps it on the tick, but `leaf wait`'s delivery (`session.delivery_record`) and the Codex capture (`codex_run`, `codex_adapter.capture_batch`, `codex.append_batch`) read and folded it again. Both now pass the tick's work on, as `batch_data` takes it, so a pass folds a page once. `append_batch` and `capture_batch` read the work themselves only where a caller hands none. Behaviour is unchanged.
# Conflicts: # src/leaf/agents/codex_adapter.py
leaf-agent
approved these changes
Nov 26, 2026
This was referenced Nov 26, 2026
max-sixty
added a commit
that referenced
this pull request
Nov 26, 2026
…a cycle (#7230) import-linter's layers contract checks only the imports between `src/leaf`'s eight packages, so the modules inside a package could import each other in a cycle, and six groups did. The cuts that ended them have landed (#7216, #7217, #7218, #7222, #7224). This adds import-linter's own `acyclic_siblings` contract over every package (`ancestors = leaf.*`). It recurses into subpackages such as `served_state` and `render_gate`, so a new cycle fails pre-commit as an upward import does, naming the import that closes it. A planted deferred import from `presence` into `delivery`, and one from `served_state.context` into `served_state.page`, each fail the contract; the tree as it stands passes. The config's header now says how to fix a cycle, and that a deferred import counts like any other, so deferring one ends no cycle; `src/AGENTS.md` states the rule beside the layering one. The maintainer asked for the cycles inside `agents` to be broken; this widens that to every package and keeps them broken. The two allowances the layers contract still lists are cut by #7223 and the PR after it. Tests: `lint-imports` (both contracts), pre-commit, and the two planted cycles above. > _This was written by Claude Code on behalf of max-sixty_
max-sixty
added a commit
that referenced
this pull request
Nov 26, 2026
A session was woken, or nudged, for advice when nothing it had to answer
was left. With an advisory render pending beside a comment the agent had
already answered before its receipt, `deliverable` returns both;
`wake_inputs` dropped the answered comment and returned the advice
alone, which the watch between turns, `nudge_pending` and
`server_rows.nudge_interrupted` each treated as new input ("has new
input").
`wake_inputs` now skips any event `requires_agent_attention` rejects,
the predicate admission already marks input with, so only input marked
for the agent's attention wakes it. The receipt batch is unchanged, so
the advice still reaches the agent with its next input.
Codex found this while reviewing #7222, which moved `wake_inputs` beside
`delivery.deliverable`; the bug predates the move.
Tests: `test_advice_wakes_nobody_once_what_came_with_it_is_answered`
checks the batch still holds both and that none of the three routes
wakes or nudges; each route failed it before the fix. `lint-imports`,
pre-commit, and 309 tests across 8 covering model files.
> _This was written by Claude Code on behalf of max-sixty_
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.
Ends two of
src/leaf's remaining backward imports,agents.codex_adapter -> commands.sessionandagents.presence -> commands.session, and the last edge of the import cycle amongcodex,codex_state,delivery,presenceandthread, which leavesagentswithout a cycle.run_adapterand_hand_over, its handover to a newer installed copy) moves fromagents/codex_adapter.pyto a newcommands/codex_run.py, named forleaf codex run. It runs the session watch (session.Watch,read_watch_pass), asleaf waitand the hooks' between-turns watch do, and the watch can't sit belowpagesbecause it revives dead servers throughhosting.codex_adapterkeeps what the loop drives and other processes read: the transports,TaskConnection, the lease record andpreparing_adapter, as Split the page server's records from its process's lifetime #7208 split the page server's records from its process.capture_batchnow takes the page's transaction and batch, so nothing inagentsreads the watch's tick.presence.nudge_interruptedmoves toserving/server_rows.py, its only caller.wake_inputs, the rule for which deliverable input wakes a session, which the between-turns watch,nudge_pendingandnudge_interruptedall apply, moves besidedelivery.deliverable. It now takes the work readingdeliverablealready holds, so a nudge or a watch pass folds the page once where it folded it twice.Behaviour is unchanged apart from that second fold going.
To weigh:
_offer_queued_deliveryand_start_adapterlost their underscores, sincecodex_runnow calls them across modules.This is one of the cuts the maintainer asked for to end
src/leaf's backward imports and the cycles inside its packages. Codex reviewed it and found no regression; its findings on where code reads the watch's tick and on stale pointers are folded in. It also reproduced a pre-existing bug, advice waking a session after its only actionable input was answered, which gets its own PR.Tests:
lint-imports, pre-commit, and 482 covering model tests (the adapter, App Server, hooks, wait, native watch, delivery, receipts, stop guard, server lifetime, handover pass, hook registration and session lifecycle). The edited journey files compile but weren't run here; CI runs them.