"A large language model is a probabilistic sampler. Side effects require a transaction manager; a sampler is not one. If you rely on prompts to police disk mutation—or opaque ORMs to guess query plans when an agent fires hybrid vector-graph joins on every turn—you have built an engine on quicksand. Real guardrails live in relational schemas, explicit indexes, sandbox verifications, and atomic apply journals."
0 JS ORMs
48 Repos · 234 Explicit Indexes
15.25 ms
HNSW+GIN CTE (vs 252ms Scan)
2.64x
Polyglot 2PC Lift (19.6% → 51.6%)
3.51 µs
p50 CPU Stage-0 Pre-Route*

*Measured across bare-metal x86_64 PostgreSQL 16 (54,456 AST symbols) and isolated 96 GB NVIDIA RTX PRO 6000 Blackwell clean-room benchmarks (73/73 SHA-256 gates).

⚠️ 1. The Core Tension: Prompts as Policy vs. Real Working Trees

Between 2024 and 2026, the software industry saw a massive wave of agent frameworks attempting to automate software engineering. While these frameworks demonstrated compelling chat demos, many engineering teams found them brittle when pointed at complex, multi-file production repositories.

The root problem was not that modern frontier models lack intelligence. The tension is architectural:

Many advanced teams have mitigated this using git worktrees, patch sandboxes, and continuous integration. But these remain fragmented scripts wrapped around an untrusted agent. What happens when we elevate this principle into a formal relational state plane and transaction manager?

🏛️ 2. The Krusch Architecture: Five Sovereign Layers

Bundled into a single zero-ORM distribution via krusch-stack (39 MCP tools across 7 pillars, with Okapi BM25 --defer-tools cutting upfront schema payload by -68.5%), the ecosystem replaces conversational guardrails with hard relational invariants:

Stage 0
krusch-pre-router (Microsecond Syntactic Gate) CPU regex & LRU memoization engine. Intercepts structured code fences, SQL, LaTeX, and stack traces in <10µs with $0.00 cost, zero token burn, and honest misses to Stage 1.
Stage 1 & 2
krusch-cascade-router (Pareto Neural Cascade & Live SSE Logprob Abort) #1 on ACL 2026 LLMRouterBench (0.4269 Pareto, -83.95% cost across 12,566 prompts) & 0.7883 on RouterArena CI (PR #169 open). Streams initial fast-tier tokens, aborts low-confidence/looping streams mid-flight (-48.0% TTFT), and escalates to 120B or frontier reasoning.
Tier 1 & 2 Substrates
krusch-context-mcp v2.0.0, krusch-git v1.2.3 & krusch-fs-mcp (Memory, AST & Fleet State) Backed by PostgreSQL 16 (pgvector HNSW + weighted tsvector GIN + git_edges) and zero-Docker node:sqlite WAL + external-content Porter FTS5 with 8K snowflake-arctic-embed2 (1024d), temporal lineage (supersedes_id), and SHA-256 span-drift gates.
Tier 3 State Plane
krusch-coding-harness (2PC Relational FSM & Verify-on-Stop Loop) Authoritative PostgreSQL finite state machine. Enforces Bubblewrap-sandboxed test verification, KruschFailureClassifier attribution, single-writer file leases, and atomic fsync apply journals—lifting open-weights Aider Polyglot Pass@1 19.56% → Pass@2 51.56%.
Workbench
kd-Code & @pierre/diffs (Developer Control Plane & 2PC Human Gate) Visual review surface where developers inspect verified staged diffs, evaluate sandbox test outcomes, and grant explicit commit approvals.

3. Stage 0: krusch-pre-router — Eradicating the "Routing Tax"

One of the most wasteful practices in modern AI systems is spending a 500-millisecond, $0.02 LLM preflight call simply to classify whether a query is pure SQL, arithmetic, or a stack trace.

krusch-pre-router is an in-process, zero-dependency CPU heuristic gate that intercepts closed-world tasks before any model API is invoked:

import { createPreRouter } from 'krusch-pre-router';

const router = createPreRouter({ cache: { maxSize: 2000 } });

// Stage-0 Hot Path (<4µs CPU)
const route = router.classify(prompt);

if (route.isFastPath) {
  // Direct specialist dispatch ($0.00 routing overhead)
  return dispatchSpecialist(route.role, prompt);
}

// Clean delegation to L1/L2 Neural Cascade
return cascadeRouter.dispatch(prompt);

🔀 4. Stage 1 & 2: krusch-cascade-router — Speculative SSE Abort & Verified Pareto Routing

When a query requires semantic reasoning beyond syntactic heuristics, krusch-cascade-router combines 42.5 µs CPU structural complexity scoring with 1.27 ms snowflake-arctic-embed2 (1024d) kNN prototype routing and live SSE streaming logprob inspection (read the full G4 Cascade Router benchmark paper →).

