Skip to content

Commit 3bc28f5

Browse files
d-csclaude
andcommitted
refactor(core): remove test-only ksuid-mint process-global from friendlyId
Production mints ksuid runs explicitly via generateKsuidId() at gated trigger sites; the mutable process-global only ever let tests puppet RunId/WaitpointId.generate() into ksuids and cannot express per-org behavior. Delete setKsuidMintEnabled/isKsuidMintEnabled and the flag branch; generateInternalId() and IdUtil now always mint cuid. Tests drive the ksuid path via generateKsuidId() directly, preserving the 25-vs-27 disjoint-length, k-sortability, uniqueness, and payload round-trip coverage. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent eaf82d3 commit 3bc28f5

3 files changed

Lines changed: 23 additions & 65 deletions

File tree

packages/core/src/v3/isomorphic/friendlyId.test.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import {
44
WaitpointId,
55
SnapshotId,
66
QueueId,
7-
setKsuidMintEnabled,
8-
isKsuidMintEnabled,
97
generateKsuidId,
108
decodeKsuid,
119
KSUID_PAYLOAD_BYTES,
@@ -14,12 +12,8 @@ import {
1412
const CUID_LEN = 25;
1513
const KSUID_LEN = 27;
1614

17-
afterEach(() => setKsuidMintEnabled(false)); // never leak flag state across tests
18-
19-
describe("KSUID mint (flag-gated) for RunId + WaitpointId", () => {
20-
it("flag OFF: run + waitpoint mint cuid (25) and round-trip", () => {
21-
setKsuidMintEnabled(false);
22-
expect(isKsuidMintEnabled()).toBe(false);
15+
describe("RunId + WaitpointId mint cuid by default; ksuid via generateKsuidId", () => {
16+
it("default: run + waitpoint mint cuid (25) and round-trip", () => {
2317
for (const util of [RunId, WaitpointId]) {
2418
const { id, friendlyId } = util.generate();
2519
expect(id.length).toBe(CUID_LEN);
@@ -30,37 +24,27 @@ describe("KSUID mint (flag-gated) for RunId + WaitpointId", () => {
3024
}
3125
});
3226

33-
it("flag ON: run + waitpoint mint 27-char and round-trip", () => {
34-
setKsuidMintEnabled(true);
35-
expect(isKsuidMintEnabled()).toBe(true);
27+
it("explicit ksuid: a run/waitpoint friendlyId over generateKsuidId() is 27-char and round-trips", () => {
3628
for (const util of [RunId, WaitpointId]) {
37-
const { id, friendlyId } = util.generate();
29+
const id = generateKsuidId();
30+
const friendlyId = util.toFriendlyId(id);
3831
expect(id.length).toBe(KSUID_LEN);
3932
expect(util.fromFriendlyId(friendlyId)).toBe(id);
4033
expect(util.toId(friendlyId)).toBe(id);
4134
expect(util.toId(id)).toBe(id);
42-
expect(util.toFriendlyId(id)).toBe(friendlyId);
4335
}
4436
});
4537

46-
it("flag ON: SnapshotId stays cuid (25) — never 27-char", () => {
47-
setKsuidMintEnabled(true);
48-
const { id } = SnapshotId.generate();
49-
expect(id.length).toBe(CUID_LEN);
50-
});
51-
52-
it("flag ON: a non-run/waitpoint entity stays cuid (25)", () => {
53-
setKsuidMintEnabled(true);
38+
it("SnapshotId + QueueId stay cuid (25)", () => {
39+
expect(SnapshotId.generate().id.length).toBe(CUID_LEN);
5440
expect(QueueId.generate().id.length).toBe(CUID_LEN);
5541
});
5642

57-
it("disjoint lengths: 27 (new) vs 25 (cuid) — the classifier margin", () => {
58-
setKsuidMintEnabled(true);
59-
expect(RunId.generate().id.length).not.toBe(SnapshotId.generate().id.length);
43+
it("disjoint lengths: 27 (ksuid) vs 25 (cuid) — the classifier margin", () => {
44+
expect(generateKsuidId().length).not.toBe(SnapshotId.generate().id.length);
6045
});
6146

62-
it("generateKsuidId() is directly callable, independent of the flag", () => {
63-
setKsuidMintEnabled(false); // must NOT be gated behind the global flag
47+
it("generateKsuidId() is directly callable and yields 27 chars", () => {
6448
expect(generateKsuidId().length).toBe(KSUID_LEN);
6549
});
6650
});

packages/core/src/v3/isomorphic/friendlyId.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,6 @@ export function generateFriendlyId(prefix: string, size?: number) {
77
return `${prefix}_${idGenerator(size)}`;
88
}
99

10-
// Injected by the server split-mode bootstrap. Default false =
11-
// today's behavior (cuid). Isomorphic file: never read process.env here.
12-
let ksuidMintEnabled = false;
13-
14-
/** Server bootstrap calls this once with isSplitEnabled(). Off by default. */
15-
export function setKsuidMintEnabled(enabled: boolean): void {
16-
ksuidMintEnabled = enabled;
17-
}
18-
19-
/** Test/diagnostic read-back of the current mint mode. */
20-
export function isKsuidMintEnabled(): boolean {
21-
return ksuidMintEnabled;
22-
}
23-
2410
// KSUID epoch (2014-05-13T16:53:20Z) — seconds offset applied to the unix timestamp.
2511
const KSUID_EPOCH = 1_400_000_000;
2612
const KSUID_TIMESTAMP_BYTES = 4;
@@ -136,10 +122,7 @@ export function decodeKsuid(idOrFriendlyId: string): DecodedKsuid {
136122
};
137123
}
138124

139-
export function generateInternalId(useKsuidWhenEnabled = false): string {
140-
if (useKsuidWhenEnabled && ksuidMintEnabled) {
141-
return generateKsuidId();
142-
}
125+
export function generateInternalId(): string {
143126
return cuid();
144127
}
145128

@@ -190,13 +173,10 @@ export function fromFriendlyId(friendlyId: string, expectedEntityName?: string):
190173
}
191174

192175
export class IdUtil {
193-
constructor(
194-
private entityName: string,
195-
private ksuidWhenEnabled = false
196-
) {}
176+
constructor(private entityName: string) {}
197177

198178
generate() {
199-
const internalId = generateInternalId(this.ksuidWhenEnabled);
179+
const internalId = generateInternalId();
200180

201181
return {
202182
id: internalId,
@@ -225,9 +205,9 @@ export class IdUtil {
225205
export const BackgroundWorkerId = new IdUtil("worker");
226206
export const CheckpointId = new IdUtil("checkpoint");
227207
export const QueueId = new IdUtil("queue");
228-
export const RunId = new IdUtil("run", true);
208+
export const RunId = new IdUtil("run");
229209
export const SnapshotId = new IdUtil("snapshot");
230-
export const WaitpointId = new IdUtil("waitpoint", true);
210+
export const WaitpointId = new IdUtil("waitpoint");
231211
export const BatchId = new IdUtil("batch");
232212
export const BulkActionId = new IdUtil("bulk");
233213
export const AttemptId = new IdUtil("attempt");

packages/core/src/v3/isomorphic/runOpsResidency.test.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { afterEach, describe, expect, it } from "vitest";
2-
import { RunId, WaitpointId, SnapshotId, setKsuidMintEnabled } from "./friendlyId.js";
1+
import { describe, expect, it } from "vitest";
2+
import { RunId, WaitpointId, SnapshotId, generateKsuidId } from "./friendlyId.js";
33
import {
44
ownerEngine,
55
classifyResidency,
@@ -8,13 +8,10 @@ import {
88
UnclassifiableRunId,
99
} from "./runOpsResidency.js";
1010

11-
afterEach(() => setKsuidMintEnabled(false)); // never leak mint-flag state across tests
12-
1311
const SAMPLES = 50_000; // property-scale; CI-fast. (Bump locally toward "millions" for deeper coverage.)
1412

1513
describe("ownerEngine — residency classifier", () => {
16-
it("cuid-length ids (mint flag OFF) classify LEGACY, friendly + internal", () => {
17-
setKsuidMintEnabled(false);
14+
it("cuid-length ids (default mint) classify LEGACY, friendly + internal", () => {
1815
for (const util of [RunId, WaitpointId]) {
1916
const { id, friendlyId } = util.generate();
2017
expect(ownerEngine(id)).toBe("LEGACY");
@@ -25,10 +22,10 @@ describe("ownerEngine — residency classifier", () => {
2522
}
2623
});
2724

28-
it("ksuid-length ids (mint flag ON) classify NEW, friendly + internal", () => {
29-
setKsuidMintEnabled(true);
25+
it("ksuid-length ids (explicit generateKsuidId) classify NEW, friendly + internal", () => {
3026
for (const util of [RunId, WaitpointId]) {
31-
const { id, friendlyId } = util.generate();
27+
const id = generateKsuidId();
28+
const friendlyId = util.toFriendlyId(id);
3229
expect(ownerEngine(id)).toBe("NEW");
3330
expect(ownerEngine(friendlyId)).toBe("NEW");
3431
expect(classifyResidency(id)).toBe("NEW");
@@ -38,10 +35,8 @@ describe("ownerEngine — residency classifier", () => {
3835

3936
it("disjointness: no cuid sample is ever NEW, no ksuid sample is ever LEGACY", () => {
4037
for (let i = 0; i < SAMPLES; i++) {
41-
setKsuidMintEnabled(false);
4238
expect(ownerEngine(RunId.generate().id)).toBe("LEGACY");
43-
setKsuidMintEnabled(true);
44-
expect(ownerEngine(RunId.generate().id)).toBe("NEW");
39+
expect(ownerEngine(generateKsuidId())).toBe("NEW");
4540
}
4641
});
4742

@@ -61,8 +56,7 @@ describe("ownerEngine — residency classifier", () => {
6156
}
6257
});
6358

64-
it("SnapshotId (always cuid, even with flag ON) classifies LEGACY — proves snapshot needs no residency key", () => {
65-
setKsuidMintEnabled(true);
59+
it("SnapshotId (always cuid) classifies LEGACY — proves snapshot needs no residency key", () => {
6660
expect(ownerEngine(SnapshotId.generate().id)).toBe("LEGACY");
6761
});
6862
});

0 commit comments

Comments
 (0)