Files
oh-my-pi/packages/coding-agent/test/sdk-autolearn-active-tools.test.ts
T
oldschoola a2854ba768 fix: migrate coding-agent tests from fs.rm to removeWithRetries
Migrate 203 test files (356 call sites) from fs.rm/fs.rmSync to
removeWithRetries/removeSyncWithRetries to reduce EBUSY test failures
on Windows. removeWithRetries is now exported from @oh-my-pi/pi-utils.

The migration uses a regex-based approach that:
- Replaces fs.rm(path, { recursive, force }) → removeWithRetries(path)
- Replaces fs.rmSync(path, { recursive, force }) → removeSyncWithRetries(path)
- Replaces fs.rm(path) → removeWithRetries(path) (no options)
- Skips fs.rm/fs.rmSync inside template literals (bun --eval scripts)
- Adds imports to existing @oh-my-pi/pi-utils import or creates new one
- Removes unused fs imports where fs.rm was the only fs usage (4 files)
2026-06-23 15:28:05 -07:00

90 lines
3.6 KiB
TypeScript

import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { AuthStorage } from "@oh-my-pi/pi-ai";
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 { createAgentSession } from "@oh-my-pi/pi-coding-agent/sdk";
import type { AgentSession } from "@oh-my-pi/pi-coding-agent/session/agent-session";
import { SessionManager } from "@oh-my-pi/pi-coding-agent/session/session-manager";
import { removeSyncWithRetries, Snowflake } from "@oh-my-pi/pi-utils";
// Guards the auto-learn tool ACTIVATION wiring in createAgentSession: createTools
// force-includes manage_skill into the built registry for an enabled top-level
// session, but an explicit `toolNames` whitelist would otherwise drop it from the
// active set — so the SDK must re-activate it (mirroring the `yield` invariant),
// or the nudge/guidance would point at a tool the model cannot call. No memory
// backend is configured (manage_skill needs only `autolearn.enabled`), so the
// session starts without a heavy backend.
describe("createAgentSession auto-learn tool activation", () => {
let registryDir: string;
let authStorage: AuthStorage;
let modelRegistry: ModelRegistry;
const sessions: AgentSession[] = [];
beforeAll(async () => {
registryDir = path.join(os.tmpdir(), `pi-autolearn-active-${Snowflake.next()}`);
fs.mkdirSync(registryDir, { recursive: true });
authStorage = await AuthStorage.create(path.join(registryDir, "auth.db"));
modelRegistry = new ModelRegistry(authStorage);
});
afterAll(async () => {
for (const session of sessions) await session.dispose().catch(() => {});
authStorage.close();
if (fs.existsSync(registryDir)) removeSyncWithRetries(registryDir);
});
async function activeToolNames(settings: Settings): Promise<string[]> {
const { session } = await createAgentSession({
cwd: registryDir,
agentDir: registryDir,
modelRegistry,
sessionManager: SessionManager.inMemory(),
settings,
model: getBundledModel("openai", "gpt-4o-mini"),
disableExtensionDiscovery: true,
toolNames: ["read"],
});
sessions.push(session);
return session.getActiveToolNames();
}
it("activates force-included manage_skill in a restricted top-level session", async () => {
const names = await activeToolNames(Settings.isolated({ "autolearn.enabled": true }));
expect(names).toContain("read");
// Built by createTools' force-include AND activated by the SDK's explicit-list
// re-inclusion, so guidance/controller point at a callable tool.
expect(names).toContain("manage_skill");
});
it("initializes the selected memory backend before an auto-learn session can run", async () => {
const { session } = await createAgentSession({
cwd: registryDir,
agentDir: registryDir,
modelRegistry,
sessionManager: SessionManager.inMemory(),
settings: Settings.isolated({
"autolearn.enabled": true,
"memory.backend": "hindsight",
"hindsight.apiUrl": "http://127.0.0.1:1",
"hindsight.mentalModelsEnabled": false,
}),
model: getBundledModel("openai", "gpt-4o-mini"),
disableExtensionDiscovery: true,
toolNames: ["read"],
});
sessions.push(session);
expect(session.getHindsightSessionState()).toBeDefined();
});
it("omits manage_skill from a restricted session when auto-learn is off", async () => {
const names = await activeToolNames(Settings.isolated({}));
expect(names).toContain("read");
expect(names).not.toContain("manage_skill");
});
});