Files
loki/scripts/test/agent-resolution.ts
T
CatoandClaude Opus 5 b9a28bff0b fix(providers): let the operator's ranking govern every fallback (#760)
b71941be gave the operator a provider order in Settings → Agent and wired
three surfaces to it. Two resolvers kept walking the hardcoded fleet order,
so for one rate-limited project:

  Feedback row        "Try Grok"      (operator's order)
  Control banner      "Switch to Cursor"  (fleet default)
  /api/beacon reroute  → Cursor, headless, with nobody watching

Two surfaces disagreeing about one run is a defect by this product's own
contract (docs/foundation/LOKI-WORKS.md). The headless case is the worst of
the three: it moves work onto a provider the operator deliberately ranked
last, and they only find out from the run history.

Both resolvers now take the operator's ranking, defaulting to
AGENT_FALLBACK_ORDER so a caller with no user context behaves exactly as
before rather than guessing. /api/beacon and /api/control read it from
user_preferences; it reaches ProjectCard on the control snapshot.

An order that ranks only some agents still reaches the rest — a half-filled
preference must not shrink the fallback set — and it can never resurrect an
agent the builder has not installed.

WHAT THIS DOES NOT DO, deliberately: the two paths still ADVANCE differently.
The human one-tap returns the first usable provider in the ranking; these
resolvers advance PAST the current agent. That is load-bearing rather than
sloppy — current moves on each capacity wall, so advancing walks the whole
list, while "always the top choice" re-suggests an already-tried agent and
decideAutoReroute stops at "all-tried" after about two hops. A first draft of
the tests here asserted "top choice" and failed, which is how the rule was
found; the tests now pin the real semantics and say why.

So they can still name different agents when the current one sits mid-ranking.
Converging them means giving the reroute path the same observed-quota filter
the chooser uses, which touches auto-reroute loop safety — named as the next
piece of work, not smuggled in here.

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-17 02:55:52 -07:00

175 lines
5.4 KiB
TypeScript

/**
* Inline self-tests for agent-resolution helpers.
* Run: npm run test:agent-resolution
*/
import {
inferAdapterFromTabName,
resolveDetectedAgentIds,
resolveOutgoingAgent,
hasAgentLabelMismatch,
detectCapacityIssueFromProject,
resolveNextFallbackAgent,
looksLikeAgentCapacityIssue,
} from "@/lib/agent-resolution";
import type { ProjectState } from "@/lib/control-types";
function stubProject(overrides: Partial<ProjectState> & Pick<ProjectState, "tab">): ProjectState {
return {
id: "1",
projectId: null,
liveTab: overrides.tab,
dir: "/tmp/proj",
agentPref: "claude",
modelPref: null,
session: null,
git: null,
sessionLifecycleSignals: true,
agentRunning: false,
activeAgents: [],
profile: null,
currentPrompt: null,
readyAt: null,
lockAt: null,
closingAt: null,
closedAt: null,
recentCustomPrompts: [],
recentActivity: [],
recentOutcomes: [],
latestOrchestrationRun: null,
autoInjectModeOverride: null,
...overrides,
};
}
function assert(condition: boolean, message: string): void {
if (!condition) throw new Error(message);
}
function runTests(): void {
let passed = 0;
const check = (label: string, fn: () => void) => {
fn();
passed += 1;
console.log(` ✓ ${label}`);
};
check("inferAdapterFromTabName suffix", () => {
assert(inferAdapterFromTabName("Loki Codex") === "codex", "expected codex");
});
check("resolveDetectedAgentIds prefers activeAgents", () => {
const project = stubProject({
tab: "Loki",
activeAgents: ["codex"],
agentPref: "claude",
});
assert(resolveDetectedAgentIds(project)[0] === "codex", "expected live codex");
});
check("resolveOutgoingAgent uses live scan over preference", () => {
const project = stubProject({
tab: "Loki",
activeAgents: ["codex"],
agentPref: "claude",
});
assert(resolveOutgoingAgent(project, "claude") === "codex", "expected codex outgoing");
});
check("hasAgentLabelMismatch when pref disagrees with /proc", () => {
const project = stubProject({
tab: "Loki",
agentRunning: true,
activeAgents: ["codex"],
agentPref: "claude",
});
assert(hasAgentLabelMismatch(project, "claude"), "expected mismatch");
});
check("detectCapacityIssueFromProject reads session text", () => {
const project = stubProject({
tab: "Loki",
session: {
status: "working",
done: "rate limit exceeded",
next: "",
tests: "",
todos: "",
health: "good",
mtime: Date.now(),
},
});
assert(detectCapacityIssueFromProject(project), "expected capacity issue");
});
check("resolveNextFallbackAgent skips current", () => {
assert(
resolveNextFallbackAgent("codex", ["claude", "cursor", "codex"]) === "claude",
"expected claude",
);
});
// ── The operator's ranking governs which list the fallback walks ─────────
// b71941be gave the operator a provider order in Settings → Agent, and every
// resolver kept walking the hardcoded fleet order instead — so the ranking
// changed nothing on Control or under the headless reroute.
//
// NOTE the semantics these pin: this resolver ADVANCES PAST the current agent
// in the ranking; it does not return the operator's top choice. That is
// deliberate and load-bearing for auto-reroute — current moves on each
// capacity wall, so advancing walks the whole list, while "always the top
// choice" would re-suggest an already-tried agent and decideAutoReroute would
// stop at "all-tried" after about two hops instead of trying everything.
check("the operator's order changes which agent comes next", () => {
const available = ["claude", "cursor", "codex", "grok"];
// Same inputs, two rankings, two answers — the ranking is load-bearing.
assert(
resolveNextFallbackAgent("codex", available) === "grok",
"fleet default: grok follows codex",
);
assert(
resolveNextFallbackAgent("codex", available, ["claude", "grok", "codex", "cursor"]) ===
"cursor",
"operator order: cursor follows codex",
);
});
check("an agent the operator did not rank is still reachable", () => {
// A half-filled preference must not shrink the fallback set to nothing.
assert(
resolveNextFallbackAgent("claude", ["claude", "cursor"], ["grok"]) === "cursor",
"expected the unranked-but-installed agent",
);
});
check("the operator's order never resurrects an uninstalled agent", () => {
assert(
resolveNextFallbackAgent("claude", ["claude", "codex"], ["grok", "cursor", "codex"]) ===
"codex",
"expected codex — grok and cursor are not installed",
);
});
check("no order given still behaves exactly as before", () => {
assert(
resolveNextFallbackAgent("codex", ["claude", "cursor", "codex"]) === "claude",
"expected the fleet default for a caller with no user context",
);
});
check("looksLikeAgentCapacityIssue matches quota", () => {
assert(looksLikeAgentCapacityIssue("You have exceeded your quota"), "expected quota match");
});
check("looksLikeAgentCapacityIssue matches CLI weekly-limit screens", () => {
assert(
looksLikeAgentCapacityIssue("You hit your weekly limit. Weekly limit left: 0%"),
"expected weekly-limit match",
);
});
console.log(`\n${passed} agent-resolution tests passed`);
}
runTests();