Skip to content

Compact intermediate tool calls in message history - #25

Merged
Rikul merged 4 commits into
mainfrom
claude/keen-newton-hzxgru
Oct 8, 2026
Merged

Rikul merged 4 commits into
mainfrom
claude/keen-newton-hzxgru

Conversation

@Rikul

@Rikul Rikul commented Oct 7, 2026

Copy link
Copy Markdown
Owner

Summary

This change improves message history storage by compacting intermediate tool calls within a single agent turn. Instead of storing full tool call messages with arguments and results, only the tool names are preserved in a (used tools: ...) summary line appended to the assistant's text. This reduces storage overhead while maintaining conversation context.

Key Changes

  • New strip_tool_summary() function in app/core/agent.py: Removes the (used tools: ...) line from assistant messages, allowing callers to recover the original text if needed.

  • New Agent._compact_turn() classmethod: Processes a turn's message chain (assistant messages with tool calls + tool results) and produces a compacted list of assistant text messages. Intermediate text is preserved, tool names are collected into a summary line, and tool arguments/results/reasoning are dropped (kept only in traces).

  • Updated agent_loop() in both CliAgent and BackgroundAgent: Now calls _compact_turn() to process the turn's messages before appending to self.messages and MessageHistory. This ensures both the in-memory message list and the persistent history store the compacted format.

  • Updated documentation: MessageHistory docstring clarified to reflect that tool calls are now reduced to a summary line rather than stored in full.

Implementation Details

  • The compaction logic groups consecutive tool calls by their preceding assistant text, preserving the semantic flow of the conversation.
  • A turn that ends mid-tool-call (due to stop or max iterations) is handled correctly—the final message is appended only if the turn doesn't already end on a tool-call message.
  • The regex pattern _TOOL_SUMMARY_RE matches the summary line only at the end of a message (after optional \n\n), ensuring similar text elsewhere in the message is not removed.
  • All four test cases cover edge cases: normal multi-tool turns, tool-only messages, cut-short turns, and turns with no tool calls.

https://claude.ai/code/session_01E7PTP6WSGTKfqwPiL2wvBc

claude added 2 commits October 7, 2026 23:17
Instead of saving only the user prompt and final reply, each turn now
stores intermediate assistant text plus a one-line summary of the tool
calls that followed it (name + truncated args). Tool results and
reasoning are dropped; traces and logs still have everything.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7PTP6WSGTKfqwPiL2wvBc
Replace the [tool calls: name(args)] summary, which looks like call
syntax the model might imitate, with a plain trailing line listing the
deduped tool names. Add strip_tool_summary() to remove it again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7PTP6WSGTKfqwPiL2wvBc
@Rikul
Rikul requested a balanced review from Copilot October 7, 2026 23:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The implementation increases persisted history despite the stated storage-reduction goal.

1 open finding
What changed in this PR

Adds compact tool-use summaries to assistant message history while dropping tool arguments, results, and reasoning.

Changes:

  • Adds turn compaction and summary stripping.
  • Persists compacted messages in CLI and background agents.
  • Adds tests and updates documentation.
File Description
app/​core/​agent.py Implements turn compaction.
app/​cli/​cli_agent.py Persists compacted CLI turns.
app/​core/​background_agent.py Persists compacted background turns.
app/​infra/​message_history.py Updates storage documentation.
tests/​test_agent.py Tests compaction and persistence.
CLAUDE.md Documents the new history behavior.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment thread app/core/agent.py
for text, names in entries:
summary = f"{TOOL_SUMMARY_PREFIX}{', '.join(names)})"
content = f"{text}\n\n{summary}" if text else summary
compacted.append({"role": "assistant", "content": content})
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7PTP6WSGTKfqwPiL2wvBc
@Rikul
Rikul requested a balanced review from Copilot October 7, 2026 23:57

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Variable-length persisted turns can break history boundaries and exclude final replies from automatic title generation.

1 open finding
Previously missed (1)

In code that hasn't changed since last review

Medium severity Auto-naming omits final answer after expanded tool messages

app/​cli/​cli_agent.py:105

This expanded list is also passed unchanged to _auto_name(), which only examines messages[:4] (app/core/agent.py:269-271). On a first turn with three text-bearing tool rounds, those slots become the user plus three intermediate messages, so the final answer is excluded from title generation. Please make auto-naming select the first user and final assistant response explicitly rather than relying on the first four stored rows.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Turns now store intermediate assistant messages, so conversations
reach the limit sooner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7PTP6WSGTKfqwPiL2wvBc
@Rikul
Rikul marked this pull request as ready for review October 8, 2026 00:33
@Rikul
Rikul requested a balanced review from Copilot October 8, 2026 00:33
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-08T00:35:31.545590Z 2c42dc1 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The compaction behavior is consistent across agent types and covers the described edge cases with focused tests.

1 open finding

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

@Rikul
Rikul merged commit fc91b8d into main Oct 8, 2026
3 checks passed
Rikul added a commit that referenced this pull request Oct 13, 2026
This reverts commit fc91b8d because persisted tool summaries leaked into later model responses. Keep only the user prompt and final assistant reply in durable history.
@Rikul
Rikul deleted the claude/keen-newton-hzxgru branch October 21, 2026 20:48
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.

3 participants