docs: align snip_compact README guards with code

Match the snip_compact teaching snippet (en/zh/ja) to code.py: add the
`head_end > 0` head guard and the `tail_start > 0 and tail_start <
len(messages)` tail bounds check before indexing messages, mirroring the
reactive_compact alignment.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011UJumwVeBXSGYh8nSzoVTF
This commit is contained in:
costajohnt
2026-06-21 16:52:41 -07:00
parent 7659f28976
commit 90a044883f
3 changed files with 12 additions and 6 deletions

View File

@@ -46,10 +46,12 @@ def snip_compact(messages, max_messages=50):
if len(messages) <= max_messages:
return messages
head_end, tail_start = 3, len(messages) - (max_messages - 3)
if _message_has_tool_use(messages[head_end - 1]):
if head_end > 0 and _message_has_tool_use(messages[head_end - 1]):
while head_end < len(messages) and _is_tool_result_message(messages[head_end]):
head_end += 1
if _is_tool_result_message(messages[tail_start]) and _message_has_tool_use(messages[tail_start - 1]):
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
snipped = tail_start - head_end
placeholder = {"role": "user", "content": f"[snipped {snipped} messages from conversation middle]"}