Skip to content

An open-source, faster Devin alternative

TypeScript

cli: the idempotency header names the operation, never the request body

Igor Zalutski181467c767c8
diff --git a/cli/src/api.test.ts b/cli/src/api.test.ts
index 8cca67d5..a91dbbfe 100644
--- a/cli/src/api.test.ts
+++ b/cli/src/api.test.ts
@@ -3,12 +3,16 @@ import test from "node:test";
import { APIError, OpenComputerClient } from "./api.js";
-test("mutations derive stable, operation-specific idempotency headers", async (context) => {
+// Review reproduction (U6): the header names the operation the caller's key
+// stands for, not the request it was sent with. The body is the backend's
+// to compare; hashing it here turned a retry with different inputs into a
+// second resource instead of the conflict the key promises.
+test("mutations derive idempotency headers from the caller's key and the target only", async (context) => {
const requests: Request[] = [];
context.mock.method(globalThis, "fetch", async (input: string | URL | Request, init?: RequestInit) => {
const request = new Request(input, init);
requests.push(request);
- return Response.json({ id: "project", agents: [], environments: [] });
+ return Response.json({ id: "project", agents: [], environments: [], session: { id: "ses" } });
});
const client = new OpenComputerClient(
{ apiUrl: "https://app.opencomputer.dev", apiKey: "test" },
@@ -18,11 +22,15 @@ test("mutations derive stable, operation-specific idempotency headers", async (c
await client.createProject("Agent", "agent");
await client.createProject("Agent", "agent");
await client.createProject("Different", "different");
+ await client.createSession("muse@development");
+ await client.projects();
const keys = requests.map((request) => request.headers.get("idempotency-key"));
assert.ok(keys[0]);
assert.equal(keys[0], keys[1]);
- assert.notEqual(keys[0], keys[2]);
+ assert.equal(keys[0], keys[2], "a different body is the same operation");
+ assert.notEqual(keys[0], keys[3], "a different target is a different operation");
+ assert.equal(keys[4], null, "reads carry no key");
assert.equal(keys[0]?.includes("retry-42"), false);
});
diff --git a/cli/src/api.ts b/cli/src/api.ts
index d36643c5..f075ef9f 100644
--- a/cli/src/api.ts
+++ b/cli/src/api.ts
@@ -356,6 +356,10 @@ export class OpenComputerClient {
headers.set("x-api-key", this.config.apiKey);
}
const method = (init.method ?? "GET").toUpperCase();
+ // The caller's key names an operation on a target; the body is what the
+ // backend compares under that key. Hashing the body in would make a
+ // retry with different inputs a new operation instead of the conflict
+ // the key promises.
if (this.idempotencyKey && method !== "GET" && method !== "HEAD") {
headers.set(
"idempotency-key",
@@ -365,8 +369,6 @@ export class OpenComputerClient {
.update(method)
.update("\0")
.update(path)
- .update("\0")
- .update(typeof init.body === "string" ? init.body : "")
.digest("hex"),
);
}
diff --git a/cli/src/memory-commands.test.ts b/cli/src/memory-commands.test.ts
index f8d85a6c..452c9892 100644
--- a/cli/src/memory-commands.test.ts
+++ b/cli/src/memory-commands.test.ts
@@ -342,6 +342,53 @@ test("--create-document creates missing bound documents, keeps existing ones, an
);
});
+// Review reproduction (U6): a backend keyed like the real one, by account
+// and Idempotency-Key header, comparing the inputs it stored under it.
+test("one caller key with different bindings is a conflict, not a second session", async (context) => {
+ const receipts = new Map<string, { body: string; id: string }>();
+ let created = 0;
+ context.mock.method(globalThis, "fetch", async (input: string | URL | Request, init?: RequestInit) => {
+ const request = new Request(input, init);
+ assert.equal(route(request), "/api/managed-agents/sessions");
+ const key = request.headers.get("idempotency-key");
+ assert.ok(key, "the CLI sends the header");
+ const body = await request.text();
+ const receipt = receipts.get(key);
+ if (receipt && receipt.body !== body) {
+ return Response.json(
+ { error: { code: "idempotency_conflict", message: "Idempotency-Key was already used with different session input" } },
+ { status: 409 },
+ );
+ }
+ if (receipt) {
+ return Response.json({ session: { id: receipt.id, status: "idle" } }, { status: 200 });
+ }
+ created += 1;
+ const id = `ses-${String(created)}`;
+ receipts.set(key, { body, id });
+ return Response.json({ session: { id, status: "connecting" } }, { status: 201 });
+ });
+ const client = new OpenComputerClient(config, "topic/workshop/1");
+ const workshop = { topics: { scope: "document" as const, id: "workshop", access: "read-write" as const } };
+ const other = { topics: { scope: "document" as const, id: "other", access: "read-write" as const } };
+
+ const first = await createSessionWithMemory(client, "muse@development", workshop);
+ assert.equal(first.created, true);
+ const replayed = await createSessionWithMemory(client, "muse@development", workshop);
+ assert.equal(replayed.created, false);
+ assert.equal(replayed.session.id, first.session.id);
+ await assert.rejects(
+ createSessionWithMemory(client, "muse@development", other),
+ (error: unknown) => error instanceof CLIError && error.code === "session_idempotency_conflict",
+ );
+ await assert.rejects(
+ createSessionWithMemory(client, "muse@development"),
+ (error: unknown) => error instanceof CLIError && error.code === "session_idempotency_conflict",
+ );
+ assert.equal(created, 1);
+ assert.equal(receipts.size, 1);
+});
+
test("session create sends bindings, reports replay, and names a reused key's conflict", async (context) => {
const bodies: Array<Record<string, unknown>> = [];
let status = 201;