Repository navigation
optimize: detect recurring context pasted at the start of sessions - #1021
Merged
Merged
Conversation
Groups sessions by their opening block (whitespace/ANSI-normalized, hashed over the first 2 KB) and flags a block of at least 1.5 KB that opens five or more sessions. Class nudge: CodeBurn will not move the user's own text into CLAUDE.md, so the fix asks Claude to give the block a permanent home. Only the repeats count as savings, sized from the block's bytes because provider usage is per API call and cannot isolate the paste. The opener comes from the session scan that already runs, so nothing extra is read.
An SDK session's opening prompt and a subagent's task prompt are written by a program: they repeat by design and have no home in CLAUDE.md. Both are flagged on the entry, but a user entry over the parser's large-line threshold comes back without its root flags - routine for generated prompts, which are exactly the long ones - so the markers are read off the ends of the raw line, where the fields sit either side of the oversized message.
…ng-context # Conflicts: # CHANGELOG.md
This was referenced Aug 18, 2026
Closed
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.
What (optimize PR 3 of 3 — one new detector, no new commands)
recurring-context: the same block (≥ 1.5 KB) opening ≥ 5 sessions in the period — a spec, repo brief, or rules pasted by hand each time. Classnudge, no--apply(destination isprompt, soplans.tsreturns null — asserted; we never move the user's own text into CLAUDE.md automatically). Lands under Habits.optimizealready re-streams every in-range JSONL inscanJsonlFile; the opener is captured inside that pass. Real-corpus wall time unchanged (main 8.79 s vs branch 8.50 s median, ~16k sessions).promptSource === 'sdk',isSidechain === true). Without this the detector is dominated by SDK/subagent prompts — on the author's corpus a naive version flagged 19M tokens of pipeline prompts, where "move it to CLAUDE.md" is wrong advice. BecauseparseLargeJsonldrops those root fields for lines > 32 KB (exactly where generated prompts live), the flags are read from the raw line's head/tail — the shared parser is untouched.<system-reminder>/command wrappers and short generic openers are skipped.estimated, deliberately: provider usage on the first turn mixes system prompt, tool schemas and CLAUDE.md with the paste, so no counted number isolates the block; it is sized from its own bytes and only repeats are counted (first paste is legit).Render:
Docs:
docs/optimize.md,docs/architecture.md(19 → 20 detectors), CHANGELOG.Note for a follow-up (not fixed here)
parseLargeJsonl(src/parser.ts) keeps only type/timestamp/sessionId/cwd/gitBranch/message for > 32 KB lines, soisSidechainis invisible for large lines atparser.ts:~1295too. Worth a separate look.Tests
10 new in
tests/optimize-fs.test.tsthrough the realscanJsonlFilepath: threshold on/off, tokensSaved math, whitespace grouping, short-opener exclusion, system-reminder exclusion, SDK + subagent exclusion, > 32 KB flag-loss case, first-message-only, project attribution, render block. Class/basis registration +plans.tsnull asserted.tscclean ·npm test2769 passed · app 489 passed.Depends on/conflicts textually with #1020 (both touch
src/optimize.ts,docs/optimize.md, CHANGELOG) — will rebase whichever lands second.