Skip to content

Commit 47f1c35

Browse files
committed
Split served browser projection by scope
Move the shared wire serializer, conversation projection, and document projection into direct owners. Keep browser.py as the requested-view assembler and update its tests and ownership map.
1 parent 7fc6e1d commit 47f1c35

6 files changed

Lines changed: 289 additions & 277 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@ Within `registry/`, `contract` owns shared schema helpers and layer readings,
8585
page lookup, and `reactions` owns reaction descriptions. Import the owner
8686
directly; the package initializer is only a marker.
8787

88-
Within `served_state/`, `browser` owns the document-and-log projection shipped
89-
to the runtime, `page` composes the complete served response, and `reading`
90-
names filesystem changes for the news stream. Import the owner directly; the
91-
package initializer is only a marker.
88+
Within `served_state/`, `wire` serializes one declared fold, `conversation` and
89+
`document` own their scoped browser readings, `browser` assembles the requested
90+
views, `page` composes the complete served response, and `reading` names
91+
filesystem changes for the news stream. Import the owner directly; the package
92+
initializer is only a marker.
9293

9394
Within `render_gate/`, `models` owns the values passed between phases, `scheme`
9495
owns one browser/color lifecycle, `readings` owns raw probe results, `reporting`

‎plugins/leaf/skills/leaf/scripts/leaf/served_state/browser.py‎

Lines changed: 6 additions & 270 deletions
Original file line numberDiff line numberDiff line change
@@ -1,279 +1,15 @@
1-
"""Browser projections from page documents and the standing event log."""
1+
"""Assemble browser state from requested documents and the standing log."""
22

33
from pathlib import Path
44

