AI Agent Memory for Codebases: Verified Anchors
skillBuild and correct an AI agent's project memory by anchoring each entry to verified code, de-duplicating and linking entries. Use to learn a codebase.
agents that use this skill
instructions
Overview
The product of this work is the project's long-term memory: a complete, structured, de-duplicated, retrievable knowledge base about this codebase that makes every later session faster and more correct. Read code to understand it; do not change it. In this mode write memories and nothing else: no source edits, no builds, no refactor proposals.
- "Learn the project": run the autonomous grounding pass, then report what was stored.
- "Update memory about X": recall X, verify against code, reconcile, store.
Mental model
Memory is only as useful as it is true. Phase 1 is autonomous and grounded, and its verification step is never skipped. Phase 2 is user-directed polish.
Rules
Phase 1: autonomous grounding (zero assumptions)
- Survey existing memory:
memory(action="search")with a query list on the project (architecture, modules, conventions, decisions, entry points). Know what exists before adding. - Map the codebase in one parallel discovery block: list the working directory for structure, read README, config and manifest files, outline the main source files, semantic code search for cross-cutting concerns, call-graph search to trace component relationships.
- Verify before recording, and cite the source. Every fact traces to a specific place you read (a file:line, a signature, a config value), and that anchor goes into the memory. If you cannot point to where it lives in the repo, do not store it. No inference dressed as fact.
- Reconcile with existing memory. A new fact not present: store it with
memory(action="store"). Existing memory matches code: leave it. Existing memory contradicts code: store the corrected fact withsupersedes=["<id>"]of the stale note, which replaces it;memory(action="delete", id=...)only notes that were never true. Only notes stored throughmemorycan be superseded or deleted; lessons and project docs in the results are read-only. Never duplicate something search already returned. - Connect related memories as you store them: give them a shared tag and name the related entry in
content, so search surfaces connected context rather than isolated trivia.
Phase 2: user-directed polish
- After grounding, summarize what was stored in 2 or 3 lines and ask in one line what to deepen, correct or prioritize.
- For each user request: recall the area first, verify against code, then reconcile (store, supersede, delete). User corrections are ground truth: store them immediately.
- Keep going until the user is satisfied the memory is complete and accurate.
Batch independent calls. Recall always goes in the first discovery block. Reading code one file at a time after the targets are known is wasted work: batch the reads.
What makes a good memory entry
- Atomic: one self-contained fact or decision per entry. Multi-fact entries surface for queries that match only one part and pollute recall.
- Provenance: anchor each entry to its source (
path/to/file.ext:NN, a signature, a config key). A fact a future session cannot trace is a fact it cannot trust. - Retrieval-phrased: write each entry in the vocabulary a future query will use ("authentication middleware", "retry/backoff policy", "DB migration flow"), not internal jargon. Recall is semantic; phrasing is what surfaces it.
- Right granularity: function, class or module level, not whole-file dumps and not brittle single-line snapshots. Capture the durable shape, not the volatile detail.
- Capture: architecture and module boundaries, key entry points (file:line), data flow, naming and style conventions, build and test commands, external dependencies and integrations, deliberate design decisions and their rationale, known constraints and gotchas.
- Skip: transient state, line-by-line restatement of code, facts obvious from a glance at the repo ("it's a TypeScript project"), multi-step how-to procedures (those belong in skills or rules), anything that changes every commit, speculation.
Grounding labels
- [CONFIRMED]: read directly in the repo or stated by the user. Store as ground truth.
- [INFERRED]: you concluded it. Verify against code before storing; if unverifiable, do not store it.
Tool hierarchy (read-only)
- Symbol search: exact symbol or pattern.
- Semantic code search: descriptive multi-term queries about functionality.
- Call-graph search: relationships from a known file.
- File outline: layout before reading.
- Ranged reads: only what you need.
- Full read: only small or config files.
Use shell only for read-only inspection the tools cannot do (listing git history). Never edit, never run builds.
De-duplication and consolidation
Search before every store; it is the dedup gate. Then choose deliberately.
- New fact, no overlap: store it.
- Existing entry still accurate but thinner: there is no in-place update. Store the richer entry with
supersedes=["<id>"]of the thin one, never a near-copy without it. - Existing entry contradicted by code: store the corrected one with
supersedes=["<id>"]of the stale note; delete only entries that were never true. - Two facts are connected: give them a shared tag and name the other entry in
contentrather than merging them into one blob.
A bloated, redundant store retrieves worse than a small accurate one. Refine, don't accumulate.
Situations
- A user corrects a fact: store it immediately with the tag
user_confirmed, superseding the wrong entry (or deleting it, if it was never true). - Ambiguous request: ask one targeted question grounded in what you found.
- Two empty recall-and-search rounds on an area: stop, report the gap, ask.
Output format
After a grounding pass or a polish round, report briefly:
- 1 line: what was learned or changed at a high level.
- Up to 6 bullets grouped by area (Architecture, Conventions, Entry points, Decisions, Gotchas), each a one-phrase summary of what was stored or corrected.
- 1 line of counts: N stored, N corrected, N superseded.
- End phase 1 by asking what the user wants to deepen next.
No prose essays, no restating code.
Checklist
- Nothing stored that was not confirmed in code or heard from the user.
- Search run before every store; no duplicates created.
- Contradicted entries superseded, or deleted when never true.
- No transient state or line-by-line code restatements stored.
- No source edited, no build run.
Composition / References
developer-context-scoutingproduces a task-specific map of entry points; this skill stores durable project knowledge.