Live SSE Logprob Early-Token Inspection

Sequential cascades (call Model A to completion, inspect output, call Model B) double tail latency. krusch-cascade-router aborts uncertain streams mid-flight:

  1. It streams initial tokens from a fast local specialist (gemma4:e4b or qwen2.5-coder:7b) and inspects cumulative token logprob confidence and repetition entropy.
  2. If confidence holds above threshold, the fast specialist completes at local speed with zero escalation tax.
  3. If confidence drops or loop entropy spikes, the HTTP stream is aborted immediately and escalated to gpt-oss:120b (96 GB Blackwell burst) or frontier cloud reasoning (Gemini 4 Argon, Claude Sonnet 4, gpt-5).
  4. In our hermetic 96 GB Blackwell RouterArena live benchmark (809 live generations + 420 robustness perturbations, strictly billing all 325 aborted streams / 58,743 wasted tokens), live SSE cascade lifted 7B accuracy +11.93 pp (48.36% → 60.29%) while cutting 120B TTFT by -48.0% (1,694.5 ms → 880.7 ms) and cost by -37.4%.
📊 External Routing Benchmarks (October 2026): Across 12,566 prompts and 13 frontier models on official ACL 2026 LLMRouterBench (including unseen OOD ARC-AGI and Humanity's Last Exam), krusch-cascade-router achieved the #1 lowest Pareto distance (0.4269) across all 10 evaluated policies at -83.95% cost vs. gpt-5, and routed 6-language agentic Polyglot loops 1,442x faster (0.395 ms vs. 569.51 ms) than an LLM preflight classifier while beating it by +30.00 pp Pass@2 (51.67% vs. 21.67%). On RouteWorks/RouterArena official GitHub CI (PR #169, currently open), it scored 0.7883 (80.72% accuracy at $0.2117/1K).

🧠 5. The Memory Substrate: krusch-context-mcp v2.0.0

A reliable harness requires bounded, persistent context across multi-million-token project histories without prompt bloat. krusch-context-mcp (v2.0.0) is a 5-verb Model Context Protocol server backed by 8,192-context snowflake-arctic-embed2 (1024d) embeddings (verified across all 2,000 Official ICLR 2026 BEAM 100K–10M probes →):

1. Temporal Recency Decay & Explicit Lineage Superseding

Flat vector search treats a memory created six months ago identically to one created five minutes ago if their cosine similarities match. In real software projects, architectural conventions evolve. krusch-context-mcp combines an exponential recency prior with explicit relational lineage (supersedes_id via krusch_context_revise):

FinalScore = CalibratedCosine × e^(-0.01 × age_in_days)  ·  WHERE invalidated_at IS NULL AND superseded_by IS NULL

On Official ICLR 2026 BEAM, this relational grounding enabled our open-weights stack to beat commercial cloud past.dev across all 4 of 4 Contradiction Resolution splits (100K, 500K, 1M, and 10M tokens) while sustaining 92.58% (100K) and 93.00% (10M IE) recall and zero E2E QA drop from 100K to 1M (72.86%).

2. Two-Level Episode Rollups, Declarative Nuggets & SHA-256 Span Gates

Rather than bloating every turn's prompt window with a monolithic 4,000-token system prompt, declarative steering nuggets and 2-level episode rollups (ide_episode_rollups + 2-hop Gram-Schmidt orthogonal residual bridges) inject project invariants just-in-time. Every file-anchored rule carries a verbatim quoted_sentence and SHA-256 span check (INV-12), automatically flagging stale rules the moment underlying source lines drift.

3. Dual-Engine SQLite WAL & PostgreSQL 16 Parity

Single-developer laptops run zero-Docker node:sqlite WAL (.agent/context.db) with external-content Porter FTS5 and compact 4,096-byte IEEE-754 Float32Array vector blobs; multi-node swarms synchronize directly to PostgreSQL 16 + pgvector HNSW.

🛡️ 6. The Execution Core: krusch-coding-harness — 2PC Invariants Over Prompts

At the core of the execution tier is krusch-coding-harness (read the full Coding Harness architectural deep dive →). Where traditional agent loops rely on models to police their own file writes, krusch-coding-harness treats models as untrusted, interchangeable workers governed by a two-phase commit (2PC) transaction manager.

The Relational Finite State Machine (FSM)

The lifecycle of an engineering task is cataloged as a relational state graph in PostgreSQL (krusch_phase_edges):

INIT  →  PLAN  →  IMPLEMENT  →  VERIFY  →  APPROVAL_GATE  →  COMMITTED

Phase transitions are validated inside row-locked transactions and SQL triggers. If an agent attempts an illegal transition—such as jumping from PLAN directly to COMMITTED, or attempting shell execution during planning—the database engine immediately aborts the transaction with an invariant violation error.

Diff Staging & Bubblewrap Verify-on-Stop

When a worker model proposes code modifications, no physical files in the developer's working tree are touched:

  1. Proposed edits are formatted as unified diffs, checked by a truncation guard, SHA-256 hashed, and inserted into krusch_staged_diffs with status PENDING.
  2. Before any diff can advance, the harness mounts an isolated Bubblewrap (bwrap) sandbox and executes the real language test suite (npm test, pytest, cargo test, go test, gradle, cmake), recording the result in krusch_verification_runs.
  3. The Core Invariant: krusch_staged_diffs cannot transition to APPLYING or APPLIED unless the latest verification run exited with exit_code: 0.

Actionable Failure Attribution & Empirical 2.64x Coding Lift

When sandboxed tests fail, naive agents retry the entire prompt blindly. krusch-coding-harness parses compiler and test output through KruschFailureClassifier into structured failure classes (ContextManagement, ToolUse, ObservationManagement, AgentLoop) and feeds structured repair diagnostics into a bounded Verify-on-Stop turn.

🧪 Empirical 2PC Receipt (Aider Polyglot 225/225): Across all 225 official Exercism suites in C++, Go, Java, JavaScript, Python, and Rust using open-weights openai/gpt-oss-120b at $0 API cost, a single Verify-on-Stop + KruschFailureClassifier turn self-healed 72 failed tasks—lifting Pass@1 = 19.56% (44/225) to Pass@2 = 51.56% (116/225, 2.64x) with 0 truncated diffs, tying cloud Claude 3.5 Sonnet (51.6%) and beating GPT-4o (23.1%).

Atomic Apply Journal with Drift Detection & fsync Recovery

Applying verified changes to disk executes through an atomic write journal transitioning PENDING → APPLYING → APPLIED (<32 ms):

  1. Pre-Flight Drift Check: Before writing any file in a multi-file batch, the harness hashes each target file on disk against its recorded base hash. Any out-of-band edit aborts the entire batch cleanly.
  2. Atomic fsync & Rename: Files are written to temporary paths with explicit OS-level fsync() flushes before atomic filesystem renames.
  3. Startup Crash Recovery (recoverInFlightApplies): On host power loss or process crash mid-apply, next startup reconciles disk hashes—promoting completed files to APPLIED and rolling partial writes back cleanly.

📐 7. Why AI Agents Made Us Write More Raw SQL, Not Less (Zero ORMs)

A popular refrain in modern web development claims that AI coding assistants and ORMs have made learning SQL, indexing, joins, and query planning obsolete. Building production agent infrastructure taught us the exact opposite lesson.

Across all 48 Git repositories in our ecosystem, we use 0 JavaScript ORMs (zero Prisma, zero Drizzle, zero TypeORM, zero Sequelize). Instead, the stack maintains 40 dedicated .sql migration files, 41 raw pg / node:sqlite driver modules, and 234 explicit index definitions (HNSW, GIN, composite B-Trees, and external-content Porter FTS5 virtual tables). Even in our five Python services that use SQLAlchemy for schema modeling, every retrieval hot path drops straight to parameterized SQL, pgvector operators, and composite Top-K lazy hydration.

Why? Because a human web user clicks a button once every few seconds; an autonomous coding swarm fires hybrid vector, lexical, AST-graph, and lock-acquisition queries on every single reasoning step. When an ORM hides an N+1 join, eagerly detoasts megabytes of bytea source blobs during a filter scan, or tricks the planner into a Seq Scan instead of an HNSW index walk, agent latency explodes.

Production Receipt: 252 ms Seq Scan → 15.25 ms HNSW + GIN CTE (16.5x Faster)

In krusch-git (v1.2.3, indexing 54,456 AST symbols and 4,788 live blobs with snowflake-arctic-embed2), every file edit creates a new immutable Git blob while historical snapshots remain in the blobs table. A naive query that deduplicates to the latest blob per path before ordering by vector distance—or joins full file content inside the candidate scan—forces PostgreSQL out of the HNSW index into a 252 ms sequential scan and sort.

Rewriting the hot path with an oversampled MATERIALIZED ANN CTE, a composite (repository_id, file_path, last_seen_at DESC) B-Tree NOT EXISTS anti-join, and deferred Top-K TOAST hydration cut query execution to 15.25 ms (16.5x faster):

-- Production hybrid pattern in krusch-git v1.2.3 (252 ms Seq Scan → 15.25 ms HNSW + B-Tree)
WITH ann_raw AS MATERIALIZED (
    -- 1. Force pure pgvector HNSW index walk without detoasting bytea content
    SELECT b.id, b.repository_id, b.file_path, b.symbol_spine,
           b.last_seen_at, b.created_at,
           (b.embedding <=> $1::vector) AS cosine_dist
    FROM blobs b
    WHERE b.embedding IS NOT NULL
      AND (b.embed_model IS NULL OR b.embed_model = $4)
    ORDER BY b.embedding <=> $1::vector ASC
    LIMIT GREATEST(150, $3 * 20)
),
ann_latest AS (
    -- 2. Deduplicate superseded Git blobs via composite B-Tree (idx_blobs_repo_path_seen)
    SELECT * FROM ann_raw a
    WHERE a.file_path IS NULL OR NOT EXISTS (
        SELECT 1 FROM blobs b2
        WHERE b2.repository_id = a.repository_id
          AND b2.file_path = a.file_path
          AND (b2.last_seen_at > a.last_seen_at
               OR (b2.last_seen_at = a.last_seen_at AND b2.id > a.id))
    )
    ORDER BY a.cosine_dist ASC
    LIMIT $3
)
-- 3. Defer repositories JOIN and TOASTed full_b.content read strictly to surviving Top-K rows
SELECT l.*, r.name AS project, encode(full_b.content, 'escape') AS snippet
FROM ann_latest l
JOIN blobs full_b ON full_b.id = l.id
JOIN repositories r ON r.id = l.repository_id
ORDER BY l.cosine_dist ASC;

Paired with a weighted GENERATED ALWAYS AS (setweight(to_tsvector(...))) STORED GIN index splitting camelCase identifiers and AST symbol spines, plus 2-hop git_edges call-graph bridging, this exact SQL substrate propelled krusch-git v1.2.3 to 19.12% 8-Split Macro Acc@1 and 68.58% Acc@5 across 47,998 RepoBench-R Python and Java cross-file queries—surpassing published CodeBERT and UniXcoder baselines.

👤 8. The Human Gate is the Feature, Not a Compromise

A common misconception in the agent space is that human intervention represents an engineering failure. In professional software engineering, the opposite is true: unsupervised autonomy on mission-critical repositories is reckless.

The goal of the Krusch harness is not to eliminate the human; it is to eliminate the cognitive tax on the human:

💎 9. Novelty vs. Existing Practice

The individual primitives in the Krusch stack are not invented from whole cloth; they are proven distributed systems and database engineering instincts applied rigorously to coding agents:

Concept Common Existing Practice The Krusch Implementation
Data Access Layer Heavy ORMs hiding N+1 joins & Seq Scans Zero JS ORMs across 48 repos; parameterized SQL, MATERIALIZED HNSW/GIN CTEs (15.25ms) & FTS5 triggers
File Safety Direct writeFile(), ad-hoc git worktrees Diffs as first-class PostgreSQL rows with SHA-256 base-hash drift checks & atomic fsync journals
Verification Post-commit CI/CD pipelines Bubblewrap-sandboxed test pass (exit_code: 0) + Verify-on-Stop (19.56% → 51.56% Polyglot Pass@2)
Agent Memory Cloud vector SaaS, flat Markdown logs 5-verb MCP + 8K Arctic embeddings + lineage superseding + SHA-256 span-drift gates (100K–10M BEAM)
Model Routing 500ms LLM preflight classifiers Sub-15µs CPU Stage-0 gate + 1.27ms Arctic kNN + live SSE logprob abort (#1 LLMRouterBench Pareto)
State Authority In-memory agent graphs, chat transcripts PostgreSQL 16 FSM catalog (krusch_phase_edges) + FOR UPDATE SKIP LOCKED file leases

The contribution is the unified system: binding these primitives into a single, sovereign transaction manager where models are interchangeable workers and relational state is durable law.

🚀 10. Why PostgreSQL (+ Local SQLite WAL) as the State Plane Wins

Why build this substrate on PostgreSQL 16 (paired with zero-Docker node:sqlite WAL for single-box CLI portability) rather than a tangle of Redis, Mongo, Neo4j, and vector SaaS?

🏁 Conclusion: A Sampler is Not a Transaction Manager

Language models have unlocked remarkable creative capabilities in code generation. But code generation is only half of software engineering. The other half is query planning, verification, state management, transaction safety, and regression prevention.

By pairing krusch-pre-router on the CPU for sub-15µs zero-tax gating, krusch-cascade-router for Pareto-optimal multi-model cascades, krusch-context-mcp and krusch-git for index-accelerated hybrid memory and AST retrieval, and krusch-coding-harness for invariant PostgreSQL 2PC staging, we establish a deterministic contract for coding agents.

Language models provide the probabilistic spark. PostgreSQL and disciplined SQL engineering provide the transaction-managed state plane that turns that spark into production-ready software.

Explore the Sovereign Repositories & Raw Benchmark Artifacts: