Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion experiments/turn-surface-classifier.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from "bun:test";
import { classifySurface, chooseNarrow, narrowRoutingRequest, ROUTING_MODEL, shortlist, wideRoutingRequest, type RoutingCatalogue } from "../scripts/turn-surface-classifier";
import { classifySurface, chooseNarrow, MEASURED_BRIDGE_ROUTING_TOOLS, measuredRoutingTools, narrowRoutingRequest, ROUTING_MODEL, shortlist, wideRoutingRequest, type RoutingCatalogue } from "../scripts/turn-surface-classifier";
import { createJevClient } from "../packages/ui-server/src/classification/jev-client";
import type { ClassificationAnswers } from "@schlessera/brain-ui-sdk/internal";

Expand Down Expand Up @@ -74,3 +74,26 @@ test("one deadline prevents the second HTTP pass and off/no-key/circuit make no
expect(await classifySurface({ client: failing, prompt: "Read", previousTail: "", catalogue, enabled: true, deadline: deadline.signal }))
.toMatchObject({ outcome: "circuit_open", passes: 0 });
});

test("a catalogue over the 64-entry bound is refused by name, never truncated", () => {
const tools = (count: number) => Array.from({ length: count }, (_, index) => ({ name: `tool_${index}`, id: `mcp__brain__tool_${index}`,
serverName: "brain", inputSchema: { type: "object" as const }, description: `Tool ${index}` }));
const atBound = wideRoutingRequest("Read", "", { tools: tools(61), skills: catalogue.skills });
expect(Object.keys((atBound.questions.tool as { criteria: Record<string, string> }).criteria)).toHaveLength(61);
expect(() => wideRoutingRequest("Read", "", { tools: tools(62), skills: catalogue.skills }))
.toThrow("Invalid routing catalogue: 65 tool and skill entries exceed the 64-entry bound");
expect(() => wideRoutingRequest("Read", "", { tools: [], skills: catalogue.skills })).toThrow("Invalid routing catalogue: tools and skills must both be nonempty");
});

