092b9ef07a
## Problem
The run-ops split mints NEW-store run ids as **27-char base62 KSUIDs**.
The supervisor writes the run id into the Kubernetes pod name
(`runner-<id>`), and pod names must be DNS-1123 labels (lowercase
`[a-z0-9-]`) — so uppercase base62 ids make k8s reject the pod (422) and
**those runs never launch** (they loop in `PENDING_EXECUTING` until the
heartbeat-stall handler nacks them, forever). `.toLowerCase()` can't fix
it: base62 has both `A`(10) and `a`(36) as distinct symbols, so folding
collides distinct ids and destroys sort order.
## Fix: change the encoding, not the structure
Mint a **26-char lowercase base32hex** run id:
```
run_<24-char base32hex core><region char><version char>
[ 6-byte ms timestamp ][ 9 CSPRNG bytes ]
```
- **base32hex** (RFC 4648 §7, alphabet `0-9a-v`): lowercase,
order-preserving, DNS-safe; 15 bytes → exactly 24 chars, no padding.
Hand-rolled encode/decode (no new dependency).
- **48-bit ms timestamp** in the leading bytes → plain string sort ==
creation order at millisecond resolution.
- **72 bits CSPRNG** entropy; PK unique constraint is the backstop (no
retry loop).
- **region / version** are raw positional chars (read via one `charAt`
before decoding/routing), version = `"1"`.
DNS-safe from birth and hyphen-free, so **firekeeper is unchanged** —
`runner-<id>-attempt-N` → strip `runner-`, cut at first hyphen still
recovers the exact id incl. region+version.
## Residency discriminator: length → version char
`classifyKind`/`classifyResidency` (`runOpsResidency.ts`) previously
distinguished NEW vs LEGACY by **id length**. That gets ambiguous with a
third format. It now discriminates on the **version char at a fixed
position** (`isRunOpsIdBody`: 26 chars, `[25] === "1"`, base32hex
alphabet) → NEW; everything else → LEGACY. Total, never throws. The
`Residency` (NEW/LEGACY) contract the routing store consumes is
unchanged; the `"ksuid"` `ResidencyKind` label is retained only because
it's the persisted `runOpsMintKsuid` feature-flag value.
## Scope / verification
- Generator + discriminator in `@trigger.dev/core` isomorphic; mint path
+ all id-shape call sites swept (~40 webapp files); changeset added
(`@trigger.dev/core` patch).
- Core unit tests (encode/decode round-trip + property, generator shape,
ms sort-order incl. intra-second, parse partitioned-vs-legacy,
firekeeper round-trip): **24 pass**. `@trigger.dev/core` builds; webapp
typechecks; format/lint clean.
## Open decisions (flagged, not silently chosen)
1. **Backward-compat**: existing 27-char base62 KSUID runs now classify
LEGACY. On test cloud these are the broken/looping runs that never
completed, so this is acceptable — but worth a conscious call before
prod. No transitional length-recognition added (keeps the discriminator
clean).
2. **Storage collation**: the sort guarantee is byte-order — if the
run-ops id column is `TEXT` with default locale collation it's silently
not honored. Confirm whether `COLLATE "C"` / `BYTEA` is needed on the
run-ops schema.
3. **Region sourcing** wiring — see `regionCharForRegion` /
`REGION_CODES`.
---
## ⚠️ Required migration — deploy in lockstep
This PR renames a persisted feature-flag key/value and an env var. These
are **not** changed by the code alone and must be migrated when this
deploys, or affected orgs silently fall back to `cuid` minting (no crash
— `defaultValue: "cuid"`):
1. **Env var** (terraform): `RUN_OPS_MINT_KSUID_ENABLED` →
`RUN_OPS_MINT_ENABLED` (carry the value over).
2. **DB** `organization.featureFlags`: migrate both the key and value
together:
- key `runOpsMintKsuid` → `runOpsMintKind`
- value `"ksuid"` → `"runOpsId"`
Until an org's flag row is migrated, its `runOpsMintKind` lookup misses
and it mints `cuid` (legacy) — so no NEW-store ids for that org until
the data lands.
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
87 lines
3.7 KiB
TypeScript
87 lines
3.7 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
BatchId,
|
|
generateFriendlyId,
|
|
generateRunOpsId,
|
|
RunId,
|
|
} from "@trigger.dev/core/v3/isomorphic";
|
|
import { isValidFriendlyId, makeFriendlyIdValidator } from "./friendlyId";
|
|
|
|
describe("isValidFriendlyId", () => {
|
|
it("accepts every id generation the real generators produce", () => {
|
|
// nanoid (legacy V1), cuid (run-engine), run-ops v1 (run-ops split)
|
|
expect(isValidFriendlyId(generateFriendlyId("run"), "run")).toBe(true);
|
|
expect(isValidFriendlyId(RunId.generate().friendlyId, "run")).toBe(true);
|
|
expect(isValidFriendlyId(RunId.toFriendlyId(generateRunOpsId()), "run")).toBe(true);
|
|
|
|
expect(isValidFriendlyId(generateFriendlyId("batch"), "batch")).toBe(true);
|
|
expect(isValidFriendlyId(BatchId.generate().friendlyId, "batch")).toBe(true);
|
|
expect(isValidFriendlyId(BatchId.toFriendlyId(generateRunOpsId()), "batch")).toBe(true);
|
|
});
|
|
|
|
it("accepts each valid body length (21 nanoid, 25 cuid, 26 run-ops v1, 27 legacy base62)", () => {
|
|
expect(isValidFriendlyId("run_" + "a".repeat(21), "run")).toBe(true);
|
|
expect(isValidFriendlyId("run_" + "a".repeat(25), "run")).toBe(true);
|
|
expect(isValidFriendlyId("run_" + "a".repeat(26), "run")).toBe(true);
|
|
expect(isValidFriendlyId("run_" + "a".repeat(27), "run")).toBe(true);
|
|
});
|
|
|
|
it("accepts mixed-case (uppercase) legacy base62 bodies", () => {
|
|
expect(isValidFriendlyId("run_2ABCdefGHI0123456789jklMN", "run")).toBe(true);
|
|
});
|
|
|
|
it("rejects the wrong prefix", () => {
|
|
expect(isValidFriendlyId(RunId.generate().friendlyId, "batch")).toBe(false);
|
|
expect(isValidFriendlyId("batch_" + "a".repeat(25), "run")).toBe(false);
|
|
});
|
|
|
|
it("rejects a bare (unprefixed) id", () => {
|
|
expect(isValidFriendlyId("a".repeat(25), "run")).toBe(false);
|
|
});
|
|
|
|
it("rejects body lengths that match no generator", () => {
|
|
for (const len of [0, 20, 22, 24, 28]) {
|
|
expect(isValidFriendlyId("run_" + "a".repeat(len), "run")).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("rejects non-base62 characters in the body", () => {
|
|
expect(isValidFriendlyId("run_" + "-".repeat(25), "run")).toBe(false);
|
|
expect(isValidFriendlyId("run_" + "!".repeat(25), "run")).toBe(false);
|
|
// an underscore in the body is not base62
|
|
expect(isValidFriendlyId("run_" + "a".repeat(24) + "_", "run")).toBe(false);
|
|
});
|
|
|
|
it("does not treat the prefix separator as optional", () => {
|
|
// "runX..." shares the "run" prefix but not the "run_" marker
|
|
expect(isValidFriendlyId("run" + "a".repeat(25), "run")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("makeFriendlyIdValidator", () => {
|
|
const validateRunId = makeFriendlyIdValidator("run", "Run");
|
|
const validateBatchId = makeFriendlyIdValidator("batch", "Batch");
|
|
|
|
it("returns undefined for a valid id of any generation", () => {
|
|
expect(validateRunId(generateFriendlyId("run"))).toBeUndefined();
|
|
expect(validateRunId(RunId.generate().friendlyId)).toBeUndefined();
|
|
expect(validateRunId(RunId.toFriendlyId(generateRunOpsId()))).toBeUndefined();
|
|
expect(validateBatchId(BatchId.toFriendlyId(generateRunOpsId()))).toBeUndefined();
|
|
});
|
|
|
|
it("reports a wrong prefix distinctly from a wrong shape", () => {
|
|
expect(validateRunId("batch_" + "a".repeat(25))).toBe("Run IDs start with 'run_'");
|
|
expect(validateRunId("run_" + "a".repeat(20))).toBe("That doesn't look like a valid run ID");
|
|
});
|
|
|
|
it("derives the marker and label per entity", () => {
|
|
const validateWaitpointId = makeFriendlyIdValidator("waitpoint", "Waitpoint");
|
|
expect(validateWaitpointId("run_" + "a".repeat(25))).toBe(
|
|
"Waitpoint IDs start with 'waitpoint_'"
|
|
);
|
|
expect(validateWaitpointId("waitpoint_" + "a".repeat(20))).toBe(
|
|
"That doesn't look like a valid waitpoint ID"
|
|
);
|
|
});
|
|
});
|