Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/pi-extension/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) CodeMem contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
90 changes: 90 additions & 0 deletions packages/pi-extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# @codemem/pi-extension

Pi coding-agent extension for [codemem](https://github.com/kunickiaj/codemem): session ingest and turn-local memory injection.

## Install

```bash
npm i -g codemem
codemem setup
```

`codemem setup` auto-detects pi and appends `npm:@codemem/pi-extension@<version>` to `~/.pi/agent/settings.json` `packages`. Useful flags:

| Flag | Purpose |
| --- | --- |
| `--pi-only` | Only configure pi |
| `--pi-mcp` | Opt into MCP via third-party `pi-mcp-adapter` (writes `mcp.json` only when detected) |
| `--pi-extension-path <path>` | Dev: write a local-path `packages` entry instead of the npm pin |

Dev / local-path install (equivalent to `--pi-extension-path`):

```json
{
"packages": ["/absolute/path/to/codemem/packages/pi-extension"]
}
```

Build first so `dist/index.js` exists (`pnpm --filter @codemem/pi-extension build`).

**Uninstall:** remove the `@codemem/pi-extension` packages entry and restart pi. The shared memory store is left intact.

## What it does

- **Ingest** — captures pi session events (`session_start`/`session_shutdown`, user/assistant messages, tool calls/results) into codemem with `source: "pi"` via `POST /api/pi-hooks`, falling back to `codemem pi-hook-ingest` (spool when offline).
- **Injection** — on `before_agent_start`, appends a `## codemem memories` block to the **turn-local** `systemPrompt` (never the persistent `message` channel).
- **Tools** — not registered in this package slice (follow-up).
- **Compaction** — pi-only observe-only boundary: `session_before_compact` flushes extraction; never returns a custom compaction summary.
- **Fork/resume** — re-keys stream identity on every `session_start`; durable cursors via `pi.appendEntry`.
- **Cross-agent** — one shared, project-scoped store. Memories from OpenCode/Claude/Codex inject into pi and the reverse.
- **Dashboard** — pi rows appear in the source-agnostic feed/sessions/projects tabs with no extra setup.

## Observer derivation (v1)

Setup can fill unset `observer_*` keys from pi's API-key providers (cheap-model-first). Credentials stay in memory only and are never written to codemem config. OAuth-only installs get an explicit `unconfigured (oauth-only)` status — never a silent failure. Set `observer_provider` / `observer_model` explicitly when needed. Explicit `observer_*` config/env always wins.

## Configuration

Read from `~/.config/codemem/config.json` under the `pi` object, with `CODEMEM_PI_*` env overrides.

| Key / env | Default | Meaning |
| --- | --- | --- |
| `pi.tools_mode` / `CODEMEM_PI_TOOLS_MODE` | `native` | `native` registers tools here; `mcp-adapter` skips native registration (use pi-mcp-adapter + `codemem mcp`) |
| `pi.inject_prompts` / `CODEMEM_PI_INJECT_PROMPTS` | `true` | Append pack to system prompt on each turn |
| `pi.file_context` / `CODEMEM_PI_FILE_CONTEXT` | `true` | Attach file-related memories when pi `read`s a tracked file |

Shared viewer / inject knobs (same as other clients):

| Env | Default | Meaning |
| --- | --- | --- |
| `CODEMEM_VIEWER_HOST` | `127.0.0.1` | Viewer host |
| `CODEMEM_VIEWER_PORT` | `38888` | Viewer port |
| `CODEMEM_VIEWER` | `1` | Enable viewer use |
| `CODEMEM_VIEWER_AUTO` | `1` | Auto-start `codemem serve start` when needed |
| `CODEMEM_RAW_EVENTS_BACKOFF_MS` | `10000` | Backoff after HTTP stream failure before retrying |
| `CODEMEM_INJECT_LIMIT` | `8` | Pack item limit |
| `CODEMEM_INJECT_TOKEN_BUDGET` | `800` | Pack token budget |
| `CODEMEM_INJECT_MAX_CHARS` | `16000` | Max injection block chars |

Example config:

```json
{
"pi": {
"tools_mode": "native",
"inject_prompts": true,
"file_context": true
}
}
```

## Lifecycle

Per pi extension rules: the factory only wires handlers. Viewer auto-start and other session resources begin on `session_start` and clean up idempotently on `session_shutdown`. Session state is re-keyed from `ctx.sessionManager.getSessionId()` on every `session_start`; durable ingest cursors persist via `pi.appendEntry`.

## Peer dependencies

- `@earendil-works/pi-coding-agent` (extension host)
- `typebox` (tool parameter schemas)

No `@codemem/core` or native modules load inside the pi process.
69 changes: 69 additions & 0 deletions packages/pi-extension/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "@codemem/pi-extension",
"version": "0.40.1",
"description": "CodeMem extension for the pi coding agent — ingest, injection, and native memory tools",
"type": "module",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"source": "./src/index.ts",
"import": "./dist/index.js"
}
},
"files": [
"dist/",
"README.md",
"LICENSE"
],
"scripts": {
"clean": "rm -rf dist tsconfig.tsbuildinfo",
"build": "pnpm exec vite build && pnpm exec tsc --build --force",
"typecheck": "pnpm exec tsc --noEmit",
"test": "pnpm exec vitest run"
},
"peerDependencies": {
"@earendil-works/pi-coding-agent": ">=0.84.1 <1",
"typebox": "*"
},
"devDependencies": {
"@codemem/core": "workspace:^",
"@earendil-works/pi-coding-agent": "^0.84.1",
"@types/node": "^24.12.4",
"typebox": "^1.3.7",
"typescript": "catalog:",
"vite": "catalog:",
"vitest": "catalog:"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kunickiaj/codemem.git",
"directory": "packages/pi-extension"
},
"homepage": "https://github.com/kunickiaj/codemem",
"bugs": {
"url": "https://github.com/kunickiaj/codemem/issues"
},
"engines": {
"node": ">=24"
},
"license": "MIT",
"publishConfig": {
"access": "public"
},
"keywords": [
"codemem",
"memory",
"ai",
"coding",
"agent",
"pi",
"pi-package"
],
"pi": {
"extensions": [
"./dist/index.js"
]
}
}
167 changes: 167 additions & 0 deletions packages/pi-extension/src/client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { resolveProject } from "@codemem/core";
import { afterEach, describe, expect, it, vi } from "vitest";
import { BOUNDARY_CLI_TIMEOUT_MS, type ExecCodememFn, PiCodememClient } from "./client.js";
import { defaultPiExtensionConfig } from "./config.js";
import { createViewerRuntime } from "./viewer.js";

