resolve-workflow-question-tool.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // 主代理侧的应答工具(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md)。被钉住的三件事:
  2. // 1. 成功回执要说清「答案已送达」**且**「run 并没有因此停下」——否则模型会守着一个
  3. // 根本不需要它守的 run;
  4. // 2. 三种拒绝的文案**逐字**来自服务端(判别键与文案分开维护则两处迟早会说不同的话,
  5. // 而这里的读者是模型——它读到的就是它的下一步);
  6. // 3. 端口/方法缺席回结构化失败,绝不静默成功——静默成功会让一个 actor 永远等下去,
  7. // 而模型以为自己已经答过了。
  8. import {
  9. RESOLVE_WORKFLOW_QUESTION_TOOL_NAME,
  10. createRootTraceContext,
  11. createSessionId,
  12. createToolCallId,
  13. createTurnId,
  14. type DynamicWorkflowRunPort,
  15. } from "@zcode/contracts";
  16. import { describe, expect, it } from "vitest";
  17. import { PermissionService, defaultPermissionConfig } from "../src/permission/service.js";
  18. import { createToolExecutor } from "../src/tool/executor.js";
  19. import { registerBuiltInTools } from "../src/tool/handlers/index.js";
  20. import { resolveWorkflowQuestionToolEntry } from "../src/tool/handlers/resolve-workflow-question.js";
  21. import { createToolRegistry } from "../src/tool/registry.js";
  22. import type { ToolExecutionContext } from "../src/tool/types.js";
  23. function toolContext(overrides: Partial<ToolExecutionContext> = {}): ToolExecutionContext {
  24. return {
  25. abortSignal: new AbortController().signal,
  26. sessionId: "sess_resolve_question" as never,
  27. toolCallId: "tool_resolve_question",
  28. traceId: "trace_resolve_question" as never,
  29. workingDirectory: "/tmp",
  30. workspaceRoot: "/tmp",
  31. ...overrides,
  32. };
  33. }
  34. /** 只带 resolveQuestion 的端口 stub;其余成员用不到(handler 只碰这一个方法)。 */
  35. function portWith(
  36. resolveQuestion: DynamicWorkflowRunPort["resolveQuestion"],
  37. ): DynamicWorkflowRunPort {
  38. return { resolveQuestion } as unknown as DynamicWorkflowRunPort;
  39. }
  40. describe("ResolveWorkflowQuestion handler", () => {
  41. it("forwards the qid and answer, and confirms the run is still going", async () => {
  42. const seen: { qid: string; answer: string }[] = [];
  43. const port = portWith(async (qid, answer) => {
  44. seen.push({ qid, answer });
  45. return { ok: true, qid };
  46. });
  47. const output = await resolveWorkflowQuestionToolEntry.handler(
  48. { question_id: "dwfq-abc12345-1", answer: "Treat 95 as passing." },
  49. toolContext({ dynamicWorkflowRunPort: port }),
  50. );
  51. expect(seen).toEqual([{ qid: "dwfq-abc12345-1", answer: "Treat 95 as passing." }]);
  52. expect(output).toMatchObject({ ok: true, qid: "dwfq-abc12345-1" });
  53. const response = (output as { response: string }).response;
  54. expect(response).toContain("dwfq-abc12345-1");
  55. // 「run 仍在跑」必须在回执里说破:升级不是 run 生命周期事件。
  56. expect(response).toContain("run keeps going");
  57. });
  58. it.each([
  59. ["unknown_question", "未知的问题 id dwfq-nope-9。用 GetWorkflowRun 读 run 的 pendingQuestions。"],
  60. ["already_resolved", "这个问题已经被回答过了,提问的子代理早已带着那次答案继续。"],
  61. ["run_not_in_flight", "这个 qid 所属的 run 已经不在飞行中,没有人在等这个答案。"],
  62. ] as const)("passes the %s refusal message through verbatim", async (reason, message) => {
  63. const port = portWith(async () => ({ ok: false, reason, message }));
  64. const output = await resolveWorkflowQuestionToolEntry.handler(
  65. { question_id: "dwfq-nope-9", answer: "..." },
  66. toolContext({ dynamicWorkflowRunPort: port }),
  67. );
  68. expect(output).toMatchObject({ result: false });
  69. // 逐字:handler 一个字都不改写。
  70. expect((output as { message: string }).message).toBe(message);
  71. });
  72. it("gives each refusal reason its own stable error code", async () => {
  73. const codeFor = async (reason: string): Promise<number> => {
  74. const port = portWith(async () => ({ ok: false, reason: reason as never, message: "m" }));
  75. const output = await resolveWorkflowQuestionToolEntry.handler(
  76. { question_id: "dwfq-x-1", answer: "a" },
  77. toolContext({ dynamicWorkflowRunPort: port }),
  78. );
  79. return (output as { errorCode: number }).errorCode;
  80. };
  81. const codes = [
  82. await codeFor("unknown_question"),
  83. await codeFor("already_resolved"),
  84. await codeFor("run_not_in_flight"),
  85. ];
  86. expect(new Set(codes).size).toBe(3);
  87. });
  88. it("reports a capability gap when the port is absent, and when the method is absent", async () => {
  89. const expectUnavailable = (output: unknown): void => {
  90. expect(output).toMatchObject({ result: false });
  91. expect((output as { message: string }).message).toContain(
  92. "workflow_question_answering_unavailable",
  93. );
  94. };
  95. expectUnavailable(
  96. await resolveWorkflowQuestionToolEntry.handler(
  97. { question_id: "dwfq-x-1", answer: "a" },
  98. toolContext(),
  99. ),
  100. );
  101. // 端口在场但 stub 不带 resolveQuestion(typeof 探测的另一半,照 resume/listRuns 先例)。
  102. expectUnavailable(
  103. await resolveWorkflowQuestionToolEntry.handler(
  104. { question_id: "dwfq-x-1", answer: "a" },
  105. toolContext({ dynamicWorkflowRunPort: {} as unknown as DynamicWorkflowRunPort }),
  106. ),
  107. );
  108. });
  109. it("rejects an input missing the answer", async () => {
  110. await expect(
  111. resolveWorkflowQuestionToolEntry.handler(
  112. { question_id: "dwfq-x-1" },
  113. toolContext({ dynamicWorkflowRunPort: portWith(async (qid) => ({ ok: true, qid })) }),
  114. ),
  115. ).rejects.toThrow();
  116. });
  117. });
  118. describe("ResolveWorkflowQuestion executor surface", () => {
  119. it("runs without an approval prompt and returns the confirmation as model content", async () => {
  120. const sessionId = createSessionId("resolve-question-exec");
  121. const turnId = createTurnId("resolve-question-exec");
  122. const traceContext = createRootTraceContext({ sessionId, turnId });
  123. const registry = createToolRegistry();
  124. registry.register(resolveWorkflowQuestionToolEntry);
  125. const executor = createToolExecutor({
  126. emitEvent: async () => undefined,
  127. permissionService: new PermissionService(defaultPermissionConfig),
  128. registry,
  129. dynamicWorkflowRunPort: portWith(async (qid) => ({ ok: true, qid })),
  130. sessionId,
  131. turnId,
  132. traceContext,
  133. });
  134. const result = await executor.execute(
  135. {
  136. id: createToolCallId("resolve-ok"),
  137. input: { question_id: "dwfq-abc12345-1", answer: "Treat 95 as passing." },
  138. name: RESOLVE_WORKFLOW_QUESTION_TOOL_NAME,
  139. },
  140. { traceContext },
  141. );
  142. expect(result.success).toBe(true);
  143. expect(result.modelContent).toContain("dwfq-abc12345-1");
  144. });
  145. });
  146. describe("ResolveWorkflowQuestion registration", () => {
  147. it("is always registered in a main runtime (no include gate of its own)", () => {
  148. const registry = createToolRegistry();
  149. registerBuiltInTools(registry, {});
  150. expect(registry.has(RESOLVE_WORKFLOW_QUESTION_TOOL_NAME)).toBe(true);
  151. });
  152. it("is not registered when a caller disallows it (the actor-session path)", () => {
  153. const registry = createToolRegistry();
  154. registerBuiltInTools(registry, {
  155. disallowedTools: [RESOLVE_WORKFLOW_QUESTION_TOOL_NAME],
  156. });
  157. expect(registry.has(RESOLVE_WORKFLOW_QUESTION_TOOL_NAME)).toBe(false);
  158. });
  159. });