|
|
@@ -3,13 +3,21 @@ import type { AddressInfo } from "node:net";
|
|
|
|
|
|
import { streamSimple, type Context, type Model } from "@earendil-works/pi-ai";
|
|
|
import { Type } from "typebox";
|
|
|
-import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
|
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
|
import { compactContext } from "../../src/service/turn-system/compaction/algorithm/compact-context.js";
|
|
|
import { createCheckpointSession } from "../../src/service/turn-system/compaction/execution/checkpoint-provider.js";
|
|
|
|
|
|
const summary =
|
|
|
"The synthetic file was read. Continue with the pending user request.";
|
|
|
+const missingTools =
|
|
|
+ "litellm.UnsupportedParamsError: Anthropic doesn't support tool calling without `tools=` param specified";
|
|
|
+interface Rejection {
|
|
|
+ status: number;
|
|
|
+ message: string;
|
|
|
+ param?: string;
|
|
|
+ code?: string;
|
|
|
+}
|
|
|
|
|
|
function history(model: Model<"openai-completions">): Context["messages"] {
|
|
|
return [
|
|
|
@@ -54,10 +62,14 @@ describe("OpenAI compaction HTTP transport", () => {
|
|
|
let model: Model<"openai-completions">;
|
|
|
let requests: Record<string, unknown>[];
|
|
|
let allowEmptyTools: boolean;
|
|
|
+ let rejectRequest: (body: Record<string, unknown>) => Rejection | undefined;
|
|
|
+ let incompleteStream: boolean;
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
requests = [];
|
|
|
allowEmptyTools = false;
|
|
|
+ rejectRequest = () => undefined;
|
|
|
+ incompleteStream = false;
|
|
|
// Exercise the real SDK serializer and stream parser with a local fixture.
|
|
|
// This models the reported validation rule, not acceptance by a live service.
|
|
|
server = createServer(async (request, response) => {
|
|
|
@@ -65,6 +77,14 @@ describe("OpenAI compaction HTTP transport", () => {
|
|
|
for await (const chunk of request) raw += chunk;
|
|
|
const body = JSON.parse(raw);
|
|
|
requests.push(body);
|
|
|
+ const rejection = rejectRequest(body);
|
|
|
+ if (rejection) {
|
|
|
+ const { status, ...error } = rejection;
|
|
|
+ response
|
|
|
+ .writeHead(status, { "content-type": "application/json" })
|
|
|
+ .end(JSON.stringify({ error }));
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (
|
|
|
!allowEmptyTools &&
|
|
|
Array.isArray(body.tools) &&
|
|
|
@@ -92,6 +112,10 @@ describe("OpenAI compaction HTTP transport", () => {
|
|
|
],
|
|
|
})}\n\n`,
|
|
|
);
|
|
|
+ if (incompleteStream) {
|
|
|
+ response.end();
|
|
|
+ return;
|
|
|
+ }
|
|
|
response.end(
|
|
|
`data: ${JSON.stringify({
|
|
|
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
|
|
|
@@ -129,10 +153,21 @@ describe("OpenAI compaction HTTP transport", () => {
|
|
|
);
|
|
|
});
|
|
|
|
|
|
- it.each(["custom", "openai"])(
|
|
|
- "compacts tool history through the %s provider without empty tools",
|
|
|
- async (provider) => {
|
|
|
+ it.each([
|
|
|
+ { provider: "custom", requiresTools: false },
|
|
|
+ { provider: "openai", requiresTools: false },
|
|
|
+ { provider: "custom", requiresTools: true },
|
|
|
+ ])(
|
|
|
+ "compacts tool history through $provider (backend requires tools: $requiresTools)",
|
|
|
+ async ({ provider, requiresTools }) => {
|
|
|
model = { ...model, provider };
|
|
|
+ if (requiresTools) {
|
|
|
+ allowEmptyTools = true;
|
|
|
+ rejectRequest = (body) =>
|
|
|
+ body.tools === undefined
|
|
|
+ ? { status: 400, message: missingTools }
|
|
|
+ : undefined;
|
|
|
+ }
|
|
|
const result = await compactContext({
|
|
|
history: history(model),
|
|
|
allowLegacyToolTrim: false,
|
|
|
@@ -159,8 +194,10 @@ describe("OpenAI compaction HTTP transport", () => {
|
|
|
}),
|
|
|
},
|
|
|
});
|
|
|
- expect(requests).toHaveLength(1);
|
|
|
+ expect(requests).toHaveLength(requiresTools ? 2 : 1);
|
|
|
expect(requests[0]).not.toHaveProperty("tools");
|
|
|
+ if (requiresTools)
|
|
|
+ expect(requests[1]).toEqual({ ...requests[0], tools: [] });
|
|
|
expect(requests[0]).not.toHaveProperty("tool_choice");
|
|
|
expect(requests[0].messages).toEqual(
|
|
|
expect.arrayContaining([
|
|
|
@@ -200,23 +237,26 @@ describe("OpenAI compaction HTTP transport", () => {
|
|
|
expect(result.stopReason).toBe("stop");
|
|
|
});
|
|
|
|
|
|
- it.each(["explicit", "detected"])(
|
|
|
- "preserves the %s Anthropic proxy workaround even with caching disabled",
|
|
|
- async (mode) => {
|
|
|
+ it.each(["short", "none"] as const)(
|
|
|
+ "honors the independent tools requirement with cache retention %s",
|
|
|
+ async (cacheRetention) => {
|
|
|
allowEmptyTools = true;
|
|
|
- model =
|
|
|
- mode === "explicit"
|
|
|
- ? { ...model, compat: { cacheControlFormat: "anthropic" } }
|
|
|
- : { ...model, provider: "openrouter", id: "anthropic/fixture-model" };
|
|
|
+ model = { ...model, compat: { requiresToolsForToolHistory: true } };
|
|
|
+ rejectRequest = (body) =>
|
|
|
+ body.tools === undefined &&
|
|
|
+ JSON.stringify(body.messages).includes("tool_calls")
|
|
|
+ ? { status: 400, message: missingTools }
|
|
|
+ : undefined;
|
|
|
const result = await streamSimple(
|
|
|
model,
|
|
|
{ messages: history(model) },
|
|
|
{
|
|
|
apiKey: "synthetic-key",
|
|
|
- cacheRetention: "none",
|
|
|
+ cacheRetention,
|
|
|
},
|
|
|
).result();
|
|
|
expect(result.stopReason).toBe("stop");
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
expect(requests[0].tools).toEqual([]);
|
|
|
|
|
|
await streamSimple(
|
|
|
@@ -227,13 +267,238 @@ describe("OpenAI compaction HTTP transport", () => {
|
|
|
},
|
|
|
{
|
|
|
apiKey: "synthetic-key",
|
|
|
- cacheRetention: "none",
|
|
|
+ cacheRetention,
|
|
|
},
|
|
|
).result();
|
|
|
expect(requests[1]).not.toHaveProperty("tools");
|
|
|
},
|
|
|
);
|
|
|
|
|
|
+ it.each(["explicit", "detected"])(
|
|
|
+ "does not infer a tools requirement from %s Anthropic caching",
|
|
|
+ async (mode) => {
|
|
|
+ model =
|
|
|
+ mode === "explicit"
|
|
|
+ ? { ...model, compat: { cacheControlFormat: "anthropic" } }
|
|
|
+ : { ...model, provider: "openrouter", id: "anthropic/fixture-model" };
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ { apiKey: "synthetic-key" },
|
|
|
+ ).result();
|
|
|
+ expect(result.stopReason).toBe("stop");
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
+ expect(requests[0]).not.toHaveProperty("tools");
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
+ it.each(["missing_required_parameter", "missing_required_argument"])(
|
|
|
+ "recovers once from structured %s for tools",
|
|
|
+ async (code) => {
|
|
|
+ allowEmptyTools = true;
|
|
|
+ rejectRequest = (body) =>
|
|
|
+ body.tools === undefined
|
|
|
+ ? {
|
|
|
+ status: 400,
|
|
|
+ message: "Required field missing",
|
|
|
+ param: "tools",
|
|
|
+ code,
|
|
|
+ }
|
|
|
+ : undefined;
|
|
|
+ const onPayload = vi.fn((payload: unknown) => ({
|
|
|
+ ...(payload as Record<string, unknown>),
|
|
|
+ user: "synthetic-user",
|
|
|
+ }));
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ { apiKey: "synthetic-key", onPayload },
|
|
|
+ ).result();
|
|
|
+ expect(result.stopReason).toBe("stop");
|
|
|
+ expect(requests).toHaveLength(2);
|
|
|
+ expect(onPayload).toHaveBeenCalledTimes(2);
|
|
|
+ expect(requests[1]).toEqual({ ...requests[0], tools: [] });
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
+ it("does not retry again if the recovery request also fails", async () => {
|
|
|
+ rejectRequest = () => ({ status: 400, message: missingTools });
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ { apiKey: "synthetic-key" },
|
|
|
+ ).result();
|
|
|
+ expect(requests).toHaveLength(2);
|
|
|
+ expect(requests[1].tools).toEqual([]);
|
|
|
+ expect(result.stopReason).toBe("error");
|
|
|
+ });
|
|
|
+
|
|
|
+ it("disables SDK retries on the recovery request", async () => {
|
|
|
+ rejectRequest = (body) =>
|
|
|
+ body.tools === undefined
|
|
|
+ ? { status: 400, message: missingTools }
|
|
|
+ : { status: 500, message: "Synthetic server failure" };
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ {
|
|
|
+ apiKey: "synthetic-key",
|
|
|
+ maxRetries: 3,
|
|
|
+ },
|
|
|
+ ).result();
|
|
|
+ expect(requests).toHaveLength(2);
|
|
|
+ expect(result.stopReason).toBe("error");
|
|
|
+ });
|
|
|
+
|
|
|
+ it.each([
|
|
|
+ "no history",
|
|
|
+ "nonempty definitions",
|
|
|
+ "explicit true",
|
|
|
+ "hook tools",
|
|
|
+ ])("does not recover with %s", async (mode) => {
|
|
|
+ rejectRequest = () => ({ status: 400, message: missingTools });
|
|
|
+ if (mode === "explicit true")
|
|
|
+ model = { ...model, compat: { requiresToolsForToolHistory: true } };
|
|
|
+ const context: Context = {
|
|
|
+ messages:
|
|
|
+ mode === "no history"
|
|
|
+ ? [{ role: "user", content: "Hello", timestamp: 1 }]
|
|
|
+ : history(model),
|
|
|
+ };
|
|
|
+ if (mode === "nonempty definitions")
|
|
|
+ context.tools = [
|
|
|
+ {
|
|
|
+ name: "read",
|
|
|
+ description: "Read a file",
|
|
|
+ parameters: Type.Object({}),
|
|
|
+ },
|
|
|
+ ];
|
|
|
+ const result = await streamSimple(model, context, {
|
|
|
+ apiKey: "synthetic-key",
|
|
|
+ onPayload:
|
|
|
+ mode === "hook tools"
|
|
|
+ ? (payload) => ({
|
|
|
+ ...(payload as Record<string, unknown>),
|
|
|
+ tools: [],
|
|
|
+ })
|
|
|
+ : undefined,
|
|
|
+ }).result();
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
+ expect(result.stopReason).toBe("error");
|
|
|
+ });
|
|
|
+
|
|
|
+ it.each([
|
|
|
+ {
|
|
|
+ status: 400,
|
|
|
+ message: "tools must not be an empty array",
|
|
|
+ param: "tools",
|
|
|
+ },
|
|
|
+ { status: 400, message: "invalid tool schema", param: "tools" },
|
|
|
+ { status: 400, message: `Invalid user content: ${missingTools}` },
|
|
|
+ {
|
|
|
+ status: 400,
|
|
|
+ message: "Required field missing",
|
|
|
+ param: "messages",
|
|
|
+ code: "missing_required_parameter",
|
|
|
+ },
|
|
|
+ ...[401, 403, 429, 500].map((status) => ({
|
|
|
+ status,
|
|
|
+ message: missingTools,
|
|
|
+ })),
|
|
|
+ ])(
|
|
|
+ "does not recover unrelated error $status: $message ($param)",
|
|
|
+ async (rejection) => {
|
|
|
+ rejectRequest = () => rejection;
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ { apiKey: "synthetic-key" },
|
|
|
+ ).result();
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
+ expect(result.stopReason).toBe("error");
|
|
|
+ },
|
|
|
+ );
|
|
|
+
|
|
|
+ it("honors explicit false even when the backend requires tools", async () => {
|
|
|
+ model = { ...model, compat: { requiresToolsForToolHistory: false } };
|
|
|
+ rejectRequest = () => ({ status: 400, message: missingTools });
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ { apiKey: "synthetic-key" },
|
|
|
+ ).result();
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
+ expect(result.stopReason).toBe("error");
|
|
|
+ });
|
|
|
+
|
|
|
+ it("does not recover without tool history in the final payload", async () => {
|
|
|
+ rejectRequest = () => ({ status: 400, message: missingTools });
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ {
|
|
|
+ apiKey: "synthetic-key",
|
|
|
+ onPayload: (payload) => ({
|
|
|
+ ...(payload as Record<string, unknown>),
|
|
|
+ messages: [{ role: "user", content: "Hello" }],
|
|
|
+ }),
|
|
|
+ },
|
|
|
+ ).result();
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
+ expect(result.stopReason).toBe("error");
|
|
|
+ });
|
|
|
+
|
|
|
+ it("does not resend when a payload hook removes the recovery tools", async () => {
|
|
|
+ rejectRequest = () => ({ status: 400, message: missingTools });
|
|
|
+ const onPayload = vi.fn((payload: unknown) => {
|
|
|
+ const { tools: _tools, ...rest } = payload as Record<string, unknown>;
|
|
|
+ return rest;
|
|
|
+ });
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ { apiKey: "synthetic-key", onPayload },
|
|
|
+ ).result();
|
|
|
+ expect(onPayload).toHaveBeenCalledTimes(2);
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
+ expect(result.stopReason).toBe("error");
|
|
|
+ });
|
|
|
+
|
|
|
+ it("honors cancellation during the recovery payload hook", async () => {
|
|
|
+ rejectRequest = () => ({ status: 400, message: missingTools });
|
|
|
+ const controller = new AbortController();
|
|
|
+ const onPayload = vi.fn((payload: unknown) => {
|
|
|
+ if (Array.isArray((payload as Record<string, unknown>).tools))
|
|
|
+ controller.abort();
|
|
|
+ });
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ {
|
|
|
+ apiKey: "synthetic-key",
|
|
|
+ signal: controller.signal,
|
|
|
+ onPayload,
|
|
|
+ },
|
|
|
+ ).result();
|
|
|
+ expect(onPayload).toHaveBeenCalledTimes(2);
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
+ expect(result.stopReason).toBe("aborted");
|
|
|
+ });
|
|
|
+
|
|
|
+ it("does not restart a response stream that ended without a finish reason", async () => {
|
|
|
+ incompleteStream = true;
|
|
|
+ const result = await streamSimple(
|
|
|
+ model,
|
|
|
+ { messages: history(model) },
|
|
|
+ { apiKey: "synthetic-key" },
|
|
|
+ ).result();
|
|
|
+ expect(requests).toHaveLength(1);
|
|
|
+ expect(result.content).toEqual([
|
|
|
+ expect.objectContaining({ type: "text", text: summary }),
|
|
|
+ ]);
|
|
|
+ expect(result.stopReason).toBe("error");
|
|
|
+ });
|
|
|
+
|
|
|
it("preserves nonempty tool definitions on ordinary agent requests", async () => {
|
|
|
const result = await streamSimple(
|
|
|
model,
|