Repository navigation
Move src/leaf's modules into eight layer packages - #7191
Conversation
…ix backward imports `harness` sits low in the import order because the event log, admission and claims read a harness, yet six of its methods imported modules above it (`codex`, `codex_adapter`, `delivery`, `hook_transport`, `inference`, `thread_titles`) at call time. Each method now either calls only modules below `harness`, or its body moves above the module it drives and dispatches on a fact the harness declares, so `.config/importlinter.ini` drops all six allowances (50 to 44). What moved: - `Harness.adapter` (Codex: True) replaces the polymorphic delivery methods. `CodexHarness.preparing_delivery`/`ensure_delivery` become `hosting.preparing_delivery`, which starts or joins the adapter through `codex_adapter.preparing_adapter` (now also holding the "a model wait stays the watcher" check). `CodexHarness.offer_at_step` becomes `hooks.offer_at_step`, called only where `adapter` holds. The Codex Stop rule, `later_turn_takes_input`, moves to `codex_adapter`, beside the record it reads; `hook_transport.carry_turn` asks `watches_between_turns`, which is the same answer for every harness whose batches it freezes. - `service.cmd_claim` moves to `hosting`, beside `claim_and_start`, since `service` sits below the adapter it prepares. - `Harness.inference` names the inference transport (completion, app-server, print); `inference.harness_generator` maps it to a generator, replacing the three `inference_generator` methods. `Harness.title_generator` goes: `event_endpoint` composes `thread_titles.title_generator` itself. - `Harness.receive_pointer` returns None by default, and `delivery.receive_delivery` falls back to `receive`, so the base method no longer calls back into `delivery`. - `Harness.entered_context(payload, marker)` returns the hook contexts the transcript shows entered; `delivery.confirm_entered` reads the input ids out of them with `hook_transport.inline_delivery`, so the harness reads only its own transcript format. The Codex step offer lives in `hooks` rather than `codex_adapter` because importing `codex_adapter` costs about 50 ms beyond `hooks`, `service` and `codex` (measured once on this machine under load), and Codex's tool hook runs after every tool call; `hooks.offer_at_step` imports it only at Stop, as the method did. The guarantee in `harness.py`'s header holds: modules outside the harness code read declared facts, and none compares a harness's name or reads its environment variables. Tests: the transcript and title seams follow the new signatures (`entered_context` returns contexts, `inference = None` disables titling), and the delivery-failure journey patches `hosting.preparing_delivery`.
`service` held two concerns at different heights. Its claims are read low in the module order (`page_inputs`, the sandbox bridge, `codex_state`, `activity`), while its `PageTransaction` sits above the event log, `interaction_log`, `harness` and `activity`. So the order put `service` below `event_log`, and six imports from it pointed up. The transaction and the log are separate concerns rather than one: the log's lease serializes claim changes, the claimant's stream and the commit provider's publish as well as appends. The three `event_log` helpers the transaction called are the log's interface to whoever holds that lease, so they lose their underscores (`end_last_line`, `append_event_unlocked`, `matching_attempt`). - A new `transaction` module takes `PageTransaction`, the claim changes made through one (`take_page_claim`, `claim_page`, `restore_page_claim`), `owned_pages`, the acknowledgement cursor (`acknowledged`, `unacknowledged`, `requires_agent_attention`), `commit_provider` and `STREAM_ACTIVITY_RENEWAL`. `service` keeps the page claims and the stream record (`STREAM`). - `PageTransaction.set_status` becomes `session.set_status(page, state)`: a declaration is appended through admission, which takes the transaction. `PageTransaction.clear_stream` does what an `idle` declaration did to the stream. - `PageTransaction.matching_attempt` goes: the append door calls `event_log.matching_attempt(page.events, event)`, as it already did for its trial. - `codex_state.delivery_turn` reads its pages through `presence.session_turn_running`, so `codex_state` doesn't import the transaction, which now stands above it. - In `.config/importlinter.ini`, `codex` moves up to stand above `transaction`, which stands above `activity`. The six `leaf.service -> ...` allowances go. One takes their place, `leaf.transaction -> leaf.revisioning`: a transaction finishes an interrupted publication before admitting a reader, and finishing one appends reanchors through admission under that same transaction, so `revisioning` imports the transaction and stands above it. 42 allowances become 37. `harness` and `activity` stay imported inside the functions that use them, as `service` had them, so the cold hooks load the same modules as before plus `transaction` (measured: `import leaf.hooks` takes the same time within noise). Behavior is unchanged. Tests follow the moved names, and three that patched a name the transaction now reads from its own module (`now_iso`, `read_stream`, `flocked`) patch it there. > _This was written by Claude Code on behalf of max-sixty_
# Conflicts: # src/leaf/delivery.py # src/leaf/event_endpoint.py # src/leaf/service.py # src/leaf/thread.py
# Conflicts: # src/leaf/thread.py
src/leaf's modules move into commands, pages, serving, agents, records, log, source and base, top to bottom, each package holding one concern. The import-linter contract names the packages in place of a flat order of modules: a module imports its own package and the packages below it, and 13 imports that still point up across packages are allowed by name. Of the flat order's 35 allowances, 21 fall inside one package, where direction is no longer checked. The maintainer chose packages over naming the layers in the config or keeping the flat order. A reader given only the package descriptions and module docstrings placed 79 of 108 modules as here; 20% of co-change on main since 1 October falls inside a package, against 13% for random packages of the same sizes. Generated: ruff's TID252 fix makes relative imports absolute, then a script moves each module and rewrites its dotted name, `from leaf import` lines and paths across the tree. Paths modules compute from their own file are patched by hand.
leaf-agent
left a comment
There was a problem hiding this comment.
The generator gets two kinds of path wrong. Both fixes belong in the script, since this diff is regenerated on main at landing. Everything else I checked holds:
- import-linter passes.
- Every module under
leafimports. - Every other
src/leaf/…path the tree mentions resolves. bin/leaf --help,python3 -S src/leaf/base/state.pyand the Codex MCP server underpython3 -Iall start.
A data file that moved with its module, reached by a composed path. interaction-batch.schema.json moved to src/leaf/log/ with interaction_log.py. But dev/leaf_dev/docs.py builds its path as SCRIPTS / "interaction-batch.schema.json", which the textual rewrite never saw. docs.interaction_entries() now raises FileNotFoundError: …/src/leaf/interaction-batch.schema.json, so filling docs/data-structures.html (its div.schema-list) breaks the site build.
Contract docs that didn't move, rewritten as if they had. src/leaf/events.md and src/leaf/validation.md stay at the top of src/leaf. The rewrite of the events and validation module paths caught them as prefixes, so these references now point at files that don't exist:
src/leaf/log/events.md:dev/leaf_dev/docs.py(EVENTS_REFERENCE, the link on every event kind the site documents),docs/data-structures.html, three links indocs/how-it-works.html,notes/comparisons.md,skills/leaf/references/module-annotations.md.src/leaf/pages/validation.md: two citations inbrowser/AGENTS.md.
The script needs two changes. Rewrite only a module's own path (<name>.py, or <name>/ for a package), not a bare prefix. And move each sibling data file's composed references along with it, or check the result by resolving every src/leaf/ path in the tree. Running that check over this head reports exactly these two .md targets. The schema path needs its own check, since the rewrite never saw it as a path.
I'm leaving the verdict open because the description says this lands after #7158 and #7157 and is regenerated on main first.
The generator now rewrites a directory path only for a subpackage, so the state home's `leaf/packages/` and the page directory's folders keep their names; skips `.md` after a module name, so `events.md` and `validation.md` keep their place at src/leaf's top; leaves links pinned to a commit alone; and patches the paths tests and docs build from a moved module's file. base/harness.py's header and the package headers now say what holds after the move.
#7157 changed the claim code and `PageTransaction` that this branch moves out of `service`. Each change goes to its home under the split: - `service` keeps `publish_claim` (which now returns the bytes of the claims of other shapes it removes), `page_holder`, `may_receive`'s new page argument, `held_elsewhere`'s holder words, `refuse_held`, `claim_records` and `PREVIEW`, which #7157 moved here from `server` so the hooks can ask whether a page carries a preview. - `transaction` takes `ClaimTransition`, the rollback record of a claim's publication, with `PageTransaction.holder`, `publishing_claim`, `restore_claim` (which writes back the displaced claims), `watch_state` and `note_messaged` as #7157 left them, and `take_page_claim` and `restore_page_claim` returning and taking a `ClaimTransition`. `page-storage.md` names `service.PREVIEW`. Tests and dev code that #7157 changed follow the moved names. The import-linter allowances go from main's 40 to 35.
The move is generated, so rather than resolve the service split's new merge of main (#7157) by hand, this records the split's head as a parent and reruns the codemod on its tree. #7157 moved `PREVIEW`, with its read of the served-state schema, from `server` to `service`, so the path patch follows it to `base/service.py`.
…anded The move is generated, so this records main as a parent and reruns the codemod on main's tree rather than merging main in by hand.
src/leaf's modules move into eight layer packages, each holding one concern:commands,pages,serving,agents,records,log,sourceandbase, top to bottom. Each package's__init__.pysays what it holds. import-linter's contract names the packages in place of a flat order of modules: a module may import its own package and the packages below it, and 13 imports that point up across packages are allowed by name.Those 13 are 7 of the flat order's 35 allowances and 6 imports that pointed down in the flat order but point up between the packages their modules now sit in, such as
base.leasesimporting the kernel's protocol fromsource.registry.kernel. Each of the 6 is a module placed by its concern at the cost of one allowance; cutting them is follow-up work, as the splits before this did. Of the other 28 old allowances, 21 fall inside one package, where the contract no longer checks direction, and 7 now point down.The maintainer chose packages over naming the layers in the config or keeping the flat order. Before the move I tested the packages two ways beyond the import graph:
mainsince 1 October, 20% of the co-change between modules falls inside a package, against 13% for random packages of the same sizes (p < 0.0001). Packages drawn from co-change would hold 65% but leave 127 backward imports, since Leaf's changes cut across layers: the median PR touches two of these packages.A script generates the diff. ruff's TID252 fix makes
src/leaf's relative imports absolute. The script then moves each module withgit mv, and in every tracked text file it rewrites the module's dotted name (in imports, strings and comments),from leaf import <module>, and its path. It also deepens the relative paths in moved files that climb to the repository's top. Everything else is renames and rewritten import lines, so review these:.config/importlinter.ini,src/AGENTS.mdandbase/harness.py's header;src/leaf/*/__init__.pyheaders;base/state.py,agents/codex_tool.py,base/service.py,records/activity.py,base/rows.pyandpages/packages.py, withdev/leaf_dev/docs.py,.config/knip.js,tests/process/test_bridge.py,tests/model/test_interact_layer.pyandtests/model/interact_contract/test_layer_contract.py.The branch is regenerated on
main, which now holds #7157 and #7158, rather than merged with it: its last commit takesmainas a parent and reruns the script onmain's tree. Each Python PR still open when this lands needsmainmerged in, and its new imports of moved modules renamed. The Python thatleaf-dev's evals run inside an arm's own checkout, such as its cleanup, imports modules by their new paths, so an arm pinned to a revision before this move can't run it.Tests: pre-commit (ruff, ty, import-linter, vulture, knip),
pnpm run check, an import of every module, and 2,533 tests that readsrc/leafby path or import most of it, run locally.