strix icon indicating copy to clipboard operation
strix copied to clipboard

fix: prevent context-window crash from huge tool outputs

Open pranav-afk opened this issue 4 months ago • 1 comments

Cap exec_command results, recover by truncating session history instead of image-strip on ContextWindowExceededError, and surface scan failures as TUI toasts. Closes #579.

pranav-afk avatar Jul 11 '26 11:07 pranav-afk

Greptile Summary

This PR adds recovery paths for large tool output hitting the model context window. The main changes are:

  • A configurable cap for stored tool output.
  • Truncation helpers for plain text and JSON-like outputs.
  • Session-history rewriting after context-window errors.
  • TUI notifications when scans fail.
  • Tests for truncation and context-window recovery behavior.

Confidence Score: 4/5

The context-window recovery path needs fixes before merging.

  • A disabled output cap can still truncate session history during recovery.
  • One cap helper can write more characters than requested.
  • A failed session rewrite can leave the agent without its saved history.

strix/core/execution.py, strix/core/tool_output.py, strix/core/sessions.py

Important Files Changed

Filename Overview
strix/agents/factory.py Wraps exec command results so large string output is capped before session storage.
strix/config/settings.py Adds the STRIX_MAX_TOOL_OUTPUT_CHARS runtime setting.
strix/core/execution.py Adds retry logic for context-window errors, but the recovery path ignores the documented disabled cap value.
strix/core/sessions.py Adds session-output truncation and rewrite helpers, with a clear-then-add path that can lose history on write failure.
strix/core/tool_output.py Adds context-window detection and truncation utilities, with one helper that can exceed the requested cap.
strix/interface/tui/app.py Shows scan failures in the TUI while preserving the existing exit-time error propagation.
tests/test_context_window_recovery.py Adds tests for choosing truncation recovery over image stripping for context-window errors.
tests/test_tool_output.py Adds tests for context-window detection, truncation behavior, and session rewrites.
Prompt To Fix All With AI
Fix the following 3 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 3
strix/core/execution.py:418-421
**Disabled Cap Still Truncates**

When `STRIX_MAX_TOOL_OUTPUT_CHARS=0`, `exec_command` output is left untruncated, but a later context-window retry rewrites the session with the default `65_536` cap anyway. That breaks the documented disable setting and can discard the full tool output the operator explicitly configured the run to keep.

```suggestion
                    truncated = await truncate_large_outputs_in_session(
                        session,
                        max_chars=max_chars,
                    )
```

### Issue 2 of 3
strix/core/tool_output.py:93
**Hard Cap Exceeds Limit**

When the first JSON record plus the truncation header is larger than `max_chars`, `_hard_cap` slices `max_chars` characters and then appends another marker. The stored tool output can exceed the configured cap, so a recovery meant to fit the next model request can still leave oversized history behind.

```suggestion
    marker = "\n...[truncated]"
    if max_chars <= len(marker):
        return text[:max_chars]
    return text[: max_chars - len(marker)] + marker
```

### Issue 3 of 3
strix/core/sessions.py:34-39
**Rewrite Can Erase Session**

During context-window recovery, this helper clears the persisted session before it knows the rebuilt items can be written back. If `add_items` fails twice after `clear_session()` succeeds, the agent loses its conversation history and the retry continues without the state it was trying to recover.


Reviews (1): Last reviewed commit: "fix: prevent context-window crash from h..." | Re-trigger Greptile

greptile-apps avatar Jul 11 '26 11:07 greptile-apps