feat: implement message pruning to enforce context window limits in a…#12
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…gent loop
Summary / What Changed?
pruneMessageshelper ininternal/agent/messages.goto safely truncate conversation history when approaching token/context limits.internal/agent/loop.goto automatically prune history before every reasoning turn (maxKeep= 8 messages).messages[0](the original user task instruction) plus the most recentmaxKeepmessages while keeping tool calls and tool results paired properly.TestPruneMessagesinsideinternal/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.Messagehistory grows indefinitely. This leads to massive input token consumption, high costs, and eventually crashes the application with HTTP400 Bad Request (Context Exceeded)or429 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?
go test -v ./...) — All existing and new unit/golden tests pass.windmist ...locally to confirm UI behavior and prompt handling.Test Environment & Commands Executed:
# Paste exact test commands or output snippets here if applicableChecklist
CONTRIBUTING.md(or rootCONTRIBUTING.md).go build -v ./...) without errors or warnings.README.md,docs/, or CLI--helptext) if this PR changes user-facing behavior or flags.