Over time, your codebase evolves — new dependencies get added, environment variables change, files get renamed or deleted. When your .agent/ documentation doesn't reflect these changes, the AI operates on stale assumptions. Gemstack's drift detection catches this before it causes problems.
Context drift occurs when your actual codebase diverges from what's documented in your .agent/ files. For example:
- You add
redistopackage.jsonbut never mention it inARCHITECTURE.md - You remove the
STRIPE_API_KEYenv var but it's still documented - You delete
src/legacy/old-api.tsbutSTATUS.mdstill lists it as a relevant file
The AI doesn't know about these changes — it reads your .agent/ files and assumes they're accurate. Drift leads to hallucination: the AI writes code that imports removed packages, references deleted files, or uses deprecated environment variables.
Compares your .agent/ documentation against your actual codebase state:
gemstack diffThis analyzes 3 categories of drift:
Compares packages in your manifest files (package.json, pyproject.toml, go.mod, Cargo.toml) against backtick-quoted package names in ARCHITECTURE.md.
| Finding | Meaning |
|---|---|
| New dependency not documented | A package exists in your manifest but isn't mentioned in ARCHITECTURE.md |
| Documented dependency removed | A package is mentioned in ARCHITECTURE.md but no longer in your manifest |
Compares variables in .env.example against backtick-quoted variable names in ARCHITECTURE.md.
| Finding | Meaning |
|---|---|
| New env var not documented | A variable exists in .env.example but isn't mentioned in ARCHITECTURE.md |
| Documented env var removed | A variable is mentioned in ARCHITECTURE.md but no longer in .env.example |
Checks that every file path listed in STATUS.md under "Relevant Files" actually exists on disk.
| Finding | Meaning |
|---|---|
| Stale file reference | A file is listed in STATUS.md but doesn't exist on disk |
When no drift is detected:
Validates that your .agent/ directory is structurally correct:
gemstack checkThis verifies:
| Check | What It Validates |
|---|---|
| Required files exist | All 5 .agent/*.md files are present |
| STATUS.md has a valid state | The [STATE: ...] tag contains a recognized value |
| Topology is declared | ARCHITECTURE.md contains a **Topology:** [...] line |
| Plugin checks pass | Custom validation checks registered via gemstack_register_checks |
When validation fails:
Install a pre-commit hook that blocks commits when drift is detected:
gemstack hook installThis installs a pre-commit hook that runs:
gemstack check— validates.agent/integritygemstack diff— detects context drift
If either check fails, the commit is blocked with a descriptive error message.
gemstack hook remove # Remove the hooks laterAdd drift detection to your CI pipeline:
# .github/workflows/ci.yml
- name: Check .agent/ integrity
run: gemstack check
- name: Detect context drift
run: gemstack diffOr generate a full CI config automatically:
gemstack ci generate # GitHub Actions (default)
gemstack ci generate --gitlab # GitLab CIFor custom automation:
#!/bin/bash
# Pre-deploy drift check
gemstack check && gemstack diff || {
echo "❌ Context drift detected — update .agent/ before deploying"
exit 1
}When gemstack diff reports drift, here's how to fix each category:
New dependency not documented:
- Open
.agent/ARCHITECTURE.md - Add the package to the appropriate section (Core Dependencies, Optional Dependencies, etc.)
- Include the version constraint and a brief description of why it's used
Documented dependency removed:
- Open
.agent/ARCHITECTURE.md - Remove the stale reference to the package
- If it was a significant removal, note it in
STATUS.mdunder blocking issues
New env var not documented:
- Open
.agent/ARCHITECTURE.md - Add the variable to the Environment Variables section
- Include its purpose, whether it's required, and a default value if applicable
Documented env var removed:
- Open
.agent/ARCHITECTURE.md - Remove the stale reference
- Open
.agent/STATUS.md - Update the "Relevant Files" section to reflect the current file paths
- If files were renamed, update to the new paths
- If files were deleted, remove them from the list
The drift detection tools are also available as MCP tools for IDE agents:
| MCP Tool | CLI Equivalent | Description |
|---|---|---|
gemstack_diff |
gemstack diff |
Returns drift report in markdown |
gemstack_check |
gemstack check |
Returns pass/fail with error list |
This means your Cursor/Claude Desktop agent can proactively detect drift during its work without you running any commands.
-
Run
gemstack diffbefore starting new features — Catch drift early before it compounds across multiple features. -
Install git hooks on every project —
gemstack hook installtakes 2 seconds and prevents stale documentation from ever being committed. -
Keep
ARCHITECTURE.mdas the source of truth — When you add a dependency or env var, updateARCHITECTURE.mdin the same commit. -
Use
gemstack init --aifor bulk updates — If your project has drifted significantly, re-runninggemstack init --aican regenerate context files with current reality. -
Don't ignore drift warnings — Every unresolved drift item is a potential hallucination vector. The AI will reference documented packages that no longer exist.
- ⚙️ Learn what the
.agent/files contain in The.agent/Context System - 🛠️ Set up automated hooks in Configuration & Integrations
- 🧠 Understand how context is compiled in The Context Compiler