Skip to content

fix(core): preserve latest user query when pruning chat history during tool loop (#13383) - #13402

Open
DevOps22X wants to merge 1 commit into
continuedev:mainfrom
DevOps22X:fix/compile-chat-preserve-user-query-13383
Open

DevOps22X wants to merge 1 commit into
continuedev:mainfrom
DevOps22X:fix/compile-chat-preserve-user-query-13383

Conversation

@DevOps22X

Copy link
Copy Markdown

Description

Addresses #13383.

In long agent sessions with substantial tool execution outputs, compileChatMessages prunes message history from the top until it fits within the context budget. Because extractToolSequence separates only trailing assistant toolCalls and tool outputs, the opening and latest user queries were previously eligible for pruning from the history array.

When all prior turns were pruned, the request reached models/providers (e.g. Ollama with Qwen 3.8 renderers) without any user turn, causing HTTP 500 (no user query found in messages).

This patch pins the latest user turn when the trailing sequence is an assistant/tool loop, budgeting its tokens alongside the system message and tool sequence, and preserving it in place during history pruning.

Changes

  • core/llm/countTokens.ts: Separate the latest user message from prunable history when trailing turns comprise an active tool loop, budgeting its tokens upfront.
  • core/llm/compileChatMessages.vitest.ts: Added regression tests verifying that the user turn is preserved and older intermediate turns are pruned first.

Validation

  • npx vitest run compileChatMessages: 2/2 tests passed (100% green).

@DevOps22X
DevOps22X requested a review from a team as a code owner October 19, 2026 20:28
@DevOps22X
DevOps22X requested review from sestinj and removed request for a team October 19, 2026 20:28
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

…g tool loop (continuedev#13383)

In long agent sessions with substantial tool execution outputs, compileChatMessages prunes message history from the top until it fits within the context budget. Because extractToolSequence separates only trailing assistant toolCalls and tool outputs, the opening and latest user queries were previously eligible for pruning from the history array. When all prior turns were pruned, the request reached models/providers (e.g. Ollama with Qwen 3.8 renderers) without any user turn, causing HTTP 500 ('no user query found in messages').

This patch pins the latest user turn when the trailing sequence is an assistant/tool loop, budgeting its tokens alongside the system message and tool sequence, and preserving it in place during history pruning. Adds vitest regression coverage.

Closes continuedev#13383

Signed-off-by: David Vance <devops@ribbsaetersystems.com>
@DevOps22X
DevOps22X force-pushed the fix/compile-chat-preserve-user-query-13383 branch from 86a6822 to 439d298 Compare October 19, 2026 20:46
@DevOps22X

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

This branch has not been deployed

No deployments
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.

1 participant