gastownhall/beads · error

failed to iterate issues for prefix %q: %w

Error message

failed to iterate issues for prefix %q: %w

What it means

After iterating rows in SeedCounterFromExistingIssuesTx, rows.Err() returned non-nil, meaning the row iteration was aborted by a driver/network error mid-scan (not a normal end of results).

Source

Thrown at internal/storage/issueops/helpers.go:304

	defer rows.Close()

	maxNum := 0
	pfxDash := prefix + "-"
	for rows.Next() {
		var id string
		if err := rows.Scan(&id); err != nil {
			continue
		}
		suffix := strings.TrimPrefix(id, pfxDash)
		if strings.Contains(suffix, ".") {
			continue // skip child IDs
		}
		if n, err := strconv.Atoi(suffix); err == nil && n > maxNum {
			maxNum = n
		}
	}
	if err := rows.Err(); err != nil {
		return fmt.Errorf("failed to iterate issues for prefix %q: %w", prefix, err)
	}

	if maxNum > 0 {
		_, err = tx.ExecContext(ctx, "INSERT INTO issue_counter (prefix, last_id) VALUES (?, ?)", prefix, maxNum)
		if err != nil {
			return fmt.Errorf("failed to seed issue counter for prefix %q at %d: %w", prefix, maxNum, err)
		}
	}
	return nil
}

// GetAdaptiveIDLengthTx returns the appropriate hash length based on database size.
//
//nolint:gosec // G201: table is a hardcoded constant
func GetAdaptiveIDLengthTx(ctx context.Context, tx DBTX, table, prefix string) (int, error) {
	var count int
	err := tx.QueryRowContext(ctx, fmt.Sprintf(`
		SELECT COUNT(*)

View on GitHub (pinned to 71377f2769)

Solutions

  1. Retry the ID-generation operation; seeding is idempotent and will re-scan.
  2. Increase the context timeout / remove the deadline so large scans can complete.
  3. Check connectivity to the Dolt server (dolt sql-server logs) for restarts or dropped connections.
  4. If persistent, run the seeding locally against the .beads DB rather than over the network.

Example fix

// before
ctx := context.Background() // no deadline, but network drops abort scan
// after: give the seeding scan explicit headroom and retry
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
err := retry.OnError(3, isTransient, func() error { return generateID(ctx) })
Defensive patterns

Strategy: retry

Try / catch

err := retry.Do(func() error {
    return storage.GenerateIssueIDInTable(ctx, tx, "issues", prefix, issue)
}, retry.Attempts(3), retry.RetryIf(isNetworkError))

Prevention

When it happens

Trigger: Long issues table scan during counter seeding interrupted by connection drop, Dolt server restart, or context cancellation while rows.Next() was streaming.

Common situations: Large issue databases over a flaky connection to a remote Dolt server; context timeout (ctx deadline exceeded) during seeding; server killed mid-query.

Related errors


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