fix(honcho): prevent orphan sessions + restore gateway_session_key#8424
fix(honcho): prevent orphan sessions + restore gateway_session_key#8424Kathie-yu wants to merge 2 commits into
Conversation
Three code paths constructed temporary AIAgent instances without skip_memory=True, causing the Honcho memory provider to initialise and create empty sessions that were never written to: 1. gateway/run.py — hygiene auto-compress and /compress temp agents lacked skip_memory=True (flush agent at line 717 already had it) 2. plugins/memory/honcho/__init__.py — per-session strategy ran migrate_memory_files on every new session, uploading MEMORY.md / USER.md / SOUL.md into short-lived sessions that were immediately abandoned after session rotation 3. run_agent.py — _spawn_background_review created a full AIAgent to review conversation history for local memory extraction, but did not need Honcho (it uses the shared _memory_store directly) All three now pass skip_memory=True or short-circuit appropriately, consistent with the existing convention across 20+ call sites. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…isolation The plugin architecture refactor (924bc67, 2026-04-02) removed the honcho_session_key parameter from AIAgent, breaking the gateway → Honcho session key plumbing. This caused all Honcho sessions to use bare timestamp IDs (e.g. 20260412_171002_xxx) instead of the stable per-chat key (e.g. agent-main-telegram-dm-8439114563), fragmenting conversation history across disposable sessions. Fix: thread gateway_session_key through the full path: 1. gateway/run.py — pass gateway_session_key=session_key to AIAgent 2. run_agent.py — accept, store, and forward via _init_kwargs 3. plugins/memory/honcho/__init__.py — extract and pass to resolver 4. plugins/memory/honcho/client.py — add gateway_session_key as a new priority level in resolve_session_name (after /title override, before per-session fallback) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Fixes Honcho memory provider session handling in gateway scenarios by preventing “temporary” agents from creating orphan Honcho sessions and restoring stable per-chat session naming via a gateway-provided session key.
Changes:
- Thread
gateway_session_keyfrom the gateway into the Honcho session name resolver to restore stable per-chat Honcho sessions. - Add
skip_memory=Trueto temporary agents (gateway hygiene +/compress, and background review) to avoid triggering Honcho eager init and creating orphan sessions. - Guard Honcho memory-file migration so it doesn’t run under
session_strategy == "per-session", and add regression tests for both migration behavior and session-name resolution priority.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
gateway/run.py |
Passes gateway_session_key to main agent; sets skip_memory=True on temp agents to avoid Honcho orphan sessions. |
run_agent.py |
Adds gateway_session_key plumbed into memory-provider initialization; background review agent now skips memory init. |
plugins/memory/honcho/__init__.py |
Forwards gateway_session_key into session resolution; skips memory-file migration for per-session strategy. |
plugins/memory/honcho/client.py |
Adds gateway_session_key priority in resolve_session_name() and sanitizes it for Honcho compatibility. |
tests/honcho_plugin/test_session.py |
Adds tests asserting migration is skipped for per-session and runs for per-directory. |
tests/honcho_plugin/test_client.py |
Adds tests asserting gateway key priority and sanitization in session-name resolution. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| model=_hyg_model, | ||
| max_iterations=4, | ||
| quiet_mode=True, | ||
| skip_memory=True, | ||
| enabled_toolsets=["memory"], |
There was a problem hiding this comment.
skip_memory=True prevents the built-in MemoryStore and memory provider plugins from initializing, but _compress_context() always calls flush_memories() before compression. With skip_memory=True, flush_memories() will always hit MemoryStore is None and the pre-compression memory flush becomes a no-op.
If the intent is to still preserve memory before truncating the transcript, consider initializing/reusing a MemoryStore for this temp agent (without initializing Honcho), or alternatively avoid _compress_context() and call context_compressor.compress() directly when you explicitly want compression to have zero memory side effects.
| model=model, | ||
| max_iterations=4, | ||
| quiet_mode=True, | ||
| skip_memory=True, |
There was a problem hiding this comment.
Same issue as the hygiene temp agent: skip_memory=True means _compress_context()'s pre-compression flush_memories() cannot write anything because self._memory_store was never initialized. If /compress is supposed to preserve durable memory before dropping context, consider wiring a MemoryStore into this temp agent (without initializing Honcho), or bypass _compress_context() when you intentionally want no memory writes/notifications during compression.
| skip_memory=True, | |
| skip_memory=False, |
| # cwd-based strategies cannot provide. | ||
| if gateway_session_key: | ||
| sanitized = re.sub(r'[^a-zA-Z0-9_-]', '-', gateway_session_key).strip('-') | ||
| if sanitized: |
There was a problem hiding this comment.
The gateway_session_key branch returns the sanitized key without applying session_peer_prefix/peer_name, unlike the session_title/per-session/per-repo/per-directory branches. If sessionPeerPrefix is enabled in config, this effectively gets ignored for gateway-derived sessions and can break the expected isolation/naming scheme.
Consider applying the same peer-prefix rule here as well (when session_peer_prefix and peer_name are set).
| if sanitized: | |
| if sanitized: | |
| if self.session_peer_prefix and self.peer_name: | |
| return f"{self.peer_name}-{sanitized}" |
Summary
Fixes two related Honcho memory provider issues observed after #4623 and later made visible by #6995 eager init behavior:
Orphan sessions from temporary agents — Three internal code paths constructed
AIAgentinstances withoutskip_memory=True, triggering Honcho eager init and creating sessions that were never written to:gateway/run.py— hygiene auto-compress and/compresstemp agentsplugins/memory/honcho/__init__.py—migrate_memory_filesran unconditionally underper-sessionstrategyrun_agent.py—_spawn_background_reviewcreated a full agent for local memory extraction but unnecessarily initialised HonchoSession key regression from plugin refactor — The pluggable memory provider refactor (feat(memory): pluggable memory provider interface with profile isolation, review fixes, and honcho CLI restoration #4623, commit
924bc67e) removedhoncho_session_keyfromAIAgent, breaking the gateway → Honcho session key plumbing. All Honcho sessions fell back to bare timestamp IDs instead of the stable per-chat key (e.g.agent-main-telegram-dm-<CHAT_SCOPE_ID>), fragmenting conversation history across disposable sessions.Value / Why this matters
Non-claim
gateway_session_keyis provided (CLI path unaffected)./taskbackground agent path (line 5227) which also lacksskip_memory— that is a lower-priority item due to infrequent use.Changes
Commit 1:
fix(honcho): prevent orphan Honcho sessions from temporary agentsgateway/run.pyskip_memory=Trueto hygiene and/compresstemp agentsplugins/memory/honcho/__init__.pymigrate_memory_fileswhensession_strategy == "per-session"run_agent.pyskip_memory=Trueto_spawn_background_reviewagenttests/honcho_plugin/test_session.pyCommit 2:
fix(honcho): restore gateway_session_key for stable per-chat session isolationgateway/run.pygateway_session_key=session_keyto main agent constructorrun_agent.pygateway_session_keyvia_init_kwargsplugins/memory/honcho/__init__.pygateway_session_keyto session name resolverplugins/memory/honcho/client.pygateway_session_keyas priority level inresolve_session_nametests/honcho_plugin/test_client.pyRegression introduction
924bc67e, 2026-04-02) —honcho_session_keywas no longer passed throughAIAgentinitOnSessionStart=truemade the orphan creation visible (eager init exposed paths that lazy init previously masked)Validation
tests/honcho_plugin/— 140 passed (134 existing + 6 new regression tests)agent-main-telegram-dm-<CHAT_SCOPE_ID>session/new→ same stable Honcho session continues accumulating turns/compress→ no new orphan sessions created💾 Memory updatednotifications continue working (local memory path unaffected byskip_memory)