Skip to content

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

feat(adapters): record each signed hop's provider and arm on the context

Jack Rudenko3a5c904d9135
diff --git a/packages/cli/src/handlers/composed-handler.ts b/packages/cli/src/handlers/composed-handler.ts
index 733aa822..0b38b5d8 100644
--- a/packages/cli/src/handlers/composed-handler.ts
+++ b/packages/cli/src/handlers/composed-handler.ts
@@ -110,6 +110,7 @@ import {
respondAuthFailure,
} from "./shared/auth-failure.js";
import {
+ type HopAttempt,
abortedResponse,
addSigningSecrets,
callAbortError,
@@ -117,6 +118,7 @@ import {
callAborted,
credentialValuesFromRequest,
raceCallAbort,
+ recordHopEntry,
sleepUnlessAborted,
withCallSignal,
} from "./shared/call-context.js";
@@ -592,6 +594,17 @@ export class ComposedHandler implements ModelHandler {
);
}
+ /**
+ * Fill a hop-log entry once the request is signed: the arm from the
+ * binding the credential half returned (`RequestAuth.arm`, never inferred
+ * from a resolved object's truthiness), and the billing the provider has
+ * now, which for `openai-codex` reflects the arm just recorded.
+ */
+ private markHopSigned(hop: HopAttempt, binding: AuthBinding | undefined): void {
+ hop.arm = binding?.auth?.arm ?? null;
+ hop.billing = isSubscriptionProvider(this.provider.name) ? "subscription" : "metered";
+ }
+
/**
* The answer when an internal call's deadline passed (`call-context.ts`).
* No classification, no recovery, nothing on stderr: the caller has already
@@ -1176,6 +1189,10 @@ export class ComposedHandler implements ModelHandler {
// Undefined on every request from Claude Code, and then every use below
// collapses to the expression that ran before it existed.
const callSignal = callAbortSignal(c);
+ // This hop's entry in an internal call's hop log (advisor gateway only):
+ // pushed on entry, so the log lists every hop whose handle() ran, and
+ // filled with the signing arm once 5b has signed.
+ const hop = recordHopEntry(c, this.provider.name, this.bareModelName);
if (callSignal?.aborted) return this.respondCallAborted(startTime, fallbackMeta);
// 1. Transform incoming Claude-format request
const { claudeRequest, droppedParams } = transformOpenAIToClaude(payload);
@@ -1582,6 +1599,7 @@ export class ComposedHandler implements ModelHandler {
this.tokenTracker.setProviderDisplayName(this.provider.displayName);
}
}
+ if (hop) this.markHopSigned(hop, binding);
// Update context window if provider dynamically discovered it
// (e.g., from OpenRouter model catalog or local model API).
//
@@ -2188,6 +2206,8 @@ export class ComposedHandler implements ModelHandler {
return notResent;
}
binding = refreshed;
+ // A re-sign replaces the arm on this hop's entry in place.
+ if (hop) this.markHopSigned(hop, binding);
endpoint = this.provider.getEndpoint(this.targetModel, binding);
}
const retryHeaders = await raceCallAbort(fetchHeaders(binding), callSignal);
diff --git a/packages/cli/src/handlers/shared/call-context.ts b/packages/cli/src/handlers/shared/call-context.ts
index 143a0f0f..a64a8aa9 100644
--- a/packages/cli/src/handlers/shared/call-context.ts
+++ b/packages/cli/src/handlers/shared/call-context.ts
@@ -11,6 +11,7 @@
*/
import type { Context } from "hono";
+import type { CredentialArm } from "../../auth/credentials/types.js";
import { type SigningLease, acquireSigningLease } from "../../redact.js";
// ─── Signing secrets ─────────────────────────────────────────────────────────
@@ -343,3 +344,60 @@ export function abortedResponse(): Response {
}
);
}
+
+// ─── Hop log ─────────────────────────────────────────────────────────────────
+
+/**
+ * The Context key of an internal call's hop log. Installed ONLY by a caller
+ * that needs to know which hop served it and on which credential arm (the
+ * advisor panel gateway); a main request has none and records nothing.
+ */
+export const HOP_LOG_KEY = "claudishHopLog" as const;
+
+/** How a hop bills: by the provider's subscription status right after signing. */
+export type HopBilling = "subscription" | "metered";
+
+/**
+ * One `handle()` entry of one hop. A closed set of non-secret fields: no
+ * header, no credential, no request body, so a log of these can be printed
+ * or serialised as it is.
+ */
+export interface HopAttempt {
+ /** The handler's claudish provider (`provider.name`). */
+ provider: string;
+ /** The model id this hop sends upstream. */
+ wireModel: string;
+ /** The arm that signed (`RequestAuth.arm`); null until signed, or when the transport names none. */
+ arm: CredentialArm | null;
+ /** `isSubscriptionProvider(provider)` right after signing; null until signed. */
+ billing: HopBilling | null;
+}
+
+declare module "hono" {
+ interface ContextVariableMap {
+ /** An internal call's hop log; see `HOP_LOG_KEY`. */
+ claudishHopLog: HopAttempt[];
+ }
+}
+
+/** The installed hop log, or `undefined` (every main request). */
+export function hopLogOf(c: Context | undefined): HopAttempt[] | undefined {
+ return readCallVar<HopAttempt[]>(c, HOP_LOG_KEY);
+}
+
+/**
+ * Append an unsigned entry for a hop whose `handle()` was just entered, and
+ * return it so the handler can fill `arm` and `billing` once it has signed.
+ * `undefined`, and nothing recorded, when no hop log is installed.
+ */
+export function recordHopEntry(
+ c: Context | undefined,
+ provider: string,
+ wireModel: string
+): HopAttempt | undefined {
+ const hops = hopLogOf(c);
+ if (!hops) return undefined;
+ const entry: HopAttempt = { provider, wireModel, arm: null, billing: null };
+ hops.push(entry);
+ return entry;
+}