From 62a03e86882b59ff2ca496b435fa5044f970ba56 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 28 Jan 2026 21:40:39 +0000 Subject: [PATCH] fix: prevent hallucination after failed compaction summary - Improve FALLBACK_SUMMARY to instruct agent to check CURRENT_WORK.md - Add clear instructions to read memory files before responding - Warn against hallucinating non-existent conversations - Make memory flush prompt explicitly require CURRENT_WORK.md update - Strengthen system prompt to emphasize this is mandatory This prevents the agent from continuing conversations that never happened when compaction summarization fails (e.g., no API key, network error, etc.). --- PROPOSED_CHANGES.md | 47 +++++++++++++++++++ .../pi-extensions/compaction-safeguard.ts | 15 +++++- src/auto-reply/reply/memory-flush.ts | 15 ++++-- 3 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 PROPOSED_CHANGES.md diff --git a/PROPOSED_CHANGES.md b/PROPOSED_CHANGES.md new file mode 100644 index 000000000..d938895bc --- /dev/null +++ b/PROPOSED_CHANGES.md @@ -0,0 +1,47 @@ +# Proposed Clawdbot Improvements + +## Issue: Hallucination after failed compaction summary + +When compaction summarization fails (no API key, error, etc.), the agent receives: +"Summary unavailable due to context limits. Older messages were truncated." + +This provides ZERO context, leading to hallucination about non-existent conversations. + +## Proposed Fix 1: Better fallback message + +In `src/agents/pi-extensions/compaction-safeguard.ts`: + +```typescript +const FALLBACK_SUMMARY = [ + "Summary unavailable due to context limits. Older messages were truncated.", + "", + "⚠️ IMPORTANT: You have NO context from previous messages.", + "Before continuing:", + "1. Read CURRENT_WORK.md if it exists", + "2. Read memory/YYYY-MM-DD.md for today's context", + "3. ASK the user what they were working on if unclear", + "4. DO NOT assume or hallucinate previous conversation content", +].join("\n"); +``` + +## Proposed Fix 2: Better default memory flush prompt + +In `src/auto-reply/reply/memory-flush.ts`: + +```typescript +export const DEFAULT_MEMORY_FLUSH_PROMPT = [ + "Pre-compaction memory flush.", + "MANDATORY: Update CURRENT_WORK.md with the current task and context.", + "Also store durable memories to memory/YYYY-MM-DD.md.", + `If nothing to store, reply with ${SILENT_REPLY_TOKEN}.`, +].join(" "); +``` + +## Why this matters + +Without proper context recovery instructions, Claude will: +- Hallucinate about previous conversations +- Continue non-existent tasks +- Confuse users with irrelevant responses + +The fix ensures the agent knows to CHECK files and ASK rather than assume. diff --git a/src/agents/pi-extensions/compaction-safeguard.ts b/src/agents/pi-extensions/compaction-safeguard.ts index b2fe39884..e1860b515 100644 --- a/src/agents/pi-extensions/compaction-safeguard.ts +++ b/src/agents/pi-extensions/compaction-safeguard.ts @@ -12,8 +12,19 @@ import { summarizeInStages, } from "../compaction.js"; import { getCompactionSafeguardRuntime } from "./compaction-safeguard-runtime.js"; -const FALLBACK_SUMMARY = - "Summary unavailable due to context limits. Older messages were truncated."; +const FALLBACK_SUMMARY = [ + "Summary unavailable due to context limits. Older messages were truncated.", + "", + "⚠️ CRITICAL: You have NO context from previous messages.", + "Before responding, you MUST:", + "1. Read CURRENT_WORK.md if it exists - this contains your active task", + "2. Read memory/YYYY-MM-DD.md for today's context", + "3. ASK the user what they need if context is still unclear", + "4. DO NOT assume or hallucinate previous conversation content", + "", + "If you proceed without checking these files, you risk responding to", + "conversations that never happened.", +].join("\n"); const TURN_PREFIX_INSTRUCTIONS = "This summary covers the prefix of a split turn. Focus on the original request," + " early progress, and any details needed to understand the retained suffix."; diff --git a/src/auto-reply/reply/memory-flush.ts b/src/auto-reply/reply/memory-flush.ts index 9ad14b0ba..acf9e04cd 100644 --- a/src/auto-reply/reply/memory-flush.ts +++ b/src/auto-reply/reply/memory-flush.ts @@ -9,14 +9,21 @@ export const DEFAULT_MEMORY_FLUSH_SOFT_TOKENS = 4000; export const DEFAULT_MEMORY_FLUSH_PROMPT = [ "Pre-compaction memory flush.", - "Store durable memories now (use memory/YYYY-MM-DD.md; create memory/ if needed).", + "MANDATORY: Update CURRENT_WORK.md with your current task, context, and any important state.", + "Also store durable memories to memory/YYYY-MM-DD.md (create memory/ if needed).", + "This is critical to avoid context loss and hallucination after compaction.", `If nothing to store, reply with ${SILENT_REPLY_TOKEN}.`, ].join(" "); export const DEFAULT_MEMORY_FLUSH_SYSTEM_PROMPT = [ - "Pre-compaction memory flush turn.", - "The session is near auto-compaction; capture durable memories to disk.", - `You may reply, but usually ${SILENT_REPLY_TOKEN} is correct.`, + "⚠️ PRE-COMPACTION MEMORY FLUSH - ACTION REQUIRED.", + "The session is near auto-compaction. You MUST update CURRENT_WORK.md with:", + "- Current task description", + "- Important context and decisions", + "- Any state needed to resume after compaction", + "Also write to memory/YYYY-MM-DD.md for daily logs.", + "Failure to do this will result in context loss and potential hallucination.", + `After writing, reply with ${SILENT_REPLY_TOKEN}.`, ].join(" "); export type MemoryFlushSettings = {