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/listrole 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.
- Load the session and its turns.
- Minimum size. A session with fewer than
minTurnsForCollapseturns is skipped. - 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.
- One LLM call returns a structured summary: facts, decisions, strategies and a rated outcome.
- Surprise gate. Each extracted fact is compared with what is already stored. A fact that is already known is dropped.
- Plan. Topic-key collisions are resolved and the episode narrative is written. Every read, embedding and search happens before the first write.
- 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
collapsedstatus. A failure rolls all of it back and leaves the session retryable. A session collapses once. - 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:
status | Meaning |
|---|---|
completed | Memories were extracted and stored |
skipped | Nothing was extracted and the session was archived; the turns are kept |
failed | The collapse did not finish; the session is kept so it can be retried |
collapse_pending | An 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.
| Setting | Default | Meaning |
|---|---|---|
mode | auto | auto, explicit or hybrid |
inactivityTimeout | 1800 | Seconds of inactivity |
maxTurns | 50 | Turn limit |
maxTokens | 8192 | Token limit |
minTurnsForCollapse | 3 | Shorter sessions are skipped |
requirePin | false | Collapse only when a turn was pinned |
extractFacts | true | |
extractDecisions | true | |
extractStrategies | true | |
extractTasks | false | |
recurrenceThreshold | 0.6 | Minimum coherence for the gate in step 3 |
skipLlmIfBelowThreshold | true | Skip 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.