@granular-software/fractory 0.0.0-stage → 0.1.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (273) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +28 -2
  3. package/dist/activity-volume.d.ts +18 -0
  4. package/dist/activity-volume.js +94 -0
  5. package/dist/annotations.d.ts +118 -0
  6. package/dist/annotations.js +304 -0
  7. package/dist/benchmark-cli.d.ts +1 -0
  8. package/dist/benchmark-cli.js +95 -0
  9. package/dist/benchmark-worker.d.ts +2 -0
  10. package/dist/benchmark-worker.js +22 -0
  11. package/dist/benchmarks.d.ts +19 -0
  12. package/dist/benchmarks.js +414 -0
  13. package/dist/bin.d.ts +2 -0
  14. package/dist/bin.js +63 -0
  15. package/dist/block-cli.d.ts +1 -0
  16. package/dist/block-cli.js +88 -0
  17. package/dist/block-documents.d.ts +35 -0
  18. package/dist/block-documents.js +84 -0
  19. package/dist/change-impact.d.ts +55 -0
  20. package/dist/change-impact.js +223 -0
  21. package/dist/check-source.d.ts +2 -0
  22. package/dist/check-source.js +66 -0
  23. package/dist/checks-model.d.ts +64 -0
  24. package/dist/checks-model.js +36 -0
  25. package/dist/cli.d.ts +2 -0
  26. package/dist/cli.js +359 -0
  27. package/dist/code-model.d.ts +38 -0
  28. package/dist/code-model.js +94 -0
  29. package/dist/code-store.d.ts +9 -0
  30. package/dist/code-store.js +79 -0
  31. package/dist/compare-artifact.d.ts +6 -0
  32. package/dist/compare-artifact.js +277 -0
  33. package/dist/compare.d.ts +81 -0
  34. package/dist/compare.js +291 -0
  35. package/dist/compiler.d.ts +10 -0
  36. package/dist/compiler.js +1083 -0
  37. package/dist/compose.d.ts +9 -0
  38. package/dist/compose.js +72 -0
  39. package/dist/core.d.ts +86 -0
  40. package/dist/core.js +206 -0
  41. package/dist/coverage-cli.d.ts +1 -0
  42. package/dist/coverage-cli.js +42 -0
  43. package/dist/declaration-sources.d.ts +3 -0
  44. package/dist/declaration-sources.js +51 -0
  45. package/dist/declared-graph.d.ts +3 -0
  46. package/dist/declared-graph.js +66 -0
  47. package/dist/display-name.d.ts +2 -0
  48. package/dist/display-name.js +8 -0
  49. package/dist/error-heat.d.ts +58 -0
  50. package/dist/error-heat.js +96 -0
  51. package/dist/evidence.d.ts +3 -0
  52. package/dist/evidence.js +68 -0
  53. package/dist/factory-graph.d.ts +4 -0
  54. package/dist/factory-graph.js +318 -0
  55. package/dist/factory-model.d.ts +97 -0
  56. package/dist/factory-model.js +1 -0
  57. package/dist/git-code.d.ts +47 -0
  58. package/dist/git-code.js +352 -0
  59. package/dist/git-refs.d.ts +4 -0
  60. package/dist/git-refs.js +37 -0
  61. package/dist/history-activity.d.ts +4 -0
  62. package/dist/history-activity.js +129 -0
  63. package/dist/history-churn.d.ts +36 -0
  64. package/dist/history-churn.js +302 -0
  65. package/dist/history-git.d.ts +21 -0
  66. package/dist/history-git.js +125 -0
  67. package/dist/history-ledger.d.ts +43 -0
  68. package/dist/history-ledger.js +450 -0
  69. package/dist/history-ownership.d.ts +10 -0
  70. package/dist/history-ownership.js +75 -0
  71. package/dist/history-source.d.ts +13 -0
  72. package/dist/history-source.js +100 -0
  73. package/dist/integrations-cli.d.ts +2 -0
  74. package/dist/integrations-cli.js +159 -0
  75. package/dist/issue-cache.d.ts +9 -0
  76. package/dist/issue-cache.js +68 -0
  77. package/dist/issue-cli.d.ts +1 -0
  78. package/dist/issue-cli.js +89 -0
  79. package/dist/issue-links.d.ts +15 -0
  80. package/dist/issue-links.js +125 -0
  81. package/dist/linear-issues.d.ts +19 -0
  82. package/dist/linear-issues.js +124 -0
  83. package/dist/local-runtime.d.ts +8 -0
  84. package/dist/local-runtime.js +59 -0
  85. package/dist/map-activity.d.ts +73 -0
  86. package/dist/map-activity.js +234 -0
  87. package/dist/map-model.d.ts +5 -0
  88. package/dist/map-model.js +166 -0
  89. package/dist/map-quality.d.ts +14 -0
  90. package/dist/map-quality.js +56 -0
  91. package/dist/mapping-coverage.d.ts +87 -0
  92. package/dist/mapping-coverage.js +404 -0
  93. package/dist/model.d.ts +182 -0
  94. package/dist/model.js +66 -0
  95. package/dist/operation-benchmark-worker.d.ts +2 -0
  96. package/dist/operation-benchmark-worker.js +60 -0
  97. package/dist/operation-benchmark.d.ts +29 -0
  98. package/dist/operation-benchmark.js +1 -0
  99. package/dist/otel-metrics.d.ts +21 -0
  100. package/dist/otel-metrics.js +146 -0
  101. package/dist/performance-model.d.ts +185 -0
  102. package/dist/performance-model.js +396 -0
  103. package/dist/product-map.d.ts +69 -0
  104. package/dist/product-map.js +168 -0
  105. package/dist/provider-access.cjs +207 -0
  106. package/dist/provider-access.d.cts +14 -0
  107. package/dist/reports-cli.d.ts +1 -0
  108. package/dist/reports-cli.js +111 -0
  109. package/dist/reports-model.d.ts +27 -0
  110. package/dist/reports-model.js +39 -0
  111. package/dist/reports.d.ts +23 -0
  112. package/dist/reports.js +223 -0
  113. package/dist/repository-map.d.ts +22 -0
  114. package/dist/repository-map.js +151 -0
  115. package/dist/resource-cache.d.ts +17 -0
  116. package/dist/resource-cache.js +65 -0
  117. package/dist/review-cli.d.ts +19 -0
  118. package/dist/review-cli.js +224 -0
  119. package/dist/review.d.ts +48 -0
  120. package/dist/review.js +289 -0
  121. package/dist/runtime-metrics.d.ts +26 -0
  122. package/dist/runtime-metrics.js +290 -0
  123. package/dist/scope.d.ts +12 -0
  124. package/dist/scope.js +94 -0
  125. package/dist/sentry-activity.d.ts +39 -0
  126. package/dist/sentry-activity.js +174 -0
  127. package/dist/sentry.d.ts +130 -0
  128. package/dist/sentry.js +369 -0
  129. package/dist/service-release.d.ts +16 -0
  130. package/dist/service-release.js +14 -0
  131. package/dist/setup-cli.d.ts +2 -0
  132. package/dist/setup-cli.js +52 -0
  133. package/dist/setup-kit.cjs +163 -0
  134. package/dist/setup-kit.d.cts +32 -0
  135. package/dist/source-index.d.ts +37 -0
  136. package/dist/source-index.js +166 -0
  137. package/dist/source-review-git.d.ts +7 -0
  138. package/dist/source-review-git.js +103 -0
  139. package/dist/source-review.d.ts +16 -0
  140. package/dist/source-review.js +493 -0
  141. package/dist/telemetry.d.ts +17 -0
  142. package/dist/telemetry.js +51 -0
  143. package/dist/time-window.d.ts +9 -0
  144. package/dist/time-window.js +35 -0
  145. package/dist/trace-evidence.d.ts +23 -0
  146. package/dist/trace-evidence.js +55 -0
  147. package/dist/trace-model.d.ts +26 -0
  148. package/dist/trace-model.js +1 -0
  149. package/dist/tracing.d.ts +44 -0
  150. package/dist/tracing.js +123 -0
  151. package/dist/work-check.d.ts +17 -0
  152. package/dist/work-check.js +110 -0
  153. package/dist/work-cli.d.ts +1 -0
  154. package/dist/work-cli.js +148 -0
  155. package/dist/work-context.d.ts +57 -0
  156. package/dist/work-context.js +568 -0
  157. package/dist/work-intents.d.ts +6 -0
  158. package/dist/work-intents.js +140 -0
  159. package/dist/work-items.d.ts +58 -0
  160. package/dist/work-items.js +86 -0
  161. package/dist/work-model.d.ts +110 -0
  162. package/dist/work-model.js +261 -0
  163. package/dist/work-review-git.d.ts +8 -0
  164. package/dist/work-review-git.js +111 -0
  165. package/dist/work-review.d.ts +26 -0
  166. package/dist/work-review.js +426 -0
  167. package/dist/work-switcher-model.d.ts +43 -0
  168. package/dist/work-switcher-model.js +79 -0
  169. package/dist/work-switcher.d.ts +6 -0
  170. package/dist/work-switcher.js +171 -0
  171. package/dist/working-cli.d.ts +1 -0
  172. package/dist/working-cli.js +33 -0
  173. package/dist/working-code.d.ts +74 -0
  174. package/dist/working-code.js +421 -0
  175. package/dist/workspace-notes.d.ts +71 -0
  176. package/dist/workspace-notes.js +177 -0
  177. package/dist/workspace-tests.d.ts +21 -0
  178. package/dist/workspace-tests.js +573 -0
  179. package/package.json +547 -4
  180. package/src/activity-volume.ts +149 -0
  181. package/src/annotations.ts +481 -0
  182. package/src/benchmark-cli.ts +118 -0
  183. package/src/benchmark-worker.ts +22 -0
  184. package/src/benchmarks.ts +493 -0
  185. package/src/bin.ts +54 -0
  186. package/src/block-cli.ts +106 -0
  187. package/src/block-documents.ts +117 -0
  188. package/src/change-impact.ts +280 -0
  189. package/src/check-source.ts +82 -0
  190. package/src/checks-model.ts +108 -0
  191. package/src/cli.ts +440 -0
  192. package/src/code-model.ts +159 -0
  193. package/src/code-store.ts +98 -0
  194. package/src/compare-artifact.ts +314 -0
  195. package/src/compare.ts +440 -0
  196. package/src/compiler.ts +1326 -0
  197. package/src/compose.ts +94 -0
  198. package/src/core.ts +357 -0
  199. package/src/coverage-cli.ts +52 -0
  200. package/src/declaration-sources.ts +69 -0
  201. package/src/declared-graph.ts +74 -0
  202. package/src/display-name.ts +8 -0
  203. package/src/error-heat.ts +161 -0
  204. package/src/evidence.ts +94 -0
  205. package/src/factory-graph.ts +378 -0
  206. package/src/factory-model.ts +73 -0
  207. package/src/git-code.ts +461 -0
  208. package/src/git-refs.ts +43 -0
  209. package/src/history-activity.ts +146 -0
  210. package/src/history-churn.ts +416 -0
  211. package/src/history-git.ts +163 -0
  212. package/src/history-ledger.ts +636 -0
  213. package/src/history-ownership.ts +116 -0
  214. package/src/history-source.ts +117 -0
  215. package/src/integrations-cli.ts +187 -0
  216. package/src/issue-cache.ts +96 -0
  217. package/src/issue-cli.ts +137 -0
  218. package/src/issue-links.ts +153 -0
  219. package/src/linear-issues.ts +154 -0
  220. package/src/local-runtime.ts +70 -0
  221. package/src/map-activity.ts +309 -0
  222. package/src/map-model.ts +213 -0
  223. package/src/map-quality.ts +61 -0
  224. package/src/mapping-coverage.ts +570 -0
  225. package/src/model.ts +221 -0
  226. package/src/operation-benchmark-worker.ts +60 -0
  227. package/src/operation-benchmark.ts +28 -0
  228. package/src/otel-metrics.ts +176 -0
  229. package/src/performance-model.ts +575 -0
  230. package/src/product-map.ts +236 -0
  231. package/src/provider-access.cjs +207 -0
  232. package/src/provider-access.d.cts +14 -0
  233. package/src/reports-cli.ts +125 -0
  234. package/src/reports-model.ts +73 -0
  235. package/src/reports.ts +276 -0
  236. package/src/repository-map.ts +189 -0
  237. package/src/resource-cache.ts +76 -0
  238. package/src/review-cli.ts +256 -0
  239. package/src/review.ts +376 -0
  240. package/src/runtime-metrics.ts +386 -0
  241. package/src/scope.ts +103 -0
  242. package/src/sentry-activity.ts +234 -0
  243. package/src/sentry.ts +545 -0
  244. package/src/service-release.ts +18 -0
  245. package/src/setup-cli.ts +58 -0
  246. package/src/setup-kit.cjs +163 -0
  247. package/src/setup-kit.d.cts +32 -0
  248. package/src/source-index.ts +205 -0
  249. package/src/source-review-git.ts +129 -0
  250. package/src/source-review.ts +637 -0
  251. package/src/telemetry.ts +68 -0
  252. package/src/time-window.ts +51 -0
  253. package/src/trace-evidence.ts +80 -0
  254. package/src/trace-model.ts +26 -0
  255. package/src/tracing.ts +165 -0
  256. package/src/work-check.ts +143 -0
  257. package/src/work-cli.ts +195 -0
  258. package/src/work-context.ts +713 -0
  259. package/src/work-intents.ts +196 -0
  260. package/src/work-items.ts +151 -0
  261. package/src/work-model.ts +400 -0
  262. package/src/work-review-git.ts +130 -0
  263. package/src/work-review.ts +573 -0
  264. package/src/work-switcher-model.ts +121 -0
  265. package/src/work-switcher.ts +191 -0
  266. package/src/working-cli.ts +38 -0
  267. package/src/working-code.ts +582 -0
  268. package/src/workspace-notes.ts +203 -0
  269. package/src/workspace-tests.ts +654 -0
  270. package/toolkit/GUIDE.md +305 -0
  271. package/toolkit/SKILL.md +131 -0
  272. package/toolkit/bin/fractory +2 -0
  273. package/toolkit/build.json +1 -0
