model-retry-budget.test.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. ModelRetryBudget,
  4. modelNetworkStatusEventJsonSchema,
  5. type ModelTextRequest,
  6. } from "../src/model/index.js";
  7. import {
  8. getCurrentModelInvocationContext,
  9. runWithModelInvocationContext,
  10. } from "../src/model/invocation-context.js";
  11. // docs/dynamic-workflow/concurrency.md「Unbounded retries for workflow traffic」:重试预算档位是 runtime-only 字段;
  12. // 状态事件用 maxAttempts = 0 表示无上限,协议 schema 的下界因此是 0。
  13. describe("ModelRetryBudget", () => {
  14. it("exposes the two budget tiers", () => {
  15. expect(ModelRetryBudget).toEqual({ Default: "default", Unbounded: "unbounded" });
  16. });
  17. it("is a runtime-only request field and stays out of the model request JSON schema", async () => {
  18. const request: ModelTextRequest = {
  19. model: "test/model",
  20. messages: [],
  21. modelRetryBudget: ModelRetryBudget.Unbounded,
  22. };
  23. expect(request.modelRetryBudget).toBe("unbounded");
  24. const { modelTextRequestJsonSchema } = await import("../src/model/index.js");
  25. const properties = (modelTextRequestJsonSchema as { properties: Record<string, unknown> })
  26. .properties;
  27. expect(properties).not.toHaveProperty("modelRetryBudget");
  28. expect(properties).not.toHaveProperty("modelRequestSessionType");
  29. });
  30. it("allows maxAttempts = 0 (unbounded marker) in the network status schema", () => {
  31. const properties = (
  32. modelNetworkStatusEventJsonSchema as { properties: Record<string, { minimum?: number }> }
  33. ).properties;
  34. expect(properties.maxAttempts?.minimum).toBe(0);
  35. // attempt 仍从 1 起:只有预算总数才有「无上限」这个哨兵。
  36. expect(properties.attempt?.minimum).toBe(1);
  37. });
  38. it("travels with the model invocation context", () => {
  39. const seen = runWithModelInvocationContext(
  40. { modelRetryBudget: ModelRetryBudget.Unbounded },
  41. () => getCurrentModelInvocationContext()?.modelRetryBudget,
  42. );
  43. expect(seen).toBe("unbounded");
  44. expect(getCurrentModelInvocationContext()).toBeUndefined();
  45. });
  46. });