code wiki / _hdl_build / nx_eff_board_lib.nx
nx_eff_board_lib.nx source
↩ module page · 374 lines · 29265 B
1// nx_eff_board_lib.nx -- shared core for the efficiency / ROI-investment board (eff lane F732).
2// Split lib+CLI+gate like nx_pm_roi_lib (D001 gate-able). Reads the eff- plane (measured spend rows
3// seeded laptop-side by nx_eff_scan) + the roi- plane (value, same plane the pm-cockpit reads),
4// converts via nx_eff_lib's DECLARED coefficients, emits JSON/HTML. Fail-closed: unseeded -> UNSEEDED,
5// no fabricated spend. This is the COST/INVESTMENT half of ROI (value half = nx_pm_cockpit).
6// license_tier: ORIGINAL No hw writes (Rule 26).
7import "nx_eff_conv.nx"
8import "nx_store_seed_lib.nx"
9const EB_MAGIC_3600: i64 = 3600
10const EB_MAGIC_1000000: i64 = 1000000
11
12const EB_PLANE_CAP: i64 = 524288
13const EB_OUT_CAP: i64 = 262144
14const EB_MAXCOL: i64 = 16
15const EB_MONTH_DAYS: i64 = 30
16const EB_TAB: i64 = 9
17const EB_NL: i64 = 10
18const EB_HASH: i64 = 35
19const EB_ZERO: i64 = 48
20const EB_NINE: i64 = 57
21const EB_HTMLH: i64 = 104
22const EB_MAXDAYS: i64 = 28
23const M_ACT: i64 = 0
24const M_SEC: i64 = 1
25const M_TIN: i64 = 2
26const M_TOUT: i64 = 3
27const M_TCR: i64 = 4
28const M_TCW: i64 = 5
29const M_DAYS: i64 = 6
30const M_WIN: i64 = 7
31const M_TRUNC: i64 = 8
32const M_TRF: i64 = 9
33const M_GEN: i64 = 10
34const M_MCP: i64 = 11
35const M_SH: i64 = 12
36const M_FILE: i64 = 13
37const M_AVAIL: i64 = 14
38const M_ROIAV: i64 = 15
39const M_ROICENTS: i64 = 16
40const M_MODBASE: i64 = 20 // 20 slots: m[M_MODBASE + tier*4 + class] (class 0=in 1=out 2=cr 3=cw)
41const M_MODSEEN: i64 = 40 // 1 if per-model rows present -> real cost available
42
43func eb_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
44func eb_cols(buf: *u8, ls: i64, le: i64, sp: *i64) -> i64 {
45 var c: i64 = 0
46 var p: i64 = ls
47 while c < EB_MAXCOL {
48 var e: i64 = p
49 var s: i64 = 1
50 while s == 1 { if e >= le { s = 0 } else { if buf[e] == (EB_TAB as u8) { s = 0 } else { e = e + 1 } } }
51 sp[c*2] = p
52 sp[c*2+1] = e
53 c = c + 1
54 if e >= le { return c }
55 p = e + 1
56 }
57 return c
58}
59func eb_slice_int(buf: *u8, a: i64, b: i64) -> i64 {
60 var v: i64 = 0
61 var i: i64 = a
62 while i < b { let c: i64 = buf[i]; if c >= EB_ZERO { if c <= EB_NINE { v = v * 10 + (c - EB_ZERO) } } i = i + 1 }
63 return v
64}
65func eb_slice_eq(buf: *u8, a: i64, b: i64, lit: *u8) -> i64 {
66 let l: i64 = eb_len(lit)
67 if b - a != l { return 0 }
68 var i: i64 = 0
69 while i < l { if buf[a+i] != lit[i] { return 0 } i = i + 1 }
70 return 1
71}
72func eb_is_dayrow(buf: *u8, a: i64, b: i64) -> i64 {
73 if b - a < 2 { return 0 }
74 if buf[a] != (100 as u8) { return 0 }
75 var i: i64 = a + 1
76 while i < b { let c: i64 = buf[i]; if c < EB_ZERO { return 0 } if c > EB_NINE { return 0 } i = i + 1 }
77 return 1
78}
79// col0 == 'm' + a single tier digit 0..(EFL_NTIER-1) => a per-model spend row; returns tier idx or -1
80func eb_modrow_tier(buf: *u8, a: i64, b: i64) -> i64 {
81 if b - a != 2 { return 0 - 1 }
82 if buf[a] != (109 as u8) { return 0 - 1 }
83 let d: i64 = buf[a+1] as i64
84 if d < EB_ZERO { return 0 - 1 }
85 if d > EB_NINE { return 0 - 1 }
86 let t: i64 = d - EB_ZERO
87 if t >= EFL_NTIER { return 0 - 1 }
88 return t
89}
90// real per-model-attributed cost cents = sum over tiers of efl_cost_tier(tier tokens x that tier's rate)
91func eb_realcost(m: *i64) -> i64 {
92 var tot: i64 = 0
93 var t: i64 = 0
94 while t < EFL_NTIER {
95 let b: i64 = M_MODBASE + t * 4
96 tot = tot + efl_cost_tier(t, m[b], m[b+1], m[b+2], m[b+3])
97 t = t + 1
98 }
99 return tot
100}
101// ---- FinOps efficiency KPIs (actionable signals; the lane's whole point) ----
102// cost per logged action, in cents (real cost / actions)
103func eb_cost_per_action(cost: i64, actions: i64) -> i64 { if actions <= 0 { return 0 } return cost / actions }
104// cache-hit ratio = cache_read as % of all input-side tokens (input + cache_read + cache_write).
105// High = context caching is working (each turn re-reads cheaply instead of re-sending). The efficiency win.
106func eb_cache_ratio_pct(m: *i64) -> i64 {
107 let denom: i64 = m[M_TIN] + m[M_TCR] + m[M_TCW]
108 if denom <= 0 { return 0 }
109 return m[M_TCR] * 100 / denom
110}
111func eb_eff_scan(buf: *u8, n: i64, m: *i64) -> i64 {
112 if n <= 0 { m[M_AVAIL] = 0; return 0 }
113 let sp: *i64 = sys_mmap(EB_MAXCOL*2*8) as *i64
114 var days: i64 = 0
115 var i: i64 = 0
116 while i < n {
117 var le: i64 = i
118 var s: i64 = 1
119 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (EB_NL as u8) { s = 0 } else { le = le + 1 } } }
120 if le > i { if buf[i] != (EB_HASH as u8) {
121 let nc: i64 = eb_cols(buf, i, le, sp)
122 if eb_is_dayrow(buf, sp[0], sp[1]) == 1 { if nc >= 12 {
123 days = days + 1
124 m[M_ACT] = m[M_ACT] + eb_slice_int(buf, sp[4], sp[5])
125 m[M_MCP] = m[M_MCP] + eb_slice_int(buf, sp[6], sp[7])
126 m[M_SH] = m[M_SH] + eb_slice_int(buf, sp[8], sp[9])
127 m[M_FILE] = m[M_FILE] + eb_slice_int(buf, sp[10], sp[11])
128 m[M_SEC] = m[M_SEC] + eb_slice_int(buf, sp[12], sp[13])
129 m[M_TIN] = m[M_TIN] + eb_slice_int(buf, sp[14], sp[15])
130 m[M_TOUT] = m[M_TOUT] + eb_slice_int(buf, sp[16], sp[17])
131 m[M_TCR] = m[M_TCR] + eb_slice_int(buf, sp[18], sp[19])
132 m[M_TCW] = m[M_TCW] + eb_slice_int(buf, sp[20], sp[21])
133 m[M_TRF] = m[M_TRF] + eb_slice_int(buf, sp[22], sp[23])
134 } }
135 if eb_slice_eq(buf, sp[0], sp[1], "meta" as *u8) == 1 { if nc >= 7 {
136 m[M_WIN] = eb_slice_int(buf, sp[2], sp[3])
137 m[M_TRUNC] = eb_slice_int(buf, sp[10], sp[11])
138 m[M_GEN] = eb_slice_int(buf, sp[12], sp[13])
139 } }
140 let mtier: i64 = eb_modrow_tier(buf, sp[0], sp[1])
141 if mtier >= 0 { if nc >= 5 {
142 let mb: i64 = M_MODBASE + mtier * 4
143 m[mb] = eb_slice_int(buf, sp[2], sp[3])
144 m[mb+1] = eb_slice_int(buf, sp[4], sp[5])
145 m[mb+2] = eb_slice_int(buf, sp[6], sp[7])
146 m[mb+3] = eb_slice_int(buf, sp[8], sp[9])
147 m[M_MODSEEN] = 1
148 } }
149 } }
150 i = le + 1
151 }
152 m[M_DAYS] = days
153 if days > 0 { m[M_AVAIL] = 1 }
154 return days
155}
156func eb_roi_value(buf: *u8, n: i64, m: *i64) -> i64 {
157 if n <= 0 { m[M_ROIAV] = 0; m[M_ROICENTS] = 0; return 0 }
158 let sp: *i64 = sys_mmap(EB_MAXCOL*2*8) as *i64
159 var totc: i64 = 0
160 var i: i64 = 0
161 while i < n {
162 var le: i64 = i
163 var s: i64 = 1
164 while s == 1 { if le >= n { s = 0 } else { if buf[le] == (EB_NL as u8) { s = 0 } else { le = le + 1 } } }
165 if le > i { if buf[i] != (EB_HASH as u8) {
166 let nc: i64 = eb_cols(buf, i, le, sp)
167 if nc >= 6 {
168 let qty: i64 = eb_slice_int(buf, sp[6], sp[7])
169 let cents: i64 = eb_slice_int(buf, sp[8], sp[9])
170 totc = totc + qty * cents
171 }
172 } }
173 i = le + 1
174 }
175 m[M_ROIAV] = 1
176 m[M_ROICENTS] = totc
177 return totc
178}
179func eb_money(d: *u8, o: i64, cents: i64) -> i64 {
180 var oo: i64 = o
181 var c: i64 = cents
182 if c < 0 { oo = ss_cat(d, oo, "-" as *u8); c = 0 - c }
183 oo = ss_cat(d, oo, "$" as *u8)
184 oo = ss_catn(d, oo, c / 100)
185 oo = ss_cat(d, oo, "." as *u8)
186 let r: i64 = c % 100
187 if r < 10 { oo = ss_cat(d, oo, "0" as *u8) }
188 oo = ss_catn(d, oo, r)
189 return oo
190}
191func eb_monthly(v: i64, win: i64) -> i64 { if win <= 0 { return v } return v * EB_MONTH_DAYS / win }
192
193func eb_emit_json(out: *u8, m: *i64) -> i64 {
194 var o: i64 = 0
195 let win: i64 = m[M_WIN]
196 let flatcost: i64 = efl_cost_cents(m[M_TIN], m[M_TOUT], m[M_TCR], m[M_TCW])
197 var cost: i64 = flatcost
198 if m[M_MODSEEN] == 1 { cost = eb_realcost(m) }
199 let wh: i64 = efl_energy_wh(m[M_TIN], m[M_TOUT], m[M_TCR], m[M_TCW])
200 let mcost: i64 = eb_monthly(cost, win)
201 let mval: i64 = m[M_ROICENTS]
202 let sec_hr: i64 = m[M_SEC] / EB_MAGIC_3600
203 let netv: i64 = mval - mcost
204 let c_in: i64 = efl_cost_cents(m[M_TIN], 0, 0, 0)
205 let c_out: i64 = efl_cost_cents(0, m[M_TOUT], 0, 0)
206 let c_cr: i64 = efl_cost_cents(0, 0, m[M_TCR], 0)
207 let c_cw: i64 = efl_cost_cents(0, 0, 0, m[M_TCW])
208 let cm_fable: i64 = efl_cost_tier(EFL_T_FABLE, m[M_MODBASE], m[M_MODBASE+1], m[M_MODBASE+2], m[M_MODBASE+3])
209 let cm_opus: i64 = efl_cost_tier(EFL_T_OPUS, m[M_MODBASE+4], m[M_MODBASE+5], m[M_MODBASE+6], m[M_MODBASE+7])
210 let cm_sonnet: i64 = efl_cost_tier(EFL_T_SONNET, m[M_MODBASE+8], m[M_MODBASE+9], m[M_MODBASE+10], m[M_MODBASE+11])
211 let cm_haiku: i64 = efl_cost_tier(EFL_T_HAIKU, m[M_MODBASE+12], m[M_MODBASE+13], m[M_MODBASE+14], m[M_MODBASE+15])
212 let cm_other: i64 = efl_cost_tier(EFL_T_OTHER, m[M_MODBASE+16], m[M_MODBASE+17], m[M_MODBASE+18], m[M_MODBASE+19])
213 o = ss_cat(out, o, "{\"tool\":\"nx_eff_board\",\"epoch\":" as *u8); o = ss_catn(out, o, sys_now_realtime_sec())
214 o = ss_cat(out, o, ",\"envelope\":{\"plane_cap\":" as *u8); o = ss_catn(out, o, EB_PLANE_CAP)
215 o = ss_cat(out, o, ",\"window_days\":" as *u8); o = ss_catn(out, o, win)
216 o = ss_cat(out, o, ",\"days_with_data\":" as *u8); o = ss_catn(out, o, m[M_DAYS])
217 o = ss_cat(out, o, ",\"transcript_truncations\":" as *u8); o = ss_catn(out, o, m[M_TRUNC])
218 o = ss_cat(out, o, ",\"price_cents_per_Mtok_input\":" as *u8); o = ss_catn(out, o, EFL_CENTS_PER_MTOK_IN)
219 o = ss_cat(out, o, ",\"energy_basis\":\"compute-weighted (output 5x, cache-read 0.1x, cache-write 1.25x vs input 1x -- energy tracks FLOPs, so cache reads are cheap not full-energy); calibrated to ~5e-4 Wh/output-token from fetched research (Joule 2026 S2542-4351(26)00114-5; TokenPowerBench arXiv 2512.03024; GPT-4o 3e-4, LLaMA-3.1-405B 8.6e-4 Wh/tok); ESTIMATE, conservative-mid (reasoning models trend higher)\"" as *u8)
220 o = ss_cat(out, o, ",\"cost_multipliers\":\"input1x output5x cache-read0.1x cache-write1.25x\",\"truncation\":\"marked\"}" as *u8)
221 if m[M_AVAIL] == 0 {
222 o = ss_cat(out, o, ",\"spend\":{\"status\":\"UNSEEDED\",\"note\":\"knowledge/store/eff- absent -- run nx_eff_scan laptop-side + push rows; no spend invented\"}}" as *u8)
223 o = ss_cat(out, o, "\n" as *u8)
224 return o
225 }
226 o = ss_cat(out, o, ",\"spend_measured\":{\"actions\":" as *u8); o = ss_catn(out, o, m[M_ACT])
227 o = ss_cat(out, o, ",\"active_hours\":" as *u8); o = ss_catn(out, o, sec_hr)
228 o = ss_cat(out, o, ",\"tokens\":{\"input\":" as *u8); o = ss_catn(out, o, m[M_TIN])
229 o = ss_cat(out, o, ",\"output\":" as *u8); o = ss_catn(out, o, m[M_TOUT])
230 o = ss_cat(out, o, ",\"cache_read\":" as *u8); o = ss_catn(out, o, m[M_TCR])
231 o = ss_cat(out, o, ",\"cache_write\":" as *u8); o = ss_catn(out, o, m[M_TCW])
232 o = ss_cat(out, o, "},\"action_mix\":{\"mcp\":" as *u8); o = ss_catn(out, o, m[M_MCP])
233 o = ss_cat(out, o, ",\"shell\":" as *u8); o = ss_catn(out, o, m[M_SH])
234 o = ss_cat(out, o, ",\"file\":" as *u8); o = ss_catn(out, o, m[M_FILE])
235 o = ss_cat(out, o, "}}" as *u8)
236 let cpa: i64 = eb_cost_per_action(cost, m[M_ACT])
237 let cratio: i64 = eb_cache_ratio_pct(m)
238 o = ss_cat(out, o, ",\"efficiency_kpi\":{\"cost_per_action_cents\":" as *u8); o = ss_catn(out, o, cpa)
239 o = ss_cat(out, o, ",\"cache_hit_ratio_pct\":" as *u8); o = ss_catn(out, o, cratio)
240 o = ss_cat(out, o, ",\"basis\":\"cost_per_action = real per-model cost / logged actions (FinOps unit economics); cache_hit_ratio = cache_read as pct of all input-side tokens (high = context caching working = each turn re-reads cheaply). These are the actionable efficiency levers.\"}" as *u8)
241 o = ss_cat(out, o, ",\"spend_estimate\":{\"status\":\"ESTIMATE\",\"window_cost_cents\":" as *u8); o = ss_catn(out, o, cost)
242 o = ss_cat(out, o, ",\"monthly_cost_cents\":" as *u8); o = ss_catn(out, o, mcost)
243 o = ss_cat(out, o, ",\"window_energy_wh\":" as *u8); o = ss_catn(out, o, wh)
244 o = ss_cat(out, o, ",\"cost_by_class_cents\":{\"input\":" as *u8); o = ss_catn(out, o, c_in)
245 o = ss_cat(out, o, ",\"output\":" as *u8); o = ss_catn(out, o, c_out)
246 o = ss_cat(out, o, ",\"cache_read\":" as *u8); o = ss_catn(out, o, c_cr)
247 o = ss_cat(out, o, ",\"cache_write\":" as *u8); o = ss_catn(out, o, c_cw)
248 o = ss_cat(out, o, "},\"real_cost_cents\":" as *u8); o = ss_catn(out, o, cost)
249 o = ss_cat(out, o, ",\"flat_ceiling_cents\":" as *u8); o = ss_catn(out, o, flatcost)
250 o = ss_cat(out, o, ",\"cost_basis\":\"" as *u8)
251 if m[M_MODSEEN] == 1 { o = ss_cat(out, o, "PER-MODEL-ATTRIBUTED (real; transcript model field x rates fetched 2026-07-19 platform.claude.com)" as *u8) } else { o = ss_cat(out, o, "flat-ceiling (per-model rows absent)" as *u8) }
252 o = ss_cat(out, o, "\",\"by_model_cents\":{\"fable\":" as *u8); o = ss_catn(out, o, cm_fable)
253 o = ss_cat(out, o, ",\"opus\":" as *u8); o = ss_catn(out, o, cm_opus)
254 o = ss_cat(out, o, ",\"sonnet\":" as *u8); o = ss_catn(out, o, cm_sonnet)
255 o = ss_cat(out, o, ",\"haiku\":" as *u8); o = ss_catn(out, o, cm_haiku)
256 o = ss_cat(out, o, ",\"other\":" as *u8); o = ss_catn(out, o, cm_other)
257 o = ss_cat(out, o, "},\"caveat\":\"cost is PER-MODEL ATTRIBUTED (flat_ceiling = old Opus-flat ~5x upper bound); cache ops dominate. The value side (roi-) is a conservative floor, so a negative net reflects an UNDER-COUNTED value plane, not true unprofitability\"}" as *u8)
258 if m[M_ROIAV] == 1 {
259 o = ss_cat(out, o, ",\"roi\":{\"status\":\"ESTIMATE\",\"monthly_value_cents\":" as *u8); o = ss_catn(out, o, mval)
260 o = ss_cat(out, o, ",\"monthly_cost_cents\":" as *u8); o = ss_catn(out, o, mcost)
261 o = ss_cat(out, o, ",\"net_monthly_cents\":" as *u8); o = ss_catn(out, o, netv)
262 var ratio: i64 = 0
263 if mcost > 0 { ratio = mval * 100 / mcost }
264 o = ss_cat(out, o, ",\"roi_pct\":" as *u8); o = ss_catn(out, o, ratio)
265 o = ss_cat(out, o, ",\"net_status\":\"" as *u8)
266 if m[M_MODSEEN] == 1 { o = ss_cat(out, o, "PER_MODEL_ATTRIBUTED" as *u8) } else { o = ss_cat(out, o, "PENDING_PER_MODEL_RECONCILIATION" as *u8) }
267 o = ss_cat(out, o, "\",\"basis\":\"cost = per-model attributed (real); value = conservative roi- floor (under-seeded -> a negative net reflects an under-counted value plane, not true loss). return also drives /pm; ROI-CONVERGE tracks value maturation\",\"value_detail\":\"/pm\"}" as *u8)
268 } else {
269 o = ss_cat(out, o, ",\"roi\":{\"status\":\"VALUE_UNSEEDED\",\"note\":\"knowledge/store/roi- absent -- investment measured but no value rows; seed roi- to compute the ratio\"}" as *u8)
270 }
271 o = ss_cat(out, o, ",\"feedback\":[\"cache read+write dominate estimated cost -> the efficiency lever is CONTEXT/CACHE management, not output brevity\",\"active_hours is a gap300-strict LOWER bound (idle>5min + untracked work not counted)\",\"" as *u8)
272 o = ss_catn(out, o, m[M_TRUNC])
273 o = ss_cat(out, o, " transcripts hit the 32MiB read cap -> those token totals are undercounts\"]" as *u8)
274 o = ss_cat(out, o, ",\"research_opps\":[\"DONE per-message model attribution -> exact multi-model pricing\",\"DONE energy grounded in fetched research (compute-weighted, ~5e-4 Wh/output-tok)\",\"stream transcripts (kill the 32MiB truncation) for exact token totals\",\"message-timestamp day-bucketing (currently session-mtime granular)\",\"real per-NODE hardware energy via RAPL/rdtsc (good-triangle) for OUR compute -- distinct from the inference-energy estimate (Anthropic servers are unmeasurable from here)\"]" as *u8)
275 o = ss_cat(out, o, ",\"fetch_targets\":[\"Anthropic per-model token pricing table\",\"published LLM inference energy Wh/token measurements\",\"Langfuse/Helicone/OpenLLMetry token-cost observability schemas\",\"CodeCarbon/Kepler/Scaphandre software energy attribution\",\"DORA/SPACE developer-throughput metric definitions\"]" as *u8)
276 o = ss_cat(out, o, ",\"links\":{\"exec_dashboard\":\"/pm\",\"program\":\"/frontier\",\"coordination\":\"/standup\",\"hub\":\"/compare\"}}" as *u8)
277 o = ss_cat(out, o, "\n" as *u8)
278 return o
279}
280func eb_emit_html(out: *u8, m: *i64) -> i64 {
281 var o: i64 = 0
282 let win: i64 = m[M_WIN]
283 let flatcost: i64 = efl_cost_cents(m[M_TIN], m[M_TOUT], m[M_TCR], m[M_TCW])
284 var cost: i64 = flatcost
285 if m[M_MODSEEN] == 1 { cost = eb_realcost(m) }
286 let mcost: i64 = eb_monthly(cost, win)
287 let wh: i64 = efl_energy_wh(m[M_TIN], m[M_TOUT], m[M_TCR], m[M_TCW])
288 let mval: i64 = m[M_ROICENTS]
289 let c_in: i64 = efl_cost_cents(m[M_TIN],0,0,0)
290 let c_out: i64 = efl_cost_cents(0,m[M_TOUT],0,0)
291 let c_cr: i64 = efl_cost_cents(0,0,m[M_TCR],0)
292 let c_cw: i64 = efl_cost_cents(0,0,0,m[M_TCW])
293 let cm_fable: i64 = efl_cost_tier(EFL_T_FABLE, m[M_MODBASE], m[M_MODBASE+1], m[M_MODBASE+2], m[M_MODBASE+3])
294 let cm_opus: i64 = efl_cost_tier(EFL_T_OPUS, m[M_MODBASE+4], m[M_MODBASE+5], m[M_MODBASE+6], m[M_MODBASE+7])
295 let cm_sonnet: i64 = efl_cost_tier(EFL_T_SONNET, m[M_MODBASE+8], m[M_MODBASE+9], m[M_MODBASE+10], m[M_MODBASE+11])
296 let cm_haiku: i64 = efl_cost_tier(EFL_T_HAIKU, m[M_MODBASE+12], m[M_MODBASE+13], m[M_MODBASE+14], m[M_MODBASE+15])
297 let cm_other: i64 = efl_cost_tier(EFL_T_OTHER, m[M_MODBASE+16], m[M_MODBASE+17], m[M_MODBASE+18], m[M_MODBASE+19])
298 let sec_hr: i64 = m[M_SEC] / EB_MAGIC_3600
299 let toktot_m: i64 = (m[M_TIN]+m[M_TOUT]+m[M_TCR]+m[M_TCW]) / EB_MAGIC_1000000
300 let wh_k: i64 = wh / 1000
301 let tin_k: i64 = m[M_TIN] / 1000
302 let tout_k: i64 = m[M_TOUT] / 1000
303 let tcr_m: i64 = m[M_TCR] / EB_MAGIC_1000000
304 let tcw_m: i64 = m[M_TCW] / EB_MAGIC_1000000
305 let netv: i64 = mval - mcost
306 let cpa: i64 = eb_cost_per_action(cost, m[M_ACT])
307 let cratio: i64 = eb_cache_ratio_pct(m)
308 o = ss_cat(out, o, "<!doctype html><html lang=en><head><meta charset=utf-8><meta name=viewport content='width=device-width,initial-scale=1'><title>Nishi Efficiency & ROI</title><style>" as *u8)
309 o = ss_cat(out, o, "body{margin:0;font:15px/1.5 -apple-system,Segoe UI,Roboto,sans-serif;background:#0e1116;color:#e6edf3}header{padding:26px 24px;background:linear-gradient(135deg,#161b22,#0e1116);border-bottom:1px solid #30363d}" as *u8)
310 o = ss_cat(out, o, "h1{margin:0 0 6px;font-size:22px}.sub{color:#8b949e;font-size:14px}h2{margin:24px 24px 8px;font-size:13px;text-transform:uppercase;letter-spacing:.08em;color:#8b949e}" as *u8)
311 o = ss_cat(out, o, "table{border-collapse:collapse;margin:0 24px 8px;max-width:1020px}td,th{padding:7px 10px;border-bottom:1px solid #21262d;text-align:left;font-size:14px}th{color:#8b949e;font-size:12px;text-transform:uppercase}" as *u8)
312 o = ss_cat(out, o, ".g{color:#3fb950}.a{color:#d29922}.r{color:#f85149}.m{color:#8b949e;font-size:12px}a{color:#58a6ff;text-decoration:none}" as *u8)
313 o = ss_cat(out, o, ".tile{display:inline-block;background:#161b22;border:1px solid #30363d;border-radius:10px;padding:12px 18px;margin:10px 8px 0 0;min-width:140px}.tile .n{font-size:24px;font-weight:700}.tile .l{color:#8b949e;font-size:12px}" as *u8)
314 o = ss_cat(out, o, "footer{padding:18px 24px;color:#6e7681;font-size:12px;border-top:1px solid #21262d;margin-top:22px}</style></head><body>" as *u8)
315 o = ss_cat(out, o, "<header><h1>Nishi Efficiency & ROI — operations spend</h1><div class=sub>measured from god: worklog + transcripts → time, tokens, money, energy. The INVESTMENT half of ROI (value half: <a href=/pm>/pm</a>). Measured facts vs declared-rate estimates, labeled.</div>" as *u8)
316 if m[M_AVAIL] == 0 {
317 o = ss_cat(out, o, "<p class=a>UNSEEDED — the knowledge/store/eff- plane is absent. Run nx_eff_scan laptop-side and push its rows; no spend is invented.</p></header></body></html>\n" as *u8)
318 return o
319 }
320 o = ss_cat(out, o, "<div><span class=tile><div class=n>" as *u8); o = eb_money(out, o, cost)
321 o = ss_cat(out, o, "</div><div class=l>real spend / " as *u8); o = ss_catn(out, o, win); o = ss_cat(out, o, "d (per-model)</div></span>" as *u8)
322 o = ss_cat(out, o, "<span class=tile><div class=n>" as *u8); o = ss_catn(out, o, toktot_m); o = ss_cat(out, o, "M</div><div class=l>tokens (measured)</div></span>" as *u8)
323 o = ss_cat(out, o, "<span class=tile><div class=n>" as *u8); o = ss_catn(out, o, sec_hr); o = ss_cat(out, o, "h</div><div class=l>active time (floor)</div></span>" as *u8)
324 o = ss_cat(out, o, "<span class=tile><div class=n>" as *u8); o = ss_catn(out, o, wh_k); o = ss_cat(out, o, " kWh</div><div class=l>energy (ESTIMATE)</div></span>" as *u8)
325 o = ss_cat(out, o, "<span class=tile><div class=n>" as *u8); o = eb_money(out, o, cpa); o = ss_cat(out, o, "</div><div class=l>cost / action (unit econ)</div></span>" as *u8)
326 o = ss_cat(out, o, "<span class=tile><div class=n>" as *u8); o = ss_catn(out, o, cratio); o = ss_cat(out, o, "%</div><div class=l>cache-hit ratio (caching works)</div></span>" as *u8)
327 if m[M_ROIAV] == 1 {
328 o = ss_cat(out, o, "<span class=tile><div class=n>" as *u8); o = eb_money(out, o, mval); o = ss_cat(out, o, "</div><div class=l>value floor / mo (roi-)</div></span>" as *u8)
329 }
330 o = ss_cat(out, o, "</div></header>" as *u8)
331 o = ss_cat(out, o, "<h2>Operations — measured spend (hard facts)</h2><table><tr><th>meter</th><th>value</th><th>source</th></tr>" as *u8)
332 o = ss_cat(out, o, "<tr><td>actions logged</td><td>" as *u8); o = ss_catn(out, o, m[M_ACT]); o = ss_cat(out, o, " (mcp " as *u8); o = ss_catn(out, o, m[M_MCP]); o = ss_cat(out, o, " / shell " as *u8); o = ss_catn(out, o, m[M_SH]); o = ss_cat(out, o, " / file " as *u8); o = ss_catn(out, o, m[M_FILE]); o = ss_cat(out, o, ")</td><td class=m>worklog.tsv rows</td></tr>" as *u8)
333 o = ss_cat(out, o, "<tr><td>active time</td><td>" as *u8); o = ss_catn(out, o, sec_hr); o = ss_cat(out, o, "h (gap300-strict floor)</td><td class=m>inter-action gaps ≤ 5min</td></tr>" as *u8)
334 o = ss_cat(out, o, "<tr><td>tokens</td><td>in " as *u8); o = ss_catn(out, o, tin_k); o = ss_cat(out, o, "k · out " as *u8); o = ss_catn(out, o, tout_k); o = ss_cat(out, o, "k · cache-rd " as *u8); o = ss_catn(out, o, tcr_m); o = ss_cat(out, o, "M · cache-wr " as *u8); o = ss_catn(out, o, tcw_m); o = ss_cat(out, o, "M</td><td class=m>transcript usage fields</td></tr></table>" as *u8)
335 o = ss_cat(out, o, "<h2>Estimated cost by token class — where the money goes</h2><table><tr><th>class</th><th>multiplier</th><th>est. cost / window</th></tr>" as *u8)
336 o = ss_cat(out, o, "<tr><td>input (fresh)</td><td class=m>1.0×</td><td>" as *u8); o = eb_money(out, o, c_in); o = ss_cat(out, o, "</td></tr>" as *u8)
337 o = ss_cat(out, o, "<tr><td>output</td><td class=m>5.0×</td><td>" as *u8); o = eb_money(out, o, c_out); o = ss_cat(out, o, "</td></tr>" as *u8)
338 o = ss_cat(out, o, "<tr><td>cache read</td><td class=m>0.1×</td><td>" as *u8); o = eb_money(out, o, c_cr); o = ss_cat(out, o, "</td></tr>" as *u8)
339 o = ss_cat(out, o, "<tr><td>cache write</td><td class=m>1.25×</td><td>" as *u8); o = eb_money(out, o, c_cw); o = ss_cat(out, o, "</td></tr>" as *u8)
340 o = ss_cat(out, o, "<tr><td><b>flat-ceiling total</b></td><td class=m>at " as *u8); o = eb_money(out, o, EFL_CENTS_PER_MTOK_IN); o = ss_cat(out, o, "/Mtok flat (OLD ceiling)</td><td class=a><b>" as *u8); o = eb_money(out, o, flatcost); o = ss_cat(out, o, "</b></td></tr></table>" as *u8)
341 o = ss_cat(out, o, "<p class=m style='margin:4px 24px'>Cache read+write dominate — the efficiency lever is CONTEXT/CACHE management, not output brevity. The flat-ceiling above is the OLD upper bound; the REAL cost below is per-model attributed.</p>" as *u8)
342 o = ss_cat(out, o, "<h2>Real cost by model — per-message attribution (rates fetched 2026-07-19 platform.claude.com)</h2><table><tr><th>model</th><th>base input rate</th><th>real cost / window</th></tr>" as *u8)
343 o = ss_cat(out, o, "<tr><td>Fable 5 / Mythos 5</td><td class=m>$10.00/Mtok</td><td class=g>" as *u8); o = eb_money(out, o, cm_fable); o = ss_cat(out, o, "</td></tr>" as *u8)
344 o = ss_cat(out, o, "<tr><td>Opus 4.5-4.8</td><td class=m>$5.00/Mtok</td><td class=g>" as *u8); o = eb_money(out, o, cm_opus); o = ss_cat(out, o, "</td></tr>" as *u8)
345 o = ss_cat(out, o, "<tr><td>Sonnet</td><td class=m>$3.00/Mtok</td><td class=g>" as *u8); o = eb_money(out, o, cm_sonnet); o = ss_cat(out, o, "</td></tr>" as *u8)
346 o = ss_cat(out, o, "<tr><td>Haiku 4.5</td><td class=m>$1.00/Mtok</td><td class=g>" as *u8); o = eb_money(out, o, cm_haiku); o = ss_cat(out, o, "</td></tr>" as *u8)
347 o = ss_cat(out, o, "<tr><td>other/unknown</td><td class=m>$5.00/Mtok</td><td class=g>" as *u8); o = eb_money(out, o, cm_other); o = ss_cat(out, o, "</td></tr>" as *u8)
348 o = ss_cat(out, o, "<tr><td><b>REAL total / window</b></td><td class=m>per-model attributed</td><td class=g><b>" as *u8); o = eb_money(out, o, cost); o = ss_cat(out, o, "</b></td></tr></table>" as *u8)
349 o = ss_cat(out, o, "<h2>Return on investment — honest ESTIMATE, REAL per-model cost, both sides visible</h2>" as *u8)
350 if m[M_ROIAV] == 1 {
351 o = ss_cat(out, o, "<table><tr><th>side</th><th>monthly</th><th>basis</th></tr>" as *u8)
352 o = ss_cat(out, o, "<tr><td>RETURN (value/savings)</td><td class=g>" as *u8); o = eb_money(out, o, mval); o = ss_cat(out, o, "</td><td class=m>declared rows in knowledge/store/roi- (also drives <a href=/pm>/pm</a>)</td></tr>" as *u8)
353 o = ss_cat(out, o, "<tr><td>INVESTMENT (spend, REAL)</td><td class=a>" as *u8); o = eb_money(out, o, mcost); o = ss_cat(out, o, "</td><td class=m>PER-MODEL attributed cost × 30d (real, not the flat ceiling)</td></tr>" as *u8)
354 o = ss_cat(out, o, "<tr><td><b>NET / mo</b></td><td class=a><b>" as *u8); o = eb_money(out, o, netv); o = ss_cat(out, o, "</b></td><td class=m>cost is now REAL (per-model). The value side (roi-) is a CONSERVATIVE FLOOR (SaaS-replacement + autonomy dividend only) — a negative net reflects an UNDER-COUNTED value plane, not true unprofitability. Maturing the value side = the ROI-CONVERGE work</td></tr></table>" as *u8)
355 } else {
356 o = ss_cat(out, o, "<p class=a style='margin:4px 24px'>VALUE UNSEEDED — investment is measured, but knowledge/store/roi- has no value rows yet. Seed rate rows (nx_store_put) to compute the ratio; the same plane lights up <a href=/pm>/pm</a>.</p>" as *u8)
357 }
358 o = ss_cat(out, o, "<h2>vs state of the art — honest, no self-graded wins</h2><table><tr><th>axis</th><th>vs Langfuse / Helicone / OpenLLMetry · CodeCarbon / Kepler · DORA</th></tr>" as *u8)
359 o = ss_cat(out, o, "<tr><td class=g>BEST (measured)</td><td>one sovereign stack measures tokens+time+money+energy AND ties them to program ROI + debt + maturity; SOTA tools silo LLM-cost (Langfuse) from energy (CodeCarbon) from delivery (DORA)</td></tr>" as *u8)
360 o = ss_cat(out, o, "<tr><td class=g>BEST (measured)</td><td>zero third-party SaaS/agent in the measurement path; reads our own transcripts+worklog bits-up</td></tr>" as *u8)
361 o = ss_cat(out, o, "<tr><td class=g>BEST (measured)</td><td>PER-MODEL cost attribution from the transcript model field × fetched per-model rates — matches Langfuse/Helicone per-model tables, in a sovereign zero-SaaS stack (was flat-rate; shipped 2026-07-19)</td></tr>" as *u8)
362 o = ss_cat(out, o, "<tr><td class=r>GAP</td><td>streamed exact tokens (kill 32MiB truncation), message-timestamp bucketing, real per-node RAPL energy — the research opportunities below</td></tr></table>" as *u8)
363 o = ss_cat(out, o, "<h2>Research opportunities & fetch targets — beyond SOTA</h2><table><tr><th>opportunity</th><th>what to look for in our fetches</th></tr>" as *u8)
364 o = ss_cat(out, o, "<tr><td>per-message model attribution → exact multi-model pricing</td><td class=m>Anthropic per-model token pricing (Opus/Sonnet/Haiku, input/output/cache tiers)</td></tr>" as *u8)
365 o = ss_cat(out, o, "<tr><td>real per-node energy (RAPL/rdtsc coefficient) instead of Wh/Mtok estimate</td><td class=m>good-triangle energy model; ML CO2 / published inference Wh/token</td></tr>" as *u8)
366 o = ss_cat(out, o, "<tr><td>stream transcripts (kill 32MiB truncation) + message-timestamp bucketing</td><td class=m>OpenLLMetry/Langfuse ingestion schemas; OTel GenAI semantic conventions</td></tr>" as *u8)
367 o = ss_cat(out, o, "<tr><td>unify cost+energy+delivery in one ROI (SOTA silos them)</td><td class=m>DORA/SPACE metrics; FinOps FOCUS spec; CodeCarbon/Kepler/Scaphandre</td></tr></table>" as *u8)
368 o = ss_cat(out, o, "<footer>sources: knowledge/store/eff- (measured spend) · knowledge/store/roi- (value) · nx_eff_lib coefficients — envelope: plane reads ≤ " as *u8)
369 o = ss_catn(out, o, EB_PLANE_CAP)
370 o = ss_cat(out, o, "B · window " as *u8); o = ss_catn(out, o, win); o = ss_cat(out, o, "d · " as *u8); o = ss_catn(out, o, m[M_TRUNC])
371 o = ss_cat(out, o, " truncations marked · measured facts vs declared-rate estimates labeled · generated epoch " as *u8); o = ss_catn(out, o, sys_now_realtime_sec())
372 o = ss_cat(out, o, " · exec: <a href=/pm>/pm</a> · hub: <a href=/compare>/compare</a></footer></body></html>\n" as *u8)
373 return o
374}