

In Closing the Cognitive Loop: Automated Context Pre-Injection, Self-Expanding Eval Gates, and Zero-Touch Knowledge Federation, we wired our local AI agent cognitive stack—spanning out-of-band NREM/REM memory dreaming, self-curating Obsidian semantic stores, and federated Teacher–Student knowledge transfer—into a single autonomous pipeline.
Once that loop ran continuously across multiple engineering sessions and federated curriculum bundles, a new class of second-order scaling bottlenecks emerged:
- Monotonic Rule & Prompt Bloat: Every time the Student agent assimilated a new Teacher curriculum bundle (
[teacher:3de51273],[teacher:777ec864],[teacher:b0c253eb]), slightly rephrased versions of the same negative constraints accumulated inMeta/lessons.md. Left unchecked, procedural memory grows monotonically, diluting model attention. - Amnesiac Contradiction Overwrites: When
antigravity_dream.pydetected that an infrastructure node upgraded from Fedora 43 to Fedora 45 or changed a service port, replacing the old bullet kept active memory clean—but destroyed the historical timeline of when the previous state was valid (valid_from→superseded_at) and why it changed. - Linear Tool & Skill Context Scaling: With 41 custom CLI scripts in
~/scripts/and 38 globalSKILL.mdrunbooks across~/.agents/skills/and~/.claude/skills/, scanning or injecting static tool catalogs into prompts wastes tokens on irrelevant capabilities. - Multi-Agent Swarm Contention & Context Exhaustion: When spawning parallel subagents (
research,verifier,author), having workers append to shared state files or return verbose execution traces directly to the parent coordinator causes lock contention and rapidly exhausts the coordinator’s context window.
This post walks through Phase B of our Cognitive Architecture Roadmap: transforming a flat Markdown memory store into a self-compacting, bi-temporal, dynamically routed, and swarm-isolated cognitive engine—gated at every step by antigravity_eval.py.
Overview of the Phase B Architecture
| Phase | Capability | Core Script / Vault Target | Primary Engineering Benefit |
|---|---|---|---|
| Phase B3 | Closed-Loop Prompt & Lesson Compaction | antigravity_compact.py + Meta/lessons.md |
Clusters and merges overlapping procedural rules while enforcing 100.0% recall on antigravity_eval.py (33 → 25 rules, +2,432 chars saved). |
| Phase B1 | Bi-Temporal Memory & State Lineage | antigravity_dream.py + Meta/memory-lineage.jsonl + Meta/Memory Lineage.base |
Preserves structured state transitions (valid_from → superseded_at, previous_fact → new_fact) without polluting active memory. |
| Phase B2 | Sub-10ms Semantic Skill & Script Router | antigravity_router.py + Meta/states/capability-index.json |
Indexes 79 scripts and skills into a cached JSON index and injects the top-3 relevant CLI invocations into antigravity_context.py in ~2.2 ms. |
| Phase B4 | Lock-Free Swarm Blackboard Protocol | antigravity_blackboard.py + Meta/states/blackboard/<task-id>/ |
Gives each parallel worker an isolated JSON scratchpad and reduces all worker outputs into a single <500-token coordinator synthesis. |
1. Phase B3: Closed-Loop Prompt & Lesson Compaction (antigravity_compact.py)
The Problem: Cross-Bundle Semantic Duplication
When an agent learns both from local post-mortems and from periodic federated Teacher curriculum bundles, the same engineering invariant often arrives in multiple surface forms:
- **Use pipefail so pipelines do not mask failures**: Run scripts with set -o pipefail, or capture command output into a variable and check it... [teacher:3de51273]
- **Pipes hide failures**: Use `set -o pipefail` or capture output into a variable and check it. `cmd | next` hides cmd's failure... [teacher:777ec864]
- **Never judge success from a pipeline's exit code**: Never judge success from a pipeline's exit code. 'cmd | next' reports next's status... [teacher:b0c253eb]
Naive string deduplication misses these because their phrasing differs. Worse, naive LLM summarization risks dropping critical backticked technical anchors (KotlinCompileDaemon, set -o pipefail, ^- ⚠️) that our dynamic evaluation probes in antigravity_eval.py assert against.
The Solution: Anchor-Preserving Clustering + Transactional Eval Gate
We built ~/scripts/antigravity_compact.py to perform deterministic, anchor-preserving rule compaction inside a closed verification loop:
- Markdown Normalization: Repairs malformed bullets (such as
- **Title: Full body bolded**) into canonical- **Title**: Bodyform. - Hybrid Lexical & Domain-Signature Clustering: Groups rules within each section using a combination of
difflib.SequenceMatcher, content-word Jaccard similarity, and high-signal technical domain signatures (pipefail,KotlinCompileDaemon,Gradle + tail,Kubernetes args + ENTRYPOINT,symlink + directory,Deployment + Pending). - Canonical Rule Synthesis:
- Selects the candidate with the richest backticked technical spans (
extract_backticks(body)). - Verifies that every backticked identifier from all other rules in the cluster appears in the merged body (appending any missing backticked token so zero technical anchors are lost).
- Merges all provenance tags into a single composite lineage tag:
[teacher:3de51273,777ec864,b0c253eb]. - Transactional Evaluation Gate (
antigravity_eval.py): - Acquires the exclusive memory lock (
/tmp/antigravity-memory.lockviafcntl.flock). - Writes the compacted
Meta/lessons.mdand immediately runsantigravity_eval.run_evaluation_suite(mode="retrieval"). - Commits only if recall remains at
100.0%across all static and dynamically synthesized lesson probes; if even a single probe fails, it automatically restores the pre-compaction snapshot.
def merge_rule_cluster(cluster: list[dict]) -> dict:
"""
Synthesize a single canonical rule from a cluster of overlapping rules.
Prefers the rule with backtick formatting and richest technical detail, while
preserving all unique provenance tags and backticked technical anchors.
"""
if len(cluster) == 1:
r = cluster[0]
return {
"title": r["title"],
"body": r["body"],
"teachers": r["teachers"],
"merged_count": 1,
"merged_titles": [r["title"]],
}
def candidate_score(r: dict) -> tuple[int, int]:
bt_count = len(extract_backticks(r["body"]))
return (bt_count, len(r["body"]))
best = max(cluster, key=candidate_score)
canonical_title = best["title"]
canonical_body = best["body"]
all_teachers = []
for r in cluster:
for tid in r["teachers"]:
if tid not in all_teachers:
all_teachers.append(tid)
# Guarantee zero loss of backticked technical identifiers across cluster members
canonical_lower = canonical_body.lower()
missing_bts = []
for r in cluster:
for bt in extract_backticks(r["body"]):
if bt.lower() not in canonical_lower and bt not in missing_bts:
missing_bts.append(bt)
if missing_bts:
canonical_body = canonical_body.rstrip(".") + f" (e.g. {', '.join(f'`{b}`' for b in missing_bts)})."
return {
"title": canonical_title,
"body": canonical_body,
"teachers": all_teachers,
"merged_count": len(cluster),
"merged_titles": [r["title"] for r in cluster],
}
Live Compaction Results
Running python3 ~/scripts/antigravity_compact.py compacted Meta/lessons.md from 33 rules down to 25 canonical rules, merging all 7 overlapping Teacher rule clusters and saving 2,432 characters (~600 tokens) on every full-ledger read while passing 33/33 evaluation probes (100.0%):
=== Antigravity Prompt & Lesson Compaction [COMMITTED] ===
Lessons (`Meta/lessons.md`): 33 -> 25 rules (8 redundant rules merged, 8 malformed bullets normalized, +2432 chars saved)
* Merged 3 rules -> 'Pipes hide failures' [teacher:3de51273,777ec864,b0c253eb]
- Use pipefail so pipelines do not mask failures
- Pipes hide failures
- Never judge success from a pipeline's exit code
* Merged 2 rules -> 'Do not pipe long Gradle runs into tail' [teacher:3de51273,777ec864]
* Merged 2 rules -> 'Stale Kotlin compile daemon' [teacher:3de51273,777ec864]
* Merged 2 rules -> 'Image CMD vs Kubernetes args' [teacher:3de51273,777ec864]
* Merged 2 rules -> 'Symlink data dirs before first tool run' [teacher:3de51273,777ec864]
* Merged 2 rules -> 'Scale to 0 before lowering requests on a full node' [teacher:3de51273,777ec864]
* Merged 2 rules -> 'Anchor status-marker checks' [teacher:3de51273,777ec864]
Closed-Loop Eval Gate (`antigravity_eval.py`): 33/33 passed (100.0% — 11 static + 22 dynamic probes)
2. Phase B1: Bi-Temporal Memory & State Lineage (Meta/memory-lineage.jsonl)
The Problem: Present-State Bias vs. Historical Auditability
In an evolving system environment, facts have a lifespan:
* An infrastructure server runs Fedora 43 from August until November, then upgrades to Fedora 45.
* An NFS export table initially authorizes two local subnet nodes, and later authorizes a Tailscale mesh endpoint.
* A CLI wrapper unconditionally passes --effort low, and later gains an automatic capability fallback when the active model changes.
If you keep both old and new facts in Meta/antigravity-memory.md, the agent suffers from proactive interference (citing stale OS versions or obsolete flags). Conversely, if you simply overwrite the old bullet in place, you lose the temporal lineage of when the previous configuration was valid and why it was superseded.
The Solution: Active Present Store + Append-Only Bi-Temporal Ledger
Inspired by bi-temporal database design, we separated current valid state from historical state lineage in ~/scripts/antigravity_dream.py:
- In-Place Active Replacement with Lineage Pointers: When
apply_contradictions_with_lineage()resolves a contradiction inMeta/antigravity-memory.md, it extracts thevalid_fromdate from the superseded bullet, replaces the bullet in place with the newly valid fact, and appends a compact 8-character SHA-256 lineage tag ([lineage:a1b2c3d4]). Active memory stays 100% free of stale facts. - Append-Only JSONL Ledger (
Meta/memory-lineage.jsonl): Every supersession appends an immutable JSON record capturing the full state transition:
{
"fact_id": "9f8ef2f2",
"entity": "lessons.md",
"previous_fact": "Meta/lessons.md contained 33 uncompacted rules with duplicate Teacher provenance entries.",
"new_fact": "Meta/lessons.md compacted to 25 canonical rules with merged provenance tags ([teacher:id1,id2]) and 100% eval recall via antigravity_compact.py.",
"valid_from": "2026-11-11",
"superseded_at": "2026-11-16T15:33:30+02:00",
"reason": "Closed-loop Phase B3 lesson compaction merged 7 overlapping rule clusters (+2,432 chars saved).",
"source_session": "5b0e0e71"
}
- Native Obsidian Lineage Dashboard (
Meta/Memory Lineage.md&Meta/Memory Lineage.base): Using the official Obsidian Skills conventions,render_lineage_views()automatically generates a human-readable Markdown transition table, callout cards for each transition, and a native.basedatabase view insideMeta/.
3. Phase B2: Sub-10ms Semantic Skill & Script Router (antigravity_router.py)
The Problem: Linear Growth of the Tool Catalog
An autonomous agent operating across cloud infrastructure, smart-home telemetry, storage arrays, media archives, and Obsidian vaults accumulates dozens of specialized levers:
* 41 custom CLI scripts in ~/scripts/*.{py,sh}
* 38 global SKILL.md runbooks in ~/.agents/skills/ and ~/.claude/skills/
Stuffing all 79 tool descriptions and CLI usage signatures into every prompt burns thousands of tokens per turn. Yet relying on the agent to run ls ~/scripts/ or grep through skill folders adds extra tool-call round trips.
The Solution: Cached Capability Indexing + ~2.2 ms Hybrid Routing
We implemented ~/scripts/antigravity_router.py as a two-tier lazy tool loader:
- mtime-Invalidated Capability Index (
Meta/states/capability-index.json): - Parses YAML frontmatter (
name,description) from everySKILL.mdand extracts module docstrings, header comments, andUsage:lines from every.pyand.shscript in~/scripts/. - Compiles a 19 KB JSON index cached in
Meta/states/capability-index.json. On subsequent calls,compute_sources_mtime()checks directory modification timestamps in<0.2 msand skips re-parsing unless a script or skill was added or edited. - Sub-10ms Hybrid Scorer:
- Scores the incoming user prompt against all 79 capabilities using:
- Exact script/skill stem matching (
+14.0) - Tokenized keyword overlap (
+4.5per token) - Domain-synonym expansion (
SYNONYM_MAP, e.g., mapping"blinds"→cover,somfy,control_ha_cover, or"tape"→backup_to_tape,lto,ultrium) - Character 3-gram similarity (
char_trigrams()) to handle morphological variations.
- Exact script/skill stem matching (
- Returns the top $K=3$ capabilities with their exact invocation command in ~2.2 ms.
$ python3 ~/scripts/antigravity_router.py "mount nfs share over tailscale and check ups battery"
=== Antigravity Capability Router [2.19 ms | 41 scripts + 38 skills indexed] ===
Query: mount nfs share over tailscale and check ups battery
1. [39.8] 🔧 Script: mount_nas1_nfs.sh — Authorize, mount, unmount, or check status of NFS share over Tailscale.
Invoke: `bash ~/scripts/mount_nas1_nfs.sh [mount|unmount|status|authorize]`
2. [14.79] 🔧 Script: mount_nas3_smb.sh — Idempotently mount, unmount, or check status of Samba shares.
Invoke: `bash ~/scripts/mount_nas3_smb.sh [mount|unmount|status]`
3. [11.19] 🔧 Script: get_ups_status.sh — Query UPS telemetry and battery load status.
Invoke: `bash ~/scripts/get_ups_status.sh`
[TIP] Implementation Gotcha: Polyglot Header Parsing Across Tooling
During integration testing across 10 diverse natural-language queries, we identified a subtle indexing blind spot: the script parser originally extracted docstrings using"""blocks for Python, while scanning#comments only for Bash scripts. Python scripts documented with top-level#comments (such aswp_stats.py) ended up with sparse generic tokens, allowing unrelated tools to rank higher.Unifying the parser to scan both docstrings and initial
#comment blocks across all scripts immediately resolved the token deficit, elevating router precision to a perfect 10/10 exact top-target match across all test queries at ~3 ms latency.
- Direct Integration into Cold-Start Context (
antigravity_context.py):
Whenantigravity_context.pybuilds the<800-tokencold-start context packet for a new session, it invokesantigravity_router.route_query(query, top_k=3)and prepends a**Recommended Skills & Scripts**section containing only the 2–3 exact CLI levers needed for that specific prompt.
4. Phase B4: Lock-Free Multi-Agent Swarm Blackboard (antigravity_blackboard.py)
The Problem: Shared-File Contention and Coordinator Context Flood
When decomposing a complex engineering task across multiple subagents (research, verifier, author), two anti-patterns frequently break multi-agent workflows:
* Shared Mutable State Contention: Multiple workers attempting to read-modify-write the same Markdown note or JSON state file race against each other or block on locks.
* Coordinator Context Exhaustion: If three subagents each return 4,000 tokens of raw grep outputs, stack traces, and intermediate logs to the parent coordinator, the coordinator’s context window fills up with transient noise.
The Solution: “Separate Before Serializing” + Bounded Coordinator Reduction
Following the systems principle Separate Before Serializing, ~/scripts/antigravity_blackboard.py gives every multi-agent task an isolated directory under Meta/states/blackboard/<task-id>/:
- Lock-Free Per-Worker Scratchpads (
worker-<name>.json):
Each worker writes exclusively to its own owned file (worker-research.json,worker-verifier.json,worker-author.json) via atomic temp-file replacement (os.replace). Because no two workers ever touch the same file, zero locking is required during parallel execution. - Bounded
<500-TokenCoordinator Reducer (reduce <task-id>):
Once workers finish, the coordinator runsantigravity_blackboard.py reduce <task-id>. The reducer inspectsmanifest.jsonand allworker-*.jsonfiles, verifies that all expected workers succeeded, deduplicates findings and artifact paths across workers, writesSUMMARY.md, and emits a single distilled synthesis capped at<500 tokens:
### 🐝 Swarm Blackboard Synthesis: `Phase B Architecture Verification` (`phase-b-verification`)
- **Status**: `COMPLETED` (`3/3` workers done)
**Worker Outcomes**:
- ✅ **`author`** (`done`): Implemented antigravity_compact.py, antigravity_router.py, antigravity_blackboard.py, and bi-temporal lineage.
- ✅ **`research`** (`done`): Audited 33 rules in Meta/lessons.md and identified 7 overlapping teacher clusters.
- ✅ **`verifier`** (`done`): Verified 33/33 evaluation probes (100.0% accuracy) and 2.1ms capability routing latency.
**Distilled Findings**:
- [research] 8 redundant teacher rules merged (+2,432 chars saved)
- [research] 5 historical lineage transitions recorded in memory-lineage.jsonl
- [verifier] Eval gate passed 33/33 (11 static + 22 dynamic probes)
- [verifier] Capability router indexed 41 scripts and 38 skills in Meta/states/capability-index.json
- Nightly Dream Harvesting & 7-Day Auto-Pruning:
During nightly out-of-band execution,antigravity_dream.pycallsantigravity_blackboard.harvest_completed_for_dream()to ingest completed swarm outcomes into long-term memory and automatically prunes blackboard task directories older than 7 days.
Summary: The Complete Cognitive Engineering Stack
With Phase B complete, every layer of the local agent’s memory and context lifecycle operates under explicit bounds and automated verification:
| Capability | Module & Levers | Verified Operational Metric |
|---|---|---|
| Cold-Start Context & Routing | antigravity_context.py + antigravity_router.py |
10/10 exact tool matches in ~2.2–3.8 ms; prepended in <800 tokens. |
| Parallel Execution & Swarms | antigravity_blackboard.py (init, post, reduce) |
Lock-free per-worker scratchpads; reduced to <140–450 tokens. |
| Nightly Consolidation & Lineage | antigravity_dream.py + memory-lineage.jsonl |
Zero stale active facts; full bi-temporal audit trail (valid_from → superseded_at). |
| Self-Compaction & Regression Gate | antigravity_compact.py + antigravity_eval.py |
33 → 25 rules (+2,432 chars saved); 33/33 eval probes passed (100.0%). |
- Cold-Start Context (
antigravity_context.py+antigravity_router.py): Injects top-$K$ scripts/skills (~2.2 ms), matching negative constraints, and active memory facts in<800 tokens. - Parallel Execution (
antigravity_blackboard.py): Isolates subagent scratchpads per worker and reduces multi-agent outputs to<500 tokens. - Nightly Consolidation (
antigravity_dream.py+Meta/memory-lineage.jsonl): Distills durable facts, records bi-temporal state lineage (valid_from→superseded_at), and harvests completed swarm blackboards. - Self-Compaction & Regression Gate (
antigravity_compact.py+antigravity_eval.py): Compacts overlapping lessons (33 → 25rules) while guaranteeing100.0%recall across all static and dynamic evaluation probes.