Files
Kaname/src/lib/agent-tuning.test.ts
T
Pascal LinxweilerandClaude Opus 5.5 716bd8f2f0
Release / release (push) Successful in 3m0s
Release 0.13.0: agent API for deployments and benchmarks
Scopes deployments:read/write and benchmarks:read/run; /api/v1 endpoints
for runtimes, deployments (create with dry run, edit, start, stop, delete
own ones), benchmark runs, A/B, sweeps and comparisons; ?wait= on all
status endpoints. Guardrails: approval, agent deployments at once, one
GPU job at a time, time limits, sweep variants, runtime and argument
allowlists, host port range, memory/CPU caps, protected deployments with
a VRAM check. Requested-by filters, approval bars, per-token activity log,
scope editing, docs/agent/SKILL-tuning.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 15:11:06 +02:00

156 lines
8.1 KiB
TypeScript
Raw 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.
import assert from "node:assert/strict";
import { describe, it } from "node:test";
import { checkArgs, checkHostPort, checkLimits, checkRun, checkStart, checkVram, DEFAULT_TUNING, overTime, parseFlagList, parseTuning, type TuningError } from "./agent-tuning";
import { parseWait, waitFor } from "./wait";
const g = DEFAULT_TUNING;
const rejects = (fn: () => void, code: TuningError["code"], message?: RegExp) =>
assert.throws(fn, (e: TuningError) => e.code === code && (!message || message.test(e.message)));
describe("tuning guardrails: settings", () => {
it("defaults: approve, 1 deployment, 30/180 min, 8 variants, no ports, no caps", () => {
assert.deepEqual(parseTuning(null), DEFAULT_TUNING);
assert.equal(g.approvalMode, "approve");
assert.equal(g.maxAgentDeployments, 1);
assert.equal(g.maxRunMinutes, 30);
assert.equal(g.maxSweepMinutes, 180);
assert.equal(g.maxSweepVariants, 8);
assert.equal(g.hostPortRange, null);
});
it("falls back to defaults for invalid stored values and drops non-flags", () => {
const t = parseTuning({ approvalMode: "yolo", maxAgentDeployments: 0, maxSweepVariants: 99, hostPortRange: { from: 9000, to: 80 }, allowedArgs: { "llama.cpp": ["--no-mmap", "rm -rf"], vllm: "x" } });
assert.equal(t.approvalMode, "approve");
assert.equal(t.maxAgentDeployments, 1);
assert.equal(t.maxSweepVariants, 8);
assert.equal(t.hostPortRange, null);
assert.deepEqual(t.allowedArgs["llama.cpp"], ["--no-mmap"]);
assert.deepEqual(t.allowedArgs.vllm, DEFAULT_TUNING.allowedArgs.vllm);
assert.deepEqual(parseFlagList("--no-mmap, -ctk --mlock --no-mmap"), ["--no-mmap", "-ctk", "--mlock"]);
assert.throws(() => parseFlagList("--ok /etc/passwd"), /isn't a flag/);
});
});
describe("tuning guardrails: extra arguments", () => {
it("passes allowlisted flags with values", () => {
checkArgs(g, "llama.cpp", ["--no-mmap", "-ctk", "q8_0", "--override-tensor=blk\\..*_exps=CPU", "-n", "-1"]);
checkArgs(g, "vllm", ["--kv-cache-dtype", "fp8", "--enable-prefix-caching"]);
});
it("refuses flags not on the engine's list, naming them", () => {
rejects(() => checkArgs(g, "llama.cpp", ["--host", "0.0.0.0"]), "arg_not_allowed", /"--host"/);
rejects(() => checkArgs(g, "llama.cpp", ["--api-key=x"]), "arg_not_allowed", /"--api-key"/);
rejects(() => checkArgs(g, "llama.cpp", ["-m", "x.gguf"]), "arg_not_allowed", /"-m"/);
rejects(() => checkArgs(g, "vllm", ["--trust-remote-code"]), "arg_not_allowed", /"--trust-remote-code"/);
rejects(() => checkArgs(g, "vllm", ["--no-mmap"]), "arg_not_allowed", /on vllm/);
});
it("refuses paths, URLs and stray values", () => {
rejects(() => checkArgs(g, "llama.cpp", ["--chat-template", "/etc/passwd"]), "arg_not_allowed", /path or URL/);
rejects(() => checkArgs(g, "llama.cpp", ["--chat-template=../x"]), "arg_not_allowed", /path or URL/);
rejects(() => checkArgs(g, "vllm", ["--compilation-config", "http://evil/x"]), "arg_not_allowed", /path or URL/);
rejects(() => checkArgs(g, "llama.cpp", ["q8_0"]), "arg_not_allowed", /isn't a value of a flag/);
rejects(() => checkArgs(g, "llama.cpp", ["--no-mmap=1", "extra"]), "arg_not_allowed", /isn't a value/);
});
});
describe("tuning guardrails: ports and limits", () => {
it("allows no host ports unless a range is set", () => {
checkHostPort(g, null);
rejects(() => checkHostPort(g, 18080), "port_not_allowed", /none configured/);
const r = { ...g, hostPortRange: { from: 18000, to: 18099 } };
checkHostPort(r, 18080);
rejects(() => checkHostPort(r, 19000), "port_not_allowed", /18000–18099/);
});
it("applies the memory cap to the given limit or the estimate, and the CPU cap", () => {
const capped = { ...g, maxMemoryMb: 16384, maxCpus: 8 };
assert.deepEqual(checkLimits(capped, { memoryMb: null, cpus: null, estimateMb: 9000, hostMemoryMb: 65536, hostCpus: 32 }), { memoryMb: 9000, cpus: 8 });
rejects(() => checkLimits(capped, { memoryMb: 20000, cpus: null, estimateMb: 9000, hostMemoryMb: 65536, hostCpus: 32 }), "limit_exceeded", /16384 MiB/);
rejects(() => checkLimits(capped, { memoryMb: null, cpus: null, estimateMb: 30000, hostMemoryMb: 65536, hostCpus: 32 }), "limit_exceeded", /the estimate/);
rejects(() => checkLimits(capped, { memoryMb: 1024, cpus: 12, estimateMb: 900, hostMemoryMb: 65536, hostCpus: 32 }), "limit_exceeded", /8 cores/);
// Without caps, the host's size is the cap.
rejects(() => checkLimits(g, { memoryMb: 70000, cpus: null, estimateMb: 1, hostMemoryMb: 65536, hostCpus: 32 }), "limit_exceeded");
});
});
describe("tuning guardrails: one GPU job at a time", () => {
const idle = { agentDeployments: 0, agentRuns: 0, agentStarting: 0 };
it("lets a deployment start when nothing of an agent runs", () => {
checkStart(g, idle);
checkRun(idle);
});
it("refuses starts while an agent run is open or a deployment is loading, and over the count", () => {
rejects(() => checkStart(g, { ...idle, agentRuns: 1 }), "gpu_busy", /benchmark or sweep/);
rejects(() => checkStart(g, { ...idle, agentStarting: 1 }), "gpu_busy", /starting/);
rejects(() => checkStart(g, { ...idle, agentDeployments: 1 }), "too_many_deployments", /the limit is 1/);
checkStart({ ...g, maxAgentDeployments: 2 }, { ...idle, agentDeployments: 1 });
});
it("refuses a second run, and runs while a deployment is loading", () => {
rejects(() => checkRun({ ...idle, agentRuns: 1 }), "gpu_busy", /only one at a time/);
rejects(() => checkRun({ ...idle, agentStarting: 1 }), "gpu_busy", /still starting/);
});
});
describe("tuning guardrails: protected deployments' VRAM", () => {
it("ignores GPUs without protected deployments", () => {
checkVram({ protectedNames: [], newMb: null, runningMb: [30000], totalMb: 32768 });
});
it("refuses what wouldn't fit next to a protected deployment", () => {
checkVram({ protectedNames: ["prod"], newMb: 8000, runningMb: [20000], totalMb: 32768 });
rejects(() => checkVram({ protectedNames: ["prod"], newMb: 12000, runningMb: [20000], totalMb: 32768 }), "vram_protected", /crowd protected deployment\(s\) prod/);
});
it("refuses when the need can't be estimated", () => {
rejects(() => checkVram({ protectedNames: ["prod"], newMb: null, runningMb: [20000], totalMb: 32768 }), "vram_protected", /can't be estimated/);
rejects(() => checkVram({ protectedNames: ["prod"], newMb: 1000, runningMb: [-1], totalMb: 32768 }), "vram_protected");
rejects(() => checkVram({ protectedNames: ["prod"], newMb: 1000, runningMb: [], totalMb: null }), "vram_protected");
});
it("knows when a run or sweep is over its time limit", () => {
const start = new Date(1_000_000);
assert.equal(overTime(start, 60, 1_000_000 + 59_000), false);
assert.equal(overTime(start, 60, 1_000_000 + 61_000), true);
assert.equal(overTime(null, 60), false);
assert.equal(overTime(start, null), false);
});
});
describe("?wait=", () => {
it("parses and caps the seconds", () => {
assert.equal(parseWait(new URLSearchParams("wait=30")), 30);
assert.equal(parseWait(new URLSearchParams("wait=5000")), 900);
assert.equal(parseWait(new URLSearchParams("wait=-1")), 0);
assert.equal(parseWait(new URLSearchParams("")), 0);
});
it("returns once the state settles", async () => {
let n = 0;
const r = await waitFor(async () => ++n, (v) => v >= 3, 5, { pollMs: 10 });
assert.equal(r.value, 3);
assert.equal(r.timedOut, false);
});
it("returns right away when already settled, and after the wait otherwise", async () => {
let loads = 0;
const now = await waitFor(async () => (loads++, "done"), () => true, 900, { pollMs: 10 });
assert.equal(loads, 1);
assert.equal(now.waitedS, 0);
const t0 = Date.now();
const late = await waitFor(async () => "running", () => false, 1, { pollMs: 50 });
assert.ok(Date.now() - t0 >= 950);
assert.equal(late.timedOut, true);
});
it("stops waiting when the client goes away", async () => {
const ac = new AbortController();
setTimeout(() => ac.abort(), 50);
const t0 = Date.now();
await waitFor(async () => "running", () => false, 30, { pollMs: 20, signal: ac.signal });
assert.ok(Date.now() - t0 < 1000);
});
});