Repository navigation
fix(digest): make outlier sessions digestible + stop the pipeline digesting itself - #65
anand-jha-veho wants to merge 3 commits into
Conversation
…IMEOUT_S)
Large sessions (100MB+ transcripts) exceeded the hardcoded 180s claude -p
timeout even after chunking, failing 3 of 20 sessions during a laptop-
migration backfill. Default stays 180s; override per run, e.g.:
DIGEST_LLM_TIMEOUT_S=900 digest_cli.py backfill --source claude
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…sted _chunk_messages never split inside a message, so a single huge message became a chunk larger than the model's input budget. The provider then rejected the call outright — `claude -p` returned is_error with zero token usage — which no timeout increase can fix. Real case: a 145K-token (581KB) tool result inside a 138MB / 4.5M-token session. - _split_oversized() slices any message exceeding the chunk target into labeled parts; falls back to the original message if it isn't a copyable dataclass, so unexpected types degrade instead of raising. - DIGEST_CHUNK_TOKEN_TARGET overrides the derived chunk target. The documented 120K target was unreachable in production because min(CHUNK_TOKEN_TARGET, SINGLE_PASS_TOKEN_LIMIT // 2) pins it to 30K — the very "hours of wall time" problem the 120K constant was raised to avoid. Unset keeps the derived value, so default and test behavior are unchanged. Verified on the 138MB session: largest chunk 145,357 → 100,008 tokens, chunk count 165 → 49, and no content lost (17,956,236 → 17,956,416 chars, delta = part markers only). Full suite: 1743 passed, 3 pre-existing failures that reproduce identically on a clean checkout (2 MCP SDK API drift, 1 real-data dispatcher check). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Follow-up: pushed two more commits. The timeout raise turned out to be necessary but not sufficient, and investigating why surfaced a second, larger problem. (Edited: I first reported the self-digestion scale as "12 of 80 digests" based on title matching. That was wrong — classifying by The timeout raise was necessary but not sufficientRe-running the 3 previously-failing sessions at Measuring the normalized session found the cause: one message estimates at 145,357 tokens (581KB), and The documented 120K chunk target is unreachable in productionchunk_target = min(CHUNK_TOKEN_TARGET, max(1, SINGLE_PASS_TOKEN_LIMIT // 2))With I added The pipeline was digesting its own LLM callsEvery internal It is hard to see by eye, which is why my first estimate was 10x low. A chunk-digest call's content is the user's real transcript, so the digests get plausible titles — "wire cerbos shadow descriptors 6 domains", "fix CI snapshot failures from macOS esbuild platform variance" — and only the obvious Two-layer fix:
On 376 local transcripts: 362 filtered, 14 kept, every known real session preserved (145MB session plus 13 others); each borderline file inspected individually. Side effects worth knowing
🤖 Generated with Claude Code |
Every internal `claude -p` call persisted a transcript into ~/.claude/projects/, which the next backfill then discovered and digested. The brain filled with digests of its own digest calls, and each run made the next one worse: one backfill wrote 238 self-transcripts and grew this brain from 80 to 121 digests with ~none of the growth being real sessions. The noise is hard to spot by eye because a chunk-digest call's *content* is the user's real transcript, so the resulting digests carry plausible titles like "wire cerbos shadow descriptors 6 domains" rather than anything obviously machine-generated. Classifying by source_path found 115 of 130 sidecar entries were self-generated. - claude_code provider passes --no-session-persistence, so internal calls write no transcript at all. Fixes it at the source. - _is_self_generated() filters transcripts already on disk from before that, keyed on the `SYSTEM:\n...\n\nUSER:\n` envelope appearing as the first user turn. Detection is by prompt shape, not project directory: calls inherit the runner's cwd, which may also hold real sessions, and the digests themselves look genuine. Later turns are not required to match — schema-enforced calls add structured-output turns, so these are not single-turn despite being one-shot runs. Verified on 376 local transcripts: 362 filtered, 14 kept, with every known real session preserved (a 145MB session plus 13 others). Each flagged borderline file was inspected individually; e.g. one 24KB "session" was a query-expansion call. Tests: 32/32 in test_migrate_dispatcher.py (this also clears its real-data check), 38/38 across the digest suites. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Final verification: the 138MB session now digests successfullyThe session that motivated this PR — 138MB, 4.5M est. tokens, 17,999 messages, which failed first on the 180s timeout and then on the oversized-message rejection — completed in ~9 minutes: Output is a well-formed 5.4KB digest ( Before/after on the same machine and the same data:
Two notes for anyone reading the numbers:
Both open questions from the earlier comment still stand and are yours to decide: whether to drop 🤖 Generated with Claude Code |
Three related fixes for the session-digest pipeline. Each was found by the previous one turning out to be insufficient; the comment below has the full investigation trail.
1. Env-overridable per-call timeout
timeout_s=180was hardcoded; nowLLM_TIMEOUT_S = int(os.getenv("DIGEST_LLM_TIMEOUT_S", "180")), applied to all three provider calls (single-pass, per-chunk, merge). In a backfill over 20 sessions, 3 failed withLLM FAIL: claude -p timeout after 180s; 2 of the 3 digest successfully at 900s.2. Splitting oversized messages
_chunk_messagesnever split inside a message, so a single huge message became a chunk exceeding the model's input budget — the provider rejected the call before inference (is_error: true, zero token usage). No timeout fixes this. Real case: a 145,357-token (581KB) message inside a 138MB / 4.5M-token session — the 3rd failing session, which the timeout raise alone did not rescue._split_oversized()slices any message over the chunk target into labeled parts, falling back to the original if it isn't a copyable dataclass.DIGEST_CHUNK_TOKEN_TARGEToverrides the derived chunk target, which is pinned to 30K in production bymin(CHUNK_TOKEN_TARGET, SINGLE_PASS_TOKEN_LIMIT // 2)despite the 120K constant (see comment).3. The pipeline was digesting its own LLM calls
Internal
claude -pcalls persisted transcripts into~/.claude/projects/, which the next backfill digested — self-reinforcing. One backfill wrote 238 self-transcripts and grew a brain 80 → 121 digests, nearly all noise. Classifying bysource_path: 115 of 130 sidecar entries were self-generated. Titles look genuine because a chunk-digest call's content is the user's real transcript.--no-session-persistence— internal calls write no transcript (source fix)._is_self_generated()filters pre-existing ones by theSYSTEM:\n…\n\nUSER:\nenvelope on the first user turn.Verification
test_migrate_dispatcher.py(fix 3 also clears its long-standing real-data failure). Full suite at the time of fix 2: 1743 passed, with 2 pre-existing MCP SDK failures that reproduce on a clean checkout.Out of scope
Adaptive/per-call timeouts and retry-with-backoff. Also flagged but not changed: internal calls still allow auto-memory writes — a sub-Claude wrote a real digest into
~/.claude/projects/<slug>/memory/digests/where nothing reads it (details in the comment).🤖 Generated with Claude Code