Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(optimize): detect context-heavy sessions#246

Merged
iamtoruk merged 2 commits into
mainfrom
feat/context-bloat-analyzer
May 6, 2026
Merged

feat(optimize): detect context-heavy sessions#246
iamtoruk merged 2 commits into
mainfrom
feat/context-bloat-analyzer

Conversation

@iamtoruk

@iamtoruk iamtoruk commented May 6, 2026

Copy link
Copy Markdown
Member

Supersedes #242 (cross-fork PR by @ozymandiashh — original commit preserved as the first commit here, follow-up commit applies review fixes).

Summary

Adds a detectContextBloat() finding to codeburn optimize that flags sessions where effective input/cache tokens (cache-discounted via existing pricing constants) are large and disproportionate to output. Suggests starting fresh with a tightened context.

  • effective input/cache floor: 75K tokens
  • minimum effective input/output ratio: 25:1
  • token-savings estimate: context above a healthier 15:1 ratio
  • preview limited to top 5 by excess; rest summarized as "+N more"
  • ratio capped at "1000+:1" to avoid noise from zero-output sessions
  • deterministic ordering for stable previews and tests

Review fixes on top of #242

Adversarial review against real session data (22.6K sessions / $4.8K spend) found three concrete issues that this commit addresses:

  1. Heavy overlap with detectSessionOutliers. All 5 of the top-5 context-bloat sessions also appeared in the top-5 outlier list. Same sessions, two framings, two "potential savings" lines that the user mentally adds together. Fix: detectSessionOutliers now accepts an optional excludedSessionIds set; scanAndDetect runs context-bloat first, builds the candidate ID set, and passes it through. Real-data outlier count dropped from 96 → 19, and the two findings' top-5 lists are now disjoint. Headline "potential savings" went from a misleading 62% of spend to an honest 10%.

  2. Time-blind growth ratio. "1131x previous session input" was alarming on the surface but was sometimes just an artifact of resuming after a long gap (small test session → real working session weeks later). Fix: new CONTEXT_BLOAT_GROWTH_MAX_GAP_MS = 7 days; growth ratio is suppressed when the predecessor is older than that.

  3. Binary impact tiers. Old logic was >=3 candidates || >=500K total → high, else medium. A 300-session pile-up scored the same as a 3-session minor finding. Fix: three real tiers — high (≥10 candidates or ≥500K total), low (≤2 candidates AND <200K total), medium otherwise.

Plus 7 added tests:

  • medium-tier boundary
  • high tier at exactly 10 candidates
  • 1000+:1 cap with non-zero output (previously only zero-output covered)
  • time-gap suppression (>7 day predecessor → no growth callout)
  • below-threshold predecessor still anchors growth (matches existing code comment)
  • detectSessionOutliers skips sessions in the exclusion set
  • detectSessionOutliers still flags cost outliers not in the exclusion set

One existing test updated: a single 93K-token session (1 candidate, <200K total) is now low impact rather than medium.

Validation

Security

No new attack surface. Pure read-only data transform over existing parsed ProjectSummary. No I/O, shell, eval, or external input.

ozymandiashh and others added 2 commits May 6, 2026 03:12
- Exclude sessions already flagged by detectContextBloat from the
  detectSessionOutliers preview. On real data the two findings shared
  most of their top-5 sessions; the outlier list now focuses on cost
  anomalies that are not also context-bloated.
- Suppress the "Nx previous session input" growth callout when the
  previous session is more than 7 days back. Prevents alarming
  numbers like "1131x growth" that are actually artifacts of resuming
  a project after a long break, not bad context management.
- Replace the binary high/medium impact tiering with three tiers:
  high at >=10 candidates or >=500K total effective tokens, low at
  <=2 candidates and <200K total, medium otherwise. Stops a single
  small finding from competing visually with a 300-session pile-up.
- Tests added: medium-tier boundary, high tier at 10+ candidates,
  1000+:1 cap with non-zero output, time-gap suppression, anchor
  growth from a below-threshold predecessor, outlier exclusion when
  a session is in the context-bloat exclusion set.
@iamtoruk iamtoruk merged commit f92d57d into main May 6, 2026
3 checks passed
@iamtoruk iamtoruk deleted the feat/context-bloat-analyzer branch May 6, 2026 07:11
iamtoruk added a commit that referenced this pull request May 6, 2026
Adds a low-worth detector to codeburn optimize that flags expensive sessions with weak delivery signals (no edits, repeated retries, or no one-shot edits) when no git/gh delivery command is observed. Priority order is low-worth → context-bloat → outliers; each later detector excludes sessions named by an earlier one so the same session is never listed in three findings. Detection:  floor,  for no-edit, 3+ retries, regex matches git commit/push and gh pr create/merge but excludes commit-tree/commit-graph and dry-run. Three impact tiers consistent with #246. Token-savings uses full session tokens for no-edit sessions and the retry fraction for edit-with-retry sessions. Supersedes #241 with review fixes. Original implementation by @ozymandiashh.
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.

2 participants