test("the live collector routes over the measured bridge tools only, excluded by name", () => {
const tool = (serverName: string, name: string) => ({ name, id: `mcp__${serverName}__${name}`, serverName, inputSchema: { type: "object" as const }, description: name });
// Later registrations sit before, between and after the measured eight, so a
// prefix or truncation could not produce the expected list.
const later = ["brain_apify_run", "read_attachment", "brain_compute_plan", "write_file", "inbox_add", "turn_temp", "edit_file", "brain_add", "resolve_action"];
const bridge = MEASURED_BRIDGE_ROUTING_TOOLS.flatMap((name, index) => [tool("brain-ui", later[index]!), tool("brain-ui", name)]).concat(tool("brain-ui", later[8]!));
const core = ["brain_read", "brain_apify_run", "brain_compute_plan"].map(name => tool("brain", name));
expect(measuredRoutingTools([...core, ...bridge]).map(entry => entry.id)).toEqual([...core.map(entry => entry.id),
...MEASURED_BRIDGE_ROUTING_TOOLS.map(name => `mcp__brain-ui__${name}`)]);
expect(() => measuredRoutingTools([...core, ...bridge.filter(entry => entry.name !== "show_block")]))
.toThrow("Measured bridge routing tools missing: show_block");
});
4 changes: 2 additions & 2 deletions scripts/measure-turn-surface-live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { Options, SDKMessage } from "@anthropic-ai/claude-agent-sdk";
import { createJevClient } from "../packages/ui-server/src/classification/jev-client";
import { createFixture, fixtureTurn, connectBrainSurface, installedRuntime } from "./turn-surface-fixture";
import { ARMS, type Arm } from "./turn-surface-routing";
import { classifySurface } from "./turn-surface-classifier";
import { classifySurface, measuredRoutingTools } from "./turn-surface-classifier";
import { gatedSurfaceServer, startOverlappedTurn, type SurfaceDecision } from "./turn-surface-overlap";
import { routingMeasurementHook } from "./turn-surface-isolation";
import { disableMeasurementMemory, type MeasurementToolAccess } from "./measurement-isolation";
Expand Down Expand Up @@ -142,7 +142,7 @@ export async function measureLiveSurface(params: { manifestPath: string; reviewP
activeDeadline = new AbortController();
const routingTimer = setTimeout(() => activeDeadline.abort(), 2000);
const routing = classifySurface({ client: jev, prompt: task.test.prompt, previousTail: "",
catalogue: { tools: [...brain.tools, ...prepared.peer.tools], skills: catalogue.skills }, enabled: task.arm !== "baseline", deadline: activeDeadline.signal,
catalogue: { tools: measuredRoutingTools([...brain.tools, ...prepared.peer.tools]), skills: catalogue.skills }, enabled: task.arm !== "baseline", deadline: activeDeadline.signal,
}).finally(() => clearTimeout(routingTimer));
const decision: Promise<SurfaceDecision> = routing.then(value => ({ arm: task.arm, routed: value.outcome === "answered",
tools: value.tools, skills: value.skills }));
Expand Down
27 changes: 26 additions & 1 deletion scripts/turn-surface-classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ import type { SkillEntry, SurfaceTool } from "./turn-surface-routing";

export const ROUTING_MODEL = "jev-1.13.0";
export const ROUTING_THRESHOLD = 0.6;
/** Tool plus skill entries one wide choice question may carry. The 2026-10-07
* #587 run completed under it, and nothing measured a wider question, so a
* larger catalogue is refused rather than truncated. */
export const ROUTING_CATALOGUE_BOUND = 64;
/** The `brain-ui` bridge tools `createBrainUiMcpServer` registered at the
* measured #587 source (380ad6eaf), the same eight the archived native
* catalogue lists as loaded. The retained live collector routes over these and
* no later bridge registration, so it keeps the catalogue #587 measured (the
* #3886 ruling, option 3). Later bridge tools stay on the native surface but
* outside the routed choice. Core `brain` tools are not filtered. */
export const MEASURED_BRIDGE_ROUTING_TOOLS = [
"ask_user", "ask_user_form", "ask_user_list", "ask_user_rank",
"get_current_location", "query_activity", "request_image_mask", "show_block",
] as const;
/** Keep every non-bridge tool and only the measured bridge tools, by name and
* in listed order. A measured bridge tool that is no longer listed fails, since
* the control would then route over a catalogue #587 never saw. */
export function measuredRoutingTools(tools: readonly SurfaceTool[]): SurfaceTool[] {
const measured: readonly string[] = MEASURED_BRIDGE_ROUTING_TOOLS;
const missing = measured.filter(name => !tools.some(tool => tool.serverName === "brain-ui" && tool.name === name));
if (missing.length) throw Error(`Measured bridge routing tools missing: ${missing.join(", ")}`);
return tools.filter(tool => tool.serverName !== "brain-ui" || measured.includes(tool.name));
}
export interface RoutingCatalogue { tools: readonly SurfaceTool[]; skills: readonly SkillEntry[] }
export interface LiveSelection { outcome: string; tools: string[]; skills: string[]; durationMs: number; passes: number }
type Kind = "tool" | "skill";
Expand All @@ -15,7 +38,9 @@ function entries(catalogue: RoutingCatalogue, kind: Kind): SkillEntry[] {
}
function state(prompt: string, previousTail: string) { return { request: prompt, previous_assistant_tail: previousTail.slice(-1000) }; }
export function wideRoutingRequest(prompt: string, previousTail: string, catalogue: RoutingCatalogue): ClassificationRequest {
if (!catalogue.tools.length || !catalogue.skills.length || catalogue.tools.length + catalogue.skills.length > 64) throw Error("Invalid routing catalogue");
if (!catalogue.tools.length || !catalogue.skills.length) throw Error("Invalid routing catalogue: tools and skills must both be nonempty");
const size = catalogue.tools.length + catalogue.skills.length;
if (size > ROUTING_CATALOGUE_BOUND) throw Error(`Invalid routing catalogue: ${size} tool and skill entries exceed the ${ROUTING_CATALOGUE_BOUND}-entry bound`);
const request: ClassificationRequest = { model: ROUTING_MODEL, state: state(prompt, previousTail), questions: {} };
for (const kind of ["tool", "skill"] as const) {
request.questions[kind] = { type: "choice", instructions: `Which ${kind} best fits satisfying the current request?`,
Expand Down
Loading