Instruction file imported from YoussefChouj/Model_Reference_adaptive-controller_UAV_linux (
.cursor/rules/knowledge-stack.mdc). Copyright stays with the author.
Knowledge Stack
| Layer | Tool | Query method | STOP here when it answers |
|---|---|---|---|
| Code search | CocoIndex | ccc search "query" |
exact code locations, symbol defs |
| Code graph | Graphify | read graphify-out/GRAPH_REPORT.md |
system-wide deps, who owns what |
| Knowledge wiki | LLM Wiki | read wiki/index.md |
architecture, design decisions, gotchas |
| Decisions | decisions.md | read docs/decisions.md |
why something was built a certain way |
| Interfaces | interfaces.md | read docs/interfaces.md |
cross-subsystem contracts |
| Lessons | lessons.jsonl | read .agent_memory/lessons.jsonl |
past task learnings, mistakes to avoid |
| External research | Apify MCP | wiki/concepts/external-research-tools.md |
looping, no local answer, fact-check, YouTube, web docs |
Three-layer knowledge hierarchy (see session-lifecycle.mdc)
| Layer | Storage | Coyle's term | Scope |
|---|---|---|---|
| Permanent structural knowledge | wiki/ |
Ontology | Principles, architectures, domain facts — durable, queryable, shared across all agents and sessions |
| Decision rationale | docs/decisions.md, docs/adr/ |
Axioms | Why something was built a certain way; constrains future choices |
| Session digest | sessions_summary/YYYY-MM-DD-digest.md |
Ephemeral ledger | One-session findings, decisions, unresolved questions; cross-session continuity only |
The wiki is the Ontology. It holds the formal shared conceptualization — what entities exist, what relationships matter, what invariants must hold. It is queried before planning, consulted before assuming, and updated only by explicit operator confirmation.
The knowledge-stack query order maps to these layers:
1. ccc search → code layer (exact locations)
2. GRAPH_REPORT.md → code layer (system-wide deps)
3. wiki/ → Ontology layer (why, design, principles)
4. docs/decisions.md → Axioms layer (decision rationale)
5. docs/interfaces.md → Axioms layer (cross-subsystem contracts)
6. lessons.jsonl → Axioms layer (past learnings)
7. Apify MCP → External loop (no local answer)
When to use ccc vs grep
Use ccc search |
Use raw grep |
|---|---|
| conceptual / semantic queries | exact symbol/function name known |
| cross-subsystem exploration | need ALL call sites (ccc caps ~10) |
| noisy codebase, broad terms | scoped to a known directory, speed critical |
Adding knowledge: drop sources in raw/, then run the wiki ingest. Re-index the graph after wiki/code changes.
Gate enforcement (PreToolUse hook)
Claude Code's PreToolUse hook on Glob|Grep runs .agent_scripts/knowledge_gate.py.
- The gate classifies the command (snake_case / file:line → ccc; cross-subsystem / god node → graphify; why / ADR / architecture → wiki) and tells you which layer to consult.
- For each layer you consult, record it:
python .agent_scripts/knowledge_gate.py --touch <layer>. - After all three layers are touched,
python .agent_scripts/knowledge_gate.py --unlocklets grep/glob through silently for the rest of the day. --unlockis hard to skip — it refuses until all three layers have been touched at least once. This forces the agent to know the full stack before raw-searching.- Staleness-aware (via
knowledge_state.py): if a layer is stale (HEAD moved pastlast_commit, or known files drifted), the gate's block message says so and tells you which refresh command to run (/graphify --update,/wiki ingest, etc.). - Full protocol:
wiki/concepts/knowledge-gate-enforcement.md.
Per-path refresh (Stop hook)
Claude Code's Stop hook runs knowledge_loop.py after every turn. The flow is:
- The
PostToolUsehook onWrite|Edit|MultiEdit|NotebookEditrecords each file the agent touches to a session-keyed JSONL trail:.agent_state/trail_<session>.jsonl. - At session stop,
knowledge_loop.py runreads the trail, identifies the touched files, and runs the structural stages:- drift detect — re-evaluate stale flags in the manifest
- annotate graph — mark touched graph nodes with
last_touched_session+ append achange_logentry - Recent change — append
## Recent change (YYYY-MM-DD)to affected wiki concept pages (idempotent — same date is skipped) - fresh stamp — mark
graphify,wiki, andcccfresh inknowledge_manifest.json - audit — log everything to
.agent_state/knowledge_loop_log.json
- The LLM stages are NOT in the hook path. Two options for them:
- Mid-task: the working agent invokes
knowledge_loop.py delta_update --paths a,b,c(orwiki_check --paths a,b,c). The script returns JSON prompts; the agent runs them through its own model and applies the result viapath_refresh.merge_delta_into_graph(...)orpath_refresh.autonomous_wiki_rewrite_from_verdict(...). - End-of-task: the working agent dispatches the dedicated
uav-knowledge-writersubagent (.cursor/agents/uav-knowledge-writer.md, pinned tocursor-grok-4.5-high). That subagent has its own model + system prompt + read access to source files; it does the LLM call, parses the result, applies the rewrites (with backup), and appends to the audit log.
- Mid-task: the working agent invokes
- The whole-corpus
/graphifyis not triggered automatically — only the structural annotation. The next user-initiated/graphify --updateis when the LLM re-extraction runs. - The git post-commit hook fires the same structural loop in the background on every commit (installed via
python .agent_scripts/install_post_commit.py install).
This means the knowledge graph becomes a journal of the agent's path, growing richer with each task, with no human in the loop and no external LLM API key required.
