mnemon icon indicating copy to clipboard operation
mnemon copied to clipboard

Pi integration: full guide injected as a visible message every turn + unconditional agent_end notify (chat noise, ~1K tokens/turn)

Open yufeng-liang opened this issue 3 weeks ago • 0 comments

Environment

  • mnemon CLI 0.2.13, installed via npm i -g --include=optional @mnemon-dev/mnemon (Windows)
  • Host: PI-Desktop 0.22.0 (a Pi-compatible fork host; also reproduced the extension shape against plain pi), extension loaded from internal/memory/setup/assets/pi/mnemon.ts (the copy mnemon setup --target pi --global deploys)
  • Memory: local Ollama bge-m3 embeddings, 1.4k insights — i.e. a normal working setup, not an edge case

Symptom

Every single turn produces two pieces of UI chatter, and the model re-pays for the whole guide each turn:

  1. before_agent_start returns the injection with display: true, so the entire memory block — the full guide.md — is drawn into the conversation as a visible message on every turn, not once per session.
  2. agent_end fires an unconditional ctx.ui.notify(...) on every turn:
pi.on("agent_end", async (_event, ctx) => {
  ctx.ui.notify("[mnemon] Consider whether this exchange warrants durable memory.", "info");
});

PI-Desktop prefixes host-side notifications with the plugin name, so the user sees this line after every reply, forever:

Mnemon Memory: [mnemon] Consider whether this exchange warrants durable memory.

It also duplicates the reminder already inside the injected block ("[mnemon] Evaluate: recall needed? After responding, evaluate: remember needed?"), so the same instruction arrives twice per turn — once in context, once in the UI.

Cost measured

guide.md shipped by setup is 3,584 B ≈ 986 tokens (4,916 B ≈ 1,228 tokens on this machine, because I appended a store-conventions section). Injected per turn, unconditionally.

Reproduced with a stubbed ExtensionAPI (Node native TS type-stripping + a Proxy api object, real handlers invoked — no GUI needed):

turn 1 turn 2 turn 3 visible bubble notify per turn
upstream asset 4,550 chars 4,550 chars 4,550 chars yes 1
patched (below) 4,697 chars 275 chars 275 chars no 0

A 20-turn session pays ~22,000 tokens for a block the model already saw in turn 1 of the same session.

Why the fix is small

PI's own renderer and context builder disagree about display, which makes the UI half free to fix:

// render path: only drawn when display is truthy
else if (e.entry.type === "custom_message" && e.entry.display) this.addMessageToChat(...)

// context path: always included, display is never consulted
case "custom": return { role: "user", content: typeof e.content === "string" ? [{ type: "text", text: e.content }] : e.content }

So display: false removes the bubble and loses nothing from the prompt.

Proposed minimal changes

  1. visibleMessage() → display: false (or make it a setup-time option for hosts that do want the transcript).
  2. Inject the full guide once per session, a one-line reminder afterwards. What we run locally:
const GUIDE_REMINDER = "[mnemon] Protocol from session start applies: recall before answering when the turn depends on past decisions/preferences/infrastructure; save durable facts with remember when warranted; never store secrets or raw transcript.";
let guideInjectedSessionId: string | null = null;

pi.on("session_start", async (_event, ctx) => {
  ctx.ui.setStatus("mnemon", "mnemon");
  guideInjectedSessionId = null;          // baseline must NOT be written here
});

pi.on("before_agent_start", async (_event, ctx) => {
  let sid: string | null = null;
  try { sid = ctx.sessionManager.getSessionId(); } catch { sid = null; }
  const full = sid === null || guideInjectedSessionId !== sid;   // no sid => conservative: always full
  const parts = full ? [memoryStatus(), readGuide(), GUIDE_REMINDER] : [memoryStatus(), GUIDE_REMINDER];
  const content = parts.filter(Boolean).join("\n\n");
  if (full) guideInjectedSessionId = sid;
  return { message: memoryMessage(content) };
});
  1. Drop the agent_end notify, or gate it (config key / only when remember was actually consulted) — it is the loudest part and carries no information the injection doesn't already carry.

⚠️ Trap worth documenting: setting the baseline inside session_start (guideInjectedSessionId = ctx.sessionManager.getSessionId()) makes the very first turn look already-injected, so the full guide never lands. session_start must clear the marker; only before_agent_start writes it. My first implementation did exactly this and silently shipped the reminder-only path from turn 1.

Notes

  • testdata/memory/pi/mnemon-before-164.ts and test/memory/pi/fixtures.mjs pin this file's behaviour, so tests need updating alongside.
  • Not reporting this as a security or correctness bug — it's noise plus a recurring token tax in GUI hosts. Happy to open a PR for the three changes above if you want them; I have them working behind a local patch marker (MNEMON PI LOCAL PATCH v1) with the stubbed-extension test as regression cover.

yufeng-liang avatar Nov 10 '26 09:11 yufeng-liang