workflow-driver-submit-profile.test.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * submit profile(docs/execution-engine.md「Typed asks and `submit_result`」)的
  3. * 确定性集成测试:真实脚本 → 编译期 profile → 真实 driver + 真实 AgentRuntime(脚本化模型)。
  4. * 断言的是**模型面**:每次请求里 submit_result 的声明(typed / generic / 缺席)与 ask 尾注的形态,
  5. * 以及运行时守卫在静态 profile 错误时的两条路。
  6. */
  7. import { describe, expect, it } from "vitest";
  8. import { SUBMIT_RESULT_TOOL_NAME } from "@zcode/contracts";
  9. import { GENERIC_SUBMIT_PROFILE, type ActorSubmitProfile } from "@zcode/dynamic-workflow";
  10. import { TYPED_TOOL_EPILOGUE } from "../src/app/workflow-driver-helpers.js";
  11. import { actorSubmitProfilesFor, runDriverScript } from "./workflow-driver.helpers.js";
  12. const FULL_SCHEMA_EPILOGUE = "conforming to this JSON Schema";
  13. const MONO_SCRIPT = [
  14. `interface Summary { title: string; points: string[] }`,
  15. `const a = agent("summarizer", "You summarize.");`,
  16. `const s = await a.ask<Summary>("Summarize the repo");`,
  17. `return s;`,
  18. ].join("\n");
  19. const MULTI_SCRIPT = [
  20. `interface Summary { title: string; points: string[] }`,
  21. `interface Verdict { ok: boolean }`,
  22. `const a = agent("worker", "You do work.");`,
  23. `const s = await a.ask<Summary>("Summarize the repo");`,
  24. `const v = await a.ask<Verdict>("Judge " + s.title);`,
  25. `return [s, v];`,
  26. ].join("\n");
  27. const UNTYPED_SCRIPT = [
  28. `const a = agent("talker", "You talk.");`,
  29. `const t = await a.ask("Say something");`,
  30. `return t;`,
  31. ].join("\n");
  32. const SUMMARY = { title: "Repo", points: ["x"] };
  33. function submitTool(log: { submitTools: unknown[] } | undefined, index = 0) {
  34. return (log?.submitTools[index] ?? undefined) as
  35. | { inputSchema: Record<string, unknown>; strict: boolean | undefined }
  36. | undefined;
  37. }
  38. describe("workflow driver — submit profile: mono subagent", () => {
  39. it("declares { result: schema } on the tool, marks it strict-eligible, and shrinks the epilogue", async () => {
  40. const profiles = actorSubmitProfilesFor(MONO_SCRIPT);
  41. expect(profiles.get("actor#1")?.kind).toBe("mono");
  42. const { settlement, modelCalls } = await runDriverScript(MONO_SCRIPT, {
  43. actorScripts: { "actor#1@1": [{ kind: "submit", result: SUMMARY }] },
  44. actorSubmitProfiles: "derive",
  45. });
  46. expect(settlement).toMatchObject({ status: "completed", artifact: SUMMARY });
  47. const tool = submitTool(modelCalls["actor#1@1"]);
  48. expect(tool).toBeDefined();
  49. expect(tool?.strict).toBe(true);
  50. const result = (tool?.inputSchema.properties as Record<string, Record<string, unknown>>).result;
  51. expect(result.type).toBe("object");
  52. expect(result.required).toEqual(["title", "points"]);
  53. expect(tool?.inputSchema.required).toEqual(["result"]);
  54. const prompt = modelCalls["actor#1@1"]?.prompts[0] ?? "";
  55. expect(prompt).toContain("Summarize the repo");
  56. expect(prompt).toContain("Standard for this result:");
  57. expect(prompt).toContain(TYPED_TOOL_EPILOGUE);
  58. expect(prompt).not.toContain(FULL_SCHEMA_EPILOGUE);
  59. // 质量尾注仍在,且在 typed 尾注之前。
  60. expect(prompt.indexOf("Standard for this result:")).toBeLessThan(
  61. prompt.indexOf(TYPED_TOOL_EPILOGUE),
  62. );
  63. });
  64. it("still repairs in-session against the engine's validator (strict is a guarantee, not the only defence)", async () => {
  65. const { settlement, modelCalls } = await runDriverScript(MONO_SCRIPT, {
  66. actorScripts: {
  67. "actor#1@1": [
  68. { kind: "submit", result: { title: 42 } },
  69. { kind: "submit", result: SUMMARY },
  70. ],
  71. },
  72. actorSubmitProfiles: "derive",
  73. });
  74. expect(settlement).toMatchObject({ status: "completed", artifact: SUMMARY });
  75. // 同一 turn 内两次模型请求(第一次被拒回 error tool_result),声明始终是 typed 的。
  76. expect(modelCalls["actor#1@1"]?.calls).toBe(2);
  77. expect(submitTool(modelCalls["actor#1@1"], 1)?.strict).toBe(true);
  78. });
  79. });
  80. describe("workflow driver — submit profile: generic and untyped subagents", () => {
  81. it("a subagent with two result types keeps the generic tool and the full schema epilogue", async () => {
  82. const profiles = actorSubmitProfilesFor(MULTI_SCRIPT);
  83. expect(profiles.get("actor#1")).toEqual(GENERIC_SUBMIT_PROFILE);
  84. const { settlement, modelCalls } = await runDriverScript(MULTI_SCRIPT, {
  85. actorScripts: {
  86. "actor#1@1": [
  87. { kind: "submit", result: SUMMARY },
  88. { kind: "submit", result: { ok: true } },
  89. ],
  90. },
  91. actorSubmitProfiles: "derive",
  92. });
  93. expect(settlement).toMatchObject({ status: "completed", artifact: [SUMMARY, { ok: true }] });
  94. const log = modelCalls["actor#1@1"];
  95. for (const index of [0, 1]) {
  96. const tool = submitTool(log, index);
  97. expect(tool?.strict).toBeUndefined();
  98. const result = (tool?.inputSchema.properties as Record<string, Record<string, unknown>>)
  99. .result;
  100. expect(result.type).toBeUndefined();
  101. expect(log?.prompts[index]).toContain(FULL_SCHEMA_EPILOGUE);
  102. expect(log?.prompts[index]).not.toContain(TYPED_TOOL_EPILOGUE);
  103. }
  104. // 两个 ask 之间工具声明逐字节不变(缓存不变式)。
  105. expect(submitTool(log, 0)?.inputSchema).toEqual(submitTool(log, 1)?.inputSchema);
  106. });
  107. it("an untyped-only subagent gets no submit_result at all and settles from its final text", async () => {
  108. const profiles = actorSubmitProfilesFor(UNTYPED_SCRIPT);
  109. expect(profiles.get("actor#1")).toEqual({ kind: "untyped" });
  110. const { settlement, modelCalls } = await runDriverScript(UNTYPED_SCRIPT, {
  111. actorScripts: { "actor#1@1": [{ kind: "text", text: "hello there" }] },
  112. actorSubmitProfiles: "derive",
  113. });
  114. expect(settlement).toMatchObject({ status: "completed", artifact: "hello there" });
  115. expect(modelCalls["actor#1@1"]?.toolNames[0]).not.toContain(SUBMIT_RESULT_TOOL_NAME);
  116. expect(submitTool(modelCalls["actor#1@1"])).toBeUndefined();
  117. });
  118. it("without a profile map every subagent is generic (the pre-2026-09-13 behaviour, byte for byte)", async () => {
  119. const { modelCalls } = await runDriverScript(MONO_SCRIPT, {
  120. actorScripts: { "actor#1@1": [{ kind: "submit", result: SUMMARY }] },
  121. });
  122. const tool = submitTool(modelCalls["actor#1@1"]);
  123. expect(tool?.strict).toBeUndefined();
  124. expect(modelCalls["actor#1@1"]?.prompts[0]).toContain(FULL_SCHEMA_EPILOGUE);
  125. });
  126. });
  127. describe("workflow driver — submit profile: runtime guard", () => {
  128. it("a mono declaration that does not match the ask's schema falls back to generic for that session", async () => {
  129. // 喂一份**错误**的静态 profile:声明的是 Verdict 的形状,实际 ask 要的是 Summary。
  130. const wrong: ActorSubmitProfile = {
  131. kind: "mono",
  132. schema: {
  133. type: "object",
  134. properties: { ok: { type: "boolean" } },
  135. required: ["ok"],
  136. additionalProperties: false,
  137. },
  138. };
  139. const { settlement, modelCalls } = await runDriverScript(MONO_SCRIPT, {
  140. actorScripts: { "actor#1@1": [{ kind: "submit", result: SUMMARY }] },
  141. actorSubmitProfiles: new Map([["actor#1", wrong]]),
  142. });
  143. expect(settlement).toMatchObject({ status: "completed", artifact: SUMMARY });
  144. // 模型看到的是换回来的通用声明 + 整份 schema 尾注,而不是错误的 typed 声明。
  145. const tool = submitTool(modelCalls["actor#1@1"]);
  146. expect(tool?.strict).toBeUndefined();
  147. const result = (tool?.inputSchema.properties as Record<string, Record<string, unknown>>).result;
  148. expect(result.type).toBeUndefined();
  149. expect(modelCalls["actor#1@1"]?.prompts[0]).toContain(FULL_SCHEMA_EPILOGUE);
  150. });
  151. it("a typed ask reaching an 'untyped' subagent fails that ask loudly instead of burning the nudge budget", async () => {
  152. const { settlement, modelCalls } = await runDriverScript(MONO_SCRIPT, {
  153. actorScripts: { "actor#1@1": [{ kind: "submit", result: SUMMARY }] },
  154. actorSubmitProfiles: new Map([["actor#1", { kind: "untyped" }]]),
  155. });
  156. expect(settlement.status).toBe("errored");
  157. const message = settlement.status === "errored" ? settlement.error.message : "";
  158. expect(message).toContain('"untyped"');
  159. expect(message).toContain("ask#1@1");
  160. // 一次模型请求都没发:守卫在派发 turn 之前就把 ask 判失败了。
  161. expect(modelCalls["actor#1@1"]?.calls ?? 0).toBe(0);
  162. });
  163. });