nx_content_put.nx
buildroot/runtime/nx_content_put.nx
about
nx_content_put.nx -- CHUNKED CONTENT UPLOAD RECEIVER: the sovereign door for laptop->NAS bytes.
WHY THIS EXISTS: two same-day break-glass scp transfers (94 MB of donor FBX, 389 MB of authored
textures) because the estate had NO upload organ and the MCP tools/call body is capped -- measured
twice independently 2026-08-23: the edge answers a clean 413 for any body >= 64 KiB, and
tsv_serve_one performs ONE sys_read(65536). This organ turns that cap into a chunk protocol:
the client splits, every chunk is sha-receipted (CAS), commit reassembles ATOMICALLY and verifies
the WHOLE-FILE sha against the one declared at begin. scp for content transfer is retired.
nx_content_put begin <dest> <total_bytes> <sha256hex> -> CP-BEGIN id=... chunk_raw=...
nx_content_put chunk <id> <index> <base64> -> CP-CHUNK OK ... chunk_sha256=...
nx_content_put commit <id> -> CP-COMMIT OK dest=... sha256=...
nx_content_put status <id> -> received/missing map (resume source)
nx_content_put abort <id> -> staging removed
DESIGN LAWS APPLIED:
- chunk size is DERIVED, one owner (this organ), returned in the begin receipt; the client reads
it from there. Components: the wire body cap (65536, measured), the JSON-RPC skeleton length
MEASURED FROM THE LITERAL AT RUNTIME (a hand-counted length beside a literal is the defect that
put 112 beside a 120-byte struct the same day this was written), the largest capability token
observed in the estate's cap store (831 B, measured 2026-08-23), and the i64 digit bound (19,
from the type). The reserve is a THROUGHPUT choice, never a correctness bound: an oversized
call fails LOUD at the edge (413) and per-chunk sha CAS means corruption cannot land silently.
- destination allowlist BY NAME: world/ knowledge/rigcorpus/ knowledge/fetched/ knowledge/library/
plus /tmp/ (the gate-fixture lane, the same safe-root set nx_mkdirp enforces). No '..' ever.
- assembly is staged BESIDE the destination (<dest>.cptmp) then renamed: rename is atomic only
within a device; staging->dest across volumes would EXDEV-fail exactly when it mattered.
- chunks are idempotent: an identical re-send is a receipted no-op; a DIFFERENT body for an index
that already landed is REFUSED (CAS) -- a blind retry can never double-apply or corrupt.
- stale staging is reaped at begin: age bound = nchunks * CP_WORST_CALL_MS * CP_RESUME_ALLOWANCE,
i.e. the time the transfer would take at the estate's measured WORST per-call latency, times a
declared crash-resume policy allowance. Both factors named below.
- partition law: commit prints received/declared and bytes/total, and they must SUM.
exit: 0 ok | 2 io | 3 usage | 4 refused-dest | 5 refused-cas | 6 missing-chunks | 7 sha-mismatch
license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
dependencies 4 imports · 0 importers
imports: nx_syscalls.nxnx_base64.nxnx_sha256.nxnx_cdc_lib.nx
imported by: nobody (leaf or entry point)
call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown
structs
| none |
consts
| 43 | const CP_WIRE_BODY_CAP: i64 = 65536 |
| 44 | const CP_CAP_OBSERVED_MAX: i64 = 831 |
| 45 | const CP_I64_DIGITS: i64 = 19 |
| 47 | const CP_SKEL: *u8 = "{\x22jsonrpc\x22:\x222.0\x22,\x22id\x22:1,\x22method\x22:\x22tools/call\x22,\x22params\x22:{\x22name\x22:\x22nx_content_put\x22,\x22arguments\x22:{\x22argv\x22:[\x22chunk\x22,\x22\x22,\x22\x22,\x22\x22],\x22_cap\x22:\x22\x22}}}" as *u8 |
| 50 | const CP_WORST_CALL_MS: i64 = 7540 |
| 53 | const CP_RESUME_ALLOWANCE: i64 = 4 |
| 55 | const CP_DIR: *u8 = "knowledge/contentput" as *u8 |
| 56 | const CP_INDEX: *u8 = "knowledge/contentput/index.txt" as *u8 |
| 57 | const CP_INDEX_TMP: *u8 = "knowledge/contentput/index.tmp" as *u8 |
| 58 | const CP_PATHCAP: i64 = 1024 |
| 59 | const CP_SHAHEX: i64 = 64 |
| 60 | const CP_DIGEST: i64 = 32 |
| 62 | const CP_EXIT_IO: i64 = 2 |
| 63 | const CP_EXIT_USAGE: i64 = 3 |
| 64 | const CP_EXIT_DEST: i64 = 4 |
| 65 | const CP_EXIT_CAS: i64 = 5 |
| 66 | const CP_EXIT_MISSING: i64 = 6 |
| 67 | const CP_EXIT_SHA: i64 = 7 |
| 358 | const CP_CDC_AVG_DIV: i64 = 2 |
| 359 | const CP_CDC_MIN_DIV: i64 = 8 |
| 656 | const CP_FICLONERANGE: i64 = 1075876877 // _IOW(0x94, 13, struct file_clone_range{s64 src_fd; u64 src_off; u64 src_len; u64 dst_off}) = (1<<30)|(32<<16)|(0x94<<8)|13 = 0x4020940D, derived from the ioctl encoding, not picked |
| 657 | const CP_CLONE_RANGE_WORDS: i64 = 4 |
| 665 | const CP_STAT_BLKSIZE_OFF: i64 = 56 // x86-64 struct stat: st_size at 48, st_blksize at 56, st_blocks at 64 (nx_artifactdrift reads st_mode from the same layout at 24) |
| 666 | const CP_STAT_CAP: i64 = 256 |
| 667 | const CP_SEEK_SET: i64 = 0 |
functions
| 69 | func cp_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } |
| 70 | func cp_out(s: *u8) -> i64 { sys_write(1, s, cp_slen(s)); return 0 } |
| 71 | func cp_eo(s: *u8) -> i64 { sys_write(2, s, cp_slen(s)); return 0 } |
| 72 | func cp_itoa(v: i64, out: *u8) -> i64 |
| 84 | func cp_num(v: i64) -> i64 { let b: *u8 = sys_mmap(32); let n: i64 = cp_itoa(v, b); sys_write(1, b, n); return 0 } |
| 85 | func cp_streq(a: *u8, b: *u8) -> i64 |
| 91 | func cp_starts(p: *u8, pre: *u8) -> i64 |
| 96 | func cp_dotdot(p: *u8) -> i64 |
| 101 | func cp_append(dst: *u8, pos: i64, src: *u8) -> i64 |
| 107 | func cp_append_num(dst: *u8, pos: i64, v: i64) -> i64 called by 11: cp_path_metacp_path_shascp_path_chunkcp_path_cdccp_path_rlcp_path_tchunk+5 calls 1: cp_itoa |
| 113 | func cp_parse(s: *u8, pos: *i64) -> i64 |
| 120 | func cp_hex_into(d: *u8, out: *u8) -> i64 called by 1: cp_sha_hex_of |
| 133 | func cp_sha_hex_of(buf: *u8, n: i64, outhex: *u8) -> i64 |
| 140 | func cp_mkdir(path: *u8) -> i64 |
| 147 | func cp_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 } |
| 165 | func cp_stage_dest(p: *u8) -> i64 |
| 178 | func cp_is_source_dest(p: *u8) -> i64 |
| 184 | func cp_current_sha(path: *u8, outhex: *u8) -> i64 |
| 193 | func cp_expect_value(arg: *u8, out: *u8) -> i64 |
| 207 | func cp_expect_wellformed(v: *u8, n: i64) -> i64 |
| 228 | func cp_chunk_expect(arg: *u8, arrived_hex: *u8) -> i64 |
| 239 | func cp_read_meta_expect(id: i64, out: *u8) -> i64 |
| 256 | func cp_dest_ok(p: *u8) -> i64 |
| 267 | func cp_path_meta(out: *u8, id: i64) -> i64 { var p: i64 = cp_append(out, 0, CP_DIR); p = cp_append(out, p, "/" as *u8); p = cp_append_num(out, p, id); p = cp_append(out, p, ".meta" as *u8); return p } called by 4: cp_read_meta_expectcp_read_metacp_unlink_transferv_begin calls 2: cp_appendcp_append_num |
| 268 | func cp_path_shas(out: *u8, id: i64) -> i64 { var p: i64 = cp_append(out, 0, CP_DIR); p = cp_append(out, p, "/" as *u8); p = cp_append_num(out, p, id); p = cp_append(out, p, ".shas" as *u8); return p } |
| 269 | func cp_path_chunk(out: *u8, id: i64, idx: i64) -> i64 { var p: i64 = cp_append(out, 0, CP_DIR); p = cp_append(out, p, "/" as *u8); p = cp_append_num(out, p, id); p = cp_append(out, p, ".c" as *u8); p = cp_append_num(out, p, idx); return p } called by 8: cp_unlink_transferv_chunkcp_resume_offsetv_commitv_statuscp_append_at+2 calls 2: cp_appendcp_append_num |
| 273 | func cp_path_cdc(out: *u8, id: i64) -> i64 { var p: i64 = cp_append(out, 0, CP_DIR); p = cp_append(out, p, "/" as *u8); p = cp_append_num(out, p, id); p = cp_append(out, p, ".cdc" as *u8); return p } |
| 276 | func cp_path_rl(out: *u8, id: i64) -> i64 { var p: i64 = cp_append(out, 0, CP_DIR); p = cp_append(out, p, "/" as *u8); p = cp_append_num(out, p, id); p = cp_append(out, p, ".rl" as *u8); return p } |
| 278 | func cp_cdc_count(id: i64) -> i64 |
| 292 | func cp_file_size(path: *u8) -> i64 |
| 299 | func cp_path_tchunk(out: *u8, id: i64, idx: i64) -> i64 { var p: i64 = cp_append(out, 0, CP_DIR); p = cp_append(out, p, "/" as *u8); p = cp_append_num(out, p, id); p = cp_append(out, p, ".t" as *u8); p = cp_append_num(out, p, idx); return p } |
| 309 | func cp_write_atomic_x(tmppath: *u8, finalpath: *u8, buf: *u8, n: i64, sync: i64) -> i64 |
| 323 | func cp_write_atomic(tmppath: *u8, finalpath: *u8, buf: *u8, n: i64) -> i64 { return cp_write_atomic_x(tmppath, finalpath, buf, n, 1) } |
| 325 | func cp_dir_sync() -> i64 |
| 332 | func cp_append_line(path: *u8, line: *u8) -> i64 |
| 342 | func cp_chunk_b64() -> i64 |
| 348 | func cp_chunk_raw() -> i64 { return cp_chunk_b64() / 4 * 3 } |
| 360 | func cp_cdc_gear(buf: *u8, n: i64, offs: *i64, cap: i64) -> i64 |
| 369 | func cp_read_meta(id: i64, dest: *u8, nums: *i64, sha: *u8) -> i64 |
| 405 | func cp_unlink_transfer(id: i64, nchunks: i64) -> i64 called by 3: cp_reapv_commitv_abort calls 8: sys_mmapcp_path_chunksys_unlinkatcp_path_tchunkcp_path_shascp_path_meta+2 |
| 427 | func cp_reap(now_s: i64) -> i64 |
| 485 | func v_begin(dest: *u8, total_s: *u8, sha: *u8, expect_arg: *u8) -> i64 called by 1: main calls 22: sys_mmapcp_expect_valuecp_expect_wellformedcp_eocp_is_source_destcp_dest_ok+16 |
| 584 | func cp_shas_append(id: i64, idx: i64, hex: *u8) -> i64 called by 2: cp_stage_write_xcp_stage_clone calls 5: sys_mmapcp_path_shascp_append_numcp_appendcp_append_line |
| 598 | func cp_rl_append(id: i64, k: i64, off: i64, len: i64) -> i64 |
| 613 | func cp_rl_load(id: i64, nch: i64, offs: *i64, lens: *i64) -> i64 |
| 640 | func cp_stage_write_x(id: i64, idx: i64, cpath: *u8, raw: *u8, dn: i64, hex: *u8, sync: i64) -> i64 |
| 647 | func cp_stage_write(id: i64, idx: i64, cpath: *u8, raw: *u8, dn: i64, hex: *u8) -> i64 { return cp_stage_write_x(id, idx, cpath, raw, dn, hex, 1) } calls 1: cp_stage_write_x |
| 669 | func cp_clone_window(rs: i64, re: i64, blk: i64, out: *i64) -> i64 called by 1: v_commit |
| 680 | func cp_blksize_of(path: *u8) -> i64 |
| 689 | func cp_copy_range(src_fd: i64, dst_fd: i64, off: i64, len: i64, buf: *u8, cap: i64) -> i64 |
| 708 | func cp_hex_eq(a: *u8, b: *u8) -> i64 { var t: i64 = 0; while t < CP_SHAHEX { if a[t] != b[t] { return 0 } t = t + 1 } return 1 } called by 1: cp_stage_clone |
| 709 | func cp_stage_clone(id: i64, idx: i64, cpath: *u8, inc_fd: i64, inc: *u8, off: i64, len: i64, hex: *u8, why: *i64) -> i64 |
| 754 | func v_chunk(id_s: *u8, idx_s: *u8, b64: *u8, decl: *u8) -> i64 |
| 848 | func cp_sidecar_sha(id: i64, idx: i64, out: *u8) -> i64 called by 2: cp_stream_verifyv_commit calls 5: sys_mmapcp_path_shassys_read_filecp_parsesys_free_file |
| 879 | func cp_resume_offset(id: i64, nch: i64, craw: i64, total: i64) -> i64 |
| 895 | func cp_stream_verify(id: i64, idx: i64, buf: *u8, n: i64) -> i64 |
| 903 | func v_commit(id_s: *u8) -> i64 |
| 1116 | func v_status(id_s: *u8) -> i64 |
| 1158 | func v_abort(id_s: *u8) -> i64 |
| 1174 | func v_reap() -> i64 |
| 1186 | func cp_src_ok(p: *u8) -> i64 |
| 1201 | func cp_append_at(id: i64, off: i64, raw: *u8, dn: i64, decl: *u8) -> i64 |
| 1257 | func v_append(id_s: *u8, off_s: *u8, b64: *u8, decl: *u8) -> i64 |
| 1275 | func v_plan(id_s: *u8, count_s: *u8) -> i64 |
| 1310 | func v_have(id_s: *u8, from_arg: *u8) -> i64 |
| 1361 | func v_reuse(id_s: *u8, b64: *u8) -> i64 |
| 1484 | func v_cdcplan(src: *u8) -> i64 |
| 1523 | func main(argc: i64, argv: *i64) -> i64 |