Problem
contextFor() in tree.ts flattens the conversation history into a text blob:
[system] You are reef...
[user] What is 2+2?
[tool] bash
[result] 4
[assistant] The answer is 4.
This gets injected via --append-system-prompt as a single string. The LLM sees it as unstructured text rather than proper conversation turns. Tool call params and full results are lost.
Proposal
- Add
contextForStructured(nodeId) to tree.ts — returns an array of {role, content, toolCalls?, toolCallId?} objects preserving full tool_call/tool_result pairs
- Reef writes structured context to a temp file before spawning pi
- A pi extension reads the file on
before_agent_start / context event and injects proper structured messages
This gives the LLM:
- Distinct role boundaries (user/assistant/tool)
- Full tool call params and results
- Better context window utilization (no wasted tokens on
[tag] prefixes)
Key pi extension hooks
before_agent_start — can inject messages and modify system prompt
context — can modify the full message array before each LLM call
pi.sendMessage() — inject custom messages into the session
References
src/tree.ts — contextFor() method
src/reef.ts — spawnTask() uses --append-system-prompt
- Pi extension docs:
before_agent_start, context events
Problem
contextFor()intree.tsflattens the conversation history into a text blob:This gets injected via
--append-system-promptas a single string. The LLM sees it as unstructured text rather than proper conversation turns. Tool call params and full results are lost.Proposal
contextForStructured(nodeId)totree.ts— returns an array of{role, content, toolCalls?, toolCallId?}objects preserving full tool_call/tool_result pairsbefore_agent_start/contextevent and injects proper structured messagesThis gives the LLM:
[tag]prefixes)Key pi extension hooks
before_agent_start— can inject messages and modify system promptcontext— can modify the full message array before each LLM callpi.sendMessage()— inject custom messages into the sessionReferences
src/tree.ts—contextFor()methodsrc/reef.ts—spawnTask()uses--append-system-promptbefore_agent_start,contextevents