nx_fixshot_lib.nx source
↩ module page · 140 lines · 5966 B
1// nx_fixshot_lib.nx -- RETRIEVAL FEW-SHOTS for the autonomous fix loop (bugforge-swe-scale rung 2,
2// 2026-07-24). The episode prompt's few-shots are HARDCODED (min2/abs1) -- the harness never learns
3// from its own verified history. This lib retrieves the K most-similar (buggy TAB fixed) pairs from
4// the fixcorpus and composes them as prompt shots, so every mined/verified fix improves future
5// episodes (the cross-instance learning SOTA rung).
6//
7// CONTAMINATION GUARD (bench honesty): rows whose function NAME equals the instance under repair are
8// EXCLUDED -- retrieval may teach patterns from SIMILAR fixes, never leak the target's own answer.
9// Similarity: 4-byte shingle overlap (stride 4) between the buggy fn and each row's buggy half --
10// cheap, deterministic, no model. Rows scored 0 are never emitted (no random shots).
11// API:
12// fsl_shots(corpus, buggy, bn, selfname, dst, dstcap) -> bytes written into dst (0 = no shots;
13// caller falls back to the hardcoded shots => rule-19 additive, default behavior unchanged)
14// license_tier: ORIGINAL No hw writes (Rule 26).
15import "nx_syscalls.nx"
16
17const FSL_FCAP: i64 = 131072 // corpus read cap
18const FSL_MAXROWS: i64 = 256
19const FSL_K: i64 = 2 // shots emitted
20const FSL_SH_K: i64 = 4 // shingle length
21const FSL_SH_STRIDE: i64 = 4
22const FSL_SH_MAX: i64 = 128 // shingles fingerprinted per fn
23
24func fsl_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
25
26// count shingles of a[0,an) (stride/k pinned) that appear anywhere in b[0,bn)
27func fsl_overlap(a: *u8, an: i64, b: *u8, bn: i64) -> i64 {
28 var score: i64 = 0
29 var i: i64 = 0
30 var taken: i64 = 0
31 while i + FSL_SH_K <= an {
32 if taken >= FSL_SH_MAX { i = an }
33 else {
34 var j: i64 = 0
35 var hit: i64 = 0
36 while j + FSL_SH_K <= bn {
37 var m: i64 = 0
38 var eq: i64 = 1
39 while m < FSL_SH_K { if a[i + m] != b[j + m] { eq = 0; m = FSL_SH_K } else { m = m + 1 } }
40 if eq == 1 { hit = 1; j = bn }
41 if hit == 0 { j = j + 1 }
42 }
43 score = score + hit
44 taken = taken + 1
45 i = i + FSL_SH_STRIDE
46 }
47 }
48 return score
49}
50
51// does row-buggy rb[0,rn) belong to function `selfname`? (matches "func <selfname>(")
52func fsl_isself(rb: *u8, rn: i64, selfname: *u8) -> i64 {
53 let sl: i64 = fsl_slen(selfname)
54 if rn < sl + 6 { return 0 }
55 var i: i64 = 0
56 while i < 5 { if rb[i] != ("func " as *u8)[i] { return 0 } i = i + 1 }
57 var k: i64 = 0
58 while k < sl { if rb[5 + k] != selfname[k] { return 0 } k = k + 1 }
59 if rb[5 + sl] == (40 as u8) { return 1 }
60 return 0
61}
62
63// read the corpus, score rows, emit the top-K as "Buggy: <b>\nFixed: <f>\n" into dst.
64func fsl_shots(corpus: *u8, buggy: *u8, bn: i64, selfname: *u8, dst: *u8, dstcap: i64) -> i64 {
65 let fb: *u8 = sys_mmap(FSL_FCAP)
66 let fd: i64 = sys_openat_rd(corpus)
67 if fd < 0 { return 0 }
68 var n: i64 = 0
69 var go: i64 = 1
70 while go == 1 {
71 if n >= FSL_FCAP { sys_close(fd); return 0 }
72 let r: i64 = sys_read(fd, ((fb as i64) + n) as *u8, FSL_FCAP - n)
73 if r <= 0 { go = 0 } else { n = n + r }
74 }
75 sys_close(fd)
76 if n <= 0 { return 0 }
77 // row table: offsets of buggy-start, tab, line-end
78 let rb: *i64 = sys_mmap(FSL_MAXROWS * 8) as *i64
79 let rt: *i64 = sys_mmap(FSL_MAXROWS * 8) as *i64
80 let re: *i64 = sys_mmap(FSL_MAXROWS * 8) as *i64
81 let sc: *i64 = sys_mmap(FSL_MAXROWS * 8) as *i64
82 var nrows: i64 = 0
83 var p: i64 = 0
84 while p < n {
85 var e: i64 = p
86 var f: i64 = 0
87 while f == 0 { if e >= n { f = 1 } else { if fb[e] == (10 as u8) { f = 1 } else { e = e + 1 } } }
88 if e > p { if nrows < FSL_MAXROWS {
89 var tb: i64 = 0 - 1
90 var q: i64 = p
91 var tf: i64 = 0
92 while tf == 0 { if q >= e { tf = 1 } else { if fb[q] == (9 as u8) { tb = q; tf = 1 } else { q = q + 1 } } }
93 if tb > p {
94 rb[nrows] = p
95 rt[nrows] = tb
96 re[nrows] = e
97 let self: i64 = fsl_isself(((fb as i64) + p) as *u8, tb - p, selfname)
98 if self == 1 { sc[nrows] = 0 - 1 }
99 if self == 0 { sc[nrows] = fsl_overlap(buggy, bn, ((fb as i64) + p) as *u8, tb - p) }
100 nrows = nrows + 1
101 }
102 } }
103 p = e + 1
104 }
105 // emit top-K by score (>0), simple selection
106 var o: i64 = 0
107 var kk: i64 = 0
108 while kk < FSL_K {
109 var best: i64 = 0 - 1
110 var bs: i64 = 0
111 var i2: i64 = 0
112 while i2 < nrows { if sc[i2] > bs { bs = sc[i2]; best = i2 } i2 = i2 + 1 }
113 if best < 0 { kk = FSL_K }
114 else {
115 sc[best] = 0 - 1
116 let blen: i64 = rt[best] - rb[best]
117 let flen: i64 = re[best] - rt[best] - 1
118 if o + blen + flen + 24 < dstcap {
119 let bg: *u8 = "Buggy: " as *u8
120 var z: i64 = 0
121 while bg[z] != (0 as u8) { dst[o] = bg[z]; o = o + 1; z = z + 1 }
122 // storage v2: byte 0x01 encodes a newline inside a multi-line fn -- unescape on emit
123 var x: i64 = rb[best]
124 while x < rt[best] { var cb: u8 = fb[x]; if cb == (1 as u8) { cb = 10 as u8 } dst[o] = cb; o = o + 1; x = x + 1 }
125 dst[o] = 10 as u8
126 o = o + 1
127 let fx: *u8 = "Fixed: " as *u8
128 var z2: i64 = 0
129 while fx[z2] != (0 as u8) { dst[o] = fx[z2]; o = o + 1; z2 = z2 + 1 }
130 var y: i64 = rt[best] + 1
131 while y < re[best] { var cf: u8 = fb[y]; if cf == (1 as u8) { cf = 10 as u8 } dst[o] = cf; o = o + 1; y = y + 1 }
132 dst[o] = 10 as u8
133 o = o + 1
134 }
135 kk = kk + 1
136 }
137 }
138 dst[o] = 0 as u8
139 return o
140}