Skip to content

optimize: detect recurring context pasted at the start of sessions - #1021

Merged
iamtoruk merged 3 commits into
mainfrom
feat/optimize-recurring-context
Aug 18, 2026
Merged

iamtoruk merged 3 commits into
mainfrom
feat/optimize-recurring-context

Conversation

@iamtoruk

Copy link
Copy Markdown
Member

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. Class nudge, no --apply (destination is prompt, so plans.ts returns null — asserted; we never move the user's own text into CLAUDE.md automatically). Lands under Habits.

  • Zero extra I/O. optimize already re-streams every in-range JSONL in scanJsonlFile; the opener is captured inside that pass. Real-corpus wall time unchanged (main 8.79 s vs branch 8.50 s median, ~16k sessions).
  • Machine-written prompts are excluded (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. Because parseLargeJsonl drops 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.
  • Normalize (strip ANSI, collapse whitespace) before hashing so re-flowed pastes group; <system-reminder>/command wrappers and short generic openers are skipped.
  • Basis 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:

─── 1. Same 2.4 KB block pasted at the start of 14 sessions  Low ───
… "REPO CONTEXT: monorepo, pnpm workspaces …" — 14 sessions in 2 projects, ~8.1K tokens.
Standing rules belong in CLAUDE.md; reference material in a file Claude reads on demand.
Potential savings: ~8.1K tokens (~$0.020)  estimated

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, so isSidechain is invisible for large lines at parser.ts:~1295 too. Worth a separate look.

Tests

10 new in tests/optimize-fs.test.ts through the real scanJsonlFile path: 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.ts null asserted.

tsc clean · npm test 2769 passed · app 489 passed.

Depends on/conflicts textually with #1020 (both touch src/optimize.ts, docs/optimize.md, CHANGELOG) — will rebase whichever lands second.

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.
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