Files
9router/tests/unit/antigravity-retry-hook.test.js
Sutarto Jordan Chrisfivo 639f1204d0 fix(antigravity): retry transient upstream failures
Retry short-lived 5xx/capacity errors (500/502/503/504 + message
patterns) with bounded backoff capped at 15s; honor Retry-After/reset
hints and skip when wait is too long. Keep 400 non-retryable. Enable the
retry hook for 500 alongside existing 429/503.

Deduplicate sanitized Antigravity tool names before emitting the single
functionDeclarations group to avoid upstream "Tool names must be unique"
rejections.

Add Headroom size diagnostics and phantom-savings warning when reported
token delta does not shrink the outbound payload.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-26 17:19:46 +07:00

75 lines
3.0 KiB
JavaScript

// Guards D3: antigravity 429/503 retry merged into base via computeRetryDelay hook.
import { describe, it, expect } from "vitest";
import { AntigravityExecutor } from "../../open-sse/executors/antigravity.js";
const MAX = 10000;
function res(status, headers = {}, body = null) {
return {
status,
headers: { get: (k) => headers[k.toLowerCase()] ?? null },
clone: () => ({ text: async () => (body == null ? "" : JSON.stringify(body)) }),
};
}
describe("antigravity computeRetryDelay hook (D3)", () => {
const ag = new AntigravityExecutor();
it("uses Retry-After header (seconds → ms) when within cap", async () => {
expect(await ag.computeRetryDelay(res(429, { "retry-after": "5" }), 1)).toBe(5000);
});
it("vetoes (false) when Retry-After exceeds cap", async () => {
expect(await ag.computeRetryDelay(res(429, { "retry-after": "60" }), 1)).toBe(false);
});
it("parses retry time from error body when no header", async () => {
const r = res(429, {}, { error: { message: "quota will reset after 3s" } });
expect(await ag.computeRetryDelay(r, 1)).toBe(3000);
});
it("exponential backoff for 429 when no retry info", async () => {
expect(await ag.computeRetryDelay(res(429), 1)).toBe(Math.min(1000 * 2 ** 1, MAX));
expect(await ag.computeRetryDelay(res(429), 3)).toBe(Math.min(1000 * 2 ** 3, MAX));
});
it("503 without retry info → transient backoff", async () => {
expect(await ag.computeRetryDelay(res(503), 1)).toBe(2000);
});
it("retries Antigravity agent terminated body even when status is not 429", async () => {
const r = res(500, {}, { error: { message: "Agent execution terminated due to error" } });
expect(await ag.computeRetryDelay(r, 1)).toBe(2000);
});
it("retries high traffic body", async () => {
const r = res(500, {}, { error: { message: "Our servers are experiencing high traffic" } });
expect(await ag.computeRetryDelay(r, 2)).toBe(4000);
});
it("does not retry non-transient 400 errors", async () => {
const r = res(400, {}, { error: { message: "Invalid request" } });
expect(await ag.computeRetryDelay(r, 1)).toBe(false);
});
it("deduplicates sanitized tool names", () => {
const out = ag.transformRequest("claude-opus-4-6-thinking", {
request: {
contents: [{ role: "user", parts: [{ text: "hi" }] }],
tools: [{ functionDeclarations: [
{ name: "read/file", parameters: { type: "object", properties: {} } },
{ name: "read file", parameters: { type: "object", properties: {} } },
{ name: "read/file", parameters: { type: "object", properties: {} } },
] }],
},
}, true, { projectId: "project-1", connectionId: "conn-1" });
expect(out.request.tools[0].functionDeclarations.map(fn => fn.name)).toEqual(["read_file"]);
});
it("buildHeaders includes cached session id after transformRequest", () => {
ag._lastSessionId = "sess-123";
const h = ag.buildHeaders({ accessToken: "tok" }, true);
expect(h["X-Machine-Session-Id"]).toBe("sess-123");
});
});