This commit is contained in:
Ian Gulliver
2026-10-21 09:05:33 -07:00
parent cdfc148ba9
commit a977c3cad9
2 changed files with 28 additions and 40 deletions
+23 -38
View File
@@ -4,7 +4,6 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/binary" "encoding/binary"
"fmt"
"strings" "strings"
) )
@@ -13,36 +12,29 @@ const (
randomLength = 11 randomLength = 11
) )
type Prefix string
const ( const (
Project = "prj" Project Prefix = "prj"
Task = "tsk" Task Prefix = "tsk"
Session = "ses" Session Prefix = "ses"
Snapshot = "snp" Snapshot Prefix = "snp"
Review = "rev" Review Prefix = "rev"
InboxItem = "inb" InboxItem Prefix = "inb"
InboxResponse = "rsp" InboxResponse Prefix = "rsp"
Blob = "blb" Blob Prefix = "blb"
LeadNote = "not" LeadNote Prefix = "not"
WorkflowDefinition = "wfd" WorkflowDefinition Prefix = "wfd"
WorkflowRun = "run" WorkflowRun Prefix = "run"
Send = "snd" Send Prefix = "snd"
Client = "cli" Client Prefix = "cli"
ClientRequest = "req" ClientRequest Prefix = "req"
Transfer = "xfr" Transfer Prefix = "xfr"
Terminal = "trm" Terminal Prefix = "trm"
SavedCommand = "cmd" SavedCommand Prefix = "cmd"
) )
var prefixes = map[string]bool{ func New(prefix Prefix) string {
Project: true, Task: true, Session: true, Snapshot: true, Review: true,
InboxItem: true, InboxResponse: true, Blob: true, LeadNote: true,
WorkflowDefinition: true, WorkflowRun: true, Send: true,
Client: true, ClientRequest: true, Transfer: true,
Terminal: true, SavedCommand: true,
}
func New(prefix string) string {
check(prefix)
out := make([]byte, randomLength) out := make([]byte, randomLength)
buf := make([]byte, 1) buf := make([]byte, 1)
for i := 0; i < len(out); { for i := 0; i < len(out); {
@@ -53,11 +45,10 @@ func New(prefix string) string {
out[i] = alphabet[int(buf[0])%len(alphabet)] out[i] = alphabet[int(buf[0])%len(alphabet)]
i++ i++
} }
return prefix + string(out) return string(prefix) + string(out)
} }
func Derive(prefix string, parts ...string) string { func Derive(prefix Prefix, parts ...string) string {
check(prefix)
sum := sha256.Sum256([]byte(strings.Join(parts, "\x00"))) sum := sha256.Sum256([]byte(strings.Join(parts, "\x00")))
n := binary.BigEndian.Uint64(sum[:8]) n := binary.BigEndian.Uint64(sum[:8])
out := make([]byte, randomLength) out := make([]byte, randomLength)
@@ -65,11 +56,5 @@ func Derive(prefix string, parts ...string) string {
out[i] = alphabet[n%uint64(len(alphabet))] out[i] = alphabet[n%uint64(len(alphabet))]
n /= uint64(len(alphabet)) n /= uint64(len(alphabet))
} }
return prefix + string(out) return string(prefix) + string(out)
}
func check(prefix string) {
if !prefixes[prefix] {
panic(fmt.Sprintf("no id prefix %q", prefix))
}
} }
+5 -2
View File
@@ -102,7 +102,10 @@ func migratePrefixIDs(tx *sql.Tx, dataDir string) error {
} }
func idRenames(tx *sql.Tx) (map[string]string, error) { func idRenames(tx *sql.Tx) (map[string]string, error) {
sources := []struct{ prefix, query string }{ sources := []struct {
prefix ids.Prefix
query string
}{
{ids.Project, `SELECT id FROM projects`}, {ids.Project, `SELECT id FROM projects`},
{ids.Task, `SELECT id FROM tasks`}, {ids.Task, `SELECT id FROM tasks`},
{ids.WorkflowRun, `SELECT DISTINCT workflow_run FROM tasks`}, {ids.WorkflowRun, `SELECT DISTINCT workflow_run FROM tasks`},
@@ -133,7 +136,7 @@ func idRenames(tx *sql.Tx) (map[string]string, error) {
return nil, err return nil, err
} }
_, mapped := renames[old] _, mapped := renames[old]
if old == "" || mapped || strings.HasPrefix(old, src.prefix) { if old == "" || mapped || strings.HasPrefix(old, string(src.prefix)) {
continue continue
} }
renames[old] = ids.Derive(src.prefix, old) renames[old] = ids.Derive(src.prefix, old)