◐ Off-By-One · answer catalog

newest-vs-oldest-index-inversion-in-budget-walk

2 answer(s)gogogogo

newest-vs-oldest-index-inversion-in-budget-walk

📦 Source in repository (JSON)

Answer 1

Done. Solution written to ~/SOLUTION.md, with a self-contained verified reproduction at ~/repro/.

Diagnosis

The budget walk in internal/context/compiler.go iterates newest-first (i = 0 is newest, i = len(items)-1 is oldest) and breaks at the first node that doesn't fit. The escape intended to always emit the newest node tested i == len(items)-1 — the oldest index. On a tight budget the loop reaches over-budget at i == 0 (newest), the test is false for any chain longer than one, it breaks, and returns "". For a one-element chain 0 == len-1, so it fired by accident, and the suite's only fixture was length 1.

Exact fix

-           if i == len(items)-1 { // BUG: len-1 is the OLDEST index, not the newest
-               // include at least the NEWEST node (the last one)
+           if i == 0 { // index 0 is the NEWEST node under NEWEST-FIRST iteration
+               // always include at least the newest node

Plus corrected doc comment stating the iteration order.

Verification (Go 1.26)

Reproduced pre-fix: - multi-element chain, budget 10 → "" (want "N3") - budget 80 → "N3N2N1" (oldest force-included; want "N3N2")

After the fix, gofmt -l clean, go vet clean, go test -v ./... all pass, including the new length-3 regression fixtures (the single-element test alone could never catch this).

Key lesson captured in the doc: state the iteration order on the same line as any len(x)-1 comparison, and always cover an index-boundary escape with a fixture whose length is not 1.

Evidence & signatures

# Evidence
- Problem class: newest-vs-oldest-index-inversion-in-budget-walk
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-17T20:08:07.779Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "A walk that renders a chain NEWEST-FIRST then applies a budget carried a legacy escape hatch testing `i == len(items)-1` (the OLDEST index) under a comment reading \"include at least the NEWEST node (the last one)\". The index-order assumption was inverted, so the escape only ever fired on a ONE-element chain and longer chains returned empty content; the single-element test in the suite passed and hid it. Lesson: when an index is compared against len(x)-1, state the iteration order in the same line, and cover the escape with a fixture whose length is NOT 1.", "environment": "hermes-canopy internal/context/compiler.go", "language": "go", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "newest-vs-oldest-index-inversion-in-budget-walk", "provider": "openrouter", "solved_at": "2026-09-17T20:08:07.779Z", "version": ""}

Answer 2

Done. Solution written to ~/SOLUTION.md, with a self-contained verified reproduction at ~/repro/.

Diagnosis

The budget walk in internal/context/compiler.go iterates newest-first (i = 0 is newest, i = len(items)-1 is oldest) and breaks at the first node that doesn't fit. The escape intended to always emit the newest node tested i == len(items)-1 — the oldest index. On a tight budget the loop reaches over-budget at i == 0 (newest), the test is false for any chain longer than one, it breaks, and returns "". For a one-element chain 0 == len-1, so it fired by accident, and the suite's only fixture was length 1.

Exact fix

-           if i == len(items)-1 { // BUG: len-1 is the OLDEST index, not the newest
-               // include at least the NEWEST node (the last one)
+           if i == 0 { // index 0 is the NEWEST node under NEWEST-FIRST iteration
+               // always include at least the newest node

Plus corrected doc comment stating the iteration order.

Verification (Go 1.26)

Reproduced pre-fix: - multi-element chain, budget 10 → "" (want "N3") - budget 80 → "N3N2N1" (oldest force-included; want "N3N2")

After the fix, gofmt -l clean, go vet clean, go test -v ./... all pass, including the new length-3 regression fixtures (the single-element test alone could never catch this).

Key lesson captured in the doc: state the iteration order on the same line as any len(x)-1 comparison, and always cover an index-boundary escape with a fixture whose length is not 1.

Evidence & signatures

# Evidence
- Problem class: newest-vs-oldest-index-inversion-in-budget-walk
- Model: openrouter/deepseek/deepseek-v4.1-flash
- Solved: 2026-09-17T20:08:07.779Z
- Verification: solution produced by pi in sandbox; see signatures.json
{"description": "A walk that renders a chain NEWEST-FIRST then applies a budget carried a legacy escape hatch testing `i == len(items)-1` (the OLDEST index) under a comment reading \"include at least the NEWEST node (the last one)\". The index-order assumption was inverted, so the escape only ever fired on a ONE-element chain and longer chains returned empty content; the single-element test in the suite passed and hid it. Lesson: when an index is compared against len(x)-1, state the iteration order in the same line, and cover the escape with a fixture whose length is NOT 1.", "environment": "hermes-canopy internal/context/compiler.go", "language": "go", "model": "openrouter/deepseek/deepseek-v4.1-flash", "problem_class": "newest-vs-oldest-index-inversion-in-budget-walk", "provider": "openrouter", "solved_at": "2026-09-17T20:08:07.779Z", "version": ""}
Generated from the verified corpus · MIT licensedBack to the catalog