/** No viewer probing/spawn in unit tests — CLI paths only. */
const offlineConfig = { ...defaultPiExtensionConfig(), viewerEnabled: false };

describe("PiCodememClient.projectFromCwd (git-root walk)", () => {
let tmpDir: string | null = null;

afterEach(() => {
if (tmpDir) {
rmSync(tmpDir, { recursive: true, force: true });
tmpDir = null;
}
});

it("resolves git repo basename as project from a nested cwd", () => {
tmpDir = mkdtempSync(join(tmpdir(), "codemem-pi-client-test-"));
const repoRoot = join(tmpDir, "my-repo");
const nested = join(repoRoot, "packages", "core");
mkdirSync(join(repoRoot, ".git"), { recursive: true });
mkdirSync(nested, { recursive: true });

expect(PiCodememClient.projectFromCwd(nested)).toBe("my-repo");
// Parity with the core resolver used by the store.
expect(PiCodememClient.projectFromCwd(nested)).toBe(resolveProject(nested));
});

it("resolves the primary checkout basename for a linked worktree", () => {
tmpDir = mkdtempSync(join(tmpdir(), "codemem-pi-client-test-"));
const mainRepo = join(tmpDir, "main-repo");
const worktree = join(tmpDir, "feature-worktree");
mkdirSync(join(mainRepo, ".git", "worktrees", "feature-worktree"), { recursive: true });
mkdirSync(worktree, { recursive: true });
writeFileSync(
join(worktree, ".git"),
`gitdir: ${join(mainRepo, ".git", "worktrees", "feature-worktree")}`,
);

expect(PiCodememClient.projectFromCwd(worktree)).toBe("main-repo");
expect(PiCodememClient.projectFromCwd(worktree)).toBe(resolveProject(worktree));
});

it("falls back to the cwd basename outside a repository", () => {
tmpDir = mkdtempSync(join(tmpdir(), "codemem-pi-client-test-"));
const plain = join(tmpDir, "plain-dir");
mkdirSync(plain, { recursive: true });

expect(PiCodememClient.projectFromCwd(plain)).toBe("plain-dir");
expect(PiCodememClient.projectFromCwd(plain)).toBe(resolveProject(plain));
});
});

