code wiki / _hdl_build / nx_tokgate.nx

nx_tokgate.nx

buildroot/runtime/_hdl_build/nx_tokgate.nx

28235 B597 linesdepth 3pulls 3 transitivereach 0 importersview sourcekind tool
docsdependenciesstructsconstsfunctions

about

nx_tokgate.nx -- TOKEN PRE-FLIGHT (engineshift ES17): the cheaper read is named BEFORE the expensive one lands. Operator 2026-09-02: "design the llm capabilities around minimizing unnecessary token usage autonomously without my need for engagement as part of our standard ways of working without making the design and coding and other stuff worse." MEASURED the same day by nx_seat_meter_beat residue over 40 days of this seat's transcripts: reasoning 56 permil, authored 245 permil, MECHANICAL 668 permil -- two thirds of every token is tool output re-read into context. The lever is the read, not the thinking. So this rides PreToolUse on Read and on the estate's nx_fs read. It sizes the subject and, when a whole-file read would land more than the floor, names the digest that answers the same question in fewer bytes. It NEVER blocks and emits nothing under the floor: (law) A PRE-FLIGHT THAT CAN REFUSE WILL BE DISABLED; ONE THAT ONLY INFORMS SURVIVES -- and one that fires on every small read becomes noise that gets switched off. (ASCII only in this file: a non-ASCII byte in the source arrived on the NAS twin 2 bytes longer through the JSON argv lane, so the two trees could not be proven identical by hash.) The floor is DATA (hooks/tokgate.conf, floor_bytes|N); when the conf is unreadable the bootstrap floor is used and the message SAYS floor_src=bootstrap, so a silent default can never masquerade as a measurement. NO POWERSHELL IN THE PATH: this organ parses the hook JSON itself. Sibling of nx_memnew / nx_organnew. nx_tokgate [conf-path] [shellverb-conf-path] stdin = the PreToolUse payload; stdout = {"systemMessage":...} or nothing ES22 / sovereignty SV3 (2026-09-05) -- THE SHELL INTERLOCK, tg_shell_interlock. Operator, verbatim: "stop using its own tools when it loads and to use these and nishi estate only and if there is a gap to build into the nishi estate from the first byte, no more ps1 and python and shell". The same organ now also rides PreToolUse on Bash and PowerShell: it reads the command and, from DATA (hooks/shellverb.conf: tree | allow | verb rows), names the estate verb that does the same work -- one row per DISTINCT verb, each stat'd against the declared source trees so the advisory can never name a verb the tree cannot show (source=present / ABSENT / unverified) -- or prints GAP when no row matches, never a guess. The sanctioned launcher (wsl -e _offc/<organ>.elf) is allow-listed by needle and draws nothing. Advisory only: the interlock exists because the law was prose in two channels, and the estate has measured what happens to an invariant that depends on a seat remembering (nx_memnew: the same doctrine written 4x with the organ one call away). license_tier: ORIGINAL. No writes. No hw writes (Rule 26).

dependencies 2 imports · 0 importers

nx_syscalls.nx nx_gatekit_lib.nx nx_tokgate.nx

imports: nx_syscalls.nxnx_gatekit_lib.nx

imported by: nobody (leaf or entry point)

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main 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 ↻ tg_slurp sys_read tg_strval gk_len tg_is_shell_tool gk_streq tg_shell_interlock sys_mmap ↻ tg_jstr gk_out_pos gk_len ↻ gk_len ↻ gk_cat sys_read_file sys_openat_rd sys_lseek sys_mmap ↻ sys_read ↻ sys_munmap sys_close tg_escs tg_esc gk_len ↻ gk_eol

structs

none

consts

