code wiki / (root) / nx_content_put.nx

nx_content_put.nx

buildroot/runtime/nx_content_put.nx

82931 B1593 linesdepth 6pulls 7 transitivereach 0 importersview sourcekind tooltopic content
docsdependenciesstructsconstsfunctions

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

nx_syscalls.nx nx_base64.nx nx_sha256.nx nx_cdc_lib.nx nx_content_put.nx

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

main cp_eo sys_write cp_slen sys_exit cp_streq v_begin sys_mmap nxa_die sys_write ↻ sys_exit ↻ nxa_lock_take nxa_lock_addr sys_write ↻ nxa_lock_give nxa_lock_addr ↻ nxa_report_overrun sys_write ↻ nxa_dump_printable sys_write ↻ nxa_dump_sizes sys_write ↻ cp_expect_value cp_starts cp_append cp_expect_wellformed cp_streq ↻ cp_eo ↻ cp_is_source_dest cp_dotdot cp_starts ↻ cp_dest_ok cp_dotdot ↻ cp_stage_dest cp_slen ↻ cp_starts ↻ cp_current_sha sys_mmap ↻ sys_read_file sys_openat_rd

structs

none

consts

43const CP_WIRE_BODY_CAP: i64 = 65536
44const CP_CAP_OBSERVED_MAX: i64 = 831
45const CP_I64_DIGITS: i64 = 19
47const 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
50const CP_WORST_CALL_MS: i64 = 7540
53const CP_RESUME_ALLOWANCE: i64 = 4
55const CP_DIR: *u8 = "knowledge/contentput" as *u8
56const CP_INDEX: *u8 = "knowledge/contentput/index.txt" as *u8
57const CP_INDEX_TMP: *u8 = "knowledge/contentput/index.tmp" as *u8
58const CP_PATHCAP: i64 = 1024
59const CP_SHAHEX: i64 = 64
60const CP_DIGEST: i64 = 32
62const CP_EXIT_IO: i64 = 2
63const CP_EXIT_USAGE: i64 = 3
64const CP_EXIT_DEST: i64 = 4
65const CP_EXIT_CAS: i64 = 5
66const CP_EXIT_MISSING: i64 = 6
67const CP_EXIT_SHA: i64 = 7
358const CP_CDC_AVG_DIV: i64 = 2
359const CP_CDC_MIN_DIV: i64 = 8
656const 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
657const CP_CLONE_RANGE_WORDS: i64 = 4
665const 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)
666const CP_STAT_CAP: i64 = 256
667const CP_SEEK_SET: i64 = 0

functions

