Skip to content

refactor: summarize only trimmed history in reactive_compact#364

Open
costajohnt wants to merge 1 commit into
shareAI-lab:mainfrom
costajohnt:refactor/reactive-compact-summarize-old-history
Open

refactor: summarize only trimmed history in reactive_compact#364
costajohnt wants to merge 1 commit into
shareAI-lab:mainfrom
costajohnt:refactor/reactive-compact-summarize-old-history

Conversation

@costajohnt

Copy link
Copy Markdown

Fixes #350.

reactive_compact is the emergency compaction path that runs after the main call hits prompt_too_long. It was calling summarize_history(messages) on the full history first, then slicing off the last few messages as a verbatim tail. So the kept tail got summarized and then re-attached verbatim, and the emergency path re-summarized the whole oversized context it was trying to shrink.

This moves the summarize_history call below the tail computation (including the tool_use/tool_result pair adjustment) and summarizes only messages[:tail_start], so the summary covers the older history while the recent tail stays verbatim. That matches the intent described in the issue.

The same function is duplicated in s08, s09, and s20, so all three are updated together. s11 keeps its own tail only version since it never calls summarize_history. The s08 README snippets (en/zh/ja) are updated to match.

Added two tests, one for the normal path and one where a tool_use/tool_result pair straddles the tail boundary, confirming the summary excludes the tool_use that gets pulled into the verbatim tail.

Two notes. When a conversation is 5 messages or fewer, tail_start is 0 and the whole list is kept verbatim, so summarize_history is called with an empty list. That is cheaper than the previous full re-summary and nothing is lost, but happy to add a guard to skip the call entirely if you prefer. I also left web/src/data/generated/docs.json untouched since the build regenerates it from source via npm run extract, let me know if you would rather I commit the regenerated file.

reactive_compact summarized the full message history before slicing off
the recent tail, so the kept tail was summarized and then re-attached
verbatim, and the emergency prompt-too-long path re-summarized the whole
oversized context. Move the summarize_history call below the tail
computation (including the tool_use/tool_result pair adjustment) and
summarize only messages[:tail_start], so the summary covers older history
while the recent tail stays verbatim.

Applied to the duplicated function in s08, s09, and s20, with the s08
README snippets (en/zh/ja) updated to match. Adds tests covering the
normal path and the case where a tool pair straddles the tail boundary.

Fixes shareAI-lab#350
@vercel

vercel Bot commented Jun 16, 2026

Copy link
Copy Markdown

@costajohnt is attempting to deploy a commit to the crazyboym's projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

reactive_compact 的执行顺序可能有点容易误解

1 participant