nx_mediaflow_lib.nx source
↩ module page · 270 lines · 12918 B
1// nx_mediaflow_lib.nx -- aesthetictwin AT36 (2026-09-17): THE MEDIA WORKFLOW COMPILED TO ONE CALL. Operator: "work should
2// just be composable orchestrated mcp and apis and workflows and agents with a focus to 1 2 3 step optimization ... an
3// optimized path on repeatable work to get steps to results reduced". Judging one media file took five hand steps (make
4// the report directory, cut a one-row oracle, write plan rows, seed them, run the plan). This library compiles them:
5// it validates the slug, the kind and the paths AT THE BOUNDARY (they become a directory name, a store prefix and plan
6// columns, so a tab, a newline or a dot-dot would be an injected column, an injected STEP or an escaped directory),
7// makes the report directory, picks the media's own row out of a many-row oracle file and rewrites its path so the
8// referee can open it, and compiles the plan rows the estate's own executor (nx_plan_run) runs: vault classify, judge,
9// seed the asset plane, emit the page, verify the live page. WORKFLOWS STAY DATA: the rows land in
10// knowledge/store/plan-mediaflow-<slug>- and every step is an allowlisted tool under the tools-plane security model.
11// Consumers: nx_mediaflow (CLI), nx_mediaflow_gate (planted controls).
12// license_tier: ORIGINAL No hw writes (Rule 26).
13import "nx_syscalls.nx"
14import "nx_colread_lib.nx"
15
16const MF_SITE_DIR: *u8 = "sites/nishifamily/compare/aesthetictwin/media/"
17const MF_HREF_BASE: *u8 = "/compare/aesthetictwin/media/"
18const MF_URL_BASE: *u8 = "https://nishifamily.com/compare/aesthetictwin/media/"
19const MF_ASSET_PLANE_PRE: *u8 = "knowledge/store/asset-"
20const MF_PLAN_PREFIX_PRE: *u8 = "knowledge/store/plan-mediaflow-"
21const MF_PLAN_ID_PRE: *u8 = "mediaflow-"
22const MF_ROOT_KNOWLEDGE: *u8 = "knowledge/" // the three places submitted media may be read from: the estate's
23const MF_ROOT_TMP: *u8 = "/tmp/" // knowledge tree (the fetch and put doors land there), scratch,
24const MF_ROOT_VAULT: *u8 = "/volume1/vault/" // and the vault itself
25const MF_KIND_REAL: *u8 = "real"
26const MF_KIND_GEN: *u8 = "gen"
27const MF_NO_ORACLE: *u8 = "-"
28const MF_EXT_NONE: *u8 = "bin"
29const MF_SLUG_MAX: i64 = 64
30const MF_PATH_CAP: i64 = 1024
31const MF_PLAN_CAP: i64 = 8192 // five rows of at most five paths each: a derived ceiling, exceeded = refused
32const MF_PLAN_STEPS: i64 = 5
33const MF_OF_FILE: i64 = 1 // the oracle row's file column (oracle|<file>|<sha>|...)
34const MF_PIPE: i64 = 124
35const MF_TAB: i64 = 9
36const MF_NL: i64 = 10
37const MF_HASH: i64 = 35
38const MF_SLASH: i64 = 47
39const MF_DOT: i64 = 46
40const MF_DASH: i64 = 45
41const MF_UNDER: i64 = 95
42const MF_PRINT_LO: i64 = 33 // the first printable non-space byte: below it lie space, tab, newline, controls
43const MF_DEL: i64 = 127
44const MF_LOWER_A: i64 = 97
45const MF_LOWER_Z: i64 = 122
46const MF_DIGIT_0: i64 = 48
47const MF_DIGIT_9: i64 = 57
48const MF_MODE755: i64 = 493
49const MF_MODE644: i64 = 420
50const MF_E_UNREADABLE: i64 = 0 - 1
51
52func mf_cat(out: *u8, o: i64, s: *u8) -> i64 {
53 var p: i64 = o
54 var i: i64 = 0
55 while s[i] != (0 as u8) { out[p] = s[i]; p = p + 1; i = i + 1 }
56 out[p] = 0 as u8
57 return p
58}
59func mf_starts(s: *u8, pre: *u8) -> i64 {
60 var i: i64 = 0
61 while pre[i] != (0 as u8) {
62 if s[i] != pre[i] { return 0 }
63 i = i + 1
64 }
65 return 1
66}
67func mf_streq(a: *u8, b: *u8) -> i64 {
68 var i: i64 = 0
69 while a[i] != (0 as u8) {
70 if a[i] != b[i] { return 0 }
71 i = i + 1
72 }
73 if b[i] != (0 as u8) { return 0 }
74 return 1
75}
76// a slug becomes a directory name, a store prefix and a URL segment: lower-case letters, digits, dash and underscore,
77// 1..MF_SLUG_MAX bytes, starting with a letter or a digit. Anything else is refused, never repaired.
78func mf_slug_ok(slug: *u8) -> i64 {
79 let n: i64 = cr_slen(slug)
80 if n < 1 { return 0 }
81 if n > MF_SLUG_MAX { return 0 }
82 var i: i64 = 0
83 while i < n {
84 let c: i64 = slug[i] as i64
85 var ok: i64 = 0
86 if c >= MF_LOWER_A { if c <= MF_LOWER_Z { ok = 1 } }
87 if c >= MF_DIGIT_0 { if c <= MF_DIGIT_9 { ok = 1 } }
88 if i > 0 { if c == MF_DASH { ok = 1 } if c == MF_UNDER { ok = 1 } }
89 if ok == 0 { return 0 }
90 i = i + 1
91 }
92 return 1
93}
94// a path becomes a plan COLUMN: a tab would add a column, a newline would add a STEP, a space breaks the clock
95// dispatcher's split, and a dot-dot leaves the allowed roots. Every byte must be printable non-space, no dot-dot,
96// and the path must start under one of the three roots media is read from.
97func mf_path_ok(path: *u8) -> i64 {
98 let n: i64 = cr_slen(path)
99 if n < 1 { return 0 }
100 if n >= MF_PATH_CAP { return 0 }
101 var i: i64 = 0
102 while i < n {
103 let c: i64 = path[i] as i64
104 if c < MF_PRINT_LO { return 0 }
105 if c >= MF_DEL { return 0 }
106 if c == MF_DOT { if i + 1 < n { if (path[i + 1] as i64) == MF_DOT { return 0 } } }
107 i = i + 1
108 }
109 if mf_starts(path, MF_ROOT_KNOWLEDGE) == 1 { return 1 }
110 if mf_starts(path, MF_ROOT_TMP) == 1 { return 1 }
111 if mf_starts(path, MF_ROOT_VAULT) == 1 { return 1 }
112 return 0
113}
114func mf_kind_ok(kind: *u8) -> i64 {
115 if mf_streq(kind, MF_KIND_REAL) == 1 { return 1 }
116 if mf_streq(kind, MF_KIND_GEN) == 1 { return 1 }
117 return 0
118}
119// the file name of a path: the bytes after its last slash
120func mf_basename(path: *u8) -> *u8 {
121 var i: i64 = 0
122 var last: i64 = 0 - 1
123 while path[i] != (0 as u8) {
124 if (path[i] as i64) == MF_SLASH { last = i }
125 i = i + 1
126 }
127 return (path as i64 + last + 1) as *u8
128}
129// the extension of a path's file name, or MF_EXT_NONE when it has none
130func mf_ext(path: *u8) -> *u8 {
131 let base: *u8 = mf_basename(path)
132 var i: i64 = 0
133 var dot: i64 = 0 - 1
134 while base[i] != (0 as u8) {
135 if (base[i] as i64) == MF_DOT { dot = i }
136 i = i + 1
137 }
138 if dot < 0 { return MF_EXT_NONE }
139 if base[dot + 1] == (0 as u8) { return MF_EXT_NONE }
140 return (base as i64 + dot + 1) as *u8
141}
142// the report directory of a slug into out; returns its length
143func mf_outdir(slug: *u8, out: *u8) -> i64 {
144 var o: i64 = mf_cat(out, 0, MF_SITE_DIR)
145 o = mf_cat(out, o, slug)
146 return o
147}
148// make every directory of a relative or absolute path; an existing directory is not an error. Returns the number of
149// components walked (the caller's first write is the proof the leaf exists).
150func mf_mkdirp(path: *u8) -> i64 {
151 let n: i64 = cr_slen(path)
152 let part: *u8 = sys_mmap(MF_PATH_CAP)
153 var made: i64 = 0
154 var i: i64 = 0
155 while i <= n {
156 var cut: i64 = 0
157 if i == n { cut = 1 } else { if (path[i] as i64) == MF_SLASH { if i > 0 { cut = 1 } } }
158 if cut == 1 {
159 var k: i64 = 0
160 while k < i { part[k] = path[k]; k = k + 1 }
161 part[i] = 0 as u8
162 sys_mkdir(part, MF_MODE755)
163 made = made + 1
164 }
165 i = i + 1
166 }
167 return made
168}
169// pick the media's own row out of an oracle file and write it, alone, to out_path with its file column rewritten to
170// the media path (intake oracle rows name bare files; the referee opens the column as a path). The row matches when
171// its file column equals the media path or the media's file name. 1 picked, 0 no row for this media, -1 unreadable.
172func mf_oracle_pick(oracle_path: *u8, media_path: *u8, out_path: *u8) -> i64 {
173 let fl: *i64 = sys_mmap(16) as *i64
174 fl[0] = 0
175 let buf: *u8 = sys_read_file(oracle_path, fl)
176 if (buf as i64) == 0 { return MF_E_UNREADABLE }
177 let n: i64 = fl[0]
178 if n <= 0 { return MF_E_UNREADABLE }
179 let base: *u8 = mf_basename(media_path)
180 let blen: i64 = cr_slen(base)
181 let mlen: i64 = cr_slen(media_path)
182 let span: *i64 = sys_mmap(16) as *i64
183 var ls: i64 = 0
184 while ls < n {
185 let le: i64 = cr_eol(buf, n, ls)
186 let te: i64 = cr_trim(buf, ls, le)
187 if te > ls {
188 if (buf[ls] as i64) != MF_HASH {
189 if cr_col(buf, ls, te, MF_PIPE, MF_OF_FILE, span) == 1 {
190 var hit: i64 = cr_eq(buf, span[0], span[1], media_path, mlen)
191 if hit == 0 { hit = cr_eq(buf, span[0], span[1], base, blen) }
192 if hit == 1 {
193 // the row with its file column replaced: [ls,span0) + media_path + [span1,te) + NL
194 let row: *u8 = sys_mmap((te - ls) + mlen + 2)
195 var o: i64 = 0
196 var i: i64 = ls
197 while i < span[0] { row[o] = buf[i]; o = o + 1; i = i + 1 }
198 i = 0
199 while i < mlen { row[o] = media_path[i]; o = o + 1; i = i + 1 }
200 i = span[1]
201 while i < te { row[o] = buf[i]; o = o + 1; i = i + 1 }
202 row[o] = MF_NL as u8
203 o = o + 1
204 let fd: i64 = sys_openat_wr(out_path, MF_MODE644)
205 if fd < 0 { return MF_E_UNREADABLE }
206 let w: i64 = sys_write(fd, row, o)
207 sys_close(fd)
208 if w != o { return MF_E_UNREADABLE }
209 return 1
210 }
211 }
212 }
213 }
214 ls = le + 1
215 }
216 return 0
217}
218func mf_tab(out: *u8, o: i64) -> i64 { out[o] = MF_TAB as u8; out[o + 1] = 0 as u8; return o + 1 }
219func mf_nl(out: *u8, o: i64) -> i64 { out[o] = MF_NL as u8; out[o + 1] = 0 as u8; return o + 1 }
220// the plan id of a slug into out (mediaflow-<slug>)
221func mf_plan_id(slug: *u8, out: *u8) -> i64 { var o: i64 = mf_cat(out, 0, MF_PLAN_ID_PRE); o = mf_cat(out, o, slug); return o }
222// the plan plane prefix of a slug into out (knowledge/store/plan-mediaflow-<slug>-)
223func mf_plan_prefix(slug: *u8, out: *u8) -> i64 {
224 var o: i64 = mf_cat(out, 0, MF_PLAN_PREFIX_PRE)
225 o = mf_cat(out, o, slug)
226 o = mf_cat(out, o, "-" as *u8)
227 return o
228}
229// the live URL of a slug's report into out
230func mf_url(slug: *u8, out: *u8) -> i64 { var o: i64 = mf_cat(out, 0, MF_URL_BASE); o = mf_cat(out, o, slug); o = mf_cat(out, o, "/" as *u8); return o }
231// THE COMPILER: the five plan rows for one media file. Inputs are already validated; oracle is a readable one-row
232// oracle path or MF_NO_ORACLE. Returns the byte count, or -1 when the rows would not fit the derived ceiling.
233func mf_plan_rows(slug: *u8, media: *u8, outdir: *u8, oracle: *u8, kind: *u8, buf: *u8) -> i64 {
234 let need: i64 = cr_slen(slug) * 8 + cr_slen(media) * 2 + cr_slen(outdir) * 3 + cr_slen(oracle) + 512
235 if need >= MF_PLAN_CAP { return 0 - 1 }
236 // 1: the vault's content id and class
237 var o: i64 = mf_cat(buf, 0, "1" as *u8); o = mf_tab(buf, o)
238 o = mf_cat(buf, o, "nx_mvault" as *u8); o = mf_tab(buf, o)
239 o = mf_cat(buf, o, "classify" as *u8); o = mf_tab(buf, o)
240 o = mf_cat(buf, o, media); o = mf_tab(buf, o)
241 o = mf_cat(buf, o, kind); o = mf_tab(buf, o)
242 o = mf_cat(buf, o, MF_PLAN_ID_PRE); o = mf_cat(buf, o, slug); o = mf_tab(buf, o)
243 o = mf_cat(buf, o, mf_ext(media)); o = mf_nl(buf, o)
244 // 2: the judge, with the site-absolute href prefix the page law demands
245 o = mf_cat(buf, o, "2" as *u8); o = mf_tab(buf, o)
246 o = mf_cat(buf, o, "nx_mediajudge" as *u8); o = mf_tab(buf, o)
247 o = mf_cat(buf, o, "judge" as *u8); o = mf_tab(buf, o)
248 o = mf_cat(buf, o, slug); o = mf_tab(buf, o)
249 o = mf_cat(buf, o, media); o = mf_tab(buf, o)
250 o = mf_cat(buf, o, outdir); o = mf_tab(buf, o)
251 o = mf_cat(buf, o, oracle); o = mf_tab(buf, o)
252 o = mf_cat(buf, o, MF_HREF_BASE); o = mf_cat(buf, o, slug); o = mf_cat(buf, o, "/" as *u8); o = mf_nl(buf, o)
253 // 3: the slug-keyed rows into the asset plane
254 o = mf_cat(buf, o, "3" as *u8); o = mf_tab(buf, o)
255 o = mf_cat(buf, o, "nx_store_seed" as *u8); o = mf_tab(buf, o)
256 o = mf_cat(buf, o, MF_ASSET_PLANE_PRE); o = mf_cat(buf, o, slug); o = mf_cat(buf, o, "-" as *u8); o = mf_tab(buf, o)
257 o = mf_cat(buf, o, outdir); o = mf_cat(buf, o, "/" as *u8); o = mf_cat(buf, o, slug); o = mf_cat(buf, o, ".plane.tsv" as *u8); o = mf_nl(buf, o)
258 // 4: the three-tier page from the plane
259 o = mf_cat(buf, o, "4" as *u8); o = mf_tab(buf, o)
260 o = mf_cat(buf, o, "nx_asset_page" as *u8); o = mf_tab(buf, o)
261 o = mf_cat(buf, o, "emit" as *u8); o = mf_tab(buf, o)
262 o = mf_cat(buf, o, MF_ASSET_PLANE_PRE); o = mf_cat(buf, o, slug); o = mf_cat(buf, o, "-" as *u8); o = mf_tab(buf, o)
263 o = mf_cat(buf, o, slug); o = mf_tab(buf, o)
264 o = mf_cat(buf, o, outdir); o = mf_cat(buf, o, "/index.html" as *u8); o = mf_nl(buf, o)
265 // 5: the live page, checked from the edge
266 o = mf_cat(buf, o, "5" as *u8); o = mf_tab(buf, o)
267 o = mf_cat(buf, o, "nx_page_verify" as *u8); o = mf_tab(buf, o)
268 o = mf_cat(buf, o, MF_URL_BASE); o = mf_cat(buf, o, slug); o = mf_cat(buf, o, "/" as *u8); o = mf_nl(buf, o)
269 return o
270}