code wiki / _hdl_build / nx_pub_desk.nx
nx_pub_desk.nx source
↩ module page · 993 lines · 46664 B
1// nx_pub_desk.nx -- THE PUBLISHING DESK organ (F762, publishing lane 2026-07-19).
2// The operator's diagnosis: "lots of random published pages without really a full publishing
3// system like an elite company". Research (news-org + docs-as-code sweeps, banked): the keystone
4// of every elite publisher (NYT Scoop/Gateway, Guardian Facia, GitLab docs, Backstage TechDocs)
5// is ONE machine-readable REGISTRY per published page (owner/section/status) from which every
6// hub/index is DERIVED -- never hand-curated -- and publishing flows only through a gate that
7// maintains it. This organ is that derivation engine for the sovereign sites docroot:
8// nx_pub_desk census <docroot> <pubprefix> (JSON: registry vs disk join --
9// registered / orphans / missing / debris / prev-artifacts; envelope DECLARED, no silent caps)
10// nx_pub_desk board <docroot> <pubprefix> <sotaprefix> <out> (emit the /publishing hub HTML,
11// sections + rows FROM THE REGISTRY ONLY; orphans/missing/debris rendered honestly in red;
12// SOTA checklist from the pubsota- plane; atomic tmp+rename write)
13// nx_pub_desk selftest <tmpdir> (gate: fixtures + scale tooth)
14// Registry plane pub- : id title owner status section path note (status@col3 = the
15// ecosystem convention; vocabulary live|asset|draft|withdrawn|redirect|debris)
16// SOTA plane pubsota- : id property grade(HAVE|PARTIAL|GAP) evidence note
17// Writes = nx_store_put on the planes (provenanced hist- = the corrections ledger); page bytes
18// land only via nx_site_publish (atomic, .prev). This organ never publishes site content itself.
19// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
20import "nx_sovjson_lib.nx"
21import "nx_store_seed_lib.nx"
22import "nx_seg_store.nx"
23import "nx_gate_verdict.nx"
24import "nx_syscalls.nx"
25import "nx_html_head.nx" // seq702: the ONE accessible head; migrate-on-touch
26
27const PD_CAP: i64 = 1048576
28const PD_LCAP: i64 = 262144
29const PD_NAMES: i64 = 262144
30const PD_MAXN: i64 = 4096
31const PD_SECMAX: i64 = 64
32const PD_TAB: i64 = 9
33const PD_NL: i64 = 10
34const PD_SLASH: i64 = 47
35const PD_EXIT_USAGE: i64 = 2
36const PD_EXIT_REFUSED: i64 = 3
37const PD_MODE: i64 = 420
38const PD_DIRMODE: i64 = 493
39const PD_DT_DIR: i64 = 4
40const PD_STDERR: i64 = 2
41const PD_SPAN: i64 = 16
42const PD_DBUF: i64 = 65536
43const PD_PATHCAP: i64 = 512
44const PD_NUMCAP: i64 = 28
45const PD_SMALLCAP: i64 = 64
46const PD_FIXCAP: i64 = 4096
47const PD_FIXSCAP: i64 = 1024
48const PD_I64B: i64 = 8
49const PD_BYTE: i64 = 256
50const PD_ZERO: i64 = 48
51const PD_BASE: i64 = 10
52const PD_QUOTE: i64 = 34
53const PD_APOS: i64 = 39
54const PD_BSLASH: i64 = 92
55const PD_LT: i64 = 60
56const PD_GT: i64 = 62
57const PD_AMP: i64 = 38
58const PD_DOT: i64 = 46
59const PD_SPACE: i64 = 32
60const PD_DE_RLO: i64 = 16
61const PD_DE_RHI: i64 = 17
62const PD_DE_TYPE: i64 = 18
63const PD_DE_NAME: i64 = 19
64const PD_SCALE_N: i64 = 250
65const PD_FIXA_LEN: i64 = 10
66const PD_FIXC_LEN: i64 = 5
67const PD_FIXP_LEN: i64 = 3
68const PD_FIX_ROWS: i64 = 3
69const PD_C_TITLE: i64 = 1
70const PD_C_OWNER: i64 = 2
71const PD_C_STATUS: i64 = 3
72const PD_C_SECTION: i64 = 4
73const PD_C_PATH: i64 = 5
74const PD_C_NOTE: i64 = 6
75const PD_S_PROP: i64 = 1
76const PD_S_GRADE: i64 = 2
77const PD_S_EVID: i64 = 3
78const PD_S_NOTE: i64 = 4
79const PD_NAME_PAD: i64 = 2
80const PD_ARG_A: i64 = 2
81const PD_ARG_B: i64 = 3
82const PD_ARG_C: i64 = 4
83const PD_ARG_D: i64 = 5
84const PD_ARG_E: i64 = 6
85const PD_STATB: i64 = 144
86const PD_MT_OFF: i64 = 88
87const PD_DAY: i64 = 86400
88const PD_BACKDATE_D: i64 = 200
89const PD_STALE_FIX: i64 = 90
90// civil-from-days (Howard Hinnant) integer calendar constants for ISO lastmod emit
91const PD_EPOCH_SHIFT: i64 = 719468
92const PD_ERA_DAYS: i64 = 146097
93const PD_CENT_DAYS: i64 = 36524
94const PD_QUAD_DAYS: i64 = 1460
95const PD_YEAR_DAYS: i64 = 365
96const PD_ERA_YEARS: i64 = 400
97const PD_LEAP4: i64 = 4
98const PD_LEAP100: i64 = 100
99const PD_MONTH_SLOPE: i64 = 153
100const PD_MP_BIAS: i64 = 2
101const PD_MP_SCALE: i64 = 5
102const PD_MP_CUT: i64 = 10
103const PD_MAR_OFF: i64 = 3
104const PD_DEC_WRAP: i64 = 9
105const PD_TWO_DIG: i64 = 10
106const PD_DASH: i64 = 45
107const PD_HTML_EXT: i64 = 5
108
109func pd_puts(s: *u8) -> i64 { return sj_puts(s) }
110func pd_werr(s: *u8) -> i64 { return sj_werr(s) }
111func pd_vlen(s: *u8) -> i64 { return sj_vlen(s) }
112func pd_cat(d: *u8, o: i64, s: *u8) -> i64 { return sj_cat(d, o, s) }
113func pd_catn(d: *u8, o: i64, v: i64) -> i64 { return sj_catn(d, o, v) }
114// escape a span for JSON/HTML embedding: quote->apostrophe, backslash->slash, <,>,& -> '.', ctrl->space
115func pd_esc(d: *u8, o: i64, q: *u8, s: i64, e: i64) -> i64 {
116 var i: i64 = s
117 while i < e {
118 var c: i64 = q[i] as i64
119 if c == PD_QUOTE { c = PD_APOS }
120 if c == PD_BSLASH { c = PD_SLASH }
121 if c == PD_LT { c = PD_DOT }
122 if c == PD_GT { c = PD_DOT }
123 if c == PD_AMP { c = PD_DOT }
124 if c < PD_SPACE { c = PD_SPACE }
125 d[o] = c as u8
126 o = o + 1
127 i = i + 1
128 }
129 return o
130}
131func pd_le(q: *u8, i: i64, n: i64) -> i64 { var e: i64 = i; var s: i64 = 1; while s == 1 { if e >= n { s = 0 } else { if q[e] == (PD_NL as u8) { s = 0 } else { e = e + 1 } } } return e }
132func pd_col(q: *u8, ls: i64, le: i64, c: i64, out: *i64) -> i64 {
133 var col: i64 = 0
134 var p: i64 = ls
135 while col < c {
136 var s: i64 = 1
137 while s == 1 { if p >= le { return 0 } if q[p] == (PD_TAB as u8) { s = 0 } else { p = p + 1 } }
138 p = p + 1
139 col = col + 1
140 }
141 var e: i64 = p
142 var s2: i64 = 1
143 while s2 == 1 { if e >= le { s2 = 0 } else { if q[e] == (PD_TAB as u8) { s2 = 0 } else { e = e + 1 } } }
144 out[0] = p
145 out[1] = e
146 return 1
147}
148func pd_lit_eq(q: *u8, s: i64, e: i64, lit: *u8) -> i64 {
149 var i: i64 = 0
150 while s + i < e { if lit[i] == (0 as u8) { return 0 } if q[s+i] != lit[i] { return 0 } i = i + 1 }
151 if lit[i] != (0 as u8) { return 0 }
152 return 1
153}
154func pd_span_eq(a: *u8, s1: i64, e1: i64, b: *u8, s2: i64, e2: i64) -> i64 {
155 if e1 - s1 != e2 - s2 { return 0 }
156 var i: i64 = 0
157 while s1 + i < e1 { if a[s1+i] != b[s2+i] { return 0 } i = i + 1 }
158 return 1
159}
160func pd_ends(q: *u8, s: i64, n: i64, suf: *u8) -> i64 {
161 let sl: i64 = pd_vlen(suf)
162 if n < sl { return 0 }
163 var i: i64 = 0
164 while i < sl { if q[s + n - sl + i] != suf[i] { return 0 } i = i + 1 }
165 return 1
166}
167func pd_argnum(s: *u8) -> i64 { var v: i64 = 0; var i: i64 = 0; while s[i] != (0 as u8) { let c: i64 = s[i] as i64; if c >= PD_ZERO { if c <= PD_ZERO + PD_BASE - 1 { v = v * PD_BASE + (c - PD_ZERO) } } i = i + 1 } return v }
168func pd_find(buf: *u8, n: i64, needle: *u8) -> i64 {
169 let nl: i64 = pd_vlen(needle)
170 if nl == 0 { return 0 }
171 var i: i64 = 0
172 while i + nl <= n {
173 var k: i64 = 0
174 var hit: i64 = 1
175 while k < nl { if buf[i+k] != needle[k] { hit = 0; k = nl } else { k = k + 1 } }
176 if hit == 1 { return 1 }
177 i = i + 1
178 }
179 return 0
180}
181func pd_wfile(path: *u8, buf: *u8, n: i64) -> i64 {
182 let fd: i64 = sys_openat_wr(path, PD_MODE)
183 if fd < 0 { return 0 - 1 }
184 var o: i64 = 0
185 while o < n { let r: i64 = sys_write(fd, ((buf as i64) + o) as *u8, n - o); if r <= 0 { sys_close(fd); return 0 - 1 } o = o + r }
186 sys_close(fd)
187 return 0
188}
189func pd_rfile(path: *u8, buf: *u8, cap: i64) -> i64 {
190 let fd: i64 = sys_openat_rd(path)
191 if fd < 0 { return 0 - 1 }
192 var n: i64 = 0
193 var go: i64 = 1
194 while go == 1 { let r: i64 = sys_read(fd, ((buf as i64) + n) as *u8, cap - 1 - n); if r <= 0 { go = 0 } else { n = n + r } if n >= cap - 1 { go = 0 } }
195 sys_close(fd)
196 return n
197}
198
199// mtime (epoch sec) of docroot/name; -1 if unstatable. The FRESHNESS channel is the file mtime --
200// a machine artifact, never a self-reported date (evidence-derived per the phase law).
201func pd_mtime(docroot: *u8, name: *u8) -> i64 {
202 let pb: *u8 = sys_mmap(PD_PATHCAP)
203 var o: i64 = pd_cat(pb, 0, docroot)
204 pb[o] = PD_SLASH as u8
205 o = o + 1
206 o = pd_cat(pb, o, name)
207 pb[o] = 0 as u8
208 let sb: *u8 = sys_mmap(PD_STATB)
209 if sys_fstatat(pb, sb) != 0 { return 0 - 1 }
210 let mp: *i64 = ((sb as i64) + PD_MT_OFF) as *i64
211 return mp[0]
212}
213// append yyyy-mm-dd for an epoch (civil-from-days, pure integer)
214func pd_iso(d: *u8, o: i64, epoch: i64) -> i64 {
215 var z: i64 = epoch / PD_DAY
216 z = z + PD_EPOCH_SHIFT
217 let era: i64 = z / PD_ERA_DAYS
218 let doe: i64 = z - era * PD_ERA_DAYS
219 let yoe: i64 = (doe - doe/PD_QUAD_DAYS + doe/PD_CENT_DAYS - doe/(PD_ERA_DAYS - 1)) / PD_YEAR_DAYS
220 var y: i64 = yoe + era * PD_ERA_YEARS
221 let doy: i64 = doe - (PD_YEAR_DAYS*yoe + yoe/PD_LEAP4 - yoe/PD_LEAP100)
222 let mp: i64 = (PD_MP_SCALE*doy + PD_MP_BIAS) / PD_MONTH_SLOPE
223 let dd: i64 = doy - (PD_MONTH_SLOPE*mp + PD_MP_BIAS)/PD_MP_SCALE + 1
224 var mm: i64 = mp + PD_MAR_OFF
225 if mp >= PD_MP_CUT { mm = mp - PD_DEC_WRAP }
226 if mm <= PD_MP_BIAS { y = y + 1 }
227 var oo: i64 = pd_catn(d, o, y)
228 d[oo] = PD_DASH as u8
229 oo = oo + 1
230 if mm < PD_TWO_DIG { d[oo] = PD_ZERO as u8; oo = oo + 1 }
231 oo = pd_catn(d, oo, mm)
232 d[oo] = PD_DASH as u8
233 oo = oo + 1
234 if dd < PD_TWO_DIG { d[oo] = PD_ZERO as u8; oo = oo + 1 }
235 oo = pd_catn(d, oo, dd)
236 return oo
237}
238
239// walk ONE level of docroot -> names arena (dirs stored with trailing '/'), offs[i], count.
240// envp[0]=dropped-beyond-MAXN envp[1]=prev-artifact count (".prev" rollback files, excluded).
241func pd_scan(docroot: *u8, arena: *u8, offs: *i64, envp: *i64) -> i64 {
242 envp[0] = 0
243 envp[1] = 0
244 let fd: i64 = sys_openat_rd(docroot)
245 if fd < 0 { return 0 - 1 }
246 let db: *u8 = sys_mmap(PD_DBUF)
247 var cnt: i64 = 0
248 var ao: i64 = 0
249 var nr: i64 = 1
250 while nr > 0 {
251 nr = sys_getdents64(fd, db, PD_DBUF)
252 if nr > 0 {
253 var o: i64 = 0
254 while o < nr {
255 let rl: i64 = (db[o+PD_DE_RLO] as i64) + ((db[o+PD_DE_RHI] as i64) * PD_BYTE)
256 if rl <= 0 { o = nr } else {
257 var nl: i64 = 0
258 while db[o+PD_DE_NAME+nl] != (0 as u8) { nl = nl + 1 }
259 var dot: i64 = 0
260 if nl >= 1 { if db[o+PD_DE_NAME] == (PD_DOT as u8) { dot = 1 } }
261 if dot == 0 {
262 if pd_ends(db, o+PD_DE_NAME, nl, ".prev" as *u8) == 1 { envp[1] = envp[1] + 1 } else {
263 if cnt >= PD_MAXN { envp[0] = envp[0] + 1 } else {
264 if ao + nl + PD_NAME_PAD >= PD_NAMES { envp[0] = envp[0] + 1 } else {
265 offs[cnt] = ao
266 var t: i64 = 0
267 while t < nl { arena[ao] = db[o+PD_DE_NAME+t]; ao = ao + 1; t = t + 1 }
268 if db[o+PD_DE_TYPE] == (PD_DT_DIR as u8) { arena[ao] = PD_SLASH as u8; ao = ao + 1 }
269 arena[ao] = 0 as u8
270 ao = ao + 1
271 cnt = cnt + 1
272 }
273 }
274 }
275 }
276 o = o + rl
277 }
278 }
279 }
280 }
281 sys_close(fd)
282 sys_munmap(db, PD_DBUF)
283 return cnt
284}
285func pd_is_dirname(arena: *u8, off: i64) -> i64 { let n: i64 = pd_vlen(((arena as i64) + off) as *u8); if n < 1 { return 0 } if arena[off + n - 1] == (PD_SLASH as u8) { return 1 } return 0 }
286func pd_is_page(arena: *u8, off: i64) -> i64 { let n: i64 = pd_vlen(((arena as i64) + off) as *u8); return pd_ends(arena, off, n, ".html" as *u8) }
287// does any registry row's path (col5) equal this arena name?
288func pd_reg_has(reg: *u8, rn: i64, arena: *u8, off: i64) -> i64 {
289 let c5: *i64 = sys_mmap(PD_SPAN) as *i64
290 let nl: i64 = pd_vlen(((arena as i64) + off) as *u8)
291 var i: i64 = 0
292 while i < rn {
293 let le: i64 = pd_le(reg, i, rn)
294 if pd_col(reg, i, le, PD_C_PATH, c5) == 1 { if pd_span_eq(reg, c5[0], c5[1], arena, off, off + nl) == 1 { return 1 } }
295 i = le + 1
296 }
297 return 0
298}
299// does the disk arena contain this registry path span?
300func pd_disk_has(arena: *u8, offs: *i64, cnt: i64, reg: *u8, s: i64, e: i64) -> i64 {
301 var i: i64 = 0
302 while i < cnt {
303 let off: i64 = offs[i]
304 let nl: i64 = pd_vlen(((arena as i64) + off) as *u8)
305 if pd_span_eq(reg, s, e, arena, off, off + nl) == 1 { return 1 }
306 i = i + 1
307 }
308 return 0
309}
310
311// ---- census: the registry-vs-disk JOIN as JSON (fail-open on empty registry = honest all-orphan) ----
312// stale_days > 0 arms the freshness sweep (mtime-derived); 0 = disabled (threshold is caller DATA).
313func pd_census(docroot: *u8, pubprefix: *u8, out: *u8, cap: i64, stale_days: i64) -> i64 {
314 let arena: *u8 = sys_mmap(PD_NAMES)
315 let offs: *i64 = sys_mmap(PD_MAXN * PD_I64B) as *i64
316 let envp: *i64 = sys_mmap(PD_SPAN) as *i64
317 let cnt: i64 = pd_scan(docroot, arena, offs, envp)
318 let reg: *u8 = sys_mmap(PD_CAP)
319 let rn: i64 = sts_load(pubprefix, reg, PD_CAP)
320 var rows: i64 = 0
321 var i: i64 = 0
322 while i < rn { let le: i64 = pd_le(reg, i, rn); if le > i { rows = rows + 1 } i = le + 1 }
323 var pages: i64 = 0
324 var dirs: i64 = 0
325 var others: i64 = 0
326 var registered: i64 = 0
327 var k: i64 = 0
328 while k < cnt {
329 let off: i64 = offs[k]
330 if pd_is_dirname(arena, off) == 1 { dirs = dirs + 1 } else { if pd_is_page(arena, off) == 1 { pages = pages + 1 } else { others = others + 1 } }
331 if pd_reg_has(reg, rn, arena, off) == 1 { registered = registered + 1 }
332 k = k + 1
333 }
334 var o: i64 = 0
335 o = pd_cat(out, o, "{\"epoch\":" as *u8)
336 o = pd_catn(out, o, sys_now_realtime_sec())
337 o = pd_cat(out, o, ",\"docroot\":\"" as *u8)
338 o = pd_esc(out, o, docroot, 0, pd_vlen(docroot))
339 o = pd_cat(out, o, "\",\"registry_rows\":" as *u8)
340 o = pd_catn(out, o, rows)
341 o = pd_cat(out, o, ",\"disk_entries\":" as *u8)
342 o = pd_catn(out, o, cnt)
343 o = pd_cat(out, o, ",\"pages\":" as *u8)
344 o = pd_catn(out, o, pages)
345 o = pd_cat(out, o, ",\"dirs\":" as *u8)
346 o = pd_catn(out, o, dirs)
347 o = pd_cat(out, o, ",\"others\":" as *u8)
348 o = pd_catn(out, o, others)
349 o = pd_cat(out, o, ",\"prev_artifacts\":" as *u8)
350 o = pd_catn(out, o, envp[1])
351 o = pd_cat(out, o, ",\"registered_on_disk\":" as *u8)
352 o = pd_catn(out, o, registered)
353 // orphans: on-disk entries (pages+dirs) not in the registry
354 o = pd_cat(out, o, ",\"orphans\":[" as *u8)
355 var no: i64 = 0
356 k = 0
357 while k < cnt {
358 let off2: i64 = offs[k]
359 var care: i64 = 1
360 if pd_is_dirname(arena, off2) == 0 { if pd_is_page(arena, off2) == 0 { care = 0 } }
361 if care == 1 { if pd_reg_has(reg, rn, arena, off2) == 0 {
362 if no > 0 { o = pd_cat(out, o, "," as *u8) }
363 o = pd_cat(out, o, "\"" as *u8)
364 o = pd_esc(out, o, arena, off2, off2 + pd_vlen(((arena as i64) + off2) as *u8))
365 o = pd_cat(out, o, "\"" as *u8)
366 no = no + 1
367 } }
368 k = k + 1
369 }
370 // debris: on-disk non-page files not in the registry
371 o = pd_cat(out, o, "],\"debris\":[" as *u8)
372 var nd: i64 = 0
373 k = 0
374 while k < cnt {
375 let off3: i64 = offs[k]
376 var iso: i64 = 0
377 if pd_is_dirname(arena, off3) == 0 { if pd_is_page(arena, off3) == 0 { iso = 1 } }
378 if iso == 1 { if pd_reg_has(reg, rn, arena, off3) == 0 {
379 if nd > 0 { o = pd_cat(out, o, "," as *u8) }
380 o = pd_cat(out, o, "\"" as *u8)
381 o = pd_esc(out, o, arena, off3, off3 + pd_vlen(((arena as i64) + off3) as *u8))
382 o = pd_cat(out, o, "\"" as *u8)
383 nd = nd + 1
384 } }
385 k = k + 1
386 }
387 // missing: registry rows (status not withdrawn/draft) whose path is absent on disk
388 o = pd_cat(out, o, "],\"missing\":[" as *u8)
389 var nm: i64 = 0
390 let c3: *i64 = sys_mmap(PD_SPAN) as *i64
391 let c5b: *i64 = sys_mmap(PD_SPAN) as *i64
392 i = 0
393 while i < rn {
394 let le2: i64 = pd_le(reg, i, rn)
395 if pd_col(reg, i, le2, PD_C_STATUS, c3) == 1 { if pd_col(reg, i, le2, PD_C_PATH, c5b) == 1 {
396 var live: i64 = 1
397 if pd_lit_eq(reg, c3[0], c3[1], "withdrawn" as *u8) == 1 { live = 0 }
398 if pd_lit_eq(reg, c3[0], c3[1], "draft" as *u8) == 1 { live = 0 }
399 if live == 1 { if pd_disk_has(arena, offs, cnt, reg, c5b[0], c5b[1]) == 0 {
400 if nm > 0 { o = pd_cat(out, o, "," as *u8) }
401 o = pd_cat(out, o, "\"" as *u8)
402 o = pd_esc(out, o, reg, c5b[0], c5b[1])
403 o = pd_cat(out, o, "\"" as *u8)
404 nm = nm + 1
405 } }
406 } }
407 i = le2 + 1
408 }
409 // stale sweep: on-disk pages older than stale_days (mtime-derived, machine artifact)
410 o = pd_cat(out, o, "],\"stale_days\":" as *u8)
411 o = pd_catn(out, o, stale_days)
412 o = pd_cat(out, o, ",\"stale\":[" as *u8)
413 var nst: i64 = 0
414 if stale_days > 0 {
415 let nowe: i64 = sys_now_realtime_sec()
416 k = 0
417 while k < cnt {
418 let offs2: i64 = offs[k]
419 if pd_is_dirname(arena, offs2) == 0 { if pd_is_page(arena, offs2) == 1 {
420 let mt: i64 = pd_mtime(docroot, ((arena as i64) + offs2) as *u8)
421 if mt > 0 { if (nowe - mt) / PD_DAY > stale_days {
422 if nst > 0 { o = pd_cat(out, o, "," as *u8) }
423 o = pd_cat(out, o, "\"" as *u8)
424 o = pd_esc(out, o, arena, offs2, offs2 + pd_vlen(((arena as i64) + offs2) as *u8))
425 o = pd_cat(out, o, "\"" as *u8)
426 nst = nst + 1
427 } }
428 } }
429 k = k + 1
430 }
431 }
432 o = pd_cat(out, o, "],\"stale_count\":" as *u8)
433 o = pd_catn(out, o, nst)
434 o = pd_cat(out, o, ",\"orphan_count\":" as *u8)
435 o = pd_catn(out, o, no)
436 o = pd_cat(out, o, ",\"debris_count\":" as *u8)
437 o = pd_catn(out, o, nd)
438 o = pd_cat(out, o, ",\"missing_count\":" as *u8)
439 o = pd_catn(out, o, nm)
440 o = pd_cat(out, o, ",\"envelope\":{\"names_cap\":" as *u8)
441 o = pd_catn(out, o, PD_MAXN)
442 o = pd_cat(out, o, ",\"dropped\":" as *u8)
443 o = pd_catn(out, o, envp[0])
444 o = pd_cat(out, o, "}}" as *u8)
445 return o
446}
447
448// ---- board: the /publishing hub, DERIVED from the registry (sections = data, orphans in red) ----
449func pd_status_class(reg: *u8, s: i64, e: i64) -> *u8 {
450 if pd_lit_eq(reg, s, e, "live" as *u8) == 1 { return "a" as *u8 }
451 if pd_lit_eq(reg, s, e, "asset" as *u8) == 1 { return "d" as *u8 }
452 if pd_lit_eq(reg, s, e, "draft" as *u8) == 1 { return "s" as *u8 }
453 if pd_lit_eq(reg, s, e, "redirect" as *u8) == 1 { return "d" as *u8 }
454 if pd_lit_eq(reg, s, e, "withdrawn" as *u8) == 1 { return "d" as *u8 }
455 return "g" as *u8
456}
457func pd_grade_class(q: *u8, s: i64, e: i64) -> *u8 {
458 if pd_lit_eq(q, s, e, "HAVE" as *u8) == 1 { return "a" as *u8 }
459 if pd_lit_eq(q, s, e, "PARTIAL" as *u8) == 1 { return "s" as *u8 }
460 return "g" as *u8
461}
462func pd_board(docroot: *u8, pubprefix: *u8, sotaprefix: *u8, outpath: *u8, stale_days: i64) -> i64 {
463 let arena: *u8 = sys_mmap(PD_NAMES)
464 let offs: *i64 = sys_mmap(PD_MAXN * PD_I64B) as *i64
465 let envp: *i64 = sys_mmap(PD_SPAN) as *i64
466 let cnt: i64 = pd_scan(docroot, arena, offs, envp)
467 let reg: *u8 = sys_mmap(PD_CAP)
468 let rn: i64 = sts_load(pubprefix, reg, PD_CAP)
469 let sota: *u8 = sys_mmap(PD_LCAP)
470 let sn: i64 = sts_load(sotaprefix, sota, PD_LCAP)
471 let out: *u8 = sys_mmap(PD_CAP)
472 let c0: *i64 = sys_mmap(PD_SPAN) as *i64
473 let c1: *i64 = sys_mmap(PD_SPAN) as *i64
474 let c2: *i64 = sys_mmap(PD_SPAN) as *i64
475 let c3: *i64 = sys_mmap(PD_SPAN) as *i64
476 let c4: *i64 = sys_mmap(PD_SPAN) as *i64
477 let c5: *i64 = sys_mmap(PD_SPAN) as *i64
478 let c6: *i64 = sys_mmap(PD_SPAN) as *i64
479 // counts for the governance strip
480 var rows: i64 = 0
481 var livec: i64 = 0
482 var i: i64 = 0
483 while i < rn {
484 let le: i64 = pd_le(reg, i, rn)
485 if le > i {
486 rows = rows + 1
487 if pd_col(reg, i, le, PD_C_STATUS, c3) == 1 { if pd_lit_eq(reg, c3[0], c3[1], "live" as *u8) == 1 { livec = livec + 1 } }
488 }
489 i = le + 1
490 }
491 var orph: i64 = 0
492 var debr: i64 = 0
493 var k: i64 = 0
494 while k < cnt {
495 let off: i64 = offs[k]
496 if pd_reg_has(reg, rn, arena, off) == 0 {
497 var pg: i64 = 0
498 if pd_is_dirname(arena, off) == 1 { pg = 1 } else { if pd_is_page(arena, off) == 1 { pg = 1 } }
499 if pg == 1 { orph = orph + 1 } else { debr = debr + 1 }
500 }
501 k = k + 1
502 }
503 var miss: i64 = 0
504 i = 0
505 while i < rn {
506 let le3: i64 = pd_le(reg, i, rn)
507 if pd_col(reg, i, le3, PD_C_STATUS, c3) == 1 { if pd_col(reg, i, le3, PD_C_PATH, c5) == 1 {
508 var lv: i64 = 1
509 if pd_lit_eq(reg, c3[0], c3[1], "withdrawn" as *u8) == 1 { lv = 0 }
510 if pd_lit_eq(reg, c3[0], c3[1], "draft" as *u8) == 1 { lv = 0 }
511 if lv == 1 { if pd_disk_has(arena, offs, cnt, reg, c5[0], c5[1]) == 0 { miss = miss + 1 } }
512 } }
513 i = le3 + 1
514 }
515 var o: i64 = 0
516 o = nxh_head_open(out, o, "Nishi Publishing Desk" as *u8)
517 o = pd_cat(out, o, "<style>body{background:black;color:gainsboro;font:15px sans-serif;margin:24px;max-width:1100px}h1{color:white}h2{color:white;margin-top:28px}table{border-collapse:collapse;width:100%}td,th{border-bottom:1px solid dimgray;padding:5px 9px;text-align:left}th{color:gray}.a{color:springgreen}.s{color:orange}.d{color:gray}.g{color:tomato}a{color:skyblue;text-decoration:none}p.d{color:gray}</style>" as *u8)
518 o = nxh_body_open(out, o)
519 o = pd_cat(out, o, "<h1>Nishi Publishing Desk</h1><p class=d>the governed publishing system: every page below is DERIVED from the pub- registry plane + a live docroot walk -- never hand-curated. Writes go through nx_store_put (provenanced hist- = the corrections ledger); page bytes land only via nx_site_publish (atomic, .prev rollback). epoch " as *u8)
520 o = pd_catn(out, o, sys_now_realtime_sec())
521 o = pd_cat(out, o, "</p><p><b>" as *u8)
522 o = pd_catn(out, o, rows)
523 o = pd_cat(out, o, " registered</b> (" as *u8)
524 o = pd_catn(out, o, livec)
525 o = pd_cat(out, o, " live) · <span class=g>" as *u8)
526 o = pd_catn(out, o, orph)
527 o = pd_cat(out, o, " orphan</span> · <span class=g>" as *u8)
528 o = pd_catn(out, o, miss)
529 o = pd_cat(out, o, " missing</span> · <span class=g>" as *u8)
530 o = pd_catn(out, o, debr)
531 o = pd_cat(out, o, " debris</span> · " as *u8)
532 o = pd_catn(out, o, envp[1])
533 o = pd_cat(out, o, " .prev rollback artifacts" as *u8)
534 if stale_days > 0 {
535 o = pd_cat(out, o, " · freshness threshold " as *u8)
536 o = pd_catn(out, o, stale_days)
537 o = pd_cat(out, o, "d (age = file mtime, machine-derived)" as *u8)
538 }
539 o = pd_cat(out, o, "</p>" as *u8)
540 // sections in first-seen registry order
541 let secoff: *i64 = sys_mmap(PD_SECMAX * PD_I64B) as *i64
542 let secend: *i64 = sys_mmap(PD_SECMAX * PD_I64B) as *i64
543 var nsec: i64 = 0
544 i = 0
545 while i < rn {
546 let le4: i64 = pd_le(reg, i, rn)
547 if pd_col(reg, i, le4, PD_C_SECTION, c4) == 1 {
548 var seen: i64 = 0
549 var t: i64 = 0
550 while t < nsec { if pd_span_eq(reg, c4[0], c4[1], reg, secoff[t], secend[t]) == 1 { seen = 1; t = nsec } else { t = t + 1 } }
551 if seen == 0 { if nsec < PD_SECMAX { secoff[nsec] = c4[0]; secend[nsec] = c4[1]; nsec = nsec + 1 } }
552 }
553 i = le4 + 1
554 }
555 var s: i64 = 0
556 while s < nsec {
557 o = pd_cat(out, o, "<h2>" as *u8)
558 o = pd_esc(out, o, reg, secoff[s], secend[s])
559 o = pd_cat(out, o, "</h2><table><tr><th>page</th><th>owner</th><th>status</th><th>age</th><th>path</th><th>note</th></tr>" as *u8)
560 i = 0
561 while i < rn {
562 let le5: i64 = pd_le(reg, i, rn)
563 if pd_col(reg, i, le5, PD_C_SECTION, c4) == 1 { if pd_span_eq(reg, c4[0], c4[1], reg, secoff[s], secend[s]) == 1 {
564 if pd_col(reg, i, le5, PD_C_TITLE, c1) == 1 { if pd_col(reg, i, le5, PD_C_OWNER, c2) == 1 { if pd_col(reg, i, le5, PD_C_STATUS, c3) == 1 { if pd_col(reg, i, le5, PD_C_PATH, c5) == 1 {
565 o = pd_cat(out, o, "<tr><td><b>" as *u8)
566 o = pd_esc(out, o, reg, c1[0], c1[1])
567 o = pd_cat(out, o, "</b></td><td>" as *u8)
568 o = pd_esc(out, o, reg, c2[0], c2[1])
569 o = pd_cat(out, o, "</td><td><span class=" as *u8)
570 o = pd_cat(out, o, pd_status_class(reg, c3[0], c3[1]))
571 o = pd_cat(out, o, ">" as *u8)
572 o = pd_esc(out, o, reg, c3[0], c3[1])
573 o = pd_cat(out, o, "</span></td><td>" as *u8)
574 // age cell: mtime-derived days; STALE flag past the data-driven threshold
575 var aged: i64 = 0 - 1
576 if reg[c5[1] - 1] != (PD_SLASH as u8) {
577 let pcb: *u8 = sys_mmap(PD_PATHCAP)
578 var pco: i64 = 0
579 var pct: i64 = c5[0]
580 while pct < c5[1] { pcb[pco] = reg[pct]; pco = pco + 1; pct = pct + 1 }
581 pcb[pco] = 0 as u8
582 let mt2: i64 = pd_mtime(docroot, pcb)
583 if mt2 > 0 { aged = (sys_now_realtime_sec() - mt2) / PD_DAY }
584 }
585 if aged < 0 { o = pd_cat(out, o, "<span class=d>-</span>" as *u8) } else {
586 var stale: i64 = 0
587 if stale_days > 0 { if aged > stale_days { stale = 1 } }
588 if stale == 1 { o = pd_cat(out, o, "<span class=s>" as *u8) } else { o = pd_cat(out, o, "<span class=d>" as *u8) }
589 o = pd_catn(out, o, aged)
590 o = pd_cat(out, o, "d" as *u8)
591 if stale == 1 { o = pd_cat(out, o, " STALE" as *u8) }
592 o = pd_cat(out, o, "</span>" as *u8)
593 }
594 o = pd_cat(out, o, "</td><td><a href=\"/" as *u8)
595 o = pd_esc(out, o, reg, c5[0], c5[1])
596 o = pd_cat(out, o, "\">" as *u8)
597 o = pd_esc(out, o, reg, c5[0], c5[1])
598 o = pd_cat(out, o, "</a></td><td class=d>" as *u8)
599 var n6s: i64 = 0
600 var n6e: i64 = 0
601 if pd_col(reg, i, le5, PD_C_NOTE, c6) == 1 { n6s = c6[0]; n6e = c6[1] }
602 o = pd_esc(out, o, reg, n6s, n6e)
603 o = pd_cat(out, o, "</td></tr>" as *u8)
604 } } } }
605 } }
606 i = le5 + 1
607 }
608 o = pd_cat(out, o, "</table>" as *u8)
609 s = s + 1
610 }
611 // honesty tables: orphans + missing + debris
612 if orph > 0 {
613 o = pd_cat(out, o, "<h2 class=g>UNREGISTERED on disk (orphans -- register or withdraw)</h2><p class=g>" as *u8)
614 k = 0
615 while k < cnt {
616 let off4: i64 = offs[k]
617 var pg2: i64 = 0
618 if pd_is_dirname(arena, off4) == 1 { pg2 = 1 } else { if pd_is_page(arena, off4) == 1 { pg2 = 1 } }
619 if pg2 == 1 { if pd_reg_has(reg, rn, arena, off4) == 0 {
620 o = pd_esc(out, o, arena, off4, off4 + pd_vlen(((arena as i64) + off4) as *u8))
621 o = pd_cat(out, o, " · " as *u8)
622 } }
623 k = k + 1
624 }
625 o = pd_cat(out, o, "</p>" as *u8)
626 }
627 if miss > 0 {
628 o = pd_cat(out, o, "<h2 class=g>REGISTERED but absent on disk (missing)</h2><p class=g>" as *u8)
629 i = 0
630 while i < rn {
631 let le6: i64 = pd_le(reg, i, rn)
632 if pd_col(reg, i, le6, PD_C_STATUS, c3) == 1 { if pd_col(reg, i, le6, PD_C_PATH, c5) == 1 {
633 var lv2: i64 = 1
634 if pd_lit_eq(reg, c3[0], c3[1], "withdrawn" as *u8) == 1 { lv2 = 0 }
635 if pd_lit_eq(reg, c3[0], c3[1], "draft" as *u8) == 1 { lv2 = 0 }
636 if lv2 == 1 { if pd_disk_has(arena, offs, cnt, reg, c5[0], c5[1]) == 0 {
637 o = pd_esc(out, o, reg, c5[0], c5[1])
638 o = pd_cat(out, o, " · " as *u8)
639 } }
640 } }
641 i = le6 + 1
642 }
643 o = pd_cat(out, o, "</p>" as *u8)
644 }
645 if debr > 0 {
646 o = pd_cat(out, o, "<h2 class=g>DEBRIS in the live docroot (unregistered non-page files)</h2><p class=g>" as *u8)
647 k = 0
648 while k < cnt {
649 let off5: i64 = offs[k]
650 var isf: i64 = 0
651 if pd_is_dirname(arena, off5) == 0 { if pd_is_page(arena, off5) == 0 { isf = 1 } }
652 if isf == 1 { if pd_reg_has(reg, rn, arena, off5) == 0 {
653 o = pd_esc(out, o, arena, off5, off5 + pd_vlen(((arena as i64) + off5) as *u8))
654 o = pd_cat(out, o, " · " as *u8)
655 } }
656 k = k + 1
657 }
658 o = pd_cat(out, o, "</p>" as *u8)
659 }
660 // SOTA checklist (pubsota- plane): elite-publisher properties, honestly graded
661 if sn > 0 {
662 o = pd_cat(out, o, "<h2>elite-publisher checklist (researched 2026-07-19; graded honestly, GAP = filed rung)</h2><table><tr><th>property</th><th>grade</th><th>evidence</th><th>note</th></tr>" as *u8)
663 i = 0
664 while i < sn {
665 let le7: i64 = pd_le(sota, i, sn)
666 if pd_col(sota, i, le7, PD_S_PROP, c1) == 1 { if pd_col(sota, i, le7, PD_S_GRADE, c2) == 1 {
667 o = pd_cat(out, o, "<tr><td>" as *u8)
668 o = pd_esc(out, o, sota, c1[0], c1[1])
669 o = pd_cat(out, o, "</td><td><span class=" as *u8)
670 o = pd_cat(out, o, pd_grade_class(sota, c2[0], c2[1]))
671 o = pd_cat(out, o, ">" as *u8)
672 o = pd_esc(out, o, sota, c2[0], c2[1])
673 o = pd_cat(out, o, "</span></td><td class=d>" as *u8)
674 var e3s: i64 = 0
675 var e3e: i64 = 0
676 if pd_col(sota, i, le7, PD_S_EVID, c3) == 1 { e3s = c3[0]; e3e = c3[1] }
677 o = pd_esc(out, o, sota, e3s, e3e)
678 o = pd_cat(out, o, "</td><td class=d>" as *u8)
679 var e4s: i64 = 0
680 var e4e: i64 = 0
681 if pd_col(sota, i, le7, PD_S_NOTE, c4) == 1 { e4s = c4[0]; e4e = c4[1] }
682 o = pd_esc(out, o, sota, e4s, e4e)
683 o = pd_cat(out, o, "</td></tr>" as *u8)
684 } }
685 i = le7 + 1
686 }
687 o = pd_cat(out, o, "</table>" as *u8)
688 }
689 o = pd_cat(out, o, "<p class=d>derived by nx_pub_desk · registry=pub- plane · sota=pubsota- plane · envelope: names_cap " as *u8)
690 o = pd_catn(out, o, PD_MAXN)
691 o = pd_cat(out, o, " dropped " as *u8)
692 o = pd_catn(out, o, envp[0])
693 o = pd_cat(out, o, " · nav: <a href=\"/program\">/program</a> <a href=\"/standup\">/standup</a> <a href=\"/frontier\">/frontier</a> <a href=\"/roi\">/roi</a> <a href=\"/raci\">/raci</a> <a href=\"/sota\">/sota</a></p>" as *u8)
694 o = nxh_close(out, o)
695 // atomic: tmp + rename
696 let tmp: *u8 = sys_mmap(PD_PATHCAP)
697 var to: i64 = pd_cat(tmp, 0, outpath)
698 to = pd_cat(tmp, to, ".nxtmp" as *u8)
699 tmp[to] = 0 as u8
700 if pd_wfile(tmp, out, o) != 0 { return 0 - 1 }
701 if sys_renameat(tmp, outpath) != 0 { return 0 - 1 }
702 pd_puts("PUB-BOARD " as *u8)
703 let nb: *u8 = sys_mmap(PD_NUMCAP)
704 var no2: i64 = pd_catn(nb, 0, o)
705 nb[no2] = 0 as u8
706 pd_puts(nb)
707 pd_puts("B -> " as *u8)
708 pd_puts(outpath)
709 pd_puts("\n" as *u8)
710 return 0
711}
712
713// ---- sitemap: REGISTRY-EMITTED sitemap.xml (F768 half; Guardian dedicated-renderer pattern) ----
714// Only status=live rows that EXIST on disk enter; loc = clean URL per the edge law (/X for X.html,
715// index.html -> /); lastmod = file mtime as ISO date. The hand-maintained sitemap class is dead.
716func pd_sitemap(docroot: *u8, pubprefix: *u8, baseurl: *u8, outpath: *u8) -> i64 {
717 let reg: *u8 = sys_mmap(PD_CAP)
718 let rn: i64 = sts_load(pubprefix, reg, PD_CAP)
719 let out: *u8 = sys_mmap(PD_CAP)
720 let c3: *i64 = sys_mmap(PD_SPAN) as *i64
721 let c5: *i64 = sys_mmap(PD_SPAN) as *i64
722 var o: i64 = 0
723 o = pd_cat(out, o, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n" as *u8)
724 var nurl: i64 = 0
725 var i: i64 = 0
726 while i < rn {
727 let le: i64 = pd_le(reg, i, rn)
728 if pd_col(reg, i, le, PD_C_STATUS, c3) == 1 { if pd_lit_eq(reg, c3[0], c3[1], "live" as *u8) == 1 {
729 if pd_col(reg, i, le, PD_C_PATH, c5) == 1 {
730 let pcb: *u8 = sys_mmap(PD_PATHCAP)
731 var pco: i64 = 0
732 var pct: i64 = c5[0]
733 while pct < c5[1] { pcb[pco] = reg[pct]; pco = pco + 1; pct = pct + 1 }
734 pcb[pco] = 0 as u8
735 let mt: i64 = pd_mtime(docroot, pcb)
736 if mt > 0 {
737 o = pd_cat(out, o, "<url><loc>" as *u8)
738 o = pd_cat(out, o, baseurl)
739 o = pd_cat(out, o, "/" as *u8)
740 // clean URL: index.html -> root; X.html -> X; dirs + other files verbatim
741 var emit_n: i64 = pco
742 if pd_lit_eq(pcb, 0, pco, "index.html" as *u8) == 1 { emit_n = 0 } else {
743 if pd_ends(pcb, 0, pco, ".html" as *u8) == 1 { emit_n = pco - PD_HTML_EXT }
744 }
745 o = pd_esc(out, o, pcb, 0, emit_n)
746 o = pd_cat(out, o, "</loc><lastmod>" as *u8)
747 o = pd_iso(out, o, mt)
748 o = pd_cat(out, o, "</lastmod></url>\n" as *u8)
749 nurl = nurl + 1
750 }
751 }
752 } }
753 i = le + 1
754 }
755 o = pd_cat(out, o, "</urlset>\n" as *u8)
756 let tmp: *u8 = sys_mmap(PD_PATHCAP)
757 var to: i64 = pd_cat(tmp, 0, outpath)
758 to = pd_cat(tmp, to, ".nxtmp" as *u8)
759 tmp[to] = 0 as u8
760 if pd_wfile(tmp, out, o) != 0 { return 0 - 1 }
761 if sys_renameat(tmp, outpath) != 0 { return 0 - 1 }
762 pd_puts("PUB-SITEMAP urls=" as *u8)
763 let nb: *u8 = sys_mmap(PD_NUMCAP)
764 var no: i64 = pd_catn(nb, 0, nurl)
765 nb[no] = 0 as u8
766 pd_puts(nb)
767 pd_puts(" -> " as *u8)
768 pd_puts(outpath)
769 pd_puts("\n" as *u8)
770 return 0
771}
772
773// ---- check: the publish GATEWAY tooth (F763, NYT-Gateway pattern) -----------------------------
774// A relpath may be published ONLY if the registry carries it with a publishable status
775// (live|asset|draft). Unregistered, withdrawn, redirect, or debris -> REFUSED (exit 3).
776// The pipeline (plan/beat/commontask) calls this BEFORE nx_site_publish; enforcement inside
777// nx_site_publish itself = the F763 second half (sites lane owns that organ).
778func pd_check(relpath: *u8, pubprefix: *u8) -> i64 {
779 let reg: *u8 = sys_mmap(PD_CAP)
780 let rn: i64 = sts_load(pubprefix, reg, PD_CAP)
781 let c3: *i64 = sys_mmap(PD_SPAN) as *i64
782 let c5: *i64 = sys_mmap(PD_SPAN) as *i64
783 let pl: i64 = pd_vlen(relpath)
784 var i: i64 = 0
785 while i < rn {
786 let le: i64 = pd_le(reg, i, rn)
787 if pd_col(reg, i, le, PD_C_PATH, c5) == 1 { if pd_lit_eq(reg, c5[0], c5[1], relpath) == 1 {
788 if pd_col(reg, i, le, PD_C_STATUS, c3) == 1 {
789 var ok: i64 = 0
790 if pd_lit_eq(reg, c3[0], c3[1], "live" as *u8) == 1 { ok = 1 }
791 if pd_lit_eq(reg, c3[0], c3[1], "asset" as *u8) == 1 { ok = 1 }
792 if pd_lit_eq(reg, c3[0], c3[1], "draft" as *u8) == 1 { ok = 1 }
793 if ok == 1 {
794 pd_puts("PUB-CHECK OK " as *u8)
795 pd_puts(relpath)
796 pd_puts(" status=" as *u8)
797 sys_write(1, ((reg as i64) + c3[0]) as *u8, c3[1] - c3[0])
798 pd_puts("\n" as *u8)
799 return 0
800 }
801 pd_puts("PUB-CHECK REFUSED " as *u8)
802 pd_puts(relpath)
803 pd_puts(" non-publishable status=" as *u8)
804 sys_write(1, ((reg as i64) + c3[0]) as *u8, c3[1] - c3[0])
805 pd_puts(" (withdraw/redirect/debris never republish; flip the registry row first)\n" as *u8)
806 return PD_EXIT_REFUSED
807 }
808 } }
809 i = le + 1
810 }
811 pd_puts("PUB-CHECK REFUSED " as *u8)
812 pd_puts(relpath)
813 pd_puts(" UNREGISTERED (rows=" as *u8)
814 let nb: *u8 = sys_mmap(PD_NUMCAP)
815 var rows: i64 = 0
816 i = 0
817 while i < rn { let le2: i64 = pd_le(reg, i, rn); if le2 > i { rows = rows + 1 } i = le2 + 1 }
818 var no: i64 = pd_catn(nb, 0, rows)
819 nb[no] = 0 as u8
820 pd_puts(nb)
821 pd_puts(") -- register via nx_store_put pub- put, then publish\n" as *u8)
822 if pl == 0 { return PD_EXIT_REFUSED }
823 return PD_EXIT_REFUSED
824}
825
826// ---- selftest: fixtures + scale tooth (gate contract via nx_gate_verdict) ----
827func pd_mkpath(d: *u8, a: *u8, b: *u8) -> i64 { var o: i64 = pd_cat(d, 0, a); o = pd_cat(d, o, b); d[o] = 0 as u8; return o }
828func pd_selftest(tmpdir: *u8) -> i64 {
829 let ctr: *i64 = gv_ctr()
830 gv_head("nx_pub_desk gate -- registry-vs-disk join + board derivation + scale" as *u8)
831 let root: *u8 = sys_mmap(PD_PATHCAP)
832 pd_mkpath(root, tmpdir, "/pd_root" as *u8)
833 sys_mkdir(root, PD_DIRMODE)
834 let p: *u8 = sys_mmap(PD_PATHCAP)
835 pd_mkpath(p, root, "/a.html" as *u8)
836 pd_wfile(p, "<h1>A</h1>" as *u8, PD_FIXA_LEN)
837 pd_mkpath(p, root, "/b.html" as *u8)
838 pd_wfile(p, "<h1>B</h1>" as *u8, PD_FIXA_LEN)
839 pd_mkpath(p, root, "/c.txt" as *u8)
840 pd_wfile(p, "probe" as *u8, PD_FIXC_LEN)
841 pd_mkpath(p, root, "/a.html.prev" as *u8)
842 pd_wfile(p, "old" as *u8, PD_FIXP_LEN)
843 // scale tooth: production-shape page count (PD_SCALE_N)
844 var fi: i64 = 0
845 while fi < PD_SCALE_N {
846 let nb: *u8 = sys_mmap(PD_SMALLCAP)
847 var no: i64 = pd_cat(nb, 0, "/f" as *u8)
848 no = pd_catn(nb, no, fi)
849 no = pd_cat(nb, no, ".html" as *u8)
850 nb[no] = 0 as u8
851 pd_mkpath(p, root, nb)
852 pd_wfile(p, "x" as *u8, 1)
853 fi = fi + 1
854 }
855 // registry fixture: a registered-live, ghost registered-live-but-absent, gone withdrawn-absent
856 let pubpfx: *u8 = sys_mmap(PD_PATHCAP)
857 pd_mkpath(pubpfx, tmpdir, "/pdgate_pub-" as *u8)
858 let rbuf: *u8 = sys_mmap(PD_FIXCAP)
859 var ro: i64 = 0
860 ro = pd_cat(rbuf, ro, "a\tPage A\tpm\tlive\tprogram\ta.html\tseed\n" as *u8)
861 ro = pd_cat(rbuf, ro, "ghost\tGhost\tpm\tlive\tprogram\tghost.html\tseed\n" as *u8)
862 ro = pd_cat(rbuf, ro, "gone\tOld\tpm\twithdrawn\tlegacy\tgone.html\tseed\n" as *u8)
863 let sr: i64 = sts_seed(pubpfx, rbuf, ro)
864 gv_check("T0 registry fixture seeded (3 rows)" as *u8, (sr == PD_FIX_ROWS) as i64, ctr)
865 let sotapfx: *u8 = sys_mmap(PD_PATHCAP)
866 pd_mkpath(sotapfx, tmpdir, "/pdgate_sota-" as *u8)
867 let sbuf: *u8 = sys_mmap(PD_FIXSCAP)
868 var so: i64 = 0
869 so = pd_cat(sbuf, so, "p1\tregistry-derived hubs\tHAVE\tthis page\tseed\n" as *u8)
870 so = pd_cat(sbuf, so, "p2\tfeeds-sitemap\tGAP\t-\tseed\n" as *u8)
871 sts_seed(sotapfx, sbuf, so)
872 // census
873 let out: *u8 = sys_mmap(PD_CAP)
874 let n: i64 = pd_census(root, pubpfx, out, PD_CAP, 0)
875 gv_check("T1 census counts 252 pages (250 scale + a + b)" as *u8, pd_find(out, n, "\"pages\":252" as *u8), ctr)
876 gv_check("T2 registered join = 1 (a.html)" as *u8, pd_find(out, n, "\"registered_on_disk\":1" as *u8), ctr)
877 gv_check("T3 orphan detected (b.html unregistered)" as *u8, pd_find(out, n, "b.html" as *u8), ctr)
878 gv_check("T4 debris detected (c.txt)" as *u8, pd_find(out, n, "c.txt" as *u8), ctr)
879 gv_check("T5 .prev excluded as rollback artifact" as *u8, (1 - pd_find(out, n, "a.html.prev" as *u8)) as i64, ctr)
880 gv_check("T6 missing detects ghost.html" as *u8, pd_find(out, n, "ghost.html" as *u8), ctr)
881 gv_check("T7 withdrawn gone.html NOT missing" as *u8, (1 - pd_find(out, n, "gone.html" as *u8)) as i64, ctr)
882 gv_check("T8 scale envelope declared, dropped=0" as *u8, pd_find(out, n, "\"dropped\":0" as *u8), ctr)
883 gv_check("T9 prev_artifacts counted = 1" as *u8, pd_find(out, n, "\"prev_artifacts\":1" as *u8), ctr)
884 // no-fabrication: unseeded registry -> rows 0, honest all-orphan
885 let nopfx: *u8 = sys_mmap(PD_PATHCAP)
886 pd_mkpath(nopfx, tmpdir, "/pdgate_nope-" as *u8)
887 let n2: i64 = pd_census(root, nopfx, out, PD_CAP, 0)
888 gv_check("T10 unseeded registry = honest rows:0 (no fabrication)" as *u8, pd_find(out, n2, "\"registry_rows\":0" as *u8), ctr)
889 // board
890 let bp: *u8 = sys_mmap(PD_PATHCAP)
891 pd_mkpath(bp, tmpdir, "/pd_board.html" as *u8)
892 let brc: i64 = pd_board(root, pubpfx, sotapfx, bp, PD_STALE_FIX)
893 gv_check("T11 board emit rc=0 (atomic tmp+rename)" as *u8, (brc == 0) as i64, ctr)
894 let bbuf: *u8 = sys_mmap(PD_CAP)
895 let bn: i64 = pd_rfile(bp, bbuf, PD_CAP)
896 gv_check("T12 board renders section from registry (program)" as *u8, pd_find(bbuf, bn, "<h2>program</h2>" as *u8), ctr)
897 gv_check("T13 board renders orphans honestly" as *u8, pd_find(bbuf, bn, "UNREGISTERED" as *u8), ctr)
898 gv_check("T14 board renders sota GAP row" as *u8, pd_find(bbuf, bn, "feeds-sitemap" as *u8), ctr)
899 // gateway tooth (F763): registered-live passes, unregistered refused, withdrawn refused
900 gv_check("T15 check passes registered-live a.html" as *u8, (pd_check("a.html" as *u8, pubpfx) == 0) as i64, ctr)
901 gv_check("T16 check REFUSES unregistered b.html" as *u8, (pd_check("b.html" as *u8, pubpfx) == PD_EXIT_REFUSED) as i64, ctr)
902 gv_check("T17 check REFUSES withdrawn gone.html" as *u8, (pd_check("gone.html" as *u8, pubpfx) == PD_EXIT_REFUSED) as i64, ctr)
903 // freshness teeth: DETERMINISTIC backdate via the sovereign touch (utimensat)
904 let tsb: *i64 = sys_mmap(PD_SPAN * PD_MP_BIAS) as *i64
905 let oldep: i64 = sys_now_realtime_sec() - PD_BACKDATE_D * PD_DAY
906 tsb[0] = oldep
907 tsb[1] = 0
908 tsb[PD_MP_BIAS] = oldep
909 tsb[PD_MAR_OFF] = 0
910 pd_mkpath(p, root, "/a.html" as *u8)
911 sys_utimensat(p, tsb)
912 let n3: i64 = pd_census(root, pubpfx, out, PD_CAP, PD_STALE_FIX)
913 gv_check("T18 stale sweep flags backdated a.html (200d > 90d)" as *u8, pd_find(out, n3, "\"stale\":[\"a.html\"]" as *u8), ctr)
914 gv_check("T19 stale_count exactly 1 (fresh pages not flagged)" as *u8, pd_find(out, n3, "\"stale_count\":1" as *u8), ctr)
915 // sitemap teeth: registry-emitted, clean URLs, absent/unregistered excluded
916 let smp: *u8 = sys_mmap(PD_PATHCAP)
917 pd_mkpath(smp, tmpdir, "/pd_sitemap.xml" as *u8)
918 let src: i64 = pd_sitemap(root, pubpfx, "https://x.test" as *u8, smp)
919 gv_check("T20 sitemap emit rc=0" as *u8, (src == 0) as i64, ctr)
920 let smb: *u8 = sys_mmap(PD_LCAP)
921 let smn: i64 = pd_rfile(smp, smb, PD_LCAP)
922 gv_check("T21 sitemap has clean-url loc + lastmod for a.html" as *u8, pd_find(smb, smn, "<loc>https://x.test/a</loc><lastmod>" as *u8), ctr)
923 gv_check("T22 registered-but-absent ghost NOT in sitemap" as *u8, (1 - pd_find(smb, smn, "ghost" as *u8)) as i64, ctr)
924 gv_check("T23 unregistered b.html NOT in sitemap (registry-derived)" as *u8, (1 - pd_find(smb, smn, "x.test/b" as *u8)) as i64, ctr)
925 let rc: i64 = gv_verdict("PUB-DESK-GATE" as *u8, ctr, "registry-derived publishing desk discriminates orphan/missing/debris at scale" as *u8)
926 return rc
927}
928
929func main(argc: i64, argv: *i64) -> i64 {
930 if argc < 2 { pd_werr("usage: nx_pub_desk {census <docroot> <pubprefix> [stale_days] | board <docroot> <pubprefix> <sotaprefix> <out> [stale_days] | sitemap <docroot> <pubprefix> <baseurl> <out> | check <relpath> [pubprefix] | selftest <tmpdir>}\n" as *u8); sys_exit(PD_EXIT_USAGE); return PD_EXIT_USAGE }
931 let verb: *u8 = argv[1] as *u8
932 if pd_lit_eq(verb, 0, pd_vlen(verb), "census" as *u8) == 1 {
933 var dr: *u8 = "sites/nishifamily" as *u8
934 var pp: *u8 = "knowledge/store/pub-" as *u8
935 if argc > PD_ARG_A{ dr = argv[PD_ARG_A] as *u8 }
936 if argc > PD_ARG_B{ pp = argv[PD_ARG_B] as *u8 }
937 var sd: i64 = 0
938 if argc > PD_ARG_C { sd = pd_argnum(argv[PD_ARG_C] as *u8) }
939 let out: *u8 = sys_mmap(PD_CAP)
940 let n: i64 = pd_census(dr, pp, out, PD_CAP, sd)
941 sys_write(1, out, n)
942 pd_puts("\n" as *u8)
943 sys_exit(0)
944 return 0
945 }
946 if pd_lit_eq(verb, 0, pd_vlen(verb), "board" as *u8) == 1 {
947 var dr2: *u8 = "sites/nishifamily" as *u8
948 var pp2: *u8 = "knowledge/store/pub-" as *u8
949 var sp2: *u8 = "knowledge/store/pubsota-" as *u8
950 var op2: *u8 = "web_assets/publishing_stage.html" as *u8
951 if argc > PD_ARG_A{ dr2 = argv[PD_ARG_A] as *u8 }
952 if argc > PD_ARG_B{ pp2 = argv[PD_ARG_B] as *u8 }
953 if argc > PD_ARG_C{ sp2 = argv[PD_ARG_C] as *u8 }
954 if argc > PD_ARG_D { op2 = argv[PD_ARG_D] as *u8 }
955 var sd2: i64 = 0
956 if argc > PD_ARG_E { sd2 = pd_argnum(argv[PD_ARG_E] as *u8) }
957 let rc: i64 = pd_board(dr2, pp2, sp2, op2, sd2)
958 if rc != 0 { pd_werr("PUB-FAIL board emit\n" as *u8); sys_exit(1); return 1 }
959 sys_exit(0)
960 return 0
961 }
962 if pd_lit_eq(verb, 0, pd_vlen(verb), "sitemap" as *u8) == 1 {
963 var dr3: *u8 = "sites/nishifamily" as *u8
964 var pp4: *u8 = "knowledge/store/pub-" as *u8
965 var bu3: *u8 = "https://nishifamily.com" as *u8
966 var op3: *u8 = "web_assets/sitemap_stage.xml" as *u8
967 if argc > PD_ARG_A { dr3 = argv[PD_ARG_A] as *u8 }
968 if argc > PD_ARG_B { pp4 = argv[PD_ARG_B] as *u8 }
969 if argc > PD_ARG_C { bu3 = argv[PD_ARG_C] as *u8 }
970 if argc > PD_ARG_D { op3 = argv[PD_ARG_D] as *u8 }
971 let rcs: i64 = pd_sitemap(dr3, pp4, bu3, op3)
972 if rcs != 0 { pd_werr("PUB-FAIL sitemap emit\n" as *u8); sys_exit(1); return 1 }
973 sys_exit(0)
974 return 0
975 }
976 if pd_lit_eq(verb, 0, pd_vlen(verb), "check" as *u8) == 1 {
977 if argc < PD_ARG_B { pd_werr("PUB-FAIL check needs <relpath> [pubprefix]\n" as *u8); sys_exit(PD_EXIT_USAGE); return PD_EXIT_USAGE }
978 var pp3: *u8 = "knowledge/store/pub-" as *u8
979 if argc > PD_ARG_B { pp3 = argv[PD_ARG_B] as *u8 }
980 let rc3: i64 = pd_check(argv[PD_ARG_A] as *u8, pp3)
981 sys_exit(rc3)
982 return rc3
983 }
984 if pd_lit_eq(verb, 0, pd_vlen(verb), "selftest" as *u8) == 1 {
985 if argc < PD_ARG_B { pd_werr("PUB-FAIL selftest needs <tmpdir>\n" as *u8); sys_exit(PD_EXIT_USAGE); return PD_EXIT_USAGE }
986 let rc2: i64 = pd_selftest(argv[PD_ARG_A] as *u8)
987 sys_exit(rc2)
988 return rc2
989 }
990 pd_werr("PUB-FAIL unknown verb\n" as *u8)
991 sys_exit(PD_EXIT_USAGE)
992 return PD_EXIT_USAGE
993}