nx_lorebook.nx
buildroot/runtime/nx_lorebook.nx
about
nx_lorebook.nx -- THE LOREBOOK PLANE (companionchat CC3): keyed world entries as DATA, injected into the chat turn ONLY when
the turn touches one of an entry's trigger terms, under an insertion budget that REFUSES LOUDLY instead of truncating.
Rows (knowledge/lorebook.conf -- daemon cwd first, then the nishihost tree, the gen_chat_budget.conf idiom):
key|trigger1, trigger2, ...|entry text (# comments and blank lines skipped)
@budget N (total bytes the [WORLD] block may carry, header included)
A row with fewer than three fields is skipped and COUNTED as malformed, never guessed at. Matching is an ASCII
case-insensitive substring of a trimmed trigger inside the user turn (non-ASCII case folding is deliberately not done --
a documented imprecision, not an accident); an entry fires at most once per turn however many of its triggers match; a
turn that touches no trigger writes ZERO bytes (the neg-control). When the fired entries would exceed the budget, NOTHING
is injected partially: out carries one refusal line naming the bytes wanted and the budget, and stats[LB_ST_REFUSED]=1, so
the persona is never silently cut and the caller can log the refusal rather than the model reading half a fact.
Contract symbol: em_lorebook (the companionchat.matrix CC3 watch). Composed by nx_companion_mind into the chat turn.
stats[]: LB_ST_ROWS parsed rows, LB_ST_MALFORMED, LB_ST_FIRED, LB_ST_WANTED bytes, LB_ST_BUDGET effective, LB_ST_REFUSED 0/1.
Gate: nx_lorebook_gate (fixture confs under /tmp/<gate>/, written at run time). license_tier: ORIGINAL
dependencies 1 imports · 2 importers
imports: nx_syscalls.nx
imported by: nx_companion_mind.nxnx_lorebook_gate.nx
structs
| none |
consts
| 17 | const LB_CONF_REL: *u8 = "knowledge/lorebook.conf" as *u8 |
| 18 | const LB_CONF_ABS: *u8 = "/volume1/homes/elderwesto/nishihost/knowledge/lorebook.conf" as *u8 |
| 21 | const LB_BUDGET_DEFAULT: i64 = 4096 |
| 22 | const LB_STATS_N: i64 = 8 |
| 23 | const LB_ST_ROWS: i64 = 0 |
| 24 | const LB_ST_MALFORMED: i64 = 1 |
| 25 | const LB_ST_FIRED: i64 = 2 |
| 26 | const LB_ST_WANTED: i64 = 3 |
| 27 | const LB_ST_BUDGET: i64 = 4 |
| 28 | const LB_ST_REFUSED: i64 = 5 |
| 30 | const LB_REFUSE_MIN_CAP: i64 = 256 |
| 31 | const LB_ACC_HEADROOM: i64 = 4096 |
| 32 | const LB_NUM_CAP: i64 = 24 |
| 33 | const LB_DEC_BASE: i64 = 10 |
| 34 | const LB_CH_NL: i64 = 10 |
| 35 | const LB_CH_CR: i64 = 13 |
| 36 | const LB_CH_SPACE: i64 = 32 |
| 37 | const LB_CH_HASH: i64 = 35 |
| 38 | const LB_CH_AT: i64 = 64 |
| 39 | const LB_CH_PIPE: i64 = 124 |
| 40 | const LB_CH_COMMA: i64 = 44 |
| 41 | const LB_CH_0: i64 = 48 |
| 42 | const LB_CH_9: i64 = 57 |
| 43 | const LB_CH_A_UP: i64 = 65 |
| 44 | const LB_CH_Z_UP: i64 = 90 |
| 45 | const LB_CASE_DELTA: i64 = 32 |
| 46 | const LB_BUDGET_KEY: *u8 = "@budget" as *u8 |
| 47 | const LB_HEAD: *u8 = "[WORLD -- facts about the world this conversation just touched; treat them as true and use the exact detail]\n" as *u8 |
| 48 | const LB_ITEM: *u8 = "- " as *u8 |
| 49 | const LB_SEP: *u8 = ": " as *u8 |
| 50 | const LB_REFUSE_HEAD: *u8 = "[WORLD refused: the entries this turn touched need " as *u8 |
| 51 | const LB_REFUSE_MID: *u8 = " bytes against a lorebook budget of " as *u8 |
| 52 | const LB_REFUSE_TAIL: *u8 = " -- raise @budget in lorebook.conf or split the entries; nothing was injected partially]\n" as *u8 |
functions
| 54 | func lb_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } called by 1: em_lorebook_from |
| 55 | func lb_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } return o } called by 1: em_lorebook_from |
| 56 | func lb_catn(dst: *u8, off: i64, src: *u8, n: i64) -> i64 { var o: i64 = off; var i: i64 = 0; while i < n { dst[o] = src[i]; o = o + 1; i = i + 1 } return o } called by 1: em_lorebook_from |
| 57 | func lb_itoa(dst: *u8, off: i64, v: i64) -> i64 |
| 65 | func lb_lower(c: i64) -> i64 { if c >= LB_CH_A_UP { if c <= LB_CH_Z_UP { return c + LB_CASE_DELTA } } return c } called by 1: lb_ci_find |
| 68 | func lb_ci_find(hay: *u8, hn: i64, needle: *u8, nn: i64) -> i64 |
| 87 | func lb_triggers_hit(trig: *u8, tn: i64, umsg: *u8, umlen: i64) -> i64 |
| 110 | func em_lorebook_from(path: *u8, umsg: *u8, umlen: i64, out: *u8, cap: i64, stats: *i64) -> i64 called by 3: em_lorebooklg_runmain calls 8: sys_mmapsys_read_filelb_ci_findlb_lenlb_triggers_hitlb_cat+2 |
| 196 | func em_lorebook(umsg: *u8, umlen: i64, out: *u8, cap: i64, stats: *i64) -> i64 |