gastownhall/beads · error

scan same-content compaction snapshots: %w

Error message

scan same-content compaction snapshots: %w

What it means

This error wraps a failure while querying compaction_snapshots for pre-existing rows with identical content (issue_id, compaction_level, snapshot_json, created_at) inside InsertDerivedCompactionSnapshot. The library runs this same-content lookup before deriving a collision-free snapshot ID, so the error means the deduplication check itself failed at the SQL layer. It propagates the underlying driver/database error unchanged.

Source

Thrown at internal/storage/issueops/derivedid.go:256

// InsertDerivedCompactionSnapshot inserts a compaction_snapshots row under
// its content-derived id, with the same ordinal discipline as events. Two
// clones compacting the same issue at the same tier in the same second
// produce byte-identical snapshots and therefore the same id.
func InsertDerivedCompactionSnapshot(ctx context.Context, tx DBTX, issueID string, level int, snapshotJSON []byte, createdAt string) error {
	if createdAt == "" {
		createdAt = NowAuxTime()
	}
	snap := string(snapshotJSON)
	digest := rowid.Digest([]sql.NullString{
		str(issueID), str(fmt.Sprintf("%d", level)), str(snap), str(createdAt),
	})
	taken := make(map[string]bool)
	rows, err := tx.QueryContext(ctx, `
		SELECT id FROM compaction_snapshots
		WHERE issue_id = ? AND compaction_level = ? AND snapshot_json = ? AND created_at = ?`,
		issueID, level, snap, createdAt)
	if err != nil {
		return fmt.Errorf("scan same-content compaction snapshots: %w", err)
	}
	for rows.Next() {
		var id string
		if err := rows.Scan(&id); err != nil {
			_ = rows.Close()
			return fmt.Errorf("scan same-content compaction snapshots: %w", err)
		}
		taken[id] = true
	}
	_ = rows.Close()
	if err := rows.Err(); err != nil {
		return fmt.Errorf("scan same-content compaction snapshots: %w", err)
	}
	if _, err := tx.ExecContext(ctx, `
		INSERT INTO compaction_snapshots (id, issue_id, compaction_level, snapshot_json, created_at)
		VALUES (?, ?, ?, ?, ?)`,
		firstFreeDerivedID("compaction_snapshots", digest, taken),
		issueID, level, snap, createdAt); err != nil {

View on GitHub (pinned to 71377f2769)

Solutions

  1. Inspect the wrapped %w cause for the real driver error and fix that underlying condition first
  2. Verify the compaction_snapshots table exists with columns (id, issue_id, compaction_level, snapshot_json, created_at)
  3. Check database connectivity and retry the compaction operation
  4. Ensure the context passed to SnapshotIssueInTx has an adequate deadline

Example fix

// before
if err != nil {
    return fmt.Errorf("scan same-content compaction snapshots: %w", err)
}
// after
if err != nil {
    if errors.Is(err, context.DeadlineExceeded) {
        return fmt.Errorf("scan same-content compaction snapshots (timeout, raise ctx deadline): %w", err)
    }
    return fmt.Errorf("scan same-content compaction snapshots: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// before calling SnapshotIssueInTx
rows, err := db.Query("SELECT 1 FROM compaction_snapshots LIMIT 1")
if err != nil {
    return fmt.Errorf("compaction_snapshots table unavailable: %w", err)
}
rows.Close()

Try / catch

err := SnapshotIssueInTx(ctx, tx, issue)
var derr *storage.DBError
if errors.As(err, &derr) && strings.Contains(derr.Error(), "scan same-content compaction snapshots") {
    // inspect unwrapped cause, retry compaction after connectivity is restored
    return fmt.Errorf("compaction snapshot dedup check failed, retry: %w", err)
}

Prevention

When it happens

Trigger: The tx.QueryContext call on compaction_snapshots fails: table missing/corrupt, connection dropped mid-transaction, SQL/driver incompatibility, or context cancellation while the query executes. Reached via SnapshotIssueInTx during issue compaction.

Common situations: Dolt server restart or network blip during a compaction run; a migration that renamed/dropped compaction_snapshots; context deadline exceeded on a slow database; an older database file lacking the compaction_snapshots table.

Understand the failure class

Background: "query failed", "%w: SQL error" — wrapped database query errors in Go libraries explained — this error's family across 3 libraries.

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/8c1413f76b614a0b. Report an issue: GitHub.