@vellumai/assistant 0.12.4-dev.202609242017.5a6d38c → 0.12.4-dev.202609250044.584e917

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vellumai/assistant",
3
- "version": "0.12.4-dev.202609242017.5a6d38c",
3
+ "version": "0.12.4-dev.202609250044.584e917",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {
@@ -181,6 +181,82 @@ describe("AgentLoop", () => {
181
181
  expect(calls[1].options?.config?.overrideProfile).toBe("quality-optimized");
182
182
  });
183
183
 
184
+ test("the auto origin follows the routed override and drops once a switch replaces it", async () => {
185
+ const toolCallId = "tool-1";
186
+ const { provider, calls } = createMockProvider([
187
+ toolUseResponse(toolCallId, "read_file", { path: "/tmp/test.txt" }),
188
+ textResponse("done"),
189
+ ]);
190
+ let overrideProfile = "quality-optimized";
191
+ const loop = new AgentLoop({
192
+ provider: provider,
193
+ systemPrompt: "system",
194
+ conversationId: "test-conversation",
195
+ tools: dummyTools,
196
+ toolExecutor: async () => {
197
+ // A confirmed profile session mid-turn: the user's pick, not Auto's.
198
+ overrideProfile = "cost-optimized";
199
+ return { content: "ok", isError: false };
200
+ },
201
+ });
202
+
203
+ await loop.run({
204
+ messages: [userMessage],
205
+ onEvent: collectEvents([]),
206
+ trust: { sourceChannel: "vellum", trustClass: "unknown" },
207
+ requestId: "req-1",
208
+ callSite: "mainAgent",
209
+ overrideProfile: "quality-optimized",
210
+ overrideProfileOrigin: "auto",
211
+ resolveOverrideProfile: () => overrideProfile,
212
+ });
213
+
214
+ expect(calls).toHaveLength(2);
215
+ expect(calls[0].options?.config?.overrideProfile).toBe("quality-optimized");
216
+ expect(calls[0].options?.config?.overrideProfileOrigin).toBe("auto");
217
+ expect(calls[1].options?.config?.overrideProfile).toBe("cost-optimized");
218
+ expect(calls[1].options?.config?.overrideProfileOrigin).toBeUndefined();
219
+ });
220
+
221
+ test("a pre-model-call hook keeps the auto origin unless it changes the profile", async () => {
222
+ let hookProfile: string | null | undefined;
223
+ registerPlugin({
224
+ manifest: { name: "profile-hook", version: "0.0.1" },
225
+ hooks: {
226
+ "pre-model-call": async (ctx) =>
227
+ hookProfile === undefined
228
+ ? ctx
229
+ : { ...ctx, modelProfile: hookProfile },
230
+ },
231
+ });
232
+ const run = async () => {
233
+ const { provider, calls } = createMockProvider([textResponse("ok")]);
234
+ const loop = new AgentLoop({
235
+ provider: provider,
236
+ systemPrompt: "system",
237
+ conversationId: "test-conversation",
238
+ tools: dummyTools,
239
+ });
240
+ await loop.run({
241
+ messages: [userMessage],
242
+ onEvent: collectEvents([]),
243
+ trust: { sourceChannel: "vellum", trustClass: "unknown" },
244
+ requestId: "req-1",
245
+ callSite: "mainAgent",
246
+ overrideProfile: "quality-optimized",
247
+ overrideProfileOrigin: "auto",
248
+ });
249
+ return calls[0].options?.config;
250
+ };
251
+
252
+ hookProfile = undefined;
253
+ expect((await run())?.overrideProfileOrigin).toBe("auto");
254
+ hookProfile = "cost-optimized";
255
+ expect((await run())?.overrideProfileOrigin).toBeUndefined();
256
+ hookProfile = null;
257
+ expect((await run())?.overrideProfileOrigin).toBeUndefined();
258
+ });
259
+
184
260
  test("re-resolves max input tokens before truncating tool results", async () => {
185
261
  const toolCallId = "tool-1";
186
262
  const toolOutput = "x".repeat(2_500);
@@ -109,6 +109,37 @@ describe("resolveUsageAttribution", () => {
109
109
  expectResolvedProviderModelMatchesResolver("mainAgent", "pinned");
110
110
  });
111
111
 
112
+ test("reports an override the Auto router chose as the auto source", () => {
113
+ setLlmConfig({
114
+ profiles: {
115
+ active: { provider: "openai", model: "gpt-5.4" },
116
+ routed: { provider: "gemini", model: "gemini-3-pro" },
117
+ },
118
+ activeProfile: "active",
119
+ });
120
+
121
+ const snapshot = resolveUsageAttribution({
122
+ callSite: "mainAgent",
123
+ overrideProfile: "routed",
124
+ overrideProfileOrigin: "auto",
125
+ });
126
+
127
+ expect(snapshot).toMatchObject({
128
+ appliedProfile: "routed",
129
+ profileSource: "auto",
130
+ resolvedProvider: "gemini",
131
+ resolvedModel: "gemini-3-pro",
132
+ });
133
+ // The origin only relabels an override that actually won.
134
+ expect(
135
+ resolveUsageAttribution({
136
+ callSite: "mainAgent",
137
+ overrideProfile: "missing",
138
+ overrideProfileOrigin: "auto",
139
+ }).profileSource,
140
+ ).toBe("active");
141
+ });
142
+
112
143
  test("resolves call-site profile attribution", () => {
113
144
  setLlmConfig({
114
145
  profiles: {
package/src/agent/loop.ts CHANGED
@@ -747,6 +747,12 @@ interface AgentLoopRunOptionsBase {
747
747
  * call-site named profile. Missing profile names silently fall through.
748
748
  */
749
749
  overrideProfile?: string;
750
+ /**
751
+ * Who chose `overrideProfile`: `"auto"` when the Auto profile's router
752
+ * picked it for this turn. Threaded onto each send's config so usage
753
+ * attribution reports the routed profile under the `auto` source.
754
+ */
755
+ overrideProfileOrigin?: "auto";
750
756
  /**
751
757
  * Float the override profile above the call-site layers (named site
752
758
  * profile + call-site override) for non-main-agent call sites — the
@@ -1454,6 +1460,7 @@ export class AgentLoop {
1454
1460
  supportsDynamicUi = true,
1455
1461
  trust,
1456
1462
  overrideProfile,
1463
+ overrideProfileOrigin,
1457
1464
  forceOverrideProfile = false,
1458
1465
  resolveOverrideProfile,
1459
1466
  onModelCallPrepared,
@@ -2036,6 +2043,16 @@ export class AgentLoop {
2036
2043
  const effectiveOverrideProfile = resolveEffectiveOverrideProfile();
2037
2044
  if (effectiveOverrideProfile) {
2038
2045
  providerConfig.overrideProfile = effectiveOverrideProfile;
2046
+ // The origin describes the turn-start override. A profile switched
2047
+ // in mid-turn (a confirmed profile session) is the user's pick, so
2048
+ // the origin applies only while the effective override is still
2049
+ // the one the turn started with.
2050
+ if (
2051
+ overrideProfileOrigin &&
2052
+ effectiveOverrideProfile === overrideProfile
2053
+ ) {
2054
+ providerConfig.overrideProfileOrigin = overrideProfileOrigin;
2055
+ }
2039
2056
  if (forceOverrideProfile) {
2040
2057
  providerConfig.forceOverrideProfile = true;
2041
2058
  }
@@ -2236,7 +2253,13 @@ export class AgentLoop {
2236
2253
  // resolver layers `llm.profiles[overrideProfile]` at the top of
2237
2254
  // precedence for the user-facing call, so a model router can pick
2238
2255
  // the profile per message; clearing it drops any seeded override.
2256
+ // The hook context is seeded with the effective override, so an
2257
+ // unchanged profile is still the router's pick and keeps its
2258
+ // origin; a profile the hook changed or cleared is the hook's.
2239
2259
  const hookModelProfile = finalPreModelCtx.modelProfile?.trim();
2260
+ if (hookModelProfile !== effectiveOverrideProfile) {
2261
+ delete providerConfig.overrideProfileOrigin;
2262
+ }
2240
2263
  if (hookModelProfile) {
2241
2264
  providerConfig.overrideProfile = hookModelProfile;
2242
2265
  if (forceOverrideProfile) {
@@ -1723,6 +1723,7 @@ export async function runAgentLoopImpl(
1723
1723
  supportsDynamicUi: conversationSupportsDynamicUi(ctx),
1724
1724
  trust: loopTrust,
1725
1725
  overrideProfile: turnOverrideProfile,
1726
+ ...(autoRoute ? { overrideProfileOrigin: "auto" as const } : {}),
1726
1727
  ...(forceOverrideProfile ? { forceOverrideProfile: true } : {}),
1727
1728
  resolveOverrideProfile: refreshCurrentProfileState,
1728
1729
  ...(onModelCallPrepared !== undefined ? { onModelCallPrepared } : {}),
@@ -2147,6 +2148,9 @@ export async function runAgentLoopImpl(
2147
2148
  // path to the terminal SSE that re-enables the composer.
2148
2149
  ctx.messages = restoredHistory;
2149
2150
 
2151
+ // The row's override is whichever profile served the last call.
2152
+ const mainRowOverrideProfile =
2153
+ state.exchangeInferenceProfile ?? refreshCurrentProfileState() ?? null;
2150
2154
  emitUsage(
2151
2155
  ctx,
2152
2156
  state.exchangeInputTokens,
@@ -2171,16 +2175,17 @@ export async function runAgentLoopImpl(
2171
2175
  // backup (via `state.exchangeProviderName` / `state.model`), so
2172
2176
  // attributing the profile from the primary would write a row that
2173
2177
  // contradicts itself. `forceOverrideProfile` floats it above the
2174
- // call-site profile exactly as the fallback dispatch did.
2178
+ // call-site profile exactly as the fallback dispatch did. The Auto
2179
+ // origin applies only while the row's override is still the router's
2180
+ // pick: a profile switched in mid-turn is the user's.
2175
2181
  {
2176
2182
  callSite: inferenceCallSite,
2177
- overrideProfile:
2178
- state.exchangeInferenceProfile ??
2179
- refreshCurrentProfileState() ??
2180
- null,
2183
+ overrideProfile: mainRowOverrideProfile,
2181
2184
  ...(state.exchangeInferenceProfile !== undefined
2182
2185
  ? { forceOverrideProfile: true }
2183
- : {}),
2186
+ : autoRoute && mainRowOverrideProfile === autoRoute.profile
2187
+ ? { overrideProfileOrigin: "auto" as const }
2188
+ : {}),
2184
2189
  },
2185
2190
  turnCronRunId,
2186
2191
  );
@@ -689,6 +689,7 @@ function normalizeSendMessageOptions(
689
689
  // (after the `openai` promptCacheKey copy above) so they never leak into
690
690
  // provider request bodies even when callers set them without a `callSite`.
691
691
  delete nextConfig.overrideProfile;
692
+ delete nextConfig.overrideProfileOrigin;
692
693
  delete nextConfig.forceOverrideProfile;
693
694
  delete nextConfig.selectionSeed;
694
695
  delete nextConfig.conversationId;
@@ -703,6 +704,7 @@ function normalizeSendMessageOptions(
703
704
  const attribution = resolveUsageAttribution({
704
705
  callSite: config.callSite,
705
706
  overrideProfile: config.overrideProfile,
707
+ overrideProfileOrigin: config.overrideProfileOrigin,
706
708
  forceOverrideProfile: config.forceOverrideProfile,
707
709
  selectionSeed: config.selectionSeed,
708
710
  });
@@ -346,6 +346,13 @@ export interface SendMessageConfig {
346
346
  * silently fall through.
347
347
  */
348
348
  overrideProfile?: string;
349
+ /**
350
+ * Who chose `overrideProfile`: `"auto"` when the Auto profile's router
351
+ * picked it for the turn. Usage attribution reports it as the profile
352
+ * source so a routed turn is not counted as a user pin. A routing-time
353
+ * concern only; stripped before any provider wire request.
354
+ */
355
+ overrideProfileOrigin?: "auto";
349
356
  /**
350
357
  * When true, the resolver floats `overrideProfile` above the call-site
351
358
  * layers (named site profile + call-site override) for non-main-agent call
@@ -77,9 +77,11 @@ export class UsageTrackingProvider implements Provider {
77
77
  const attribution = resolveUsageAttribution({
78
78
  callSite: config.callSite,
79
79
  overrideProfile: appliedOverride,
80
- ...(response.actualInferenceProfile !== undefined
81
- ? { forceOverrideProfile: true }
82
- : {}),
80
+ // A rerouted serve ran on the backup the wrapper chose, not on the
81
+ // router's pick, so the origin applies only to the original route.
82
+ ...(response.actualInferenceProfile === undefined
83
+ ? { overrideProfileOrigin: config.overrideProfileOrigin }
84
+ : { forceOverrideProfile: true }),
83
85
  });
84
86
  const providerName = response.actualProvider ?? this.inner.name;
85
87
  const pricingUsage = buildPricingUsageFromResponse(
@@ -10,11 +10,15 @@ import { safeStringSlice } from "../util/unicode.js";
10
10
 
11
11
  const MAX_METADATA_VALUE_LENGTH = 128;
12
12
 
13
+ /** `profileSource` reported for a turn whose profile the Auto router chose. */
14
+ export const AUTO_PROFILE_SOURCE = "auto";
15
+
13
16
  export type UsageAttributionProfileSource =
14
17
  | "call_site"
15
18
  | "conversation"
16
19
  | "active"
17
20
  | "default"
21
+ | typeof AUTO_PROFILE_SOURCE
18
22
  | "unknown";
19
23
 
20
24
  export interface UsageAttributionInput {
@@ -27,6 +31,12 @@ export interface UsageAttributionInput {
27
31
  */
28
32
  profileResolutionCallSite?: LLMCallSite | null;
29
33
  overrideProfile?: string | null;
34
+ /**
35
+ * Who chose `overrideProfile`. `"auto"` marks a profile the Auto profile's
36
+ * router picked for the turn, so usage rows can tell an Auto pick from a
37
+ * user pin that landed on the same profile. Absent for a user pin.
38
+ */
39
+ overrideProfileOrigin?: "auto" | null;
30
40
  /**
31
41
  * Mirrors `ResolveCallSiteOpts.forceOverrideProfile`: the override profile
32
42
  * was floated above the call-site layers for this request, so attribution
@@ -227,9 +237,16 @@ function appliedProfileFromWinnerSelection(
227
237
  call_site: "call_site",
228
238
  default: "default",
229
239
  } as const;
240
+ // An override the Auto router chose is reported as its own source: the
241
+ // applied profile is the routed arm, so without this a routed turn would
242
+ // be indistinguishable from a user who pinned that arm.
243
+ const profileSource =
244
+ selection.source === "override" && input.overrideProfileOrigin === "auto"
245
+ ? AUTO_PROFILE_SOURCE
246
+ : sourceBySelection[selection.source];
230
247
  return {
231
248
  appliedProfile: selection.profileName,
232
- profileSource: sourceBySelection[selection.source],
249
+ profileSource,
233
250
  };
234
251
  }
235
252