nx_entity_pin_gate.nx source
↩ module page · 111 lines · 6673 B
1// nx_entity_pin_gate.nx -- the entity pin projection is proven on fixture dossiers (S11, 2026-09-17).
2// Teeth: a resolved dossier becomes one row with every field; a second projection of the same key REPLACES the row
3// (never a duplicate); another entity's row survives; a missing dossier and an unresolved one are counted and change
4// nothing (neg-controls); a differently spaced and cased name normalises onto the same key. Counts are printed as
5// values. Fixture prefix /tmp/nx_entity_pin_gate- (never the production knowledge/status/).
6// expect_exit: 0 license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_entity_pin_lib.nx"
9import "nx_gate_verdict.nx"
10
11const PG_PREFIX: *u8 = "/tmp/nx_entity_pin_gate-"
12const PG_TSV: *u8 = "/tmp/nx_entity_pin_gate-entitypin.tsv"
13const PG_DOSSIER_A: *u8 = "/tmp/nx_entity_pin_gate-dossier-zq-name.txt"
14const PG_DOSSIER_B: *u8 = "/tmp/nx_entity_pin_gate-dossier-other-one.txt"
15const PG_DOSSIER_G: *u8 = "/tmp/nx_entity_pin_gate-dossier-ghost.txt"
16const PG_DOSSIER_N: *u8 = "/tmp/nx_entity_pin_gate-dossier-nobody-here.txt"
17const PG_FILE_MODE: i64 = 420
18const PG_CID_A: i64 = 853
19const PG_CID_A2: i64 = 854
20const PG_CID_B: i64 = 900
21const PG_AUTH_A: i64 = 1051
22const PG_ONE: i64 = 1
23const PG_TWO: i64 = 2
24const PG_ZERO: i64 = 0
25
26func pg_write(path: *u8, s: *u8) -> i64 {
27 let fd: i64 = sys_openat_wr(path, PG_FILE_MODE)
28 if fd < 0 { return 0 - 1 }
29 let n: i64 = ep_slen(s)
30 var w: i64 = 0
31 var bad: i64 = 0
32 while w < n { if bad == 1 { w = n } else { let k: i64 = sys_write(fd, ((s as i64) + w) as *u8, n - w); if k <= 0 { bad = 1 } else { w = w + k } } }
33 sys_close(fd)
34 return n
35}
36func pg_unlink(path: *u8) -> i64 { return sys_unlinkat(path) }
37func pg_count(buf: *u8, n: i64, needle: *u8) -> i64 {
38 let m: i64 = ep_slen(needle)
39 var cnt: i64 = 0
40 var i: i64 = 0
41 while i + m <= n {
42 var j: i64 = 0
43 var same: i64 = 1
44 while j < m { if buf[i + j] != needle[j] { same = 0; j = m } else { j = j + 1 } }
45 if same == 1 { cnt = cnt + 1 }
46 i = i + 1
47 }
48 return cnt
49}
50func pg_table_count(needle: *u8) -> i64 {
51 let lenp: *i64 = sys_mmap(8) as *i64
52 let buf: *u8 = ep_read(PG_TSV, lenp)
53 if (buf as i64) == 0 { return 0 }
54 let c: i64 = pg_count(buf, lenp[0], needle)
55 ep_free(buf, lenp[0])
56 return c
57}
58func pg_project1(name: *u8, counts: *i64) -> i64 {
59 let names: *i64 = sys_mmap(8) as *i64
60 names[0] = name as i64
61 return ep_project(PG_PREFIX, names, 1, counts)
62}
63
64func main(argc: i64, argv: *i64) -> i64 {
65 let ctr: *i64 = sys_mmap(64) as *i64
66 gv_puts("nx_entity_pin_gate -- the dossier-to-served-table projection, on fixtures\n\n" as *u8)
67 // setup: a clean fixture prefix (an absent table reads as no rows)
68 pg_unlink(PG_TSV)
69 pg_unlink(PG_DOSSIER_A)
70 pg_unlink(PG_DOSSIER_B)
71 pg_unlink(PG_DOSSIER_G)
72 pg_unlink(PG_DOSSIER_N)
73 let counts: *i64 = sys_mmap(EP_C_N * 8) as *i64
74 // T1 a resolved dossier becomes one row with every field
75 pg_write(PG_DOSSIER_A, "{\"v\":1,\"entity\":\"zq name\",\"found\":true,\"evidence\":{\"pick_kind\":2,\"card_fields\":8,\"authority_total\":1051},\"canonical_url\":\"https://content.example.com/notes/zq\",\"home_host\":\"content.example.com\",\"landing_cid\":\"853\",\"card\":{\"jp\":\"ZQ\",\"age\":\"39\"}}\n" as *u8)
76 pg_project1("zq name" as *u8, counts)
77 gv_check_eq("T1 one resolved dossier pins one row" as *u8, counts[EP_C_PINNED], PG_ONE, ctr)
78 gv_check_eq("T1a the table holds one row" as *u8, counts[EP_C_ROWS], PG_ONE, ctr)
79 gv_check("T1b the row carries key, cid, url, jp and authority in order" as *u8, (pg_table_count("zq name\t853\thttps://content.example.com/notes/zq\tZQ\t1051\t" as *u8) == PG_ONE) as i64, ctr)
80 // T2 projecting the same key again REPLACES the row (a changed cid), never duplicates it
81 pg_write(PG_DOSSIER_A, "{\"v\":1,\"entity\":\"zq name\",\"found\":true,\"evidence\":{\"authority_total\":1051},\"canonical_url\":\"https://content.example.com/notes/zq\",\"landing_cid\":\"854\",\"card\":{\"jp\":\"ZQ\"}}\n" as *u8)
82 pg_project1("zq name" as *u8, counts)
83 gv_check_eq("T2 the second projection keeps one row for the key" as *u8, pg_table_count("zq name\t" as *u8), PG_ONE, ctr)
84 gv_check_eq("T2a the row now carries the new cid" as *u8, pg_table_count("\t854\t" as *u8), PG_ONE, ctr)
85 gv_check_eq("neg-control-T2b the old cid is gone" as *u8, pg_table_count("\t853\t" as *u8), PG_ZERO, ctr)
86 // T3 another entity's row survives the next upsert
87 pg_write(PG_DOSSIER_B, "{\"found\":true,\"evidence\":{\"authority_total\":7},\"canonical_url\":\"https://content.example.com/other\",\"landing_cid\":\"900\",\"card\":{\"jp\":\"OTHER\"}}\n" as *u8)
88 pg_project1("Other One" as *u8, counts)
89 gv_check_eq("T3 two entities give two rows" as *u8, counts[EP_C_ROWS], PG_TWO, ctr)
90 gv_check_eq("T3a the earlier key is still there" as *u8, pg_table_count("zq name\t854\t" as *u8), PG_ONE, ctr)
91 gv_check_eq("T3b the new key is lowercased and single-spaced" as *u8, pg_table_count("other one\t900\t" as *u8), PG_ONE, ctr)
92 // T4 neg-control: a missing dossier is counted and changes nothing
93 pg_project1("nobody here" as *u8, counts)
94 gv_check_eq("neg-control-T4 a missing dossier is counted MISSING" as *u8, counts[EP_C_MISSING], PG_ONE, ctr)
95 gv_check_eq("neg-control-T4a and pins nothing" as *u8, counts[EP_C_PINNED], PG_ZERO, ctr)
96 gv_check_eq("neg-control-T4b and leaves the two rows" as *u8, counts[EP_C_ROWS], PG_TWO, ctr)
97 // T5 neg-control: a dossier that did not resolve (found:false) is counted and changes nothing
98 pg_write(PG_DOSSIER_G, "{\"v\":1,\"entity\":\"ghost\",\"found\":false,\"landing_cid\":\"0\"}\n" as *u8)
99 pg_project1("ghost" as *u8, counts)
100 gv_check_eq("neg-control-T5 an unresolved dossier is counted UNRESOLVED" as *u8, counts[EP_C_UNRESOLVED], PG_ONE, ctr)
101 gv_check_eq("neg-control-T5a and leaves the two rows" as *u8, counts[EP_C_ROWS], PG_TWO, ctr)
102 // T6 a differently spaced and cased name normalises onto the same key (an upsert, not a third row)
103 pg_project1(" Zq NAME " as *u8, counts)
104 gv_check_eq("T6 a re-spaced re-cased name upserts the same key" as *u8, counts[EP_C_ROWS], PG_TWO, ctr)
105 gv_check_eq("T6a still exactly one row for that key" as *u8, pg_table_count("zq name\t" as *u8), PG_ONE, ctr)
106 gv_kv("rows" as *u8, counts[EP_C_ROWS])
107 gv_kv("pinned_last" as *u8, counts[EP_C_PINNED])
108 let rc: i64 = gv_verdict("ENTITY-PIN-GATE" as *u8, ctr, "dossier resolutions project into the one served table, upsert by normalised key, misses named" as *u8)
109 sys_exit_group(rc)
110 return rc
111}