convergence.test.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { describe, expect, it } from "vitest";
  2. import { analyzeWorkflowScript } from "../src/index.js";
  3. import { corpusDiagnostics } from "./helpers/analysis-corpus.js";
  4. /**
  5. * Fixpoint convergence pins (docs/analysis.md, "Convergence": the canonical-heap rule).
  6. *
  7. * The taint fixpoint is gen-only over a finite lattice, so the only way it fails to settle
  8. * is a transfer that reports a change every pass without the state growing: a persistent
  9. * merge INTO a value that is recreated each pass. Each case here is a shape that once did
  10. * exactly that. The assertion is the analysis completing at all — `analyzeWorkflowScript`
  11. * throws at ITERATION_CAP — plus, where the fix changed what the graph can see, the fact it
  12. * now sees.
  13. */
  14. function analyze(script: string) {
  15. const result = analyzeWorkflowScript(script);
  16. expect(corpusDiagnostics(result.diagnostics)).toEqual([]);
  17. const causality = result.causality;
  18. if (causality === undefined) throw new Error("expected a causality graph");
  19. return { causality, result };
  20. }
  21. const hasEdge = (edges: ReadonlyArray<{ from: string; to: string }>, from: string, to: string): boolean =>
  22. edges.some((edge) => edge.from === from && edge.to === to);
  23. describe("convergence — container literals are allocation-site places", () => {
  24. it("converges when a callee writes a NEW field through a parameter bound to an inline object literal", () => {
  25. // The minimal shape: nothing here is exotic. Before literals had a persistent place this
  26. // failed on every analyzer generation since 2026-08-17.
  27. analyze(`
  28. function tag(box: { name: string; note?: string }, t: string): void { box.note = t; }
  29. const t = await agent("s").ask<string>("q");
  30. tag({ name: "x" }, t);
  31. return t;
  32. `);
  33. });
  34. it("converges for an inline array literal mutated by the callee", () => {
  35. analyze(`
  36. function fill(xs: string[], t: string): void { xs.push(t); }
  37. const t = await agent("s").ask<string>("q");
  38. fill([], t);
  39. return t;
  40. `);
  41. });
  42. it("converges when the write-back carries no taint at all", () => {
  43. // The re-reported change was the write-back's own doing, not the data it carried.
  44. analyze(`
  45. function tag(box: { name: string; note?: string }, t: string): void { box.note = t; }
  46. tag({ name: "x" }, "plain");
  47. return await agent("s").ask<string>("q");
  48. `);
  49. });
  50. it("converges for a nested literal argument written through two levels", () => {
  51. analyze(`
  52. function deep(p: { inner: { items: string[] } }, t: string): void { p.inner.items.push(t); }
  53. const t = await agent("s").ask<string>("q");
  54. deep({ inner: { items: [] } }, t);
  55. return t;
  56. `);
  57. });
  58. it("makes the field written on the literal visible through the reference the callee returns", () => {
  59. // Persistence is not only about termination: the note now survives to the reader.
  60. const { causality } = analyze(`
  61. function tag(box: { name: string; note?: string }, t: string): { name: string; note?: string } {
  62. box.note = t;
  63. return box;
  64. }
  65. const t = await agent("writer").ask<string>("draft");
  66. const r = tag({ name: "x" }, t);
  67. return agent("reader").ask<string>(\`use \${r.note}\`);
  68. `);
  69. expect(hasEdge(causality.edges, "ask#1", "ask#2")).toBe(true);
  70. });
  71. it("keeps a literal's field that stores a live place aliased to that place", () => {
  72. // `{ box }` stores box's slot itself; the persistent literal place must not turn that alias
  73. // into a copy on later passes (heap-aliasing-param-literal-wrap pins the golden).
  74. const { causality } = analyze(`
  75. const box: { f: string } = { f: "" };
  76. async function poke(p: { box: { f: string } }): Promise<void> {
  77. p.box.f = await agent("writer").ask<string>("secret");
  78. }
  79. await poke({ box });
  80. return agent("reader").ask<string>(\`use \${box.f}\`);
  81. `);
  82. expect(hasEdge(causality.edges, "ask#1", "ask#2")).toBe(true);
  83. });
  84. it("re-evaluates the literal every pass (a site inside it is never short-circuited)", () => {
  85. // The place is reached only through evaluation, never through resolvePlace, so the ask
  86. // nested in the literal keeps its node and its edge.
  87. const { causality } = analyze(`
  88. const seed = await agent("seed").ask<string>("s");
  89. const bundle = { text: await agent("writer").ask<string>(seed), tag: "x" };
  90. return agent("reader").ask<string>(bundle.text);
  91. `);
  92. expect(hasEdge(causality.edges, "ask#1", "ask#2")).toBe(true);
  93. expect(hasEdge(causality.edges, "ask#2", "ask#3")).toBe(true);
  94. });
  95. });
  96. describe("convergence — the keystone-leak family (recorded real scripts, 2026-09-03)", () => {
  97. // Seven recorded workflows stopped converging when a library member call started dispatching
  98. // on tracked functions carried by its receiver: the keystone pot had leaked the callback
  99. // itself into the result of `sort(cmp)` / `JSON.parse(text, reviver)`, so a later
  100. // `top.push({ … })` applied the callback with the literal as its actual and hit the
  101. // write-back above. Two independent tightenings close the path (the pot strips callables,
  102. // library members never dispatch on receiver fns); the literal place removes the cliff.
  103. it("sort comparator, then push of a literal into the sorted result", () => {
  104. analyze(`
  105. interface C { name: string; confidence: number }
  106. const candidates = await agent("s").ask<C[]>("q");
  107. const top = candidates.sort((a, b) => b.confidence - a.confidence);
  108. top.push({ name: "x", confidence: 0.5 });
  109. return top;
  110. `);
  111. });
  112. it("JSON.parse reviver (no registry entry), then push of a literal", () => {
  113. analyze(`
  114. interface C { name: string; confidence: number }
  115. const text = await agent("s").ask<string>("q");
  116. const top = JSON.parse(text, (k, v) => v) as C[];
  117. top.push({ name: "x", confidence: 0.5 });
  118. return top;
  119. `);
  120. });
  121. it("String.replace callback, then a literal pushed into a sibling field", () => {
  122. analyze(`
  123. interface C { name: string; confidence: number }
  124. const text = await agent("s").ask<string>("q");
  125. const holder = { items: [] as C[], text: text.replace(/x/g, (m) => m.toUpperCase()) };
  126. holder.items.push({ name: holder.text, confidence: 0.5 });
  127. return holder;
  128. `);
  129. });
  130. });