Closing the Cognitive Loop: Automated Context Pre-Injection, Self-Expanding Eval Gates, and Zero-Touch Knowledge Federation

By | November 16, 2026
Closing the Cognitive Loop: Automated Context Pre-Injection, Self-Expanding Eval Gates, and Zero-Touch Knowledge Federation

Closed-Loop Cognitive Architecture Pipeline

Over the course of our local AI agent engineering series—culminating in The Autonomous Homelab: Building a State-of-the-Art Local AI Agent Architecture, Federated AI Agent Memory: Teacher–Student Knowledge Transfer, and Zero-Trust Signal Gateways for Local AI Agents—we built every core primitive required for a production-grade cognitive operating system:

  1. Episodic & Semantic Vault Memory (obsidian_memory.py + Meta/antigravity-memory.md)
  2. Sub-Second Hybrid Retrieval & Context Scoring (search_vault.py + antigravity_context.py)
  3. Out-of-Band NREM/REM Memory Dreaming (antigravity_dream.py)
  4. Procedural Skill Compilation & Negative Constraint Ledgers (SKILL.md + Meta/lessons.md)
  5. Ground-Truth Evaluation & Live System Verification (antigravity_eval.py + antigravity_verify.py)
  6. Cross-Machine Teacher–Student Distillation (teacher_export.py + student_assimilate.py)

Yet when we audited our live runtime against the latest 2026 Context Engineering standards, we uncovered a classic systems engineering anti-pattern: the Last-Mile Wiring Trap.

Even when you build best-in-class scripts for dynamic context assembly, memory evaluation, and cross-machine export, a tool that relies on a human or an LLM remembering to invoke it manually is an open loop. Specifically, four critical connections were still manual:
* antigravity_context.py synthesized high-density, sub-800-token context packets, but our mobile edge gateways (signal_bot.py and meshtastic_bot.py) weren’t automatically prepending them on cold-start conversations.
* antigravity_eval.py verified memory recall, but relied on a static list of 11 hardcoded probes—meaning newly distilled rules in Meta/lessons.md were never automatically regression-tested.
* antigravity_dream.py consolidated transcripts nightly with SHA-256 Optimistic Concurrency Control (OCC), but didn’t run antigravity_eval.py as a transactional post-commit gate to auto-rollback if a dream cycle accidentally pruned a ground-truth fact.
* antigravity-dream.service automatically assimilated incoming student bundles (ExecStartPre), but lacked the matching ExecStartPost hook to broadcast newly dreamed teacher knowledge immediately after consolidation.

In this post, we walk through the exact architectural changes and Python implementations that close all four loops—turning standalone cognitive tools into a self-verifying, zero-touch feedback system.


1. Cold-Start Context Pre-Injection (Preserving Warm KV-Caches)

The Context Engineering Dilemma

A naive RAG implementation prepends retrieved chunks to every single user message in a multi-turn conversation. In 2026 agent architectures, doing that destroys Prompt Caching (KV-cache reuse) and bloats the rolling conversation history with redundant metadata on every follow-up turn.

Conversely, if you inject nothing at the start of a headless mobile session (via Signal or Meshtastic), the agent starts cold—forcing it to burn an extra tool-call turn (5–10 seconds of latency) just to read Meta/states/antigravity.md or search Meta/lessons.md.

The Solution: Cold-Start-Only Pre-Injection

In signal_bot.py, we already track a 2-hour sliding conversation window via active_cid = get_active_conversation_id().
* When active_cid is present (a warm follow-up turn using --conversation <cid>), the conversation history and KV-cache already hold the initial context. We send the user’s message directly without modification.
* When active_cid is None (a brand-new topic or after a new / reset command), signal_bot.py automatically invokes antigravity_context.assemble_context(message_text, limit=3) in-process (~80ms) and prepends the resulting sub-800-token Markdown packet to the initial prompt:

