fix(sessions): mem_session_start derives namespace from agent_id#475
Merged
fix(sessions): mem_session_start derives namespace from agent_id#475
Conversation
`mem_session_start(agent_id="planner")` previously stored the session row with `namespace="default"` because line 82 only consulted the explicit `namespace=` arg, `app.current_namespace`, then `"default"` — the supplied `agent_id` was set on `AppContext.current_agent_id` for downstream `mem_agent_search` resolution but never flowed into the session row itself. The LangGraph adapter `MemtomemStore.start_agent_session` already auto-derived `agent-runtime:<id>`; the MCP tool now matches. New priority order: 1. explicit `namespace=` (escape hatch — wins everything) 2. `agent-runtime:<agent_id>` when `agent_id != "default"` 3. `app.current_namespace` (pre-multi-agent fallback) 4. `"default"` Only the session record's namespace field is derived. We do *not* touch `app.current_namespace`, so `mem_add` / `mem_search` without explicit `namespace=` keep the legacy fallback semantic and the "namespace and agent_id are separate axes" invariant on AppContext is preserved. Backward compat: - Callers passing explicit `namespace=` are unaffected (priority 1). - Callers not passing `agent_id` (it defaults to "default") are unaffected (priority 3 fallback). - Only `agent_id=<non-default>` without `namespace=` sees new behavior — and the new behavior matches what the LangGraph adapter already does. Caught while walking the multi-agent test scenarios — a tester running `mem_session_start(agent_id="planner")` saw `Namespace: default` in the response and reasonably read that as "agent_id was ignored," which surfaced the underlying tool/adapter divergence. Tests: four new cases in `TestSessionNamespaceDerivation` pin each priority level and the back-compat path. Existing `TestSessionAgentInheritance` cases unchanged (none of them passed `agent_id` expecting a specific namespace). Co-Authored-By: Claude <[email protected]>
CI lint job (ruff format --check on whole repo, not just src/) flagged the 3-line break of `mem_session_start(agent_id=..., namespace=..., ctx=ctx)` in `test_explicit_namespace_wins_over_agent_id` — ruff prefers it on one line. No behavior change. Co-Authored-By: Claude <[email protected]>
memtomem
approved these changes
Apr 25, 2026
Owner
memtomem
left a comment
There was a problem hiding this comment.
LGTM — small focused fix; priority ladder cleanly documented in docstring + CHANGELOG; 4 tests pin each tier; back-compat preserved. Manual smoke deferred to post-merge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mem_session_start(agent_id="planner")previously stored the session row withnamespace="default"becausesession.py:82only consulted explicitnamespace=, thenapp.current_namespace, then"default". The suppliedagent_idwas set onAppContext.current_agent_idfor downstreammem_agent_searchresolution but never flowed into the session row itself. The LangGraph adapterMemtomemStore.start_agent_sessionalready auto-derivedagent-runtime:<id>; the MCP tool now matches.New priority order:
namespace=(escape hatch)agent-runtime:<agent_id>whenagent_id != "default"app.current_namespace(pre-multi-agent fallback)"default"Only the session record's namespace field is derived —
app.current_namespaceis not touched, somem_add/mem_searchwithout explicitnamespace=keep the legacy fallback. The "namespace and agent_id are separate axes on AppContext" invariant is preserved.Backward compat
namespace=are unaffected (priority 1).agent_id(defaults to"default") are unaffected (priority 3 fallback).agent_id=<non-default>withoutnamespace=sees new behavior, and the new behavior matches what the LangGraph adapter already does.Why now
Caught while walking the v0.1.28 multi-agent test scenarios from
memtomem-docs— the tester ranmem_session_start(agent_id="planner", title="Multi-agent guide review")and saw:Reasonable expectation: if
agent_idis recorded for downstream tools, it should also drive the session's namespace. The MCP/adapter divergence becomes confusing the moment a tester switches between LangGraph code and the raw MCP tool.Test plan
uv run pytest packages/memtomem/tests/test_sessions.py -v— 14 / 14 pass (10 existing + 4 new inTestSessionNamespaceDerivation)uv run pytest -m "not ollama"— 2445 / 2445 pass, 46 deselected, no regressionsuv run ruff check packages/memtomem/src && uv run ruff format --check packages/memtomem/src— cleanmm session start --agent-id planner→mm session listshowsagent-runtime:plannernamespace🤖 Generated with Claude Code