Skip to content

feat: implement message pruning to enforce context window limits in a…#12

Merged
Nithwin merged 2 commits into
mainfrom
feat/4-history-pruning
Jul 16, 2026
Merged

feat: implement message pruning to enforce context window limits in a…#12
Nithwin merged 2 commits into
mainfrom
feat/4-history-pruning

Conversation

@Nithwin

@Nithwin Nithwin commented Jul 16, 2026

Copy link
Copy Markdown
Owner

…gent loop

Summary / What Changed?

  • Added pruneMessages helper in internal/agent/messages.go to safely truncate conversation history when approaching token/context limits.
  • Updated the iterative agent loop in internal/agent/loop.go to automatically prune history before every reasoning turn (maxKeep = 8 messages).
  • Strictly preserves messages[0] (the original user task instruction) plus the most recent maxKeep messages while keeping tool calls and tool results paired properly.
  • Added comprehensive unit test suite TestPruneMessages inside internal/agent/messages_test.go.

Why? (Motivation & Context)

When the agent executes long multi-turn workflows (like editing multiple files or running terminal commands), the []ai.Message history grows indefinitely. This leads to massive input token consumption, high costs, and eventually crashes the application with HTTP 400 Bad Request (Context Exceeded) or 429 Too Many Requests (Tokens Per Minute limit).

By implementing a sliding window pruning algorithm that always keeps the initial task goal (index 0) and the last 4 turns (8 messages), the agent stays lean, avoids context overflow, and remains fast and reliable during long debugging sessions.


Related Issue(s)

Closes #4


How Was This Tested?

  • Unit Tests (go test -v ./...) — All existing and new unit/golden tests pass.
  • Manual TUI / CLI Verification — Ran windmist ... locally to confirm UI behavior and prompt handling.
  • Provider API Verification — Tested against live or mock LLM provider endpoints (e.g., Gemini, OpenAI).

Test Environment & Commands Executed:

# Paste exact test commands or output snippets here if applicable

Checklist

  • My code follows the code style and guidelines outlined in CONTRIBUTING.md (or root CONTRIBUTING.md).
  • I have verified that the project builds cleanly (go build -v ./...) without errors or warnings.
  • I have added or updated unit tests / golden tests for any new functionality or bug fixes.
  • I have updated relevant documentation (README.md, docs/, or CLI --help text) if this PR changes user-facing behavior or flags.
  • My commit messages are clean, descriptive, and follow conventional commit formats where applicable.
  • I have verified that no API keys, tokens, or sensitive credentials are inadvertently included in this branch.

@Nithwin
Nithwin merged commit 9dfafef into main Jul 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT]: implement conversation history pruning when approaching context limits

1 participant