pi-lean-memory0.1.0
Lean, zero-dependency memory for Pi: three tools, fast built-in BM25 search, bounded prompt footprint, no shutdown LLM calls
Untested on omp
This is a pi package. No one has reported whether it works on omp yet. If you try it, add your result below.
Reports 0
No reports yet.
Readme
pi-lean-memory
Lean, zero-runtime-dependency memory for Pi. Replaces pi-memory.
- 3 tools (
memory_search,memory_write,memory_manage), 1,934 chars of tool prompt in total. - Built-in fast search: BM25 + exact substring/token (IPs, ids,
#tags) + CJK bigrams. ~10 ms on a 450 KB corpus, no index, no external binary. - Bounded injection: one
<memory>prompt section, hard-capped at 6,500 chars, rebuilt only after a memory mutation.daily/logs are never injected. - No background work: no LLM call on shutdown, no
qmd update/embed, nothing appended todaily/on compaction. - Safe paths: roots/parents are canonicalized, symlinks and non-regular files are rejected for mutations and skipped when reading; reserved topic names (
backlog,memory,scratchpad, Windows device stems) are refused case-insensitively. - Safe writes: secret guard (content and topic),
MEMORY.mdcapped at 6,000 chars, compact receipts,forgetwrites a recovery record first and refuses mass deletes.
Install
pi install npm:pi-lean-memory
Or add "npm:pi-lean-memory" to packages in ~/.pi/agent/settings.json (remove npm:pi-memory first, as both register memory_search and memory_write).
Layout
| Scope | Location |
|---|---|
| global | ~/.pi/agent/memory/ (override with PI_MEMORY_DIR): MEMORY.md (standing rules), SCRATCHPAD.md (global backlog), topics/*.md, daily/*.md (legacy, search-only), recovery/<uuid>.json |
| project | <project>/docs/memory-notes/*.md, backlog.md. Project = nearest parent of cwd containing docs/memory-notes/ or .git |
Tools
memory_search({ query, scope?, mode?, limit? }): scope all (default) / project / global; mode fast (default) / semantic; limit 1..10 (default 5). Returns path:line snippet (snippet <= 240 chars, max 2 hits per file). daily/ files older than 30 days are skipped unless the query contains a YYYY-MM-DD date. semantic runs qmd vsearch --json -c pi-memory -n <limit> <query> (global collection only; project hits are topped up by fast search) and falls back to fast with a one-line note if qmd is missing, times out (30 s) or fails.
memory_write({ content, scope?, target?, topic? }): scope project (default inside a project) / global.
| target | writes to | limits |
|---|---|---|
note (default) |
<project>/docs/memory-notes/<topic>.md or ~/.pi/agent/memory/topics/<topic>.md, as <!-- YYYY-MM-DD HH:MM --> + text (default topic notes) |
<= 4,000 chars |
backlog |
- [ ] [YYYY-MM-DD] text in project backlog.md or global SCRATCHPAD.md |
one line, <= 300 chars |
rules |
bullet appended to global MEMORY.md |
<= 700 chars, no raw IPv4, file <= 6,000 chars |
Every write rejects obvious secrets (sk-…, ghp_…, AKIA…, private-key headers, password: …). The receipt is <= 200 chars (target, path, bytes) and never echoes file contents.
memory_manage({ action, target?, scope?, topic?, match?, recoveryId?, force?, maxChars? })
| action | behaviour |
|---|---|
read |
bounded read of MEMORY.md (rules, default), backlog, or a note (topic; no topic lists notes). Default 4,000 chars, max 12,000. |
forget |
removes blocks containing match (case-insensitive). Blocks = timestamp-comment sections; outside those, paragraphs / bullets / table rows. recovery/<uuid>.json is written before the file changes. More than 3 matches is refused (with previews) unless force: true. |
restore |
re-appends blocks from recovery/<recoveryId>.json (v2 records; legacy v1 long_term/daily records from pi-memory are decoded — a legacy daily restore is the one exception to "never writes daily/"). Compares whole blocks with multiplicity, no-ops once restoredAt is set, and only targets the global dir or the active project's notes. |
backlog_list / backlog_done |
list open - [ ] items / mark the matching one - [x] (ambiguous matches need force). |
status |
dirs, MEMORY.md usage vs cap, note counts, open backlog, qmd availability. |
Commands
/memory: status viactx.ui.notify./memory-clean: deleterecovery/*.jsonolder than 30 days.
What gets injected
before_agent_start sets systemPromptOptions.sections.memory (Pi wraps it in <memory>…</memory> and sends only a patch when it changes):
- global
MEMORY.md, up to 5,200 chars (tail-truncated with an explicit marker, never a silent middle cut) - one-line list of global
topics/*.md - in a project with
docs/memory-notes/: one-line list of notes + up to 5 openbacklog.mditems (<= 800 chars)
The snapshot is cached for the session. Only memory_write / memory_manage mutations mark it dirty; external edits show up after /reload or a new session.
Measured prompt footprint
Measured by npm test (tool figures) and against a copy of the owner's real memory dir (injection):
| Component | pi-memory 0.4.2 | pi-lean-memory |
|---|---|---|
| Tool declarations (description + schema + snippet + guidelines) | 7 tools, 4,698 chars | 3 tools, 1,934 chars (search 559, write 624, manage 751) |
Injected context, owner's 4,983-char MEMORY.md |
4,843 chars (MEMORY.md middle-truncated to 4,000) | ~5,130-5,210 chars (whole MEMORY.md + topic/note index) |
| Injected context ceiling | 16,000 chars | 6,500 chars |
| Daily logs in prompt | today + yesterday (up to 6,000) | never |
| Write receipt | up to 4,000-char echo of the log tail | <= 200 chars |
LLM / qmd calls at shutdown or compaction |
yes | none |
Development
npm install
npm run typecheck
npm test
MIT © 2026 kslamph