Skip to content

fix: routed-agent context management not respecting history, summarization, compression, and seahorse bootstrap - #3316

Open
j-v wants to merge 4 commits into
sipeed:mainfrom
j-v:upstream/routed-agent-context-fix
Open

fix: routed-agent context management not respecting history, summarization, compression, and seahorse bootstrap#3316
j-v wants to merge 4 commits into
sipeed:mainfrom
j-v:upstream/routed-agent-context-fix

Conversation

@j-v

@j-v j-v commented Aug 3, 2026

Copy link
Copy Markdown

📝 Description

Bug: I set up dispatch rules to route an agent to a specific discord channel, and noticed it wasn't remembering anything from previous messages, and auto-compaction never triggered regardless of the number of messages or tokens used in the session for that agent & channel.

This PR fixes routed-agent context management so that per-agent context (history, summarization, compression, and seahorse bootstrap) operates on the owning agent's session store instead of the default agent's.

For sessions owned by a routed (non-default) agent, three code paths incorrectly used registry.GetDefaultAgent():

  • context_legacy.goAssemble: read history/summary from the default agent's store, so routed agents always saw empty history (the bug reported in [BUG] /clear and session auto-compression don't work in chats routed to non-default agent via dispatch rules #3301).
  • context_legacy.gomaybeSummarize / forceCompression: summarization and overflow compression ran against the default agent's store, leaving the routed agent's context unbounded and uncompressed.
  • context_seahorse.go — constructor: bootstrapped only the default agent's sessions, so routed agents never got their session metadata initialized.

Fix:

  • Assemble, maybeSummarize, and forceCompression now resolve the owning agent via agentForSession(sessionKey) (the same pattern Clear already used), with fallback to the default agent preserved when the session can't be resolved.
  • The seahorse context manager constructor now bootstraps all registered agents' stores (registry.ListAgentIDs()), passing the owning store into bootstrapSession.

Tests: 4 repro tests added covering routed-agent Assemble, summarize, overflow-compression, and seahorse bootstrap — each failed against the old code and passes with the fix.

🗣️ Type of Change

  • 🐞 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 📖 Documentation update
  • ⚡ Code refactoring (no functional changes, no api changes)

🤖 AI Code Generation

  • 🤖 Fully AI-generated (100% AI, 0% Human)
  • 🛠️ Mostly AI-generated (AI draft, Human verified/modified)
  • 👨‍💻 Mostly Human-written (Human lead, AI assisted or none)

🔗 Related Issue

Fixes #3301

📚 Technical Context (Skip for Docs)

  • Reference URL: [BUG] /clear and session auto-compression don't work in chats routed to non-default agent via dispatch rules #3301
  • Reasoning: Routed agents (sessions owned by a non-default agent via agentForSession) were being serviced by the default agent's context store in three paths: Assemble (history reads), maybeSummarize/forceCompression (compaction writes), and the seahorse constructor (session bootstrap). For any deployment with routed agents this meant empty history on every turn, no summarization/compaction, and uninitialized seahorse session metadata. The fix follows the existing Clear pattern: resolve the owning agent per-session and fall back to the default agent only when unresolvable.

🧪 Test Environment

  • Hardware: GitHub Actions runner (CI) / Raspberry Pi 3B aarch64 (runtime deployment)
  • OS: ubuntu-latest (CI), Debian 13 trixie (runtime)
  • Model/Provider: N/A — backend agent logic, no model-specific behavior
  • Channels: N/A — affects core agent context, channel-agnostic (verified on Discord/Telegram gateways)

📸 Evidence (Optional)

Click to view Logs/Screenshots

Full CI run (fork validation before opening this PR) — all green:

--- PASS: TestLegacyAssemble_RoutedAgent
--- PASS: TestLegacyCompact_Summarize_RoutedAgent
--- PASS: TestLegacyCompact_Overflow_RoutedAgent
--- PASS: TestSeahorseBootstrap_RoutedAgent
ok  	github.com/sipeed/picoclaw/pkg/agent	68.569s

Checks: Tests ✅, Linter ✅, Integration ✅, Security ✅, Cross-Compile (linux/arm64) ✅, gitleaks ✅

☑️ Checklist

  • My code/docs follow the style of this project.
  • I have performed a self-review of my own changes.
  • I have updated the documentation accordingly. (N/A — bug fix, no docs change)

j-v and others added 4 commits August 3, 2026 12:26
…orse)

Issue sipeed#3301: routed-chat sessions are invisible to the context managers because both resolve the owning agent via GetDefaultAgent() instead of resolving per-session ownership.

Legacy tests (context_manager_test.go): TestLegacyAssemble_RoutedAgent (Assemble must read from routed store), TestLegacyCompact_Summarize_RoutedAgent (maybeSummarize against routed store), TestLegacyCompact_Overflow_RoutedAgent (forceCompression drops oldest turns in routed store, default store untouched).

Seahorse test (context_seahorse_test.go): TestSeahorseBootstrap_RoutedAgent (constructor must import pre-existing history for all registered agents, not just default).

These fail against the current code and pass once the fixes land.
Legacy: Assemble, maybeSummarize, and forceCompression all read from the default agent's session store via GetDefaultAgent(), so routed-agent sessions were stateless: history never loaded, summarization never triggered, and emergency compression operated on the wrong store. Resolve the owning agent with agentForSession (same as Clear already does).

Seahorse: the constructor bootstrapped pre-existing history only from the default agent's store, so routed sessions started with empty context on the first seahorse turn. Bootstrap all registered agents' stores, passing the owning store into bootstrapSession.
@github-actions

Copy link
Copy Markdown

This PR has had no activity for 7 days and has been marked as stale. If you are still working on it, please push an update or leave a comment; otherwise it will be closed automatically in 7 days.

@github-actions github-actions Bot added the stale label Aug 11, 2026
@j-v

j-v commented Aug 11, 2026

Copy link
Copy Markdown
Author

Still active
Awaiting review

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.

[BUG] /clear and session auto-compression don't work in chats routed to non-default agent via dispatch rules

1 participant