edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching0 forksRust370.3 MiBb9fb499Tahinli2 months ago3776 commits
| 1 | #!/usr/bin/env python3 |
| 2 | """pin-gate-audit.py — census of the gates that read a PINNED fixture. |
| 3 | |
| 4 | The r1 invariant was too weak: it matched only single-name call shapes |
| 5 | (`crate_pin("X")`, `pin_dir().join("X")`). It is blind to a much worse shape |
| 6 | that exists in the crate right now -- a gate that reads N pins through a |
| 7 | DIRECTORY literal plus a runtime-formatted name list: |
| 8 | |
| 9 | let fixtures = concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures"); |
| 10 | ["warp-mismatch", "warp-flake-5", ...].iter().map(|n| format!("{fixtures}/{n}.obu")) |
| 11 | |
| 12 | Fourteen names, none committed under the crate, so no clean tree and no runner |
| 13 | can satisfy them, and the gate is #[ignore]d -- a no-op that reports nothing. |
| 14 | A preflight that passes it is the silent shape again. |
| 15 | |
| 16 | This audit, for every `#[test]` fn in crates/*/src and crates/*/tests that |
| 17 | reaches for a fixture: |
| 18 | |
| 19 | * resolves the fixture ROOT literal (concat! or Path::new(env!).join chain) |
| 20 | * enumerates every runtime name built from that root |
| 21 | - a `[..].iter().map(|n| format!("{var}/{n}.obu"))` name list |
| 22 | - a `format!` naming the root variable directly |
| 23 | * for each name, checks the path under the ROOT (gitignored, so never tracked) |
| 24 | and under crates/<crate>/fixtures/ (the committed copy) |
| 25 | * counts pin-reading gates, how many resolve through a committed copy, and how |
| 26 | many do not -- printed as numbers, not prose |
| 27 | |
| 28 | ASSERTION CLASSIFICATION uses the helper CALL GRAPH, not the gate body: a gate |
| 29 | that delegates to a helper (the warp gate calls check_pinned_warp_stream, which |
| 30 | asserts all three planes per frame) is NOT a no-assert gate, and reading only the |
| 31 | body gets that wrong -- which is exactly the mistake that produced the |
| 32 | "asserts nothing" classification earlier today. |
| 33 | |
| 34 | Output (one record per line, tab separated fields): |
| 35 | GATE <file>:<line>\t<fn>\t<root-kind>\t<asserts:yes|no|via-helper NAME>\t<ignored:yes|no>\t<names> |
| 36 | NAME <file>:<line>\t<gate-fn>\t<name>\t<committed-copy:present|absent>\t<tracked:yes|no|->\t<exists-under-root:yes|no> |
| 37 | COUNT total=<n>\tcommitted=<n>\tuncommitted=<n>\tignored=<n>\tassertless=<n> |
| 38 | |
| 39 | Exit 0 always: the caller decides what is a failure. Set PIN_AUDIT_STRICT=1 to |
| 40 | exit 1 when any name lacks a tracked committed copy. |
| 41 | """ |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | = or |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | = |
| 56 | |
| 57 | continue |
| 58 | |
| 59 | |
| 60 | yield , |
| 61 | |
| 62 | |
| 63 | |
| 64 | """Yield (start_line, name, body, preceding_attrs) for every fn. |
| 65 | |
| 66 | The body runs from the `fn` line to the NEXT `fn` line rather than to a |
| 67 | brace-balanced end: brace counting over 39k lines of Rust miscounts the |
| 68 | moment one string literal or macro contains an unbalanced brace, and a |
| 69 | single bad walk swallows every gate after it. Over-capturing trailing |
| 70 | attributes of the next fn is harmless here -- attributes are read from the |
| 71 | lines immediately above the `fn` line, which this method never crosses. |
| 72 | """ |
| 73 | = |
| 74 | = |
| 75 | |
| 76 | = |
| 77 | |
| 78 | |
| 79 | |
| 80 | = |
| 81 | = |
| 82 | = - 1 |
| 83 | |
| 84 | |
| 85 | -= 1 |
| 86 | yield + 1, , , |
| 87 | |
| 88 | |
| 89 | = |
| 90 | = |
| 91 | |
| 92 | |
| 93 | |
| 94 | """Blank [lo, hi) in place, preserving newlines so line numbers survive.""" |
| 95 | |
| 96 | |
| 97 | = |
| 98 | |
| 99 | |
| 100 | |
| 101 | """Blank the INTERIOR of every string literal that SPANS LINES. |
| 102 | |
| 103 | THE RULE: a match inside a string literal that spans lines is not a gate. |
| 104 | Rust has no line-spanning non-raw string, so a literal containing a newline |
| 105 | is EMBEDDED SOURCE -- the input to a scanner's own capability test, or a |
| 106 | doc blob. `pin_inventory_tests` holds exactly that: a `r#"..."#` whose body |
| 107 | is a synthetic gate reproducing the forbidden `/../../fixtures` root, fed |
| 108 | to `pins_read` as DATA. Read as code it is a live violation, and it made |
| 109 | this audit red on a tree with no real violation in it -- the audit reading |
| 110 | its own test data, the mirror of the class the same script already fixed |
| 111 | for prose ("a doc comment reproducing a root literal invented a gate"). |
| 112 | |
| 113 | Why this rule and not a path allowlist: a path allowlist is a second list |
| 114 | of places to remember, and the next scanner test lands somewhere else. |
| 115 | The property that actually separates the two cases is structural -- a real |
| 116 | pin read names a path in a single-line literal; only test data wraps Rust |
| 117 | source across lines -- and that property is checkable, not a location. |
| 118 | |
| 119 | Why it cannot hide a real violation: a non-raw literal aborts on the first |
| 120 | newline, so it can only ever blank within one line, and a one-line literal |
| 121 | holds no Rust source. A char literal is matched only when it really closes |
| 122 | within three characters, so a LIFETIME (`&'a str`) is not mistaken for the |
| 123 | start of a string and cannot swallow the rest of a line. |
| 124 | """ |
| 125 | = |
| 126 | , = 0, |
| 127 | |
| 128 | = |
| 129 | |
| 130 | = + |
| 131 | = |
| 132 | |
| 133 | = |
| 134 | continue |
| 135 | |
| 136 | |
| 137 | = + |
| 138 | continue |
| 139 | |
| 140 | = + 1 |
| 141 | = None |
| 142 | |
| 143 | = |
| 144 | |
| 145 | += 2 |
| 146 | continue |
| 147 | |
| 148 | = |
| 149 | break |
| 150 | |
| 151 | break |
| 152 | += 1 |
| 153 | |
| 154 | = + 1 |
| 155 | |
| 156 | += 1 |
| 157 | continue |
| 158 | = |
| 159 | = |
| 160 | return |
| 161 | |
| 162 | |
| 163 | |
| 164 | """Comment-stripped, embedded-source-masked body -- the thing the shapes |
| 165 | are matched against. Comment stripping removes PROSE reproducing a shape; |
| 166 | this removes SOURCE reproducing a shape.""" |
| 167 | = |
| 168 | = |
| 169 | return |
| 170 | |
| 171 | |
| 172 | |
| 173 | """Read a Rust source with every line-spanning string literal's interior |
| 174 | blanked. The mask has to happen at FILE level, not per body: `fn_bodies` |
| 175 | finds `fn` lines by regex, so a synthetic gate inside a test's `r#"..."#` |
| 176 | input is picked up as if it were a real fn, and its body -- the raw |
| 177 | string's own text -- is then scanned as CODE. Masking first means that |
| 178 | region is blank before any gate attribution happens, which is the only |
| 179 | place the distinction can be made.""" |
| 180 | |
| 181 | return |
| 182 | |
| 183 | |
| 184 | = |
| 185 | |
| 186 | |
| 187 | = |
| 188 | |
| 189 | |
| 190 | |
| 191 | = |
| 192 | = |
| 193 | = |
| 194 | = |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | return |
| 200 | = |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | return |
| 206 | |
| 207 | |
| 208 | |
| 209 | = |
| 210 | |
| 211 | |
| 212 | |
| 213 | """A POSITIVE CONTROL for the census itself, in the exact shape that cost a |
| 214 | gate: a doc comment reproducing the forbidden root literal, sitting between |
| 215 | two gates, one of which reads crate_pin. |
| 216 | |
| 217 | It runs the REAL pipeline -- a synthetic crate written into the tree, the |
| 218 | normal scan, the normal records -- because a control that re-implements the |
| 219 | decision order would pass while the real path is broken, which is the failure |
| 220 | this control exists to catch. Losing a gate to a comment is SILENT: the |
| 221 | census count drops and nothing goes red, so only a control that asserts the |
| 222 | count can see it. |
| 223 | """ |
| 224 | # Build the synthetic tree in a temp dir and point the scan at it, so the |
| 225 | # control runs even where the staged tree is read-only. |
| 226 | |
| 227 | = |
| 228 | = |
| 229 | = |
| 230 | = |
| 231 | |
| 232 | = |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | = |
| 274 | |
| 275 | |
| 276 | |
| 277 | = |
| 278 | |
| 279 | = |
| 280 | = |
| 281 | |
| 282 | |
| 283 | continue |
| 284 | |
| 285 | continue |
| 286 | = |
| 287 | = |
| 288 | = |
| 289 | |
| 290 | = |
| 291 | = |
| 292 | |
| 293 | = |
| 294 | = |
| 295 | = + \ |
| 296 | |
| 297 | = not and not and not and not |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | return 0 |
| 307 | |
| 308 | = |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | = # fn name -> body (all fns in the tree) |
| 315 | |
| 316 | = |
| 317 | |
| 318 | |
| 319 | |
| 320 | = = = = = 0 |
| 321 | = |
| 322 | |
| 323 | |
| 324 | = |
| 325 | = |
| 326 | |
| 327 | |
| 328 | continue |
| 329 | # Match every shape on a COMMENT-STRIPPED body. A doc comment that |
| 330 | # reproduces a root literal to explain the shape it removed used to |
| 331 | # invent a gate here, the same blindness invariant 1 had. |
| 332 | = |
| 333 | = None |
| 334 | = |
| 335 | = None |
| 336 | = |
| 337 | |
| 338 | , = , None |
| 339 | = |
| 340 | |
| 341 | = |
| 342 | |
| 343 | = |
| 344 | = |
| 345 | = |
| 346 | |
| 347 | # The single-name shapes the FIRST invariant matched, kept so |
| 348 | # the census counts every pin-reading gate, not only the ones |
| 349 | # reached through a root literal. |
| 350 | = |
| 351 | = |
| 352 | # A gate that builds the pin name at runtime (crate_pin(&format!(..))) |
| 353 | # matches no single-name shape and would VANISH from the census -- |
| 354 | # the same blind spot in a new place. Surface it instead. |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | += |
| 362 | |
| 363 | |
| 364 | continue |
| 365 | += 1 |
| 366 | |
| 367 | += 1 |
| 368 | |
| 369 | |
| 370 | |
| 371 | = |
| 372 | = |
| 373 | = |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | += 1 |
| 379 | |
| 380 | += 1 |
| 381 | |
| 382 | |
| 383 | continue |
| 384 | |
| 385 | # the variable the root is bound to, if any |
| 386 | = |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | = |
| 393 | |
| 394 | # names: ONLY the two shapes that matter. |
| 395 | # (i) a runtime NAME LIST formatted against the root variable -- |
| 396 | # the shape the weak invariant was blind to |
| 397 | # (ii) a single file literal appended to the root |
| 398 | # Anything else (a format! over some other variable, a scratch dump |
| 399 | # path) is not a pin read and is left to the manifest scan. |
| 400 | = |
| 401 | |
| 402 | # The name list is bracketed, but comments sit between its |
| 403 | # items ("lane-rect r2: HORP strip ..."), so strip line |
| 404 | # comments before parsing the list and anchor on the format! |
| 405 | # that consumes it. It is `code` and not `body` that feeds |
| 406 | # this: `code` is the comment-stripped, embedded-source-masked |
| 407 | # text, and reading the RAW body here re-imported the shape |
| 408 | # out of a scanner test's own `r#"..."#` input -- the same |
| 409 | # gate resurrected through a different match. |
| 410 | = |
| 411 | |
| 412 | = |
| 413 | |
| 414 | continue |
| 415 | = |
| 416 | |
| 417 | continue |
| 418 | = or |
| 419 | |
| 420 | |
| 421 | |
| 422 | = |
| 423 | # a lane dump under lanes/ is not a fixture and is not this gate |
| 424 | |
| 425 | |
| 426 | |
| 427 | continue |
| 428 | |
| 429 | # assertion classification through the CALL GRAPH |
| 430 | = in |
| 431 | = |
| 432 | |
| 433 | |
| 434 | = |
| 435 | |
| 436 | = True |
| 437 | = |
| 438 | break |
| 439 | = |
| 440 | |
| 441 | continue |
| 442 | += 1 |
| 443 | |
| 444 | += 1 |
| 445 | |
| 446 | += 1 |
| 447 | = |
| 448 | |
| 449 | = + |
| 450 | |
| 451 | |
| 452 | = |
| 453 | = |
| 454 | = |
| 455 | |
| 456 | = |
| 457 | = |
| 458 | = |
| 459 | = \ |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 | |
| 466 | |
| 467 | += 1 |
| 468 | |
| 469 | += 1 |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | # a bare directory root feeding a runtime name list can never be |
| 475 | # satisfied by a committed tree |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | return 1 |
| 490 | return 0 |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 |