# Inside signal_bot.py -> process_message()
with AGY_EXEC_LOCK:
    active_cid = get_active_conversation_id()
    context_block = ""
    if not active_cid:
        try:
            import antigravity_context
            ctx_packet = antigravity_context.assemble_context(message_text, limit=3)
            ctx_md = (ctx_packet.get("markdown") or "").strip()
            if ctx_md:
                context_block = f"{ctx_md}\n\n"
        except Exception as ctx_err:
            print(f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] Context injection skipped: {ctx_err}", flush=True)

    prompt = (
        f"{context_block}{message_text}\n\n"
        "[Execution Context: Triggered headlessly via Signal Bot. stdin is closed (DEVNULL); "
        "never run interactive prompts (always pass -y/--assumeyes to dnf/apt and sudo -n when testing local sudo). "
        "Keep the final response concise and well-formatted for Signal.]"
    )
    reply, new_cid = execute_agy(prompt, active_cid=active_cid)

We also upgraded the keyword scorer inside antigravity_context.py (len(w) >= 3 and flexible bold-colon header matching) so three-letter infrastructure acronyms (ups, nut, nfs, smb, ssh, dns) immediately pull matching negative constraints from Meta/lessons.md:

$ python3 ~/scripts/antigravity_context.py "Check NUT UPS status on storage node"
### Dynamic Context Injection: `Check NUT UPS status on storage node`
- **Active Focus**: Managing NFS & SMB mounts, fleet-wide NUT UPS monitoring, and Obsidian vault curation

**Persistent Memory Facts**:
- *2026-11-15*: NUT UPS configuration: requires MODE=netserver in /etc/ups/nut.conf and nut.target + nut-driver-enumerator.service enabled so nut-server starts automatically after kernel reboots.

**Applicable Negative Constraints & Lessons**:
- **NUT Service Boot Persistence on Fedora: Enabling `nut-server.service` alone does not start NUT after a reboot because its `[Install]` section is `WantedBy=nut.target` (not `multi-user.target`) and its `ExecCondition` checks `/etc/ups/nut.conf`; always set `MODE=netserver`...

Before the agent even executes its first tool call, it already has the exact architectural facts and negative constraints for that task in its initial attention window.


2. Self-Expanding Evaluation Suites (build_dynamic_lesson_probes)

The Problem with Static Benchmarks

In Hardening Local AI Agents: Procedural Skills, Mistake Ledgers, and Real-Time Verification, we introduced antigravity_eval.py with 11 curated ground-truth probes testing infrastructure facts, personal preferences, and negative constraints.

However, as our nightly dreaming engine (antigravity_dream.py) and Teacher–Student federation pipeline (student_assimilate.py) continued running, Meta/lessons.md grew from 6 initial rules to 34 production engineering rules (covering everything from set -o pipefail and Gradle/Kotlin compile daemons to DNF5 flag ordering and Kubernetes ENTRYPOINT vs args semantics). Meanwhile, PROBES in antigravity_eval.py remained stuck at 11 static checks.

Dynamic Probe Synthesis from Meta/lessons.md

To ensure that every lesson ever learned becomes a permanent regression test, we added build_dynamic_lesson_probes() to antigravity_eval.py. Every time the evaluation suite runs, it parses Meta/lessons.md, extracts the rule title and high-signal technical identifiers (backticked flags, commands, or domain tokens), and dynamically generates an assertion probe for every rule not already covered by the static battery:

def build_dynamic_lesson_probes() -> list[dict]:
    """Dynamically synthesize evaluation probes for every rule in Meta/lessons.md not covered by static PROBES."""
    if not LESSONS_FILE.exists():
        return []
    text = LESSONS_FILE.read_text(encoding="utf-8")
    static_queries_lower = " ".join(
        p["query"].lower() + " " + " ".join(p["expected_all"]).lower()
        for p in PROBES if p["target_file"] == "lessons.md"
    )
    dynamic_probes = []
    for line in text.splitlines():
        clean = line.strip()
        m = re.match(r"^-\s+\*\*([^*:]+?)(?::\*\*|\*\*:)\s*(.+)$", clean)
        if not m:
            m2 = re.match(r"^-\s+\*\*([^*:]+?):\s*(.+?)\*\*$", clean)
            if not m2:
                continue
            title, body = m2.group(1).strip(), m2.group(2).strip()
        else:
            title, body = m.group(1).strip(), m.group(2).strip()

        body_clean = re.sub(r"\[teacher:[a-f0-9]+\]", "", body).strip()
        slug = re.sub(r"[^a-z0-9]+", "-", title.lower()).strip("-")[:32]
        title_words = [w for w in re.findall(r"[A-Za-z0-9_-]{4,}", title.lower()) if w not in STOPWORDS]
        if title_words and all(w in static_queries_lower for w in title_words[:2]):
            continue

        backticks = [b.strip() for b in re.findall(r"`([^`]{2,28})`", body_clean) if len(b.strip()) >= 2]
        words = [
            w for w in re.findall(r"[A-Za-z0-9_.-]{5,}", body_clean)
            if w.lower() not in STOPWORDS and w in body_clean
        ]
        expected = [title]
        if backticks:
            expected.append(backticks[0])
        elif words:
            expected.append(words[0])

        dynamic_probes.append({
            "id": f"dyn-lesson-{slug}",
            "category": "lessons-dynamic",
            "query": f"What is the negative constraint or rule for {title}?",
            "expected_all": expected,
            "prohibited": [],
            "target_file": "lessons.md",
        })
    return dynamic_probes

