-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(core): id-shape residency classifier + ksuid mint primitives (run-ops split base) #4112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+394
−1
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b089309
feat(run-ops): core residency classifier + ksuid mint primitives
d-cs 49eb05d
refactor(core): remove test-only ksuid-mint process-global from frien…
d-cs 8b617ba
fix(core): avoid BigInt literals in decodeKsuid for pre-ES2020 consumers
d-cs b6f60e3
fix(core): guard ksuid random source and address review nits
d-cs a98ebac
chore(changeset): add @trigger.dev/core changeset for pr01
d-cs a2261e9
fix(core): resolve KSUID crypto source lazily so importing friendlyId…
d-cs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@trigger.dev/core": patch | ||
| --- | ||
|
|
||
| Add isomorphic id-shape run-ops residency classifier and ksuid mint/decode primitives. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { afterEach, describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| RunId, | ||
| WaitpointId, | ||
| SnapshotId, | ||
| QueueId, | ||
| generateKsuidId, | ||
| decodeKsuid, | ||
| KSUID_PAYLOAD_BYTES, | ||
| } from "./friendlyId.js"; | ||
|
|
||
| const CUID_LEN = 25; | ||
| const KSUID_LEN = 27; | ||
|
|
||
| describe("RunId + WaitpointId mint cuid by default; ksuid via generateKsuidId", () => { | ||
| it("default: run + waitpoint mint cuid (25) and round-trip", () => { | ||
| for (const util of [RunId, WaitpointId]) { | ||
| const { id, friendlyId } = util.generate(); | ||
| expect(id.length).toBe(CUID_LEN); | ||
| expect(util.fromFriendlyId(friendlyId)).toBe(id); | ||
| expect(util.toId(friendlyId)).toBe(id); | ||
| expect(util.toId(id)).toBe(id); | ||
| expect(util.toFriendlyId(id)).toBe(friendlyId); | ||
| } | ||
| }); | ||
|
|
||
| it("explicit ksuid: a run/waitpoint friendlyId over generateKsuidId() is 27-char and round-trips", () => { | ||
| for (const util of [RunId, WaitpointId]) { | ||
| const id = generateKsuidId(); | ||
| const friendlyId = util.toFriendlyId(id); | ||
| expect(id.length).toBe(KSUID_LEN); | ||
| expect(util.fromFriendlyId(friendlyId)).toBe(id); | ||
| expect(util.toId(friendlyId)).toBe(id); | ||
| expect(util.toId(id)).toBe(id); | ||
| } | ||
| }); | ||
|
|
||
| it("SnapshotId + QueueId stay cuid (25)", () => { | ||
| expect(SnapshotId.generate().id.length).toBe(CUID_LEN); | ||
| expect(QueueId.generate().id.length).toBe(CUID_LEN); | ||
| }); | ||
|
|
||
| it("disjoint lengths: 27 (ksuid) vs 25 (cuid) — the classifier margin", () => { | ||
| expect(generateKsuidId().length).not.toBe(SnapshotId.generate().id.length); | ||
| }); | ||
|
|
||
| it("generateKsuidId() is directly callable and yields 27 chars", () => { | ||
| expect(generateKsuidId().length).toBe(KSUID_LEN); | ||
| }); | ||
| }); | ||
|
|
||
| describe("generateKsuidId is a genuine KSUID (decodable timestamp, time-ordered)", () => { | ||
| afterEach(() => vi.useRealTimers()); | ||
|
|
||
| it("is exactly 27 base62 chars", () => { | ||
| expect(generateKsuidId()).toMatch(/^[0-9A-Za-z]{27}$/); | ||
| }); | ||
|
|
||
| it("carries a decodable timestamp within a few seconds of now", () => { | ||
| const before = Math.floor(Date.now() / 1000); | ||
| const { timestampSeconds: ts } = decodeKsuid(generateKsuidId()); | ||
| expect(ts).toBeGreaterThanOrEqual(before - 2); | ||
| expect(ts).toBeLessThanOrEqual(Math.floor(Date.now() / 1000) + 2); | ||
| }); | ||
|
|
||
| it("is k-sortable: ids from later seconds sort lexicographically after earlier ones", () => { | ||
| vi.useFakeTimers(); | ||
| const ids: string[] = []; | ||
| for (const t of ["2026-01-01T00:00:00Z", "2026-01-01T00:05:00Z", "2026-09-01T12:00:00Z"]) { | ||
| vi.setSystemTime(new Date(t)); | ||
| ids.push(generateKsuidId()); | ||
| } | ||
| expect([...ids].sort()).toEqual(ids); | ||
| }); | ||
|
|
||
| it("is unique across many mints in the same second", () => { | ||
| const n = 1000; | ||
| expect(new Set(Array.from({ length: n }, () => generateKsuidId())).size).toBe(n); | ||
| }); | ||
| }); | ||
|
|
||
| describe("KSUID payload encode/decode (foundation primitive)", () => { | ||
| it("round-trips a full 16-byte payload exactly", () => { | ||
| const payload = new Uint8Array(KSUID_PAYLOAD_BYTES).map((_, i) => (i * 17 + 1) & 0xff); | ||
| const { payload: decoded } = decodeKsuid(generateKsuidId(payload)); | ||
| expect(Array.from(decoded)).toEqual(Array.from(payload)); | ||
| }); | ||
|
|
||
| it("preserves a partial payload prefix and keeps the remainder for entropy", () => { | ||
| const meta = new Uint8Array([9, 8, 7, 6]); | ||
| const { payload } = decodeKsuid(generateKsuidId(meta)); | ||
| expect(Array.from(payload.slice(0, 4))).toEqual([9, 8, 7, 6]); | ||
| expect(payload.length).toBe(KSUID_PAYLOAD_BYTES); | ||
| }); | ||
|
|
||
| it("still carries a decodable timestamp when a payload is embedded", () => { | ||
| const before = Math.floor(Date.now() / 1000); | ||
| const { timestampSeconds } = decodeKsuid(generateKsuidId(new Uint8Array([1, 2, 3]))); | ||
| expect(timestampSeconds).toBeGreaterThanOrEqual(before - 2); | ||
| expect(timestampSeconds).toBeLessThanOrEqual(Math.floor(Date.now() / 1000) + 2); | ||
| }); | ||
|
|
||
| it("stays 27 chars with a full payload and decodes through a friendlyId prefix", () => { | ||
| const id = generateKsuidId(new Uint8Array(KSUID_PAYLOAD_BYTES).fill(0xab)); | ||
| expect(id).toMatch(/^[0-9A-Za-z]{27}$/); | ||
| expect(Array.from(decodeKsuid(`run_${id}`).payload)).toEqual( | ||
| new Array(KSUID_PAYLOAD_BYTES).fill(0xab) | ||
| ); | ||
| }); | ||
|
|
||
| it("throws if the payload exceeds the 16-byte budget", () => { | ||
| expect(() => generateKsuidId(new Uint8Array(KSUID_PAYLOAD_BYTES + 1))).toThrow(); | ||
| }); | ||
|
|
||
| it("decodeKsuid rejects a body that is not 27 base62 chars", () => { | ||
| expect(() => decodeKsuid("run_tooShort")).toThrow(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { RunId, WaitpointId, SnapshotId, generateKsuidId } from "./friendlyId.js"; | ||
| import { | ||
| ownerEngine, | ||
| classifyResidency, | ||
| classifyKind, | ||
| isClassifiable, | ||
| UnclassifiableRunId, | ||
| } from "./runOpsResidency.js"; | ||
|
|
||
| const SAMPLES = 50_000; // property-scale; CI-fast. (Bump locally toward "millions" for deeper coverage.) | ||
|
|
||
| describe("ownerEngine — residency classifier", () => { | ||
| it("cuid-length ids (default mint) classify LEGACY, friendly + internal", () => { | ||
| for (const util of [RunId, WaitpointId]) { | ||
| const { id, friendlyId } = util.generate(); | ||
| expect(ownerEngine(id)).toBe("LEGACY"); | ||
| expect(ownerEngine(friendlyId)).toBe("LEGACY"); // strips run_/waitpoint_ prefix | ||
| expect(classifyResidency(id)).toBe("LEGACY"); // alias agrees | ||
| expect(classifyKind(id)).toBe("cuid"); | ||
| expect(isClassifiable(id)).toBe(true); | ||
| } | ||
| }); | ||
|
|
||
| it("ksuid-length ids (explicit generateKsuidId) classify NEW, friendly + internal", () => { | ||
| for (const util of [RunId, WaitpointId]) { | ||
| const id = generateKsuidId(); | ||
| const friendlyId = util.toFriendlyId(id); | ||
| expect(ownerEngine(id)).toBe("NEW"); | ||
| expect(ownerEngine(friendlyId)).toBe("NEW"); | ||
| expect(classifyResidency(id)).toBe("NEW"); | ||
| expect(classifyKind(id)).toBe("ksuid"); | ||
| } | ||
| }); | ||
|
|
||
| it("disjointness: no cuid sample is ever NEW, no ksuid sample is ever LEGACY", () => { | ||
| for (let i = 0; i < SAMPLES; i++) { | ||
| expect(ownerEngine(RunId.generate().id)).toBe("LEGACY"); | ||
| expect(ownerEngine(generateKsuidId())).toBe("NEW"); | ||
| } | ||
| }); | ||
|
|
||
| it("throws UnclassifiableRunId on malformed lengths (24, 26, 28, empty)", () => { | ||
| for (const bad of ["", "x".repeat(24), "x".repeat(26), "x".repeat(28), "x".repeat(40)]) { | ||
| expect(() => ownerEngine(bad)).toThrow(UnclassifiableRunId); | ||
| expect(isClassifiable(bad)).toBe(false); | ||
| } | ||
| }); | ||
|
|
||
| it("error carries the offending value + length for diagnostics", () => { | ||
| try { | ||
| ownerEngine("x".repeat(26)); | ||
| throw new Error("should have thrown"); | ||
| } catch (e) { | ||
| expect(e).toBeInstanceOf(UnclassifiableRunId); | ||
| expect((e as UnclassifiableRunId).message).toContain("26"); | ||
| } | ||
| }); | ||
|
|
||
| it("SnapshotId (always cuid) classifies LEGACY — proves snapshot needs no residency key", () => { | ||
| expect(ownerEngine(SnapshotId.generate().id)).toBe("LEGACY"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.