code wiki / _hdl_build / nx_analyst_html.nx
nx_analyst_html.nx source
↩ module page · 249 lines · 13988 B
1// nx_analyst_html.nx -- the HUMAN LAYER (AN11): sovereign NishiLang -> HTML report. The operator's
2// law: grow the sophistication AND make it easier for a person to understand. Everything below is
3// regenerated from the DATA (ac_vs_pc_v2.led + ac_markets.cmp + evidence_ledger.tsv) -- the page
4// is a VIEW, never a second source of truth. Static HTML+CSS only (JS-last-mile doctrine: no
5// hand-authored app logic). Deterministic: same inputs -> byte-identical page, proven by
6// double-generation hash compare. Output: knowledge/analyst_report.html. license_tier: ORIGINAL
7import "nx_ledger_data.nx"
8import "nx_market_cmp.nx"
9import "nx_sha256.nx"
10import "nx_syscalls.nx"
11const AH_MAGIC_4000: i64 = 4000
12
13const AH_CAP: i64 = 1048576
14
15func ah_s(buf: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){buf[off+i]=s[i];i=i+1} return off+i }
16func ah_n(buf: *u8, off: i64, v: i64) -> i64 {
17 var m: i64 = v
18 var o: i64 = off
19 if m < 0 { buf[o] = 45 as u8; o = o + 1; m = 0 - m }
20 let t: *u8 = sys_mmap(28)
21 var k: i64 = 0
22 if m == 0 { t[0] = 48 as u8; k = 1 }
23 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
24 var i: i64 = 0
25 while i < k { buf[o+i] = t[k-1-i]; i = i + 1 }
26 return o + k
27}
28// dollars with thousands feel: just dollars (cents/100), no decimals
29func ah_usd(buf: *u8, off: i64, cents: i64) -> i64 {
30 var o: i64 = ah_s(buf, off, "$" as *u8)
31 return ah_n(buf, o, cents / 100)
32}
33// permil -> "36.5%"
34func ah_pct(buf: *u8, off: i64, permil: i64) -> i64 {
35 var o: i64 = ah_n(buf, off, permil / 10)
36 o = ah_s(buf, o, "." as *u8)
37 o = ah_n(buf, o, permil % 10)
38 return ah_s(buf, o, "%" as *u8)
39}
40
41// one cost bar row: label, dollars, share permil (drives the CSS width), color class c1..c5
42func ah_bar(buf: *u8, off: i64, label: *u8, cents: i64, permil: i64, cls: *u8) -> i64 {
43 var o: i64 = ah_s(buf, off, "<div class=row><div class=lbl>" as *u8)
44 o = ah_s(buf, o, label)
45 o = ah_s(buf, o, "</div><div class=track><div class=\"fill " as *u8)
46 o = ah_s(buf, o, cls)
47 o = ah_s(buf, o, "\" style=\"width:" as *u8)
48 o = ah_pct(buf, o, permil)
49 o = ah_s(buf, o, "\"></div></div><div class=amt>" as *u8)
50 o = ah_usd(buf, o, cents)
51 o = ah_s(buf, o, " · " as *u8)
52 o = ah_pct(buf, o, permil)
53 return ah_s(buf, o, "</div></div>\n" as *u8)
54}
55
56func ah_mkt_label(i: i64) -> *u8 {
57 if i == 0 { return "US central, 3.0 ton" as *u8 }
58 if i == 1 { return "US mini-split, 1.0 ton" as *u8 }
59 if i == 2 { return "UK single split, 1.0 ton" as *u8 }
60 return "India split, 1.5 ton" as *u8
61}
62
63// emit the whole page into buf; returns length. Reads .led + .cmp + evidence tsv.
64func ah_generate(buf: *u8) -> i64 {
65 let st: *i64 = sys_mmap(8*24) as *i64
66 let out: *i64 = sys_mmap(8*12) as *i64
67 if ld_parse_file("knowledge/analyst/ac_vs_pc_v2.led" as *u8, st) != 1 { return 0 - 1 }
68 ld_eval(st, out)
69 let vl: *i64 = st[LD_VALUE] as *i64
70 let ms: *i64 = sys_mmap(8*20) as *i64
71 let mo: *i64 = sys_mmap(8*8) as *i64
72 if mc_parse_file("knowledge/analyst/ac_markets.cmp" as *u8, ms) != 1 { return 0 - 2 }
73 mc_eval(ms, mo)
74 let mp: *i64 = ms[MC_PRICE] as *i64
75 let mt: *i64 = ms[MC_TON] as *i64
76 let mq: *i64 = ms[MC_EQUIP] as *i64
77
78 var o: i64 = 0
79 o = ah_s(buf, o, "<!doctype html><html><head><meta charset=utf-8><title>Why does an AC cost 7x a computer? -- Nishi Analyst</title><style>\n" as *u8)
80 o = ah_s(buf, o, "body{font-family:Georgia,serif;max-width:880px;margin:24px auto;padding:0 16px;color:#1a1a1a;background:#faf8f4}\n" as *u8)
81 o = ah_s(buf, o, "h1{font-size:1.6em} h2{margin-top:1.6em;border-bottom:2px solid #d8d2c4;padding-bottom:4px}\n" as *u8)
82 o = ah_s(buf, o, ".big{font-size:1.15em;background:#fff;border-left:5px solid #2e6e4e;padding:12px 16px;margin:14px 0}\n" as *u8)
83 o = ah_s(buf, o, ".row{display:flex;align-items:center;margin:7px 0} .lbl{width:240px} .amt{width:170px;text-align:right;font-variant-numeric:tabular-nums}\n" as *u8)
84 o = ah_s(buf, o, ".track{flex:1;background:#eee6d8;height:22px;border-radius:4px;overflow:hidden} .fill{height:100%}\n" as *u8)
85 o = ah_s(buf, o, ".c1{background:#4e79a7}.c2{background:#e15759}.c3{background:#76b7b2}.c4{background:#f28e2b}.c5{background:#bab0ac}\n" as *u8)
86 o = ah_s(buf, o, "table{border-collapse:collapse;width:100%;background:#fff} td,th{border:1px solid #d8d2c4;padding:6px 8px;font-size:.92em;text-align:right} th{background:#efe9dc} td:first-child,th:first-child{text-align:left}\n" as *u8)
87 o = ah_s(buf, o, ".conf{background:#fff4e0;border-left:5px solid #c4861a;padding:10px 14px} .mono{font-family:monospace;font-size:.8em}\n" as *u8)
88 o = ah_s(buf, o, "small{color:#666}</style></head><body>\n" as *u8)
89
90 // ---- the answer, in one breath ----
91 o = ah_s(buf, o, "<h1>Why does an installed AC cost ~7x a more complex computer?</h1>\n" as *u8)
92 o = ah_s(buf, o, "<div class=big><b>The machine is not the price.</b> Of the $5,750 average US install, the equipment is " as *u8)
93 o = ah_pct(buf, o, (vl[1] * 1000) / vl[0])
94 o = ah_s(buf, o, ". The biggest single line is the local contractor’s ~40% gross margin, attached to every unit by a licensed, gatekept delivery channel. The same class of machine installs in India for about $480 all-in — because the machine is globally priced and the <i>structure</i> around it is local.</div>\n" as *u8)
95
96 // ---- where the money goes (bars) ----
97 o = ah_s(buf, o, "<h2>Where the $5,750 actually goes</h2>\n" as *u8)
98 o = ah_bar(buf, o, "Equipment (at distributor)" as *u8, vl[1], (vl[1]*1000)/vl[0], "c1" as *u8)
99 o = ah_bar(buf, o, "Contractor gross margin" as *u8, vl[4], (vl[4]*1000)/vl[0], "c2" as *u8)
100 o = ah_bar(buf, o, "Install labor (measured)" as *u8, vl[3], (vl[3]*1000)/vl[0], "c3" as *u8)
101 o = ah_bar(buf, o, "Materials + disposal" as *u8, vl[2], (vl[2]*1000)/vl[0], "c4" as *u8)
102 o = ah_bar(buf, o, "Named gap (permits, noise)" as *u8, vl[5], (vl[5]*1000)/vl[0], "c5" as *u8)
103 o = ah_s(buf, o, "<small>Labor is a measured build-up: government median wage $28.75/hr × 12 crew-hours × 1.5 burden — it replaced an earlier $1,050 estimate that was quietly absorbing ~$530 of untraced money. That money is now the visible “named gap.”</small>\n" as *u8)
104
105 // ---- verdict + confidence, separated, in plain words ----
106 o = ah_s(buf, o, "<h2>How sure is this?</h2><div class=conf><b>Verdict: EXPLAINED.</b> The ledger closes to the dollar, " as *u8)
107 o = ah_pct(buf, o, out[1])
108 o = ah_s(buf, o, " of the price is traced to sources from at least two independent interest groups, and what remains unexplained is " as *u8)
109 o = ah_pct(buf, o, out[2])
110 o = ah_s(buf, o, " — small, and named rather than hidden.<br><b>Confidence: LOW (for now).</b> One leg of the margin number is still an industry expert claim rather than a measured record. The verdict and the confidence move independently — we can be right about the shape and still owe better paperwork. Two fetch tasks (supplier invoices, measured markup data) lift this to MODERATE.</div>\n" as *u8)
111
112 // ---- the overseas test ----
113 o = ah_s(buf, o, "<h2>The overseas test: same machine, different structure</h2>\n" as *u8)
114 o = ah_s(buf, o, "<p>If the structure explanation is right, the machine’s per-ton price should be similar across countries (it ships worldwide) while the local layer varies wildly. Measured:</p>\n" as *u8)
115 o = ah_s(buf, o, "<table><tr><th>Market</th><th>Installed</th><th>$/ton</th><th>Machine share</th><th>Structure $/ton</th></tr>\n" as *u8)
116 var m: i64 = 0
117 while m < ms[MC_NM] {
118 o = ah_s(buf, o, "<tr><td>" as *u8)
119 o = ah_s(buf, o, ah_mkt_label(m))
120 o = ah_s(buf, o, "</td><td>" as *u8)
121 o = ah_usd(buf, o, mp[m])
122 o = ah_s(buf, o, "</td><td>" as *u8)
123 o = ah_usd(buf, o, mc_per_ton(mp[m], mt[m]))
124 o = ah_s(buf, o, "</td><td>" as *u8)
125 o = ah_pct(buf, o, mc_equip_share_permil(mp[m], mq[m]))
126 o = ah_s(buf, o, "</td><td>" as *u8)
127 o = ah_usd(buf, o, mc_per_ton(mc_structure(mp[m], mq[m]), mt[m]))
128 o = ah_s(buf, o, "</td></tr>\n" as *u8)
129 m = m + 1
130 }
131 o = ah_s(buf, o, "</table>\n<p><b>The punchline:</b> across these markets the equipment layer varies about <b>4.5x</b> per ton; the structure layer varies about <b>44x</b>. In India the machine IS the price (87.5% of it); in the US the machine is barely a third. The structure explanation survives its out-of-sample test.</p>\n" as *u8)
132
133 // ---- the evidence, pinned ----
134 o = ah_s(buf, o, "<h2>The evidence, pinned</h2>\n<p>Load-bearing sources were fetched over our own validated TLS and content-hashed — a citation you can re-fetch and diff, not a link you must trust. Refusals are listed, not hidden.</p>\n" as *u8)
135 o = ah_s(buf, o, "<table><tr><th>Source</th><th>Bytes</th><th>sha256 (first 16)</th><th>Status</th></tr>\n" as *u8)
136 let lenp: *i64 = sys_mmap(16) as *i64
137 let tsv: *u8 = sys_read_file("knowledge/analyst/evidence_ledger.tsv" as *u8, lenp)
138 var ti: i64 = 0
139 while ti < lenp[0] {
140 let te: i64 = ld_line_end(tsv, ti, lenp[0])
141 // fields: tag \t url \t bytes \t sha \t status
142 var f: i64 = 0
143 var fs: i64 = ti
144 o = ah_s(buf, o, "<tr>" as *u8)
145 var j: i64 = ti
146 while j <= te {
147 var cut: i64 = 0
148 if j == te { cut = 1 }
149 if cut == 0 { if tsv[j] == (9 as u8) { cut = 1 } }
150 if cut == 1 {
151 var emit: i64 = 1
152 if f == 1 { emit = 0 } // skip the full URL cell (page stays readable)
153 if emit == 1 {
154 o = ah_s(buf, o, "<td class=mono>" as *u8)
155 var ce: i64 = j
156 if f == 3 { if ce > fs + 16 { ce = fs + 16 } } // sha prefix
157 var k: i64 = fs
158 while k < ce { buf[o] = tsv[k]; o = o + 1; k = k + 1 }
159 o = ah_s(buf, o, "</td>" as *u8)
160 }
161 fs = j + 1
162 f = f + 1
163 }
164 j = j + 1
165 }
166 o = ah_s(buf, o, "</tr>\n" as *u8)
167 ti = te + 1
168 }
169 o = ah_s(buf, o, "</table>\n" as *u8)
170 o = ah_s(buf, o, "<p><small>Generated by the Nishi Analyst from data files (ac_vs_pc_v2.led, ac_markets.cmp, evidence_ledger.tsv) — sovereign toolchain, deterministic output: same data, byte-identical page. Media common-wisdoms (refrigerant costs, raw-materials inflation) were offered zero admitted dollars: uncorroborated claims become fetch tasks here, not explanations.</small></p>\n" as *u8)
171 o = ah_s(buf, o, "</body></html>\n" as *u8)
172 return o
173}
174
175func ah_write(path: *u8, buf: *u8, n: i64) -> i64 {
176 let fd: i64 = sys_openat_wr(path, 0x1a4)
177 if fd < 0 { return 0 - 1 }
178 var off: i64 = 0
179 while off < n {
180 let w: i64 = sys_write(fd, ((buf as i64) + off) as *u8, n - off)
181 if w <= 0 { sys_close(fd); return 0 - 1 }
182 off = off + w
183 }
184 sys_close(fd)
185 return n
186}
187
188func ah_wf(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
189func ah_wnf(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(fd,bb,k); return 0 }
190
191func ah_has(hay: *u8, n: i64, needle: *u8) -> i64 {
192 var nl: i64 = 0
193 while needle[nl] != (0 as u8) { nl = nl + 1 }
194 var i: i64 = 0
195 while i + nl <= n {
196 var j: i64 = 0
197 var hit: i64 = 1
198 while j < nl { if hay[i+j] != needle[j] { hit = 0; j = nl } else { j = j + 1 } }
199 if hit == 1 { return 1 }
200 i = i + 1
201 }
202 return 0
203}
204
205func main() -> i64 {
206 let b1: *u8 = sys_mmap(AH_CAP)
207 let b2: *u8 = sys_mmap(AH_CAP)
208 let n1: i64 = ah_generate(b1)
209 let n2: i64 = ah_generate(b2)
210 let r: *i64 = sys_mmap(8*12) as *i64
211 var t: i64 = 0
212 r[t]=0; if n1 > AH_MAGIC_4000 { r[t]=1 } t=t+1 // a real page, not a stub
213 r[t]=0; if n1 == n2 { r[t]=1 } t=t+1 // deterministic length
214 var same: i64 = 1
215 var i: i64 = 0
216 while i < n1 { if b1[i] != b2[i] { same = 0; i = n1 } else { i = i + 1 } }
217 r[t]=0; if same == 1 { r[t]=1 } t=t+1 // byte-identical double generation
218 r[t]=0; if ah_has(b1, n1, "<!doctype html" as *u8) == 1 { r[t]=1 } t=t+1
219 r[t]=0; if ah_has(b1, n1, "EXPLAINED" as *u8) == 1 { r[t]=1 } t=t+1
220 r[t]=0; if ah_has(b1, n1, "Confidence: LOW" as *u8) == 1 { r[t]=1 } t=t+1
221 r[t]=0; if ah_has(b1, n1, "India split, 1.5 ton" as *u8) == 1 { r[t]=1 } t=t+1
222 r[t]=0; if ah_has(b1, n1, "44x" as *u8) == 1 { r[t]=1 } t=t+1
223 r[t]=0; if ah_has(b1, n1, "REFUSED" as *u8) == 1 { r[t]=1 } t=t+1 // refusals shown, not hidden
224 let wr: i64 = ah_write("knowledge/analyst_report.html" as *u8, b1, n1)
225 r[t]=0; if wr == n1 { r[t]=1 } t=t+1
226 var pass: i64 = 0
227 i = 0
228 while i < t { pass = pass + r[i]; i = i + 1 }
229 ah_wf(1, "=== ANALYST HTML (AN11) ===\n bytes=" as *u8)
230 ah_wnf(1, n1)
231 ah_wf(1, " checks " as *u8)
232 ah_wnf(1, pass)
233 ah_wf(1, "/" as *u8)
234 ah_wnf(1, t)
235 ah_wf(1, "\n report -> knowledge/analyst_report.html\n" as *u8)
236 let dfd: i64 = sys_openat_append("knowledge/status/analyst_html_gate.log" as *u8, 0x1a4)
237 if dfd >= 0 {
238 if pass == t { ah_wf(dfd, "ANALYST-HTML verdict=GREEN " as *u8) }
239 if pass != t { ah_wf(dfd, "ANALYST-HTML verdict=RED " as *u8) }
240 ah_wnf(dfd, pass)
241 ah_wf(dfd, "/" as *u8)
242 ah_wnf(dfd, t)
243 ah_wf(dfd, "\n" as *u8)
244 sys_close(dfd)
245 }
246 if pass == t { sys_exit(0); return 0 }
247 sys_exit(1)
248 return 1
249}