@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,47 @@
|
|
|
1
|
+
export { resolveComparisonRef, comparisonMergeBase } from "./git-refs.js";
|
|
2
|
+
import type { DefinitionGraph } from "./model.js";
|
|
3
|
+
import { locateCode, type CodeSnapshot } from "./code-model.js";
|
|
4
|
+
export declare function resolveCommit(root: string, ref: string): string;
|
|
5
|
+
/** One bounded, null-delimited read. No similarity search or source execution. */
|
|
6
|
+
export declare function changedPaths(root: string, base: string, head: string): {
|
|
7
|
+
path: string;
|
|
8
|
+
status: "added" | "removed" | "modified";
|
|
9
|
+
digest?: string;
|
|
10
|
+
}[];
|
|
11
|
+
export declare function captureCode(root: string, graph: DefinitionGraph, cacheDirectory?: string): CodeSnapshot;
|
|
12
|
+
export { saveCode, loadCode, codeTimeline } from "./code-store.js";
|
|
13
|
+
/** Two bounded cat-file batches; no checkout, source execution, or history scan. */
|
|
14
|
+
export declare function captureCommit(root: string, ref: string, definitionFiles: string[], compose: (definitions: DefinitionGraph[]) => DefinitionGraph, annotationsFile?: string): CodeSnapshot;
|
|
15
|
+
export declare function codeHistory(root: string, snapshot: CodeSnapshot, elementId: string, limit?: number, skip?: number): {
|
|
16
|
+
commit: string;
|
|
17
|
+
date: string;
|
|
18
|
+
subject: string;
|
|
19
|
+
association: "mapped-files";
|
|
20
|
+
}[];
|
|
21
|
+
export declare function definitionHistory(root: string, graph: DefinitionGraph, elementId: string, anchor: string, limit?: number, skip?: number): {
|
|
22
|
+
commit: string;
|
|
23
|
+
date: string;
|
|
24
|
+
subject: string;
|
|
25
|
+
association: "mapped-files";
|
|
26
|
+
}[];
|
|
27
|
+
export declare function codeChanges(root: string, before: CodeSnapshot, after: CodeSnapshot): {
|
|
28
|
+
attachments: {
|
|
29
|
+
file: string;
|
|
30
|
+
kind: "notes" | "tests" | "auth";
|
|
31
|
+
elementId: string;
|
|
32
|
+
}[];
|
|
33
|
+
side: "before" | "after";
|
|
34
|
+
file: string;
|
|
35
|
+
start: number;
|
|
36
|
+
count: number;
|
|
37
|
+
digest: string;
|
|
38
|
+
matches: ReturnType<typeof locateCode>[];
|
|
39
|
+
}[];
|
|
40
|
+
export declare function matchPatch(patch: string, before: CodeSnapshot, after: CodeSnapshot): {
|
|
41
|
+
side: "before" | "after";
|
|
42
|
+
file: string;
|
|
43
|
+
start: number;
|
|
44
|
+
count: number;
|
|
45
|
+
digest: string;
|
|
46
|
+
matches: ReturnType<typeof locateCode>[];
|
|
47
|
+
}[];
|
package/dist/git-code.js
ADDED
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/** Build/CLI adapter only. The Worker/browser matching core has no dependency on Git. */
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
import { readGit as git } from "./git-refs.js";
|
|
4
|
+
export { resolveComparisonRef, comparisonMergeBase } from "./git-refs.js";
|
|
5
|
+
import { readFileSync, mkdirSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { blockFiles, locateCode } from "./code-model.js";
|
|
9
|
+
import { indexSources } from "./source-index.js";
|
|
10
|
+
import { factoryGraph } from "./factory-graph.js";
|
|
11
|
+
import { blockNoteFile } from "./workspace-notes.js";
|
|
12
|
+
import { relatedTests } from "./model.js";
|
|
13
|
+
import { graphIndex } from "./change-impact.js";
|
|
14
|
+
function attachments(snapshot) {
|
|
15
|
+
const items = snapshot.graph.elements.flatMap((node) => [
|
|
16
|
+
...[blockNoteFile(node.id), ...(node.attachments?.docs || [])].map((file) => ({
|
|
17
|
+
file: file.split("#")[0],
|
|
18
|
+
kind: "notes",
|
|
19
|
+
elementId: node.id,
|
|
20
|
+
})),
|
|
21
|
+
...(node.attachments?.auth?.source
|
|
22
|
+
? [
|
|
23
|
+
{
|
|
24
|
+
file: node.attachments.auth.source.file,
|
|
25
|
+
kind: "auth",
|
|
26
|
+
elementId: node.id,
|
|
27
|
+
},
|
|
28
|
+
]
|
|
29
|
+
: []),
|
|
30
|
+
]);
|
|
31
|
+
items.push(...snapshot.graph.tests.flatMap((test) => test.targets.map((elementId) => ({
|
|
32
|
+
file: test.source.file,
|
|
33
|
+
kind: "tests",
|
|
34
|
+
elementId,
|
|
35
|
+
}))));
|
|
36
|
+
return items;
|
|
37
|
+
}
|
|
38
|
+
export function resolveCommit(root, ref) {
|
|
39
|
+
if (!/^(?:HEAD(?:~\d{1,5}|\^\d?)?|[a-f0-9]{7,64})$/i.test(ref))
|
|
40
|
+
throw new Error("Use HEAD, HEAD~N, or a commit SHA");
|
|
41
|
+
return git(root, ["rev-parse", "--verify", `${ref}^{commit}`]).trim();
|
|
42
|
+
}
|
|
43
|
+
/** One bounded, null-delimited read. No similarity search or source execution. */
|
|
44
|
+
export function changedPaths(root, base, head) {
|
|
45
|
+
if (![base, head].every((ref) => /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(ref)))
|
|
46
|
+
throw Error("Expected resolved commits");
|
|
47
|
+
const parts = git(root, [
|
|
48
|
+
"diff",
|
|
49
|
+
"--no-ext-diff",
|
|
50
|
+
"--no-textconv",
|
|
51
|
+
"--no-renames",
|
|
52
|
+
"--raw",
|
|
53
|
+
"--no-abbrev",
|
|
54
|
+
"-z",
|
|
55
|
+
base,
|
|
56
|
+
head,
|
|
57
|
+
"--",
|
|
58
|
+
]).split("\0");
|
|
59
|
+
const files = [];
|
|
60
|
+
for (let i = 0; i + 1 < parts.length; i += 2) {
|
|
61
|
+
const [, , before, after, status] = parts[i].split(" ");
|
|
62
|
+
files.push({
|
|
63
|
+
path: parts[i + 1],
|
|
64
|
+
status: status === "A" ? "added" : status === "D" ? "removed" : "modified",
|
|
65
|
+
digest: `${before}:${after}`,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
if (files.length > 20000)
|
|
69
|
+
throw Error("PR footprint exceeds the path budget");
|
|
70
|
+
return files;
|
|
71
|
+
}
|
|
72
|
+
export function captureCode(root, graph, cacheDirectory) {
|
|
73
|
+
const baseCommit = resolveCommit(root, "HEAD");
|
|
74
|
+
let dirty = !!git(root, [
|
|
75
|
+
"status",
|
|
76
|
+
"--porcelain",
|
|
77
|
+
"--untracked-files=normal",
|
|
78
|
+
]).trim();
|
|
79
|
+
if (cacheDirectory)
|
|
80
|
+
mkdirSync(cacheDirectory, { recursive: true });
|
|
81
|
+
const indexed = indexSources(graph, (file) => {
|
|
82
|
+
try {
|
|
83
|
+
return readFileSync(join(root, file), "utf8");
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
return undefined;
|
|
87
|
+
}
|
|
88
|
+
}, cacheDirectory
|
|
89
|
+
? {
|
|
90
|
+
get: (key) => {
|
|
91
|
+
try {
|
|
92
|
+
return JSON.parse(readFileSync(join(cacheDirectory, `${key}.json`), "utf8"));
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return undefined;
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
put: (key, value) => writeFileSync(join(cacheDirectory, `${key}.json`), JSON.stringify(value)),
|
|
99
|
+
}
|
|
100
|
+
: undefined, { resolveUniqueAnchors: true });
|
|
101
|
+
// Catch a checkout or edit racing the capture. CI normally takes this fast clean path.
|
|
102
|
+
if (!dirty)
|
|
103
|
+
dirty =
|
|
104
|
+
resolveCommit(root, "HEAD") !== baseCommit ||
|
|
105
|
+
!!git(root, ["status", "--porcelain", "--untracked-files=normal"]).trim();
|
|
106
|
+
graph = { ...graph, codeRevision: dirty ? undefined : baseCommit };
|
|
107
|
+
const graphContent = { ...graph, generatedAt: "content" };
|
|
108
|
+
const id = createHash("sha256")
|
|
109
|
+
.update(JSON.stringify({ baseCommit, dirty, graph: graphContent, ...indexed }))
|
|
110
|
+
.digest("hex");
|
|
111
|
+
return {
|
|
112
|
+
schema: "fractal.code.v1",
|
|
113
|
+
id,
|
|
114
|
+
commit: dirty ? null : baseCommit,
|
|
115
|
+
baseCommit,
|
|
116
|
+
capturedAt: new Date().toISOString(),
|
|
117
|
+
graph,
|
|
118
|
+
...indexed,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
export { saveCode, loadCode, codeTimeline } from "./code-store.js";
|
|
122
|
+
/** Two bounded cat-file batches; no checkout, source execution, or history scan. */
|
|
123
|
+
export function captureCommit(root, ref, definitionFiles, compose, annotationsFile) {
|
|
124
|
+
const commit = resolveCommit(root, ref);
|
|
125
|
+
const read = (paths) => {
|
|
126
|
+
if (paths.some((path) => /[\r\n\0]/.test(path) ||
|
|
127
|
+
path.startsWith("/") ||
|
|
128
|
+
path.split(/[\\/]/).includes("..")))
|
|
129
|
+
throw new Error("Expected repository source paths");
|
|
130
|
+
const output = execFileSync("git", ["cat-file", "--batch"], {
|
|
131
|
+
cwd: root,
|
|
132
|
+
input: paths.map((path) => `${commit}:${path}\n`).join(""),
|
|
133
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
134
|
+
timeout: 15_000,
|
|
135
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
136
|
+
});
|
|
137
|
+
const texts = new Map();
|
|
138
|
+
let offset = 0;
|
|
139
|
+
for (const path of paths) {
|
|
140
|
+
const end = output.indexOf(10, offset);
|
|
141
|
+
if (end < 0)
|
|
142
|
+
throw new Error("Incomplete Git object batch");
|
|
143
|
+
const header = output.subarray(offset, end).toString();
|
|
144
|
+
offset = end + 1;
|
|
145
|
+
if (header.endsWith(" missing"))
|
|
146
|
+
continue;
|
|
147
|
+
const [, type, size] = header.split(" ");
|
|
148
|
+
if (type !== "blob" || !/^\d+$/.test(size))
|
|
149
|
+
throw new Error("Expected a Git source blob");
|
|
150
|
+
texts.set(path, output.subarray(offset, offset + Number(size)).toString());
|
|
151
|
+
offset += Number(size) + 1;
|
|
152
|
+
}
|
|
153
|
+
return texts;
|
|
154
|
+
};
|
|
155
|
+
const definitions = read([
|
|
156
|
+
...definitionFiles,
|
|
157
|
+
...(annotationsFile ? [annotationsFile] : []),
|
|
158
|
+
]);
|
|
159
|
+
const graphs = definitionFiles.map((file) => {
|
|
160
|
+
const text = definitions.get(file);
|
|
161
|
+
if (!text)
|
|
162
|
+
throw new Error("No captured definition at this commit; Fractal may not have been installed yet");
|
|
163
|
+
const graph = JSON.parse(text);
|
|
164
|
+
if (graph.schema !== "fractal.definition.v1" ||
|
|
165
|
+
!Array.isArray(graph.elements))
|
|
166
|
+
throw new Error("Unsupported historical factory definition");
|
|
167
|
+
return graph;
|
|
168
|
+
});
|
|
169
|
+
const annotationText = annotationsFile && definitions.get(annotationsFile);
|
|
170
|
+
const annotations = annotationText
|
|
171
|
+
? JSON.parse(annotationText)
|
|
172
|
+
: undefined;
|
|
173
|
+
if (annotations && annotations.schema !== "fractal.factory.v2")
|
|
174
|
+
throw new Error("Unsupported historical annotation model");
|
|
175
|
+
const graph = {
|
|
176
|
+
...(annotations
|
|
177
|
+
? factoryGraph(annotations, compose(graphs), { legacySnapshot: true })
|
|
178
|
+
: compose(graphs)),
|
|
179
|
+
codeRevision: commit,
|
|
180
|
+
};
|
|
181
|
+
const paths = [
|
|
182
|
+
...new Set(graph.elements.flatMap((node) => (node.sources || (node.source ? [node.source] : [])).map((source) => source.file))),
|
|
183
|
+
];
|
|
184
|
+
const texts = read(paths);
|
|
185
|
+
const indexed = indexSources(graph, (file) => texts.get(file), undefined, {
|
|
186
|
+
resolveUniqueAnchors: true,
|
|
187
|
+
});
|
|
188
|
+
const id = createHash("sha256")
|
|
189
|
+
.update(JSON.stringify({
|
|
190
|
+
baseCommit: commit,
|
|
191
|
+
dirty: false,
|
|
192
|
+
graph: { ...graph, generatedAt: "content" },
|
|
193
|
+
...indexed,
|
|
194
|
+
}))
|
|
195
|
+
.digest("hex");
|
|
196
|
+
return {
|
|
197
|
+
schema: "fractal.code.v1",
|
|
198
|
+
id,
|
|
199
|
+
commit,
|
|
200
|
+
baseCommit: commit,
|
|
201
|
+
capturedAt: git(root, ["show", "-s", "--format=%cI", commit]).trim(),
|
|
202
|
+
graph,
|
|
203
|
+
...indexed,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
export function codeHistory(root, snapshot, elementId, limit = 20, skip = 0) {
|
|
207
|
+
if (!Number.isSafeInteger(skip) || skip < 0 || skip > 10000)
|
|
208
|
+
throw Error("History offset exceeds the read budget");
|
|
209
|
+
const { within } = graphIndex(snapshot.graph);
|
|
210
|
+
const paths = [
|
|
211
|
+
...new Set([
|
|
212
|
+
...blockFiles(snapshot, elementId),
|
|
213
|
+
...attachments(snapshot)
|
|
214
|
+
.filter((item) => within(item.elementId, elementId))
|
|
215
|
+
.map((item) => item.file),
|
|
216
|
+
...relatedTests(snapshot.graph, elementId).map((test) => test.source.file),
|
|
217
|
+
]),
|
|
218
|
+
];
|
|
219
|
+
return readPathHistory(root, paths, snapshot.baseCommit, limit, skip);
|
|
220
|
+
}
|
|
221
|
+
export function definitionHistory(root, graph, elementId, anchor, limit = 30, skip = 0) {
|
|
222
|
+
if (!graph.elements.some((node) => node.id === elementId))
|
|
223
|
+
throw Error("Unknown block");
|
|
224
|
+
const { within } = graphIndex(graph);
|
|
225
|
+
const paths = [
|
|
226
|
+
...new Set([
|
|
227
|
+
...graph.elements
|
|
228
|
+
.filter((node) => within(node.id, elementId))
|
|
229
|
+
.flatMap((node) => (node.sources || (node.source ? [node.source] : [])).map((source) => source.file)),
|
|
230
|
+
...attachments({ graph })
|
|
231
|
+
.filter((item) => within(item.elementId, elementId))
|
|
232
|
+
.map((item) => item.file),
|
|
233
|
+
...relatedTests(graph, elementId).map((test) => test.source.file),
|
|
234
|
+
]),
|
|
235
|
+
];
|
|
236
|
+
return readPathHistory(root, paths, anchor, limit, skip);
|
|
237
|
+
}
|
|
238
|
+
function readPathHistory(root, paths, anchor, limit, skip) {
|
|
239
|
+
if (!Number.isSafeInteger(skip) ||
|
|
240
|
+
skip < 0 ||
|
|
241
|
+
skip > 10000 ||
|
|
242
|
+
paths.length > 2000 ||
|
|
243
|
+
paths.join("").length > 200000)
|
|
244
|
+
throw Error("History exceeds the read budget");
|
|
245
|
+
if (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(anchor))
|
|
246
|
+
throw Error("Expected a resolved history commit");
|
|
247
|
+
if (!paths.length)
|
|
248
|
+
return [];
|
|
249
|
+
const log = git(root, [
|
|
250
|
+
"log",
|
|
251
|
+
`-n${Math.min(50, Math.max(1, limit))}`,
|
|
252
|
+
`--skip=${skip}`,
|
|
253
|
+
"--format=%H%x00%cI%x00%s",
|
|
254
|
+
"--no-renames",
|
|
255
|
+
anchor,
|
|
256
|
+
"--",
|
|
257
|
+
...paths,
|
|
258
|
+
]);
|
|
259
|
+
return log
|
|
260
|
+
.trim()
|
|
261
|
+
.split("\n")
|
|
262
|
+
.filter(Boolean)
|
|
263
|
+
.map((line) => {
|
|
264
|
+
const [commit, date, subject] = line.split("\0");
|
|
265
|
+
return { commit, date, subject, association: "mapped-files" };
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
export function codeChanges(root, before, after) {
|
|
269
|
+
if (!before.commit || !after.commit)
|
|
270
|
+
throw new Error("Diff matching requires two committed snapshots");
|
|
271
|
+
const paths = [
|
|
272
|
+
...new Set([
|
|
273
|
+
...Object.keys(before.files),
|
|
274
|
+
...Object.keys(after.files),
|
|
275
|
+
...[before, after].flatMap((snapshot) => snapshot.graph.connections.flatMap((edge) => edge.id.startsWith("call-site:")
|
|
276
|
+
? (edge.sources || (edge.sourceRef ? [edge.sourceRef] : [])).map((source) => source.file)
|
|
277
|
+
: [])),
|
|
278
|
+
...[before, after].flatMap((snapshot) => attachments(snapshot).map((item) => item.file)),
|
|
279
|
+
]),
|
|
280
|
+
];
|
|
281
|
+
if (paths.length > 2000 || paths.join("").length > 200000)
|
|
282
|
+
throw Error("Comparison source paths exceed the read budget");
|
|
283
|
+
if (!paths.length)
|
|
284
|
+
return [];
|
|
285
|
+
// No rename similarity search, text conversion, external diff, blame, or source reparse.
|
|
286
|
+
const patch = git(root, [
|
|
287
|
+
"diff",
|
|
288
|
+
"--no-ext-diff",
|
|
289
|
+
"--no-textconv",
|
|
290
|
+
"--no-renames",
|
|
291
|
+
"--no-color",
|
|
292
|
+
"--unified=0",
|
|
293
|
+
before.commit,
|
|
294
|
+
after.commit,
|
|
295
|
+
"--",
|
|
296
|
+
...paths,
|
|
297
|
+
]);
|
|
298
|
+
const associations = {
|
|
299
|
+
before: attachments(before),
|
|
300
|
+
after: attachments(after),
|
|
301
|
+
};
|
|
302
|
+
return matchPatch(patch, before, after).map((change) => ({
|
|
303
|
+
...change,
|
|
304
|
+
attachments: associations[change.side].filter((item) => item.file === change.file),
|
|
305
|
+
}));
|
|
306
|
+
}
|
|
307
|
+
export function matchPatch(patch, before, after) {
|
|
308
|
+
const result = [];
|
|
309
|
+
let oldFile = "", newFile = "";
|
|
310
|
+
const lines = patch.split("\n");
|
|
311
|
+
for (let index = 0; index < lines.length; index++) {
|
|
312
|
+
const line = lines[index];
|
|
313
|
+
if (line.startsWith("--- "))
|
|
314
|
+
oldFile = line.slice(4).replace(/^a\//, "");
|
|
315
|
+
else if (line.startsWith("+++ "))
|
|
316
|
+
newFile = line.slice(4).replace(/^b\//, "");
|
|
317
|
+
else {
|
|
318
|
+
const hunk = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
319
|
+
if (!hunk)
|
|
320
|
+
continue;
|
|
321
|
+
let end = index + 1;
|
|
322
|
+
while (end < lines.length &&
|
|
323
|
+
!lines[end].startsWith("@@ ") &&
|
|
324
|
+
!lines[end].startsWith("diff --git "))
|
|
325
|
+
end++;
|
|
326
|
+
const digest = createHash("sha256")
|
|
327
|
+
.update(lines.slice(index + 1, end).join("\n"))
|
|
328
|
+
.digest("hex");
|
|
329
|
+
for (const [side, file, start, count, snapshot] of [
|
|
330
|
+
["before", oldFile, +hunk[1], +(hunk[2] ?? 1), before],
|
|
331
|
+
["after", newFile, +hunk[3], +(hunk[4] ?? 1), after],
|
|
332
|
+
]) {
|
|
333
|
+
if (!count || file === "/dev/null")
|
|
334
|
+
continue;
|
|
335
|
+
// Query range boundaries, not every changed line; capture every intersecting owner.
|
|
336
|
+
const ranges = snapshot.files[file]?.ranges || [];
|
|
337
|
+
const points = new Set([
|
|
338
|
+
start,
|
|
339
|
+
...ranges
|
|
340
|
+
.filter((r) => r.start < start + count && r.end >= start)
|
|
341
|
+
.flatMap((r) => [
|
|
342
|
+
Math.max(start, r.start),
|
|
343
|
+
...(r.end + 1 < start + count ? [r.end + 1] : []),
|
|
344
|
+
]),
|
|
345
|
+
]);
|
|
346
|
+
const matches = [...points].map((point) => locateCode(snapshot, file, point));
|
|
347
|
+
result.push({ side, file, start, count, matches, digest });
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return result;
|
|
352
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function readGit(root: string, args: string[]): string;
|
|
2
|
+
/** Resolve only local refs. No shell, fetch, revision ranges, paths or option arguments. */
|
|
3
|
+
export declare function resolveComparisonRef(root: string, ref: string): string;
|
|
4
|
+
export declare function comparisonMergeBase(root: string, base: string, head: string): string;
|
package/dist/git-refs.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/** Lightweight Git identity reads shared by review links and source adapters. */
|
|
2
|
+
import { execFileSync } from "node:child_process";
|
|
3
|
+
export function readGit(root, args) {
|
|
4
|
+
return execFileSync("git", ["--no-pager", "-c", "core.quotePath=false", ...args], {
|
|
5
|
+
cwd: root,
|
|
6
|
+
encoding: "utf8",
|
|
7
|
+
maxBuffer: 16 * 1024 * 1024,
|
|
8
|
+
timeout: 15_000,
|
|
9
|
+
env: {
|
|
10
|
+
...process.env,
|
|
11
|
+
GIT_OPTIONAL_LOCKS: "0",
|
|
12
|
+
GIT_LITERAL_PATHSPECS: "1",
|
|
13
|
+
},
|
|
14
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/** Resolve only local refs. No shell, fetch, revision ranges, paths or option arguments. */
|
|
18
|
+
export function resolveComparisonRef(root, ref) {
|
|
19
|
+
if (!ref ||
|
|
20
|
+
ref.length > 180 ||
|
|
21
|
+
!/^[A-Za-z0-9][A-Za-z0-9._/~-]*(?:\^\d*)?$/.test(ref) ||
|
|
22
|
+
ref.includes("..") ||
|
|
23
|
+
ref.includes("//") ||
|
|
24
|
+
ref.endsWith(".lock"))
|
|
25
|
+
throw Error("Use a local branch, tag, HEAD~N or commit SHA.");
|
|
26
|
+
return readGit(root, [
|
|
27
|
+
"rev-parse",
|
|
28
|
+
"--verify",
|
|
29
|
+
"--end-of-options",
|
|
30
|
+
`${ref}^{commit}`,
|
|
31
|
+
]).trim();
|
|
32
|
+
}
|
|
33
|
+
export function comparisonMergeBase(root, base, head) {
|
|
34
|
+
if (![base, head].every((ref) => /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(ref)))
|
|
35
|
+
throw Error("Expected resolved commit identities");
|
|
36
|
+
return readGit(root, ["merge-base", base, head]).trim();
|
|
37
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { HistorySample } from "./map-activity.js";
|
|
2
|
+
import { type TimeWindow } from "./time-window.js";
|
|
3
|
+
export declare function parseHistory(output: string): HistorySample["commits"];
|
|
4
|
+
export declare function historyActivity(root: string, files: string[], days: number, commit?: string, window?: TimeWindow): Promise<HistorySample>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/** One bounded native Git read. No source parsing, snapshot generation or history database. */
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import { promisify } from "node:util";
|
|
4
|
+
import { inTimeWindow } from "./time-window.js";
|
|
5
|
+
import { latestHistoryAt } from "./activity-volume.js";
|
|
6
|
+
const execute = promisify(execFile);
|
|
7
|
+
export function parseHistory(output) {
|
|
8
|
+
const fields = output.split("\0"), commits = [];
|
|
9
|
+
let i = 0;
|
|
10
|
+
while (i < fields.length) {
|
|
11
|
+
if (fields[i] !== "")
|
|
12
|
+
throw Error("Invalid Git history record");
|
|
13
|
+
i++;
|
|
14
|
+
if (i === fields.length)
|
|
15
|
+
break;
|
|
16
|
+
const commit = fields[i++], timestamp = fields[i++], subject = fields[i++];
|
|
17
|
+
if (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(commit) ||
|
|
18
|
+
!/^\d+$/.test(timestamp) ||
|
|
19
|
+
subject === undefined)
|
|
20
|
+
throw Error("Invalid Git commit metadata");
|
|
21
|
+
const files = [];
|
|
22
|
+
const changes = [];
|
|
23
|
+
let first = true;
|
|
24
|
+
while (i < fields.length && fields[i] !== "") {
|
|
25
|
+
const file = fields[i++];
|
|
26
|
+
const record = first && file.startsWith("\n") ? file.slice(1) : file;
|
|
27
|
+
const stat = /^(\d+|-)\t(\d+|-)\t([\s\S]+)$/.exec(record);
|
|
28
|
+
if (stat) {
|
|
29
|
+
files.push(stat[3]);
|
|
30
|
+
changes.push({
|
|
31
|
+
path: stat[3],
|
|
32
|
+
added: Number(stat[1]) || 0,
|
|
33
|
+
deleted: Number(stat[2]) || 0,
|
|
34
|
+
binary: stat[1] === "-",
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else
|
|
38
|
+
files.push(record);
|
|
39
|
+
first = false;
|
|
40
|
+
}
|
|
41
|
+
commits.push({
|
|
42
|
+
commit,
|
|
43
|
+
at: new Date(Number(timestamp) * 1000).toISOString(),
|
|
44
|
+
subject,
|
|
45
|
+
files,
|
|
46
|
+
...(changes.length ? { changes } : {}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return commits;
|
|
50
|
+
}
|
|
51
|
+
export async function historyActivity(root, files, days, commit = "HEAD", window) {
|
|
52
|
+
if (!window && ![0, 7, 30, 90].includes(days))
|
|
53
|
+
throw Error("Choose latest commits, or 7, 30 or 90 days");
|
|
54
|
+
if (commit !== "HEAD" && !/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/.test(commit))
|
|
55
|
+
throw Error("Expected a full commit SHA");
|
|
56
|
+
if (files.length > 1000 || files.join("").length > 128000)
|
|
57
|
+
throw Error("Mapped source exceeds the history read budget");
|
|
58
|
+
const checkedAt = new Date().toISOString();
|
|
59
|
+
let windowEnd = window?.to || checkedAt;
|
|
60
|
+
if (!window && commit !== "HEAD" && days) {
|
|
61
|
+
const { stdout } = await execute("git", ["show", "-s", "--format=%ct", commit], {
|
|
62
|
+
cwd: root,
|
|
63
|
+
encoding: "utf8",
|
|
64
|
+
timeout: 8000,
|
|
65
|
+
maxBuffer: 1024,
|
|
66
|
+
env: { ...process.env, GIT_OPTIONAL_LOCKS: "0" },
|
|
67
|
+
});
|
|
68
|
+
if (!/^\d+\s*$/.test(stdout))
|
|
69
|
+
throw Error("Cannot determine historical activity window");
|
|
70
|
+
windowEnd = new Date(Number(stdout.trim()) * 1000).toISOString();
|
|
71
|
+
}
|
|
72
|
+
if (!files.length)
|
|
73
|
+
return {
|
|
74
|
+
checkedAt,
|
|
75
|
+
days,
|
|
76
|
+
partial: false,
|
|
77
|
+
commits: [],
|
|
78
|
+
...(window ? { windowStart: window.from, windowEnd: window.to } : {}),
|
|
79
|
+
};
|
|
80
|
+
const [{ stdout }, latestAt] = await Promise.all([
|
|
81
|
+
execute("git", [
|
|
82
|
+
"--no-pager",
|
|
83
|
+
"-c",
|
|
84
|
+
"core.fsmonitor=false",
|
|
85
|
+
"log",
|
|
86
|
+
"--format=%x00%H%x00%ct%x00%s",
|
|
87
|
+
"-z",
|
|
88
|
+
"--numstat",
|
|
89
|
+
"--no-renames",
|
|
90
|
+
...(window ? ["--first-parent"] : []),
|
|
91
|
+
...(window || days
|
|
92
|
+
? [
|
|
93
|
+
`--since-as-filter=${window?.from || new Date(Date.parse(windowEnd) - days * 86400000).toISOString()}`,
|
|
94
|
+
`--until=${windowEnd}`,
|
|
95
|
+
]
|
|
96
|
+
: []),
|
|
97
|
+
"--max-count=201",
|
|
98
|
+
commit,
|
|
99
|
+
"--",
|
|
100
|
+
...files,
|
|
101
|
+
], {
|
|
102
|
+
cwd: root,
|
|
103
|
+
encoding: "utf8",
|
|
104
|
+
timeout: 8000,
|
|
105
|
+
maxBuffer: 2 * 1024 * 1024,
|
|
106
|
+
env: {
|
|
107
|
+
...process.env,
|
|
108
|
+
GIT_OPTIONAL_LOCKS: "0",
|
|
109
|
+
GIT_LITERAL_PATHSPECS: "1",
|
|
110
|
+
},
|
|
111
|
+
}),
|
|
112
|
+
latestHistoryAt(root, files, commit),
|
|
113
|
+
]);
|
|
114
|
+
const commits = parseHistory(stdout);
|
|
115
|
+
return {
|
|
116
|
+
checkedAt,
|
|
117
|
+
latestAt,
|
|
118
|
+
windowEnd: window?.to || (days ? windowEnd : commits[0]?.at || checkedAt),
|
|
119
|
+
...(window ? { windowStart: window.from } : {}),
|
|
120
|
+
...(!window && !days && commits.length
|
|
121
|
+
? { windowStart: commits[Math.min(commits.length, 200) - 1].at }
|
|
122
|
+
: {}),
|
|
123
|
+
days,
|
|
124
|
+
partial: commits.length > 200,
|
|
125
|
+
commits: commits
|
|
126
|
+
.slice(0, 200)
|
|
127
|
+
.filter((c) => !window || inTimeWindow(c.at, window)),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { DefinitionGraph } from "./model.js";
|
|
2
|
+
import { indexSources } from "./source-index.js";
|
|
3
|
+
import type { CodeSnapshot } from "./code-model.js";
|
|
4
|
+
import type { TimeWindow } from "./time-window.js";
|
|
5
|
+
type Range = CodeSnapshot["files"][string]["ranges"][number];
|
|
6
|
+
export interface BlockChurn {
|
|
7
|
+
added: number;
|
|
8
|
+
deleted: number;
|
|
9
|
+
baseline: number;
|
|
10
|
+
percent: number | null;
|
|
11
|
+
partial: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface HistoryChurn {
|
|
14
|
+
basis: "net-lines";
|
|
15
|
+
base?: string;
|
|
16
|
+
head?: string;
|
|
17
|
+
blocks: Record<string, BlockChurn>;
|
|
18
|
+
unmappedLines: number;
|
|
19
|
+
unmappedFiles?: Record<string, {
|
|
20
|
+
added: number;
|
|
21
|
+
deleted: number;
|
|
22
|
+
}>;
|
|
23
|
+
unresolved: number;
|
|
24
|
+
error?: string;
|
|
25
|
+
}
|
|
26
|
+
/** Partition source into uniquely owned intervals. Shared siblings roll up to
|
|
27
|
+
* their common ancestors, rather than giving each leaf all of a shared file. */
|
|
28
|
+
export declare function ownedIntervals(graph: DefinitionGraph, ranges: Range[]): {
|
|
29
|
+
start: number;
|
|
30
|
+
end: number;
|
|
31
|
+
owners: string[];
|
|
32
|
+
ambiguous: boolean;
|
|
33
|
+
}[];
|
|
34
|
+
export declare function churnFromPatch(graph: DefinitionGraph, before: ReturnType<typeof indexSources>, after: ReturnType<typeof indexSources>, patch: string): HistoryChurn;
|
|
35
|
+
export declare function historyChurn(root: string, graph: DefinitionGraph, window: TimeWindow, anchor?: string): Promise<HistoryChurn>;
|
|
36
|
+
export {};
|