|
1 | | -"""Browser projections from page documents and the standing event log.""" |
| 1 | +"""Assemble browser state from requested documents and the standing log.""" |
2 | 2 |
|
3 | 3 | from pathlib import Path |
4 | 4 |
|
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 |
20 | 6 | 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 |
30 | 9 | from ..registry.contract import RegistryError |
31 | 10 | 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 |
277 | 13 |
|
278 | 14 |
|
279 | 15 | def browser_state( |
|
0 commit comments