@wolfx/pi-magic-context 0.21.8

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 ADDED
@@ -0,0 +1,167 @@
1
+ # Magic Context — Pi extension
2
+
3
+ Cross-session memory and context management for [Pi coding agent](https://github.com/earendil-works/pi-mono). Shares the same SQLite database as the [OpenCode plugin](https://www.npmjs.com/package/@cortexkit/opencode-magic-context), so memories, embeddings, dreamer state, and project knowledge follow you across both harnesses.
4
+
5
+ > ⚠️ **Beta release.** The Pi extension is newer than the OpenCode plugin and is published as **beta**. Core flows (tagging, historian, memories, dreamer, `/ctx-aug`) are validated end-to-end on interactive `pi` sessions and `pi --print --mode json` subagents. Please [report issues](https://github.com/cortexkit/magic-context/issues) — every Pi-side rough edge is fixable.
6
+
7
+ Requires `@earendil-works/pi-coding-agent` and `@earendil-works/pi-tui` `>= 0.71.0`.
8
+
9
+ ---
10
+
11
+ ## What it does
12
+
13
+ Magic Context is a context engine that keeps long Pi sessions productive by:
14
+
15
+ | Feature | What it does |
16
+ |---|---|
17
+ | **Tagging + drops** | Tags every assistant/user/tool message with `§N§ ` so you can drop specific turns later via `ctx_reduce` |
18
+ | **Historian** | Background subagent compresses old conversation into compartments + facts at threshold pressure or commit boundaries |
19
+ | **`<session-history>` injection** | Prepends compressed history into the system prompt every turn so the agent never loses context |
20
+ | **Project memories** | Persistent cross-session knowledge store with embedding-based semantic search |
21
+ | **Dreamer** | Scheduled background subagent that consolidates, verifies, archives, and improves stored memories |
22
+ | **`/ctx-aug`** | On-demand sidekick that augments the next turn with relevant memories |
23
+ | **Auto-search hint** | When user prompts mention previously-discussed topics, appends a compact memory hint |
24
+ | **Note nudges** | Surface deferred intentions at natural work boundaries (commit, todo completion, historian publication) |
25
+ | **Cross-harness sharing** | Memories written from OpenCode appear in Pi (and vice versa) for the same project |
26
+
27
+ ---
28
+
29
+ ## Installation
30
+
31
+ 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 `magic-context.jsonc`, and verifies your model picks):
32
+
33
+ ```bash
34
+ npx @cortexkit/magic-context@latest setup --harness pi
35
+ ```
36
+
37
+ This handles everything for you:
38
+ 1. Adds `npm:@wolfx/pi-magic-context` to Pi's `packages` array in `~/.pi/agent/settings.json` (the same place `pi install` writes to)
39
+ 2. Creates `~/.pi/agent/magic-context.jsonc` with defaults
40
+ 3. Prompts you for historian, dreamer, sidekick, and embedding model choices
41
+ 4. Warns about provider-specific gotchas (e.g. GitHub Copilot reasoning models need an explicit `thinking_level`)
42
+
43
+ If you'd rather register the Pi extension package directly with Pi (skipping the wizard), use Pi's own installer:
44
+
45
+ ```bash
46
+ pi install npm:@wolfx/pi-magic-context
47
+ ```
48
+
49
+ This adds the extension to `~/.pi/agent/settings.json` but won't write `magic-context.jsonc` for you — you'll need to create it manually (see Configuration below).
50
+
51
+ To check installation health later:
52
+
53
+ ```bash
54
+ npx @cortexkit/magic-context@latest doctor --harness pi
55
+ ```
56
+
57
+ ---
58
+
59
+ ## Configuration
60
+
61
+ Magic Context reads two config files (in this priority order):
62
+
63
+ 1. `$cwd/.pi/magic-context.jsonc` (project-level overrides)
64
+ 2. `~/.pi/agent/magic-context.jsonc` (user-level defaults)
65
+
66
+ Both are merged through a Zod schema. Invalid fields fall back to defaults — bad config never disables the plugin entirely.
67
+
68
+ ### Minimal config
69
+
70
+ ```jsonc
71
+ {
72
+ "$schema": "https://raw.githubusercontent.com/cortexkit/magic-context/master/assets/magic-context.schema.json",
73
+ "enabled": true,
74
+ "historian": {
75
+ "model": "anthropic/claude-haiku-4-5"
76
+ },
77
+ "embedding": {
78
+ "provider": "local"
79
+ }
80
+ }
81
+ ```
82
+
83
+ For the full configuration reference (including dreamer, sidekick, auto-search, and experimental features), see [CONFIGURATION.md](https://github.com/cortexkit/magic-context/blob/master/CONFIGURATION.md) in the main repository — the schema is shared between both plugins.
84
+
85
+ ---
86
+
87
+ ## Slash commands
88
+
89
+ All commands trigger `triggerTurn: false` (never sent to the LLM):
90
+
91
+ | Command | What it does |
92
+ |---|---|
93
+ | `/ctx-status` | Live token breakdown + queued ops + cache state |
94
+ | `/ctx-flush` | Force-process pending ops queue |
95
+ | `/ctx-recomp` | Rebuild compartments from raw history (heavy operation) |
96
+ | `/ctx-dream` | Trigger a dream run on demand |
97
+ | `/ctx-aug` | Augment your next prompt with sidekick-retrieved memories |
98
+
99
+ ---
100
+
101
+ ## Storage
102
+
103
+ Magic Context stores everything in a single shared SQLite database at:
104
+
105
+ ```
106
+ ~/.local/share/cortexkit/magic-context/context.db
107
+ ```
108
+
109
+ This is the **same database** the OpenCode plugin uses. Tables are scoped by:
110
+ - `harness` column (`'pi'` or `'opencode'`) for session-scoped data (tags, compartments, facts, notes)
111
+ - `project_path` (resolved git root) for project-scoped data (memories, embeddings, dreamer runs)
112
+
113
+ So memories and dreamer state are shared across both harnesses for the same project; per-session tagging stays correctly attributed.
114
+
115
+ 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.
116
+
117
+ ---
118
+
119
+ ## Cross-harness coherence
120
+
121
+ For semantic search to work across harnesses, both plugins must use the **same embedding model**. Magic Context detects mismatch on Pi startup and warns:
122
+
123
+ ```
124
+ WARN embedding model mismatch detected for project ...:
125
+ stored vectors use "openai-compatible:Qwen/Qwen3-Embedding-8B" but Pi is configured with "local:Xenova/all-MiniLM-L6-v2".
126
+ Cross-harness search will return zero results until vectors are re-embedded.
127
+ ```
128
+
129
+ Easiest fix: configure `embedding` once in `~/.pi/agent/magic-context.jsonc` (Pi) and `~/.config/opencode/magic-context.jsonc` (OpenCode) with identical settings.
130
+
131
+ ---
132
+
133
+ ## Tools available to the agent
134
+
135
+ | Tool | Action set | Purpose |
136
+ |---|---|---|
137
+ | `ctx_search` | n/a | Search memories + raw session history; returns ranked results with previews |
138
+ | `ctx_memory` | `write`, `delete` | Manage project memories explicitly (most writes happen via dreamer instead) |
139
+ | `ctx_note` | `read`, `write`, `update`, `dismiss` | Defer intentions for later — surfaced via note nudges at work boundaries |
140
+
141
+ `ctx_expand` and `ctx_reduce` from the OpenCode plugin are **intentionally not exposed on Pi** — they depend on raw OpenCode message ordinals, while Pi has its own message identity model. Drops still happen automatically via threshold-driven historian; you don't need an explicit `ctx_reduce` to trigger reduction.
142
+
143
+ ---
144
+
145
+ ## Architecture & implementation
146
+
147
+ This package is part of the [magic-context monorepo](https://github.com/cortexkit/magic-context). The Pi extension shares the core implementation with the OpenCode plugin via the `@magic-context/core` workspace dependency, exposing only the Pi-specific adapter layer:
148
+
149
+ | Pi-specific module | Responsibility |
150
+ |---|---|
151
+ | `context-handler.ts` | Pi `pi.on("context", ...)` adapter — tags, drops, runs nudges and auto-search |
152
+ | `subagent-runner.ts` | Spawns `pi --print --mode json --no-extensions --extension <lean-entry> ...` for historian/sidekick/dreamer subagents, with a 2-second drain after the terminal `message_end` so child processes don't keep the parent waiting |
153
+ | `tools/` | Pi `pi.registerTool` wrappers around the shared tool implementations |
154
+ | `commands/` | Pi `pi.registerCommand` wrappers for the five `/ctx-*` slash commands |
155
+ | `dreamer/` | Pi-side adapter for the shared dreamer scheduler |
156
+ | `system-prompt.ts` | Pi `before_agent_start` injector for `<session-history>`, `<project-memory>`, `<project-docs>` |
157
+ | `config/` | Pi-convention config loader (`$cwd/.pi/magic-context.jsonc` + `~/.pi/agent/magic-context.jsonc`) |
158
+
159
+ The CLI lives in the unified [`@cortexkit/magic-context`](https://www.npmjs.com/package/@cortexkit/magic-context) package — `setup --harness pi` and `doctor --harness pi` route to the Pi-specific code paths in `packages/cli/src/commands/`.
160
+
161
+ For deeper architectural detail, see the main repo's [ARCHITECTURE.md](https://github.com/cortexkit/magic-context/blob/master/ARCHITECTURE.md).
162
+
163
+ ---
164
+
165
+ ## License
166
+
167
+ MIT — see [LICENSE](https://github.com/cortexkit/magic-context/blob/master/LICENSE).