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/sha256"
"encoding/binary"
"fmt"
"strings"
)
@@ -13,36 +12,29 @@ const (
randomLength = 11
)
type Prefix string
const (
Project = "prj"
Task = "tsk"
Session = "ses"
Snapshot = "snp"
Review = "rev"
InboxItem = "inb"
InboxResponse = "rsp"
Blob = "blb"
LeadNote = "not"
WorkflowDefinition = "wfd"
WorkflowRun = "run"
Send = "snd"
Client = "cli"
ClientRequest = "req"
Transfer = "xfr"
Terminal = "trm"
SavedCommand = "cmd"
Project Prefix = "prj"
Task Prefix = "tsk"
Session Prefix = "ses"
Snapshot Prefix = "snp"
Review Prefix = "rev"
InboxItem Prefix = "inb"
InboxResponse Prefix = "rsp"
Blob Prefix = "blb"
LeadNote Prefix = "not"
WorkflowDefinition Prefix = "wfd"
WorkflowRun Prefix = "run"
Send Prefix = "snd"
Client Prefix = "cli"
ClientRequest Prefix = "req"
Transfer Prefix = "xfr"
Terminal Prefix = "trm"
SavedCommand Prefix = "cmd"
)
var prefixes = map[string]bool{
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)
func New(prefix Prefix) string {
out := make([]byte, randomLength)
buf := make([]byte, 1)
for i := 0; i < len(out); {
@@ -53,11 +45,10 @@ func New(prefix string) string {
out[i] = alphabet[int(buf[0])%len(alphabet)]
i++
}
return prefix + string(out)
return string(prefix) + string(out)
}
func Derive(prefix string, parts ...string) string {
check(prefix)
func Derive(prefix Prefix, parts ...string) string {
sum := sha256.Sum256([]byte(strings.Join(parts, "\x00")))
n := binary.BigEndian.Uint64(sum[:8])
out := make([]byte, randomLength)
@@ -65,11 +56,5 @@ func Derive(prefix string, parts ...string) string {
out[i] = alphabet[n%uint64(len(alphabet))]
n /= uint64(len(alphabet))
}
return prefix + string(out)
}
func check(prefix string) {
if !prefixes[prefix] {
panic(fmt.Sprintf("no id prefix %q", prefix))
}
return string(prefix) + string(out)
}
+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) {
sources := []struct{ prefix, query string }{
sources := []struct {
prefix ids.Prefix
query string
}{
{ids.Project, `SELECT id FROM projects`},
{ids.Task, `SELECT id 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
}
_, mapped := renames[old]
if old == "" || mapped || strings.HasPrefix(old, src.prefix) {
if old == "" || mapped || strings.HasPrefix(old, string(src.prefix)) {
continue
}
renames[old] = ids.Derive(src.prefix, old)