69func cp_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
70func cp_out(s: *u8) -> i64 { sys_write(1, s, cp_slen(s)); return 0 }
71func cp_eo(s: *u8) -> i64 { sys_write(2, s, cp_slen(s)); return 0 }
72func cp_itoa(v: i64, out: *u8) -> i64
84func 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 }
85func cp_streq(a: *u8, b: *u8) -> i64
91func cp_starts(p: *u8, pre: *u8) -> i64
96func cp_dotdot(p: *u8) -> i64
101func cp_append(dst: *u8, pos: i64, src: *u8) -> i64
107func cp_append_num(dst: *u8, pos: i64, v: i64) -> i64
113func cp_parse(s: *u8, pos: *i64) -> i64
120func cp_hex_into(d: *u8, out: *u8) -> i64
called by 1: cp_sha_hex_of
133func cp_sha_hex_of(buf: *u8, n: i64, outhex: *u8) -> i64
140func cp_mkdir(path: *u8) -> i64
called by 1: v_begin calls 2: sys_mmapsys_munmap
147func cp_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
165func cp_stage_dest(p: *u8) -> i64
called by 1: cp_dest_ok calls 1: cp_slen
178func cp_is_source_dest(p: *u8) -> i64
called by 1: v_begin calls 2: cp_dotdotcp_starts
184func cp_current_sha(path: *u8, outhex: *u8) -> i64
193func cp_expect_value(arg: *u8, out: *u8) -> i64
called by 1: v_begin calls 2: cp_startscp_append
207func cp_expect_wellformed(v: *u8, n: i64) -> i64
called by 2: cp_chunk_expectv_begin calls 1: cp_streq
228func cp_chunk_expect(arg: *u8, arrived_hex: *u8) -> i64
239func cp_read_meta_expect(id: i64, out: *u8) -> i64
256func cp_dest_ok(p: *u8) -> i64
267func 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 }
268func 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 }
269func 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 }
273func 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 }
276func 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 }
278func cp_cdc_count(id: i64) -> i64
292func cp_file_size(path: *u8) -> i64
299func 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 }
309func cp_write_atomic_x(tmppath: *u8, finalpath: *u8, buf: *u8, n: i64, sync: i64) -> i64
323func cp_write_atomic(tmppath: *u8, finalpath: *u8, buf: *u8, n: i64) -> i64 { return cp_write_atomic_x(tmppath, finalpath, buf, n, 1) }
325func cp_dir_sync() -> i64
332func cp_append_line(path: *u8, line: *u8) -> i64
342func cp_chunk_b64() -> i64
called by 2: cp_chunk_rawv_begin calls 1: cp_slen
348func cp_chunk_raw() -> i64 { return cp_chunk_b64() / 4 * 3 }
360func cp_cdc_gear(buf: *u8, n: i64, offs: *i64, cap: i64) -> i64
369func cp_read_meta(id: i64, dest: *u8, nums: *i64, sha: *u8) -> i64
427func cp_reap(now_s: i64) -> i64
485func v_begin(dest: *u8, total_s: *u8, sha: *u8, expect_arg: *u8) -> i64
584func cp_shas_append(id: i64, idx: i64, hex: *u8) -> i64
598func cp_rl_append(id: i64, k: i64, off: i64, len: i64) -> i64
613func cp_rl_load(id: i64, nch: i64, offs: *i64, lens: *i64) -> i64
640func cp_stage_write_x(id: i64, idx: i64, cpath: *u8, raw: *u8, dn: i64, hex: *u8, sync: i64) -> i64
647func 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) }
669func cp_clone_window(rs: i64, re: i64, blk: i64, out: *i64) -> i64
called by 1: v_commit
680func cp_blksize_of(path: *u8) -> i64
called by 1: v_commit calls 2: sys_mmapsys_fstatat
689func cp_copy_range(src_fd: i64, dst_fd: i64, off: i64, len: i64, buf: *u8, cap: i64) -> i64
called by 1: v_commit calls 3: sys_lseeksys_readsys_write
708func 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
709func cp_stage_clone(id: i64, idx: i64, cpath: *u8, inc_fd: i64, inc: *u8, off: i64, len: i64, hex: *u8, why: *i64) -> i64
754func v_chunk(id_s: *u8, idx_s: *u8, b64: *u8, decl: *u8) -> i64
848func cp_sidecar_sha(id: i64, idx: i64, out: *u8) -> i64
879func cp_resume_offset(id: i64, nch: i64, craw: i64, total: i64) -> i64
895func cp_stream_verify(id: i64, idx: i64, buf: *u8, n: i64) -> i64
903func v_commit(id_s: *u8) -> i64
1116func v_status(id_s: *u8) -> i64
1158func v_abort(id_s: *u8) -> i64
1174func v_reap() -> i64
1186func cp_src_ok(p: *u8) -> i64
called by 1: v_cdcplan calls 2: cp_dotdotcp_starts
1201func cp_append_at(id: i64, off: i64, raw: *u8, dn: i64, decl: *u8) -> i64
1257func v_append(id_s: *u8, off_s: *u8, b64: *u8, decl: *u8) -> i64
1275func v_plan(id_s: *u8, count_s: *u8) -> i64
1310func v_have(id_s: *u8, from_arg: *u8) -> i64
1361func v_reuse(id_s: *u8, b64: *u8) -> i64
1484func v_cdcplan(src: *u8) -> i64
1523func main(argc: i64, argv: *i64) -> i64