Skip to content

Commit e4be680

Browse files
Make Python the canonical browser projector (#87)
Leaf previously reconstructed durable page state in both Python and browser JavaScript, which let winner, retraction, ask, conversation, update, and undo rules drift. This change makes Python produce the browser-ready projection for an exact document revision and log sequence. JavaScript consumes that projection, maps it to the DOM, and overlays only unresolved local gestures. `/api/state` returns the shown and active revision views, accepted event responses retain the revision shown by the tab, and `/api/view` projects a historical comparison at the sequence the DOM has applied. The static documentation host keeps a small in-memory emulator because it has no Python process; that path resets on reload and is not used by served Leaf pages. Its shim now answers `/api/view` as well as the other runtime routes. Local evidence on the current head: `pre-commit run --all-files`; 225 startup, server, undo, reaction, conversation, and static-site cases. Before the final `main` refreshes, the full branch passed 661 tests with 1 skip. > _This was written by Codex on behalf of max-sixty_ --------- Co-authored-by: leaf-agent <[email protected]>
1 parent 8eb3a8f commit e4be680

31 files changed

Lines changed: 1567 additions & 1166 deletions

‎CLAUDE.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ the decision rests on. Use `restated` when a rewrite invalidates one. An `undo`
5555
event names the gesture withdrawn; it never deletes or invents a counter-event.
5656

5757
Actions and reports share the registry-declared coordinate of owner widget,
58-
fold unit, and facet. Python and JavaScript derive winners, retractions, and
59-
settlement from the same declarations. Page-widget state is bounded by document
60-
version; widgets frozen into thread markup use the conversation window.
58+
fold unit, and facet. Python derives winners, retractions, settlement, asks,
59+
threads, and updates in one transaction-consistent browser view. JavaScript
60+
resolves that view onto live DOM nodes and overlays only unresolved local
61+
gestures. Page-widget state is bounded by document version; widgets frozen into
62+
thread markup use the conversation window.
6163

6264
The page directory is the durable record and deployment unit. `index.html` is
6365
mutable author source; revisions and stamped versions are immutable. The event

‎docs/session.js‎

Lines changed: 292 additions & 49 deletions
Large diffs are not rendered by default.

‎plugins/leaf/skills/leaf/CLAUDE.md‎

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ replacement and supplies the page-block reading used to start directional walks;
9292
`runtime/geometry.js` owns the shared readings of visible boxes and clipping;
9393
`runtime/navigation.js` owns reader travel and scroller selection;
9494
`runtime/anchors.js` owns anchor resolution, paint, and anchor-specific travel;
95-
`runtime/conversation/model.js` owns the thread fold;
95+
`runtime/conversation/model.js` adapts server-projected threads to browser callers;
9696
`runtime/conversation/messages.js` owns message rendering;
9797
`runtime/conversation/replies.js` owns reply drafts, mirrored send state, and delivery;
9898
`runtime/conversation/inline.js` owns conversation seats rendered into the page;
@@ -103,7 +103,8 @@ replacement and supplies the page-block reading used to start directional walks;
103103
`runtime/conversation/reconcile.js` composes panel reconciliation;
104104
`runtime/projection/authored.js` owns captured authored state and restore
105105
statements; `runtime/projection/data.js` owns keyed runtime-data DOM
106-
reconciliation; `runtime/projection/fold.js` owns canonical action and report state;
106+
reconciliation; `runtime/projection/fold.js` adapts canonical action and report state
107+
to live DOM nodes and the local outbox;
107108
`runtime/projection.js` owns projection reconciliation and undo. The entry module
108109
composes their mutually dependent callbacks.
109110

@@ -293,11 +294,11 @@ accepted event because its rendering failed.
293294

294295
A recorded action may be optimistic because its gesture has already changed the
295296
DOM. Drag and edit are examples. `stageOutboxAction` gives that local value the
296-
same semantic coordinate as logged state and commits it on the exact widget and
297-
unit nodes that carry it. `stateProjection` overlays all surviving recorded
298-
outbox actions after logged winners in `outboxOrder`. Until a complete read
299-
accounts for an attempt, its local winner outranks any older log winner on the
300-
same coordinate.
297+
same semantic coordinate as the server view and commits it on the exact widget
298+
and unit nodes that carry it. The browser projection adapter overlays all
299+
surviving recorded outbox actions after authoritative winners in `outboxOrder`.
300+
Until a complete read accounts for an attempt, its local winner outranks any
301+
older log winner on the same coordinate.
301302

302303
A press whose result has not changed the DOM waits for the log. Recordless
303304
settlements and completion presses do not enter the optimistic overlay. The
@@ -322,11 +323,11 @@ represents the DOM. Delivery may continue while that correction waits for a
322323
live drag or editor to finish.
323324

324325
`accountOutbox` runs only after `receiveState` has installed and rendered a
325-
complete state. It links accepted events to entries, resolves readers waiting on
326+
complete state. It links receipt events to entries, resolves readers waiting on
326327
those events, removes non-action entries whose delivery is complete, and calls
327-
`releaseProjectedOutbox` for actions. Never remove an action merely because a
328-
POST returned 200 or because an attempt appears in an event array that failed
329-
partway through rendering.
328+
`releaseProjectedOutbox` for actions. Never remove an action merely because a POST
329+
returned 200 or because an attempt appears in a receipt list that failed partway
330+
through rendering.
330331

331332
`unaccountedGesture` is true while undo is in flight, the outbox is nonempty, or
332333
a widget is visibly dragging. Navigation and undo both consult it. Navigating
@@ -358,7 +359,7 @@ therefore survives its response.
358359
The DOM is a projection of three ordered inputs:
359360

360361
1. the authored state captured from this version;
361-
2. standing actions and reports in the authoritative log window;
362+
2. standing actions and reports in the server's transaction-consistent browser view;
362363
3. surviving optimistic recorded actions in the outbox.
363364

364365
The semantic coordinate is
@@ -377,16 +378,24 @@ projections. `x-awaits.answers` says which actions actually close the request;
377378
orthogonal actions do not, and neither does a conversation standing in the widget's
378379
declared `x-conversation` seat — that takes the request off the reader's list without
379380
answering it, which is why this gate reads the projection with no seats in
380-
it. `x-awaits.rollup` derives a nested request from direct
381-
interventions and child roll-ups, using the same reducer in the browser and file
382-
projection.
383-
384-
`stateProjection(upto)` is the pure derived view. It classifies every action and
385-
report, applies version and retraction windows, drops withdrawn actions and
386-
answered reports, folds the last action for each coordinate, retains report
387-
winners, overlays unresolved local records, and gives a reader action precedence
388-
over a provisional report on the same coordinate. Winners on independent
389-
coordinates compose in event order through `compareProjected`.
381+
it. `x-awaits.rollup` derives a nested request from direct interventions and child
382+
roll-ups in Python; the browser receives the resulting ids and awaiting values.
383+
384+
Python's `state_projection` is the durable derived view. Under the same page
385+
transaction as `/api/state`, `browser_state` serializes its classified events and
386+
winners, asks, conversations, updates, undo candidates, receipts, and coverage at
387+
one `through_seq`. A normal response projects the revision the tab shows and the
388+
active revision it may install next. A version comparison requests its older base
389+
from `/api/view` at the exact `through_seq` already applied to the live DOM, so every
390+
view used together has the same sequence basis without every state read parsing all
391+
historical revisions. Page coordinates use that revision's document window;
392+
conversation coordinates use the unbounded frozen-markup window.
393+
394+
The browser's `stateProjection` is a DOM adapter. It resolves those declared
395+
coordinates back to current widget modules and overlays unresolved local records.
396+
It does not derive retractions, settlements, thread structure, asks, updates, or
397+
undo eligibility from raw events. Winners on independent coordinates still
398+
compose in event order through `compareProjected`.
390399

391400
The two durable channels share the coordinate model but retain their meaning:
392401

@@ -400,8 +409,8 @@ The two durable channels share the coordinate model but retain their meaning:
400409
`stateProjection` is uncached because registry declarations resolve through the
401410
live DOM. Thread construction and recordless restoration can replace node
402411
identities. Its result has four views: `actions`, `reports`, `classified`, and
403-
`desired`. Add a consumer to one of these views instead of building another fold
404-
over raw `events`.
412+
`desired`. Add a browser consumer to one of these views or extend the Python wire
413+
view instead of building another fold over raw `events`.
405414

406415
`committedProjection` is not a second state authority. It is a checkpoint of
407416
what node identities and semantic winner the DOM currently represents. Each
@@ -412,11 +421,11 @@ its authored baseline stands.
412421

413422
`projectionCommitted` compares the desired coordinate with that checkpoint.
414423
Terminal events count as committed because this version has no applicable state
415-
to paint. `projectionCoverage` converts coordinate commits back to event
416-
coverage for `data-lf-applied`: superseded actions and answered reports are
417-
covered when the coordinate that represents them is committed, and an undo is
418-
covered when its target's coordinate has moved to the prior winner or authored
419-
baseline.
424+
to paint. The server supplies coverage records and `projectionCoverage` checks
425+
their coordinates against DOM commits for `data-lf-applied`: superseded actions
426+
and answered reports are covered when the coordinate that represents them is
427+
committed, and an undo is covered when its target's coordinate has moved to the
428+
prior winner or authored baseline.
420429

421430
### Reconciliation
422431

@@ -524,9 +533,9 @@ documents written after this version. A widget instantiated inside frozen thread
524533
markup is in chrome and reads the whole action sequence because the conversation,
525534
not a page version, owns it.
526535

527-
Threads also read the whole log. `retractionFloors(Infinity)` keeps a
528-
conversation current on a pinned page even when the document projection remains
529-
historical. Registry-declared `x-conversation` seats show an exact-section
536+
The server projects threads from the whole log, so a conversation stays current
537+
on a pinned page even when the document projection remains historical.
538+
Registry-declared `x-conversation` seats show an exact-section
530539
textual view while the owner exists in the current document; the Comments panel
531540
keeps the complete thread and its interactive replies. A root declared with
532541
`response: {kind: version, verb: <answer>}` keeps that exact-section view
@@ -535,8 +544,9 @@ response. Dropping the owner drops only the inline seat.
535544

536545
`restated` and answered-report relations persist through version notes. The note
537546
records the version floor for each affected id or report event; silence in a
538-
later version does not revive retracted state. `retractedIds` uses containment,
539-
not a global id lookup, when deciding which detailed parts an action rests on.
547+
later version does not revive retracted state. Python's projection uses
548+
containment, not a global id lookup, when deciding which detailed parts an action
549+
rests on.
540550

541551
### Event sequences for modules
542552

@@ -578,12 +588,12 @@ field, verb, or widget name. Claims use their required detail as `detail.text`
578588
and `text`. The state boundary performs this normalization once, before
579589
downstream code sees private status storage.
580590

581-
`sequence` is the action traversal. It applies widget, optional verb, version
582-
window, and liveness in one place, then returns structured clones so modules
583-
cannot mutate the private event list. `watchActions` and `watchUpdates` subscribe
584-
the two public readings to `lf-actions` and invoke the callback immediately. The
585-
same rendering function therefore handles a module connected before the first
586-
state and one constructed by a later thread reconcile.
591+
`actionSequence` traverses the classified events in the installed server view,
592+
then returns structured clones so modules cannot mutate the reading.
593+
`updateSequence` filters the server-normalized update feed. `watchActions` and
594+
`watchUpdates` subscribe the two public readings to `lf-actions` and invoke the
595+
callback immediately. The same rendering function therefore handles a module
596+
connected before the first state and one constructed by a later thread reconcile.
587597

588598
`lf-actions` fires after a complete state has reconciled, including a read whose
589599
event list did not grow and the heartbeat's re-application of the state the page
@@ -602,7 +612,7 @@ assertion is as old as its revision.
602612
`actionStands` answers whether one accepted action is still the reader's winner
603613
for its semantic coordinate. It treats a newly accepted event as standing when
604614
the tab has not yet installed an event list containing its id, then asks
605-
`stateProjection` once authoritative history contains it. Modules use this
615+
the installed projection once an authoritative receipt contains it. Modules use this
606616
after a send whose visible choreography depends on whether the accepted action
607617
survived later events.
608618

‎plugins/leaf/skills/leaf/assets/leaf.js‎

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,7 @@ mirrorDraft(generalInput, syncGeneral, "general");
15811581

15821582
let approving = false;
15831583
function paintApproval() {
1584-
const approved = runtime.events.some(
1584+
const approved = (runtime.browser?.conversation?.done ?? []).some(
15851585
(e) =>
15861586
e.kind === "done" &&
15871587
e.revision === runtime.currentRevision &&
@@ -1968,27 +1968,16 @@ function allButTheReference(binding) {
19681968
// page stands down under them — and each declares what it keeps, which is how the
19691969
// reference's own key goes on working while every other one is suspended.
19701970

1971-
const { askEntry, isAwaiting, projectedParent, threadMarkupAwaiting, unansweredAsks } =
1972-
createAskModel({
1973-
authoredParentOf: (node) => authoredParents.get(node),
1974-
awaitsAgent,
1975-
buildThreads,
1976-
closestAcross: (...args) => closestAcross(...args),
1977-
elementById: (...args) => elementById(...args),
1978-
inChrome: (node) => inChrome(node),
1979-
matchesProjectedWhen: (...args) => matchesProjectedWhen(...args),
1980-
matchesWhen,
1981-
pagePresented,
1982-
projectedFacet: (...args) => projectedFacet(...args),
1983-
quoted,
1984-
registry,
1985-
runtime,
1986-
seatRoot,
1987-
settledAway: (...args) => settledAway(...args),
1988-
stateCoordinate: (...args) => stateCoordinate(...args),
1989-
stateProjection: (...args) => stateProjection(...args),
1990-
tagsDeclaring,
1991-
});
1971+
const { askEntry, isAwaiting, projectedParent, unansweredAsks } = createAskModel({
1972+
authoredParentOf: (node) => authoredParents.get(node),
1973+
closestAcross: (...args) => closestAcross(...args),
1974+
elementById: (...args) => elementById(...args),
1975+
pagePresented,
1976+
registry,
1977+
runtime,
1978+
stateProjection: (...args) => stateProjection(...args),
1979+
tagsDeclaring,
1980+
});
19921981

19931982
const {
19941983
ASK_CONTROL,
@@ -3065,12 +3054,12 @@ const {
30653054
chooserLabel: () => labelOf(CHOOSER),
30663055
domFacet: (...args) => domFacet(...args),
30673056
elementById: (...args) => elementById(...args),
3068-
foldedFacet: (...args) => foldedFacet(...args),
30693057
inChrome: (...args) => inChrome(...args),
30703058
quoted,
3059+
projectionFromView: (...args) => projectionFromView(...args),
3060+
sameLayer,
30713061
showToast,
30723062
stateCoordinate: (...args) => stateCoordinate(...args),
3073-
stateProjection: (...args) => stateProjection(...args),
30743063
stateSpecs: (...args) => stateSpecs(...args),
30753064
textBlockSelector: () => TEXT_BLOCK,
30763065
versionBtn,
@@ -3368,9 +3357,6 @@ const runtimeProjection = createProjection(runtime, {
33683357
projectedParent,
33693358
quoteFrom,
33703359
reachScrollers,
3371-
// Whether a reaction still paints, off the conversation's last fold — built after this
3372-
// module, so asked lazily.
3373-
reactionStanding: (e) => conversationRuntime.reactionStanding(e),
33743360
rememberPassageParts,
33753361
removeOutbox,
33763362
renderQuiet,
@@ -3394,11 +3380,11 @@ const {
33943380
committedProjection,
33953381
coordinateProjectionCommitted,
33963382
domFacet,
3397-
foldedFacet,
33983383
markSettled,
33993384
matchesProjectedWhen,
34003385
paintPending,
34013386
projectedFacet,
3387+
projectionFromView,
34023388
projectionCommitted,
34033389
rebuild,
34043390
reconcileKnownState,
@@ -3407,13 +3393,10 @@ const {
34073393
rememberAuthoredMarkup,
34083394
resetAuthoredPage,
34093395
requirementMatches,
3410-
retractedIds,
3411-
retractionFloors,
34123396
stageOutboxAction,
34133397
stateCoordinate,
34143398
stateProjection,
34153399
stateSpecs,
3416-
takenBack,
34173400
undoable,
34183401
unitOf,
34193402
withdraw,
@@ -3495,8 +3478,6 @@ conversationRuntime = createConversation({
34953478
renderQuiet,
34963479
renderSaid,
34973480
reportPageError,
3498-
retractedIds,
3499-
retractionFloors,
35003481
runtime,
35013482
saveDraft,
35023483
scrollToElement,
@@ -3505,10 +3486,8 @@ conversationRuntime = createConversation({
35053486
sendDraft,
35063487
setPanel,
35073488
settling,
3508-
takenBack,
35093489
tellDraft,
35103490
threadsBox,
3511-
threadMarkupAwaiting,
35123491
toggleBtn,
35133492
updateSequence,
35143493
visualPartLabel,
@@ -3519,10 +3498,8 @@ conversationRuntime = createConversation({
35193498
updateRuntime = createUpdates(runtime, {
35203499
closestAcross,
35213500
coordinateProjectionCommitted,
3522-
inChrome,
35233501
projectionCommitted,
35243502
stateProjection,
3525-
threadList: () => conversationRuntime.threadList,
35263503
});
35273504

35283505
anchorRuntime = createAnchors({

0 commit comments

Comments
 (0)