nx_mediaflow_gate.nx source
↩ module page · 146 lines · 10428 B
1// nx_mediaflow_gate.nx -- the referee for nx_mediaflow_lib (aesthetictwin AT36, 2026-09-17). The library turns caller
2// input into a directory name, a store prefix and plan COLUMNS, so its first job is the boundary: a slug or a path that
3// carries a tab, a newline, a space, a dot-dot or a root outside the three media roots is refused. Then the pieces the
4// one-call workflow stands on: the file name and extension readers (a dot in a DIRECTORY is not an extension), the
5// directory maker, the oracle-row picker (exact file-column match, CRLF tolerated, the path rewritten, one row out) and
6// the plan compiler, held to a byte-exact known answer and to five rows whose tools are the five workflow steps in order.
7// license_tier: ORIGINAL No hw writes (Rule 26).
8import "nx_syscalls.nx"
9import "nx_gate_verdict.nx"
10import "nx_mediaflow_lib.nx"
11
12const FG_DIR: *u8 = "/tmp/mediaflow_gate"
13const FG_DEEP: *u8 = "/tmp/mediaflow_gate/a/b/c"
14const FG_DEEP_FILE: *u8 = "/tmp/mediaflow_gate/a/b/c/probe.txt"
15const FG_ORACLE: *u8 = "/tmp/mediaflow_gate/many.oracle"
16const FG_PICKED: *u8 = "/tmp/mediaflow_gate/one.oracle"
17const FG_NOPICK: *u8 = "/tmp/mediaflow_gate/none.oracle"
18const FG_ABSENT: *u8 = "/tmp/mediaflow_gate/absent/none.oracle"
19// a comment, a row for a.jpg, a CRLF row for b.jpg and a look-alike whose file column only ENDS with b.jpg
20const FG_ORACLE_TEXT: *u8 = "# oracle rows\noracle|a.jpg|sha-a|768|1014|1|11,12|13,14\noracle|xb.jpg|sha-x|640|480|1|91,92|93,94\noracle|b.jpg|sha-b|512|384|1|21,22|23,24\r\n"
21const FG_MEDIA_B: *u8 = "knowledge/fetched/b.jpg"
22const FG_MEDIA_NONE: *u8 = "knowledge/fetched/zzz.jpg"
23const FG_PICKED_EXPECT: *u8 = "oracle|knowledge/fetched/b.jpg|sha-b|512|384|1|21,22|23,24\n"
24const FG_KAT_SLUG: *u8 = "s1"
25const FG_KAT_MEDIA: *u8 = "knowledge/fetched/m.jpg"
26const FG_KAT_OUTDIR: *u8 = "sites/nishifamily/compare/aesthetictwin/media/s1"
27const FG_KAT_ROWS: *u8 = "1\tnx_mvault\tclassify\tknowledge/fetched/m.jpg\treal\tmediaflow-s1\tjpg\n2\tnx_mediajudge\tjudge\ts1\tknowledge/fetched/m.jpg\tsites/nishifamily/compare/aesthetictwin/media/s1\t-\t/compare/aesthetictwin/media/s1/\n3\tnx_store_seed\tknowledge/store/asset-s1-\tsites/nishifamily/compare/aesthetictwin/media/s1/s1.plane.tsv\n4\tnx_asset_page\temit\tknowledge/store/asset-s1-\ts1\tsites/nishifamily/compare/aesthetictwin/media/s1/index.html\n5\tnx_page_verify\thttps://nishifamily.com/compare/aesthetictwin/media/s1/\n"
28const FG_SLUG_65: *u8 = "a1234567890123456789012345678901234567890123456789012345678901234"
29const FG_MODE755: i64 = 493
30const FG_MODE644: i64 = 420
31const FG_COL_TOOL: i64 = 1
32const FG_I64: i64 = 8
33
34func fg_write(path: *u8, text: *u8) -> i64 {
35 let fd: i64 = sys_openat_wr(path, FG_MODE644)
36 if fd < 0 { return 0 - 1 }
37 let n: i64 = sys_write(fd, text, cr_slen(text))
38 sys_close(fd)
39 return n
40}
41// 1 when the file's bytes equal the literal exactly
42func fg_file_is(path: *u8, text: *u8) -> i64 {
43 let fl: *i64 = sys_mmap(16) as *i64
44 fl[0] = 0
45 let buf: *u8 = sys_read_file(path, fl)
46 if (buf as i64) == 0 { return 0 }
47 return cr_eq(buf, 0, fl[0], text, cr_slen(text))
48}
49func fg_exists(path: *u8) -> i64 {
50 let fl: *i64 = sys_mmap(16) as *i64
51 fl[0] = 0
52 let buf: *u8 = sys_read_file(path, fl)
53 if (buf as i64) == 0 { return 0 }
54 return 1
55}
56// 1 when row k (0-based) of the compiled rows names the tool in its tool column
57func fg_row_tool(rows: *u8, n: i64, k: i64, tool: *u8) -> i64 {
58 let span: *i64 = sys_mmap(2 * FG_I64) as *i64
59 var ls: i64 = 0
60 var row: i64 = 0
61 while ls < n {
62 let le: i64 = cr_eol(rows, n, ls)
63 if row == k {
64 if cr_col(rows, ls, le, MF_TAB, FG_COL_TOOL, span) == 0 { return 0 }
65 return cr_is(rows, span[0], span[1], tool)
66 }
67 row = row + 1
68 ls = le + 1
69 }
70 return 0
71}
72func fg_count_nl(buf: *u8, n: i64) -> i64 {
73 var c: i64 = 0
74 var i: i64 = 0
75 while i < n { if (buf[i] as i64) == MF_NL { c = c + 1 } i = i + 1 }
76 return c
77}
78
79func main(argc: i64, argv: *i64) -> i64 {
80 gv_head("nx_mediaflow_gate -- the one-call media workflow: the input boundary, the oracle-row picker, the plan compiler" as *u8)
81 let ctr: *i64 = gv_ctr()
82 sys_mkdir(FG_DIR, FG_MODE755)
83 // the slug boundary
84 gv_check_eq("slug-plain-accepted" as *u8, mf_slug_ok("diora14" as *u8), 1, ctr)
85 gv_check_eq("slug-with-dash-and-underscore-accepted" as *u8, mf_slug_ok("a-b_c9" as *u8), 1, ctr)
86 gv_check_eq("neg-control-empty-slug-refused" as *u8, mf_slug_ok("" as *u8), 0, ctr)
87 gv_check_eq("neg-control-slug-starting-with-dash-refused" as *u8, mf_slug_ok("-a" as *u8), 0, ctr)
88 gv_check_eq("neg-control-upper-case-slug-refused" as *u8, mf_slug_ok("Diora" as *u8), 0, ctr)
89 gv_check_eq("neg-control-slug-with-slash-refused" as *u8, mf_slug_ok("a/b" as *u8), 0, ctr)
90 gv_check_eq("neg-control-slug-with-dot-dot-refused" as *u8, mf_slug_ok(".." as *u8), 0, ctr)
91 gv_check_eq("neg-control-slug-with-space-refused" as *u8, mf_slug_ok("a b" as *u8), 0, ctr)
92 gv_check_eq("neg-control-slug-with-tab-refused" as *u8, mf_slug_ok("a\tb" as *u8), 0, ctr)
93 gv_check_eq("neg-control-slug-with-newline-refused" as *u8, mf_slug_ok("a\nb" as *u8), 0, ctr)
94 gv_check_eq("neg-control-slug-one-byte-over-the-limit-refused" as *u8, mf_slug_ok(FG_SLUG_65), 0, ctr)
95 // the path boundary
96 gv_check_eq("path-under-knowledge-accepted" as *u8, mf_path_ok("knowledge/fetched/x.jpg" as *u8), 1, ctr)
97 gv_check_eq("path-under-tmp-accepted" as *u8, mf_path_ok("/tmp/x.png" as *u8), 1, ctr)
98 gv_check_eq("path-under-the-vault-accepted" as *u8, mf_path_ok("/volume1/vault/real/image/23/x.jpg" as *u8), 1, ctr)
99 gv_check_eq("neg-control-path-outside-the-roots-refused" as *u8, mf_path_ok("/etc/passwd" as *u8), 0, ctr)
100 gv_check_eq("neg-control-site-docroot-is-not-a-media-root" as *u8, mf_path_ok("sites/nishifamily/x.jpg" as *u8), 0, ctr)
101 gv_check_eq("neg-control-dot-dot-escape-refused" as *u8, mf_path_ok("knowledge/../etc/passwd" as *u8), 0, ctr)
102 gv_check_eq("neg-control-path-with-space-refused" as *u8, mf_path_ok("knowledge/a b.jpg" as *u8), 0, ctr)
103 gv_check_eq("neg-control-path-with-tab-is-an-injected-column-refused" as *u8, mf_path_ok("knowledge/a\tb.jpg" as *u8), 0, ctr)
104 gv_check_eq("neg-control-path-with-newline-is-an-injected-step-refused" as *u8, mf_path_ok("knowledge/a\n9\tnx_fs_write" as *u8), 0, ctr)
105 gv_check_eq("neg-control-empty-path-refused" as *u8, mf_path_ok("" as *u8), 0, ctr)
106 gv_check_eq("kind-real-accepted" as *u8, mf_kind_ok("real" as *u8), 1, ctr)
107 gv_check_eq("kind-gen-accepted" as *u8, mf_kind_ok("gen" as *u8), 1, ctr)
108 gv_check_eq("neg-control-unknown-kind-refused" as *u8, mf_kind_ok("fake" as *u8), 0, ctr)
109 // names
110 gv_check_eq("basename-is-the-bytes-after-the-last-slash" as *u8, mf_streq(mf_basename("knowledge/fetched/ri_x.jpg" as *u8), "ri_x.jpg" as *u8), 1, ctr)
111 gv_check_eq("extension-is-read-from-the-file-name" as *u8, mf_streq(mf_ext("knowledge/fetched/ri_x.jpg" as *u8), "jpg" as *u8), 1, ctr)
112 gv_check_eq("neg-control-a-dot-in-a-directory-is-not-an-extension" as *u8, mf_streq(mf_ext("knowledge/dir.v2/file" as *u8), MF_EXT_NONE), 1, ctr)
113 gv_check_eq("neg-control-a-trailing-dot-is-not-an-extension" as *u8, mf_streq(mf_ext("knowledge/file." as *u8), MF_EXT_NONE), 1, ctr)
114 let pbuf: *u8 = sys_mmap(MF_PATH_CAP)
115 mf_plan_id("s1" as *u8, pbuf)
116 gv_check_eq("plan-id-is-mediaflow-dash-slug" as *u8, mf_streq(pbuf, "mediaflow-s1" as *u8), 1, ctr)
117 mf_plan_prefix("s1" as *u8, pbuf)
118 gv_check_eq("plan-prefix-is-the-full-plane-path" as *u8, mf_streq(pbuf, "knowledge/store/plan-mediaflow-s1-" as *u8), 1, ctr)
119 mf_url("s1" as *u8, pbuf)
120 gv_check_eq("url-is-the-live-report-address" as *u8, mf_streq(pbuf, "https://nishifamily.com/compare/aesthetictwin/media/s1/" as *u8), 1, ctr)
121 // the directory maker: a leaf three levels down takes a write
122 mf_mkdirp(FG_DEEP)
123 gv_check("mkdirp-leaf-takes-a-write" as *u8, (fg_write(FG_DEEP_FILE, "probe\n" as *u8) > 0) as i64, ctr)
124 // the oracle-row picker
125 gv_need("fixture-many-row-oracle-written" as *u8, (fg_write(FG_ORACLE, FG_ORACLE_TEXT) > 0) as i64, ctr)
126 gv_check_eq("picker-finds-the-medias-own-row" as *u8, mf_oracle_pick(FG_ORACLE, FG_MEDIA_B, FG_PICKED), 1, ctr)
127 gv_check_eq("picked-file-is-one-row-with-the-path-rewritten-and-the-cr-dropped" as *u8, fg_file_is(FG_PICKED, FG_PICKED_EXPECT), 1, ctr)
128 gv_check_eq("neg-control-no-row-for-this-media" as *u8, mf_oracle_pick(FG_ORACLE, FG_MEDIA_NONE, FG_NOPICK), 0, ctr)
129 gv_check_eq("neg-control-no-row-writes-no-file" as *u8, fg_exists(FG_NOPICK), 0, ctr)
130 gv_check_eq("neg-control-absent-oracle-is-unreadable" as *u8, mf_oracle_pick(FG_ABSENT, FG_MEDIA_B, FG_NOPICK), MF_E_UNREADABLE, ctr)
131 // the compiler
132 let rows: *u8 = sys_mmap(MF_PLAN_CAP)
133 let rn: i64 = mf_plan_rows(FG_KAT_SLUG, FG_KAT_MEDIA, FG_KAT_OUTDIR, MF_NO_ORACLE, MF_KIND_REAL, rows)
134 gv_check_eq("compiled-rows-length-is-the-known-answer" as *u8, rn, cr_slen(FG_KAT_ROWS), ctr)
135 gv_check_eq("compiled-rows-are-the-known-answer-byte-for-byte" as *u8, cr_eq(rows, 0, rn, FG_KAT_ROWS, cr_slen(FG_KAT_ROWS)), 1, ctr)
136 gv_check_eq("compiled-rows-are-one-per-step" as *u8, fg_count_nl(rows, rn), MF_PLAN_STEPS, ctr)
137 gv_check_eq("step-1-is-the-vault" as *u8, fg_row_tool(rows, rn, 0, "nx_mvault" as *u8), 1, ctr)
138 gv_check_eq("step-2-is-the-judge" as *u8, fg_row_tool(rows, rn, 1, "nx_mediajudge" as *u8), 1, ctr)
139 gv_check_eq("step-3-is-the-plane-seed" as *u8, fg_row_tool(rows, rn, 2, "nx_store_seed" as *u8), 1, ctr)
140 gv_check_eq("step-4-is-the-page" as *u8, fg_row_tool(rows, rn, 3, "nx_asset_page" as *u8), 1, ctr)
141 gv_check_eq("step-5-is-the-live-page-verifier" as *u8, fg_row_tool(rows, rn, 4, "nx_page_verify" as *u8), 1, ctr)
142 gv_check_eq("neg-control-there-is-no-sixth-step" as *u8, fg_row_tool(rows, rn, MF_PLAN_STEPS, "nx_page_verify" as *u8), 0, ctr)
143 gv_values_head()
144 gv_kv("compiled_bytes" as *u8, rn); gv_kv("compiled_rows" as *u8, fg_count_nl(rows, rn)); gv_kv("plan_steps" as *u8, MF_PLAN_STEPS)
145 return gv_verdict("nx_mediaflow_gate" as *u8, ctr, "the media workflow's boundary refuses what would become an injected column, step or directory; the picker hands the judge one openable oracle row; the compiler's rows are the five workflow steps byte for byte" as *u8)
146}