Running antigravity_eval.py now automatically scales from 11 static probes to 41 total probes (11 static Omnisearch probes + 30 sub-millisecond dynamic lesson probes) with zero manual test maintenance:

--------------------------------------------------------------------------------
SCORECARD: 41/41 PASSED (100.0%)
CATEGORY BREAKDOWN:
  - infrastructure  : 4/4 (100.0%)
  - lessons         : 4/4 (100.0%)
  - engineering     : 1/1 (100.0%)
  - personal        : 2/2 (100.0%)
  - lessons-dynamic : 30/30 (100.0%)
--------------------------------------------------------------------------------

Whenever a future dream cycle or Teacher curriculum bundle appends a 35th rule to Meta/lessons.md, the test suite automatically expands to 42 probes on its very next execution.


3. Transactional Post-Dream Regression Gate (antigravity_dream.py)

Guarding Against Destructive Consolidation

Out-of-band dreaming (antigravity_dream.py) gives the agent permission to deduplicate, compact, and rewrite Meta/antigravity-memory.md, Meta/states/antigravity.md, and Meta/lessons.md. While we already protected against concurrent file clobbering using fcntl.flock and SHA-256 Optimistic Concurrency Control (atomic_write_with_occ), what happens if an LLM consolidation pass hallucinates a truncation or accidentally drops a critical fact?

Without a post-commit verification gate, the dream daemon would write the corrupted file, advance the session step_count watermarks in dream-state.json, and permanently move on.

Atomic Snapshot Rollback on Eval Failure

We wired antigravity_eval.run_evaluation_suite() directly into antigravity_dream.py immediately after the OCC commit and before advancing the session step_count watermarks:

    # Commit with SHA-256 Optimistic Concurrency Control
    _, conflict_mem = atomic_write_with_occ(MEMORY_FILE, deduped_memory, mem_sha, fallback_lines=fallback_lines)
    _, conflict_state = atomic_write_with_occ(STATE_FILE, pruned_state_text, state_sha)
    conflict_lessons = False
    if new_lessons and LESSONS_FILE.exists():
        _, conflict_lessons = atomic_write_with_occ(LESSONS_FILE, updated_lessons_text, lessons_sha, fallback_lines=lessons_fallback_lines)
    conflict_any = conflict_mem or conflict_state or conflict_lessons

    # Post-Dream Evaluation Gate (antigravity_eval.py): verify 100% ground-truth & dynamic lesson recall
    eval_results = None
    try:
        import antigravity_eval
        eval_results = antigravity_eval.run_evaluation_suite(mode="retrieval")
        esm = eval_results.get("summary", {})
        if esm.get("failed", 0) > 0:
            # Regression detected! Automatically restore pre-dream memory & lessons snapshots and abort watermark advance
            print(
                f"Dream cycle ABORTED: Post-dream evaluation gate failed ({esm.get('passed')}/{esm.get('total')}). "
                "Restoring pre-dream memory & lessons snapshots.",
                file=sys.stderr,
            )
            MEMORY_FILE.write_text(memory_text, encoding="utf-8")
            STATE_FILE.write_text(state_text, encoding="utf-8")
            if new_lessons and LESSONS_FILE.exists():
                LESSONS_FILE.write_text(lessons_text, encoding="utf-8")
            return 1
        antigravity_eval.save_results(eval_results)
    except Exception as e:
        print(f"Post-dream evaluation gate warning: {e}", file=sys.stderr)

