-
Notifications
You must be signed in to change notification settings - Fork 207
Description
📋 Pre-flight Checks
- I have searched existing issues and this is not a duplicate
- I understand this issue needs
status:approvedbefore a PR can be opened
📝 Bug Description
When Claude Code sub-agents (especially Explore agents) are launched by an orchestrator, they inherit the engram memory protocol from multiple injection sources — all marking SESSION CLOSE PROTOCOL as mandatory. The sub-agent interprets its task completion as a "session close" and enters an infinite loop of engram MCP calls:
session_start → mem_context → [actual work] → mem_session_summary → mem_context → mem_session_summary → session_start → mem_context → mem_session_summary → ...
The sub-agent saves the same session summary multiple times with slight variations, never terminating. With bypassPermissions enabled, all MCP calls auto-approve and the loop runs unchecked — sessions can hang for up to 1 hour until the user manually cancels.
Root cause: The engram protocol is injected into sub-agents through overlapping sources:
session-start.shhook — injects into main agent context at startupmemory/SKILL.md— marked "ALWAYS ACTIVE", inherited by sub-agents- User's CLAUDE.md — may contain additional copies of the protocol
All sources say "SESSION CLOSE PROTOCOL (mandatory)" and "This is NOT optional", which sub-agents follow literally when finishing their delegated task.
🔄 Steps to Reproduce
- Configure Claude Code with
bypassPermissionsenabled - Have the engram plugin installed and active
- Start a new session and ask any question that triggers the orchestrator to launch an Explore sub-agent (e.g., exploring a codebase)
- Observe the sub-agent enters an infinite loop of
mem_session_start,mem_context, andmem_session_summarycalls - The session hangs indefinitely until manually cancelled
✅ Expected Behavior
Sub-agents should complete their delegated task and return results to the parent agent without calling session lifecycle tools (mem_session_start, mem_session_end, mem_session_summary). Session lifecycle should only be managed by the top-level agent that directly interacts with the human user.
❌ Actual Behavior
Sub-agents enter an infinite loop of session lifecycle calls. Example trace from an Explore sub-agent:
plugin:engram:engram - Start Session (MCP)(id: "mp-web-session-continue", project: "mp-web")
plugin:engram:engram - Get Memory Context (MCP)(project: "mp-web", limit: 30)
plugin:engram:engram - Start Session (MCP)(id: "continue-auth-exploration-2026-03-26", project: "mp-web")
plugin:engram:engram - Get Memory Context (MCP)(project: "mp-web", limit: 30)
[... actual exploration work ...]
plugin:engram:engram - Get Memory Context (MCP)(project: "mp-web", limit: 20)
plugin:engram:engram - Save Session Summary (MCP)(project: "mp-web", content: "## Goal\n...")
plugin:engram:engram - Get Memory Context (MCP)(project: "mp-web", limit: 20)
plugin:engram:engram - Save Session Summary (MCP)(project: "mp-web", content: "## Goal\n...") # same content, slight variation
plugin:engram:engram - Start Session (MCP)(id: "mp-web-session-continued", project: "mp-web")
plugin:engram:engram - Get Memory Context (MCP)(project: "mp-web", limit: 20)
plugin:engram:engram - Save Session Summary (MCP)(project: "mp-web", content: "## Goal\n...") # repeats indefinitely
Workaround: Cancel the stuck operation and tell the agent "Continua" — the main agent then proceeds normally.
🖥️ Environment
- Operating System: macOS
- Engram Version: 1.10.8
- Agent / Client: Claude Code
💡 Additional Context
Proposed fix: Add a "SUB-AGENT / DELEGATED TASK SCOPE" section to the engram protocol (memory/SKILL.md and session-start.sh injected text) that explicitly tells sub-agents:
- DO NOT call
mem_session_start,mem_session_end, ormem_session_summary - DO NOT follow the SESSION CLOSE PROTOCOL
- MAY call
mem_savefor genuinely important discoveries (once, not repeatedly) - MAY call
mem_searchormem_contextif needed for the task - When done, return result to parent agent — that is the only "close" action
Also scope the existing headers: SESSION CLOSE PROTOCOL (mandatory) → SESSION CLOSE PROTOCOL (mandatory — TOP-LEVEL AGENT ONLY) and AFTER COMPACTION → AFTER COMPACTION (TOP-LEVEL AGENT ONLY).
This fix has been implemented and validated in gentle-ai for the engram-protocol.md asset that gets injected into CLAUDE.md. Applying the same pattern to the plugin's memory/SKILL.md and session-start.sh would fix it at the source for all users.