| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703 |
- import { describe, expect, it, vi } from "vitest";
- import { createTestModelSelection } from "./test-model-selection.js";
- import {
- CoreErrorType,
- ModelErrorCode,
- ModelProtocolError,
- ModelRetryBudget,
- SessionEventType,
- createModelId,
- createModelProviderId,
- createSessionId,
- type Logger,
- } from "@zcode/contracts";
- import { createTestAgentRuntime } from "./test-agent-runtime.js";
- import { createTestSessionEventStore } from "./test-event-store.js";
- import { createTestModelFactory } from "./test-runtime-model.js";
- import { createRecordingSubagentSessionStore } from "./subagent-test-store.js";
- /** 只捕获 warn 的 Logger 桩:退回路径的留痕本身是回归断言的一部分。 */
- function createWarnCapturingLogger(warn: ReturnType<typeof vi.fn>): Logger {
- const logger = {
- debug: vi.fn(),
- info: vi.fn(),
- warn,
- error: vi.fn(),
- child: () => logger,
- };
- return logger as unknown as Logger;
- }
- /** 加速 provider 上的请求按卡过期(3402)失败,其他 provider 正常完成。 */
- function createCardExpiryModelFactory(
- highspeedProviderId: string,
- requests: Array<{ modelId: string; providerId: string }>,
- ) {
- return createTestModelFactory({
- async generateText(_request, observation) {
- requests.push({
- modelId: String(observation.model.modelId),
- providerId: String(observation.model.providerId),
- });
- if (String(observation.model.providerId) === highspeedProviderId) {
- throw Object.assign(new Error("highspeed card is invalid"), {
- code: "invalid_model_request",
- context: {
- providerCode: "3402",
- providerId: highspeedProviderId,
- modelId: "GLM-5.3",
- source: "provider",
- statusCode: 400,
- },
- name: "AiSdkModelAdapterError",
- });
- }
- return {
- finishReason: "stop",
- model: observation.model,
- text: "continued with the original model",
- usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
- };
- },
- });
- }
- const cardExpiryFallbackRules = [
- { reason: "highspeed_card_expired" as const, providerErrorCode: "3402" },
- { reason: "highspeed_request_failed" as const },
- ];
- describe("AgentRuntime model selection event", () => {
- it("首轮只按 admitted Active Model 构造一次 Context", async () => {
- const runtime = createTestAgentRuntime(
- createSessionId("runtime-first-turn-single-context-build"),
- {
- modelSelection: createTestModelSelection("provider-a/model-a"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(_request, observation) {
- return {
- finishReason: "stop",
- model: observation.model,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- }),
- },
- );
- const internalRuntime = runtime as typeof runtime & {
- createContextBuilderFromSnapshot: (...args: unknown[]) => unknown;
- };
- const createContextBuilderFromSnapshot =
- internalRuntime.createContextBuilderFromSnapshot.bind(internalRuntime);
- let contextBuildCount = 0;
- internalRuntime.createContextBuilderFromSnapshot = (...args: unknown[]) => {
- contextBuildCount++;
- return createContextBuilderFromSnapshot(...args);
- };
- await runtime.executeTurn("first turn");
- expect(contextBuildCount).toBe(1);
- await runtime.executeTurn("second turn");
- expect(contextBuildCount).toBe(2);
- });
- it("uses the admitted Active Model for both Context and the provider request", async () => {
- const requests: Array<{ modelId: string; providerId: string; text: string }> = [];
- const runtime = createTestAgentRuntime(
- createSessionId("runtime-active-model-context"),
- {
- modelSelection: createTestModelSelection("provider-a/model-a"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request, observation) {
- requests.push({
- modelId: String(observation.model.modelId),
- providerId: String(observation.model.providerId),
- text: JSON.stringify(request.messages),
- });
- return {
- finishReason: "stop",
- model: observation.model,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- }),
- },
- );
- await runtime.executeTurn("switch now", undefined, {
- intent: {
- kind: "sendText",
- modelSelection: {
- providerId: "provider-b",
- modelId: "model-b",
- options: { reasoningLevel: "high" },
- },
- requestedDelivery: "start-now",
- sourceCommandId: "switch-command",
- },
- });
- expect(requests).toHaveLength(1);
- expect(requests[0]).toMatchObject({ providerId: "provider-b", modelId: "model-b" });
- expect(requests[0]?.text).toContain("provider-b/model-b");
- expect(requests[0]?.text).not.toContain("provider-a/model-a");
- });
- it("uses an execution-scoped Active Model in Context without changing Session Selection", async () => {
- const requests: Array<{ modelId: string; providerId: string; text: string }> = [];
- const sessionSelection = createTestModelSelection("provider-a/model-a");
- const runtime = createTestAgentRuntime(
- createSessionId("runtime-execution-model-context"),
- {
- modelSelection: sessionSelection,
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request, observation) {
- requests.push({
- modelId: String(observation.model.modelId),
- providerId: String(observation.model.providerId),
- text: JSON.stringify(request.messages),
- });
- return {
- finishReason: "stop",
- model: observation.model,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- }),
- },
- );
- await runtime.executeTurn("idle task", undefined, {
- intent: {
- kind: "sendText",
- modelSelection: {
- providerId: "account:zai-offpeak-idle-plan",
- modelId: "GLM-5.2",
- options: { reasoningLevel: "high" },
- },
- requestedDelivery: "start-now",
- sourceCommandId: "idle-command",
- },
- modelExecution: { selectionScope: "execution" },
- });
- expect(requests).toHaveLength(1);
- expect(requests[0]).toMatchObject({
- providerId: "account:zai-offpeak-idle-plan",
- modelId: "GLM-5.2",
- });
- expect(requests[0]?.text).toContain("account:zai-offpeak-idle-plan/GLM-5.2");
- expect(requests[0]?.text).not.toContain("provider-a/model-a");
- expect(runtime.getSessionModelSelection()).toEqual(sessionSelection);
- });
- it("applies a Session Selection change during an active turn only to the next Active Model", async () => {
- const requests: Array<{ modelId: string; providerId: string; text: string }> = [];
- let runtime: ReturnType<typeof createTestAgentRuntime>;
- runtime = createTestAgentRuntime(
- createSessionId("runtime-active-turn-selection-change"),
- {
- modelSelection: createTestModelSelection("provider-a/model-a"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request, observation) {
- requests.push({
- modelId: String(observation.model.modelId),
- providerId: String(observation.model.providerId),
- text: JSON.stringify(request.messages),
- });
- if (requests.length === 1) {
- runtime.setSessionModelSelection({
- providerId: "provider-b",
- modelId: "model-b",
- options: { reasoningLevel: "high" },
- });
- }
- return {
- finishReason: "stop",
- model: observation.model,
- text: "done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- }),
- },
- );
- await runtime.executeTurn("first");
- await runtime.executeTurn("second");
- expect(requests).toHaveLength(2);
- expect(requests[0]).toMatchObject({ providerId: "provider-a", modelId: "model-a" });
- expect(requests[0]?.text).toContain("provider-a/model-a");
- expect(requests[0]?.text).not.toContain("provider-b/model-b");
- expect(requests[1]).toMatchObject({ providerId: "provider-b", modelId: "model-b" });
- expect(requests[1]?.text).toContain("provider-b/model-b");
- expect(requests[1]?.text).toContain("provider-a/model-a");
- expect(requests[1]?.text.indexOf("provider-a/model-a")).toBeLessThan(
- requests[1]?.text.indexOf("provider-b/model-b") ?? -1,
- );
- });
- it("uses structured ModelSelection as the current Session model fact", () => {
- const runtime = createTestAgentRuntime(
- createSessionId("runtime-structured-session-model-selection"),
- {
- modelSelection: {
- providerId: "provider-a",
- modelId: "model-a",
- options: {
- reasoningLevel: "high",
- },
- },
- },
- { eventStore: createTestSessionEventStore() },
- );
- expect(runtime.getSessionModelSelection()).toEqual({
- providerId: "provider-a",
- modelId: "model-a",
- options: {
- reasoningLevel: "high",
- },
- });
- const nextSelection = {
- providerId: "provider-b",
- modelId: "model-b",
- options: {
- reasoningLevel: "low",
- },
- };
- runtime.setSessionModelSelection(nextSelection);
- nextSelection.options.reasoningLevel = "mutated-outside-runtime";
- expect(runtime.getSessionModelSelection()).toEqual({
- providerId: "provider-b",
- modelId: "model-b",
- options: {
- reasoningLevel: "low",
- },
- });
- });
- it("publishes the Active Model context window with ModelSelected", async () => {
- const sessionId = createSessionId("runtime-model-selection-context-window");
- const eventStore = createTestSessionEventStore();
- const runtime = createTestAgentRuntime(
- sessionId,
- {},
- {
- eventStore,
- modelFactory: createTestModelFactory({ properties: { contextWindow: 1_000_000 } }),
- },
- );
- await runtime.emitModelSelected({
- modelSelection: {
- providerId: createModelProviderId("deepseek"),
- modelId: createModelId("deepseek-v4-flash"),
- },
- supportedThoughtLevels: ["max", "high", "nothink"],
- traceContext: { traceId: "trace-runtime-model-selection" } as never,
- });
- const selected = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.ModelSelected,
- );
- expect(selected?.payload).toMatchObject({
- contextWindow: 1_000_000,
- modelSelection: {
- providerId: "deepseek",
- modelId: "deepseek-v4-flash",
- },
- supportedThoughtLevels: ["max", "high", "nothink"],
- });
- });
- it("preserves the registry fallback origin in ModelSelected", async () => {
- const sessionId = createSessionId("runtime-model-selection-registry-fallback");
- const eventStore = createTestSessionEventStore();
- const runtime = createTestAgentRuntime(sessionId, {}, { eventStore });
- await runtime.emitModelSelected({
- modelSelection: {
- providerId: createModelProviderId("provider-b"),
- modelId: createModelId("model-b"),
- },
- previousModelSelection: {
- providerId: createModelProviderId("provider-a"),
- modelId: createModelId("model-a"),
- },
- origin: "registryFallback",
- traceContext: { traceId: "trace-runtime-model-selection-fallback" } as never,
- });
- const selected = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.ModelSelected,
- );
- expect(selected?.payload).toMatchObject({
- origin: "registryFallback",
- previousModelSelection: {
- providerId: "provider-a",
- modelId: "model-a",
- },
- });
- });
- it("uses the execution-scoped Selection for foreground children without applying profile reasoning", async () => {
- const sessionId = createSessionId("runtime-turn-model-foreground-subagents");
- const eventStore = createTestSessionEventStore();
- const sessionModel = createTestModelSelection("user-provider/user-model");
- const childExecutions: Array<Record<string, unknown>> = [];
- const mainRequests: Array<Record<string, unknown>> = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection(sessionModel),
- subagents: {
- profiles: [
- {
- description: "Search source code",
- modelSelection: {
- providerId: "custom-provider",
- modelId: "custom-code-model",
- options: { reasoningLevel: "high" },
- },
- name: "Code Search",
- source: "user",
- systemPrompt: "Search code.",
- tools: ["Read"],
- },
- ],
- },
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- maxOutputTokens: (input) =>
- input.selection.providerId === "account:zai-offpeak-idle-plan" ? 16_000 : 32_000,
- async generateText(request, observation) {
- const metadata = observation.invocationContext?.metadata as
- | { querySource?: string; turnNumber?: number }
- | undefined;
- const capturedRequest = {
- maxOutputTokens: request.options?.maxOutputTokens,
- model: observation.model,
- };
- if (metadata?.querySource === "subagent") {
- childExecutions.push(capturedRequest);
- return {
- finishReason: "stop",
- model: observation.model,
- text: "child done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- mainRequests.push(capturedRequest);
- if (metadata?.turnNumber === 0 && mainRequests.length === 1) {
- return {
- finishReason: "tool-calls",
- model: observation.model,
- text: "",
- toolCalls: [
- {
- id: "call_explore",
- name: "Agent",
- input: {
- description: "Explore source",
- prompt: "Explore the source.",
- subagent_type: "Explore",
- },
- },
- {
- id: "call_code_search",
- name: "Agent",
- input: {
- description: "Search source",
- prompt: "Search the source.",
- subagent_type: "Code Search",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- if (metadata?.turnNumber === 1 && mainRequests.length === 3) {
- return {
- finishReason: "tool-calls",
- model: observation.model,
- text: "",
- toolCalls: [
- {
- id: "call_code_search_ordinary",
- name: "Agent",
- input: {
- description: "Search source with profile settings",
- prompt: "Search the source with the configured profile.",
- subagent_type: "Code Search",
- },
- },
- ],
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- }
- return {
- finishReason: "stop",
- model: observation.model,
- text: "turn done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- await runtime.executeTurn("run foreground children", undefined, {
- intent: {
- sourceCommandId: "idle-command",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: {
- providerId: "account:zai-offpeak-idle-plan",
- modelId: "GLM-5.2",
- options: { reasoningLevel: "high" },
- },
- },
- modelExecution: {
- selectionScope: "execution",
- subagents: { foregroundModel: "submission", background: "deny" },
- },
- });
- await runtime.executeTurn("ordinary prompt");
- expect(childExecutions).toHaveLength(3);
- expect(childExecutions.slice(0, 2).map((execution) => execution.model)).toEqual([
- expect.objectContaining({
- providerId: "account:zai-offpeak-idle-plan",
- modelId: "GLM-5.2",
- }),
- expect.objectContaining({
- providerId: "account:zai-offpeak-idle-plan",
- modelId: "GLM-5.2",
- }),
- ]);
- expect(
- childExecutions
- .slice(0, 2)
- .map(
- (execution) =>
- (execution.model as { options?: { reasoningLevel?: string } }).options?.reasoningLevel,
- ),
- ).toEqual(["high", "high"]);
- expect(childExecutions.slice(0, 2).map((execution) => execution.maxOutputTokens)).toEqual([
- 16_000, 16_000,
- ]);
- expect(childExecutions[2]?.model).toEqual(
- expect.objectContaining({
- providerId: "custom-provider",
- modelId: "custom-code-model",
- }),
- );
- expect(childExecutions[2]?.model).toEqual(
- expect.objectContaining({ options: expect.objectContaining({ reasoningLevel: "high" }) }),
- );
- expect(mainRequests).toHaveLength(4);
- expect(mainRequests.at(-1)?.model).toEqual(
- expect.objectContaining({
- providerId: "user-provider",
- modelId: "user-model",
- }),
- );
- expect(runtime.getSessionModelSelection()).toEqual({
- providerId: String(sessionModel.providerId),
- modelId: String(sessionModel.modelId),
- });
- });
- it.each(["failure", "cancel"] as const)(
- "does not retain an execution-scoped Selection after %s",
- async (outcome) => {
- const sessionId = createSessionId(`runtime-turn-model-cleanup-${outcome}`);
- const abortController = new AbortController();
- const requests: Array<Record<string, unknown>> = [];
- let failFirstRequest = true;
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection(
- createTestModelSelection("user-provider/user-model"),
- ),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- async generateText(request, observation) {
- requests.push({
- model: observation.model,
- request,
- });
- if (failFirstRequest) {
- failFirstRequest = false;
- if (outcome === "cancel") {
- abortController.abort(new Error("user cancelled idle turn"));
- }
- throw new Error(`${outcome} idle turn`);
- }
- return {
- finishReason: "stop",
- model: observation.model,
- text: "ordinary turn done",
- usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
- };
- },
- } as never),
- },
- );
- await expect(
- runtime.executeTurn("idle turn", undefined, {
- abortSignal: abortController.signal,
- intent: {
- sourceCommandId: `idle-${outcome}`,
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: {
- providerId: "account:zai-offpeak-idle-plan",
- modelId: "GLM-5.2",
- },
- },
- modelExecution: {
- selectionScope: "execution",
- },
- }),
- ).rejects.toMatchObject({
- type: outcome === "cancel" ? CoreErrorType.TurnCancelled : CoreErrorType.UnknownError,
- });
- await runtime.executeTurn("ordinary prompt");
- expect(requests).toHaveLength(2);
- expect(requests[0]?.model).toEqual(
- expect.objectContaining({
- providerId: "account:zai-offpeak-idle-plan",
- modelId: "GLM-5.2",
- }),
- );
- expect(requests[1]?.model).toEqual(
- expect.objectContaining({ providerId: "user-provider", modelId: "user-model" }),
- );
- },
- );
- it.each([ModelErrorCode.ProviderNotFound, ModelErrorCode.ModelNotFound])(
- "falls back before the request when execution Selection creation fails with %s",
- async (code) => {
- const sessionId = createSessionId(`runtime-execution-selection-creation-${code}`);
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:bigmodel-highspeed-card";
- const created: Array<{ providerId: string; modelId: string }> = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- onCreate(input) {
- created.push(input.selection);
- if (input.selection.providerId === highspeedProviderId) {
- throw new ModelProtocolError(code, "Highspeed selection is unavailable");
- }
- },
- async generateText(_request, observation) {
- return {
- finishReason: "stop",
- model: observation.model,
- text: "continued with the original model",
- usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
- };
- },
- }),
- },
- );
- const result = await runtime.executeTurn("continue before request", undefined, {
- inputId: `command-creation-${code}`,
- intent: {
- sourceCommandId: `command-creation-${code}`,
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: cardExpiryFallbackRules,
- target: { providerId: "user-provider", modelId: "user-model" },
- },
- },
- });
- expect(result.response).toBe("continued with the original model");
- expect(created).toEqual([
- { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- { providerId: "user-provider", modelId: "user-model" },
- ]);
- const fallbackEvents = (await eventStore.getEvents(sessionId)).filter(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvents).toHaveLength(1);
- expect(fallbackEvents[0]?.payload).toMatchObject({
- inputId: `command-creation-${code}`,
- reason: "highspeed_selection_unavailable",
- fromModelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- toModelSelection: { providerId: "user-provider", modelId: "user-model" },
- });
- },
- );
- it("reports the ordinary model creation error when Highspeed creation and fallback both fail", async () => {
- const sessionId = createSessionId("runtime-execution-selection-creation-fallback-fails");
- const eventStore = createTestSessionEventStore();
- const sessionStore = createRecordingSubagentSessionStore();
- const highspeedProviderId = "account:bigmodel-highspeed-card";
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- sessionStore,
- modelFactory: createTestModelFactory({
- onCreate(input) {
- if (input.selection.providerId === highspeedProviderId) {
- throw new ModelProtocolError(
- ModelErrorCode.ProviderNotFound,
- "Highspeed selection is unavailable",
- );
- }
- throw new ModelProtocolError(
- ModelErrorCode.ProviderNotFound,
- "Original model selection is unavailable",
- );
- },
- }),
- },
- );
- await expect(
- runtime.executeTurn("continue before request", undefined, {
- inputId: "command-creation-fallback-fails",
- intent: {
- sourceCommandId: "command-creation-fallback-fails",
- queueItemId: "input-creation-fallback-fails",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- highspeed: {
- schemaVersion: 1,
- cardId: "card-creation-fallback-fails",
- taskId: String(sessionId),
- provider: "builtin:zai-coding-plan",
- model: "GLM-5.3",
- issuedAt: 1_700_000_000_000,
- expiresAt: 1_700_000_060_000,
- },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: cardExpiryFallbackRules,
- target: { providerId: "user-provider", modelId: "user-model" },
- },
- },
- }),
- ).rejects.toMatchObject({
- message: "Model creation failed",
- cause: expect.objectContaining({ message: "Original model selection is unavailable" }),
- });
- const events = await eventStore.getEvents(sessionId);
- const turnStartedIndex = events.findIndex(
- (event) => event.type === SessionEventType.TurnStarted,
- );
- const turnErrorIndex = events.findIndex((event) => event.type === SessionEventType.TurnError);
- expect(turnStartedIndex).toBeGreaterThanOrEqual(0);
- expect(turnErrorIndex).toBeGreaterThan(turnStartedIndex);
- expect(events[turnStartedIndex]?.payload).toMatchObject({
- input: "continue before request",
- inputId: "command-creation-fallback-fails",
- });
- const [persistedUser] = await sessionStore.messages({ sessionID: sessionId });
- expect(persistedUser).toMatchObject({
- info: {
- role: "user",
- anchor: { sourceCommandId: "command-creation-fallback-fails" },
- metadata: {
- inputIntent: {
- queueItemId: "input-creation-fallback-fails",
- sourceCommandId: "command-creation-fallback-fails",
- },
- },
- },
- parts: [{ type: "text", text: "continue before request" }],
- });
- expect(
- events.filter((event) => event.type === SessionEventType.TurnExecutionModelFallback),
- ).toHaveLength(0);
- expect(
- events.find((event) => event.type === SessionEventType.TurnError)?.payload,
- ).toMatchObject({
- error: {
- message: "Original model selection is unavailable",
- // 创建期所有候选都失败时同样没有 fallback 事件;Renderer 按该事实作废匹配卡。
- attribution: {
- highspeedCardId: "card-creation-fallback-fails",
- highspeedFallbackUnavailable: true,
- },
- },
- });
- });
- it("falls back to the Session Selection inside the same turn when the declared provider error hits", async () => {
- const sessionId = createSessionId("runtime-execution-selection-fallback");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:zai-highspeed-card";
- const requests: Array<{ modelId: string; providerId: string }> = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(_request, observation) {
- requests.push({
- modelId: String(observation.model.modelId),
- providerId: String(observation.model.providerId),
- });
- if (String(observation.model.providerId) === highspeedProviderId) {
- throw Object.assign(new Error("highspeed card is invalid"), {
- code: "invalid_model_request",
- context: {
- providerCode: "3402",
- providerId: highspeedProviderId,
- modelId: "GLM-5.3",
- source: "provider",
- },
- name: "AiSdkModelAdapterError",
- });
- }
- return {
- finishReason: "stop",
- model: observation.model,
- text: "continued with the session model",
- usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
- };
- },
- }),
- },
- );
- const result = await runtime.executeTurn("continue after expiry", undefined, {
- inputId: "command-highspeed-expired",
- intent: {
- sourceCommandId: "command-highspeed-expired",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: [
- { reason: "highspeed_card_expired", providerErrorCode: "3402" },
- { reason: "highspeed_request_failed" },
- ],
- },
- },
- });
- expect(result.response).toBe("continued with the session model");
- expect(requests).toEqual([
- { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- { providerId: "user-provider", modelId: "user-model" },
- ]);
- const fallbackEvent = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvent?.payload).toMatchObject({
- inputId: "command-highspeed-expired",
- reason: "highspeed_card_expired",
- fromModelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- toModelSelection: { providerId: "user-provider", modelId: "user-model" },
- });
- // execution 作用域从不改写 Session Selection:退回目标就是它自己。
- expect(runtime.getSessionModelSelection()).toMatchObject({
- providerId: "user-provider",
- modelId: "user-model",
- });
- });
- it("rebuilds the context prefix with the fallback model's Harness wording", async () => {
- const sessionId = createSessionId("runtime-execution-selection-fallback-harness");
- const highspeedProviderId = "account:zai-highspeed-card";
- const requests: Array<{ providerId: string; messages: string }> = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore: createTestSessionEventStore(),
- modelFactory: createTestModelFactory({
- // 执行模型支持 mid-conversation system,退回的会话模型不支持。
- properties: (input) => ({
- supportsMidConversationSystem: input.selection.providerId === highspeedProviderId,
- }),
- async generateText(request, observation) {
- const providerId = String(observation.model.providerId);
- requests.push({ providerId, messages: JSON.stringify(request.messages) });
- if (providerId === highspeedProviderId) {
- throw Object.assign(new Error("highspeed card is invalid"), {
- code: "invalid_model_request",
- context: {
- providerCode: "3402",
- providerId: highspeedProviderId,
- modelId: "GLM-5.3",
- source: "provider",
- },
- name: "AiSdkModelAdapterError",
- });
- }
- return {
- finishReason: "stop",
- model: observation.model,
- text: "continued with the session model",
- usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
- };
- },
- }),
- },
- );
- await runtime.executeTurn("continue after expiry", undefined, {
- inputId: "command-highspeed-harness",
- intent: {
- sourceCommandId: "command-highspeed-harness",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: [{ reason: "highspeed_card_expired", providerErrorCode: "3402" }],
- },
- },
- });
- expect(requests.map((request) => request.providerId)).toEqual([
- highspeedProviderId,
- "user-provider",
- ]);
- expect(requests[0]?.messages).toContain("mid-conversation system turns");
- // 退回请求按退回模型投影:system 不能继续声称 reminder 走 mid-conversation system。
- expect(requests[1]?.messages).toContain(
- "tags in messages and tool results are injected by the harness",
- );
- expect(requests[1]?.messages).not.toContain("mid-conversation system turns");
- });
- it("falls back to the Session Selection on any execution provider failure with a single-attempt budget", async () => {
- // docs/highspeed/highspeed-card-spec.md §2.2:加速请求任何失败(这里是网关 401,没有业务码)都退回;
- // 加速句柄 0 次重试(single-attempt),退回后的会话模型恢复默认预算。
- const sessionId = createSessionId("runtime-execution-selection-any-failure-fallback");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:zai-highspeed-card";
- const requests: Array<{ modelId: string; providerId: string; retryBudget: unknown }> = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(_request, observation) {
- requests.push({
- modelId: String(observation.model.modelId),
- providerId: String(observation.model.providerId),
- retryBudget: observation.invocationContext?.modelRetryBudget,
- });
- if (String(observation.model.providerId) === highspeedProviderId) {
- throw Object.assign(new Error("Provider authentication failed."), {
- code: "provider_not_configured",
- context: {
- providerId: highspeedProviderId,
- modelId: "GLM-5.3",
- reason: "auth_failed",
- retryable: false,
- source: "provider",
- statusCode: 401,
- },
- name: "AiSdkModelAdapterError",
- });
- }
- return {
- finishReason: "stop",
- model: observation.model,
- text: "continued with the session model",
- usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
- };
- },
- }),
- },
- );
- const result = await runtime.executeTurn("continue after gateway failure", undefined, {
- inputId: "command-highspeed-401",
- intent: {
- sourceCommandId: "command-highspeed-401",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: [
- { reason: "highspeed_card_expired", providerErrorCode: "3402" },
- { reason: "highspeed_request_failed" },
- ],
- },
- },
- });
- expect(result.response).toBe("continued with the session model");
- expect(requests).toEqual([
- {
- providerId: highspeedProviderId,
- modelId: "GLM-5.3",
- retryBudget: ModelRetryBudget.SingleAttempt,
- },
- { providerId: "user-provider", modelId: "user-model", retryBudget: ModelRetryBudget.Default },
- ]);
- const fallbackEvent = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvent?.payload).toMatchObject({
- inputId: "command-highspeed-401",
- reason: "highspeed_request_failed",
- fromModelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- toModelSelection: { providerId: "user-provider", modelId: "user-model" },
- });
- });
- it("falls back to the declared target when the runtime has no resident Session Selection", async () => {
- // 回归(2026-10-09 日志复盘):冷恢复落在 Registry 刚就绪、账号权益未解析的窗口时,持久化的会话选择
- // 校验失败不绑定;之后只发加速轮不会再绑定,3402 时退回目标为空,整轮按 "highspeed card is invalid"
- // 失败弹错。发起方抽卡时知道原模型,现在随 selectionFallback.target 下发,退回不再依赖 runtime 常驻选择。
- const sessionId = createSessionId("runtime-execution-selection-fallback-declared-target");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:bigmodel-highspeed-card";
- const target = { providerId: "account:bigmodel-team-coding-plan", modelId: "GLM-5.3" };
- const requests: Array<{ modelId: string; providerId: string }> = [];
- const warn = vi.fn();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- logger: createWarnCapturingLogger(warn),
- modelFactory: createCardExpiryModelFactory(highspeedProviderId, requests),
- },
- );
- // 复现恢复竞态之后的 runtime:没有常驻选择。
- runtime.setSessionModelSelection(undefined);
- const result = await runtime.executeTurn("continue after expiry", undefined, {
- inputId: "command-highspeed-expired-unbound",
- intent: {
- sourceCommandId: "command-highspeed-expired-unbound",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: cardExpiryFallbackRules,
- target,
- },
- },
- });
- expect(result.response).toBe("continued with the original model");
- expect(requests).toEqual([{ providerId: highspeedProviderId, modelId: "GLM-5.3" }, target]);
- const fallbackEvent = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvent?.payload).toMatchObject({
- inputId: "command-highspeed-expired-unbound",
- reason: "highspeed_card_expired",
- fromModelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- toModelSelection: target,
- });
- // 接纳用户消息时保存其普通选择;执行和回退不会把加速端点写入 Session。
- expect(runtime.getSessionModelSelection()).toEqual(target);
- expect(warn).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- event: "model.execution_selection.fallback",
- toSource: "declared_target",
- toProviderId: target.providerId,
- providerErrorCode: "3402",
- }),
- );
- });
- it("prefers the declared target over the resident Session Selection", async () => {
- // target 是发起方抽卡时 Composer 的提交选择,比 runtime 内存里的常驻选择更接近用户此刻的意图;
- // 正常情况下两者相同,不同则以发起方为准。
- const sessionId = createSessionId("runtime-execution-selection-fallback-target-first");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:bigmodel-highspeed-card";
- const target = { providerId: "account:bigmodel-team-coding-plan", modelId: "GLM-5.3" };
- const requests: Array<{ modelId: string; providerId: string }> = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- { eventStore, modelFactory: createCardExpiryModelFactory(highspeedProviderId, requests) },
- );
- await runtime.executeTurn("continue after expiry", undefined, {
- intent: {
- sourceCommandId: "command-highspeed-expired-target-first",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: cardExpiryFallbackRules,
- target,
- },
- },
- });
- expect(requests).toEqual([{ providerId: highspeedProviderId, modelId: "GLM-5.3" }, target]);
- expect(runtime.getSessionModelSelection()).toEqual(target);
- });
- it("skips a declared target the model factory cannot resolve and falls back to the Session Selection", async () => {
- const sessionId = createSessionId("runtime-execution-selection-fallback-target-unresolvable");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:bigmodel-highspeed-card";
- const requests: Array<{ modelId: string; providerId: string }> = [];
- const warn = vi.fn();
- const baseFactory = createCardExpiryModelFactory(highspeedProviderId, requests);
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- logger: createWarnCapturingLogger(warn),
- modelFactory: (factoryInput) => {
- // 模拟目标 provider 不在当前 Registry:建模抛错只能跳过该候选,不能替换真实的加速失败原因。
- if (factoryInput.selection.providerId === "ghost-provider") {
- throw new Error("Provider Registry 中不存在 Provider: ghost-provider");
- }
- return baseFactory(factoryInput);
- },
- },
- );
- const result = await runtime.executeTurn("continue after expiry", undefined, {
- intent: {
- sourceCommandId: "command-highspeed-expired-ghost-target",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: cardExpiryFallbackRules,
- target: { providerId: "ghost-provider", modelId: "GLM-5.3" },
- },
- },
- });
- expect(result.response).toBe("continued with the original model");
- expect(requests).toEqual([
- { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- { providerId: "user-provider", modelId: "user-model" },
- ]);
- expect(warn).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- event: "model.execution_selection.fallback",
- toSource: "session_selection",
- skippedCandidates: [
- expect.objectContaining({ source: "declared_target", providerId: "ghost-provider" }),
- ],
- }),
- );
- });
- it("fails with the real execution error when neither a declared target nor a resident Session Selection resolves", async () => {
- // 无处可退时不能用建模错误或泛化文案掩埋根因,但必须留痕:过去这个分支完全静默。
- const sessionId = createSessionId("runtime-execution-selection-fallback-unavailable");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:bigmodel-highspeed-card";
- const requests: Array<{ modelId: string; providerId: string }> = [];
- const warn = vi.fn();
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- logger: createWarnCapturingLogger(warn),
- modelFactory: createCardExpiryModelFactory(highspeedProviderId, requests),
- },
- );
- runtime.setSessionModelSelection(undefined);
- const failure = (await runtime
- .executeTurn("continue after expiry", undefined, {
- intent: {
- sourceCommandId: "command-highspeed-expired-nowhere",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- highspeed: {
- schemaVersion: 1,
- cardId: "card-request-fallback-unavailable",
- taskId: String(sessionId),
- provider: "builtin:bigmodel-coding-plan",
- model: "GLM-5.3",
- issuedAt: 1_700_000_000_000,
- expiresAt: 1_700_000_060_000,
- },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: { providerId: highspeedProviderId, rules: cardExpiryFallbackRules },
- },
- })
- .then(
- () => undefined,
- (error: unknown) => error,
- )) as (Error & { cause?: unknown }) | undefined;
- expect(failure).toBeInstanceOf(Error);
- const causeMessage = failure?.cause instanceof Error ? failure.cause.message : undefined;
- expect([failure?.message, causeMessage]).toContain("highspeed card is invalid");
- expect(requests).toEqual([{ providerId: highspeedProviderId, modelId: "GLM-5.3" }]);
- const events = await eventStore.getEvents(sessionId);
- const fallbackEvents = events.filter(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvents).toHaveLength(0);
- // 无处可退时没有 fallback 事件,Renderer 只能靠 TurnError 归因退卡;3402 是 provider 错误码,
- // 不在目录错误白名单里,所以 Core 必须显式发布“已归因且无处可退”的事实(spec §2.2)。
- const turnError = events.find((event) => event.type === SessionEventType.TurnError);
- expect(turnError?.payload).toMatchObject({
- error: {
- code: "3402",
- attribution: {
- source: "provider",
- providerErrorCode: "3402",
- highspeedCardId: "card-request-fallback-unavailable",
- highspeedFallbackUnavailable: true,
- },
- },
- });
- expect(warn).toHaveBeenCalledWith(
- expect.any(String),
- expect.objectContaining({
- event: "model.execution_selection.fallback_unavailable",
- hasDeclaredTarget: false,
- hasSessionSelection: false,
- providerErrorCode: "3402",
- }),
- );
- });
- it("does not fall back when local streaming event persistence fails", async () => {
- // Bug 根因:selectionFallback 的无错误码规则曾把 stream 消费期间的本地落库异常也当成 provider 失败,
- // 不仅掩埋真实存储故障,还会错误地向原模型再发一次请求。
- const sessionId = createSessionId("runtime-execution-selection-local-persistence-failure");
- const eventStore = createTestSessionEventStore();
- const appendEvent = eventStore.append.bind(eventStore);
- eventStore.append = vi.fn(async (event) => {
- if (event.type === SessionEventType.ModelStreaming) {
- throw new Error("event store append failed");
- }
- return appendEvent(event);
- });
- const highspeedProviderId = "account:zai-highspeed-card";
- const streamedProviders: string[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- modelStreaming: "on",
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async *streamText(_request, observation) {
- streamedProviders.push(String(observation.model.providerId));
- yield { id: "text-1", type: "text_start" };
- yield { id: "text-1", text: "provider output", type: "text_delta" };
- yield { id: "text-1", type: "text_end" };
- yield {
- finishReason: "stop",
- type: "finish",
- usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
- };
- },
- }),
- },
- );
- const failure = (await runtime
- .executeTurn("persist this stream", undefined, {
- inputId: "command-highspeed-local-persistence-failure",
- intent: {
- sourceCommandId: "command-highspeed-local-persistence-failure",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: [{ reason: "highspeed_request_failed" }],
- },
- },
- })
- .then(
- () => undefined,
- (error: unknown) => error,
- )) as (Error & { cause?: unknown }) | undefined;
- expect(failure).toBeInstanceOf(Error);
- const causeMessage = failure?.cause instanceof Error ? failure.cause.message : undefined;
- expect([failure?.message, causeMessage]).toContain("event store append failed");
- expect(streamedProviders).toEqual([highspeedProviderId]);
- const fallbackEvents = (await eventStore.getEvents(sessionId)).filter(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvents).toHaveLength(0);
- });
- it("does not retry the accelerated stream when a matched fallback has no resolvable target", async () => {
- // 无处可退已是终止边界;若继续进入通用 stream recovery,会在同一个失败加速端点重复请求。
- const sessionId = createSessionId("runtime-execution-selection-stream-fallback-unavailable");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:zai-highspeed-card";
- const streamedProviders: string[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- modelStreaming: "on",
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async *streamText(_request, observation) {
- const providerId = String(observation.model.providerId);
- streamedProviders.push(providerId);
- yield { id: "text-1", type: "text_start" };
- yield { id: "text-1", text: "partial accelerated output", type: "text_delta" };
- throw Object.assign(new Error("accelerated stream failed"), {
- code: "model_request_failed",
- context: {
- providerId,
- modelId: "GLM-5.3",
- reason: "network_error",
- retryable: true,
- source: "network",
- },
- name: "AiSdkModelAdapterError",
- });
- },
- }),
- },
- );
- runtime.setSessionModelSelection(undefined);
- const failure = (await runtime
- .executeTurn("stream without fallback target", undefined, {
- inputId: "command-highspeed-stream-no-target",
- intent: {
- sourceCommandId: "command-highspeed-stream-no-target",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: [{ reason: "highspeed_request_failed" }],
- },
- },
- })
- .then(
- () => undefined,
- (error: unknown) => error,
- )) as (Error & { cause?: unknown }) | undefined;
- expect(failure).toBeInstanceOf(Error);
- const causeMessage = failure?.cause instanceof Error ? failure.cause.message : undefined;
- expect([failure?.message, causeMessage]).toContain("accelerated stream failed");
- expect(streamedProviders).toEqual([highspeedProviderId]);
- const fallbackEvents = (await eventStore.getEvents(sessionId)).filter(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvents).toHaveLength(0);
- });
- it("falls back only once: a session model failure after the fallback fails the turn", async () => {
- const sessionId = createSessionId("runtime-execution-selection-fallback-once");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:zai-highspeed-card";
- const providers: string[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(_request, observation) {
- const providerId = String(observation.model.providerId);
- providers.push(providerId);
- throw Object.assign(new Error("Provider returned a server error."), {
- code: "model_request_failed",
- context: {
- providerId,
- modelId: String(observation.model.modelId),
- reason: "server_error",
- retryable: true,
- source: "provider",
- statusCode: 503,
- },
- name: "AiSdkModelAdapterError",
- });
- },
- }),
- },
- );
- await expect(
- runtime.executeTurn("both models fail", undefined, {
- inputId: "command-highspeed-503",
- intent: {
- sourceCommandId: "command-highspeed-503",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: [{ reason: "highspeed_request_failed" }],
- },
- },
- }),
- ).rejects.toThrow();
- // 加速模型失败一次即退回;退回后的会话模型再失败按真实错误整轮失败,不再二次降级。
- expect(providers).toEqual([highspeedProviderId, "user-provider"]);
- const events = await eventStore.getEvents(sessionId);
- const fallbackEvents = events.filter(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvents).toHaveLength(1);
- // 退回已经发生,卡由 fallback 事件作废;终态错误属于会话模型,不得再标成“无处可退”。
- const turnError = events.find((event) => event.type === SessionEventType.TurnError);
- expect(turnError?.payload).toMatchObject({
- error: { attribution: { providerId: "user-provider" } },
- });
- expect(
- (turnError?.payload as { error: { attribution: Record<string, unknown> } }).error.attribution,
- ).not.toHaveProperty("highspeedFallbackUnavailable");
- });
- it("falls back before same-model stream recovery when the execution stream fails after visible output", async () => {
- // 退回必须先于 stream recovery:否则可见输出后的瞬态失败会先在加速模型上重试到预算耗尽。
- const sessionId = createSessionId("runtime-execution-selection-stream-fallback");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:zai-highspeed-card";
- const streamedProviders: string[] = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection("user-provider/user-model"),
- modelStreaming: "on",
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText() {
- throw new Error("streaming runtime must not call generateText");
- },
- async *streamText(_request, observation) {
- const providerId = String(observation.model.providerId);
- streamedProviders.push(providerId);
- yield { id: "text-1", type: "text_start" };
- if (providerId === highspeedProviderId) {
- yield { id: "text-1", text: "partial accelerated output", type: "text_delta" };
- throw Object.assign(
- new Error("Network connection failed for the provider request."),
- {
- code: "model_request_failed",
- context: {
- providerId,
- modelId: "GLM-5.3",
- reason: "network_error",
- retryable: true,
- source: "network",
- },
- name: "AiSdkModelAdapterError",
- },
- );
- }
- yield { id: "text-1", text: "continued with the session model", type: "text_delta" };
- yield { id: "text-1", type: "text_end" };
- yield {
- finishReason: "stop",
- type: "finish",
- usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
- };
- },
- }),
- },
- );
- const result = await runtime.executeTurn("stream fails mid-way", undefined, {
- inputId: "command-highspeed-stream",
- intent: {
- sourceCommandId: "command-highspeed-stream",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- rules: [{ reason: "highspeed_request_failed" }],
- },
- },
- });
- expect(result.response).toBe("continued with the session model");
- // 加速模型只开流一次,没有同模型重试;随后直接由会话模型接管。
- expect(streamedProviders).toEqual([highspeedProviderId, "user-provider"]);
- const events = await eventStore.getEvents(sessionId);
- expect(
- events.filter((event) => event.type === SessionEventType.TurnExecutionModelFallback),
- ).toHaveLength(1);
- });
- it("preserves an explicit source-less model boundary in ModelSelected", async () => {
- const sessionId = createSessionId("runtime-model-selection-source-less-boundary");
- const eventStore = createTestSessionEventStore();
- const runtime = createTestAgentRuntime(sessionId, {}, { eventStore });
- await runtime.emitModelSelected({
- modelSelection: {
- providerId: createModelProviderId("child-provider"),
- modelId: createModelId("child-model"),
- },
- previousModelSelection: null,
- traceContext: { traceId: "trace-runtime-model-selection-source-less" } as never,
- });
- const selected = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.ModelSelected,
- );
- expect(selected?.payload).toMatchObject({
- previousModelSelection: null,
- modelSelection: {
- providerId: "child-provider",
- modelId: "child-model",
- },
- });
- });
- it.each([false, true])(
- "keeps the highspeed fallback fixed when Session changes (explicit=%s)",
- async (explicit) => {
- const sessionId = createSessionId("runtime-execution-selection-fallback");
- const eventStore = createTestSessionEventStore();
- const highspeedProviderId = "account:zai-highspeed-card";
- const requests: Array<{ modelId: string; providerId: string }> = [];
- const runtime = createTestAgentRuntime(
- sessionId,
- {
- modelSelection: createTestModelSelection(
- explicit ? "idle-provider/idle-model" : "user-provider/user-model",
- ),
- titleGeneration: { enabled: false },
- workingDirectory: "/workspace/project",
- },
- {
- eventStore,
- modelFactory: createTestModelFactory({
- async generateText(_request, observation) {
- requests.push({
- modelId: String(observation.model.modelId),
- providerId: String(observation.model.providerId),
- });
- if (String(observation.model.providerId) === highspeedProviderId) {
- // 模拟较新的用户输入已经接管续跑选择;旧加速轮不能跟随这个选择回退。
- runtime.setSessionModelSelection(
- createTestModelSelection("new-provider/new-model"),
- );
- throw Object.assign(new Error("highspeed card is invalid"), {
- code: "invalid_model_request",
- context: {
- providerCode: "3402",
- providerId: highspeedProviderId,
- modelId: "GLM-5.3",
- source: "provider",
- },
- name: "AiSdkModelAdapterError",
- });
- }
- return {
- finishReason: "stop",
- model: observation.model,
- text: "continued with the session model",
- usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
- };
- },
- }),
- },
- );
- const result = await runtime.executeTurn("continue after expiry", undefined, {
- inputId: "command-highspeed-expired",
- intent: {
- sourceCommandId: "command-highspeed-expired",
- kind: "sendText",
- requestedDelivery: "start-now",
- modelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- },
- modelExecution: {
- selectionScope: "execution",
- selectionFallback: {
- providerId: highspeedProviderId,
- ...(explicit ? { target: createTestModelSelection("user-provider/user-model") } : {}),
- rules: [{ reason: "highspeed_card_expired", providerErrorCode: "3402" }],
- },
- },
- });
- expect(result.response).toBe("continued with the session model");
- expect(requests).toEqual([
- { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- { providerId: "user-provider", modelId: "user-model" },
- ]);
- const fallbackEvent = (await eventStore.getEvents(sessionId)).find(
- (event) => event.type === SessionEventType.TurnExecutionModelFallback,
- );
- expect(fallbackEvent?.payload).toMatchObject({
- inputId: "command-highspeed-expired",
- reason: "highspeed_card_expired",
- fromModelSelection: { providerId: highspeedProviderId, modelId: "GLM-5.3" },
- toModelSelection: { providerId: "user-provider", modelId: "user-model" },
- });
- // 回退只影响当前轮,不能把较新的用户选择覆盖回去。
- expect(runtime.getSessionModelSelection()).toMatchObject({
- providerId: "new-provider",
- modelId: "new-model",
- });
- },
- );
- });
|