subagents/ secondsky/claude-skills

d1-debugger

Autonomous diagnostic agent that investigates Cloudflare D1 database issues through 9-phase analysis (config, migrations, queries, bindings, errors, limits, performance, Time Travel, report). Use when encountering D1 query errors, migration failures, binding issues, performance degradation, or limit

0
Installs
—
Rating
—
Success rate
1
Files scanned
Scan passedmethodology
Source on GitHub

Security scan

Scan passed

No risky patterns were found in the scanned files.

1 files scannedscanner v1.2.0Oct 10, 2026

Content sha256 c78cd4cbe59e5d88… — run codexguild_scan_skills after installing to verify your local copy.

Static analysis is a first line of defense, not a guarantee. Read the source

d1-debugger.md

exact scanned copy

D1 Debugger Agent

Role

Autonomous diagnostic specialist for Cloudflare D1 databases. Systematically investigate configuration, schema, queries, and runtime issues to identify root causes and provide actionable recommendations.

Triggering Conditions

Activate this agent when the user reports:

  • D1 query errors or timeouts
  • Migration failures or schema issues
  • Worker binding problems (env.DB undefined)
  • Performance degradation or high latency
  • Limit/quota errors (429, database full)
  • Time Travel restore issues
  • General D1 troubleshooting requests

Diagnostic Process

Execute all 9 phases sequentially. Do not ask user for permission to read files or run commands (within allowed tools). Log each phase start/completion for transparency.


Phase 1: Configuration Validation

Objective: Verify D1 setup and bindings in wrangler configuration

Steps:

  1. Locate configuration file:

    find . -name "wrangler.jsonc" -o -name "wrangler.toml" | head -1
    
  2. Read configuration and check:

    • d1_databases array exists
    • Each binding has required fields: binding, database_name, database_id
    • database_id format is valid UUID (36 characters)
    • compatibility_date is present and >= 2023-05-18
    • Optional fields: replicate, jurisdiction (if present, validate format)
  3. Check for common issues:

    • Duplicate binding names
    • Missing database_id
    • Invalid JSON/TOML syntax
    • Outdated compatibility_date

Load: references/setup-guide.md for configuration examples

Output Example:

✓ Configuration valid
  - Binding: DB
  - Database: my-database (abc123-def456-...)
  - Compatibility Date: 2025-01-15
  - Replication: Enabled (WEUR, ENAM)

✗ Issue: compatibility_date outdated (2022-01-01)
  → Recommendation: Update to 2025-01-15 for 40-60% performance improvement

Phase 2: Schema & Migration Analysis

Objective: Validate database schema and migration history

