Skip to content

fix(cgp): §B3-honest frame token_cost (+ unbreak clippy CI) - #355

Merged
macanderson merged 2 commits into
mainfrom
fix/cgp-token-cost-honesty
Jul 23, 2026
Merged

fix(cgp): §B3-honest frame token_cost (+ unbreak clippy CI)#355
macanderson merged 2 commits into
mainfrom
fix/cgp-token-cost-honesty

Conversation

@macanderson

@macanderson macanderson commented Jul 23, 2026

Copy link
Copy Markdown
Owner

Context

The user asked to rebase the CGP pin forward. While I was doing that, #354 landed the pin bump on main (58a933a29fb559aa) — so the rebase itself is already done. This PR is the residual gap #354 left, plus the CI breakage it landed with.

The bug: main claims §B3-honest, isn't

#354's description says it adopted the sweep's "canonical token accounting." It didn't. Both production frame builders still declare the old estimate:

  • store (stella-context): estimate_tokens(content) + estimate_tokens(title) — adds the title's tokens (always non-zero) and counts chars, not bytes.
  • graph (stella-graph): estimate_tokens(content).max(1) — the .max(1) diverges on empty content.

Under the pinned protocol, §B3 requires token_cost == budget_tokens(content) (canonical ceil(bytes/4), exact, no tolerance). Neither builder is honest.

The conformance gate (#347) didn't catch it — its probe query returns zero frames from a mismatched seed, so §B3 never fires on a real frame (a vacuous pass).

The fix (red → green, proven)

  • Both builders now declare contextgraph_types::budget_tokens(content). pack_to_budget already packs against each frame's own token_cost, so the summed-budget invariant is preserved.
  • New recalled_frames_declare_honest_token_cost drives the real recall builder with a query that provably surfaces a frame (mirrors recall_returns_cited_budget_respecting_frames) and asserts declares_honest_token_cost. It fails on pre-fix code (token_cost 18 vs canonical 15 — the 3-token gap is the title) and passes after. A matching assertion guards the graph builder. Dead estimate_tokens helpers removed.

Second commit: unbreak CI (pre-existing, unrelated)

main currently fails cargo clippy --workspace --all-targets -- -D warnings — the exact CI gate — on two warnings that merged unprotected: a missing #[allow(clippy::too_many_arguments)] on run_shared_candidates (from #352, its five siblings have it) and an unused Representation import (from #354, used only in a test helper). Cleared so this PR's CI is green. Heads-up: main is red on CI right now — worth branch-protecting (tracked P0).

Verification

cargo test --workspace                              # 2753 passed, 0 failed
cargo clippy --workspace --all-targets -- -D warnings   # clean (was 2 warnings)
cargo fmt --all -- --check                          # clean

The CGP pin rebase (#354) claimed to adopt "canonical token accounting" but
kept the old estimate: the store declared
`estimate_tokens(content) + estimate_tokens(title)` and the graph declared
`estimate_tokens(content).max(1)`. Under the pinned protocol, §B3 requires
`token_cost == budget_tokens(content)` — the canonical inline count
(ceil(bytes/4)), exact, no tolerance. Neither builder was honest: the store
added the title's tokens (always non-zero) and used chars not bytes; the
graph's `.max(1)` diverges on empty content.

The existing conformance gate didn't catch it: its probe query returns zero
frames from the mismatched seed, so §B3 never fired on a real frame.

Fix: both production builders now declare `budget_tokens(content)`.
`pack_to_budget` already packs against each frame's own token_cost, so the
summed-budget invariant is preserved. New `recalled_frames_declare_honest_token_cost`
drives the real `recall` builder with a query that provably surfaces a frame
and asserts `declares_honest_token_cost()` — it fails on the pre-fix code
(token_cost 18 vs canonical 15) and passes after. A matching assertion guards
the graph builder. Dead `estimate_tokens` helpers removed.
`main` (b0117f3) fails `cargo clippy --workspace --all-targets -- -D warnings`
— the exact CI gate — on two warnings that landed unprotected:

- `run_shared_candidates` (pipeline.rs, from #352) lacks the
  `#[allow(clippy::too_many_arguments)]` its five sibling functions in the
  same file already carry.
- `use contextgraph_types::Representation` (contextgraph.rs, from #354) is
  used only in a `#[cfg(test)]` helper, so it is unused in the bin build.
  Import removed; the one test use is fully-qualified.

Both unrelated to the token_cost fix but required for this PR's CI to be green.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @macanderson, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@vercel

vercel Bot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
stella-cli-docs Ignored Ignored Jul 23, 2026 1:22pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant