gastownhall/beads · error

insert compaction snapshot: %w

Error message

insert compaction snapshot: %w

What it means

This error wraps a failure inserting the compaction snapshot row into compaction_snapshots after a collision-free derived ID was chosen. It is raised when tx.ExecContext returns an error: typically a duplicate-key race, FK violation on issue_id, or an aborted transaction. The enclosing transaction (SnapshotIssueInTx) will roll back.

Source

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

	}
	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 {
		return fmt.Errorf("insert compaction snapshot: %w", err)
	}
	return nil
}

View on GitHub (pinned to 71377f2769)

Solutions

  1. Check the wrapped cause; on duplicate-key simply retry — the retry's same-content check will see the existing row and behave correctly
  2. Verify issue_id exists in issues (FK integrity)
  3. Serialize compaction per issue (lock or single-writer) to avoid concurrent derived-ID races
  4. Check server disk space and transaction state if errors persist

Example fix

// before
if _, err := tx.ExecContext(ctx, insertSQL, id, issueID, level, snap, createdAt); err != nil {
    return fmt.Errorf("insert compaction snapshot: %w", err)
}
// after
if _, err := tx.ExecContext(ctx, insertSQL, id, issueID, level, snap, createdAt); err != nil {
    if isDuplicateKeyErr(err) {
        return nil // concurrent compaction already stored identical content
    }
    return fmt.Errorf("insert compaction snapshot: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure the parent issue exists before snapshotting
var n int
db.QueryRow("SELECT COUNT(*) FROM issues WHERE id = ?", issueID).Scan(&n)
if n == 0 {
    return fmt.Errorf("issue %s does not exist; cannot snapshot", issueID)
}

Try / catch

err := SnapshotIssueInTx(ctx, tx, issue)
if err != nil {
    var derr *storage.DBError
    if errors.As(err, &derr) && strings.Contains(err.Error(), "insert compaction snapshot") {
        if isDuplicateKey(err) {
            return nil // identical snapshot already stored concurrently; safe to proceed
        }
    }
    return err
}

Prevention

When it happens

Trigger: INSERT INTO compaction_snapshots fails: primary-key collision on the derived ID (concurrent compaction of identical content), FK violation because the issue no longer exists, or the transaction was already aborted by a prior statement.

Common situations: Two processes compacting the same issue simultaneously deriving the same ID; the parent issue deleted between the dedup check and the insert; disk full on the Dolt server.

Related errors


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