code wiki / _hdl_build / nx_memcliff.nx
nx_memcliff.nx
buildroot/runtime/_hdl_build/nx_memcliff.nx
about
nx_memcliff.nx -- SOVEREIGN owner of the MEMORY.md byte budget. Replaces memindex_enforce.py
(Stop hook) and the eviction half of memory_cliff_check.ps1 (SessionStart/PostToolUse) with ONE
nishi organ, so no interpreter sits in the hook path.
WHY AN ORGAN AND NOT A SCRIPT
-----------------------------
MEMORY.md is injected into every session; past a byte limit the harness DROPS THE TAIL SILENTLY --
no error, no marker. The dropped lines are exactly the ones that would have told you they were
dropped. The rail-side enforcer cannot own this file (it runs NAS-side; MEMORY.md is laptop-local,
nx_memindex_put -> `no-md-file`), so ownership has to live here, next to the file.
WHAT WAS ACTUALLY BROKEN -- MEASURED 2026-08-06, not assumed
------------------------------------------------------------
The Python enforcer was NOT missing: it had run 64 times (64 eviction stamps in the overflow) and
had compacted the file at 13:47 the same day. It had JAMMED:
"22013 bytes over budget but NO evictable entries found -- refusing to guess"
Root cause, measured: its rule NEVER EVICT THE NEWEST DAY pinned 8 of 8 candidates, because every
evictable entry carried today's date. Survivors after the pin: ZERO.
★LAW: A PIN THAT CAN COVER 100% OF ITS CANDIDATES IS AN OFF SWITCH. The rule that protects today's
record disables enforcement precisely on the heaviest days -- the file grew 4.2KB in the two hours
after that successful compaction, with nothing left able to evict it.
The fix is not to drop the pin (it earned its place: a pure oldest-first cut once ate the current
session's own record) but to make it YIELD: keep the newest `keep_newest_day` entries of the newest
day and release the rest, rather than pinning the whole day unconditionally.
SECOND MEASURED DEFECT -- the enforcer could not measure its subject in the unit the limit uses.
MEMORY.md is CRLF (63 CRLF, 0 bare LF). Python read it in text mode, which collapses CRLF to LF, so
it measured 22,013 against a file of 22,076 bytes -- UNDERSTATING by exactly the line count. The
harness cliff is a property of the bytes on disk, so every decision was made against a number that
was never the file's size.
★LAW: A BUDGET ENFORCER THAT CANNOT MEASURE ITS SUBJECT IN THE UNIT THE LIMIT USES IS GUESSING.
⚠WHAT I FIRST WROTE HERE AND THEN DISPROVED: I claimed the two owners also flipped the file's line
endings against each other (py writing LF, the ps1 writing CRLF). THE EXPERIMENT REFUTED IT -- a
CRLF fixture through the oracle came out CRLF=26 / bare-LF=0, because Python's text-mode WRITE
translates back on Windows, exactly undoing the read. The round trip is lossless; only the
measurement was wrong. Recorded rather than quietly deleted, because a plausible mechanism I had
not run is precisely the kind of premise that spends like evidence.
This organ works in RAW BYTES and reconstructs each line with its original terminator, so it is
byte-exact by construction rather than by a translation that happens to cancel out (verified: the
same CRLF fixture out at CRLF=26 / bare-LF=0).
dependencies 1 imports · 0 importers
imports: nx_memplane_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
| 83 | const MC_DIR: *u8 = "/mnt/c/Users/elder/.claude/projects/C--Users-elder/memory" |
| 84 | const MC_N_MEM: *u8 = "MEMORY.md" |
| 85 | const MC_N_OVF: *u8 = "reference-index-recent-refs-dormant.md" |
| 86 | const MC_N_CNF: *u8 = "memcliff.conf" |
| 87 | const MC_N_TMP: *u8 = "MEMORY.md.mctmp" |
| 89 | const MC_MAXL: i64 = 8192 // max lines in the index (64 today; vast headroom) |
| 90 | const MC_BUF: i64 = 4194304 // 4MB read buffer |
| 91 | const MC_OUT: i64 = 4194304 // 4MB assembled output |
| 92 | const MC_MSG: i64 = 8192 // report buffer |
| 95 | const MC_D_CLIFF: i64 = 25000 // below the 26,229 B measured truncation |
| 96 | const MC_D_TARGET: i64 = 23000 // evict down to this; above the 22,076 B measured-good load |
| 97 | const MC_D_WARN: i64 = 21000 // report headroom from here up |
| 98 | const MC_D_KEEP: i64 = 3 // newest-day entries always kept when the pin has to yield |
| 99 | const MC_D_PIN: i64 = 12 // pinned head: header + survival block, never evicted |
| 105 | const MC_D_MAXLINE: i64 = 200 // measured: line 201+ is not delivered |
| 106 | const MC_D_TGTLINE: i64 = 180 // evict down to this, so ordinary appends don't immediately rebreach |
| 108 | const MC_PTRTAG: *u8 = "verbatim by nx_memcliff:" |
functions
| 116 | func mc_at(buf: *u8, pos: i64, lim: i64, pat: *u8) -> i64 |
| 128 | func mc_find(buf: *u8, from: i64, lim: i64, pat: *u8) -> i64 |
| 140 | func mc_isdigit(c: u8) -> i64 |
| 150 | func mc_emit(json: i64, buf: *u8, n: i64) -> i64 |
| 180 | func mc_join(dir: *u8, name: *u8) -> *u8 called by 1: main |
| 194 | func mc_fsize(path: *u8) -> i64 called by 1: main |
| 202 | func mc_exists(path: *u8) -> i64 called by 1: main |
| 209 | func mp_readf(path: *u8, buf: *u8, cap: i64) -> i64 called by 1: main |
| 228 | func mc_conf(cbuf: *u8, clen: i64, key: *u8, dflt: i64) -> i64 |
| 271 | func mc_split(buf: *u8, n: i64, ls: *i64, ll: *i64, cap: i64) -> i64 called by 1: main |
| 290 | func mc_datekey(buf: *u8, s: i64, e: i64) -> i64 |
| 346 | func mc_is_entry(buf: *u8, s: i64, e: i64) -> i64 |
| 357 | func main(argc: i64, argv: *i64) -> i64 |