registry.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import ts from "typescript";
  2. import { describe, expect, it } from "vitest";
  3. import { createWorkflowProgram } from "../src/compiler/compile.js";
  4. import { collectSites } from "../src/analysis/sites.js";
  5. import {
  6. facadeMemberOf,
  7. isSiteProducing,
  8. SITE_MEMBER_NAMES,
  9. siteProducingFunctionOfSymbol,
  10. WORLD_READ_REGISTRY,
  11. worldReadOp,
  12. worldReadOpOfSymbol,
  13. } from "../src/facade/registry.js";
  14. /**
  15. * world-read 注册表:(facade 容器, 成员) → op 的唯一真源(docs/execution-engine.md 的
  16. * "The world-read registry")。这里钉住的核心性质是**身份按声明容器判定,不按裸名字**——
  17. * `git.log` 与顶层 `log()` 撞名,这是下一步加 `git.*` 时唯一会伤人的地方。
  18. */
  19. /** 编译一份脚本,返回一个"按名字找到该标识符/属性名节点的 symbol"的查询器。 */
  20. function symbols(scriptText: string): (name: string) => ts.Symbol | undefined {
  21. const { program, scriptFile } = createWorkflowProgram(scriptText);
  22. const checker = program.getTypeChecker();
  23. const found = new Map<string, ts.Symbol>();
  24. const walk = (node: ts.Node): void => {
  25. if (ts.isIdentifier(node)) {
  26. const symbol = checker.getSymbolAtLocation(node);
  27. if (symbol !== undefined && !found.has(node.text)) found.set(node.text, symbol);
  28. }
  29. ts.forEachChild(node, walk);
  30. };
  31. walk(scriptFile);
  32. return (name) => found.get(name);
  33. }
  34. describe("world-read registry — pure lookup", () => {
  35. it("keys on (container, member), so a bare member name never resolves alone", () => {
  36. expect(worldReadOp("files", "glob")).toBe("glob");
  37. expect(worldReadOp("files", "read")).toBe("read");
  38. expect(worldReadOp("files", "grep")).toBe("grep");
  39. expect(worldReadOp("git", "changedFiles")).toBe("git-changed-files");
  40. expect(worldReadOp("git", "diff")).toBe("git-diff");
  41. expect(worldReadOp("git", "status")).toBe("git-status");
  42. expect(worldReadOp("git", "log")).toBe("git-log");
  43. // 无容器(顶层函数)永不命中:world-read 一律挂在 facade 容器对象上。
  44. expect(worldReadOp(undefined, "glob")).toBeUndefined();
  45. // 同名成员挂在别的容器上就不是同一个 op —— 这正是 `git.log` vs 顶层 `log()` 的形状。
  46. expect(worldReadOp("git", "glob")).toBeUndefined();
  47. expect(worldReadOp("files", "log")).toBeUndefined();
  48. expect(worldReadOp("files", "nope")).toBeUndefined();
  49. });
  50. it("gives every op a distinct name and every member a distinct one too", () => {
  51. // op 名唯一:它是 journal 的 inputHash 与 Boundary A 的线上词汇表。
  52. const ops = WORLD_READ_REGISTRY.map((row) => row.op);
  53. expect(new Set(ops).size).toBe(ops.length);
  54. // 成员名跨容器唯一:facade-misuse 的 retyping 关卡按裸名字反查候选,只为把名字填进
  55. // 诊断文案。重名不会让判定出错,但会让文案引出的名字二义(见 facade-misuse 的
  56. // facadeSiteMember 注释)。破坏它就得把容器也穿进那一层。
  57. const members = WORLD_READ_REGISTRY.map((row) => row.member);
  58. expect(new Set(members).size).toBe(members.length);
  59. });
  60. it("derives the site-producing member set from the table (plus ask)", () => {
  61. for (const row of WORLD_READ_REGISTRY) {
  62. expect(SITE_MEMBER_NAMES.has(row.member)).toBe(true);
  63. // 每一行的 op 都能被 (容器, 成员) 反查出来 —— 表就是唯一真源。
  64. expect(worldReadOp(row.container, row.member)).toBe(row.op);
  65. }
  66. expect(SITE_MEMBER_NAMES.has("ask")).toBe(true);
  67. // `"log"` **在**这个集合里,因为 `git.log` 在表里——而顶层 `log()` 依然不产生站点。
  68. // 这一条就是这份裸名字清单只能当候选键、绝不能当身份依据的证明。
  69. expect(SITE_MEMBER_NAMES.has("log")).toBe(true);
  70. expect(isSiteProducing(undefined, "log")).toBe(false);
  71. });
  72. it("treats report as a site-producing top-level function and log as not", () => {
  73. // report 与 log 都是**顶层无容器**的 facade 函数,分野只有一条:report 有一行按站点
  74. // 建键的 journal,所以 facade-siting 规则(只许直接调用)必须约束它。
  75. expect(isSiteProducing(undefined, "report")).toBe(true);
  76. expect(isSiteProducing(undefined, "log")).toBe(false);
  77. expect(siteProducingFunctionOfSymbol(undefined)).toBeUndefined();
  78. // report 不是 world-read,也不是任何容器的成员——不该出现在那两张表里。
  79. expect(worldReadOp(undefined, "report")).toBeUndefined();
  80. expect(SITE_MEMBER_NAMES.has("report")).toBe(false);
  81. });
  82. it("treats agent/Agent.ask as site-producing and log as not", () => {
  83. expect(isSiteProducing(undefined, "agent")).toBe(true);
  84. expect(isSiteProducing("Agent", "ask")).toBe(true);
  85. expect(isSiteProducing("files", "glob")).toBe(true);
  86. expect(isSiteProducing("files", "grep")).toBe(true);
  87. expect(isSiteProducing("git", "log")).toBe(true);
  88. expect(isSiteProducing(undefined, "log")).toBe(false);
  89. // 关键防线:一个**没有容器**的 `log` 不产生站点。`git.log` 已经落地,所以这一条正在
  90. // 现役地阻止"每条进度消息都铸出一个 world-read 站点"。
  91. expect(worldReadOp(undefined, "log")).toBeUndefined();
  92. });
  93. });
  94. describe("world-read registry — declaration-based resolution", () => {
  95. const lookup = symbols(
  96. `const paths = await files.glob("src/**");\n` +
  97. `const body = await files.read(paths[0] ?? "x");\n` +
  98. `log(body);\n` +
  99. `const out = await agent("a").ask<string>("go");\n` +
  100. `return out;\n`,
  101. );
  102. it("resolves a facade member's declaring container from its declaration", () => {
  103. expect(facadeMemberOf(lookup("glob"))).toEqual({ container: "files", member: "glob" });
  104. expect(facadeMemberOf(lookup("read"))).toEqual({ container: "files", member: "read" });
  105. // `ask` 声明在 `declare interface Agent` 里 → 容器是 Agent(接口容器)。
  106. expect(facadeMemberOf(lookup("ask"))).toEqual({ container: "Agent", member: "ask" });
  107. // 顶层 facade 函数无容器 —— 这就是 `git.log` 不会与它相撞的原因。
  108. expect(facadeMemberOf(lookup("log"))).toEqual({ container: undefined, member: "log" });
  109. expect(facadeMemberOf(lookup("agent"))).toEqual({ container: undefined, member: "agent" });
  110. });
  111. it("resolves the two bare site-producing functions and rejects log", () => {
  112. const fns = symbols(
  113. `report({ a: 1 });\nlog("x");\nconst out = await agent("a").ask<string>("go");\nreturn out;\n`,
  114. );
  115. expect(siteProducingFunctionOfSymbol(fns("report"))).toBe("report");
  116. expect(siteProducingFunctionOfSymbol(fns("agent"))).toBe("agent");
  117. expect(siteProducingFunctionOfSymbol(fns("log"))).toBeUndefined();
  118. // 容器成员走另一条路(worldReadOp / ASK_MEMBER),不该从这个入口命中。
  119. expect(siteProducingFunctionOfSymbol(fns("ask"))).toBeUndefined();
  120. });
  121. it("does not resolve a script-local function named report", () => {
  122. const local = symbols(
  123. `const report = (item: unknown): void => { void item; };\nreport({ a: 1 });\nreturn 1;\n`,
  124. );
  125. expect(siteProducingFunctionOfSymbol(local("report"))).toBeUndefined();
  126. expect(facadeMemberOf(local("report"))).toBeUndefined();
  127. });
  128. it("maps only facade-declared members to ops", () => {
  129. expect(worldReadOpOfSymbol(lookup("glob"))).toBe("glob");
  130. expect(worldReadOpOfSymbol(lookup("read"))).toBe("read");
  131. expect(worldReadOpOfSymbol(lookup("log"))).toBeUndefined();
  132. expect(worldReadOpOfSymbol(lookup("ask"))).toBeUndefined();
  133. expect(worldReadOpOfSymbol(undefined)).toBeUndefined();
  134. });
  135. it("resolves the two `log` declarations to different identities in one script", () => {
  136. // 同一份脚本里两个 `log`:`git.log` 的容器是 git(→ "git-log" op),顶层 `log` 无容器
  137. // (→ 不是 world-read)。symbols() 的查询器按名字取**首次**出现的 symbol,所以两侧要
  138. // 分开取:先只放顶层 log,再只放 git.log。
  139. const topLevel = symbols(`log("progress");\nreturn 1;\n`);
  140. expect(facadeMemberOf(topLevel("log"))).toEqual({ container: undefined, member: "log" });
  141. expect(worldReadOpOfSymbol(topLevel("log"))).toBeUndefined();
  142. const gitLog = symbols(`const c = await git.log(3);\nreturn c.length;\n`);
  143. expect(facadeMemberOf(gitLog("log"))).toEqual({ container: "git", member: "log" });
  144. expect(worldReadOpOfSymbol(gitLog("log"))).toBe("git-log");
  145. });
  146. it("resolves the whole git container and files.grep from declarations", () => {
  147. const git = symbols(
  148. `const changed = await git.changedFiles();\n` +
  149. `const d = await git.diff("HEAD");\n` +
  150. `const s = await git.status();\n` +
  151. `const hits = await files.grep("TODO", "*.ts");\n` +
  152. `return [changed.length, d.length, s.clean, hits.length];\n`,
  153. );
  154. expect(worldReadOpOfSymbol(git("changedFiles"))).toBe("git-changed-files");
  155. expect(worldReadOpOfSymbol(git("diff"))).toBe("git-diff");
  156. expect(worldReadOpOfSymbol(git("status"))).toBe("git-status");
  157. expect(worldReadOpOfSymbol(git("grep"))).toBe("grep");
  158. expect(facadeMemberOf(git("grep"))).toEqual({ container: "files", member: "grep" });
  159. });
  160. it("does not resolve a script-local object whose members merely share the names", () => {
  161. const local = symbols(
  162. `const fake = { glob(p: string): string[] { return [p]; }, read(p: string): string { return p; } };\n` +
  163. `const out = fake.glob("x").join(fake.read("y"));\n` +
  164. `return out;\n`,
  165. );
  166. expect(facadeMemberOf(local("glob"))).toBeUndefined();
  167. expect(worldReadOpOfSymbol(local("glob"))).toBeUndefined();
  168. expect(worldReadOpOfSymbol(local("read"))).toBeUndefined();
  169. });
  170. it("does not resolve a script-local `git`-shaped object (shadowing the facade)", () => {
  171. // 一个自建的 `myGit.status()`:名字与容器形状都对得上,但声明不在 facade .d.ts 里。
  172. const local = symbols(
  173. `const myGit = { status(): string { return "clean"; }, log(n: number): number { return n; } };\n` +
  174. `return myGit.status() + String(myGit.log(1));\n`,
  175. );
  176. expect(worldReadOpOfSymbol(local("status"))).toBeUndefined();
  177. expect(worldReadOpOfSymbol(local("log"))).toBeUndefined();
  178. });
  179. });
  180. describe("world-read registry — site collection uses it", () => {
  181. it("sites facade world reads with the registry op, not the member name", () => {
  182. const table = collectSites(
  183. createWorkflowProgram(
  184. `const paths = await files.glob("src/**");\n` +
  185. `const body = await files.read(paths[0] ?? "x");\n` +
  186. `return body;\n`,
  187. ),
  188. );
  189. expect(table.worldReads.map((site) => site.op)).toEqual(["glob", "read"]);
  190. expect(table.worldReads.map((site) => site.id)).toEqual(["world-read#1", "world-read#2"]);
  191. // 实参按位置收集(多参 op 的接缝;1 参 op 就是单元素数组)。
  192. expect(table.worldReads[0]?.args).toHaveLength(1);
  193. });
  194. it("sites every new op on one shared world-read counter, args packed positionally", () => {
  195. const table = collectSites(
  196. createWorkflowProgram(
  197. `const hits = await files.grep("TODO", "*.ts");\n` +
  198. `const changed = await git.changedFiles();\n` +
  199. `const d = await git.diff("main", "src/a.ts");\n` +
  200. `const s = await git.status();\n` +
  201. `const c = await git.log(5);\n` +
  202. `return [hits.length, changed.length, d.length, s.clean, c.length];\n`,
  203. ),
  204. );
  205. expect(table.worldReads.map((site) => site.op)).toEqual([
  206. "grep",
  207. "git-changed-files",
  208. "git-diff",
  209. "git-status",
  210. "git-log",
  211. ]);
  212. // 一个**共享的** per-kind 计数器(`world-read#N` 带 op 字段),不是 `grep#N`/`git-log#N`。
  213. expect(table.worldReads.map((site) => site.id)).toEqual([
  214. "world-read#1",
  215. "world-read#2",
  216. "world-read#3",
  217. "world-read#4",
  218. "world-read#5",
  219. ]);
  220. // 元数按位置原样收集:可选实参缺席就是更短的数组(journal 键因此可区分)。
  221. expect(table.worldReads.map((site) => site.args.length)).toEqual([2, 0, 2, 0, 1]);
  222. });
  223. it("mints no world-read site for a same-named script-local method", () => {
  224. const table = collectSites(
  225. createWorkflowProgram(
  226. `const fake = { glob(p: string): string[] { return [p]; } };\n` +
  227. `const out = fake.glob("x");\n` +
  228. `return out.length;\n`,
  229. ),
  230. );
  231. expect(table.worldReads).toEqual([]);
  232. });
  233. it("mints a site for git.log and none for the top-level log, in one script", () => {
  234. // 这是注册表按容器建键的可执行证明。裸名字集合的两种下场都会在这里露出来:
  235. // 要么 `log("progress")` 也被铸成 world-read 站点(站点数 2),要么 `git.log` 一个站点
  236. // 都没有(站点数 0)——而没有站点的 facade 调用就没有 journal 键。
  237. const table = collectSites(
  238. createWorkflowProgram(
  239. `log("progress");\n` +
  240. `const c = await git.log(3);\n` +
  241. `log("done");\n` +
  242. `const out = await agent("a").ask<string>(String(c.length));\n` +
  243. `return out;\n`,
  244. ),
  245. );
  246. expect(table.worldReads).toHaveLength(1);
  247. expect(table.worldReads[0]?.op).toBe("git-log");
  248. expect(table.worldReads[0]?.id).toBe("world-read#1");
  249. expect(table.asks).toHaveLength(1);
  250. });
  251. it("mints no world-read site for the top-level log alone", () => {
  252. const table = collectSites(
  253. createWorkflowProgram(
  254. `log("progress");\nconst out = await agent("a").ask<string>("go");\nreturn out;\n`,
  255. ),
  256. );
  257. expect(table.worldReads).toEqual([]);
  258. expect(table.asks).toHaveLength(1);
  259. });
  260. });