@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,573 @@
1
+ /** Saved working code versus the work's immutable starting revision. No source execution. */
2
+ import ts from "typescript";
3
+ import { posix } from "node:path";
4
+ import { sourceDigest } from "./source-index";
5
+ import type { Connection, DefinitionGraph, SourceRef } from "./model";
6
+
7
+ export type WorkChangeKind =
8
+ | "implementation"
9
+ | "interface"
10
+ | "connection"
11
+ | "mapping"
12
+ | "support";
13
+ export interface WorkChange {
14
+ kind: WorkChangeKind;
15
+ block: string;
16
+ file: string;
17
+ label: string;
18
+ before?: string;
19
+ after?: string;
20
+ connection?: Connection;
21
+ change?: "added" | "removed" | "modified";
22
+ }
23
+ export interface WorkReview {
24
+ basis: "saved-code";
25
+ changes: WorkChange[];
26
+ /** Files remain accounted for even when ownership or syntax cannot be proved. */
27
+ unassigned: string[];
28
+ historicalOwnership: boolean;
29
+ }
30
+ type Declaration = {
31
+ key: string;
32
+ node:
33
+ | ts.FunctionDeclaration
34
+ | ts.MethodDeclaration
35
+ | ts.ArrowFunction
36
+ | ts.FunctionExpression;
37
+ name: string;
38
+ className?: string;
39
+ start: number;
40
+ end: number;
41
+ };
42
+ type Parsed = {
43
+ file: ts.SourceFile;
44
+ declarations: Declaration[];
45
+ imports: Map<string, { file: string; name: string }>;
46
+ digest: string;
47
+ valid: boolean;
48
+ };
49
+ const printer = ts.createPrinter({ removeComments: true });
50
+ const print = (node: ts.Node, sf: ts.SourceFile) =>
51
+ printer.printNode(ts.EmitHint.Unspecified, node, sf);
52
+ function parse(path: string, text: string): Parsed {
53
+ const file = ts.createSourceFile(
54
+ path,
55
+ text,
56
+ ts.ScriptTarget.Latest,
57
+ true,
58
+ path.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
59
+ );
60
+ const declarations: Declaration[] = [],
61
+ imports = new Map<string, { file: string; name: string }>();
62
+ function visit(node: ts.Node) {
63
+ if (
64
+ ts.isImportDeclaration(node) &&
65
+ ts.isStringLiteral(node.moduleSpecifier) &&
66
+ node.moduleSpecifier.text.startsWith(".")
67
+ ) {
68
+ const bindings = node.importClause?.namedBindings;
69
+ if (bindings && ts.isNamedImports(bindings))
70
+ for (const item of bindings.elements)
71
+ imports.set(item.name.text, {
72
+ file: posix.normalize(
73
+ posix.join(posix.dirname(path), node.moduleSpecifier.text),
74
+ ),
75
+ name: item.propertyName?.text || item.name.text,
76
+ });
77
+ }
78
+ if (
79
+ (ts.isFunctionDeclaration(node) ||
80
+ ts.isMethodDeclaration(node) ||
81
+ ts.isArrowFunction(node) ||
82
+ ts.isFunctionExpression(node)) &&
83
+ node.body
84
+ ) {
85
+ const owner = ts.isVariableDeclaration(node.parent)
86
+ ? node.parent.name.getText(file)
87
+ : "";
88
+ const name = ("name" in node && node.name?.getText(file)) || owner;
89
+ const cls =
90
+ ts.isMethodDeclaration(node) &&
91
+ (ts.isClassDeclaration(node.parent) ||
92
+ ts.isClassExpression(node.parent))
93
+ ? node.parent.name?.text
94
+ : undefined;
95
+ if (name)
96
+ declarations.push({
97
+ key: cls ? `${cls}.${name}` : name,
98
+ node,
99
+ name,
100
+ className: cls,
101
+ start:
102
+ file.getLineAndCharacterOfPosition(node.getStart(file)).line + 1,
103
+ end: file.getLineAndCharacterOfPosition(node.end).line + 1,
104
+ });
105
+ }
106
+ ts.forEachChild(node, visit);
107
+ }
108
+ visit(file);
109
+ return {
110
+ file,
111
+ declarations,
112
+ imports,
113
+ digest: sourceDigest(text),
114
+ valid: !(file as ts.SourceFile & { parseDiagnostics: unknown[] })
115
+ .parseDiagnostics.length,
116
+ };
117
+ }
118
+ function anchor(parsed: Parsed | undefined, ref: SourceRef | undefined) {
119
+ if (!parsed?.valid || !ref || ref.scope === "file") return;
120
+ const candidates = ref.symbol
121
+ ? parsed.declarations.filter(
122
+ (d) => d.key === ref.symbol || d.name === ref.symbol,
123
+ )
124
+ : parsed.digest === ref.digest
125
+ ? parsed.declarations
126
+ .filter((d) => d.start <= ref.line && ref.line <= d.end)
127
+ .sort((a, b) => a.end - a.start - (b.end - b.start))
128
+ .slice(0, 1)
129
+ : [];
130
+ return candidates.length === 1 ? candidates[0] : undefined;
131
+ }
132
+ function unique(parsed: Parsed | undefined, key: string) {
133
+ const ds = parsed?.declarations.filter((d) => d.key === key) || [];
134
+ return ds.length === 1 ? ds[0] : undefined;
135
+ }
136
+ function signature(d: Declaration, p: Parsed) {
137
+ const parameters = d.node.parameters.map((n) => print(n, p.file)).join(", ");
138
+ const generics = d.node.typeParameters?.length
139
+ ? `<${d.node.typeParameters.map((n) => print(n, p.file)).join(", ")}>`
140
+ : "";
141
+ const modifiers =
142
+ d.node.modifiers
143
+ ?.filter((m) =>
144
+ [
145
+ ts.SyntaxKind.AsyncKeyword,
146
+ ts.SyntaxKind.PublicKeyword,
147
+ ts.SyntaxKind.PrivateKeyword,
148
+ ts.SyntaxKind.ProtectedKeyword,
149
+ ts.SyntaxKind.StaticKeyword,
150
+ ].includes(m.kind),
151
+ )
152
+ .map((m) => print(m, p.file))
153
+ .join(" ") || "";
154
+ return `${modifiers ? modifiers + " " : ""}${d.key}${generics}(${parameters}): ${d.node.type ? print(d.node.type, p.file) : "inferred"}`;
155
+ }
156
+
157
+ const refFor = (graph: DefinitionGraph, id: string, file: string) => {
158
+ const node = graph.elements.find((n) => n.id === id);
159
+ return (node?.sources || (node?.source ? [node.source] : [])).find(
160
+ (r) => r.file === file,
161
+ );
162
+ };
163
+
164
+ /** Only explicit, uniquely mapped functions/methods are classified. Dynamic dispatch is not guessed. */
165
+ export function reviewWorkingCode(
166
+ beforeGraph: DefinitionGraph,
167
+ graph: DefinitionGraph,
168
+ files: {
169
+ path: string;
170
+ before?: string;
171
+ after?: string;
172
+ unavailable?: string;
173
+ }[],
174
+ historicalOwnership: boolean,
175
+ ): WorkReview {
176
+ const changes: WorkChange[] = [],
177
+ unassigned: string[] = [],
178
+ parsed = new Map<string, { before?: Parsed; after?: Parsed }>();
179
+ for (const f of files)
180
+ if (/\.[cm]?[jt]sx?$/.test(f.path) && !f.unavailable)
181
+ parsed.set(f.path, {
182
+ before: f.before === undefined ? undefined : parse(f.path, f.before),
183
+ after: f.after === undefined ? undefined : parse(f.path, f.after),
184
+ });
185
+ const ownership = new Map<
186
+ string,
187
+ Map<string, { before?: Declaration; after?: Declaration }>
188
+ >();
189
+ for (const [file, p] of parsed) {
190
+ const owners = new Map<
191
+ string,
192
+ { before?: Declaration; after?: Declaration }
193
+ >();
194
+ for (const id of new Set(
195
+ [...beforeGraph.elements, ...graph.elements]
196
+ .filter((n) =>
197
+ (n.sources || (n.source ? [n.source] : [])).some(
198
+ (r) => r.file === file,
199
+ ),
200
+ )
201
+ .map((n) => n.id),
202
+ )) {
203
+ let b = anchor(p.before, refFor(beforeGraph, id, file)),
204
+ a = anchor(p.after, refFor(graph, id, file));
205
+ if (!b && a) b = unique(p.before, a.key);
206
+ if (!a && b) a = unique(p.after, b.key);
207
+ if (b || a) owners.set(id, { before: b, after: a });
208
+ }
209
+ ownership.set(file, owners);
210
+ }
211
+ const nodes = new Map(
212
+ [...beforeGraph.elements, ...graph.elements].map((n) => [n.id, n]),
213
+ );
214
+ function interfaceOf(id: string) {
215
+ let current = id;
216
+ const seen = new Set<string>();
217
+ while (current && !seen.has(current)) {
218
+ seen.add(current);
219
+ if (nodes.get(current)?.kind === "interface") return current;
220
+ current = nodes.get(current)?.parent || "";
221
+ }
222
+ return id;
223
+ }
224
+ const unresolvedCalls = new Map<string, Set<string>>();
225
+ function calls(
226
+ file: string,
227
+ d: Declaration | undefined,
228
+ side: "before" | "after",
229
+ ) {
230
+ const result = new Set<string>(),
231
+ p = parsed.get(file)?.[side];
232
+ if (!d || !p) return result;
233
+ const declaration = d;
234
+ const unresolved = new Set<string>();
235
+ unresolvedCalls.set(`${file}:${d.key}:${side}`, unresolved);
236
+ const shadowed = new Set<string>();
237
+ function collect(n: ts.Node) {
238
+ if (
239
+ (ts.isParameter(n) || ts.isVariableDeclaration(n)) &&
240
+ ts.isIdentifier(n.name)
241
+ )
242
+ shadowed.add(n.name.text);
243
+ ts.forEachChild(n, collect);
244
+ }
245
+ collect(declaration.node);
246
+ function visit(n: ts.Node) {
247
+ // A nested callback's dependencies still belong to its enclosing mapped operation.
248
+ if (ts.isCallExpression(n)) {
249
+ let targetFile = file,
250
+ key = "";
251
+ if (ts.isIdentifier(n.expression)) {
252
+ if (shadowed.has(n.expression.text)) {
253
+ unresolved.add(n.expression.getText(p!.file));
254
+ ts.forEachChild(n, visit);
255
+ return;
256
+ }
257
+ const imp = p!.imports.get(n.expression.text);
258
+ key = imp?.name || n.expression.text;
259
+ if (imp) {
260
+ const options = [
261
+ imp.file,
262
+ `${imp.file}.ts`,
263
+ `${imp.file}.tsx`,
264
+ `${imp.file}/index.ts`,
265
+ ];
266
+ const matched = [
267
+ ...new Set(
268
+ [...ownership.keys(), ...nodes.values()].flatMap((v) =>
269
+ typeof v === "string"
270
+ ? [v]
271
+ : (v.sources || (v.source ? [v.source] : [])).map(
272
+ (r) => r.file,
273
+ ),
274
+ ),
275
+ ),
276
+ ].filter((f) => options.includes(f));
277
+ if (matched.length !== 1) {
278
+ unresolved.add(n.expression.getText(p!.file));
279
+ ts.forEachChild(n, visit);
280
+ return;
281
+ }
282
+ targetFile = matched[0];
283
+ } else if (declaration.className) {
284
+ /* Bare calls cannot refer to another instance method. */
285
+ }
286
+ } else if (
287
+ ts.isPropertyAccessExpression(n.expression) &&
288
+ n.expression.expression.kind === ts.SyntaxKind.ThisKeyword &&
289
+ declaration.className
290
+ )
291
+ key = `${declaration.className}.${n.expression.name.text}`;
292
+ if (key) {
293
+ // External unchanged files can only be resolved through an explicit source symbol.
294
+ const mapped = [...(ownership.get(targetFile) || new Map())]
295
+ .filter(([, pair]) => pair[side]?.key === key)
296
+ .map(([id]) => id);
297
+ if (!ownership.has(targetFile))
298
+ for (const node of (side === "before" ? beforeGraph : graph)
299
+ .elements)
300
+ if (
301
+ (node.sources || (node.source ? [node.source] : [])).some(
302
+ (r) => r.file === targetFile && r.symbol === key,
303
+ )
304
+ )
305
+ mapped.push(node.id);
306
+ if (mapped.length === 1) result.add(mapped[0]);
307
+ else unresolved.add(n.expression.getText(p!.file));
308
+ } else unresolved.add(n.expression.getText(p!.file));
309
+ }
310
+ ts.forEachChild(n, visit);
311
+ }
312
+ visit(d.node.body!);
313
+ return result;
314
+ }
315
+ for (const file of files) {
316
+ if (file.before === file.after && !file.unavailable) continue;
317
+ const p = parsed.get(file.path),
318
+ owners = ownership.get(file.path);
319
+ const mapped = [...nodes.values()].filter((n) =>
320
+ (n.sources || (n.source ? [n.source] : [])).some(
321
+ (r) => r.file === file.path,
322
+ ),
323
+ );
324
+ if (!mapped.length) {
325
+ const supportOwners = new Set(
326
+ [...nodes.values()]
327
+ .filter(
328
+ (n) =>
329
+ n.attachments?.docs?.some(
330
+ (path) => path.split("#")[0] === file.path,
331
+ ) || n.attachments?.tests?.includes(file.path),
332
+ )
333
+ .map((n) => n.id),
334
+ );
335
+ for (const test of [...beforeGraph.tests, ...graph.tests])
336
+ if (test.source.file === file.path)
337
+ for (const id of test.targets) supportOwners.add(id);
338
+ if (file.path === "fractory.json") supportOwners.add(graph.id);
339
+ if (supportOwners.size) {
340
+ for (const id of supportOwners)
341
+ changes.push({
342
+ kind: "support",
343
+ block: id,
344
+ file: file.path,
345
+ label:
346
+ file.path === "fractory.json"
347
+ ? "Map declarations changed"
348
+ : /\.mdx?$/.test(file.path)
349
+ ? "Attached documentation changed"
350
+ : "Associated tests changed",
351
+ });
352
+ continue;
353
+ }
354
+ unassigned.push(file.path);
355
+ continue;
356
+ }
357
+ if (
358
+ !p ||
359
+ (!p.before?.valid && file.before !== undefined) ||
360
+ (!p.after?.valid && file.after !== undefined)
361
+ ) {
362
+ for (const n of mapped)
363
+ changes.push({
364
+ kind: "mapping",
365
+ block: n.id,
366
+ file: file.path,
367
+ label: file.unavailable || "No parseable declaration evidence",
368
+ });
369
+ continue;
370
+ }
371
+ // A mapped file can also contain changed code outside its mapped declarations.
372
+ const covered = new Set(
373
+ [...(owners?.values() || [])].flatMap((pair) =>
374
+ [pair.before?.key, pair.after?.key].filter(Boolean),
375
+ ),
376
+ );
377
+ const unmatched = new Set(
378
+ [...(p.before?.declarations || []), ...(p.after?.declarations || [])]
379
+ .map((d) => d.key)
380
+ .filter((key) => !covered.has(key)),
381
+ );
382
+ if (
383
+ [...unmatched].some((key) => {
384
+ const b = unique(p.before, key),
385
+ a = unique(p.after, key);
386
+ return (
387
+ (b && p.before ? print(b.node, p.before.file) : undefined) !==
388
+ (a && p.after ? print(a.node, p.after.file) : undefined)
389
+ );
390
+ })
391
+ )
392
+ unassigned.push(file.path);
393
+ for (const n of mapped) {
394
+ const pair = owners?.get(n.id);
395
+ if (!pair) {
396
+ changes.push({
397
+ kind: "mapping",
398
+ block: n.id,
399
+ file: file.path,
400
+ label: "Refresh this block’s source anchor",
401
+ });
402
+ continue;
403
+ }
404
+ const { before: b, after: a } = pair;
405
+ if (!historicalOwnership && (b?.key !== a?.key || !b || !a)) {
406
+ changes.push({
407
+ kind: "mapping",
408
+ block: n.id,
409
+ file: file.path,
410
+ label: "Starting revision has no verified block ownership",
411
+ });
412
+ continue;
413
+ }
414
+ const sb = b && p.before ? signature(b, p.before) : undefined,
415
+ sa = a && p.after ? signature(a, p.after) : undefined;
416
+ if (sb !== sa)
417
+ changes.push({
418
+ kind: "interface",
419
+ block: interfaceOf(n.id),
420
+ file: file.path,
421
+ label: !b
422
+ ? "Added callable"
423
+ : !a
424
+ ? "Removed callable"
425
+ : "Declaration changed",
426
+ before: sb?.slice(0, 1800),
427
+ after: sa?.slice(0, 1800),
428
+ });
429
+ const bodyB =
430
+ b && p.before ? print(b.node.body!, p.before.file) : undefined,
431
+ bodyA = a && p.after ? print(a.node.body!, p.after.file) : undefined;
432
+ if (bodyB !== bodyA) {
433
+ changes.push({
434
+ kind: "implementation",
435
+ block: n.id,
436
+ file: file.path,
437
+ label: "Implementation changed",
438
+ });
439
+ if (a && !a.node.type)
440
+ changes.push({
441
+ kind: "mapping",
442
+ block: n.id,
443
+ file: file.path,
444
+ label:
445
+ "Return type is inferred; contract compatibility needs a type check",
446
+ });
447
+ }
448
+ const cb = calls(file.path, b, "before"),
449
+ ca = calls(file.path, a, "after");
450
+ const ub = b
451
+ ? unresolvedCalls.get(`${file.path}:${b.key}:before`) ||
452
+ new Set<string>()
453
+ : new Set<string>(),
454
+ ua = a
455
+ ? unresolvedCalls.get(`${file.path}:${a.key}:after`) ||
456
+ new Set<string>()
457
+ : new Set<string>();
458
+ if (
459
+ [...ub].some((key) => !ua.has(key)) ||
460
+ [...ua].some((key) => !ub.has(key))
461
+ )
462
+ changes.push({
463
+ kind: "mapping",
464
+ block: n.id,
465
+ file: file.path,
466
+ label: "Some changed call targets are dynamic or not mapped",
467
+ });
468
+ for (const [set, other, change] of [
469
+ [ca, cb, "added"],
470
+ [cb, ca, "removed"],
471
+ ] as const)
472
+ for (const target of set)
473
+ if (!other.has(target) && target !== n.id) {
474
+ const existing = (
475
+ change === "removed" ? beforeGraph : graph
476
+ ).connections.find(
477
+ (e) =>
478
+ e.source === n.id &&
479
+ e.target === target &&
480
+ e.relation === "calls",
481
+ );
482
+ const connection: Connection = existing || {
483
+ id: `work-call:${n.id}:${target}`,
484
+ source: n.id,
485
+ target,
486
+ relation: "calls",
487
+ label: nodes.get(target)?.label || target,
488
+ provenance: "source",
489
+ };
490
+ changes.push({
491
+ kind: "connection",
492
+ block: n.id,
493
+ file: file.path,
494
+ label: `${change === "added" ? "Calls" : "No longer calls"} ${nodes.get(target)?.label || target}`,
495
+ connection,
496
+ change,
497
+ });
498
+ }
499
+ }
500
+ }
501
+ if (historicalOwnership) {
502
+ const stable = (value: unknown): unknown =>
503
+ Array.isArray(value)
504
+ ? value.map(stable)
505
+ : value && typeof value === "object"
506
+ ? Object.fromEntries(
507
+ Object.entries(value)
508
+ .filter(
509
+ ([key]) =>
510
+ ![
511
+ "source",
512
+ "sources",
513
+ "sourceRef",
514
+ "line",
515
+ "digest",
516
+ "provenance",
517
+ ].includes(key),
518
+ )
519
+ .sort(([a], [b]) => a.localeCompare(b))
520
+ .map(([key, value]) => [key, stable(value)]),
521
+ )
522
+ : value;
523
+ const beforeNodes = new Map(beforeGraph.elements.map((n) => [n.id, n]));
524
+ for (const node of graph.elements) {
525
+ const old = beforeNodes.get(node.id);
526
+ if (!old) continue;
527
+ const contract = (n: typeof node) => ({
528
+ contract: n.contract,
529
+ auth: n.attachments?.auth?.symbol,
530
+ });
531
+ if (
532
+ JSON.stringify(stable(contract(old))) !==
533
+ JSON.stringify(stable(contract(node)))
534
+ )
535
+ changes.push({
536
+ kind: "interface",
537
+ block: interfaceOf(node.id),
538
+ file: node.source?.file || "fractory.json",
539
+ label: "Declared contract or auth changed",
540
+ before: JSON.stringify(stable(contract(old))).slice(0, 1800),
541
+ after: JSON.stringify(stable(contract(node))).slice(0, 1800),
542
+ });
543
+ }
544
+ const oldEdges = new Map(beforeGraph.connections.map((e) => [e.id, e])),
545
+ newEdges = new Map(graph.connections.map((e) => [e.id, e]));
546
+ for (const id of new Set([...oldEdges.keys(), ...newEdges.keys()])) {
547
+ const b = oldEdges.get(id),
548
+ a = newEdges.get(id);
549
+ if (JSON.stringify(stable(b)) === JSON.stringify(stable(a))) continue;
550
+ if (changes.some((c) => c.connection?.id === id)) continue;
551
+ const edge = a || b!;
552
+ changes.push({
553
+ kind: "connection",
554
+ block: edge.source,
555
+ file: edge.sourceRef?.file || "fractory.json",
556
+ label: !b
557
+ ? "Declared connection added"
558
+ : !a
559
+ ? "Declared connection removed"
560
+ : "Declared connection changed",
561
+ connection: edge,
562
+ change: !b ? "added" : !a ? "removed" : "modified",
563
+ });
564
+ }
565
+ }
566
+ // One interface can group several methods with independent signature changes.
567
+ return {
568
+ basis: "saved-code",
569
+ changes,
570
+ unassigned: [...new Set(unassigned)],
571
+ historicalOwnership,
572
+ };
573
+ }
@@ -0,0 +1,121 @@
1
+ import type { WorkTarget } from "./work-model";
2
+ export interface WorkChoice extends WorkTarget {
3
+ detached: boolean;
4
+ checkout?: string;
5
+ commit?: { subject: string; at: string; author: string };
6
+ blocks: { id: string; label: string }[];
7
+ blockBasis: "plan" | "files";
8
+ status?: {
9
+ changed: number | null;
10
+ conflicts: number | null;
11
+ checkedAt: string | null;
12
+ editedAt?: string;
13
+ unavailable?: string;
14
+ partial: boolean;
15
+ };
16
+ }
17
+ export interface WorkChoices {
18
+ repository: string;
19
+ targets: WorkChoice[];
20
+ checkedAt: string;
21
+ detailed: boolean;
22
+ }
23
+ export type WorkChoiceView = "recent" | "worktrees" | "branches";
24
+ export interface WorkVisit {
25
+ id: string;
26
+ path?: string;
27
+ at: number;
28
+ }
29
+ export const friendlyBranch = (name: string) => {
30
+ const text = name.replace(/^[^/]+\//, "").replace(/[-_]+/g, " ");
31
+ return text ? text[0].toUpperCase() + text.slice(1) : "Local worktree";
32
+ };
33
+ export function choiceTitle(t: WorkChoice) {
34
+ if (t.managed) return t.title;
35
+ if (!t.detached) return friendlyBranch(t.branch);
36
+ return t.blocks.length
37
+ ? t.blocks
38
+ .slice(0, 2)
39
+ .map((b) => b.label)
40
+ .join(" · ")
41
+ : "Local worktree";
42
+ }
43
+ export function choiceDescription(t: WorkChoice) {
44
+ if (t.detached && !t.managed)
45
+ return [
46
+ t.commit?.subject ? `Last commit: ${t.commit.subject}` : undefined,
47
+ t.checkout,
48
+ ]
49
+ .filter(Boolean)
50
+ .join(" · ");
51
+ return t.blocks.length
52
+ ? t.blocks
53
+ .slice(0, 2)
54
+ .map((b) => b.label)
55
+ .join(" · ") + (t.blocks.length > 2 ? ` +${t.blocks.length - 2}` : "")
56
+ : t.commit?.subject
57
+ ? `Last commit: ${t.commit.subject}`
58
+ : t.path
59
+ ? "Local checkout"
60
+ : "No local checkout";
61
+ }
62
+ export function visitTime(t: WorkChoice, visits: WorkVisit[]) {
63
+ return Math.max(
64
+ 0,
65
+ ...visits
66
+ .filter((v) => v.id === t.id || (!!v.path && v.path === t.path))
67
+ .map((v) => v.at),
68
+ );
69
+ }
70
+ /** Stable ordering within an open switcher is supplied by the caller, not status arrival order. */
71
+ export function choiceOrder(t: WorkChoice, visits: WorkVisit[]) {
72
+ return (
73
+ visitTime(t, visits) ||
74
+ Math.max(
75
+ 0,
76
+ ...[t.status?.editedAt, t.plannedAt, t.commit?.at].map(
77
+ (at) => Date.parse(at || "") || 0,
78
+ ),
79
+ )
80
+ );
81
+ }
82
+ export function recentChoices(
83
+ targets: WorkChoice[],
84
+ visits: WorkVisit[],
85
+ active: string,
86
+ now = Date.now(),
87
+ ) {
88
+ const recentSince = now - 30 * 86400000;
89
+ return targets
90
+ .filter(
91
+ (t) =>
92
+ visitTime(t, visits) > recentSince ||
93
+ t.id === active ||
94
+ (t.path &&
95
+ (t.managed ||
96
+ (t.status?.changed && choiceOrder(t, []) >= recentSince) ||
97
+ (t.current && !t.detached))),
98
+ )
99
+ .sort(
100
+ (a, b) =>
101
+ visitTime(b, visits) - visitTime(a, visits) ||
102
+ Number(b.id === active) - Number(a.id === active) ||
103
+ Number(b.current) - Number(a.current) ||
104
+ choiceOrder(b, visits) - choiceOrder(a, visits) ||
105
+ Number(b.managed) - Number(a.managed) ||
106
+ a.id.localeCompare(b.id),
107
+ )
108
+ .slice(0, 8);
109
+ }
110
+ export function relativeWorkTime(at: string | undefined, now = Date.now()) {
111
+ if (!at || !Number.isFinite(Date.parse(at))) return "";
112
+ const minutes = Math.max(0, Math.floor((now - Date.parse(at)) / 60000));
113
+ if (minutes < 1) return "just now";
114
+ if (minutes < 60) return `${minutes}m ago`;
115
+ if (minutes < 1440) return `${Math.floor(minutes / 60)}h ago`;
116
+ if (minutes < 10080) return `${Math.floor(minutes / 1440)}d ago`;
117
+ return new Intl.DateTimeFormat(undefined, {
118
+ month: "short",
119
+ day: "numeric",
120
+ }).format(new Date(at));
121
+ }