import { assertModelAllowed } from "../models/roster.js"; import { WAR_ROOM_MEMBERS } from "../workflows/war-room.js"; import type { Mode, RouteDecision, TaskShape, Topology, UltraConfig } from "../types.js"; import { effectiveInitial } from "./progressive-widening.js"; export function warRoomRoundLimit(requestedMode: Mode, configured: number): number { return Math.min(requestedMode === "warroom" ? 3 : 2, configured); } export function isHardDirect(shape: TaskShape): boolean { return shape.knownScope && (shape.breadth === "tiny" || shape.breadth === "local") && shape.risk !== "high" && shape.coupling !== "high" && shape.repeatedFailureCount === 0 && shape.independentUnits < 2 && (shape.verifiability !== "weak" || shape.intent === "answer"); } export function isHardDeep(shape: TaskShape): boolean { return (shape.coupling === "high" && shape.uncertainty === "high") || shape.risk === "high" || shape.liveCrossAgentDependency || shape.repeatedFailureCount >= 2 || shape.priorDisagreement; } function modelFor(topology: Topology, config: UltraConfig, shape: TaskShape): Pick { if (topology === "deep" || topology === "warroom") return { model: config.deep.model, thinking: config.deep.thinking }; if (topology === "scout" || topology === "swarm") return { model: config.scout.model, thinking: topology === "swarm" ? "high" : config.scout.thinking }; const cheapIntent = config.profile === "free" ? shape.intent === "answer" || shape.intent === "investigate" : shape.intent !== "change"; const freshTiny = shape.breadth === "tiny" && cheapIntent && shape.uncertainty === "low"; return freshTiny ? { model: config.scout.model, thinking: "medium" } : { model: config.root.model, thinking: config.root.thinking }; } function fanoutFor(topology: Topology, config: UltraConfig): number { if (topology === "scout" || topology === "swarm") return Math.min(effectiveInitial(config, config.policy), config.scout.maxParallel); return topology === "warroom" ? WAR_ROOM_MEMBERS.length : 0; } function autoTopology(shape: TaskShape, config: UltraConfig, contextRatio: number): Topology { if (shape.reasonCodes.includes("execution:first-proven")) return "deep"; if (isHardDeep(shape)) return shape.liveCrossAgentDependency && config.warRoom.autoEnabled ? "warroom" : "deep"; if (isHardDirect(shape)) return "direct"; if (shape.liveCrossAgentDependency) return "deep"; if (shape.independentUnits >= 2 && shape.coupling !== "high" && !shape.writerOverlapRisk && contextRatio <= config.context.blockWideSwarmAtRatio) return "swarm"; if (shape.uncertainty === "high" || !shape.knownScope) return "scout"; return "direct"; } export function routeTask(shape: TaskShape, config: UltraConfig, requestedMode: Mode = config.mode, contextRatio = 0): RouteDecision { const reasonCodes = [...shape.reasonCodes]; const proposedTopology = requestedMode === "auto" ? autoTopology(shape, config, contextRatio) : requestedMode; let topology = proposedTopology; let overrideReason: string | undefined; if (isHardDeep(shape) && proposedTopology !== "deep" && proposedTopology !== "warroom") { topology = "deep"; overrideReason = "hard-deep-gate"; reasonCodes.push("route:hard-deep-override"); } else if (requestedMode !== "auto") { reasonCodes.push(`mode:forced-${requestedMode}`); } else { reasonCodes.push(`route:${topology}`); } if (topology === "swarm" && contextRatio > config.context.blockWideSwarmAtRatio) { topology = shape.uncertainty === "high" ? "scout" : "direct"; overrideReason = "context-wide-swarm-gate"; reasonCodes.push("route:wide-swarm-blocked"); } assertModelAllowed(config, modelFor(topology, config, shape).model); const proposedModel = modelFor(proposedTopology, config, shape).model; const model = modelFor(topology, config, shape); return { topology, ...model, fanout: fanoutFor(topology, config), proposedTopology, proposedModel, proposedFanout: fanoutFor(proposedTopology, config), overridden: topology !== proposedTopology, ...(overrideReason ? { overrideReason } : {}), reasonCodes, needsProposal: requestedMode === "auto" && shape.confidence < 0.6, }; }