@retaw106/mmc-pi 0.0.0-stage → 1.2.2

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/README.md CHANGED
@@ -1,3 +1,202 @@
1
- # Temporary Holding Version
1
+ # Magic Context — Pi / OMP extension
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Cross-session memory and context management for [Pi coding agent](https://github.com/earendil-works/pi-mono) and [Oh My Pi (OMP)](https://github.com/can1357/oh-my-pi). The same extension package runs on both hosts and shares its SQLite database with the [OpenCode plugin](https://www.npmjs.com/package/@cortexkit/opencode-magic-context).
4
+
5
+ Requires Pi `>= 0.74.0` or OMP `>= 17.1.7`.
6
+
7
+ ---
8
+
9
+ ## What it does
10
+
11
+ Magic Context is a context engine that keeps long Pi sessions productive by:
12
+
13
+ | Feature | What it does |
14
+ |---|---|
15
+ | **Tagging + drops** | Tags every assistant/user/tool message with `§N§ ` so you can drop specific turns later via `mmc_reduce` |
16
+ | **Historian** | Background subagent compresses old conversation into compartments + facts at threshold pressure or commit boundaries |
17
+ | **`<session-history>` injection** | Prepends compressed history into the system prompt every turn so the agent never loses context |
18
+ | **Project memories** | Persistent cross-session knowledge store with embedding-based semantic search |
19
+ | **Auto-search hint** | When user prompts mention previously-discussed topics, appends a compact memory hint |
20
+ | **Cross-harness sharing** | Memories written from OpenCode appear in Pi (and vice versa) for the same project |
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ The fastest path is the unified Magic Context CLI — `--harness pi` selects the Pi-specific setup pipeline (registers the extension with Pi, writes a sensible `mmc.jsonc`, and verifies your model picks):
27
+
28
+ ```bash
29
+ npx @retaw106/mmc-oc@latest setup --harness pi
30
+ ```
31
+
32
+ This handles everything for you:
33
+ 1. Adds `npm:@retaw106/mmc-pi` to Pi's `packages` array in `~/.pi/agent/settings.json` (the same place `pi install` writes to)
34
+ 2. Creates `~/.pi/agent/mmc.jsonc` with defaults
35
+ 3. Prompts you for historian and embedding model choices
36
+ 4. Warns about provider-specific gotchas (e.g. GitHub Copilot reasoning models need an explicit `thinking_level`)
37
+
38
+ If you'd rather register the Pi extension package directly with Pi (skipping the wizard), use Pi's own installer:
39
+
40
+ ```bash
41
+ pi install npm:@retaw106/mmc-pi
42
+ ```
43
+
44
+ This adds the extension to `~/.pi/agent/settings.json` but won't write `mmc.jsonc` for you — create it manually at `~/.pi/agent/mmc.jsonc` (see Configuration below).
45
+
46
+ To check installation health later:
47
+
48
+ ```bash
49
+ npx @retaw106/mmc-oc@latest doctor --harness pi
50
+ ```
51
+
52
+ ### Oh My Pi (OMP)
53
+
54
+ Use the OMP-specific setup path. It installs the package through OMP's plugin manager, disables OMP native compaction and automatic memory to prevent duplicate context managers, and verifies the effective project plugin state:
55
+
56
+ ```bash
57
+ npx @retaw106/mmc-oc@latest setup --harness omp
58
+ ```
59
+
60
+ Manual installation is also supported:
61
+
62
+ ```bash
63
+ omp plugin install @retaw106/mmc-pi
64
+ omp config set compaction.enabled false
65
+ omp config set memory.backend off
66
+ ```
67
+
68
+ Verify with:
69
+
70
+ ```bash
71
+ npx @retaw106/mmc-oc@latest doctor --harness omp
72
+ ```
73
+
74
+ OMP's legacy Pi loader maps `@earendil-works/*` imports to its bundled `@oh-my-pi/*` runtime. Magic Context identifies OMP from `@oh-my-pi/pi-utils`'s in-process `APP_NAME` through the running host's module graph, not from an executable basename, so compiled and symlinked launches behave the same. It also re-invokes the current host executable for historian and dreamer children, so OMP children remain OMP processes.
75
+
76
+ ---
77
+
78
+ ## Configuration
79
+
80
+ Magic Context reads the Pi host's own config (project overrides user):
81
+
82
+ 1. `$cwd/.mmc/mmc.jsonc`
83
+ 2. `~/.pi/agent/mmc.jsonc`
84
+
85
+ Session discovery follows the active host. Relative `pi.subagent_extensions` are deliberately isolated by host: plain Pi resolves them from `~/.pi/agent`, while a positively identified OMP process uses `PI_CODING_AGENT_DIR` when set and otherwise derives the active `~/.omp` config/profile agent directory.
86
+
87
+ ### Minimal config
88
+
89
+ ```jsonc
90
+ {
91
+ "$schema": "https://raw.githubusercontent.com/cortexkit/magic-context/master/assets/magic-context.schema.json",
92
+ "enabled": true,
93
+ "historian": {
94
+ "pi": {
95
+ "model": "anthropic/claude-haiku-4-5"
96
+ },
97
+ "omp": {
98
+ "model": { "model": "opencode/claude-haiku-4-5", "thinking_level": "auto" }
99
+ }
100
+ },
101
+ "embedding": {
102
+ "provider": "local"
103
+ }
104
+ }
105
+ ```
106
+
107
+ OMP uses `historian.omp ?? historian.pi`; leaving the OMP block absent preserves existing Pi-compatible configuration. OMP supports `off`, `minimal`, `low`, `medium`, `high`, `xhigh`, `max`, `inherit`, and `auto` thinking levels.
108
+
109
+ For the full configuration reference (including auto-search and experimental features), see [CONFIGURATION.md](https://github.com/cortexkit/magic-context/blob/master/CONFIGURATION.md) in the main repository — OpenCode, Pi, and OMP share the same schema.
110
+
111
+ ---
112
+
113
+ ## Slash commands
114
+
115
+ All commands trigger `triggerTurn: false` (never sent to the LLM):
116
+
117
+ | Command | What it does |
118
+ |---|---|
119
+ | `/mmc-status` | Live token breakdown + queued ops + cache state |
120
+ | `/mmc-flush` | Force-process pending ops queue |
121
+ | `/mmc-recomp` | Rebuild compartments from raw history (heavy operation) |
122
+ | `/mmc-wrapup [messages_to_keep]` | Compact older live history while keeping the newest N messages raw |
123
+
124
+ ---
125
+
126
+ ## Storage
127
+
128
+ Magic Context stores everything in a single shared SQLite database at:
129
+
130
+ ```
131
+ ~/.local/share/mmc/context.db
132
+ ```
133
+
134
+ When a host isolates `XDG_DATA_HOME` per process, set
135
+ `MMC_STORAGE_DIR` to the absolute, complete shared Magic Context
136
+ storage directory. The explicit path takes precedence over the XDG-derived path
137
+ and does not change Pi's own configuration or session directories. The host must
138
+ propagate one identical value to every process that shares the database; Magic
139
+ Context never originates this variable from its default. This affects the
140
+ harness-side database only, not `subc` daemon state. Mixed-version processes on
141
+ one directory still block migrations until all processes use a compatible build.
142
+
143
+ This is the **same database** the OpenCode plugin and OMP extension use. Tables are scoped by:
144
+ - `harness` column (`'pi'` or `'opencode'`) for session-scoped data; OMP intentionally uses `'pi'`
145
+ - `project_path` (resolved git root) for project-scoped data (memories, embeddings)
146
+
147
+ Memories are shared across all three harnesses for the same project; per-session tagging stays correctly attributed.
148
+
149
+ Storage failures are fatal — Magic Context will refuse to register hooks rather than run with ephemeral state, since that would let context grow unbounded across restarts.
150
+
151
+ ---
152
+
153
+ ## Cross-harness coherence
154
+
155
+ For semantic search to work across harnesses, every host must use the **same embedding model**. Magic Context detects a mismatch on Pi or OMP startup and warns:
156
+
157
+ ```
158
+ WARN embedding model mismatch detected for project ...:
159
+ stored vectors use "openai-compatible:Qwen/Qwen3-Embedding-8B" but Pi is configured with "local:Xenova/all-MiniLM-L6-v2".
160
+ Cross-harness search will return zero results until vectors are re-embedded.
161
+ ```
162
+
163
+ Configure `embedding` once in the shared `~/.config/mmc/mmc.jsonc`; OpenCode, Pi, and OMP all read it. Use `$cwd/.mmc/mmc.jsonc` only when that project intentionally needs an override.
164
+
165
+ ---
166
+
167
+ ## Tools available to the agent
168
+
169
+ | Tool | Action set | Purpose |
170
+ |---|---|---|
171
+ | `mmc_search` | n/a | Search memories + raw session history; returns ranked results with previews |
172
+ | `mmc_memory` | `write`, `delete` | Manage project memories explicitly |
173
+ | `mmc_note` | `read`, `write`, `update`, `dismiss` | Defer intentions for later — listed in the glance on demand |
174
+ | `mmc_expand` | `start`/`end`, `message`, `verbose` | Recover complete messages or expand a compressed conversation range |
175
+ | `mmc_reduce` | `drop` | Queue tagged turns for cache-safe removal from the live context |
176
+
177
+ `mmc_note`, `mmc_expand`, and `mmc_reduce` are session-scoped and are exposed in primary Pi/OMP sessions. Magic Context omits them from ephemeral `--no-session` child processes, where they would otherwise target the hidden child session; `mmc_reduce` is additionally omitted when compaction is disabled.
178
+
179
+ ---
180
+
181
+ ## Architecture & implementation
182
+
183
+ This package is part of the [magic-context monorepo](https://github.com/cortexkit/magic-context). Pi and OMP share the same adapter layer and core implementation:
184
+
185
+ | Pi-specific module | Responsibility |
186
+ |---|---|
187
+ | `context-handler.ts` | Pi `pi.on("context", ...)` adapter — tags, drops, runs nudges and auto-search |
188
+ | `subagent-runner.ts` | Re-invokes the current Pi/OMP host with `--print --mode json --no-session`; resolves relative allowlisted extensions from plain Pi's `~/.pi/agent` or an identified OMP host's active agent directory; and applies per-agent tool isolation |
189
+ | `tools/` | Pi `pi.registerTool` wrappers around the shared tool implementations |
190
+ | `commands/` | Pi `pi.registerCommand` wrappers for the four `/mmc-*` slash commands |
191
+ | `system-prompt.ts` | Pi `before_agent_start` injector for the `## Magic Context` guidance block and sticky date (v2 never emits data blocks such as `<project-docs>`/`<user-profile>`/`<key-files>`) |
192
+ | `config/` | Shared MMC config loader with legacy Pi migration fallback |
193
+
194
+ The unified [`@retaw106/mmc-oc`](https://www.npmjs.com/package/@retaw106%2Fmmc-oc) CLI exposes separate `pi` and `omp` setup/doctor pipelines while both use this runtime adapter.
195
+
196
+ For deeper architectural detail, see the main repo's [ARCHITECTURE.md](https://github.com/cortexkit/magic-context/blob/master/ARCHITECTURE.md).
197
+
198
+ ---
199
+
200
+ ## License
201
+
202
+ MIT — see [LICENSE](https://github.com/cortexkit/magic-context/blob/master/LICENSE).
@@ -0,0 +1,62 @@
1
+ // src/historian-calibration-extension.ts
2
+ var HISTORIAN_TEMPERATURE_ENV = "MMC_HISTORIAN_TEMPERATURE";
3
+ var HISTORIAN_MAX_OUTPUT_TOKENS_ENV = "MMC_HISTORIAN_MAX_OUTPUT_TOKENS";
4
+ function finiteNumber(value) {
5
+ if (value === undefined || value.trim().length === 0)
6
+ return;
7
+ const parsed = Number(value);
8
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
9
+ }
10
+ function calibrateHistorianProviderPayload(payload, temperature, maxOutputTokens) {
11
+ if (typeof payload !== "object" || payload === null || Array.isArray(payload))
12
+ return payload;
13
+ if (temperature === undefined && maxOutputTokens === undefined)
14
+ return payload;
15
+ const calibrated = { ...payload };
16
+ const generationConfig = calibrated.generationConfig;
17
+ if (typeof generationConfig === "object" && generationConfig !== null && !Array.isArray(generationConfig)) {
18
+ calibrated.generationConfig = {
19
+ ...generationConfig,
20
+ ...temperature !== undefined ? { temperature } : {},
21
+ ...maxOutputTokens !== undefined ? { maxOutputTokens } : {}
22
+ };
23
+ return calibrated;
24
+ }
25
+ const inferenceConfig = calibrated.inferenceConfig;
26
+ if (typeof inferenceConfig === "object" && inferenceConfig !== null && !Array.isArray(inferenceConfig)) {
27
+ calibrated.inferenceConfig = {
28
+ ...inferenceConfig,
29
+ ...temperature !== undefined ? { temperature } : {},
30
+ ...maxOutputTokens !== undefined ? { maxTokens: maxOutputTokens } : {}
31
+ };
32
+ return calibrated;
33
+ }
34
+ if (temperature !== undefined) {
35
+ calibrated.temperature = temperature;
36
+ }
37
+ if (maxOutputTokens !== undefined) {
38
+ if ("max_output_tokens" in calibrated) {
39
+ calibrated.max_output_tokens = maxOutputTokens;
40
+ } else if ("max_completion_tokens" in calibrated) {
41
+ calibrated.max_completion_tokens = maxOutputTokens;
42
+ } else if ("max_tokens" in calibrated) {
43
+ calibrated.max_tokens = maxOutputTokens;
44
+ } else if ("maxTokens" in calibrated) {
45
+ calibrated.maxTokens = maxOutputTokens;
46
+ }
47
+ }
48
+ return calibrated;
49
+ }
50
+ function historianCalibrationExtension(pi) {
51
+ const temperature = finiteNumber(process.env[HISTORIAN_TEMPERATURE_ENV]);
52
+ const maxOutputTokens = finiteNumber(process.env[HISTORIAN_MAX_OUTPUT_TOKENS_ENV]);
53
+ if (temperature === undefined && maxOutputTokens === undefined)
54
+ return;
55
+ pi.on("before_provider_request", (event) => calibrateHistorianProviderPayload(event.payload, temperature, maxOutputTokens));
56
+ }
57
+ export {
58
+ HISTORIAN_MAX_OUTPUT_TOKENS_ENV,
59
+ HISTORIAN_TEMPERATURE_ENV,
60
+ calibrateHistorianProviderPayload,
61
+ historianCalibrationExtension as default
62
+ };