@@ -64,6 +64,7 @@ class FoundUpBrainView:
6464 verified_outcomes : tuple [dict [str , Any ], ...]
6565 learning_candidates : tuple [dict [str , Any ], ...] = ()
6666 roadmap_signals : tuple [dict [str , Any ], ...] = ()
67+ assembly_receipt : dict [str , Any ] | None = None
6768 invariants : dict [str , bool ] | None = None
6869
6970 def to_dict (self ) -> dict [str , Any ]:
@@ -100,6 +101,9 @@ def assemble_foundup_brain_current_state(
100101 roadmap_state : Mapping [str , Any ] | None = None ,
101102 verified_outcomes : Sequence [Mapping [str , Any ]] = (),
102103 now_iso : str | None = None ,
104+ resident_mode : bool = True ,
105+ legacy_single_foundup_compatibility : bool = False ,
106+ policy_foundup_scope : Sequence [str ] | None = None ,
103107) -> FoundUpBrainAssemblyResult :
104108 """Assemble one FoundUp's current cognition from existing receipts."""
105109
@@ -129,28 +133,77 @@ def assemble_foundup_brain_current_state(
129133 identity_clean = _normalize_identity (identity )
130134 if not identity_clean .get ("name" ):
131135 reasons .append ("missing_identity_name" )
132- identity_foundup_id = str (identity_clean .get ("foundup_id" , normalized_foundup_id )).strip ()
133- if identity_foundup_id and identity_foundup_id != normalized_foundup_id :
136+ identity_foundup_id = str (identity_clean .get ("foundup_id" , "" )).strip ()
137+ if not identity_foundup_id :
138+ if resident_mode and not legacy_single_foundup_compatibility :
139+ reasons .append ("identity_missing_foundup_id" )
140+ elif legacy_single_foundup_compatibility :
141+ identity_clean ["scope_origin" ] = "legacy_single_foundup_compatibility"
142+ elif identity_foundup_id != normalized_foundup_id :
134143 reasons .append ("identity_foundup_id_mismatch" )
135144 identity_clean ["foundup_id" ] = normalized_foundup_id
136145
137- roadmap_clean = _normalize_roadmap_state (roadmap_state , normalized_foundup_id , reasons )
146+ policy_scope = tuple (str (item ).strip () for item in (policy_foundup_scope or ()) if str (item ).strip ())
147+ if resident_mode :
148+ if policy_scope != (normalized_foundup_id ,):
149+ reasons .append ("policy_foundup_scope_mismatch" )
150+
151+ roadmap_clean = _normalize_roadmap_state (
152+ roadmap_state ,
153+ normalized_foundup_id ,
154+ reasons ,
155+ resident_mode = resident_mode ,
156+ legacy_single_foundup_compatibility = legacy_single_foundup_compatibility ,
157+ )
138158 outcomes_clean = tuple (
139- _normalize_verified_outcome (outcome , normalized_foundup_id , reasons )
159+ _normalize_verified_outcome (
160+ outcome ,
161+ normalized_foundup_id ,
162+ reasons ,
163+ resident_mode = resident_mode ,
164+ legacy_single_foundup_compatibility = legacy_single_foundup_compatibility ,
165+ )
140166 for outcome in verified_outcomes
141167 )
142- active_work = _scope_work_records (
168+ active_work , excluded_active_work = _scope_work_records (
143169 snapshot .work_state .get ("worker_claims" , ()),
144170 normalized_foundup_id ,
145171 "worker_claim" ,
146172 reasons ,
173+ resident_mode = resident_mode ,
174+ legacy_single_foundup_compatibility = legacy_single_foundup_compatibility ,
147175 )
148- queued_work = _scope_work_records (
176+ queued_work , excluded_queued_work = _scope_work_records (
149177 snapshot .work_state .get ("wre_queue_items" , ()),
150178 normalized_foundup_id ,
151179 "queue_item" ,
152180 reasons ,
181+ resident_mode = resident_mode ,
182+ legacy_single_foundup_compatibility = legacy_single_foundup_compatibility ,
153183 )
184+ excluded_records = excluded_active_work + excluded_queued_work
185+ excluded_record_digest = _digest (excluded_records )
186+ assembly_receipt_payload = {
187+ "schema_version" : "foundup_memex_assembly_receipt.v1" ,
188+ "foundup_id" : normalized_foundup_id ,
189+ "snapshot_id" : snapshot .snapshot_receipt_id ,
190+ "snapshot_content_digest" : snapshot .snapshot_content_digest ,
191+ "resident_mode" : resident_mode is True ,
192+ "legacy_single_foundup_compatibility" : legacy_single_foundup_compatibility is True ,
193+ "policy_foundup_scope" : policy_scope ,
194+ "included_worker_claims" : len (active_work ),
195+ "included_queue_items" : len (queued_work ),
196+ "excluded_record_count" : len (excluded_records ),
197+ "excluded_record_digest" : excluded_record_digest ,
198+ "no_brain_write_performed" : True ,
199+ "no_breadcrumb_write_performed" : True ,
200+ "no_holoindex_mutation_performed" : True ,
201+ "no_queue_mutation_performed" : True ,
202+ }
203+ assembly_receipt = {
204+ ** assembly_receipt_payload ,
205+ "receipt_id" : _digest (assembly_receipt_payload ),
206+ }
154207
155208 candidate_payload = {
156209 "identity" : identity_clean ,
@@ -195,6 +248,7 @@ def assemble_foundup_brain_current_state(
195248 "verified_outcomes" : outcomes_clean ,
196249 "learning_candidates" : (),
197250 "roadmap_signals" : (),
251+ "assembly_receipt" : assembly_receipt ,
198252 }
199253 view_id = _digest (content )
200254 view = FoundUpBrainView (
@@ -218,6 +272,7 @@ def assemble_foundup_brain_current_state(
218272 "no_worker_spawn" : True ,
219273 "no_repo_mutation" : True ,
220274 },
275+ assembly_receipt = assembly_receipt ,
221276 )
222277 return FoundUpBrainAssemblyResult (
223278 accepted = True ,
@@ -236,10 +291,19 @@ def _normalize_roadmap_state(
236291 roadmap_state : Mapping [str , Any ] | None ,
237292 foundup_id : str ,
238293 reasons : list [str ],
294+ * ,
295+ resident_mode : bool ,
296+ legacy_single_foundup_compatibility : bool ,
239297) -> dict [str , Any ]:
240298 data = dict (roadmap_state or {})
241- scoped_id = str (data .get ("foundup_id" , foundup_id )).strip ()
242- if scoped_id and scoped_id != foundup_id :
299+ scoped_id = str (data .get ("foundup_id" , "" )).strip ()
300+ scope_origin = ""
301+ if not scoped_id :
302+ if resident_mode and not legacy_single_foundup_compatibility :
303+ reasons .append ("roadmap_missing_foundup_id" )
304+ elif legacy_single_foundup_compatibility :
305+ scope_origin = "legacy_single_foundup_compatibility"
306+ elif scoped_id != foundup_id :
243307 reasons .append ("roadmap_foundup_id_mismatch" )
244308 roadmap_id = str (data .get ("roadmap_id" , "" )).strip ()
245309 version = str (data .get ("version" , "" )).strip ()
@@ -257,16 +321,26 @@ def _normalize_roadmap_state(
257321 "content_digest" : content_digest ,
258322 "active_item_ids" : tuple (str (value ) for value in data .get ("active_item_ids" , ())),
259323 "blocked_item_ids" : tuple (str (value ) for value in data .get ("blocked_item_ids" , ())),
324+ "scope_origin" : scope_origin ,
260325 }
261326
262327
263328def _normalize_verified_outcome (
264329 outcome : Mapping [str , Any ],
265330 foundup_id : str ,
266331 reasons : list [str ],
332+ * ,
333+ resident_mode : bool ,
334+ legacy_single_foundup_compatibility : bool ,
267335) -> dict [str , Any ]:
268- scoped_id = str (outcome .get ("foundup_id" , foundup_id )).strip ()
269- if scoped_id and scoped_id != foundup_id :
336+ scoped_id = str (outcome .get ("foundup_id" , "" )).strip ()
337+ scope_origin = ""
338+ if not scoped_id :
339+ if resident_mode and not legacy_single_foundup_compatibility :
340+ reasons .append ("verified_outcome_missing_foundup_id" )
341+ elif legacy_single_foundup_compatibility :
342+ scope_origin = "legacy_single_foundup_compatibility"
343+ elif scoped_id != foundup_id :
270344 reasons .append ("verified_outcome_foundup_id_mismatch" )
271345 accepted = bool (outcome .get ("accepted" , False ))
272346 held_out_passed = bool (outcome .get ("held_out_passed" , False ))
@@ -287,6 +361,7 @@ def _normalize_verified_outcome(
287361 ** required_ids ,
288362 "accepted" : accepted ,
289363 "held_out_passed" : held_out_passed ,
364+ "scope_origin" : scope_origin ,
290365 }
291366
292367
@@ -295,19 +370,67 @@ def _scope_work_records(
295370 foundup_id : str ,
296371 record_kind : str ,
297372 reasons : list [str ],
298- ) -> tuple [dict [str , Any ], ...]:
373+ * ,
374+ resident_mode : bool ,
375+ legacy_single_foundup_compatibility : bool ,
376+ ) -> tuple [tuple [dict [str , Any ], ...], tuple [dict [str , Any ], ...]]:
299377 scoped : list [dict [str , Any ]] = []
300- for record in records :
378+ excluded : list [dict [str , Any ]] = []
379+ for index , record in enumerate (records ):
301380 data = dict (record )
302381 record_foundup_id = str (data .get ("foundup_id" , "" )).strip ()
303382 if record_foundup_id and record_foundup_id != foundup_id :
304- reasons .append (f"{ record_kind } _foundup_id_mismatch" )
383+ excluded .append (
384+ _excluded_record_summary (
385+ record_kind = record_kind ,
386+ record = data ,
387+ index = index ,
388+ foundup_id = record_foundup_id ,
389+ reason = f"{ record_kind } _foundup_id_mismatch" ,
390+ )
391+ )
305392 continue
306393 if not record_foundup_id :
394+ if resident_mode and not legacy_single_foundup_compatibility :
395+ reasons .append (f"{ record_kind } _missing_foundup_id" )
396+ excluded .append (
397+ _excluded_record_summary (
398+ record_kind = record_kind ,
399+ record = data ,
400+ index = index ,
401+ foundup_id = "" ,
402+ reason = f"{ record_kind } _missing_foundup_id" ,
403+ )
404+ )
405+ continue
307406 data ["foundup_id" ] = foundup_id
308- data ["scope_origin" ] = "legacy_single_foundup_poc "
407+ data ["scope_origin" ] = "legacy_single_foundup_compatibility "
309408 scoped .append (data )
310- return tuple (scoped )
409+ return tuple (scoped ), tuple (excluded )
410+
411+
412+ def _excluded_record_summary (
413+ * ,
414+ record_kind : str ,
415+ record : Mapping [str , Any ],
416+ index : int ,
417+ foundup_id : str ,
418+ reason : str ,
419+ ) -> dict [str , Any ]:
420+ identifier = (
421+ record .get ("claim_id" )
422+ or record .get ("queue_item_id" )
423+ or record .get ("work_order_id" )
424+ or record .get ("slice_name" )
425+ or record .get ("selected_slice" )
426+ or f"{ record_kind } :{ index } "
427+ )
428+ return {
429+ "record_kind" : record_kind ,
430+ "record_ref" : str (identifier ),
431+ "foundup_id" : str (foundup_id ),
432+ "reason" : reason ,
433+ }
311434
312435
313436def _snapshot_expired (valid_until : str , now_iso : str ) -> bool :
0 commit comments