code wiki / _hdl_build / _ale_agent_gate.nx

_ale_agent_gate.nx source

↩ module page · 376 lines · 16428 B

1// _ale_agent_gate.nx -- the ALE-R2d gate: sovereign agent-core SUBMIT/compose entrypoint. 2// Diff-lane / no-mocks: drives the REAL composed entrypoint nx_ale_agent.elf (which itself 3// fork/execves the REAL sovereign phase organs nx_ale_plan.elf -> nx_ale_exec.elf -> 4// nx_ale_verify.elf, all nx_cc_sovereign->nxasm_x86_main, NO gcc) on the existing fixture 5// knowledge/specs/ale_exec_examples/task_exec.txt, and asserts ALL GREEN: 6// (1) ONE-ENTRYPOINT -- nx_ale_agent.elf <task> <sandbox> <outdir> runs the full 7// PLAN->EXECUTE->SELF-VERIFY pipeline in ONE invocation and exits 0. 8// (2) REAL-ARTIFACT -- a real result.units lands under the sandbox, BYTE-IDENTICAL to the 9// artifact produced by the STANDALONE plan->exec path (no mocks). 10// (3) HONEST-SELF-SCORE -- outdir/self_score BYTE-EQUALS an INDEPENDENT nx_ale_grade run on the 11// same (reference, artifact) AND == 1000 (the agent does not lie). 12// (4) NO-LEAKAGE-ORDER -- planting a decoy reference.txt in the sandbox does NOT change the 13// artifact or the self_score (agent never reads the reference during 14// plan/exec; only SELF-VERIFY resolves it, from the spec, post-run). 15// (5) DETERMINISTIC -- two runs on the same (task, fresh sandbox, fresh outdir) yield 16// byte-identical {plan, artifact, self_score} triples. 17// (6) TAMPER/NEG-CONTROL -- a task spec MISSING a required field makes nx_ale_agent REFUSE 18// (non-zero exit, NO artifact, NO self_score) -> not vacuously green. 19// Evidence -> knowledge/status/ale_agent.log (ALEAGENTGATE row; the queue row's ||MARK= reads it). 20// Sovereign orchestration (fork/dup3/execve/wait4 + mkdirat). license_tier: ORIGINAL 21import "nx_syscalls.nx" 22 23func g_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 24func g_fp(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 25func g_fn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m }; let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48; k = 1 }; while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }; var i: i64 = 0; while i < k { bb[i] = t[k - 1 - i]; i = i + 1 }; sys_write(fd, bb, k); return 0 } 26 27// run nx_ale_agent.elf <task> <sandbox> <outdir>; return child exit code (silenced child IO). 28func g_run_agent(task: *u8, sandbox: *u8, outdir: *u8) -> i64 { 29 let pid: i64 = sys_fork() 30 if pid == 0 { 31 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 32 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 33 let argv: *i64 = sys_mmap(48) as *i64 34 argv[0] = "_offc/nx_ale_agent.elf" as *u8 as i64 35 argv[1] = task as i64 36 argv[2] = sandbox as i64 37 argv[3] = outdir as i64 38 argv[4] = 0 39 let envp: *i64 = sys_mmap(16) as *i64 40 envp[0] = 0 41 sys_execve("_offc/nx_ale_agent.elf" as *u8, argv, envp) 42 sys_exit(127) 43 } 44 let st: *i64 = sys_mmap(16) as *i64 45 sys_wait4(pid, st, 0) 46 return (st[0] >> 8) & 0xff 47} 48 49// run nx_ale_plan.elf <task> <planout>; return child exit code (standalone-path reference). 50func g_run_plan(task: *u8, planout: *u8) -> i64 { 51 let pid: i64 = sys_fork() 52 if pid == 0 { 53 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 54 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 55 let argv: *i64 = sys_mmap(32) as *i64 56 argv[0] = "_offc/nx_ale_plan.elf" as *u8 as i64 57 argv[1] = task as i64 58 argv[2] = planout as i64 59 argv[3] = 0 60 let envp: *i64 = sys_mmap(16) as *i64 61 envp[0] = 0 62 sys_execve("_offc/nx_ale_plan.elf" as *u8, argv, envp) 63 sys_exit(127) 64 } 65 let st: *i64 = sys_mmap(16) as *i64 66 sys_wait4(pid, st, 0) 67 return (st[0] >> 8) & 0xff 68} 69 70// run nx_ale_exec.elf <plan> <task> <sandbox>; return child exit code (standalone-path reference). 71func g_run_exec(plan: *u8, task: *u8, sandbox: *u8) -> i64 { 72 let pid: i64 = sys_fork() 73 if pid == 0 { 74 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 75 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 76 let argv: *i64 = sys_mmap(48) as *i64 77 argv[0] = "_offc/nx_ale_exec.elf" as *u8 as i64 78 argv[1] = plan as i64 79 argv[2] = task as i64 80 argv[3] = sandbox as i64 81 argv[4] = 0 82 let envp: *i64 = sys_mmap(16) as *i64 83 envp[0] = 0 84 sys_execve("_offc/nx_ale_exec.elf" as *u8, argv, envp) 85 sys_exit(127) 86 } 87 let st: *i64 = sys_mmap(16) as *i64 88 sys_wait4(pid, st, 0) 89 return (st[0] >> 8) & 0xff 90} 91 92// run nx_ale_grade.elf <ref> <artifact> <scoreout>; return child exit code (independent grade). 93func g_run_grade(ref: *u8, art: *u8, scoreout: *u8) -> i64 { 94 let pid: i64 = sys_fork() 95 if pid == 0 { 96 let dn: i64 = sys_openat_wr("/dev/null" as *u8, 0x1a4) 97 if dn >= 0 { sys_dup3(dn, 1, 0); sys_dup3(dn, 2, 0) } 98 let argv: *i64 = sys_mmap(48) as *i64 99 argv[0] = "_offc/nx_ale_grade.elf" as *u8 as i64 100 argv[1] = ref as i64 101 argv[2] = art as i64 102 argv[3] = scoreout as i64 103 argv[4] = 0 104 let envp: *i64 = sys_mmap(16) as *i64 105 envp[0] = 0 106 sys_execve("_offc/nx_ale_grade.elf" as *u8, argv, envp) 107 sys_exit(127) 108 } 109 let st: *i64 = sys_mmap(16) as *i64 110 sys_wait4(pid, st, 0) 111 return (st[0] >> 8) & 0xff 112} 113 114// read whole file into buf (cap), return byte count (0 on open-fail / empty) 115func g_read(path: *u8, buf: *u8, cap: i64) -> i64 { 116 let fd: i64 = sys_openat_rd(path) 117 if fd < 0 { return 0 } 118 var n: i64 = 0 119 var go: i64 = 1 120 while go == 1 { 121 let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n) 122 if r <= 0 { go = 0 } else { n = n + r } 123 if n >= cap - 1 { go = 0 } 124 } 125 sys_close(fd) 126 return n 127} 128 129func g_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 130 131// does path EXIST as a readable file? 1/0 (used by the negative control: absent => refuse-proven) 132func g_exists(path: *u8) -> i64 { 133 let fd: i64 = sys_openat_rd(path) 134 if fd < 0 { return 0 } 135 sys_close(fd) 136 return 1 137} 138 139// two files byte-identical? 1/0 (both must be non-empty) 140func g_files_eq(pa: *u8, pb: *u8) -> i64 { 141 let ba: *u8 = sys_mmap(262144) 142 let bb: *u8 = sys_mmap(262144) 143 let na: i64 = g_read(pa, ba, 262144) 144 let nb: i64 = g_read(pb, bb, 262144) 145 if na != nb { return 0 } 146 if na == 0 { return 0 } 147 var i: i64 = 0 148 while i < na { if ba[i] != bb[i] { return 0 } i = i + 1 } 149 return 1 150} 151 152// parse leading non-negative decimal score from a file (0 if none/empty) 153func g_score_of(path: *u8) -> i64 { 154 let sb: *u8 = sys_mmap(64) 155 let n: i64 = g_read(path, sb, 64) 156 var v: i64 = 0 157 var i: i64 = 0 158 while i < n { 159 if sb[i] >= (48 as u8) { if sb[i] <= (57 as u8) { v = v * 10 + (sb[i] - 48) } } 160 i = i + 1 161 } 162 return v 163} 164 165// write n bytes from buf to a fresh file at path. 1/0. 166func g_write_file(path: *u8, buf: *u8, n: i64) -> i64 { 167 let fd: i64 = sys_openat_wr(path, 0x1a4) 168 if fd < 0 { return 0 } 169 if n > 0 { sys_write(fd, buf, n) } 170 sys_close(fd) 171 return 1 172} 173 174// copy a complete line of buf into out UNLESS it begins with skippref (drop that FIRST line). 175// Returns the produced byte count. (used to make the missing-field negative-control task.) 176func g_drop_line(buf: *u8, n: i64, skippref: *u8, out: *u8) -> i64 { 177 let sp: i64 = g_len(skippref) 178 var o: i64 = 0 179 var i: i64 = 0 180 var dropped: i64 = 0 181 while i < n { 182 var atbol: i64 = 0 183 if i == 0 { atbol = 1 } else { if buf[i - 1] == (10 as u8) { atbol = 1 } } 184 var ismatch: i64 = 0 185 if atbol == 1 { 186 if dropped == 0 { 187 if i + sp <= n { 188 var k: i64 = 0 189 var eq: i64 = 1 190 while k < sp { if buf[i + k] != skippref[k] { eq = 0 } k = k + 1 } 191 if eq == 1 { ismatch = 1 } 192 } 193 } 194 } 195 if ismatch == 1 { 196 dropped = 1 197 var p: i64 = i 198 var go: i64 = 1 199 while go == 1 { 200 if p >= n { go = 0 } else { 201 if buf[p] == (10 as u8) { p = p + 1; go = 0 } else { p = p + 1 } 202 } 203 } 204 i = p 205 } else { 206 out[o] = buf[i]; o = o + 1; i = i + 1 207 } 208 } 209 return o 210} 211 212func main() -> i64 { 213 let task: *u8 = "knowledge/specs/ale_exec_examples/task_exec.txt" as *u8 214 let reference: *u8 = "knowledge/specs/ale_exec_examples/reference.txt" as *u8 215 216 // agent run #1 (primary) 217 let sb1: *u8 = "/tmp/_aleagent_g_sb1" as *u8 218 let out1: *u8 = "/tmp/_aleagent_g_out1" as *u8 219 let art1: *u8 = "/tmp/_aleagent_g_sb1/result.units" as *u8 220 let plan1: *u8 = "/tmp/_aleagent_g_out1/plan" as *u8 221 let score1: *u8 = "/tmp/_aleagent_g_out1/self_score" as *u8 222 // agent run #2 (determinism) 223 let sb2: *u8 = "/tmp/_aleagent_g_sb2" as *u8 224 let out2: *u8 = "/tmp/_aleagent_g_out2" as *u8 225 let art2: *u8 = "/tmp/_aleagent_g_sb2/result.units" as *u8 226 let plan2: *u8 = "/tmp/_aleagent_g_out2/plan" as *u8 227 let score2: *u8 = "/tmp/_aleagent_g_out2/self_score" as *u8 228 // agent run #decoy (no-leakage) 229 let sbdec: *u8 = "/tmp/_aleagent_g_sbdec" as *u8 230 let outdec: *u8 = "/tmp/_aleagent_g_outdec" as *u8 231 let artdec: *u8 = "/tmp/_aleagent_g_sbdec/result.units" as *u8 232 let scoredec: *u8 = "/tmp/_aleagent_g_outdec/self_score" as *u8 233 let decoyref: *u8 = "/tmp/_aleagent_g_sbdec/reference.txt" as *u8 234 // standalone plan->exec reference (byte-identity oracle for the artifact) 235 let sbref: *u8 = "/tmp/_aleagent_g_sbref" as *u8 236 let planref: *u8 = "/tmp/_aleagent_g_planref" as *u8 237 let artref: *u8 = "/tmp/_aleagent_g_sbref/result.units" as *u8 238 // independent grade (honest-self-score oracle) 239 let indscore: *u8 = "/tmp/_aleagent_g_indscore" as *u8 240 // negative control (missing required field) 241 let badtask: *u8 = "/tmp/_aleagent_g_badtask.txt" as *u8 242 let sbbad: *u8 = "/tmp/_aleagent_g_sbbad" as *u8 243 let outbad: *u8 = "/tmp/_aleagent_g_outbad" as *u8 244 let artbad: *u8 = "/tmp/_aleagent_g_sbbad/result.units" as *u8 245 let scorebad: *u8 = "/tmp/_aleagent_g_outbad/self_score" as *u8 246 247 g_p("=== ALE-agent gate (ALE-R2d: sovereign agent-core SUBMIT/compose entrypoint) ===\n" as *u8) 248 249 // fresh sandbox + outdir dirs (mkdir idempotent; the executor writes with O_TRUNC inside) 250 sys_mkdir(sb1, 0x1ed); sys_mkdir(out1, 0x1ed) 251 sys_mkdir(sb2, 0x1ed); sys_mkdir(out2, 0x1ed) 252 sys_mkdir(sbdec, 0x1ed); sys_mkdir(outdec, 0x1ed) 253 sys_mkdir(sbref, 0x1ed) 254 sys_mkdir(sbbad, 0x1ed); sys_mkdir(outbad, 0x1ed) 255 256 // (1) ONE-ENTRYPOINT: the WHOLE pipeline in one invocation, exit 0. 257 let rca1: i64 = g_run_agent(task, sb1, out1) 258 var c1: i64 = 0 259 if rca1 == 0 { c1 = 1 } 260 261 // Build the STANDALONE plan->exec reference artifact (no agent) for the byte-identity oracle. 262 let rcsp: i64 = g_run_plan(task, planref) 263 let rcse: i64 = g_run_exec(planref, task, sbref) 264 265 // (2) REAL-ARTIFACT: a real result.units under the sandbox, byte-identical to standalone exec. 266 var c2: i64 = 0 267 if g_exists(art1) == 1 { if g_files_eq(art1, artref) == 1 { c2 = 1 } } 268 269 // (3) HONEST-SELF-SCORE: agent's self_score BYTE-EQUALS an INDEPENDENT nx_ale_grade of the same 270 // (reference, artifact) AND == 1000. The independent grade uses the SAME artifact the agent made. 271 let rcig: i64 = g_run_grade(reference, art1, indscore) 272 let self1: i64 = g_score_of(score1) 273 let ind1: i64 = g_score_of(indscore) 274 var c3: i64 = 0 275 if rcig == 0 { if g_files_eq(score1, indscore) == 1 { if self1 == 1000 { c3 = 1 } } } 276 277 // (4) NO-LEAKAGE-ORDER: plant a DELIBERATELY-WRONG decoy reference.txt in the sandbox, run the 278 // agent there, and assert the artifact + self_score are byte-identical to the clean run. The 279 // agent resolves the grading reference ONLY from the task spec (post-completion), never the 280 // sandbox -> a decoy cannot perturb the deliverable or the score. 281 let decoytxt: *u8 = "unit|row_count|999\nunit|header|XXX\nunit|sorted_by|nope\nunit|checksum|dead\nunit|extra|leak\n" as *u8 282 g_write_file(decoyref, decoytxt, g_len(decoytxt)) 283 let rcad: i64 = g_run_agent(task, sbdec, outdec) 284 let selfdec: i64 = g_score_of(scoredec) 285 var c4: i64 = 0 286 if rcad == 0 { 287 if g_files_eq(artdec, art1) == 1 { 288 if g_files_eq(scoredec, score1) == 1 { 289 if selfdec == self1 { c4 = 1 } 290 } 291 } 292 } 293 294 // (5) DETERMINISTIC: a 2nd agent run on the same (task, FRESH sandbox, FRESH outdir) yields a 295 // byte-identical {plan, artifact, self_score} TRIPLE. 296 let rca2: i64 = g_run_agent(task, sb2, out2) 297 var c5: i64 = 0 298 if rca2 == 0 { 299 if g_files_eq(plan1, plan2) == 1 { 300 if g_files_eq(art1, art2) == 1 { 301 if g_files_eq(score1, score2) == 1 { c5 = 1 } 302 } 303 } 304 } 305 306 // (6) TAMPER/NEGATIVE-CONTROL: a task spec MISSING a required field (artifact_path) -> the 307 // agent REFUSES: non-zero exit, NO artifact, NO self_score file. Proves not vacuously green. 308 let tbuf: *u8 = sys_mmap(262144) 309 let tn: i64 = g_read(task, tbuf, 262144) 310 let bbuf: *u8 = sys_mmap(262144) 311 let bn: i64 = g_drop_line(tbuf, tn, "task|artifact_path|" as *u8, bbuf) 312 g_write_file(badtask, bbuf, bn) 313 let rcab: i64 = g_run_agent(badtask, sbbad, outbad) 314 let art_absent: i64 = g_exists(artbad) 315 let score_absent: i64 = g_exists(scorebad) 316 var c6: i64 = 0 317 if rcab != 0 { if art_absent == 0 { if score_absent == 0 { c6 = 1 } } } 318 319 g_p(" rc_agent1=" as *u8); g_fn(1, rca1) 320 g_p(" rc_plan_ref=" as *u8); g_fn(1, rcsp) 321 g_p(" rc_exec_ref=" as *u8); g_fn(1, rcse) 322 g_p(" self_good=" as *u8); g_fn(1, self1) 323 g_p(" ind_good=" as *u8); g_fn(1, ind1) 324 g_p(" self_decoy=" as *u8); g_fn(1, selfdec) 325 g_p(" rc_agent_bad=" as *u8); g_fn(1, rcab) 326 g_p(" art_absent_bad=" as *u8); g_fn(1, art_absent) 327 g_p(" score_absent_bad=" as *u8); g_fn(1, score_absent) 328 g_p("\n" as *u8) 329 g_p(" c1_one_entrypoint=" as *u8); g_fn(1, c1) 330 g_p(" c2_real_artifact=" as *u8); g_fn(1, c2) 331 g_p(" c3_honest_self_score=" as *u8); g_fn(1, c3) 332 g_p(" c4_no_leakage_order=" as *u8); g_fn(1, c4) 333 g_p(" c5_deterministic=" as *u8); g_fn(1, c5) 334 g_p(" c6_tamper_refuse=" as *u8); g_fn(1, c6) 335 g_p("\n" as *u8) 336 337 var allok: i64 = 1 338 if c1 == 0 { allok = 0 } 339 if c2 == 0 { allok = 0 } 340 if c3 == 0 { allok = 0 } 341 if c4 == 0 { allok = 0 } 342 if c5 == 0 { allok = 0 } 343 if c6 == 0 { allok = 0 } 344 345 let lfd: i64 = sys_openat_append("knowledge/status/ale_agent.log" as *u8, 0x1a4) 346 if allok == 1 { 347 g_p("ALEAGENTGATE verdict=GREEN (one-entrypoint exit0; real-artifact byte==standalone-exec; honest-self-score==independent==1000; no-leakage decoy-ignored; deterministic triple; missing-field REFUSE no-artifact no-self-score)\n" as *u8) 348 if lfd >= 0 { 349 g_fp(lfd, "ALEAGENTGATE verdict=GREEN one_entrypoint=1 real_artifact=1 honest_self_score=1 no_leakage_order=1 deterministic=1 tamper_refuse=1 self_good=1000 ind_good=" as *u8) 350 g_fn(lfd, ind1) 351 g_fp(lfd, " self_decoy=" as *u8) 352 g_fn(lfd, selfdec) 353 g_fp(lfd, " rc_agent_bad=" as *u8) 354 g_fn(lfd, rcab) 355 g_fp(lfd, " rung=ALE-R2d epoch=" as *u8) 356 g_fn(lfd, sys_now_realtime_sec()) 357 g_fp(lfd, "\n" as *u8) 358 sys_close(lfd) 359 } 360 sys_exit(0) 361 return 0 362 } 363 g_p("ALEAGENTGATE verdict=RED (a check did not fire)\n" as *u8) 364 if lfd >= 0 { 365 g_fp(lfd, "ALEAGENTGATE verdict=RED c1=" as *u8); g_fn(lfd, c1) 366 g_fp(lfd, " c2=" as *u8); g_fn(lfd, c2) 367 g_fp(lfd, " c3=" as *u8); g_fn(lfd, c3) 368 g_fp(lfd, " c4=" as *u8); g_fn(lfd, c4) 369 g_fp(lfd, " c5=" as *u8); g_fn(lfd, c5) 370 g_fp(lfd, " c6=" as *u8); g_fn(lfd, c6) 371 g_fp(lfd, "\n" as *u8) 372 sys_close(lfd) 373 } 374 sys_exit(1) 375 return 1 376}