From 36742a38f11a383aa1e507edefb1083c4676cacd Mon Sep 17 00:00:00 2001 From: Mohammed Alkindi Date: Fri, 13 Nov 2026 22:58:47 -0800 Subject: [PATCH] fix(setup): stop the Pi extension repeating the guide as a visible message The Pi extension returned the full guide with display=true on every turn and also notified the user after every reply. Pi renders only display=true messages but always passes the message to the model, so the guide is now a hidden message marked in its details. Before each turn the extension looks for that marker in the current session branch, honouring the compaction cut-off, and sends only a one-line reminder when the guide is still in context. A new session, compaction, or /tree navigation that drops the guide sends it again, as does an unreadable branch. The agent_end notification is removed because the injected reminder already carries it. Fixes #177 --- CHANGELOG.md | 8 ++ README.md | 2 +- docs/zh/README.md | 2 +- internal/memory/setup/assets/pi/mnemon.ts | 46 ++++++-- internal/memory/setup/pi_test.go | 1 - test/memory/pi/extension.test.mjs | 124 ++++++++++++++++++++++ test/memory/pi/installed.test.mjs | 2 +- 7 files changed, 171 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96661716..1c785af4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 6bc4f745..1cffda00 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/docs/zh/README.md b/docs/zh/README.md index 08acc5ab..6dc9f355 100644 --- a/docs/zh/README.md +++ b/docs/zh/README.md @@ -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) diff --git a/internal/memory/setup/assets/pi/mnemon.ts b/internal/memory/setup/assets/pi/mnemon.ts index b35725b9..24644636 100644 --- a/internal/memory/setup/assets/pi/mnemon.ts +++ b/internal/memory/setup/assets/pi/mnemon.ts @@ -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"); @@ -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 () => { diff --git a/internal/memory/setup/pi_test.go b/internal/memory/setup/pi_test.go index d4722568..295abc0f 100644 --- a/internal/memory/setup/pi_test.go +++ b/internal/memory/setup/pi_test.go @@ -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", diff --git a/test/memory/pi/extension.test.mjs b/test/memory/pi/extension.test.mjs index 9ade557b..86ab1915 100644 --- a/test/memory/pi/extension.test.mjs +++ b/test/memory/pi/extension.test.mjs @@ -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/); +}); diff --git a/test/memory/pi/installed.test.mjs b/test/memory/pi/installed.test.mjs index 7726f67b..6e8cdcbd 100644 --- a/test/memory/pi/installed.test.mjs +++ b/test/memory/pi/installed.test.mjs @@ -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") {