nx_id_ledger_existing_keys_audit_t219.nx source
↩ module page · 206 lines · 9499 B
1// nx_id_ledger_existing_keys_audit_t219.nx -- Audits existing ledger keys by checking their presence in job files and generating hash-based evidence.
2import "nx_apistack_idempotency.nx"
3import "nx_toolcall_idem_lib.nx"
4import "nx_fsops_lib.nx"
5import "nx_site_lock_lib.nx"
6import "nx_sha256.nx"
7// Metadata only: no journal keys, result bodies, paths from rows or credentials.
8func la_len(p:*u8)->i64{var n:i64=0;while p[n]!=0 as u8{n=n+1};return n}
9func la_prefix(p:*u8,n:i64,s:*u8)->i64{
10 let sn:i64=la_len(s);if n<sn{return 0};var i:i64=0
11 while i<sn{if p[i]!=s[i]{return 0};i=i+1};return 1
12}
13func la_hash(p:*u8,n:i64)->i64{
14 let digest:*u8=sys_mmap_try(SHA256_DIGEST_BYTES)
15 if (digest as i64)<=0{fsx_puts("null");return ID_RESOURCE}
16 let rc:i64=sha256_digest_checked_native(p,n,digest)
17 if rc!=0{sys_munmap_direct(digest,SHA256_DIGEST_BYTES);fsx_puts("null");return rc}
18 let hex:*u8="0123456789abcdef";var i:i64=0;fsx_puts("\"")
19 while i<SHA256_DIGEST_BYTES{let v:i64=digest[i] as i64;sys_write(1,hex+(v>>4),1);sys_write(1,hex+(v&15),1);i=i+1}
20 fsx_puts("\"");sys_munmap_direct(digest,SHA256_DIGEST_BYTES);return 0
21}
22func la_kind(p:*u8,n:i64)->i64{
23 if la_prefix(p,n,"rc=0 verb=")==1{return 1}
24 if la_prefix(p,n,"lane=")==1{return 2}
25 if la_prefix(p,n,"rc=0 request_v=1 ")==1{return 3}
26 return 0
27}
28func la_jobs(p:*u8,n:i64)->i64{
29 fsx_puts("[");var i:i64=0;var count:i64=0
30 while i<n{
31 if la_prefix(p+i,n-i," job=")==1{
32 var j:i64=i+5;var value:i64=0;var digits:i64=0;var overflow:i64=0
33 while j<n{
34 let ch:i64=p[j] as i64;if ch<48||ch>57{break}
35 let d:i64=ch-48
36 if value>(ID_SIGNED_MAX-d)/10{overflow=1}else{value=value*10+d}
37 digits=digits+1;j=j+1
38 }
39 if digits>0&&overflow==0{if count>0{fsx_puts(",")};fsx_putn(value);count=count+1}
40 i=j
41 }else{i=i+1}
42 }
43 fsx_puts("]");return count
44}
45
46func la_claim_exists(key:*u8,n:i64,hex:*u8,path:*u8,stat:*u8)->i64{
47 if ti_key_ok(key,n)==0{return FIO_EINVAL}
48 ti_key_hex(key,n,hex)
49 ti_claim_path("../_jobs",hex,path)
50 let fd:i64=__syscall(257,AT_FDCWD,path,ID_O_RDONLY|ID_O_NONBLOCK|ID_O_NOFOLLOW|ID_O_CLOEXEC,0,0,0)
51 if fd<0{return fd}
52 let rc:i64=sys_fstat_fd(fd,stat);let cl:i64=sys_close(fd)
53 if rc!=0{return rc};if cl!=0{return cl}
54 let mode:i64=*((stat+NX_STAT_X64_MODE_OFFSET) as *i64)
55 if (mode&NX_STAT_TYPE_MASK)!=NX_STAT_REGULAR_FILE{return FIO_EINVAL}
56 return 1
57}
58func la_occ(p:*u8,n:i64,s:*u8)->i64{
59 var count:i64=0;var i:i64=0
60 while i<n{if la_prefix(p+i,n-i,s)==1{count=count+1};i=i+1};return count
61}
62
63func la_output_evidence(source:*u8,bytes:i64,hex:*u8,path:*u8,stat:*u8,label:*u8)->i64{
64 let pn:i64=ti_out_path("_jobs",hex,path)
65 fsx_puts(label);fsx_puts("{\"ledgerOffsets\":[");var count:i64=0;var i:i64=0
66 while i<bytes{
67 if la_prefix(source+i,bytes-i,path)==1{
68 if count>0{fsx_puts(",")};count=count+1;fsx_putn(i)
69 };i=i+1
70 }
71 fsx_puts("],\"pathBytes\":");fsx_putn(pn)
72 ti_out_path("../_jobs",hex,path)
73 let fd:i64=__syscall(257,AT_FDCWD,path,ID_O_RDONLY|ID_O_NONBLOCK|ID_O_NOFOLLOW|ID_O_CLOEXEC,0,0,0)
74 var rc:i64=fd;var size:i64=0-1
75 if fd>=0{
76 rc=sys_fstat_fd(fd,stat);let closed:i64=sys_close(fd);if rc==0&&closed!=0{rc=closed}
77 if rc==0{
78 let mode:i64=*((stat+NX_STAT_X64_MODE_OFFSET) as *i64)
79 if (mode&NX_STAT_TYPE_MASK)!=NX_STAT_REGULAR_FILE{rc=FIO_EINVAL}else{size=*((stat+NX_STAT_X64_SIZE_OFFSET) as *i64)}
80 }
81 }
82 fsx_puts(",\"fileStatus\":");fsx_putn(rc);fsx_puts(",\"fileBytes\":");fsx_putn(size);fsx_puts("}")
83 return rc
84}
85
86func la_split_evidence(source:*u8,row:*NxIdLegacyRow,bytes:i64)->i64{
87 let hex1:*u8=sys_mmap_try(TI_HEX_CH+1);let hex2:*u8=sys_mmap_try(TI_HEX_CH+1)
88 let pathbytes:i64=la_len("../_jobs")+la_len("/idem_")+TI_HEX_CH+la_len(".claim")+1
89 let path:*u8=sys_mmap_try(pathbytes);let stat:*u8=sys_mmap_try(NX_STAT_X64_BYTES)
90 if (hex1 as i64)<=0||(hex2 as i64)<=0||(path as i64)<=0||(stat as i64)<=0{return ID_RESOURCE}
91 var count:i64=0;var probes:i64=0;var errors:i64=0
92 fsx_puts(",\"claimSplitCandidates\":[")
93 if row.tabs==2{
94 let k:i64=row.legacy_key_bytes
95 var split:i64=1;var stop:i64=k
96 if row.second_tab>row.first_tab+1{split=k;stop=k+1}
97 while split<stop{
98 let p1:*u8=source+row.start;var p2:*u8=source+row.start+split
99 var n2:i64=k-split
100 if split==k{p2=source+row.first_tab+1;n2=row.second_tab-row.first_tab-1}
101 probes=probes+1
102 let a:i64=la_claim_exists(p1,split,hex1,path,stat)
103 if a!=1&&a!=0-2&&a!=FIO_EINVAL{errors=errors+1}
104 if a==1{
105 probes=probes+1
106 let b:i64=la_claim_exists(p2,n2,hex2,path,stat)
107 if b!=1&&b!=0-2&&b!=FIO_EINVAL{errors=errors+1}
108 if b==1{
109 if count>0{fsx_puts(",")};count=count+1
110 fsx_puts("{\"firstKeyBytes\":");fsx_putn(split);fsx_puts(",\"secondKeyBytes\":");fsx_putn(n2)
111 fsx_puts(",\"firstKeySha256\":");la_hash(p1,split)
112 fsx_puts(",\"secondKeySha256\":");la_hash(p2,n2)
113 fsx_puts(",\"firstClaimDigest\":\"");sys_write(1,hex1,TI_HEX_CH);fsx_puts("\"")
114 fsx_puts(",\"secondClaimDigest\":\"");sys_write(1,hex2,TI_HEX_CH);fsx_puts("\"")
115 fsx_puts(",\"firstDigestOccurrencesInRow\":");fsx_putn(la_occ(source+row.start,row.end-row.start,hex1))
116 fsx_puts(",\"secondDigestOccurrencesInRow\":");fsx_putn(la_occ(source+row.start,row.end-row.start,hex2))
117 la_output_evidence(source,bytes,hex1,path,stat,",\"firstOutput\":")
118 la_output_evidence(source,bytes,hex2,path,stat,",\"secondOutput\":")
119 fsx_puts("}")
120 }
121 }
122 split=split+1
123 }
124 }
125 fsx_puts("],\"claimProbes\":");fsx_putn(probes);fsx_puts(",\"claimProbeErrors\":");fsx_putn(errors)
126 fsx_puts(",\"legacyWriterResultMarkers\":");fsx_putn(la_occ(source+row.start,row.end-row.start,"rc=0 verb="))
127 fsx_puts(",\"legacyToolcallResultMarkers\":");fsx_putn(la_occ(source+row.start,row.end-row.start,"lane="))
128 sys_munmap_direct(hex1,TI_HEX_CH+1);sys_munmap_direct(hex2,TI_HEX_CH+1)
129 sys_munmap_direct(path,pathbytes);sys_munmap_direct(stat,NX_STAT_X64_BYTES)
130 return 0
131}
132
133
134func la_existing_key(p:*u8,n:i64,source:*u8,bytes:i64,skip:i64,view:*NxIdLegacyRow)->i64{
135 var at:i64=0
136 while at<bytes{
137 if id_legacy_row_next(source,bytes,at,view)!=1{return 0-2}
138 if at!=skip&&view.flags==0{
139 if slk_eq(p,n,source+view.legacy_key_start,view.legacy_key_bytes)==1{return at}
140 }
141 at=view.next
142 }
143 return 0-1
144}
145func la_ledger_splits(source:*u8,bytes:i64,row:*NxIdLegacyRow)->i64{
146 let view:*NxIdLegacyRow=sys_mmap_try(__size_of(NxIdLegacyRow)) as *NxIdLegacyRow
147 if (view as i64)<=0{return ID_RESOURCE}
148 var count:i64=0;var split:i64=1;fsx_puts(",\"existingNormalKeySplitCandidates\":[")
149 if row.tabs==2&&row.second_tab==row.first_tab+1{
150 while split<row.legacy_key_bytes{
151 let first:i64=la_existing_key(source+row.start,split,source,bytes,row.start,view)
152 if first>=0{
153 let second:i64=la_existing_key(source+row.start+split,row.legacy_key_bytes-split,source,bytes,row.start,view)
154 if second>=0{
155 if count>0{fsx_puts(",")};count=count+1
156 fsx_puts("{\"split\":");fsx_putn(split);fsx_puts(",\"firstEvidenceOffset\":");fsx_putn(first)
157 fsx_puts(",\"secondEvidenceOffset\":");fsx_putn(second);fsx_puts("}")
158 }
159 }
160 split=split+1
161 }
162 }
163 fsx_puts("]");sys_munmap_direct(view as *u8,__size_of(NxIdLegacyRow));return 0
164}
165
166func main(argc:i64,argv:*i64)->i64{
167 if argc!=2{return 2}
168 let ev:*NxIdEvidence=sys_mmap_try(__size_of(NxIdEvidence)) as *NxIdEvidence
169 let row:*NxIdLegacyRow=sys_mmap_try(__size_of(NxIdLegacyRow)) as *NxIdLegacyRow
170 if (ev as i64)<=0||(row as i64)<=0{return 3}
171 let key:*u8="nishi-legacy-structural-observation-t219"
172 let state:i64=id_read_evidence(argv[1] as *u8,key,la_len(key),ev)
173 if state!=ID_EVIDENCE_ERROR||ev.system_code!=FIO_EBADMSG{
174 fsx_puts("{\"observed\":false,\"reason\":\"requires-unmodified-malformed-snapshot\",\"state\":");fsx_putn(state);fsx_puts("}\n")
175 id_evidence_close(ev);sys_munmap_direct(ev as *u8,__size_of(NxIdEvidence));sys_munmap_direct(row as *u8,__size_of(NxIdLegacyRow));return 4
176 }
177 fsx_puts("{\"observed\":true,\"snapshotOnly\":true,\"bytes\":");fsx_putn(ev.bytes)
178 fsx_puts(",\"sha256\":");la_hash(ev.buffer,ev.bytes);fsx_puts(",\"anomalies\":[")
179 var start:i64=0;var count:i64=0
180 while start<ev.bytes{
181 let rc:i64=id_legacy_row_next(ev.buffer,ev.bytes,start,row)
182 if rc!=1{return 5}
183 if (row.flags&(ID_ROW_EMPTY|ID_ROW_COMMENT))==0{
184 if row.flags!=0{
185 if count>0{fsx_puts(",")};count=count+1
186 fsx_puts("{\"offset\":");fsx_putn(row.start);fsx_puts(",\"end\":");fsx_putn(row.end)
187 fsx_puts(",\"flags\":");fsx_putn(row.flags);fsx_puts(",\"tabs\":");fsx_putn(row.tabs)
188 fsx_puts(",\"rowSha256\":");la_hash(ev.buffer+row.start,row.end-row.start)
189 fsx_puts(",\"legacyKeySha256\":");la_hash(ev.buffer+row.legacy_key_start,row.legacy_key_bytes)
190 fsx_puts(",\"segments\":[");var seg:i64=row.start;var sc:i64=0
191 while seg<=row.end{
192 var end:i64=seg;while end<row.end{if ev.buffer[end]==9 as u8{break};end=end+1}
193 if sc>0{fsx_puts(",")};sc=sc+1
194 fsx_puts("{\"offset\":");fsx_putn(seg);fsx_puts(",\"bytes\":");fsx_putn(end-seg)
195 fsx_puts(",\"grammarKind\":");fsx_putn(la_kind(ev.buffer+seg,end-seg));fsx_puts("}")
196 if end==row.end{break};seg=end+1
197 }
198 fsx_puts("],\"jobRefs\":");la_jobs(ev.buffer+row.start,row.end-row.start);la_split_evidence(ev.buffer,row,ev.bytes);la_ledger_splits(ev.buffer,ev.bytes,row);fsx_puts("}")
199 }
200 }
201 start=row.next
202 }
203 fsx_puts("],\"anomalyCount\":");fsx_putn(count);fsx_puts(",\"mutationPerformed\":false}\n")
204 id_evidence_close(ev);sys_munmap_direct(ev as *u8,__size_of(NxIdEvidence));sys_munmap_direct(row as *u8,__size_of(NxIdLegacyRow))
205 return 0
206}