This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-sqlite-wal-read-starvation-guardrail

Bun 1.2 Native SQLite Concurrent WAL Read-Starvation Prevention Guardrail

Enforces connection segregation between single-writer and multi-reader connections in Bun 1.2 native SQLite, preventing high-throughput or analytical reads from holding persistent read locks that stall WAL checkpointing. Guarantees deterministic WAL frame reclamation and bounded write latencies by mandating non-blocking checkpoint strategies and read transaction boundaries.

Prohibited Anti-Pattern: Executing long-running or unbounded queries on shared SQLite connections, blocking WAL checkpoints and starving write transactions.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Native SQLite Concurrent WAL Read-Starvation Prevention Guardrail - Enforces connection segregation between single-writer and multi-reader connections in Bun 1.2 native SQLite, preventing high-throughput or analytical reads from holding persistent read locks that stall WAL checkpointing. Guarantees deterministic WAL frame reclamation and bounded write latencies by mandating non-blocking checkpoint strategies and read transaction boundaries.
globs: src/**/db.ts, src/**/database.ts, src/**/*.sqlite.ts, src/repositories/**/*.ts, src/services/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-sqlite-wal-read-starvation-guardrail"
---

# Bun 1.2 Native SQLite Concurrent WAL Read-Starvation Prevention Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Executing long-running or unbounded queries on shared SQLite connections, blocking WAL checkpoints and starving write transactions.

## System Constraints
You are a Principal Software Architect enforcing production safety standards for Bun 1.2 native SQLite (bun:sqlite). You must guarantee zero write starvation and bounded WAL file growth by enforcing distinct reader-writer connection isolation, mandatory PRAGMA busy_timeout configurations, keyset-paginated read scans, and non-blocking asynchronous WAL checkpointing.

## Strict Guidelines
- Engineers MUST ALWAYS isolate SQLite handles into a single dedicated writer connection and dedicated read-only connections (using { readonly: true }) for read/analytical workloads.
- Engineers MUST ALWAYS configure PRAGMA journal_mode = WAL, PRAGMA synchronous = NORMAL, and PRAGMA busy_timeout = 5000 on every initialized Bun Database instance.
- Engineers MUST NOT execute streaming or unbounded cursor reads over large tables inside uncommitted transaction blocks or without pagination.
- Code MUST explicitly decouple analytical aggregation from transactional write paths using chunked keyset pagination to release WAL read marks iteratively.
- Services MUST schedule background non-blocking checkpoints using PRAGMA wal_checkpoint(PASSIVE) and NEVER execute blocking TRUNCATE or RESTART checkpoints within request-response write hot paths.
- Components NEVER hold open SQLite Statement iterators across asynchronous await boundaries without exhausting or finalizing the statement cursor.

## Bad vs Good Implementations
### Dual Connection Architecture with Non-Blocking Checkpointing

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: A single shared database handle performs an unpaginated, asynchronous iterated read. This maintains an open SQLite read transaction at an old WAL frame mark. Concurrently, write operations force a synchronous blocking TRUNCATE checkpoint, causing SQLITE_BUSY errors, thread contention, and severe WAL file ballooning.
```typescript
import { Database } from "bun:sqlite";

// ANTI-PATTERN: Single shared connection executing long-running read
// while concurrent writes are queued, indefinitely blocking WAL truncation.
export const db = new Database("production.db");
db.run("PRAGMA journal_mode = WAL;");

export async function runAnalyticalReport(): Promise<any[]> {
  // Holds the read mark open for the entire duration of the long table scan
  const query = db.query("SELECT * FROM telemetry_events");
  const results: any[] = [];
  
  for (const row of query.iterate()) {
    results.push(row);
    // Simulating heavy in-loop processing that holds the read mark
    if (results.length % 50_000 === 0) {
      await Bun.sleep(10);
    }
  }
  return results;
}

export function recordEvent(data: string): void {
  // Writer times out or causes massive WAL expansion because checkpoint cannot pass reader
  db.run("INSERT INTO telemetry_events (payload) VALUES (?)", [data]);
  db.run("PRAGMA wal_checkpoint(TRUNCATE);"); // Blocking checkpoint halts concurrent ops
}
```

