Repository navigation
Make browser ownership explicit across state and UI - #525
Conversation
leaf-agent
left a comment
There was a problem hiding this comment.
Five behaviour changes the description does not claim, four of them guards the old modules held. Each is inline except the last.
runtime.restoringState no longer marks a refused action's rollback. The base pass set it around reconciliation whenever a rejected entry still held a projection — if (outbox.some((entry) => entry.rejected && entry.projection)) runtime.restoringState = true; in projection.js — and motion() reads it to collapse the animation. At HEAD the version activation in state-application.js is the only writer left, and projection/presentation.js's present() never touches it, so a refused lf-board move plays the FLIP back to the authoritative position: the server's correction is presented as motion the reader caused. Setting it around present() under the same condition restores it.
…gain (#808) `TODO.md` #4. `application.ts` folded the whole Ask reading a second time — which Asks a document holds, which of them the reader still owes, every declared target's awaiting value, and each thread's `awaits_reader` — over DOM-captured descriptors, while `leaf.asks` folds all of it under the page transaction that answers every state read. The browser copy is gone and the wire is what it reads. Deleted: `deriveAsks` (176 lines), `deriveThreadReaderObligations` (55), the ask predicates between them, `admittedUnansweredAsks` with the approval gate's `projectionDeferred` branch, and `askDescriptor` / `conversationDescriptor` / `retirementAncestors` in `widget-descriptors.js`, which existed only to feed them. In their place, `normalizedAsks` maps the wire's records to the public `{id, tag, sourceId, sourceTag, thread}` shape, page asks before thread asks. `served_state/document.py` serializes the page reading it has been computing and dropping since #747, and `served_state/conversation.py` builds its own through one `_AskReducer` rather than two. Each listed Ask now names the `source` that answers it beside the surface the reader is sent to, which is what the browser's records carried and the wire did not; `leaf state` shows both as well. The two copies of the `when` predicate disagreed: Python asks whether an attribute is present and TypeScript asked whether it was non-null, so an attribute a value record had set to `null` read differently on each side. Python's reading is now the only one, and the browser's action-availability gate and the server's POST door read the same `unanswered_awaiting` map. ## What moves on screen **The Ask reading is no longer optimistic, and that is the one judgement call in this change.** A reader still sees their own gesture in its widget at once — the pick, the cleared pick, the swiped card, the withdrawn classification, the controls rebuilt by an Undo — because the unresolved-attempt ledger, `foldThreads`' pending branch, the DOM-captured authored baseline and `foldWidgetStates` all stay. But the Asks tray, the banner count, the `a`/`A` walk, the approval gate and an action's `requires` gate now turn over when the state that gesture's own POST returns is adopted, where they used to turn over as the gesture was sent. Keeping them optimistic is what the deleted code was for: doing it exactly needs the answer verbs, the `until` verb, the nonempty-facet rule, the completion containers, the rollup tree and the existence reading — which is the duplication itself. Against a local server the gap is one short round trip: answering and withdrawing an answer on `review-a-plan` in Chrome moved `Asks 0/1` and the Approve control with no visible lag, console clean. Eight tests asserted the reading at the held-POST moment and now assert it on the log's answer, and `CLAUDE.md`'s cross-runtime invariant now says which gestures the page can draw for itself and which wait for the log. Revert that paragraph and those eight tests together if the optimism should stay. One case is not that delay and is fixed rather than accepted (leaf-agent found it): a thread carrying a frozen Ask the reader still owes used to lose its "awaits you" mark the moment they started sending a reply and take it back when the reply landed — true → false → true, reversing direction rather than lagging. A thread the server still names in the reader's Ask list now keeps `awaits_reader` over `foldThreads`' pending-reply branch, read off that same list rather than folded again. ## A correction to the item `TODO.md` #4 also said `foldProjection` re-folds the winner resolution although the wire carries `actions` / `reports` / `desired`. It does not: it has consumed those id lists since #525. Its only re-derivation is `recompute()`, which runs solely for coordinates a pending entry touches, to reveal the next durable winner when a local undo withdraws the server's — the pending ledger's own work. Removing that would take a wire addition (per-coordinate ordered eligible candidates), not a deletion, so it stayed. ## Validation - `uv run pytest tests` — 1,145 passed on this head - `uv run pytest tests --run-nightly` on the first commit — 2,723 passed, 1 failure (`test_render_export.py::test_a_detached_preview_restarts_under_its_original_codex_claim`) traced to a `leaf ack` waiter left standing by my own earlier `scripts/preview.py` run; `tests/test_render_export.py` is 59/59 green with that state cleared, and 59/59 on the merge base as a control - `npm run check:browser` and `npm run test:browser` — 55 passed - `uv run pre-commit run --all-files` - Chrome, `review-a-plan` preview: answered the Ask, watched `Asks 0/1` → `1/1` and Approve enable; deselected, watched both reverse - `test_a_live_revision_reapplies_the_authored_conversation_seat_predicate` read `openAsks()` straight after `sending(...)` and failed 1 in 3 alone, because the Ask list is the applied state's now and `round_trip` returns one beat before `applyAcceptedState` finishes; it reads through `told(page)` and is 4/4 > _This was written by Claude Code on behalf of @max-sixty_ --------- Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Leaf's browser controllers depended on each other through a 47-module import cycle, making construction order and state updates difficult to reason about. This change replaces that cycle with explicit owners and capabilities, and enforces dependency direction in ESLint.
leaf.jsconstructs the application, conversation, navigation and geometry owners before mounting them. Pure models receive records; renderers receive the specific actions they invoke. The application owns one pending ledger, delivery queue and state-application transaction. Desired widget state is separate from proof that the current DOM node rendered it, so an accepted gesture is not resent or released merely because transport finished.State adoption and delivery reconciliation share a serialization boundary. Adoption rereads pending gestures after asynchronous preparation and restores the previous view and chrome after a required render failure. Background projection wakes wait for that boundary before releasing pending work; undo becomes available only after adoption completes. Navigation resolves its semantic destination again after revealing it, handling replaced or removed nodes. Panel visibility has one explicit state writer, and visual response controls remain withheld until presentation. Private imports and their test callers are cut over completely; the public widget helper contract and event protocol stay unchanged.
The site verifier now retains startup browser errors for cross-tab checks and prints each completed profile immediately, preserving evidence when a later page fails; a real-browser regression covers those diagnostics.
The runtime guide documents the new ownership boundaries. The graph gate checks cycles, transitive dependency direction, protected pure models and application imports. This adds explicit construction code and roughly 16 KiB of JavaScript before presentation in the measured pages; it removes one request per page.
Validation: the pre-integration refactor passed the complete 2,370-test suite, including nightly cases. The reviewed architecture passes all 46 outbox tests. After the latest integration with main, all 989 everyday tests, the startup regressions, every pre-commit hook, and the Worker type check and 59 tests pass. Focused checks exercise immediate reactions, resolution, approval and requests; refused and queued actions; startup control withholding; panel navigation; and asynchronous wakeups during failed candidate application. A composed-page journey preserves the same reply field, draft and focus through Threads/Asks, narrow and wide layouts, and dark mode. Graph negative controls reject renamed policy modules, missing dependencies, computed runtime imports, and URL-encoded or query-suffixed forbidden imports. Independent reviews cover the ownership cutover, integration and review corrections.
Final CI passed on GitHub’s merge result against main, including 992 tests (6 skipped), Worker type checking and 60 tests, and the full Worker activation, browser startup and cross-tab verifier. Against its matching main baseline, each profiled page transfers one fewer JavaScript request and about 16 KiB more JavaScript before presentation. Elapsed startup time is diagnostic because it varies with runner conditions.