Repository navigation
Move harness behaviour that needs higher modules above them, ending six backward imports - #7153
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`.
leaf-agent
left a comment
There was a problem hiding this comment.
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.
harnessstands low insrc/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 inharnessthe 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 ofcompletion,app-serverorprint.Where each behaviour went:
inference.harness_generator(harness)readsinference.event_endpointbuilds the title generator from it, replacingHarness.inference_generatorandHarness.title_generator.hosting.preparing_deliverystarts or joins the adapter whereadapteris set, replacingHarness.preparing_delivery.codex_adapter.preparing_adapternow holds the check that a runningleaf waitstays the watcher.cmd_claimmoved fromservicetohosting, becauseservicesits below the adapter it starts.hooks.offer_at_step, called whereadapteris set. Codex's rule that a later turn takes the input moved tocodex_adapter.later_turn_takes_input, beside the record it reads.hook_transport.carry_turnnow askswatches_between_turns(), which gives the same answer there, because Codex's hooks never put input into a delivery.Harness.receive_pointerreturns None by default, anddelivery.receive_deliverythen takes the ordinary receipt.Harness.entered_contextreturns the hook contexts it finds in the transcript, anddelivery.confirm_enteredreads the inputs out of them, soharnessonly reads its own transcript format.To weigh:
adapterandinferenceeach 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 outsideharnesschanging. The Codex hook offer stays inhooksrather than moving intocodex_adapter, because importingcodex_adaptercosts 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 declaresinference = Nonein place of the removedtitle_generator. The two journey tests this PR edits weren't run locally.git merge-treeagainst the head of #7114, another session's open PR, reports no conflicts.