OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[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

Open za8209 opened this issue 1 month ago • 1 comments

Environment

  • OpenViking 0.5.1.1 (also verified identical code on v0.5.2 and main — working_memory.py is 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_update template (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" on session/working_memory.py and session/session.py returns 0 / 0. RULE 4's "entire Working Memory stays under ~12000 tokens" is prompt-only.
  • The sibling guard for Key Facts & Decisions does have an emergency path (_WM_OVERSIZED_APPEND_CAP = 5, plus an emergency sentinel when a section exceeds 2× the thresholds). Files & Context has 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 & Context accounts 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:

  1. Give Files & Context the same emergency path Key Facts & Decisions already has: once the section exceeds the thresholds, accept a consolidating UPDATE (optionally limited to keeping the N most recently referenced paths), instead of forcing KEEP.
  2. 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 MemoryUpdater rewriting directory .overview.md without a length budget for embedding limits; different code path (MemoryUpdater vs WM section guards).
  • I have also verified that a failed/oversized WM step is silent in the observer (Errors stays 0), reported separately in #6428.

za8209 avatar Oct 23 '26 17:10 za8209

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 & Decisions only grow, which is why a Working Memory document ends up at 60–177 KB.
  • vlm.max_tokens can 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.

dzwalker avatar Nov 17 '26 06:11 dzwalker