interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / aienux / crates / aienos-explain / explain.om

4977 lines · 108439 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-explain/explain.om: Pure-Omega AIENOS Explanation Bridge.
3note Contract validation, bounded causal backtracking, cycle cutting, Kahn linearization,
4note canonical SHA-256 bundle digests, Cortex/branch adapters, deterministic rendering,
5note and clarification sessions.
6note Ported from aienux crates/aienos-explain/src/{contract,bundle,build,adapters,render,session,main,lib}.rs.
7note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-explain/explain.om ...
8
9note ============================================================================
10note Section 1: Contract Constants, Error Codes, Enums, and Limits
11note ============================================================================
12
13const EXP_VERSION ulong 1
14const EXP_MAX_GRAPH_NODES ulong 65536
15const EXP_MAX_PARENTS_PER_NODE ulong 1024
16
17const EXP_HARD_MAX_DEPTH ulong 256
18const EXP_HARD_MAX_NODES ulong 4096
19const EXP_DEFAULT_MAX_DEPTH ulong 32
20const EXP_DEFAULT_MAX_NODES ulong 256
21
22note Stable contract error codes (1..10; 0 = ok)
23const EXP_OK int 0
24const EXP_ERR_BAD_FORMAT int 1
25const EXP_ERR_UNSUPPORTED_VERSION int 2
26const EXP_ERR_UNKNOWN_CODE int 3
27const EXP_ERR_DUPLICATE_NODE_ID int 4
28const EXP_ERR_TARGET_NOT_FOUND int 5
29const EXP_ERR_GRAPH_TOO_LARGE int 6
30const EXP_ERR_STEP_OUT_OF_RANGE int 7
31const EXP_ERR_NOT_IN_BRANCH_SCOPE int 8
32const EXP_ERR_JOURNAL int 9
33const EXP_ERR_INVALID_LIMITS int 10
34
35note Epistemic status codes (0..6)
36const EXP_ST_UNCLASSIFIED ulong 0
37const EXP_ST_DIRECT_OBS ulong 1
38const EXP_ST_VERIFIED_FACT ulong 2
39const EXP_ST_INFERENCE ulong 3
40const EXP_ST_HYPOTHESIS ulong 4
41const EXP_ST_CONTRADICTION ulong 5
42const EXP_ST_OPERATOR_DECISION ulong 6
43
44note NodeKind codes (1..13)
45const EXP_KD_CORTEX_RECORD ulong 1
46const EXP_KD_CAP_METRIC ulong 2
47const EXP_KD_CAP_ANALYSIS ulong 3
48const EXP_KD_DIAGNOSTIC ulong 4
49const EXP_KD_HYPOTHESIS ulong 5
50const EXP_KD_PREDICTION ulong 6
51const EXP_KD_FALSIFICATION ulong 7
52const EXP_KD_EVAL_RECEIPT ulong 8
53const EXP_KD_PROMOTION_STATE ulong 9
54const EXP_KD_OPERATOR_DECISION ulong 10
55const EXP_KD_AGENT_BRANCH ulong 11
56const EXP_KD_CONCLUSION ulong 12
57const EXP_KD_ALTERNATIVE ulong 13
58
59note Relation codes (1..6)
60const EXP_REL_SUPPORTED_BY ulong 1
61const EXP_REL_EVALUATES ulong 2
62const EXP_REL_PART_OF ulong 3
63const EXP_REL_CONTRADICTS ulong 4
64const EXP_REL_ALTERNATIVE_TO ulong 5
65const EXP_REL_REFERENCES ulong 6
66
67note Narrative Role codes (0..7, matching causal order)
68const EXP_ROLE_OBSERVED ulong 0
69const EXP_ROLE_ANALYSIS ulong 1
70const EXP_ROLE_HYPOTHESIS ulong 2
71const EXP_ROLE_PREDICTION ulong 3
72const EXP_ROLE_FALSIFICATION ulong 4
73const EXP_ROLE_EVALUATION ulong 5
74const EXP_ROLE_DECISION ulong 6
75const EXP_ROLE_CONCLUSION ulong 7
76
77note UncertaintyKind codes (1..10)
78const EXP_UK_CYCLE ulong 1
79const EXP_UK_CONFLICT ulong 2
80const EXP_UK_MISSING_PARENT ulong 3
81const EXP_UK_MISSING_LINK ulong 4
82const EXP_UK_UNSUPPORTED ulong 5
83const EXP_UK_HYPO_NOT_FALSIFIED ulong 6
84const EXP_UK_INFERRED ulong 7
85const EXP_UK_UNKNOWN ulong 8
86const EXP_UK_TRUNCATED ulong 9
87const EXP_UK_PRODUCER_NOTE ulong 10
88
89note Presentation Mode and Audience codes
90const EXP_MODE_SUMMARY ulong 0
91const EXP_MODE_STEP_BY_STEP ulong 1
92const EXP_MODE_TECHNICAL ulong 2
93const EXP_MODE_ANALOGY ulong 3
94
95const EXP_AUD_GENERAL ulong 0
96const EXP_AUD_OPERATOR ulong 1
97const EXP_AUD_DEVELOPER ulong 2
98const EXP_AUD_SYSTEMS_ENG ulong 3
99const EXP_AUD_RESEARCHER ulong 4
100const EXP_AUD_INVALID ulong 255
101
102const EXP_MAX_LEVEL ulong 4
103const EXP_LEVEL_DEFAULT ulong 255
104
105note Internal table capacities
106const EXP_STR_CAP ulong 131072
107const EXP_OUT_CAP ulong 32768
108const EXP_CAP_NODES ulong 320
109const EXP_CAP_EDGES ulong 1024
110const EXP_CAP_MEAS ulong 256
111const EXP_CAP_MISS ulong 128
112const EXP_CAP_NOTES ulong 128
113const EXP_CAP_TRUNC ulong 128
114const EXP_CAP_SCOPE ulong 128
115const EXP_CAP_UNCS ulong 512
116const EXP_CAP_CONTRAS ulong 128
117const EXP_CAP_CON_INV ulong 256
118const EXP_CAP_ALTS ulong 128
119const EXP_CX_CAP ulong 64
120const EXP_CX_PAR_CAP ulong 256
121const EXP_BR_CAP ulong 16
122const EXP_BR_REF_CAP ulong 64
123const EXP_BR_TOK_CAP ulong 128
124const EXP_NONE ulong 999999
125
126note ============================================================================
127note Section 2: Constant Texts and Global State Tables
128note ============================================================================
129
130text exp_txt_format "aien.explain.evidence-graph"
131text exp_txt_builder "aienos-explain/1"
132text exp_txt_hex "0123456789abcdef"
133text exp_txt_cx_prod "aienos-explain/cortex-adapter"
134text exp_txt_br_prod "aienos-explain/branch-adapter"
135text exp_txt_cx_sys "aienos-cortex"
136text exp_txt_cx_art "EpistemicRecord"
137text exp_txt_cx_cbas "Cortex record confidence"
138text exp_txt_cx_pfx "cortex:"
139text exp_txt_br_sys "aienos-agent-state"
140text exp_txt_br_art "LogicalBranch"
141text exp_txt_br_pfx "branch:"
142text exp_txt_vby_pfx "verified by "
143text exp_txt_jmagic "ACXJ"
144
145note Status technical names
146text exp_sn_0 "Unclassified"
147text exp_sn_1 "DirectObservation"
148text exp_sn_2 "VerifiedFact"
149text exp_sn_3 "Inference"
150text exp_sn_4 "Hypothesis"
151text exp_sn_5 "Contradiction"
152text exp_sn_6 "OperatorDecision"
153
154note Status plain words (render.rs status_words)
155text exp_sw_0 "status unknown"
156text exp_sw_1 "observed"
157text exp_sw_2 "checked fact"
158text exp_sw_3 "inferred, not checked"
159text exp_sw_4 "hypothesis, not yet established"
160text exp_sw_5 "conflict between records"
161text exp_sw_6 "operator decision"
162
163note NodeKind technical names
164text exp_kn_1 "CortexRecord"
165text exp_kn_2 "CapabilityMetric"
166text exp_kn_3 "CapabilityAnalysis"
167text exp_kn_4 "Diagnostic"
168text exp_kn_5 "Hypothesis"
169text exp_kn_6 "Prediction"
170text exp_kn_7 "Falsification"
171text exp_kn_8 "EvaluationReceipt"
172text exp_kn_9 "PromotionState"
173text exp_kn_10 "OperatorDecision"
174text exp_kn_11 "AgentBranch"
175text exp_kn_12 "Conclusion"
176text exp_kn_13 "Alternative"
177
178note Relation technical names
179text exp_rn_1 "SupportedBy"
180text exp_rn_2 "Evaluates"
181text exp_rn_3 "PartOf"
182text exp_rn_4 "Contradicts"
183text exp_rn_5 "AlternativeTo"
184text exp_rn_6 "References"
185
186note Role headings
187text exp_rh_0 "Observed"
188text exp_rh_1 "System analysis"
189text exp_rh_2 "Hypothesis"
190text exp_rh_3 "Prediction"
191text exp_rh_4 "Falsification"
192text exp_rh_5 "Evaluation"
193text exp_rh_6 "Decision"
194text exp_rh_7 "Conclusion"
195
196note Role debug names (technical mode)
197text exp_rd_0 "Observed"
198text exp_rd_1 "Analysis"
199text exp_rd_2 "Hypothesis"
200text exp_rd_3 "Prediction"
201text exp_rd_4 "Falsification"
202text exp_rd_5 "Evaluation"
203text exp_rd_6 "Decision"
204text exp_rd_7 "Conclusion"
205
206note Uncertainty labels
207text exp_ul_1 "circular support"
208text exp_ul_2 "conflicting evidence"
209text exp_ul_3 "missing evidence"
210text exp_ul_4 "missing link"
211text exp_ul_5 "unsupported claim"
212text exp_ul_6 "hypothesis not yet tested"
213text exp_ul_7 "inferred step"
214text exp_ul_8 "unknown status"
215text exp_ul_9 "explanation cut short"
216text exp_ul_10 "caveat"
217
218note Uncertainty technical names
219text exp_un_1 "Cycle"
220text exp_un_2 "ConflictingEvidence"
221text exp_un_3 "MissingParent"
222text exp_un_4 "MissingLink"
223text exp_un_5 "Unsupported"
224text exp_un_6 "HypothesisNotFalsified"
225text exp_un_7 "Inferred"
226text exp_un_8 "Unknown"
227text exp_un_9 "Truncated"
228text exp_un_10 "ProducerNote"
229
230note Audience names
231text exp_an_0 "general"
232text exp_an_1 "operator"
233text exp_an_2 "developer"
234text exp_an_3 "systems-engineer"
235text exp_an_4 "researcher"
236
237note Presentation-only analogy catalog (never stored in bundle)
238text exp_ana_cap "The flagged part acts like a toll booth on a busy road: every request has to pass through it, so when it slows down, everything behind it queues up."
239text exp_ana_hyp "A hypothesis is like a mechanic's first guess about a noise in the engine: useful for deciding what to check, but not a finding until the check is done."
240text exp_ana_fal "A falsification test is like agreeing in advance which gauge reading would prove the repair did not work."
241text exp_ana_evl "The evaluation receipt is like an inspection sheet signed after a test drive: it records what was measured, not what was hoped."
242text exp_ana_brn "A branch is like one line of investigation in a case file: it lists the documents it actually relied on, not every document in the archive."
243text exp_ana_con "A contradiction is like two witnesses whose accounts do not match: the system keeps both and flags the conflict instead of picking one quietly."
244
245note Reusable rendering strings
246text exp_s_concl "Conclusion: "
247text exp_s_mreason "Main reason: "
248text exp_s_no_reason "Main reason: none recorded. The system has no evidence supporting this conclusion.\n"
249text exp_s_sev "Strongest evidence: "
250text exp_s_no_ev "Strongest evidence: no observed or checked evidence on this path.\n"
251text exp_s_munc "Main uncertainty: "
252text exp_s_no_munc "Main uncertainty: none recorded on this path.\n"
253text exp_s_therefore ". Therefore: "
254text exp_s_rests " (rests on step "
255text exp_s_refers " (refers to step "
256text exp_s_weak "; a weak link, not a recorded one)"
257text exp_s_from " [from "
258text exp_s_br_ref " (referenced by the branch)"
259text exp_s_meas " measurements: "
260text exp_s_meas_ni "measurements: "
261text exp_s_src " source: "
262text exp_s_dig " digest "
263text exp_s_unc_hdr "Uncertainty:\n"
264text exp_s_no_gaps "- no gaps recorded on this path (inferred steps are marked as such)\n"
265text exp_s_conf_hdr "Conflicts:\n"
266text exp_s_inv_a " (involves "
267text exp_s_inv_b " step(s))\n"
268text exp_s_alt_hdr "Other explanations the system recorded:\n"
269text exp_s_ana_hdr "Analogy (illustration only, not evidence; about step "
270text exp_s_ana_none "Analogy: none available for this explanation.\n"
271text exp_s_ev_itself "\nThe evidence itself:\n"
272text exp_s_step_hdr "Step "
273text exp_s_start_pt "It is a starting point: recorded directly, not derived.\n"
274text exp_s_no_sup "Nothing earlier on the path supports it.\n"
275text exp_s_it_rests "It rests on step "
276text exp_s_it_refs "It refers to (weak link, not a recorded one) step "
277text exp_s_rests_on_it " rests on it.\n"
278text exp_s_caveat "Caveat: "
279text exp_s_comes " comes from "
280text exp_s_ref_lbl "), reference "
281text exp_s_dig_c ", digest "
282text exp_s_node_sc "; node "
283text exp_s_st_sc "; status "
284text exp_s_no_ev_att "No digests, receipts or measurements are attached to this path.\n"
285text exp_s_no_unc_rec "No uncertainty is recorded on this path.\n"
286text exp_s_no_alt_rec "No alternative explanations are recorded in the evidence.\n"
287text exp_s_conflict_pfx "- conflict: "
288text exp_s_t_expl "explanation target="
289text exp_s_t_prod " producer="
290text exp_s_t_bldr " builder="
291text exp_s_t_cont " contract="
292text exp_s_t_concl "conclusion step="
293text exp_s_t_step "step "
294text exp_s_t_id " id="
295text exp_s_t_kind " kind="
296text exp_s_t_status " status="
297text exp_s_t_role " role="
298text exp_s_t_depth " depth="
299text exp_s_t_gnd " grounding="
300text exp_s_t_sup " supported_by=["
301text exp_s_t_stmt " statement: "
302text exp_s_t_conf " confidence_permille="
303text exp_s_t_basis " basis="
304text exp_s_t_unspec "unspecified"
305text exp_s_t_src " source system="
306text exp_s_t_art " artifact="
307text exp_s_t_ref " reference="
308text exp_s_t_dig " digest="
309text exp_s_t_rec " recorded_at_utc="
310text exp_s_t_rec_n " (informational, not used for order)\n"
311text exp_s_t_note " note: "
312text exp_s_t_unc "uncertainty code="
313text exp_s_t_node " node="
314text exp_s_t_rel " related="
315text exp_s_t_det " detail="
316text exp_s_t_contra "contradiction id="
317text exp_s_t_invs " involves=["
318text exp_s_t_stmteq "] statement="
319text exp_s_t_alt "alternative id="
320text exp_s_t_to " to="
321text exp_s_t_stmts " statement="
322text exp_s_t_idig "input_digest="
323text exp_s_t_bdig " bundle_digest="
324text exp_s_t_vis " visited="
325text exp_s_t_lim " limits=depth:"
326text exp_s_t_nod ",nodes:"
327text exp_s_true "true"
328text exp_s_false "false"
329text exp_s_miss_a " lists "
330text exp_s_miss_b " as support, but no such artifact is available; the system has no evidence for that link"
331text exp_s_trunc_a "support of "
332text exp_s_trunc_b " was not followed further (depth limit "
333text exp_s_trunc_c ", node limit "
334text exp_s_ptrunc_a "the producer of this graph stopped collecting evidence around "
335text exp_s_ptrunc_b " at its own limits; more support or contradictions may exist"
336text exp_s_cyc_a " rests on "
337text exp_s_cyc_b ", which already rests on "
338text exp_s_cyc_c "; the loop was cut and this support is circular, not grounding"
339text exp_s_mlink_a "no evidence for: "
340text exp_s_unk_a "the epistemic status of step "
341text exp_s_unk_b " is not known; it is neither observed nor verified"
342text exp_s_unsup_a "step "
343text exp_s_unsup_b " is a "
344text exp_s_unsup_c " with no recorded support"
345text exp_s_inf_a " is inferred from earlier steps, not observed directly"
346text exp_s_hyp_a "hypothesis at step "
347text exp_s_hyp_b " has no evaluation that tests it directly; it remains a hypothesis"
348text exp_s_contra_a " contradicts "
349text exp_s_contra_b " is involved in a recorded contradiction: "
350text exp_s_br_a "Agent branch "
351text exp_s_br_b " (lineage depth "
352text exp_s_br_c ", parent "
353text exp_s_br_d ") depends on "
354text exp_s_br_e " referenced Cortex record(s)"
355text exp_s_br_root "none (root branch)"
356text exp_s_json_fmt "\"format\""
357text exp_s_json_ver "\"version\""
358
359note String arena
360global exp_str uchar 131072
361global exp_str_len ulong 1
362
363note Output buffer for rendering and session responses
364global exp_out uchar 32768
365global exp_out_len ulong 1
366
367note EvidenceGraph header
368global exp_g_fmt_off ulong 1
369global exp_g_fmt_len ulong 1
370global exp_g_ver ulong 1
371global exp_g_prod_off ulong 1
372global exp_g_prod_len ulong 1
373global exp_g_ncnt ulong 1
374global exp_g_decl_ncnt ulong 1
375global exp_g_ecnt ulong 1
376global exp_g_mcnt ulong 1
377global exp_g_xcnt ulong 1
378global exp_g_notecnt ulong 1
379global exp_g_tcnt ulong 1
380global exp_g_has_scope ulong 1
381global exp_g_sbr_off ulong 1
382global exp_g_sbr_len ulong 1
383global exp_g_scnt ulong 1
384
385note EvidenceGraph nodes (0..EXP_CAP_NODES-1)
386global exp_gn_id_off ulong 320
387global exp_gn_id_len ulong 320
388global exp_gn_kind ulong 320
389global exp_gn_status ulong 320
390global exp_gn_stmt_off ulong 320
391global exp_gn_stmt_len ulong 320
392global exp_gn_sys_off ulong 320
393global exp_gn_sys_len ulong 320
394global exp_gn_art_off ulong 320
395global exp_gn_art_len ulong 320
396global exp_gn_ref_off ulong 320
397global exp_gn_ref_len ulong 320
398global exp_gn_has_dig ulong 320
399global exp_gn_dig_off ulong 320
400global exp_gn_dig_len ulong 320
401global exp_gn_has_conf ulong 320
402global exp_gn_conf ulong 320
403global exp_gn_has_cbas ulong 320
404global exp_gn_cbas_off ulong 320
405global exp_gn_cbas_len ulong 320
406global exp_gn_has_ts ulong 320
407global exp_gn_ts ulong 320
408global exp_gn_decl_par ulong 320
409
410note EvidenceGraph raw edges (owner node idx -> target id string, relation)
411global exp_ge_owner ulong 1024
412global exp_ge_to_off ulong 1024
413global exp_ge_to_len ulong 1024
414global exp_ge_rel ulong 1024
415
416note EvidenceGraph measurements
417global exp_gm_owner ulong 256
418global exp_gm_name_off ulong 256
419global exp_gm_name_len ulong 256
420global exp_gm_val_off ulong 256
421global exp_gm_val_len ulong 256
422global exp_gm_unit_off ulong 256
423global exp_gm_unit_len ulong 256
424
425note EvidenceGraph missing links
426global exp_gx_owner ulong 128
427global exp_gx_exp_off ulong 128
428global exp_gx_exp_len ulong 128
429global exp_gx_rsn_off ulong 128
430global exp_gx_rsn_len ulong 128
431
432note EvidenceGraph notes
433global exp_go_owner ulong 128
434global exp_go_off ulong 128
435global exp_go_len ulong 128
436
437note EvidenceGraph truncated_at and scope.referenced
438global exp_gt_off ulong 128
439global exp_gt_len ulong 128
440global exp_gs_off ulong 128
441global exp_gs_len ulong 128
442
443note Builder scratch arrays
444global exp_w_sup_off ulong 320
445global exp_w_sup_cnt ulong 320
446global exp_w_sup_rel ulong 1024
447global exp_w_sup_to_off ulong 1024
448global exp_w_sup_to_len ulong 1024
449global exp_w_sup_total ulong 1
450global exp_w_depth ulong 320
451global exp_w_vis ulong 320
452global exp_w_trunc ulong 320
453global exp_w_queue ulong 320
454global exp_w_color ulong 320
455global exp_w_ve_off ulong 320
456global exp_w_ve_cnt ulong 320
457global exp_w_ve_to ulong 1024
458global exp_w_ve_rel ulong 1024
459global exp_w_ve_cut ulong 1024
460global exp_w_ve_total ulong 1
461global exp_w_stk_n ulong 640
462global exp_w_stk_i ulong 640
463global exp_w_rem ulong 320
464global exp_w_placed ulong 320
465global exp_w_pos ulong 320
466global exp_w_sorted_n ulong 320
467global exp_w_tmp_off ulong 128
468global exp_w_tmp_len ulong 128
469global exp_w_inv_off ulong 128
470global exp_w_inv_len ulong 128
471global exp_w_bfs_st ulong 2
472
473note Built ExplanationBundle
474global exp_b_scnt ulong 1
475global exp_b_concl_step ulong 1
476global exp_bs_nid ulong 320
477global exp_bs_role ulong 320
478global exp_bs_depth ulong 320
479global exp_bs_gnd ulong 320
480global exp_bs_bref ulong 320
481global exp_bs_sup_off ulong 320
482global exp_bs_sup_cnt ulong 320
483global exp_bs_sup_step ulong 1024
484global exp_bs_sup_rel ulong 1024
485global exp_bs_sup_total ulong 1
486
487global exp_be_cnt ulong 1
488global exp_be_step ulong 320
489
490global exp_bu_cnt ulong 1
491global exp_bu_kind ulong 512
492global exp_bu_nid_off ulong 512
493global exp_bu_nid_len ulong 512
494global exp_bu_has_rel ulong 512
495global exp_bu_rel_off ulong 512
496global exp_bu_rel_len ulong 512
497global exp_bu_det_off ulong 512
498global exp_bu_det_len ulong 512
499
500global exp_bc_cnt ulong 1
501global exp_bc_nid ulong 128
502global exp_bc_stmt_off ulong 128
503global exp_bc_stmt_len ulong 128
504global exp_bc_inv_off ulong 128
505global exp_bc_inv_cnt ulong 128
506global exp_bc_inv_total ulong 1
507global exp_bc_inv_id_off ulong 256
508global exp_bc_inv_id_len ulong 256
509
510global exp_ba_cnt ulong 1
511global exp_ba_nid ulong 128
512global exp_ba_to_off ulong 128
513global exp_ba_to_len ulong 128
514
515global exp_bp_tgt_off ulong 1
516global exp_bp_tgt_len ulong 1
517global exp_bp_max_d ulong 1
518global exp_bp_max_n ulong 1
519global exp_bp_vis ulong 1
520global exp_bp_gnodes ulong 1
521global exp_bp_in_dig uchar 32
522global exp_bp_bun_dig uchar 32
523
524note Cortex adapter store
525global exp_cx_str uchar 16384
526global exp_cx_str_len ulong 1
527global exp_cx_cnt ulong 1
528global exp_cx_seq ulong 1
529global exp_cx_id uchar 1024
530global exp_cx_status ulong 64
531global exp_cx_stmt_off ulong 64
532global exp_cx_stmt_len ulong 64
533global exp_cx_prov_off ulong 64
534global exp_cx_prov_len ulong 64
535global exp_cx_evhash uchar 2048
536global exp_cx_ts ulong 64
537global exp_cx_has_conf ulong 64
538global exp_cx_conf ulong 64
539global exp_cx_has_vby ulong 64
540global exp_cx_vby_off ulong 64
541global exp_cx_vby_len ulong 64
542global exp_cx_pcnt ulong 1
543global exp_cx_p_own ulong 256
544global exp_cx_p_id uchar 4096
545
546note Cortex adapter scratch
547global exp_cxw_depth ulong 64
548global exp_cxw_vis ulong 64
549global exp_cxw_trunc ulong 64
550global exp_cxw_isroot ulong 64
551global exp_cxw_q ulong 64
552global exp_cxw_ord ulong 64
553global exp_cxw_emit ulong 64
554global exp_cxw_con ulong 64
555
556note Agent branch adapter store
557global exp_br_cnt ulong 1
558global exp_br_id uchar 256
559global exp_br_has_par ulong 16
560global exp_br_par_id uchar 256
561global exp_br_depth ulong 16
562global exp_br_ts ulong 16
563global exp_br_rcnt ulong 1
564global exp_br_r_own ulong 64
565global exp_br_r_id uchar 1024
566global exp_br_tcnt ulong 1
567global exp_br_t_own ulong 128
568global exp_br_t_val ulong 128
569
570note ExplanationSession state (0=level, 1=audience)
571global exp_sess ulong 2
572
573note ============================================================================
574note Section 3: Contract Predicates and Enum Helpers
575note ============================================================================
576
577fn exp_status_valid code ulong to bool
578ret le code EXP_ST_OPERATOR_DECISION
579end
580
581fn exp_status_is_grounding code ulong to bool
582if eq code EXP_ST_DIRECT_OBS
583ret true
584end
585if eq code EXP_ST_VERIFIED_FACT
586ret true
587end
588if eq code EXP_ST_OPERATOR_DECISION
589ret true
590end
591ret false
592end
593
594fn exp_kind_valid code ulong to bool
595if lt code EXP_KD_CORTEX_RECORD
596ret false
597end
598if gt code EXP_KD_ALTERNATIVE
599ret false
600end
601ret true
602end
603
604fn exp_rel_valid code ulong to bool
605if lt code EXP_REL_SUPPORTED_BY
606ret false
607end
608if gt code EXP_REL_REFERENCES
609ret false
610end
611ret true
612end
613
614fn exp_rel_is_support code ulong to bool
615if eq code EXP_REL_SUPPORTED_BY
616ret true
617end
618if eq code EXP_REL_EVALUATES
619ret true
620end
621if eq code EXP_REL_PART_OF
622ret true
623end
624if eq code EXP_REL_REFERENCES
625ret true
626end
627ret false
628end
629
630fn exp_role_of kd ulong st ulong to ulong
631if eq kd EXP_KD_CAP_METRIC
632ret EXP_ROLE_OBSERVED
633end
634if eq kd EXP_KD_CAP_ANALYSIS
635ret EXP_ROLE_ANALYSIS
636end
637if eq kd EXP_KD_DIAGNOSTIC
638ret EXP_ROLE_ANALYSIS
639end
640if eq kd EXP_KD_HYPOTHESIS
641ret EXP_ROLE_HYPOTHESIS
642end
643if eq kd EXP_KD_PREDICTION
644ret EXP_ROLE_PREDICTION
645end
646if eq kd EXP_KD_FALSIFICATION
647ret EXP_ROLE_FALSIFICATION
648end
649if eq kd EXP_KD_EVAL_RECEIPT
650ret EXP_ROLE_EVALUATION
651end
652if eq kd EXP_KD_PROMOTION_STATE
653ret EXP_ROLE_DECISION
654end
655if eq kd EXP_KD_OPERATOR_DECISION
656ret EXP_ROLE_DECISION
657end
658if eq kd EXP_KD_CONCLUSION
659ret EXP_ROLE_CONCLUSION
660end
661if eq kd EXP_KD_AGENT_BRANCH
662ret EXP_ROLE_CONCLUSION
663end
664if eq kd EXP_KD_ALTERNATIVE
665ret EXP_ROLE_ANALYSIS
666end
667note CortexRecord maps by epistemic status
668if eq st EXP_ST_DIRECT_OBS
669ret EXP_ROLE_OBSERVED
670end
671if eq st EXP_ST_VERIFIED_FACT
672ret EXP_ROLE_OBSERVED
673end
674if eq st EXP_ST_OPERATOR_DECISION
675ret EXP_ROLE_DECISION
676end
677if eq st EXP_ST_HYPOTHESIS
678ret EXP_ROLE_HYPOTHESIS
679end
680ret EXP_ROLE_ANALYSIS
681end
682
683fn exp_uk_severity uk ulong to ulong
684if eq uk EXP_UK_CYCLE
685ret 0
686end
687if eq uk EXP_UK_CONFLICT
688ret 1
689end
690if eq uk EXP_UK_MISSING_PARENT
691ret 2
692end
693if eq uk EXP_UK_MISSING_LINK
694ret 3
695end
696if eq uk EXP_UK_UNSUPPORTED
697ret 4
698end
699if eq uk EXP_UK_HYPO_NOT_FALSIFIED
700ret 5
701end
702if eq uk EXP_UK_UNKNOWN
703ret 6
704end
705if eq uk EXP_UK_TRUNCATED
706ret 7
707end
708if eq uk EXP_UK_PRODUCER_NOTE
709ret 8
710end
711ret 9
712end
713
714fn exp_check_limits max_d ulong max_n ulong to int
715if eq max_d 0
716ret EXP_ERR_INVALID_LIMITS
717end
718if eq max_n 0
719ret EXP_ERR_INVALID_LIMITS
720end
721if gt max_d EXP_HARD_MAX_DEPTH
722ret EXP_ERR_INVALID_LIMITS
723end
724if gt max_n EXP_HARD_MAX_NODES
725ret EXP_ERR_INVALID_LIMITS
726end
727ret EXP_OK
728end
729
730note ============================================================================
731note Section 4: String Arena, Output Buffer, Hex & Decimal Helpers
732note ============================================================================
733
734fn exp_str_reset to void
735put exp_str_len 0 0
736end
737
738fn exp_str_mark to ulong
739ret get exp_str_len 0
740end
741
742fn exp_str_put_b b uchar to void
743p = get exp_str_len 0
744if lt p EXP_STR_CAP
745put exp_str p b
746put exp_str_len 0 add p 1
747end
748end
749
750fn exp_str_add_view s view uchar * to ulong
751start = get exp_str_len 0
752n = len s
753for i ulong n bound 1024
754exp_str_put_b get s i
755end
756ret start
757end
758
759fn exp_str_add_arena off ulong ln ulong to ulong
760start = get exp_str_len 0
761for i ulong ln bound 1024
762exp_str_put_b get exp_str add off i
763end
764ret start
765end
766
767fn exp_str_add_u64 v ulong to void
768buf = new uchar 24 0
769if eq v 0
770exp_str_put_b uchar 48
771ret
772end
773k = ulong 0
774cur = v
775while gt cur 0 bound 24
776put buf k uchar add 48 rem cur 10
777cur = div cur 10
778k = add k 1
779end
780for i ulong k bound 24
781exp_str_put_b get buf sub sub k 1 i
782end
783end
784
785fn exp_str_add_hex16 src_base ulong is_cx ulong to void
786for i ulong 16
787b = ulong 0
788if eq is_cx 1
789b = ulong get exp_cx_id add src_base i
790else
791if eq is_cx 2
792b = ulong get exp_br_id add src_base i
793else
794b = ulong get exp_br_par_id add src_base i
795end
796end
797exp_str_put_b get exp_txt_hex shr b 4
798exp_str_put_b get exp_txt_hex and b 15
799end
800end
801
802fn exp_str_add_hex16_view id view uchar * to void
803for i ulong 16
804b = ulong get id i
805exp_str_put_b get exp_txt_hex shr b 4
806exp_str_put_b get exp_txt_hex and b 15
807end
808end
809
810fn exp_str_add_hex32_cx base ulong to void
811for i ulong 32
812b = ulong get exp_cx_evhash add base i
813exp_str_put_b get exp_txt_hex shr b 4
814exp_str_put_b get exp_txt_hex and b 15
815end
816end
817
818fn exp_str_cmp a_off ulong a_ln ulong b_off ulong b_ln ulong to int
819ml = a_ln
820if lt b_ln ml
821ml = b_ln
822end
823for i ulong ml bound 1024
824ca = get exp_str add a_off i
825cb = get exp_str add b_off i
826if lt ca cb
827ret -1
828end
829if gt ca cb
830ret 1
831end
832end
833if lt a_ln b_ln
834ret -1
835end
836if gt a_ln b_ln
837ret 1
838end
839ret 0
840end
841
842fn exp_str_eq a_off ulong a_ln ulong b_off ulong b_ln ulong to bool
843if ne a_ln b_ln
844ret false
845end
846for i ulong a_ln bound 1024
847if ne get exp_str add a_off i get exp_str add b_off i
848ret false
849end
850end
851ret true
852end
853
854fn exp_str_eq_view a_off ulong a_ln ulong s view uchar * to bool
855if ne a_ln len s
856ret false
857end
858for i ulong a_ln bound 1024
859if ne get exp_str add a_off i get s i
860ret false
861end
862end
863ret true
864end
865
866fn exp_view_eq a view uchar * b view uchar * to bool
867if ne len a len b
868ret false
869end
870for i ulong len a bound 1024
871if ne get a i get b i
872ret false
873end
874end
875ret true
876end
877
878fn exp_out_reset to void
879put exp_out_len 0 0
880end
881
882fn exp_out_b b uchar to void
883p = get exp_out_len 0
884if lt p EXP_OUT_CAP
885put exp_out p b
886put exp_out_len 0 add p 1
887end
888end
889
890fn exp_out_view s view uchar * to void
891n = len s
892for i ulong n bound 1024
893exp_out_b get s i
894end
895end
896
897fn exp_out_arena off ulong ln ulong to void
898for i ulong ln bound 1024
899exp_out_b get exp_str add off i
900end
901end
902
903fn exp_out_u64 v ulong to void
904buf = new uchar 24 0
905if eq v 0
906exp_out_b uchar 48
907ret
908end
909k = ulong 0
910cur = v
911while gt cur 0 bound 24
912put buf k uchar add 48 rem cur 10
913cur = div cur 10
914k = add k 1
915end
916for i ulong k bound 24
917exp_out_b get buf sub sub k 1 i
918end
919end
920
921fn exp_out_dig32 is_bun ulong to void
922for i ulong 32
923b = ulong 0
924if eq is_bun 1
925b = ulong get exp_bp_bun_dig i
926else
927b = ulong get exp_bp_in_dig i
928end
929exp_out_b get exp_txt_hex shr b 4
930exp_out_b get exp_txt_hex and b 15
931end
932end
933
934fn exp_out_copy dst edit uchar * to ulong
935n = get exp_out_len 0
936if gt n len dst
937n = len dst
938end
939for i ulong n bound 32768
940put dst i get exp_out i
941end
942ret n
943end
944
945fn exp_out_match_at pos ulong needle view uchar * to bool
946nlen = len needle
947for j ulong nlen bound 256
948if ne get exp_out add pos j get needle j
949ret false
950end
951end
952ret true
953end
954
955fn exp_out_contains needle view uchar * to bool
956nlen = len needle
957olen = get exp_out_len 0
958if eq nlen 0
959ret true
960end
961if gt nlen olen
962ret false
963end
964lim = add sub olen nlen 1
965for i ulong lim bound 32768
966if exp_out_match_at i needle
967ret true
968end
969end
970ret false
971end
972
973fn exp_slice_contains_out start ulong endp ulong needle view uchar * to bool
974nlen = len needle
975slen = sub endp start
976if eq nlen 0
977ret true
978end
979if gt nlen slen
980ret false
981end
982lim = add sub slen nlen 1
983for i ulong lim bound 2048
984if exp_out_match_at add start i needle
985ret true
986end
987end
988ret false
989end
990
991fn exp_out_any_line_has_both a view uchar * b view uchar * to bool
992olen = get exp_out_len 0
993pos = ulong 0
994while lt pos olen bound 4096
995st = pos
996while lt pos olen bound 2048
997if eq get exp_out pos 10
998break
999end
1000pos = add pos 1
1001end
1002if exp_slice_contains_out st pos a
1003if exp_slice_contains_out st pos b
1004ret true
1005end
1006end
1007if lt pos olen
1008pos = add pos 1
1009end
1010end
1011ret false
1012end
1013
1014note ============================================================================
1015note Section 5: Streaming SHA-256 Helpers for Canonical Digests
1016note ============================================================================
1017
1018fn exp_sha_u64 ctx edit ulong * v ulong to void
1019buf = new uchar 8 0
1020for i ulong 8
1021sh = mul sub 7 i 8
1022put buf i uchar and shr v sh 0xff
1023end
1024sha256_update ctx buf
1025end
1026
1027fn exp_sha_arena ctx edit ulong * off ulong ln ulong to void
1028exp_sha_u64 ctx ln
1029buf = new uchar 64 0
1030pos = ulong 0
1031while lt pos ln bound 2048
1032k = sub ln pos
1033if gt k 64
1034k = 64
1035end
1036for i ulong k bound 64
1037put buf i get exp_str add add off pos i
1038end
1039sha256_update ctx part buf 0 k
1040pos = add pos k
1041end
1042end
1043
1044fn exp_sha_tag ctx edit ulong * tag ulong to void
1045buf = new uchar 1 0
1046put buf 0 uchar and tag 0xff
1047sha256_update ctx buf
1048end
1049
1050note ============================================================================
1051note Section 6: EvidenceGraph Construction & Contract Validation (contract.rs)
1052note ============================================================================
1053
1054fn exp_g_reset_prod prod view uchar * to void
1055exp_str_reset
1056fo = exp_str_add_view exp_txt_format
1057put exp_g_fmt_off 0 fo
1058put exp_g_fmt_len 0 len exp_txt_format
1059put exp_g_ver 0 EXP_VERSION
1060po = exp_str_add_view prod
1061put exp_g_prod_off 0 po
1062put exp_g_prod_len 0 len prod
1063put exp_g_ncnt 0 0
1064put exp_g_decl_ncnt 0 0
1065put exp_g_ecnt 0 0
1066put exp_g_mcnt 0 0
1067put exp_g_xcnt 0 0
1068put exp_g_notecnt 0 0
1069put exp_g_tcnt 0 0
1070put exp_g_has_scope 0 0
1071put exp_g_sbr_off 0 0
1072put exp_g_sbr_len 0 0
1073put exp_g_scnt 0 0
1074end
1075
1076fn exp_g_set_format fmt view uchar * to void
1077fo = exp_str_add_view fmt
1078put exp_g_fmt_off 0 fo
1079put exp_g_fmt_len 0 len fmt
1080end
1081
1082fn exp_g_set_version ver ulong to void
1083put exp_g_ver 0 ver
1084end
1085
1086fn exp_g_set_producer prod view uchar * to void
1087po = exp_str_add_view prod
1088put exp_g_prod_off 0 po
1089put exp_g_prod_len 0 len prod
1090end
1091
1092fn exp_g_add_node_raw id_o ulong id_l ulong kd ulong st ulong to ulong
1093idx = get exp_g_ncnt 0
1094if ge idx EXP_CAP_NODES
1095ret EXP_NONE
1096end
1097put exp_gn_id_off idx id_o
1098put exp_gn_id_len idx id_l
1099put exp_gn_kind idx kd
1100put exp_gn_status idx st
1101put exp_gn_stmt_off idx 0
1102put exp_gn_stmt_len idx 0
1103put exp_gn_sys_off idx 0
1104put exp_gn_sys_len idx 0
1105put exp_gn_art_off idx 0
1106put exp_gn_art_len idx 0
1107put exp_gn_ref_off idx 0
1108put exp_gn_ref_len idx 0
1109put exp_gn_has_dig idx 0
1110put exp_gn_dig_off idx 0
1111put exp_gn_dig_len idx 0
1112put exp_gn_has_conf idx 0
1113put exp_gn_conf idx 0
1114put exp_gn_has_cbas idx 0
1115put exp_gn_cbas_off idx 0
1116put exp_gn_cbas_len idx 0
1117put exp_gn_has_ts idx 0
1118put exp_gn_ts idx 0
1119put exp_gn_decl_par idx 0
1120put exp_g_ncnt 0 add idx 1
1121put exp_g_decl_ncnt 0 add idx 1
1122ret idx
1123end
1124
1125fn exp_g_add_node id view uchar * kd ulong st ulong stmt view uchar * to ulong
1126id_o = exp_str_add_view id
1127nid = exp_g_add_node_raw id_o len id kd st
1128if eq nid EXP_NONE
1129ret EXP_NONE
1130end
1131st_o = exp_str_add_view stmt
1132put exp_gn_stmt_off nid st_o
1133put exp_gn_stmt_len nid len stmt
1134ret nid
1135end
1136
1137fn exp_g_set_source_sa nid ulong sys view uchar * art view uchar * to void
1138if ge nid get exp_g_ncnt 0
1139ret
1140end
1141so = exp_str_add_view sys
1142ao = exp_str_add_view art
1143put exp_gn_sys_off nid so
1144put exp_gn_sys_len nid len sys
1145put exp_gn_art_off nid ao
1146put exp_gn_art_len nid len art
1147end
1148
1149fn exp_g_set_source_ref nid ulong rf view uchar * to void
1150if ge nid get exp_g_ncnt 0
1151ret
1152end
1153ro = exp_str_add_view rf
1154put exp_gn_ref_off nid ro
1155put exp_gn_ref_len nid len rf
1156end
1157
1158fn exp_g_set_digest nid ulong dig view uchar * to void
1159if ge nid get exp_g_ncnt 0
1160ret
1161end
1162dig_o = exp_str_add_view dig
1163put exp_gn_has_dig nid 1
1164put exp_gn_dig_off nid dig_o
1165put exp_gn_dig_len nid len dig
1166end
1167
1168fn exp_g_set_conf nid ulong pm ulong bas view uchar * to void
1169if ge nid get exp_g_ncnt 0
1170ret
1171end
1172put exp_gn_has_conf nid 1
1173put exp_gn_conf nid pm
1174bo = exp_str_add_view bas
1175put exp_gn_has_cbas nid 1
1176put exp_gn_cbas_off nid bo
1177put exp_gn_cbas_len nid len bas
1178end
1179
1180fn exp_g_set_ts nid ulong ts ulong to void
1181if ge nid get exp_g_ncnt 0
1182ret
1183end
1184put exp_gn_has_ts nid 1
1185put exp_gn_ts nid ts
1186end
1187
1188fn exp_g_add_edge_raw nid ulong to_o ulong to_l ulong rl ulong to void
1189ec = get exp_g_ecnt 0
1190if ge ec EXP_CAP_EDGES
1191ret
1192end
1193put exp_ge_owner ec nid
1194put exp_ge_to_off ec to_o
1195put exp_ge_to_len ec to_l
1196put exp_ge_rel ec rl
1197put exp_g_ecnt 0 add ec 1
1198end
1199
1200fn exp_g_add_edge nid ulong target view uchar * rl ulong to void
1201to_o = exp_str_add_view target
1202exp_g_add_edge_raw nid to_o len target rl
1203end
1204
1205fn exp_g_add_meas nid ulong nm view uchar * vl view uchar * to ulong
1206mc = get exp_g_mcnt 0
1207if ge mc EXP_CAP_MEAS
1208ret EXP_NONE
1209end
1210put exp_gm_owner mc nid
1211no = exp_str_add_view nm
1212vo = exp_str_add_view vl
1213put exp_gm_name_off mc no
1214put exp_gm_name_len mc len nm
1215put exp_gm_val_off mc vo
1216put exp_gm_val_len mc len vl
1217put exp_gm_unit_off mc 0
1218put exp_gm_unit_len mc 0
1219put exp_g_mcnt 0 add mc 1
1220ret mc
1221end
1222
1223fn exp_g_meas_set_unit mid ulong un view uchar * to void
1224if ge mid get exp_g_mcnt 0
1225ret
1226end
1227uo = exp_str_add_view un
1228put exp_gm_unit_off mid uo
1229put exp_gm_unit_len mid len un
1230end
1231
1232fn exp_g_add_missing nid ulong ex view uchar * rsn view uchar * to void
1233xc = get exp_g_xcnt 0
1234if ge xc EXP_CAP_MISS
1235ret
1236end
1237put exp_gx_owner xc nid
1238eo = exp_str_add_view ex
1239ro = exp_str_add_view rsn
1240put exp_gx_exp_off xc eo
1241put exp_gx_exp_len xc len ex
1242put exp_gx_rsn_off xc ro
1243put exp_gx_rsn_len xc len rsn
1244put exp_g_xcnt 0 add xc 1
1245end
1246
1247fn exp_g_add_note nid ulong nt view uchar * to void
1248nc = get exp_g_notecnt 0
1249if ge nc EXP_CAP_NOTES
1250ret
1251end
1252put exp_go_owner nc nid
1253no = exp_str_add_view nt
1254put exp_go_off nc no
1255put exp_go_len nc len nt
1256put exp_g_notecnt 0 add nc 1
1257end
1258
1259fn exp_g_add_truncated_raw id_o ulong id_l ulong to void
1260tc = get exp_g_tcnt 0
1261if ge tc EXP_CAP_TRUNC
1262ret
1263end
1264put exp_gt_off tc id_o
1265put exp_gt_len tc id_l
1266put exp_g_tcnt 0 add tc 1
1267end
1268
1269fn exp_g_add_truncated id view uchar * to void
1270io = exp_str_add_view id
1271exp_g_add_truncated_raw io len id
1272end
1273
1274fn exp_g_set_scope_branch br_o ulong br_l ulong to void
1275put exp_g_has_scope 0 1
1276put exp_g_sbr_off 0 br_o
1277put exp_g_sbr_len 0 br_l
1278end
1279
1280fn exp_g_add_scope_ref_raw id_o ulong id_l ulong to void
1281sc = get exp_g_scnt 0
1282if ge sc EXP_CAP_SCOPE
1283ret
1284end
1285put exp_gs_off sc id_o
1286put exp_gs_len sc id_l
1287put exp_g_scnt 0 add sc 1
1288end
1289
1290fn exp_g_find_node id_o ulong id_l ulong to ulong
1291nc = get exp_g_ncnt 0
1292for i ulong nc bound 320
1293if exp_str_eq get exp_gn_id_off i get exp_gn_id_len i id_o id_l
1294ret i
1295end
1296end
1297ret EXP_NONE
1298end
1299
1300fn exp_g_find_node_view id view uchar * to ulong
1301nc = get exp_g_ncnt 0
1302for i ulong nc bound 320
1303if exp_str_eq_view get exp_gn_id_off i get exp_gn_id_len i id
1304ret i
1305end
1306end
1307ret EXP_NONE
1308end
1309
1310fn exp_g_is_prod_truncated id_o ulong id_l ulong to bool
1311tc = get exp_g_tcnt 0
1312for i ulong tc bound 128
1313if exp_str_eq get exp_gt_off i get exp_gt_len i id_o id_l
1314ret true
1315end
1316end
1317ret false
1318end
1319
1320fn exp_g_is_scope_ref id_o ulong id_l ulong to bool
1321sc = get exp_g_scnt 0
1322for i ulong sc bound 128
1323if exp_str_eq get exp_gs_off i get exp_gs_len i id_o id_l
1324ret true
1325end
1326end
1327ret false
1328end
1329
1330fn exp_g_validate to int
1331if not exp_str_eq_view get exp_g_fmt_off 0 get exp_g_fmt_len 0 exp_txt_format
1332ret EXP_ERR_UNSUPPORTED_VERSION
1333end
1334if ne get exp_g_ver 0 EXP_VERSION
1335ret EXP_ERR_UNSUPPORTED_VERSION
1336end
1337if gt get exp_g_decl_ncnt 0 EXP_MAX_GRAPH_NODES
1338ret EXP_ERR_GRAPH_TOO_LARGE
1339end
1340nc = get exp_g_ncnt 0
1341if gt nc EXP_MAX_GRAPH_NODES
1342ret EXP_ERR_GRAPH_TOO_LARGE
1343end
1344for i ulong nc bound 320
1345if not exp_kind_valid get exp_gn_kind i
1346ret EXP_ERR_UNKNOWN_CODE
1347end
1348if not exp_status_valid get exp_gn_status i
1349ret EXP_ERR_UNKNOWN_CODE
1350end
1351if ne get exp_gn_has_conf i 0
1352if gt get exp_gn_conf i 1000
1353ret EXP_ERR_UNKNOWN_CODE
1354end
1355end
1356if gt get exp_gn_decl_par i EXP_MAX_PARENTS_PER_NODE
1357ret EXP_ERR_GRAPH_TOO_LARGE
1358end
1359for j ulong i bound 320
1360if exp_str_eq get exp_gn_id_off i get exp_gn_id_len i get exp_gn_id_off j get exp_gn_id_len j
1361ret EXP_ERR_DUPLICATE_NODE_ID
1362end
1363end
1364end
1365ec = get exp_g_ecnt 0
1366for i ulong ec bound 1024
1367if not exp_rel_valid get exp_ge_rel i
1368ret EXP_ERR_UNKNOWN_CODE
1369end
1370end
1371for i ulong nc bound 320
1372pc = ulong 0
1373for e ulong ec bound 1024
1374if eq get exp_ge_owner e i
1375pc = add pc 1
1376end
1377end
1378if gt pc EXP_MAX_PARENTS_PER_NODE
1379ret EXP_ERR_GRAPH_TOO_LARGE
1380end
1381end
1382ret EXP_OK
1383end
1384
1385note Structural JSON envelope check (rejects malformed JSON with E1)
1386fn exp_json_check raw view uchar * to int
1387n = len raw
1388if lt n 2
1389ret EXP_ERR_BAD_FORMAT
1390end
1391if ne get raw 0 123
1392ret EXP_ERR_BAD_FORMAT
1393end
1394if ne get raw sub n 1 125
1395ret EXP_ERR_BAD_FORMAT
1396end
1397depth = ulong 0
1398in_str = false
1399esc = false
1400for i ulong n bound 8192
1401ch = get raw i
1402if in_str
1403if esc
1404esc = false
1405else
1406if eq ch 92
1407esc = true
1408else
1409if eq ch 34
1410in_str = false
1411end
1412end
1413end
1414else
1415if eq ch 34
1416in_str = true
1417else
1418if or eq ch 123 eq ch 91
1419depth = add depth 1
1420else
1421if or eq ch 125 eq ch 93
1422if eq depth 0
1423ret EXP_ERR_BAD_FORMAT
1424end
1425depth = sub depth 1
1426end
1427end
1428end
1429end
1430end
1431if in_str
1432ret EXP_ERR_BAD_FORMAT
1433end
1434if ne depth 0
1435ret EXP_ERR_BAD_FORMAT
1436end
1437has_colon = false
1438for i ulong n bound 8192
1439if eq get raw i 58
1440has_colon = true
1441break
1442end
1443end
1444if not has_colon
1445ret EXP_ERR_BAD_FORMAT
1446end
1447ret EXP_OK
1448end
1449
1450note ============================================================================
1451note Section 7: Canonical Input & Bundle SHA-256 Digests (bundle.rs)
1452note ============================================================================
1453
1454fn exp_hash_graph_node ctx edit ulong * nid ulong to void
1455exp_sha_tag ctx 1
1456exp_sha_arena ctx get exp_gn_id_off nid get exp_gn_id_len nid
1457exp_sha_u64 ctx get exp_gn_kind nid
1458exp_sha_u64 ctx get exp_gn_status nid
1459exp_sha_arena ctx get exp_gn_stmt_off nid get exp_gn_stmt_len nid
1460exp_sha_arena ctx get exp_gn_sys_off nid get exp_gn_sys_len nid
1461exp_sha_arena ctx get exp_gn_art_off nid get exp_gn_art_len nid
1462exp_sha_arena ctx get exp_gn_ref_off nid get exp_gn_ref_len nid
1463exp_sha_u64 ctx get exp_gn_has_dig nid
1464if ne get exp_gn_has_dig nid 0
1465exp_sha_arena ctx get exp_gn_dig_off nid get exp_gn_dig_len nid
1466end
1467exp_sha_u64 ctx get exp_gn_has_conf nid
1468if ne get exp_gn_has_conf nid 0
1469exp_sha_u64 ctx get exp_gn_conf nid
1470end
1471exp_sha_u64 ctx get exp_gn_has_cbas nid
1472if ne get exp_gn_has_cbas nid 0
1473exp_sha_arena ctx get exp_gn_cbas_off nid get exp_gn_cbas_len nid
1474end
1475exp_sha_u64 ctx get exp_gn_has_ts nid
1476if ne get exp_gn_has_ts nid 0
1477exp_sha_u64 ctx get exp_gn_ts nid
1478end
1479end
1480
1481fn exp_hash_graph_node_lists ctx edit ulong * nid ulong to void
1482ec = get exp_g_ecnt 0
1483for e ulong ec bound 1024
1484if eq get exp_ge_owner e nid
1485exp_sha_tag ctx 2
1486exp_sha_arena ctx get exp_ge_to_off e get exp_ge_to_len e
1487exp_sha_u64 ctx get exp_ge_rel e
1488end
1489end
1490mc = get exp_g_mcnt 0
1491for m ulong mc bound 256
1492if eq get exp_gm_owner m nid
1493exp_sha_tag ctx 3
1494exp_sha_arena ctx get exp_gm_name_off m get exp_gm_name_len m
1495exp_sha_arena ctx get exp_gm_val_off m get exp_gm_val_len m
1496exp_sha_arena ctx get exp_gm_unit_off m get exp_gm_unit_len m
1497end
1498end
1499xc = get exp_g_xcnt 0
1500for x ulong xc bound 128
1501if eq get exp_gx_owner x nid
1502exp_sha_tag ctx 4
1503exp_sha_arena ctx get exp_gx_exp_off x get exp_gx_exp_len x
1504exp_sha_arena ctx get exp_gx_rsn_off x get exp_gx_rsn_len x
1505end
1506end
1507noc = get exp_g_notecnt 0
1508for o ulong noc bound 128
1509if eq get exp_go_owner o nid
1510exp_sha_tag ctx 5
1511exp_sha_arena ctx get exp_go_off o get exp_go_len o
1512end
1513end
1514end
1515
1516fn exp_compute_input_digest dst edit uchar * to void
1517ctx = new ulong 40 0
1518sha256_init ctx
1519exp_sha_arena ctx get exp_g_fmt_off 0 get exp_g_fmt_len 0
1520exp_sha_u64 ctx get exp_g_ver 0
1521exp_sha_arena ctx get exp_g_prod_off 0 get exp_g_prod_len 0
1522nc = get exp_g_ncnt 0
1523exp_sha_u64 ctx nc
1524for i ulong nc bound 320
1525exp_hash_graph_node ctx i
1526exp_hash_graph_node_lists ctx i
1527end
1528tc = get exp_g_tcnt 0
1529exp_sha_u64 ctx tc
1530for i ulong tc bound 128
1531exp_sha_arena ctx get exp_gt_off i get exp_gt_len i
1532end
1533exp_sha_u64 ctx get exp_g_has_scope 0
1534if ne get exp_g_has_scope 0 0
1535exp_sha_arena ctx get exp_g_sbr_off 0 get exp_g_sbr_len 0
1536sc = get exp_g_scnt 0
1537exp_sha_u64 ctx sc
1538for i ulong sc bound 128
1539exp_sha_arena ctx get exp_gs_off i get exp_gs_len i
1540end
1541end
1542sha256_final ctx dst
1543end
1544
1545fn exp_hash_bundle_steps ctx edit ulong * to void
1546sc = get exp_b_scnt 0
1547exp_sha_u64 ctx sc
1548for i ulong sc bound 320
1549nid = get exp_bs_nid i
1550exp_sha_tag ctx 10
1551exp_sha_u64 ctx add i 1
1552exp_hash_graph_node ctx nid
1553exp_sha_u64 ctx get exp_bs_role i
1554exp_sha_u64 ctx get exp_bs_depth i
1555exp_sha_u64 ctx get exp_bs_gnd i
1556exp_sha_u64 ctx get exp_bs_bref i
1557soff = get exp_bs_sup_off i
1558scnt = get exp_bs_sup_cnt i
1559exp_sha_u64 ctx scnt
1560for j ulong scnt bound 1024
1561exp_sha_u64 ctx get exp_bs_sup_step add soff j
1562exp_sha_u64 ctx get exp_bs_sup_rel add soff j
1563end
1564end
1565end
1566
1567fn exp_hash_bundle_ev_unc ctx edit ulong * to void
1568evc = get exp_be_cnt 0
1569exp_sha_u64 ctx evc
1570for i ulong evc bound 320
1571exp_sha_tag ctx 11
1572exp_sha_u64 ctx get exp_be_step i
1573end
1574uc = get exp_bu_cnt 0
1575exp_sha_u64 ctx uc
1576for i ulong uc bound 512
1577exp_sha_tag ctx 12
1578exp_sha_u64 ctx get exp_bu_kind i
1579exp_sha_arena ctx get exp_bu_nid_off i get exp_bu_nid_len i
1580exp_sha_u64 ctx get exp_bu_has_rel i
1581if ne get exp_bu_has_rel i 0
1582exp_sha_arena ctx get exp_bu_rel_off i get exp_bu_rel_len i
1583end
1584exp_sha_arena ctx get exp_bu_det_off i get exp_bu_det_len i
1585end
1586end
1587
1588fn exp_hash_contra_one ctx edit ulong * i ulong to void
1589exp_sha_tag ctx 13
1590nid = get exp_bc_nid i
1591exp_sha_arena ctx get exp_gn_id_off nid get exp_gn_id_len nid
1592exp_sha_arena ctx get exp_bc_stmt_off i get exp_bc_stmt_len i
1593io = get exp_bc_inv_off i
1594ic = get exp_bc_inv_cnt i
1595exp_sha_u64 ctx ic
1596for j ulong ic bound 256
1597exp_sha_arena ctx get exp_bc_inv_id_off add io j get exp_bc_inv_id_len add io j
1598end
1599end
1600
1601fn exp_hash_bundle_extras ctx edit ulong * to void
1602exp_hash_bundle_ev_unc ctx
1603cc = get exp_bc_cnt 0
1604exp_sha_u64 ctx cc
1605for i ulong cc bound 128
1606exp_hash_contra_one ctx i
1607end
1608ac = get exp_ba_cnt 0
1609exp_sha_u64 ctx ac
1610for i ulong ac bound 128
1611exp_sha_tag ctx 14
1612nid = get exp_ba_nid i
1613exp_sha_arena ctx get exp_gn_id_off nid get exp_gn_id_len nid
1614exp_sha_arena ctx get exp_ba_to_off i get exp_ba_to_len i
1615end
1616end
1617
1618fn exp_compute_bundle_digest dst edit uchar * to void
1619ctx = new ulong 40 0
1620sha256_init ctx
1621exp_sha_u64 ctx get exp_b_concl_step 0
1622exp_hash_bundle_steps ctx
1623exp_hash_bundle_extras ctx
1624exp_sha_tag ctx 15
1625exp_sha_arena ctx get exp_g_fmt_off 0 get exp_g_fmt_len 0
1626exp_sha_u64 ctx get exp_g_ver 0
1627exp_sha_arena ctx get exp_g_prod_off 0 get exp_g_prod_len 0
1628exp_sha_arena ctx get exp_bp_tgt_off 0 get exp_bp_tgt_len 0
1629exp_sha_u64 ctx get exp_bp_max_d 0
1630exp_sha_u64 ctx get exp_bp_max_n 0
1631exp_sha_u64 ctx get exp_bp_vis 0
1632exp_sha_u64 ctx get exp_bp_gnodes 0
1633indig = new uchar 32 0
1634for i ulong 32
1635put indig i get exp_bp_in_dig i
1636end
1637sha256_update ctx indig
1638sha256_final ctx dst
1639end
1640
1641fn exp_bundle_verify_digest to bool
1642dig = new uchar 32 0
1643exp_compute_bundle_digest dig
1644for i ulong 32
1645if ne get dig i get exp_bp_bun_dig i
1646ret false
1647end
1648end
1649ret true
1650end
1651
1652fn exp_bundle_get_digest dst edit uchar * to void
1653for i ulong 32
1654put dst i get exp_bp_bun_dig i
1655end
1656end
1657
1658fn exp_bundle_get_input_digest dst edit uchar * to void
1659for i ulong 32
1660put dst i get exp_bp_in_dig i
1661end
1662end
1663
1664note ============================================================================
1665note Section 8: Bounded Backtracking, Cycle Cutting & Kahn Linearization (build.rs)
1666note ============================================================================
1667
1668fn exp_add_unc uk ulong no ulong nl ulong det_o ulong det_l ulong to ulong
1669uc = get exp_bu_cnt 0
1670if ge uc EXP_CAP_UNCS
1671ret EXP_NONE
1672end
1673put exp_bu_kind uc uk
1674put exp_bu_nid_off uc no
1675put exp_bu_nid_len uc nl
1676put exp_bu_has_rel uc 0
1677put exp_bu_rel_off uc 0
1678put exp_bu_rel_len uc 0
1679put exp_bu_det_off uc det_o
1680put exp_bu_det_len uc det_l
1681put exp_bu_cnt 0 add uc 1
1682ret uc
1683end
1684
1685fn exp_unc_set_rel idx ulong ro ulong rl ulong to void
1686if eq idx EXP_NONE
1687ret
1688end
1689put exp_bu_has_rel idx 1
1690put exp_bu_rel_off idx ro
1691put exp_bu_rel_len idx rl
1692end
1693
1694fn exp_sup_gt p1 ulong p2 ulong to bool
1695r1 = get exp_w_sup_rel p1
1696r2 = get exp_w_sup_rel p2
1697if gt r1 r2
1698ret true
1699end
1700if eq r1 r2
1701ret gt exp_str_cmp get exp_w_sup_to_off p1 get exp_w_sup_to_len p1 get exp_w_sup_to_off p2 get exp_w_sup_to_len p2 0
1702end
1703ret false
1704end
1705
1706fn exp_sup_swap p1 ulong p2 ulong to void
1707t = get exp_w_sup_rel p1
1708put exp_w_sup_rel p1 get exp_w_sup_rel p2
1709put exp_w_sup_rel p2 t
1710t = get exp_w_sup_to_off p1
1711put exp_w_sup_to_off p1 get exp_w_sup_to_off p2
1712put exp_w_sup_to_off p2 t
1713t = get exp_w_sup_to_len p1
1714put exp_w_sup_to_len p1 get exp_w_sup_to_len p2
1715put exp_w_sup_to_len p2 t
1716end
1717
1718fn exp_sup_sort base ulong cnt ulong to void
1719for a ulong cnt bound 1024
1720j = a
1721while gt j 0 bound 1024
1722p1 = add base sub j 1
1723p2 = add base j
1724if exp_sup_gt p1 p2
1725exp_sup_swap p1 p2
1726j = sub j 1
1727else
1728break
1729end
1730end
1731end
1732end
1733
1734fn exp_sup_dedup base ulong cnt ulong to ulong
1735ucnt = ulong 0
1736for a ulong cnt bound 1024
1737cur = add base a
1738dup = false
1739if gt ucnt 0
1740prv = add base sub ucnt 1
1741if eq get exp_w_sup_rel prv get exp_w_sup_rel cur
1742if exp_str_eq get exp_w_sup_to_off prv get exp_w_sup_to_len prv get exp_w_sup_to_off cur get exp_w_sup_to_len cur
1743dup = true
1744end
1745end
1746end
1747if not dup
1748dst = add base ucnt
1749put exp_w_sup_rel dst get exp_w_sup_rel cur
1750put exp_w_sup_to_off dst get exp_w_sup_to_off cur
1751put exp_w_sup_to_len dst get exp_w_sup_to_len cur
1752ucnt = add ucnt 1
1753end
1754end
1755ret ucnt
1756end
1757
1758fn exp_build_support_one nid ulong to void
1759base = get exp_w_sup_total 0
1760cnt = ulong 0
1761ec = get exp_g_ecnt 0
1762for e ulong ec bound 1024
1763if eq get exp_ge_owner e nid
1764rl = get exp_ge_rel e
1765if exp_rel_is_support rl
1766pos = add base cnt
1767if lt pos EXP_CAP_EDGES
1768put exp_w_sup_rel pos rl
1769put exp_w_sup_to_off pos get exp_ge_to_off e
1770put exp_w_sup_to_len pos get exp_ge_to_len e
1771cnt = add cnt 1
1772end
1773end
1774end
1775end
1776exp_sup_sort base cnt
1777ucnt = exp_sup_dedup base cnt
1778put exp_w_sup_off nid base
1779put exp_w_sup_cnt nid ucnt
1780put exp_w_sup_total 0 add base ucnt
1781end
1782
1783note Build sorted, deduplicated support edge list for each graph node
1784fn exp_build_support_index to void
1785put exp_w_sup_total 0 0
1786nc = get exp_g_ncnt 0
1787for i ulong nc bound 320
1788exp_build_support_one i
1789end
1790end
1791
1792fn exp_emit_missing_parent nid ulong po ulong pl ulong to void
1793no = get exp_gn_id_off nid
1794nl = get exp_gn_id_len nid
1795st = exp_str_mark
1796t = exp_str_add_arena no nl
1797t = exp_str_add_view exp_s_miss_a
1798t = exp_str_add_arena po pl
1799t = exp_str_add_view exp_s_miss_b
1800ui = exp_add_unc EXP_UK_MISSING_PARENT no nl st sub exp_str_mark st
1801exp_unc_set_rel ui po pl
1802end
1803
1804fn exp_emit_trunc_builder nid ulong max_d ulong max_n ulong to void
1805no = get exp_gn_id_off nid
1806nl = get exp_gn_id_len nid
1807st = exp_str_mark
1808t = exp_str_add_view exp_s_trunc_a
1809t = exp_str_add_arena no nl
1810t = exp_str_add_view exp_s_trunc_b
1811exp_str_add_u64 max_d
1812t = exp_str_add_view exp_s_trunc_c
1813exp_str_add_u64 max_n
1814exp_str_put_b uchar 41
1815ui = exp_add_unc EXP_UK_TRUNCATED no nl st sub exp_str_mark st
1816end
1817
1818fn exp_emit_trunc_prod nid ulong to void
1819no = get exp_gn_id_off nid
1820nl = get exp_gn_id_len nid
1821st = exp_str_mark
1822t = exp_str_add_view exp_s_ptrunc_a
1823t = exp_str_add_arena no nl
1824t = exp_str_add_view exp_s_ptrunc_b
1825ui = exp_add_unc EXP_UK_TRUNCATED no nl st sub exp_str_mark st
1826end
1827
1828fn exp_bfs_expand u ulong max_d ulong max_n ulong to void
1829d = get exp_w_depth u
1830so = get exp_w_sup_off u
1831sc = get exp_w_sup_cnt u
1832for k ulong sc bound 1024
1833po = get exp_w_sup_to_off add so k
1834pl = get exp_w_sup_to_len add so k
1835pid = exp_g_find_node po pl
1836if eq pid EXP_NONE
1837if not exp_g_is_prod_truncated get exp_gn_id_off u get exp_gn_id_len u
1838exp_emit_missing_parent u po pl
1839end
1840else
1841if eq get exp_w_vis pid 0
1842vcnt = get exp_w_bfs_st 1
1843if or gt add d 1 max_d ge vcnt max_n
1844put exp_w_trunc u 1
1845else
1846qt = get exp_w_bfs_st 0
1847put exp_w_vis pid 1
1848put exp_w_depth pid add d 1
1849put exp_w_queue qt pid
1850put exp_w_bfs_st 0 add qt 1
1851put exp_w_bfs_st 1 add vcnt 1
1852end
1853end
1854end
1855end
1856end
1857
1858fn exp_build_bfs tgt ulong max_d ulong max_n ulong to ulong
1859nc = get exp_g_ncnt 0
1860for i ulong nc bound 320
1861put exp_w_vis i 0
1862put exp_w_depth i EXP_NONE
1863put exp_w_trunc i 0
1864end
1865put exp_w_vis tgt 1
1866put exp_w_depth tgt 0
1867put exp_w_queue 0 tgt
1868put exp_w_bfs_st 0 1
1869put exp_w_bfs_st 1 1
1870qh = ulong 0
1871while lt qh get exp_w_bfs_st 0 bound 320
1872u = get exp_w_queue qh
1873qh = add qh 1
1874skip = false
1875if ne u tgt
1876if exp_status_is_grounding get exp_gn_status u
1877skip = true
1878end
1879end
1880if not skip
1881exp_bfs_expand u max_d max_n
1882end
1883end
1884for i ulong nc bound 320
1885if ne get exp_w_trunc i 0
1886exp_emit_trunc_builder i max_d max_n
1887else
1888if ne get exp_w_vis i 0
1889if exp_g_is_prod_truncated get exp_gn_id_off i get exp_gn_id_len i
1890exp_emit_trunc_prod i
1891end
1892end
1893end
1894end
1895ret get exp_w_bfs_st 1
1896end
1897
1898fn exp_build_vis_edges_one nid ulong tgt ulong to void
1899put exp_w_color nid 0
1900put exp_w_ve_off nid 0
1901put exp_w_ve_cnt nid 0
1902if eq get exp_w_vis nid 0
1903ret
1904end
1905base = get exp_w_ve_total 0
1906cnt = ulong 0
1907skip = false
1908if ne nid tgt
1909if exp_status_is_grounding get exp_gn_status nid
1910skip = true
1911end
1912end
1913if not skip
1914so = get exp_w_sup_off nid
1915sc = get exp_w_sup_cnt nid
1916for k ulong sc bound 1024
1917pid = exp_g_find_node get exp_w_sup_to_off add so k get exp_w_sup_to_len add so k
1918if ne pid EXP_NONE
1919if ne get exp_w_vis pid 0
1920pos = add base cnt
1921put exp_w_ve_to pos pid
1922put exp_w_ve_rel pos get exp_w_sup_rel add so k
1923put exp_w_ve_cut pos 0
1924cnt = add cnt 1
1925end
1926end
1927end
1928end
1929put exp_w_ve_off nid base
1930put exp_w_ve_cnt nid cnt
1931put exp_w_ve_total 0 add base cnt
1932end
1933
1934fn exp_emit_cycle_edge from_n ulong to_n ulong to void
1935fo = get exp_gn_id_off from_n
1936fl = get exp_gn_id_len from_n
1937t_o = get exp_gn_id_off to_n
1938t_l = get exp_gn_id_len to_n
1939st = exp_str_mark
1940t = exp_str_add_arena fo fl
1941t = exp_str_add_view exp_s_cyc_a
1942t = exp_str_add_arena t_o t_l
1943t = exp_str_add_view exp_s_cyc_b
1944t = exp_str_add_arena fo fl
1945t = exp_str_add_view exp_s_cyc_c
1946ui = exp_add_unc EXP_UK_CYCLE fo fl st sub exp_str_mark st
1947exp_unc_set_rel ui t_o t_l
1948end
1949
1950fn exp_dfs_cut tgt ulong to void
1951sp = ulong 1
1952put exp_w_stk_n 0 tgt
1953put exp_w_stk_i 0 0
1954put exp_w_color tgt 1
1955while gt sp 0 bound 4096
1956sp = sub sp 1
1957u = get exp_w_stk_n sp
1958ei = get exp_w_stk_i sp
1959vc = get exp_w_ve_cnt u
1960if lt ei vc
1961put exp_w_stk_n sp u
1962put exp_w_stk_i sp add ei 1
1963sp = add sp 1
1964epos = add get exp_w_ve_off u ei
1965p = get exp_w_ve_to epos
1966col = get exp_w_color p
1967if eq col 0
1968put exp_w_color p 1
1969put exp_w_stk_n sp p
1970put exp_w_stk_i sp 0
1971sp = add sp 1
1972else
1973if eq col 1
1974put exp_w_ve_cut epos 1
1975end
1976end
1977else
1978put exp_w_color u 2
1979end
1980end
1981end
1982
1983note Build visited-only support edges and run 3-color iterative DFS to cut back edges
1984fn exp_build_dag_cut_cycles tgt ulong to void
1985nc = get exp_g_ncnt 0
1986put exp_w_ve_total 0 0
1987for i ulong nc bound 320
1988exp_build_vis_edges_one i tgt
1989end
1990exp_dfs_cut tgt
1991for i ulong nc bound 320
1992if ne get exp_w_vis i 0
1993vo = get exp_w_ve_off i
1994vc = get exp_w_ve_cnt i
1995for k ulong vc bound 1024
1996epos = add vo k
1997if ne get exp_w_ve_cut epos 0
1998exp_emit_cycle_edge i get exp_w_ve_to epos
1999end
2000end
2001end
2002end
2003end
2004
2005fn exp_node_less a ulong b ulong to bool
2006ra = exp_role_of get exp_gn_kind a get exp_gn_status a
2007rb = exp_role_of get exp_gn_kind b get exp_gn_status b
2008if lt ra rb
2009ret true
2010end
2011if gt ra rb
2012ret false
2013end
2014ret lt exp_str_cmp get exp_gn_id_off a get exp_gn_id_len a get exp_gn_id_off b get exp_gn_id_len b 0
2015end
2016
2017fn exp_kahn_init to void
2018nc = get exp_g_ncnt 0
2019for i ulong nc bound 320
2020put exp_w_placed i 0
2021put exp_w_pos i 0
2022rc = ulong 0
2023if ne get exp_w_vis i 0
2024vo = get exp_w_ve_off i
2025vc = get exp_w_ve_cnt i
2026for k ulong vc bound 1024
2027if eq get exp_w_ve_cut add vo k 0
2028rc = add rc 1
2029end
2030end
2031end
2032put exp_w_rem i rc
2033end
2034end
2035
2036fn exp_kahn_pick_ready tgt ulong to ulong
2037nc = get exp_g_ncnt 0
2038best = EXP_NONE
2039for i ulong nc bound 320
2040if ne get exp_w_vis i 0
2041if eq get exp_w_placed i 0
2042if ne i tgt
2043if eq get exp_w_rem i 0
2044if eq best EXP_NONE
2045best = i
2046else
2047if exp_node_less i best
2048best = i
2049end
2050end
2051end
2052end
2053end
2054end
2055end
2056ret best
2057end
2058
2059fn exp_kahn_release best ulong to void
2060nc = get exp_g_ncnt 0
2061for c ulong nc bound 320
2062if ne get exp_w_vis c 0
2063if eq get exp_w_placed c 0
2064vo = get exp_w_ve_off c
2065vc = get exp_w_ve_cnt c
2066for k ulong vc bound 1024
2067if eq get exp_w_ve_cut add vo k 0
2068if eq get exp_w_ve_to add vo k best
2069put exp_w_rem c sub get exp_w_rem c 1
2070end
2071end
2072end
2073end
2074end
2075end
2076end
2077
2078note Kahn topological sort: tie-break by (Role, node_id), target always last
2079fn exp_build_kahn tgt ulong vcnt ulong to void
2080exp_kahn_init
2081ocnt = ulong 0
2082need = sub vcnt 1
2083for iter ulong need bound 320
2084best = exp_kahn_pick_ready tgt
2085if eq best EXP_NONE
2086break
2087end
2088put exp_w_placed best 1
2089put exp_bs_nid ocnt best
2090put exp_w_pos best add ocnt 1
2091ocnt = add ocnt 1
2092exp_kahn_release best
2093end
2094put exp_bs_nid ocnt tgt
2095put exp_w_pos tgt add ocnt 1
2096ocnt = add ocnt 1
2097put exp_b_scnt 0 ocnt
2098put exp_b_concl_step 0 ocnt
2099end
2100
2101fn exp_step_sup_gt p1 ulong p2 ulong to bool
2102s1 = get exp_bs_sup_step p1
2103s2 = get exp_bs_sup_step p2
2104if gt s1 s2
2105ret true
2106end
2107if eq s1 s2
2108ret gt get exp_bs_sup_rel p1 get exp_bs_sup_rel p2
2109end
2110ret false
2111end
2112
2113fn exp_sort_step_sups base ulong cnt ulong to void
2114for a ulong cnt bound 1024
2115j = a
2116while gt j 0 bound 1024
2117p1 = add base sub j 1
2118p2 = add base j
2119if exp_step_sup_gt p1 p2
2120t = get exp_bs_sup_step p1
2121put exp_bs_sup_step p1 get exp_bs_sup_step p2
2122put exp_bs_sup_step p2 t
2123t = get exp_bs_sup_rel p1
2124put exp_bs_sup_rel p1 get exp_bs_sup_rel p2
2125put exp_bs_sup_rel p2 t
2126j = sub j 1
2127else
2128break
2129end
2130end
2131end
2132end
2133
2134fn exp_build_step_one si ulong to void
2135nid = get exp_bs_nid si
2136put exp_bs_role si exp_role_of get exp_gn_kind nid get exp_gn_status nid
2137put exp_bs_depth si get exp_w_depth nid
2138if exp_status_is_grounding get exp_gn_status nid
2139put exp_bs_gnd si 1
2140else
2141put exp_bs_gnd si 0
2142end
2143if exp_g_is_scope_ref get exp_gn_id_off nid get exp_gn_id_len nid
2144put exp_bs_bref si 1
2145else
2146put exp_bs_bref si 0
2147end
2148base = get exp_bs_sup_total 0
2149cnt = ulong 0
2150vo = get exp_w_ve_off nid
2151vc = get exp_w_ve_cnt nid
2152for k ulong vc bound 1024
2153if eq get exp_w_ve_cut add vo k 0
2154p = get exp_w_ve_to add vo k
2155pos = add base cnt
2156put exp_bs_sup_step pos get exp_w_pos p
2157put exp_bs_sup_rel pos get exp_w_ve_rel add vo k
2158cnt = add cnt 1
2159end
2160end
2161exp_sort_step_sups base cnt
2162put exp_bs_sup_off si base
2163put exp_bs_sup_cnt si cnt
2164put exp_bs_sup_total 0 add base cnt
2165end
2166
2167fn exp_build_steps to void
2168sc = get exp_b_scnt 0
2169put exp_bs_sup_total 0 0
2170for i ulong sc bound 320
2171exp_build_step_one i
2172end
2173end
2174
2175fn exp_str_add_status_name st ulong to void
2176if eq st EXP_ST_UNCLASSIFIED
2177t = exp_str_add_view exp_sn_0
2178ret
2179end
2180if eq st EXP_ST_DIRECT_OBS
2181t = exp_str_add_view exp_sn_1
2182ret
2183end
2184if eq st EXP_ST_VERIFIED_FACT
2185t = exp_str_add_view exp_sn_2
2186ret
2187end
2188if eq st EXP_ST_INFERENCE
2189t = exp_str_add_view exp_sn_3
2190ret
2191end
2192if eq st EXP_ST_HYPOTHESIS
2193t = exp_str_add_view exp_sn_4
2194ret
2195end
2196if eq st EXP_ST_CONTRADICTION
2197t = exp_str_add_view exp_sn_5
2198ret
2199end
2200t = exp_str_add_view exp_sn_6
2201end
2202
2203fn exp_step_tests_hypo onid ulong ho ulong hl ulong to bool
2204okd = get exp_gn_kind onid
2205ost = get exp_gn_status onid
2206so = get exp_w_sup_off onid
2207scnt = get exp_w_sup_cnt onid
2208if eq okd EXP_KD_EVAL_RECEIPT
2209for k ulong scnt bound 1024
2210if eq get exp_w_sup_rel add so k EXP_REL_EVALUATES
2211if exp_str_eq get exp_w_sup_to_off add so k get exp_w_sup_to_len add so k ho hl
2212ret true
2213end
2214end
2215end
2216end
2217if eq okd EXP_KD_CORTEX_RECORD
2218if eq ost EXP_ST_VERIFIED_FACT
2219for k ulong scnt bound 1024
2220if exp_str_eq get exp_w_sup_to_off add so k get exp_w_sup_to_len add so k ho hl
2221ret true
2222end
2223end
2224end
2225end
2226ret false
2227end
2228
2229fn exp_step_has_hypo_test h_nid ulong to bool
2230ho = get exp_gn_id_off h_nid
2231hl = get exp_gn_id_len h_nid
2232sc = get exp_b_scnt 0
2233for j ulong sc bound 320
2234if exp_step_tests_hypo get exp_bs_nid j ho hl
2235ret true
2236end
2237end
2238ret false
2239end
2240
2241fn exp_step_emit_mn nid ulong to bool
2242no = get exp_gn_id_off nid
2243nl = get exp_gn_id_len nid
2244has_miss = false
2245xc = get exp_g_xcnt 0
2246for x ulong xc bound 128
2247if eq get exp_gx_owner x nid
2248has_miss = true
2249eo = get exp_gx_exp_off x
2250el = get exp_gx_exp_len x
2251st = exp_str_mark
2252t = exp_str_add_view exp_s_mlink_a
2253t = exp_str_add_arena eo el
2254exp_str_put_b uchar 32
2255exp_str_put_b uchar 40
2256t = exp_str_add_arena get exp_gx_rsn_off x get exp_gx_rsn_len x
2257exp_str_put_b uchar 41
2258ui = exp_add_unc EXP_UK_MISSING_LINK no nl st sub exp_str_mark st
2259exp_unc_set_rel ui eo el
2260end
2261end
2262noc = get exp_g_notecnt 0
2263for o ulong noc bound 128
2264if eq get exp_go_owner o nid
2265ui = exp_add_unc EXP_UK_PRODUCER_NOTE no nl get exp_go_off o get exp_go_len o
2266end
2267end
2268ret has_miss
2269end
2270
2271fn exp_step_emit_st sidx ulong nid ulong has_sup bool to void
2272no = get exp_gn_id_off nid
2273nl = get exp_gn_id_len nid
2274kd = get exp_gn_kind nid
2275if eq kd EXP_KD_AGENT_BRANCH
2276ret
2277end
2278sts = get exp_gn_status nid
2279if eq sts EXP_ST_UNCLASSIFIED
2280st = exp_str_mark
2281t = exp_str_add_view exp_s_unk_a
2282exp_str_add_u64 sidx
2283t = exp_str_add_view exp_s_unk_b
2284ui = exp_add_unc EXP_UK_UNKNOWN no nl st sub exp_str_mark st
2285else
2286if or eq sts EXP_ST_INFERENCE eq sts EXP_ST_HYPOTHESIS
2287if not has_sup
2288st = exp_str_mark
2289t = exp_str_add_view exp_s_unsup_a
2290exp_str_add_u64 sidx
2291t = exp_str_add_view exp_s_unsup_b
2292exp_str_add_status_name sts
2293t = exp_str_add_view exp_s_unsup_c
2294ui = exp_add_unc EXP_UK_UNSUPPORTED no nl st sub exp_str_mark st
2295else
2296if eq sts EXP_ST_INFERENCE
2297st = exp_str_mark
2298t = exp_str_add_view exp_s_unsup_a
2299exp_str_add_u64 sidx
2300t = exp_str_add_view exp_s_inf_a
2301ui = exp_add_unc EXP_UK_INFERRED no nl st sub exp_str_mark st
2302end
2303end
2304end
2305end
2306is_hyp = false
2307if eq kd EXP_KD_HYPOTHESIS
2308is_hyp = true
2309else
2310if eq kd EXP_KD_CORTEX_RECORD
2311if eq sts EXP_ST_HYPOTHESIS
2312is_hyp = true
2313end
2314end
2315end
2316if is_hyp
2317if not exp_step_has_hypo_test nid
2318st = exp_str_mark
2319t = exp_str_add_view exp_s_hyp_a
2320exp_str_add_u64 sidx
2321t = exp_str_add_view exp_s_hyp_b
2322ui = exp_add_unc EXP_UK_HYPO_NOT_FALSIFIED no nl st sub exp_str_mark st
2323end
2324end
2325end
2326
2327fn exp_build_step_uncs to void
2328sc = get exp_b_scnt 0
2329for i ulong sc bound 320
2330nid = get exp_bs_nid i
2331has_sup = exp_step_emit_mn nid
2332if gt get exp_w_sup_cnt nid 0
2333has_sup = true
2334end
2335exp_step_emit_st add i 1 nid has_sup
2336end
2337end
2338
2339fn exp_sort_dedup_tmp cnt ulong to ulong
2340for a ulong cnt bound 128
2341j = a
2342while gt j 0 bound 128
2343p1 = sub j 1
2344if gt exp_str_cmp get exp_w_tmp_off p1 get exp_w_tmp_len p1 get exp_w_tmp_off j get exp_w_tmp_len j 0
2345o1 = get exp_w_tmp_off p1
2346l1 = get exp_w_tmp_len p1
2347put exp_w_tmp_off p1 get exp_w_tmp_off j
2348put exp_w_tmp_len p1 get exp_w_tmp_len j
2349put exp_w_tmp_off j o1
2350put exp_w_tmp_len j l1
2351j = sub j 1
2352else
2353break
2354end
2355end
2356end
2357ucnt = ulong 0
2358for a ulong cnt bound 128
2359dup = false
2360if gt ucnt 0
2361prv = sub ucnt 1
2362if exp_str_eq get exp_w_tmp_off prv get exp_w_tmp_len prv get exp_w_tmp_off a get exp_w_tmp_len a
2363dup = true
2364end
2365end
2366if not dup
2367put exp_w_tmp_off ucnt get exp_w_tmp_off a
2368put exp_w_tmp_len ucnt get exp_w_tmp_len a
2369ucnt = add ucnt 1
2370end
2371end
2372ret ucnt
2373end
2374
2375fn exp_sort_dedup_inv cnt ulong to ulong
2376for a ulong cnt bound 128
2377j = a
2378while gt j 0 bound 128
2379p1 = sub j 1
2380if gt exp_str_cmp get exp_w_inv_off p1 get exp_w_inv_len p1 get exp_w_inv_off j get exp_w_inv_len j 0
2381o1 = get exp_w_inv_off p1
2382l1 = get exp_w_inv_len p1
2383put exp_w_inv_off p1 get exp_w_inv_off j
2384put exp_w_inv_len p1 get exp_w_inv_len j
2385put exp_w_inv_off j o1
2386put exp_w_inv_len j l1
2387j = sub j 1
2388else
2389break
2390end
2391end
2392end
2393ucnt = ulong 0
2394for a ulong cnt bound 128
2395dup = false
2396if gt ucnt 0
2397prv = sub ucnt 1
2398if exp_str_eq get exp_w_inv_off prv get exp_w_inv_len prv get exp_w_inv_off a get exp_w_inv_len a
2399dup = true
2400end
2401end
2402if not dup
2403put exp_w_inv_off ucnt get exp_w_inv_off a
2404put exp_w_inv_len ucnt get exp_w_inv_len a
2405ucnt = add ucnt 1
2406end
2407end
2408ret ucnt
2409end
2410
2411fn exp_sort_nodes_by_id to void
2412nc = get exp_g_ncnt 0
2413for i ulong nc bound 320
2414put exp_w_sorted_n i i
2415end
2416for a ulong nc bound 320
2417j = a
2418while gt j 0 bound 320
2419n1 = get exp_w_sorted_n sub j 1
2420n2 = get exp_w_sorted_n j
2421if gt exp_str_cmp get exp_gn_id_off n1 get exp_gn_id_len n1 get exp_gn_id_off n2 get exp_gn_id_len n2 0
2422put exp_w_sorted_n sub j 1 n2
2423put exp_w_sorted_n j n1
2424j = sub j 1
2425else
2426break
2427end
2428end
2429end
2430end
2431
2432fn exp_contra_emit nid ulong tcnt ulong icnt ulong to void
2433stmt_o = get exp_gn_stmt_off nid
2434stmt_l = get exp_gn_stmt_len nid
2435if ne get exp_gn_status nid EXP_ST_CONTRADICTION
2436st = exp_str_mark
2437t = exp_str_add_arena get exp_gn_id_off nid get exp_gn_id_len nid
2438t = exp_str_add_view exp_s_contra_a
2439for k ulong tcnt bound 128
2440if gt k 0
2441exp_str_put_b uchar 44
2442exp_str_put_b uchar 32
2443end
2444t = exp_str_add_arena get exp_w_tmp_off k get exp_w_tmp_len k
2445end
2446exp_str_put_b uchar 58
2447exp_str_put_b uchar 32
2448t = exp_str_add_arena stmt_o stmt_l
2449stmt_o = st
2450stmt_l = sub exp_str_mark st
2451end
2452for k ulong icnt bound 128
2453io = get exp_w_inv_off k
2454il = get exp_w_inv_len k
2455oo = get exp_gn_id_off nid
2456ol = get exp_gn_id_len nid
2457if exp_str_eq io il oo ol
2458oo = get exp_w_tmp_off 0
2459ol = get exp_w_tmp_len 0
2460end
2461st = exp_str_mark
2462t = exp_str_add_arena io il
2463t = exp_str_add_view exp_s_contra_b
2464t = exp_str_add_arena stmt_o stmt_l
2465ui = exp_add_unc EXP_UK_CONFLICT io il st sub exp_str_mark st
2466exp_unc_set_rel ui oo ol
2467end
2468cc = get exp_bc_cnt 0
2469if lt cc EXP_CAP_CONTRAS
2470base = get exp_bc_inv_total 0
2471put exp_bc_nid cc nid
2472put exp_bc_stmt_off cc stmt_o
2473put exp_bc_stmt_len cc stmt_l
2474put exp_bc_inv_off cc base
2475put exp_bc_inv_cnt cc icnt
2476for k ulong icnt bound 128
2477if lt add base k EXP_CAP_CON_INV
2478put exp_bc_inv_id_off add base k get exp_w_inv_off k
2479put exp_bc_inv_id_len add base k get exp_w_inv_len k
2480end
2481end
2482put exp_bc_inv_total 0 add base icnt
2483put exp_bc_cnt 0 add cc 1
2484end
2485end
2486
2487fn exp_build_contra_node nid ulong to void
2488ec = get exp_g_ecnt 0
2489tcnt = ulong 0
2490has_alt = false
2491alt_o = ulong 0
2492alt_l = ulong 0
2493for e ulong ec bound 1024
2494if eq get exp_ge_owner e nid
2495rl = get exp_ge_rel e
2496eo = get exp_ge_to_off e
2497el = get exp_ge_to_len e
2498if eq rl EXP_REL_CONTRADICTS
2499if lt tcnt 128
2500put exp_w_tmp_off tcnt eo
2501put exp_w_tmp_len tcnt el
2502tcnt = add tcnt 1
2503end
2504else
2505if eq rl EXP_REL_ALTERNATIVE_TO
2506if not has_alt
2507tnid = exp_g_find_node eo el
2508if ne tnid EXP_NONE
2509if ne get exp_w_vis tnid 0
2510has_alt = true
2511alt_o = eo
2512alt_l = el
2513end
2514end
2515end
2516end
2517end
2518end
2519end
2520icnt = ulong 0
2521for k ulong tcnt bound 128
2522tnid = exp_g_find_node get exp_w_tmp_off k get exp_w_tmp_len k
2523if ne tnid EXP_NONE
2524if ne get exp_w_vis tnid 0
2525put exp_w_inv_off icnt get exp_w_tmp_off k
2526put exp_w_inv_len icnt get exp_w_tmp_len k
2527icnt = add icnt 1
2528end
2529end
2530end
2531if gt tcnt 0
2532if ne get exp_w_vis nid 0
2533put exp_w_inv_off icnt get exp_gn_id_off nid
2534put exp_w_inv_len icnt get exp_gn_id_len nid
2535icnt = add icnt 1
2536end
2537end
2538icnt = exp_sort_dedup_inv icnt
2539if gt icnt 0
2540tcnt = exp_sort_dedup_tmp tcnt
2541exp_contra_emit nid tcnt icnt
2542end
2543if has_alt
2544if eq get exp_gn_kind nid EXP_KD_ALTERNATIVE
2545ac = get exp_ba_cnt 0
2546if lt ac EXP_CAP_ALTS
2547put exp_ba_nid ac nid
2548put exp_ba_to_off ac alt_o
2549put exp_ba_to_len ac alt_l
2550put exp_ba_cnt 0 add ac 1
2551end
2552end
2553end
2554end
2555
2556fn exp_build_contras_and_alts to void
2557put exp_bc_cnt 0 0
2558put exp_bc_inv_total 0 0
2559put exp_ba_cnt 0 0
2560exp_sort_nodes_by_id
2561nc = get exp_g_ncnt 0
2562for i ulong nc bound 320
2563nid = get exp_w_sorted_n i
2564exp_build_contra_node nid
2565end
2566end
2567
2568fn exp_unc_cmp a ulong b ulong to int
2569ka = get exp_bu_kind a
2570kb = get exp_bu_kind b
2571if lt ka kb
2572ret -1
2573end
2574if gt ka kb
2575ret 1
2576end
2577c1 = exp_str_cmp get exp_bu_nid_off a get exp_bu_nid_len a get exp_bu_nid_off b get exp_bu_nid_len b
2578if ne c1 0
2579ret c1
2580end
2581ha = get exp_bu_has_rel a
2582hb = get exp_bu_has_rel b
2583if lt ha hb
2584ret -1
2585end
2586if gt ha hb
2587ret 1
2588end
2589if ne ha 0
2590c2 = exp_str_cmp get exp_bu_rel_off a get exp_bu_rel_len a get exp_bu_rel_off b get exp_bu_rel_len b
2591if ne c2 0
2592ret c2
2593end
2594end
2595ret exp_str_cmp get exp_bu_det_off a get exp_bu_det_len a get exp_bu_det_off b get exp_bu_det_len b
2596end
2597
2598fn exp_unc_swap a ulong b ulong to void
2599t0 = get exp_bu_kind a
2600put exp_bu_kind a get exp_bu_kind b
2601put exp_bu_kind b t0
2602t0 = get exp_bu_nid_off a
2603put exp_bu_nid_off a get exp_bu_nid_off b
2604put exp_bu_nid_off b t0
2605t0 = get exp_bu_nid_len a
2606put exp_bu_nid_len a get exp_bu_nid_len b
2607put exp_bu_nid_len b t0
2608t0 = get exp_bu_has_rel a
2609put exp_bu_has_rel a get exp_bu_has_rel b
2610put exp_bu_has_rel b t0
2611t0 = get exp_bu_rel_off a
2612put exp_bu_rel_off a get exp_bu_rel_off b
2613put exp_bu_rel_off b t0
2614t0 = get exp_bu_rel_len a
2615put exp_bu_rel_len a get exp_bu_rel_len b
2616put exp_bu_rel_len b t0
2617t0 = get exp_bu_det_off a
2618put exp_bu_det_off a get exp_bu_det_off b
2619put exp_bu_det_off b t0
2620t0 = get exp_bu_det_len a
2621put exp_bu_det_len a get exp_bu_det_len b
2622put exp_bu_det_len b t0
2623end
2624
2625fn exp_unc_copy dst ulong src ulong to void
2626put exp_bu_kind dst get exp_bu_kind src
2627put exp_bu_nid_off dst get exp_bu_nid_off src
2628put exp_bu_nid_len dst get exp_bu_nid_len src
2629put exp_bu_has_rel dst get exp_bu_has_rel src
2630put exp_bu_rel_off dst get exp_bu_rel_off src
2631put exp_bu_rel_len dst get exp_bu_rel_len src
2632put exp_bu_det_off dst get exp_bu_det_off src
2633put exp_bu_det_len dst get exp_bu_det_len src
2634end
2635
2636fn exp_sort_dedup_uncs to void
2637uc = get exp_bu_cnt 0
2638for a ulong uc bound 512
2639j = a
2640while gt j 0 bound 512
2641p1 = sub j 1
2642if gt exp_unc_cmp p1 j 0
2643exp_unc_swap p1 j
2644j = sub j 1
2645else
2646break
2647end
2648end
2649end
2650outc = ulong 0
2651for a ulong uc bound 512
2652dup = false
2653if gt outc 0
2654if eq exp_unc_cmp sub outc 1 a 0
2655dup = true
2656end
2657end
2658if not dup
2659if ne outc a
2660exp_unc_copy outc a
2661end
2662outc = add outc 1
2663end
2664end
2665put exp_bu_cnt 0 outc
2666end
2667
2668fn exp_node_has_meas nid ulong to bool
2669mc = get exp_g_mcnt 0
2670for m ulong mc bound 256
2671if eq get exp_gm_owner m nid
2672ret true
2673end
2674end
2675ret false
2676end
2677
2678fn exp_build_evidence to void
2679put exp_be_cnt 0 0
2680sc = get exp_b_scnt 0
2681for i ulong sc bound 320
2682nid = get exp_bs_nid i
2683keep = false
2684if ne get exp_gn_has_dig nid 0
2685keep = true
2686else
2687if exp_node_has_meas nid
2688keep = true
2689end
2690end
2691if keep
2692ec = get exp_be_cnt 0
2693put exp_be_step ec add i 1
2694put exp_be_cnt 0 add ec 1
2695end
2696end
2697end
2698
2699fn exp_build_bundle_raw tgt_o ulong tgt_l ulong max_d ulong max_n ulong to int
2700rc = exp_check_limits max_d max_n
2701if ne rc EXP_OK
2702ret rc
2703end
2704rc = exp_g_validate
2705if ne rc EXP_OK
2706ret rc
2707end
2708tgt = exp_g_find_node tgt_o tgt_l
2709if eq tgt EXP_NONE
2710ret EXP_ERR_TARGET_NOT_FOUND
2711end
2712put exp_bu_cnt 0 0
2713exp_build_support_index
2714vcnt = exp_build_bfs tgt max_d max_n
2715exp_build_dag_cut_cycles tgt
2716exp_build_kahn tgt vcnt
2717exp_build_steps
2718exp_build_step_uncs
2719exp_build_contras_and_alts
2720exp_build_evidence
2721exp_sort_dedup_uncs
2722put exp_bp_tgt_off 0 tgt_o
2723put exp_bp_tgt_len 0 tgt_l
2724put exp_bp_max_d 0 max_d
2725put exp_bp_max_n 0 max_n
2726put exp_bp_vis 0 vcnt
2727put exp_bp_gnodes 0 get exp_g_ncnt 0
2728idig = new uchar 32 0
2729exp_compute_input_digest idig
2730for i ulong 32
2731put exp_bp_in_dig i get idig i
2732end
2733bdig = new uchar 32 0
2734exp_compute_bundle_digest bdig
2735for i ulong 32
2736put exp_bp_bun_dig i get bdig i
2737end
2738ret EXP_OK
2739end
2740
2741fn exp_build_bundle target view uchar * max_d ulong max_n ulong to int
2742tgt_o = exp_str_add_view target
2743ret exp_build_bundle_raw tgt_o len target max_d max_n
2744end
2745
2746note Bundle query helpers
2747fn exp_bundle_has_unc uk ulong to bool
2748uc = get exp_bu_cnt 0
2749for i ulong uc bound 512
2750if eq get exp_bu_kind i uk
2751ret true
2752end
2753end
2754ret false
2755end
2756
2757fn exp_bundle_step_eq idx1 ulong want view uchar * to bool
2758if eq idx1 0
2759ret false
2760end
2761if gt idx1 get exp_b_scnt 0
2762ret false
2763end
2764nid = get exp_bs_nid sub idx1 1
2765ret exp_str_eq_view get exp_gn_id_off nid get exp_gn_id_len nid want
2766end
2767
2768fn exp_bundle_find_step id view uchar * to ulong
2769sc = get exp_b_scnt 0
2770for i ulong sc bound 320
2771nid = get exp_bs_nid i
2772if exp_str_eq_view get exp_gn_id_off nid get exp_gn_id_len nid id
2773ret add i 1
2774end
2775end
2776ret 0
2777end
2778
2779note ============================================================================
2780note Section 9: Cortex Store, Sealed Journal & Agent Branch Adapters (adapters.rs)
2781note ============================================================================
2782
2783fn exp_cx_reset to void
2784put exp_cx_str_len 0 0
2785put exp_cx_cnt 0 0
2786put exp_cx_seq 0 1
2787put exp_cx_pcnt 0 0
2788end
2789
2790fn exp_cx_str_add_view s view uchar * to ulong
2791start = get exp_cx_str_len 0
2792n = len s
2793for i ulong n bound 1024
2794p = get exp_cx_str_len 0
2795if lt p 16384
2796put exp_cx_str p get s i
2797put exp_cx_str_len 0 add p 1
2798end
2799end
2800ret start
2801end
2802
2803fn exp_str_add_cx_arena off ulong ln ulong to ulong
2804start = get exp_str_len 0
2805for i ulong ln bound 1024
2806exp_str_put_b get exp_cx_str add off i
2807end
2808ret start
2809end
2810
2811fn exp_cx_id_eq a_base ulong b_base ulong to bool
2812for i ulong 16
2813if ne get exp_cx_id add a_base i get exp_cx_id add b_base i
2814ret false
2815end
2816end
2817ret true
2818end
2819
2820fn exp_cx_id_eq_view base ulong v view uchar * to bool
2821for i ulong 16
2822if ne get exp_cx_id add base i get v i
2823ret false
2824end
2825end
2826ret true
2827end
2828
2829fn exp_cx_id_cmp_view a view uchar * b view uchar * to int
2830for i ulong 16
2831ca = get a i
2832cb = get b i
2833if lt ca cb
2834ret -1
2835end
2836if gt ca cb
2837ret 1
2838end
2839end
2840ret 0
2841end
2842
2843fn exp_cx_find id view uchar * to ulong
2844cnt = get exp_cx_cnt 0
2845for i ulong cnt bound 64
2846if exp_cx_id_eq_view mul i 16 id
2847ret i
2848end
2849end
2850ret EXP_NONE
2851end
2852
2853fn exp_cx_make_id seq ulong stmt view uchar * dst edit uchar * to void
2854ctx = new ulong 40 0
2855dig = new uchar 32 0
2856sha256_init ctx
2857exp_sha_u64 ctx seq
2858sha256_update ctx stmt
2859sha256_final ctx dig
2860for i ulong 16
2861put dst i get dig i
2862end
2863end
2864
2865fn exp_cx_add_rec status ulong stmt view uchar * prov view uchar * to ulong
2866idx = get exp_cx_cnt 0
2867if ge idx EXP_CX_CAP
2868ret EXP_NONE
2869end
2870seq = get exp_cx_seq 0
2871put exp_cx_seq 0 add seq 1
2872id = new uchar 16 0
2873exp_cx_make_id seq stmt id
2874base16 = mul idx 16
2875for i ulong 16
2876put exp_cx_id add base16 i get id i
2877end
2878base32 = mul idx 32
2879for i ulong 32
2880put exp_cx_evhash add base32 i 0
2881end
2882put exp_cx_status idx status
2883so = exp_cx_str_add_view stmt
2884po = exp_cx_str_add_view prov
2885put exp_cx_stmt_off idx so
2886put exp_cx_stmt_len idx len stmt
2887put exp_cx_prov_off idx po
2888put exp_cx_prov_len idx len prov
2889put exp_cx_ts idx seq
2890put exp_cx_has_conf idx 0
2891put exp_cx_conf idx 0
2892put exp_cx_has_vby idx 0
2893put exp_cx_vby_off idx 0
2894put exp_cx_vby_len idx 0
2895put exp_cx_cnt 0 add idx 1
2896ret idx
2897end
2898
2899fn exp_cx_set_ev idx ulong ev view uchar * to void
2900if ge idx get exp_cx_cnt 0
2901ret
2902end
2903evh = new uchar 32 0
2904dl = sha2_digest 256 ev evh
2905base32 = mul idx 32
2906for i ulong 32
2907put exp_cx_evhash add base32 i get evh i
2908end
2909end
2910
2911fn exp_cx_set_id idx ulong id view uchar * to void
2912if ge idx get exp_cx_cnt 0
2913ret
2914end
2915base16 = mul idx 16
2916for i ulong 16
2917put exp_cx_id add base16 i get id i
2918end
2919end
2920
2921fn exp_cx_get_id idx ulong dst edit uchar * to void
2922base16 = mul idx 16
2923for i ulong 16
2924put dst i get exp_cx_id add base16 i
2925end
2926end
2927
2928fn exp_cx_set_meta idx ulong ts ulong has_c ulong conf ulong to void
2929if ge idx get exp_cx_cnt 0
2930ret
2931end
2932put exp_cx_ts idx ts
2933put exp_cx_has_conf idx has_c
2934put exp_cx_conf idx conf
2935end
2936
2937fn exp_cx_set_verified_by idx ulong vby view uchar * to void
2938if ge idx get exp_cx_cnt 0
2939ret
2940end
2941vo = exp_cx_str_add_view vby
2942put exp_cx_has_vby idx 1
2943put exp_cx_vby_off idx vo
2944put exp_cx_vby_len idx len vby
2945end
2946
2947fn exp_cx_add_parent idx ulong pid view uchar * to void
2948pc = get exp_cx_pcnt 0
2949if ge pc EXP_CX_PAR_CAP
2950ret
2951end
2952put exp_cx_p_own pc idx
2953base = mul pc 16
2954for i ulong 16
2955put exp_cx_p_id add base i get pid i
2956end
2957put exp_cx_pcnt 0 add pc 1
2958end
2959
2960fn exp_cx_node_id_str id view uchar * to ulong
2961st = exp_str_mark
2962t0 = exp_str_add_view exp_txt_cx_pfx
2963exp_str_add_hex16_view id
2964ret st
2965end
2966
2967fn exp_cx_emit_edges ridx ulong nid ulong sts ulong to void
2968rel = EXP_REL_SUPPORTED_BY
2969if eq sts EXP_ST_CONTRADICTION
2970rel = EXP_REL_CONTRADICTS
2971end
2972pc = get exp_cx_pcnt 0
2973pid = new uchar 16 0
2974for p ulong pc bound 256
2975if eq get exp_cx_p_own p ridx
2976base = mul p 16
2977for k ulong 16
2978put pid k get exp_cx_p_id add base k
2979end
2980po = exp_cx_node_id_str pid
2981exp_g_add_edge_raw nid po 39 rel
2982end
2983end
2984end
2985
2986fn exp_cx_emit_node ridx ulong to ulong
2987id = new uchar 16 0
2988exp_cx_get_id ridx id
2989id_o = exp_cx_node_id_str id
2990sts = get exp_cx_status ridx
2991nid = exp_g_add_node_raw id_o 39 EXP_KD_CORTEX_RECORD sts
2992if eq nid EXP_NONE
2993ret EXP_NONE
2994end
2995t = exp_str_add_cx_arena get exp_cx_stmt_off ridx get exp_cx_stmt_len ridx
2996put exp_gn_stmt_off nid t
2997put exp_gn_stmt_len nid get exp_cx_stmt_len ridx
2998t = exp_str_add_view exp_txt_cx_sys
2999put exp_gn_sys_off nid t
3000put exp_gn_sys_len nid len exp_txt_cx_sys
3001t = exp_str_add_view exp_txt_cx_art
3002put exp_gn_art_off nid t
3003put exp_gn_art_len nid len exp_txt_cx_art
3004t = exp_str_add_cx_arena get exp_cx_prov_off ridx get exp_cx_prov_len ridx
3005put exp_gn_ref_off nid t
3006put exp_gn_ref_len nid get exp_cx_prov_len ridx
3007t = exp_str_mark
3008exp_str_add_hex32_cx mul ridx 32
3009put exp_gn_has_dig nid 1
3010put exp_gn_dig_off nid t
3011put exp_gn_dig_len nid 64
3012if ne get exp_cx_has_conf ridx 0
3013put exp_gn_has_conf nid 1
3014put exp_gn_conf nid get exp_cx_conf ridx
3015t = exp_str_add_view exp_txt_cx_cbas
3016put exp_gn_has_cbas nid 1
3017put exp_gn_cbas_off nid t
3018put exp_gn_cbas_len nid len exp_txt_cx_cbas
3019end
3020put exp_gn_has_ts nid 1
3021put exp_gn_ts nid get exp_cx_ts ridx
3022if ne get exp_cx_has_vby ridx 0
3023ns = exp_str_mark
3024t = exp_str_add_view exp_txt_vby_pfx
3025t = exp_str_add_cx_arena get exp_cx_vby_off ridx get exp_cx_vby_len ridx
3026nc = get exp_g_notecnt 0
3027if lt nc EXP_CAP_NOTES
3028put exp_go_owner nc nid
3029put exp_go_off nc ns
3030put exp_go_len nc sub exp_str_mark ns
3031put exp_g_notecnt 0 add nc 1
3032end
3033end
3034exp_cx_emit_edges ridx nid sts
3035ret nid
3036end
3037
3038fn exp_cx_contra_touches ci ulong to bool
3039pc = get exp_cx_pcnt 0
3040pid = new uchar 16 0
3041for p ulong pc bound 256
3042if eq get exp_cx_p_own p ci
3043base = mul p 16
3044for j ulong 16
3045put pid j get exp_cx_p_id add base j
3046end
3047pi = exp_cx_find pid
3048if ne pi EXP_NONE
3049if ne get exp_cxw_vis pi 0
3050ret true
3051end
3052end
3053end
3054end
3055ret false
3056end
3057
3058fn exp_cx_mark_contra_trunc ci ulong to void
3059pc = get exp_cx_pcnt 0
3060pid = new uchar 16 0
3061for p ulong pc bound 256
3062if eq get exp_cx_p_own p ci
3063base = mul p 16
3064for j ulong 16
3065put pid j get exp_cx_p_id add base j
3066end
3067pi = exp_cx_find pid
3068if ne pi EXP_NONE
3069if ne get exp_cxw_vis pi 0
3070put exp_cxw_trunc pi 1
3071end
3072end
3073end
3074end
3075end
3076
3077fn exp_cx_bfs_disclose mn ulong to void
3078cnt = get exp_cx_cnt 0
3079extra = ulong 0
3080for ci ulong cnt bound 64
3081if eq get exp_cx_status ci EXP_ST_CONTRADICTION
3082if eq get exp_cxw_emit ci 0
3083if exp_cx_contra_touches ci
3084if ge extra mn
3085exp_cx_mark_contra_trunc ci
3086else
3087extra = add extra 1
3088put exp_cxw_emit ci 1
3089dn = exp_cx_emit_node ci
3090end
3091end
3092end
3093end
3094end
3095end
3096
3097fn exp_cx_seed_roots roots view ulong * mn ulong to void
3098cnt = get exp_cx_cnt 0
3099rcnt = len roots
3100for k ulong rcnt bound 64
3101ri = get roots k
3102if lt ri cnt
3103put exp_cxw_isroot ri 1
3104if eq get exp_cxw_vis ri 0
3105vcnt = get exp_w_bfs_st 1
3106if ge vcnt mn
3107for m ulong cnt bound 64
3108if ne get exp_cxw_vis m 0
3109put exp_cxw_trunc m 1
3110end
3111end
3112break
3113end
3114qt = get exp_w_bfs_st 0
3115put exp_cxw_vis ri 1
3116put exp_cxw_depth ri 0
3117put exp_cxw_q qt ri
3118put exp_w_bfs_st 0 add qt 1
3119put exp_w_bfs_st 1 add vcnt 1
3120end
3121end
3122end
3123end
3124
3125fn exp_cx_bfs_expand u ulong md ulong mn ulong to void
3126d = get exp_cxw_depth u
3127pc = get exp_cx_pcnt 0
3128pid = new uchar 16 0
3129for p ulong pc bound 256
3130if eq get exp_cx_p_own p u
3131base = mul p 16
3132for j ulong 16
3133put pid j get exp_cx_p_id add base j
3134end
3135pi = exp_cx_find pid
3136if ne pi EXP_NONE
3137if eq get exp_cxw_vis pi 0
3138vcnt = get exp_w_bfs_st 1
3139if or gt add d 1 md ge vcnt mn
3140put exp_cxw_trunc u 1
3141else
3142qt = get exp_w_bfs_st 0
3143put exp_cxw_vis pi 1
3144put exp_cxw_depth pi add d 1
3145put exp_cxw_q qt pi
3146put exp_w_bfs_st 0 add qt 1
3147put exp_w_bfs_st 1 add vcnt 1
3148end
3149end
3150end
3151end
3152end
3153end
3154
3155fn exp_cx_graph_from_roots roots view ulong * max_d ulong max_n ulong to void
3156exp_g_reset_prod exp_txt_cx_prod
3157cnt = get exp_cx_cnt 0
3158for i ulong cnt bound 64
3159put exp_cxw_vis i 0
3160put exp_cxw_depth i EXP_NONE
3161put exp_cxw_trunc i 0
3162put exp_cxw_isroot i 0
3163put exp_cxw_emit i 0
3164end
3165md = add max_d 1
3166mn = add max_n 1
3167put exp_w_bfs_st 0 0
3168put exp_w_bfs_st 1 0
3169exp_cx_seed_roots roots mn
3170ocnt = ulong 0
3171qh = ulong 0
3172while lt qh get exp_w_bfs_st 0 bound 64
3173u = get exp_cxw_q qh
3174qh = add qh 1
3175put exp_cxw_ord ocnt u
3176ocnt = add ocnt 1
3177st = get exp_cx_status u
3178skip = false
3179if eq st EXP_ST_CONTRADICTION
3180skip = true
3181else
3182if exp_status_is_grounding st
3183if eq get exp_cxw_isroot u 0
3184skip = true
3185end
3186end
3187end
3188if not skip
3189exp_cx_bfs_expand u md mn
3190end
3191end
3192for k ulong ocnt bound 64
3193ri = get exp_cxw_ord k
3194if eq get exp_cxw_emit ri 0
3195put exp_cxw_emit ri 1
3196dn = exp_cx_emit_node ri
3197end
3198end
3199exp_cx_bfs_disclose mn
3200pid = new uchar 16 0
3201for i ulong cnt bound 64
3202if ne get exp_cxw_trunc i 0
3203exp_cx_get_id i pid
3204po = exp_cx_node_id_str pid
3205exp_g_add_truncated_raw po 39
3206end
3207end
3208end
3209
3210fn exp_cx_graph_for_record id view uchar * max_d ulong max_n ulong to int
3211ri = exp_cx_find id
3212if eq ri EXP_NONE
3213ret EXP_ERR_TARGET_NOT_FOUND
3214end
3215roots = new ulong 1 0
3216put roots 0 ri
3217exp_cx_graph_from_roots roots max_d max_n
3218ret EXP_OK
3219end
3220
3221note Sealed Cortex journal export and tamper-detecting import
3222fn exp_j_put_u64 dst edit uchar * off ulong v ulong to void
3223for i ulong 8
3224sh = mul sub 7 i 8
3225put dst add off i uchar and shr v sh 0xff
3226end
3227end
3228
3229fn exp_j_get_u64 src view uchar * off ulong to ulong
3230acc = ulong 0
3231for i ulong 8
3232acc = or shl acc 8 ulong get src add off i
3233end
3234ret acc
3235end
3236
3237fn exp_j_put_cx_str dst edit uchar * pos ulong off ulong ln ulong to ulong
3238exp_j_put_u64 dst pos ln
3239pos = add pos 8
3240for k ulong ln bound 512
3241put dst add pos k get exp_cx_str add off k
3242end
3243ret add pos ln
3244end
3245
3246fn exp_j_get_cx_str src view uchar * pos ulong ln ulong to ulong
3247so = get exp_cx_str_len 0
3248for k ulong ln bound 512
3249p = get exp_cx_str_len 0
3250if lt p 16384
3251put exp_cx_str p get src add pos k
3252put exp_cx_str_len 0 add p 1
3253end
3254end
3255ret so
3256end
3257
3258fn exp_cx_export_rec dst edit uchar * pos ulong i ulong to ulong
3259if gt add pos add add 136 get exp_cx_stmt_len i get exp_cx_prov_len i len dst
3260ret 0
3261end
3262base = mul i 16
3263for k ulong 16
3264put dst add pos k get exp_cx_id add base k
3265end
3266pos = add pos 16
3267base = mul i 32
3268for k ulong 32
3269put dst add pos k get exp_cx_evhash add base k
3270end
3271pos = add pos 32
3272exp_j_put_u64 dst pos get exp_cx_status i
3273pos = add pos 8
3274exp_j_put_u64 dst pos get exp_cx_ts i
3275pos = add pos 8
3276exp_j_put_u64 dst pos get exp_cx_has_conf i
3277pos = add pos 8
3278exp_j_put_u64 dst pos get exp_cx_conf i
3279pos = add pos 8
3280pos = exp_j_put_cx_str dst pos get exp_cx_stmt_off i get exp_cx_stmt_len i
3281ret exp_j_put_cx_str dst pos get exp_cx_prov_off i get exp_cx_prov_len i
3282end
3283
3284fn exp_cx_export_journal dst edit uchar * to ulong
3285cap = len dst
3286if lt cap 64
3287ret 0
3288end
3289for i ulong 4
3290put dst i get exp_txt_jmagic i
3291end
3292exp_j_put_u64 dst 4 1
3293cnt = get exp_cx_cnt 0
3294pc = get exp_cx_pcnt 0
3295exp_j_put_u64 dst 12 cnt
3296exp_j_put_u64 dst 20 pc
3297pos = ulong 28
3298for i ulong cnt bound 64
3299pos = exp_cx_export_rec dst pos i
3300if eq pos 0
3301ret 0
3302end
3303end
3304for p ulong pc bound 256
3305if gt add add pos 24 32 cap
3306ret 0
3307end
3308exp_j_put_u64 dst pos get exp_cx_p_own p
3309pos = add pos 8
3310base = mul p 16
3311for k ulong 16
3312put dst add pos k get exp_cx_p_id add base k
3313end
3314pos = add pos 16
3315end
3316seal_dig = new uchar 32 0
3317dl = sha2_digest 256 part dst 0 pos seal_dig
3318for k ulong 32
3319put dst add pos k get seal_dig k
3320end
3321ret add pos 32
3322end
3323
3324fn exp_cx_import_rec src view uchar * pos ulong body_len ulong i ulong to ulong
3325if gt add pos 96 body_len
3326ret 0
3327end
3328base = mul i 16
3329for k ulong 16
3330put exp_cx_id add base k get src add pos k
3331end
3332pos = add pos 16
3333base = mul i 32
3334for k ulong 32
3335put exp_cx_evhash add base k get src add pos k
3336end
3337pos = add pos 32
3338put exp_cx_status i exp_j_get_u64 src pos
3339pos = add pos 8
3340put exp_cx_ts i exp_j_get_u64 src pos
3341pos = add pos 8
3342put exp_cx_has_conf i exp_j_get_u64 src pos
3343pos = add pos 8
3344put exp_cx_conf i exp_j_get_u64 src pos
3345pos = add pos 8
3346ln = exp_j_get_u64 src pos
3347pos = add pos 8
3348if gt add pos ln body_len
3349ret 0
3350end
3351put exp_cx_stmt_off i exp_j_get_cx_str src pos ln
3352put exp_cx_stmt_len i ln
3353pos = add pos ln
3354if gt add pos 8 body_len
3355ret 0
3356end
3357ln = exp_j_get_u64 src pos
3358pos = add pos 8
3359if gt add pos ln body_len
3360ret 0
3361end
3362put exp_cx_prov_off i exp_j_get_cx_str src pos ln
3363put exp_cx_prov_len i ln
3364put exp_cx_has_vby i 0
3365ret add pos ln
3366end
3367
3368fn exp_cx_import_par src view uchar * pos ulong body_len ulong p ulong to ulong
3369if gt add pos 24 body_len
3370ret 0
3371end
3372put exp_cx_p_own p exp_j_get_u64 src pos
3373pos = add pos 8
3374base = mul p 16
3375for k ulong 16
3376put exp_cx_p_id add base k get src add pos k
3377end
3378ret add pos 16
3379end
3380
3381fn exp_cx_journal_seal_ok src view uchar * to bool
3382n = len src
3383if lt n 60
3384ret false
3385end
3386for i ulong 4
3387if ne get src i get exp_txt_jmagic i
3388ret false
3389end
3390end
3391if ne exp_j_get_u64 src 4 1
3392ret false
3393end
3394body_len = sub n 32
3395chk = new uchar 32 0
3396dl = sha2_digest 256 part src 0 body_len chk
3397for i ulong 32
3398if ne get chk i get src add body_len i
3399ret false
3400end
3401end
3402ret true
3403end
3404
3405fn exp_cx_import_journal src view uchar * to int
3406if not exp_cx_journal_seal_ok src
3407ret EXP_ERR_JOURNAL
3408end
3409body_len = sub len src 32
3410cnt = exp_j_get_u64 src 12
3411pc = exp_j_get_u64 src 20
3412if or gt cnt EXP_CX_CAP gt pc EXP_CX_PAR_CAP
3413ret EXP_ERR_JOURNAL
3414end
3415exp_cx_reset
3416pos = ulong 28
3417for i ulong cnt bound 64
3418pos = exp_cx_import_rec src pos body_len i
3419if eq pos 0
3420ret EXP_ERR_JOURNAL
3421end
3422end
3423for p ulong pc bound 256
3424pos = exp_cx_import_par src pos body_len p
3425if eq pos 0
3426ret EXP_ERR_JOURNAL
3427end
3428end
3429put exp_cx_cnt 0 cnt
3430put exp_cx_pcnt 0 pc
3431ret EXP_OK
3432end
3433
3434note Agent branch adapter
3435fn exp_br_reset to void
3436put exp_br_cnt 0 0
3437put exp_br_rcnt 0 0
3438put exp_br_tcnt 0 0
3439end
3440
3441fn exp_br_create id view uchar * depth ulong ts ulong to ulong
3442idx = get exp_br_cnt 0
3443if ge idx EXP_BR_CAP
3444ret EXP_NONE
3445end
3446base = mul idx 16
3447for i ulong 16
3448put exp_br_id add base i get id i
3449end
3450put exp_br_has_par idx 0
3451put exp_br_depth idx depth
3452put exp_br_ts idx ts
3453put exp_br_cnt 0 add idx 1
3454ret idx
3455end
3456
3457fn exp_br_set_parent bidx ulong pid view uchar * to void
3458if ge bidx get exp_br_cnt 0
3459ret
3460end
3461base = mul bidx 16
3462for i ulong 16
3463put exp_br_par_id add base i get pid i
3464end
3465put exp_br_has_par bidx 1
3466end
3467
3468fn exp_br_add_ref bidx ulong rec_id view uchar * to void
3469rc = get exp_br_rcnt 0
3470if ge rc EXP_BR_REF_CAP
3471ret
3472end
3473put exp_br_r_own rc bidx
3474base = mul rc 16
3475for i ulong 16
3476put exp_br_r_id add base i get rec_id i
3477end
3478put exp_br_rcnt 0 add rc 1
3479end
3480
3481fn exp_br_append_token bidx ulong tok ulong to void
3482tc = get exp_br_tcnt 0
3483if ge tc EXP_BR_TOK_CAP
3484ret
3485end
3486put exp_br_t_own tc bidx
3487put exp_br_t_val tc tok
3488put exp_br_tcnt 0 add tc 1
3489end
3490
3491fn exp_br_node_id_str bidx ulong to ulong
3492st = exp_str_mark
3493t0 = exp_str_add_view exp_txt_br_pfx
3494exp_str_add_hex16 mul bidx 16 2
3495ret st
3496end
3497
3498fn exp_br_emit_refs bidx ulong bnid ulong to void
3499tot = get exp_br_rcnt 0
3500rid = new uchar 16 0
3501for i ulong tot bound 64
3502if eq get exp_br_r_own i bidx
3503rbase = mul i 16
3504for k ulong 16
3505put rid k get exp_br_r_id add rbase k
3506end
3507co = exp_cx_node_id_str rid
3508exp_g_add_edge_raw bnid co 39 EXP_REL_REFERENCES
3509exp_g_add_scope_ref_raw co 39
3510end
3511end
3512end
3513
3514fn exp_br_emit_node bidx ulong to ulong
3515bo = exp_br_node_id_str bidx
3516bnid = exp_g_add_node_raw bo 39 EXP_KD_AGENT_BRANCH EXP_ST_UNCLASSIFIED
3517st = exp_str_mark
3518t = exp_str_add_view exp_s_br_a
3519base = mul bidx 16
3520for i ulong 8
3521b = ulong get exp_br_id add base i
3522exp_str_put_b get exp_txt_hex shr b 4
3523exp_str_put_b get exp_txt_hex and b 15
3524end
3525t = exp_str_add_view exp_s_br_b
3526exp_str_add_u64 get exp_br_depth bidx
3527t = exp_str_add_view exp_s_br_c
3528if ne get exp_br_has_par bidx 0
3529for i ulong 8
3530b = ulong get exp_br_par_id add base i
3531exp_str_put_b get exp_txt_hex shr b 4
3532exp_str_put_b get exp_txt_hex and b 15
3533end
3534else
3535t = exp_str_add_view exp_s_br_root
3536end
3537t = exp_str_add_view exp_s_br_d
3538ref_total = ulong 0
3539tot = get exp_br_rcnt 0
3540for i ulong tot bound 64
3541if eq get exp_br_r_own i bidx
3542ref_total = add ref_total 1
3543end
3544end
3545exp_str_add_u64 ref_total
3546t = exp_str_add_view exp_s_br_e
3547put exp_gn_stmt_off bnid st
3548put exp_gn_stmt_len bnid sub exp_str_mark st
3549t = exp_str_add_view exp_txt_br_sys
3550put exp_gn_sys_off bnid t
3551put exp_gn_sys_len bnid len exp_txt_br_sys
3552t = exp_str_add_view exp_txt_br_art
3553put exp_gn_art_off bnid t
3554put exp_gn_art_len bnid len exp_txt_br_art
3555t = exp_str_mark
3556exp_str_add_hex16 mul bidx 16 2
3557put exp_gn_ref_off bnid t
3558put exp_gn_ref_len bnid 32
3559put exp_gn_has_ts bnid 1
3560put exp_gn_ts bnid get exp_br_ts bidx
3561exp_g_set_scope_branch bo 39
3562exp_br_emit_refs bidx bnid
3563ret bnid
3564end
3565
3566fn exp_br_build_graph bidx ulong max_d ulong max_n ulong to ulong
3567roots = new ulong 64 0
3568rcnt = ulong 0
3569tot = get exp_br_rcnt 0
3570rid = new uchar 16 0
3571for i ulong tot bound 64
3572if eq get exp_br_r_own i bidx
3573base = mul i 16
3574for k ulong 16
3575put rid k get exp_br_r_id add base k
3576end
3577ri = exp_cx_find rid
3578if ne ri EXP_NONE
3579put roots rcnt ri
3580rcnt = add rcnt 1
3581end
3582end
3583end
3584exp_cx_graph_from_roots part roots 0 rcnt max_d max_n
3585exp_g_set_producer exp_txt_br_prod
3586ret exp_br_emit_node bidx
3587end
3588
3589fn exp_br_scope_expand u ulong to void
3590ec = get exp_g_ecnt 0
3591for e ulong ec bound 1024
3592if eq get exp_ge_owner e u
3593if exp_rel_is_support get exp_ge_rel e
3594pi = exp_g_find_node get exp_ge_to_off e get exp_ge_to_len e
3595if ne pi EXP_NONE
3596if eq get exp_w_vis pi 0
3597qt = get exp_w_bfs_st 0
3598put exp_w_vis pi 1
3599put exp_w_queue qt pi
3600put exp_w_bfs_st 0 add qt 1
3601end
3602end
3603end
3604end
3605end
3606end
3607
3608fn exp_br_scope_bfs to void
3609nc = get exp_g_ncnt 0
3610for i ulong nc bound 320
3611put exp_w_vis i 0
3612end
3613put exp_w_bfs_st 0 0
3614sc = get exp_g_scnt 0
3615for i ulong sc bound 128
3616ni = exp_g_find_node get exp_gs_off i get exp_gs_len i
3617if ne ni EXP_NONE
3618if eq get exp_w_vis ni 0
3619qt = get exp_w_bfs_st 0
3620put exp_w_vis ni 1
3621put exp_w_queue qt ni
3622put exp_w_bfs_st 0 add qt 1
3623end
3624end
3625end
3626qh = ulong 0
3627while lt qh get exp_w_bfs_st 0 bound 320
3628u = get exp_w_queue qh
3629qh = add qh 1
3630exp_br_scope_expand u
3631end
3632end
3633
3634fn exp_br_graph_for_record bidx ulong rec_id view uchar * max_d ulong max_n ulong to int
3635bnid = exp_br_build_graph bidx max_d max_n
3636wo = exp_cx_node_id_str rec_id
3637wnid = exp_g_find_node wo 39
3638if eq wnid EXP_NONE
3639ret EXP_ERR_NOT_IN_BRANCH_SCOPE
3640end
3641exp_br_scope_bfs
3642if eq get exp_w_vis wnid 0
3643ret EXP_ERR_NOT_IN_BRANCH_SCOPE
3644end
3645ret EXP_OK
3646end
3647
3648note ============================================================================
3649note Section 10: Deterministic Renderer & Analogy Catalog (render.rs)
3650note ============================================================================
3651
3652fn exp_out_status_words st ulong to void
3653if eq st EXP_ST_DIRECT_OBS
3654exp_out_view exp_sw_1
3655ret
3656end
3657if eq st EXP_ST_VERIFIED_FACT
3658exp_out_view exp_sw_2
3659ret
3660end
3661if eq st EXP_ST_INFERENCE
3662exp_out_view exp_sw_3
3663ret
3664end
3665if eq st EXP_ST_HYPOTHESIS
3666exp_out_view exp_sw_4
3667ret
3668end
3669if eq st EXP_ST_CONTRADICTION
3670exp_out_view exp_sw_5
3671ret
3672end
3673if eq st EXP_ST_OPERATOR_DECISION
3674exp_out_view exp_sw_6
3675ret
3676end
3677exp_out_view exp_sw_0
3678end
3679
3680fn exp_out_status_name st ulong to void
3681if eq st EXP_ST_DIRECT_OBS
3682exp_out_view exp_sn_1
3683ret
3684end
3685if eq st EXP_ST_VERIFIED_FACT
3686exp_out_view exp_sn_2
3687ret
3688end
3689if eq st EXP_ST_INFERENCE
3690exp_out_view exp_sn_3
3691ret
3692end
3693if eq st EXP_ST_HYPOTHESIS
3694exp_out_view exp_sn_4
3695ret
3696end
3697if eq st EXP_ST_CONTRADICTION
3698exp_out_view exp_sn_5
3699ret
3700end
3701if eq st EXP_ST_OPERATOR_DECISION
3702exp_out_view exp_sn_6
3703ret
3704end
3705exp_out_view exp_sn_0
3706end
3707
3708fn exp_out_kind_name kd ulong to void
3709if eq kd 1
3710exp_out_view exp_kn_1
3711ret
3712end
3713if eq kd 2
3714exp_out_view exp_kn_2
3715ret
3716end
3717if eq kd 3
3718exp_out_view exp_kn_3
3719ret
3720end
3721if eq kd 4
3722exp_out_view exp_kn_4
3723ret
3724end
3725if eq kd 5
3726exp_out_view exp_kn_5
3727ret
3728end
3729if eq kd 6
3730exp_out_view exp_kn_6
3731ret
3732end
3733if eq kd 7
3734exp_out_view exp_kn_7
3735ret
3736end
3737if eq kd 8
3738exp_out_view exp_kn_8
3739ret
3740end
3741if eq kd 9
3742exp_out_view exp_kn_9
3743ret
3744end
3745if eq kd 10
3746exp_out_view exp_kn_10
3747ret
3748end
3749if eq kd 11
3750exp_out_view exp_kn_11
3751ret
3752end
3753if eq kd 12
3754exp_out_view exp_kn_12
3755ret
3756end
3757exp_out_view exp_kn_13
3758end
3759
3760fn exp_out_rel_name rl ulong to void
3761if eq rl 1
3762exp_out_view exp_rn_1
3763ret
3764end
3765if eq rl 2
3766exp_out_view exp_rn_2
3767ret
3768end
3769if eq rl 3
3770exp_out_view exp_rn_3
3771ret
3772end
3773if eq rl 4
3774exp_out_view exp_rn_4
3775ret
3776end
3777if eq rl 5
3778exp_out_view exp_rn_5
3779ret
3780end
3781exp_out_view exp_rn_6
3782end
3783
3784fn exp_out_role_heading rl ulong to void
3785if eq rl 0
3786exp_out_view exp_rh_0
3787ret
3788end
3789if eq rl 1
3790exp_out_view exp_rh_1
3791ret
3792end
3793if eq rl 2
3794exp_out_view exp_rh_2
3795ret
3796end
3797if eq rl 3
3798exp_out_view exp_rh_3
3799ret
3800end
3801if eq rl 4
3802exp_out_view exp_rh_4
3803ret
3804end
3805if eq rl 5
3806exp_out_view exp_rh_5
3807ret
3808end
3809if eq rl 6
3810exp_out_view exp_rh_6
3811ret
3812end
3813exp_out_view exp_rh_7
3814end
3815
3816fn exp_out_role_debug rl ulong to void
3817if eq rl 0
3818exp_out_view exp_rd_0
3819ret
3820end
3821if eq rl 1
3822exp_out_view exp_rd_1
3823ret
3824end
3825if eq rl 2
3826exp_out_view exp_rd_2
3827ret
3828end
3829if eq rl 3
3830exp_out_view exp_rd_3
3831ret
3832end
3833if eq rl 4
3834exp_out_view exp_rd_4
3835ret
3836end
3837if eq rl 5
3838exp_out_view exp_rd_5
3839ret
3840end
3841if eq rl 6
3842exp_out_view exp_rd_6
3843ret
3844end
3845exp_out_view exp_rd_7
3846end
3847
3848fn exp_out_uk_label uk ulong to void
3849if eq uk 1
3850exp_out_view exp_ul_1
3851ret
3852end
3853if eq uk 2
3854exp_out_view exp_ul_2
3855ret
3856end
3857if eq uk 3
3858exp_out_view exp_ul_3
3859ret
3860end
3861if eq uk 4
3862exp_out_view exp_ul_4
3863ret
3864end
3865if eq uk 5
3866exp_out_view exp_ul_5
3867ret
3868end
3869if eq uk 6
3870exp_out_view exp_ul_6
3871ret
3872end
3873if eq uk 7
3874exp_out_view exp_ul_7
3875ret
3876end
3877if eq uk 8
3878exp_out_view exp_ul_8
3879ret
3880end
3881if eq uk 9
3882exp_out_view exp_ul_9
3883ret
3884end
3885exp_out_view exp_ul_10
3886end
3887
3888fn exp_out_uk_name uk ulong to void
3889if eq uk 1
3890exp_out_view exp_un_1
3891ret
3892end
3893if eq uk 2
3894exp_out_view exp_un_2
3895ret
3896end
3897if eq uk 3
3898exp_out_view exp_un_3
3899ret
3900end
3901if eq uk 4
3902exp_out_view exp_un_4
3903ret
3904end
3905if eq uk 5
3906exp_out_view exp_un_5
3907ret
3908end
3909if eq uk 6
3910exp_out_view exp_un_6
3911ret
3912end
3913if eq uk 7
3914exp_out_view exp_un_7
3915ret
3916end
3917if eq uk 8
3918exp_out_view exp_un_8
3919ret
3920end
3921if eq uk 9
3922exp_out_view exp_un_9
3923ret
3924end
3925exp_out_view exp_un_10
3926end
3927
3928fn exp_effective_level md ulong lv ulong to ulong
3929eff = lv
3930if eq eff EXP_LEVEL_DEFAULT
3931if eq md EXP_MODE_SUMMARY
3932eff = 1
3933else
3934if eq md EXP_MODE_TECHNICAL
3935eff = 4
3936else
3937eff = 2
3938end
3939end
3940end
3941if gt eff EXP_MAX_LEVEL
3942eff = EXP_MAX_LEVEL
3943end
3944ret eff
3945end
3946
3947fn exp_aud_shows_ids aud ulong to bool
3948ret ne aud EXP_AUD_GENERAL
3949end
3950
3951fn exp_out_short_id off ulong ln ulong to void
3952colon = EXP_NONE
3953for i ulong ln bound 256
3954if eq get exp_str add off i 58
3955colon = i
3956break
3957end
3958end
3959if ne colon EXP_NONE
3960rest = sub ln add colon 1
3961if gt rest 12
3962exp_out_arena off add colon 13
3963ret
3964end
3965end
3966exp_out_arena off ln
3967end
3968
3969fn exp_out_header aud ulong to void
3970ci = sub get exp_b_concl_step 0 1
3971nid = get exp_bs_nid ci
3972exp_out_view exp_s_concl
3973exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid
3974exp_out_b uchar 32
3975exp_out_b uchar 91
3976exp_out_status_words get exp_gn_status nid
3977exp_out_b uchar 93
3978if exp_aud_shows_ids aud
3979exp_out_b uchar 32
3980exp_out_b uchar 40
3981exp_out_short_id get exp_gn_id_off nid get exp_gn_id_len nid
3982exp_out_b uchar 41
3983end
3984exp_out_b uchar 10
3985end
3986
3987fn exp_out_meas_line nid ulong indent ulong to bool
3988mc = get exp_g_mcnt 0
3989found = ulong 0
3990for m ulong mc bound 256
3991if eq get exp_gm_owner m nid
3992if eq found 0
3993if ne indent 0
3994exp_out_view exp_s_meas
3995else
3996exp_out_view exp_s_meas_ni
3997end
3998else
3999exp_out_b uchar 44
4000exp_out_b uchar 32
4001end
4002exp_out_arena get exp_gm_name_off m get exp_gm_name_len m
4003exp_out_b uchar 61
4004exp_out_arena get exp_gm_val_off m get exp_gm_val_len m
4005if gt get exp_gm_unit_len m 0
4006exp_out_b uchar 32
4007exp_out_arena get exp_gm_unit_off m get exp_gm_unit_len m
4008end
4009found = add found 1
4010end
4011end
4012if gt found 0
4013exp_out_b uchar 10
4014ret true
4015end
4016ret false
4017end
4018
4019fn exp_out_step_line si ulong lv ulong aud ulong to void
4020nid = get exp_bs_nid si
4021exp_out_u64 add si 1
4022exp_out_b uchar 46
4023exp_out_b uchar 32
4024exp_out_b uchar 91
4025exp_out_status_words get exp_gn_status nid
4026exp_out_b uchar 93
4027exp_out_b uchar 32
4028exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid
4029if ge lv 2
4030soff = get exp_bs_sup_off si
4031scnt = get exp_bs_sup_cnt si
4032sc = ulong 0
4033for k ulong scnt bound 1024
4034if ne get exp_bs_sup_rel add soff k EXP_REL_REFERENCES
4035if eq sc 0
4036exp_out_view exp_s_rests
4037else
4038exp_out_b uchar 44
4039exp_out_b uchar 32
4040end
4041exp_out_u64 get exp_bs_sup_step add soff k
4042sc = add sc 1
4043end
4044end
4045if gt sc 0
4046exp_out_b uchar 41
4047end
4048wc = ulong 0
4049for k ulong scnt bound 1024
4050if eq get exp_bs_sup_rel add soff k EXP_REL_REFERENCES
4051if eq wc 0
4052exp_out_view exp_s_refers
4053else
4054exp_out_b uchar 44
4055exp_out_b uchar 32
4056end
4057exp_out_u64 get exp_bs_sup_step add soff k
4058wc = add wc 1
4059end
4060end
4061if gt wc 0
4062exp_out_view exp_s_weak
4063end
4064end
4065if exp_aud_shows_ids aud
4066exp_out_b uchar 32
4067exp_out_b uchar 91
4068exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid
4069exp_out_b uchar 32
4070exp_out_short_id get exp_gn_id_off nid get exp_gn_id_len nid
4071exp_out_b uchar 93
4072else
4073exp_out_view exp_s_from
4074exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid
4075exp_out_b uchar 93
4076end
4077if ne get exp_bs_bref si 0
4078exp_out_view exp_s_br_ref
4079end
4080exp_out_b uchar 10
4081end
4082
4083fn exp_reason_better si ulong best ulong to bool
4084bg = get exp_bs_gnd best
4085sg = get exp_bs_gnd si
4086if gt sg bg
4087ret true
4088end
4089if lt sg bg
4090ret false
4091end
4092bnid = get exp_bs_nid best
4093snid = get exp_bs_nid si
4094bnh = ulong 1
4095if eq get exp_gn_status bnid EXP_ST_HYPOTHESIS
4096bnh = 0
4097end
4098snh = ulong 1
4099if eq get exp_gn_status snid EXP_ST_HYPOTHESIS
4100snh = 0
4101end
4102if gt snh bnh
4103ret true
4104end
4105if lt snh bnh
4106ret false
4107end
4108bc = ulong 0
4109if ne get exp_gn_has_conf bnid 0
4110bc = get exp_gn_conf bnid
4111end
4112sc = ulong 0
4113if ne get exp_gn_has_conf snid 0
4114sc = get exp_gn_conf snid
4115end
4116if gt sc bc
4117ret true
4118end
4119if lt sc bc
4120ret false
4121end
4122ret gt si best
4123end
4124
4125fn exp_strongest_reason to ulong
4126ci = sub get exp_b_concl_step 0 1
4127soff = get exp_bs_sup_off ci
4128scnt = get exp_bs_sup_cnt ci
4129best = EXP_NONE
4130for k ulong scnt bound 1024
4131if ne get exp_bs_sup_rel add soff k EXP_REL_REFERENCES
4132si = sub get exp_bs_sup_step add soff k 1
4133if eq best EXP_NONE
4134best = si
4135else
4136if exp_reason_better si best
4137best = si
4138end
4139end
4140end
4141end
4142ret best
4143end
4144
4145fn exp_strongest_evidence to ulong
4146evc = get exp_be_cnt 0
4147best = EXP_NONE
4148for i ulong evc bound 320
4149si = sub get exp_be_step i 1
4150if ne get exp_bs_gnd si 0
4151if eq best EXP_NONE
4152best = si
4153else
4154db = get exp_bs_depth best
4155ds = get exp_bs_depth si
4156if lt ds db
4157best = si
4158else
4159if eq ds db
4160if lt si best
4161best = si
4162end
4163end
4164end
4165end
4166end
4167end
4168ret best
4169end
4170
4171fn exp_material_unc to ulong
4172uc = get exp_bu_cnt 0
4173best = EXP_NONE
4174for i ulong uc bound 512
4175if eq best EXP_NONE
4176best = i
4177else
4178sb = exp_uk_severity get exp_bu_kind best
4179si = exp_uk_severity get exp_bu_kind i
4180if lt si sb
4181best = i
4182else
4183if eq si sb
4184c1 = exp_str_cmp get exp_bu_nid_off i get exp_bu_nid_len i get exp_bu_nid_off best get exp_bu_nid_len best
4185if lt c1 0
4186best = i
4187else
4188if eq c1 0
4189if lt exp_str_cmp get exp_bu_det_off i get exp_bu_det_len i get exp_bu_det_off best get exp_bu_det_len best 0
4190best = i
4191end
4192end
4193end
4194end
4195end
4196end
4197end
4198ret best
4199end
4200
4201fn exp_render_step_body si ulong lv ulong aud ulong to void
4202nid = get exp_bs_nid si
4203if eq add si 1 get exp_b_concl_step 0
4204exp_out_u64 add si 1
4205exp_out_view exp_s_therefore
4206exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid
4207exp_out_b uchar 32
4208exp_out_b uchar 91
4209exp_out_status_words get exp_gn_status nid
4210exp_out_b uchar 93
4211exp_out_b uchar 10
4212else
4213exp_out_step_line si lv aud
4214end
4215if ge lv 3
4216dm = exp_out_meas_line nid 1
4217end
4218if ge lv 4
4219exp_out_view exp_s_src
4220exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid
4221exp_out_b uchar 32
4222exp_out_arena get exp_gn_art_off nid get exp_gn_art_len nid
4223exp_out_b uchar 32
4224exp_out_arena get exp_gn_ref_off nid get exp_gn_ref_len nid
4225if ne get exp_gn_has_dig nid 0
4226exp_out_view exp_s_dig
4227exp_out_arena get exp_gn_dig_off nid get exp_gn_dig_len nid
4228end
4229exp_out_b uchar 10
4230end
4231end
4232
4233fn exp_render_steps_footer lv ulong to void
4234exp_out_view exp_s_unc_hdr
4235uc = get exp_bu_cnt 0
4236shown_u = ulong 0
4237for i ulong uc bound 512
4238uk = get exp_bu_kind i
4239show = true
4240if lt lv 3
4241if eq uk EXP_UK_INFERRED
4242show = false
4243end
4244end
4245if show
4246exp_out_b uchar 45
4247exp_out_b uchar 32
4248exp_out_uk_label uk
4249exp_out_b uchar 58
4250exp_out_b uchar 32
4251exp_out_arena get exp_bu_det_off i get exp_bu_det_len i
4252exp_out_b uchar 10
4253shown_u = add shown_u 1
4254end
4255end
4256if eq shown_u 0
4257exp_out_view exp_s_no_gaps
4258end
4259cc = get exp_bc_cnt 0
4260if gt cc 0
4261exp_out_view exp_s_conf_hdr
4262for i ulong cc bound 128
4263exp_out_b uchar 45
4264exp_out_b uchar 32
4265exp_out_arena get exp_bc_stmt_off i get exp_bc_stmt_len i
4266exp_out_view exp_s_inv_a
4267exp_out_u64 get exp_bc_inv_cnt i
4268exp_out_view exp_s_inv_b
4269end
4270end
4271if ge lv 2
4272ac = get exp_ba_cnt 0
4273if gt ac 0
4274exp_out_view exp_s_alt_hdr
4275for i ulong ac bound 128
4276anid = get exp_ba_nid i
4277exp_out_b uchar 45
4278exp_out_b uchar 32
4279exp_out_arena get exp_gn_stmt_off anid get exp_gn_stmt_len anid
4280exp_out_b uchar 10
4281end
4282end
4283end
4284end
4285
4286fn exp_render_steps lv ulong aud ulong to void
4287if eq lv 0
4288exp_out_header aud
4289ret
4290end
4291last_role = EXP_NONE
4292if eq lv 1
4293ci = sub get exp_b_concl_step 0 1
4294soff = get exp_bs_sup_off ci
4295scnt = get exp_bs_sup_cnt ci
4296for k ulong scnt bound 1024
4297si = sub get exp_bs_sup_step add soff k 1
4298rl = get exp_bs_role si
4299if ne rl last_role
4300exp_out_role_heading rl
4301exp_out_b uchar 58
4302exp_out_b uchar 10
4303last_role = rl
4304end
4305exp_render_step_body si lv aud
4306end
4307rl = get exp_bs_role ci
4308if ne rl last_role
4309exp_out_role_heading rl
4310exp_out_b uchar 58
4311exp_out_b uchar 10
4312end
4313exp_render_step_body ci lv aud
4314else
4315sc = get exp_b_scnt 0
4316for si ulong sc bound 320
4317rl = get exp_bs_role si
4318if ne rl last_role
4319exp_out_role_heading rl
4320exp_out_b uchar 58
4321exp_out_b uchar 10
4322last_role = rl
4323end
4324exp_render_step_body si lv aud
4325end
4326end
4327exp_render_steps_footer lv
4328end
4329
4330fn exp_render_summary lv ulong aud ulong to void
4331exp_out_header aud
4332if eq lv 0
4333ret
4334end
4335sr = exp_strongest_reason
4336if ne sr EXP_NONE
4337rnid = get exp_bs_nid sr
4338exp_out_view exp_s_mreason
4339exp_out_arena get exp_gn_stmt_off rnid get exp_gn_stmt_len rnid
4340exp_out_b uchar 32
4341exp_out_b uchar 91
4342exp_out_status_words get exp_gn_status rnid
4343exp_out_b uchar 93
4344exp_out_b uchar 10
4345else
4346exp_out_view exp_s_no_reason
4347end
4348se = exp_strongest_evidence
4349if ne se EXP_NONE
4350enid = get exp_bs_nid se
4351exp_out_view exp_s_sev
4352exp_out_arena get exp_gn_stmt_off enid get exp_gn_stmt_len enid
4353exp_out_b uchar 32
4354exp_out_b uchar 91
4355exp_out_status_words get exp_gn_status enid
4356exp_out_b uchar 93
4357exp_out_b uchar 10
4358dm = exp_out_meas_line enid 0
4359else
4360exp_out_view exp_s_no_ev
4361end
4362mu = exp_material_unc
4363if ne mu EXP_NONE
4364exp_out_view exp_s_munc
4365exp_out_arena get exp_bu_det_off mu get exp_bu_det_len mu
4366exp_out_b uchar 10
4367else
4368exp_out_view exp_s_no_munc
4369end
4370if ge lv 2
4371exp_out_b uchar 10
4372exp_render_steps lv aud
4373end
4374end
4375
4376fn exp_render_tech_step si ulong lv ulong to void
4377nid = get exp_bs_nid si
4378exp_out_view exp_s_t_step
4379exp_out_u64 add si 1
4380exp_out_view exp_s_t_id
4381exp_out_arena get exp_gn_id_off nid get exp_gn_id_len nid
4382exp_out_view exp_s_t_kind
4383exp_out_kind_name get exp_gn_kind nid
4384exp_out_view exp_s_t_status
4385exp_out_status_name get exp_gn_status nid
4386exp_out_view exp_s_t_role
4387exp_out_role_debug get exp_bs_role si
4388exp_out_view exp_s_t_depth
4389exp_out_u64 get exp_bs_depth si
4390exp_out_view exp_s_t_gnd
4391if ne get exp_bs_gnd si 0
4392exp_out_view exp_s_true
4393else
4394exp_out_view exp_s_false
4395end
4396exp_out_view exp_s_t_sup
4397soff = get exp_bs_sup_off si
4398scnt = get exp_bs_sup_cnt si
4399for k ulong scnt bound 1024
4400if gt k 0
4401exp_out_b uchar 44
4402end
4403exp_out_rel_name get exp_bs_sup_rel add soff k
4404exp_out_b uchar 58
4405exp_out_u64 get exp_bs_sup_step add soff k
4406end
4407exp_out_b uchar 93
4408exp_out_b uchar 10
4409exp_out_view exp_s_t_stmt
4410exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid
4411exp_out_b uchar 10
4412if ne get exp_gn_has_conf nid 0
4413exp_out_view exp_s_t_conf
4414exp_out_u64 get exp_gn_conf nid
4415exp_out_view exp_s_t_basis
4416if ne get exp_gn_has_cbas nid 0
4417exp_out_arena get exp_gn_cbas_off nid get exp_gn_cbas_len nid
4418else
4419exp_out_view exp_s_t_unspec
4420end
4421exp_out_b uchar 10
4422end
4423if ge lv 3
4424dm = exp_out_meas_line nid 1
4425end
4426if ge lv 4
4427exp_out_view exp_s_t_src
4428exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid
4429exp_out_view exp_s_t_art
4430exp_out_arena get exp_gn_art_off nid get exp_gn_art_len nid
4431exp_out_view exp_s_t_ref
4432exp_out_arena get exp_gn_ref_off nid get exp_gn_ref_len nid
4433exp_out_view exp_s_t_dig
4434if ne get exp_gn_has_dig nid 0
4435exp_out_arena get exp_gn_dig_off nid get exp_gn_dig_len nid
4436else
4437exp_out_b uchar 45
4438end
4439exp_out_b uchar 10
4440if ne get exp_gn_has_ts nid 0
4441exp_out_view exp_s_t_rec
4442exp_out_u64 get exp_gn_ts nid
4443exp_out_view exp_s_t_rec_n
4444end
4445end
4446noc = get exp_g_notecnt 0
4447for o ulong noc bound 128
4448if eq get exp_go_owner o nid
4449exp_out_view exp_s_t_note
4450exp_out_arena get exp_go_off o get exp_go_len o
4451exp_out_b uchar 10
4452end
4453end
4454end
4455
4456fn exp_render_tech_contra i ulong to void
4457nid = get exp_bc_nid i
4458exp_out_view exp_s_t_contra
4459exp_out_arena get exp_gn_id_off nid get exp_gn_id_len nid
4460exp_out_view exp_s_t_invs
4461io = get exp_bc_inv_off i
4462ic = get exp_bc_inv_cnt i
4463for j ulong ic bound 128
4464if gt j 0
4465exp_out_b uchar 44
4466end
4467exp_out_arena get exp_bc_inv_id_off add io j get exp_bc_inv_id_len add io j
4468end
4469exp_out_view exp_s_t_stmteq
4470exp_out_arena get exp_bc_stmt_off i get exp_bc_stmt_len i
4471exp_out_b uchar 10
4472end
4473
4474fn exp_render_tech_footer lv ulong to void
4475uc = get exp_bu_cnt 0
4476for i ulong uc bound 512
4477uk = get exp_bu_kind i
4478exp_out_view exp_s_t_unc
4479exp_out_u64 uk
4480exp_out_view exp_s_t_kind
4481exp_out_uk_name uk
4482exp_out_view exp_s_t_node
4483exp_out_arena get exp_bu_nid_off i get exp_bu_nid_len i
4484exp_out_view exp_s_t_rel
4485if ne get exp_bu_has_rel i 0
4486exp_out_arena get exp_bu_rel_off i get exp_bu_rel_len i
4487else
4488exp_out_b uchar 45
4489end
4490exp_out_view exp_s_t_det
4491exp_out_arena get exp_bu_det_off i get exp_bu_det_len i
4492exp_out_b uchar 10
4493end
4494cc = get exp_bc_cnt 0
4495for i ulong cc bound 128
4496exp_render_tech_contra i
4497end
4498ac = get exp_ba_cnt 0
4499for i ulong ac bound 128
4500nid = get exp_ba_nid i
4501exp_out_view exp_s_t_alt
4502exp_out_arena get exp_gn_id_off nid get exp_gn_id_len nid
4503exp_out_view exp_s_t_to
4504exp_out_arena get exp_ba_to_off i get exp_ba_to_len i
4505exp_out_view exp_s_t_stmts
4506exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid
4507exp_out_b uchar 10
4508end
4509if ge lv 4
4510exp_out_view exp_s_t_idig
4511exp_out_dig32 0
4512exp_out_view exp_s_t_bdig
4513exp_out_dig32 1
4514exp_out_view exp_s_t_vis
4515exp_out_u64 get exp_bp_vis 0
4516exp_out_b uchar 47
4517exp_out_u64 get exp_bp_gnodes 0
4518exp_out_view exp_s_t_lim
4519exp_out_u64 get exp_bp_max_d 0
4520exp_out_view exp_s_t_nod
4521exp_out_u64 get exp_bp_max_n 0
4522exp_out_b uchar 10
4523end
4524end
4525
4526fn exp_render_technical lv ulong to void
4527ci = sub get exp_b_concl_step 0 1
4528cnid = get exp_bs_nid ci
4529exp_out_view exp_s_t_expl
4530exp_out_arena get exp_bp_tgt_off 0 get exp_bp_tgt_len 0
4531exp_out_view exp_s_t_prod
4532exp_out_arena get exp_g_prod_off 0 get exp_g_prod_len 0
4533exp_out_view exp_s_t_bldr
4534exp_out_view exp_txt_builder
4535exp_out_view exp_s_t_cont
4536exp_out_arena get exp_g_fmt_off 0 get exp_g_fmt_len 0
4537exp_out_b uchar 32
4538exp_out_b uchar 118
4539exp_out_u64 get exp_g_ver 0
4540exp_out_b uchar 10
4541exp_out_view exp_s_t_concl
4542exp_out_u64 add ci 1
4543exp_out_view exp_s_t_id
4544exp_out_arena get exp_gn_id_off cnid get exp_gn_id_len cnid
4545exp_out_view exp_s_t_status
4546exp_out_status_name get exp_gn_status cnid
4547exp_out_view exp_s_t_kind
4548exp_out_kind_name get exp_gn_kind cnid
4549exp_out_b uchar 10
4550if eq lv 0
4551ret
4552end
4553sc = get exp_b_scnt 0
4554for si ulong sc bound 320
4555exp_render_tech_step si lv
4556end
4557exp_render_tech_footer lv
4558end
4559
4560fn exp_analogy_kind kd ulong st ulong to ulong
4561if eq kd EXP_KD_CAP_ANALYSIS
4562ret 1
4563end
4564if eq kd EXP_KD_HYPOTHESIS
4565ret 2
4566end
4567if eq kd EXP_KD_CORTEX_RECORD
4568if eq st EXP_ST_HYPOTHESIS
4569ret 2
4570end
4571end
4572if eq kd EXP_KD_FALSIFICATION
4573ret 3
4574end
4575if eq kd EXP_KD_EVAL_RECEIPT
4576ret 4
4577end
4578if eq kd EXP_KD_AGENT_BRANCH
4579ret 5
4580end
4581if eq st EXP_ST_CONTRADICTION
4582ret 6
4583end
4584ret 0
4585end
4586
4587fn exp_out_analogy_text ak ulong to void
4588if eq ak 1
4589exp_out_view exp_ana_cap
4590ret
4591end
4592if eq ak 2
4593exp_out_view exp_ana_hyp
4594ret
4595end
4596if eq ak 3
4597exp_out_view exp_ana_fal
4598ret
4599end
4600if eq ak 4
4601exp_out_view exp_ana_evl
4602ret
4603end
4604if eq ak 5
4605exp_out_view exp_ana_brn
4606ret
4607end
4608exp_out_view exp_ana_con
4609end
4610
4611fn exp_render_analogy lv ulong aud ulong to void
4612sc = get exp_b_scnt 0
4613pick_step = ulong 0
4614pick_ak = ulong 0
4615for k ulong sc bound 320
4616si = sub sub sc 1 k
4617nid = get exp_bs_nid si
4618ak = exp_analogy_kind get exp_gn_kind nid get exp_gn_status nid
4619if ne ak 0
4620pick_step = add si 1
4621pick_ak = ak
4622break
4623end
4624end
4625if ne pick_ak 0
4626exp_out_view exp_s_ana_hdr
4627exp_out_u64 pick_step
4628exp_out_b uchar 41
4629exp_out_b uchar 58
4630exp_out_b uchar 10
4631exp_out_b uchar 32
4632exp_out_b uchar 32
4633exp_out_analogy_text pick_ak
4634exp_out_b uchar 10
4635else
4636exp_out_view exp_s_ana_none
4637end
4638exp_out_view exp_s_ev_itself
4639rlv = lv
4640if lt rlv 1
4641rlv = 1
4642end
4643exp_render_steps rlv aud
4644end
4645
4646fn exp_render md ulong lv ulong aud ulong to ulong
4647exp_out_reset
4648eff = exp_effective_level md lv
4649if eq md EXP_MODE_SUMMARY
4650exp_render_summary eff aud
4651else
4652if eq md EXP_MODE_STEP_BY_STEP
4653exp_out_header aud
4654if gt eff 0
4655exp_out_b uchar 10
4656exp_render_steps eff aud
4657end
4658else
4659if eq md EXP_MODE_TECHNICAL
4660exp_render_technical eff
4661else
4662exp_render_analogy eff aud
4663end
4664end
4665end
4666ret get exp_out_len 0
4667end
4668
4669note ============================================================================
4670note Section 11: Clarification Session & CLI Helpers (session.rs, main.rs)
4671note ============================================================================
4672
4673fn exp_sess_init aud ulong to void
4674put exp_sess 0 0
4675put exp_sess 1 aud
4676end
4677
4678fn exp_sess_level to ulong
4679ret get exp_sess 0
4680end
4681
4682fn exp_sess_explain to ulong
4683put exp_sess 0 0
4684ret exp_render EXP_MODE_STEP_BY_STEP 0 get exp_sess 1
4685end
4686
4687fn exp_sess_deeper to ulong
4688lv = add get exp_sess 0 1
4689if gt lv EXP_MAX_LEVEL
4690lv = EXP_MAX_LEVEL
4691end
4692put exp_sess 0 lv
4693ret exp_render EXP_MODE_STEP_BY_STEP lv get exp_sess 1
4694end
4695
4696fn exp_sess_shallower to ulong
4697lv = get exp_sess 0
4698if gt lv 0
4699lv = sub lv 1
4700end
4701put exp_sess 0 lv
4702ret exp_render EXP_MODE_STEP_BY_STEP lv get exp_sess 1
4703end
4704
4705fn exp_sess_step_deps si ulong to void
4706soff = get exp_bs_sup_off si
4707scnt = get exp_bs_sup_cnt si
4708if eq scnt 0
4709if ne get exp_bs_gnd si 0
4710exp_out_view exp_s_start_pt
4711else
4712exp_out_view exp_s_no_sup
4713end
4714ret
4715end
4716for k ulong scnt bound 1024
4717pstep = get exp_bs_sup_step add soff k
4718prel = get exp_bs_sup_rel add soff k
4719pnid = get exp_bs_nid sub pstep 1
4720if eq prel EXP_REL_REFERENCES
4721exp_out_view exp_s_it_refs
4722else
4723exp_out_view exp_s_it_rests
4724end
4725exp_out_u64 pstep
4726exp_out_b uchar 32
4727exp_out_b uchar 40
4728exp_out_rel_name prel
4729exp_out_b uchar 41
4730exp_out_b uchar 58
4731exp_out_b uchar 32
4732exp_out_arena get exp_gn_stmt_off pnid get exp_gn_stmt_len pnid
4733exp_out_b uchar 10
4734end
4735end
4736
4737fn exp_sess_step_later n ulong nid ulong to void
4738sc = get exp_b_scnt 0
4739for j ulong sc bound 320
4740lso = get exp_bs_sup_off j
4741lsc = get exp_bs_sup_cnt j
4742for k ulong lsc bound 1024
4743if eq get exp_bs_sup_step add lso k n
4744exp_out_view exp_s_step_hdr
4745exp_out_u64 add j 1
4746exp_out_view exp_s_rests_on_it
4747break
4748end
4749end
4750end
4751uc = get exp_bu_cnt 0
4752no = get exp_gn_id_off nid
4753nl = get exp_gn_id_len nid
4754for i ulong uc bound 512
4755if exp_str_eq get exp_bu_nid_off i get exp_bu_nid_len i no nl
4756exp_out_view exp_s_caveat
4757exp_out_arena get exp_bu_det_off i get exp_bu_det_len i
4758exp_out_b uchar 10
4759end
4760end
4761end
4762
4763fn exp_sess_step n ulong to int
4764if eq n 0
4765ret EXP_ERR_STEP_OUT_OF_RANGE
4766end
4767if gt n get exp_b_scnt 0
4768ret EXP_ERR_STEP_OUT_OF_RANGE
4769end
4770exp_out_reset
4771si = sub n 1
4772nid = get exp_bs_nid si
4773exp_out_view exp_s_step_hdr
4774exp_out_u64 n
4775exp_out_b uchar 58
4776exp_out_b uchar 32
4777exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid
4778exp_out_b uchar 32
4779exp_out_b uchar 91
4780exp_out_status_words get exp_gn_status nid
4781exp_out_b uchar 93
4782exp_out_b uchar 10
4783exp_sess_step_deps si
4784exp_sess_step_later n nid
4785ret EXP_OK
4786end
4787
4788fn exp_sess_source n ulong to int
4789if eq n 0
4790ret EXP_ERR_STEP_OUT_OF_RANGE
4791end
4792if gt n get exp_b_scnt 0
4793ret EXP_ERR_STEP_OUT_OF_RANGE
4794end
4795exp_out_reset
4796si = sub n 1
4797nid = get exp_bs_nid si
4798exp_out_view exp_s_step_hdr
4799exp_out_u64 n
4800exp_out_view exp_s_comes
4801exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid
4802exp_out_b uchar 32
4803exp_out_b uchar 40
4804exp_out_arena get exp_gn_art_off nid get exp_gn_art_len nid
4805exp_out_view exp_s_ref_lbl
4806exp_out_arena get exp_gn_ref_off nid get exp_gn_ref_len nid
4807if ne get exp_gn_has_dig nid 0
4808exp_out_view exp_s_dig_c
4809exp_out_arena get exp_gn_dig_off nid get exp_gn_dig_len nid
4810end
4811exp_out_view exp_s_node_sc
4812exp_out_arena get exp_gn_id_off nid get exp_gn_id_len nid
4813exp_out_view exp_s_st_sc
4814exp_out_status_name get exp_gn_status nid
4815exp_out_b uchar 46
4816exp_out_b uchar 10
4817ret EXP_OK
4818end
4819
4820fn exp_sess_evidence to ulong
4821exp_out_reset
4822evc = get exp_be_cnt 0
4823if eq evc 0
4824exp_out_view exp_s_no_ev_att
4825ret get exp_out_len 0
4826end
4827mc = get exp_g_mcnt 0
4828for i ulong evc bound 320
4829step = get exp_be_step i
4830nid = get exp_bs_nid sub step 1
4831exp_out_view exp_s_step_hdr
4832exp_out_u64 step
4833exp_out_b uchar 58
4834exp_out_b uchar 32
4835exp_out_arena get exp_gn_sys_off nid get exp_gn_sys_len nid
4836exp_out_b uchar 32
4837exp_out_arena get exp_gn_art_off nid get exp_gn_art_len nid
4838exp_out_b uchar 32
4839exp_out_arena get exp_gn_ref_off nid get exp_gn_ref_len nid
4840if ne get exp_gn_has_dig nid 0
4841exp_out_view exp_s_dig
4842exp_out_arena get exp_gn_dig_off nid get exp_gn_dig_len nid
4843end
4844exp_out_b uchar 10
4845for m ulong mc bound 256
4846if eq get exp_gm_owner m nid
4847exp_out_b uchar 32
4848exp_out_b uchar 32
4849exp_out_arena get exp_gm_name_off m get exp_gm_name_len m
4850exp_out_b uchar 32
4851exp_out_b uchar 61
4852exp_out_b uchar 32
4853exp_out_arena get exp_gm_val_off m get exp_gm_val_len m
4854exp_out_b uchar 32
4855exp_out_arena get exp_gm_unit_off m get exp_gm_unit_len m
4856exp_out_b uchar 10
4857end
4858end
4859end
4860ret get exp_out_len 0
4861end
4862
4863fn exp_sess_technical to ulong
4864ret exp_render EXP_MODE_TECHNICAL EXP_MAX_LEVEL get exp_sess 1
4865end
4866
4867fn exp_sess_simple to ulong
4868ret exp_render EXP_MODE_SUMMARY 1 EXP_AUD_GENERAL
4869end
4870
4871fn exp_sess_analogy to ulong
4872ret exp_render EXP_MODE_ANALOGY 2 get exp_sess 1
4873end
4874
4875fn exp_sess_uncertainties to ulong
4876exp_out_reset
4877uc = get exp_bu_cnt 0
4878cc = get exp_bc_cnt 0
4879if and eq uc 0 eq cc 0
4880exp_out_view exp_s_no_unc_rec
4881ret get exp_out_len 0
4882end
4883for i ulong uc bound 512
4884exp_out_b uchar 45
4885exp_out_b uchar 32
4886exp_out_uk_label get exp_bu_kind i
4887exp_out_b uchar 58
4888exp_out_b uchar 32
4889exp_out_arena get exp_bu_det_off i get exp_bu_det_len i
4890exp_out_b uchar 10
4891end
4892for i ulong cc bound 128
4893exp_out_view exp_s_conflict_pfx
4894exp_out_arena get exp_bc_stmt_off i get exp_bc_stmt_len i
4895exp_out_b uchar 10
4896end
4897ret get exp_out_len 0
4898end
4899
4900fn exp_sess_alternatives to ulong
4901exp_out_reset
4902ac = get exp_ba_cnt 0
4903if eq ac 0
4904exp_out_view exp_s_no_alt_rec
4905ret get exp_out_len 0
4906end
4907for i ulong ac bound 128
4908nid = get exp_ba_nid i
4909exp_out_b uchar 45
4910exp_out_b uchar 32
4911exp_out_arena get exp_gn_stmt_off nid get exp_gn_stmt_len nid
4912exp_out_b uchar 10
4913end
4914ret get exp_out_len 0
4915end
4916
4917note CLI helpers (main.rs)
4918fn exp_cli_exit_code err int to int
4919if eq err EXP_OK
4920ret 0
4921end
4922ret add 10 err
4923end
4924
4925fn exp_parse_audience s view uchar * to ulong
4926if exp_view_eq s exp_an_0
4927ret EXP_AUD_GENERAL
4928end
4929if exp_view_eq s exp_an_1
4930ret EXP_AUD_OPERATOR
4931end
4932if exp_view_eq s exp_an_2
4933ret EXP_AUD_DEVELOPER
4934end
4935if exp_view_eq s exp_an_3
4936ret EXP_AUD_SYSTEMS_ENG
4937end
4938if exp_view_eq s exp_an_4
4939ret EXP_AUD_RESEARCHER
4940end
4941ret EXP_AUD_INVALID
4942end
4943
4944fn exp_hex_nib b uchar to int
4945if and ge b 48 le b 57
4946ret int sub b 48
4947end
4948if and ge b 97 le b 102
4949ret int add 10 sub b 97
4950end
4951if and ge b 65 le b 70
4952ret int add 10 sub b 65
4953end
4954ret -1
4955end
4956
4957fn exp_parse_cortex_id s view uchar * dst edit uchar * to int
4958n = len s
4959off = ulong 0
4960if ge n 7
4961if exp_view_eq part s 0 7 exp_txt_cx_pfx
4962off = 7
4963end
4964end
4965if ne sub n off 32
4966ret EXP_ERR_TARGET_NOT_FOUND
4967end
4968for k ulong 16
4969hi = exp_hex_nib get s add off mul k 2
4970lo = exp_hex_nib get s add add off mul k 2 1
4971if or lt hi 0 lt lo 0
4972ret EXP_ERR_TARGET_NOT_FOUND
4973end
4974put dst k uchar or shl ulong hi 4 ulong lo
4975end
4976ret EXP_OK
4977end