34const TG_IN: i64 = 262144
35const TG_MSG: i64 = 65536
36const TG_PATH: i64 = 4096
37const TG_ROW: i64 = 256
38const TG_CONF_DEFAULT: *u8 = "/mnt/c/Users/elder/nishi-ops/hooks/tokgate.conf"
39const TG_FLOOR_BOOTSTRAP: i64 = 3072 // ONLY when the conf is unreadable, and the message names that source
40const TG_BYTES_PER_TOKEN: i64 = 4 // the estimate the message prints, declared as an estimate
41const TG_SRC_BOOTSTRAP: i64 = 0
42const TG_SRC_CONF: i64 = 1
44const TG_SHELLCONF_DEFAULT: *u8 = "/mnt/c/Users/elder/nishi-ops/hooks/shellverb.conf"
45const TG_CMD: i64 = 65536 // a harness command body (the payload buffer TG_IN bounds it anyway)
46const TG_FIELD: i64 = 1024 // one conf field: needle, verb, note or source name
47const TG_VERB_TABLE: i64 = 64 // distinct verbs one advisory names before it says verbs_capped=1
48const TG_EXCERPT: i64 = 96 // bytes of the command echoed back so the reader sees WHICH call drew this
49const TG_MSG_RESERVE: i64 = 4096 // headroom kept for the closing lines; verb rows stop (announced) before it
50const TG_VS_ABSENT: i64 = 0 // tg_verb_source: no tree row holds <source>.nx
51const TG_VS_PRESENT: i64 = 1 // a tree row holds it
52const TG_VS_UNVERIFIED: i64 = 2 // the conf declares no tree row, so nothing could be checked
54const TG_CH_QUOTE: i64 = 34
55const TG_CH_BACKSLASH: i64 = 92
56const TG_CH_SLASH: i64 = 47
57const TG_CH_SPACE: i64 = 32
58const TG_CH_LF: i64 = 10
59const TG_CH_CR: i64 = 13
60const TG_CH_HASH: i64 = 35 // a comment row in a conf
61const TG_CH_PIPE: i64 = 124
62const TG_CH_COLON: i64 = 58
63const TG_CH_RBRACKET: i64 = 93
64const TG_CH_QMARK: i64 = 63 // what a unicode escape reads as for matching
65const TG_CH_n: i64 = 110
66const TG_CH_u: i64 = 117
67const TG_UPPER_A: i64 = 65
68const TG_UPPER_Z: i64 = 90
69const TG_CASE_DELTA: i64 = 32
70const TG_ZERO: i64 = 48
71const TG_NINE: i64 = 57
72const TG_DECIMAL: i64 = 10
73const TG_UESC_HEX_DIGITS: i64 = 4 // the four hex digits after backslash-u
74const TG_I64_BYTES: i64 = 8
75const TG_NUMBUF: i64 = 64
76const TG_MISMATCH: i64 = 9 // added to kl so the compare loop leaves on the first differing byte (never equals kl)
77const TG_FOUND_EOF: i64 = 2 // tg_strval: the key was seen but the payload ended before its value
78const TG_DRIVE_COLON_AT: i64 = 1 // X:/... -- where the colon and the slash of a Windows drive path sit
79const TG_DRIVE_SLASH_AT: i64 = 2
80const TG_DRIVE_PREFIX_LEN: i64 = 3
81const TG_ARGV_VERB: i64 = 0 // nx_fs argv: verb, path, byte cap
82const TG_ARGV_PATH: i64 = 1
83const TG_ARGV_CAP: i64 = 2
84const TG_ARG_CONF: i64 = 1 // this organ's own argv: the tokgate conf, then the shellverb conf
85const TG_ARG_SHELLCONF: i64 = 2
86const TG_COL_KIND: i64 = 0 // shellverb.conf columns
87const TG_COL_NEEDLE: i64 = 1
88const TG_COL_TREEDIR: i64 = 1 // a tree row carries its directory in the needle column
89const TG_COL_VERB: i64 = 2
90const TG_COL_NOTE: i64 = 3
91const TG_COL_SOURCE: i64 = 4

functions

94func tg_slurp(b: *u8, cap: i64) -> i64
called by 1: main calls 1: sys_read
111func tg_strval(b: *u8, n: i64, key: *u8, out: *u8, cap: i64) -> i64
called by 2: tg_advisemain calls 1: gk_len
162func tg_argv_str(b: *u8, n: i64, idx: i64, out: *u8, cap: i64) -> i64
called by 1: tg_advise calls 2: gk_out_posgk_len
200func tg_wsl(src: *u8, out: *u8, cap: i64) -> i64
called by 1: tg_advise calls 2: gk_lengk_cat
225func tg_base(p: *u8) -> *u8
called by 1: tg_advise calls 1: gk_len
237func tg_is_jsonl(p: *u8) -> i64
called by 1: tg_advise calls 1: gk_len
248func tg_esc(dst: *u8, o: i64, src: *u8, n: i64) -> i64
263func tg_escs(dst: *u8, o: i64, src: *u8) -> i64 { return tg_esc(dst, o, src, gk_len(src)) }
264func tg_escn(dst: *u8, o: i64, v: i64) -> i64
272func tg_floor(conf: *u8, src: *i64) -> i64
299func tg_emit(msg: *u8, m: i64) -> i64
called by 1: main calls 2: gk_catgk_write_all
308func tg_advise(inb: *u8, n: i64, tool: *u8, floor: i64, fsrc: *i64, msg: *u8) -> i64
369func tg_is_shell_tool(tool: *u8) -> i64
called by 1: main calls 1: gk_streq
379func tg_jstr(b: *u8, n: i64, key: *u8, out: *u8, cap: i64) -> i64
called by 1: tg_shell_interlock calls 2: gk_out_posgk_len
422func tg_field(b: *u8, s: i64, e: i64, idx: i64, out: *u8, cap: i64) -> i64
446func tg_verb_source(cb: *u8, cn: i64, name: *u8, kind: *u8, root: *u8, path: *u8) -> i64
472func tg_shell_interlock(inb: *u8, n: i64, tool: *u8, shellconf: *u8, msg: *u8) -> i64
576func main(argc: i64, argv: *i64) -> i64