Skip to content

fix(plugin): prevent sub-agent infinite loop on session lifecycle calls#129

Open
jzambrano12 wants to merge 1 commit intoGentleman-Programming:mainfrom
jzambrano12:fix/subagent-session-loop
Open

fix(plugin): prevent sub-agent infinite loop on session lifecycle calls#129
jzambrano12 wants to merge 1 commit intoGentleman-Programming:mainfrom
jzambrano12:fix/subagent-session-loop

Conversation

@jzambrano12
Copy link
Copy Markdown

Closes #128

Summary

  • Add "SUB-AGENT SCOPE" sections to session-start.sh, post-compaction.sh, and memory/SKILL.md that tell sub-agents to skip session lifecycle tools (mem_session_start, mem_session_end, mem_session_summary)
  • Scope SESSION CLOSE and AFTER COMPACTION headers as TOP-LEVEL AGENT ONLY so the mandatory language no longer applies to delegated tasks
  • Sub-agents may still call mem_save (once) and mem_search/mem_context as needed

Changes

File Change
plugin/claude-code/scripts/session-start.sh Added SUB-AGENT SCOPE section, scoped SESSION CLOSE as TOP-LEVEL AGENT ONLY
plugin/claude-code/scripts/post-compaction.sh Added SUB-AGENT SCOPE section, scoped SESSION CLOSE and POST-COMPACTION as TOP-LEVEL AGENT ONLY
plugin/claude-code/skills/memory/SKILL.md Added SUB-AGENT / DELEGATED TASK SCOPE section, scoped SESSION CLOSE PROTOCOL and AFTER COMPACTION as TOP-LEVEL AGENT ONLY

Test plan

  • go test ./... — 596 tests pass, no regressions
  • Manual: start a Claude Code session with bypassPermissions, trigger an Explore sub-agent, confirm it no longer loops on mem_session_summary
  • Manual: verify top-level agent still calls mem_session_summary on session close
  • Manual: verify sub-agents can still call mem_save for important discoveries

Checklist

  • Commit messages follow conventional commit format
  • No Co-Authored-By trailers
  • Changes are limited to protocol text — no Go code or infrastructure changes

Sub-agents (Explore, etc.) inherit the engram memory protocol and
interpret their task completion as a session close, entering an infinite
loop of mem_session_summary → mem_context → mem_session_summary calls.
With bypassPermissions enabled, sessions hang for up to 1 hour.

Add "SUB-AGENT SCOPE" sections to the injected protocol text in
session-start.sh, post-compaction.sh, and memory/SKILL.md that
explicitly tell sub-agents to skip session lifecycle tools
(mem_session_start, mem_session_end, mem_session_summary).

Scope SESSION CLOSE and AFTER COMPACTION headers as TOP-LEVEL AGENT
ONLY so the mandatory language no longer applies to delegated tasks.

Closes Gentleman-Programming#128
MattKotsenas added a commit to MattKotsenas/engram that referenced this pull request Mar 27, 2026
Includes PR Gentleman-Programming#129 sub-agent scope guards. Ensures server running,
creates session, imports git-synced chunks, injects memory protocol
with PROACTIVE SAVE, SEARCH MEMORY, SUB-AGENT SCOPE, and
SESSION CLOSE (TOP-LEVEL AGENT ONLY) sections.
5 Pester tests verifying protocol output content.
MattKotsenas added a commit to MattKotsenas/engram that referenced this pull request Mar 27, 2026
- post-compaction.ps1: protocol re-injection with PR Gentleman-Programming#129 scope guards
- session-stop.ps1: session end REST call
- subagent-stop.ps1: passive capture POST
- user-prompt-submit.ps1: first-message ToolSearch + save nudge timer
10 Pester tests covering protocol output, exit codes, first-message
detection, and second-message empty response.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant