Skip to content

Move harness behaviour that needs higher modules above them, ending six backward imports - #7153

Merged
max-sixty merged 4 commits into
mainfrom
agent-af097e2b73dd422bd
Nov 25, 2026
Merged

max-sixty merged 4 commits into
mainfrom
agent-af097e2b73dd422bd

Conversation

@max-sixty

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

Copy link
Copy Markdown
Owner

harness stands low in src/leaf's import order, because the event log, admission and claims all read a harness. Several of its methods called up into modules ordered above it, importing each inside the method, which import-linter allowed as six backward imports. This PR keeps in harness the identity, the declared facts and the methods that call only modules below it. Each behaviour that needed a higher module now lives above that module and dispatches on a fact the harness declares, so none of them compares a harness's name. Allowances: 48 to 42.

Two facts are new:

  • Harness.adapter: the session's input goes through Codex's adapter and its delivery record.
  • Harness.inference: how Leaf asks the harness's model, one of completion, app-server or print.

Where each behaviour went:

  • Inference and thread titles: inference.harness_generator(harness) reads inference. event_endpoint builds the title generator from it, replacing Harness.inference_generator and Harness.title_generator.
  • Claims: hosting.preparing_delivery starts or joins the adapter where adapter is set, replacing Harness.preparing_delivery. codex_adapter.preparing_adapter now holds the check that a running leaf wait stays the watcher. cmd_claim moved from service to hosting, because service sits below the adapter it starts.
  • Codex's tool-hook and Stop offer: this is now hooks.offer_at_step, called where adapter is set. Codex's rule that a later turn takes the input moved to codex_adapter.later_turn_takes_input, beside the record it reads. hook_transport.carry_turn now asks watches_between_turns(), which gives the same answer there, because Codex's hooks never put input into a delivery.
  • Delivery receipt: Harness.receive_pointer returns None by default, and delivery.receive_delivery then takes the ordinary receipt. Harness.entered_context returns the hook contexts it finds in the transcript, and delivery.confirm_entered reads the inputs out of them, so harness only reads its own transcript format.

To weigh: adapter and inference each match one harness today, so they can read as harness names under another name. They state what Leaf dispatches on, and a new harness sets them without any module outside harness changing. The Codex hook offer stays in hooks rather than moving into codex_adapter, because importing codex_adapter costs about 50 ms, and Codex runs that hook after every tool call.

Checks: pre-commit passes, import-linter included. About 600 focused tests pass, covering hooks, thread titles, the Codex adapter, receipts, delivery, read state, claims, the Stop guard, waits, responses, hook registration, activity, session lifecycle, App Server, native watch, server lifetime, the Claude Code module, Pi and LLM. test_forks' stub harness now declares inference = None in place of the removed title_generator. The two journey tests this PR edits weren't run locally. git merge-tree against the head of #7114, another session's open PR, reports no conflicts.

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`.

@leaf-agent leaf-agent left a comment

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.

tests/model/test_forks.py::test_a_thread_message_reaches_the_session_at_once_and_its_line_goes_in_beside_it fails on this head. Its stub harness, class _Untitled, still answers the removed title_generator() and doesn't declare inference. event_endpoint now calls harness_generator(harness), which reads harness.inference:

src/leaf/inference.py:50: in harness_generator
    if harness.inference == "completion":
AttributeError: '_Untitled' object has no attribute 'inference'

The fix is to replace the stub's title_generator staticmethod with inference = None, the declaration that now says "this harness names no thread". The line isn't in the diff, so I can't attach a suggestion to it; I'm happy to push that commit if you'd like.

test_forks.py, test_hooks.py and test_read_state.py otherwise pass (107 tests), and import-linter keeps the contract with 42 allowances.

@max-sixty
max-sixty merged commit 77a3e46 into main Nov 25, 2026
6 checks passed
@max-sixty
max-sixty deleted the agent-af097e2b73dd422bd branch November 25, 2026 10:11
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