nx_incumbents.nx source
↩ module page · 124 lines · 6311 B
1// nx_incumbents.nx -- CLI over nx_incumbent_lib (engineshift ES28): build the incumbent index, or answer a board's rung
2// contracts from it. The clock runs the bare form daily (nx_clockjob put incumbentidx 86400 nx_incumbents).
3// nx_incumbents -> build the index (roots probed from the CWD: buildroot/runtime, else runtime) and print
4// the envelope
5// nx_incumbents index -> the same
6// nx_incumbents <domain> [rung] -> one INCUMBENTS(...) line per rung| row of the domain's plan (or the one rung), the
7// envelope, and a verdict LAST. The CLI knows no matrix, so nothing is excluded here;
8// the ranker excludes the rung's own organ.
9// exit: 0 answered or built | 1 index STALE or MALFORMED (present but refused as an answer) | 2 usage | 3 index ABSENT,
10// plan unreadable in both trees, or build failed
11// license_tier: ORIGINAL Writes only knowledge/status/incumbent_index.txt (atomic). No hw writes (Rule 26).
12import "nx_syscalls.nx"
13import "nx_incumbent_lib.nx"
14import "nx_barfresh_lib.nx"
15
16const NIC_ARG_VERB: i64 = 1
17const NIC_ARG_RUNG: i64 = 2
18const NIC_EXIT_OK: i64 = 0
19const NIC_EXIT_REFUSED: i64 = 1
20const NIC_EXIT_USAGE: i64 = 2
21const NIC_EXIT_ABSENT: i64 = 3
22const NIC_STDOUT: i64 = 1
23const NIC_OUT_CAP: i64 = 16384
24const NIC_VERB_INDEX: *u8 = "index"
25const NIC_RUNG_TAG: *u8 = "rung|"
26const NIC_F_ID: i64 = 1
27const NIC_F_TITLE: i64 = 2
28const NIC_F_SYM: i64 = 3
29const NIC_F_NOTE: i64 = 4
30
31func nic_w(s: *u8) -> i64 { return sys_write(NIC_STDOUT, s, inc_slen(s)) }
32func nic_wn(v: i64) -> i64 {
33 let b: *u8 = sys_mmap(INC_NUM_CAP)
34 inc_catn(b, 0, INC_NUM_CAP, v)
35 return nic_w(b)
36}
37// a plan field copied out NUL-terminated into dst (cap bytes)
38func nic_field(buf: *u8, p: i64, e: i64, k: i64, dst: *u8, cap: i64) -> i64 {
39 let fo: *i64 = sys_mmap(INC_I64) as *i64
40 var l: i64 = bf_field(buf, p, e, k, fo)
41 if l < 0 { l = 0 }
42 var i: i64 = 0
43 while i < l { if i < cap - 1 { dst[i] = buf[fo[0] + i] } i = i + 1 }
44 if l >= cap { l = cap - 1 }
45 dst[l] = 0 as u8
46 return l
47}
48
49func main(argc: i64, argv: *i64) -> i64 {
50 let c: *i64 = inc_conf_load()
51 var build: i64 = 0
52 if argc <= NIC_ARG_VERB { build = 1 } else { if inc_streq(argv[NIC_ARG_VERB] as *u8, NIC_VERB_INDEX) == 1 { build = 1 } }
53 let out: *u8 = sys_mmap(NIC_OUT_CAP)
54 if build == 1 {
55 let host: i64 = inc_cwd_is_nishihost()
56 var rt: *u8 = INC_ROOT_RT_B
57 var hdl: *u8 = INC_ROOT_HDL_B
58 var assets: *u8 = INC_ASSET_B
59 var idx: *u8 = INC_INDEX_B
60 if host == 1 { rt = INC_ROOT_RT_A; hdl = INC_ROOT_HDL_A; assets = INC_ASSET_A; idx = INC_INDEX_A }
61 let env: *i64 = inc_index_build(c, rt, hdl, assets, idx)
62 nic_w("INCUMBENTS-INDEX path=" as *u8); nic_w(idx)
63 nic_w(" roots=" as *u8); nic_w(rt); nic_w("," as *u8); nic_w(hdl); nic_w(" assets_dir=" as *u8); nic_w(assets)
64 nic_w(" files=" as *u8); nic_wn(env[INC_E_FILES])
65 nic_w(" head_truncated=" as *u8); nic_wn(env[INC_E_TRUNC])
66 nic_w(" assets=" as *u8); nic_wn(env[INC_E_ASSETS])
67 nic_w(" corpus_complete=" as *u8); nic_wn(env[INC_E_COMPLETE])
68 nic_w(" generated_at=" as *u8); nic_wn(env[INC_E_GEN])
69 nic_w(" bytes=" as *u8); nic_wn(env[INC_E_BYTES])
70 nic_w(" conf_src=" as *u8); if c[INC_C_SRC] == 1 { nic_w("conf" as *u8) } else { nic_w("defaults" as *u8) }
71 if env[INC_E_BYTES] < 0 { nic_w(" verdict=BUILD-FAILED (the index could not be published; the previous index, if any, stands)\n" as *u8); return NIC_EXIT_ABSENT }
72 if env[INC_E_COMPLETE] == 0 { nic_w(" verdict=BUILT-PARTIAL (a root or the asset dir was unreadable; every answer from this index says corpus_complete=0)\n" as *u8); return NIC_EXIT_OK }
73 nic_w(" verdict=BUILT\n" as *u8)
74 return NIC_EXIT_OK
75 }
76 let dom: *u8 = argv[NIC_ARG_VERB] as *u8
77 var only: *u8 = "" as *u8
78 if argc > NIC_ARG_RUNG { only = argv[NIC_ARG_RUNG] as *u8 }
79 let g: *i64 = inc_load(c)
80 inc_envelope(g, out, NIC_OUT_CAP)
81 nic_w(out); nic_w("\n" as *u8)
82 if g[INC_G_STATE] == INC_S_ABSENT { nic_w("INCUMBENTS verdict=INDEX-ABSENT (run nx_incumbents index)\n" as *u8); return NIC_EXIT_ABSENT }
83 if g[INC_G_STATE] != INC_S_FRESH { nic_w("INCUMBENTS verdict=INDEX-" as *u8); nic_w(inc_state_name(g[INC_G_STATE])); nic_w(" (refused as an answer; run nx_incumbents index)\n" as *u8); return NIC_EXIT_REFUSED }
84 let lenp: *i64 = sys_mmap(INC_I64) as *i64
85 let which: *i64 = sys_mmap(INC_I64) as *i64
86 lenp[0] = 0
87 let plan: *u8 = bf_read_plan(ct_first_published(), ct_second_published(), dom, lenp, which)
88 if lenp[0] <= 0 { nic_w("INCUMBENTS domain=" as *u8); nic_w(dom); nic_w(" plan=UNREADABLE-IN-BOTH-TREES verdict=NO-PLAN\n" as *u8); return NIC_EXIT_ABSENT }
89 let n: i64 = lenp[0]
90 let rid: *u8 = sys_mmap(INC_NAME_CAP)
91 let title: *u8 = sys_mmap(n + 1)
92 let sym: *u8 = sys_mmap(INC_NAME_CAP)
93 let note: *u8 = sys_mmap(n + 1)
94 var rungs: i64 = 0
95 var answered: i64 = 0
96 var none: i64 = 0
97 var p: i64 = 0
98 while p < n {
99 let e: i64 = bf_line_end(plan, n, p)
100 if bf_line_starts(plan, p, e, NIC_RUNG_TAG) == 1 {
101 rungs = rungs + 1
102 nic_field(plan, p, e, NIC_F_ID, rid, INC_NAME_CAP)
103 var take: i64 = 1
104 if only[0] != (0 as u8) { if inc_streq(only, rid) == 0 { take = 0 } }
105 if take == 1 {
106 nic_field(plan, p, e, NIC_F_TITLE, title, n + 1)
107 nic_field(plan, p, e, NIC_F_SYM, sym, INC_NAME_CAP)
108 nic_field(plan, p, e, NIC_F_NOTE, note, n + 1)
109 let r: *i64 = inc_match(g, c, title, sym, note, "" as *u8)
110 inc_render(g, c, r, rid, out, NIC_OUT_CAP)
111 nic_w(" " as *u8); nic_w(out); nic_w("\n" as *u8)
112 answered = answered + 1
113 if r[INC_R_N] == 0 { none = none + 1 }
114 }
115 }
116 p = e + 1
117 }
118 nic_w("INCUMBENTS domain=" as *u8); nic_w(dom)
119 nic_w(" rungs=" as *u8); nic_wn(rungs)
120 nic_w(" answered=" as *u8); nic_wn(answered)
121 nic_w(" none=" as *u8); nic_wn(none)
122 nic_w(" verdict=ANSWERED\n" as *u8)
123 return NIC_EXIT_OK
124}