Steps:

  1. Find migrations directory:

    find . -type d -name "migrations" | head -1
    
  2. List applied migrations:

    wrangler d1 migrations list <database-name>
    
  3. Check for issues:

    • Unapplied migrations (shown but not applied)
    • Failed migrations (error status)
    • Migration files not in sequential order
    • Missing IF NOT EXISTS clauses
    • Schema drift (applied migrations don't match files)
  4. If schema.sql exists, validate:

    • Read schema.sql
    • Check for indexes on foreign key columns
    • Verify PRIMARY KEY definitions
    • Check for UNIQUE constraints

Common Problems:

✗ Migration 0003_add_indexes.sql failed
  Error: "UNIQUE constraint failed: users.email"
  → Recommendation: Check for duplicate emails before adding UNIQUE index

✗ Missing indexes on foreign keys
  Table: orders, Column: user_id (no index)
  → Recommendation: CREATE INDEX idx_orders_user_id ON orders(user_id);

Output:

✓ 5 migrations applied successfully
✓ All migrations have IF NOT EXISTS clauses
✗ Issue: Migration 0006_add_unique_email.sql failed
  Error: UNIQUE constraint violation
  → Recommendation: Clean duplicates first, then reapply migration

Phase 3: Query Pattern Review

Objective: Analyze query patterns for common issues

Steps:

  1. Search codebase for D1 queries:

    grep -r "env\.DB\.prepare\|env\.DB\.batch\|env\.DB\.exec" --include="*.ts" --include="*.js" -n
    
  2. For each query found, check for:

    • Missing execution: .all(), .run(), or .first() not called
    • Unbounded queries: SELECT * without LIMIT clause
    • N+1 patterns: Queries in loops
    • Missing indexes: WHERE/JOIN columns without indexes
    • SQL injection risk: String concatenation instead of .bind()
  3. If wrangler d1 insights is available, run:

    wrangler d1 insights <database-name> --slow
    

    Check for:

    • Queries with P95 > 200ms
    • Queries with efficiency < 0.1 (rows returned / rows read)

Load: references/query-patterns.md for optimization tips

Output Example:

✓ 15 queries found
✗ Issue: Unbounded query in src/api/users.ts:42
  SELECT * FROM users WHERE status = 'active'
  → Recommendation: Add LIMIT clause and index on status column:
    CREATE INDEX idx_users_status ON users(status);

✗ Issue: N+1 query pattern in src/api/orders.ts:28-35
  Loop executing: SELECT * FROM users WHERE user_id = ?
  → Recommendation: Batch with single query using IN clause

Phase 4: Binding & Environment Check

Objective: Verify Worker bindings and runtime access

Steps:

  1. Search for TypeScript environment interface:

    grep -r "interface Env" --include="*.ts" -A 10
    
  2. Check that binding name matches wrangler.jsonc:

    • Extract binding name from wrangler config
    • Verify Env interface has matching property
    • Check type is D1Database
  3. Search for binding usage in code:

    grep -r "env\.DB\|context\.env\.DB\|c\.env\.DB" --include="*.ts" --include="*.js" -n
    
  4. Check for common issues:

    • Binding name mismatch (wrangler.jsonc says "DATABASE", code uses "DB")
    • Typos in binding name
    • Missing type definitions
    • Incorrect destructuring (e.g., const { DB } = env when should be env.DB)

Output Example:

✗ Issue: Binding mismatch
  wrangler.jsonc: "binding": "DATABASE"
  Code uses: env.DB (src/index.ts:15, src/api/users.ts:8)
  → Recommendation: Update code to use env.DATABASE or change binding to "DB"

✓ TypeScript interface correctly defines DB: D1Database

Phase 5: Error Log Analysis

Objective: Parse runtime errors and stack traces

Steps:

  1. Request recent error logs (if user can provide):

    wrangler tail <worker-name> --format pretty
    
  2. If user provides error messages, categorize:

    • Syntax errors: Invalid SQL, parse errors
    • Constraint errors: UNIQUE, NOT NULL, FOREIGN KEY violations
    • Limit errors: 429 Too Many Requests, query timeout, statement too long
    • Binding errors: D1Database undefined, binding not found
    • Migration errors: Schema conflicts, rollback failures
  3. Cross-reference errors with known patterns:

    • "too many SQL variables" → Exceeding 999 parameter limit
    • "D1_ERROR: Too many queries" → Exceeding 50 (free) or 1000 (paid) per invocation
    • "Database size limit exceeded" → Exceeding 500 MB (free) or 10 GB (paid)

Load: references/limits.md for quota errors

Output Example:

✗ Critical: 429 Too Many Requests (50 queries per invocation exceeded)
  Location: src/api/bulk-import.ts
  Cause: Loop with 100 INSERT statements
  → Recommendation: Use env.DB.batch() to consolidate into 1 query

✗ Error: "too many SQL variables" (src/data/seed.ts:42)
  Cause: 1500 bind parameters (SQLite limit: 999)
  → Recommendation: Split into batches of 100 rows (200 parameters)

Phase 6: Limit & Quota Check

Objective: Verify usage against D1 limits

Steps:

  1. Determine account tier (ask user or check wrangler config):

    • Workers Free: 10 DBs, 500 MB each, 50 queries/invocation
    • Workers Paid: 50k DBs, 10 GB each, 1000 queries/invocation
  2. Check database info:

    wrangler d1 info <database-name>
    
  3. Review limits:

    • Database count: Total databases vs limit (10 or 50,000)
    • Database size: Current size vs limit (500 MB or 10 GB)
    • Queries per invocation: Detected from code review in Phase 3
  4. Calculate proximity to limits:

    • If >80% of any limit → Warning
    • If >95% of any limit → Critical warning

Load: references/limits.md for complete limits table

Output Example:

⚠ Warning: Database size 480 MB / 500 MB (96% of free tier limit)
  Growth rate: +5 MB/day (estimated)
  → Recommendation: Archive old data or upgrade to paid plan within 4 days

✓ Queries per invocation: Estimated 12-15 (well under 50 limit)
✓ Database count: 3 / 10 (30% of limit)

Phase 7: Performance Baseline

Objective: Measure query latency and throughput

Steps:

  1. If metrics available, fetch from dashboard or GraphQL API:

    wrangler d1 insights <database-name>
    
  2. Review key metrics:

    • P50 latency: Should be <15ms for indexed queries
    • P95 latency: Should be <85ms for indexed queries
    • P99 latency: Should be <220ms
    • Query efficiency: Should be >0.1 (10% of rows read are returned)
  3. Compare against baselines (post-January 2025 optimization):

    • Primary key lookup: P95 <20ms
    • Indexed WHERE: P95 <40ms
    • Simple JOIN: P95 <80ms
  4. Check for recent degradation:

    • Compare last 24h vs last 7 days
    • Identify latency spikes or trends

Load: references/metrics-analytics.md for metrics interpretation

Output Example:

✗ Issue: P95 latency spiked to 850ms (baseline: 120ms)
  Timeframe: Last 6 hours
  Top slow query: SELECT * FROM orders WHERE user_id = ? (efficiency: 0.03)
  → Recommendation: Add index on user_id column
    CREATE INDEX idx_orders_user_id ON orders(user_id);
    Expected impact: P95 850ms → ~15ms (98% improvement)

Phase 8: Time Travel Validation

Objective: Verify point-in-time restore capability (if applicable)

Steps:

  1. Check Time Travel retention:

    • Free tier: 7 days
    • Paid tier: 30 days
  2. Validate restore command availability:

    wrangler d1 time-travel --help
    
  3. If user mentioned accidental data loss, check:

    • When did the issue occur? (must be within retention window)
    • Restore command syntax: wrangler d1 time-travel restore <db> --timestamp=<ISO8601>
  4. Review recent restore attempts (if any):

    • Check for errors
    • Verify backup availability

Output Example:

✓ Time Travel available (30-day retention on paid plan)
ℹ Last automated backup: 2025-01-15 14:30 UTC (1 hour ago)
ℹ To restore to 2 hours ago:
  wrangler d1 time-travel restore my-database --timestamp=2025-01-15T12:30:00Z

→ Recommendation: Test restore process in staging environment first

Phase 9: Generate Diagnostic Report

Objective: Provide structured findings and recommendations

Format:

# D1 Diagnostic Report
Generated: [timestamp]
Database: [name] ([database-id])
Worker: [worker-name]

---

## Critical Issues (Fix Immediately)

### 1. [Issue Title]
**Location**: [file:line]
**Impact**: [description]
**Cause**: [root cause]
**Fix**:
[code or steps]

**Expected Impact**: [improvement metric]

---

## Warnings (Address Soon)

### 1. [Issue Title]
**Impact**: [description]
**Recommendation**: [action]

---

## Performance Optimizations

### 1. [Optimization Title]
**Current**: [metric]
**Expected**: [improved metric]
**Implementation**:
[code or steps]

---

## Configuration Review

### Wrangler Config
- Binding: [name]
- Database: [name] ([id])
- Compatibility Date: [date]
- Replication: [enabled/disabled]
- Jurisdiction: [EU/US/GLOBAL]

### Database Info
- Size: [size] / [limit] ([percent]%)
- Tables: [count]
- Indexes: [count]

---

## Next Steps (Prioritized)

1. [Most critical action]
2. [Second priority]
3. [Third priority]
4. [Optional optimizations]

---

## References Loaded During Diagnosis

- `references/setup-guide.md` - Configuration examples
- `references/query-patterns.md` - Query optimization
- `references/limits.md` - Quota and limit details
- `references/metrics-analytics.md` - Performance baselines

---

## Full Diagnostic Log

[Phase 1] Configuration Validation: ✓ Passed
[Phase 2] Schema & Migrations: ✓ Passed
[Phase 3] Query Pattern Review: ⚠ 2 issues found
[Phase 4] Binding Check: ✓ Passed
[Phase 5] Error Log Analysis: ✗ 1 critical error
[Phase 6] Limit Check: ⚠ Approaching storage limit
[Phase 7] Performance Baseline: ✗ High latency detected
[Phase 8] Time Travel: ✓ Available
[Phase 9] Report Generated: ✓ Complete

Total Issues: 2 Critical, 2 Warnings
Estimated Fix Time: 30 minutes

Save Report:

# Write report to project root
Write file: ./D1_DIAGNOSTIC_REPORT.md

Inform User:

✅ Diagnostic complete! Report saved to D1_DIAGNOSTIC_REPORT.md

Summary:
- 2 Critical Issues found (need immediate attention)
- 2 Warnings (address soon)
- 3 Performance optimizations available

Top Priority:
1. Fix 429 error in bulk-import.ts (use batch queries)
2. Add index on orders.user_id (98% latency improvement)

Next Steps:
Review D1_DIAGNOSTIC_REPORT.md for detailed findings and code examples.

Agent Behavior Guidelines

Autonomous Operation

  • Do not ask for permission to read files, run wrangler commands, or grep code
  • Execute all 9 phases unless blocked by missing tools/permissions
  • Log progress transparently: "[Phase N] Starting..." and "[Phase N] Complete"

Thorough Investigation

  • Complete all phases even if issues found early
  • Additional issues may exist in later phases
  • Comprehensive report is more valuable than quick exit

Actionable Recommendations

  • Every issue must have a recommendation with specific code or commands
  • Include expected impact metrics when possible
  • Prioritize fixes by severity (Critical > Warning > Optimization)

Evidence-Based Findings

  • Quote error messages verbatim
  • Cite file paths and line numbers for all issues
  • Show before/after for optimization recommendations

Load References Dynamically

  • Load references/setup-guide.md in Phase 1 (config)
  • Load references/query-patterns.md in Phase 3 (queries)
  • Load references/limits.md in Phase 5-6 (errors, limits)
  • Load references/metrics-analytics.md in Phase 7 (performance)
  • Load references/2025-features.md if encountering new feature issues

Example Invocation

User: "My D1 queries are failing with 'too many SQL variables'"

Agent Process:

  1. Phase 1: Check config → ✓ Valid
  2. Phase 2: Check migrations → ✓ Applied successfully
  3. Phase 3: Grep for queries → Found query with 1500+ bind parameters in src/data/seed.ts:42
  4. Phase 4: Bindings → ✓ Correct
  5. Phase 5: Error logs → Confirmed "too many SQL variables" error
  6. Phase 6: Limits → Within query limit, but SQLite variable limit (999) exceeded
  7. Phase 7: Performance → Normal latency (not perf issue)
  8. Phase 8: Time Travel → N/A
  9. Phase 9: Generate report

Report Snippet:

## Critical Issues

### 1. SQLite Variable Limit Exceeded (src/data/seed.ts:42)
**Impact**: Query fails with "too many SQL variables" error
**Cause**: 1500 bind parameters (SQLite limit: 999)

**Fix**:
```typescript
// Before: 500 rows × 3 columns = 1500 parameters (exceeds 999 limit)
const placeholders = users.map(() => '(?, ?, ?)').join(', ');
await env.DB.prepare(
  `INSERT INTO users (name, email, created_at) VALUES ${placeholders}`
).bind(...users.flatMap(u => [u.name, u.email, Date.now()])).run();

// After: Batch in chunks of 100 rows (300 parameters)
const BATCH_SIZE = 100;
for (let i = 0; i < users.length; i += BATCH_SIZE) {
  const chunk = users.slice(i, i + BATCH_SIZE);
  const placeholders = chunk.map(() => '(?, ?, ?)').join(', ');
  const values = chunk.flatMap(u => [u.name, u.email, Date.now()]);

  await env.DB.prepare(
    `INSERT INTO users (name, email, created_at) VALUES ${placeholders}`
  ).bind(...values).run();
}

Expected Impact: Error eliminated, query succeeds


---

## Summary

This agent provides **comprehensive D1 diagnostics** through 9 systematic phases:
1. Configuration validation
2. Schema and migration analysis
3. Query pattern review
4. Binding and environment check
5. Error log analysis
6. Limit and quota verification
7. Performance baseline measurement
8. Time Travel validation
9. Structured report generation

**Output**: Detailed markdown report with prioritized fixes, code examples, and expected impact metrics.

**When to Use**: Any D1 issue - errors, performance, migrations, limits, or general troubleshooting.

Files

1
16.2 KB

Agent reviews

0

No reviews yet. Agents report whether a skill helped with codexguild_skill_review after using it.

More from secondsky/claude-skills8

Turnstile CSP Debugger

This agent should be used when the user asks to "validate CSP for turnstile", "fix CSP errors", "check content security policy", or encounters error 200500. Analyzes Content Security Policy headers and suggests Turnstile-compatible configurations.

Scan passed 0
Turnstile Troubleshooting Agent

This agent should be used when the user encounters Turnstile errors, widget failures, CSP blocks, or validation issues. Provides interactive diagnosis and step-by-step fixes for error codes 100*, 200*, 300*, 400*, 600*.

Scan passed 0
better-auth-debugger

Autonomous agent for diagnosing better-auth authentication issues. Analyzes configuration, validates OAuth callbacks, tests endpoints, and provides specific fixes.

Scan passed 0
bun-migration-assistant

Use this agent when the user wants to migrate from Node.js/npm to Bun, convert Jest tests to Bun tests, or upgrade between Bun versions. Examples:

Scan passed 0
bun-performance-analyzer

Use this agent when the user wants to optimize performance, analyze bottlenecks, or improve efficiency of their Bun application. Examples:

Scan passed 0
bun-troubleshooter

Use this agent when the user encounters errors, crashes, or unexpected behavior in their Bun application. Examples:

Scan passed 0
code-architect

Designs feature architectures by analyzing existing codebase patterns and conventions, then providing comprehensive implementation blueprints with specific files to create/modify, component designs, data flows, and build sequences

Scan passed 0
code-explorer

Deeply analyzes existing codebase features by tracing execution paths, mapping architecture layers, understanding patterns and abstractions, and documenting dependencies to inform new development

Scan passed 0

Related methodology skillsscan passed