Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(advisor): keep a built-pending hop's planned uid in the hop log
Jack Rudenkoe69fb1eb910b
diff --git a/packages/cli/src/handlers/advisor-panel-gateway.ts b/packages/cli/src/handlers/advisor-panel-gateway.tsindex 61a4c2db..f7f27665 100644--- a/packages/cli/src/handlers/advisor-panel-gateway.ts+++ b/packages/cli/src/handlers/advisor-panel-gateway.ts@@ -122,7 +122,7 @@ export function createAdvisorPanelGateway(runtime: AdvisorRuntime): AdvisorPanelconst hops: HopAttempt[] = [];const snapshot = (): AdvisorPanelResult["hops"] =>hops.slice().map((h) => {- const hop = build ? planHopForTransport(req.plan, build, h.provider) : null;+ const hop = build ? planHopForTransport(req.plan, build, h) : null;return { ...h, uid: hop?.provider ?? null, displayName: hop?.displayName ?? h.provider };});if (!build?.handler) {diff --git a/packages/cli/src/handlers/advisor-route-plan.ts b/packages/cli/src/handlers/advisor-route-plan.tsindex 442817d8..c0377927 100644--- a/packages/cli/src/handlers/advisor-route-plan.ts+++ b/packages/cli/src/handlers/advisor-route-plan.ts@@ -21,6 +21,7 @@* lookup the runtime also uses.*/+import type { Context } from "hono";import type { ChildAuthControl, ChildAuthSource } from "../claude-runner.js";import { log } from "../logger.js";import { ensureCatalogReady } from "../providers/catalog-client.js";@@ -45,7 +46,7 @@ import { ANTHROPIC_API_HOST, claudeAdvisorModelFor } from "./advisor-claude-routimport { ComposedHandler } from "./composed-handler.js";import { type FallbackCandidate, FallbackHandler, isRetryableError } from "./fallback-handler.js";import type { HandlerSet } from "./handler-set.js";-import type { HopAttempt, HopBilling } from "./shared/call-context.js";+import { type HopAttempt, type HopBilling, withPlannedHop } from "./shared/call-context.js";import { isSubscriptionProvider } from "./shared/remote-provider-types.js";import type { ModelHandler } from "./types.js";@@ -513,7 +514,38 @@ async function classifyHandler(handler: ModelHandler | null, hop: AdvisorHop): P: handler instanceof CatalogPendingHandler? "catalog-pending": "other";- return { kind: "built-pending", handler, handlerKind };+ return {+ kind: "built-pending",+ handler: new PlannedHopHandler(handler, hop.provider),+ handlerKind,+ };+}++/**+ * A built-pending hop, carrying its planned identity to call time. The+ * transport it serves through is chosen per request, and its name need not be+ * the planned uid (`google` serves through `GeminiProviderTransport`, named+ * `gemini`), so every hop-log entry recorded while this hop runs carries the+ * planned uid (`withPlannedHop` → `recordHopEntry`) rather than being matched+ * back by name. The uid is on the entry from the moment it is pushed, so a+ * timed-out call's snapshot already has it.+ *+ * Wrapping is safe here and nowhere else: `FallbackHandler` reads fallback+ * metadata only off a `ComposedHandler`, and a pending handler is never one.+ */+export class PlannedHopHandler implements ModelHandler {+ constructor(+ readonly inner: ModelHandler,+ readonly plannedUid: string+ ) {}++ handle(c: Context, payload: unknown): Promise<Response> {+ return withPlannedHop(c, this.plannedUid, () => this.inner.handle(c, payload));+ }++ shutdown(): Promise<void> {+ return this.inner.shutdown();+ }}function assembleChain(key: string, plan: RoutedPlan, builds: HopBuild[]): AdvisorChainBuild {@@ -549,16 +581,17 @@ function assembleChain(key: string, plan: RoutedPlan, builds: HopBuild[]): Advis}/**- * The plan hop a hop-log entry belongs to: through the build's transport-name- * map for a `built` hop, else by provider uid (a `built-pending` hop's- * transport is the provider the plan named). Null when neither matches.+ * The plan hop a hop-log entry belongs to: the planned uid a `built-pending`+ * hop stamped on it (`PlannedHopHandler`), else the build's transport-name map+ * for a `built` hop, else the transport name taken as a uid. Null when none+ * matches.*/export function planHopForTransport(plan: RoutedPlan,build: AdvisorChainBuild,- transportName: string+ entry: Pick<HopAttempt, "provider" | "plannedUid">): AdvisorHop | null {- const uid = build.uidByTransportName.get(transportName) ?? transportName;+ const uid = entry.plannedUid ?? build.uidByTransportName.get(entry.provider) ?? entry.provider;return plan.hops.find((hop) => hop.provider === uid) ?? null;}diff --git a/packages/cli/src/handlers/shared/call-context.ts b/packages/cli/src/handlers/shared/call-context.tsindex 6be920f4..43a1cf4e 100644--- a/packages/cli/src/handlers/shared/call-context.ts+++ b/packages/cli/src/handlers/shared/call-context.ts@@ -376,12 +376,21 @@ export interface HopAttempt {arm: CredentialArm | null;/** `isSubscriptionProvider(provider)` right after signing; null until signed. */billing: HopBilling | null;+ /**+ * The planned hop's provider uid, stamped by a caller that knows which+ * planned hop this entry ran under (the advisor's built-pending hops), so it+ * is never reconstructed from `provider`: a pending `google` hop serves+ * through a transport named `gemini`.+ */+ plannedUid?: string;}declare module "hono" {interface ContextVariableMap {/** An internal call's hop log; see `HOP_LOG_KEY`. */claudishHopLog: HopAttempt[];+ /** The planned hop a pending advisor hop runs under; see `PLANNED_HOP_KEY`. */+ claudishPlannedHop: string | undefined;}}@@ -402,7 +411,41 @@ export function recordHopEntry(): HopAttempt | undefined {const hops = hopLogOf(c);if (!hops) return undefined;- const entry: HopAttempt = { provider, wireModel, arm: null, billing: null };+ const plannedUid = readCallVar<string>(c, PLANNED_HOP_KEY);+ const entry: HopAttempt = {+ provider,+ wireModel,+ arm: null,+ billing: null,+ ...(plannedUid ? { plannedUid } : {}),+ };hops.push(entry);return entry;}++/**+ * The Context key of the planned hop uid a pending advisor hop is running+ * under. Set only by `withPlannedHop`, only while a hop log is installed.+ */+export const PLANNED_HOP_KEY = "claudishPlannedHop" as const;++/**+ * Run `work` with `plannedUid` on `c`, so every hop-log entry it records+ * carries it; the previous value is restored when the work settles (the next+ * candidate of a chain must not inherit it). Without a hop log, `work` runs+ * unchanged and nothing is written.+ */+export async function withPlannedHop<T>(+ c: Context,+ plannedUid: string,+ work: () => Promise<T>+): Promise<T> {+ if (!hopLogOf(c)) return work();+ const previous = readCallVar<string>(c, PLANNED_HOP_KEY);+ writeCallVar(c, PLANNED_HOP_KEY, plannedUid);+ try {+ return await work();+ } finally {+ writeCallVar(c, PLANNED_HOP_KEY, previous);+ }+}