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>
110 lines
4.9 KiB
TypeScript
110 lines
4.9 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
// Module-level db wiring is imported transitively by the service file. The mint
|
|
// helper under test never touches the DB (it is driven with injected deps), so
|
|
// these empty singletons only satisfy the import graph — same boundary pattern
|
|
// as triggerTask.server.test.ts and runEngineBatchTriggerStoreRouting.test.ts.
|
|
vi.mock("~/db.server", () => ({
|
|
prisma: {},
|
|
$replica: {},
|
|
runOpsNewPrisma: {},
|
|
runOpsLegacyPrisma: {},
|
|
runOpsNewReplica: {},
|
|
runOpsLegacyReplica: {},
|
|
}));
|
|
|
|
import { BatchId, generateRunOpsId, ownerEngine, RunId } from "@trigger.dev/core/v3/isomorphic";
|
|
import type { AuthenticatedEnvironment } from "~/services/apiAuth.server";
|
|
import { BatchTriggerV3Service } from "~/v3/services/batchTriggerV3.server";
|
|
|
|
vi.setConfig({ testTimeout: 60_000 });
|
|
|
|
const CUID_LEN = 25;
|
|
const RUN_OPS_ID_LEN = 26;
|
|
|
|
// Minimal AuthenticatedEnvironment — only the fields the mint path reads
|
|
// (organizationId, id, organization.featureFlags) need to be real. A root batch
|
|
// (no parentRunId) with no run-ops id override mints cuid, which is the env-default
|
|
// branch we assert on below.
|
|
function fakeEnv(): AuthenticatedEnvironment {
|
|
return {
|
|
id: "env_123",
|
|
organizationId: "org_123",
|
|
organization: { featureFlags: {} },
|
|
} as unknown as AuthenticatedEnvironment;
|
|
}
|
|
|
|
// Build the service with resolveMintKind forced to "cuid" (its production default
|
|
// when split is off / org not cut over), proving the CHILD branch overrides the env
|
|
// default purely from the parent's id-shape.
|
|
function buildService() {
|
|
return new BatchTriggerV3Service(undefined, undefined, {} as any, {} as any, async () => "cuid");
|
|
}
|
|
|
|
describe("BatchTriggerV3Service child-residency inheritance", () => {
|
|
it("a run-ops parent yields run-ops id (NEW) child friendlyIds", async () => {
|
|
const service = buildService();
|
|
const parentFriendlyId = RunId.toFriendlyId(
|
|
// v1 internal id (version "1" at index 25) → NEW residency parent
|
|
"a".repeat(RUN_OPS_ID_LEN - 1) + "1"
|
|
);
|
|
expect(ownerEngine(RunId.fromFriendlyId(parentFriendlyId))).toBe("NEW");
|
|
|
|
const childFriendlyId = await (service as any).mintChildFriendlyId(fakeEnv(), parentFriendlyId);
|
|
|
|
expect(RunId.fromFriendlyId(childFriendlyId).length).toBe(RUN_OPS_ID_LEN);
|
|
expect(ownerEngine(RunId.fromFriendlyId(childFriendlyId))).toBe("NEW");
|
|
});
|
|
|
|
it("a cuid parent yields cuid (LEGACY) child friendlyIds", async () => {
|
|
const service = buildService();
|
|
const parentFriendlyId = RunId.generate().friendlyId; // cuid (25) → LEGACY parent
|
|
expect(ownerEngine(RunId.fromFriendlyId(parentFriendlyId))).toBe("LEGACY");
|
|
|
|
const childFriendlyId = await (service as any).mintChildFriendlyId(fakeEnv(), parentFriendlyId);
|
|
|
|
expect(RunId.fromFriendlyId(childFriendlyId).length).toBe(CUID_LEN);
|
|
expect(ownerEngine(RunId.fromFriendlyId(childFriendlyId))).toBe("LEGACY");
|
|
});
|
|
|
|
it("a ROOT batch (no parentRunId) mints by the env setting (cuid default here)", async () => {
|
|
const service = buildService();
|
|
const childFriendlyId = await (service as any).mintChildFriendlyId(fakeEnv(), undefined);
|
|
expect(RunId.fromFriendlyId(childFriendlyId).length).toBe(CUID_LEN);
|
|
expect(ownerEngine(RunId.fromFriendlyId(childFriendlyId))).toBe("LEGACY");
|
|
});
|
|
|
|
// A root batch's children are anchored to the batch's friendlyId, NOT to a
|
|
// re-resolution of the per-org flag. Even with the env flag forced to "cuid" (a flip
|
|
// away from the batch's residency), a run-ops batch anchor yields run-ops children — so
|
|
// batch + children stay co-resident and TaskRun.batchId never crosses the seam.
|
|
it("a run-ops batch anchor yields run-ops children even when the env flag resolves cuid", async () => {
|
|
const service = buildService(); // resolveMintKind forced to "cuid"
|
|
const batchFriendlyId = BatchId.toFriendlyId(generateRunOpsId()); // run-ops id (NEW) batch
|
|
expect(ownerEngine(batchFriendlyId)).toBe("NEW");
|
|
|
|
const childFriendlyId = await (service as any).mintChildFriendlyId(fakeEnv(), batchFriendlyId);
|
|
|
|
expect(RunId.fromFriendlyId(childFriendlyId).length).toBe(RUN_OPS_ID_LEN);
|
|
expect(ownerEngine(RunId.fromFriendlyId(childFriendlyId))).toBe("NEW");
|
|
});
|
|
|
|
// The cuid mirror: a cuid batch anchor yields cuid children even if the flag flipped ON.
|
|
it("a cuid batch anchor yields cuid children even when the env flag resolves 'runOpsId'", async () => {
|
|
const service = new BatchTriggerV3Service(
|
|
undefined,
|
|
undefined,
|
|
{} as any,
|
|
{} as any,
|
|
async () => "runOpsId" // env flag flipped ON mid-batch
|
|
);
|
|
const batchFriendlyId = BatchId.generate().friendlyId; // cuid (LEGACY) batch
|
|
expect(ownerEngine(batchFriendlyId)).toBe("LEGACY");
|
|
|
|
const childFriendlyId = await (service as any).mintChildFriendlyId(fakeEnv(), batchFriendlyId);
|
|
|
|
expect(RunId.fromFriendlyId(childFriendlyId).length).toBe(CUID_LEN);
|
|
expect(ownerEngine(RunId.fromFriendlyId(childFriendlyId))).toBe("LEGACY");
|
|
});
|
|
});
|