9a26a945a7
- Validated queued toolChoice against active tools in agent and coding-agent sessions. - Rejected queued forced choices with reason "unavailable" when selected tools were inactive. - Dropped provider toolChoice payloads when requested function tools were not offered. - Probed Tokio worker-thread support and fell back to current-thread runtime creation.
105 lines
3.5 KiB
TypeScript
105 lines
3.5 KiB
TypeScript
import { afterEach, beforeEach, expect, it } from "bun:test";
|
|
import * as path from "node:path";
|
|
import { Agent, type AgentTool } from "@oh-my-pi/pi-agent-core";
|
|
import { AssistantMessageEventStream } from "@oh-my-pi/pi-ai/utils/event-stream";
|
|
import { getBundledModel } from "@oh-my-pi/pi-catalog/models";
|
|
import { ModelRegistry } from "@oh-my-pi/pi-coding-agent/config/model-registry";
|
|
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
|
|
import { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session";
|
|
import { AuthStorage } from "@oh-my-pi/pi-coding-agent/session/auth-storage";
|
|
import { convertToLlm } from "@oh-my-pi/pi-coding-agent/session/messages";
|
|
import { SessionManager } from "@oh-my-pi/pi-coding-agent/session/session-manager";
|
|
import { TempDir } from "@oh-my-pi/pi-utils";
|
|
import { z } from "zod/v4";
|
|
|
|
let tempDir: TempDir;
|
|
let authStorage: AuthStorage | undefined;
|
|
let session: AgentSession;
|
|
|
|
beforeEach(async () => {
|
|
tempDir = TempDir.createSync("@pi-agent-session-force-tool-");
|
|
const model = getBundledModel("anthropic", "claude-sonnet-4-5");
|
|
if (!model) throw new Error("Expected claude-sonnet-4-5 model to exist");
|
|
|
|
authStorage = await AuthStorage.create(path.join(tempDir.path(), "testauth.db"));
|
|
authStorage.setRuntimeApiKey("anthropic", "test-key");
|
|
const modelRegistry = new ModelRegistry(authStorage, path.join(tempDir.path(), "models.yml"));
|
|
const settings = Settings.isolated({ "compaction.enabled": false });
|
|
const sessionManager = SessionManager.inMemory(tempDir.path());
|
|
|
|
const bashTool: AgentTool = {
|
|
name: "bash",
|
|
label: "Bash",
|
|
description: "Mock bash tool",
|
|
parameters: z.object({}),
|
|
execute: async () => ({ content: [{ type: "text" as const, text: "ok" }] }),
|
|
};
|
|
const writeTool: AgentTool = {
|
|
name: "write",
|
|
label: "Write",
|
|
description: "Mock write tool",
|
|
parameters: z.object({}),
|
|
execute: async () => ({ content: [{ type: "text" as const, text: "ok" }] }),
|
|
};
|
|
|
|
const agent = new Agent({
|
|
getApiKey: () => "test-key",
|
|
initialState: {
|
|
model,
|
|
systemPrompt: ["Test"],
|
|
tools: [bashTool, writeTool],
|
|
messages: [],
|
|
},
|
|
convertToLlm,
|
|
streamFn: () => new AssistantMessageEventStream(),
|
|
});
|
|
|
|
session = new AgentSession({
|
|
agent,
|
|
sessionManager,
|
|
settings,
|
|
modelRegistry,
|
|
toolRegistry: new Map([
|
|
[bashTool.name, bashTool],
|
|
[writeTool.name, writeTool],
|
|
]),
|
|
});
|
|
});
|
|
|
|
afterEach(async () => {
|
|
await session.dispose();
|
|
authStorage?.close();
|
|
authStorage = undefined;
|
|
tempDir.removeSync();
|
|
});
|
|
|
|
it("forces specific tool, then transitions to none, then clears", () => {
|
|
session.setForcedToolChoice("write");
|
|
|
|
const first = session.nextToolChoice();
|
|
const second = session.nextToolChoice();
|
|
const third = session.nextToolChoice();
|
|
|
|
expect(first).toEqual({ type: "tool", name: "write" });
|
|
// After the forced call, "none" prevents the loop from making more tool calls
|
|
expect(second).toBe("none");
|
|
// After "none" is consumed, override clears entirely
|
|
expect(third).toBeUndefined();
|
|
});
|
|
|
|
it("requeues a forced choice whose tool is filtered out before dequeue", async () => {
|
|
session.setForcedToolChoice("write");
|
|
|
|
await session.setActiveToolsByName(["bash"]);
|
|
expect(session.nextToolChoice()).toBeUndefined();
|
|
expect(session.toolChoiceQueue.hasInFlight).toBe(false);
|
|
|
|
await session.setActiveToolsByName(["bash", "write"]);
|
|
expect(session.nextToolChoice()).toEqual({ type: "tool", name: "write" });
|
|
session.toolChoiceQueue.clear();
|
|
});
|
|
|
|
it("throws when forcing a non-active tool", () => {
|
|
expect(() => session.setForcedToolChoice("read")).toThrow('Tool "read" is not currently active.');
|
|
});
|