pi-observational-memory-extension 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nikita Nosov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Pi Observational Memory (pi-observational-memory)
2
+
3
+ A Mastra-style **Observational Memory (OM)** extension for Pi.
4
+
5
+ This extension completely replaces Pi's legacy, lossy, single-block conversation summarization (`/compact`) with a transparent, highly-structured, two-layer memory model: **Mastra-style Observational Memory + Recall-driven retrieval**.
6
+
7
+ By running an Observer-Reflector agent pipeline, this package allows Pi to prune old, verbose raw message history from the active context while preserving structured observations, current tasks, and continuation guidelines. When precise details are needed, the main agent uses the `om_recall` tool to fetch raw conversations from a scrollable history index.
8
+
9
+ ---
10
+
11
+ ## ๐Ÿง  Philosophy and the 3-Agent Model
12
+
13
+ Legacy compaction compresses raw history into a single monolithic block of text, losing timestamps, status signals, temporal connections, and distinct user vs. agent traits. Observational Memory breaks memory down into a distinct psychological subsystem:
14
+
15
+ 1. **The Actor (Main Agent):** Interacts with the user, performs tool calls, and references the current Observations block. It is only presented with active observations, current-task instructions, suggested-response guidelines, and a trailing window of raw, unobserved messages.
16
+ 2. **The Observer (Extraction Agent):** Runs automatically when unobserved messages exceed a token threshold. It converts raw message blocks into chronological, priority-labeled (๐Ÿ”ด, ๐ŸŸก, ๐ŸŸข, โœ…) observations.
17
+ 3. **The Reflector (Consolidation Agent):** Runs when the accumulated Observations block exceeds a memory threshold. It reorganizes, cleans up, and condenses older observations into high-density facts, scaling up through 5 instruction levels (0โ€“4) if the output size does not meet the targeted compression ratio.
18
+
19
+ ---
20
+
21
+ ## ๐Ÿ›  Features
22
+
23
+ - **No Silent Fallbacks:** Observer, Reflector, or model failures throw loud, descriptive errors (`Observational Memory Observer failed: ...`) rather than failing silently or leaving the context in an ambiguous state.
24
+ - **Context Injection & Pruning:** Injects structured observations, `current-task`, `suggested-response`, retrieval guidelines, and a continuation hint into the context. Prunes observed messages from the LLM context automatically to save tokens and avoid redundant text.
25
+ - **Async Buffering:** Extracts observations in the background at regular intervals (every 20% of the threshold) so that when the main threshold is hit, the accumulated memory is swapped/activated instantly with zero LLM-latency overhead.
26
+ - **Adaptive Thresholds:** The active message-observation threshold expands dynamically based on remaining memory capacity in the reflection pool, maximizing raw context usage safely.
27
+ - **Recall-driven Retrieval:** Exposes an `om_recall` tool to the Actor so it can retrieve full, raw message payloads from observed history when exact code, quotes, or numbers are needed.
28
+ - **Custom Compaction Hook:** Plugs directly into Pi's `session_before_compact` lifecycle event. Typing `/compact` or triggering auto-compaction launches the Observer/Reflector memory consolidation flow instead of Pi's legacy summary compaction.
29
+ - **TUI Overlay Panel:** Fully custom, responsive, multi-tab overlay (`/om-memory`) in interactive mode for inspecting memory. Features tabbed navigation, smooth scroll, and precise border framing.
30
+ - **Durable Persistence:** Serializes and loads state files safely under `.pi/om/<session-id>.json`. Outputs JSON-formatted diagnostic logs to `.pi/om/debug/` for every operation.
31
+
32
+ ---
33
+
34
+ ## ๐Ÿ“‹ Configuration & Settings
35
+
36
+ You can customize Observational Memory behaviors by specifying fields inside your project's `.pi/settings.json` or globally inside `~/.pi/agent/settings.json`.
37
+
38
+ ```json
39
+ {
40
+ "compaction": {
41
+ "enabled": true,
42
+ "reserveTokens": 16384,
43
+ "keepRecentTokens": 20000
44
+ }
45
+ }
46
+ ```
47
+
48
+ - **Observation Trigger Threshold:** ~`30,000` raw message tokens.
49
+ - **Reflection Trigger Threshold:** ~`40,000` observation tokens.
50
+ - **Default Models:** `google/gemini-2.5-flash` with 0.3 temperature for Observer, and 0.0 temperature for Reflector.
51
+
52
+ ---
53
+
54
+ ## ๐Ÿ•น Commands
55
+
56
+ This extension registers the following slash commands in Pi:
57
+
58
+ - `/om-status` โ€” Shows a detailed breakdown of pending/observation tokens, active locks, thresholds, and last operation results.
59
+ - `/om-memory` โ€” Opens the interactive multi-tab overlay panel (observations, status stats, and background debug details).
60
+ - `/om-observe` โ€” Forces an immediate Observation pass on all pending raw message history.
61
+ - `/om-reflect` โ€” Forces an immediate Reflection/compression run on all existing observations. Supports optional guidance (e.g. `/om-reflect consolidate auth files`).
62
+ - `/om-enable` โ€” Enables Observational Memory for the current session.
63
+ - `/om-disable` โ€” Disables Observational Memory (falls back to legacy summarizing).
64
+ - `/om-compact` โ€” Starts the standard Pi compaction pipeline, routed through pi-observational-memory.
65
+
66
+ ---
67
+
68
+ ## ๐Ÿ’ป TUI Overlay Panel Controls
69
+
70
+ Type `/om-memory` in Pi's interactive terminal mode to open the fullscreen-ish overlay panel:
71
+
72
+ - `โ† / โ†’` (Arrow keys) โ€” Switch tabs:
73
+ - **Memory:** Displays the active Observations list, Current Task, and Suggested Response blocks.
74
+ - **Status:** Shows live token meters, ratios, and model state.
75
+ - **Debug:** Lists background buffered chunks, filenames, state paths, and latest errors.
76
+ - `โ†‘ / โ†“` (Arrow keys) โ€” Scroll content.
77
+ - `Home / End` โ€” Jump to top/bottom of scroll.
78
+ - `q / Esc` โ€” Close overlay and return to chat.
79
+
80
+ ---
81
+
82
+ ## ๐Ÿงช Development and Validation
83
+
84
+ This package is structured to comply with Pi's strict peer dependency models and TypeScript validation rules.
85
+
86
+ ### Check Types and Manifests
87
+ Run the typechecker and validation scripts before packaging or releasing:
88
+ ```bash
89
+ npm run typecheck
90
+ npm run validate
91
+ ```
92
+
93
+ ### Commit Guidelines
94
+ We enforce Conventional Commits. Ensure all commits use standard prefixes (`feat(om):`, `fix(tui):`, `refactor(memory):`, etc.).
95
+
96
+ ---
97
+
98
+ ## ๐Ÿ“„ License
99
+
100
+ MIT ยฉ Nikita Nosov