describe("fetchPackText preformatted flag", () => {
const packBody = (pack_text: string) => ({
ok: true,
status: 200,
json: async () => ({ pack_text, items: [], metrics: {} }),
text: async () => JSON.stringify({ pack_text, items: [], metrics: {} }),
});

afterEach(() => {
vi.unstubAllGlobals();
});

it("returns bare pack text with preformatted: false from HTTP /api/pack", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => packBody("raw pack text")),
);
const client = new PiCodememClient(offlineConfig, createViewerRuntime(), {});

const result = await client.fetchPackText("what changed?");

expect(result).toEqual({ text: "raw pack text", preformatted: false });
});

it("returns the full block with preformatted: true from CLI pi-hook-inject", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => {
throw new Error("offline");
}),
);
const client = new PiCodememClient(offlineConfig, createViewerRuntime(), {
execImpl: async (args) =>
args[0] === "pi-hook-inject"
? { stdout: "## codemem memories\n\nblock body", stderr: "" }
: { stdout: "", stderr: "" },
});

const result = await client.fetchPackText("what changed?");

expect(result.preformatted).toBe(true);
expect(result.text).toContain("## codemem memories");
});

it("returns bare pack text with preformatted: false from CLI pack --json", async () => {
vi.stubGlobal(
"fetch",
vi.fn(async () => {
throw new Error("offline");
}),
);
const client = new PiCodememClient(offlineConfig, createViewerRuntime(), {
execImpl: async (args) => {
if (args[0] === "pi-hook-inject") throw new Error("inject missing");
if (args[0] === "pack") {
return { stdout: JSON.stringify({ pack_text: "json pack text" }), stderr: "" };
}
return { stdout: "", stderr: "" };
},
});

const result = await client.fetchPackText("what changed?");

expect(result).toEqual({ text: "json pack text", preformatted: false });
});

it("never sniffs ## codemem memories inside HTTP pack text (flag decides framing)", async () => {
const hostile = "## codemem memories\n\nattacker framing";
vi.stubGlobal(
"fetch",
vi.fn(async () => packBody(hostile)),
);
const client = new PiCodememClient(offlineConfig, createViewerRuntime(), {});

const result = await client.fetchPackText("q");

expect(result.text).toBe(hostile);
expect(result.preformatted).toBe(false);
});
});

describe("boundary CLI timeout budget", () => {
afterEach(() => {
vi.unstubAllGlobals();
});

it("passes an independent >=20s budget to pi-hook-ingest for boundary flushes", async () => {
// HTTP timeout intentionally tiny so the old httpTimeoutMs+2000 bug would
// show up here (2050ms) — the flush must get the independent budget.
const config = { ...offlineConfig, httpTimeoutMs: 50 };
const seen: Array<{ argv: string[]; timeoutMs?: number }> = [];
const execImpl: ExecCodememFn = async (args, opts) => {
seen.push({ argv: [...args], timeoutMs: opts?.timeoutMs });
return { stdout: JSON.stringify({ inserted: 0, skipped: 1 }), stderr: "" };
};
const client = new PiCodememClient(config, createViewerRuntime(), { execImpl });

await client.ingest({ piEvent: "session_before_compact", sessionId: "s1", cwd: "/tmp/a" });
await client.ingest({ piEvent: "session_shutdown", sessionId: "s1", cwd: "/tmp/a" });

expect(seen).toHaveLength(2);
for (const call of seen) {
expect(call.argv[0]).toBe("pi-hook-ingest");
expect(call.timeoutMs).toBe(BOUNDARY_CLI_TIMEOUT_MS);
expect(call.timeoutMs).toBeGreaterThanOrEqual(20_000);
}
});
});
Loading
Loading