Issue #2646: The walk past n loops in a row costs n walks, not 2^n, with a proof that every result stays the same

Open and answer this issue on GitHub.

ledger, blocked, ci · opened 2026-11-21 · updated 2026-11-29 · 12 comments

Blocks “The corpus-wide ledger tests are 4-6x slower in CI since the panics inference goes through loops” (#2474). Found by its third run (docs/briefs/ci/issue-2474-d3.md.run1.report.md).

Facts: Ledger and bounds proofs

What happens today

bounding_past_loop (crates/nikaia-std/src/compiler/bounds_walk.nika) walks the statements after a loop twice: leaves from the facts past the loop, and, where the loop’s invariant settles, again from the facts with the invariant; again stands only where it proves more. Each of the two walks meets the later loops and walks past each of them twice again, so a run of n loops costs 2^n walks of what follows.

Measured (counter in the lowered bounds_walk.rs, check --bounds), N loops in a row, each with its own counter (let mut iK = lo; for j in lo..<hi { if a[j] < K { iK = iK + 1 } }; out.push(a[iK])):

Two routes tried, neither enough:

Decision

Supervisor, 2026-11-21: the owner’s rule stands, “exactly, not probably”: no cut lands that is only measured identical. The cut lands with a proof - an argument in the report, by the code, that for every program it returns exactly the proofs the double walk returns - and a test that pins the shapes where it matters. This keeps every proof, so it is not the owner’s trade; if no exact cut is found, the run stops (Result: blocked) and the question whether the walk past loops may change what it proves goes to the owner as a decision paper.

Candidates, in order:

  1. Close run 2’s gap. The trial misses only where a later loop’s invariant settles under the first loop’s facts and not without them. Detect exactly that (the later loop’s search is asked from the facts with the invariant wherever its entry facts differ from the trial’s in a name it changes) instead of not asking it; prove the trial with that rule returns what the full again returns.
  2. One walk from the stronger facts. If the walk is monotone in its entry facts (more facts, never fewer proofs), again proves at least what leaves proves, and leaves is needed only to find the wanted names. Show monotonicity in the code (or name what breaks it - a budget, poisoned, written - which is then its own finding), and find wanted without the full recursive leaves.

To build

  1. Read bounding_past_loop, bounding_loop_invariant, bounding_stmts and run 2’s patch; write the argument for the chosen candidate in the report first. If neither can be proved, stop as the Decision says, with the case that breaks it as a probe.
  2. Build it in bounds_walk.nika (Nikaia; the lowered file is output).
  3. Check it as well as prove it: run 2’s counter over std (every D10 decision: the same proofs), and the 5 shapes of run 2.
  4. Add a test in tests/compiler/tests/ with a .stdout: several loops in a row and nested, including a later loop whose invariant needs the first loop’s, whose results are the same as before.
  5. Measure with callgrind: Ledger::infer on packages/logic/src/sv_strategy.nika and on benches/lowering/source.nika.

Done when

Files and limits

Comments

keywan-ghadami, 2026-11-21

Brief queued: issue-2646


agent: builder model: opus max_turns: 200 budget_usd: 30 effort: medium —

Issue #2646

Your work order is issue #2646, inlined below with its comments. There is no brief written around it: plan your own steps from its To build list, build them in order, commit after each finished step, and end when its Done when holds (or at the limits of builder.md, reporting what is left). The facts in your prompt are Core of docs/builder-facts.md; more on a topic: nikaia run scripts/syntax.nika -- KEYWORD, a probe: nikaia run scripts/probe.nika -- SNIPPET.

Measure the issue against docs/issues.md while you work: what it lacked, what was wrong in it. That answer goes into the report’s Issue: field. A finding, a changed plan or a finished item also goes into the issue as a comment (gh api repos/Nikaia-Foundation/Nikaia/issues/2646/comments -F body=@file, ending with the Claude Code footer), rule I13.

Rules (CLAUDE.md; shared checkout)

Report (at most 15 lines, to exactly /home/user/Nikaia/docs/briefs/ci/issue-2646.md.report.md)

Fields: Result, Files, Commit, Body (optional), Fixes, Pushed, Tests, Gate, Deviation, Found, Needs, Changelog, Issue, Owner (a question for the owner), Comment (#N <text>, what to say on another issue; the driver posts it, one line each), Friction, Disk freed, and last Attribution: with your commit trailer lines below it.


Generated by Claude Code

keywan-ghadami, 2026-11-21

Run: issue-2646, Result: blocked

Tokens: input 90, cache read 3412183, cache write 97976, output 37734 Cost: 2.27 USD, time 0 min 23 s, turns 45

Report

Result: blocked Files: none Commit: none Body: No exact cut found, so per the Decision nothing is committed; the decision paper for the owner is docs/briefs/ci/issue-2646-decision.md (options A-D, recommendation C). Candidate 1 (run 2’s trial) is refuted by a probe; candidate 1 refined and candidate 2 (monotonicity) are refuted from the code of bounds_walk.nika. The gap was found with an env-gated detector (thread-local trial flag; NIKAIA_D10=check runs the trial, then the full again, and prints D10GAP where the full again stands but its trial does not) patched into the lowered bounds_walk.rs and built once. The lowered file was then restored by copy (md5 9b4560cf… matches HEAD) and target/fast rebuilt clean. Fixes: none Pushed: none Tests: 2026-11-21 f03819a5e, scratch detector build: the probe in Needs gives 8 D10GAP at byte 161; packages/logic/src/sv_strategy.nika has 4063 past-loop walks and 0 gaps (20 s); crates/nikaia-std/src/compiler/libraries.nika: 11654 walks, 0 gaps before the 900 s timeout cut it off (bounds_walk.nika and ledger.nika were not reached: the background limit stopped the run). No test run, nothing changed Gate: green Deviation: To build 2-5 not built: the Decision stops the run when no exact cut can be proved (step 1’s argument fails for both candidates) Found: none Needs: owner decision: may the walk past loops (bounding_past_loop, ADR-361 D10) prove fewer checks than today in rare shapes, so that n loops in a row cost linear walks instead of 2^n? Paper: docs/briefs/ci/issue-2646-decision.md. Candidate 1 (run 2’s trial) loses a proof: let mut k = lo; let mut s = lo; let mut t = lo; while t < hi { t = t + 1; if a[t - 1] > 0 { s = s + 1 }; if a[t - 1] > 5 { k = s } }; while t < a.len() { t = t + 1; if a[t - 1] > 2 { s = s + 1 }; if a[t - 1] > 7 { k = s } }; return a[s - k] (fn chained(a: ref Vec[i64], lo: i64, hi: i64) -> i64 with assert(0 < lo), assert(lo <= hi), assert(hi <= a.len())): s - k proved today, s - k checked: a counter the loop on line 8 changes ... (ADR-361 D13, #1634) under the trial, because the second loop’s invariant settles only from the first loop’s invariant. Candidate 1 refined (ask a later loop that changes a differing name) keeps that shape but has no proof for the loops it skips: the reach check bounding_proves asks bounding_known’s 48 nearest of up to 256 held facts, and a candidate may read names the loop leaves alone. Candidate 2 fails because the walk is not monotone in its entry facts: bounding_hold evicts and most_facts outranks; again starts from invariant.past, not a superset of leaves’ facts; it stands by count, not inclusion; and the state outside BoundsMark carries over between walks Changelog: none Issue: wrong I7: the Decision expected one of its two candidates to be provable, but the walk is not monotone (most_held/most_facts caps, the count-based stand rule, state outside BoundsMark), and run 2’s gap holds in a 2-loop probe Owner: may ADR-361 D10’s walk past loops prove fewer checks than the double walk in shapes like the probe in Needs (a later loop whose invariant settles only from an earlier loop’s invariant), in exchange for linear cost? Recommended: run 2’s trial plus a later loop asked through in the trial wherever it changes a name the earlier invariant reads, with the probe pinned as a test Comment: #2474 #2646’s run (2026-11-21, f03819a5e) found no exact cut: run 2’s trial loses s - k in a 2-loop probe where the second loop’s invariant needs the first loop’s (paper docs/briefs/ci/issue-2646-decision.md); this now waits for an owner decision on whether D10 may prove fewer checks for linear cost Friction: checking whether a cut changes results needed a hand-patched lowered bounds_walk.rs, a build and a restore in the shared checkout, because no --bounds mode compares the full D10 walk against a trial Disk freed: target/nikaia-project-tests absent (nothing cached by this run, no entry over 100 MB); / 6.8G free of 252G (83 %); scratch detector binary removed Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDtmukJNapbMLmZMzTV

Debrief

  1. Most time: finding an example that breaks run 2’s trial. Four of them (gap3, gap4, gap5, gap7) were proved without any search, by the per-turn step rule, or were never proved at all. The background detector run over the compiler files hit its 900 s timeout after one file.
  2. Looked up: which rules outside the invariant search prove a loop (the per-turn bounds, relative steps, the #1470 counter rule). Also the fact caps (most_held 256, most_facts 48), and that --bounds-trace LINE shows a loop’s search rows only at the loop’s own line.
  3. Checks: useful were the env-gated detector build (one build lets off, check and trial run side by side), the gap6 example, and sv_strategy (20 s, 0 gaps). Wasted were the shapes that never searched an invariant, and starting three compiler files in a row: one check of a std file ran past 900 s.
  4. One change: the issue should say plainly that a refutation by example is the expected result of step 1. It should also give the detector recipe: a thread-local trial flag plus a D10GAP line in the lowered bounds_walk.rs, behind an env var.
  5. Issue:
    • wrong I7: the Decision expected one of the two candidates to be provable. The code is not monotone: fact caps, a count-based stand rule, and state outside BoundsMark that carries between walks.
    • rules-gap: no rule asks an issue that demands a proof to say first whether the code’s shape allows one. Here the caps and the count rule rule it out.
    • knowledge: Ledger and bounds proofs | an invariant search is needed only where the per-turn steps and relatives rules cannot state the relation, e.g. a reset like k = s; shapes where a counter only steps are proved without one.
  6. Fact: Ledger and bounds proofs | a scratch counter or flag in the lowered bounds_walk.rs must be thread_local, not a static atomic, because the ledger's first search runs in parallel. Fact: Ledger and bounds proofs | nikaia check on one crates/nikaia-std/src/compiler/*.nika file with a D10 detector runs past 900 s; use packages/logic files for whole-file checks.
  7. Fact: Ledger and bounds proofs | 2026-11-21 f03819a5e: check packages/logic/src/sv_strategy.nika makes 4063 past-loop walks in 20 s; run 2's trial misses 0 proofs there. Fact: Ledger and bounds proofs | 2026-11-21 f03819a5e: libraries.nika made 11654 past-loop walks, 0 trial misses, before a 900 s timeout cut it off. Fact: Ledger and bounds proofs | the example that breaks run 2's trial, and the options A-D for the owner, are in docs/briefs/ci/issue-2646-decision.md.

Generated by Claude Code

keywan-ghadami, 2026-11-21

Driver: not queued, this issue waits for #1634, which is still open (#1819). Set ready again when it is closed.


Generated by Claude Code

keywan-ghadami, 2026-11-21

Supervisor, 2026-11-21: run 1 is blocked. No exact cut was found; the report is docs/briefs/ci/issue-2646.md.run1.report.md. The question whether the walk past loops may prove fewer checks now goes to the owner as “Decision: may the walk past loops prove fewer checks in rare shapes, in exchange for a cost linear in the loops?” (#2652). This issue waits for that decision. The driver’s “waits for #1634” was a misread: #1634 is the ADR-361 D13 reference in the probe’s message, not a blocker.


Generated by Claude Code

keywan-ghadami, 2026-11-21

Run 2: a measurement only (owner, 2026-11-21, on #2652). This comment replaces this issue’s To build and Done when for this run. Commit no change to the walk.

Option E: per function, run the flat trial of run 1 (candidate 1). If every check of the function stands, keep that result. Otherwise walk the function again with today’s double walk (bounding_past_loop, ADR-361 D10) and keep the full walk’s result. Option E’: like E, but walk again only when an unproved check lies after a loop where the trial cut the full walk.

To build

  1. Gate E and E’ behind an env variable, as run 1 did with NIKAIA_D10=check: a patch to the lowered bounds_walk.rs, built once. Restore it afterwards by copy, check the md5 against HEAD, and rebuild target/fast clean.
  2. Measure A (today), E and E’ with deterministic counts: the number of walks per function and in total, and callgrind instruction counts (docs/measuring.md; never time) on cases small enough for callgrind. Cases:
    • the corpus ledger tests of #2474, rs2nika_free_methods included (walk counts where callgrind is too slow);
    • sv_strategy, libraries.nika and std;
    • the 10-loop probe;
    • the fn chained probe of docs/briefs/ci/issue-2646-decision.md.
  3. For each case, count the functions that needed the rerun, and why: a check that stays checked under A as well, or a gap of the trial.
  4. Diff the proved/checked results of E and E’ against A. Every difference is reported, and none may lose a proof. List separately the checks where the trial proves more than A (functions the trial proved in full).

Done when


Generated by Claude Code

keywan-ghadami, 2026-11-21

Driver: not queued, this issue waits for #1634, which is still open (#1819). Set ready again when it is closed.


Generated by Claude Code

keywan-ghadami, 2026-11-21

Back to ready. The driver held this issue on #1634 by mistake: that number only appears inside the backtick probe in the run-1 report’s Needs: owner decision. The driver commit “drive: a #N a report only quotes holds no issue up” (15c267871) makes it ignore numbers inside quotes, and the driver’s appended Needs: #1634 line is gone from the report. Run 2 is the measurement order in the comment above.


Generated by Claude Code

keywan-ghadami, 2026-11-21

Run: issue-2646, Result: done

Tokens: input 138, cache read 4741633, cache write 129541, output 48657 Cost: 3.12 USD, time 0 min 31 s, turns 69

Report

Result: done Files: none Commit: none (measurement only, owner on #2652) Body: HEAD 15c267871, run 2’s flat trial gated by NIKAIA_D10 in a patched lowered bounds_walk.rs, restored by copy (md5 de718d42 = HEAD), target/fast rebuilt clean. Each row is case | past-loop walks in checks A/E/E’ | Ir A/E/E’ | reruns E/E’ (kind) | ledger returns walks (E does not touch them). corpus (#2474 ledger tests: std, libraries.nika, source.nika, 10112 common functions) | 17484/22408/18904 | callgrind not run (800-930 s per run) | 3264/301 (3263/300 same, 1 gap: 253858..255723 trial lacks d19063:0 i18959, recovered by the rerun) | 13167-13377. rs2nika_free_methods (812181..819773) | 5151/5170/5170 | in corpus | 1/1 same | -. sv_strategy | 4505/6627/5466 | 54.8 s/64.5 s/60.5 s wall | 1148/189 all same | 3484. 10-loop probe | ~4092 (from 8 loops: 4x(2^n-1))/-/4132 | 1627 s wall for E’ | 4/4 gap (trial lacks i129 i148 i167 i186) | 8184. 8-loop probe | 1020/1052/1052 | 283 s/285 s/289 s | 4/4 gap | 2040. 6-loop probe | 252/276/276 | 256.42G/259.44G/259.44G | 4/4 gap | 504. fn chained | 12/20/20 | 17.10G/18.67G/18.67G | 4/4 gap (s - k) | 12. Diff against A: 0 lost proofs (every gap rerun gives back A’s set; E with NIKAIA_D10_VERIFY gave 8629 + 1655 functions, every one same; --bounds output of sv_strategy identical in A/E/E’). The trial proves more nowhere (trial_more empty in every log). Fixes: Pushed: none Tests: bytes::the_corpus_needs_no_tether passed in A, E’ and E+verify (patched it binary); check –bounds of sv_strategy, chained and the 6/8/10-loop probes in each mode; no commit, nothing else run Gate: green Deviation: no callgrind on the corpus or on sv_strategy (55-930 s native each, hours under valgrind). Wall times and deterministic walk counts stand in for them. The 10-loop probe in A and E was not run (27 min for E’ alone); A’s figure is extrapolated from the 4x(2^n-1) pattern measured at 6 and 8 loops. Found: Needs: Changelog: Issue: meets I1-I13. Its premise (D10’s again-walk drives the cost) does not hold for the ledger: the 2^n lies in bounds_returns’ own walks (8184 at 10 loops, untouched by E/E’), and a function with an unproved check after a cut loop pays the trial on top of A. Owner: by these figures neither is buildable. E costs +28 % walks on the corpus and +47 % on sv_strategy; E’ costs +8 % and +21 %. Neither saves a walk in any case, because the functions whose cost is 2^n are exactly those whose checks stay open after a cut loop, so they always rerun (the decision stays with the owner). Comment: #2652 Measured E/E’ (HEAD 15c267871, deterministic walk counts per function). Past-loop walks in checks, A/E/E’: corpus 17484/22408/18904; rs2nika_free_methods 5151/5170/5170; sv_strategy 4505/6627/5466 (54.8/64.5/60.5 s); 8 loops 1020/1052/1052; 6 loops Ir 256.42G/259.44G/259.44G; fn chained Ir 17.10G/18.67G/18.67G. 0 lost proofs, the trial nowhere proves more. Every 2^n function reruns (its open checks lie after cut loops), and bounds_returns’ walks (8184 at 10 loops), the larger 2^n, are untouched. Neither E nor E’ pays. Friction: each corpus run took 800-930 s and the 10-loop probe 27 min, so three modes of the corpus filled most of the run’s wall time Disk freed: scratch 150M->5.3M (binaries, callgrind outputs); target/nikaia-project-tests 379M (one new 379M rlib entry), / 5.8G free Attribution: Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com Claude-Session: https://claude.ai/code/session_01UVxJDtmukJNapbMLmZMzTV

Debrief

  1. Most time: waiting on wall-clock runs, not turns. The corpus test took 800–930 s per mode, the 10-loop probe 27 min, and the 6-loop probe under callgrind about 256 G Ir per mode. Before the compaction, patching the lowered file cost two failed builds: the lowered condition text didn’t match my pattern, and format! there returns Text.
  2. Looked up: where the trial goes (bounds_aggressive, bounding_past_loop, bounding_pass). I also had to find that bounds_returns has walks of its own, outside D10, and the byte range of rs2nika_free_methods in benches/lowering/source.nika. The brief could have named all three.
  3. Checks run: the corpus test in A/E’/E+verify, check --bounds on sv_strategy, chained and the 6/8/10-loop probes, callgrind on chained and loops6, and the md5 check plus clean rebuild.
    • Wasted: the 10-loop probe in E’ (27 min to confirm what 8 loops already showed). Arguably also the E run without verify on sv_strategy, since Ev covers it.
  4. One change: the brief could say “patch the lowered file with std::format!/String, log per function to a file via an env var”, and name the cheap cases first (8 loops, chained), so the expensive corpus runs come only after the shape is settled.
  5. What the issue lacked:
    • wrong I7 the premise that D10’s again-walk drives the ledger cost: the 2^n that dominates is in bounds_returns’ own walks (8184 at 10 loops), which E/E’ never touch.
    • rules-gap the expected run time of each measured case, which would have let me drop the corpus callgrind up front.
  6. Traps:
    • Fact: Lowering In a lowered .rs, format! is shadowed to return Text; a hand patch uses std::format! and std::string::String.
    • Fact: Core Callgrind on check --bounds of a 6-loop probe already runs about 256 G Ir; use deterministic counters logged by an env-gated patch instead.
  7. Ledger and bounds proofs:
    • Fact: Ledger and bounds proofs Key files: bounds_walk.nika (bounds_aggressive, bounding_past_loop, bounding_pass); bounds_returns makes its own 2^n walks outside D10.
    • Fact: Ledger and bounds proofs 2026-11-21 HEAD 15c267871: past-loop walks in checks A/E/E’, corpus 17484/22408/18904, sv_strategy 4505/6627/5466; 0 lost proofs, the trial nowhere proves more.
    • Fact: Ledger and bounds proofs 2026-11-21 HEAD 15c267871: bytes::the_corpus_needs_no_tether takes 800–930 s; rs2nika_free_methods (bytes 812181..819773) alone makes 5151 past-loop walks.

Generated by Claude Code

keywan-ghadami, 2026-11-21

Run 2 (measurement) is done. The figures are on the decision issue “may the walk past loops prove fewer checks…” (#2652). Neither E nor E’ saves a walk: E adds 28 % walks on the corpus and E’ adds 8 %. This issue is now blocked, waiting on the owner’s decision in #2652.


Generated by Claude Code

keywan-ghadami, 2026-11-22

Stays blocked. The measured variants E and E’ are not built: neither saves a walk, and the owner refused proving less (2026-11-21, see “Decision: may the walk past loops prove fewer checks in rare shapes…” (#2652), closed). This issue waits for the solver lane’s “The bounds walk walks a pass again whose inputs did not change” (#2695), “The bounds walk walks a loop’s rest again within one pass from an equal state” (#2697) and the decision on factorising by loop cones (#2698).


Generated by Claude Code

keywan-ghadami, 2026-11-28

Research result: is there an exact linear walk (one where every result stays the same)?

Not today. The losing walk leaves state behind that BoundsMark does not restore: return summaries, axioms, fresh-name counters, flagged, leaving and the arena. Later questions read that state. An exact speed-up could only share work where two walks reach a loop in the same state, and that never happens, because these fields only grow.

Ideas we checked and ruled out:

The route, in order:

  1. Answer each solver question once per function (#2885). This is exact and keeps the 2^n walks, but cuts their cost.
  2. Make the losing walk leave nothing behind (#2886). This is what ADR-361 D10 means: a losing walk does not stand. It can only gain proofs.
  3. After #2886, measure how often walks reach a later loop in the same state, compared up to fresh names. If the rate is high, merging those walks gives a linear walk that is exact relative to the state after #2886. That becomes a new issue once the measurement is in.

Options B, C and D of the decision paper stay out under the owner’s rule.


Generated by Claude Code

keywan-ghadami, 2026-11-29

From the report of the run of issue-2897 (#2897):

The work gate (#2897) is blocked on item 2: the bounds walk has no file path (it lives only in Rust), so the per-file WORK line needs a decision on Rust plumbing of the path into bounds_proven


Generated by Claude Code

Back to Open Issues