diff --git a/s08_context_compact/README.en.md b/s08_context_compact/README.en.md index d738b946..dcf254cc 100644 --- a/s08_context_compact/README.en.md +++ b/s08_context_compact/README.en.md @@ -140,7 +140,9 @@ This triggers **reactive_compact**: more aggressive than compact_history, it ret def reactive_compact(messages): transcript = write_transcript(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]): + 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", diff --git a/s08_context_compact/README.ja.md b/s08_context_compact/README.ja.md index 89f55ee2..72cae428 100644 --- a/s08_context_compact/README.ja.md +++ b/s08_context_compact/README.ja.md @@ -140,7 +140,9 @@ API がまだ `prompt_too_long`(413)を返すことがある。コンテキ def reactive_compact(messages): transcript = write_transcript(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]): + 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", diff --git a/s08_context_compact/README.md b/s08_context_compact/README.md index db71a6cf..771257d4 100644 --- a/s08_context_compact/README.md +++ b/s08_context_compact/README.md @@ -140,7 +140,9 @@ def compact_history(messages): def reactive_compact(messages): transcript = write_transcript(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]): + 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",