code wiki / _hdl_build / nx_seat_meter_beat_gate.nx
nx_seat_meter_beat_gate.nx source
↩ module page · 170 lines · 16185 B
1// nx_seat_meter_beat_gate.nx -- the REFEREE for nx_seat_meter_beat (ES1 of /compare/engineshift, 2026-09-02).
2// Drives the DEPLOYED subject over a fixture transcript tree under /tmp/<gate>/ (created at setup, never the live
3// plane) and asserts the sums a hand count gives: usage counted ONCE per message id across repeated content blocks,
4// non-assistant lines never counted, today never emitted, a truncated tail counted PARTIAL and never as zero, the
5// marker file honoured on an anchored (day, family) match, and an unreadable sessions dir read as UNOBSERVABLE.
6// nx_seat_meter_beat_gate [subject-elf] default _build/nx_seat_meter_beat.sov.elf (cwd nxc2)
7// Every tooth prints its values; the verdict line is LAST (gv_verdict). license_tier: ORIGINAL. No hw writes.
8import "nx_syscalls.nx"
9import "nx_gate_verdict.nx"
10import "nx_gatekit_lib.nx"
11import "nx_tool_run.nx"
12
13const MG_OUT: i64 = 65536
14const MG_I64: i64 = 8
15const MG_ARGV: i64 = 16
16const MG_TIMEOUT_MS: i64 = 30000
17const MG_NOW: i64 = 1786536000 // 2026-08-12T12:00:00Z: today is 08-12, the newest complete day is 08-11
18const MG_ROOT: *u8 = "/tmp/nx_seat_meter_beat_gate"
19const MG_SESS: *u8 = "/tmp/nx_seat_meter_beat_gate/sessions"
20const MG_STATE: *u8 = "/tmp/nx_seat_meter_beat_gate/state"
21const MG_MARK: *u8 = "/tmp/nx_seat_meter_beat_gate/state/meterbeat.days"
22
23func mg_run(elf: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8, a6: *u8, a7: *u8, a8: *u8, a9: *u8, a10: *u8, a11: *u8, out: *u8, outlen: *i64) -> i64 {
24 let av: *i64 = sys_mmap(MG_ARGV * MG_I64) as *i64
25 av[0] = elf as i64; av[1] = a1 as i64; av[2] = a2 as i64; av[3] = a3 as i64; av[4] = a4 as i64; av[5] = a5 as i64
26 av[6] = a6 as i64; av[7] = a7 as i64; av[8] = a8 as i64; av[9] = a9 as i64; av[10] = a10 as i64; av[11] = a11 as i64
27 var k: i64 = 1
28 var z: i64 = 0
29 while k < 12 { if z == 0 { if gk_len(av[k] as *u8) == 0 { av[k] = 0; z = 1 } } else { av[k] = 0 } k = k + 1 }
30 av[12] = 0
31 let rc: i64 = tr_run_capture_to(elf, av, out, MG_OUT, outlen, MG_TIMEOUT_MS)
32 let n: i64 = outlen[0]
33 if n >= 0 { if n < MG_OUT { out[n] = 0 as u8 } }
34 sys_munmap(av as *u8, MG_ARGV * MG_I64)
35 return rc
36}
37
38func mg_line(model: *u8, id: *u8, ov: *u8, cr: *u8, cc: *u8, ts: *u8, out: *u8, off: i64) -> i64 {
39 var o: i64 = off
40 o = gk_cat(out, o, "{\"parentUuid\":\"p\",\"message\":{\"model\":\"" as *u8)
41 o = gk_cat(out, o, model)
42 o = gk_cat(out, o, "\",\"id\":\"" as *u8)
43 o = gk_cat(out, o, id)
44 o = gk_cat(out, o, "\",\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"y\"}],\"usage\":{\"input_tokens\":2,\"cache_creation_input_tokens\":" as *u8)
45 o = gk_cat(out, o, cc)
46 o = gk_cat(out, o, ",\"cache_read_input_tokens\":" as *u8)
47 o = gk_cat(out, o, cr)
48 o = gk_cat(out, o, ",\"output_tokens\":" as *u8)
49 o = gk_cat(out, o, ov)
50 o = gk_cat(out, o, "}},\"type\":\"assistant\",\"uuid\":\"u\",\"timestamp\":\"" as *u8)
51 o = gk_cat(out, o, ts)
52 o = gk_cat(out, o, "\",\"version\":\"2.1.258\"}\n" as *u8)
53 return o
54}
55
56func main(argc: i64, argv: *i64) -> i64 {
57 var elf: *u8 = "_build/nx_seat_meter_beat.sov.elf" as *u8
58 if argc >= 2 { elf = argv[1] as *u8 }
59 gv_head("NX-SEAT-METER-BEAT-GATE: the scoreboard producer, driven over a fixture transcript tree" as *u8)
60 let ctr: *i64 = gv_ctr()
61 gv_need("subject elf present" as *u8, gk_exists(elf), ctr)
62
63 // ---- setup: fixture tree under /tmp/<gate>/ (a teardown does not run when a run crashes, so setup creates) ----
64 gk_mkdir(MG_ROOT)
65 gk_mkdir(MG_SESS)
66 gk_mkdir(MG_STATE)
67 gk_rm(MG_MARK)
68 let fb: *u8 = sys_mmap(MG_OUT)
69 let p: *u8 = sys_mmap(4096)
70 // a.jsonl: a user line carrying a top-level usage object (must never count), message A in THREE blocks, B, then C on the next day
71 var o: i64 = 0
72 o = gk_cat(fb, o, "{\"type\":\"user\",\"message\":{\"role\":\"user\",\"content\":\"hi\"},\"usage\":{\"output_tokens\":9999,\"cache_read_input_tokens\":9999,\"cache_creation_input_tokens\":9999},\"uuid\":\"u0\",\"timestamp\":\"2026-08-10T01:00:00.000Z\"}\n" as *u8)
73 o = mg_line("claude-fable-5-1" as *u8, "msg_A" as *u8, "100" as *u8, "1000" as *u8, "10" as *u8, "2026-08-10T01:00:01.000Z" as *u8, fb, o)
74 o = mg_line("claude-fable-5-1" as *u8, "msg_A" as *u8, "100" as *u8, "1000" as *u8, "10" as *u8, "2026-08-10T01:00:02.000Z" as *u8, fb, o)
75 o = mg_line("claude-fable-5-1" as *u8, "msg_A" as *u8, "100" as *u8, "1000" as *u8, "10" as *u8, "2026-08-10T01:00:03.000Z" as *u8, fb, o)
76 o = mg_line("claude-fable-5-1" as *u8, "msg_B" as *u8, "50" as *u8, "500" as *u8, "5" as *u8, "2026-08-10T02:00:00.000Z" as *u8, fb, o)
77 o = mg_line("claude-fable-5-1" as *u8, "msg_C" as *u8, "7" as *u8, "70" as *u8, "0" as *u8, "2026-08-11T03:00:00.000Z" as *u8, fb, o)
78 fb[o] = 0 as u8
79 gk_join(p, MG_SESS, "a.jsonl" as *u8); gk_write(p, fb)
80 // b.jsonl: another family on 08-10
81 o = 0
82 o = mg_line("claude-opus-5" as *u8, "msg_D" as *u8, "1" as *u8, "2" as *u8, "3" as *u8, "2026-08-10T04:00:00.000Z" as *u8, fb, o)
83 fb[o] = 0 as u8
84 gk_join(p, MG_SESS, "b.jsonl" as *u8); gk_write(p, fb)
85 // c.jsonl: E complete, then a TRUNCATED tail (no newline) that must not count
86 o = 0
87 o = mg_line("claude-fable-5-1" as *u8, "msg_E" as *u8, "9" as *u8, "90" as *u8, "1" as *u8, "2026-08-11T05:00:00.000Z" as *u8, fb, o)
88 o = gk_cat(fb, o, "{\"parentUuid\":\"p\",\"message\":{\"model\":\"claude-fable-5-1\",\"id\":\"msg_F\",\"usage\":{\"cache_creation_input_tokens\":1,\"cache_read_input_tokens\":1,\"output_tokens\":1000}},\"type\":\"assistant\",\"timestamp\":\"2026-08-11T06:00:00.000Z\"" as *u8)
89 fb[o] = 0 as u8
90 gk_join(p, MG_SESS, "c.jsonl" as *u8); gk_write(p, fb)
91 // d.jsonl: G before the window, H on TODAY -- both outside_window
92 o = 0
93 o = mg_line("claude-fable-5-1" as *u8, "msg_G" as *u8, "33" as *u8, "33" as *u8, "33" as *u8, "2026-08-01T05:00:00.000Z" as *u8, fb, o)
94 o = mg_line("claude-fable-5-1" as *u8, "msg_H" as *u8, "44" as *u8, "44" as *u8, "44" as *u8, "2026-08-12T05:00:00.000Z" as *u8, fb, o)
95 // a harness-written API-error line: model <synthetic>, no msg_ id, usage of zeros -- the shape measured 2026-09-02
96 o = gk_cat(fb, o, "{\"parentUuid\":\"p\",\"isSidechain\":false,\"type\":\"assistant\",\"uuid\":\"s1\",\"timestamp\":\"2026-08-11T08:00:00.000Z\",\"message\":{\"id\":\"0299\",\"model\":\"<synthetic>\",\"role\":\"assistant\",\"content\":[{\"type\":\"text\",\"text\":\"API Error\"}],\"usage\":{\"input_tokens\":0,\"output_tokens\":0,\"cache_read_input_tokens\":0,\"cache_creation_input_tokens\":0}},\"isApiErrorMessage\":true}\n" as *u8)
97 fb[o] = 0 as u8
98 gk_join(p, MG_SESS, "d.jsonl" as *u8); gk_write(p, fb)
99 // x.jsonl: the message's own fields AFTER content, and a tool_use input carrying raw model and id fields of
100 // its own (the shape measured on 2026-09-01) -- the message is fable (msg_X, out=4 cread=40 ccre=0), never opus
101 o = 0
102 o = gk_cat(fb, o, "{\"parentUuid\":\"p\",\"message\":{\"type\":\"message\",\"role\":\"assistant\",\"content\":[{\"type\":\"tool_use\",\"id\":\"toolu_1\",\"name\":\"Agent\",\"input\":{\"model\":\"claude-opus-4-8\",\"id\":\"msg_FAKE\",\"usage\":{\"output_tokens\":7777,\"cache_read_input_tokens\":7777,\"cache_creation_input_tokens\":7777},\"prompt\":\"say {\\\"model\\\":\\\"claude-opus-4-8\\\"}\"}}],\"id\":\"msg_X\",\"model\":\"claude-fable-5-1\",\"usage\":{\"input_tokens\":2,\"cache_creation_input_tokens\":0,\"cache_read_input_tokens\":40,\"output_tokens\":4}},\"type\":\"assistant\",\"uuid\":\"u\",\"timestamp\":\"2026-08-11T07:00:00.000Z\"}\n" as *u8)
103 fb[o] = 0 as u8
104 gk_join(p, MG_SESS, "x.jsonl" as *u8); gk_write(p, fb)
105 gk_join(p, MG_SESS, "notes.txt" as *u8); gk_write(p, "not a transcript\n" as *u8)
106
107 // ---- T1..: scan over the fixture with the clock pinned ----
108 let out: *u8 = sys_mmap(MG_OUT)
109 let ol: *i64 = sys_mmap(MG_I64) as *i64
110 mg_run(elf, "scan" as *u8, MG_SESS, "--now" as *u8, "1786536000" as *u8, "--days" as *u8, "5" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
111 let n: i64 = ol[0]
112 gv_puts(out)
113 gv_check("T1 fixture-reached-the-condition: 5 transcripts read, the .txt ignored" as *u8, gk_out_has(out, n, "files_seen=5 files_read=5" as *u8), ctr)
114 gv_check("T2 message A counted ONCE across three blocks: 08-10 fable out=150 cread=1500 ccreate=15 from A+B" as *u8, gk_out_has(out, n, "DAY 2026-08-10 fam=fable sessions=1 msgs=2 out=150 cread=1500 ccreate=15" as *u8), ctr)
115 gv_check("T3 dup_blocks=2 names the two repeated blocks" as *u8, gk_out_has(out, n, "dup_blocks=2" as *u8), ctr)
116 gv_check("T4 a second family files under its own row: 08-10 opus out=1 cread=2 ccreate=3" as *u8, gk_out_has(out, n, "DAY 2026-08-10 fam=opus sessions=1 msgs=1 out=1 cread=2 ccreate=3" as *u8), ctr)
117 gv_check("T5 sessions counts transcripts active that day: 08-11 fable sessions=3 (a, c and x) out=20 cread=200 ccreate=1" as *u8, gk_out_has(out, n, "DAY 2026-08-11 fam=fable sessions=3 msgs=3 out=20 cread=200 ccreate=1" as *u8), ctr)
118 gv_check("neg-control-a-tool-input-model-or-id-field-never-reclassifies-the-message (no 08-11 opus row, 7777 nowhere, msg_X filed as fable)" as *u8, (1 - gk_out_has(out, n, "DAY 2026-08-11 fam=opus" as *u8)) * (1 - gk_out_has(out, n, "7777" as *u8)), ctr)
119 gv_check("neg-control-a-user-line-carrying-a-usage-object-is-never-counted (9999 appears in no row)" as *u8, 1 - gk_out_has(out, n, "9999" as *u8), ctr)
120 gv_check("neg-control-a-truncated-tail-is-PARTIAL-not-a-row (partial_files=1, 1000 appears nowhere)" as *u8, gk_out_has(out, n, "partial_files=1" as *u8) * (1 - gk_out_has(out, n, "out=1000" as *u8)), ctr)
121 gv_check("neg-control-today-and-pre-window-days-are-outside_window=2-and-emit-no-row" as *u8, gk_out_has(out, n, "outside_window=2" as *u8) * (1 - gk_out_has(out, n, "DAY 2026-08-12" as *u8)) * (1 - gk_out_has(out, n, "DAY 2026-08-01" as *u8)), ctr)
122 gv_check("T6 the partition sums: assistant_lines=11 = msgs 6 + dup 2 + outside 2 + malformed 0 + synthetic 1" as *u8, gk_out_has(out, n, "assistant_lines=11" as *u8) * gk_out_has(out, n, "partition=11" as *u8) * gk_out_has(out, n, "malformed=0" as *u8) * gk_out_has(out, n, " msgs=6 " as *u8), ctr)
123 gv_check("neg-control-a-synthetic-API-error-line-is-its-own-bucket-never-a-message-and-never-malformed" as *u8, gk_out_has(out, n, "synthetic=1" as *u8) * gk_out_has(out, n, "malformed=0" as *u8), ctr)
124 gv_check("T7 scan is read-only and GREEN" as *u8, gk_out_has(out, n, "verdict=GREEN" as *u8) * (1 - gk_exists(MG_MARK)), ctr)
125
126 // ---- beat --dry: the marker file decides, anchored on day TAB family ----
127 gk_write(MG_MARK, "2026-08-10\tfable\t1786536000\t1\t150\t1500\t15\n2026-08-11\tfab\t1786536000\t9\t9\t9\t9\n" as *u8)
128 mg_run(elf, "beat" as *u8, MG_SESS, MG_STATE, "/nonexistent/nx_mcp_call.elf" as *u8, "https://example.invalid" as *u8, "/nonexistent/cap" as *u8, "--now" as *u8, "1786536000" as *u8, "--days" as *u8, "5" as *u8, "--dry" as *u8, out, ol)
129 let n2: i64 = ol[0]
130 gv_puts(out)
131 gv_check("T8 a marked (day, family) reads ALREADY-MARKED" as *u8, gk_out_has(out, n2, "DAY 2026-08-10 fam=fable sessions=1 msgs=2 out=150 cread=1500 ccreate=15 status=ALREADY-MARKED" as *u8), ctr)
132 gv_check("T9 an unmarked pair on the same day is still WOULD-PUSH" as *u8, gk_out_has(out, n2, "fam=opus sessions=1 msgs=1 out=1 cread=2 ccreate=3 status=WOULD-PUSH" as *u8), ctr)
133 gv_check("neg-control-a-marker-whose-family-is-a-PREFIX-does-not-match (08-11 fable still WOULD-PUSH beside a fab marker)" as *u8, gk_out_has(out, n2, "DAY 2026-08-11 fam=fable sessions=3 msgs=3 out=20 cread=200 ccreate=1 status=WOULD-PUSH" as *u8), ctr)
134 gv_check("T10 the beat summary partitions planned=2 already=1 pushed=0 failed=0, a dry run never reads the estate log, and stays GREEN" as *u8, gk_out_has(out, n2, "planned=2 pushed=0 already=1 failed=0 recovered=0 deferred=0 remote_log=NOT-READ-dry dry=1 verdict=GREEN" as *u8), ctr)
135 gv_check("T11 --dry forked nothing: the marker file is byte-identical to what setup wrote (2 lines)" as *u8, gk_count(MG_MARK, "\n" as *u8) == 2, ctr)
136
137 // ---- unobservable and hook envelope ----
138 mg_run(elf, "scan" as *u8, "/nonexistent/sessions" as *u8, "--now" as *u8, "1786536000" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
139 let n3: i64 = ol[0]
140 gv_check("T12 an unreadable sessions dir is UNOBSERVABLE, never zero" as *u8, gk_out_has(out, n3, "sessions_dir_unreadable=1" as *u8) * gk_out_has(out, n3, "verdict=UNOBSERVABLE" as *u8), ctr)
141 mg_run(elf, "beat" as *u8, MG_SESS, MG_STATE, "/nonexistent/nx_mcp_call.elf" as *u8, "https://example.invalid" as *u8, "/nonexistent/cap" as *u8, "--now" as *u8, "1786536000" as *u8, "--days" as *u8, "5" as *u8, "--dry" as *u8, out, ol)
142 let n4: i64 = ol[0]
143 gv_check("T13 dry beat is idempotent: a second run plans the same rows (planned=2 already=1)" as *u8, gk_out_has(out, n4, "planned=2 pushed=0 already=1" as *u8), ctr)
144 // ---- remotecheck: the estate-log read-back ruler on a captured (JSON-escaped) meterlog reply. Families alternate
145 // day by day in a real log, so the fixture plants opus-08-30 directly before fable-08-31: a checker that looks back
146 // across the row boundary would read (08-31, opus) as marked. MEASURED 2026-09-02: the first live run reported one
147 // push failed while the estate had accepted it, and the retry double-applied the row -- this read-back is the fix.
148 gk_join(p, MG_ROOT, "meterlog.capture" as *u8)
149 gk_write(p, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"content\":[{\"type\":\"text\",\"text\":\"METERLOG file_bytes=1870\\n1788359440\\tclaude-opus-daily\\t2\\t100\\t200\\t300\\tbeat-2026-08-30-msgs=9-files=2-src=nx_seat_meter_beat\\n1788359447\\tclaude-fable-daily\\t3\\t549488\\t180153580\\t6259476\\tbeat-2026-08-31-msgs=284-files=3-src=nx_seat_meter_beat\\n\"}],\"isError\":false}}" as *u8)
150 mg_run(elf, "remotecheck" as *u8, p, "2026-08-31" as *u8, "fable" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
151 let n6: i64 = ol[0]
152 gv_check("T14 a row the estate log already holds reads REMOTE-MARKED (the dropped-receipt case)" as *u8, gk_out_has(out, n6, "verdict=REMOTE-MARKED" as *u8), ctr)
153 mg_run(elf, "remotecheck" as *u8, p, "2026-08-31" as *u8, "opus" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
154 let n7: i64 = ol[0]
155 gv_check("neg-control-the-previous-row-s-family-does-not-leak-across-the-row-boundary (08-31 opus is REMOTE-ABSENT beside opus-08-30)" as *u8, gk_out_has(out, n7, "verdict=REMOTE-ABSENT" as *u8), ctr)
156 mg_run(elf, "remotecheck" as *u8, p, "2026-08-29" as *u8, "fable" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
157 let n8: i64 = ol[0]
158 gv_check("neg-control-a-day-with-no-row-is-REMOTE-ABSENT" as *u8, gk_out_has(out, n8, "verdict=REMOTE-ABSENT" as *u8), ctr)
159 mg_run(elf, "remotecheck" as *u8, "/nonexistent/capture" as *u8, "2026-08-31" as *u8, "fable" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
160 let n9: i64 = ol[0]
161 gv_check("T15 an unreadable capture is UNOBSERVABLE, never ABSENT (absence would license a push)" as *u8, gk_out_has(out, n9, "verdict=UNOBSERVABLE" as *u8), ctr)
162 // usage: a bad verb is refused by name
163 mg_run(elf, "bogus" as *u8, MG_SESS, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, "" as *u8, out, ol)
164 let n5: i64 = ol[0]
165 gv_check("neg-control-an-unknown-verb-prints-usage-and-no-row" as *u8, gk_out_has(out, n5, "usage: nx_seat_meter_beat" as *u8) * (1 - gk_out_has(out, n5, "DAY " as *u8)), ctr)
166
167 let rc: i64 = gv_verdict("NX-SEAT-METER-BEAT-GATE" as *u8, ctr, "ES1 sm_beat: usage once per message id, non-assistant lines and today never counted, truncation reads PARTIAL, markers anchored on day and family, unreadable input is UNOBSERVABLE" as *u8)
168 sys_exit(rc)
169 return rc
170}