@korrlabs/mnemospi 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,121 @@
1
+ ---
2
+ name: mnemos-context-lifecycle
3
+ description: Context lifecycle automation — assemble the pre-LLM context block, report context rewrites losslessly, and wire session/tool lifecycle hooks (ADR-0017/0018)
4
+ ---
5
+
6
+ # Mnemos Context Lifecycle
7
+
8
+ The publication-engine tools that run the context lifecycle end to end:
9
+ `mnemos_assemble_context` composes the model-facing context block through a
10
+ fixed, security-gated pipeline; `mnemos_context_rewrite` is the lossless
11
+ report of a context rewrite (compaction/slimming) so the original is never
12
+ lost; `mnemos_hooks` groups the automation entry points behind one
13
+ `action:` enum. ADR-0017 D1 defines the provider contract, ADR-0018 the
14
+ rewrite lifecycle.
15
+
16
+ ## WHEN
17
+
18
+ - **Session start (bootstrap)** — recall recent checkpoints so a new or
19
+ post-compaction session resumes from real state (`hooks` action
20
+ `on_session_start`).
21
+ - **Pre-LLM injection (context assembly)** — before an important model call
22
+ where you want mnemos-retrieved memory in the prompt, composed under a
23
+ token budget with the entry invariant applied (secret scan, provenance,
24
+ published-only status gate).
25
+ - **Compaction (context rewrite)** — the harness replaced or slimmed a
26
+ block of its working context: report the original to mnemos so nothing is
27
+ lost, then keep a thin marker in the window.
28
+ - **Tool output compression (hooks.post_tool_call)** — a tool returned a
29
+ huge output you want substituted by a zero-loss CCR marker, with
30
+ provenance stamped for strict marker validation.
31
+
32
+ ## STEPS
33
+
34
+ 1. **Bootstrap a session** — recall recent checkpoints:
35
+
36
+ ```text
37
+ mnemos_hooks(action="on_session_start", session=<session-id>,
38
+ project=<project-slug>, agent=<agent-slug>, limit=5)
39
+ # → recent checkpoints, already secret-scanned at issuance
40
+ ```
41
+
42
+ 2. **Assemble the pre-LLM context block** (or use the equivalent hook):
43
+
44
+ ```text
45
+ mnemos_assemble_context(session=<session-id>, project=<project-slug>,
46
+ file=<optional-path>, agent=<agent-slug>,
47
+ budget=2048, mode="sync")
48
+ # → assembled text, per-block provenance lines, redaction counts,
49
+ # token stats
50
+ ```
51
+
52
+ The fixed pipeline runs in order: hybrid RRF recall (published/processed
53
+ only) → optional CCR marker expansion (`expand_ccr=true`, needs `agent`)
54
+ → context filter → mandatory secret scan → CacheAligner → token budget.
55
+ Prefer `mode="async"` on latency-sensitive paths: the first call returns
56
+ a handle, fetch the block on a later call with `async_handle=<handle>`.
57
+ Use `mode="code"` / `mode="prose"` to bias recall candidates to a stored
58
+ content type.
59
+
60
+ 3. **Report a context rewrite** (compaction, window slimming) — send the
61
+ ORIGINAL of the replaced block:
62
+
63
+ ```text
64
+ mnemos_context_rewrite(content=<original text>, project=<project-slug>,
65
+ agent=<agent-slug>, session=<session-id>,
66
+ supersedes=<memory-id-of-replaced-block>,
67
+ include_marker=true)
68
+ # → memory id (supersedes edge new → old); with include_marker=true
69
+ # also a CCR marker to keep in the window
70
+ ```
71
+
72
+ The original enters the normal knowledge pipeline (raw → processed →
73
+ published); it is context-reachable again only after the pipeline
74
+ advances it. Rehydrate later via `mnemos_retrieve` or
75
+ `mnemos_assemble_context` — both re-scan and carry provenance.
76
+
77
+ 4. **Compress a tool output through the hook** (autocompression is
78
+ opt-in — pass `auto_compress=true` per call, or enable the
79
+ `hooks.auto_compress` config knob):
80
+
81
+ ```text
82
+ mnemos_hooks(action="post_tool_call", session=<session-id>,
83
+ project=<project-slug>, agent=<agent-slug>,
84
+ tool_name=<tool-that-ran>, output_text=<raw output>,
85
+ auto_compress=true)
86
+ # → marker-headed compressed_text; SUBSTITUTE it into your window
87
+ ```
88
+
89
+ 5. **Handle backpressure** — `mnemos_context_rewrite` may return
90
+ `{"error": ..., "rate_limited": true}`. Back off and re-deliver later:
91
+ the event is idempotent (content-addressed over
92
+ project/agent/session/supersedes/content), so re-delivery cannot
93
+ duplicate writes.
94
+
95
+ ## DISCIPLINE
96
+
97
+ - **Never assemble context by hand.** The pipeline stages (context filter,
98
+ secret scan, CacheAligner, budget) are not optional — a hand-pasted
99
+ "context block" bypasses the entry invariant and may carry secrets or
100
+ noise into the prompt.
101
+ - **The rewrite diff is advisory, the original is load-bearing.** The
102
+ `diff` argument is stored as metadata only; the original `content` is the
103
+ source of truth and must be passed verbatim, unsummarized.
104
+ - **Identity is mandatory.** Every call needs `session` + `project` +
105
+ `agent` — identity-less compression mints unverifiable cache rows that
106
+ strict marker validation will later reject.
107
+ - **Rewrite is version-less.** Do not invent version numbering; replacement
108
+ lineage is the `supersedes` edge. Pass `supersedes` when you know the
109
+ memory id of the replaced block.
110
+ - **Trust the provenance line.** Injected blocks carry
111
+ `[mnemos:<id> project=… status=… v=<n> retrieved=<iso>]` — surface it,
112
+ don't strip it; it is what makes later rehydration auditable.
113
+
114
+ ## See also
115
+
116
+ - Skill `mnemos-session-init` — the manual recall counterpart at session start
117
+ - Skill `mnemos-compress` — direct CCR compression when no lifecycle hook applies
118
+ - Skill `mnemos-cache-align` — stabilizing prompts for provider KV caches
119
+ - Skill `mnemos-write` — persisting markers and memories into the store
120
+ - ADR `docs/project/adr/0017-memory-system-evolution-roadmap.md` — provider contract D1
121
+ - ADR `docs/project/adr/0018-context-rewrite-ltm-bridge.md` — rewrite lifecycle
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@korrlabs/mnemospi",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Mnemos memory & knowledge server — Pi extension. Spawns `mnemos mcp-server` over stdio and exposes every mnemos_* tool as a native Pi tool, plus the mnemos skill pack.",
5
5
  "type": "module",
6
6
  "license": "MIT",