turn-execution-fallback.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { describe, expect, it } from "vitest";
  2. import { ModelRetryBudget } from "@zcode/contracts";
  3. import {
  4. matchExecutionSelectionFallback,
  5. resolveExecutionFallbackCandidates,
  6. } from "../src/runtime/methods/turn-execution-fallback.js";
  7. import { resolveExecutionModelRetryBudget } from "../src/runtime/methods/turn-model.js";
  8. // docs/highspeed/highspeed-card-spec.md §2.2:加速请求任何失败都退回会话模型;卡过期按 3402 单独给原因。
  9. const HIGHSPEED_PROVIDER_ID = "account:zai-highspeed-card";
  10. const SESSION_PROVIDER_ID = "builtin:zai-coding-plan";
  11. const CARD_EXPIRED_CODE = "3402";
  12. const declaration = {
  13. providerId: HIGHSPEED_PROVIDER_ID,
  14. rules: [
  15. { reason: "highspeed_card_expired" as const, providerErrorCode: CARD_EXPIRED_CODE },
  16. { reason: "highspeed_request_failed" as const },
  17. ],
  18. };
  19. /** adapter 归一化后的错误形状:归因字段都在 context 里。 */
  20. function adapterError(
  21. context: Record<string, unknown>,
  22. message = "Model request failed.",
  23. code = "model_request_failed",
  24. ): Error {
  25. return Object.assign(new Error(message), {
  26. code,
  27. context: {
  28. providerId: HIGHSPEED_PROVIDER_ID,
  29. modelId: "GLM-5.3",
  30. source: "provider",
  31. ...context,
  32. },
  33. name: "AiSdkModelAdapterError",
  34. });
  35. }
  36. function match(
  37. error: unknown,
  38. overrides: Partial<Parameters<typeof matchExecutionSelectionFallback>[0]> = {},
  39. ) {
  40. return matchExecutionSelectionFallback({
  41. error,
  42. executionProviderId: HIGHSPEED_PROVIDER_ID,
  43. selectionFallback: declaration,
  44. turnAborted: false,
  45. ...overrides,
  46. });
  47. }
  48. describe("matchExecutionSelectionFallback", () => {
  49. it("卡过期错误码命中首条规则,原因为 highspeed_card_expired 并带回归因", () => {
  50. expect(match(adapterError({ providerCode: CARD_EXPIRED_CODE, statusCode: 400 }))).toEqual({
  51. reason: "highspeed_card_expired",
  52. providerErrorCode: CARD_EXPIRED_CODE,
  53. statusCode: 400,
  54. });
  55. });
  56. it.each([
  57. ["401 鉴权失败", { statusCode: 401, reason: "auth_failed", retryable: false }],
  58. ["429 限流", { statusCode: 429, reason: "rate_limited", retryable: true }],
  59. ["5xx", { statusCode: 503, reason: "server_error", retryable: true }],
  60. ["网络错误(无 HTTP 状态、无业务码)", { reason: "network_error", retryable: true }],
  61. ["流空闲超时", { reason: "stream_idle_timeout", retryable: true }],
  62. ["其他业务码", { providerCode: "1234", statusCode: 400 }],
  63. ])("%s 命中兜底规则,原因为 highspeed_request_failed", (_label, context) => {
  64. expect(match(adapterError(context))?.reason).toBe("highspeed_request_failed");
  65. });
  66. it("没有声明、用户取消或本步模型不是声明 provider 时不退回", () => {
  67. const error = adapterError({ statusCode: 500 });
  68. expect(match(error, { selectionFallback: undefined })).toBeUndefined();
  69. expect(match(error, { turnAborted: true })).toBeUndefined();
  70. expect(match(error, { executionProviderId: SESSION_PROVIDER_ID })).toBeUndefined();
  71. });
  72. it("失败归因明确指向其他 provider 时不退回", () => {
  73. expect(
  74. match(adapterError({ providerId: SESSION_PROVIDER_ID, statusCode: 500 })),
  75. ).toBeUndefined();
  76. });
  77. it("无上游归因或归因为本地 runtime 的失败不退回", () => {
  78. expect(match(new Error("event store append failed"))).toBeUndefined();
  79. expect(
  80. match(adapterError({ source: "runtime", reason: "persistence_failed" })),
  81. ).toBeUndefined();
  82. });
  83. it("上下文超窗不退回,交给 reactive compact", () => {
  84. expect(
  85. match(
  86. adapterError(
  87. { reason: "context_exceeded", statusCode: 400 },
  88. "Model request exceeded the provider context window.",
  89. "model_context_exceeded",
  90. ),
  91. ),
  92. ).toBeUndefined();
  93. });
  94. it("只声明错误码规则时,其他失败不退回;规则按声明顺序首个命中", () => {
  95. const expiredOnly = { providerId: HIGHSPEED_PROVIDER_ID, rules: [declaration.rules[0]!] };
  96. expect(
  97. match(adapterError({ statusCode: 500 }), { selectionFallback: expiredOnly }),
  98. ).toBeUndefined();
  99. expect(
  100. match(adapterError({ providerCode: CARD_EXPIRED_CODE }), { selectionFallback: expiredOnly })
  101. ?.reason,
  102. ).toBe("highspeed_card_expired");
  103. // 兜底规则排在前面会遮蔽后面的错误码规则:声明顺序就是优先级。
  104. const catchAllFirst = {
  105. providerId: HIGHSPEED_PROVIDER_ID,
  106. rules: [declaration.rules[1]!, declaration.rules[0]!],
  107. };
  108. expect(
  109. match(adapterError({ providerCode: CARD_EXPIRED_CODE }), { selectionFallback: catchAllFirst })
  110. ?.reason,
  111. ).toBe("highspeed_request_failed");
  112. });
  113. });
  114. describe("resolveExecutionFallbackCandidates", () => {
  115. // spec §2.2:退回目标先取发起方声明的 target(抽卡时的提交选择),再取 runtime 会话常驻 Selection。
  116. const target = {
  117. providerId: SESSION_PROVIDER_ID,
  118. modelId: "GLM-5.3",
  119. options: { reasoningLevel: "low" },
  120. };
  121. const resident = { providerId: "user-provider", modelId: "user-model" };
  122. it("先取声明的 target,再取会话常驻 Selection", () => {
  123. expect(
  124. resolveExecutionFallbackCandidates({
  125. declaredTarget: target,
  126. executionProviderId: HIGHSPEED_PROVIDER_ID,
  127. sessionSelection: resident,
  128. }),
  129. ).toEqual([
  130. { source: "declared_target", selection: target },
  131. { source: "session_selection", selection: resident },
  132. ]);
  133. });
  134. it("没有 target 时只剩会话常驻 Selection;都没有时无候选", () => {
  135. expect(
  136. resolveExecutionFallbackCandidates({
  137. declaredTarget: undefined,
  138. executionProviderId: HIGHSPEED_PROVIDER_ID,
  139. sessionSelection: resident,
  140. }),
  141. ).toEqual([{ source: "session_selection", selection: resident }]);
  142. expect(
  143. resolveExecutionFallbackCandidates({
  144. declaredTarget: undefined,
  145. executionProviderId: HIGHSPEED_PROVIDER_ID,
  146. sessionSelection: undefined,
  147. }),
  148. ).toEqual([]);
  149. });
  150. it("候选不能是本轮 execution provider 本身;相同候选只保留一个", () => {
  151. // 退回同一个失败端点只会再失败一次;误声明成加速 provider 的 target 直接忽略。
  152. expect(
  153. resolveExecutionFallbackCandidates({
  154. declaredTarget: { providerId: HIGHSPEED_PROVIDER_ID, modelId: "GLM-5.3" },
  155. executionProviderId: HIGHSPEED_PROVIDER_ID,
  156. sessionSelection: target,
  157. }),
  158. ).toEqual([{ source: "session_selection", selection: target }]);
  159. expect(
  160. resolveExecutionFallbackCandidates({
  161. declaredTarget: target,
  162. executionProviderId: HIGHSPEED_PROVIDER_ID,
  163. sessionSelection: { ...target, options: { ...target.options } },
  164. }),
  165. ).toEqual([{ source: "declared_target", selection: target }]);
  166. });
  167. });
  168. describe("resolveExecutionModelRetryBudget", () => {
  169. it("带退回声明且 Selection 指向声明 provider 的执行句柄 0 次重试", () => {
  170. expect(
  171. resolveExecutionModelRetryBudget({
  172. selection: { providerId: HIGHSPEED_PROVIDER_ID, modelId: "GLM-5.3" },
  173. selectionFallback: declaration,
  174. }),
  175. ).toBe(ModelRetryBudget.SingleAttempt);
  176. });
  177. it("无声明、无 Selection 或 Selection 不是声明 provider 时沿用默认预算", () => {
  178. expect(
  179. resolveExecutionModelRetryBudget({
  180. selection: { providerId: HIGHSPEED_PROVIDER_ID, modelId: "GLM-5.3" },
  181. selectionFallback: undefined,
  182. }),
  183. ).toBeUndefined();
  184. expect(
  185. resolveExecutionModelRetryBudget({
  186. selection: { providerId: SESSION_PROVIDER_ID, modelId: "GLM-5.3" },
  187. selectionFallback: declaration,
  188. }),
  189. ).toBeUndefined();
  190. expect(
  191. resolveExecutionModelRetryBudget({ selection: undefined, selectionFallback: declaration }),
  192. ).toBeUndefined();
  193. });
  194. });