fix: prevent write_interim_draft context overflow (465K → ≤300K chars) #5

Open
chaim wants to merge 0 commits from fix/write-interim-draft-context-overflow into main
Owner

Summary

  • Bug 1 (קריטי): source_context הוכנס פעמיים לכל פרומפט — פעם בתוך הטמפלט ({source_context}) ופעם שנייה כ-prepend ב-write_block. תוצאה: 232K × 2 = 465K chars. כל 5 בלוקי הטיוטה נכשלו עם "Claude CLI failed (exit 1): unknown error".
  • Bug 2: _build_source_context טען את כל 9 מסמכי התיק לכל בלוק ללא סינון.

Root cause discovered from: CMPA-20 heartbeat run 2b50d42a on 2026-05-10, case 8137-24.

Changes

block_writer.py

  • Remove duplicate source_context prepend — template already embeds it
  • Add _BLOCK_DOC_TYPES per-block filtering: he/zayin→empty, vav→appeal+protocol, chet→protocol, tet→appraisal
  • Add 400K char guard with descriptive error

claude_session.py

  • Warning log for prompts >150K chars
  • Include prompt_len in RuntimeError

Test plan

  • _build_source_context(case_id, 'block-zayin') returns 0 chars ✓
  • _build_source_context(case_id, 'block-tet') returns ~254K (appraisals only) ✓
  • All 5 block prompts below 400K: he ~30K, zayin ~30K, vav ~172K, chet ~45K, tet ~300K ✓
  • write_interim_draft('8137-24') completes without errors

🤖 Generated with Claude Code

## Summary - **Bug 1 (קריטי):** `source_context` הוכנס פעמיים לכל פרומפט — פעם בתוך הטמפלט (`{source_context}`) ופעם שנייה כ-prepend ב-`write_block`. תוצאה: 232K × 2 = 465K chars. כל 5 בלוקי הטיוטה נכשלו עם "Claude CLI failed (exit 1): unknown error". - **Bug 2:** `_build_source_context` טען את כל 9 מסמכי התיק לכל בלוק ללא סינון. **Root cause discovered from:** CMPA-20 heartbeat run `2b50d42a` on 2026-05-10, case 8137-24. ## Changes ### `block_writer.py` - Remove duplicate `source_context` prepend — template already embeds it - Add `_BLOCK_DOC_TYPES` per-block filtering: he/zayin→empty, vav→appeal+protocol, chet→protocol, tet→appraisal - Add 400K char guard with descriptive error ### `claude_session.py` - Warning log for prompts >150K chars - Include `prompt_len` in RuntimeError ## Test plan - [ ] `_build_source_context(case_id, 'block-zayin')` returns 0 chars ✓ - [ ] `_build_source_context(case_id, 'block-tet')` returns ~254K (appraisals only) ✓ - [ ] All 5 block prompts below 400K: he ~30K, zayin ~30K, vav ~172K, chet ~45K, tet ~300K ✓ - [ ] `write_interim_draft('8137-24')` completes without errors 🤖 Generated with [Claude Code](https://claude.com/claude-code)
chaim added 1 commit 2026-05-10 10:51:15 +00:00
Two bugs caused all 5 interim blocks to fail with "Claude CLI failed
(exit 1): unknown error":

1. source_context was embedded BOTH inside the prompt template (via
   {source_context}) AND prepended again in write_block — doubling every
   block's context size (232K chars × 2 = 465K chars).

2. _build_source_context loaded all 9 case documents for every block
   regardless of relevance.

Fixes:
- Remove the duplicate source_context prepend in write_block; the
  template already contains it via {source_context}
- Add per-block document filtering (_BLOCK_DOC_TYPES): block-he/zayin →
  empty, block-chet → protocol only, block-tet → appraisals only
- Add 400K char guard before calling claude -p with a descriptive error
  (vs opaque "exit 1: unknown error")
- Add prompt-size warning and size info in claude_session error messages

Result: block-he 0 chars, block-zayin 0 chars, block-vav ~172K,
block-chet ~45K, block-tet ~300K (all under 400K limit)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This branch is already included in the target branch. There is nothing to merge.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin fix/write-interim-draft-context-overflow:fix/write-interim-draft-context-overflow
git checkout fix/write-interim-draft-context-overflow
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ezer-mishpati/legal-ai#5