feat: context-hook injection mode (v1.5.0)#24
Open
samfoy wants to merge 1 commit into
Open
Conversation
Adds injectionMode: "context-hook" as the new default injection strategy.
Instead of mutating event.systemPrompt in before_agent_start, memory is
now injected as an ephemeral custom message immediately before the latest
user message via the pi.on("context") hook.
Cache story:
- System prompt is never modified -> unconditional cache hits on system prompt
- Injected message falls at the natural Anthropic cache breakpoint (boundary
before the new user turn), so full conversation history before that point
always caches
- A topic shift (different relevant memories) only misses at the injection
point and forward, not from the system prompt root
Implementation (Mode D hybrid):
- before_agent_start: run semantic search once per user turn, cache result
in pendingContextBlock (no return value, no systemPrompt mutation)
- context hook: read pendingContextBlock, splice as custom message before
last user message on every LLM call; injection is ephemeral (not written
to agent.state.messages, session history, or consolidation queue)
Backwards compat:
- injectionMode: "system-prompt" restores v1.4.0 behaviour
- perTurnInjection: false keeps session_start fallback dump
- Default changes from "system-prompt" to "context-hook"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #23
Thanks @gebeer for the well-reasoned proposal — this implements exactly what you described, with one refinement to the architecture.
What changed
Memory is no longer injected by mutating
event.systemPrompt. Instead, a new Mode D hybrid approach:before_agent_start— fires once per user turn; runs the semantic search and caches the result inpendingContextBlock. Returns nothing — system prompt is never touched.pi.on("context")— fires on every LLM call within the turn; readspendingContextBlockand splices acustom/display: falsememory message immediately before the latest user message.Why the hybrid over pure context-hook
The
contexthook only receivesmessages[]— noevent.prompt. Rather than extracting the user's prompt text from the messages array on every LLM call (including every tool-call continuation),before_agent_startcaptures it cleanly once and does the single expensive search there. The context hook becomes a cheap read + array splice on each continuation.Cache story
The injection position (immediately before the latest user message) falls at the natural Anthropic cache breakpoint — everything before it caches anyway.
Ephemerality
Injected messages are not persisted to
agent.state.messages, session history, or the consolidation queue.transformContextreceives astructuredCloneand the result is only used for that one LLM call. No meta-memory accumulation.Settings
{ "memory": { "injectionMode": "context-hook" } } // new default { "memory": { "injectionMode": "system-prompt" } } // restore v1.4.0 behaviour { "memory": { "perTurnInjection": false } } // session_start dump (unchanged)Testing
@gebeer — would love it if you could test this branch against your setup, especially if you're on a provider where prompt caching is observable (Anthropic Bedrock / claude.ai API). To try it:
Then point pi at the local build in your
settings.json:{ "packages": ["/path/to/pi-memory"] }The default is now
context-hook— no config needed to test the new behaviour. SetinjectionMode: "system-prompt"to compare against the old path.Things worth verifying: