Commit d41b05e
Enforce neural-boundary numeric provenance guardrail (P0) (#37)
**Makes the flagship guarantee real.** "No number is ever produced by
the LLM" was, until now, prompt-only. This adds code-level enforcement
plus the boundary hardening that guarantee depends on.
## What changed
- **NEW `src/tools/guardrail.jl`** — `collect_numbers` (recursive
harvest of every number a tool call produced, skipping Bool flags),
`extract_numeric_tokens`, and `validate_numeric_provenance(text,
tool_results, user_numbers; rtol)`. A numeric literal in the assistant's
prose is legitimate iff it matches (within rtol, or after
display-rounding) a harvested tool-result number, its ÷100/×100 percent
variant, a user-supplied number, or is a small structural integer (0–12:
df, counts, indices). Anything else is an **orphan** (a likely mollock).
- **`chat.jl`** — records tool results per turn; runs the guardrail on
the final assistant content; on orphans, retries **once** (restate using
only tool-derived numbers) then appends a visible warning block. Also
flags numeric answers given with **no** tool call. **Never rewrites the
model's text — flags, never fabricates.** REPL turn body wrapped in
try/catch so one bad turn can't kill the session.
- **`lmstudio.jl` `process_tool_calls`** — per-tool-call try/catch →
clean `role:"tool"` error payload (recover instead of crash); **forwards
the previously-dropped `tools`** on the follow-up call; bounded
multi-round loop (max 5) so compound queries actually chain. HTTP
`connect_timeout=10, readtimeout=120, retry=false`.
- **`executor.jl`** — clamps `n_reps`/`n_permutations` ≤ 100 000 and `k`
≤ 20; trailing `else` "Unknown type" guard on all 21 non-exhaustive
inner sub-type dispatches (kills the silent-`null`→invented-number
path); catch-all backtrace gated behind `STATISTIKLES_DEBUG`.
- **NEW `test/guardrail_test.jl`** (wired into `runtests.jl`) — clean
fixture passes; **injected-fabrication fixture is flagged** (`r=0.87`,
`t=3.14159` orphaned while the real `42.73` is not); malformed-tool-call
recovery (no HTTP); clamps; unknown-sub-type guards.
## Verification
Full suite in WSL (Julia 1.10.11, serialized under flock): **4484 / 4484
passing** — 4404 baseline unchanged + 80 new guardrail tests. Zero
regressions.
## Notes / deviations
- The timeout-pattern reference file is `src/bridge/echidna_adapter.jl`
(not `src/tools/`); exact kwargs from the spec were used regardless.
- `k`-clamp applied to the three params literally named `k`
(bayesian_em, ica, topic_modeling_nmf); `pca`'s `n_components` left
unclamped (bounded by data dimensionality).
- `else`-guards added to all non-exhaustive inner dispatches;
already-exhaustive ones (chi_square, correlation, regression) left
alone.
Part of the production-readiness wave-1 remainder (W1-1, the P0).
Implemented by an Opus agent, diff reviewed against the guardrail spec.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>1 parent b92dd09 commit d41b05e
7 files changed
Lines changed: 568 additions & 48 deletions
File tree
- src
- tools
- test
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
101 | 101 | | |
102 | 102 | | |
103 | 103 | | |
| 104 | + | |
104 | 105 | | |
105 | 106 | | |
106 | 107 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
101 | 103 | | |
102 | | - | |
103 | | - | |
| 104 | + | |
| 105 | + | |
104 | 106 | | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
110 | 112 | | |
111 | | - | |
112 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
113 | 120 | | |
114 | | - | |
115 | | - | |
116 | 121 | | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
117 | 128 | | |
118 | 129 | | |
119 | 130 | | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
120 | 134 | | |
121 | 135 | | |
122 | 136 | | |
123 | 137 | | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
124 | 202 | | |
125 | 203 | | |
126 | 204 | | |
| |||
201 | 279 | | |
202 | 280 | | |
203 | 281 | | |
204 | | - | |
| 282 | + | |
| 283 | + | |
205 | 284 | | |
206 | 285 | | |
207 | 286 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
100 | 102 | | |
101 | 103 | | |
102 | 104 | | |
| 105 | + | |
103 | 106 | | |
104 | 107 | | |
105 | 108 | | |
| |||
113 | 116 | | |
114 | 117 | | |
115 | 118 | | |
| 119 | + | |
| 120 | + | |
116 | 121 | | |
117 | 122 | | |
118 | 123 | | |
| |||
122 | 127 | | |
123 | 128 | | |
124 | 129 | | |
| 130 | + | |
| 131 | + | |
125 | 132 | | |
126 | 133 | | |
127 | 134 | | |
| |||
132 | 139 | | |
133 | 140 | | |
134 | 141 | | |
| 142 | + | |
| 143 | + | |
135 | 144 | | |
136 | 145 | | |
137 | 146 | | |
| |||
147 | 156 | | |
148 | 157 | | |
149 | 158 | | |
| 159 | + | |
| 160 | + | |
150 | 161 | | |
151 | 162 | | |
152 | 163 | | |
| |||
162 | 173 | | |
163 | 174 | | |
164 | 175 | | |
| 176 | + | |
| 177 | + | |
165 | 178 | | |
166 | 179 | | |
167 | 180 | | |
| |||
170 | 183 | | |
171 | 184 | | |
172 | 185 | | |
| 186 | + | |
| 187 | + | |
173 | 188 | | |
174 | 189 | | |
175 | 190 | | |
| |||
180 | 195 | | |
181 | 196 | | |
182 | 197 | | |
| 198 | + | |
| 199 | + | |
183 | 200 | | |
184 | 201 | | |
185 | 202 | | |
| |||
189 | 206 | | |
190 | 207 | | |
191 | 208 | | |
192 | | - | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
193 | 214 | | |
194 | 215 | | |
195 | 216 | | |
| |||
199 | 220 | | |
200 | 221 | | |
201 | 222 | | |
| 223 | + | |
| 224 | + | |
202 | 225 | | |
203 | 226 | | |
204 | 227 | | |
205 | 228 | | |
206 | 229 | | |
207 | | - | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
208 | 235 | | |
209 | 236 | | |
210 | 237 | | |
211 | | - | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
212 | 241 | | |
213 | 242 | | |
214 | 243 | | |
| |||
219 | 248 | | |
220 | 249 | | |
221 | 250 | | |
| 251 | + | |
| 252 | + | |
222 | 253 | | |
223 | 254 | | |
224 | 255 | | |
| |||
228 | 259 | | |
229 | 260 | | |
230 | 261 | | |
| 262 | + | |
| 263 | + | |
231 | 264 | | |
232 | 265 | | |
233 | 266 | | |
| |||
237 | 270 | | |
238 | 271 | | |
239 | 272 | | |
| 273 | + | |
| 274 | + | |
240 | 275 | | |
241 | 276 | | |
242 | 277 | | |
| |||
250 | 285 | | |
251 | 286 | | |
252 | 287 | | |
| 288 | + | |
| 289 | + | |
253 | 290 | | |
254 | 291 | | |
255 | 292 | | |
256 | 293 | | |
257 | 294 | | |
258 | 295 | | |
| 296 | + | |
| 297 | + | |
259 | 298 | | |
260 | 299 | | |
261 | 300 | | |
| |||
282 | 321 | | |
283 | 322 | | |
284 | 323 | | |
| 324 | + | |
| 325 | + | |
285 | 326 | | |
286 | 327 | | |
287 | 328 | | |
288 | 329 | | |
289 | 330 | | |
290 | 331 | | |
291 | 332 | | |
| 333 | + | |
292 | 334 | | |
293 | 335 | | |
294 | 336 | | |
| |||
324 | 366 | | |
325 | 367 | | |
326 | 368 | | |
| 369 | + | |
| 370 | + | |
327 | 371 | | |
328 | 372 | | |
329 | 373 | | |
| |||
390 | 434 | | |
391 | 435 | | |
392 | 436 | | |
| 437 | + | |
| 438 | + | |
393 | 439 | | |
394 | 440 | | |
395 | 441 | | |
| |||
413 | 459 | | |
414 | 460 | | |
415 | 461 | | |
| 462 | + | |
| 463 | + | |
416 | 464 | | |
417 | 465 | | |
418 | 466 | | |
| |||
435 | 483 | | |
436 | 484 | | |
437 | 485 | | |
| 486 | + | |
| 487 | + | |
438 | 488 | | |
439 | 489 | | |
440 | 490 | | |
441 | 491 | | |
442 | 492 | | |
443 | 493 | | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
448 | 501 | | |
449 | 502 | | |
0 commit comments