Skip to content

Split the page transaction out of service, ending its backward imports - #7158

Merged
max-sixty merged 7 commits into
mainfrom
agent-ab21fde577807a56a
Nov 26, 2026
Merged

max-sixty merged 7 commits into
mainfrom
agent-ab21fde577807a56a

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

This PR splits the page transaction out of service. service was doing two jobs that sit at different heights in src/leaf's import order. Low-level readers such as page_inputs, the sandbox bridge, codex_state and activity need its page claims. Its PageTransaction needs event_log, interaction_log, harness, activity, event_contracts and revisioning. The order therefore put service below the event log, and import-linter allowed six backward imports from it. After the split, the count drops from 40 to 35.

  • New module, transaction. It takes PageTransaction and the claim changes made through one (take_page_claim, claim_page, restore_page_claim, and ClaimTransition, the record a failed claim's rollback restores from). It also takes owned_pages, commit_provider and the acknowledgement cursor (acknowledged, unacknowledged, requires_agent_attention). In the order it stands just above activity.
  • What service keeps. The page claims with page_holder, session_lives, the stream record (STREAM) and PREVIEW.
  • Log helpers made public. The transaction holds the log's lease, which serializes claim changes, the stream, the commit provider's publish and appends. So the three event_log helpers it calls are the log's interface for whoever holds that lease, and they lose their underscores: end_last_line, append_event_unlocked and matching_attempt.
  • set_status moves to session. PageTransaction.set_status becomes session.set_status(page, state), since a declaration is appended through admission, which takes the transaction. The transaction keeps clear_stream() for idle.
  • delivery_turn reads through presence. codex_state.delivery_turn now calls presence.session_turn_running, so codex_state doesn't import the transaction above it.
  • One allowance added. transaction -> revisioning closes a real cycle. A transaction finishes an interrupted publication before admitting a reader, and finishing one appends through admission under that same transaction. The transaction.py header says so.

Most of the 116 files only change an import from service to transaction. The substance is in transaction.py, service.py, session.py, presence.py, codex_state.py and event_log.py. The cold hooks import the same modules as before, plus transaction, and import leaf.hooks takes the same time within noise.

Checks: pre-commit passes, ty and import-linter included. The focused model and process tests for claims, activity, receipts, session lifecycle, delivery, hooks, the Codex adapter and App Server, the Stop guard, forks, the interact contract and the website server pass. Main, with #7157, is merged in, and #7157's changes to the moved code are ported to transaction.py.

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

…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
leaf-agent
leaf-agent previously approved these changes Nov 25, 2026
Comment thread src/leaf/hooks.py
Comment on lines +95 to +97

if TYPE_CHECKING:
from .harness import Harness

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats the TYPE_CHECKING import of Harness that already stands just below.

Suggested change
if TYPE_CHECKING:
from .harness import Harness

leaf-agent
leaf-agent previously approved these changes Nov 25, 2026
#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.
@max-sixty
max-sixty merged commit f2aa572 into main Nov 26, 2026
5 of 6 checks passed
@max-sixty
max-sixty deleted the agent-ab21fde577807a56a branch November 26, 2026 00:19
max-sixty added a commit that referenced this pull request Nov 26, 2026
`src/leaf`'s modules move into eight layer packages, each holding one
concern: `commands`, `pages`, `serving`, `agents`, `records`, `log`,
`source` and `base`, top to bottom. Each package's `__init__.py` says
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.leases` importing the kernel's
protocol from `source.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:

- A reader given only each package's line and each module's docstring
put 78 of the 107 modules in the package this PR puts them in. Moving 27
of the other 29 to the reader's package would add between one and
thirteen backward imports.
- Over the 526 commits on `main` since 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 with `git
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.md` and `base/harness.py`'s
header;
- the eight `src/leaf/*/__init__.py` headers;
- the paths code computes from a moved module's file: `base/state.py`,
`agents/codex_tool.py`, `base/service.py`, `records/activity.py`,
`base/rows.py` and `pages/packages.py`, with `dev/leaf_dev/docs.py`,
`.config/knip.js`, `tests/process/test_bridge.py`,
`tests/model/test_interact_layer.py` and
`tests/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 takes `main` as a parent and
reruns the script on `main`'s tree. Each Python PR still open when this
lands needs `main` merged in, and its new imports of moved modules
renamed. The Python that `leaf-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 read `src/leaf`
by path or import most of it, run locally.

> _This was written by Claude Code on behalf of max-sixty_
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