peppa/packages/testkit/test/model-per-provider.test.ts

103 lines
6.5 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// PEP-264 (QLFW-78): a model is kept per provider, and a model of another CLI never reaches
// the launch — Codex with `-m opus` refuses every message.
import assert from "node:assert/strict";
import { after, before, describe, test } from "node:test";
import { events } from "@peppa/db";
import { core, db, project, runner, setDefaultOwner, teardown } from "./helpers.ts";
process.env.PEPPA_SECRET = "mock-secret-for-tests";
before(async () => {
const pm = await core.upsertUserFromGitea(db, { id: 2641, login: "mock-pm-2641" }, { access_token: "x" });
setDefaultOwner(pm.id);
});
after(teardown);
const argvOf = (e: object) => ((e as { argv?: string[] }).argv ?? []);
const flag = (argv: string[], name: string) => {
const i = argv.indexOf(name);
return i >= 0 ? argv[i + 1] : undefined;
};
const sessionEvents = async (sid: number, type: string) => (await db.select().from(events)).filter((e) => e.sessionId === sid && e.type === type).map((e) => e.payload as Record<string, unknown>);
type P = "claude" | "codex" | "opencode";
const PROVIDERS: P[] = ["claude", "codex", "opencode"];
// One model name of each CLI and one that says nothing.
const MODELS: Record<string, P | null> = { opus: "claude", sonnet: "claude", "claude-sonnet-4-5": "claude", "gpt-5-codex": "codex", o3: "codex", "openrouter/qwen": "opencode", "my-model": null };
describe("PEP-264: модель по провайдеру", () => {
for (const configured of [null, ...PROVIDERS] as (P | null)[]) {
for (const provider of PROVIDERS) {
for (const [model, owner] of Object.entries(MODELS)) {
test(`настройка ${configured ?? "без провайдера"} · модель ${model} → запуск на ${provider}`, () => {
const s = { ...core.DEFAULT_AGENT_SETTINGS, provider: configured, model, escalateModel: model };
const own = configured == null || configured === provider;
// A named model — only to its CLI; an unknown name — to the configured provider (default Claude).
const fits = own && (owner ?? configured ?? "claude") === provider;
assert.equal(core.launchModelFor(s, provider, false).model, fits ? model : null);
assert.equal(core.launchModelFor(s, provider, true).model, fits ? model : null);
const fitted = core.fitLaunchModel(provider, model);
assert.equal(fitted.model, owner == null || owner === provider ? model : null);
assert.equal(fitted.dropped, owner != null && owner !== provider ? model : null);
});
}
}
}
test("Задача с opus, назначенная Codex, идёт на модели Codex по умолчанию; своя модель Codex — важнее", () => {
const s = { ...core.DEFAULT_AGENT_SETTINGS, provider: "codex" as const, model: "opus" };
assert.equal(core.launchModelFor(s, "codex", false).model, null);
assert.equal(core.launchModelFor({ ...s, codexModel: "gpt-5-codex" }, "codex", false).model, "gpt-5-codex");
// Per-provider fields go only to their provider, even when misfilled.
assert.equal(core.launchModelFor({ ...s, codexModel: "sonnet" }, "codex", false).model, null);
const both = { ...core.DEFAULT_AGENT_SETTINGS, claudeModel: "opus", codexModel: "gpt-5-codex" };
assert.equal(core.launchModelFor(both, "claude", false).model, "opus");
assert.equal(core.launchModelFor(both, "codex", false).model, "gpt-5-codex");
assert.equal(core.launchModelFor(both, "opencode", false).model, null);
});
test("Запасной провайдер и переезд claude↔codex не уносят чужую модель", () => {
const s = { ...core.DEFAULT_AGENT_SETTINGS, provider: "claude" as const, model: "opus", fallbackProvider: "codex" as const, fallbackModel: "sonnet" };
assert.equal(core.launchModelFor(s, "codex", false).model, null);
assert.equal(core.launchModelFor({ ...s, fallbackModel: "gpt-5-codex" }, "codex", false).model, "gpt-5-codex");
assert.equal(core.launchModelFor({ ...s, codexModel: "gpt-5" }, "codex", false).model, "gpt-5");
// Moved to Codex without a fallback setting (rotation PEP-107): only Codex's own model.
const moved = { ...core.DEFAULT_AGENT_SETTINGS, provider: "claude" as const, model: "opus", codexModel: "gpt-5-codex" };
assert.equal(core.launchModelFor(moved, "codex", false).model, "gpt-5-codex");
assert.equal(core.launchModelFor({ ...moved, codexModel: null }, "codex", false).model, null);
});
test("Поля моделей провайдеров наследуются и проверяются", async () => {
const p = await project({ turns: [[{ say: "ok" }]] });
const proj = await core.getProject(db, p.key);
const ref = await core.resolveTask(db, p.taskKey);
await core.setProjectAgentSettings(db, proj.id, { codexModel: "gpt-5-codex", claudeModel: "sonnet" });
await core.setTaskAgentSettings(db, ref.task.id, { claudeModel: "opus" });
const s = await core.resolveAgentSettings(db, proj.id, ref.task.id);
assert.deepEqual([s.claudeModel, s.codexModel, s.opencodeModel], ["opus", "gpt-5-codex", null]);
await assert.rejects(core.setProjectAgentSettings(db, proj.id, { codexModel: "bad model!" }), /Недопустимое значение codexModel/);
});
test("Раннер: стори с провайдером codex и моделью opus стартует на Codex без --model", async () => {
const p = await project({ turns: [[{ say: "ok" }]] });
const ref = await core.resolveTask(db, p.taskKey);
await core.setTaskAgentSettings(db, ref.task.id, { provider: "codex", model: "opus" });
const sid = await p.start({ provider: "codex" });
const argv = argvOf(await p.waitLog("started", (e) => e.ev === "start"));
assert.equal(flag(argv, "--model"), undefined, argv.join(" "));
const [ev] = await sessionEvents(sid, "started");
assert.equal(ev!.model ?? null, null);
await runner.stopSession(db, sid);
});
test("Раннер: модель Claude, переданная в запуск Codex напрямую, отбрасывается с событием model_dropped", async () => {
const p = await project({ turns: [[{ say: "ok" }]] });
const s = await runner.startSession(db, p.taskKey, { provider: "codex", model: "opus" });
const argv = argvOf(await p.waitLog("started", (e) => e.ev === "start"));
assert.equal(flag(argv, "--model"), undefined, argv.join(" "));
const [dropped] = await sessionEvents(s.id, "model_dropped");
assert.deepEqual(dropped, { provider: "codex", model: "opus" });
await runner.stopSession(db, s.id);
});
});