@flyingrobots/footprint 0.0.0-stage → 0.6.1
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/CHANGELOG.md +446 -0
- package/LICENSE +202 -0
- package/NOTICE +14 -0
- package/README.md +129 -2
- package/RELEASE.md +14 -0
- package/package.json +47 -4
- package/schemas/footprint.blast_radius.schema.json +218 -0
- package/schemas/footprint.comment.schema.json +60 -0
- package/schemas/footprint.composition.schema.json +67 -0
- package/schemas/footprint.landing.schema.json +98 -0
- package/schemas/footprint.overlap.schema.json +489 -0
- package/skills/footprint/SKILL.md +237 -0
- package/src/args.ts +28 -0
- package/src/bound.ts +134 -0
- package/src/callsites.ts +204 -0
- package/src/cell.ts +11 -0
- package/src/classify.ts +48 -0
- package/src/cli.ts +331 -0
- package/src/comment.ts +291 -0
- package/src/exports.ts +356 -0
- package/src/footprint.ts +95 -0
- package/src/graft.ts +121 -0
- package/src/impact.ts +109 -0
- package/src/landing.ts +316 -0
- package/src/languages.ts +83 -0
- package/src/overlap.ts +274 -0
- package/src/pulls.ts +124 -0
- package/src/remote.ts +177 -0
- package/src/render.ts +604 -0
- package/src/report.ts +53 -0
- package/src/symbols.ts +551 -0
- package/src/systems.ts +175 -0
package/src/landing.ts
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
// How a change lands: what the diff itself puts in front of a reviewer before the merge, and what
|
|
2
|
+
// to do after it.
|
|
3
|
+
//
|
|
4
|
+
// The after-merge steps are **read from the repository**, never invented. A repository's
|
|
5
|
+
// `RELEASE.md` and `AGENTS.md` already state how a change lands there; a tool that makes up
|
|
6
|
+
// post-merge steps makes up wrong ones, and wrong ones are worse than none because they are
|
|
7
|
+
// believed. Where no document declares any, this says so and stops.
|
|
8
|
+
//
|
|
9
|
+
// Everything here is a pure function of a changed-file list and the text of those documents, so
|
|
10
|
+
// the derivation is testable without git and without a network.
|
|
11
|
+
|
|
12
|
+
import { readFileSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { classify } from "./classify.ts";
|
|
15
|
+
|
|
16
|
+
/** The documents footprint reads, in the order it reads them. */
|
|
17
|
+
export const LANDING_DOCUMENTS = ["RELEASE.md", "AGENTS.md"] as const;
|
|
18
|
+
|
|
19
|
+
export type GuardKind = "migration" | "flag" | "version";
|
|
20
|
+
export interface Guard {
|
|
21
|
+
readonly path: string;
|
|
22
|
+
readonly kind: GuardKind;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The guard rule. A guard is a **path pattern**, not a judgement: footprint cannot tell whether a
|
|
27
|
+
* change is risky, and the moment it tries, the answer becomes arguable and the warning becomes
|
|
28
|
+
* noise. These patterns say only "a file whose path means this change is gated, switchable or
|
|
29
|
+
* recorded is in the diff". Ordered, first match wins per file, like `classify`.
|
|
30
|
+
*/
|
|
31
|
+
const GUARD_RULES: ReadonlyArray<readonly [GuardKind, RegExp]> = [
|
|
32
|
+
["migration", /(^|\/)(migrations?|migrate)\//i],
|
|
33
|
+
["migration", /(^|\/)\d{3,}[-_][^/]*\.(sql|[cm]?[jt]s|py|rb|go)$/],
|
|
34
|
+
["migration", /\.migration\.[a-z]+$/i],
|
|
35
|
+
["flag", /(^|\/)(feature[-_]?flags?|flags?)\//i],
|
|
36
|
+
["flag", /(^|\/)(feature[-_.]?flags?|flags?)\.(json|ya?ml|toml|[cm]?[jt]sx?)$/i],
|
|
37
|
+
["version", /(^|\/)(CHANGELOG|CHANGES|HISTORY|NEWS|RELEASES?)(\.[a-z]+)?$/i],
|
|
38
|
+
["version", /(^|\/)(VERSION|version\.txt|\.version)$/i],
|
|
39
|
+
["version", /(^|\/)(package\.json|Cargo\.toml|pyproject\.toml|go\.mod|composer\.json|gradle\.properties)$/],
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
/** Every changed file that matches the guard rule, in diff order, with its first matching kind. */
|
|
43
|
+
export function guardsIn(paths: readonly string[]): Guard[] {
|
|
44
|
+
const out: Guard[] = [];
|
|
45
|
+
for (const path of paths) {
|
|
46
|
+
for (const [kind, pattern] of GUARD_RULES) {
|
|
47
|
+
if (pattern.test(path)) {
|
|
48
|
+
out.push({ path, kind });
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* A dependency manifest changed with no lockfile beside it in the same change: the lockfile is a
|
|
58
|
+
* generated artifact that may need regenerating. Pure path patterns again — footprint does not
|
|
59
|
+
* read the manifest, so it cannot know whether the dependency set actually moved.
|
|
60
|
+
*/
|
|
61
|
+
const REGENERATE_RULES: ReadonlyArray<readonly [RegExp, RegExp, string]> = [
|
|
62
|
+
[/(^|\/)package\.json$/, /(^|\/)(package-lock\.json|npm-shrinkwrap\.json|bun\.lockb?|yarn\.lock|pnpm-lock\.yaml)$/, "a JavaScript lockfile"],
|
|
63
|
+
[/(^|\/)Cargo\.toml$/, /(^|\/)Cargo\.lock$/, "`Cargo.lock`"],
|
|
64
|
+
[/(^|\/)(pyproject\.toml|requirements\.in)$/, /(^|\/)(poetry\.lock|uv\.lock|requirements\.txt)$/, "a Python lockfile"],
|
|
65
|
+
[/(^|\/)go\.mod$/, /(^|\/)go\.sum$/, "`go.sum`"],
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
export type BeforeMergeKind = "migration" | "generated" | "regenerate";
|
|
69
|
+
export interface BeforeMergeItem {
|
|
70
|
+
readonly kind: BeforeMergeKind;
|
|
71
|
+
readonly paths: readonly string[];
|
|
72
|
+
readonly says: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface BeforeMerge {
|
|
76
|
+
readonly items: readonly BeforeMergeItem[];
|
|
77
|
+
readonly guards: readonly Guard[];
|
|
78
|
+
readonly unguarded: boolean;
|
|
79
|
+
readonly unguarded_because: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface DeclaredStep {
|
|
83
|
+
readonly step: string;
|
|
84
|
+
readonly file: string;
|
|
85
|
+
readonly heading: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface DocumentRead {
|
|
89
|
+
readonly file: string;
|
|
90
|
+
readonly present: boolean;
|
|
91
|
+
readonly headings: readonly string[];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface AfterMerge {
|
|
95
|
+
readonly read: readonly DocumentRead[];
|
|
96
|
+
readonly steps: readonly DeclaredStep[];
|
|
97
|
+
readonly declared: boolean;
|
|
98
|
+
readonly nothing_to_do: boolean;
|
|
99
|
+
readonly statement: string;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface Landing {
|
|
103
|
+
readonly before_merge: BeforeMerge;
|
|
104
|
+
readonly after_merge: AfterMerge;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface LandingDoc {
|
|
108
|
+
readonly file: string;
|
|
109
|
+
/** `null` when the document is not there, which is reported rather than skipped. */
|
|
110
|
+
readonly text: string | null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* A heading that names the landing process. Deliberately narrow: footprint reads how a change
|
|
115
|
+
* lands, not how a system is deployed, and `--help` would be a poor place to discover that it
|
|
116
|
+
* had quietly adopted a "Deployment" section as merge instructions.
|
|
117
|
+
*/
|
|
118
|
+
const LANDING_HEADING = /\b(releases?|releasing|publish(?:es|ing)?|after[-\s]?(?:the[-\s]?)?merge|post[-\s]?merge|landing|lands|ship(?:ping)?)\b/i;
|
|
119
|
+
|
|
120
|
+
/** A top-level list item: `- x`, `* x`, `+ x`, `1. x`, `1) x`, with up to three spaces of indent. */
|
|
121
|
+
const LIST_ITEM = /^ {0,3}(?:[-*+]|\d{1,9}[.)])\s+(.*)$/;
|
|
122
|
+
const CHECKBOX = /^\[[ xX]\]\s*/;
|
|
123
|
+
const HEADING = /^(#{1,6})\s+(.+?)\s*#*\s*$/;
|
|
124
|
+
const FENCE = /^ {0,3}(`{3,}|~{3,})/;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Walk a document once, reporting the headings of its landing sections and the list items under
|
|
128
|
+
* them.
|
|
129
|
+
*
|
|
130
|
+
* Three rules keep this from reading things that were never declared as steps:
|
|
131
|
+
*
|
|
132
|
+
* - **A heading is never a step.** Only a list item is, which is also why a paragraph of prose
|
|
133
|
+
* under a `## Release` heading contributes nothing.
|
|
134
|
+
* - **A fenced code block is never a step.** A command shown as an example was not declared as
|
|
135
|
+
* one, and footprint is not in the business of deciding which lines of a script to run.
|
|
136
|
+
* - **The document's title is not a section.** The first heading, when it is top-level, names the
|
|
137
|
+
* document; treating `# RELEASE` as a landing section would make the whole file one.
|
|
138
|
+
*/
|
|
139
|
+
function walk(
|
|
140
|
+
text: string,
|
|
141
|
+
onHeading: (heading: string) => void,
|
|
142
|
+
onStep: (step: string, heading: string) => void,
|
|
143
|
+
): void {
|
|
144
|
+
let fence: string | null = null;
|
|
145
|
+
let seenHeading = false;
|
|
146
|
+
let openDepth: number | null = null;
|
|
147
|
+
let heading = "";
|
|
148
|
+
for (const line of text.split("\n")) {
|
|
149
|
+
const fenced = FENCE.exec(line);
|
|
150
|
+
if (fenced !== null) {
|
|
151
|
+
const marker = fenced[1]![0]!;
|
|
152
|
+
if (fence === null) fence = marker;
|
|
153
|
+
else if (marker === fence) fence = null;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (fence !== null) continue;
|
|
157
|
+
|
|
158
|
+
const head = HEADING.exec(line);
|
|
159
|
+
if (head !== null) {
|
|
160
|
+
const depth = head[1]!.length;
|
|
161
|
+
const title = head[2]!.trim();
|
|
162
|
+
const isDocumentTitle = !seenHeading && depth === 1;
|
|
163
|
+
seenHeading = true;
|
|
164
|
+
if (isDocumentTitle) continue;
|
|
165
|
+
heading = title;
|
|
166
|
+
// A heading at or above the depth that opened the section closes it.
|
|
167
|
+
if (openDepth !== null && depth <= openDepth) openDepth = null;
|
|
168
|
+
if (openDepth === null && LANDING_HEADING.test(title)) openDepth = depth;
|
|
169
|
+
if (openDepth !== null) onHeading(title);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (openDepth === null) continue;
|
|
174
|
+
const item = LIST_ITEM.exec(line);
|
|
175
|
+
if (item === null) continue;
|
|
176
|
+
const step = item[1]!.replace(CHECKBOX, "").trim();
|
|
177
|
+
if (step !== "") onStep(step, heading);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The steps one document declares, in document order, each quoting the document verbatim. */
|
|
182
|
+
export function declaredSteps(file: string, text: string): DeclaredStep[] {
|
|
183
|
+
const steps: DeclaredStep[] = [];
|
|
184
|
+
walk(text, () => {}, (step, heading) => steps.push({ step, file, heading }));
|
|
185
|
+
return steps;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The headings inside a document's landing sections, including the heading that opened each. */
|
|
189
|
+
export function landingHeadings(text: string): string[] {
|
|
190
|
+
const headings: string[] = [];
|
|
191
|
+
walk(text, (h) => headings.push(h), () => {});
|
|
192
|
+
return headings;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Read the landing documents from a directory. A document that is not there reads as absent. */
|
|
196
|
+
export function readLandingDocs(dir: string): LandingDoc[] {
|
|
197
|
+
return LANDING_DOCUMENTS.map((file) => {
|
|
198
|
+
try {
|
|
199
|
+
return { file, text: readFileSync(join(dir, file), "utf8") };
|
|
200
|
+
} catch {
|
|
201
|
+
return { file, text: null };
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function list(paths: readonly string[]): string {
|
|
207
|
+
return paths.map((p) => `\`${p}\``).join(", ");
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function beforeMerge(paths: readonly string[]): BeforeMerge {
|
|
211
|
+
const guards = guardsIn(paths);
|
|
212
|
+
const sources = paths.filter((p) => classify(p) === "source");
|
|
213
|
+
const items: BeforeMergeItem[] = [];
|
|
214
|
+
|
|
215
|
+
const migrations = guards.filter((g) => g.kind === "migration").map((g) => g.path);
|
|
216
|
+
if (migrations.length > 0) {
|
|
217
|
+
items.push({
|
|
218
|
+
kind: "migration",
|
|
219
|
+
paths: migrations,
|
|
220
|
+
says:
|
|
221
|
+
`${migrations.length} migration file${migrations.length === 1 ? "" : "s"} in this change: ${list(migrations)}. ` +
|
|
222
|
+
"A migration and the code that depends on it land in one order or the other, and whichever merges first runs first: " +
|
|
223
|
+
"confirm the migration is applied before the code that reads what it changes.",
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const generated = paths.filter((p) => classify(p) === "generated");
|
|
228
|
+
if (generated.length > 0) {
|
|
229
|
+
items.push({
|
|
230
|
+
kind: "generated",
|
|
231
|
+
paths: generated,
|
|
232
|
+
says:
|
|
233
|
+
`${generated.length} generated or vendored file${generated.length === 1 ? "" : "s"} in this change: ${list(generated)}. ` +
|
|
234
|
+
"These are produced by a tool rather than written, so they are worth confirming as regenerated rather than hand-edited.",
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const stale: string[] = [];
|
|
239
|
+
const wants: string[] = [];
|
|
240
|
+
for (const [manifest, lock, what] of REGENERATE_RULES) {
|
|
241
|
+
const changed = paths.filter((p) => manifest.test(p));
|
|
242
|
+
if (changed.length === 0 || paths.some((p) => lock.test(p))) continue;
|
|
243
|
+
stale.push(...changed);
|
|
244
|
+
if (!wants.includes(what)) wants.push(what);
|
|
245
|
+
}
|
|
246
|
+
if (stale.length > 0) {
|
|
247
|
+
items.push({
|
|
248
|
+
kind: "regenerate",
|
|
249
|
+
paths: stale,
|
|
250
|
+
says: `${list(stale)} changed and no lockfile is in this change; ${wants.join(" and ")} may need regenerating.`,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const unguarded = sources.length > 0 && guards.length === 0;
|
|
255
|
+
const unguarded_because = unguarded
|
|
256
|
+
? `${sources.length} source file${sources.length === 1 ? "" : "s"} changed and no changed file is a migration, a feature flag, or a version or changelog entry: ` +
|
|
257
|
+
"by path, nothing in this change gates it, switches it off, or records that it happened."
|
|
258
|
+
: sources.length === 0
|
|
259
|
+
? "The guard rule does not apply, because no source file changed."
|
|
260
|
+
: `${guards.length} changed file${guards.length === 1 ? "" : "s"} match the guard rule: ${guards.map((g) => `\`${g.path}\` (${g.kind})`).join(", ")}.`;
|
|
261
|
+
|
|
262
|
+
return { items, guards, unguarded, unguarded_because };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function fallback(read: readonly DocumentRead[]): string {
|
|
266
|
+
if (read.length === 0) {
|
|
267
|
+
return "No landing steps are declared: footprint read no document, so it states none rather than inventing them.";
|
|
268
|
+
}
|
|
269
|
+
const parts = read.map((d) =>
|
|
270
|
+
!d.present
|
|
271
|
+
? `\`${d.file}\` is absent`
|
|
272
|
+
: d.headings.length > 0
|
|
273
|
+
? `\`${d.file}\` has a landing section (${d.headings.join(", ")}) with no steps listed under it`
|
|
274
|
+
: `\`${d.file}\` declares no release or landing section`,
|
|
275
|
+
);
|
|
276
|
+
return `No landing steps are declared: ${parts.join("; ")}. footprint states none rather than inventing them.`;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* The landing plan for a change: the before-merge observations from the changed-file list, and the
|
|
281
|
+
* after-merge steps from the documents.
|
|
282
|
+
*
|
|
283
|
+
* A change that touches no executable surface — no files at all, or documentation only, by the
|
|
284
|
+
* same rule the composition's gut check uses — has nothing to do after merge, and the declared
|
|
285
|
+
* steps are set aside rather than repeated at a reader who cannot act on them. What was read is
|
|
286
|
+
* still reported, so nothing is hidden.
|
|
287
|
+
*/
|
|
288
|
+
export function landingPlan(paths: readonly string[], docs: readonly LandingDoc[]): Landing {
|
|
289
|
+
const read: DocumentRead[] = docs.map((d) => ({
|
|
290
|
+
file: d.file,
|
|
291
|
+
present: d.text !== null,
|
|
292
|
+
headings: d.text === null ? [] : landingHeadings(d.text),
|
|
293
|
+
}));
|
|
294
|
+
const found = docs.flatMap((d) => (d.text === null ? [] : declaredSteps(d.file, d.text)));
|
|
295
|
+
const declared = found.length > 0;
|
|
296
|
+
const files = [...new Set(found.map((s) => s.file))].map((f) => `\`${f}\``);
|
|
297
|
+
|
|
298
|
+
const empty = paths.length === 0;
|
|
299
|
+
const docsOnly = !empty && paths.every((p) => classify(p) === "docs");
|
|
300
|
+
const nothing_to_do = empty || docsOnly;
|
|
301
|
+
|
|
302
|
+
const statement = empty
|
|
303
|
+
? "Nothing to do after merge: the range changes no files."
|
|
304
|
+
: docsOnly
|
|
305
|
+
? declared
|
|
306
|
+
? `Nothing to do after merge: the change touches documentation only, so the ${found.length} step${found.length === 1 ? "" : "s"} declared in ${files.join(" and ")} do not apply.`
|
|
307
|
+
: "Nothing to do after merge: the change touches documentation only, so no executable surface moved."
|
|
308
|
+
: declared
|
|
309
|
+
? `${found.length} step${found.length === 1 ? "" : "s"} read from ${files.join(" and ")}.`
|
|
310
|
+
: fallback(read);
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
before_merge: beforeMerge(paths),
|
|
314
|
+
after_merge: { read, steps: nothing_to_do ? [] : found, declared, nothing_to_do, statement },
|
|
315
|
+
};
|
|
316
|
+
}
|
package/src/languages.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Which changed files footprint does not recognise as a language graft's tree-sitter grammars can
|
|
2
|
+
// parse, by extension, with a count (flyingrobots/footprint#12). tree-sitter has grammars for some
|
|
3
|
+
// languages; a changed file in another language is invisible to symbol analysis. Silently
|
|
4
|
+
// skipping it reads as "nothing moved", which is the absent-signal failure AGENTS.md forbids — so
|
|
5
|
+
// the blast-radius result names it instead.
|
|
6
|
+
//
|
|
7
|
+
// This is a pure function of paths, and nothing else: it never looks at whether graft ran, or
|
|
8
|
+
// what it returned. That is deliberate, not an oversight — a changed file in a *supported*
|
|
9
|
+
// language whose parse failed (or whose diff was formatting-only, with no symbol to report) must
|
|
10
|
+
// not be counted as unsupported. Basing this on absence from graft's own output would conflate
|
|
11
|
+
// "no grammar" with "graft failed" or "nothing changed here"; basing it on the extension alone
|
|
12
|
+
// cannot.
|
|
13
|
+
//
|
|
14
|
+
// The set of supported extensions mirrors graft's own tree-sitter language support
|
|
15
|
+
// (`SUPPORTED_LANGS` in `@flyingrobots/graft`'s `dist/parser/lang.js`, as of graft 0.14.0). graft
|
|
16
|
+
// does not publish this list through its package exports map or any CLI contract, so footprint
|
|
17
|
+
// keeps its own copy by hand; it will drift if graft's grammar coverage changes before this list
|
|
18
|
+
// is updated. Asking graft to expose its supported-language set through a versioned CLI contract,
|
|
19
|
+
// instead of footprint mirroring it privately, is a decision the owner may want to make instead.
|
|
20
|
+
import { classify } from "./classify.ts";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Extensions footprint believes graft's export-surface analysis (`graft struct exports`) can
|
|
24
|
+
* read. Out of scope: adding grammars (flyingrobots/footprint#12 §5) — this only names the gap.
|
|
25
|
+
*/
|
|
26
|
+
const SUPPORTED_EXTENSIONS: ReadonlySet<string> = new Set([
|
|
27
|
+
".ts", ".mts", ".cts",
|
|
28
|
+
".tsx", ".jsx",
|
|
29
|
+
".js", ".mjs", ".cjs",
|
|
30
|
+
".rs",
|
|
31
|
+
".graphql", ".gql", ".graphqls",
|
|
32
|
+
".py", ".pyi",
|
|
33
|
+
".go",
|
|
34
|
+
".json",
|
|
35
|
+
".toml",
|
|
36
|
+
".yaml", ".yml",
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
export interface UnsupportedLanguage {
|
|
40
|
+
readonly extension: string;
|
|
41
|
+
readonly count: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The extension of a path: the basename's last dot onward, lowercase. `""` when the basename
|
|
46
|
+
* carries no dot (an extensionless file such as `Makefile`).
|
|
47
|
+
*/
|
|
48
|
+
export function extensionOf(path: string): string {
|
|
49
|
+
const normalized = path.trim().toLowerCase();
|
|
50
|
+
const lastSeparator = Math.max(normalized.lastIndexOf("/"), normalized.lastIndexOf("\\"));
|
|
51
|
+
const basename = lastSeparator >= 0 ? normalized.slice(lastSeparator + 1) : normalized;
|
|
52
|
+
const lastDot = basename.lastIndexOf(".");
|
|
53
|
+
return lastDot > 0 ? basename.slice(lastDot) : "";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Changed files classify() calls "source" (docs, config, tests, CI and generated files were never
|
|
58
|
+
* going to be symbol-analysed regardless of extension, so flagging them would be noise, not
|
|
59
|
+
* signal) whose extension is not one footprint recognises as supported, grouped by extension with
|
|
60
|
+
* a count. Sorted by extension, so the result — and any digest built over it — does not follow
|
|
61
|
+
* input order. `[]` when every changed file was in a supported language: present, never absent.
|
|
62
|
+
*/
|
|
63
|
+
export function unsupportedLanguages(paths: readonly string[]): UnsupportedLanguage[] {
|
|
64
|
+
const counts = new Map<string, number>();
|
|
65
|
+
for (const path of paths) {
|
|
66
|
+
if (classify(path) !== "source") continue;
|
|
67
|
+
const ext = extensionOf(path);
|
|
68
|
+
if (SUPPORTED_EXTENSIONS.has(ext)) continue;
|
|
69
|
+
counts.set(ext, (counts.get(ext) ?? 0) + 1);
|
|
70
|
+
}
|
|
71
|
+
return [...counts.entries()]
|
|
72
|
+
.map(([extension, count]) => ({ extension: extension === "" ? "(none)" : extension, count }))
|
|
73
|
+
.sort((a, b) => a.extension.localeCompare(b.extension));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Whether graft's grammars can read this path at all, by extension alone. The symbol-level overlap
|
|
78
|
+
* (flyingrobots/footprint#33) asks it before spending a graft call on a file: a Markdown file has no
|
|
79
|
+
* symbols to compare, whatever classify() calls it.
|
|
80
|
+
*/
|
|
81
|
+
export function parsableByGraft(path: string): boolean {
|
|
82
|
+
return SUPPORTED_EXTENSIONS.has(extensionOf(path));
|
|
83
|
+
}
|
package/src/overlap.ts
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
// Cross-pull-request overlap: which open pull requests touch a file this change touches, which of
|
|
2
|
+
// them touch each other's files, and an order they could land in.
|
|
3
|
+
//
|
|
4
|
+
// Everything here is a pure function over `{number, title, isDraft, files}` records. The `gh`
|
|
5
|
+
// invocation that produces those records lives in `src/pulls.ts`, so the computation is testable
|
|
6
|
+
// without the network and without this repository having open pull requests.
|
|
7
|
+
//
|
|
8
|
+
// The wave grouping is a **reimplementation** of graft's `findParallelGroups`
|
|
9
|
+
// (`src/operations/footprint-parallelism.ts`): greedy first-fit over a conflict graph, where a
|
|
10
|
+
// group holds records whose footprints do not overlap and group N+1 comes after group N. It is
|
|
11
|
+
// reimplemented rather than imported because footprint depends on graft's CLI contract and never
|
|
12
|
+
// on its source — graft's barrel does not export the function, and reaching into its `dist` is
|
|
13
|
+
// exactly the coupling that AGENTS.md forbids. The algorithm is twenty lines; the coupling would
|
|
14
|
+
// outlive it.
|
|
15
|
+
//
|
|
16
|
+
// The symbol level of a shared-file edge (flyingrobots/footprint#33) is computed in
|
|
17
|
+
// `src/symbols.ts` and passed in as data, so this file stays pure. A symbol-disjoint edge is
|
|
18
|
+
// reported and does not separate waves; an overlapping or undetermined one separates them as a
|
|
19
|
+
// file-level edge always did.
|
|
20
|
+
|
|
21
|
+
import { edgeKey, undeterminedLevel, type SymbolAnalysis, type SymbolLevel, type Symbols } from "./symbols.ts";
|
|
22
|
+
|
|
23
|
+
/** A pull request as the overlap computation needs it. `files` is null when gh did not report them. */
|
|
24
|
+
export interface PullRequest {
|
|
25
|
+
readonly number: number;
|
|
26
|
+
readonly title: string;
|
|
27
|
+
readonly isDraft: boolean;
|
|
28
|
+
readonly files: readonly string[] | null;
|
|
29
|
+
/** The pull request's head commit id, from `gh pr list --json headRefOid`. */
|
|
30
|
+
readonly headCommit?: string;
|
|
31
|
+
/** Its head branch name, from `headRefName`. */
|
|
32
|
+
readonly headBranch?: string;
|
|
33
|
+
/** True when the head branch lives in a fork, from `isCrossRepository`. */
|
|
34
|
+
readonly crossRepository?: boolean;
|
|
35
|
+
/** The branch it merges into, from `baseRefName`; named in reasons only. */
|
|
36
|
+
readonly baseBranch?: string;
|
|
37
|
+
/** The base branch's commit as `gh` reported it, from `baseRefOid`; the merge base is taken against this. */
|
|
38
|
+
readonly baseCommit?: string;
|
|
39
|
+
/**
|
|
40
|
+
* True when `gh pr list`, asked again after the files were listed, reported a different head
|
|
41
|
+
* commit or none: the files and `headCommit` are then not one snapshot.
|
|
42
|
+
*/
|
|
43
|
+
readonly headMoved?: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** What identifies the range's head: its commit id, and its branch name when it has one. */
|
|
47
|
+
export interface RangeHead {
|
|
48
|
+
readonly commit: string | null;
|
|
49
|
+
readonly branch: string | null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The pull request under review: the one whose head is the range's head. The commit id decides
|
|
54
|
+
* first; the head branch name decides only when no pull request's head commit matches, and only
|
|
55
|
+
* for a same-repository pull request, because a fork's branch of the same name is a different
|
|
56
|
+
* change. When several share the deciding fact the lowest number is the one named, so the answer
|
|
57
|
+
* does not follow the order `gh` listed them. Null when none matches.
|
|
58
|
+
*/
|
|
59
|
+
export function underReview(prs: readonly PullRequest[], head: RangeHead): number | null {
|
|
60
|
+
const ordered = [...prs].sort((a, b) => a.number - b.number);
|
|
61
|
+
if (head.commit !== null) {
|
|
62
|
+
const byCommit = ordered.find((p) => p.headCommit !== undefined && p.headCommit === head.commit);
|
|
63
|
+
if (byCommit !== undefined) return byCommit.number;
|
|
64
|
+
}
|
|
65
|
+
if (head.branch !== null) {
|
|
66
|
+
const byBranch = ordered.find((p) => p.crossRepository === false && p.headBranch === head.branch);
|
|
67
|
+
if (byBranch !== undefined) return byBranch.number;
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** An open pull request that touches a file this change touches. */
|
|
73
|
+
export interface SharedWith {
|
|
74
|
+
readonly number: number;
|
|
75
|
+
readonly title: string;
|
|
76
|
+
readonly isDraft: boolean;
|
|
77
|
+
readonly sharedFiles: readonly string[];
|
|
78
|
+
/** The shared files at the symbol level; side `a` is this change, side `b` this pull request. */
|
|
79
|
+
readonly symbolLevel: SymbolLevel;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** One edge of the conflict graph: two open pull requests that change the same files. */
|
|
83
|
+
export interface ConflictEdge {
|
|
84
|
+
readonly a: number;
|
|
85
|
+
readonly b: number;
|
|
86
|
+
readonly sharedFiles: readonly string[];
|
|
87
|
+
readonly reason: string;
|
|
88
|
+
/** The shared files at the symbol level; sides `a` and `b` are the edge's own. */
|
|
89
|
+
readonly symbolLevel: SymbolLevel;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* One wave of the proposed merge order. Within a wave no two pull requests share a file, unless
|
|
94
|
+
* the edge between them is symbol-disjoint.
|
|
95
|
+
*/
|
|
96
|
+
export interface Wave {
|
|
97
|
+
readonly wave: number;
|
|
98
|
+
readonly pullRequests: readonly number[];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A pull request whose file list could not be read, and therefore cannot be placed. */
|
|
102
|
+
export interface Undetermined {
|
|
103
|
+
readonly number: number;
|
|
104
|
+
readonly title: string;
|
|
105
|
+
readonly reason: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface OverlapAvailable {
|
|
109
|
+
readonly available: true;
|
|
110
|
+
/** The pull request under review (its head is the range's head), excluded from `overlapping`; null when none matches. */
|
|
111
|
+
readonly self: number | null;
|
|
112
|
+
readonly openPullRequests: number;
|
|
113
|
+
/** The `gh pr list --limit` used, and whether the list came back at it. */
|
|
114
|
+
readonly limit: number;
|
|
115
|
+
readonly truncated: boolean;
|
|
116
|
+
readonly overlapping: readonly SharedWith[];
|
|
117
|
+
readonly conflicts: readonly ConflictEdge[];
|
|
118
|
+
readonly waves: readonly Wave[];
|
|
119
|
+
readonly undetermined: readonly Undetermined[];
|
|
120
|
+
/** How the symbol level was bounded this run. */
|
|
121
|
+
readonly symbolAnalysis: SymbolAnalysis;
|
|
122
|
+
readonly summary: string;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface OverlapUnavailable {
|
|
126
|
+
readonly available: false;
|
|
127
|
+
readonly code: string;
|
|
128
|
+
readonly reason: string;
|
|
129
|
+
readonly summary: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export type Overlap = OverlapAvailable | OverlapUnavailable;
|
|
133
|
+
|
|
134
|
+
/** How many pull requests `gh pr list` is asked for. The result says when the list came back at it. */
|
|
135
|
+
export const LIST_LIMIT = 100;
|
|
136
|
+
|
|
137
|
+
/** The sorted intersection of two path lists. Two pull requests conflict when it is not empty. */
|
|
138
|
+
export function sharedFiles(a: readonly string[], b: readonly string[]): string[] {
|
|
139
|
+
const inB = new Set(b);
|
|
140
|
+
return [...new Set(a.filter((p) => inB.has(p)))].sort();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Ascending by number, so a plan does not depend on the order gh happened to list them. */
|
|
144
|
+
function byNumber(prs: readonly PullRequest[]): PullRequest[] {
|
|
145
|
+
return [...prs].sort((x, y) => x.number - y.number);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/** The pull requests whose files are known; the rest cannot be placed without guessing. */
|
|
149
|
+
function placeable(prs: readonly PullRequest[]): Array<PullRequest & { files: readonly string[] }> {
|
|
150
|
+
return byNumber(prs).flatMap((p) => (p.files === null ? [] : [{ ...p, files: p.files }]));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Every unordered pair of open pull requests that change the same file, with the files named.
|
|
155
|
+
* These are the edges the merge order has to respect.
|
|
156
|
+
*/
|
|
157
|
+
export function conflictGraph(prs: readonly PullRequest[], symbols?: Symbols): ConflictEdge[] {
|
|
158
|
+
const known = placeable(prs);
|
|
159
|
+
const edges: ConflictEdge[] = [];
|
|
160
|
+
for (let i = 0; i < known.length; i += 1) {
|
|
161
|
+
for (let j = i + 1; j < known.length; j += 1) {
|
|
162
|
+
const a = known[i]!, b = known[j]!;
|
|
163
|
+
const shared = sharedFiles(a.files, b.files);
|
|
164
|
+
if (shared.length === 0) continue;
|
|
165
|
+
const symbolLevel = symbols?.edges.get(edgeKey(a.number, b.number)) ?? notAnalysed(shared);
|
|
166
|
+
edges.push({ a: a.number, b: b.number, sharedFiles: shared, reason: edgeReason(a.number, b.number, shared, symbolLevel), symbolLevel });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return edges;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Partition the open pull requests into waves: greedy first-fit, so a pull request goes into the
|
|
174
|
+
* first wave none of whose members it conflicts with. Wave N+1 lands after wave N. This is a
|
|
175
|
+
* proposal, not an optimum — greedy colouring can use more waves than the graph needs — and it is
|
|
176
|
+
* deterministic because the input is sorted by number first.
|
|
177
|
+
*/
|
|
178
|
+
export function mergeWaves(prs: readonly PullRequest[], symbols?: Symbols): Wave[] {
|
|
179
|
+
const groups: Array<Array<PullRequest & { files: readonly string[] }>> = [];
|
|
180
|
+
// Two pull requests are kept apart when they share a file, unless that edge is symbol-disjoint.
|
|
181
|
+
const apart = (x: PullRequest & { files: readonly string[] }, y: PullRequest & { files: readonly string[] }): boolean =>
|
|
182
|
+
sharedFiles(x.files, y.files).length > 0 && symbols?.edges.get(edgeKey(x.number, y.number))?.level !== "symbol-disjoint";
|
|
183
|
+
for (const pr of placeable(prs)) {
|
|
184
|
+
const fits = groups.find((g) => g.every((other) => !apart(other, pr)));
|
|
185
|
+
if (fits === undefined) groups.push([pr]);
|
|
186
|
+
else fits.push(pr);
|
|
187
|
+
}
|
|
188
|
+
return groups.map((g, i) => ({ wave: i + 1, pullRequests: g.map((p) => p.number) }));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** What an edge or an overlap carries when no symbol analysis was given. */
|
|
192
|
+
function notAnalysed(files: readonly string[]): SymbolLevel {
|
|
193
|
+
return undeterminedLevel(files, "symbols.not_analysed", "symbol-level analysis was not run");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Why the edge exists, and what the symbol level says about it, in prose. */
|
|
197
|
+
function edgeReason(a: number, b: number, shared: readonly string[], level: SymbolLevel): string {
|
|
198
|
+
const base = `#${a} and #${b} both change ${list(shared)}`;
|
|
199
|
+
if (level.level === "symbol-disjoint") return `${base}, but different symbols in ${shared.length === 1 ? "it" : "each"}, so the edge does not separate waves`;
|
|
200
|
+
if (level.level === "symbol-overlapping") {
|
|
201
|
+
const same = level.files.filter((f) => f.level === "symbol-overlapping").map((f) => f.path);
|
|
202
|
+
return `${base}, and the same symbols in ${list(same)}`;
|
|
203
|
+
}
|
|
204
|
+
return `${base}; the symbol level is undetermined, so the edge separates waves as a file-level one does`;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Prose for a path list: `a.ts`, `a.ts and b.ts`, `a.ts, b.ts and c.ts`. */
|
|
208
|
+
function list(items: readonly string[]): string {
|
|
209
|
+
if (items.length <= 1) return items[0] ?? "";
|
|
210
|
+
return `${items.slice(0, -1).join(", ")} and ${items.at(-1)}`;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function plural(n: number, one: string, many: string): string {
|
|
214
|
+
return n === 1 ? one : many;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The overlap result. `changed` is what this change touches, from `git diff --numstat`; `prs` are
|
|
219
|
+
* the open pull requests. A pull request whose files are unknown is reported as undetermined
|
|
220
|
+
* rather than assumed disjoint: an absent signal must not read as a clean one.
|
|
221
|
+
*/
|
|
222
|
+
export function computeOverlap(
|
|
223
|
+
changed: readonly string[],
|
|
224
|
+
prs: readonly PullRequest[],
|
|
225
|
+
opts: { readonly self?: number | null; readonly limit?: number; readonly symbols?: Symbols } = {},
|
|
226
|
+
): OverlapAvailable {
|
|
227
|
+
const self = opts.self ?? null;
|
|
228
|
+
const limit = opts.limit ?? LIST_LIMIT;
|
|
229
|
+
const mine = new Set(changed);
|
|
230
|
+
|
|
231
|
+
const overlapping = placeable(prs).flatMap((p) => {
|
|
232
|
+
if (p.number === self) return [];
|
|
233
|
+
const shared = [...new Set(p.files.filter((f) => mine.has(f)))].sort();
|
|
234
|
+
if (shared.length === 0) return [];
|
|
235
|
+
const symbolLevel = opts.symbols?.overlapping.get(p.number) ?? notAnalysed(shared);
|
|
236
|
+
return [{ number: p.number, title: p.title, isDraft: p.isDraft, sharedFiles: shared, symbolLevel }];
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
const undetermined = byNumber(prs).flatMap((p) =>
|
|
240
|
+
p.files === null ? [{ number: p.number, title: p.title, reason: `gh did not report the changed files of #${p.number}` }] : [],
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
return {
|
|
244
|
+
available: true,
|
|
245
|
+
self,
|
|
246
|
+
openPullRequests: prs.length,
|
|
247
|
+
limit,
|
|
248
|
+
truncated: prs.length >= limit,
|
|
249
|
+
overlapping,
|
|
250
|
+
conflicts: conflictGraph(prs, opts.symbols),
|
|
251
|
+
waves: mergeWaves(prs, opts.symbols),
|
|
252
|
+
undetermined,
|
|
253
|
+
symbolAnalysis: opts.symbols?.analysis ?? { outlinesRun: 0, outlineBudget: 0, outlineTimeoutMs: 0, remoteCalls: 0, remoteBudget: 0, remoteTimeoutMs: 0 },
|
|
254
|
+
summary: summarize(prs.length, overlapping.length, undetermined.length, self !== null && prs.some((p) => p.number === self)),
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function summarize(listed: number, overlapping: number, undetermined: number, hasSelf: boolean): string {
|
|
259
|
+
// The pull request under review is not a candidate for overlap, so it is not in the count.
|
|
260
|
+
const open = hasSelf ? listed - 1 : listed;
|
|
261
|
+
const other = hasSelf ? "other " : "";
|
|
262
|
+
const tail = undetermined === 0
|
|
263
|
+
? ""
|
|
264
|
+
: ` ${undetermined} ${plural(undetermined, "pull request's", "pull requests'")} files could not be read.`;
|
|
265
|
+
if (open === 0) return `No ${hasSelf ? "other " : ""}pull request is open, so nothing can overlap this change.${tail}`;
|
|
266
|
+
const requests = `${open} ${other}open ${plural(open, "pull request", "pull requests")}`;
|
|
267
|
+
if (overlapping === 0) return `None of the ${requests} ${plural(open, "touches", "touch")} a file this change touches.${tail}`;
|
|
268
|
+
return `${overlapping} of ${requests} ${plural(overlapping, "touches", "touch")} a file this change touches.${tail}`;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/** The stated limitation: gh could not answer, so the section says so rather than vanishing. */
|
|
272
|
+
export function unavailableOverlap(code: string, reason: string): OverlapUnavailable {
|
|
273
|
+
return { available: false, code, reason, summary: `Cross-pull-request overlap unavailable: ${reason}.` };
|
|
274
|
+
}
|