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

Bun 1.2 Multi-Worker SQLite Isolated WAL Checkpoint Coordinator Standard

Enforces architectural isolation of SQLite WAL checkpoint execution in multi-threaded Bun 1.2 deployments to prevent unbounded WAL bloat and unrecoverable SQLITE_BUSY starvation. Mandates explicit disabling of worker-level auto-checkpoints and directs all PRAGMA wal_checkpoint operations through a singleton background coordinator worker.

Prohibited Anti-Pattern: Concurrent Worker Checkpoints Causing SQLITE_BUSY Cascades and Unbounded WAL Bloat

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Multi-Worker SQLite Isolated WAL Checkpoint Coordinator Standard - Enforces architectural isolation of SQLite WAL checkpoint execution in multi-threaded Bun 1.2 deployments to prevent unbounded WAL bloat and unrecoverable SQLITE_BUSY starvation. Mandates explicit disabling of worker-level auto-checkpoints and directs all PRAGMA wal_checkpoint operations through a singleton background coordinator worker.
globs: src/**/*.ts, src/db/**/*.ts, workers/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-sqlite-multi-worker-wal-coordinator"
---

# Bun 1.2 Multi-Worker SQLite Isolated WAL Checkpoint Coordinator Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Concurrent Worker Checkpoints Causing SQLITE_BUSY Cascades and Unbounded WAL Bloat

## System Constraints
You are a Principal Engineer enforcing the Bun 1.2 Embedded SQLite Multi-Worker WAL Checkpoint Standard. Mandate that multi-threaded or multi-worker Bun processes disable SQLite automatic checkpoints across standard reader/writer instances and serialize all WAL maintenance strictly within an isolated coordinator thread.

## Strict Guidelines
- Engineers MUST ALWAYS set 'PRAGMA wal_autocheckpoint = 0;' on all Bun SQLite instances initialized in application workers.
- Engineers MUST serialize all PRAGMA wal_checkpoint operations through a single, dedicated coordinator worker to avoid SQLITE_BUSY contention.
- Workers MUST NOT execute PRAGMA wal_checkpoint(FULL), PRAGMA wal_checkpoint(TRUNCATE), or PRAGMA wal_checkpoint(PASSIVE) directly during request/task handling.
- The checkpoint coordinator SHOULD favor 'PRAGMA wal_checkpoint(PASSIVE);' under active load and reserve 'PRAGMA wal_checkpoint(TRUNCATE);' for low-concurrency or idle windows.
- Database connections across all workers MUST configure a non-zero busy_timeout (e.g., 'PRAGMA busy_timeout = 5000;') to prevent immediate query termination during concurrent read-lock periods.

## Bad vs Good Implementations
### Anti-Pattern: Concurrent Worker Auto-Checkpoints and In-Band Checkpointing

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Each worker maintains SQLite's default autocheckpoint behavior (1000 pages). When multiple workers cross this threshold simultaneously or attempt ad-hoc TRUNCATE checkpoints during active queries, thread-level race conditions cause catastrophic SQLITE_BUSY lockups and reader starvation.
```typescript
import { Database } from 'bun:sqlite';

// Invoked across multiple Bun worker threads
export function getWorkerDb(path: string): Database {
  const db = new Database(path, { strict: true });
  db.run('PRAGMA journal_mode = WAL;');
  // DANGER: Default wal_autocheckpoint (1000 pages) is active on every worker.
  // Workers concurrently hit checkpoint thresholds, triggering SQLITE_BUSY locks.
  return db;
}

export function flushWorkerLogs(db: Database): void {
  // DANGER: In-band truncate checkpoint on an active reader/writer thread
  // halts sibling workers and causes lockups.
  db.run('PRAGMA wal_checkpoint(TRUNCATE);');
}
```

