@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.
- 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 +118 -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 +1083 -0
- package/dist/compose.d.ts +9 -0
- package/dist/compose.js +72 -0
- package/dist/core.d.ts +86 -0
- package/dist/core.js +206 -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 +66 -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 +4 -0
- package/dist/factory-graph.js +318 -0
- package/dist/factory-model.d.ts +97 -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/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 +14 -0
- package/dist/map-quality.js +56 -0
- package/dist/mapping-coverage.d.ts +87 -0
- package/dist/mapping-coverage.js +404 -0
- package/dist/model.d.ts +182 -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 +69 -0
- package/dist/product-map.js +168 -0
- package/dist/provider-access.cjs +207 -0
- package/dist/provider-access.d.cts +14 -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 +151 -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 +12 -0
- package/dist/scope.js +94 -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 +166 -0
- package/dist/source-review-git.d.ts +7 -0
- package/dist/source-review-git.js +103 -0
- package/dist/source-review.d.ts +16 -0
- package/dist/source-review.js +493 -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 +17 -0
- package/dist/work-check.js +110 -0
- package/dist/work-cli.d.ts +1 -0
- package/dist/work-cli.js +148 -0
- package/dist/work-context.d.ts +57 -0
- package/dist/work-context.js +568 -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-model.d.ts +110 -0
- package/dist/work-model.js +261 -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 +481 -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 +1326 -0
- package/src/compose.ts +94 -0
- package/src/core.ts +357 -0
- package/src/coverage-cli.ts +52 -0
- package/src/declaration-sources.ts +69 -0
- package/src/declared-graph.ts +74 -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 +378 -0
- package/src/factory-model.ts +73 -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/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 +61 -0
- package/src/mapping-coverage.ts +570 -0
- package/src/model.ts +221 -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 +236 -0
- package/src/provider-access.cjs +207 -0
- package/src/provider-access.d.cts +14 -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 +189 -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 +103 -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 +205 -0
- package/src/source-review-git.ts +129 -0
- package/src/source-review.ts +637 -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 +143 -0
- package/src/work-cli.ts +195 -0
- package/src/work-context.ts +713 -0
- package/src/work-intents.ts +196 -0
- package/src/work-items.ts +151 -0
- package/src/work-model.ts +400 -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 +305 -0
- package/toolkit/SKILL.md +131 -0
- package/toolkit/bin/fractory +2 -0
- package/toolkit/build.json +1 -0
package/dist/review.js
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { logicalMap } from "./map-model.js";
|
|
2
|
+
import { graphIndex, connectionEvidence } from "./change-impact.js";
|
|
3
|
+
export const reviewFacets = [
|
|
4
|
+
"internal",
|
|
5
|
+
"interfaces",
|
|
6
|
+
"structure",
|
|
7
|
+
"metadata",
|
|
8
|
+
"source",
|
|
9
|
+
];
|
|
10
|
+
const empty = () => ({
|
|
11
|
+
internal: 0,
|
|
12
|
+
interfaces: 0,
|
|
13
|
+
structure: 0,
|
|
14
|
+
metadata: 0,
|
|
15
|
+
source: 0,
|
|
16
|
+
consumers: 0,
|
|
17
|
+
});
|
|
18
|
+
/** A review checksum, not a security or source integrity hash. */
|
|
19
|
+
function fingerprint(value) {
|
|
20
|
+
const text = JSON.stringify(value);
|
|
21
|
+
let a = 2166136261, b = 5381;
|
|
22
|
+
for (let i = 0; i < text.length; i++) {
|
|
23
|
+
a = Math.imul(a ^ text.charCodeAt(i), 16777619);
|
|
24
|
+
b = Math.imul(b, 33) ^ text.charCodeAt(i);
|
|
25
|
+
}
|
|
26
|
+
return `${(a >>> 0).toString(16)}${(b >>> 0).toString(16)}:${text.length}`;
|
|
27
|
+
}
|
|
28
|
+
function normalized(value) {
|
|
29
|
+
if (Array.isArray(value))
|
|
30
|
+
return value.map(normalized);
|
|
31
|
+
if (!value || typeof value !== "object")
|
|
32
|
+
return value;
|
|
33
|
+
return Object.fromEntries(Object.entries(value)
|
|
34
|
+
.filter(([key]) => ![
|
|
35
|
+
"source",
|
|
36
|
+
"sources",
|
|
37
|
+
"sourceRef",
|
|
38
|
+
"digest",
|
|
39
|
+
"line",
|
|
40
|
+
"definitionRevision",
|
|
41
|
+
"generatedAt",
|
|
42
|
+
].includes(key))
|
|
43
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
44
|
+
.map(([key, item]) => [key, normalized(item)]));
|
|
45
|
+
}
|
|
46
|
+
export function factoryReview(input, indirect = false) {
|
|
47
|
+
const exact = input.schema === "fractal.comparison.v1" ? input : undefined;
|
|
48
|
+
const footprint = input.schema === "fractal.footprint.v1" ? input : undefined;
|
|
49
|
+
const graph = logicalMap(input.graph), { byId, within } = graphIndex(graph);
|
|
50
|
+
const ancestors = (id) => {
|
|
51
|
+
const result = [], seen = new Set();
|
|
52
|
+
while (byId.has(id) && !seen.has(id)) {
|
|
53
|
+
result.push(id);
|
|
54
|
+
seen.add(id);
|
|
55
|
+
id = byId.get(id)?.parent || "";
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
|
+
const direct = {}, files = {}, evidence = new Map();
|
|
60
|
+
const add = (id, facet, item) => {
|
|
61
|
+
if (!byId.has(id))
|
|
62
|
+
return;
|
|
63
|
+
direct[id] = [...new Set([...(direct[id] || []), facet])];
|
|
64
|
+
if (item !== undefined)
|
|
65
|
+
evidence.set(id, [...(evidence.get(id) || []), item]);
|
|
66
|
+
};
|
|
67
|
+
const interfaceOf = (id) => ancestors(id).find((parent) => byId.get(parent)?.kind === "interface");
|
|
68
|
+
if (exact) {
|
|
69
|
+
for (const [id, change] of Object.entries(exact.elements)) {
|
|
70
|
+
const node = change.after || change.before;
|
|
71
|
+
const source = change.source.map((part) => ({
|
|
72
|
+
side: part.side,
|
|
73
|
+
file: part.file,
|
|
74
|
+
digest: part.digest || exact.after.commit,
|
|
75
|
+
}));
|
|
76
|
+
const item = [
|
|
77
|
+
normalized(change.before),
|
|
78
|
+
normalized(change.after),
|
|
79
|
+
change.fields,
|
|
80
|
+
source,
|
|
81
|
+
];
|
|
82
|
+
if (change.kind !== "modified")
|
|
83
|
+
add(id, "structure", item);
|
|
84
|
+
if (change.fields.some((field) => ["Placement", "Kind"].includes(field)))
|
|
85
|
+
add(id, "structure", item);
|
|
86
|
+
if (change.fields.some((field) => ["Code", "Shared source"].includes(field)))
|
|
87
|
+
add(id, "internal", item);
|
|
88
|
+
if (change.fields.some((field) => ["Contract", "Contract types", "Auth", "Auth source"].includes(field)) ||
|
|
89
|
+
(node.kind === "interface" && change.kind !== "modified")) {
|
|
90
|
+
const boundary = interfaceOf(id);
|
|
91
|
+
if (boundary)
|
|
92
|
+
add(boundary, "interfaces", item);
|
|
93
|
+
else
|
|
94
|
+
add(id, "internal", item);
|
|
95
|
+
}
|
|
96
|
+
if (change.fields.some((field) => ![
|
|
97
|
+
"Code",
|
|
98
|
+
"Shared source",
|
|
99
|
+
"Contract",
|
|
100
|
+
"Contract types",
|
|
101
|
+
"Auth",
|
|
102
|
+
"Auth source",
|
|
103
|
+
"Placement",
|
|
104
|
+
"Kind",
|
|
105
|
+
].includes(field)))
|
|
106
|
+
add(id, "metadata", item);
|
|
107
|
+
}
|
|
108
|
+
for (const [id, change] of Object.entries(exact.connections)) {
|
|
109
|
+
const edge = change.after || change.before;
|
|
110
|
+
// The wire itself owns one change; both endpoint containers can summarize it.
|
|
111
|
+
for (const endpoint of new Set([edge.source, edge.target]))
|
|
112
|
+
add(endpoint, "structure", [id, normalized(change)]);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
const unmapped = [];
|
|
116
|
+
if (footprint?.concepts) {
|
|
117
|
+
for (const change of footprint.concepts.changes) {
|
|
118
|
+
add(change.block, {
|
|
119
|
+
implementation: "internal",
|
|
120
|
+
interface: "interfaces",
|
|
121
|
+
connection: "structure",
|
|
122
|
+
mapping: "source",
|
|
123
|
+
support: "metadata",
|
|
124
|
+
}[change.kind], change);
|
|
125
|
+
files[change.block] = [
|
|
126
|
+
...new Set([...(files[change.block] || []), change.file]),
|
|
127
|
+
];
|
|
128
|
+
if (change.connection)
|
|
129
|
+
add(change.connection.target, "structure", change);
|
|
130
|
+
}
|
|
131
|
+
unmapped.push(...footprint.concepts.unassigned);
|
|
132
|
+
}
|
|
133
|
+
else if (footprint) {
|
|
134
|
+
const associations = new Map();
|
|
135
|
+
const associate = (file, id) => {
|
|
136
|
+
const ids = associations.get(file) || new Set();
|
|
137
|
+
ids.add(id);
|
|
138
|
+
associations.set(file, ids);
|
|
139
|
+
};
|
|
140
|
+
for (const node of graph.elements) {
|
|
141
|
+
for (const ref of node.sources || (node.source ? [node.source] : []))
|
|
142
|
+
associate(ref.file, node.id);
|
|
143
|
+
for (const file of node.attachments?.docs || [])
|
|
144
|
+
associate(file.split("#")[0], node.id);
|
|
145
|
+
if (node.attachments?.auth?.source)
|
|
146
|
+
associate(node.attachments.auth.source.file, node.id);
|
|
147
|
+
}
|
|
148
|
+
for (const test of graph.tests)
|
|
149
|
+
for (const target of test.targets)
|
|
150
|
+
associate(test.source.file, target);
|
|
151
|
+
for (const edge of graph.connections)
|
|
152
|
+
for (const ref of edge.sources ||
|
|
153
|
+
(edge.sourceRef ? [edge.sourceRef] : []))
|
|
154
|
+
associate(ref.file, edge.source);
|
|
155
|
+
for (const file of footprint.files) {
|
|
156
|
+
const matches = [...(associations.get(file.path) || [])];
|
|
157
|
+
if (!matches.length) {
|
|
158
|
+
unmapped.push(file.path);
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
// File association is conservative and may include several logical siblings.
|
|
162
|
+
const leaves = matches.filter((id) => !matches.some((other) => other !== id && within(other, id)));
|
|
163
|
+
for (const id of leaves) {
|
|
164
|
+
add(id, "source", [file, file.digest ? undefined : footprint.head]);
|
|
165
|
+
files[id] = [...(files[id] || []), file.path];
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
const seeds = Object.keys(direct).filter((id) => direct[id].includes("interfaces") ||
|
|
170
|
+
(footprint && !footprint.concepts && direct[id].includes("source")));
|
|
171
|
+
const links = graph.connections.flatMap((edge) => {
|
|
172
|
+
const [provider, consumer] = [
|
|
173
|
+
"flows",
|
|
174
|
+
"writes",
|
|
175
|
+
"produces",
|
|
176
|
+
"resumes",
|
|
177
|
+
].includes(edge.relation)
|
|
178
|
+
? [edge.source, edge.target]
|
|
179
|
+
: [edge.target, edge.source];
|
|
180
|
+
return [
|
|
181
|
+
{ provider, consumer, edge },
|
|
182
|
+
...(edge.bidirectional
|
|
183
|
+
? [{ provider: consumer, consumer: provider, edge }]
|
|
184
|
+
: []),
|
|
185
|
+
];
|
|
186
|
+
});
|
|
187
|
+
const consumers = [], connections = new Set();
|
|
188
|
+
const reached = new Set(seeds);
|
|
189
|
+
let frontier = seeds;
|
|
190
|
+
for (let distance = 1; frontier.length && distance <= graph.elements.length; distance++) {
|
|
191
|
+
const next = [];
|
|
192
|
+
for (const link of links) {
|
|
193
|
+
const provider = frontier.find((id) => within(link.provider, id));
|
|
194
|
+
if (!provider ||
|
|
195
|
+
within(link.consumer, provider) ||
|
|
196
|
+
reached.has(link.consumer))
|
|
197
|
+
continue;
|
|
198
|
+
connections.add(link.edge.id);
|
|
199
|
+
consumers.push({
|
|
200
|
+
id: link.consumer,
|
|
201
|
+
provider,
|
|
202
|
+
via: link.edge.id,
|
|
203
|
+
distance,
|
|
204
|
+
evidence: connectionEvidence(link.edge),
|
|
205
|
+
});
|
|
206
|
+
reached.add(link.consumer);
|
|
207
|
+
next.push(link.consumer);
|
|
208
|
+
}
|
|
209
|
+
if (!indirect)
|
|
210
|
+
break;
|
|
211
|
+
frontier = next;
|
|
212
|
+
}
|
|
213
|
+
const marks = {}, items = new Map();
|
|
214
|
+
const sourceFiles = new Map();
|
|
215
|
+
for (const [id, facets] of Object.entries(direct)) {
|
|
216
|
+
const parents = new Set(ancestors(id));
|
|
217
|
+
// Moves also light the old container; a review must not hide what left it.
|
|
218
|
+
let oldParent = exact?.elements[id]?.before?.parent;
|
|
219
|
+
const seen = new Set();
|
|
220
|
+
while (oldParent && byId.has(oldParent) && !seen.has(oldParent)) {
|
|
221
|
+
seen.add(oldParent);
|
|
222
|
+
parents.add(oldParent);
|
|
223
|
+
oldParent =
|
|
224
|
+
exact?.elements[oldParent]?.before?.parent ||
|
|
225
|
+
byId.get(oldParent)?.parent;
|
|
226
|
+
}
|
|
227
|
+
for (const parent of parents) {
|
|
228
|
+
const mark = (marks[parent] ||= empty());
|
|
229
|
+
for (const facet of facets)
|
|
230
|
+
mark[facet]++;
|
|
231
|
+
if (facets.includes("source")) {
|
|
232
|
+
const paths = sourceFiles.get(parent) || new Set();
|
|
233
|
+
for (const file of files[id] || [])
|
|
234
|
+
paths.add(file);
|
|
235
|
+
sourceFiles.set(parent, paths);
|
|
236
|
+
mark.source = paths.size;
|
|
237
|
+
}
|
|
238
|
+
items.set(parent, [
|
|
239
|
+
...(items.get(parent) || []),
|
|
240
|
+
[id, facets, evidence.get(id)],
|
|
241
|
+
]);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
for (const reason of consumers)
|
|
245
|
+
for (const parent of ancestors(reason.id)) {
|
|
246
|
+
(marks[parent] ||= empty()).consumers++;
|
|
247
|
+
items.set(parent, [...(items.get(parent) || []), reason]);
|
|
248
|
+
}
|
|
249
|
+
const fingerprints = Object.fromEntries([...items].map(([id, values]) => [id, fingerprint(values)]));
|
|
250
|
+
return {
|
|
251
|
+
graph,
|
|
252
|
+
key: `${graph.id}:${exact?.before.commit || footprint.base}`,
|
|
253
|
+
basis: exact
|
|
254
|
+
? "snapshots"
|
|
255
|
+
: footprint?.concepts
|
|
256
|
+
? "working-code"
|
|
257
|
+
: footprint?.mappingRevision
|
|
258
|
+
? "head-source"
|
|
259
|
+
: "current-map",
|
|
260
|
+
direct,
|
|
261
|
+
marks,
|
|
262
|
+
consumers,
|
|
263
|
+
connectionIds: [...connections],
|
|
264
|
+
fingerprints,
|
|
265
|
+
files,
|
|
266
|
+
unmapped,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
/** A parent review is valid only while all of its scoped evidence is unchanged. */
|
|
270
|
+
export function reviewedScope(review, saved, id) {
|
|
271
|
+
const { within } = graphIndex(review.graph);
|
|
272
|
+
return Object.keys(saved).some((parent) => within(id, parent) && saved[parent] === review.fingerprints[parent]);
|
|
273
|
+
}
|
|
274
|
+
export function reviewUnits(review) {
|
|
275
|
+
const { byId } = graphIndex(review.graph);
|
|
276
|
+
const units = new Set();
|
|
277
|
+
for (const id of new Set([
|
|
278
|
+
...Object.keys(review.direct),
|
|
279
|
+
...review.consumers.map((reason) => reason.id),
|
|
280
|
+
])) {
|
|
281
|
+
let node = byId.get(id);
|
|
282
|
+
while (node && !["block", "process"].includes(node.kind))
|
|
283
|
+
node = byId.get(node.parent || "");
|
|
284
|
+
if (node)
|
|
285
|
+
units.add(node.id);
|
|
286
|
+
}
|
|
287
|
+
// Ancestor-owned edits are not covered by reviewing only a descendant.
|
|
288
|
+
return [...units];
|
|
289
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { DefinitionGraph } from "./model.js";
|
|
2
|
+
import type { TimeWindow } from "./time-window.js";
|
|
3
|
+
import type { RuntimeMetrics } from "./performance-model.js";
|
|
4
|
+
export interface MetricsConnection {
|
|
5
|
+
url?: string;
|
|
6
|
+
token?: string;
|
|
7
|
+
username?: string;
|
|
8
|
+
environment?: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function metricsConnection(env?: NodeJS.ProcessEnv): MetricsConnection;
|
|
11
|
+
type PromSeries = {
|
|
12
|
+
metric: Record<string, string>;
|
|
13
|
+
value?: [number, string];
|
|
14
|
+
values?: [number, string][];
|
|
15
|
+
};
|
|
16
|
+
export declare class PrometheusReader {
|
|
17
|
+
private configuration;
|
|
18
|
+
private transport;
|
|
19
|
+
constructor(configuration: MetricsConnection, transport?: typeof fetch);
|
|
20
|
+
query(query: string, at: string, range?: {
|
|
21
|
+
from: string;
|
|
22
|
+
step: number;
|
|
23
|
+
}): Promise<PromSeries[]>;
|
|
24
|
+
}
|
|
25
|
+
export declare function readRuntimeMetrics(root: string, graph: DefinitionGraph, window: TimeWindow, connection?: MetricsConnection, transport?: typeof fetch): Promise<RuntimeMetrics>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import { readFileSync, existsSync, statSync } from "node:fs";
|
|
2
|
+
import { checkedPath } from "./workspace-notes.js";
|
|
3
|
+
export function metricsConnection(env = process.env) {
|
|
4
|
+
return {
|
|
5
|
+
url: env.FRACTAL_METRICS_URL,
|
|
6
|
+
token: env.FRACTAL_METRICS_TOKEN,
|
|
7
|
+
username: env.FRACTAL_METRICS_USERNAME,
|
|
8
|
+
environment: env.FRACTAL_METRICS_ENVIRONMENT || "production",
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export class PrometheusReader {
|
|
12
|
+
configuration;
|
|
13
|
+
transport;
|
|
14
|
+
constructor(configuration, transport = fetch) {
|
|
15
|
+
this.configuration = configuration;
|
|
16
|
+
this.transport = transport;
|
|
17
|
+
}
|
|
18
|
+
async query(query, at, range) {
|
|
19
|
+
if (!this.configuration.url)
|
|
20
|
+
throw Error("Connect a Prometheus metrics source in Integrations.");
|
|
21
|
+
const base = new URL(this.configuration.url);
|
|
22
|
+
if (base.username ||
|
|
23
|
+
base.password ||
|
|
24
|
+
(base.protocol !== "https:" &&
|
|
25
|
+
!(base.protocol === "http:" &&
|
|
26
|
+
["127.0.0.1", "localhost", "[::1]"].includes(base.hostname))))
|
|
27
|
+
throw Error("Use HTTPS, or a local Prometheus endpoint.");
|
|
28
|
+
const url = new URL(base.pathname.replace(/\/$/, "") +
|
|
29
|
+
"/api/v1/" +
|
|
30
|
+
(range ? "query_range" : "query"), base.origin);
|
|
31
|
+
url.searchParams.set("query", query);
|
|
32
|
+
if (range) {
|
|
33
|
+
url.searchParams.set("start", range.from);
|
|
34
|
+
url.searchParams.set("end", at);
|
|
35
|
+
url.searchParams.set("step", String(range.step));
|
|
36
|
+
}
|
|
37
|
+
else
|
|
38
|
+
url.searchParams.set("time", at);
|
|
39
|
+
const headers = { Accept: "application/json" };
|
|
40
|
+
if (this.configuration.token)
|
|
41
|
+
headers.Authorization = this.configuration.username
|
|
42
|
+
? "Basic " +
|
|
43
|
+
Buffer.from(this.configuration.username + ":" + this.configuration.token).toString("base64")
|
|
44
|
+
: "Bearer " + this.configuration.token;
|
|
45
|
+
const response = await this.transport(url, {
|
|
46
|
+
headers,
|
|
47
|
+
redirect: "error",
|
|
48
|
+
signal: AbortSignal.timeout(15000),
|
|
49
|
+
});
|
|
50
|
+
if (!response.ok)
|
|
51
|
+
throw Object.assign(Error(`Metrics source returned ${response.status}. Check its read credential and URL.`), { status: response.status });
|
|
52
|
+
const reader = response.body?.getReader();
|
|
53
|
+
if (!reader)
|
|
54
|
+
throw Error("Empty metrics response");
|
|
55
|
+
const chunks = [];
|
|
56
|
+
let size = 0;
|
|
57
|
+
while (true) {
|
|
58
|
+
const { done, value } = await reader.read();
|
|
59
|
+
if (done)
|
|
60
|
+
break;
|
|
61
|
+
size += value.length;
|
|
62
|
+
if (size > 6 * 1024 * 1024) {
|
|
63
|
+
await reader.cancel();
|
|
64
|
+
throw Error("Metrics query exceeds the 6 MB read limit. Narrow the selected period.");
|
|
65
|
+
}
|
|
66
|
+
chunks.push(value);
|
|
67
|
+
}
|
|
68
|
+
let payload;
|
|
69
|
+
try {
|
|
70
|
+
payload = JSON.parse(Buffer.concat(chunks).toString());
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
throw Error("Metrics source returned an invalid response.");
|
|
74
|
+
}
|
|
75
|
+
if (payload.status !== "success" || !Array.isArray(payload.data?.result))
|
|
76
|
+
throw Error("The metrics source rejected this query.");
|
|
77
|
+
if (payload.data.result.length > 24000)
|
|
78
|
+
throw Error("Metrics query returned more than 24,000 series.");
|
|
79
|
+
return payload.data.result;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function gaugeDefinitions(root, graph) {
|
|
83
|
+
const path = checkedPath(root, "fractory.metrics.json", true);
|
|
84
|
+
if (!existsSync(path))
|
|
85
|
+
return [];
|
|
86
|
+
if (statSync(path).size > 64000)
|
|
87
|
+
throw Error("Keep fractory.metrics.json below 64 KB");
|
|
88
|
+
const data = JSON.parse(readFileSync(path, "utf8"));
|
|
89
|
+
if (data.schema !== "fractory.metrics.v1" ||
|
|
90
|
+
!Array.isArray(data.gauges) ||
|
|
91
|
+
data.gauges.length > 12)
|
|
92
|
+
throw Error("Invalid fractory.metrics.json");
|
|
93
|
+
const ids = new Set(graph.elements.map((node) => node.id));
|
|
94
|
+
for (const gauge of data.gauges)
|
|
95
|
+
if (!/^[a-z][a-z0-9_-]{0,40}$/.test(gauge.id) ||
|
|
96
|
+
typeof gauge.label !== "string" ||
|
|
97
|
+
gauge.label.length > 80 ||
|
|
98
|
+
typeof gauge.unit !== "string" ||
|
|
99
|
+
gauge.unit.length > 24 ||
|
|
100
|
+
!["sum", "max"].includes(gauge.aggregation) ||
|
|
101
|
+
(gauge.description !== undefined &&
|
|
102
|
+
(typeof gauge.description !== "string" ||
|
|
103
|
+
gauge.description.length > 300)) ||
|
|
104
|
+
(gauge.chart !== undefined && !["line", "bar"].includes(gauge.chart)) ||
|
|
105
|
+
!Array.isArray(gauge.sources) ||
|
|
106
|
+
gauge.sources.length > 20 ||
|
|
107
|
+
gauge.sources.some((source) => !ids.has(source.block) ||
|
|
108
|
+
typeof source.query !== "string" ||
|
|
109
|
+
source.query.length > 1000))
|
|
110
|
+
throw Error("Choose valid gauge labels, units, aggregation and mapped blocks.");
|
|
111
|
+
if (data.gauges.reduce((sum, gauge) => sum + gauge.sources.length, 0) > 20)
|
|
112
|
+
throw Error("Keep gauge attachments to at most 20 source queries.");
|
|
113
|
+
return data.gauges;
|
|
114
|
+
}
|
|
115
|
+
export async function readRuntimeMetrics(root, graph, window, connection = metricsConnection(), transport) {
|
|
116
|
+
const output = {
|
|
117
|
+
source: "prometheus",
|
|
118
|
+
configured: !!connection.url,
|
|
119
|
+
window,
|
|
120
|
+
environment: connection.environment || "production",
|
|
121
|
+
checkedAt: new Date().toISOString(),
|
|
122
|
+
boundaries: [],
|
|
123
|
+
gauges: [],
|
|
124
|
+
warnings: [],
|
|
125
|
+
};
|
|
126
|
+
const definitions = existsSync(root) ? gaugeDefinitions(root, graph) : [];
|
|
127
|
+
output.gauges = definitions.map((d) => ({
|
|
128
|
+
...d,
|
|
129
|
+
status: "empty",
|
|
130
|
+
values: [],
|
|
131
|
+
}));
|
|
132
|
+
if (!connection.url)
|
|
133
|
+
return output;
|
|
134
|
+
const seconds = Math.ceil((Date.parse(window.to) - Date.parse(window.from)) / 1000), step = Math.max(60, Math.ceil(seconds / 32));
|
|
135
|
+
const selector = `{fractory_environment=${JSON.stringify(output.environment)}}`;
|
|
136
|
+
const labels = "fractory_target,fractory_caller";
|
|
137
|
+
const reader = new PrometheusReader(connection, transport);
|
|
138
|
+
const queried = await Promise.allSettled([
|
|
139
|
+
reader.query(`sum by (${labels},fractory_outcome) (increase(fractory_calls_total${selector}[${seconds}s]))`, window.to),
|
|
140
|
+
reader.query(`sum by (${labels},le) (increase(fractory_duration_seconds_bucket${selector}[${seconds}s]))`, window.to),
|
|
141
|
+
reader.query(`sum by (${labels},fractory_outcome) (rate(fractory_calls_total${selector}[${Math.max(60, step * 2)}s]))`, window.to, { from: window.from, step }),
|
|
142
|
+
reader.query(`max(timestamp(fractory_calls_total${selector})) by ()`, window.to),
|
|
143
|
+
reader.query(`sum by (${labels},le) (rate(fractory_duration_seconds_bucket${selector}[${Math.max(300, step * 2)}s]))`, window.to, { from: window.from, step }),
|
|
144
|
+
]);
|
|
145
|
+
if (queried[0].status === "rejected" &&
|
|
146
|
+
[401, 403].includes(queried[0].reason?.status))
|
|
147
|
+
throw queried[0].reason;
|
|
148
|
+
const [counts, buckets, trend, latest, latency] = queried.map((result, index) => {
|
|
149
|
+
if (result.status === "fulfilled")
|
|
150
|
+
return result.value;
|
|
151
|
+
output.warnings.push(`${["Calls", "Latency", "Trend", "Last sample", "Latency trend"][index]} unavailable: ${result.reason instanceof Error ? result.reason.message : "query failed"}`);
|
|
152
|
+
return [];
|
|
153
|
+
});
|
|
154
|
+
const rows = new Map();
|
|
155
|
+
const row = (metric) => {
|
|
156
|
+
if (!metric.fractory_target)
|
|
157
|
+
return undefined;
|
|
158
|
+
const caller = metric.fractory_caller === "external"
|
|
159
|
+
? undefined
|
|
160
|
+
: metric.fractory_caller;
|
|
161
|
+
const key = JSON.stringify([metric.fractory_target, caller]);
|
|
162
|
+
let value = rows.get(key);
|
|
163
|
+
if (!value) {
|
|
164
|
+
value = {
|
|
165
|
+
target: metric.fractory_target,
|
|
166
|
+
caller,
|
|
167
|
+
calls: 0,
|
|
168
|
+
callsKnown: false,
|
|
169
|
+
errors: 0,
|
|
170
|
+
buckets: [],
|
|
171
|
+
trend: [],
|
|
172
|
+
};
|
|
173
|
+
rows.set(key, value);
|
|
174
|
+
}
|
|
175
|
+
return value;
|
|
176
|
+
};
|
|
177
|
+
for (const series of counts) {
|
|
178
|
+
const target = row(series.metric), value = Number(series.value?.[1]);
|
|
179
|
+
if (!target || !Number.isFinite(value) || value < 0)
|
|
180
|
+
continue;
|
|
181
|
+
target.callsKnown = true;
|
|
182
|
+
target.calls += value;
|
|
183
|
+
if (series.metric.fractory_outcome === "error")
|
|
184
|
+
target.errors += value;
|
|
185
|
+
}
|
|
186
|
+
const unlabelled = counts.filter((series) => !series.metric.fractory_target).length;
|
|
187
|
+
if (unlabelled)
|
|
188
|
+
output.warnings.push(`${unlabelled} aggregate series have no factory target label; check instrumentation cardinality and attribute export.`);
|
|
189
|
+
for (const series of buckets) {
|
|
190
|
+
const target = row(series.metric), count = Number(series.value?.[1]), le = series.metric.le === "+Inf"
|
|
191
|
+
? Number.MAX_VALUE
|
|
192
|
+
: Number(series.metric.le);
|
|
193
|
+
if (!target || !Number.isFinite(count) || count < 0 || !Number.isFinite(le))
|
|
194
|
+
continue;
|
|
195
|
+
target.buckets.push({ le, count });
|
|
196
|
+
}
|
|
197
|
+
for (const series of trend) {
|
|
198
|
+
const target = row(series.metric);
|
|
199
|
+
if (!target)
|
|
200
|
+
continue;
|
|
201
|
+
for (const [at, text] of series.values || []) {
|
|
202
|
+
const value = Number(text) * 60;
|
|
203
|
+
if (!Number.isFinite(value) || value < 0)
|
|
204
|
+
continue;
|
|
205
|
+
const time = at * 1000;
|
|
206
|
+
const existing = target.trend.find((p) => p.at === time);
|
|
207
|
+
if (existing)
|
|
208
|
+
existing.value += value;
|
|
209
|
+
else
|
|
210
|
+
target.trend.push({ at: time, value });
|
|
211
|
+
if (series.metric.fractory_outcome === "error") {
|
|
212
|
+
target.failedTrend ||= [];
|
|
213
|
+
const point = target.failedTrend.find((p) => p.at === time);
|
|
214
|
+
if (point)
|
|
215
|
+
point.value += value;
|
|
216
|
+
else
|
|
217
|
+
target.failedTrend.push({ at: time, value });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
for (const series of latency) {
|
|
222
|
+
const target = row(series.metric), le = series.metric.le === "+Inf"
|
|
223
|
+
? Number.MAX_VALUE
|
|
224
|
+
: Number(series.metric.le);
|
|
225
|
+
if (!target || !Number.isFinite(le))
|
|
226
|
+
continue;
|
|
227
|
+
target.latencyTrend ||= [];
|
|
228
|
+
for (const [at, text] of series.values || []) {
|
|
229
|
+
const count = Number(text);
|
|
230
|
+
if (!Number.isFinite(count) || count < 0)
|
|
231
|
+
continue;
|
|
232
|
+
const time = at * 1000;
|
|
233
|
+
let point = target.latencyTrend.find((p) => p.at === time);
|
|
234
|
+
if (!point) {
|
|
235
|
+
point = { at: time, buckets: [] };
|
|
236
|
+
target.latencyTrend.push(point);
|
|
237
|
+
}
|
|
238
|
+
point.buckets.push({ le, count });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
for (const value of rows.values())
|
|
242
|
+
value.buckets.sort((a, b) => a.le - b.le);
|
|
243
|
+
output.boundaries = [...rows.values()];
|
|
244
|
+
const last = Number(latest[0]?.value?.[1]);
|
|
245
|
+
if (Number.isFinite(last))
|
|
246
|
+
output.lastSampleAt = new Date(last * 1000).toISOString();
|
|
247
|
+
output.gauges = [];
|
|
248
|
+
for (const definition of definitions) {
|
|
249
|
+
const gauge = { ...definition, status: "empty", values: [] };
|
|
250
|
+
const results = await Promise.all(definition.sources.map(async (source) => {
|
|
251
|
+
let result;
|
|
252
|
+
try {
|
|
253
|
+
result = await reader.query(source.query.replace(/\$environment\b/g, JSON.stringify(output.environment)), window.to, { from: window.from, step });
|
|
254
|
+
}
|
|
255
|
+
catch {
|
|
256
|
+
gauge.status = "error";
|
|
257
|
+
gauge.error = "Source query unavailable";
|
|
258
|
+
output.warnings.push(`${definition.label}: source query unavailable`);
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
// Gauges must explicitly reduce to one series; arbitrary series are never silently summed.
|
|
262
|
+
if (result.length > 1) {
|
|
263
|
+
output.warnings.push(`${definition.label}: query must return one series per attached source`);
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const points = (result[0]?.values || []).flatMap(([at, text]) => Number.isFinite(Number(text))
|
|
267
|
+
? [{ at: at * 1000, value: Number(text) }]
|
|
268
|
+
: []);
|
|
269
|
+
if (points.length)
|
|
270
|
+
return {
|
|
271
|
+
block: source.block,
|
|
272
|
+
value: points.at(-1).value,
|
|
273
|
+
trend: points,
|
|
274
|
+
};
|
|
275
|
+
}));
|
|
276
|
+
gauge.values = results.filter((value) => !!value);
|
|
277
|
+
if (gauge.values.length && gauge.status !== "error")
|
|
278
|
+
gauge.status = "ready";
|
|
279
|
+
for (const value of gauge.values) {
|
|
280
|
+
const at = value.trend.at(-1)?.at;
|
|
281
|
+
if (at && (!output.lastSampleAt || at > Date.parse(output.lastSampleAt)))
|
|
282
|
+
output.lastSampleAt = new Date(at).toISOString();
|
|
283
|
+
}
|
|
284
|
+
output.gauges.push(gauge);
|
|
285
|
+
}
|
|
286
|
+
if (queried.every((r) => r.status === "rejected") &&
|
|
287
|
+
!output.gauges.some((g) => g.values.length))
|
|
288
|
+
throw queried[0].reason;
|
|
289
|
+
return output;
|
|
290
|
+
}
|
package/dist/scope.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { DefinitionGraph, Element, Connection } from "./model.js";
|
|
2
|
+
/** A focused scope shows its contents and genuine external endpoints, never unrelated siblings. */
|
|
3
|
+
export declare function scopeGraph(graph: DefinitionGraph, focus: string, expanded: ReadonlySet<string>): {
|
|
4
|
+
scope: Element;
|
|
5
|
+
nodes: Element[];
|
|
6
|
+
edges: (Connection & {
|
|
7
|
+
originalSource: string;
|
|
8
|
+
originalTarget: string;
|
|
9
|
+
})[];
|
|
10
|
+
external: Set<string>;
|
|
11
|
+
contextLabel: (node: Element) => string;
|
|
12
|
+
};
|