Repository navigation
optimize: bound zero-edit low-worth savings estimate to a recovery fraction - #691
Merged
Merged
Conversation
iamtoruk
marked this pull request as ready for review
July 16, 2026 18:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
estimateLowWorthRecoverableTokensreturns the entire session token total wheneditTurns === 0, and that number flows into the headline "Potential savings:X tokens ($Y, ~Z% of spend)" and the JSONpotentialSavingsCostUSD/ per-findingestimatedSavingsUSD. A zero-edit (exploration/reading) session claims 100% of its cost as recoverable — an upper bound presented as a point estimate. The retry path already applies a bounded fraction; only the no-edit branch claimed everything. (#640)Note: the old behavior was documented as intentional in the comment above the function, so this is a deliberate behavior change, not a typo fix — the doc comment is rewritten accordingly.
Fix
New
WORTH_IT_NO_EDIT_RECOVERY_FRACTION = 0.5next to the otherWORTH_IT_*constants (aligned with the existingCAPABILITY_RELIABILITY_RECOVERY_FRACTION = 0.5precedent): the no-edit branch now returnsround(tokens * 0.5). Retry branch untouched. The two existing tests pinning the old ceiling now pin the fraction.Tuning note
0.5 is a judgment call — read-only sessions may still contain useful exploration, so claiming half is conservative in both directions. Easy to adjust in one place, guarded by tests.
Tests
tests/optimize-zero-edit-savings.test.ts(written red-first: both cases failed withexpected 10000 to be less than 10000before the fix, pass after).tests/optimize.test.ts(8K → 4K for the no-edit cases).npx tsc --noEmitclean; full suite 1712 passed / 0 failed.Closes #640