#### Production Standard (Correct)
> **Standard**: Explicitly disabling autocheckpoints across workers stops uncoordinated lock contention. All checkpoint executions are delegated to an isolated coordinator instance that uses PASSIVE non-blocking passes with explicit busy-detection and backoff.
```typescript
import { Database } from 'bun:sqlite';

// 1. Worker database initialization
export function createWorkerConnection(path: string): Database {
  const db = new Database(path, { strict: true });
  db.run('PRAGMA journal_mode = WAL;');
  db.run('PRAGMA busy_timeout = 5000;');
  // Disable implicit autocheckpoint on worker threads
  db.run('PRAGMA wal_autocheckpoint = 0;');
  return db;
}

// 2. Dedicated Singleton Background Coordinator (coordinator-worker.ts)
export class WalCheckpointCoordinator {
  private db: Database;
  private intervalId: Timer | null = null;

  constructor(dbPath: string) {
    this.db = new Database(dbPath, { strict: true });
    this.db.run('PRAGMA busy_timeout = 5000;');
    this.db.run('PRAGMA wal_autocheckpoint = 0;');
  }

  public start(intervalMs: number = 30000): void {
    this.intervalId = setInterval(() => this.executeControlledCheckpoint(), intervalMs);
  }

  public executeControlledCheckpoint(): void {
    try {
      // Execute non-blocking PASSIVE checkpoint during normal runtime
      const result = this.db.query('PRAGMA wal_checkpoint(PASSIVE);').get() as {
        busy: number;
        log: number;
        checkpointed: number;
      };

      if (result && result.busy === 1) {
        // Readers active; coordinator backs off without blocking workers
        return;
      }
    } catch (err) {
      console.error('Safe WAL checkpoint encountered error:', err);
    }
  }

  public close(): void {
    if (this.intervalId) clearInterval(this.intervalId);
    // Clean flush on coordinator shutdown
    this.db.run('PRAGMA wal_checkpoint(TRUNCATE);');
    this.db.close();
  }
}
```

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 Multi-Worker SQLite Isolated WAL Checkpoint Coordinator Standard

Bun 1.2 Multi-Worker SQLite Isolated WAL Checkpoint Coordinator Standard defines an industrial-grade behavioral standard for backend engineering. Enforces architectural isolation of SQLite WAL checkpoint execution in multi-threaded Bun 1.2 deployments to prevent unbounded WAL bloat and unrecoverable SQLITE_BUSY starvation. Mandates explicit disabling of worker-level auto-checkpoints and directs all PRAGMA wal_checkpoint operations through a singleton background coordinator worker. 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: Concurrent Worker Checkpoints Causing SQLITE_BUSY Cascades and Unbounded WAL Bloat 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 DriftConcurrent Worker Checkpoints Causing SQLITE_BUSY Cascades and Unbounded WAL BloatCritical
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 set 'PRAGMA wal_autocheckpoint = 0

Engineers MUST ALWAYS set 'PRAGMA wal_autocheckpoint = 0;' on all Bun SQLite instances initialized in application workers.

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 serialize all PRAGMA wal_checkpoint operations through a single, dedicated coordinator worker to avoid SQLITE_BUSY contention.

Engineers MUST serialize all PRAGMA wal_checkpoint operations through a single, dedicated coordinator worker to avoid SQLITE_BUSY contention.

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

Workers MUST NOT execute PRAGMA wal_checkpoint(FULL), PRAGMA wal_checkpoint(TRUNCATE), or PRAGMA wal_checkpoint(PASSIVE) directly during request/task handling.

Workers MUST NOT execute PRAGMA wal_checkpoint(FULL), PRAGMA wal_checkpoint(TRUNCATE), or PRAGMA wal_checkpoint(PASSIVE) directly during request/task handling.

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

The checkpoint coordinator SHOULD favor 'PRAGMA wal_checkpoint(PASSIVE)

The checkpoint coordinator SHOULD favor 'PRAGMA wal_checkpoint(PASSIVE);' under active load and reserve 'PRAGMA wal_checkpoint(TRUNCATE);' for low-concurrency or idle windows.

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

Database connections across all workers MUST configure a non-zero busy_timeout (e.g., 'PRAGMA busy_timeout = 5000

Database connections across all workers MUST configure a non-zero busy_timeout (e.g., 'PRAGMA busy_timeout = 5000;') to prevent immediate query termination during concurrent read-lock periods.

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 Multi-Worker SQLite Isolated WAL Checkpoint Coordinator Standard critical for AI coding assistants?

Enforces architectural isolation of SQLite WAL checkpoint execution in multi-threaded Bun 1.2 deployments to prevent unbounded WAL bloat and unrecoverable SQLITE_BUSY starvation. Mandates explicit disabling of worker-level auto-checkpoints and directs all PRAGMA wal_checkpoint operations through a singleton background coordinator worker. 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 (Concurrent Worker Checkpoints Causing SQLITE_BUSY Cascades and Unbounded WAL Bloat) 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