fix(context): degrade scratchpad section when it exceeds the context budget - #735
Merged
Merged
Conversation
…budget Closes ibl-2b09abdadd25. context.py::_warn_if_over_budget only logged when the scratchpad section exceeded SCRATCHPAD_SECTION_BUDGET_CHARS (90_000); the raw file was still shipped whole into every context build. - ouroboros/context.py: new _render_scratchpad_for_context degrades to the newest whole blocks that fit (block-boundary cuts only), always retains at least the single newest block, and appends an in-band gap-marker (BIBLE P1: no silent truncation) whenever older blocks were dropped OR the retained block(s) alone still exceed budget. The raw WARNING log is preserved (fires on the pre-trim value) so the rot class stays visible. - ouroboros/context_budget.py: SCRATCHPAD_MAX_CONTENT_CHARS=60_000 — source-side content cap for Memory.append_scratchpad_block, ANDed with the existing _SCRATCHPAD_MAX_BLOCKS count cap (single-pass eviction). - ouroboros/memory.py: append_scratchpad_block now evicts on whichever cap (count or content) is violated first; pinned blocks stay exempt from both, same FIFO+journal invariants as before. - tests/test_scratchpad_byte_cap.py: new coverage for both caps and the render-side degrade path (legacy fallback, gap marker on drop, gap marker on the always-retained-newest-alone case). Follow-up: closes ibl-d1ade41fe54d as well (that item tracked exactly this gap-marker-not-firing bug, filed by a prior task attempt that ran out of budget before landing the fix). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AZGh7G7f2g7w8KkQW74Z4V
The fork-local block-pinning feature (ibl-3d7b7b7d5dc9, Memory.pin_scratchpad_block) does not exist on this branch, so the eviction loop in append_scratchpad_block is adapted to plain FIFO (oldest-first, pop(0)) instead of skipping pinned blocks, and test_content_cap_respects_pinning is dropped along with it. Everything else (byte-size cap alongside the block-count cap, and the render-side block-boundary degrade in context.py) is unchanged. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AZGh7G7f2g7w8KkQW74Z4V
- ouroboros/context.py: SCRATCHPAD_MAX_CONTENT_CHARS is only consumed by memory.py's eviction logic now, not referenced in context.py itself. - tests/test_scratchpad_byte_cap.py: unused `import pytest` (no pytest.mark/pytest.raises usage in this file). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AZGh7G7f2g7w8KkQW74Z4V
…chpad content-cap growth The scratchpad content-size cap (ibl-2b09abdadd25) added to append_scratchpad_block's eviction loop pushes ouroboros/memory.py from under 1000 lines into the 1001-1500 band, which this repo's size-ratchet CI lane blocks without an explicit rationale. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AZGh7G7f2g7w8KkQW74Z4V
14 tasks
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
`ouroboros/context.py::_warn_if_over_budget` only LOGS a warning when the scratchpad context section exceeds `SCRATCHPAD_SECTION_BUDGET_CHARS` (90,000 chars) — the raw file still gets shipped whole into every context build. Root cause: `_SCRATCHPAD_MAX_BLOCKS` is a block-COUNT cap only, with no byte-size cap, so N blocks can still add up to well past the char budget (observed 213KB / 2.4x budget in production).
Fix
Notes for this branch
This fork's block-pinning feature (`Memory.pin_scratchpad_block`) doesn't exist on `ouroboros` upstream, so the eviction loop here is plain FIFO (`pop(0)`) rather than pin-aware; the corresponding pinning test was dropped from the cherry-picked test file for the same reason. Everything else is unchanged from the original fix.
Verification
🤖 Generated with Claude Code
https://claude.ai/code/session_01AZGh7G7f2g7w8KkQW74Z4V