/simplify: centralize ULID minting, simplify re-stamp, single walk · Entire

/simplify: centralize ULID minting, simplify re-stamp, single walk

c5e10dd·

Soph·1w ago·5 files·+102 added/-107 removed

Cleanups from /simplify on the migrate-to-ulid tool (quality only):

Skipped (noted): hardcoded "Entire-Checkpoint: " in the sed script (stable contract; threading trailers.CheckpointTrailerKey through a format string reads worse for one-shot tooling); go-git for ref listing/deletion (deletion deliberately uses the git CLI for packed-refs correctness — see cleanup.go/ephemeral.go/common.go — and the command already requires git for filter-branch); per-session tree reads + JSON key reorder (negligible one-shot).

Sessions

755d3a0d9603View transcript

Changes

5

// (chosen by checkpoint.GenerateCheckpointID); the value is canonical and passes
// KindOf/Validate as KindULID.
//
// The timestamp is Unix epoch milliseconds (via ulid.Now), so it is inherently
// timezone-independent — the machine's local zone does not affect the ID.
// The timestamp is Unix epoch milliseconds, so it is inherently timezone-
// independent — the machine's local zone does not affect the ID.
func GenerateULID() (CheckpointID, error) {
    u, err := ulid.New(ulid.Now(), rand.Reader)
    return GenerateULIDAt(time.Now())
}

// GenerateULIDAt is GenerateULID with an explicit timestamp: the ULID's
// millisecond prefix is taken from t (falling back to now when t is zero), with
// crypto-random entropy so same-millisecond IDs stay unique. Use it to mint an ID
// that sorts at a historical time — e.g. re-identifying existing checkpoints as
// ULIDs by their original creation time.
func GenerateULIDAt(t time.Time) (CheckpointID, error) {
    if t.IsZero() {
        t = time.Now()
    }
    u, err := ulid.New(ulid.Timestamp(t.UTC()), rand.Reader)
    if err != nil {
        return EmptyCheckpointID, fmt.Errorf("failed to generate ULID checkpoint ID: %w", err)
    }
}

func TestGenerateULIDAt(t *testing.T) {
    t.Parallel()
    when := time.Date(2025, 3, 4, 5, 6, 7, 0, time.UTC)

a, err := GenerateULIDAt(when)
    if err != nil {
        t.Fatalf("GenerateULIDAt() error = %v", err)
    }
    if a.Kind() != KindULID {
        t.Errorf("Kind() = %v, want KindULID for %q", a.Kind(), a)
    }
}

func TestMintULIDAt(t *testing.T) {
    t.Parallel()

t.Run("carries the given timestamp", func(t *testing.T) {
        t.Parallel()
        when := time.Date(2025, 3, 4, 5, 6, 7, 0, time.UTC)
        cid, err := mintULIDAt(when)
        require.NoError(t, err)
        require.Equal(t, id.KindULID, cid.Kind())
    
    })

// additional tests omitted for brevity
}