Skip to content

fix(honcho): prevent orphan sessions + restore gateway_session_key#8424

Open
Kathie-yu wants to merge 2 commits into
NousResearch:mainfrom
Kathie-yu:fix/honcho-orphan-sessions
Open

fix(honcho): prevent orphan sessions + restore gateway_session_key#8424
Kathie-yu wants to merge 2 commits into
NousResearch:mainfrom
Kathie-yu:fix/honcho-orphan-sessions

Conversation

@Kathie-yu

@Kathie-yu Kathie-yu commented Apr 12, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes two related Honcho memory provider issues observed after #4623 and later made visible by #6995 eager init behavior:

  1. Orphan sessions from temporary agents — Three internal code paths constructed AIAgent instances without skip_memory=True, triggering Honcho eager init and creating sessions that were never written to:

    • gateway/run.py — hygiene auto-compress and /compress temp agents
    • plugins/memory/honcho/__init__.pymigrate_memory_files ran unconditionally under per-session strategy
    • run_agent.py_spawn_background_review created a full agent for local memory extraction but unnecessarily initialised Honcho
  2. Session 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) removed honcho_session_key from AIAgent, 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

  • Restores stable per-chat Honcho session continuity in gateway chats. Turns from the same Telegram/Discord chat accumulate into one stable Honcho session instead of being split across ephemeral Hermes session IDs.
  • Prevents long-memory fragmentation caused by per-session slicing in gateway scenarios — Honcho's observation/conclusion/representation extraction requires sustained turn accumulation to form useful context.
  • Eliminates orphan session pollution — temporary agents (compress, hygiene, background review) no longer create empty Honcho sessions that waste storage and pollute the session list.

Non-claim

  • This PR does not change non-gateway per-session semantics when no gateway_session_key is provided (CLI path unaffected).
  • This PR does not address the /task background agent path (line 5227) which also lacks skip_memory — that is a lower-priority item due to infrequent use.

Changes

Commit 1: fix(honcho): prevent orphan Honcho sessions from temporary agents

File Change
gateway/run.py Add skip_memory=True to hygiene and /compress temp agents
plugins/memory/honcho/__init__.py Short-circuit migrate_memory_files when session_strategy == "per-session"
run_agent.py Add skip_memory=True to _spawn_background_review agent
tests/honcho_plugin/test_session.py +2 tests: per-session migrate guard (skipped) + per-directory migrate (preserved)

Commit 2: fix(honcho): restore gateway_session_key for stable per-chat session isolation

File Change
gateway/run.py Pass gateway_session_key=session_key to main agent constructor
run_agent.py Accept, store, and forward gateway_session_key via _init_kwargs
plugins/memory/honcho/__init__.py Extract and pass gateway_session_key to session name resolver
plugins/memory/honcho/client.py Add gateway_session_key as priority level in resolve_session_name
tests/honcho_plugin/test_client.py +4 tests: gateway_key > per-session, title > gateway_key, CLI fallback, sanitization

Regression introduction

Validation

  • Unit tests: tests/honcho_plugin/140 passed (134 existing + 6 new regression tests)
  • E2E (Telegram gateway):
    • Send messages → turns write to stable agent-main-telegram-dm-<CHAT_SCOPE_ID> session
    • /new → same stable Honcho session continues accumulating turns
    • /compress → no new orphan sessions created
    • Gateway restart → no orphan sessions from re-initialization
    • 💾 Memory updated notifications continue working (local memory path unaffected by skip_memory)

Kathie yu and others added 2 commits April 12, 2026 22:26
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>
@Kathie-yu Kathie-yu marked this pull request as ready for review April 12, 2026 15:02
Copilot AI review requested due to automatic review settings April 12, 2026 15:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_key from the gateway into the Honcho session name resolver to restore stable per-chat Honcho sessions.
  • Add skip_memory=True to 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.

Comment thread gateway/run.py
Comment on lines 3356 to 3360
model=_hyg_model,
max_iterations=4,
quiet_mode=True,
skip_memory=True,
enabled_toolsets=["memory"],

Copilot AI Apr 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread gateway/run.py
model=model,
max_iterations=4,
quiet_mode=True,
skip_memory=True,

Copilot AI Apr 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
skip_memory=True,
skip_memory=False,

Copilot uses AI. Check for mistakes.
# cwd-based strategies cannot provide.
if gateway_session_key:
sanitized = re.sub(r'[^a-zA-Z0-9_-]', '-', gateway_session_key).strip('-')
if sanitized:

Copilot AI Apr 12, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
if sanitized:
if sanitized:
if self.session_peer_prefix and self.peer_name:
return f"{self.peer_name}-{sanitized}"

Copilot uses AI. Check for mistakes.
@Kathie-yu Kathie-yu closed this Apr 12, 2026
@Kathie-yu Kathie-yu deleted the fix/honcho-orphan-sessions branch April 12, 2026 18:13
@Kathie-yu Kathie-yu restored the fix/honcho-orphan-sessions branch April 12, 2026 18:16
@Kathie-yu Kathie-yu reopened this Apr 12, 2026
@alt-glitch alt-glitch added type/bug Something isn't working P3 Low — cosmetic, nice to have comp/plugins Plugin system and bundled plugins comp/gateway Gateway runner, session dispatch, delivery tool/memory Memory tool and memory providers labels Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery comp/plugins Plugin system and bundled plugins P3 Low — cosmetic, nice to have tool/memory Memory tool and memory providers type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants