Skip to content
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
paths, and no longer admits ordinary words written in capitals (`NEVER`,
`ONLY`, `NOTE`, `FIXED`) as acronyms. Such tokens linked unrelated memories
through entity edges.
- The Pi extension no longer draws the full Mnemon guide into the conversation
and repeats it on every turn. It injects the guide once per session as a
hidden message (Pi still passes it to the model), then a one-line reminder on
later turns, and the per-reply "Consider whether this exchange warrants
durable memory" notification is gone. The extension looks for the guide in
the current session branch, so the full guide is sent again in a new
session, after compaction or `/tree` navigation removes it from the
conversation, and whenever the branch cannot be read.

## [0.2.7] - 2026-09-01

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ mnemon setup --target pi --yes

One command deploys the mnemon skill, prompt files, and a Pi TypeScript extension
to `.pi/`. The extension maps Mnemon's lifecycle reminders onto Pi events
(`resources_discover`, `before_agent_start`, `agent_end`,
(`resources_discover`, `before_agent_start`,
`session_before_compact`). Start a new Pi session or run `/reload` to activate.

### [Hermes Agent](https://github.com/NousResearch/hermes-agent)
Expand Down
2 changes: 1 addition & 1 deletion docs/zh/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ mnemon setup --target pi --yes

一条命令将 mnemon skill、prompt 文件和 Pi TypeScript extension 部署到
`.pi/`。这个 extension 会把 Mnemon 的 lifecycle reminder 映射到 Pi 事件
(`resources_discover`、`before_agent_start`、`agent_end`、
(`resources_discover`、`before_agent_start`、
`session_before_compact`)。启动新的 Pi session 或运行 `/reload` 即可激活。

### [Hermes Agent](https://github.com/NousResearch/hermes-agent)
Expand Down
46 changes: 36 additions & 10 deletions internal/memory/setup/assets/pi/mnemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,43 @@ function memoryStatus(): string {
}
}

function visibleMessage(content: string) {
function memoryMessage(content: string, guide: boolean) {
return {
customType: "mnemon",
content,
display: true,
// Pi's renderer draws only display=true messages, while its context
// builder includes the message either way.
display: false,
...(guide ? { details: GUIDE_MARK } : {}),
};
}

const GUIDE_REMINDER = "[mnemon] Evaluate: recall needed? After responding, evaluate: remember needed?";

// The guide message sent by before_agent_start carries this marker so it can be
// found again in the session. Pi keeps the message as a custom_message entry.
const GUIDE_MARK = { mnemonGuide: true };

// Whether the current branch still holds the guide in the model's context. Pi
// rebuilds that context from the branch after /tree, and after compaction keeps
// only the entries from firstKeptEntryId on, so the branch is the one source of
// truth. Any failure to read it counts as absent.
function guideInContext(ctx: { sessionManager?: { getBranch(): any[] } } | undefined): boolean {
try {
const branch = ctx?.sessionManager?.getBranch() ?? [];
let start = 0;
const compaction = branch.map((entry) => entry.type).lastIndexOf("compaction");
if (compaction >= 0) {
const kept = branch.findIndex((entry) => entry.id === branch[compaction].firstKeptEntryId);
start = kept >= 0 ? kept : compaction + 1;
}
return branch.slice(start).some((entry) =>
entry.type === "custom_message" && entry.customType === "mnemon" && entry.details?.mnemonGuide === true);
} catch {
return false;
}
}

export default function (pi: ExtensionAPI) {
pi.on("resources_discover", async () => {
const skill = path.join(agentDir(), "skills", "mnemon", "SKILL.md");
Expand All @@ -119,17 +148,14 @@ export default function (pi: ExtensionAPI) {
ctx.ui.setStatus("mnemon", "mnemon");
});

pi.on("before_agent_start", async () => {
const guide = readGuide();
const content = [memoryStatus(), guide, "[mnemon] Evaluate: recall needed? After responding, evaluate: remember needed?"]
pi.on("before_agent_start", async (_event, ctx) => {
// Send the full guide whenever the current branch does not hold it.
const guide = guideInContext(ctx) ? "" : readGuide();
const content = [memoryStatus(), guide, GUIDE_REMINDER]
.filter(Boolean)
.join("\n\n");

return { message: visibleMessage(content) };
});

pi.on("agent_end", async (_event, ctx) => {
ctx.ui.notify("[mnemon] Consider whether this exchange warrants durable memory.", "info");
return { message: memoryMessage(content, guide !== "") };
});

pi.on("session_before_compact", async () => {
Expand Down
1 change: 0 additions & 1 deletion internal/memory/setup/pi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func TestPiExtensionMapsLifecycleEvents(t *testing.T) {
`pi.on("resources_discover"`,
`pi.on("session_start"`,
`pi.on("before_agent_start"`,
`pi.on("agent_end"`,
`pi.on("session_before_compact"`,
"process.env.MNEMON_DATA_DIR",
"process.env.PI_CODING_AGENT_DIR",
Expand Down
124 changes: 124 additions & 0 deletions test/memory/pi/extension.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,127 @@ test("legacy control cannot execute the Windows npm shim", async (t) => {
assert.ok((await p.content()).startsWith("[mnemon] Memory active.\n"));
assert.equal(p.calls[0].command, "mnemon");
});

// Pi's context builder includes custom messages whatever `display` says, while
// its renderer draws only display=true. A hidden message costs the model nothing.
const piGuide = path.win32.join("C:\\Users\\Pi 中文 home", ".mnemon", "prompt", "pi", "guide.md");

// A session that records entries the way Pi does: each injected message becomes
// a custom_message entry on the current branch, and the extension may read the
// branch through ctx.sessionManager.getBranch().
function session(entries = []) {
let next = entries.length;
const s = {
entries,
add(entry) { s.entries.push({ id: `e${++next}`, ...entry }); return `e${next}`; },
ctx: {
ui: { setStatus() {}, notify() { throw new Error("unexpected notification"); } },
sessionManager: { getBranch: () => s.entries },
},
// What /tree does: the branch is replaced, no session_start, same session id.
navigate(branch) { s.entries = branch; },
async turn(p) {
s.add({ type: "message", message: { role: "user" } });
const message = (await p.hooks.get("before_agent_start")({}, s.ctx)).message;
s.add({ type: "custom_message", customType: message.customType, content: message.content, display: message.display, details: message.details });
s.add({ type: "message", message: { role: "assistant" } });
return message;
},
};
return s;
}

const hasGuide = (message) => /Pi guide/.test(message.content);

test("the injected message is not drawn into the conversation", async (t) => {
const p = await load(t, { files: { [piGuide]: "Pi guide" } });
assert.equal((await session().turn(p)).display, false);
});

test("the full guide is injected once per conversation, then a reminder", async (t) => {
const p = await load(t, { files: { [piGuide]: "Pi guide" } });
const s = session();
await p.hooks.get("session_start")({}, s.ctx);
const first = await s.turn(p);
assert.ok(hasGuide(first));
assert.match(first.content, /72 insights, 1106 edges/);
for (const again of [await s.turn(p), await s.turn(p)]) {
assert.ok(!hasGuide(again));
assert.match(again.content, /^\[mnemon\] Memory active/);
assert.match(again.content, /recall/);
assert.ok(again.content.length < first.content.length);
}
// A new or empty session gets the guide again.
assert.ok(hasGuide(await session().turn(p)));
});

test("a conversation without a readable branch keeps injecting the full guide", async (t) => {
const p = await load(t, { files: { [piGuide]: "Pi guide" } });
for (const ctx of [undefined, {}, { sessionManager: { getBranch() { throw new Error("no session"); } } }]) {
for (let i = 0; i < 2; i++) assert.match((await p.hooks.get("before_agent_start")({}, ctx)).message.content, /Pi guide/);
}
});

test("the extension does not notify the user after every reply", async (t) => {
const p = await load(t);
assert.equal(p.hooks.has("agent_end"), false);
});

test("compaction that summarizes the guide away sends it again", async (t) => {
const p = await load(t, { files: { [piGuide]: "Pi guide" } });
const s = session();
assert.ok(hasGuide(await s.turn(p)));
assert.ok(!hasGuide(await s.turn(p)));
// Keep only the last turn: the guide falls before firstKeptEntryId.
const kept = s.entries[s.entries.length - 3].id;
s.add({ type: "compaction", summary: "summary", firstKeptEntryId: kept });
assert.ok(hasGuide(await s.turn(p)));
assert.ok(!hasGuide(await s.turn(p)));
});

test("compaction that keeps the guide does not repeat it", async (t) => {
const p = await load(t, { files: { [piGuide]: "Pi guide" } });
const s = session();
assert.ok(hasGuide(await s.turn(p)));
s.add({ type: "compaction", summary: "summary", firstKeptEntryId: s.entries[0].id });
assert.ok(!hasGuide(await s.turn(p)));
});

test("navigating the tree re-sends the guide only when it left the branch", async (t) => {
const p = await load(t, { files: { [piGuide]: "Pi guide" } });
const s = session();
assert.ok(hasGuide(await s.turn(p)));
const withGuide = [...s.entries];
assert.ok(!hasGuide(await s.turn(p)));
// /tree back to before the first turn: same session id, no session_start.
s.navigate([]);
assert.ok(hasGuide(await s.turn(p)));
// /tree to a branch that still holds the guide.
s.navigate(withGuide);
assert.ok(!hasGuide(await s.turn(p)));
});

test("a guide message from an older extension version is replaced once", async (t) => {
const p = await load(t, { files: { [piGuide]: "Pi guide" } });
const s = session([{ id: "old", type: "custom_message", customType: "mnemon", content: "Pi guide", display: true }]);
assert.ok(hasGuide(await s.turn(p)));
assert.ok(!hasGuide(await s.turn(p)));
});

test("another extension's message is not taken for the guide", async (t) => {
const p = await load(t, { files: { [piGuide]: "Pi guide" } });
const s = session([{ id: "x", type: "custom_message", customType: "other", content: "Pi guide", display: false, details: { guide: true } }]);
assert.ok(hasGuide(await s.turn(p)));
});

test("a missing guide file is retried instead of being marked as sent", async (t) => {
const p = await load(t);
const s = session();
assert.equal((await s.turn(p)).details, undefined);
assert.equal((await s.turn(p)).details, undefined);
});

test("the compaction instructions hook is kept", async (t) => {
const p = await load(t);
assert.match((await p.hooks.get("session_before_compact")()).customInstructions, /Before compacting/);
});
2 changes: 1 addition & 1 deletion test/memory/pi/installed.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ for (const mode of ["native", "npm global", "npm local"]) {
assert.match(actual.start.message.content, /directly in the main conversation/);
assert.doesNotMatch(actual.start.message.content, /Task|sonnet|subagent_type/);
assert.deepEqual(actual.resources.skillPaths, [path.join(agent, "skills", "mnemon", "SKILL.md")]);
assert.equal(actual.start.message.display, true);
assert.equal(actual.start.message.display, false);

const old = JSON.parse(run(process.execPath, ["--no-warnings", probe, legacy], cwd, env));
if (process.platform === "win32") {
Expand Down
Loading