mirror of
https://github.com/shareAI-lab/analysis_claude_code.git
synced 2026-08-06 12:03:37 +08:00
refactor: summarize only trimmed history in reactive_compact
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 #350
This commit is contained in:
@@ -139,10 +139,10 @@ This triggers **reactive_compact**: more aggressive than compact_history, it ret
|
||||
```python
|
||||
def reactive_compact(messages):
|
||||
transcript = write_transcript(messages)
|
||||
summary = summarize_history(messages)
|
||||
tail_start = max(0, len(messages) - 5)
|
||||
if _is_tool_result_message(messages[tail_start]) and _message_has_tool_use(messages[tail_start - 1]):
|
||||
tail_start -= 1
|
||||
summary = summarize_history(messages[:tail_start])
|
||||
return [{"role": "user",
|
||||
"content": f"[Reactive compact]\n\n{summary}"}, *messages[tail_start:]]
|
||||
```
|
||||
|
||||
@@ -139,10 +139,10 @@ API がまだ `prompt_too_long`(413)を返すことがある。コンテキ
|
||||
```python
|
||||
def reactive_compact(messages):
|
||||
transcript = write_transcript(messages)
|
||||
summary = summarize_history(messages)
|
||||
tail_start = max(0, len(messages) - 5)
|
||||
if _is_tool_result_message(messages[tail_start]) and _message_has_tool_use(messages[tail_start - 1]):
|
||||
tail_start -= 1
|
||||
summary = summarize_history(messages[:tail_start])
|
||||
return [{"role": "user",
|
||||
"content": f"[Reactive compact]\n\n{summary}"}, *messages[tail_start:]]
|
||||
```
|
||||
|
||||
@@ -139,10 +139,10 @@ def compact_history(messages):
|
||||
```python
|
||||
def reactive_compact(messages):
|
||||
transcript = write_transcript(messages)
|
||||
summary = summarize_history(messages)
|
||||
tail_start = max(0, len(messages) - 5)
|
||||
if _is_tool_result_message(messages[tail_start]) and _message_has_tool_use(messages[tail_start - 1]):
|
||||
tail_start -= 1
|
||||
summary = summarize_history(messages[:tail_start])
|
||||
return [{"role": "user",
|
||||
"content": f"[Reactive compact]\n\n{summary}"}, *messages[tail_start:]]
|
||||
```
|
||||
|
||||
@@ -382,12 +382,12 @@ def compact_history(messages):
|
||||
# Emergency: reactiveCompact — on API error
|
||||
def reactive_compact(messages):
|
||||
transcript = write_transcript(messages)
|
||||
summary = summarize_history(messages)
|
||||
tail_start = max(0, len(messages) - 5)
|
||||
if (tail_start > 0 and tail_start < len(messages)
|
||||
and _is_tool_result_message(messages[tail_start])
|
||||
and _message_has_tool_use(messages[tail_start - 1])):
|
||||
tail_start -= 1
|
||||
summary = summarize_history(messages[:tail_start])
|
||||
return [{"role": "user", "content": f"[Reactive compact]\n\n{summary}"}, *messages[tail_start:]]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user