Install
Install with PiG
pig install npm:pi-agents-md-contextInstall with Pi
pi install npm:pi-agents-md-contextPackages run code with your permissions and influence agent behavior. Catalog inclusion is not a security audit. Review the source before installing a third-party package.
Pi manifest JSON
The pi field of package.json.
{
"extensions": [
"./src/index.ts"
]
}README
Written by the package's publisher, shown as published in version 0.3.3 (README.md). PiG does not review it.
pi-agents-md-context
English | 简体中文
Nested AGENTS.md context for Pi, deduped through the visible session projection.
Pi loads context files from the working directory and its ancestors. Files in subdirectories are not loaded. This extension appends them when the agent reads a file under them.
Install
pi install npm:pi-agents-md-context # global, once published
pi install ./path/to/pi-agents-md-context
pi -e ./path/to/pi-agents-md-context # try once, no settings change
What it does
Reading packages/web/src/server.ts appends every context file between that directory and the working directory, root-most first:
packages/AGENTS.md
packages/web/AGENTS.md
packages/web/src/AGENTS.md (if present)
Filename precedence matches Pi: AGENTS.override.md > AGENTS.md > CLAUDE.md, one file per directory.
Why another one
Most nested-context loaders keep their dedupe state in a process-local cache. That causes two problems: resuming a session after a restart injects the same text again (and again), and nothing reloads after compaction drops the message that carried it.
This extension stores what it injected in the read result's details, which is persisted with the message, and rebuilds its state from sessionManager.buildSessionContext() — the compaction-aware projection sent to the model.
| Situation | Process-local cache | This extension |
|---|---|---|
| Restart + resume session | injects again (duplicates) | no duplicate |
| Compaction dropped the read result | never reloads (lost) | reloads on next read |
AGENTS.md edited on disk | new session only | re-injected when content changed |
| Same file read twice in a row | no duplicate | no duplicate |
Behaviour details
- Successful
read,edit, andwriteresults trigger it: a file's content, or a change to it, is in the transcript. - So do
bashcommands that read a file's content:cat,sed(not-i),head,tail,bat,less,awk. Pi agents reach for these as often asread, so they count as the same channel. cdonly moves the directory used to resolve later paths; it never loads anything by itself.ls,grep,findand other exploration commands never trigger.- At most two context files are appended per tool result, so one
cat a b ccannot flood the context. - Targets outside the working directory are ignored; the ancestor chain above it is Pi's job.
- Content is hashed. Unchanged files stay quiet; changed files are injected again.
- Reading a context file itself is recorded, not appended back — but only when the whole file was shown. A slice (
head,sed -n,cat f | head, a ranged or truncatedread, anedit) leaves it unrecorded, so the next call in that directory injects the full file. - Injection lands in the tool result, not the system prompt, as one extra text block after the tool output: Pi's own
Project-specific instructions and guidelines:preamble plus a<project_instructions path="…">wrapper, so the model reads it the same way it reads the context files Pi already loaded — and can see it is not part of the file the tool just returned. - Being a tool result, it can still be compacted away; rules that must survive belong in a root
AGENTS.mdor.pi/APPEND_SYSTEM.md. - Search-only work (
grep,rg,ls,find) loads nothing; the context arrives at the first file read, whether throughreadorcat/sed. - Writes through the shell (
sed -i,tee, output redirection) are not detected either;editandwriteare.
Source layout
src/projection.ts rebuiltState(messages) pure: visible records -> known map
src/attachments.ts planAttachments(input) pure: one file -> what to append
src/bashReads.ts bashReadTargets(cmd, cwd) pure: shell command -> files it reads
src/index.ts extension wiring (session_start / session_compact / tool_result)
Both pure functions are covered by node --test; no Pi process is started.
npm install
npm test
npm run typecheck
License
MIT