diff --git a/packages/core/src/v3/idempotencyKeys.test.ts b/packages/core/src/v3/idempotencyKeys.test.ts index 8749e1bc2c0..34bf6866812 100644 --- a/packages/core/src/v3/idempotencyKeys.test.ts +++ b/packages/core/src/v3/idempotencyKeys.test.ts @@ -9,7 +9,9 @@ import { resetIdempotencyKey, resetIdempotencyKeyCatalog, } from "./idempotencyKeys.js"; +import { taskContext } from "./task-context-api.js"; import { digestSHA256 } from "./utils/crypto.js"; +import { unregisterGlobal } from "./utils/globals.js"; describe("idempotencyKeys metadata retention", () => { it("retains key/scope options for every key created in a run, even beyond 1000", async () => { @@ -47,6 +49,49 @@ describe("idempotencyKeys metadata retention", () => { }); }); +describe("makeIdempotencyKey", () => { + const digestShapedKey = "a".repeat(64); + const parentRunId = "run_parent_a"; + + beforeEach(() => { + resetIdempotencyKeyCatalog(); + taskContext.setGlobalTaskContext({ + ctx: { run: { id: parentRunId }, attempt: { number: 1 } }, + worker: {}, + } as never); + }); + + afterEach(() => { + unregisterGlobal("task-context"); + resetIdempotencyKeyCatalog(); + }); + + it("hashes a raw 64-character string with the parent run ID", async () => { + // Catches: a raw 64-char digest being treated as an already-hashed global key. + const key = await makeIdempotencyKey(digestShapedKey); + + expect(key).toBe(await digestSHA256(`${digestShapedKey}-${parentRunId}`)); + expect(key).not.toBe(digestShapedKey); + expect(getIdempotencyKeyOptions(key!)).toEqual({ key: digestShapedKey, scope: "run" }); + }); + + it("keeps a global key created with idempotencyKeys.create() global", async () => { + // Catches: a created global key being re-scoped to the parent run. + const created = await createIdempotencyKey("my-key", { scope: "global" }); + + expect(await makeIdempotencyKey(created)).toBe(created); + expect(created).toBe(await digestSHA256("my-key")); + }); + + it("keeps a run-scoped key created with idempotencyKeys.create() run-scoped", async () => { + // Catches: a created run key being re-hashed with the parent run ID a second time. + const created = await createIdempotencyKey("my-key", { scope: "run" }); + + expect(await makeIdempotencyKey(created)).toBe(created); + expect(created).toBe(await digestSHA256(`my-key-${parentRunId}`)); + }); +}); + describe("resetIdempotencyKey", () => { const digestShapedKey = "a".repeat(64); @@ -174,9 +219,8 @@ describe("resetIdempotencyKey", () => { expect(await resetAndCaptureKey("my-task", digestShapedKey)).toBe(digestShapedKey); }); - it("resets 64-character material that trigger stored verbatim when no scope is passed", async () => { - // trigger() forwards 64-character material as-is, so that is what the server stored - expect(await makeIdempotencyKey(digestShapedKey)).toBe(digestShapedKey); + it("resets 64-character material that was stored verbatim when no scope is passed", async () => { + // Older clients forwarded 64-character material as-is, so that is what the server stored existingKeys = new Set([digestShapedKey]); expect(await resetAndCaptureKey("my-task", digestShapedKey)).toBe(digestShapedKey); diff --git a/packages/core/src/v3/idempotencyKeys.ts b/packages/core/src/v3/idempotencyKeys.ts index 97a21aa9d6e..520622e573a 100644 --- a/packages/core/src/v3/idempotencyKeys.ts +++ b/packages/core/src/v3/idempotencyKeys.ts @@ -55,8 +55,9 @@ export function isIdempotencyKey( ): value is IdempotencyKey { // Cannot check the brand at runtime because it doesn't exist (it's a TypeScript-only construct) // Check for primitive strings only (we no longer use String objects) - if (typeof value === "string") { - return value.length === 64; + // A 64-char key is only assumed pre-hashed if the catalog knows it + if (typeof value === "string" && value.length === 64) { + return getIdempotencyKeyOptions(value) !== undefined; } return false; }