Skip to content

fix: inject memory as a system prompt section instead of replacing the prompt - #52

Open
felipeadeildo wants to merge 1 commit into
jayzeng:mainfrom
felipeadeildo:fix/memory-prompt-section
Open

felipeadeildo wants to merge 1 commit into
jayzeng:mainfrom
felipeadeildo:fix/memory-prompt-section

Conversation

@felipeadeildo

Copy link
Copy Markdown

Fixes #50. Covers the part of #37 that main still has.

before_agent_start now writes the memory block to event.systemPromptOptions.sections.memory when pi exposes sections, instead of returning a replacement systemPrompt. When the block changes, pi appends it as a system message and keeps the cached prefix. Older pi versions, which have no sections, and per-turn mode keep the replacement, with the same bytes as before.

main already drops the snapshot timestamp, so a /reload with unchanged memory keeps the cache. The block still changes when anything was written since the last snapshot, a daily entry for example. With a replacement prompt, that rewrites the whole prompt.

Measured

Pi 1.2.0, claude-haiku-5-5, synthetic memory in PI_MEMORY_DIR. Two pi -p runs on one session, with a daily-log line appended between them. Usage of the second run's request:

pi-memory cacheRead cacheWrite
0.4.2 3,377 7,494
main 3,377 7,484
this PR 10,842 1,370

On 0.4.2 and main, only the tools stay cached. With the section, the second run reads the whole previous prompt and writes the new memory block.

Tests

  • Baseline on main: npm test 197 pass, npm run build and npm run lint clean.
  • Added 3 tests in test/unit.test.ts: the block goes to the section and nothing is returned, empty memory removes the section, per-turn still replaces the prompt. The first two failed before the change.
  • After: npm test 200 pass, npm run build and npm run lint clean.

No change to on-disk memory formats or qmd behavior.

…e prompt

Signed-off-by: Adeildo <adeildo@ranqia.com>
@srod

srod commented Nov 16, 2026

Copy link
Copy Markdown

One more case for this PR, seen on Pi 1.5.2 with pi-memory 0.4.2. With the replacement systemPrompt, sections added by extensions that load after pi-memory never reach the model. The cache cost is the smaller part of the problem.

ExtensionRunner.emitBeforeAgentStart runs the handlers in package order. When pi-memory returns systemPrompt, the runner stores the prompt rendered at that moment as forceSystemPrompt. A later handler can still write to event.systemPromptOptions.sections, but the forced string sent to the provider does not include it.

Repro: the pstack-claude Pi extension writes a pstack-models section in before_agent_start, and it loads after pi-memory in my settings.json. I asked pi -p to "reply with the line starting with 'bug-fix:' in your system prompt, or NONE":

packages order Reply
pi-memory before pstack-claude NONE
pi-memory last bug-fix: <provider>/<model> @high

The session transcript records the section in the first case too. Only the provider request loses it.

Moving pi-memory to the end of packages works around it for now. Writing to sections.memory, as this PR does, removes the ordering dependency. per-turn mode still returns a replacement here, so it keeps the dependency. A note in the README for that mode might save the next person some digging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stable snapshot still replaces the full Pi system prompt and can defeat structured-section cache preservation

2 participants