subagent-effective-selection.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { describe, expect, it, vi } from "vitest";
  2. import { resolveSubagentSelection } from "../src/runtime/helpers/subagent-selection.js";
  3. import { projectExecutionErrorPayload } from "../src/errors/error-payload.js";
  4. import { createErrorResult } from "../src/tool/executor/errors.js";
  5. import type { EffectiveModelSelectionResult } from "@zcode/shared/model-selection";
  6. import { modelContentForToolResult } from "../src/runtime/helpers/tool-result.js";
  7. describe("显式 Subagent 选择使用注入的公共解析", () => {
  8. const original = Object.freeze({
  9. providerId: "account:bigmodel-individual-coding-plan",
  10. modelId: "GLM-5.3",
  11. options: Object.freeze({ reasoningLevel: "high" }),
  12. });
  13. const effective = { ...original, providerId: "account:bigmodel-team-coding-plan" };
  14. it.each([
  15. "selection-missing",
  16. "account-connection-unavailable",
  17. "provider-not-found",
  18. "model-not-found",
  19. "reasoning-level-missing",
  20. "reasoning-level-not-supported",
  21. ] as const)("真实错误投影与工具结果保留原因 %s", (reason) => {
  22. let caught: Error | undefined;
  23. try {
  24. resolveSubagentSelection({
  25. profileSelection: original,
  26. resolveSelection: () => ({ effectiveSelection: effective, selectionIssue: reason }),
  27. });
  28. } catch (error) {
  29. caught = error as Error;
  30. }
  31. expect(caught).toBeDefined();
  32. const projected = projectExecutionErrorPayload(caught);
  33. expect(projected.attribution).toMatchObject({
  34. reason,
  35. providerId: effective.providerId,
  36. modelId: effective.modelId,
  37. });
  38. expect(projected.detail).toContain(`reason=${reason}`);
  39. expect(projected.message).toContain(reason);
  40. expect(projected.message).toContain(original.providerId);
  41. expect(projected.message).toMatch(/[\u4e00-\u9fff]/);
  42. const result = createErrorResult(
  43. { id: "tool-selection", name: "Agent", input: {} } as Parameters<typeof createErrorResult>[0],
  44. caught!,
  45. );
  46. expect(result.error?.message).toBe(projected.message);
  47. expect(result.error?.detail).toContain(reason);
  48. expect(modelContentForToolResult(result)).toContain(reason);
  49. expect(result.success).toBe(false);
  50. });
  51. it("没有有效身份时只描述原选择,不冒充已重映射执行身份", () => {
  52. let caught: unknown;
  53. try {
  54. resolveSubagentSelection({
  55. profileSelection: original,
  56. resolveSelection: (): EffectiveModelSelectionResult => ({
  57. effectiveSelection: null,
  58. selectionIssue: "account-connection-unavailable",
  59. }),
  60. });
  61. } catch (error) {
  62. caught = error;
  63. }
  64. const projected = projectExecutionErrorPayload(caught);
  65. expect(projected.message).toContain(original.providerId);
  66. expect(projected.attribution).not.toHaveProperty("providerId");
  67. });
  68. it("只解析本次显式意图,保留原 profile", () => {
  69. const resolveSelection = vi.fn(() => ({ effectiveSelection: effective }));
  70. expect(resolveSubagentSelection({ profileSelection: original, resolveSelection })).toEqual({
  71. selection: effective,
  72. hasConcreteModel: true,
  73. });
  74. expect(resolveSelection).toHaveBeenCalledWith(original);
  75. });
  76. it("无有效同模型时阻断,不能回退父模型", () => {
  77. expect(() =>
  78. resolveSubagentSelection({
  79. profileSelection: original,
  80. parentSelection: effective,
  81. resolveSelection: () => ({ effectiveSelection: null, selectionIssue: "model-not-found" }),
  82. }),
  83. ).toThrow();
  84. });
  85. it("继承和已明确的内部 override 不重解析", () => {
  86. const resolveSelection = vi.fn(() => ({ effectiveSelection: null }));
  87. expect(
  88. resolveSubagentSelection({ parentSelection: original, resolveSelection }).selection,
  89. ).toEqual(original);
  90. expect(
  91. resolveSubagentSelection({
  92. profileSelection: original,
  93. overrideSelection: effective,
  94. resolveSelection,
  95. }).selection,
  96. ).toEqual(effective);
  97. expect(resolveSelection).not.toHaveBeenCalled();
  98. });
  99. it("未注入服务的独立 CLI 保持原行为", () => {
  100. expect(resolveSubagentSelection({ profileSelection: original }).selection).toEqual(original);
  101. });
  102. it("公共解析返回缺档位的部分结果时保留原因并阻断执行", () => {
  103. expect(() =>
  104. resolveSubagentSelection({
  105. profileSelection: original,
  106. resolveSelection: () => ({
  107. effectiveSelection: { providerId: effective.providerId, modelId: effective.modelId },
  108. selectionIssue: "reasoning-level-not-supported",
  109. }),
  110. }),
  111. ).toThrow(
  112. expect.objectContaining({
  113. context: expect.objectContaining({ selectionIssue: "reasoning-level-not-supported" }),
  114. }),
  115. );
  116. });
  117. });