Skip to content

fix(sessions): mem_session_start derives namespace from agent_id#475

Merged
tsdata merged 2 commits intomainfrom
fix/session-start-agent-id-derives-namespace
Apr 25, 2026
Merged

fix(sessions): mem_session_start derives namespace from agent_id#475
tsdata merged 2 commits intomainfrom
fix/session-start-agent-id-derives-namespace

Conversation

@tsdata
Copy link
Copy Markdown
Collaborator

@tsdata tsdata commented Apr 25, 2026

Summary

mem_session_start(agent_id="planner") previously stored the session row with namespace="default" because session.py:82 only consulted explicit namespace=, then 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)
  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 — app.current_namespace is not touched, so mem_add / mem_search without explicit namespace= keep the legacy fallback. The "namespace and agent_id are separate axes on AppContext" invariant is preserved.

Backward compat

  • Callers passing explicit namespace= are unaffected (priority 1).
  • Callers not passing agent_id (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.

Why now

Caught while walking the v0.1.28 multi-agent test scenarios from memtomem-docs — the tester ran mem_session_start(agent_id="planner", title="Multi-agent guide review") and saw:

Session started: 89b2c742-...
- Title: Multi-agent guide review
- Agent: planner
- Namespace: default

Reasonable expectation: if agent_id is 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 in TestSessionNamespaceDerivation)
  • uv run pytest -m "not ollama" — 2445 / 2445 pass, 46 deselected, no regressions
  • uv run ruff check packages/memtomem/src && uv run ruff format --check packages/memtomem/src — clean
  • Manual smoke: mm session start --agent-id plannermm session list shows agent-runtime:planner namespace

🤖 Generated with Claude Code

pandas-studio and others added 2 commits April 25, 2026 22:21
`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]>
Copy link
Copy Markdown
Owner

@memtomem memtomem left a comment

Choose a reason for hiding this comment

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

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.

@tsdata tsdata merged commit d598b15 into main Apr 25, 2026
7 checks passed
@tsdata tsdata deleted the fix/session-start-agent-id-derives-namespace branch April 25, 2026 13:59
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants