[Bug]: Working Memory 'Files & Context' can never shrink — the section guard rejects every UPDATE that drops a path, so a single section reaches 1M tokens
Environment
- OpenViking 0.5.1.1 (also verified identical code on
v0.5.2andmain—working_memory.pyis 1060 lines on all three) - Working Memory enabled (
memory_policy.working_memory.enabled = true) - VLM: an OpenAI-compatible endpoint,
temperature: 0,timeout: 600,max_retries: 2
Bug
wm_enforce_files_no_regression (openviking/session/working_memory.py, def at line 841) rejects every UPDATE op for the Files & Context section that drops even one previously-listed path, converting it to KEEP (or KEEP + APPEND of the new paths). Because a section body can only change through UPDATE (or APPEND), the Files & Context section is structurally monotonic: it can grow, never shrink.
This contradicts both the prompt contract and the documented intent of the guard suite:
- The
compression.ov_wm_v2_updatetemplate (RULE 2, lines 150-156 and RULE 4, lines 207-209) explicitly tells the model to filter for relevance and not to carry bulk path/URL lists forward. - PR #1782 (merged 2026-05-02) that introduced WM v2 advertises "anti-bloat guards, per-section integrity guards". For a path list this guard is anti-shrink, not anti-bloat.
- No total-size clamp exists anywhere on the WM path:
grep -c "clamp_total\|max_overview_tokens\|truncate_summary"onsession/working_memory.pyandsession/session.pyreturns 0 / 0. RULE 4's "entire Working Memory stays under ~12000 tokens" is prompt-only. - The sibling guard for
Key Facts & Decisionsdoes have an emergency path (_WM_OVERSIZED_APPEND_CAP = 5, plus an emergency sentinel when a section exceeds 2× the thresholds).Files & Contexthas no equivalent.
Reproduction (stock 0.5.1.1, no server, pure function)
old = "- /repo/src/pipeline/graph_stage.py — stop-loss code\n"
"- /repo/src/ovlayer/publish.py — publish lock\n"
"- /repo/configs/pipeline.yaml — config seam"
Case 1 — model emits UPDATE to drop two irrelevant paths:
wm_enforce_files_no_regression({"op":"UPDATE","content":"- /repo/src/pipeline/graph_stage.py — stop-loss code"}, old)
# -> {'op': 'KEEP'} # UPDATE rejected
Case 2 — model emits UPDATE that also introduces a new path:
wm_enforce_files_no_regression({"op":"UPDATE","content":"...graph_stage.py...\n- /repo/new/file.py — new ref"}, old)
# -> {'op': 'APPEND', 'items': ['/repo/new/file.py — new ref']}
# old body kept verbatim, new path appended -> net growth
Case 3 — model keeps every old path and adds one (the only accepted shape):
wm_enforce_files_no_regression({"op":"UPDATE","content": old + "\n- /repo/new/file.py — new ref"}, old)
# -> {'op': 'UPDATE'} # accepted
So the only accepted UPDATE is one that preserves the entire path set; the section's token count is therefore non-decreasing by construction.
Measured impact (5,335 persisted archive_*/.overview.md)
Files & Contextaccounts for 21,784,315 estimated tokens across all WM documents — 23.9 % of all WM text (91.0 M chars total).- One long session, measured per commit (this section only):
| archive | bullets | section tokens |
|---|---|---|
| 091 | 269 | 109,130 |
| 093 | 281 | 212,808 |
| 095 | 302 | 410,476 |
| 098 | 317 | 532,664 |
| 100 | 324 | 594,972 |
| 101 | 364 | 1,034,350 |
bullets and tokens increase at every step (9.5× in ten commits); the resulting document is 3.02 MB, i.e. overview_tokens = 1,082,386 versus the template's 12,000-token target.
- Because the whole prior document is fed back as
<current_wm>on the next commit, this section also multiplies the prompt size of every later WM call (related, but distinct from the cross-commit reuse described in #4963).
Why the other guards do not contain it
build_wm_section_reminders (line 334, thresholds bullets > 25 or tokens > 1500) does fire — for the oversized document the reminder block is 891 chars and 4 of 7 sections are flagged — but the reminder only instructs the model to consolidate; nothing on the server bounds the accepted output, and for this section consolidation via UPDATE is exactly what the guard rejects.
Suggested fix
Either of:
- Give
Files & Contextthe same emergency pathKey Facts & Decisionsalready has: once the section exceeds the thresholds, accept a consolidatingUPDATE(optionally limited to keeping the N most recently referenced paths), instead of forcingKEEP. - Or enforce a hard per-section token cap at the merge boundary (
truncate+ marker), so at least the persisted document cannot grow without bound.
Relationship to existing issues
- #4963 (closed, not planned) is about the cross-commit re-send of the accumulated session; this report is about a single section that can never shrink.
- #3226 is about unbounded prompts on the compression path generally; this is a concrete mechanism that produces them.
- #4526 (closed, not planned) was about
MemoryUpdaterrewriting directory.overview.mdwithout a length budget for embedding limits; different code path (MemoryUpdatervs WM section guards). - I have also verified that a failed/oversized WM step is silent in the observer (
Errorsstays 0), reported separately in #6428.
Adding production numbers from 0.4.17.2.dev0 (local storage, deepseek-flash VLM, ~2,800 overviews) — they match the anti-shrink behavior described here:
- Overview size distribution: p50 2.6 KB, p90 38 KB, max 177 KB.
- 156 of 2,787 overviews exceed the local vectordb's 65,535-byte string-field limit (uint16 length in
storage/vectordb/store/bytes_row.py). - The growth is concentrated in long sessions (one session has 961 archives):
Files & Context/Key Facts & Decisionsonly grow, which is why a Working Memory document ends up at 60–177 KB. vlm.max_tokenscan be configured far above any WM budget (we had 40,000), and there is no startup validation against the ~12k-token prompt target, so the model is free to overshoot by 3–15×.
Two asks: (1) a total-size clamp/compaction on the WM path, not only prompt guidance; (2) a DELETE/TRIM op so stale paths/URLs can be dropped instead of being pinned by the anti-regression guard. A config warning when vlm.max_tokens exceeds the WM budget / store limit would help as well.