actor-submit-profiles.test.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. GENERIC_SUBMIT_PROFILE,
  4. buildAskSpecs,
  5. collectSites,
  6. createWorkflowProgram,
  7. deriveActorSubmitProfiles,
  8. deriveActorSubmitProfilesFor,
  9. synthesizeAskSchemas,
  10. type ActorSubmitProfile,
  11. type AskSpec,
  12. type SiteGraph,
  13. } from "../src/index.js";
  14. // 每个 actor 站点的 submit profile(docs/execution-engine.md「Submit
  15. // profiles」):真实脚本 → 同一 Program 上的站点表 + schema 合成 + 站点图 → profile。
  16. function profilesOf(script: string): {
  17. profiles: Map<string, ActorSubmitProfile>;
  18. schemas: Record<string, unknown>;
  19. } {
  20. const workflow = createWorkflowProgram(script);
  21. const table = collectSites(workflow);
  22. const { diagnostics, schemas } = synthesizeAskSchemas(workflow, table);
  23. expect(diagnostics).toEqual([]);
  24. const askSpecs = buildAskSpecs(table, schemas);
  25. return { profiles: deriveActorSubmitProfilesFor(workflow, table, askSpecs), schemas };
  26. }
  27. describe("actor submit profiles — from real scripts", () => {
  28. it("a one-shot typed actor is mono with that ask's schema", () => {
  29. const { profiles, schemas } = profilesOf(
  30. [
  31. `interface Plan { done: boolean }`,
  32. `const a = agent("a");`,
  33. `const p = await a.ask<Plan>("plan");`,
  34. `return p;`,
  35. ].join("\n"),
  36. );
  37. expect(profiles.get("actor#1")).toEqual({ kind: "mono", schema: schemas["ask#1"] });
  38. });
  39. it("two typed asks with the same T stay mono; untyped asks in between do not matter", () => {
  40. const { profiles, schemas } = profilesOf(
  41. [
  42. `interface Plan { done: boolean }`,
  43. `const a = agent("a");`,
  44. `const p = await a.ask<Plan>("plan");`,
  45. `const t = await a.ask("say hi");`,
  46. `const q = await a.ask<Plan>("plan again " + t);`,
  47. `return [p, q];`,
  48. ].join("\n"),
  49. );
  50. expect(profiles.get("actor#1")).toEqual({ kind: "mono", schema: schemas["ask#1"] });
  51. // 同一 T 在两个站点各合成一次:引用不同、内容相同,按内容判等。
  52. expect(schemas["ask#1"]).not.toBe(schemas["ask#3"]);
  53. expect(schemas["ask#1"]).toEqual(schemas["ask#3"]);
  54. });
  55. it("two typed asks with different T make the actor generic", () => {
  56. const { profiles } = profilesOf(
  57. [
  58. `interface Plan { done: boolean }`,
  59. `interface Review { ok: boolean; notes: string }`,
  60. `const a = agent("a");`,
  61. `const p = await a.ask<Plan>("plan");`,
  62. `const r = await a.ask<Review>("review");`,
  63. `return [p, r];`,
  64. ].join("\n"),
  65. );
  66. expect(profiles.get("actor#1")).toEqual(GENERIC_SUBMIT_PROFILE);
  67. });
  68. it("an actor with only untyped asks is untyped (no submit_result)", () => {
  69. const { profiles } = profilesOf(
  70. [
  71. `const a = agent("a");`,
  72. `const x = await a.ask("plain");`,
  73. `const y = await a.ask<string>("still plain " + x);`,
  74. `return y;`,
  75. ].join("\n"),
  76. );
  77. expect(profiles.get("actor#1")).toEqual({ kind: "untyped" });
  78. });
  79. it("independent actors get independent profiles", () => {
  80. const { profiles, schemas } = profilesOf(
  81. [
  82. `interface Plan { done: boolean }`,
  83. `interface Review { ok: boolean }`,
  84. `const planner = agent("planner");`,
  85. `const judge = agent("judge", { system: "You judge." });`,
  86. `const talker = agent("talker");`,
  87. `const p = await planner.ask<Plan>("plan");`,
  88. `const r1 = await judge.ask<Review>("judge " + JSON.stringify(p));`,
  89. `const r2 = await judge.ask<Plan>("re-plan " + JSON.stringify(r1));`,
  90. `const t = await talker.ask("summarize " + JSON.stringify(r2));`,
  91. `return t;`,
  92. ].join("\n"),
  93. );
  94. expect(profiles.get("actor#1")).toEqual({ kind: "mono", schema: schemas["ask#1"] });
  95. expect(profiles.get("actor#2")).toEqual(GENERIC_SUBMIT_PROFILE);
  96. expect(profiles.get("actor#3")).toEqual({ kind: "untyped" });
  97. expect([...profiles.keys()].sort()).toEqual(["actor#1", "actor#2", "actor#3"]);
  98. });
  99. it("a fan-out lane family is one actor site and stays mono across iterations", () => {
  100. const { profiles, schemas } = profilesOf(
  101. [
  102. `interface Finding { file: string; ok: boolean }`,
  103. `const files = await world.files.glob("src/**/*.ts");`,
  104. `const results = await Promise.all(files.map((file) => agent("rev-" + file).ask<Finding>("review " + file)));`,
  105. `return results;`,
  106. ].join("\n"),
  107. );
  108. expect(profiles.get("actor#1")).toEqual({ kind: "mono", schema: schemas["ask#1"] });
  109. });
  110. it("a receiver that may be either of two actors contributes its schema to both", () => {
  111. const { profiles, schemas } = profilesOf(
  112. [
  113. `interface Plan { done: boolean }`,
  114. `const a = agent("a");`,
  115. `const b = agent("b");`,
  116. `const files = await world.files.glob("src/**/*.ts");`,
  117. `const who = files.length > 3 ? a : b;`,
  118. `const p = await who.ask<Plan>("plan");`,
  119. `return p;`,
  120. ].join("\n"),
  121. );
  122. expect(profiles.get("actor#1")).toEqual({ kind: "mono", schema: schemas["ask#1"] });
  123. expect(profiles.get("actor#2")).toEqual({ kind: "mono", schema: schemas["ask#1"] });
  124. });
  125. });
  126. describe("actor submit profiles — soundness on the pure projection", () => {
  127. const schemaA = { type: "object", properties: { a: { type: "string" } }, required: ["a"] };
  128. const schemaB = { type: "object", properties: { b: { type: "number" } }, required: ["b"] };
  129. const graphWith = (nodes: SiteGraph["nodes"]): SiteGraph => ({
  130. actors: [
  131. { id: "actor#1", loc: { column: 1, line: 1 }, order: 0 },
  132. { id: "actor#2", loc: { column: 1, line: 2 }, order: 1 },
  133. ] as SiteGraph["actors"],
  134. edges: [],
  135. nodes,
  136. });
  137. const ask = (id: string, actors: string[] | undefined): SiteGraph["nodes"][number] =>
  138. ({
  139. id,
  140. kind: "ask",
  141. label: id,
  142. loc: { column: 1, line: 3 },
  143. order: 2,
  144. ...(actors === undefined ? {} : { actors }),
  145. }) as SiteGraph["nodes"][number];
  146. const specs = (entries: [string, AskSpec][]) => new Map<string, AskSpec>(entries);
  147. it("one unresolved ask makes every actor generic, even the ones with a clean picture", () => {
  148. const profiles = deriveActorSubmitProfiles(
  149. graphWith([ask("ask#1", ["actor#1"]), ask("ask#2", undefined)]),
  150. specs([
  151. ["ask#1", { typed: true, schema: schemaA }],
  152. ["ask#2", { typed: true, schema: schemaB }],
  153. ]),
  154. );
  155. expect(profiles.get("actor#1")).toEqual(GENERIC_SUBMIT_PROFILE);
  156. expect(profiles.get("actor#2")).toEqual(GENERIC_SUBMIT_PROFILE);
  157. });
  158. it("a missing ask spec is a wiring error and also falls back to generic for all", () => {
  159. const profiles = deriveActorSubmitProfiles(graphWith([ask("ask#1", ["actor#1"])]), specs([]));
  160. expect(profiles.get("actor#1")).toEqual(GENERIC_SUBMIT_PROFILE);
  161. expect(profiles.get("actor#2")).toEqual(GENERIC_SUBMIT_PROFILE);
  162. });
  163. it("schema equality is by canonical content, not by key order or identity", () => {
  164. const reordered = { required: ["a"], properties: { a: { type: "string" } }, type: "object" };
  165. const profiles = deriveActorSubmitProfiles(
  166. graphWith([ask("ask#1", ["actor#1"]), ask("ask#2", ["actor#1"])]),
  167. specs([
  168. ["ask#1", { typed: true, schema: schemaA }],
  169. ["ask#2", { typed: true, schema: reordered }],
  170. ]),
  171. );
  172. expect(profiles.get("actor#1")).toEqual({ kind: "mono", schema: schemaA });
  173. expect(profiles.get("actor#2")).toEqual({ kind: "untyped" });
  174. });
  175. it("an actor that no ask reaches is untyped", () => {
  176. const profiles = deriveActorSubmitProfiles(graphWith([]), specs([]));
  177. expect(profiles.get("actor#1")).toEqual({ kind: "untyped" });
  178. expect(profiles.get("actor#2")).toEqual({ kind: "untyped" });
  179. });
  180. });