fix(dolt): use adaptive ID length instead of hardcoded 6 chars

Port the adaptive ID length algorithm from SQLite to Dolt backend.
The ID length now scales from 3-8 characters based on database size
using birthday paradox collision probability calculations.

- Add adaptive_length.go with length computation based on issue count
- Update generateIssueID to use adaptive length with nonce fallback
- Add collision detection and retry logic matching SQLite behavior

Fixes bd-c40999

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Executed-By: mayor
Role: mayor
This commit is contained in:
mayor
2026-01-25 16:50:00 -08:00
committed by gastown/crew/joe
parent cc23ccb557
commit 1423bdc5fb
2 changed files with 170 additions and 8 deletions

View File

@@ -8,6 +8,7 @@ import (
"strings"
"time"
"github.com/steveyegge/beads/internal/idgen"
"github.com/steveyegge/beads/internal/types"
)
@@ -617,15 +618,47 @@ func markDirty(ctx context.Context, tx *sql.Tx, issueID string) error {
return err
}
// nolint:unparam // error return kept for interface consistency
func generateIssueID(_ context.Context, _ *sql.Tx, prefix string, issue *types.Issue, _ string) (string, error) {
// Simple hash-based ID generation
// Use first 6 chars of content hash
hash := issue.ComputeContentHash()
if len(hash) > 6 {
hash = hash[:6]
// generateIssueID generates a unique hash-based ID for an issue
// Uses adaptive length based on database size and tries multiple nonces on collision
func generateIssueID(ctx context.Context, tx *sql.Tx, prefix string, issue *types.Issue, actor string) (string, error) {
// Get adaptive base length based on current database size
baseLength, err := GetAdaptiveIDLengthTx(ctx, tx, prefix)
if err != nil {
// Fallback to 6 on error
baseLength = 6
}
return fmt.Sprintf("%s-%s", prefix, hash), nil
// Try baseLength, baseLength+1, baseLength+2, up to max of 8
maxLength := 8
if baseLength > maxLength {
baseLength = maxLength
}
for length := baseLength; length <= maxLength; length++ {
// Try up to 10 nonces at each length
for nonce := 0; nonce < 10; nonce++ {
candidate := generateHashID(prefix, issue.Title, issue.Description, actor, issue.CreatedAt, length, nonce)
// Check if this ID already exists
var count int
err = tx.QueryRowContext(ctx, `SELECT COUNT(*) FROM issues WHERE id = ?`, candidate).Scan(&count)
if err != nil {
return "", fmt.Errorf("failed to check for ID collision: %w", err)
}
if count == 0 {
return candidate, nil
}
}
}
return "", fmt.Errorf("failed to generate unique ID after trying lengths %d-%d with 10 nonces each", baseLength, maxLength)
}
// generateHashID creates a hash-based ID for a top-level issue.
// Uses base36 encoding (0-9, a-z) for better information density than hex.
func generateHashID(prefix, title, description, creator string, timestamp time.Time, length, nonce int) string {
return idgen.GenerateHashID(prefix, title, description, creator, timestamp, length, nonce)
}
func isAllowedUpdateField(key string) bool {