#### Production Standard (Correct)
> **Standard**: Production invariant compliant implementation ensuring memory safety, deterministic execution, and zero runtime hallucination.
```typescript
import { Database } from "bun:sqlite";

const DB_PATH = "production.db";

function configurePragmas(db: Database): void {
  db.run("PRAGMA journal_mode = WAL;");
  db.run("PRAGMA synchronous = NORMAL;");
  db.run("PRAGMA busy_timeout = 5000;");
}

// Dedicated single-writer connection
export const writerDb = new Database(DB_PATH, { strict: true });
configurePragmas(writerDb);

// Dedicated read-only connection pool instance
export const readerDb = new Database(DB_PATH, { readonly: true, strict: true });
configurePragmas(readerDb);

export async function runAnalyticalReport(batchSize = 2500): Promise<number> {
  let lastId = 0;
  let totalProcessed = 0;
  let hasMore = true;
  
  const stmt = readerDb.query<{ id: number; payload: string }, [number, number]>(
    "SELECT id, payload FROM telemetry_events WHERE id > ? ORDER BY id ASC LIMIT ?"
  );

  // Keyset pagination guarantees read marks are released per chunk
  while (hasMore) {
    const rows = stmt.all(lastId, batchSize);
    if (rows.length === 0) {
      hasMore = false;
      break;
    }
    
    totalProcessed += rows.length;
    lastId = rows[rows.length - 1].id;
    
    // Yield loop control; Bun finishes statement lifecycle, freeing WAL snapshot
    await Bun.sleep(0);
  }
  
  return totalProcessed;
}

export function recordEvent(data: string): void {
  const insertStmt = writerDb.prepare("INSERT INTO telemetry_events (payload) VALUES (?)");
  insertStmt.run(data);
}

// Non-blocking passive checkpoint scheduled independently of writer hot path
export function scheduleMaintenanceCheckpoint(): { busy: number; log: number; checkpointed: number } {
  const [res] = writerDb.query<[number, number, number], []>(
    "PRAGMA wal_checkpoint(PASSIVE);"
  ).values();
  
  return {
    busy: res[0],
    log: res[1],
    checkpointed: res[2]
  };
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Bun 1.2 Native SQLite Concurrent WAL Read-Starvation Prevention Guardrail

Bun 1.2 Native SQLite Concurrent WAL Read-Starvation Prevention Guardrail defines an industrial-grade behavioral standard for backend engineering. Enforces connection segregation between single-writer and multi-reader connections in Bun 1.2 native SQLite, preventing high-throughput or analytical reads from holding persistent read locks that stall WAL checkpointing. Guarantees deterministic WAL frame reclamation and bounded write latencies by mandating non-blocking checkpoint strategies and read transaction boundaries. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Executing long-running or unbounded queries on shared SQLite connections, blocking WAL checkpoints and starving write transactions. Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftExecuting long-running or unbounded queries on shared SQLite connections, blocking WAL checkpoints and starving write transactions.Critical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS isolate SQLite handles into a single dedicated writer connection and dedicated read-only connections (using { readonly

Engineers MUST ALWAYS isolate SQLite handles into a single dedicated writer connection and dedicated read-only connections (using { readonly: true }) for read/analytical workloads.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST ALWAYS configure PRAGMA journal_mode = WAL, PRAGMA synchronous = NORMAL, and PRAGMA busy_timeout = 5000 on every initialized Bun Database instance.

Engineers MUST ALWAYS configure PRAGMA journal_mode = WAL, PRAGMA synchronous = NORMAL, and PRAGMA busy_timeout = 5000 on every initialized Bun Database instance.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Engineers MUST NOT execute streaming or unbounded cursor reads over large tables inside uncommitted transaction blocks or without pagination.

Engineers MUST NOT execute streaming or unbounded cursor reads over large tables inside uncommitted transaction blocks or without pagination.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Code MUST explicitly decouple analytical aggregation from transactional write paths using chunked keyset pagination to release WAL read marks iteratively.

Code MUST explicitly decouple analytical aggregation from transactional write paths using chunked keyset pagination to release WAL read marks iteratively.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Services MUST schedule background non-blocking checkpoints using PRAGMA wal_checkpoint(PASSIVE) and NEVER execute blocking TRUNCATE or RESTART checkpoints within request-response write hot paths.

Services MUST schedule background non-blocking checkpoints using PRAGMA wal_checkpoint(PASSIVE) and NEVER execute blocking TRUNCATE or RESTART checkpoints within request-response write hot paths.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Components NEVER hold open SQLite Statement iterators across asynchronous await boundaries without exhausting or finalizing the statement cursor.

Components NEVER hold open SQLite Statement iterators across asynchronous await boundaries without exhausting or finalizing the statement cursor.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Bun 1.2 Native SQLite Concurrent WAL Read-Starvation Prevention Guardrail critical for AI coding assistants?

Enforces connection segregation between single-writer and multi-reader connections in Bun 1.2 native SQLite, preventing high-throughput or analytical reads from holding persistent read locks that stall WAL checkpointing. Guarantees deterministic WAL frame reclamation and bounded write latencies by mandating non-blocking checkpoint strategies and read transaction boundaries. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Executing long-running or unbounded queries on shared SQLite connections, blocking WAL checkpoints and starving write transactions.) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Bun