@@ -0,0 +1,106 @@
1
+ import { readRepositoryMap } from "./repository-map";
2
+ /** Block context and checks use the same map and runner as the workspace. */
3
+ import { existsSync, readFileSync } from "node:fs";
4
+ import { resolve, join } from "node:path";
5
+ import type { DefinitionGraph } from "./model";
6
+ import { blockNotes, readBlockNote } from "./workspace-notes";
7
+ import { listReports } from "./reports";
8
+ import { reportsForBlock } from "./reports-model";
9
+ import {
10
+ checkPlan,
11
+ WorkspaceTestRunner,
12
+ readTestSource,
13
+ } from "./workspace-tests";
14
+ export async function runBlock(args: string[]) {
15
+ if (args.includes("--help"))
16
+ return "fractal block [BLOCK_ID] [--docs] [--root PATH]\nfractal tests --block BLOCK_ID [--source | --run] [--file PATH] [--root PATH]";
17
+ const value = (name: string) => {
18
+ const i = args.indexOf(name);
19
+ return i < 0 ? undefined : args[i + 1];
20
+ };
21
+ const root = resolve(value("--root") || process.cwd());
22
+ const graph = readRepositoryMap(root);
23
+ if (!graph)
24
+ throw Error("No Fractory map in this repository. Run fractory setup.");
25
+ const id =
26
+ value("--block") ||
27
+ (args[1]?.startsWith("-") ? undefined : args[1]) ||
28
+ graph.id,
29
+ block = graph.elements.find((n) => n.id === id);
30
+ if (!block)
31
+ throw Error("Unknown block. Use its ID from Copy block in Fractory.");
32
+ const tests = checkPlan(root, graph, id);
33
+ if (args[0] === "tests" && args.includes("--run")) {
34
+ const files = value("--file")
35
+ ? [value("--file")!]
36
+ : tests.filter((t) => t.runner).map((t) => t.file);
37
+ const runner = new WorkspaceTestRunner(root, graph),
38
+ run = runner.start(id, files);
39
+ await new Promise<void>((resolve) => {
40
+ const timer = setInterval(() => {
41
+ if (!["queued", "running"].includes(run.status)) {
42
+ clearInterval(timer);
43
+ resolve();
44
+ }
45
+ }, 100);
46
+ });
47
+ if (run.status !== "passed") process.exitCode = 1;
48
+ return JSON.stringify(run, null, 2);
49
+ }
50
+ if (args[0] === "tests")
51
+ return JSON.stringify(
52
+ {
53
+ block: id,
54
+ suites: tests,
55
+ ...(args.includes("--source")
56
+ ? {
57
+ source: tests.map((t) => readTestSource(root, graph, id, t.file)),
58
+ }
59
+ : {}),
60
+ },
61
+ null,
62
+ 2,
63
+ );
64
+ const documents = blockNotes(graph, id, root);
65
+ const inside = new Set([id]);
66
+ let grew = true;
67
+ while (grew) {
68
+ grew = false;
69
+ for (const node of graph.elements)
70
+ if (node.parent && inside.has(node.parent) && !inside.has(node.id)) {
71
+ inside.add(node.id);
72
+ grew = true;
73
+ }
74
+ }
75
+ const connections = graph.connections.filter(
76
+ (edge) => inside.has(edge.source) || inside.has(edge.target),
77
+ );
78
+ return JSON.stringify(
79
+ {
80
+ definition: graph.revision,
81
+ block,
82
+ inside: graph.elements
83
+ .filter((n) => n.parent === id)
84
+ .map((n) => ({ id: n.id, label: n.label, kind: n.kind })),
85
+ connections: connections.slice(0, 100),
86
+ connectionsOmitted: Math.max(0, connections.length - 100),
87
+ documents,
88
+ reports: reportsForBlock(graph, listReports(root).reports, id),
89
+ tests,
90
+ ...(args.includes("--docs")
91
+ ? {
92
+ documentation: documents.flatMap((d) => {
93
+ try {
94
+ const note = readBlockNote(root, graph, id, d.file);
95
+ return note.revision === null ? [] : [note];
96
+ } catch {
97
+ return [];
98
+ }
99
+ }),
100
+ }
101
+ : {}),
102
+ },
103
+ null,
104
+ 2,
105
+ );
106
+ }
@@ -0,0 +1,117 @@
1
+ import {
2
+ existsSync,
3
+ mkdirSync,
4
+ writeFileSync,
5
+ statSync,
6
+ readFileSync,
7
+ } from "node:fs";
8
+ import { dirname } from "node:path";
9
+ import type { DefinitionGraph } from "./model";
10
+ import {
11
+ blockNotes,
12
+ blockNoteFile,
13
+ checkedPath,
14
+ readBlockNote,
15
+ } from "./workspace-notes";
16
+ export function createBlockDocument(
17
+ root: string,
18
+ graph: DefinitionGraph,
19
+ block: string,
20
+ title: string,
21
+ ) {
22
+ if (!graph.elements.some((n) => n.id === block)) throw Error("Unknown block");
23
+ if (!title.trim() || title.length > 100 || /[\r\n\0]/.test(title))
24
+ throw Error("Use a document title of 1–100 characters.");
25
+ const slug =
26
+ title
27
+ .toLowerCase()
28
+ .normalize("NFKD")
29
+ .replace(/[\u0300-\u036f]/g, "")
30
+ .replace(/[^a-z0-9]+/g, "-")
31
+ .replace(/^-|-$/g, "")
32
+ .slice(0, 60) || "note";
33
+ const directory = blockNoteFile(block).replace(/\.md$/, "");
34
+ if (blockNotes(graph, block, root).filter((d) => !d.inherited).length > 32)
35
+ throw Error("Keep at most 32 note pages per block.");
36
+ const file = directory + "/" + slug + ".md",
37
+ path = checkedPath(root, file, true);
38
+ if (existsSync(path)) throw Error("A note with this title already exists.");
39
+ mkdirSync(dirname(path), { recursive: true });
40
+ checkedPath(root, file, true);
41
+ writeFileSync(path, "# " + title.trim() + "\n\n", {
42
+ flag: "wx",
43
+ mode: 0o600,
44
+ });
45
+ return readBlockNote(root, graph, block, file);
46
+ }
47
+ type IndexedDocument = {
48
+ file: string;
49
+ title: string;
50
+ block: string;
51
+ label: string;
52
+ body: string;
53
+ };
54
+ const indexes = new Map<
55
+ string,
56
+ { until: number; documents: IndexedDocument[]; partial: boolean }
57
+ >();
58
+ export function searchBlockDocuments(
59
+ root: string,
60
+ graph: DefinitionGraph,
61
+ query: string,
62
+ refresh = false,
63
+ ) {
64
+ const term = query.trim().toLowerCase();
65
+ if (term.length < 2 || term.length > 120)
66
+ throw Error("Search with 2–120 characters.");
67
+ const key = root + ":" + graph.revision;
68
+ let entry = indexes.get(key);
69
+ if (!entry || refresh || entry.until < Date.now()) {
70
+ const documents: IndexedDocument[] = [],
71
+ seen = new Set<string>();
72
+ let budget = 8 * 1024 * 1024,
73
+ partial = false;
74
+ outer: for (const block of graph.elements)
75
+ for (const doc of blockNotes(graph, block.id, root)) {
76
+ if (seen.has(doc.file) || doc.inherited) continue;
77
+ seen.add(doc.file);
78
+ try {
79
+ const path = checkedPath(root, doc.file),
80
+ size = statSync(path).size;
81
+ if (size > 128000) continue;
82
+ if (size > budget || documents.length >= 500) {
83
+ partial = true;
84
+ break outer;
85
+ }
86
+ const body = readFileSync(path, "utf8");
87
+ budget -= size;
88
+ documents.push({
89
+ file: doc.file,
90
+ title: /^#\s+(.+)$/m.exec(body)?.[1]?.trim() || doc.label,
91
+ block: block.id,
92
+ label: block.label,
93
+ body,
94
+ });
95
+ } catch {}
96
+ }
97
+ entry = { until: Date.now() + 10000, documents, partial };
98
+ indexes.set(key, entry);
99
+ if (indexes.size > 4) indexes.delete(indexes.keys().next().value!);
100
+ }
101
+ const matches = entry.documents.filter((d) =>
102
+ `${d.title} ${d.label} ${d.body}`.toLowerCase().includes(term),
103
+ );
104
+ return {
105
+ documents: matches.slice(0, 30).map(({ body, ...d }) => {
106
+ const position = body.toLowerCase().indexOf(term);
107
+ return {
108
+ ...d,
109
+ excerpt: body
110
+ .slice(Math.max(0, position - 60), Math.max(0, position - 60) + 200)
111
+ .replace(/\s+/g, " "),
112
+ };
113
+ }),
114
+ more: matches.length > 30,
115
+ partial: entry.partial,
116
+ };
117
+ }
@@ -0,0 +1,280 @@
1
+ import { relatedTests, type Connection, type DefinitionGraph } from "./model";
2
+ import type { WorkingOverview } from "./working-code";
3
+
4
+ /** Evidence about a possible interaction, never evidence that it executed. */
5
+ export function connectionEvidence(edge: Connection) {
6
+ if (edge.id.startsWith("call-site:") && edge.sourceRef)
7
+ return "code" as const;
8
+ if (edge.provenance === "declared") return "declared" as const;
9
+ return "reviewed" as const;
10
+ }
11
+
12
+ export type ImpactReason = {
13
+ id: string;
14
+ distance: number;
15
+ /** The dependency that first reached this consumer. */
16
+ via?: string;
17
+ provider?: string;
18
+ };
19
+ export type ImpactInput =
20
+ | { kind: "block"; id: string }
21
+ | { kind: "files"; paths: string[]; partial?: boolean };
22
+
23
+ export function graphIndex(graph: DefinitionGraph) {
24
+ const byId = new Map(graph.elements.map((node) => [node.id, node]));
25
+ const ancestors = new Map<string, Set<string>>();
26
+ for (const node of graph.elements) {
27
+ const seen = new Set<string>();
28
+ let id = node.id;
29
+ while (byId.has(id) && !seen.has(id)) {
30
+ seen.add(id);
31
+ id = byId.get(id)!.parent || "";
32
+ }
33
+ ancestors.set(node.id, seen);
34
+ }
35
+ const within = (id: string, parent: string) =>
36
+ ancestors.get(id)?.has(parent) || false;
37
+ return { byId, within };
38
+ }
39
+
40
+ /** Git status is deliberately a conservative file association, not a line diff. */
41
+ export function workingImpactInput(overview: WorkingOverview): ImpactInput {
42
+ return {
43
+ kind: "files",
44
+ paths: overview.paths.map((path) => path.path),
45
+ partial: overview.truncated,
46
+ };
47
+ }
48
+
49
+ /** Dependencies and consumers have different directions for calls and data transfers. */
50
+ function dependency(edge: Connection) {
51
+ return ["flows", "writes", "produces", "resumes"].includes(edge.relation)
52
+ ? { provider: edge.source, consumer: edge.target }
53
+ : { provider: edge.target, consumer: edge.source };
54
+ }
55
+
56
+ function contractTypeFiles(input: unknown): string[] {
57
+ if (!input || typeof input !== "object") return [];
58
+ const value = input as Record<string, unknown>;
59
+ const own = Array.isArray(value.typeSources)
60
+ ? value.typeSources.flatMap((source) =>
61
+ source && typeof source.file === "string" ? [source.file] : [],
62
+ )
63
+ : [];
64
+ return [
65
+ ...own,
66
+ ...(Array.isArray(value.members)
67
+ ? value.members.flatMap(contractTypeFiles)
68
+ : []),
69
+ ];
70
+ }
71
+
72
+ export function changeImpact(
73
+ graph: DefinitionGraph,
74
+ input: ImpactInput,
75
+ indirect = false,
76
+ ) {
77
+ const { byId, within } = graphIndex(graph);
78
+ const seeds = new Set<string>(),
79
+ files = new Map<string, string[]>();
80
+ const basis = new Map<string, string[]>();
81
+ const unmapped: string[] = [];
82
+ if (input.kind === "block") {
83
+ if (byId.has(input.id)) seeds.add(input.id);
84
+ } else {
85
+ const associations = graph.elements.flatMap((node) => [
86
+ ...[...(node.sources || []), ...(node.source ? [node.source] : [])].map(
87
+ (source) => ({
88
+ id: node.id,
89
+ file: source.file,
90
+ basis:
91
+ source.scope === "file"
92
+ ? "Block implementation file"
93
+ : "Source file",
94
+ }),
95
+ ),
96
+ ...contractTypeFiles(node.contract?.input).map((file) => ({
97
+ id: node.id,
98
+ file,
99
+ basis: "Contract type dependency",
100
+ })),
101
+ ]);
102
+ for (const edge of graph.connections)
103
+ for (const source of [
104
+ ...(edge.sources || []),
105
+ ...(edge.sourceRef ? [edge.sourceRef] : []),
106
+ ])
107
+ if (connectionEvidence(edge) === "code")
108
+ associations.push({
109
+ id: edge.source,
110
+ file: source.file,
111
+ basis: "Call evidence",
112
+ });
113
+ for (const path of [...new Set(input.paths)]) {
114
+ const allMatches = associations.filter((source) =>
115
+ path.endsWith("/")
116
+ ? source.file.startsWith(path)
117
+ : source.file === path,
118
+ );
119
+ const preciseFiles = new Set(
120
+ allMatches
121
+ .filter((source) => source.basis !== "Block implementation file")
122
+ .map((source) => source.file),
123
+ );
124
+ const matches = allMatches.filter(
125
+ (source) =>
126
+ source.basis !== "Block implementation file" ||
127
+ !preciseFiles.has(source.file),
128
+ );
129
+ const ids = new Set(matches.map((source) => source.id));
130
+ const owners = graph.elements.filter((node) => ids.has(node.id));
131
+ if (!owners.length) unmapped.push(path);
132
+ // A file can own several unrelated logical parts. Keep them all, but avoid
133
+ // turning a shared file's broad parent anchor into a change to every sibling.
134
+ for (const owner of owners.filter(
135
+ (node) =>
136
+ !owners.some(
137
+ (other) => other.id !== node.id && within(other.id, node.id),
138
+ ),
139
+ )) {
140
+ seeds.add(owner.id);
141
+ files.set(owner.id, [
142
+ ...new Set([...(files.get(owner.id) || []), path]),
143
+ ]);
144
+ basis.set(owner.id, [
145
+ ...new Set([
146
+ ...(basis.get(owner.id) || []),
147
+ ...matches
148
+ .filter((match) => match.id === owner.id)
149
+ .map((match) => match.basis),
150
+ ]),
151
+ ]);
152
+ }
153
+ }
154
+ }
155
+ const reasons = new Map<string, ImpactReason>(
156
+ [...seeds].map((id) => [id, { id, distance: 0 }]),
157
+ );
158
+ const links = graph.connections
159
+ .flatMap((edge) => {
160
+ const link = { ...dependency(edge), edge };
161
+ return edge.bidirectional
162
+ ? [link, { provider: link.consumer, consumer: link.provider, edge }]
163
+ : [link];
164
+ })
165
+ .filter((link) => byId.has(link.provider) && byId.has(link.consumer));
166
+ const connectionIds = new Set<string>();
167
+ // Breadth first, bounded by the graph. Containers are conservative dependency
168
+ // boundaries; merely sharing a parent never creates a consumer relationship.
169
+ let frontier = [...reasons.values()];
170
+ while (frontier.length) {
171
+ const next: ImpactReason[] = [];
172
+ for (const link of links) {
173
+ const cause = frontier.find((reason) => within(link.provider, reason.id));
174
+ if (!cause) continue;
175
+ if ([...seeds].some((seed) => within(link.consumer, seed))) continue;
176
+ const old = reasons.get(link.consumer);
177
+ if (old && old.distance < cause.distance + 1) continue;
178
+ connectionIds.add(link.edge.id);
179
+ if (old) continue;
180
+ const reason = {
181
+ id: link.consumer,
182
+ distance: cause.distance + 1,
183
+ via: link.edge.id,
184
+ provider: link.provider,
185
+ };
186
+ reasons.set(reason.id, reason);
187
+ next.push(reason);
188
+ }
189
+ if (!indirect) break;
190
+ frontier = next;
191
+ }
192
+ const changed = (id: string) => [...seeds].some((seed) => within(id, seed));
193
+ const consumers = [...reasons.values()].filter(
194
+ (reason) => reason.distance && !changed(reason.id),
195
+ );
196
+ const affected = (id: string) =>
197
+ changed(id) || consumers.some((reason) => id === reason.id);
198
+ const marks: Record<
199
+ string,
200
+ { kind: "changed" | "consumer"; distance: number; count: number }
201
+ > = {};
202
+ for (const node of graph.elements) {
203
+ const insideSeeds = [...seeds].filter((id) => within(id, node.id));
204
+ const insideConsumers = consumers.filter((reason) =>
205
+ within(reason.id, node.id),
206
+ );
207
+ if (changed(node.id) || insideSeeds.length)
208
+ marks[node.id] = {
209
+ kind: "changed",
210
+ distance: 0,
211
+ count: Math.max(1, insideSeeds.length),
212
+ };
213
+ else if (insideConsumers.length)
214
+ marks[node.id] = {
215
+ kind: "consumer",
216
+ distance: Math.min(...insideConsumers.map((reason) => reason.distance)),
217
+ count: insideConsumers.length,
218
+ };
219
+ }
220
+ const tests = new Map<
221
+ string,
222
+ { file: string; targets: string[]; reasons: string[] }
223
+ >();
224
+ for (const reason of reasons.values()) {
225
+ for (const test of relatedTests(graph, reason.id)) {
226
+ const record = tests.get(test.source.file) || {
227
+ file: test.source.file,
228
+ targets: [],
229
+ reasons: [],
230
+ };
231
+ record.targets = [...new Set([...record.targets, reason.id])];
232
+ record.reasons = [
233
+ ...new Set([
234
+ ...record.reasons,
235
+ reason.distance
236
+ ? "Consumer"
237
+ : input.kind === "block"
238
+ ? "Selected block"
239
+ : "Changed file",
240
+ ...(test.association === "attachment" &&
241
+ !test.targets.some((id) => within(id, reason.id))
242
+ ? ["Inherited attachment"]
243
+ : []),
244
+ ]),
245
+ ];
246
+ tests.set(record.file, record);
247
+ }
248
+ }
249
+ const unresolved = graph.gaps.filter((gap) =>
250
+ graph.elements.some(
251
+ (node) =>
252
+ affected(node.id) &&
253
+ (node.sources || (node.source ? [node.source] : [])).some(
254
+ (source) => source.file === gap.source.file,
255
+ ),
256
+ ),
257
+ );
258
+ const interfaces = graph.elements.filter(
259
+ (node) =>
260
+ node.kind === "interface" &&
261
+ (affected(node.id) ||
262
+ graph.elements.some(
263
+ (part) => affected(part.id) && within(part.id, node.id),
264
+ )),
265
+ );
266
+ return {
267
+ input,
268
+ seeds: [...seeds],
269
+ files: Object.fromEntries(files),
270
+ basis: Object.fromEntries(basis),
271
+ consumers,
272
+ connectionIds: [...connectionIds],
273
+ marks,
274
+ interfaces: interfaces.map((node) => node.id),
275
+ tests: [...tests.values()],
276
+ unmapped,
277
+ unresolved,
278
+ };
279
+ }
280
+ export type ChangeImpact = ReturnType<typeof changeImpact>;
@@ -0,0 +1,82 @@
1
+ import ts from "typescript";
2
+ import type { CheckDefinition } from "./checks-model";
3
+
4
+ // Discovery only: runner reports remain the authority on which tests executed.
5
+ export function testDefinitions(body: string, file: string): CheckDefinition[] {
6
+ const source = ts.createSourceFile(file, body, ts.ScriptTarget.Latest, true);
7
+ const aliases = new Map([
8
+ ["describe", "describe"],
9
+ ["it", "test"],
10
+ ["test", "test"],
11
+ ]);
12
+ for (const statement of source.statements) {
13
+ if (
14
+ !ts.isImportDeclaration(statement) ||
15
+ !ts.isStringLiteral(statement.moduleSpecifier) ||
16
+ !["vitest", "bun:test"].includes(statement.moduleSpecifier.text)
17
+ )
18
+ continue;
19
+ const imports = statement.importClause?.namedBindings;
20
+ if (imports && ts.isNamedImports(imports))
21
+ for (const item of imports.elements) {
22
+ const original = (item.propertyName || item.name).text;
23
+ if (["describe", "it", "test"].includes(original))
24
+ aliases.set(
25
+ item.name.text,
26
+ original === "describe" ? "describe" : "test",
27
+ );
28
+ }
29
+ }
30
+ function head(node: ts.Expression): {
31
+ kind?: string;
32
+ parameterized: boolean;
33
+ } {
34
+ if (ts.isIdentifier(node))
35
+ return { kind: aliases.get(node.text), parameterized: false };
36
+ if (ts.isPropertyAccessExpression(node)) {
37
+ const value = head(node.expression);
38
+ return {
39
+ ...value,
40
+ parameterized:
41
+ value.parameterized ||
42
+ node.name.text === "each" ||
43
+ node.name.text === "for",
44
+ };
45
+ }
46
+ if (ts.isCallExpression(node) || ts.isTaggedTemplateExpression(node))
47
+ return head(ts.isCallExpression(node) ? node.expression : node.tag);
48
+ return { parameterized: false };
49
+ }
50
+ const result: CheckDefinition[] = [];
51
+ function visit(node: ts.Node, groups: string[]) {
52
+ if (ts.isCallExpression(node)) {
53
+ const info = head(node.expression),
54
+ name = node.arguments[0];
55
+ if (
56
+ info.kind &&
57
+ name &&
58
+ (ts.isStringLiteral(name) || ts.isNoSubstitutionTemplateLiteral(name))
59
+ ) {
60
+ if (info.kind === "test") {
61
+ result.push({
62
+ name: name.text,
63
+ groups,
64
+ parameterized: info.parameterized,
65
+ line:
66
+ source.getLineAndCharacterOfPosition(node.getStart(source)).line +
67
+ 1,
68
+ endLine:
69
+ source.getLineAndCharacterOfPosition(node.getEnd()).line + 1,
70
+ });
71
+ return;
72
+ }
73
+ for (const child of node.arguments.slice(1))
74
+ visit(child, [...groups, name.text]);
75
+ return;
76
+ }
77
+ }
78
+ ts.forEachChild(node, (child) => visit(child, groups));
79
+ }
80
+ visit(source, []);
81
+ return result;
82
+ }
@@ -0,0 +1,108 @@
1
+ import { relatedTests, type DefinitionGraph } from "./model";
2
+ export type CheckStatus =
3
+ | "queued"
4
+ | "running"
5
+ | "passed"
6
+ | "failed"
7
+ | "skipped"
8
+ | "blocked"
9
+ | "cancelled";
10
+ export interface CheckFile {
11
+ file: string;
12
+ runner: "bun" | "vitest" | null;
13
+ package: string;
14
+ reason?: string;
15
+ associations: string[];
16
+ sourceRevision?: string;
17
+ }
18
+ export interface CheckCase {
19
+ name: string;
20
+ groups: string[];
21
+ status: "passed" | "failed" | "skipped" | "todo";
22
+ duration?: number;
23
+ line?: number;
24
+ failure?: string;
25
+ }
26
+ export interface CheckDefinition {
27
+ name: string;
28
+ groups: string[];
29
+ line: number;
30
+ endLine: number;
31
+ parameterized: boolean;
32
+ }
33
+ export interface CheckSource {
34
+ file: string;
35
+ revision: string;
36
+ body: string;
37
+ definitions: CheckDefinition[];
38
+ }
39
+ export interface CheckResult {
40
+ reason?: string;
41
+ file: string;
42
+ status: CheckStatus;
43
+ passed: number;
44
+ failed: number;
45
+ skipped: number;
46
+ duration?: number;
47
+ cases?: CheckCase[];
48
+ casesTruncated?: boolean;
49
+ sourceRevision?: string;
50
+ }
51
+ export interface CheckRun {
52
+ id: string;
53
+ block: string;
54
+ definition: string;
55
+ startedAt: string;
56
+ finishedAt?: string;
57
+ status: CheckStatus;
58
+ results: CheckResult[];
59
+ output: string;
60
+ stale?: boolean;
61
+ fingerprints: Record<string, string>;
62
+ pid: number;
63
+ }
64
+ export type CheckRunView = Omit<CheckRun, "fingerprints" | "pid">;
65
+ export function blockCheckState(
66
+ graph: DefinitionGraph,
67
+ block: string,
68
+ runs: CheckRunView[],
69
+ ) {
70
+ const files = new Set(
71
+ relatedTests(graph, block).map((test) => test.source.file),
72
+ );
73
+ const latest = new Map<string, { run: CheckRunView; result: CheckResult }>();
74
+ for (const run of runs)
75
+ for (const result of run.results)
76
+ if (files.has(result.file) && !latest.has(result.file))
77
+ latest.set(result.file, { run, result });
78
+ if (!latest.size) return;
79
+ const values = [...latest.values()];
80
+ const active = values.some(
81
+ (v) => v.result.status === "running" || v.result.status === "queued",
82
+ );
83
+ const stale = values.some(
84
+ (v) => v.run.stale || v.run.definition !== graph.revision,
85
+ );
86
+ const failed = values.filter((v) => v.result.status === "failed").length;
87
+ const blocked = values.some((v) => v.result.status === "blocked");
88
+ const passed = values.filter((v) => v.result.status === "passed").length;
89
+ return {
90
+ status: active
91
+ ? "running"
92
+ : stale
93
+ ? "stale"
94
+ : failed
95
+ ? "failed"
96
+ : blocked
97
+ ? "blocked"
98
+ : passed === values.length && values.length === files.size
99
+ ? "passed"
100
+ : passed
101
+ ? "partial"
102
+ : "skipped",
103
+ checked: values.length,
104
+ total: files.size,
105
+ failed,
106
+ passed,
107
+ } as const;
108
+ }