OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug] IntentAnalyzer does not bound recent message content, causing oversized planner prompts and severe recall latency

Open edynasty opened this issue 2 months ago • 2 comments

Summary

IntentAnalyzer._build_context_prompt() limits the number of recent messages, but it does not limit the size of each message body.

As a result, a small number of large messages — for example logs, tool outputs, diffs, source snippets, or agent responses — can produce extremely large query-planner prompts.

This affects session-aware intent analysis and query expansion.

Verified on:

  • OpenViking v0.4.23
  • upstream commit 9d9bc85e

The affected implementation is unchanged between the tested v0.4.23 tree and that upstream revision.

Affected code

openviking/retrieve/intent_analyzer.py

Current behavior is effectively:

summary = self._truncate_text(
    compression_summary,
    self.MAX_COMPRESSION_SUMMARY_CHARS,
)

recent = messages[-self.max_recent_messages:] if messages else []

recent_messages = "\n".join(
    f"[{m.role}]: {m.content}"
    for m in recent
    if m.content
)

The compression summary is bounded, and the number of recent messages is bounded, but m.content itself is not.

Related paths include:

  • openviking/retrieve/context_assembler/expansion.py
  • openviking/storage/viking_fs/_semantic.py
  • openviking/session/session.py

Minimal reproduction

A pure-code reproduction does not require a running server:

messages = [
    Message(role="user", content="x" * 100_000)
    for _ in range(5)
]

prompt = analyzer._build_context_prompt(
    "",
    messages,
    "test query",
)

print(len(prompt))

This produces at least ~500 KB of planner prompt from only five messages.

A server-level reproduction is:

  1. Create a session.
  2. Add five messages containing approximately 25 KB each.
  3. Run session-aware search with that session ID.
  4. Observe the request sent to the configured query planner.

The planner request size is approximately the sum of those message bodies plus the prompt template and summary.

Observed behavior

In a real coding-agent workload:

Case Observed value
Planner request before bounding message content 110–130 KB
Same session after adding a per-message cap ~12.5 KB
Planner latency before 15–17.6 s
Planner latency after ~4.3 s

Context-mode query expansion also failed on 20/20 consecutive searches with the larger prompt.

The problem scales primarily with per-message content size. Message count is already capped.

Expected behavior

Intent-analysis prompt size should remain predictably bounded regardless of the size of an individual recent message.

Large tool results, pasted logs, source code, or agent responses should not cause planner input size to grow without a bound.

Suggested direction

The smallest conceptual fix is to apply the existing _truncate_text() helper to each recent message before inserting it into the planner prompt.

For example:

recent_messages = "\n".join(
    f"[{m.role}]: {self._truncate_text(m.content, MAX_RECENT_MESSAGE_CHARS)}"
    for m in recent
    if m.content
)

The exact limit should be decided upstream.

Reducing MAX_COMPRESSION_SUMMARY_CHARS is separate deployment tuning and is not required to fix this defect.

Suggested regression test

def test_intent_prompt_bounds_message_content():
    analyzer = IntentAnalyzer(
        max_recent_messages=5,
        query_planner=_FakePlanner(),
    )

    messages = [
        Message(role="user", content="x" * 100_000)
        for _ in range(5)
    ]

    prompt = analyzer._build_context_prompt(
        "",
        messages,
        "q",
    )

    assert len(prompt) < 50_000

An integration test could additionally verify that a session containing several very large messages does not send an unexpectedly large request to the configured planner.

Impact

This is especially visible in coding-agent integrations because individual messages commonly contain large logs, tool results, diffs, and source files.

A sufficiently fast planner may hide the latency symptom, but the unbounded input/token cost remains.

edynasty avatar Oct 04 '26 08:10 edynasty

Field data: this warning fires here too, but the line does not record why — so I cannot yet confirm or exclude the mechanism proposed here

We hit the same subsystem, and I would like to tie it to the unbounded-input cause proposed in this issue — but the exception is not being written, so the cause is unrecoverable from the log.

Verbatim (server.log, v0.4.23, macOS, local backend; ov.conf has no retrieval section, so all expansion settings are defaults):

2026-10-05 15:50:40,815 - openviking.retrieve.context_assembler.expansion - WARNING - [request_id=67c7c287-af4f-4c34-af10-8717344d0092] Query expansion failed; using original query: 
2026-10-05 18:36:32,600 - openviking.retrieve.context_assembler.expansion - WARNING - [request_id=3650f0e2-de4c-4d7c-8616-91493e037916] Query expansion failed; using original query: 
2026-10-05 20:08:29,683 - openviking.retrieve.context_assembler.expansion - WARNING - [request_id=2d6e81a0-8cfa-473f-b702-a0914cba4094] Query expansion failed; using original query: 
2026-10-05 20:08:54,068 - openviking.retrieve.context_assembler.expansion - WARNING - [request_id=d15c1464-eae3-42ed-90df-465d2c14e3d6] Query expansion failed; using original query: 

Note the trailing query: followed by nothing. Across every occurrence in the rotated logs, the payload after the colon is empty:

total 26 · empty-suffix 26 · non-empty 0

So the underlying exception is discarded in all cases, and a user cannot distinguish a timeout from a parse failure from an oversize rejection.

Counts (deduplicated by request_id, rotated logs included):

day (CST) occurrences
2026-10-02 3
2026-10-03 6
2026-10-04 12
2026-10-05 5

Window: 2026-10-02 00:53:50 → 2026-10-05 20:08:54. Spread through the day rather than clustered, which points at a per-request condition rather than one bad batch — consistent with, but not proof of, an input-size condition.

Two asks:

  1. Log the exception (type + message, or at least the first line) in that warning. Without it, confirming or ruling out the mechanism described in this issue is impossible from the outside, and so is any user-side workaround.
  2. If unbounded recent-message content is a candidate cause, is there an existing knob to bound the input rather than the time? We run defaults (retrieval unset); retrieval.recall_rewrite_timeout_s looks like the only relevant control and it caps duration, not size. Capping input size would be preferable to lowering the timeout.

Degradation on our side looks safe: recall proceeds with the original query and nothing user-visible fails — this is a WARNING only, so it is easy to miss.

za8209 avatar Oct 05 '26 13:10 za8209

The empty suffix is reproducible without an oversized input: asyncio.wait_for raises TimeoutError with an empty string, and the current warning formats only that string. #5827 adds the exception type to this warning. An isolated probe of the real expansion function produced TimeoutError after the change and still returned the original query with status failed. That does not establish what caused the failures in your logs.

On current main (20f9c113), expansion uses retrieval.recall_intent_timeout_s (default 5 seconds). recall_rewrite_timeout_s (default 30 seconds) limits digest rewriting. Neither bounds input size. IntentAnalyzer caps recent history at five messages and the compression summary at 30,000 characters, but has no configurable per-message content cap. #5606 separately proposes a fixed 2,000-character cap per recent message; it is still open.

r266-tech avatar Oct 09 '26 11:10 r266-tech