5-
from ..asks import page_ask_projection, thread_ask_projection
6-
from ..events import (
7-
action_rests_on,
8-
action_retracted,
9-
awaits_agent,
10-
bare_reaction,
11-
build_threads,
12-
is_reaction,
13-
retractions,
14-
seat_root,
15-
seats_with_agent,
16-
spoken_turns,
17-
taken_back,
18-
undo_error,
19-
)
5+
from ..events import build_threads
206
from ..files import list_revisions, revision_path
21-
from ..passages import enclosing_of, page_passages, spoken
22-
from ..projection import (
23-
StateProjection,
24-
canonical_updates,
25-
decisions,
26-
folded_facet,
27-
page_projection,
28-
state_projection,
29-
)
7+
from ..passages import enclosing_of
8+
from ..projection import canonical_updates, page_projection
309
from ..registry.contract import RegistryError
3110
from ..registry.storage import load_registry
32-
from ..thread_context import thread_roots, thread_structure
33-
34-
35-
def _browser_projection(
36-
projection: StateProjection,
37-
*,
38-
scope: str,
39-
within: dict,
40-
floors: dict,
41-
) -> dict:
42-
"""Serialize one declared projection without making the wire its authority."""
43-
entries = []
44-
for coordinate, (event, spec) in projection.classified.values():
45-
restated = []
46-
if event["kind"] == "action":
47-
restated = [
48-
identity
49-
for identity in action_rests_on(event, within)
50-
if floors.get(identity, 0) > event["revision"]
51-
]
52-
entries.append(
53-
{
54-
"event": event,
55-
"coordinate": list(coordinate),
56-
"value": folded_facet(event, spec) if spec.get("record") else None,
57-
"scope": scope,
58-
"restated": restated,
59-
}
60-
)
61-
return {
62-
"entries": entries,
63-
"actions": [event["id"] for event, _spec in projection.actions.values()],
64-
"reports": [
65-
event["id"]
66-
for standing in projection.reports.values()
67-
for event, _spec in standing
68-
],
69-
"desired": [event["id"] for event, _spec in projection.desired.values()],
70-
}
71-
72-
73-
def _thread_projection(events: list, registry: dict):
74-
structure = thread_structure(events)
75-
roots = thread_roots(events)
76-
byid, spk = {}, {}
77-
for event in events:
78-
if markup := event.get("markup"):
79-
fragment = structure.fragments[event["id"]]
80-
byid.update(fragment.by_id)
81-
spk.update(spoken(markup, registry))
82-
projection = state_projection(events, byid, spk, registry, None, floors={})
83-
return projection, byid, spk, roots, structure
84-
85-
86-
def _thread_awaits_reader(
87-
thread: dict,
88-
registry: dict,
89-
awaiting: dict[str, bool],
90-
structure,
91-
) -> bool:
92-
if thread["resolved"]:
93-
return False
94-
turns = spoken_turns(thread)
95-
if not turns or turns[-1]["author"] != "claude":
96-
return False
97-
last = turns[-1]
98-
if last["kind"] == "reply":
99-
fragment = structure.fragments.get(last["id"])
100-
asks = [
101-
rec["attrs"].get("id")
102-
for rec in (fragment.lf_elements if fragment else [])
103-
if (registry.get(rec["tag"]) or {}).get("x-awaits") is not None
104-
]
105-
structural = (
106-
any(awaiting.get(identity, False) for identity in asks) if asks else None
107-
)
108-
if structural is False or (structural is None and not last.get("awaits")):
109-
return False
110-
tokens = registry.get("$reactions", {}).get("tokens", {})
111-
return not any(
112-
is_reaction(message)
113-
and message["author"] == "user"
114-
and message.get("parent") == last["id"]
115-
and (tokens.get(message["token"]) or {}).get("settles")
116-
for message in thread["msgs"]
117-
)
118-
119-
120-
def _browser_conversation(
121-
events: list, registry: dict, threads: dict
122-
) -> tuple[dict, StateProjection]:
123-
settled = {identity for identity, thread in threads.items() if thread["resolved"]}
124-
prepared = _thread_projection(events, registry)
125-
projection, _byid, _spk, _roots, structure = prepared
126-
asks, awaiting = thread_ask_projection(events, registry, settled, prepared=prepared)
127-
rendered_threads = [
128-
{
129-
**thread,
130-
"awaits_agent": awaits_agent(thread),
131-
"awaits_reader": _thread_awaits_reader(
132-
thread, registry, awaiting, structure
133-
),
134-
"bare_reaction": bare_reaction(thread),
135-
"seat": seat_root(thread),
136-
}
137-
for thread in threads.values()
138-
]
139-
return (
140-
{
141-
"projection": _browser_projection(
142-
projection, scope="conversation", within={}, floors={}
143-
),
144-
"asks": {"reader": asks, "unanswered": asks, "awaiting": awaiting},
145-
"threads": rendered_threads,
146-
"done": [event for event in events if event["kind"] == "done"],
147-
},
148-
projection,
149-
)
150-
151-
152-
def _browser_document(
153-
html: str,
154-
events: list,
155-
registry: dict,
156-
revision: int,
157-
threads: dict,
158-
*,
159-
prepared: tuple | None = None,
160-
) -> tuple[dict, StateProjection, dict, dict]:
161-
projection, parser, spk = prepared or page_projection(
162-
html, events, registry, revision
163-
)
164-
passages = page_passages(html, registry, decisions(projection.actions, registry))
165-
dropped = set(passages.retired) | set(passages.gone)
166-
reader_asks, reader_awaiting = page_ask_projection(
167-
parser,
168-
projection,
169-
parser.by_id,
170-
spk,
171-
registry,
172-
dropped,
173-
seats_with_agent(threads),
174-
)
175-
unanswered_asks, unanswered_awaiting = page_ask_projection(
176-
parser,
177-
projection,
178-
parser.by_id,
179-
spk,
180-
registry,
181-
dropped,
182-
set(),
183-
)
184-
within = enclosing_of(spk)
185-
floors = retractions(events, revision)
186-
return (
187-
{
188-
"revision": revision,
189-
"projection": _browser_projection(
190-
projection,
191-
scope="document",
192-
within=within,
193-
floors=floors,
194-
),
195-
"asks": {
196-
"reader": reader_asks,
197-
"unanswered": unanswered_asks,
198-
"awaiting": reader_awaiting,
199-
"unanswered_awaiting": unanswered_awaiting,
200-
},
201-
},
202-
projection,
203-
within,
204-
floors,
205-
)
206-
207-
208-
def _restores_desired(
209-
event: dict,
210-
coordinate: tuple,
211-
projection: StateProjection,
212-
withdrawn: set,
213-
within: dict,
214-
floors: dict,
215-
) -> bool:
216-
"""Whether withdrawing one action exposes another durable value there."""
217-
desired = projection.desired.get(coordinate)
218-
if desired and desired[0]["id"] != event["id"]:
219-
return True
220-
if projection.reports.get(coordinate):
221-
return True
222-
return any(
223-
candidate_coordinate == coordinate
224-
and candidate["kind"] == "action"
225-
and candidate["id"] != event["id"]
226-
and candidate["id"] not in withdrawn
227-
and not action_retracted(candidate, floors, within)
228-
for candidate_coordinate, (candidate, _spec) in projection.classified.values()
229-
)
230-
231-
232-
def _browser_undo_candidates(
233-
events: list,
234-
within: dict,
235-
document_within: dict,
236-
document_floors: dict,
237-
document_projection: StateProjection,
238-
conversation_projection: StateProjection,
239-
) -> list[dict]:
240-
classified = {
241-
**document_projection.classified,
242-
**conversation_projection.classified,
243-
}
244-
candidates = []
245-
withdrawn = taken_back(events)
246-
for event in reversed(events):
247-
if (
248-
event.get("author") != "user"
249-
or event["kind"] == "undo"
250-
or event["id"] in withdrawn
251-
):
252-
continue
253-
if undo_error({"undoes": event["id"]}, events, within):
254-
continue
255-
item = {"event": event}
256-
if event["kind"] == "action" and event["id"] in classified:
257-
coordinate, _entry = classified[event["id"]]
258-
item["coordinate"] = list(coordinate)
259-
if event["id"] in document_projection.classified:
260-
projection = document_projection
261-
action_within = document_within
262-
floors = document_floors
263-
else:
264-
projection = conversation_projection
265-
action_within = {}
266-
floors = {}
267-
item["restores_desired"] = _restores_desired(
268-
event,
269-
coordinate,
270-
projection,
271-
withdrawn,
272-
action_within,
273-
floors,
274-
)
275-
candidates.append(item)
276-
return candidates
11+
from .conversation import _browser_conversation
12+
from .document import _browser_document, _browser_undo_candidates
27713

