Conceptsv3 featuresSession collapse

Session collapse

Beta in docs/STATUS.md. Mnemosyne is alpha, on the 3.6.x line, and the API can still change within 3.x.

A session holds the turns of one conversation. Collapsing it turns the conversation into durable memories: facts, decisions, strategies and one episode that links them.

The lifecycle

POST /session/start              { agentId?, label?, metadata? }            → 201
POST /session/{id}/turn          { role, content, metadata? }               → 204
POST /session/{id}/pin           { turnId | turnIndex, pinned }
POST /session/{id}/scratch       { scratch }                                → 204
POST /session/{id}/collapse      { strategy?, collapseHint? }               → 202
POST /session/{id}/feedback      { rating: "up" | "down" }                  → 204
POST /session/{id}/discard                                                  → 204
GET  /session/{id}
GET  /session/list

role is user, assistant or system. strategy is auto (the default) or explicit. A discarded session is archived without extracting anything.

With the TypeScript client:

import { MnemosyneClient } from "@mnemo-ai/client-ts";
 
const memory = new MnemosyneClient({
  url: "http://localhost:3939",
  apiKey: process.env.MNEMO_KEY!,
});
 
const session = await memory.createSession({ agentId: "agent_support" });
await memory.appendTurn(session.id, { role: "user", content: "Robot 14 keeps missing its dock." });
await memory.appendTurn(session.id, { role: "assistant", content: "Firmware 4.2.0 changed the docking offset. Rolling it back on robot 14." });
await memory.collapseSession(session.id, { strategy: "auto" });

The MCP tool is memory_session. memory_episode_start and memory_episode_end open and close an episode.

Where a session lives

The session row is in Postgres. When Redis is configured, turns and the list of active sessions are also buffered there, with a 24-hour TTL and a cap of 500 turns per session. Any Redis error falls back to Postgres; session routes do not fail because Redis is down (packages/core/src/session/session-buffer.ts). The scratch pad is stored in Postgres.

POST /session/start also returns anticipated: up to three memory ids that the anticipation engine expects the agent to need.

The collapse pipeline

Source: packages/core/src/session/collapse.ts.

  1. Load the session and its turns.
  2. Minimum size. A session with fewer than minTurnsForCollapse turns is skipped.
  3. Coherence gate. The turns are compared with each other. A session whose turns do not share a theme is skipped, because it rarely holds durable knowledge.
  4. One LLM call returns a structured summary: facts, decisions, strategies and a rated outcome.
  5. Surprise gate. Each extracted fact is compared with what is already stored. A fact that is already known is dropped.
  6. Plan. Topic-key collisions are resolved and the episode narrative is written. Every read, embedding and search happens before the first write.
  7. One transaction stores the facts, decisions, strategies, the relations between the new facts, the episode, the links from facts to the episode and the session’s collapsed status. A failure rolls all of it back and leaves the session retryable. A session collapses once.
  8. Enrichment, best effort: hyperedges among the new facts, hot-tier promotion, worth updates and, every ten collapses, a reflection cycle.

The response

202 means the request was processed, not that something was extracted. Read status:

statusMeaning
completedMemories were extracted and stored
skippedNothing was extracted and the session was archived; the turns are kept
failedThe collapse did not finish; the session is kept so it can be retried
collapse_pendingAn archived managed session was accepted for retry

skipReason is too_few_turns, low_recurrence_signal, no_pin or llm_unavailable. To force extraction past the gates, send the collapse again with "strategy": "explicit". GET /session/{id} reports the attempts, the last error and what a completed collapse produced. A background job (mnemo.session.collapse, every five minutes) retries failed collapses.

Collapse policy

The defaults are DEFAULT_COLLAPSE_POLICY in packages/core/src/session/collapse-policy.ts. An agent profile can override them in metadata.collapsePolicy.

SettingDefaultMeaning
modeautoauto, explicit or hybrid
inactivityTimeout1800Seconds of inactivity
maxTurns50Turn limit
maxTokens8192Token limit
minTurnsForCollapse3Shorter sessions are skipped
requirePinfalseCollapse only when a turn was pinned
extractFactstrue
extractDecisionstrue
extractStrategiestrue
extractTasksfalse
recurrenceThreshold0.6Minimum coherence for the gate in step 3
skipLlmIfBelowThresholdtrueSkip the LLM call when the gate fails

Outcome and worth

The rated outcome of a collapse (resolved, partial, abandoned, failed or delegated) feeds the worth of the memories involved, and POST /session/{id}/feedback adds an explicit rating. See Memory Worth.

See also