workflow-settings-adjustment.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. CreateWorkflowInputSchema,
  4. createRootTraceContext,
  5. createSessionId,
  6. createToolCallId,
  7. type DynamicWorkflowRunPort,
  8. type ModelCatalogPort,
  9. } from "@zcode/contracts";
  10. import { applyPermissionInputAdjustments } from "../src/tool/executor/permission-input-adjustments.js";
  11. import type { ToolExecutorDeps } from "../src/tool/executor/types.js";
  12. import {
  13. applyWorkflowSettingsAdjustments,
  14. describeWorkflowSettingsAdjustment,
  15. withWorkflowAdjustableSettings,
  16. } from "../src/tool/handlers/workflow-settings-adjustment.js";
  17. import type { ToolEntry, ToolInputResolutionContext } from "../src/tool/types.js";
  18. // docs/dynamic-workflow/launch.md「Adjusting the settings in the window」
  19. const PORT = { defaultConcurrency: () => 6 } as unknown as DynamicWorkflowRunPort;
  20. const CATALOG: ModelCatalogPort = {
  21. listModels: () => [
  22. { providerId: "openai", modelId: "gpt-5", reasoningLevels: [], current: true },
  23. ],
  24. };
  25. const CONTEXT: ToolInputResolutionContext = {
  26. dynamicWorkflowRunPort: PORT,
  27. modelCatalogPort: CATALOG,
  28. };
  29. const OPTIONS = { errorCode: 400, schema: CreateWorkflowInputSchema };
  30. describe("withWorkflowAdjustableSettings", () => {
  31. it("overwrites a block the caller supplied: forging it is inert", () => {
  32. const resolved = withWorkflowAdjustableSettings(
  33. {
  34. result: true,
  35. input: {
  36. script: "x",
  37. adjustable_settings: { subagent_model: false, concurrency_ceiling: 99 },
  38. },
  39. },
  40. CONTEXT,
  41. );
  42. expect(resolved).toEqual({
  43. result: true,
  44. input: { script: "x", adjustable_settings: { subagent_model: true, concurrency_ceiling: 6 } },
  45. });
  46. });
  47. it("writes no block when the shape raises no window, and strips a forged one", () => {
  48. const resolved = withWorkflowAdjustableSettings(
  49. { result: true, input: { run_id: "r", max_concurrency: 2, adjustable_settings: {} } },
  50. CONTEXT,
  51. (input) => input.script !== undefined,
  52. );
  53. expect(resolved).toEqual({ result: true, input: { run_id: "r", max_concurrency: 2 } });
  54. });
  55. it("passes a failure through untouched", () => {
  56. const failure = { result: false as const, errorCode: 400, message: "nope" };
  57. expect(withWorkflowAdjustableSettings(failure, CONTEXT)).toBe(failure);
  58. });
  59. });
  60. describe("applyWorkflowSettingsAdjustments", () => {
  61. it("ignores keys it does not know and content of the wrong shape", () => {
  62. const input = { script: "x", max_concurrency: 2 };
  63. expect(applyWorkflowSettingsAdjustments(input, { color: "red" }, CONTEXT, OPTIONS)).toEqual({
  64. result: true,
  65. input,
  66. });
  67. expect(
  68. applyWorkflowSettingsAdjustments(input, { max_concurrency: "four" }, CONTEXT, OPTIONS),
  69. ).toEqual({ result: true, input });
  70. });
  71. it("refuses a model when the host has no catalog", () => {
  72. const outcome = applyWorkflowSettingsAdjustments(
  73. { script: "x" },
  74. { subagent_model: "openai/gpt-5" },
  75. { dynamicWorkflowRunPort: PORT },
  76. OPTIONS,
  77. );
  78. expect(outcome).toMatchObject({ result: false, errorCode: 400 });
  79. });
  80. it("a null for a key the input never had changes nothing", () => {
  81. expect(
  82. applyWorkflowSettingsAdjustments(
  83. { script: "x" },
  84. { subagent_model: null, max_concurrency: null },
  85. CONTEXT,
  86. OPTIONS,
  87. ),
  88. ).toEqual({ result: true, input: { script: "x" } });
  89. });
  90. it("applies a bound above the default as is: the default is not a cap", () => {
  91. expect(
  92. applyWorkflowSettingsAdjustments({ script: "x" }, { max_concurrency: 40 }, CONTEXT, OPTIONS),
  93. ).toEqual({
  94. result: true,
  95. input: { script: "x", max_concurrency: 40 },
  96. applied: { max_concurrency: 40 },
  97. });
  98. });
  99. });
  100. describe("describeWorkflowSettingsAdjustment", () => {
  101. it("says the singular, and leaves the default out when it is unknown", () => {
  102. expect(describeWorkflowSettingsAdjustment({ max_concurrency: 1 }, undefined)).toBe(
  103. " Before approving, the user adjusted the settings in the confirmation window: at most 1 subagent runs at once.",
  104. );
  105. });
  106. it("names the default beside a bound above it, and says a null is back to the default", () => {
  107. expect(describeWorkflowSettingsAdjustment({ max_concurrency: 40 }, 6)).toBe(
  108. " Before approving, the user adjusted the settings in the confirmation window: at most 40 subagents run at once (the default is 6).",
  109. );
  110. expect(describeWorkflowSettingsAdjustment({ max_concurrency: null }, 6)).toBe(
  111. " Before approving, the user adjusted the settings in the confirmation window: the limit on subagents at once is back to the default.",
  112. );
  113. });
  114. it("says nothing when nothing changed", () => {
  115. expect(describeWorkflowSettingsAdjustment({}, 6)).toBe("");
  116. });
  117. });
  118. describe("applyPermissionInputAdjustments", () => {
  119. const toolCall = { id: createToolCallId("adj"), input: {}, name: "Probe" };
  120. const traceContext = createRootTraceContext({ sessionId: createSessionId("adj") });
  121. function deps(): { deps: ToolExecutorDeps; warn: ReturnType<typeof vi.fn> } {
  122. const warn = vi.fn();
  123. return {
  124. deps: {
  125. getWorkingDirectory: () => "/work",
  126. logger: { debug: vi.fn(), info: vi.fn(), warn, error: vi.fn() },
  127. sessionId: createSessionId("adj"),
  128. } as unknown as ToolExecutorDeps,
  129. warn,
  130. };
  131. }
  132. it("passes the input through when there is nothing to apply", () => {
  133. const { deps: executorDeps } = deps();
  134. expect(
  135. applyPermissionInputAdjustments({
  136. adjustments: undefined,
  137. deps: executorDeps,
  138. entry: {} as ToolEntry,
  139. executionInput: { a: 1 },
  140. toolCall,
  141. traceContext,
  142. }),
  143. ).toEqual({ ok: true, executionInput: { a: 1 } });
  144. });
  145. it("runs the call as approved, and logs, when the tool does not apply adjustments", () => {
  146. const { deps: executorDeps, warn } = deps();
  147. expect(
  148. applyPermissionInputAdjustments({
  149. adjustments: { max_concurrency: 2 },
  150. deps: executorDeps,
  151. entry: {} as ToolEntry,
  152. executionInput: { a: 1 },
  153. toolCall,
  154. traceContext,
  155. }),
  156. ).toEqual({ ok: true, executionInput: { a: 1 } });
  157. expect(warn).toHaveBeenCalledOnce();
  158. });
  159. it("turns the tool's refusal into a failed tool result", () => {
  160. const { deps: executorDeps } = deps();
  161. const outcome = applyPermissionInputAdjustments({
  162. adjustments: { max_concurrency: 2 },
  163. deps: executorDeps,
  164. entry: {
  165. applyInputAdjustments: () => ({ result: false, errorCode: 23, message: "refused" }),
  166. } as unknown as ToolEntry,
  167. executionInput: { a: 1 },
  168. toolCall,
  169. traceContext,
  170. });
  171. expect(outcome.ok).toBe(false);
  172. expect(outcome.ok === false && outcome.result.success).toBe(false);
  173. });
  174. it("hands the tool the resolution context and returns what it applied", () => {
  175. const { deps: executorDeps } = deps();
  176. const apply = vi.fn(() => ({ result: true as const, input: { a: 2 }, applied: { a: 2 } }));
  177. const outcome = applyPermissionInputAdjustments({
  178. adjustments: { a: 2 },
  179. deps: executorDeps,
  180. entry: { applyInputAdjustments: apply } as unknown as ToolEntry,
  181. executionInput: { a: 1 },
  182. toolCall,
  183. traceContext,
  184. });
  185. expect(outcome).toEqual({ ok: true, executionInput: { a: 2 }, applied: { a: 2 } });
  186. expect(apply).toHaveBeenCalledWith(
  187. { a: 1 },
  188. { a: 2 },
  189. expect.objectContaining({ workingDirectory: "/work" }),
  190. );
  191. });
  192. });