@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,416 @@
|
|
|
1
|
+
/** Net line churn between two times. No checkout, history replay or per-commit compilation. */
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import type { DefinitionGraph } from "./model";
|
|
5
|
+
import { indexSources } from "./source-index";
|
|
6
|
+
import type { CodeSnapshot } from "./code-model";
|
|
7
|
+
import type { TimeWindow } from "./time-window";
|
|
8
|
+
const execute = promisify(execFile);
|
|
9
|
+
const sha = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
|
|
10
|
+
type Range = CodeSnapshot["files"][string]["ranges"][number];
|
|
11
|
+
export interface BlockChurn {
|
|
12
|
+
added: number;
|
|
13
|
+
deleted: number;
|
|
14
|
+
baseline: number;
|
|
15
|
+
percent: number | null;
|
|
16
|
+
partial: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface HistoryChurn {
|
|
19
|
+
basis: "net-lines";
|
|
20
|
+
base?: string;
|
|
21
|
+
head?: string;
|
|
22
|
+
blocks: Record<string, BlockChurn>;
|
|
23
|
+
unmappedLines: number;
|
|
24
|
+
unmappedFiles?: Record<string, { added: number; deleted: number }>;
|
|
25
|
+
unresolved: number;
|
|
26
|
+
error?: string;
|
|
27
|
+
}
|
|
28
|
+
async function git(root: string, args: string[], input?: string) {
|
|
29
|
+
const task = execute(
|
|
30
|
+
"git",
|
|
31
|
+
[
|
|
32
|
+
"--no-pager",
|
|
33
|
+
"-c",
|
|
34
|
+
"core.quotePath=false",
|
|
35
|
+
"-c",
|
|
36
|
+
"core.fsmonitor=false",
|
|
37
|
+
...args,
|
|
38
|
+
],
|
|
39
|
+
{
|
|
40
|
+
cwd: root,
|
|
41
|
+
encoding: "buffer",
|
|
42
|
+
timeout: 15000,
|
|
43
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
44
|
+
env: {
|
|
45
|
+
...process.env,
|
|
46
|
+
GIT_OPTIONAL_LOCKS: "0",
|
|
47
|
+
GIT_LITERAL_PATHSPECS: "1",
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
);
|
|
51
|
+
task.child.stdin?.end(input);
|
|
52
|
+
return (await task).stdout;
|
|
53
|
+
}
|
|
54
|
+
const blobs = new Map<string, string>();
|
|
55
|
+
const indexes = new Map<
|
|
56
|
+
string,
|
|
57
|
+
{ ranges: Range[]; gaps: CodeSnapshot["gaps"] }
|
|
58
|
+
>();
|
|
59
|
+
const sourceCache = {
|
|
60
|
+
get: (key: string) => indexes.get(key),
|
|
61
|
+
put: (
|
|
62
|
+
key: string,
|
|
63
|
+
value: { ranges: Range[]; gaps: CodeSnapshot["gaps"] },
|
|
64
|
+
) => {
|
|
65
|
+
indexes.set(key, value);
|
|
66
|
+
if (indexes.size > 2500) indexes.delete(indexes.keys().next().value!);
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
async function sources(
|
|
70
|
+
root: string,
|
|
71
|
+
commit: string | undefined,
|
|
72
|
+
paths: string[],
|
|
73
|
+
) {
|
|
74
|
+
const files = new Map<string, string>();
|
|
75
|
+
if (!commit || !paths.length) return files;
|
|
76
|
+
const tree = (
|
|
77
|
+
await git(root, ["ls-tree", "-r", "-z", commit, "--", ...paths])
|
|
78
|
+
).toString();
|
|
79
|
+
const entries = tree
|
|
80
|
+
.split("\0")
|
|
81
|
+
.filter(Boolean)
|
|
82
|
+
.flatMap((row) => {
|
|
83
|
+
const match = /^\d+ blob ([a-f0-9]+)\t([\s\S]+)$/.exec(row);
|
|
84
|
+
return match && paths.includes(match[2])
|
|
85
|
+
? [{ hash: match[1], path: match[2] }]
|
|
86
|
+
: [];
|
|
87
|
+
});
|
|
88
|
+
const missing = [...new Set(entries.map((e) => e.hash))].filter(
|
|
89
|
+
(hash) => !blobs.has(hash),
|
|
90
|
+
);
|
|
91
|
+
if (missing.length) {
|
|
92
|
+
const output = await git(
|
|
93
|
+
root,
|
|
94
|
+
["cat-file", "--batch"],
|
|
95
|
+
missing.join("\n") + "\n",
|
|
96
|
+
);
|
|
97
|
+
let offset = 0,
|
|
98
|
+
bytes = 0;
|
|
99
|
+
for (const hash of missing) {
|
|
100
|
+
const end = output.indexOf(10, offset);
|
|
101
|
+
if (end < 0) throw Error("Incomplete Git blob batch");
|
|
102
|
+
const header = output.subarray(offset, end).toString();
|
|
103
|
+
offset = end + 1;
|
|
104
|
+
const [identity, type, sizeText] = header.split(" "),
|
|
105
|
+
size = Number(sizeText);
|
|
106
|
+
if (
|
|
107
|
+
identity !== hash ||
|
|
108
|
+
type !== "blob" ||
|
|
109
|
+
!Number.isSafeInteger(size) ||
|
|
110
|
+
size < 0 ||
|
|
111
|
+
offset + size > output.length
|
|
112
|
+
)
|
|
113
|
+
throw Error("Invalid Git blob batch");
|
|
114
|
+
bytes += size;
|
|
115
|
+
if (bytes > 28 * 1024 * 1024 || size > 2 * 1024 * 1024)
|
|
116
|
+
throw Error("Mapped code exceeds the churn read budget");
|
|
117
|
+
blobs.set(hash, output.subarray(offset, offset + size).toString());
|
|
118
|
+
offset += size + 1;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (const entry of entries) files.set(entry.path, blobs.get(entry.hash)!);
|
|
122
|
+
// Retain immutable source objects only, bounded by both entries and bytes.
|
|
123
|
+
let bytes = [...blobs.values()].reduce(
|
|
124
|
+
(sum, text) => sum + Buffer.byteLength(text),
|
|
125
|
+
0,
|
|
126
|
+
);
|
|
127
|
+
while (blobs.size > 2000 || bytes > 32 * 1024 * 1024) {
|
|
128
|
+
const key = blobs.keys().next().value!;
|
|
129
|
+
bytes -= Buffer.byteLength(blobs.get(key)!);
|
|
130
|
+
blobs.delete(key);
|
|
131
|
+
}
|
|
132
|
+
return files;
|
|
133
|
+
}
|
|
134
|
+
/** Partition source into uniquely owned intervals. Shared siblings roll up to
|
|
135
|
+
* their common ancestors, rather than giving each leaf all of a shared file. */
|
|
136
|
+
export function ownedIntervals(graph: DefinitionGraph, ranges: Range[]) {
|
|
137
|
+
const byId = new Map(graph.elements.map((n) => [n.id, n]));
|
|
138
|
+
const ancestors = (id: string) => {
|
|
139
|
+
const all = new Set<string>();
|
|
140
|
+
let node = byId.get(id);
|
|
141
|
+
while (node && !all.has(node.id)) {
|
|
142
|
+
all.add(node.id);
|
|
143
|
+
node = byId.get(node.parent!);
|
|
144
|
+
}
|
|
145
|
+
return all;
|
|
146
|
+
};
|
|
147
|
+
const ancestry = new Map(
|
|
148
|
+
ranges.map((r) => [r.elementId, ancestors(r.elementId)]),
|
|
149
|
+
);
|
|
150
|
+
const points = [...new Set(ranges.flatMap((r) => [r.start, r.end + 1]))].sort(
|
|
151
|
+
(a, b) => a - b,
|
|
152
|
+
);
|
|
153
|
+
return points.slice(0, -1).map((start, i) => {
|
|
154
|
+
let covering = ranges.filter((r) => r.start <= start && r.end >= start);
|
|
155
|
+
const precise = covering.filter((range) => range.basis !== "file");
|
|
156
|
+
if (precise.length) covering = precise;
|
|
157
|
+
if (
|
|
158
|
+
covering.some(
|
|
159
|
+
(r) =>
|
|
160
|
+
byId.get(r.elementId)?.provenance === "declared" &&
|
|
161
|
+
["method", "process"].includes(byId.get(r.elementId)!.kind),
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
covering = covering.filter(
|
|
165
|
+
(r) => byId.get(r.elementId)?.provenance === "declared",
|
|
166
|
+
);
|
|
167
|
+
const ids = [
|
|
168
|
+
...new Set(
|
|
169
|
+
covering.map(
|
|
170
|
+
(r) => byId.get(r.elementId)?.implementationBlock || r.elementId,
|
|
171
|
+
),
|
|
172
|
+
),
|
|
173
|
+
];
|
|
174
|
+
const leaves = ids.filter(
|
|
175
|
+
(id) => !ids.some((other) => id !== other && ancestors(other).has(id)),
|
|
176
|
+
);
|
|
177
|
+
const owners = leaves.length
|
|
178
|
+
? [...ancestors(leaves[0])].filter((id) =>
|
|
179
|
+
leaves.every((leaf) =>
|
|
180
|
+
(ancestry.get(leaf) || ancestors(leaf)).has(id),
|
|
181
|
+
),
|
|
182
|
+
)
|
|
183
|
+
: [];
|
|
184
|
+
return {
|
|
185
|
+
start,
|
|
186
|
+
end: points[i + 1] - 1,
|
|
187
|
+
owners,
|
|
188
|
+
ambiguous: leaves.length > 1,
|
|
189
|
+
};
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/** Decode Git's quoted paths, including octal bytes, without interpreting shell text. */
|
|
193
|
+
function pathFromHeader(value: string) {
|
|
194
|
+
value = value.replace(/\t$/, "");
|
|
195
|
+
if (value.startsWith('"')) {
|
|
196
|
+
const bytes: number[] = [];
|
|
197
|
+
for (let i = 1; i < value.length - 1; i++) {
|
|
198
|
+
if (value[i] !== "\\") {
|
|
199
|
+
bytes.push(...Buffer.from(value[i]));
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const octal = /^[0-7]{1,3}/.exec(value.slice(i + 1));
|
|
203
|
+
if (octal) {
|
|
204
|
+
bytes.push(parseInt(octal[0], 8));
|
|
205
|
+
i += octal[0].length;
|
|
206
|
+
} else {
|
|
207
|
+
i++;
|
|
208
|
+
bytes.push(
|
|
209
|
+
...Buffer.from(
|
|
210
|
+
(
|
|
211
|
+
{
|
|
212
|
+
n: "\n",
|
|
213
|
+
t: "\t",
|
|
214
|
+
r: "\r",
|
|
215
|
+
b: "\b",
|
|
216
|
+
f: "\f",
|
|
217
|
+
v: "\v",
|
|
218
|
+
a: "\x07",
|
|
219
|
+
} as Record<string, string>
|
|
220
|
+
)[value[i]] || value[i],
|
|
221
|
+
),
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
value = Buffer.from(bytes).toString();
|
|
226
|
+
}
|
|
227
|
+
return value.replace(/^[ab]\//, "");
|
|
228
|
+
}
|
|
229
|
+
export function churnFromPatch(
|
|
230
|
+
graph: DefinitionGraph,
|
|
231
|
+
before: ReturnType<typeof indexSources>,
|
|
232
|
+
after: ReturnType<typeof indexSources>,
|
|
233
|
+
patch: string,
|
|
234
|
+
): HistoryChurn {
|
|
235
|
+
const result: HistoryChurn = {
|
|
236
|
+
basis: "net-lines",
|
|
237
|
+
blocks: {},
|
|
238
|
+
unmappedLines: 0,
|
|
239
|
+
unresolved: before.gaps.length + after.gaps.length,
|
|
240
|
+
};
|
|
241
|
+
const intervals = (index: ReturnType<typeof indexSources>) =>
|
|
242
|
+
new Map(
|
|
243
|
+
Object.entries(index.files).map(([file, data]) => [
|
|
244
|
+
file,
|
|
245
|
+
ownedIntervals(graph, data.ranges),
|
|
246
|
+
]),
|
|
247
|
+
);
|
|
248
|
+
const old = intervals(before),
|
|
249
|
+
next = intervals(after);
|
|
250
|
+
const cell = (id: string) =>
|
|
251
|
+
(result.blocks[id] ||= {
|
|
252
|
+
added: 0,
|
|
253
|
+
deleted: 0,
|
|
254
|
+
baseline: 0,
|
|
255
|
+
percent: null,
|
|
256
|
+
partial: false,
|
|
257
|
+
});
|
|
258
|
+
for (const file of old.values())
|
|
259
|
+
for (const part of file)
|
|
260
|
+
for (const id of part.owners)
|
|
261
|
+
cell(id).baseline += part.end - part.start + 1;
|
|
262
|
+
function measure(
|
|
263
|
+
file: string,
|
|
264
|
+
start: number,
|
|
265
|
+
count: number,
|
|
266
|
+
side: "added" | "deleted",
|
|
267
|
+
) {
|
|
268
|
+
if (!count || file === "/dev/null") return;
|
|
269
|
+
let assigned = 0;
|
|
270
|
+
for (const part of (side === "added" ? next : old).get(file) || []) {
|
|
271
|
+
const lines = Math.max(
|
|
272
|
+
0,
|
|
273
|
+
Math.min(start + count - 1, part.end) - Math.max(start, part.start) + 1,
|
|
274
|
+
);
|
|
275
|
+
if (!lines) continue;
|
|
276
|
+
if (part.owners.length) assigned += lines;
|
|
277
|
+
for (const id of part.owners) {
|
|
278
|
+
cell(id)[side] += lines;
|
|
279
|
+
cell(id).partial ||= part.ambiguous;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const unmapped = count - assigned;
|
|
283
|
+
result.unmappedLines += unmapped;
|
|
284
|
+
if (unmapped) {
|
|
285
|
+
result.unmappedFiles ||= {};
|
|
286
|
+
const totals = (result.unmappedFiles[file] ||= { added: 0, deleted: 0 });
|
|
287
|
+
totals[side] += unmapped;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
let a = "",
|
|
291
|
+
b = "";
|
|
292
|
+
for (const line of patch.split("\n")) {
|
|
293
|
+
if (line.startsWith("--- ")) a = pathFromHeader(line.slice(4));
|
|
294
|
+
else if (line.startsWith("+++ ")) b = pathFromHeader(line.slice(4));
|
|
295
|
+
else {
|
|
296
|
+
const h = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
297
|
+
if (h) {
|
|
298
|
+
measure(a, +h[1], +(h[2] ?? 1), "deleted");
|
|
299
|
+
measure(b, +h[3], +(h[4] ?? 1), "added");
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
const byId = new Map(graph.elements.map((n) => [n.id, n]));
|
|
304
|
+
for (const gap of [...before.gaps, ...after.gaps]) {
|
|
305
|
+
let node = byId.get(gap.elementId);
|
|
306
|
+
const seen = new Set<string>();
|
|
307
|
+
while (node && !seen.has(node.id)) {
|
|
308
|
+
seen.add(node.id);
|
|
309
|
+
if (result.blocks[node.id]) result.blocks[node.id].partial = true;
|
|
310
|
+
node = byId.get(node.parent!);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
for (const value of Object.values(result.blocks))
|
|
314
|
+
if (value.baseline)
|
|
315
|
+
value.percent = (100 * (value.added + value.deleted)) / value.baseline;
|
|
316
|
+
return result;
|
|
317
|
+
}
|
|
318
|
+
export async function historyChurn(
|
|
319
|
+
root: string,
|
|
320
|
+
graph: DefinitionGraph,
|
|
321
|
+
window: TimeWindow,
|
|
322
|
+
anchor = "HEAD",
|
|
323
|
+
): Promise<HistoryChurn> {
|
|
324
|
+
if (anchor !== "HEAD" && !sha.test(anchor))
|
|
325
|
+
throw Error("Expected a full history commit");
|
|
326
|
+
const paths = [
|
|
327
|
+
...new Set(
|
|
328
|
+
graph.elements.flatMap((n) =>
|
|
329
|
+
(n.sources || (n.source ? [n.source] : [])).map((s) => s.file),
|
|
330
|
+
),
|
|
331
|
+
),
|
|
332
|
+
];
|
|
333
|
+
if (
|
|
334
|
+
paths.length > 2000 ||
|
|
335
|
+
paths.join("").length > 180000 ||
|
|
336
|
+
paths.some(
|
|
337
|
+
(p) =>
|
|
338
|
+
p.startsWith("/") ||
|
|
339
|
+
p.split(/[\\/]/).includes("..") ||
|
|
340
|
+
p.includes("\0"),
|
|
341
|
+
)
|
|
342
|
+
)
|
|
343
|
+
throw Error("Mapped source exceeds the churn read budget");
|
|
344
|
+
const boundary = async (time: string) =>
|
|
345
|
+
(
|
|
346
|
+
await git(root, [
|
|
347
|
+
"rev-list",
|
|
348
|
+
"--first-parent",
|
|
349
|
+
"-1",
|
|
350
|
+
`--before=${new Date((Math.ceil(Date.parse(time) / 1000) - 1) * 1000).toISOString()}`,
|
|
351
|
+
anchor,
|
|
352
|
+
])
|
|
353
|
+
)
|
|
354
|
+
.toString()
|
|
355
|
+
.trim() || undefined;
|
|
356
|
+
const [base, head] = await Promise.all([
|
|
357
|
+
boundary(window.from),
|
|
358
|
+
boundary(window.to),
|
|
359
|
+
]);
|
|
360
|
+
if (base === head)
|
|
361
|
+
return {
|
|
362
|
+
basis: "net-lines",
|
|
363
|
+
base,
|
|
364
|
+
head,
|
|
365
|
+
blocks: {},
|
|
366
|
+
unmappedLines: 0,
|
|
367
|
+
unresolved: 0,
|
|
368
|
+
};
|
|
369
|
+
const [beforeText, afterText] = await Promise.all([
|
|
370
|
+
sources(root, base, paths),
|
|
371
|
+
sources(root, head, paths),
|
|
372
|
+
]);
|
|
373
|
+
const before = indexSources(
|
|
374
|
+
graph,
|
|
375
|
+
(file) => beforeText.get(file),
|
|
376
|
+
sourceCache,
|
|
377
|
+
{ resolveUniqueAnchors: true },
|
|
378
|
+
);
|
|
379
|
+
const after = indexSources(
|
|
380
|
+
graph,
|
|
381
|
+
(file) => afterText.get(file),
|
|
382
|
+
sourceCache,
|
|
383
|
+
{ resolveUniqueAnchors: true },
|
|
384
|
+
);
|
|
385
|
+
const patch =
|
|
386
|
+
base && head && base !== head && paths.length
|
|
387
|
+
? (
|
|
388
|
+
await git(root, [
|
|
389
|
+
"diff",
|
|
390
|
+
"--no-ext-diff",
|
|
391
|
+
"--no-textconv",
|
|
392
|
+
"--no-renames",
|
|
393
|
+
"--no-color",
|
|
394
|
+
"--unified=0",
|
|
395
|
+
base,
|
|
396
|
+
head,
|
|
397
|
+
"--",
|
|
398
|
+
...paths,
|
|
399
|
+
])
|
|
400
|
+
).toString()
|
|
401
|
+
: "";
|
|
402
|
+
const result = churnFromPatch(graph, before, after, patch);
|
|
403
|
+
if (!base && head) {
|
|
404
|
+
const sizes = churnFromPatch(graph, after, after, "").blocks;
|
|
405
|
+
for (const [id, size] of Object.entries(sizes))
|
|
406
|
+
result.blocks[id] = {
|
|
407
|
+
...size,
|
|
408
|
+
baseline: 0,
|
|
409
|
+
percent: null,
|
|
410
|
+
added: size.baseline,
|
|
411
|
+
deleted: 0,
|
|
412
|
+
partial: true,
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
return { ...result, base, head };
|
|
416
|
+
}
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
/** Immutable Git metadata and blob batches. No checkout or application code execution. */
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
const execute = promisify(execFile);
|
|
5
|
+
export const commitSha = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
|
|
6
|
+
export async function historyGit(root: string, args: string[], input?: string) {
|
|
7
|
+
const task = execute(
|
|
8
|
+
"git",
|
|
9
|
+
[
|
|
10
|
+
"--no-pager",
|
|
11
|
+
"-c",
|
|
12
|
+
"core.quotePath=false",
|
|
13
|
+
"-c",
|
|
14
|
+
"core.fsmonitor=false",
|
|
15
|
+
...args,
|
|
16
|
+
],
|
|
17
|
+
{
|
|
18
|
+
cwd: root,
|
|
19
|
+
encoding: "buffer",
|
|
20
|
+
timeout: 20000,
|
|
21
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
22
|
+
env: {
|
|
23
|
+
...process.env,
|
|
24
|
+
GIT_OPTIONAL_LOCKS: "0",
|
|
25
|
+
GIT_LITERAL_PATHSPECS: "1",
|
|
26
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
task.child.stdin?.end(input);
|
|
31
|
+
return (await task).stdout;
|
|
32
|
+
}
|
|
33
|
+
const blobs = new Map<string, string>();
|
|
34
|
+
/** Batch-check first: repeated definition blobs across many commits are read once. */
|
|
35
|
+
export async function historyBlobs(root: string, objects: string[]) {
|
|
36
|
+
const unique = [...new Set(objects)];
|
|
37
|
+
if (unique.length > 50000 || unique.some((value) => /[\r\n\0]/.test(value)))
|
|
38
|
+
throw Error("History object lookup exceeds its read budget");
|
|
39
|
+
const result = new Map<string, string>();
|
|
40
|
+
if (!unique.length) return result;
|
|
41
|
+
const checks = (
|
|
42
|
+
await historyGit(
|
|
43
|
+
root,
|
|
44
|
+
["cat-file", "--batch-check"],
|
|
45
|
+
unique.join("\n") + "\n",
|
|
46
|
+
)
|
|
47
|
+
)
|
|
48
|
+
.toString()
|
|
49
|
+
.trimEnd()
|
|
50
|
+
.split("\n");
|
|
51
|
+
const identities = new Map<string, string>();
|
|
52
|
+
checks.forEach((line, index) => {
|
|
53
|
+
const match = /^([a-f0-9]+) blob (\d+)$/.exec(line);
|
|
54
|
+
if (match && Number(match[2]) <= 4 * 1024 * 1024)
|
|
55
|
+
identities.set(unique[index], match[1]);
|
|
56
|
+
});
|
|
57
|
+
const missing = [...new Set(identities.values())].filter(
|
|
58
|
+
(id) => !blobs.has(id),
|
|
59
|
+
);
|
|
60
|
+
// Keep batches small even when a repository has many distinct historical definitions.
|
|
61
|
+
for (let i = 0; i < missing.length; i += 32) {
|
|
62
|
+
const batch = missing.slice(i, i + 32);
|
|
63
|
+
const output = await historyGit(
|
|
64
|
+
root,
|
|
65
|
+
["cat-file", "--batch"],
|
|
66
|
+
batch.join("\n") + "\n",
|
|
67
|
+
);
|
|
68
|
+
let offset = 0;
|
|
69
|
+
for (const id of batch) {
|
|
70
|
+
const end = output.indexOf(10, offset);
|
|
71
|
+
if (end < 0) throw Error("Incomplete history blob batch");
|
|
72
|
+
const [identity, type, sizeText] = output
|
|
73
|
+
.subarray(offset, end)
|
|
74
|
+
.toString()
|
|
75
|
+
.split(" ");
|
|
76
|
+
const size = Number(sizeText);
|
|
77
|
+
offset = end + 1;
|
|
78
|
+
if (
|
|
79
|
+
identity !== id ||
|
|
80
|
+
type !== "blob" ||
|
|
81
|
+
!Number.isSafeInteger(size) ||
|
|
82
|
+
offset + size > output.length
|
|
83
|
+
)
|
|
84
|
+
throw Error("Invalid history blob batch");
|
|
85
|
+
blobs.set(id, output.subarray(offset, offset + size).toString());
|
|
86
|
+
offset += size + 1;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
for (const [expression, id] of identities)
|
|
90
|
+
if (blobs.has(id)) result.set(expression, blobs.get(id)!);
|
|
91
|
+
let bytes = [...blobs.values()].reduce(
|
|
92
|
+
(sum, text) => sum + Buffer.byteLength(text),
|
|
93
|
+
0,
|
|
94
|
+
);
|
|
95
|
+
while (bytes > 48 * 1024 * 1024 || blobs.size > 2000) {
|
|
96
|
+
const id = blobs.keys().next().value!;
|
|
97
|
+
bytes -= Buffer.byteLength(blobs.get(id)!);
|
|
98
|
+
blobs.delete(id);
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
export interface GitFileChange {
|
|
103
|
+
path: string;
|
|
104
|
+
added: number;
|
|
105
|
+
deleted: number;
|
|
106
|
+
binary: boolean;
|
|
107
|
+
beforeBlob?: string;
|
|
108
|
+
afterBlob?: string;
|
|
109
|
+
}
|
|
110
|
+
export interface GitChangeCommit {
|
|
111
|
+
commit: string;
|
|
112
|
+
parent?: string;
|
|
113
|
+
at: string;
|
|
114
|
+
subject: string;
|
|
115
|
+
files: GitFileChange[];
|
|
116
|
+
}
|
|
117
|
+
/** --raw and --numstat -z keep even tabs/newlines in paths out of the parser grammar. */
|
|
118
|
+
export function parseChangeCommits(output: string): GitChangeCommit[] {
|
|
119
|
+
const fields = output.split("\0"),
|
|
120
|
+
commits: GitChangeCommit[] = [];
|
|
121
|
+
let i = 0;
|
|
122
|
+
while (i < fields.length) {
|
|
123
|
+
while (i < fields.length && !fields[i].trim()) i++;
|
|
124
|
+
if (i === fields.length) break;
|
|
125
|
+
const commit = fields[i++],
|
|
126
|
+
parents = fields[i++],
|
|
127
|
+
timestamp = fields[i++],
|
|
128
|
+
subject = fields[i++];
|
|
129
|
+
if (
|
|
130
|
+
!commitSha.test(commit) ||
|
|
131
|
+
!/^\d+$/.test(timestamp) ||
|
|
132
|
+
subject === undefined
|
|
133
|
+
)
|
|
134
|
+
throw Error("Invalid history metadata");
|
|
135
|
+
const raw = new Map<string, { beforeBlob: string; afterBlob: string }>(),
|
|
136
|
+
files: GitFileChange[] = [];
|
|
137
|
+
while (i < fields.length && fields[i] !== "") {
|
|
138
|
+
const token = fields[i++].replace(/^\n+/, "");
|
|
139
|
+
const object = /^:\d+ \d+ ([a-f0-9]+) ([a-f0-9]+) \w+$/.exec(token);
|
|
140
|
+
if (object) {
|
|
141
|
+
raw.set(fields[i++], { beforeBlob: object[1], afterBlob: object[2] });
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const stat = /^(\d+|-)\t(\d+|-)\t([\s\S]+)$/.exec(token);
|
|
145
|
+
if (stat)
|
|
146
|
+
files.push({
|
|
147
|
+
path: stat[3],
|
|
148
|
+
added: Number(stat[1]) || 0,
|
|
149
|
+
deleted: Number(stat[2]) || 0,
|
|
150
|
+
binary: stat[1] === "-",
|
|
151
|
+
...raw.get(stat[3]),
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
commits.push({
|
|
155
|
+
commit,
|
|
156
|
+
parent: parents.split(" ").find(Boolean),
|
|
157
|
+
at: new Date(Number(timestamp) * 1000).toISOString(),
|
|
158
|
+
subject,
|
|
159
|
+
files,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return commits;
|
|
163
|
+
}
|