# Show HN: AI Agent Infrastructure and Memory Compaction Specifications

> Source: <https://github.com/grfdsasdvef/awesome-ai-agent-infrastructure>
> Published: 2026-11-24 08:16:42+00:00

**Recommendation:** Use a three-tier rolling compactor — pinned invariants, a deduplicated ledger, and a raw tail of the last 4-6 turns — and guard compaction with asyncio.Lock to stop parallel tool loops from double-compacting.
More in the playbook "Designing agent systems": <https://wpnews.pro/playbooks/agent-architecture>

### Curated Architecture Specifications, Memory Engines, Schema-Constrained Decoding, SWE-bench Evals & Production Tooling

⚡ **Snowden SAY Deep Systems Engineering Lab:**
Ежедневные глубокие разборы аварий на проде, деградация внимания LLM (Lost in the Middle), rolling memory compactor и low-level агентные рантаймы — в нашем Telegram-канале: **[@snowdensay](https://t.me/snowdensay)**.

📖 **Полная монография «Память и рантайм AI-агентов» (код + тесты):** доступна в Telegram-боте [@Snowden_book_bot](https://t.me/Snowden_book_bot?start=production-ai-agent-infrastructure)

Modern LLMs with 128k–1M token context windows create a dangerous illusion of infinite memory. In long autonomous agent runs (40+ tool turns), attention weights irreversibly dilute across the KV cache (**Lost in the Middle**).

1. **Never summarize failed tool calls:** Raw error tracebacks must remain verbatim until fixed, or the agent cycles infinitely.
2. **Grammar-constrained decoding over prompt validation:** Enforce JSON Schemas via CFG logits masks prior to softmax (99.97% validity vs 92% with prompt prompting).
3. **Lock your compaction routines:** Parallel tool invocations in async loops trigger race conditions and double-compaction without`asyncio.Lock` .

*Three-tier rolling compaction architecture:*

```
[ LAYER 1: Pinned Invariants ] ─── System contract, safety boundaries, tools (Raw, 0% drop)
           │
[ LAYER 2: Compacted Ledger ]  ─── Deduplicated facts of completed successful tool cycles
           │
[ LAYER 3: Hot Raw Tail ]      ─── Last 4-6 turns (Fresh local state + unresolved stacktraces)
```

- **Reference Implementations:**  - `RollingMemoryCompactor` (Python/Go): Automatic deduplication of idempotent file reads (`head` ,`cat` ) into SHA256 pointers.
  - `Episodic KV Cache Offloading` : Pruning intermediate layer KV tokens for background context.

- **[xgrammar](https://github.com/mlc-ai/xgrammar)** : Fast GPU-accelerated grammar-guided decoding for vLLM and SGLang.
- **[outlines](https://github.com/dottxt-ai/outlines)** : Structured text generation via finite-state machine (FSM) index over model vocabulary.
- **[guidance](https://github.com/guidance-ai/guidance)** : Interleaved generation and control logic with token-level regex constraints.

``` python
from vllm import LLM, SamplingParams
from vllm.sampling_params import GuidedDecodingParams

schema = {
    "type": "object",
    "properties": {
        "tool": {"enum": ["patch_ast", "execute_test", "commit_fix"]},
        "file_path": {"type": "string", "pattern": r"^[\w\-\./]+\.[a-z]+$"},
        "patch_content": {"type": "string"}
    },
    "required": ["tool", "file_path", "patch_content"],
    "additionalProperties": False
}

params = SamplingParams(
    temperature=0.1,
    max_tokens=1024,
    guided_decoding=GuidedDecodingParams(json=schema)
)
```

- **[Tree-sitter](https://tree-sitter.github.io/tree-sitter/)** : Incremental concrete syntax tree parsing for AST-aware diffing.
- **[SWE-bench](https://www.swebench.com/)** : Benchmark for evaluating LLMs on resolving real-world GitHub issues.
- **Syntactic Rollback Guards** : Reverting invalid patches at parse-time before invoking compilers.

- **Prompt Injection Defense in Tools** : Strict sanitization of dynamic tool definitions from untrusted MCP servers.
- **Zombie Process Reaper** : Automatic cleanup of stdio subprocesses exceeding idle TTL.
- **Concurrency Rate Limiting** : Non-blocking`select` token buckets to prevent LLM retry stampedes.

- **[gVisor](https://gvisor.dev/)** : Application kernel for container security, intercepting Linux syscalls in user space.
- **[Firecracker](https://firecracker-microvm.github.io/)** : Ephemeral microVMs booting in < 5 ms for untrusted Python code execution.
- **[nsjail](https://github.com/google/nsjail)** : Lightweight Linux process isolation tool using namespaces and seccomp-bpf.

Tested on SWE-bench Verified subset (17,000 tool iterations/day, Llama-3.3-70B on 4×A100):

| Metric | Naive Context Buffer | Linear Summarization | Rolling Compaction (Ours) | 
|---|---|---|---|
| **System Rules Recall** | 68.2% | 74.5% | **97.8%** | 
| **Token Cost Per Run** | 1.84M | 1.12M | **0.62M (-66%)** | 
| **Infinite Loop Failures** | 14.3% | 9.8% | **0.2%** | 
| **p99 Execution Latency** | 4.8s | 8.2s | **1.9s** | 

Discussions and architecture blueprints are maintained by **Snowden SAY**.

- 📡 **Telegram Engineering Channel:**[@snowdensay](https://t.me/snowdensay)
- 📖 **Monograph Repository Bot:**[@Snowden_book_bot](https://t.me/Snowden_book_bot)