This turns nightly memory consolidation into an ACID-like cognitive transaction:
1. Clone $MEM (Meta/) to an isolated staging directory and record pre-dream SHA-256 hashes.
2. Replay unconsolidated transcripts, distill new facts/lessons/skills, and compact redundancies.
3. Commit via SHA-256 OCC.
4. Run the 41-probe evaluation gate:
* If failed == 0 (100.0%), persist the evaluation scorecard to Meta/evals/latest.json, advance the step_count watermarks, and log the scorecard in Meta/dream-journal.md.
* If any static or dynamic probe fails, immediately restore the pre-dream snapshots (memory_text, state_text, lessons_text) and exit with code 1 without advancing watermarks.


4. Zero-Touch Federation Pipeline (ExecStartPre -> Dream -> ExecStartPost)

In Federated AI Agent Memory, we built teacher_export.py (which harvests 9 memory sources, scrubs PII/secrets/private IPs, generalizes project-specific findings into portable rules, and publishes teacher-curriculum-v1 bundles) and student_assimilate.py (which fetches, deduplicates, quarantines skills, and merges bundles).

By installing the systemd --user drop-in hook onto antigravity-dream.service:

$ python3 ~/scripts/teacher_export.py --install-systemd --dream-unit antigravity-dream.service

Our nightly 02:30:00 timer (antigravity-dream.timer) now executes a three-stage, bidirectional synchronization pipeline without a single manual command:

# ~/.config/systemd/user/antigravity-dream.service
[Unit]
Description=Antigravity Out-of-Band Memory Dreaming & Consolidation Service
After=default.target

[Service]
Type=oneshot
ExecStart=/usr/bin/python3 %h/scripts/antigravity_dream.py --engine auto --limit 8

# ~/.config/systemd/user/antigravity-dream.service.d/student-assimilate.conf
[Service]
ExecStartPre=-/usr/bin/python3 %h/scripts/student_assimilate.py --quiet

# ~/.config/systemd/user/antigravity-dream.service.d/teacher-export.conf
[Service]
ExecStartPost=-/usr/bin/python3 %h/scripts/teacher_export.py --quiet

Every night at 02:30 AM:
1. ExecStartPre (student_assimilate.py): Pulls any pending curriculum bundles dropped by peer workstations (via SSH/SCP relay, GitLab Snippet/Repo, or Signal), deduplicates them, and merges new lessons into Meta/lessons.md.
2. ExecStart (antigravity_dream.py): Replays the day’s local agent sessions, distills new semantic facts and procedural skills, probes live infrastructure (antigravity_verify.py), and enforces the 41-probe antigravity_eval.py regression gate.
3. ExecStartPost (teacher_export.py): Computes the SHA-256 delta of any net-new local learnings distilled during the dream cycle, sanitizes and generalizes them, and broadcasts an updated teacher-curriculum-v1 bundle out to all configured transports.


Summary: Open Loop vs. Closed Cognitive Loop

Cognitive Subsystem Before (Open-Loop Primitives) After (Closed-Loop Architecture)
Pre-Turn Context (antigravity_context.py) Manual CLI invocation only Automatically prepended on cold-start turns (active_cid is None) in signal_bot.py & meshtastic_bot.py; bypassed on warm turns to preserve KV-cache
Negative Constraint Testing (antigravity_eval.py) 11 static hardcoded probes Self-expanding: 11 static probes + 30+ dynamic probes synthesized automatically from every rule in Meta/lessons.md
Dream Consolidation Safety (antigravity_dream.py) SHA-256 OCC file locking only Transactional Eval Gate: runs full static + dynamic evaluation suite post-commit and auto-rollbacks snapshots if recall drops below 100%
Federation Sync (teacher_export.py) Pre-dream assimilation only (ExecStartPre) Full bidirectional systemd pipeline (ExecStartPre assimilate -> ExecStart dream + eval gate -> ExecStartPost sanitized export)

When designing AI agent architectures, the magic rarely lives in a single prompt or a larger vector database. It lives in closing the feedback loops—ensuring that every mistake automatically becomes a lesson, every lesson automatically becomes a regression test, every night’s consolidation is gated by that test suite, and every verified insight is automatically injected into the next cold-start turn.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.