From 476efb399454e2605fc77c4ec2e69c2a27966479 Mon Sep 17 00:00:00 2001 From: giveen Date: Thu, 2 Apr 2026 16:57:19 -0600 Subject: [PATCH] fix: inject compacted summary back into message_history and sharpen compaction prompt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After compaction, message_history.clear() wiped all context and the stored summary in COMPACTED_SUMMARIES was never re-injected, so the next runner turn started completely blank and the agent would repeat already-tried approaches. Fix: immediately after clearing, push a user+assistant exchange containing the summary into message_history so it flows through history_context on the next iteration as normal conversation context. Also: add an explicit 'Exhausted Approaches — DO NOT RETRY' section (§9) and 'Recommended Next Steps' section (§10) to the compaction prompt so the support model produces a checklist of dead ends the main agent must not revisit. --- src/cai/repl/commands/memory.py | 4 +++- .../agents/models/openai_chatcompletions.py | 22 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/cai/repl/commands/memory.py b/src/cai/repl/commands/memory.py index 9f963d4e..f2e283c8 100644 --- a/src/cai/repl/commands/memory.py +++ b/src/cai/repl/commands/memory.py @@ -1221,7 +1221,8 @@ After the analysis, provide a structured summary with these sections: 6. **All User Messages**: Complete list of user messages in order 7. **Pending Tasks**: What still needs to be done 8. **Current Work**: What was being worked on when the conversation ended -9. **Optional Next Step**: If there's a clear next action, mention it +9. **Exhausted Approaches — DO NOT RETRY**: Every technique, command, path, or attack vector that was attempted and failed. Format each as a bullet starting with ❌. Be specific (include exact commands, URLs, usernames, ports). This section is CRITICAL — the agent will use it to avoid wasting time on dead ends. +10. **Recommended Next Steps**: Concrete actions NOT yet tried, ordered by likelihood of success. ## Important Guidelines @@ -1232,6 +1233,7 @@ After the analysis, provide a structured summary with these sections: - Maintain technical accuracy - don't paraphrase technical terms - The summary will be used as the primary context for resuming work, so completeness is crucial - When the conversation is resumed, it should feel like a natural continuation +- Section 9 (Exhausted Approaches) is the most important section for offensive/hacking tasks: list every failed attempt so the agent doesn't loop. This session is being continued from a previous conversation that ran out of context. The conversation is summarized below:""" diff --git a/src/cai/sdk/agents/models/openai_chatcompletions.py b/src/cai/sdk/agents/models/openai_chatcompletions.py index eac4cf30..4390f82d 100644 --- a/src/cai/sdk/agents/models/openai_chatcompletions.py +++ b/src/cai/sdk/agents/models/openai_chatcompletions.py @@ -3525,6 +3525,28 @@ class OpenAIChatCompletionsModel(Model): APPLIED_MEMORY_IDS[self.agent_name] = [] COMPACTED_SUMMARIES[self.agent_name] = [_summary] self.message_history.clear() + # Re-inject the summary as the first exchange so + # the next Runner turn has full context and won't + # repeat work that was already attempted. + self.message_history.append({ + "role": "user", + "content": ( + "\n" + + _summary + + "\n\n\n" + "This is your memory from the previous context window. " + "Use it to continue your work. " + "Do NOT retry any approach already marked as failed or exhausted." + ), + }) + self.message_history.append({ + "role": "assistant", + "content": ( + "Understood. I have reviewed my previous session memory. " + "I will continue the task using only new approaches " + "and will not repeat anything already attempted." + ), + }) os.environ["CAI_CONTEXT_USAGE"] = "0.0" _console.print( "[bold green]✓ Memory summary applied — "