Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

feat(advisor)!: send panel and collector calls through routing chains

Jack Rudenkocb8c6bba380f
diff --git a/packages/cli/src/advisor-startup.test.ts b/packages/cli/src/advisor-startup.test.ts
index 3e50f0b0..15a3a228 100644
--- a/packages/cli/src/advisor-startup.test.ts
+++ b/packages/cli/src/advisor-startup.test.ts
@@ -1,20 +1,30 @@
-import { describe, expect, it } from "bun:test";
+import { afterEach, beforeEach, describe, expect, it, mock } from "bun:test";
import {
type AdvisorLaunchFacts,
- type AdvisorModelStatus,
- type AdvisorRoute,
type AdvisorStartupDecision,
- advisorModelStatus,
+ advisorMemberRefusal,
decideAdvisorStartup,
evaluateAdvisorStartup,
resolveMainModelFact,
} from "./advisor-startup.js";
import { resolveAdvisorToolEnv } from "./claude-runner.js";
+import {
+ type AdvisorChainBuild,
+ type AdvisorHop,
+ type AdvisorModelPlan,
+ advisorChainKey,
+ planAdvisorModel,
+} from "./handlers/advisor-route-plan.js";
+import type { HandlerSet } from "./handlers/handler-set.js";
+import type { ModelHandler } from "./handlers/types.js";
+import { _resetCatalogClient, _setCatalogEntriesForTest } from "./providers/catalog-client.js";
import type { ClaudishConfig } from "./types.js";
const ENABLE_ENV_VAR = "CLAUDE_CODE_ENABLE_EXPERIMENTAL_ADVISOR_TOOL";
const DISABLE_ENV_VAR = "CLAUDE_CODE_DISABLE_ADVISOR_TOOL";
+type RoutedPlan = Extract<AdvisorModelPlan, { kind: "routed" }>;
+
function config(overrides: Partial<ClaudishConfig> = {}): ClaudishConfig {
return {
autoApprove: false,
@@ -33,44 +43,75 @@ function config(overrides: Partial<ClaudishConfig> = {}): ClaudishConfig {
};
}
-const openAiRoute: AdvisorRoute = {
- kind: "openai",
- api: "chat-completions",
- host: "api.openai.com",
- url: "https://api.openai.com/v1/chat/completions",
- credential: "openai",
- wireModel: "gpt-5.6-sol",
-};
-
-const googleRoute: AdvisorRoute = {
- kind: "google",
- api: "chat-completions",
- host: "generativelanguage.googleapis.com",
- url: "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions",
- credential: "google",
- wireModel: "gemini-2.5-pro",
-};
-
-const anthropicRoute: AdvisorRoute = {
- kind: "anthropic",
- api: "anthropic-messages",
- host: "api.anthropic.com",
- url: "https://api.anthropic.com/v1/messages",
- credential: "anthropic",
- wireModel: "haiku",
-};
-
-function modelStatus(
- model: string,
- route: AdvisorRoute,
- callable = true,
- credentialName = "OPENAI_API_KEY"
-): AdvisorModelStatus {
- return { model, route, callable, credentialName };
+function routedPlan(
+ spec = "openai/gpt-5.6-sol",
+ role: "panel" | "collector" = "panel",
+ hops: AdvisorHop[] = [
+ {
+ provider: "openai-codex",
+ displayName: "OpenAI Codex",
+ modelSpec: "[email protected]",
+ wireId: "gpt-6.1-sol",
+ billing: "subscription",
+ tier: "subscription",
+ },
+ ]
+): RoutedPlan {
+ return {
+ kind: "routed",
+ spec,
+ role,
+ explanation: {
+ requestedModel: spec,
+ routedModel: spec,
+ source: "catalog",
+ candidates: [],
+ warnings: [],
+ outcome: { kind: "ok" },
+ },
+ hops,
+ invocationMode: "auto-route",
+ };
+}
+
+function builtChain(plan: RoutedPlan): AdvisorChainBuild {
+ const handler: ModelHandler = {
+ async handle() {
+ throw new Error("Startup must not invoke a built handler");
+ },
+ async shutdown() {},
+ };
+ return {
+ key: advisorChainKey(plan.role, plan.spec),
+ builds: plan.hops.map((hop) => ({ kind: "built", handler, transportName: hop.provider })),
+ handler,
+ firstCallableHop: 0,
+ uidByTransportName: new Map(plan.hops.map((hop) => [hop.provider, hop.provider])),
+ };
+}
+
+function collectorRefusal(
+ cause: "claude-no-credential" | "unresolved-alias" = "claude-no-credential"
+): Extract<AdvisorModelPlan, { kind: "refused" }> {
+ return {
+ kind: "refused",
+ spec: "haiku",
+ role: "collector",
+ cause,
+ reason:
+ cause === "unresolved-alias"
+ ? 'Cannot resolve Claude alias "haiku" without the cloud models catalog.'
+ : "haiku is a Claude model; this launch gives the advisor no Claude login (a --model of another provider), and no ANTHROPIC_API_KEY was found in env, config, keychain or 1Password",
+ };
}
function facts(overrides: Partial<AdvisorLaunchFacts> = {}): AdvisorLaunchFacts {
const toolEnv = resolveAdvisorToolEnv(config({ advisor: true }), {});
+ const panelPlans = overrides.panelPlans ?? [routedPlan()];
+ const collectorPlan = overrides.collectorPlan ?? null;
+ const routed = [...panelPlans, ...(collectorPlan ? [collectorPlan] : [])].filter(
+ (plan): plan is RoutedPlan => plan.kind === "routed"
+ );
return {
panel: ["openai/gpt-5.6-sol"],
collector: null,
@@ -78,8 +119,12 @@ function facts(overrides: Partial<AdvisorLaunchFacts> = {}): AdvisorLaunchFacts
mainModels: [],
childEnv: { ...toolEnv.vars },
toolEnv,
- panelStatus: [modelStatus("openai/gpt-5.6-sol", openAiRoute)],
- collectorStatus: null,
+ panelPlans,
+ collectorPlan,
+ builds: new Map(
+ routed.map((plan) => [advisorChainKey(plan.role, plan.spec), builtChain(plan)])
+ ),
+ childAuth: { controlled: true },
...overrides,
};
}
@@ -90,6 +135,15 @@ function expectProceed(decision: AdvisorStartupDecision) {
return decision;
}
+beforeEach(() => {
+ _resetCatalogClient();
+ _setCatalogEntriesForTest(null);
+});
+
+afterEach(() => {
+ _resetCatalogClient();
+});
+
describe("resolveMainModelFact", () => {
it("labels a bare non-Claude name as unresolved until its first routed request", () => {
expect(resolveMainModelFact("o4-mini")).toEqual({
@@ -124,127 +178,160 @@ describe("decideAdvisorStartup", () => {
);
});
- it("refuses when a panel model credential is missing and names both", () => {
- const model = "openai/gpt-5.6-sol";
- const credentialName = "OPENAI_API_KEY";
- const decision = decideAdvisorStartup(
- facts({ panelStatus: [modelStatus(model, openAiRoute, false, credentialName)] })
- );
+ it.each(["no-credential", "explicit-no-credential"] as const)(
+ "refuses a panel plan with %s and names the model and credential",
+ (cause) => {
+ const model = "openai/gpt-5.6-sol";
+ const reason = "No OPENAI_API_KEY was found";
+ const hint = "Set OPENAI_API_KEY.";
+ const decision = decideAdvisorStartup(
+ facts({
+ panelPlans: [{ kind: "refused", spec: model, role: "panel", cause, reason, hint }],
+ })
+ );
+
+ expect(decision.kind).toBe("refuse");
+ if (decision.kind === "refuse") {
+ expect(decision.reason).toBe(
+ `advisor panel model cannot be called: ${model}: ${reason} ${hint}. Remove the model from --advisor, or fix what the reason names (claudish --probe ${model} shows the routing decision).`
+ );
+ }
+ }
+ );
+
+ it.each([
+ { build: { kind: "not-built" } as const, reason: "could not be built" },
+ {
+ build: { kind: "failed", message: "construction failed" } as const,
+ reason: "could not be built: construction failed",
+ },
+ {
+ build: { kind: "refused", message: "unsupported wire", chainContinues: false } as const,
+ reason: "unsupported wire",
+ },
+ ])("refuses a routed panel when its only hop is $build.kind", ({ build, reason }) => {
+ const plan = routedPlan();
+ const chain = builtChain(plan);
+ chain.firstCallableHop = null;
+ chain.builds = [build.kind === "refused" ? { ...build, handler: chain.handler! } : build];
+ const decision = decideAdvisorStartup(facts({ builds: new Map([[chain.key, chain]]) }));
expect(decision.kind).toBe("refuse");
if (decision.kind === "refuse") {
- expect(decision.reason).toContain(model);
- expect(decision.reason).toContain(credentialName);
+ expect(decision.reason).toContain(
+ `${plan.spec}: no hop could be built — OpenAI Codex: ${reason}`
+ );
}
});
- it("proceeds when all panel models have credentials and there is no collector", () => {
+ it("proceeds when all panel chains are callable and there is no collector", () => {
const decision = expectProceed(decideAdvisorStartup(facts()));
expect(decision.effectiveCollector).toBeNull();
+ expect(decision.runtime.plans.has("panel:openai/gpt-5.6-sol")).toBe(true);
});
it("refuses when a user-named collector credential is missing and names it", () => {
- const collector = "anthropic/claude-haiku-4-5";
+ const collectorPlan = collectorRefusal();
const decision = decideAdvisorStartup(
- facts({
- collector,
- collectorDefaulted: false,
- collectorStatus: modelStatus(collector, anthropicRoute, false, "ANTHROPIC_API_KEY"),
- })
+ facts({ collector: collectorPlan.spec, collectorDefaulted: false, collectorPlan })
);
expect(decision.kind).toBe("refuse");
if (decision.kind === "refuse") {
- expect(decision.reason).toContain(collector);
+ expect(decision.reason).toBe(
+ `collector ${collectorPlan.reason}. Name another collector ("a,b:collector"), or end the value with ":" for no collector (claudish --probe haiku shows the routing decision).`
+ );
+ expect(decision.reason).toContain("haiku");
expect(decision.reason).toContain("ANTHROPIC_API_KEY");
}
});
it("drops an unavailable default collector and explains concatenation", () => {
+ const collectorPlan = collectorRefusal();
const decision = expectProceed(
- decideAdvisorStartup(
- facts({
- collector: "haiku",
- collectorDefaulted: true,
- collectorStatus: modelStatus("haiku", anthropicRoute, false, "ANTHROPIC_API_KEY"),
- })
- )
+ decideAdvisorStartup(facts({ collector: "haiku", collectorDefaulted: true, collectorPlan }))
);
expect(decision.effectiveCollector).toBeNull();
- expect(decision.notice.some((line) => line.includes("haiku"))).toBe(true);
- expect(decision.notice.some((line) => line.includes("concatenated"))).toBe(true);
+ expect(decision.notice).toContain(
+ ` collector: none — ${collectorPlan.reason}; panel answers will be concatenated`
+ );
+ expect(decision.runtime.plans.has("collector:haiku")).toBe(false);
});
describe("unresolved collector aliases", () => {
- const unresolvedRoute: AdvisorRoute = {
- ...anthropicRoute,
- unresolvedAlias: "haiku",
- };
-
- it("keeps an unresolved alias uncallable even when its credential is present", () => {
- const status = advisorModelStatus("haiku", unresolvedRoute, { anthropic: true });
+ it.each([true, false])(
+ "keeps an unresolved alias uncallable when credential presence is %p",
+ async (keyPresent) => {
+ const plan = await planAdvisorModel("haiku", "collector", {
+ childAuth: { controlled: false, reason: "proxy-auth", where: "--model" },
+ anthropicKeyPresent: async () => keyPresent,
+ ensureCatalog: async () => {},
+ explain: async () => {
+ throw new Error("A native Claude alias must not reach explainRoute");
+ },
+ });
- expect(status.callable).toBe(false);
- expect(status.unresolved).toBeTruthy();
- expect(status.unresolved).toContain("haiku");
- });
+ expect(plan).toMatchObject({ kind: "refused", cause: "unresolved-alias", spec: "haiku" });
+ const refusal = advisorMemberRefusal(plan, new Map());
+ expect(refusal).not.toBeNull();
+ expect(refusal).toContain("haiku");
+ }
+ );
it("refuses an unresolved collector named by the user", () => {
- const collector = "haiku";
- const panelStatus = [modelStatus("openai/gpt-5.6-sol", openAiRoute)];
- expect(panelStatus.every((status) => status.callable)).toBe(true);
-
const decision = decideAdvisorStartup(
facts({
- collector,
+ collector: "haiku",
collectorDefaulted: false,
- panelStatus,
- collectorStatus: advisorModelStatus(collector, unresolvedRoute, { anthropic: true }),
+ collectorPlan: collectorRefusal("unresolved-alias"),
})
);
expect(decision.kind).toBe("refuse");
- if (decision.kind === "refuse") expect(decision.reason).toContain(collector);
+ if (decision.kind === "refuse") expect(decision.reason).toContain("haiku");
});
it("drops an unresolved default collector and explains concatenation", () => {
- const collector = "haiku";
const decision = expectProceed(
decideAdvisorStartup(
facts({
- collector,
+ collector: "haiku",
collectorDefaulted: true,
- collectorStatus: advisorModelStatus(collector, unresolvedRoute, { anthropic: true }),
+ collectorPlan: collectorRefusal("unresolved-alias"),
})
)
);
expect(decision.effectiveCollector).toBeNull();
- expect(decision.notice.some((line) => line.includes(collector))).toBe(true);
+ expect(decision.notice.some((line) => line.includes("haiku"))).toBe(true);
expect(decision.notice.some((line) => line.includes("concatenat"))).toBe(true);
+ expect(decision.runtime.plans.has("collector:haiku")).toBe(false);
});
it.each([true, false])(
"keeps a resolved collector when collectorDefaulted is %p",
(collectorDefaulted) => {
- const collector = "anthropic/claude-haiku-4-5";
- const resolvedRoute: AdvisorRoute = {
- ...anthropicRoute,
- wireModel: "claude-haiku-4-5",
- };
+ const collector = "claude-haiku-4-5";
const decision = expectProceed(
decideAdvisorStartup(
facts({
collector,
collectorDefaulted,
- collectorStatus: advisorModelStatus(collector, resolvedRoute, { anthropic: true }),
+ collectorPlan: {
+ kind: "claude-native",
+ spec: collector,
+ role: "collector",
+ model: collector,
+ expectedArm: "claude-login",
+ },
})
)
);
expect(decision.effectiveCollector).toBe(collector);
+ expect(decision.runtime.plans.has(`collector:${collector}`)).toBe(true);
}
);
});
@@ -270,41 +357,62 @@ describe("decideAdvisorStartup", () => {
}
});
- it("reports panel routing, billing, subscription isolation, cost, and a claudish-set enable variable", () => {
+ it("reports panel routing, credential billing, cost, and a claudish-set enable variable", () => {
+ const plan = routedPlan("gpt-6.1-sol");
+ plan.hops.push({
+ provider: "openai",
+ displayName: "OpenAI",
+ modelSpec: "[email protected]",
+ wireId: "gpt-6.1-sol",
+ billing: "metered",
+ tier: "native",
+ });
+ plan.explanation.warnings = [
+ {
+ type: "subscription-not-served",
+ providers: ["antigravity"],
+ usedInstead: "openai-codex",
+ message: "Antigravity does not serve this model",
+ },
+ {
+ type: "subscription-credential-unreadable",
+ providers: ["devin"],
+ usedInstead: "openai-codex",
+ message: "Devin credential could not be read",
+ },
+ ];
const decision = expectProceed(
decideAdvisorStartup(
facts({
- panel: ["openai/gpt-5.6-sol", "google/gemini-2.5-pro"],
- panelStatus: [
- modelStatus("openai/gpt-5.6-sol", openAiRoute),
- modelStatus("google/gemini-2.5-pro", googleRoute, true, "GEMINI_API_KEY"),
+ panel: [plan.spec, "haiku"],
+ panelPlans: [
+ plan,
+ {
+ kind: "claude-native",
+ spec: "haiku",
+ role: "panel",
+ model: "claude-haiku-4-5",
+ expectedArm: "claude-login",
+ },
],
})
)
);
- expect(
- decision.notice.some(
- (line) =>
- line.includes("openai/gpt-5.6-sol") &&
- line.includes("api.openai.com") &&
- line.includes("billed per token")
- )
- ).toBe(true);
- expect(
- decision.notice.some(
- (line) =>
- line.includes("google/gemini-2.5-pro") &&
- line.includes("generativelanguage.googleapis.com") &&
- line.includes("billed per token")
- )
- ).toBe(true);
- expect(decision.notice.some((line) => line.includes("never use a subscription"))).toBe(true);
- expect(
- decision.notice.some(
- (line) => line.toLowerCase().includes("cost") && line.includes("FULL conversation")
- )
- ).toBe(true);
+ expect(decision.notice).toContain(" panel (each model is routed like a main-model request):");
+ expect(decision.notice).toContain(
+ " gpt-6.1-sol -> OpenAI Codex (subscription); then OpenAI (metered, billed per token) (a rejected subscription login is reported, never routed onto a metered hop)"
+ );
+ expect(decision.notice).toContain(
+ " haiku -> api.anthropic.com (subscription: your Claude Code login; if a request carries none: its x-api-key or ANTHROPIC_API_KEY, metered, billed per token)"
+ );
+ for (const warning of plan.explanation.warnings) {
+ expect(decision.notice).toContain(` note: ${warning.message}`);
+ }
+ expect(decision.notice.join("\n")).not.toContain("never use a subscription");
+ expect(decision.notice).toContain(
+ " cost: each advisor call sends the FULL conversation to every panel model, uncached; subscription hops draw on the plan's allowance, metered hops bill per token; routing keeps using the last hop that served"
+ );
expect(
decision.notice.some(
(line) => line.includes(ENABLE_ENV_VAR) && line.includes("set by claudish")
@@ -355,8 +463,27 @@ describe("decideAdvisorStartup", () => {
});
describe("evaluateAdvisorStartup", () => {
- it("returns null without --advisor and does not resolve credentials", async () => {
- let credentialLookups = 0;
+ function handlerSet(): HandlerSet {
+ const unexpected = () => {
+ throw new Error("Injected chain builder must not call a provider getter");
+ };
+ return {
+ openRouter: unexpected,
+ poe: unexpected,
+ local: unexpected,
+ remote: unexpected,
+ openRouterHandlers: new Map(),
+ localProviderHandlers: new Map(),
+ remoteProviderHandlers: new Map(),
+ poeHandlers: new Map(),
+ };
+ }
+
+ it("returns null without --advisor and performs no planning, credential lookup or construction", async () => {
+ const plan = mock(async () => routedPlan());
+ const anthropicKeyPresent = mock(async () => false);
+ const createHandlerSet = mock(handlerSet);
+ const buildChains = mock(async () => new Map<string, AdvisorChainBuild>());
const toolEnv = resolveAdvisorToolEnv(config({ advisor: false }), {});
const decision = await evaluateAdvisorStartup(
@@ -364,14 +491,114 @@ describe("evaluateAdvisorStartup", () => {
toolEnv,
{},
{
- resolveCredentials: async () => {
- credentialLookups += 1;
- return {};
- },
+ childAuth: { controlled: true },
+ plan,
+ anthropicKeyPresent,
+ createHandlerSet,
+ buildChains,
}
);
expect(decision).toBeNull();
- expect(credentialLookups).toBe(0);
+ expect(plan).not.toHaveBeenCalled();
+ expect(anthropicKeyPresent).not.toHaveBeenCalled();
+ expect(createHandlerSet).not.toHaveBeenCalled();
+ expect(buildChains).not.toHaveBeenCalled();
+ });
+
+ it.each([true, false])(
+ "plans members sequentially and shuts built chains down only on refusal (%p)",
+ async (refusePanel) => {
+ const panel = ["gpt-6.1-sol", "[email protected]"];
+ const collector = "claude-opus-5-5";
+ const events: string[] = [];
+ let planning = false;
+ const plans = panel.map((spec) => routedPlan(spec));
+ const chains = new Map(
+ plans.map((plan) => {
+ const chain = builtChain(plan);
+ if (refusePanel && plan.spec === panel[1]) {
+ chain.firstCallableHop = null;
+ chain.handler = null;
+ chain.builds = [{ kind: "not-built" }];
+ }
+ return [chain.key, chain];
+ })
+ );
+ const set = handlerSet();
+ const createHandlerSet = mock(() => set);
+ const buildChains = mock(async (routed: readonly RoutedPlan[], receivedSet: HandlerSet) => {
+ expect(planning).toBe(false);
+ expect(routed).toEqual(plans);
+ expect(receivedSet).toBe(set);
+ events.push("build");
+ return chains;
+ });
+ const shutdownChains = mock(async (builds: Iterable<AdvisorChainBuild>) => {
+ expect([...builds]).toEqual([...chains.values()]);
+ });
+ const launch = config({ advisor: true, advisorModels: panel, advisorCollector: collector });
+ const toolEnv = resolveAdvisorToolEnv(launch, {});
+ const decision = await evaluateAdvisorStartup(launch, toolEnv, toolEnv.vars, {
+ childAuth: { controlled: true },
+ anthropicKeyPresent: async () => false,
+ async plan(spec, role, deps) {
+ expect(planning).toBe(false);
+ expect(deps.childAuth).toEqual({ controlled: true });
+ planning = true;
+ events.push(`start:${role}:${spec}`);
+ await Promise.resolve();
+ events.push(`end:${role}:${spec}`);
+ planning = false;
+ return role === "panel"
+ ? plans[panel.indexOf(spec)]
+ : { kind: "claude-native", spec, role, model: spec, expectedArm: "claude-login" };
+ },
+ createHandlerSet,
+ buildChains,
+ shutdownChains,
+ });
+
+ expect(events).toEqual([
+ `start:panel:${panel[0]}`,
+ `end:panel:${panel[0]}`,
+ `start:panel:${panel[1]}`,
+ `end:panel:${panel[1]}`,
+ `start:collector:${collector}`,
+ `end:collector:${collector}`,
+ "build",
+ ]);
+ expect(createHandlerSet).toHaveBeenCalledTimes(1);
+ expect(buildChains).toHaveBeenCalledTimes(1);
+ expect(decision?.kind).toBe(refusePanel ? "refuse" : "proceed");
+ expect(shutdownChains).toHaveBeenCalledTimes(refusePanel ? 1 : 0);
+ }
+ );
+
+ it("does not construct chains for a Claude-only panel", async () => {
+ const launch = config({
+ advisor: true,
+ advisorModels: ["claude-opus-5-5"],
+ advisorCollector: null,
+ });
+ const toolEnv = resolveAdvisorToolEnv(launch, {});
+ const createHandlerSet = mock(handlerSet);
+ const buildChains = mock(async () => new Map<string, AdvisorChainBuild>());
+ const decision = await evaluateAdvisorStartup(launch, toolEnv, toolEnv.vars, {
+ childAuth: { controlled: true },
+ anthropicKeyPresent: async () => false,
+ plan: async (spec, role) => ({
+ kind: "claude-native",
+ spec,
+ role,
+ model: spec,
+ expectedArm: "claude-login",
+ }),
+ createHandlerSet,
+ buildChains,
+ });
+
+ expect(decision?.kind).toBe("proceed");
+ expect(buildChains).not.toHaveBeenCalled();
});
});
diff --git a/packages/cli/src/advisor-startup.ts b/packages/cli/src/advisor-startup.ts
index e514e289..accf7b86 100644
--- a/packages/cli/src/advisor-startup.ts
+++ b/packages/cli/src/advisor-startup.ts
@@ -1,5 +1,6 @@
/**
- * `--advisor` startup: the refusals and the notice.
+ * `--advisor` startup: plan every member, build the routed chains, then refuse
+ * or print the notice.
*
* Design rule (requirements.md, decided Q3): anything claudish can determine at
* LAUNCH is a refusal at startup with a named reason and a non-zero exit. Runtime
@@ -14,25 +15,28 @@
* RESOLVED provider's transport and a probe of that format's
* `convertTools`, never from a model list.
* 3. No panel models.
- * 4. ANY panel model whose credential does not resolve. Each panel model
- * calls one host with one metered key (never a subscription); a missing
- * key is known now, so it is refused now rather than returning an error
- * section on every advisor call.
- * 5. A collector the user NAMED whose credential does not resolve. A
- * DEFAULTED collector (`haiku`, supplied by parseAdvisorFlag) is instead
- * dropped — see `decideAdvisorStartup`.
+ * 4. ANY panel member whose plan is refused (routing found no route, a wire
+ * marker error, a Claude selector, a Claude member with no credential)
+ * or whose built chain has no callable hop. Every one is listed.
+ * 5. A collector the user NAMED that is refused likewise. A DEFAULTED
+ * collector (`haiku`, supplied by parseAdvisorFlag) is instead dropped —
+ * see `decideAdvisorStartup`.
*
- * Which host and which credential a model uses is NOT decided here: it is the
- * advisor call path's own routing (`advisorRouteFor`, native-handler-advisor.ts),
- * so startup and runtime cannot drift apart. Credentials are resolved through
- * the credential authority — env, config, keychain, 1Password — exactly as the
- * runtime resolves them; an `--advisor` launch is their point of need.
+ * Where a member goes is decided by `planAdvisorModel` (handlers/advisor-route-plan.ts),
+ * which wraps `explainRoute`, the function main routing is derived from, and
+ * decides a Claude name first, as the proxy does. Each routed member's chain is
+ * then BUILT here, before the port is bound, with the getters main routing
+ * uses, in a handler set of its own (`requestRole: "advisor"`). The notice, the
+ * refusal and the runtime all read those plans and builds, so they cannot
+ * disagree, and there is no lazy path. Members are planned, and chains built,
+ * strictly one after another: a build may resolve a credential value
+ * (1Password, Keychain), and concurrent handshakes are what 1Password refuses.
*
- * Everything that decides is exported and side-effect free
- * (`decideAdvisorStartup` and its helpers). The only I/O is credential
- * resolution, isolated in `resolveAdvisorCredentials` and injectable into
- * `evaluateAdvisorStartup`. Printing, exiting and applying the effective
- * collector belong to the caller (index.ts).
+ * `decideAdvisorStartup` is pure. `evaluateAdvisorStartup` gathers its facts
+ * (the planning and building above, injectable for tests) and shuts every
+ * built handler down when it refuses. Printing, exiting, applying the
+ * effective collector and handing the runtime to the proxy belong to the
+ * caller (index.ts).
*
* Output channel: the caller writes the notice to STDERR before the child
* spawns. In `-p` mode stdout carries Claude Code's (often machine-readable)
@@ -42,21 +46,32 @@
*/
import { OllamaAPIFormat } from "./adapters/ollama-api-format.js";
-import type { AdvisorToolEnv } from "./claude-runner.js";
-import { ADVISOR_TOOL_ENV_VAR } from "./claude-runner.js";
+import type { AdvisorToolEnv, ChildAuthControl } from "./claude-runner.js";
+import { ADVISOR_TOOL_ENV_VAR, childAuthControl } from "./claude-runner.js";
+import type { AdvisorRuntime } from "./handlers/advisor-panel-gateway.js";
import {
- ADVISOR_AUTHORITY_PROVIDER,
- type AdvisorRoute,
- type AdvisorRouteKind,
- advisorRouteFor,
- resolveAdvisorCredential,
-} from "./handlers/native-handler-advisor.js";
+ type AdvisorChainBuild,
+ type AdvisorModelPlan,
+ type AdvisorRole,
+ type PlanAdvisorDeps,
+ advisorChainKey,
+ buildAdvisorChains,
+ describeAdvisorPlan,
+ describeAdvisorRefusal,
+ planAdvisorModel,
+ shutdownAdvisorChains,
+} from "./handlers/advisor-route-plan.js";
+import { type HandlerSet, createHandlerSet } from "./handlers/handler-set.js";
+import { resolveAdvisorCredential } from "./handlers/native-handler-advisor.js";
import { parseModelSpec } from "./providers/model-parser.js";
import { nativeRouteFor } from "./providers/native-route.js";
import { type TransportType, getProviderByName } from "./providers/provider-definitions.js";
import { resolveModelProvider } from "./providers/provider-resolver.js";
+import { traceSpan } from "./startup-trace.js";
import type { ClaudishConfig } from "./types.js";
+export type { AdvisorRole };
+
/** Claude Code's kill switch for the advisor tool. Wins over the enable variable. */
export const ADVISOR_DISABLE_ENV_VAR = "CLAUDE_CODE_DISABLE_ADVISOR_TOOL";
@@ -70,153 +85,6 @@ export function isClaudeCodeBoolTrue(value: string | undefined): boolean {
return v === "1" || v === "true" || v === "yes" || v === "on";
}
-// ---------------------------------------------------------------------------
-// Advisor routing — owned by the advisor call path, NOT by this file
-// ---------------------------------------------------------------------------
-
-/** The credential an advisor call signs with. */
-export type AdvisorCredential = AdvisorRouteKind;
-
-/** Where one advisor model's request goes; the call path's own type. */
-export type { AdvisorRoute };
-
-export type AdvisorRole = "panel" | "collector";
-
-/**
- * The ONE place this module asks where an advisor model is sent. It is the
- * advisor call path's own routing (`advisorRouteFor`), so the host and the
- * credential startup checks are the ones the first advisor call will use.
- *
- * The credential SET is still computed here, not by `advisorCredentialsFor`:
- * that one counts the collector only when the panel has more than one model
- * (the only case in which it runs), while startup checks a collector the user
- * named whatever the panel size.
- */
-export function routeAdvisorModel(modelSpec: string, role: AdvisorRole): AdvisorRoute {
- return advisorRouteFor(modelSpec, role);
-}
-
-// ---------------------------------------------------------------------------
-// Credentials — presence through the credential authority
-// ---------------------------------------------------------------------------
-
-/**
- * The key's name as a user would set it. Read from the provider definition (the
- * same one the authority registered), except `anthropic`, whose
- * `native-anthropic` definition deliberately carries no env var.
- *
- * The registry name per credential is `ADVISOR_AUTHORITY_PROVIDER`, owned by
- * the advisor call path — the same table its credential lookup reads.
- */
-export function advisorCredentialEnvName(credential: AdvisorCredential): string {
- if (credential === "anthropic") return "ANTHROPIC_API_KEY";
- return (
- getProviderByName(ADVISOR_AUTHORITY_PROVIDER[credential])?.apiKeyEnvVar ||
- `a ${credential} API key`
- );
-}
-
-/**
- * A second env var the credential ALSO accepts, for the refusal sentence. Only
- * Google has one: `resolveAdvisorCredential` falls back to GOOGLE_API_KEY,
- * which the authority's `google` provider (GEMINI_API_KEY) never reads.
- */
-function alsoAcceptedEnvName(credential: AdvisorCredential): string | null {
- return credential === "google" ? "GOOGLE_API_KEY" : null;
-}
-
-/** Whether each credential resolved. Booleans only — no secret is kept. */
-export type AdvisorCredentialPresence = Partial<Record<AdvisorCredential, boolean>>;
-
-/**
- * Resolve credential PRESENCE for only the credentials this launch needs. The
- * one side-effecting function in this module (it may open 1Password, as the
- * runtime would on the first advisor call); `evaluateAdvisorStartup` takes it
- * as a dependency.
- *
- * It asks `resolveAdvisorCredential`, the advisor call path's OWN lookup, so
- * this check cannot disagree with the runtime about what is callable. Two
- * lookups disagreed before: the runtime accepted GOOGLE_API_KEY and this one
- * did not, so a working Google panel model was refused at launch.
- *
- * PRESENCE ONLY: the resolved value is tested and dropped here — never
- * returned, logged or printed. The api-key half of a credential ALWAYS returns
- * an object (`{headers:{}}`, or one carrying only static non-auth headers), so
- * neither the object nor "some header is non-empty" proves a key (CLAUDE.md);
- * the shared lookup reads the auth headers alone.
- */
-export async function resolveAdvisorCredentials(
- needed: ReadonlySet<AdvisorCredential>
-): Promise<AdvisorCredentialPresence> {
- const present = async (credential: AdvisorCredential): Promise<boolean> =>
- Boolean(await resolveAdvisorCredential(credential));
- const list = [...needed];
- const results = await Promise.all(list.map(present));
- const out: AdvisorCredentialPresence = {};
- list.forEach((c, i) => {
- out[c] = results[i];
- });
- return out;
-}
-
-/** One panel or collector model, as startup sees it. */
-export interface AdvisorModelStatus {
- model: string;
- route: AdvisorRoute;
- callable: boolean;
- /** Env-var name of the credential it needs, e.g. `OPENAI_API_KEY`. */
- credentialName: string;
- /**
- * Why this model is uncallable for a reason that is NOT a missing credential
- * — today, an alias the live catalog could not resolve to a wire id
- * (`AdvisorRoute.unresolvedAlias`). Present ⇒ `callable` is false, and this
- * sentence replaces the missing-credential one in the refusal or the notice.
- */
- unresolved?: string;
-}
-
-/** Pure: status of one model given its route and credential presence. */
-export function advisorModelStatus(
- model: string,
- route: AdvisorRoute,
- presence: AdvisorCredentialPresence
-): AdvisorModelStatus {
- const credentialName = advisorCredentialEnvName(route.credential);
- // A key cannot make an unsendable id sendable, so this outranks presence.
- if (route.unresolvedAlias) {
- return {
- model,
- route,
- callable: false,
- credentialName,
- unresolved:
- `${model} is a claudish alias, not a model id ${route.host} accepts, and the model ` +
- "catalog holds no id for it (it is cold or has never been fetched), so claudish would " +
- `have to POST "${route.unresolvedAlias}" verbatim`,
- };
- }
- return {
- model,
- route,
- callable: presence[route.credential] === true,
- credentialName,
- };
-}
-
-/** The sentence a refusal uses for a model whose credential is missing. */
-export function describeMissingCredential(s: AdvisorModelStatus): string {
- const also = alsoAcceptedEnvName(s.route.credential);
- return (
- `${s.model} calls ${s.route.host} and needs ${s.credentialName}${also ? ` (or ${also})` : ""}; ` +
- "none found in env, config, keychain or 1Password"
- );
-}
-
-/** Why a model cannot be called: its own reason when it has one, else the credential. */
-function describeUncallable(s: AdvisorModelStatus): string {
- return s.unresolved ?? describeMissingCredential(s);
-}
-
// ---------------------------------------------------------------------------
// Main-model path — can it carry a tool at all?
// ---------------------------------------------------------------------------
@@ -362,8 +230,14 @@ export interface AdvisorLaunchFacts {
/** The environment the child Claude Code will inherit. */
childEnv: Record<string, string | undefined>;
toolEnv: AdvisorToolEnv;
- panelStatus: AdvisorModelStatus[];
- collectorStatus: AdvisorModelStatus | null;
+ /** Index-aligned with `panel`: each member's startup plan. */
+ panelPlans: AdvisorModelPlan[];
+ /** The collector's plan; null when no collector was given. */
+ collectorPlan: AdvisorModelPlan | null;
+ /** Every routed member's built chain, keyed `"<role>:<spec>"` (`advisorChainKey`). */
+ builds: ReadonlyMap<string, AdvisorChainBuild>;
+ /** The launch's `childAuthControl` decision the Claude plans were made with. */
+ childAuth: ChildAuthControl;
}
export type AdvisorStartupDecision =
@@ -377,12 +251,14 @@ export type AdvisorStartupDecision =
* caller MUST apply it to the config before the proxy is created.
*/
effectiveCollector: string | null;
+ /** The plans and chains the proxy's panel gateway runs on (no lazy path). */
+ runtime: AdvisorRuntime;
};
/**
- * Pure: the refusals decidable WITHOUT credentials (1–3). Split out so the
- * caller can skip credential lookups — and any 1Password prompt — for a launch
- * that is refused anyway.
+ * Pure: the refusals decidable WITHOUT planning (1–3). Split out so the
+ * caller can skip routing, credential lookups and handler construction — and
+ * any 1Password prompt — for a launch that is refused anyway.
*/
export function refusalBeforeCredentials(
facts: Pick<AdvisorLaunchFacts, "childEnv" | "mainModels" | "panel">
@@ -414,63 +290,129 @@ export function refusalBeforeCredentials(
return null;
}
-function describeRoute(s: AdvisorModelStatus): string {
- return `${s.model} -> ${s.route.host} (${s.credentialName}, billed per token)`;
+/**
+ * Pure: why a member cannot be called, or null when it can. A refused plan
+ * reads as its refusal; a routed plan whose chain has no callable hop names
+ * every hop's outcome. A Claude member is callable once planned: its plan
+ * already refused the launch that has no credential for it.
+ */
+export function advisorMemberRefusal(
+ plan: AdvisorModelPlan,
+ builds: ReadonlyMap<string, AdvisorChainBuild>
+): string | null {
+ if (plan.kind === "refused") return describeAdvisorRefusal(plan);
+ if (plan.kind === "claude-native") return null;
+ const build = builds.get(advisorChainKey(plan.role, plan.spec));
+ if (!build) return `${plan.spec}: its chain was not built at startup`;
+ if (build.firstCallableHop === null || !build.handler) {
+ return describeAdvisorRefusal({ plan, build });
+ }
+ return null;
+}
+
+/**
+ * The notice lines for one member: its plan described from its built chain,
+ * then each routing warning (a subscription that does not serve the model, or
+ * whose credential could not be read) as a `note:` line under it. `route()`
+ * prints those per request; nothing prints them for an advisor call.
+ */
+function memberNoticeLines(
+ plan: AdvisorModelPlan,
+ builds: ReadonlyMap<string, AdvisorChainBuild>,
+ indent: string
+): string[] {
+ const build =
+ plan.kind === "routed" ? builds.get(advisorChainKey(plan.role, plan.spec)) : undefined;
+ const lines = [describeAdvisorPlan(plan, build)];
+ if (plan.kind === "routed") {
+ for (const warning of plan.explanation.warnings) {
+ if (warning.type === "rule-problem") continue;
+ lines.push(`${indent} note: ${warning.message}`);
+ }
+ }
+ return lines;
+}
+
+/** The `claudish --probe` hint of a refusal: the first refused member's spec. */
+function probeHint(specs: string[]): string {
+ return `claudish --probe ${specs[0] ?? "<model>"} shows the routing decision`;
}
/**
- * Pure: refuse, or proceed with the notice lines and the effective collector.
+ * Pure: the collector's part of the decision — refuse a NAMED collector that
+ * cannot be called, drop a DEFAULTED one, else keep it — and its notice lines.
+ */
+function decideCollector(
+ facts: Pick<AdvisorLaunchFacts, "collector" | "collectorDefaulted" | "collectorPlan" | "builds">
+):
+ | { kind: "refuse"; reason: string }
+ | { kind: "keep"; effectiveCollector: string | null; lines: string[] } {
+ const cp = facts.collectorPlan;
+ if (!cp) return { kind: "keep", effectiveCollector: facts.collector, lines: ["none"] };
+ const refusal = advisorMemberRefusal(cp, facts.builds);
+ if (!refusal) {
+ return {
+ kind: "keep",
+ effectiveCollector: facts.collector,
+ lines: memberNoticeLines(cp, facts.builds, " "),
+ };
+ }
+ if (!facts.collectorDefaulted) {
+ return {
+ kind: "refuse",
+ reason:
+ `collector ${refusal}. ` +
+ 'Name another collector ("a,b:collector"), or end the value with ":" for no collector ' +
+ `(${probeHint([cp.spec])}).`,
+ };
+ }
+ // DEFAULTED collector that cannot be called: proceed WITHOUT it rather than
+ // refuse. Every multi-model panel with no ":" defaults to `haiku`, and a
+ // launch with a non-Claude --model has no Claude login and usually no
+ // ANTHROPIC_API_KEY — refusing would break the default. The panel's answers
+ // are concatenated instead, as the notice says.
+ return {
+ kind: "keep",
+ effectiveCollector: null,
+ lines: [`none — ${refusal}; panel answers will be concatenated`],
+ };
+}
+
+/**
+ * Pure: refuse, or proceed with the notice lines, the effective collector and
+ * the runtime the proxy's panel gateway runs on.
*/
export function decideAdvisorStartup(facts: AdvisorLaunchFacts): AdvisorStartupDecision {
const early = refusalBeforeCredentials(facts);
if (early) return { kind: "refuse", reason: early };
- const missingPanel = facts.panelStatus.filter((s) => !s.callable);
- if (missingPanel.length > 0) {
+ const refused = facts.panelPlans
+ .map((plan) => ({ plan, reason: advisorMemberRefusal(plan, facts.builds) }))
+ .filter((r): r is { plan: AdvisorModelPlan; reason: string } => r.reason !== null);
+ if (refused.length > 0) {
return {
kind: "refuse",
reason:
- `advisor panel model${missingPanel.length > 1 ? "s" : ""} cannot be called: ` +
- `${missingPanel.map(describeUncallable).join("; ")}. ` +
- "Set the key, or remove the model from --advisor.",
+ `advisor panel model${refused.length > 1 ? "s" : ""} cannot be called: ` +
+ `${refused.map((r) => r.reason).join("; ")}. ` +
+ "Remove the model from --advisor, or fix what the reason names " +
+ `(${probeHint(refused.map((r) => r.plan.spec))}).`,
};
}
- let effectiveCollector = facts.collector;
- let collectorLine = "none";
- const cs = facts.collectorStatus;
- if (cs && !cs.callable) {
- if (!facts.collectorDefaulted) {
- return {
- kind: "refuse",
- reason:
- `collector ${describeUncallable(cs)}. ` +
- 'Set the key, name another collector ("a,b:collector"), or end the value with ":" for no collector.',
- };
- }
- // DEFAULTED collector that cannot be called: proceed WITHOUT it rather than
- // refuse. Every multi-model panel with no ":" defaults to `haiku`, and a
- // Claude Code OAuth session usually has no ANTHROPIC_API_KEY — refusing
- // would break the default. Before this rule the same case ended in
- // concatenation too, after a silent 401 from the collector call
- // (fetchMultiModelAdvice's fallback); the end result is unchanged, it is
- // only made visible, and the call that cannot succeed is skipped.
- effectiveCollector = null;
- collectorLine = cs.unresolved
- ? // Same rule, other cause: an alias the catalog could not resolve. Sending
- // it anyway is a guaranteed rejection from the endpoint, so the DEFAULTED
- // collector is dropped here too rather than failing on the first call.
- `none — ${cs.unresolved}; panel answers will be concatenated`
- : `none — the default collector ${cs.model} needs ${cs.credentialName}, which was not ` +
- "found; panel answers will be concatenated";
- } else if (cs) {
- collectorLine = describeRoute(cs);
- }
+ const collectorDecision = decideCollector(facts);
+ if (collectorDecision.kind === "refuse") return collectorDecision;
+ const { effectiveCollector, lines: collectorLines } = collectorDecision;
+ const cp = facts.collectorPlan;
const notice: string[] = ["[claudish] --advisor is on for this launch"];
- notice.push(" panel (panel calls never use a subscription):");
- for (const s of facts.panelStatus) notice.push(` ${describeRoute(s)}`);
- notice.push(` collector: ${collectorLine}`);
+ notice.push(" panel (each model is routed like a main-model request):");
+ for (const plan of facts.panelPlans) {
+ const [first, ...notes] = memberNoticeLines(plan, facts.builds, " ");
+ notice.push(` ${first}`, ...notes);
+ }
+ const [collectorFirst, ...collectorNotes] = collectorLines;
+ notice.push(` collector: ${collectorFirst}`, ...collectorNotes);
const main =
facts.mainModels.length === 0
@@ -500,16 +442,43 @@ export function decideAdvisorStartup(facts: AdvisorLaunchFacts): AdvisorStartupD
notice.push(
" cost: each advisor call sends the FULL conversation to every panel model, uncached; " +
- "cost grows with panel size and transcript length"
+ "subscription hops draw on the plan's allowance, metered hops bill per token; " +
+ "routing keeps using the last hop that served"
);
- return { kind: "proceed", notice, effectiveCollector };
+
+ const plans = new Map<string, AdvisorModelPlan>();
+ for (const plan of facts.panelPlans) plans.set(advisorChainKey("panel", plan.spec), plan);
+ if (cp && effectiveCollector !== null) plans.set(advisorChainKey("collector", cp.spec), cp);
+ return {
+ kind: "proceed",
+ notice,
+ effectiveCollector,
+ runtime: { plans, chains: facts.builds, childAuth: facts.childAuth },
+ };
}
-/** Injectable I/O for `evaluateAdvisorStartup`. */
+/** Injectable I/O for `evaluateAdvisorStartup`; every field defaults to the real thing. */
export interface AdvisorStartupDeps {
- resolveCredentials: (
- needed: ReadonlySet<AdvisorCredential>
- ) => Promise<AdvisorCredentialPresence>;
+ /** Default `childAuthControl(config, childEnv)`. */
+ childAuth?: ChildAuthControl;
+ /** Presence of ANTHROPIC_API_KEY; default the advisor's own lookup, value dropped. */
+ anthropicKeyPresent?: () => Promise<boolean>;
+ /** Default `planAdvisorModel`. */
+ plan?: (spec: string, role: AdvisorRole, deps: PlanAdvisorDeps) => Promise<AdvisorModelPlan>;
+ /** Default: a fresh advisor handler set (`requestRole: "advisor"`, no token file). */
+ createHandlerSet?: () => HandlerSet;
+ /** Default `buildAdvisorChains`. */
+ buildChains?: (
+ plans: readonly Extract<AdvisorModelPlan, { kind: "routed" }>[],
+ set: HandlerSet
+ ) => Promise<Map<string, AdvisorChainBuild>>;
+ /** Default `shutdownAdvisorChains`. */
+ shutdownChains?: (builds: Iterable<AdvisorChainBuild>) => Promise<void>;
+}
+
+/** Presence only: the resolved value is tested and dropped here, never returned or printed. */
+async function anthropicKeyPresent(): Promise<boolean> {
+ return Boolean(await resolveAdvisorCredential("anthropic"));
}
/**
@@ -517,14 +486,16 @@ export interface AdvisorStartupDeps {
* was not given — the advisor is opt-in per launch (N1), and this reads only
* the parsed CLI config, never stored config.
*
- * Credentials are resolved only when the launch survives the credential-free
- * refusals, and only for the credentials the panel and collector use.
+ * Planning and building run only when the launch survives the refusals that
+ * need no planning, and strictly in order: every member's plan (panel, then
+ * collector, each in its own trace span), then every routed member's chain. A
+ * refusal shuts down every handler that was built, so nothing outlives it.
*/
export async function evaluateAdvisorStartup(
config: ClaudishConfig,
toolEnv: AdvisorToolEnv,
childEnv: Record<string, string | undefined> = process.env,
- deps: AdvisorStartupDeps = { resolveCredentials: resolveAdvisorCredentials }
+ deps: AdvisorStartupDeps = {}
): Promise<AdvisorStartupDecision | null> {
if (!config.advisor) return null;
@@ -535,36 +506,59 @@ export async function evaluateAdvisorStartup(
const early = refusalBeforeCredentials({ childEnv, mainModels, panel });
if (early) return { kind: "refuse", reason: early };
- // A spec the routing cannot turn into a valid wire id is refused HERE, with
- // the reason the router gave. `advisorRouteFor` throws for exactly that case
- // (e.g. a subscription prefix that is not an OpenRouter vendor namespace and
- // whose model the catalog does not place on OpenRouter); building the invalid
- // id anyway used to pass startup on the strength of the OpenRouter key alone
- // and fail on the first advisor call.
- let panelRoutes: AdvisorRoute[];
- let collectorRoute: AdvisorRoute | null;
- try {
- panelRoutes = panel.map((m) => routeAdvisorModel(m, "panel"));
- collectorRoute = collector ? routeAdvisorModel(collector, "collector") : null;
- } catch (err) {
- return {
- kind: "refuse",
- reason: err instanceof Error ? err.message : String(err),
- };
+ const childAuth = deps.childAuth ?? childAuthControl(config, childEnv as NodeJS.ProcessEnv);
+ const planDeps: PlanAdvisorDeps = {
+ childAuth,
+ anthropicKeyPresent: deps.anthropicKeyPresent ?? anthropicKeyPresent,
+ };
+ const plan = deps.plan ?? planAdvisorModel;
+
+ const panelPlans: AdvisorModelPlan[] = [];
+ for (const [i, spec] of panel.entries()) {
+ panelPlans.push(
+ await traceSpan(`startup:advisor-plan:panel:${i}`, () => plan(spec, "panel", planDeps))
+ );
}
- const needed = new Set<AdvisorCredential>(panelRoutes.map((r) => r.credential));
- if (collectorRoute) needed.add(collectorRoute.credential);
- const presence = await deps.resolveCredentials(needed);
+ const collectorPlan = collector
+ ? await traceSpan("startup:advisor-plan:collector:0", () =>
+ plan(collector, "collector", planDeps)
+ )
+ : null;
- return decideAdvisorStartup({
+ const routed = [...panelPlans, ...(collectorPlan ? [collectorPlan] : [])].filter(
+ (p): p is Extract<AdvisorModelPlan, { kind: "routed" }> => p.kind === "routed"
+ );
+ const set =
+ deps.createHandlerSet?.() ??
+ createHandlerSet({
+ requestRole: "advisor",
+ // No token file is written for this set, so the port is never read.
+ port: 0,
+ isInteractive: config.interactive,
+ // The notice describes each member; an `[Auto-route]` line would repeat it.
+ quiet: true,
+ });
+ const builds =
+ routed.length > 0
+ ? await traceSpan("startup:advisor-build", () =>
+ (deps.buildChains ?? buildAdvisorChains)(routed, set)
+ )
+ : new Map<string, AdvisorChainBuild>();
+
+ const decision = decideAdvisorStartup({
panel,
collector,
collectorDefaulted: config.advisorCollectorDefaulted === true,
mainModels,
childEnv,
toolEnv,
- panelStatus: panel.map((m, i) => advisorModelStatus(m, panelRoutes[i], presence)),
- collectorStatus:
- collector && collectorRoute ? advisorModelStatus(collector, collectorRoute, presence) : null,
+ panelPlans,
+ collectorPlan,
+ builds,
+ childAuth,
});
+ if (decision.kind === "refuse") {
+ await (deps.shutdownChains ?? shutdownAdvisorChains)(builds.values());
+ }
+ return decision;
}
diff --git a/packages/cli/src/handlers/advisor-decorator.test.ts b/packages/cli/src/handlers/advisor-decorator.test.ts
index 8b5359f7..d250d17b 100644
--- a/packages/cli/src/handlers/advisor-decorator.test.ts
+++ b/packages/cli/src/handlers/advisor-decorator.test.ts
@@ -49,7 +49,6 @@ function createInner(
function wrap(inner: ModelHandler, warn: (message: string) => void = () => {}): ModelHandler {
return withAdvisorSwap(inner, cfg, {
presence: createAdvisorPresenceMonitor(warn),
- resolveKeys: async () => ({}),
});
}
@@ -122,7 +121,7 @@ describe("withAdvisorSwap", () => {
const inner = createInner();
const warn = mock((_message: string) => {});
const presence = createAdvisorPresenceMonitor(warn);
- const deps = { presence, resolveKeys: async () => ({}) };
+ const deps = { presence };
const firstWrapper = withAdvisorSwap(inner, cfg, deps);
const forwardingHandler: ModelHandler = {
handle: (context, payload) => firstWrapper.handle(context, payload),
diff --git a/packages/cli/src/handlers/advisor-decorator.ts b/packages/cli/src/handlers/advisor-decorator.ts
index 7aa5c8a5..2c7232d1 100644
--- a/packages/cli/src/handlers/advisor-decorator.ts
+++ b/packages/cli/src/handlers/advisor-decorator.ts
@@ -46,18 +46,20 @@ import type { Context } from "hono";
// `metadata.user_id`'s JSON `session_id`. NOT the same-named function in
// session-events/index.ts, which takes the metadata object instead.
import { extractSessionId } from "../behavior/harness.js";
+import type { ChildAuthControl } from "../claude-runner.js";
import { log, logStderr } from "../logger.js";
+import type { AdvisorPanelGateway } from "./advisor-route-plan.js";
import {
- type AdvisorApiKeys,
- type AdvisorRouteKind,
type AdvisorSwapConfig,
type AdvisorToolResult,
+ type ClaudeAdvisorAuth,
+ type ClaudeRequestFacts,
NO_SESSION_BUCKET,
- advisorCredentialsFor,
+ type RunAdvisorCallParams,
+ claudeRequestFacts,
createAdvisorStreamScanner,
findPendingAdvisorToolResults,
getAdvisorCall,
- isPlaceholderAnthropicKey,
joinOrStartAdvisorCall,
logAdvisorEvent,
markAdvisorCallConsumed,
@@ -67,6 +69,7 @@ import {
recoverUnassociatedAdvisorResult,
reportUnrecordedAdvisorCalls,
resolveAdvisorCredential,
+ resolveClaudeAdvisorAuth,
rewriteAdvisorToolResults,
runAdvisorCall,
stubAdvisorAdvice,
@@ -99,43 +102,6 @@ const ADVISOR_TOOL_NAME = "advisor";
*/
const MAX_JSON_SCAN_CHARS = 16 * 1024 * 1024;
-// ---------------------------------------------------------------------------
-// Advisor credentials
-// ---------------------------------------------------------------------------
-
-/**
- * Resolve the advisor keys for one request through `resolveAdvisorCredential`
- * (native-handler-advisor.ts) — the SAME function the `--advisor` startup
- * check asks, so a launch startup accepted is a launch this can sign, and a
- * launch it refuses is one that really has no key. They used to be two
- * lookups, and the runtime's extra GOOGLE_API_KEY fallback made startup refuse
- * launches the runtime would have served.
- *
- * Only the credentials the configured routes need (`advisorCredentialsFor`)
- * are resolved, so an unused provider never triggers a 1Password handshake.
- *
- * The one rule that belongs HERE and not in the shared resolver: for the
- * Anthropic collector, this request's own inbound `x-api-key` wins when it is
- * a real key. The inbound `authorization` header is Claude Code's OAuth bearer
- * and is NEVER sent to a collector.
- */
-export async function resolveAdvisorKeys(
- needed: ReadonlySet<AdvisorRouteKind>,
- inboundApiKey: string | undefined
-): Promise<AdvisorApiKeys> {
- const anthropicKey = async (): Promise<string | undefined> => {
- if (inboundApiKey && !isPlaceholderAnthropicKey(inboundApiKey)) return inboundApiKey;
- return resolveAdvisorCredential("anthropic");
- };
- const [openrouter, google, openai, anthropic] = await Promise.all([
- needed.has("openrouter") ? resolveAdvisorCredential("openrouter") : undefined,
- needed.has("google") ? resolveAdvisorCredential("google") : undefined,
- needed.has("openai") ? resolveAdvisorCredential("openai") : undefined,
- needed.has("anthropic") ? anthropicKey() : undefined,
- ]);
- return { openrouter, google, openai, anthropic };
-}
-
// ---------------------------------------------------------------------------
// P10 — the advisor tool stopped arriving
// ---------------------------------------------------------------------------
@@ -210,13 +176,40 @@ export interface AdvisorSwapDeps {
* (the classifier passthrough) whose requests must not count.
*/
presence?: AdvisorPresenceMonitor;
- /** Advisor key resolution; defaults to the credential authority. */
- resolveKeys?: (
- needed: ReadonlySet<AdvisorRouteKind>,
- inboundApiKey: string | undefined
- ) => Promise<AdvisorApiKeys>;
+ /**
+ * The panel gateway over the startup plans and chains. Absent ⇒ nothing was
+ * planned for this launch, and every member of a panel call is S7.
+ */
+ panel?: AdvisorPanelGateway;
+ /** What a Claude member may sign with. Absent ⇒ the bearer is never used. */
+ claude?: ClaudeAdvisorSwapDeps;
+}
+
+/** The decorator's inputs for Claude members and the Claude collector. */
+export interface ClaudeAdvisorSwapDeps {
+ /**
+ * This launch's `childAuthControl(config, env)`, decided at startup. Only a
+ * launch where claudish controls the child's auth lets the bearer sign.
+ */
+ childAuth: ChildAuthControl;
+ /** ANTHROPIC_API_KEY lookup; defaults to the advisor's own credential lookup. */
+ anthropicKey?: () => Promise<string | undefined>;
+ /** Read for ANTHROPIC_AUTH_TOKEN; defaults to `process.env`. */
+ env?: NodeJS.ProcessEnv;
+ /** Test seam for the Anthropic call; defaults to the global fetch. */
+ fetchImpl?: typeof fetch;
}
+/**
+ * Used when the decorator was given no Claude inputs: the launch's auth is
+ * unknown, so the bearer is treated as not provably the Claude login.
+ */
+const UNKNOWN_CHILD_AUTH: ChildAuthControl = {
+ controlled: false,
+ reason: "proxy-auth",
+ where: "unknown (no launch decision was passed to the advisor)",
+};
+
/**
* Wraps `inner` so its requests carry claudish's advisor function tool and its
* responses are scanned for advisor calls. Returns `inner` unchanged when the
@@ -257,13 +250,7 @@ export class AdvisorSwapHandler implements ModelHandler {
// a monitoring fault must never fail the request
}
- await applyAdvisorRequestSide(
- c,
- payload,
- this.cfg,
- sessionId,
- this.deps.resolveKeys ?? resolveAdvisorKeys
- );
+ await applyAdvisorRequestSide(c, payload, this.cfg, sessionId, this.deps);
const response = await this.inner.handle(c, payload);
return tapAdvisorResponse(response, this.cfg, sessionId);
@@ -274,6 +261,25 @@ export class AdvisorSwapHandler implements ModelHandler {
}
}
+/**
+ * What `runAdvisorCall` gets from this decorator: the panel gateway, and the
+ * Claude credential for this main request, resolved only when a Claude member
+ * runs (runAdvisorCall memoises it per call).
+ */
+function advisorCallDeps(
+ deps: AdvisorSwapDeps,
+ facts: ClaudeRequestFacts
+): Pick<RunAdvisorCallParams, "gateway" | "claude"> {
+ const anthropicKey = deps.claude?.anthropicKey ?? (() => resolveAdvisorCredential("anthropic"));
+ return {
+ ...(deps.panel ? { gateway: deps.panel } : {}),
+ claude: {
+ resolveAuth: (): Promise<ClaudeAdvisorAuth> => resolveClaudeAdvisorAuth(facts, anthropicKey),
+ ...(deps.claude?.fetchImpl ? { fetchImpl: deps.claude.fetchImpl } : {}),
+ },
+ };
+}
+
/**
* The request half: tool swap, advisor tool_result rewrite, stub path S10,
* body dump. Mutates `payload` in place before any converter reads it.
@@ -283,7 +289,7 @@ async function applyAdvisorRequestSide(
payload: Record<string, unknown>,
cfg: AdvisorSwapConfig,
sessionId: string | undefined,
- resolveKeys: NonNullable<AdvisorSwapDeps["resolveKeys"]>
+ deps: AdvisorSwapDeps
): Promise<void> {
const target = payload.model;
const sessionLabel = sessionId ?? NO_SESSION_BUCKET;
@@ -326,26 +332,28 @@ async function applyAdvisorRequestSide(
// evicted while the panel ran, which would otherwise take stub path S2
// although the advice is right here.
const delivered = new Map<string, AdvisorToolResult>();
- let apiKeys: AdvisorApiKeys | undefined;
+ // Read ONCE per main request, before any panel runs: it registers every
+ // real inbound credential on this request's signing lease, so any line
+ // the request logs from here on is redacted against it.
+ const facts: ClaudeRequestFacts = claudeRequestFacts(c, payload, {
+ childAuth: deps.claude?.childAuth ?? UNKNOWN_CHILD_AUTH,
+ ...(deps.claude?.env ? { env: deps.claude.env } : {}),
+ });
+ const callDeps = advisorCallDeps(deps, facts);
for (const id of pendingIds) {
if (cachedResult(id)) continue;
// One panel run per (session, tool_use id), even if Claude Code has
- // two requests in flight for it — see joinOrStartAdvisorCall. Key
- // resolution is inside, so a retry cannot race past it either.
+ // two requests in flight for it — see joinOrStartAdvisorCall.
const runPanel = async (): Promise<AdvisorToolResult> => {
- apiKeys ??= await resolveKeys(
- advisorCredentialsFor(models, cfg.collector),
- c.req.header("x-api-key")
- );
const outcome = await runAdvisorCall({
toolUseId: id,
sessionId,
messages: payload.messages as any[],
models,
collector: cfg.collector ?? null,
- apiKeys,
cfg,
+ ...callDeps,
});
// The false return is honoured, never dropped: an answer claudish
// cannot attach to the call is an answer the model will not receive,
diff --git a/packages/cli/src/handlers/advisor-failures.test.ts b/packages/cli/src/handlers/advisor-failures.test.ts
index e3f2b8e4..28d92638 100644
--- a/packages/cli/src/handlers/advisor-failures.test.ts
+++ b/packages/cli/src/handlers/advisor-failures.test.ts
@@ -1,20 +1,54 @@
import { describe, expect, test } from "bun:test";
+import type {
+ AdvisorPanelCall,
+ AdvisorPanelGateway,
+ AdvisorPanelResult,
+} from "./advisor-route-plan";
import { ADVISOR_STUB_PATHS, runAdvisorCall } from "./native-handler-advisor";
const messages = [{ role: "user", content: "Review this change." }];
-const apiKeys = { openrouter: "test-openrouter-key" };
-function jsonResponse(body: unknown, status = 200): Response {
- return new Response(JSON.stringify(body), {
- status,
- headers: { "Content-Type": "application/json" },
- });
+function gateway(
+ reply: (request: AdvisorPanelCall) => Pick<AdvisorPanelResult, "status" | "bodyText" | "hops">
+): AdvisorPanelGateway {
+ return {
+ plan(spec, role) {
+ return {
+ kind: "routed",
+ spec,
+ role,
+ explanation: {
+ requestedModel: spec,
+ routedModel: spec.slice(spec.indexOf("@") + 1),
+ source: "explicit",
+ candidates: [],
+ warnings: [],
+ outcome: { kind: "ok" },
+ },
+ hops: [
+ {
+ provider: "openrouter",
+ displayName: "OpenRouter",
+ modelSpec: spec,
+ wireId: spec.slice(spec.indexOf("@") + 1),
+ billing: "metered",
+ },
+ ],
+ invocationMode: "explicit-model",
+ };
+ },
+ async call(request) {
+ return { streamFailure: false, latencyMs: 1, ...reply(request) };
+ },
+ };
}
-function fixedFetch(response: Response): typeof fetch {
- return Object.assign(async () => response.clone(), {
- preconnect(_url: string | URL): void {},
- });
+function errorGateway(message: string): AdvisorPanelGateway {
+ return gateway(() => ({
+ status: 400,
+ bodyText: JSON.stringify({ error: { message } }),
+ hops: [],
+ }));
}
describe("runAdvisorCall failure handling", () => {
@@ -27,8 +61,7 @@ describe("runAdvisorCall failure handling", () => {
messages,
models: [model],
collector: null,
- apiKeys,
- fetchImpl: fixedFetch(jsonResponse({ error: { message: reason } }, 400)),
+ gateway: errorGateway(reason),
warn: () => {},
});
@@ -40,7 +73,7 @@ describe("runAdvisorCall failure handling", () => {
expect(outcome.panel[0].stubPath).toBe(ADVISOR_STUB_PATHS.PANEL_ERROR);
expect(outcome.result.isError).toBe(true);
expect(outcome.result.text).toContain(model);
- expect(outcome.result.text).toContain(reason);
+ expect(outcome.result.text).toContain(`HTTP 400: ${reason}`);
});
test("treats an HTTP 200 response with an empty answer as a failure", async () => {
@@ -51,8 +84,11 @@ describe("runAdvisorCall failure handling", () => {
messages,
models: [model],
collector: null,
- apiKeys,
- fetchImpl: fixedFetch(jsonResponse({ choices: [{ message: { content: " " } }] })),
+ gateway: gateway(() => ({
+ status: 200,
+ bodyText: JSON.stringify({ content: [{ type: "text", text: " " }] }),
+ hops: [],
+ })),
warn: () => {},
});
@@ -69,20 +105,34 @@ describe("runAdvisorCall failure handling", () => {
const successfulText = "Keep the validation at the boundary.";
const failureReason = "temporary provider rejection";
const warnings: string[] = [];
- const fetchImpl = (async (_input: RequestInfo | URL, init?: RequestInit) => {
- const request = JSON.parse(String(init?.body)) as { model: string };
- return request.model === "acme/failing-peer"
- ? jsonResponse({ error: { message: failureReason } }, 400)
- : jsonResponse({ choices: [{ message: { content: successfulText } }] });
- }) as typeof fetch;
const outcome = await runAdvisorCall({
toolUseId: "advisor-partial-panel",
messages,
models: [failedModel, successfulModel],
collector: null,
- apiKeys,
- fetchImpl,
+ gateway: gateway((request) =>
+ request.plan.spec === failedModel
+ ? {
+ status: 400,
+ bodyText: JSON.stringify({ error: { message: failureReason } }),
+ hops: [],
+ }
+ : {
+ status: 200,
+ bodyText: JSON.stringify({ content: [{ type: "text", text: successfulText }] }),
+ hops: [
+ {
+ provider: "openrouter",
+ wireModel: "acme/successful-peer",
+ arm: "api-key",
+ billing: "metered",
+ uid: "openrouter",
+ displayName: "OpenRouter",
+ },
+ ],
+ }
+ ),
warn: (message) => warnings.push(message),
});
@@ -94,6 +144,18 @@ describe("runAdvisorCall failure handling", () => {
origin: "stub",
stubPath: ADVISOR_STUB_PATHS.PANEL_ERROR,
});
+ expect(outcome.panel.find((item) => item.requestedModel === successfulModel)).toMatchObject({
+ origin: "upstream",
+ servedBy: {
+ provider: "openrouter",
+ displayName: "OpenRouter",
+ arm: "api-key",
+ billing: "metered",
+ },
+ billing: "metered",
+ hopsTried: ["openrouter"],
+ recordRoute: { kind: "routed", plannedChain: ["OpenRouter"] },
+ });
expect(warnings).toHaveLength(1);
expect(warnings[0]).toContain(failedModel);
});
@@ -107,8 +169,7 @@ describe("runAdvisorCall failure handling", () => {
messages,
models,
collector: null,
- apiKeys,
- fetchImpl: fixedFetch(jsonResponse({ error: { message: "provider refused" } }, 400)),
+ gateway: errorGateway("provider refused"),
warn: (message) => warnings.push(message),
});
@@ -127,10 +188,7 @@ describe("runAdvisorCall failure handling", () => {
messages,
models: [model],
collector: null,
- apiKeys,
- fetchImpl: fixedFetch(
- jsonResponse({ error: { message: `${reasonPrefix}: ${rawSecret}; request denied` } }, 400)
- ),
+ gateway: errorGateway(`${reasonPrefix}: ${rawSecret}; request denied`),
warn: () => {},
});
diff --git a/packages/cli/src/handlers/advisor-panel-gateway.ts b/packages/cli/src/handlers/advisor-panel-gateway.ts
index 97d30c9e..356d70b6 100644
--- a/packages/cli/src/handlers/advisor-panel-gateway.ts
+++ b/packages/cli/src/handlers/advisor-panel-gateway.ts
@@ -23,6 +23,7 @@
*/
import { type Context, Hono } from "hono";
+import type { ChildAuthControl } from "../claude-runner.js";
import { log } from "../logger.js";
import { redactExactValues, redactForClient } from "../redact.js";
import {
@@ -52,6 +53,12 @@ export interface AdvisorRuntime {
/** Keyed `"<role>:<spec>"` (`advisorChainKey`). */
plans: ReadonlyMap<string, AdvisorModelPlan>;
chains: ReadonlyMap<string, AdvisorChainBuild>;
+ /**
+ * The launch's `childAuthControl` decision, the one the Claude plans were
+ * made with: the decorator lets Claude Code's bearer sign a Claude call only
+ * when it says claudish controls the child's auth.
+ */
+ childAuth: ChildAuthControl;
}
/** The URL of the synthetic request. Never resolved: the app is called in process. */
diff --git a/packages/cli/src/handlers/advisor-routing-and-state.test.ts b/packages/cli/src/handlers/advisor-routing-and-state.test.ts
index d0e05ef5..5c8de895 100644
--- a/packages/cli/src/handlers/advisor-routing-and-state.test.ts
+++ b/packages/cli/src/handlers/advisor-routing-and-state.test.ts
@@ -1,8 +1,7 @@
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
-import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import type { DiskCacheV3 } from "../providers/all-models-cache.js";
import { _resetCatalogClient, _setCatalogEntriesForTest } from "../providers/catalog-client.js";
import {
ADVISOR_STUB_PATHS,
@@ -247,53 +246,104 @@ describe("OpenRouter advisor wire-model resolution", () => {
});
});
-describe("advisor request wire routing", () => {
- const fixture: DiskCacheV3 = JSON.parse(
+describe("advisor startup-chain routing", () => {
+ const replay = JSON.parse(
readFileSync(
- new URL(
- "../test-fixtures/catalog-v3/wire-g-20261012101007285-7770c36e.json",
- import.meta.url
- ),
+ new URL("../test-fixtures/advisor/replay-results-1792535079405.json", import.meta.url),
"utf8"
)
);
- function capture(
- models: string[],
- collector: string | null = null,
- cache: DiskCacheV3 | null = fixture
- ) {
- const home = mkdtempSync(join(tmpdir(), "advisor-wire-"));
+ const opus = replay.results.find(
+ (result: any) => result.tier === "claude-opus-5-5" && result.shape === "A"
+ );
+ const haiku = replay.results.find(
+ (result: any) => result.tier === "claude-haiku-5-5" && result.shape === "A"
+ );
+ const messages = [
+ { role: "user", content: "Review this design" },
+ { role: "assistant", content: "I will check the boundary." },
+ { role: "user", content: "Include the earlier context." },
+ ];
+
+ function capture(models: string[], collector: string | null = null) {
+ const home = mkdtempSync(join(tmpdir(), "advisor-routing-"));
try {
- if (cache) {
- mkdirSync(join(home, ".claudish"));
- writeFileSync(join(home, ".claudish/cloud-models-catalog-v3.json"), JSON.stringify(cache));
- }
const child = Bun.spawnSync(
[
process.execPath,
"--eval",
`
- const { advisorRouteFor, runAdvisorCall } = await import(${JSON.stringify(new URL("./native-handler-advisor.ts", import.meta.url).pathname)});
- const models = ${JSON.stringify(models)}, collector = ${JSON.stringify(collector)};
- const routes = models.map(m => advisorRouteFor(m, "panel"));
- if (collector) routes.push(advisorRouteFor(collector, "collector"));
- const calls = [];
const originalFetch = globalThis.fetch;
- globalThis.fetch = async (url, init) => {
- calls.push({ url: String(url), method: init.method, headers: Object.fromEntries(new Headers(init.headers)), body: JSON.parse(init.body) });
- if (String(url).endsWith("/responses")) return Response.json({ output: [{ type: "reasoning", summary: [] }, { type: "message", content: [{ type: "output_text", text: "Responses advice" }] }] });
- if (String(url).includes(":generateContent")) return Response.json({ candidates: [{ content: { parts: [{ text: "Gemini advice" }] } }] });
- if (String(url).endsWith("/messages")) return Response.json({ content: [{type: "text", text: "Messages synthesis"}] });
- return Response.json({ choices: [{ message: { content: "Chat advice" } }] });
+ const networkCalls = [];
+ globalThis.fetch = async (...args) => {
+ networkCalls.push(String(args[0]));
+ throw new Error("Unexpected network call");
};
try {
- const outcome = await runAdvisorCall({ toolUseId: "wire-advisor", messages: [{role: "user", content: "Review this design"}], models, collector,
- apiKeys: { openai: "openai-fake", google: "google-fake", openrouter: "openrouter-fake", anthropic: "anthropic-fake" }, fetchImpl: globalThis.fetch, warn: () => {} });
- console.log(JSON.stringify({ routes, calls, outcome }));
+ const { runAdvisorCall } = await import(${JSON.stringify(new URL("./native-handler-advisor.ts", import.meta.url).pathname)});
+ const { _setCatalogEntriesForTest } = await import(${JSON.stringify(new URL("../providers/catalog-client.ts", import.meta.url).pathname)});
+ _setCatalogEntriesForTest(null);
+ const models = ${JSON.stringify(models)}, collector = ${JSON.stringify(collector)};
+ const opus = ${JSON.stringify(opus)}, haiku = ${JSON.stringify(haiku)};
+ const message = result => ({
+ model: result.ok.model,
+ content: [{ type: "text", text: result.ok.text }],
+ stop_reason: result.ok.stop_reason,
+ usage: result.ok.usage,
+ });
+ const gatewayCalls = [], claudeCalls = [], plans = new Map();
+ for (const [role, specs] of [["panel", models], ["collector", collector ? [collector] : []]]) {
+ for (const spec of specs) {
+ if (spec === "claude-opus-5-5") {
+ plans.set(role + ":" + spec, { kind: "claude-native", spec, role, model: spec, expectedArm: "api-key" });
+ continue;
+ }
+ const wireId = spec.slice(spec.indexOf("@") + 1);
+ const provider = spec.slice(0, spec.indexOf("@"));
+ const displayName = { openai: "OpenAI", google: "Google", openrouter: "OpenRouter", anthropic: "Anthropic" }[provider];
+ plans.set(role + ":" + spec, {
+ kind: "routed", spec, role,
+ explanation: { requestedModel: spec, routedModel: wireId, source: "explicit", candidates: [], warnings: [], outcome: { kind: "ok" } },
+ hops: [{ provider, displayName, modelSpec: spec, wireId, billing: "metered" }],
+ invocationMode: "explicit-model",
+ });
+ }
+ }
+ let claudeAuthLookups = 0;
+ const outcome = await runAdvisorCall({
+ toolUseId: "routed-advisor", messages: ${JSON.stringify(messages)}, models, collector,
+ gateway: {
+ plan: (spec, role) => plans.get(role + ":" + spec),
+ async call(request) {
+ if (request.plan !== plans.get(request.plan.role + ":" + request.plan.spec)) throw new Error("Not the startup plan");
+ gatewayCalls.push(structuredClone(request));
+ const hop = request.plan.hops[0];
+ const result = request.plan.spec.startsWith("google@") ? haiku : opus;
+ return {
+ status: result.status, bodyText: JSON.stringify(message(result)), streamFailure: false, latencyMs: 1,
+ hops: [{ provider: hop.provider, wireModel: hop.wireId, arm: "api-key", billing: "metered", uid: hop.provider, displayName: hop.displayName }],
+ };
+ },
+ },
+ claude: {
+ async resolveAuth() {
+ claudeAuthLookups++;
+ return { arm: "api-key", source: "ANTHROPIC_API_KEY", apiKey: "anthropic-fake", facts: {} };
+ },
+ async fetchImpl(input, init) {
+ const request = input instanceof Request ? input : new Request(input, init);
+ claudeCalls.push({ url: request.url, method: request.method, headers: Object.fromEntries(request.headers), body: await request.json() });
+ return Response.json(message(opus), { status: opus.status });
+ },
+ },
+ warn: () => {},
+ });
+ console.log(JSON.stringify({ gatewayCalls, claudeCalls, claudeAuthLookups, networkCalls, outcome }));
} finally { globalThis.fetch = originalFetch; }
`,
],
{
+ cwd: home,
env: {
PATH: process.env.PATH,
HOME: home,
@@ -306,91 +356,138 @@ describe("advisor request wire routing", () => {
}
);
expect(child.exitCode, child.stderr.toString()).toBe(0);
- return JSON.parse(child.stdout.toString());
+ const result = JSON.parse(child.stdout.toString());
+ expect(result.networkCalls).toEqual([]);
+ return result;
} finally {
rmSync(home, { recursive: true, force: true });
}
}
- it("OpenAI panel follows Responses wire and extracts output_text", () => {
- const { routes, calls, outcome } = capture(["[email protected]"]);
- expect(routes[0]).toMatchObject({
- api: "responses",
- url: "https://api.openai.com/v1/responses",
- wire: { status: "known" },
- });
- const wire = routes[0].wire;
- const effort = wire.reasoning.effort;
- expect(typeof effort.default).toBe("string");
- expect(calls).toHaveLength(1);
- expect(calls[0]).toMatchObject({
- method: "POST",
- url: routes[0].url,
- headers: { authorization: "Bearer openai-fake" },
- body: {
- stream: false,
- max_output_tokens: 2048,
- reasoning: { effort: effort.values[effort.default] },
- },
- });
- expect(calls[0].body.instructions).toBeString();
- expect(calls[0].body.input).toContainEqual({ role: "user", content: "Review this design" });
- expect(calls[0].body).not.toHaveProperty("messages");
- expect(calls[0].body).not.toHaveProperty("max_tokens");
- expect(outcome.result).toEqual({ text: "Responses advice", isError: false });
- });
- it("Google panel follows native Gemini wire and extracts candidate text", () => {
- const { routes, calls, outcome } = capture(["[email protected]"]);
- expect(routes[0]).toMatchObject({
- api: "gemini",
- url: "https://generativelanguage.googleapis.com/v1beta/models/gemini-3.8-flash:generateContent",
- wire: { status: "known" },
+
+ function expectPanelCall(call: any, spec: string) {
+ expect(call.plan).toMatchObject({
+ kind: "routed",
+ spec,
+ role: "panel",
+ invocationMode: "explicit-model",
});
- expect(calls[0]).toMatchObject({
- url: routes[0].url,
- headers: { "x-goog-api-key": "google-fake" },
- body: { generationConfig: { maxOutputTokens: 2048 } },
+ expect(call.timeoutMs).toBe(60_000);
+ expect(Object.keys(call.payload).sort()).toEqual([
+ "max_tokens",
+ "messages",
+ "model",
+ "stream",
+ "system",
+ ]);
+ expect(call.payload).toMatchObject({
+ model: call.plan.hops[0].modelSpec,
+ max_tokens: 2048,
+ stream: false,
});
- expect(calls[0].body.systemInstruction.parts[0].text).toBeString();
- expect(calls[0].body.contents).toContainEqual({
- role: "user",
- parts: [{ text: "Review this design" }],
+ expect(call.payload.system).toBeString();
+ expect(call.payload.system.length).toBeGreaterThan(0);
+ expect(call.payload.messages.slice(0, messages.length)).toEqual(messages);
+ }
+
+ it("sends an OpenAI panel call through its startup chain and extracts translated advice", () => {
+ const spec = "[email protected]";
+ const { gatewayCalls, claudeCalls, claudeAuthLookups, outcome } = capture([spec]);
+ expect(gatewayCalls).toHaveLength(1);
+ expectPanelCall(gatewayCalls[0], spec);
+ expect(claudeCalls).toEqual([]);
+ expect(claudeAuthLookups).toBe(0);
+ expect(outcome.result).toEqual({ text: opus.ok.text, isError: false });
+ expect(outcome.panel[0]).toMatchObject({
+ origin: "upstream",
+ billing: "metered",
+ hopsTried: ["openai"],
+ servedBy: { provider: "openai", displayName: "OpenAI", arm: "api-key", billing: "metered" },
+ recordRoute: { kind: "routed", plannedChain: ["OpenAI"] },
});
- expect(calls[0].body).not.toHaveProperty("messages");
- expect(outcome.result).toEqual({ text: "Gemini advice", isError: false });
});
- it("Anthropic collector uses Messages body, required limit and credential headers", () => {
- const { routes, calls, outcome } = capture(
- "anthropic@claude-opus-5-5"
+
+ it("sends a Google panel call through its startup chain without resolving Claude credentials", () => {
+ const spec = "[email protected]";
+ const { gatewayCalls, claudeCalls, claudeAuthLookups, outcome } = capture([spec]);
+ expect(gatewayCalls).toHaveLength(1);
+ expectPanelCall(gatewayCalls[0], spec);
+ expect(claudeCalls).toEqual([]);
+ expect(claudeAuthLookups).toBe(0);
+ expect(outcome.result).toEqual({ text: haiku.ok.text, isError: false });
+ });
+
+ it("uses the fixed Anthropic Messages endpoint for a bare Claude collector with both panel answers", () => {
+ const models = ["[email protected]", "[email protected]"];
+ const { gatewayCalls, claudeCalls, claudeAuthLookups, outcome } = capture(
+ models,
+ "claude-opus-5-5"
);
- expect(routes[2]).toMatchObject({
- api: "anthropic-messages",
+ expect(gatewayCalls).toHaveLength(2);
+ for (const spec of models)
+ expectPanelCall(
+ gatewayCalls.find((call: any) => call.plan.spec === spec),
+ spec
+ );
+ expect(claudeAuthLookups).toBe(1);
+ expect(claudeCalls).toHaveLength(1);
+ expect(claudeCalls[0]).toMatchObject({
+ method: "POST",
url: "https://api.anthropic.com/v1/messages",
- });
- expect(calls).toHaveLength(3);
- expect(calls[2]).toMatchObject({
- url: routes[2].url,
headers: { "x-api-key": "anthropic-fake", "anthropic-version": "2023-06-01" },
body: { model: "claude-opus-5-5", max_tokens: 2048 },
});
- expect(calls[2].body.system).toBeString();
- expect(calls[2].body.messages[0].content).toContain("Responses advice");
- expect(calls[2].body.messages[0].content).toContain("Gemini advice");
- expect(outcome.result).toEqual({ text: "Messages synthesis", isError: false });
+ expect(claudeCalls[0].headers).not.toHaveProperty("authorization");
+ expect(claudeCalls[0].body.system).toBeString();
+ expect(claudeCalls[0].body.messages[0].role).toBe("user");
+ for (const spec of models) expect(claudeCalls[0].body.messages[0].content).toContain(spec);
+ expect(claudeCalls[0].body.messages[0].content).toContain(opus.ok.text);
+ expect(claudeCalls[0].body.messages[0].content).toContain(haiku.ok.text);
+ expect(outcome.result).toEqual({ text: opus.ok.text, isError: false });
+ expect(outcome.collectorOutcome).toMatchObject({
+ recordRoute: { kind: "claude-native", host: "api.anthropic.com" },
+ servedBy: { provider: "native-anthropic", arm: "api-key", billing: "metered" },
+ credentialSource: "ANTHROPIC_API_KEY",
+ billing: "metered",
+ });
});
- it.each([
- ["openai@unpublished", "https://api.openai.com/v1/chat/completions"],
- [
- "google@unpublished",
- "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions",
- ],
- ["openrouter@acme/unpublished", "https://openrouter.ai/api/v1/chat/completions"],
- ])("cold catalog uses documented chat endpoint for %s", (model, url) => {
- const { routes, calls, outcome } = capture([model!], null, null);
- expect(routes[0]).toMatchObject({ api: "chat-completions", url });
- expect(routes[0]).not.toHaveProperty("wire");
- expect(calls[0].body.messages).toBeArray();
- expect(calls[0].body).not.toHaveProperty("input");
- expect(outcome.result).toEqual({ text: "Chat advice", isError: false });
+
+ it.each(["openai@unpublished", "google@unpublished", "openrouter@acme/unpublished"])(
+ "uses the startup chain for %s even with a cold cloud models catalog",
+ (spec) => {
+ const { gatewayCalls, claudeCalls, claudeAuthLookups, outcome } = capture([spec]);
+ expect(gatewayCalls).toHaveLength(1);
+ expectPanelCall(gatewayCalls[0], spec);
+ expect(claudeCalls).toEqual([]);
+ expect(claudeAuthLookups).toBe(0);
+ expect(outcome.result).toEqual({
+ text: spec.startsWith("google@") ? haiku.ok.text : opus.ok.text,
+ isError: false,
+ });
+ }
+ );
+
+ it("keeps an explicit Anthropic collector on its routed chain, not the native Claude arm", () => {
+ const collector = "anthropic@claude-opus-5-5";
+ const { gatewayCalls, claudeCalls, claudeAuthLookups, outcome } = capture(
+ collector
+ );
+ expect(gatewayCalls).toHaveLength(3);
+ const call = gatewayCalls.find((call: any) => call.plan.role === "collector");
+ expect(call.plan.spec).toBe(collector);
+ expect(call.timeoutMs).toBe(30_000);
+ expect(call.payload.model).toBe(call.plan.hops[0].modelSpec);
+ expect(Object.keys(call.payload).sort()).toEqual([
+ "max_tokens",
+ "messages",
+ "model",
+ "stream",
+ "system",
+ ]);
+ expect(call.payload.messages[0].content).toContain(opus.ok.text);
+ expect(call.payload.messages[0].content).toContain(haiku.ok.text);
+ expect(claudeCalls).toEqual([]);
+ expect(claudeAuthLookups).toBe(0);
+ expect(outcome.result).toEqual({ text: opus.ok.text, isError: false });
});
});
diff --git a/packages/cli/src/handlers/native-handler-advisor.ts b/packages/cli/src/handlers/native-handler-advisor.ts
index a85d88b5..5a57e30d 100644
--- a/packages/cli/src/handlers/native-handler-advisor.ts
+++ b/packages/cli/src/handlers/native-handler-advisor.ts
@@ -45,6 +45,7 @@ import { resolveConnection, resolveRouteProfile } from "../providers/connection-
import { type KnownWire, type WireApi, wireRequestUrl } from "../providers/connection-wire.js";
import { parseModelSpec } from "../providers/model-parser.js";
import { nativeRouteFor } from "../providers/native-route.js";
+import { getProviderByName } from "../providers/provider-definitions.js";
import { mergeWireHeaders, shapeRequestFromWire } from "../providers/wire-request.js";
import { acquireSigningLease } from "../redact.js";
import type { StatsEvent } from "../stats-otlp.js";
@@ -58,7 +59,12 @@ import {
import {
type AdvisorBilling,
type AdvisorEmptyReason,
+ type AdvisorModelPlan,
+ type AdvisorPanelGateway,
+ type AdvisorPanelResult,
type AdvisorTurn,
+ buildAdvisorPanelPayload,
+ describeAdvisorRefusal,
extractAdvice,
} from "./advisor-route-plan.js";
import { extractProviderMessage, extractUpstreamStatus } from "./shared/anthropic-error.js";
@@ -249,26 +255,6 @@ export function sanitizeAdvisorReason(
return scrubSecrets(out);
}
-/**
- * The credential values a request actually presented, read back from the very
- * headers that were sent. Taking them from the headers rather than from
- * `AdvisorApiKeys` is deliberate: it is the exact string on the wire, so a
- * provider echoing it back is matched character for character.
- */
-function credentialValuesInHeaders(headers: Record<string, string>): string[] {
- const values: string[] = [];
- const bearer = headers.Authorization ?? headers.authorization;
- if (bearer) values.push(bearer.replace(/^Bearer\s+/i, "").trim());
- const apiKey = headers["x-api-key"];
- if (apiKey) values.push(apiKey.trim());
- return values;
-}
-
-/** Every key an advisor call could have signed with, for by-value redaction. */
-function credentialValuesOf(apiKeys: AdvisorApiKeys): string[] {
- return Object.values(apiKeys).filter((v): v is string => typeof v === "string");
-}
-
/**
* Appends a structured log entry to the configured advisor-swap log file
* (`CLAUDISH_SWAP_ADVISOR_LOG`); a no-op for the file when no path is set.
@@ -1504,14 +1490,6 @@ same coding problem. Synthesize them into a single, coherent response that:
- Produces a clear, actionable recommendation
Be concise. Do not attribute advice to specific models.`;
-/** Provider keys the advisor executor can sign with. */
-export interface AdvisorApiKeys {
- openrouter?: string;
- google?: string;
- openai?: string;
- anthropic?: string;
-}
-
/** The four places an advisor or collector request can be sent. */
export type AdvisorRouteKind = "google" | "openai" | "openrouter" | "anthropic";
@@ -1526,7 +1504,7 @@ export interface AdvisorRoute {
host: string;
/** Full endpoint URL the request is POSTed to. */
url: string;
- /** Which `AdvisorApiKeys` entry signs the request. */
+ /** Which advisor credential signs the request. */
credential: AdvisorRouteKind;
/** The model id placed in the request body, after catalog resolution. */
wireModel: string;
@@ -1831,28 +1809,6 @@ function openRouterWireModelFor(provider: string | null, model: string): string
);
}
-/**
- * The credentials an advisor configuration can use: every panel model's, plus
- * the collector's when one can run (it needs more than one panel model).
- * A spec that fails to resolve is skipped; its call reports the failure.
- */
-export function advisorCredentialsFor(
- models: string[],
- collector: string | null | undefined
-): Set<AdvisorRouteKind> {
- const needed = new Set<AdvisorRouteKind>();
- const add = (spec: string, role: "panel" | "collector") => {
- try {
- needed.add(advisorRouteFor(spec, role).credential);
- } catch {
- // reported by the call itself
- }
- };
- for (const m of models) add(m, "panel");
- if (collector && models.length > 1) add(collector, "collector");
- return needed;
-}
-
/**
* The credential-authority provider each advisor credential resolves through.
* `anthropic` is `native-anthropic`, which resolves exactly ANTHROPIC_API_KEY
@@ -1941,86 +1897,6 @@ export async function resolveAdvisorCredential(
/** How many output tokens an advisor or collector call asks for. */
const ADVISOR_MAX_OUTPUT_TOKENS = 2048;
-/**
- * One advisor or collector request, built for the route's API and shaped by
- * its connection's wire — the same step a session request takes (output-limit
- * field, reasoning encoding), so the advisor never sends a field the catalog
- * did not publish for that connection. Without a known wire, no output limit
- * and no reasoning field is sent.
- */
-function buildAdvisorRequest(
- route: AdvisorRoute,
- messages: any[],
- apiKeys: AdvisorApiKeys,
- systemPrompt: string = ADVISOR_SYSTEM_PROMPT,
- userText?: string
-): { headers: Record<string, string>; body: any; extractText: (data: any) => string | undefined } {
- const key = apiKeys[route.credential] ?? "";
- const headers: Record<string, string> = { "Content-Type": "application/json" };
- if (route.kind === "openrouter") {
- headers["HTTP-Referer"] = "https://claudish.com";
- headers["X-Title"] = "Claudish Advisor";
- }
- const turns: Array<{ role: string; content: string }> =
- userText !== undefined
- ? [{ role: "user", content: userText }]
- : convertToOpenAIMessages(messages).map((m: any) => ({
- role: m.role,
- content: typeof m.content === "string" ? m.content : extractBlocksAsText(m.content),
- }));
-
- let body: Record<string, unknown>;
- let extractText: (data: any) => string | undefined;
- switch (route.api) {
- case "anthropic-messages":
- headers["x-api-key"] = key;
- headers["anthropic-version"] = "2023-06-01";
- // The Messages API REQUIRES `max_tokens` (it is the protocol's only limit
- // field); a known wire writes the same field again below.
- body = {
- model: route.wireModel,
- system: systemPrompt,
- messages: turns,
- max_tokens: ADVISOR_MAX_OUTPUT_TOKENS,
- };
- extractText = extractAnthropicText;
- break;
- case "responses":
- headers.Authorization = `Bearer ${key}`;
- body = { model: route.wireModel, instructions: systemPrompt, input: turns, stream: false };
- extractText = extractResponsesText;
- break;
- case "gemini":
- headers["x-goog-api-key"] = key;
- body = {
- systemInstruction: { parts: [{ text: systemPrompt }] },
- contents: turns.map((t) => ({
- role: t.role === "assistant" ? "model" : "user",
- parts: [{ text: t.content }],
- })),
- generationConfig: { maxOutputTokens: ADVISOR_MAX_OUTPUT_TOKENS },
- };
- extractText = extractGeminiText;
- break;
- default:
- headers.Authorization = `Bearer ${key}`;
- body = {
- model: route.wireModel,
- messages: [{ role: "system", content: systemPrompt }, ...turns],
- };
- extractText = extractChatCompletionText;
- }
-
- if (route.wire) {
- const shaped = shapeRequestFromWire(body, route.wire, {
- maxTokens: ADVISOR_MAX_OUTPUT_TOKENS,
- hasTools: false,
- });
- mergeWireHeaders(headers, shaped.headers);
- }
- return { headers, body, extractText };
-}
-
/**
* What one panel or collector call actually did. Provenance is part of the
* data type, never reconstructed from placeholder strings: `text` exists only
@@ -2042,6 +1918,34 @@ export interface AdvisorModelOutcome {
stubPath: AdvisorStubPath | null;
text?: string;
reason?: string;
+ /** How the record names the route: the planned chain, or the fixed Claude host. */
+ recordRoute?: AdvisorRecordRoute;
+ /** Who served the call; null when nothing served (`origin` is not `upstream`). */
+ servedBy?: AdvisorServedBy | null;
+ /** The serving hop's wire model, or the resolved Anthropic id. */
+ routedModel?: string | null;
+ /** Routed only: provider uids whose `handle()` was entered, in order, once each. */
+ hopsTried?: string[];
+ /** `servedBy.billing`; null when nothing served. */
+ billing?: AdvisorBilling | null;
+ usage?: { input_tokens: number; output_tokens: number };
+ emptyReason?: AdvisorEmptyReason;
+ stopReason?: string;
+}
+
+/** The `route` field of an advisor record. */
+export type AdvisorRecordRoute =
+ | { kind: "routed"; plannedChain: string[] }
+ | { kind: "claude-native"; host: string };
+
+/** The `servedBy` field of an advisor record: labels only, never a credential. */
+export interface AdvisorServedBy {
+ /** The claudish provider that served the call. */
+ provider: string;
+ displayName: string;
+ /** The arm that signed: `oauth` for a subscription login, `api-key` otherwise; null when unknown. */
+ arm: string | null;
+ billing: AdvisorBilling | null;
}
/** One advisor call end to end: every model's outcome and the delivered result. */
@@ -2059,21 +1963,6 @@ export interface AdvisorCallOutcome {
result: AdvisorToolResult;
}
-interface AdvisorFetchPlan {
- role: AdvisorModelOutcome["role"];
- requestedModel: string;
- route: AdvisorRoute;
- headers: Record<string, string>;
- body: any;
- /** Abort after this long; undefined = no timeout. */
- timeoutMs?: number;
- extractText: (data: any) => string | undefined;
- /** Stub path when the call answered 2xx without usable advice text. */
- emptyStubPath: AdvisorStubPath;
- /** Stub path when the call failed outright. */
- errorStubPath: AdvisorStubPath;
-}
-
/** Abort a panel call after this long; a failure like any other (stub path S7). */
const ADVISOR_PANEL_TIMEOUT_MS = 60_000;
@@ -2144,200 +2033,6 @@ function describeFetchError(err: unknown, timeoutMs?: number): string {
return `request failed: ${e?.message ?? String(err)}`;
}
-/**
- * Performs one advisor HTTP call and classifies it. Never rejects: every
- * failure becomes a `stub` outcome carrying its stub path and reason.
- *
- * The status is read from the body first because the advisor path uses bare
- * `fetch`, not a claudish handler: `extractUpstreamStatus` only finds
- * `error.upstream_status` when some remapping layer wrote it, so normally the
- * source is `http_status`. No status is invented when no response arrived.
- */
-async function executeAdvisorFetch(
- plan: AdvisorFetchPlan,
- fetchImpl?: typeof fetch
-): Promise<AdvisorModelOutcome> {
- const base = { role: plan.role, requestedModel: plan.requestedModel, route: plan.route };
- // The exact credential values this request presents, for by-value redaction
- // of anything the provider echoes back.
- const secrets = credentialValuesInHeaders(plan.headers);
- const started = performance.now();
- const elapsed = () => Math.round(performance.now() - started);
- const controller = plan.timeoutMs ? new AbortController() : undefined;
- const timer = controller ? setTimeout(() => controller.abort(), plan.timeoutMs) : undefined;
- // Resolved per call, so an omitted seam is exactly the global fetch of today.
- const doFetch = fetchImpl ?? fetch;
-
- try {
- let resp: Response;
- try {
- resp = await doFetch(plan.route.url, {
- method: "POST",
- headers: plan.headers,
- body: JSON.stringify(plan.body),
- signal: controller?.signal,
- });
- } catch (err) {
- return stubOutcome(
- base,
- plan.errorStubPath,
- describeFetchError(err, plan.timeoutMs),
- elapsed(),
- secrets
- );
- }
-
- let bodyText: string;
- try {
- bodyText = await resp.text();
- } catch (err) {
- return stubOutcome(
- base,
- plan.errorStubPath,
- `HTTP ${resp.status} but the body could not be read: ${describeFetchError(err, plan.timeoutMs)}`,
- elapsed(),
- secrets,
- { status: resp.status, source: "http_status", bytes: 0 }
- );
- }
-
- const bodyStatus = extractUpstreamStatus(bodyText);
- const observed: ObservedResponse = {
- status: bodyStatus ?? resp.status,
- source: bodyStatus !== undefined ? "error.upstream_status" : "http_status",
- bytes: new TextEncoder().encode(bodyText).byteLength,
- };
-
- if (!resp.ok || (bodyStatus !== undefined && bodyStatus >= 400)) {
- return stubOutcome(
- base,
- plan.errorStubPath,
- `HTTP ${observed.status}: ${summarizeErrorBody(bodyText)}`,
- elapsed(),
- secrets,
- observed
- );
- }
-
- let data: unknown;
- try {
- data = JSON.parse(bodyText);
- } catch {
- return stubOutcome(
- base,
- plan.emptyStubPath,
- `HTTP ${observed.status} but the response body is not JSON`,
- elapsed(),
- secrets,
- observed
- );
- }
-
- const text = plan.extractText(data);
- if (typeof text !== "string" || text.trim().length === 0) {
- return stubOutcome(
- base,
- plan.emptyStubPath,
- `HTTP ${observed.status} but the response carried no advice text`,
- elapsed(),
- secrets,
- observed
- );
- }
-
- return {
- ...base,
- upstreamStatus: observed.status,
- upstreamStatusSource: observed.source,
- responseBytes: observed.bytes,
- latencyMs: elapsed(),
- origin: "upstream",
- stubPath: null,
- text,
- };
- } finally {
- if (timer) clearTimeout(timer);
- }
-}
-
-/** Advice text from an OpenAI chat-completions body. */
-function extractChatCompletionText(data: any): string | undefined {
- const content = data?.choices?.[0]?.message?.content;
- if (typeof content === "string") return content;
- if (Array.isArray(content)) {
- const joined = content.map((p: any) => (typeof p?.text === "string" ? p.text : "")).join("");
- return joined || undefined;
- }
- return undefined;
-}
-
-/** Advice text from a Responses body: its output_text parts, joined. */
-function extractResponsesText(data: any): string | undefined {
- if (typeof data?.output_text === "string" && data.output_text) return data.output_text;
- const parts = (Array.isArray(data?.output) ? data.output : [])
- .flatMap((item: any) => (Array.isArray(item?.content) ? item.content : []))
- .filter((c: any) => c?.type === "output_text" && typeof c.text === "string")
- .map((c: any) => c.text);
- return parts.length > 0 ? parts.join("") : undefined;
-}
-
-/** Advice text from a Gemini generateContent body: its first candidate's text parts. */
-function extractGeminiText(data: any): string | undefined {
- const parts = data?.candidates?.[0]?.content?.parts;
- if (!Array.isArray(parts)) return undefined;
- const text = parts
- .filter((p: any) => typeof p?.text === "string" && !p.thought)
- .map((p: any) => p.text)
- .join("");
- return text || undefined;
-}
-
-/** Advice text from an Anthropic messages body: its first text block. */
-function extractAnthropicText(data: any): string | undefined {
- const blocks = data?.content;
- if (!Array.isArray(blocks)) return undefined;
- const text = blocks.find((b: any) => b?.type === "text")?.text;
- return typeof text === "string" ? text : undefined;
-}
-
-function errorMessageOf(err: unknown): string {
- return (err as { message?: string } | undefined)?.message ?? String(err);
-}
-
-async function callAdvisorModel(
- modelSpec: string,
- messages: any[],
- apiKeys: AdvisorApiKeys,
- fetchImpl?: typeof fetch
-): Promise<AdvisorModelOutcome> {
- let plan: AdvisorFetchPlan;
- try {
- const route = advisorRouteFor(modelSpec, "panel");
- const { headers, body, extractText } = buildAdvisorRequest(route, messages, apiKeys);
- plan = {
- role: "panel",
- requestedModel: modelSpec,
- route,
- headers,
- body,
- timeoutMs: ADVISOR_PANEL_TIMEOUT_MS,
- extractText,
- emptyStubPath: ADVISOR_STUB_PATHS.PANEL_EMPTY,
- errorStubPath: ADVISOR_STUB_PATHS.PANEL_ERROR,
- };
- } catch (err) {
- const base = { role: "panel" as const, requestedModel: modelSpec, route: null };
- return stubOutcome(
- base,
- ADVISOR_STUB_PATHS.PANEL_ERROR,
- `could not build the request: ${errorMessageOf(err)}`,
- 0,
- credentialValuesOf(apiKeys)
- );
- }
- return executeAdvisorFetch(plan, fetchImpl);
-}
-
function isAnthropicModel(parsed: ReturnType<typeof parseModelSpec>): boolean {
const m = parsed.model.toLowerCase();
return (
@@ -2349,71 +2044,6 @@ function isAnthropicModel(parsed: ReturnType<typeof parseModelSpec>): boolean {
);
}
-async function callCollectorModel(
- collectorSpec: string,
- advice: Array<{ model: string; text: string }>,
- apiKeys: AdvisorApiKeys,
- fetchImpl?: typeof fetch
-): Promise<AdvisorModelOutcome> {
- let plan: AdvisorFetchPlan;
- try {
- const adviceText = advice
- .map((a, i) => `### Advisor ${i + 1} (${a.model})\n${a.text}`)
- .join("\n\n");
-
- const route = advisorRouteFor(collectorSpec, "collector");
-
- // An alias the catalog could not resolve is refused HERE rather than POSTed:
- // startup normally refuses or drops such a collector first, so this covers
- // the paths that never ran it (legacy env-var mode, a cache that went cold
- // after launch). The panel's own answers are still delivered.
- if (route.unresolvedAlias) {
- throw new Error(
- `"${route.unresolvedAlias}" is a claudish alias, not a model id ${route.host} accepts, and ` +
- "the model catalog holds no id for it, so claudish refused to send it. Name the " +
- 'collector by its full model id (for example "claude-haiku-4-5"), or refresh the ' +
- "catalog (`claudish --models-refresh`)."
- );
- }
-
- const { headers, body, extractText } = buildAdvisorRequest(
- route,
- [],
- apiKeys,
- COLLECTOR_SYSTEM_PROMPT,
- adviceText
- );
- plan = {
- role: "collector",
- requestedModel: collectorSpec,
- route,
- headers,
- body,
- timeoutMs: ADVISOR_COLLECTOR_TIMEOUT_MS,
- extractText,
- emptyStubPath:
- route.kind === "anthropic"
- ? ADVISOR_STUB_PATHS.ANTHROPIC_COLLECTOR_EMPTY
- : ADVISOR_STUB_PATHS.COLLECTOR_EMPTY,
- errorStubPath: ADVISOR_STUB_PATHS.COLLECTOR_FAILED,
- };
- } catch (err) {
- const base = {
- role: "collector" as const,
- requestedModel: collectorSpec,
- route: null,
- };
- return stubOutcome(
- base,
- ADVISOR_STUB_PATHS.COLLECTOR_FAILED,
- `could not build the request: ${errorMessageOf(err)}`,
- 0,
- credentialValuesOf(apiKeys)
- );
- }
- return executeAdvisorFetch(plan, fetchImpl);
-}
-
// ---------------------------------------------------------------------------
// Claude advisor calls: the Claude Code login arm
// ---------------------------------------------------------------------------
@@ -3085,9 +2715,21 @@ function modelCallRecord(
sessionId: call.sessionId,
role: outcome.role,
requestedModel: outcome.requestedModel,
- provider: outcome.route?.kind ?? null,
- route: outcome.route ? { kind: outcome.route.kind, host: outcome.route.host } : null,
- routedModel: outcome.route?.wireModel ?? null,
+ // The claudish provider that SERVED the call, never the planned one.
+ provider: outcome.servedBy?.provider ?? null,
+ route:
+ outcome.recordRoute ??
+ (outcome.route ? { kind: outcome.route.kind, host: outcome.route.host } : null),
+ routedModel: outcome.routedModel ?? outcome.route?.wireModel ?? null,
+ servedBy: outcome.servedBy ?? null,
+ billing: outcome.billing ?? null,
+ ...(outcome.hopsTried ? { hopsTried: outcome.hopsTried } : {}),
+ ...claudeRecordFields(outcome),
+ ...(outcome.usage ? { usage: outcome.usage } : {}),
+ ...(outcome.emptyReason ? { emptyReason: outcome.emptyReason } : {}),
+ ...(outcome.stopReason !== undefined && outcome.origin !== "upstream"
+ ? { stopReason: outcome.stopReason }
+ : {}),
upstreamStatus: outcome.upstreamStatus,
upstreamStatusSource: outcome.upstreamStatusSource,
responseBytes: outcome.responseBytes,
@@ -3098,6 +2740,17 @@ function modelCallRecord(
};
}
+/** The Claude-only record fields (labels, never a value), when the outcome carries them. */
+function claudeRecordFields(outcome: AdvisorModelOutcome): Record<string, unknown> {
+ const claude = outcome as Partial<ClaudeAdvisorOutcome>;
+ return {
+ ...(claude.credentialSource !== undefined ? { credentialSource: claude.credentialSource } : {}),
+ ...(claude.loginSkipped ? { loginSkipped: claude.loginSkipped } : {}),
+ ...(claude.systemHead ? { systemHead: claude.systemHead } : {}),
+ ...(claude.shapeRefusal ? { shapeRefusal: true } : {}),
+ };
+}
+
/**
* Writes the P7 records for one call: one `advisor_call` per panel model, one
* `advisor_collector_call` when a collector ran, and one `advisor_rewrite`.
@@ -3237,6 +2890,19 @@ export function recoverUnassociatedAdvisorResult(
return result;
}
+/** What a Claude member or collector needs from the main request, supplied by the decorator. */
+export interface ClaudeAdvisorDeps {
+ /**
+ * The credential for this advisor call's Claude members, by presence and
+ * provenance on the main request (`resolveClaudeAdvisorAuth`). Called only
+ * when a Claude member runs, and memoised per advisor call, so a routed-only
+ * panel never resolves an Anthropic key.
+ */
+ resolveAuth: () => Promise<ClaudeAdvisorAuth>;
+ /** Test seam; defaults to the global fetch at call time. */
+ fetchImpl?: typeof fetch;
+}
+
export interface RunAdvisorCallParams {
toolUseId: string;
/** Claude Code session id of the request that consumed the call. */
@@ -3244,15 +2910,15 @@ export interface RunAdvisorCallParams {
messages: any[];
models: string[];
collector: string | null;
- apiKeys: AdvisorApiKeys;
/** When given, the P7 records are written to its advisor log. */
cfg?: AdvisorSwapConfig;
/**
- * Test seam: HTTP client for panel and collector calls. Defaults to the
- * global fetch, resolved at call time. Production callers leave it unset;
- * it exists so tests can drive the failure paths (S4-S9) without a network.
+ * The startup plans and the routed members' chains. Absent ⇒ nothing was
+ * planned for this launch, and every member is S7 (there is no lazy path).
*/
- fetchImpl?: typeof fetch;
+ gateway?: AdvisorPanelGateway;
+ /** Absent ⇒ a Claude member has no credential and reports S7. */
+ claude?: ClaudeAdvisorDeps;
/**
* Test seam: receives the per-failure warning. Defaults to the sanctioned
* warning channel (`warnAdvisor` -> `logStderr`, which adds the `[advisor] `
@@ -3262,10 +2928,283 @@ export interface RunAdvisorCallParams {
warn?: (message: string) => void;
}
+/** The plan for one member; a launch with no gateway planned nothing. */
+function memberPlan(
+ gateway: AdvisorPanelGateway | undefined,
+ spec: string,
+ role: "panel" | "collector"
+): AdvisorModelPlan {
+ return (
+ gateway?.plan(spec, role) ?? {
+ kind: "refused",
+ spec,
+ role,
+ cause: "no-callable-hop",
+ reason: "advisor runtime not planned for this launch",
+ }
+ );
+}
+
+/** The stub paths one role uses for an empty answer and for a failure. */
+function stubPathsFor(
+ role: "panel" | "collector",
+ claude: boolean
+): { empty: AdvisorStubPath; error: AdvisorStubPath } {
+ if (role === "panel") {
+ return { empty: ADVISOR_STUB_PATHS.PANEL_EMPTY, error: ADVISOR_STUB_PATHS.PANEL_ERROR };
+ }
+ return {
+ empty: claude
+ ? ADVISOR_STUB_PATHS.ANTHROPIC_COLLECTOR_EMPTY
+ : ADVISOR_STUB_PATHS.COLLECTOR_EMPTY,
+ error: ADVISOR_STUB_PATHS.COLLECTOR_FAILED,
+ };
+}
+
+/** One member's request: who to ask, what to ask, and how long to wait. */
+interface MemberRequest {
+ spec: string;
+ role: "panel" | "collector";
+ system: string;
+ turns: AdvisorTurn[];
+ timeoutMs: number;
+}
+
+/**
+ * Run one panel member or the collector by its startup plan: a refused plan
+ * is S7/S9 with its refusal; a routed plan goes through the gateway's chain; a
+ * Claude plan is one call to api.anthropic.com on the arm its credential
+ * allows. Never rejects.
+ */
+async function runAdvisorMember(
+ req: MemberRequest,
+ params: Pick<RunAdvisorCallParams, "gateway" | "claude" | "warn">,
+ claudeAuth: () => Promise<ClaudeAdvisorAuth>
+): Promise<AdvisorModelOutcome> {
+ const plan = memberPlan(params.gateway, req.spec, req.role);
+ switch (plan.kind) {
+ case "refused":
+ return {
+ ...stubOutcome(
+ { role: req.role, requestedModel: req.spec, route: null },
+ stubPathsFor(req.role, false).error,
+ describeAdvisorRefusal(plan),
+ 0,
+ []
+ ),
+ servedBy: null,
+ billing: null,
+ };
+ case "routed":
+ return runRoutedMember(plan, req, params.gateway);
+ case "claude-native":
+ return runClaudeMember(plan, req, params, claudeAuth);
+ }
+}
+
+async function runRoutedMember(
+ plan: Extract<AdvisorModelPlan, { kind: "routed" }>,
+ req: MemberRequest,
+ gateway: AdvisorPanelGateway | undefined
+): Promise<AdvisorModelOutcome> {
+ const recordRoute: AdvisorRecordRoute = {
+ kind: "routed",
+ plannedChain: plan.hops.map((hop) => hop.displayName),
+ };
+ const base = { role: req.role, requestedModel: req.spec, route: null };
+ const paths = stubPathsFor(req.role, false);
+ if (!gateway) {
+ return {
+ ...stubOutcome(base, paths.error, "advisor gateway unavailable", 0, []),
+ recordRoute,
+ servedBy: null,
+ billing: null,
+ };
+ }
+ const result = await gateway.call({
+ plan,
+ payload: buildAdvisorPanelPayload(
+ plan.hops[0].modelSpec,
+ req.system,
+ req.turns,
+ ADVISOR_MAX_OUTPUT_TOKENS
+ ),
+ timeoutMs: req.timeoutMs,
+ });
+ return { ...classifyPanelResult(result, base, paths), recordRoute };
+}
+
+/** The hop-log facts of a routed call: which hops were entered, and the last one. */
+function routedHopFacts(result: AdvisorPanelResult): {
+ hopsTried: string[];
+ last: AdvisorPanelResult["hops"][number] | undefined;
+} {
+ const hopsTried: string[] = [];
+ for (const hop of result.hops) {
+ const uid = hop.uid ?? hop.provider;
+ if (!hopsTried.includes(uid)) hopsTried.push(uid);
+ }
+ return { hopsTried, last: result.hops[result.hops.length - 1] };
+}
+
+/** The outcome of a routed call that answered with no advice: a stub on one of the role's paths. */
+function routedStub(
+ base: Pick<AdvisorModelOutcome, "role" | "requestedModel" | "route">,
+ stubPath: AdvisorStubPath,
+ reason: string,
+ result: AdvisorPanelResult,
+ hopsTried: string[],
+ observed?: ObservedResponse
+): AdvisorModelOutcome {
+ return {
+ ...stubOutcome(base, stubPath, reason, result.latencyMs, [], observed),
+ hopsTried,
+ servedBy: null,
+ billing: null,
+ };
+}
+
+/**
+ * A gateway result as an outcome. The body was already redacted by value
+ * against every credential the chain signed with; the reason is sanitised by
+ * shape on top (`stubOutcome`). Status reads are `extractUpstreamStatus(body)
+ * ?? status`, so a terminal error the 400 remap rewrote is reported with the
+ * provider's own status.
+ */
+function classifyPanelResult(
+ result: AdvisorPanelResult,
+ base: Pick<AdvisorModelOutcome, "role" | "requestedModel" | "route">,
+ paths: { empty: AdvisorStubPath; error: AdvisorStubPath }
+): AdvisorModelOutcome {
+ const { hopsTried, last } = routedHopFacts(result);
+ if (result.failure || result.status === null) {
+ const reason = result.failure?.message ?? "the advisor call produced no response";
+ return routedStub(base, paths.error, reason, result, hopsTried);
+ }
+ const bodyStatus = extractUpstreamStatus(result.bodyText);
+ const observed: ObservedResponse = {
+ status: bodyStatus ?? result.status,
+ source: bodyStatus !== undefined ? "error.upstream_status" : "http_status",
+ bytes: new TextEncoder().encode(result.bodyText).byteLength,
+ };
+ const summary = summarizeErrorBody(result.bodyText);
+ if (result.streamFailure) {
+ const reason = `the stream failed after HTTP ${result.status}: ${summary}`;
+ return routedStub(base, paths.error, reason, result, hopsTried, observed);
+ }
+ const httpOk = result.status >= 200 && result.status < 300;
+ if (!httpOk || (bodyStatus !== undefined && bodyStatus >= 400)) {
+ const reason = `HTTP ${observed.status}: ${summary}`;
+ return routedStub(base, paths.error, reason, result, hopsTried, observed);
+ }
+ let data: unknown;
+ try {
+ data = JSON.parse(result.bodyText);
+ } catch {
+ const reason = `HTTP ${observed.status} but the response body is not JSON`;
+ return routedStub(base, paths.empty, reason, result, hopsTried, observed);
+ }
+ const advice = extractAdvice(data);
+ const usage = advice.usage ? { usage: advice.usage } : {};
+ if (!advice.text) {
+ const reason = `HTTP ${observed.status} but the response carried no advice text`;
+ return { ...routedStub(base, paths.empty, reason, result, hopsTried, observed), ...usage };
+ }
+ const servedBy: AdvisorServedBy | null = last
+ ? {
+ provider: last.uid ?? last.provider,
+ displayName: last.displayName,
+ arm: last.arm,
+ billing: last.billing,
+ }
+ : null;
+ return {
+ ...base,
+ upstreamStatus: observed.status,
+ upstreamStatusSource: observed.source,
+ responseBytes: observed.bytes,
+ latencyMs: result.latencyMs,
+ origin: "upstream",
+ stubPath: null,
+ text: advice.text,
+ hopsTried,
+ servedBy,
+ billing: servedBy?.billing ?? null,
+ routedModel: last?.wireModel ?? null,
+ ...usage,
+ };
+}
+
+/** The display name of the native Anthropic provider, for `servedBy`. */
+function nativeAnthropicDisplayName(): string {
+ return getProviderByName("native-anthropic")?.displayName ?? "Anthropic (Native)";
+}
+
+async function runClaudeMember(
+ plan: Extract<AdvisorModelPlan, { kind: "claude-native" }>,
+ req: MemberRequest,
+ params: Pick<RunAdvisorCallParams, "claude" | "warn">,
+ claudeAuth: () => Promise<ClaudeAdvisorAuth>
+): Promise<AdvisorModelOutcome> {
+ const outcome = await runClaudeAdvisorCall({
+ role: req.role,
+ spec: req.spec,
+ model: plan.model,
+ auth: await claudeAuth(),
+ system: req.system,
+ turns: req.turns,
+ timeoutMs: req.timeoutMs,
+ ...(params.claude?.fetchImpl ? { fetchImpl: params.claude.fetchImpl } : {}),
+ ...(params.warn ? { warn: params.warn } : {}),
+ });
+ const served = outcome.origin === "upstream" && outcome.servedArm !== null;
+ return {
+ ...outcome,
+ recordRoute: { kind: "claude-native", host: ANTHROPIC_API_HOST },
+ routedModel: plan.model,
+ servedBy: served
+ ? {
+ provider: "native-anthropic",
+ displayName: nativeAnthropicDisplayName(),
+ arm: outcome.servedArm,
+ billing: outcome.billing,
+ }
+ : null,
+ };
+}
+
+/** One credential resolution per advisor call, and none unless a Claude member asks. */
+function memoClaudeAuth(claude: ClaudeAdvisorDeps | undefined): () => Promise<ClaudeAdvisorAuth> {
+ let pending: Promise<ClaudeAdvisorAuth> | undefined;
+ return () => {
+ pending ??= claude
+ ? claude.resolveAuth()
+ : Promise.resolve({
+ arm: null,
+ reason: "no Claude credential: the advisor was given no access to the main request",
+ facts: {},
+ });
+ return pending;
+ };
+}
+
+/** The panel's turns: the prepared transcript flattened to text. */
+function panelTurns(panelMessages: any[]): AdvisorTurn[] {
+ return convertToOpenAIMessages(panelMessages).map((m) => ({
+ role: m.role === "assistant" ? "assistant" : "user",
+ content: m.content,
+ }));
+}
+
/**
* Runs one advisor call: every panel model in parallel, then the collector
* when there is more than one answer to synthesize. Never rejects.
*
+ * Each member runs by its startup plan (`gateway.plan`): a routed member as
+ * one in-process request through its startup-built chain, a Claude member as
+ * one call to api.anthropic.com. The Claude credential is resolved at most
+ * once per call, and only when a Claude member runs.
+ *
* The success path produces exactly the text it always did (R4): a single
* model's answer as-is, the collector's synthesis, or — with no collector —
* the `## model` sections. Failures produce plain text naming the model and
@@ -3273,16 +3212,29 @@ export interface RunAdvisorCallParams {
* warning.
*/
export async function runAdvisorCall(params: RunAdvisorCallParams): Promise<AdvisorCallOutcome> {
- const { toolUseId, messages, models, collector, apiKeys, fetchImpl } = params;
+ const { toolUseId, messages, models, collector } = params;
+ const claudeAuth = memoClaudeAuth(params.claude);
// What the panel is ASKED: the transcript with claudish's own "No such tool"
// plumbing for THIS call neutralised, and the advisor's question stated last.
// Built once and shared by every panel model, so they all answer the same
// question. Never mutates `messages` — it is the live upstream payload.
- const panelMessages = prepareAdvisorPanelMessages(messages, toolUseId);
+ const turns = panelTurns(prepareAdvisorPanelMessages(messages, toolUseId));
const panel = await Promise.all(
- models.map((m) => callAdvisorModel(m, panelMessages, apiKeys, fetchImpl))
+ models.map((spec) =>
+ runAdvisorMember(
+ {
+ spec,
+ role: "panel",
+ system: ADVISOR_SYSTEM_PROMPT,
+ turns,
+ timeoutMs: ADVISOR_PANEL_TIMEOUT_MS,
+ },
+ params,
+ claudeAuth
+ )
+ )
);
const successful = panel.filter(
(o): o is AdvisorModelOutcome & { text: string } => o.origin === "upstream"
@@ -3309,11 +3261,19 @@ export async function runAdvisorCall(params: RunAdvisorCallParams): Promise<Advi
result = { text: sections.join("\n\n"), isError: false };
resultOrigin = "upstream";
} else {
- collectorOutcome = await callCollectorModel(
- collector,
- successful.map((o) => ({ model: o.requestedModel, text: o.text })),
- apiKeys,
- fetchImpl
+ const adviceText = successful
+ .map((o, i) => `### Advisor ${i + 1} (${o.requestedModel})\n${o.text}`)
+ .join("\n\n");
+ collectorOutcome = await runAdvisorMember(
+ {
+ spec: collector,
+ role: "collector",
+ system: COLLECTOR_SYSTEM_PROMPT,
+ turns: [{ role: "user", content: adviceText }],
+ timeoutMs: ADVISOR_COLLECTOR_TIMEOUT_MS,
+ },
+ params,
+ claudeAuth
);
if (collectorOutcome.origin === "upstream" && collectorOutcome.text !== undefined) {
result = { text: collectorOutcome.text, isError: false };
@@ -3350,18 +3310,3 @@ export async function runAdvisorCall(params: RunAdvisorCallParams): Promise<Advi
warnOnAdvisorFailures(outcome, params.warn);
return outcome;
}
-
-/**
- * The original entry point, kept for compatibility: the delivered text only.
- * Prefer `runAdvisorCall`, which also reports provenance and `isError`.
- */
-export async function fetchMultiModelAdvice(
- toolUseId: string,
- messages: any[],
- models: string[],
- collector: string | null,
- apiKeys: AdvisorApiKeys
-): Promise<string> {
- const outcome = await runAdvisorCall({ toolUseId, messages, models, collector, apiKeys });
- return outcome.result.text;
-}
diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts
index d6249ee4..ed08c82d 100644
--- a/packages/cli/src/index.ts
+++ b/packages/cli/src/index.ts
@@ -1046,13 +1046,12 @@ async function runCli() {
// "skipped" — local model or --models-skip-update
// "hard_fail" — missing cache + network failure → exit 1
//
- // AHEAD OF THE --advisor CHECK, deliberately. That check decides whether
- // each panel model is routable, and every one of its answers is read from
- // the catalog (`advisorRouteFor` → `lookupOpenRouterId`). With a cold
- // catalog nothing is known, so the router falls back to passthrough and an
- // UNROUTABLE panel model cannot be refused — it fails on the first advisor
- // call instead, which is exactly the refusal this ordering exists to make
- // possible (`kimi-k3` is refused only because the catalog was warm).
+ // AHEAD OF THE --advisor CHECK, deliberately. That check plans every panel
+ // member through `explainRoute` (`planAdvisorModel`), which reads its route
+ // candidates from the catalog. With a cold catalog a bare member has no
+ // candidates and is refused (`catalog-unreadable`), and an explicit one is
+ // taken as written; warming first is what lets startup plan, and refuse,
+ // from real data.
const warmOutcome = await traceSpan("startup:catalog-warm", () =>
warmCatalogIfNeeded(cliConfig)
);
@@ -1097,6 +1096,11 @@ async function runCli() {
// belongs to Claude Code alone, and the cost line (N2) is not optional.
// `cliConfig.advisor` is set only by the --advisor flag on this command line
// (N1); nothing stored can turn it on. Decision logic: advisor-startup.ts.
+ //
+ // Startup PLANS every member and BUILDS every routed member's chain here,
+ // before the port exists; the proxy's panel gateway runs on exactly that
+ // runtime (no lazy path), and owns its handlers from then on.
+ let advisorRuntime: import("./handlers/advisor-panel-gateway.js").AdvisorRuntime | undefined;
if (cliConfig.advisor) {
const { evaluateAdvisorStartup } = await import("./advisor-startup.js");
const decision = await traceSpan("startup:advisor-check", () =>
@@ -1112,23 +1116,31 @@ async function runCli() {
// A DEFAULTED collector that cannot be called is dropped here, before
// createProxyServer reads advisorCollector, so no doomed call is made.
cliConfig.advisorCollector = decision.effectiveCollector;
+ advisorRuntime = decision.runtime;
// The warm step above ran first and hard-fails out, so the catalog is
// normally populated by now. It can still be COLD — `--models-skip-update`,
// a local main model (both "skipped"), or a cache claudish could not read.
- // Then `advisorRouteFor` had no data to check any panel model against, so
- // "routable" was not decided, it was assumed. Say which models that leaves
- // unverified instead of proceeding as though they had passed.
+ // A BARE member is then refused above (routing answers `catalog-unreadable`),
+ // so only an explicit `provider@model` member can have passed without the
+ // catalog: its provider was taken as written. Say which ones.
const { getCatalogEntries } = await import("./providers/catalog-client.js");
if (getCatalogEntries() === null) {
- const panel = cliConfig.advisorModels ?? [];
- decision.notice.push(
- ` WARNING: the model catalog is not loaded (catalog warm: ${warmOutcome}), so claudish ` +
- `could not verify that ${panel.length === 1 ? "this panel model is" : "these panel models are"} ` +
- `routable: ${panel.join(", ")}. An unroutable one fails on its first advisor call instead ` +
- "of being refused here. Run `claudish --models-refresh` to check them at launch."
- );
+ const unverified = [...decision.runtime.plans.values()]
+ .filter((p) => p.kind === "routed" && p.invocationMode === "explicit-model")
+ .map((p) => p.spec);
+ if (unverified.length > 0) {
+ decision.notice.push(
+ ` WARNING: the model catalog is not loaded (catalog warm: ${warmOutcome}), so claudish ` +
+ `could not check what ${unverified.length === 1 ? "this model's provider" : "these models' providers"} ` +
+ `publish: ${unverified.join(", ")}. A model the provider does not serve fails on its first ` +
+ "advisor call instead of being refused here. Run `claudish --models-refresh` to check at launch."
+ );
+ }
}
process.stderr.write(`${decision.notice.join("\n")}\n`);
+ // The debug and always-on logs get the notice too, so a run's log shows
+ // what the launch promised next to what each advisor call did.
+ for (const line of decision.notice) log(`[advisor-notice] ${line}`);
}
}
@@ -1223,6 +1235,7 @@ async function runCli() {
isInteractive: cliConfig.interactive,
advisorModels: cliConfig.advisorModels,
advisorCollector: cliConfig.advisorCollector,
+ ...(advisorRuntime ? { advisorRuntime } : {}),
// Present only when `--model` was a pinned chain; `explicitModel` above
// is its first element, so the proxy can match the two.
modelChain: cliConfig.monitor ? undefined : cliConfig.modelChain,
diff --git a/packages/cli/src/proxy-server.ts b/packages/cli/src/proxy-server.ts
index 0a23aaad..2d0328c6 100644
--- a/packages/cli/src/proxy-server.ts
+++ b/packages/cli/src/proxy-server.ts
@@ -308,12 +308,22 @@ export async function createProxyServer(
* launch without the advisor never reaches the wrapper.
*/
const advisorPresence = createAdvisorPresenceMonitor();
+ // The advisor panel gateway: an in-process Hono app that is never served,
+ // over the chains startup built from the advisor plans. It shares nothing
+ // with `app` below — no lease middleware (the gateway owns one lease per
+ // call), no `resolveHandlerForRequest` (no monitor branch, slot map or
+ // `--model` remap), no advisor wrapper.
+ const advisorRuntime = options.advisorRuntime;
+ const advisorPanel = advisorRuntime ? createAdvisorPanelGateway(advisorRuntime) : undefined;
const withAdvisor = (handler: ModelHandler, presence?: AdvisorPresenceMonitor): ModelHandler =>
withAdvisorSwap(
handler,
loadAdvisorSwapConfig(options.advisorModels, options.advisorCollector),
{
presence,
+ ...(advisorPanel ? { panel: advisorPanel } : {}),
+ // Without a runtime no launch decision exists, so no bearer may sign.
+ ...(advisorRuntime ? { claude: { childAuth: advisorRuntime.childAuth } } : {}),
}
);
/**
@@ -1064,14 +1074,6 @@ export async function createProxyServer(
}
});
- // The advisor panel gateway: an in-process Hono app that is never served,
- // over the chains startup built from the advisor plans. It shares nothing
- // with `app` above — no lease middleware (the gateway owns one lease per
- // call), no `resolveHandlerForRequest` (no monitor branch, slot map or
- // `--model` remap), no advisor wrapper.
- const advisorRuntime = options.advisorRuntime;
- const advisorPanel = advisorRuntime ? createAdvisorPanelGateway(advisorRuntime) : undefined;
-
// Bun's NATIVE server — not @hono/node-server.
//
// claudish only ever runs under Bun (bin/claudish.cjs is a Node bootstrapper