27814

27915
def browser_state(
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""Conversation-scoped browser projection."""
2+
3+
from ..asks import thread_ask_projection
4+
from ..events import (
5+
awaits_agent,
6+
bare_reaction,
7+
is_reaction,
8+
seat_root,
9+
spoken_turns,
10+
)
11+
from ..passages import spoken
12+
from ..projection import StateProjection, state_projection
13+
from ..thread_context import thread_roots, thread_structure
14+
from .wire import _browser_projection
15+
16+
17+
def _thread_projection(events: list, registry: dict):
18+
structure = thread_structure(events)
19+
roots = thread_roots(events)
20+
byid, spk = {}, {}
21+
for event in events:
22+
if markup := event.get("markup"):
23+
fragment = structure.fragments[event["id"]]
24+
byid.update(fragment.by_id)
25+
spk.update(spoken(markup, registry))
26+
projection = state_projection(events, byid, spk, registry, None, floors={})
27+
return projection, byid, spk, roots, structure
28+
29+
30+
def _thread_awaits_reader(
31+
thread: dict,
32+
registry: dict,
33+
awaiting: dict[str, bool],
34+
structure,
35+
) -> bool:
36+
if thread["resolved"]:
37+
return False
38+
turns = spoken_turns(thread)
39+
if not turns or turns[-1]["author"] != "claude":
40+
return False
41+
last = turns[-1]
42+
if last["kind"] == "reply":
43+
fragment = structure.fragments.get(last["id"])
44+
asks = [
45+
rec["attrs"].get("id")
46+
for rec in (fragment.lf_elements if fragment else [])
47+
if (registry.get(rec["tag"]) or {}).get("x-awaits") is not None
48+
]
49+
structural = (
50+
any(awaiting.get(identity, False) for identity in asks) if asks else None
51+
)
52+
if structural is False or (structural is None and not last.get("awaits")):
53+
return False
54+
tokens = registry.get("$reactions", {}).get("tokens", {})
55+
return not any(
56+
is_reaction(message)
57+
and message["author"] == "user"
58+
and message.get("parent") == last["id"]
59+
and (tokens.get(message["token"]) or {}).get("settles")
60+
for message in thread["msgs"]
61+
)
62+
63+
64+
def _browser_conversation(
65+
events: list, registry: dict, threads: dict
66+
) -> tuple[dict, StateProjection]:
67+
settled = {identity for identity, thread in threads.items() if thread["resolved"]}
68+
prepared = _thread_projection(events, registry)
69+
projection, _byid, _spk, _roots, structure = prepared
70+
asks, awaiting = thread_ask_projection(events, registry, settled, prepared=prepared)
71+
rendered_threads = [
72+
{
73+
**thread,
74+
"awaits_agent": awaits_agent(thread),
75+
"awaits_reader": _thread_awaits_reader(
76+
thread, registry, awaiting, structure
77+
),
78+
"bare_reaction": bare_reaction(thread),
79+
"seat": seat_root(thread),
80+
}
81+
for thread in threads.values()
82+
]
83+
return (
84+
{
85+
"projection": _browser_projection(
86+
projection, scope="conversation", within={}, floors={}
87+
),
88+
"asks": {"reader": asks, "unanswered": asks, "awaiting": awaiting},
89+
"threads": rendered_threads,
90+
"done": [event for event in events if event["kind"] == "done"],
91+
},
92+
projection,
93+
)

0 commit comments

Comments
 (0)