@granular-software/fractory 0.0.0-stage → 0.1.25
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.
- package/LICENSE +4 -0
- package/README.md +28 -2
- package/dist/activity-volume.d.ts +18 -0
- package/dist/activity-volume.js +94 -0
- package/dist/annotations.d.ts +122 -0
- package/dist/annotations.js +304 -0
- package/dist/benchmark-cli.d.ts +1 -0
- package/dist/benchmark-cli.js +95 -0
- package/dist/benchmark-worker.d.ts +2 -0
- package/dist/benchmark-worker.js +22 -0
- package/dist/benchmarks.d.ts +19 -0
- package/dist/benchmarks.js +414 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +63 -0
- package/dist/block-cli.d.ts +1 -0
- package/dist/block-cli.js +88 -0
- package/dist/block-documents.d.ts +35 -0
- package/dist/block-documents.js +84 -0
- package/dist/change-impact.d.ts +55 -0
- package/dist/change-impact.js +223 -0
- package/dist/check-source.d.ts +2 -0
- package/dist/check-source.js +66 -0
- package/dist/checks-model.d.ts +64 -0
- package/dist/checks-model.js +36 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +359 -0
- package/dist/code-model.d.ts +38 -0
- package/dist/code-model.js +94 -0
- package/dist/code-store.d.ts +9 -0
- package/dist/code-store.js +79 -0
- package/dist/compare-artifact.d.ts +6 -0
- package/dist/compare-artifact.js +277 -0
- package/dist/compare.d.ts +81 -0
- package/dist/compare.js +291 -0
- package/dist/compiler.d.ts +10 -0
- package/dist/compiler.js +1240 -0
- package/dist/compose.d.ts +9 -0
- package/dist/compose.js +72 -0
- package/dist/core.d.ts +87 -0
- package/dist/core.js +207 -0
- package/dist/coverage-cli.d.ts +1 -0
- package/dist/coverage-cli.js +42 -0
- package/dist/declaration-sources.d.ts +3 -0
- package/dist/declaration-sources.js +51 -0
- package/dist/declared-graph.d.ts +3 -0
- package/dist/declared-graph.js +67 -0
- package/dist/display-name.d.ts +2 -0
- package/dist/display-name.js +8 -0
- package/dist/error-heat.d.ts +58 -0
- package/dist/error-heat.js +96 -0
- package/dist/evidence.d.ts +3 -0
- package/dist/evidence.js +68 -0
- package/dist/factory-graph.d.ts +7 -0
- package/dist/factory-graph.js +389 -0
- package/dist/factory-model.d.ts +111 -0
- package/dist/factory-model.js +1 -0
- package/dist/git-code.d.ts +47 -0
- package/dist/git-code.js +352 -0
- package/dist/git-refs.d.ts +4 -0
- package/dist/git-refs.js +37 -0
- package/dist/history-activity.d.ts +4 -0
- package/dist/history-activity.js +129 -0
- package/dist/history-churn.d.ts +36 -0
- package/dist/history-churn.js +302 -0
- package/dist/history-git.d.ts +21 -0
- package/dist/history-git.js +125 -0
- package/dist/history-ledger.d.ts +43 -0
- package/dist/history-ledger.js +450 -0
- package/dist/history-ownership.d.ts +10 -0
- package/dist/history-ownership.js +75 -0
- package/dist/history-source.d.ts +13 -0
- package/dist/history-source.js +100 -0
- package/dist/integrations-cli.d.ts +2 -0
- package/dist/integrations-cli.js +159 -0
- package/dist/interface-map.d.ts +15 -0
- package/dist/interface-map.js +125 -0
- package/dist/issue-cache.d.ts +9 -0
- package/dist/issue-cache.js +68 -0
- package/dist/issue-cli.d.ts +1 -0
- package/dist/issue-cli.js +89 -0
- package/dist/issue-links.d.ts +15 -0
- package/dist/issue-links.js +125 -0
- package/dist/linear-issues.d.ts +19 -0
- package/dist/linear-issues.js +124 -0
- package/dist/local-runtime.d.ts +8 -0
- package/dist/local-runtime.js +59 -0
- package/dist/map-activity.d.ts +73 -0
- package/dist/map-activity.js +234 -0
- package/dist/map-model.d.ts +5 -0
- package/dist/map-model.js +166 -0
- package/dist/map-quality.d.ts +20 -0
- package/dist/map-quality.js +61 -0
- package/dist/mapping-coverage.d.ts +87 -0
- package/dist/mapping-coverage.js +404 -0
- package/dist/model.d.ts +186 -0
- package/dist/model.js +66 -0
- package/dist/operation-benchmark-worker.d.ts +2 -0
- package/dist/operation-benchmark-worker.js +60 -0
- package/dist/operation-benchmark.d.ts +29 -0
- package/dist/operation-benchmark.js +1 -0
- package/dist/otel-metrics.d.ts +21 -0
- package/dist/otel-metrics.js +146 -0
- package/dist/performance-model.d.ts +185 -0
- package/dist/performance-model.js +396 -0
- package/dist/product-map.d.ts +71 -0
- package/dist/product-map.js +197 -0
- package/dist/provider-access.cjs +207 -0
- package/dist/provider-access.d.cts +14 -0
- package/dist/public-boundaries.d.ts +13 -0
- package/dist/public-boundaries.js +115 -0
- package/dist/reports-cli.d.ts +1 -0
- package/dist/reports-cli.js +111 -0
- package/dist/reports-model.d.ts +27 -0
- package/dist/reports-model.js +39 -0
- package/dist/reports.d.ts +23 -0
- package/dist/reports.js +223 -0
- package/dist/repository-map.d.ts +22 -0
- package/dist/repository-map.js +153 -0
- package/dist/resource-cache.d.ts +17 -0
- package/dist/resource-cache.js +65 -0
- package/dist/review-cli.d.ts +19 -0
- package/dist/review-cli.js +224 -0
- package/dist/review.d.ts +48 -0
- package/dist/review.js +289 -0
- package/dist/runtime-metrics.d.ts +26 -0
- package/dist/runtime-metrics.js +290 -0
- package/dist/scope.d.ts +14 -0
- package/dist/scope.js +168 -0
- package/dist/sentry-activity.d.ts +39 -0
- package/dist/sentry-activity.js +174 -0
- package/dist/sentry.d.ts +130 -0
- package/dist/sentry.js +369 -0
- package/dist/service-release.d.ts +16 -0
- package/dist/service-release.js +14 -0
- package/dist/setup-cli.d.ts +2 -0
- package/dist/setup-cli.js +52 -0
- package/dist/setup-kit.cjs +163 -0
- package/dist/setup-kit.d.cts +32 -0
- package/dist/source-index.d.ts +37 -0
- package/dist/source-index.js +170 -0
- package/dist/source-review-git.d.ts +7 -0
- package/dist/source-review-git.js +105 -0
- package/dist/source-review.d.ts +18 -0
- package/dist/source-review.js +573 -0
- package/dist/telemetry.d.ts +17 -0
- package/dist/telemetry.js +51 -0
- package/dist/time-window.d.ts +9 -0
- package/dist/time-window.js +35 -0
- package/dist/trace-evidence.d.ts +23 -0
- package/dist/trace-evidence.js +55 -0
- package/dist/trace-model.d.ts +26 -0
- package/dist/trace-model.js +1 -0
- package/dist/tracing.d.ts +44 -0
- package/dist/tracing.js +123 -0
- package/dist/work-check.d.ts +18 -0
- package/dist/work-check.js +115 -0
- package/dist/work-cli.d.ts +1 -0
- package/dist/work-cli.js +176 -0
- package/dist/work-context.d.ts +59 -0
- package/dist/work-context.js +594 -0
- package/dist/work-intents.d.ts +6 -0
- package/dist/work-intents.js +140 -0
- package/dist/work-items.d.ts +58 -0
- package/dist/work-items.js +86 -0
- package/dist/work-lock-check.d.ts +12 -0
- package/dist/work-lock-check.js +339 -0
- package/dist/work-locks.d.ts +10 -0
- package/dist/work-locks.js +98 -0
- package/dist/work-model.d.ts +119 -0
- package/dist/work-model.js +310 -0
- package/dist/work-review-git.d.ts +8 -0
- package/dist/work-review-git.js +111 -0
- package/dist/work-review.d.ts +26 -0
- package/dist/work-review.js +426 -0
- package/dist/work-switcher-model.d.ts +43 -0
- package/dist/work-switcher-model.js +79 -0
- package/dist/work-switcher.d.ts +6 -0
- package/dist/work-switcher.js +171 -0
- package/dist/working-cli.d.ts +1 -0
- package/dist/working-cli.js +33 -0
- package/dist/working-code.d.ts +74 -0
- package/dist/working-code.js +421 -0
- package/dist/workspace-notes.d.ts +71 -0
- package/dist/workspace-notes.js +177 -0
- package/dist/workspace-tests.d.ts +21 -0
- package/dist/workspace-tests.js +573 -0
- package/package.json +547 -4
- package/src/activity-volume.ts +149 -0
- package/src/annotations.ts +485 -0
- package/src/benchmark-cli.ts +118 -0
- package/src/benchmark-worker.ts +22 -0
- package/src/benchmarks.ts +493 -0
- package/src/bin.ts +54 -0
- package/src/block-cli.ts +106 -0
- package/src/block-documents.ts +117 -0
- package/src/change-impact.ts +280 -0
- package/src/check-source.ts +82 -0
- package/src/checks-model.ts +108 -0
- package/src/cli.ts +440 -0
- package/src/code-model.ts +159 -0
- package/src/code-store.ts +98 -0
- package/src/compare-artifact.ts +314 -0
- package/src/compare.ts +440 -0
- package/src/compiler.ts +1512 -0
- package/src/compose.ts +94 -0
- package/src/core.ts +361 -0
- package/src/coverage-cli.ts +52 -0
- package/src/declaration-sources.ts +69 -0
- package/src/declared-graph.ts +75 -0
- package/src/display-name.ts +8 -0
- package/src/error-heat.ts +161 -0
- package/src/evidence.ts +94 -0
- package/src/factory-graph.ts +462 -0
- package/src/factory-model.ts +82 -0
- package/src/git-code.ts +461 -0
- package/src/git-refs.ts +43 -0
- package/src/history-activity.ts +146 -0
- package/src/history-churn.ts +416 -0
- package/src/history-git.ts +163 -0
- package/src/history-ledger.ts +636 -0
- package/src/history-ownership.ts +116 -0
- package/src/history-source.ts +117 -0
- package/src/integrations-cli.ts +187 -0
- package/src/interface-map.ts +166 -0
- package/src/issue-cache.ts +96 -0
- package/src/issue-cli.ts +137 -0
- package/src/issue-links.ts +153 -0
- package/src/linear-issues.ts +154 -0
- package/src/local-runtime.ts +70 -0
- package/src/map-activity.ts +309 -0
- package/src/map-model.ts +213 -0
- package/src/map-quality.ts +66 -0
- package/src/mapping-coverage.ts +570 -0
- package/src/model.ts +228 -0
- package/src/operation-benchmark-worker.ts +60 -0
- package/src/operation-benchmark.ts +28 -0
- package/src/otel-metrics.ts +176 -0
- package/src/performance-model.ts +575 -0
- package/src/product-map.ts +271 -0
- package/src/provider-access.cjs +207 -0
- package/src/provider-access.d.cts +14 -0
- package/src/public-boundaries.ts +143 -0
- package/src/reports-cli.ts +125 -0
- package/src/reports-model.ts +73 -0
- package/src/reports.ts +276 -0
- package/src/repository-map.ts +191 -0
- package/src/resource-cache.ts +76 -0
- package/src/review-cli.ts +256 -0
- package/src/review.ts +376 -0
- package/src/runtime-metrics.ts +386 -0
- package/src/scope.ts +192 -0
- package/src/sentry-activity.ts +234 -0
- package/src/sentry.ts +545 -0
- package/src/service-release.ts +18 -0
- package/src/setup-cli.ts +58 -0
- package/src/setup-kit.cjs +163 -0
- package/src/setup-kit.d.cts +32 -0
- package/src/source-index.ts +208 -0
- package/src/source-review-git.ts +131 -0
- package/src/source-review.ts +736 -0
- package/src/telemetry.ts +68 -0
- package/src/time-window.ts +51 -0
- package/src/trace-evidence.ts +80 -0
- package/src/trace-model.ts +26 -0
- package/src/tracing.ts +165 -0
- package/src/work-check.ts +149 -0
- package/src/work-cli.ts +229 -0
- package/src/work-context.ts +753 -0
- package/src/work-intents.ts +196 -0
- package/src/work-items.ts +151 -0
- package/src/work-lock-check.ts +549 -0
- package/src/work-locks.ts +108 -0
- package/src/work-model.ts +466 -0
- package/src/work-review-git.ts +130 -0
- package/src/work-review.ts +573 -0
- package/src/work-switcher-model.ts +121 -0
- package/src/work-switcher.ts +191 -0
- package/src/working-cli.ts +38 -0
- package/src/working-code.ts +582 -0
- package/src/workspace-notes.ts +203 -0
- package/src/workspace-tests.ts +654 -0
- package/toolkit/GUIDE.md +354 -0
- package/toolkit/SKILL.md +142 -0
- package/toolkit/bin/fractory +2 -0
- package/toolkit/build.json +1 -0
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
/** Read-only checks against the work's pinned starting revision. Never execute application code. */
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { readFileSync, lstatSync } from "node:fs";
|
|
4
|
+
import { resolve } from "node:path";
|
|
5
|
+
import { checkedPath } from "./workspace-notes";
|
|
6
|
+
import { readGit } from "./git-refs";
|
|
7
|
+
import type { DefinitionGraph, Element, SourceRef } from "./model";
|
|
8
|
+
import type { WorkPlan, WorkLock } from "./work-model";
|
|
9
|
+
import { scopeBlocks } from "./work-model";
|
|
10
|
+
import { publicContractIds } from "./work-locks";
|
|
11
|
+
|
|
12
|
+
export interface LockResult {
|
|
13
|
+
target: string;
|
|
14
|
+
kind: WorkLock["kind"];
|
|
15
|
+
status: "unchanged" | "violated" | "uncertain";
|
|
16
|
+
issues: { file?: string; message: string }[];
|
|
17
|
+
}
|
|
18
|
+
const stable = (value: unknown): string =>
|
|
19
|
+
JSON.stringify(value, (_key, v) =>
|
|
20
|
+
v && typeof v === "object" && !Array.isArray(v)
|
|
21
|
+
? Object.fromEntries(
|
|
22
|
+
Object.entries(v).sort(([a], [b]) => a.localeCompare(b)),
|
|
23
|
+
)
|
|
24
|
+
: v,
|
|
25
|
+
);
|
|
26
|
+
const refs = (node: Element) =>
|
|
27
|
+
node.sources?.length ? node.sources : node.source ? [node.source] : [];
|
|
28
|
+
const printer = ts.createPrinter({ removeComments: true });
|
|
29
|
+
const print = (n: ts.Node, sf: ts.SourceFile) =>
|
|
30
|
+
printer.printNode(ts.EmitHint.Unspecified, n, sf);
|
|
31
|
+
|
|
32
|
+
function declaration(text: string, ref: SourceRef, contract: boolean) {
|
|
33
|
+
const ast = ts.createSourceFile(
|
|
34
|
+
ref.file,
|
|
35
|
+
text,
|
|
36
|
+
ts.ScriptTarget.Latest,
|
|
37
|
+
true,
|
|
38
|
+
ref.file.endsWith("x") ? ts.ScriptKind.TSX : ts.ScriptKind.TS,
|
|
39
|
+
);
|
|
40
|
+
if (
|
|
41
|
+
(ast as ts.SourceFile & { parseDiagnostics: unknown[] }).parseDiagnostics
|
|
42
|
+
.length
|
|
43
|
+
)
|
|
44
|
+
throw Error("Source cannot be parsed");
|
|
45
|
+
const found: ts.Node[] = [];
|
|
46
|
+
const nameOf = (n: ts.Node) => {
|
|
47
|
+
const name = (n as ts.NamedDeclaration).name;
|
|
48
|
+
if (!name) return undefined;
|
|
49
|
+
const cls =
|
|
50
|
+
(ts.isMethodDeclaration(n) || ts.isPropertyDeclaration(n)) &&
|
|
51
|
+
ts.isClassDeclaration(n.parent)
|
|
52
|
+
? n.parent.name?.text
|
|
53
|
+
: undefined;
|
|
54
|
+
return cls ? `${cls}.${name.getText(ast)}` : name.getText(ast);
|
|
55
|
+
};
|
|
56
|
+
function visit(n: ts.Node) {
|
|
57
|
+
if (
|
|
58
|
+
(ts.isFunctionDeclaration(n) ||
|
|
59
|
+
ts.isMethodDeclaration(n) ||
|
|
60
|
+
ts.isVariableDeclaration(n) ||
|
|
61
|
+
ts.isInterfaceDeclaration(n) ||
|
|
62
|
+
ts.isTypeAliasDeclaration(n) ||
|
|
63
|
+
ts.isClassDeclaration(n) ||
|
|
64
|
+
ts.isPropertyDeclaration(n)) &&
|
|
65
|
+
(nameOf(n) === ref.symbol ||
|
|
66
|
+
(n as ts.NamedDeclaration).name?.getText(ast) === ref.symbol)
|
|
67
|
+
)
|
|
68
|
+
found.push(n);
|
|
69
|
+
ts.forEachChild(n, visit);
|
|
70
|
+
}
|
|
71
|
+
if (ref.symbol) visit(ast);
|
|
72
|
+
else if (ref.scope === "file" && contract) {
|
|
73
|
+
// A file-level interface exposes its exported declarations, never every private helper.
|
|
74
|
+
found.push(
|
|
75
|
+
...ast.statements.filter(
|
|
76
|
+
(n) =>
|
|
77
|
+
ts.canHaveModifiers(n) &&
|
|
78
|
+
ts
|
|
79
|
+
.getModifiers(n)
|
|
80
|
+
?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword),
|
|
81
|
+
),
|
|
82
|
+
);
|
|
83
|
+
} else
|
|
84
|
+
throw Error("No unique source symbol identifies the protected declaration");
|
|
85
|
+
if (!found.length) return undefined;
|
|
86
|
+
if (
|
|
87
|
+
found.length > 1 &&
|
|
88
|
+
!found.every(ts.isFunctionDeclaration) &&
|
|
89
|
+
!found.every(
|
|
90
|
+
(n) => ts.isMethodDeclaration(n) && n.parent === found[0].parent,
|
|
91
|
+
)
|
|
92
|
+
)
|
|
93
|
+
throw Error("Protected source symbol is ambiguous");
|
|
94
|
+
if (!contract)
|
|
95
|
+
return {
|
|
96
|
+
value: found.map((n) => print(n, ast)).join("\n"),
|
|
97
|
+
inferred: false,
|
|
98
|
+
};
|
|
99
|
+
let inferred = false;
|
|
100
|
+
const signatures = found.map((node) => {
|
|
101
|
+
const result = ts.transform(node, [
|
|
102
|
+
(context) => {
|
|
103
|
+
const strip: ts.Visitor = (n) => {
|
|
104
|
+
if (ts.isClassDeclaration(n)) {
|
|
105
|
+
const members = n.members.filter(
|
|
106
|
+
(m) =>
|
|
107
|
+
!(
|
|
108
|
+
ts.getCombinedModifierFlags(m as ts.Declaration) &
|
|
109
|
+
(ts.ModifierFlags.Private | ts.ModifierFlags.Protected) ||
|
|
110
|
+
(m.name && ts.isPrivateIdentifier(m.name))
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
return ts.factory.updateClassDeclaration(
|
|
114
|
+
n,
|
|
115
|
+
n.modifiers,
|
|
116
|
+
n.name,
|
|
117
|
+
n.typeParameters,
|
|
118
|
+
n.heritageClauses,
|
|
119
|
+
members.map((m) => ts.visitNode(m, strip) as ts.ClassElement),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
if (ts.isConstructorDeclaration(n))
|
|
123
|
+
return ts.factory.updateConstructorDeclaration(
|
|
124
|
+
n,
|
|
125
|
+
n.modifiers,
|
|
126
|
+
n.parameters,
|
|
127
|
+
undefined,
|
|
128
|
+
);
|
|
129
|
+
if (ts.isGetAccessorDeclaration(n)) {
|
|
130
|
+
inferred ||= !!n.body && !n.type;
|
|
131
|
+
return ts.factory.updateGetAccessorDeclaration(
|
|
132
|
+
n,
|
|
133
|
+
n.modifiers,
|
|
134
|
+
n.name,
|
|
135
|
+
n.parameters,
|
|
136
|
+
n.type,
|
|
137
|
+
undefined,
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
if (ts.isSetAccessorDeclaration(n))
|
|
141
|
+
return ts.factory.updateSetAccessorDeclaration(
|
|
142
|
+
n,
|
|
143
|
+
n.modifiers,
|
|
144
|
+
n.name,
|
|
145
|
+
n.parameters,
|
|
146
|
+
undefined,
|
|
147
|
+
);
|
|
148
|
+
if (ts.isPropertyDeclaration(n)) {
|
|
149
|
+
inferred ||= !!n.initializer && !n.type;
|
|
150
|
+
return ts.factory.updatePropertyDeclaration(
|
|
151
|
+
n,
|
|
152
|
+
n.modifiers,
|
|
153
|
+
n.name,
|
|
154
|
+
n.questionToken || n.exclamationToken,
|
|
155
|
+
n.type,
|
|
156
|
+
undefined,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
if (ts.isFunctionDeclaration(n)) {
|
|
160
|
+
inferred ||= !!n.body && !n.type;
|
|
161
|
+
return ts.factory.updateFunctionDeclaration(
|
|
162
|
+
n,
|
|
163
|
+
n.modifiers,
|
|
164
|
+
n.asteriskToken,
|
|
165
|
+
n.name,
|
|
166
|
+
n.typeParameters,
|
|
167
|
+
n.parameters,
|
|
168
|
+
n.type,
|
|
169
|
+
undefined,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
if (ts.isMethodDeclaration(n)) {
|
|
173
|
+
inferred ||= !!n.body && !n.type;
|
|
174
|
+
return ts.factory.updateMethodDeclaration(
|
|
175
|
+
n,
|
|
176
|
+
n.modifiers,
|
|
177
|
+
n.asteriskToken,
|
|
178
|
+
n.name,
|
|
179
|
+
n.questionToken,
|
|
180
|
+
n.typeParameters,
|
|
181
|
+
n.parameters,
|
|
182
|
+
n.type,
|
|
183
|
+
undefined,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
if (ts.isArrowFunction(n)) {
|
|
187
|
+
inferred ||= !n.type;
|
|
188
|
+
return ts.factory.updateArrowFunction(
|
|
189
|
+
n,
|
|
190
|
+
n.modifiers,
|
|
191
|
+
n.typeParameters,
|
|
192
|
+
n.parameters,
|
|
193
|
+
n.type,
|
|
194
|
+
n.equalsGreaterThanToken,
|
|
195
|
+
ts.factory.createBlock([]),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
if (ts.isFunctionExpression(n)) {
|
|
199
|
+
inferred ||= !n.type;
|
|
200
|
+
return ts.factory.updateFunctionExpression(
|
|
201
|
+
n,
|
|
202
|
+
n.modifiers,
|
|
203
|
+
n.asteriskToken,
|
|
204
|
+
n.name,
|
|
205
|
+
n.typeParameters,
|
|
206
|
+
n.parameters,
|
|
207
|
+
n.type,
|
|
208
|
+
ts.factory.createBlock([]),
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
return ts.visitEachChild(n, strip, context);
|
|
212
|
+
};
|
|
213
|
+
return (root) => ts.visitNode(root, strip) as typeof root;
|
|
214
|
+
},
|
|
215
|
+
]);
|
|
216
|
+
let statement: ts.Node = node;
|
|
217
|
+
while (statement.parent && statement.parent !== ast)
|
|
218
|
+
statement = statement.parent;
|
|
219
|
+
const exposure = ts.canHaveModifiers(statement)
|
|
220
|
+
? ts
|
|
221
|
+
.getModifiers(statement)
|
|
222
|
+
?.filter((m) =>
|
|
223
|
+
[
|
|
224
|
+
ts.SyntaxKind.ExportKeyword,
|
|
225
|
+
ts.SyntaxKind.DefaultKeyword,
|
|
226
|
+
].includes(m.kind),
|
|
227
|
+
)
|
|
228
|
+
.map((m) => print(m, ast))
|
|
229
|
+
.join(" ") || ""
|
|
230
|
+
: "";
|
|
231
|
+
const value = exposure + " " + print(result.transformed[0], ast);
|
|
232
|
+
result.dispose();
|
|
233
|
+
return value;
|
|
234
|
+
});
|
|
235
|
+
// Referenced local types and import bindings are part of the syntactic contract.
|
|
236
|
+
// Imported type definitions require a type check when dependencies have changed.
|
|
237
|
+
const referenced = new Set<string>();
|
|
238
|
+
const collect = (n: ts.Node) => {
|
|
239
|
+
if (ts.isTypeReferenceNode(n))
|
|
240
|
+
referenced.add(n.typeName.getText(n.getSourceFile()).split(".")[0]);
|
|
241
|
+
if (ts.isTypeQueryNode(n))
|
|
242
|
+
referenced.add(n.exprName.getText(n.getSourceFile()).split(".")[0]);
|
|
243
|
+
ts.forEachChild(n, collect);
|
|
244
|
+
};
|
|
245
|
+
// Parse the stripped declaration: implementation-only types are not part of its contract.
|
|
246
|
+
collect(
|
|
247
|
+
ts.createSourceFile(
|
|
248
|
+
"contract.ts",
|
|
249
|
+
signatures.join("\n"),
|
|
250
|
+
ts.ScriptTarget.Latest,
|
|
251
|
+
true,
|
|
252
|
+
),
|
|
253
|
+
);
|
|
254
|
+
const local = ast.statements.filter(
|
|
255
|
+
(n): n is ts.TypeAliasDeclaration | ts.InterfaceDeclaration =>
|
|
256
|
+
ts.isTypeAliasDeclaration(n) || ts.isInterfaceDeclaration(n),
|
|
257
|
+
);
|
|
258
|
+
const types: string[] = [];
|
|
259
|
+
let importedTypes = false;
|
|
260
|
+
const used = new Set<string>();
|
|
261
|
+
for (let pending = true; pending; ) {
|
|
262
|
+
pending = false;
|
|
263
|
+
for (const n of local)
|
|
264
|
+
if (referenced.has(n.name.text) && !used.has(n.name.text)) {
|
|
265
|
+
used.add(n.name.text);
|
|
266
|
+
types.push(print(n, ast));
|
|
267
|
+
collect(n);
|
|
268
|
+
pending = true;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
for (const n of ast.statements)
|
|
272
|
+
if (ts.isImportDeclaration(n)) {
|
|
273
|
+
const bindings = n.importClause?.namedBindings;
|
|
274
|
+
if (n.importClause?.name && referenced.has(n.importClause.name.text)) {
|
|
275
|
+
importedTypes = true;
|
|
276
|
+
types.push(
|
|
277
|
+
`default ${n.importClause.name.text} from ${n.moduleSpecifier.getText(ast)}`,
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
if (bindings && ts.isNamedImports(bindings))
|
|
281
|
+
for (const item of bindings.elements)
|
|
282
|
+
if (referenced.has(item.name.text))
|
|
283
|
+
types.push(
|
|
284
|
+
`${item.getText(ast)} from ${n.moduleSpecifier.getText(ast)}`,
|
|
285
|
+
);
|
|
286
|
+
if (
|
|
287
|
+
bindings &&
|
|
288
|
+
ts.isNamespaceImport(bindings) &&
|
|
289
|
+
referenced.has(bindings.name.text)
|
|
290
|
+
)
|
|
291
|
+
types.push(
|
|
292
|
+
`${bindings.getText(ast)} from ${n.moduleSpecifier.getText(ast)}`,
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
return {
|
|
296
|
+
value: signatures.join("\n") + "\n" + types.sort().join("\n"),
|
|
297
|
+
inferred,
|
|
298
|
+
importedTypes:
|
|
299
|
+
importedTypes || types.some((text) => text.includes(" from ")),
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
export function checkWorkLocks(
|
|
304
|
+
root: string,
|
|
305
|
+
plan: WorkPlan,
|
|
306
|
+
baseline: DefinitionGraph,
|
|
307
|
+
graph: DefinitionGraph,
|
|
308
|
+
changed: string[],
|
|
309
|
+
): LockResult[] {
|
|
310
|
+
const changedSet = new Set(changed);
|
|
311
|
+
const cache = new Map<string, string | undefined>();
|
|
312
|
+
function source(file: string, version: "base" | "saved" | "index") {
|
|
313
|
+
const key = `${version}:${file}`;
|
|
314
|
+
if (cache.has(key)) return cache.get(key);
|
|
315
|
+
let text: string | undefined;
|
|
316
|
+
try {
|
|
317
|
+
if (version !== "saved")
|
|
318
|
+
text = readGit(root, [
|
|
319
|
+
"show",
|
|
320
|
+
`${version === "base" ? plan.baseCommit : ""}:${file}`,
|
|
321
|
+
]);
|
|
322
|
+
else {
|
|
323
|
+
const path = checkedPath(root, file),
|
|
324
|
+
stat = lstatSync(path);
|
|
325
|
+
if (stat.isSymbolicLink() || stat.size > 1024 * 1024)
|
|
326
|
+
throw Error("Linked or oversized source");
|
|
327
|
+
text = readFileSync(resolve(path), "utf8");
|
|
328
|
+
}
|
|
329
|
+
if (text.length > 1024 * 1024 || text.includes("\0")) text = undefined;
|
|
330
|
+
} catch {
|
|
331
|
+
text = undefined;
|
|
332
|
+
}
|
|
333
|
+
cache.set(key, text);
|
|
334
|
+
return text;
|
|
335
|
+
}
|
|
336
|
+
return (plan.locks || []).map((lock) => {
|
|
337
|
+
const result: LockResult = {
|
|
338
|
+
target: lock.target,
|
|
339
|
+
kind: lock.kind,
|
|
340
|
+
status: "unchanged",
|
|
341
|
+
issues: [],
|
|
342
|
+
};
|
|
343
|
+
const issue = (
|
|
344
|
+
status: "violated" | "uncertain",
|
|
345
|
+
message: string,
|
|
346
|
+
file?: string,
|
|
347
|
+
) => {
|
|
348
|
+
if (result.status !== "violated") result.status = status;
|
|
349
|
+
result.issues.push({ ...(file ? { file } : {}), message });
|
|
350
|
+
};
|
|
351
|
+
const old = baseline.elements.find((n) => n.id === lock.target),
|
|
352
|
+
now = graph.elements.find((n) => n.id === lock.target);
|
|
353
|
+
if (!old) {
|
|
354
|
+
issue(
|
|
355
|
+
"uncertain",
|
|
356
|
+
"The starting map has no verified boundary for this lock.",
|
|
357
|
+
);
|
|
358
|
+
return result;
|
|
359
|
+
}
|
|
360
|
+
if (!now) {
|
|
361
|
+
issue("violated", "Locked boundary was removed from the map.");
|
|
362
|
+
return result;
|
|
363
|
+
}
|
|
364
|
+
const beforeIds =
|
|
365
|
+
lock.kind === "subtree"
|
|
366
|
+
? scopeBlocks(baseline, [lock.target])
|
|
367
|
+
: publicContractIds(baseline, lock.target);
|
|
368
|
+
const afterIds =
|
|
369
|
+
lock.kind === "subtree"
|
|
370
|
+
? scopeBlocks(graph, [lock.target])
|
|
371
|
+
: publicContractIds(graph, lock.target);
|
|
372
|
+
const beforeNodes = baseline.elements.filter((n) => beforeIds.has(n.id));
|
|
373
|
+
const afterNodes = graph.elements.filter((n) => afterIds.has(n.id));
|
|
374
|
+
const shape = (nodes: Element[]) =>
|
|
375
|
+
nodes
|
|
376
|
+
.map((n) => ({
|
|
377
|
+
id: n.id,
|
|
378
|
+
kind: n.kind,
|
|
379
|
+
parent: n.parent,
|
|
380
|
+
contract: n.contract,
|
|
381
|
+
signature: n.signature,
|
|
382
|
+
auth: n.attachments?.auth,
|
|
383
|
+
...(lock.kind === "subtree"
|
|
384
|
+
? { implementationBlock: n.implementationBlock }
|
|
385
|
+
: {}),
|
|
386
|
+
}))
|
|
387
|
+
.sort((a, b) => a.id.localeCompare(b.id));
|
|
388
|
+
if (stable(shape(beforeNodes)) !== stable(shape(afterNodes)))
|
|
389
|
+
issue(
|
|
390
|
+
"violated",
|
|
391
|
+
lock.kind === "contract"
|
|
392
|
+
? "Public boundary, signature or declared contract changed."
|
|
393
|
+
: "Locked subtree structure or contract changed.",
|
|
394
|
+
);
|
|
395
|
+
const sources = new Map(
|
|
396
|
+
beforeNodes
|
|
397
|
+
.flatMap((n) => [
|
|
398
|
+
...refs(n),
|
|
399
|
+
...(lock.kind === "subtree"
|
|
400
|
+
? [
|
|
401
|
+
...(n.attachments?.docs || []),
|
|
402
|
+
...(n.attachments?.tests || []),
|
|
403
|
+
].map(
|
|
404
|
+
(file): SourceRef => ({
|
|
405
|
+
file,
|
|
406
|
+
line: 1,
|
|
407
|
+
digest: "",
|
|
408
|
+
scope: "file" as const,
|
|
409
|
+
}),
|
|
410
|
+
)
|
|
411
|
+
: []),
|
|
412
|
+
])
|
|
413
|
+
.map((r) => [`${r.file}:${r.scope || r.symbol || r.line}`, r]),
|
|
414
|
+
);
|
|
415
|
+
for (const n of afterNodes)
|
|
416
|
+
for (const r of refs(n)) {
|
|
417
|
+
if (
|
|
418
|
+
![...sources.values()].some(
|
|
419
|
+
(old) =>
|
|
420
|
+
old.file === r.file &&
|
|
421
|
+
old.symbol === r.symbol &&
|
|
422
|
+
old.scope === r.scope,
|
|
423
|
+
)
|
|
424
|
+
)
|
|
425
|
+
issue(
|
|
426
|
+
"uncertain",
|
|
427
|
+
"New source ownership in a locked boundary needs review.",
|
|
428
|
+
r.file,
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
for (const ref of sources.values()) {
|
|
432
|
+
const dependencyChange =
|
|
433
|
+
lock.kind === "contract" &&
|
|
434
|
+
changed.some((f) => f !== ref.file && /\.[cm]?[jt]sx?$/.test(f));
|
|
435
|
+
if (!changedSet.has(ref.file) && !dependencyChange) continue;
|
|
436
|
+
const before = source(ref.file, "base");
|
|
437
|
+
const saved = source(ref.file, "saved"),
|
|
438
|
+
staged = source(ref.file, "index");
|
|
439
|
+
const versions: [string, string | undefined][] = [["Saved code", saved]];
|
|
440
|
+
if (staged !== saved) versions.push(["Staged code", staged]);
|
|
441
|
+
for (const [label, after] of versions) {
|
|
442
|
+
const sourceIssue = (
|
|
443
|
+
status: "violated" | "uncertain",
|
|
444
|
+
message: string,
|
|
445
|
+
file?: string,
|
|
446
|
+
) => issue(status, `${label}: ${message}`, file);
|
|
447
|
+
if (before === after && before !== undefined) {
|
|
448
|
+
if (dependencyChange && /\.[cm]?[jt]sx?$/.test(ref.file))
|
|
449
|
+
try {
|
|
450
|
+
const contract = declaration(before, ref, true);
|
|
451
|
+
if (!contract || contract.inferred || contract.importedTypes)
|
|
452
|
+
sourceIssue(
|
|
453
|
+
"uncertain",
|
|
454
|
+
"Changed type dependencies need a type/API check.",
|
|
455
|
+
ref.file,
|
|
456
|
+
);
|
|
457
|
+
} catch {
|
|
458
|
+
sourceIssue(
|
|
459
|
+
"uncertain",
|
|
460
|
+
"Dependent contract source cannot be verified.",
|
|
461
|
+
ref.file,
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
if (before === undefined) {
|
|
467
|
+
sourceIssue(
|
|
468
|
+
"uncertain",
|
|
469
|
+
"Starting source is unavailable for this lock.",
|
|
470
|
+
ref.file,
|
|
471
|
+
);
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
if (after === undefined) {
|
|
475
|
+
sourceIssue(
|
|
476
|
+
"violated",
|
|
477
|
+
"Locked source was removed or is unavailable.",
|
|
478
|
+
ref.file,
|
|
479
|
+
);
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (lock.kind === "subtree" && (ref.scope === "file" || !ref.symbol)) {
|
|
483
|
+
sourceIssue(
|
|
484
|
+
"violated",
|
|
485
|
+
"Source owned by the locked subtree changed.",
|
|
486
|
+
ref.file,
|
|
487
|
+
);
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (!/\.[cm]?[jt]sx?$/.test(ref.file)) {
|
|
491
|
+
sourceIssue(
|
|
492
|
+
"uncertain",
|
|
493
|
+
"This contract needs its language-specific check.",
|
|
494
|
+
ref.file,
|
|
495
|
+
);
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
try {
|
|
499
|
+
const b = declaration(before, ref, lock.kind === "contract"),
|
|
500
|
+
a = declaration(after, ref, lock.kind === "contract");
|
|
501
|
+
if (!b)
|
|
502
|
+
sourceIssue(
|
|
503
|
+
"uncertain",
|
|
504
|
+
"Starting source symbol cannot be verified.",
|
|
505
|
+
ref.file,
|
|
506
|
+
);
|
|
507
|
+
else if (!a || a.value !== b.value)
|
|
508
|
+
sourceIssue(
|
|
509
|
+
"violated",
|
|
510
|
+
lock.kind === "contract"
|
|
511
|
+
? "Public declaration changed."
|
|
512
|
+
: "Locked implementation changed.",
|
|
513
|
+
ref.file,
|
|
514
|
+
);
|
|
515
|
+
else if (lock.kind === "contract" && (b.inferred || a.inferred))
|
|
516
|
+
sourceIssue(
|
|
517
|
+
"uncertain",
|
|
518
|
+
"Return types are inferred; run a type/API check before asserting the contract is unchanged.",
|
|
519
|
+
ref.file,
|
|
520
|
+
);
|
|
521
|
+
else if (
|
|
522
|
+
lock.kind === "contract" &&
|
|
523
|
+
dependencyChange &&
|
|
524
|
+
(a.importedTypes || b.importedTypes)
|
|
525
|
+
)
|
|
526
|
+
sourceIssue(
|
|
527
|
+
"uncertain",
|
|
528
|
+
"Changed type dependencies need a type/API check.",
|
|
529
|
+
ref.file,
|
|
530
|
+
);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
sourceIssue(
|
|
533
|
+
"uncertain",
|
|
534
|
+
error instanceof Error
|
|
535
|
+
? error.message
|
|
536
|
+
: "Contract source unavailable.",
|
|
537
|
+
ref.file,
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (!sources.size && !old.contract && !old.signature)
|
|
543
|
+
issue(
|
|
544
|
+
"uncertain",
|
|
545
|
+
"No source or declared contract proves this boundary is unchanged.",
|
|
546
|
+
);
|
|
547
|
+
return result;
|
|
548
|
+
});
|
|
549
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { DefinitionGraph } from "./model";
|
|
2
|
+
import type { WorkLock } from "./work-model";
|
|
3
|
+
|
|
4
|
+
/** Public contracts can nest, but ordinary implementation children are not public
|
|
5
|
+
* merely because they sit inside an interface. Explicit exposure can bridge them. */
|
|
6
|
+
export function publicContractIds(graph: DefinitionGraph, target: string) {
|
|
7
|
+
const publicKinds = new Set(["interface", "method", "operation"]);
|
|
8
|
+
const byId = new Map(graph.elements.map((n) => [n.id, n]));
|
|
9
|
+
const children = new Map<string, string[]>();
|
|
10
|
+
const add = (parent: string, id: string) => {
|
|
11
|
+
if (!children.has(parent)) children.set(parent, []);
|
|
12
|
+
children.get(parent)!.push(id);
|
|
13
|
+
};
|
|
14
|
+
for (const n of graph.elements)
|
|
15
|
+
if (n.parent && publicKinds.has(n.kind)) add(n.parent, n.id);
|
|
16
|
+
for (const e of graph.connections)
|
|
17
|
+
if (
|
|
18
|
+
e.relation === "exposes" &&
|
|
19
|
+
publicKinds.has(byId.get(e.target)?.kind || "")
|
|
20
|
+
)
|
|
21
|
+
add(e.source, e.target);
|
|
22
|
+
const result = new Set<string>();
|
|
23
|
+
const queue = [target];
|
|
24
|
+
while (queue.length) {
|
|
25
|
+
const id = queue.pop()!;
|
|
26
|
+
if (result.has(id)) continue;
|
|
27
|
+
result.add(id);
|
|
28
|
+
queue.push(...(children.get(id) || []));
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** A subtree lock wins over every nested contract lock; inclusion never unlocks code. */
|
|
34
|
+
export function lockIndex(graph: DefinitionGraph, locks: WorkLock[]) {
|
|
35
|
+
const byId = new Map(graph.elements.map((n) => [n.id, n]));
|
|
36
|
+
const explicit = new Map(locks.map((l) => [l.target, l]));
|
|
37
|
+
const result = new Map<string, WorkLock>();
|
|
38
|
+
for (const lock of locks)
|
|
39
|
+
if (lock.kind === "contract")
|
|
40
|
+
for (const id of publicContractIds(graph, lock.target))
|
|
41
|
+
result.set(id, lock);
|
|
42
|
+
for (const node of graph.elements) {
|
|
43
|
+
const seen = new Set<string>();
|
|
44
|
+
let cursor: string | undefined = node.id;
|
|
45
|
+
let effective = explicit.get(node.id) || result.get(node.id);
|
|
46
|
+
while (cursor && !seen.has(cursor)) {
|
|
47
|
+
seen.add(cursor);
|
|
48
|
+
const lock = explicit.get(cursor);
|
|
49
|
+
if (lock?.kind === "subtree") effective = lock;
|
|
50
|
+
cursor = byId.get(cursor)?.parent;
|
|
51
|
+
}
|
|
52
|
+
if (effective) result.set(node.id, effective);
|
|
53
|
+
}
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
export function lockFor(graph: DefinitionGraph, locks: WorkLock[], id: string) {
|
|
57
|
+
return lockIndex(graph, locks).get(id);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function validateLocks(
|
|
61
|
+
graph: DefinitionGraph,
|
|
62
|
+
input: unknown,
|
|
63
|
+
previous: WorkLock[] = [],
|
|
64
|
+
): WorkLock[] {
|
|
65
|
+
if (!Array.isArray(input) || input.length > 500)
|
|
66
|
+
throw Error("Choose up to 500 block or interface locks.");
|
|
67
|
+
const targets = new Set<string>();
|
|
68
|
+
return input.map((value) => {
|
|
69
|
+
if (!value || typeof value !== "object") throw Error("Invalid lock.");
|
|
70
|
+
const { target, kind, note } = value;
|
|
71
|
+
if (typeof target !== "string" || !["subtree", "contract"].includes(kind))
|
|
72
|
+
throw Error("Choose a block subtree or interface contract to lock.");
|
|
73
|
+
if (targets.has(target)) throw Error("A target can only have one lock.");
|
|
74
|
+
targets.add(target);
|
|
75
|
+
const node = graph.elements.find((n) => n.id === target);
|
|
76
|
+
// Deleted targets remain locked and reviewable; adding an unknown target is rejected.
|
|
77
|
+
if (!node && !previous.some((l) => l.target === target && l.kind === kind))
|
|
78
|
+
throw Error(`Unknown lock target: ${target}`);
|
|
79
|
+
if (
|
|
80
|
+
node &&
|
|
81
|
+
(node.provenance === "proposed" ||
|
|
82
|
+
(node.kind === "interface" ? kind !== "contract" : kind !== "subtree"))
|
|
83
|
+
)
|
|
84
|
+
throw Error(
|
|
85
|
+
"Lock an interface's public contract or a real block's whole subtree.",
|
|
86
|
+
);
|
|
87
|
+
if (note !== undefined && (typeof note !== "string" || note.length > 4000))
|
|
88
|
+
throw Error("Keep the lock reason below 4,000 characters.");
|
|
89
|
+
return { target, kind, ...(note?.trim() ? { note: note.trim() } : {}) };
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function lockBrief(graph: DefinitionGraph, locks: WorkLock[]) {
|
|
94
|
+
if (!locks.length) return [];
|
|
95
|
+
return [
|
|
96
|
+
"## Locked boundaries",
|
|
97
|
+
"Locks take precedence over the editable scope and proposed changes. Do not remove or weaken a lock to make a check pass.",
|
|
98
|
+
...locks.map((lock) => {
|
|
99
|
+
const node = graph.elements.find((n) => n.id === lock.target);
|
|
100
|
+
return `- ${node?.label || lock.target} (${lock.target}): ${
|
|
101
|
+
lock.kind === "contract"
|
|
102
|
+
? "Freeze the public contract exactly: names, routes, signatures, inputs, outputs and explicitly exposed nested contracts. Internal implementation may change; this is not permission to change a signature even compatibly."
|
|
103
|
+
: "Freeze this block and its entire subtree, including code, interfaces and nested blocks."
|
|
104
|
+
}${lock.note ? ` Reason: ${lock.note}` : ""}`;
|
|
105
|
+
}),
|
|
106
|
+
"",
|
|
107
|
+
];
|
|
108
|
+
}
|