Files
plugins/packages/agentcompass/test/classifier-stub.mjs
T
Akiiwaandakiiwa-games c4e9c1099e agentcompass 0.2.0: route on AgentCompass v0.6.0 (#1083)
agentcompass routes on the AgentCompass v0.6.0 engine: four capability
categories, quota-economy seat ordering and data gates. With no caller
profile, a request keeps the model the client asked for. External
scoring stays off unless allowExternalScorer and a scorer base URL are
both set. The reference ships in the package. This supersedes #791.

Run at 52a98632, merged onto main, on WSL (Ubuntu 24.04) with
HOME=$(mktemp -d):
- npm test in packages/agentcompass: 140 pass, 0 fail
- bun scripts/check.mjs agentcompass: ok
- Read the diff for new I/O: src/ has no fetch outside the opt-in
  scorer, and no spawn, env read or disk access.
- The docs' scorer example was checked against scorerOptionsOf and
  resolveClassifier.

Not tried: a real Magpie gateway routing live traffic through it.

Co-authored-by: akiiwa-games <akiiwa-games@users.noreply.github.com>
2026-11-13 17:14:25 +08:00

53 lines
2.6 KiB
JavaScript

/**
* The answers a classifier gives in these tests. Routing starts from a task profile and nothing in `src/` reads a task's
* text, so a test that sends a text says what the classifier answered for it here (`fixtures/classifier-answers.json`).
* The answers began as a snapshot of what the deterministic profiler removed from `src/` said for each text, so that the
* assertions on routing stayed as they were; they are fixtures, and any of them can be edited by hand.
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { sha256Hex } from "../src/continuity.js";
import { extractRequestFacts } from "../src/profiler-core.js";
const FILE = path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures", "classifier-answers.json");
const answers = JSON.parse(fs.readFileSync(FILE, "utf8"));
const INPUT_FIELDS = ["objective", "work_kind", "difficulty", "needs", "multi_task", "task_count", "expected_hours", "hours", "context", "cwd"];
function keyOf(task) {
const input = {};
for (const field of INPUT_FIELDS) {
if (task?.[field] !== undefined) input[field] = task[field];
}
const key = JSON.stringify(input);
return key.length > 240 ? `${key.slice(0, 120)}#${sha256Hex(key)}` : key;
}
/** The classifier's answer for a task (`{ objective, ... }`) or a text. */
export function classifierAnswer(task) {
const input = typeof task === "string" ? { objective: task } : task;
const key = keyOf(input);
const answer = answers[key];
if (!answer) throw new Error(`no classifier answer in fixtures/classifier-answers.json for ${key}`);
return structuredClone(answer);
}
const classifierConfigured = (options) => options?.allowExternalScorer || options?.scorer?.allowExternalScorer;
/** A hook or middleware request sent the way a client does, with the classifier's answer for its text on `ctx`. */
export function answerRequest(body, ctx) {
if (!body || typeof body !== "object" || Array.isArray(body) || ctx.profile || classifierConfigured(ctx.options)) return;
const { objective } = extractRequestFacts(body, ctx);
if (objective) ctx.profile = classifierAnswer(objective);
}
/** A `/v1/systemone` request with the classifier's answer for its text in `state`. */
export function answerSystemOne(body, options) {
const state = body?.state || {};
const text = typeof state.message === "string" ? state.message : typeof state.turn === "string" ? state.turn : state.task?.objective;
if (typeof text !== "string" || state.kinds || state.profile || state.task?.profile || classifierConfigured(options)) return body;
return { ...body, state: { ...state, profile: classifierAnswer(text) } };
}