code wiki / (root) / nx_mediaflow.nx

nx_mediaflow.nx source

↩ module page · 163 lines · 8783 B

1// nx_mediaflow.nx -- aesthetictwin AT36 (2026-09-17): SUBMIT MEDIA, GET A VERIFIED REPORT, ONE CALL. The repeatable work 2// of judging a media file was five hand steps; this organ is the one step. It validates the inputs at the boundary, 3// makes the report directory, picks the media's own row out of an oracle file when one is given, compiles the workflow 4// to plan rows (nx_mediaflow_lib), seeds them into the plan plane under the plane lock, runs the estate's own plan 5// executor over them, prints every step's result and ends with ONE receipt line carrying the live URL and the verdict. 6// The 1-2-3: (1) the media is on the estate (put door, vault capture, a fetch), (2) nx_mediaflow submit, (3) open the URL. 7// nx_mediaflow submit <slug> <file> [oracle-file|-] [real|gen] -> MEDIAFLOW receipt (last line) 8// nx_mediaflow plan <slug> <file> [oracle-file|-] [real|gen] -> the compiled plan rows, nothing seeded or run 9// exit: 0 published and verified | 1 the plan failed (read the step rows) | 2 usage or a refused input | 10// 3 the plan plane refused the seed | 4 the plan executor could not be run 11// license_tier: ORIGINAL No hw writes (Rule 26). 12import "nx_syscalls.nx" 13import "nx_itoa_lib.nx" 14import "nx_store_seed_lib.nx" 15import "nx_deploy_lib.nx" 16import "nx_mediaflow_lib.nx" 17 18const MW_EXIT_PLAN_FAILED: i64 = 1 19const MW_EXIT_USAGE: i64 = 2 20const MW_EXIT_SEED: i64 = 3 21const MW_EXIT_RUNNER: i64 = 4 22const MW_ARGC_MIN: i64 = 4 23const MW_ARGC_ORACLE: i64 = 5 24const MW_ARGC_KIND: i64 = 6 25const MW_ARG_VERB: i64 = 1 26const MW_ARG_SLUG: i64 = 2 27const MW_ARG_FILE: i64 = 3 28const MW_ARG_ORACLE: i64 = 4 29const MW_ARG_KIND: i64 = 5 30const MW_RUNNER_ELF: *u8 = "./nx_plan_run.elf" 31const MW_CAPTURE_PRE: *u8 = "/tmp/nx_mediaflow_" 32const MW_DRY_ORACLE: *u8 = "/tmp/nx_mediaflow_plan.oracle" 33const MW_ORACLE_PICKED: i64 = 1 34const MW_ORACLE_NONE: i64 = 0 35const MW_ORACLE_NOT_GIVEN: i64 = 2 36const MW_I64: i64 = 8 37const MW_RC_NOEXEC: i64 = 127 // the child could not exec the plan executor (dep_run_capture's own convention) 38 39func mw_puts(s: *u8) -> i64 { sys_write(1, s, cr_slen(s)); return 0 } 40func mw_usage() -> i64 { 41 mw_puts("usage: nx_mediaflow submit <slug> <file> [oracle-file|-] [real|gen] | plan <slug> <file> [oracle-file|-] [real|gen]\n" as *u8) 42 return MW_EXIT_USAGE 43} 44func mw_refuse(rule: *u8, value: *u8, why: *u8) -> i64 { 45 mw_puts("MEDIAFLOW REFUSED rule=" as *u8); mw_puts(rule); mw_puts(" value=" as *u8); mw_puts(value) 46 mw_puts(" -- " as *u8); mw_puts(why); mw_puts("\n" as *u8) 47 return MW_EXIT_USAGE 48} 49// 1 when needle occurs in buf[0..n) 50func mw_has(buf: *u8, n: i64, needle: *u8) -> i64 { 51 let m: i64 = cr_slen(needle) 52 if m < 1 { return 0 } 53 var i: i64 = 0 54 while i + m <= n { 55 if cr_eq(buf, i, i + m, needle, m) == 1 { return 1 } 56 i = i + 1 57 } 58 return 0 59} 60func mw_oracle_word(state: i64) -> *u8 { 61 if state == MW_ORACLE_PICKED { return "PICKED" as *u8 } 62 if state == MW_ORACLE_NONE { return "NONE-FOR-THIS-MEDIA" as *u8 } 63 if state == MW_ORACLE_NOT_GIVEN { return "NOT-GIVEN" as *u8 } 64 return "UNREADABLE" as *u8 65} 66 67func main(argc: i64, argv: **u8) -> i64 { 68 if argc < MW_ARGC_MIN { return mw_usage() } 69 let verb: *u8 = argv[MW_ARG_VERB] 70 var dry: i64 = 0 71 if mf_streq(verb, "plan" as *u8) == 1 { dry = 1 } else { if mf_streq(verb, "submit" as *u8) == 0 { return mw_usage() } } 72 let slug: *u8 = argv[MW_ARG_SLUG] 73 let media: *u8 = argv[MW_ARG_FILE] 74 var oracle_in: *u8 = MF_NO_ORACLE 75 if argc >= MW_ARGC_ORACLE { oracle_in = argv[MW_ARG_ORACLE] } 76 var kind: *u8 = MF_KIND_REAL 77 if argc >= MW_ARGC_KIND { kind = argv[MW_ARG_KIND] } 78 // the boundary: every input becomes a directory name, a store prefix or a plan column 79 if mf_slug_ok(slug) == 0 { return mw_refuse("slug" as *u8, slug, "lower-case letters, digits, dash and underscore, 1 to 64 bytes, starting with a letter or a digit" as *u8) } 80 if mf_path_ok(media) == 0 { return mw_refuse("media-path" as *u8, media, "printable bytes with no space and no dot-dot, under knowledge/ or /tmp/ or /volume1/vault/" as *u8) } 81 if mf_kind_ok(kind) == 0 { return mw_refuse("kind" as *u8, kind, "real or gen, the vault's two classes" as *u8) } 82 var have_oracle_arg: i64 = 0 83 if mf_streq(oracle_in, MF_NO_ORACLE) == 0 { 84 have_oracle_arg = 1 85 if mf_path_ok(oracle_in) == 0 { return mw_refuse("oracle-path" as *u8, oracle_in, "printable bytes with no space and no dot-dot, under knowledge/ or /tmp/ or /volume1/vault/" as *u8) } 86 } 87 let outdir: *u8 = sys_mmap(MF_PATH_CAP) 88 mf_outdir(slug, outdir) 89 if dry == 0 { mf_mkdirp(outdir) } 90 // the media's own oracle row, alone, with an openable path 91 let oracle_one: *u8 = sys_mmap(MF_PATH_CAP) 92 var o: i64 = 0 93 if dry == 1 { o = mf_cat(oracle_one, 0, MW_DRY_ORACLE) } else { 94 o = mf_cat(oracle_one, 0, outdir); o = mf_cat(oracle_one, o, "/" as *u8); o = mf_cat(oracle_one, o, slug); o = mf_cat(oracle_one, o, ".oracle" as *u8) 95 } 96 var ostate: i64 = MW_ORACLE_NOT_GIVEN 97 var oracle_col: *u8 = MF_NO_ORACLE 98 if have_oracle_arg == 1 { 99 ostate = mf_oracle_pick(oracle_in, media, oracle_one) 100 if ostate == MW_ORACLE_PICKED { 101 if dry == 1 { 102 // a dry run names the path a submit would write, not the scratch copy 103 let would: *u8 = sys_mmap(MF_PATH_CAP) 104 o = mf_cat(would, 0, outdir); o = mf_cat(would, o, "/" as *u8); o = mf_cat(would, o, slug); o = mf_cat(would, o, ".oracle" as *u8) 105 oracle_col = would 106 } else { oracle_col = oracle_one } 107 } 108 } 109 let rows: *u8 = sys_mmap(MF_PLAN_CAP) 110 let rn: i64 = mf_plan_rows(slug, media, outdir, oracle_col, kind, rows) 111 if rn < 0 { return mw_refuse("plan-size" as *u8, slug, "the compiled rows exceed the derived ceiling" as *u8) } 112 let planid: *u8 = sys_mmap(MF_PATH_CAP) 113 mf_plan_id(slug, planid) 114 let url: *u8 = sys_mmap(MF_PATH_CAP) 115 mf_url(slug, url) 116 if dry == 1 { 117 sys_write(1, rows, rn) 118 mw_puts("MEDIAFLOW-PLAN slug=" as *u8); mw_puts(slug); mw_puts(" plan=" as *u8); mw_puts(planid) 119 mw_puts(" oracle_row=" as *u8); mw_puts(mw_oracle_word(ostate)); mw_puts(" steps=" as *u8); nxi_out(MF_PLAN_STEPS) 120 mw_puts(" url=" as *u8); mw_puts(url); mw_puts(" verdict=COMPILED-NOT-RUN\n" as *u8) 121 return 0 122 } 123 // seed the plan plane under its lock: sts_seed replaces the whole plane, so a second submit of one slug waits 124 let prefix: *u8 = sys_mmap(MF_PATH_CAP) 125 mf_plan_prefix(slug, prefix) 126 let lk: i64 = sts_lock(prefix) 127 let seeded: i64 = sts_seed(prefix, rows, rn) 128 sts_unlock(lk) 129 if seeded != MF_PLAN_STEPS { 130 mw_puts("MEDIAFLOW slug=" as *u8); mw_puts(slug); mw_puts(" plan=" as *u8); mw_puts(planid) 131 mw_puts(" seeded_rows=" as *u8); nxi_out(seeded); mw_puts(" verdict=SEED-REFUSED\n" as *u8) 132 return MW_EXIT_SEED 133 } 134 // the estate's own executor runs the rows; its capture is printed whole so every step's result is on the receipt 135 let capture: *u8 = sys_mmap(MF_PATH_CAP) 136 o = mf_cat(capture, 0, MW_CAPTURE_PRE); o = mf_cat(capture, o, slug); o = mf_cat(capture, o, ".out" as *u8) 137 let av: *i64 = sys_mmap(2 * MW_I64) as *i64 138 av[0] = planid as i64 139 let rc: i64 = dep_run_capture(MW_RUNNER_ELF, av, 1, capture) 140 let fl: *i64 = sys_mmap(16) as *i64 141 fl[0] = 0 142 let cap: *u8 = sys_read_file(capture, fl) 143 var ok: i64 = 0 144 if (cap as i64) != 0 { 145 if fl[0] > 0 { 146 sys_write(1, cap, fl[0]) 147 let marker: *u8 = sys_mmap(MF_PATH_CAP) 148 // the executor's own success line, with the step count taken from the compiler's constant 149 o = mf_cat(marker, 0, "PLAN " as *u8); o = mf_cat(marker, o, planid); o = mf_cat(marker, o, " steps=" as *u8) 150 o = ss_catn(marker, o, MF_PLAN_STEPS); marker[o] = 0 as u8 151 o = mf_cat(marker, o, " ok" as *u8) 152 ok = mw_has(cap, fl[0], marker) 153 } 154 } 155 mw_puts("MEDIAFLOW slug=" as *u8); mw_puts(slug); mw_puts(" kind=" as *u8); mw_puts(kind); mw_puts(" media=" as *u8); mw_puts(media) 156 mw_puts(" oracle_row=" as *u8); mw_puts(mw_oracle_word(ostate)); mw_puts(" plan=" as *u8); mw_puts(planid) 157 mw_puts(" seeded_rows=" as *u8); nxi_out(seeded); mw_puts(" plan_rc=" as *u8); nxi_out(rc); mw_puts(" url=" as *u8); mw_puts(url) 158 if rc == MW_RC_NOEXEC { mw_puts(" verdict=PLAN-EXECUTOR-NOT-RUN\n" as *u8); return MW_EXIT_RUNNER } 159 if rc < 0 { mw_puts(" verdict=PLAN-EXECUTOR-NOT-RUN\n" as *u8); return MW_EXIT_RUNNER } 160 if ok == 1 { mw_puts(" verdict=PUBLISHED-VERIFIED\n" as *u8); return 0 } 161 mw_puts(" verdict=PLAN-FAILED\n" as *u8) 162 return MW_EXIT_PLAN_FAILED 163}