Skip to content

Sub-agents enter infinite loop of session lifecycle calls (mem_session_summary) #128

@jzambrano12

Description

@jzambrano12

📋 Pre-flight Checks

  • I have searched existing issues and this is not a duplicate
  • I understand this issue needs status:approved before 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:

  1. session-start.sh hook — injects into main agent context at startup
  2. memory/SKILL.md — marked "ALWAYS ACTIVE", inherited by sub-agents
  3. 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

  1. Configure Claude Code with bypassPermissions enabled
  2. Have the engram plugin installed and active
  3. Start a new session and ask any question that triggers the orchestrator to launch an Explore sub-agent (e.g., exploring a codebase)
  4. Observe the sub-agent enters an infinite loop of mem_session_start, mem_context, and mem_session_summary calls
  5. 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, or mem_session_summary
  • DO NOT follow the SESSION CLOSE PROTOCOL
  • MAY call mem_save for genuinely important discoveries (once, not repeatedly)
  • MAY call mem_search or mem_context if 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 COMPACTIONAFTER 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions