feat(cli): hooks-friendly mm session start primitives (RFC Phase 1)#541
Merged
feat(cli): hooks-friendly mm session start primitives (RFC Phase 1)#541
Conversation
Add the SessionStart hook primitives requested by the RFC at ``memtomem-docs/memtomem/planning/hooks-session-cli-rfc.md``: - ``--idempotent`` returns the active session for the same agent_id instead of creating a new one (RFC §Q1 recommendation a). On a cross-agent collision the old session is auto-ended (§Q2 a). When the state file points to an already-ended session, treat it as advisory and create fresh (§Q6 a). - ``--auto-end-stale=<duration>`` ends active sessions whose ``started_at`` is older than the parsed duration before resolving idempotency (§Q3). Opt-in only — humans don't pay this cost. - ``--json`` emits one structured line carrying ``session_id``, ``resumed``, and ``stale_ended``, the contract the eventual hooks.json entry will parse. The plugin's ``hooks.json`` SessionStart entry, the docs example, and the parity-test extension are explicit Phase 2 work and ship in a follow-up PR — that lets reviewers gate on the CLI contract before the hook surface depends on it. Storage backing: ``find_stale_active_sessions(started_before)`` selects ``ended_at IS NULL AND started_at < ?``. Tested directly against real SQLite (``test_sessions.py``) to avoid the AsyncMock-hides-SQL-bugs trap. Closes prerequisite for PR #536's deferred SessionStart hook (memtomem-docs#24). Co-Authored-By: Claude <[email protected]>
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
Phase 1 of the hooks-session-cli RFC (memtomem-docs#24): adds the CLI
primitives a SessionStart hook needs without orphaning prior
sessions.
mm session start --idempotentreturns the active session forthe same
--agent-idinstead of creating a new one. Cross-agentcollisions auto-end the previous session.
mm session start --auto-end-stale=<duration>(e.g.24h,7d) closes any active session older than the duration beforeresolving idempotency. Opt-in only.
mm session start --jsonemits{"session_id": ..., "resumed": bool, "stale_ended": [...]}for hook parsing.Storage backing:
find_stale_active_sessions(started_before)filters on
ended_at IS NULL AND started_at < ?— a dedicatedmethod instead of forcing every caller to load
list_sessionsandfilter in Python.
Why this is Phase 1 only
The plugin's
hooks.jsonSessionStart entry, the docs example indocs/guides/integrations/claude-code.md, and theTestPluginHooksDocsParityextension all ship in a follow-up PR.Decoupling lets reviewers gate the CLI contract before the hook
surface depends on it. PR #536 documented the same staging.
Verification
mm session startcovering RFC §Verification'sfive behaviours (same-agent idempotent, cross-agent end+new, manual
end then fresh, auto-end-stale, JSON shape) plus an
invalid-duration guard.
find_stale_active_sessionsagainst realSQLite — avoids the AsyncMock-hides-SQL-bugs trap when the filter
predicate is non-trivial.
ruff check,ruff format --check,mypy(advisory) allgreen on the changed files.
Open questions decided in this PR (§ in RFC)
--agent-idliteral (recommended)Q3 (default stale duration) is a docs decision, defers to Phase 2.
Test plan
sketch (
session_id,resumed,stale_ended)--auto-end-staleparses24h/7d/30m/45sand rejects garbage with a Click errora follow-up PR matches the project's preferred cadence
🤖 Generated with Claude Code