Memory gate

From rjmurillo/ai-agents · View interactive version

Memory gate is an AI agent skill from the GitHub repository rjmurillo/ai-agents. Memory-First Gate (BLOCKING) and the Chesterton's Fence investigation protocol, split out of the memory router per ADR-063. Forces a memory search before you change existing code, constraints, or protocol, so the "why" is recovered before the fence comes down. Use when you say `memory-first gate`, `

How to use this skill

Skill file

Memory Gate

The Memory-First Gate and its investigation protocol, extracted from the

memory router per ADR-063 (memory-skill decomposition). memory still routes

here; an agent about to change an existing system loads this sub-skill instead

of the full memory surface.

Before changing an existing system, use memory-search to recover its rationale.

This skill owns enforcement and the Chesterton's Fence investigation.

Triggers

Use this skill when the user says:

Memory-First Gate (BLOCKING)

Before changing existing systems, you MUST:

  1. Run memory-search for the topic you are about to change.
  2. Review results for historical context.
  3. If Tier 1 is insufficient, check episodes and current repository evidence.
  4. Document findings in your decision rationale.
  5. Only then proceed with the change.
# Recover the "why" before you change the "what"
( for d in "${CLAUDE_PLUGIN_ROOT}" "${COPILOT_PLUGIN_ROOT}" .claude; do if [ -n "$d" ] && [ -f "$d/lib/ptk_run.py" ]; then exec python3 "$d/lib/ptk_run.py" skills/memory/scripts/search_memory.py "[topic]"; fi; done; echo "ptk_run: no plugin root holds lib/ptk_run.py" >&2; exit 2 )

Why BLOCKING: under 50% compliance with "check memory first" guidance when it

is advisory. Making it BLOCKING achieves 100% compliance, the same pattern as

the session protocol gates (ADR-070 gate semantics).

Verification: the transcript, pull request, handoff, Serena memory, or an

optional log must show the search before the decision.

The command above is the repository-file route. If tools or the script are

unavailable, use file search and git history. Record the coverage gap.

A search with no hits still satisfies the search step.

Memory-First as Chesterton's Fence

Core insight: memory-first architecture implements the Chesterton's Fence

principle for AI agents.

"Do not remove a fence until you know why it was put up" (G.K. Chesterton)

Translation for agents: do not change code, architecture, or protocol until

you search memory for why it exists.

Why this matters

Without memory search (removing the fence without investigation):

With memory search (Chesterton's Fence investigation):

Investigation Protocol

When you want to change something that already exists, match the change type to

the required search:

| Change Type | Memory Search Required |

|-------------|------------------------|

| Remove ADR constraint | search_memory.py "[constraint name]" |

| Bypass protocol | search_memory.py "[protocol name] why" |

| Delete more than 100 lines | search_memory.py "[component] purpose" |

| Refactor complex code | search_memory.py "[component] edge case" |

| Change workflow | search_memory.py "[workflow] rationale" |

What memory contains (git archaeology)

Tier 1 (Semantic): facts, patterns, constraints.

Tier 2 (Episodic): past session outcomes.

Memory is your investigation tool. It holds the "why" that Chesterton's Fence

requires you to discover before you act. For the full four-phase decision

framework (Investigation, Understanding, Evaluation, Action) and the decision

matrix for when to investigate, use the [chestertons-fence

skill](../chestertons-fence/SKILL.md).

Escalation

Tier 1 answers most "why does this exist" questions. Escalate when it does not:

Change target identified?
│
├─► Tier 1: search_memory.py "[topic]"
│   └─► Facts, constraints, ADR rationale. Enough? Proceed.
│
└─► Not enough context? Escalate to Tier 2 (episodes)
    └─► What happened last time we touched this, and how did it end?

Escalate only when the cheaper tier is insufficient. Starting at Tier 1 keeps

the token cost low; most gate checks resolve there.

Verification

| Operation | Verification |

|-----------|--------------|

| Gate search ran | Result count greater than 0 OR logged "no results" |

| Ordering correct | Search appears before the change in accepted evidence |

| Escalation justified | Tier 2 used only after Tier 1 came back thin |

| Rationale recorded | Decision rationale cites the memory findings |

Anti-Patterns

| Anti-Pattern | Do This Instead |

|--------------|-----------------|

| Changing first, searching after | Search BEFORE the decision; log it before you act |

| Treating the gate as advisory | The gate is BLOCKING; a skipped search is a failure |

| Refactoring complex code blind | Search the component's edge cases before you touch it |

| Removing a constraint on sight | Search the constraint name; recover why it was set |

| Escalating straight to Tier 2 | Start at Tier 1; escalate only when it is thin |

Process

Phase 1: Identify

Name the existing system you intend to change and the change type. Match it to a

row in the Investigation Protocol table.

Phase 2: Search

Use memory-search for the topic. Check episodes and git history when

Tier 1 is insufficient.

Phase 3: Decide

Record the findings in your decision rationale, then preserve, modify, or replace

with equal safety. Never proceed with the change before the search is logged.

Related Skills

| Skill | When to Use Instead |

|-------|---------------------|

| memory | Router for search, reflexion, or maintenance operations |

| memory-search | Plain Tier 1 lookup with no pre-change gate semantics |

| memory-reflexion | Record a completed session as an episode |

| chestertons-fence | The full four-phase investigation framework and matrix |

References

integration patterns, including the Memory-First Decision Making workflow this

gate enforces

to log a first-principles finding that contradicts conventional wisdom

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.