code wiki / _hdl_build / nx_tokgate.nx
nx_tokgate.nx
buildroot/runtime/_hdl_build/nx_tokgate.nx
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
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
structs
| none |
consts
| 34 | const TG_IN: i64 = 262144 |
| 35 | const TG_MSG: i64 = 65536 |
| 36 | const TG_PATH: i64 = 4096 |
| 37 | const TG_ROW: i64 = 256 |
| 38 | const TG_CONF_DEFAULT: *u8 = "/mnt/c/Users/elder/nishi-ops/hooks/tokgate.conf" |
| 39 | const TG_FLOOR_BOOTSTRAP: i64 = 3072 // ONLY when the conf is unreadable, and the message names that source |
| 40 | const TG_BYTES_PER_TOKEN: i64 = 4 // the estimate the message prints, declared as an estimate |
| 41 | const TG_SRC_BOOTSTRAP: i64 = 0 |
| 42 | const TG_SRC_CONF: i64 = 1 |
| 44 | const TG_SHELLCONF_DEFAULT: *u8 = "/mnt/c/Users/elder/nishi-ops/hooks/shellverb.conf" |
| 45 | const TG_CMD: i64 = 65536 // a harness command body (the payload buffer TG_IN bounds it anyway) |
| 46 | const TG_FIELD: i64 = 1024 // one conf field: needle, verb, note or source name |
| 47 | const TG_VERB_TABLE: i64 = 64 // distinct verbs one advisory names before it says verbs_capped=1 |
| 48 | const TG_EXCERPT: i64 = 96 // bytes of the command echoed back so the reader sees WHICH call drew this |
| 49 | const TG_MSG_RESERVE: i64 = 4096 // headroom kept for the closing lines; verb rows stop (announced) before it |
| 50 | const TG_VS_ABSENT: i64 = 0 // tg_verb_source: no tree row holds <source>.nx |
| 51 | const TG_VS_PRESENT: i64 = 1 // a tree row holds it |
| 52 | const TG_VS_UNVERIFIED: i64 = 2 // the conf declares no tree row, so nothing could be checked |
| 54 | const TG_CH_QUOTE: i64 = 34 |
| 55 | const TG_CH_BACKSLASH: i64 = 92 |
| 56 | const TG_CH_SLASH: i64 = 47 |
| 57 | const TG_CH_SPACE: i64 = 32 |
| 58 | const TG_CH_LF: i64 = 10 |
| 59 | const TG_CH_CR: i64 = 13 |
| 60 | const TG_CH_HASH: i64 = 35 // a comment row in a conf |
| 61 | const TG_CH_PIPE: i64 = 124 |
| 62 | const TG_CH_COLON: i64 = 58 |
| 63 | const TG_CH_RBRACKET: i64 = 93 |
| 64 | const TG_CH_QMARK: i64 = 63 // what a unicode escape reads as for matching |
| 65 | const TG_CH_n: i64 = 110 |
| 66 | const TG_CH_u: i64 = 117 |
| 67 | const TG_UPPER_A: i64 = 65 |
| 68 | const TG_UPPER_Z: i64 = 90 |
| 69 | const TG_CASE_DELTA: i64 = 32 |
| 70 | const TG_ZERO: i64 = 48 |
| 71 | const TG_NINE: i64 = 57 |
| 72 | const TG_DECIMAL: i64 = 10 |
| 73 | const TG_UESC_HEX_DIGITS: i64 = 4 // the four hex digits after backslash-u |
| 74 | const TG_I64_BYTES: i64 = 8 |
| 75 | const TG_NUMBUF: i64 = 64 |
| 76 | const TG_MISMATCH: i64 = 9 // added to kl so the compare loop leaves on the first differing byte (never equals kl) |
| 77 | const TG_FOUND_EOF: i64 = 2 // tg_strval: the key was seen but the payload ended before its value |
| 78 | const TG_DRIVE_COLON_AT: i64 = 1 // X:/... -- where the colon and the slash of a Windows drive path sit |
| 79 | const TG_DRIVE_SLASH_AT: i64 = 2 |
| 80 | const TG_DRIVE_PREFIX_LEN: i64 = 3 |
| 81 | const TG_ARGV_VERB: i64 = 0 // nx_fs argv: verb, path, byte cap |
| 82 | const TG_ARGV_PATH: i64 = 1 |
| 83 | const TG_ARGV_CAP: i64 = 2 |
| 84 | const TG_ARG_CONF: i64 = 1 // this organ's own argv: the tokgate conf, then the shellverb conf |
| 85 | const TG_ARG_SHELLCONF: i64 = 2 |
| 86 | const TG_COL_KIND: i64 = 0 // shellverb.conf columns |
| 87 | const TG_COL_NEEDLE: i64 = 1 |
| 88 | const TG_COL_TREEDIR: i64 = 1 // a tree row carries its directory in the needle column |
| 89 | const TG_COL_VERB: i64 = 2 |
| 90 | const TG_COL_NOTE: i64 = 3 |
| 91 | const TG_COL_SOURCE: i64 = 4 |
functions
| 94 | func tg_slurp(b: *u8, cap: i64) -> i64 |
| 111 | func tg_strval(b: *u8, n: i64, key: *u8, out: *u8, cap: i64) -> i64 |
| 162 | func tg_argv_str(b: *u8, n: i64, idx: i64, out: *u8, cap: i64) -> i64 |
| 200 | func tg_wsl(src: *u8, out: *u8, cap: i64) -> i64 |
| 225 | func tg_base(p: *u8) -> *u8 |
| 237 | func tg_is_jsonl(p: *u8) -> i64 |
| 248 | func tg_esc(dst: *u8, o: i64, src: *u8, n: i64) -> i64 |
| 263 | func tg_escs(dst: *u8, o: i64, src: *u8) -> i64 { return tg_esc(dst, o, src, gk_len(src)) } |
| 264 | func tg_escn(dst: *u8, o: i64, v: i64) -> i64 |
| 272 | func tg_floor(conf: *u8, src: *i64) -> i64 |
| 299 | func tg_emit(msg: *u8, m: i64) -> i64 |
| 308 | func tg_advise(inb: *u8, n: i64, tool: *u8, floor: i64, fsrc: *i64, msg: *u8) -> i64 |
| 369 | func tg_is_shell_tool(tool: *u8) -> i64 |
| 379 | func tg_jstr(b: *u8, n: i64, key: *u8, out: *u8, cap: i64) -> i64 |
| 422 | func tg_field(b: *u8, s: i64, e: i64, idx: i64, out: *u8, cap: i64) -> i64 |
| 446 | func tg_verb_source(cb: *u8, cn: i64, name: *u8, kind: *u8, root: *u8, path: *u8) -> i64 |
| 472 | func tg_shell_interlock(inb: *u8, n: i64, tool: *u8, shellconf: *u8, msg: *u8) -> i64 |
| 576 | func main(argc: i64, argv: *i64) -> i64 |