@flyingrobots/footprint 0.0.0-stage → 0.6.0
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 +436 -0
- package/LICENSE +202 -0
- package/NOTICE +14 -0
- package/README.md +128 -2
- package/RELEASE.md +12 -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
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: footprint
|
|
3
|
+
description: >-
|
|
4
|
+
Run footprint on a pull request's range, post its report as the pull request's one Footprint
|
|
5
|
+
comment (updated in place on every run, with a Changelog row and your Impact and Data flow
|
|
6
|
+
reading of the call sites), and act on what it lists before and after the merge. Use whenever a pull request is open, or is about to be opened,
|
|
7
|
+
reviewed, updated or merged, and whenever someone asks to "post the footprint", for a footprint
|
|
8
|
+
report, or for what a change touches, its blast radius, its overlap with other pull requests or
|
|
9
|
+
how it lands.
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
# footprint
|
|
13
|
+
|
|
14
|
+
footprint reads a diff and reports what it touches: its composition by file kind, the exported
|
|
15
|
+
symbols that moved and where they are called from, what to check before the merge and do after
|
|
16
|
+
it, and which other open pull requests change the same files. The report lives in one comment
|
|
17
|
+
per pull request, updated in place on every run, with a Changelog of the runs at its foot. It **reports; it does not judge**.
|
|
18
|
+
Nothing in its output is a verdict, an approval or a merge gate, and this skill must never present
|
|
19
|
+
it as one.
|
|
20
|
+
|
|
21
|
+
Source and reference: <https://github.com/flyingrobots/footprint>.
|
|
22
|
+
|
|
23
|
+
## 1. Find the program — never a bare `footprint`
|
|
24
|
+
|
|
25
|
+
On macOS, `/usr/bin/footprint` is an unrelated Apple memory tool and comes first on `PATH`. Never
|
|
26
|
+
run a bare `footprint`. Use, in this order:
|
|
27
|
+
|
|
28
|
+
1. **The published package**, when `npm view @flyingrobots/footprint version` answers:
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
fp() { bunx @flyingrobots/footprint "$@"; }
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
2. **Before the package is published**, a clone of `flyingrobots/footprint` with its dependencies
|
|
35
|
+
installed. Use one the user already has, or make one in a scratch directory:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
git clone https://github.com/flyingrobots/footprint "$SCRATCH/footprint"
|
|
39
|
+
(cd "$SCRATCH/footprint" && bun install)
|
|
40
|
+
fp() { bun "$SCRATCH/footprint/src/cli.ts" "$@"; }
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`$SCRATCH` stands for whatever directory you use for temporary files; do not write a
|
|
44
|
+
machine-specific path into any repository.
|
|
45
|
+
|
|
46
|
+
`fp` is a shell function, not a variable holding a command, because zsh (the macOS default shell)
|
|
47
|
+
does not word-split an unquoted variable: `FP="bun …"; $FP` runs the whole string as one command
|
|
48
|
+
name and exits 127. A function runs the same in bash and zsh. Shell state may not survive from one
|
|
49
|
+
tool call to the next, so define `fp` in the same command that runs it.
|
|
50
|
+
|
|
51
|
+
footprint runs on **Bun**. Its structural analysis comes from graft, which runs on **Node**, so
|
|
52
|
+
`node` must be on `PATH` (`command -v node`). Without it the report still posts, but says the
|
|
53
|
+
structural section is unavailable; fix `PATH` and re-run rather than posting a degraded report
|
|
54
|
+
when you can.
|
|
55
|
+
|
|
56
|
+
Always run footprint **from inside the repository under review**: it reads that repository's git
|
|
57
|
+
history, its `RELEASE.md` and its `AGENTS.md`.
|
|
58
|
+
|
|
59
|
+
## 2. Resolve the range
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
git fetch origin
|
|
63
|
+
BASE=$(gh pr view "$PR" --json baseRefName -q .baseRefName)
|
|
64
|
+
HEAD_REF=$(gh pr view "$PR" --json headRefName -q .headRefName)
|
|
65
|
+
RANGE="origin/$BASE...origin/$HEAD_REF"
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Three dots: the change since the branch left its base, not the base's own later commits. For a
|
|
69
|
+
pull request from a fork, `origin/$HEAD_REF` does not exist; fetch the pull request's head instead and
|
|
70
|
+
use it:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
git fetch origin "pull/$PR/head:refs/remotes/origin/pr-$PR"
|
|
74
|
+
RANGE="origin/$BASE...origin/pr-$PR"
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Before the pull request exists, use the local branch: `RANGE="origin/main...HEAD"`, after pushing
|
|
78
|
+
it.
|
|
79
|
+
|
|
80
|
+
## 3. Run the report and read it
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
OUT="$SCRATCH/footprint-$PR.md"
|
|
84
|
+
fp "$RANGE" --human --max-chars 57344 > "$OUT"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
`--human` is the Markdown comment. Without it footprint writes JSON Lines, which is what to parse
|
|
88
|
+
if you need a field rather than the prose. `--max-chars 57344` holds the report to 57344
|
|
89
|
+
characters, leaving 8192 of GitHub's 65536-character comment limit for the Changelog that step 5
|
|
90
|
+
adds. Always pass it when the report will be posted.
|
|
91
|
+
|
|
92
|
+
The report is bounded, whatever the change, so a large change prints summaries and says what it
|
|
93
|
+
omitted (`N more ... omitted`, or a `Symbols with more than 50 call sites` table with a count per
|
|
94
|
+
system). The rest is in the JSON Lines result lines and the receipt (its `files` field for the
|
|
95
|
+
file list), which are not bounded.
|
|
96
|
+
|
|
97
|
+
Read the call-site tables (under **Call sites by system**) now: step 4 is about them.
|
|
98
|
+
|
|
99
|
+
## 4. Assess the call sites: the Impact and Data flow columns
|
|
100
|
+
|
|
101
|
+
footprint found the call sites; it cannot see what the change does at them at run time. You can,
|
|
102
|
+
by reading the code. Your reading goes into two columns of the call-site tables, and the comment
|
|
103
|
+
labels them as **your reading, not footprint's measurement**. Never present them as footprint's.
|
|
104
|
+
|
|
105
|
+
- **Impact**: how run-time behaviour changes at this call site, and downstream of it in that
|
|
106
|
+
system. What now happens differently when this line runs, and what that changes further along.
|
|
107
|
+
"None: the name appears in a comment" is an answer.
|
|
108
|
+
- **Data flow**: what reads the value, what writes it, and where the call's arguments come from.
|
|
109
|
+
|
|
110
|
+
Write one JSON object per line to `$IMPACT`, a fresh file for every run:
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{"symbol": "blastRadius", "system": "src", "impact": "...", "dataFlow": "..."}
|
|
114
|
+
{"symbol": "blastRadius", "system": "src", "file": "src/cli.ts", "impact": "...", "dataFlow": "..."}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
<sub>Illustrative: the `...` are where your reading goes.</sub>
|
|
118
|
+
|
|
119
|
+
`symbol` and `system` are as the tables print them. Without `file` an entry covers every row of
|
|
120
|
+
that symbol in that system; with `file` it covers that symbol's rows in that file and wins over the
|
|
121
|
+
system-wide entry. A row no entry covers says `not assessed`. An empty file is allowed, and then
|
|
122
|
+
every row says `not assessed`.
|
|
123
|
+
|
|
124
|
+
Be proportionate, and say where you did not look:
|
|
125
|
+
|
|
126
|
+
- **Assess every row whose mitigation is not `None needed`** (not the declaring file, not a test
|
|
127
|
+
file) for each **removed** or **signature-changed** symbol. Those are the rows where a caller
|
|
128
|
+
can break.
|
|
129
|
+
- **Rows the report summarises** (the `Symbols with more than 50 call sites` table, or a table that
|
|
130
|
+
says it omitted rows): assess per system, one entry without `file` for each system the symbol
|
|
131
|
+
is called in. Read the omitted rows from the `footprint.blast_radius` result (`fp "$RANGE"`
|
|
132
|
+
without `--human`).
|
|
133
|
+
- Added symbols, test files and the declaring file: assess when it matters; otherwise leave them
|
|
134
|
+
to say `not assessed`.
|
|
135
|
+
- When you did not look at a row you were asked to assess, say so and why, rather than leave it
|
|
136
|
+
to read as an oversight: `"impact": "not assessed: generated file"`.
|
|
137
|
+
- Never carry an earlier run's entries over. Every run is a new head; assess it again. An entry
|
|
138
|
+
that matches no row of this head is reported as an `impact.unmatched` warning: fix or drop it.
|
|
139
|
+
|
|
140
|
+
## 5. Post it: the one Footprint comment
|
|
141
|
+
|
|
142
|
+
A pull request carries **one** Footprint comment. `fp comment` finds it and updates it in place,
|
|
143
|
+
or creates it, and appends one row to the **Changelog** at its foot. Never post the report with
|
|
144
|
+
`gh pr comment`; that makes a second comment.
|
|
145
|
+
|
|
146
|
+
Set the three cells of the new Changelog row:
|
|
147
|
+
|
|
148
|
+
```bash
|
|
149
|
+
CHANGE="First report: 4 files, +120/-8; two signature changes"
|
|
150
|
+
REASON="Pull request opened"
|
|
151
|
+
REMARKS="none"
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
<sub>Illustrative values.</sub>
|
|
155
|
+
|
|
156
|
+
- **Change**: what moved in the report since the previous row: files and lines, symbols added,
|
|
157
|
+
removed or re-signed, a new overlap or a cleared one. On the first run, the headline.
|
|
158
|
+
- **Reason**: why footprint was run again: `Pull request opened`, `New commits pushed`, `Review
|
|
159
|
+
fix`, `Merged main`.
|
|
160
|
+
- **Remarks**: anything a reader of the comment should know: a section that was unavailable and
|
|
161
|
+
why, call sites you did not assess, a follow-up you opened. `none` when there is nothing.
|
|
162
|
+
|
|
163
|
+
Then render the report with your assessment and post it, from inside the repository under review
|
|
164
|
+
(`fp comment` reads the pull request's repository from the working directory, as `gh` does):
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
OUT="$SCRATCH/footprint-$PR.md"
|
|
168
|
+
fp "$RANGE" --human --max-chars 57344 --impact "$IMPACT" > "$OUT"
|
|
169
|
+
fp comment "$PR" --report "$OUT" --change "$CHANGE" --reason "$REASON" --remarks "$REMARKS"
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Post the report as footprint wrote it; do not edit, trim or summarise it into the comment.
|
|
173
|
+
|
|
174
|
+
What `fp comment` does, so you can tell the user:
|
|
175
|
+
|
|
176
|
+
- It looks for the comment whose first line is `<!-- footprint:report -->`, written by the current
|
|
177
|
+
`gh` user, and edits it in place. With none, it adopts the most recent comment by the current
|
|
178
|
+
`gh` user that begins `## Footprint` (a report posted before this flow existed) and says so in
|
|
179
|
+
the row's Remarks. With neither, it creates the comment.
|
|
180
|
+
- It carries every earlier Changelog row over verbatim, oldest first, and appends the new row: the
|
|
181
|
+
UTC time, the pull request head's 7-character SHA, and your Change, Reason and Remarks.
|
|
182
|
+
- It keeps the whole comment within 65536 characters. If the Changelog grows too long, it keeps
|
|
183
|
+
the newest rows and says how many older ones were dropped.
|
|
184
|
+
- It **never edits another user's comment**. If the Footprint comment was written by someone else,
|
|
185
|
+
it stops with `comment.other_author` and writes nothing: tell the user, and do not post a second
|
|
186
|
+
report another way.
|
|
187
|
+
|
|
188
|
+
Its result line (`footprint.comment`) names the comment's `url`, whether it was `created` or
|
|
189
|
+
`updated`, and the Changelog's row count. Pass `--dry-run` to see what it would do without
|
|
190
|
+
writing.
|
|
191
|
+
|
|
192
|
+
## 6. Follow what it lists
|
|
193
|
+
|
|
194
|
+
Read the posted report and act on every item below. Each one is either handled in the pull
|
|
195
|
+
request or answered in a PR comment that says why it needs nothing. Silence is neither.
|
|
196
|
+
|
|
197
|
+
- **Before merge.** Every item: a migration and its ordering constraint, a generated file, a
|
|
198
|
+
manifest changed without its lockfile. If the report shows the unguarded-change warning (source
|
|
199
|
+
changed and no migration, flag, version or changelog entry), either add what the repository's
|
|
200
|
+
working agreement asks for or say why none applies.
|
|
201
|
+
- **Breaking changes.** Each one graft names, and each removed or signature-changed export on the
|
|
202
|
+
blast radius: confirm the callers were updated, or the change is versioned as breaking. Your
|
|
203
|
+
Impact column is where you have already read them.
|
|
204
|
+
- **Call sites outside the changed file.** Every row whose mitigation is not `None needed` names
|
|
205
|
+
a call site in another system, or outside every known system. Check each: updated, unaffected
|
|
206
|
+
(say why), or followed up. Where the report says it omitted call sites, or counted a symbol's
|
|
207
|
+
sites instead of listing them, its own figure says how many carry a mitigation other than `None
|
|
208
|
+
needed`; read those from the `footprint.blast_radius` result (run `fp "$RANGE"` without
|
|
209
|
+
`--human`) and check them the same way.
|
|
210
|
+
- **Overlap.** Each open pull request that shares a file, what each shared file looks like at the
|
|
211
|
+
symbol level, and the proposed merge order. Tell the authors of overlapping pull requests,
|
|
212
|
+
naming the shared symbols when the report gives them. Land in the proposed waves unless there is
|
|
213
|
+
a reason not to, and state that reason. A `different symbols` file does not separate waves, but
|
|
214
|
+
git can still conflict on it: rerun footprint after the first of the two merges. An `undetermined`
|
|
215
|
+
file is file-level overlap, and its reason says why (for example, a head commit that is not
|
|
216
|
+
local: `git fetch origin pull/<n>/head`).
|
|
217
|
+
- **After merge.** Once the pull request has merged, do each step the report quoted from the
|
|
218
|
+
repository's `RELEASE.md` or `AGENTS.md`, in order. footprint quotes them; it never runs them.
|
|
219
|
+
If it said no steps are declared, it invented none, and neither should you.
|
|
220
|
+
|
|
221
|
+
## 7. Re-run after every push
|
|
222
|
+
|
|
223
|
+
Every push changes the range. After each push, and always after the last one before merge, run
|
|
224
|
+
steps 2 to 5 again: a fresh report, a fresh assessment, and `fp comment`, which updates the same
|
|
225
|
+
comment and adds a row saying what changed and why. Then check step 6 against it. A report on an
|
|
226
|
+
earlier head is a report on a different change.
|
|
227
|
+
|
|
228
|
+
## What never to do
|
|
229
|
+
|
|
230
|
+
- Never call the report a pass, a fail, an approval or a verdict, and never block or merge on it
|
|
231
|
+
alone. It is input to a decision.
|
|
232
|
+
- Never reshape a diff only to make the report quieter. The moment the output becomes a gate,
|
|
233
|
+
diffs get shaped to satisfy it and the report stops meaning anything.
|
|
234
|
+
- Never fill in a number or a step the report did not state. Every figure in it is computed;
|
|
235
|
+
anything added by hand is not. Your Impact and Data flow entries are the one place your own
|
|
236
|
+
reading goes, and the comment says so.
|
|
237
|
+
- Never post a second Footprint comment, and never edit or delete another user's.
|
package/src/args.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// The arguments a command's handler sees, after parley-emit has removed its own flags (--human,
|
|
2
|
+
// --dry-run, --schema, --help). Every flag a command takes carries a value, as `--flag value` or
|
|
3
|
+
// `--flag=value`; an unknown flag is an error rather than a positional, so `--max-chars 40960`
|
|
4
|
+
// can never be read as a range named `40960`.
|
|
5
|
+
|
|
6
|
+
export type Parsed =
|
|
7
|
+
| { readonly ok: true; readonly positionals: readonly string[]; readonly flags: Readonly<Record<string, string>> }
|
|
8
|
+
| { readonly ok: false; readonly problem: string };
|
|
9
|
+
|
|
10
|
+
export function parseArgs(argv: readonly string[], valued: readonly string[]): Parsed {
|
|
11
|
+
const positionals: string[] = [];
|
|
12
|
+
const flags: Record<string, string> = {};
|
|
13
|
+
for (let i = 0; i < argv.length; i++) {
|
|
14
|
+
const a = argv[i]!;
|
|
15
|
+
if (!a.startsWith("-")) {
|
|
16
|
+
positionals.push(a);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const eq = a.indexOf("=");
|
|
20
|
+
const name = eq < 0 ? a : a.slice(0, eq);
|
|
21
|
+
if (!valued.includes(name)) return { ok: false, problem: `unknown flag ${name}; this command takes ${valued.join(", ") || "no flags"}` };
|
|
22
|
+
const value = eq < 0 ? argv[++i] : a.slice(eq + 1);
|
|
23
|
+
if (value === undefined) return { ok: false, problem: `${name} needs a value` };
|
|
24
|
+
if (name in flags) return { ok: false, problem: `${name} was given twice` };
|
|
25
|
+
flags[name] = value;
|
|
26
|
+
}
|
|
27
|
+
return { ok: true, positionals, flags };
|
|
28
|
+
}
|
package/src/bound.ts
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// The size bound on the --human report (flyingrobots/footprint#51).
|
|
2
|
+
//
|
|
3
|
+
// GitHub rejects a comment body longer than 65536 characters, and the report is posted as one
|
|
4
|
+
// comment. The four sections the CLI prints (composition, landing, blast radius, overlap) share
|
|
5
|
+
// that limit. Landing is read from the repository's own release documents and is not trimmed; the
|
|
6
|
+
// other three each get a budget and trim their lists to it, *stating* every omission with its
|
|
7
|
+
// count. The JSON Lines records are never trimmed: they carry every row the Markdown leaves out.
|
|
8
|
+
|
|
9
|
+
/** GitHub's maximum comment body, in characters. */
|
|
10
|
+
export const REPORT_LIMIT = 65536;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The smallest bound `--max-chars` accepts: the reserved budgets of composition, landing and
|
|
14
|
+
* overlap (32768 together) and at least 8192 for the blast radius.
|
|
15
|
+
*/
|
|
16
|
+
export const MIN_REPORT_CHARS = 40960;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The bound the skill runs the report at when it is posted with `footprint comment`
|
|
20
|
+
* (flyingrobots/footprint#59): it leaves the marker and the Changelog 8192 characters of the
|
|
21
|
+
* comment's 65536.
|
|
22
|
+
*/
|
|
23
|
+
export const COMMENT_REPORT_LIMIT = 57344;
|
|
24
|
+
|
|
25
|
+
/** What the composition section may take: its Files list is the only part that grows. */
|
|
26
|
+
export const COMPOSITION_BUDGET = 16384;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* What the overlap section is held to. It is rendered last, after the blast radius has already
|
|
30
|
+
* been written, so its room is reserved rather than measured. A report with no overlapping pull
|
|
31
|
+
* request wastes most of it; that is the price of not reordering the run.
|
|
32
|
+
*/
|
|
33
|
+
export const OVERLAP_BUDGET = 8192;
|
|
34
|
+
|
|
35
|
+
/** What the landing section may take: the steps it reads out of the repository's own documents. */
|
|
36
|
+
export const LANDING_BUDGET = 8192;
|
|
37
|
+
|
|
38
|
+
/** Newlines the output protocol puts between the four sections, with margin. */
|
|
39
|
+
const JOIN_SLACK = 16;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The room left for the blast-radius section once composition and landing have been rendered,
|
|
43
|
+
* under `limit` (`--max-chars`, REPORT_LIMIT unless given).
|
|
44
|
+
* Each of the other three sections is clamped to its own budget, so with composition and landing
|
|
45
|
+
* at most COMPOSITION_BUDGET and LANDING_BUDGET this is never below 32752 at the default limit,
|
|
46
|
+
* and never below 8176 at MIN_REPORT_CHARS.
|
|
47
|
+
*/
|
|
48
|
+
export function blastBudget(compositionLength: number, landingLength: number, limit: number = REPORT_LIMIT): number {
|
|
49
|
+
return Math.max(0, limit - compositionLength - landingLength - OVERLAP_BUDGET - JOIN_SLACK);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* How much of the blast radius is written out before the remainder is counted instead.
|
|
54
|
+
*
|
|
55
|
+
* - `symbols`: rows of the exported-symbols table. Signature-changed and removed symbols are kept
|
|
56
|
+
* before added ones.
|
|
57
|
+
* - `sites`: rows of each system's call-site table, and of the summary table, and names in the
|
|
58
|
+
* lists of symbols with no call site. Rows whose mitigation is not `None needed` are kept first.
|
|
59
|
+
* - `summariseAbove`: a symbol with more call sites than this, in all systems together, is
|
|
60
|
+
* summarised as a count per system instead of being listed. A name like `type` or `id`, which
|
|
61
|
+
* a data file's field gives a symbol, matches tens of thousands of whole-word sites; listing
|
|
62
|
+
* them tells a reviewer nothing a count does not. The scan still counts them all, and the
|
|
63
|
+
* record still carries each one.
|
|
64
|
+
*
|
|
65
|
+
* `symbols` and `sites` are the starting point: the CLI halves the larger section until the
|
|
66
|
+
* report fits. `summariseAbove` is never shrunk.
|
|
67
|
+
*/
|
|
68
|
+
export interface BlastLimits {
|
|
69
|
+
readonly symbols: number;
|
|
70
|
+
readonly sites: number;
|
|
71
|
+
readonly summariseAbove: number;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export const BLAST_LIMITS: BlastLimits = { symbols: 100, sites: 100, summariseAbove: 50 };
|
|
75
|
+
|
|
76
|
+
/** No trimming at all: what the section was before the bound, and what the tests measure against. */
|
|
77
|
+
export const UNBOUNDED_BLAST: BlastLimits = { symbols: Infinity, sites: Infinity, summariseAbove: Infinity };
|
|
78
|
+
|
|
79
|
+
/** A rendering and how many characters each of its trimmable sections took. */
|
|
80
|
+
export interface Rendered<K extends string> {
|
|
81
|
+
readonly text: string;
|
|
82
|
+
readonly sizes: Readonly<Record<K, number>>;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Render with `start` limits; while the text is over `budget`, halve the limit of the section that
|
|
87
|
+
* took the most characters (ties by key order) and render again. Deterministic: the same input and
|
|
88
|
+
* budget give the same text. Stops when every limit is zero, returning that rendering, which may
|
|
89
|
+
* still be over the budget when what is left is not trimmable; the caller says so.
|
|
90
|
+
*/
|
|
91
|
+
export function shrinkToFit<K extends string>(
|
|
92
|
+
start: Readonly<Record<K, number>>,
|
|
93
|
+
build: (limits: Readonly<Record<K, number>>) => Rendered<K>,
|
|
94
|
+
budget: number,
|
|
95
|
+
): Rendered<K> {
|
|
96
|
+
let limits = { ...start };
|
|
97
|
+
let out = build(limits);
|
|
98
|
+
const keys = Object.keys(start) as K[];
|
|
99
|
+
while (out.text.length > budget) {
|
|
100
|
+
const open = keys.filter((k) => limits[k] > 0);
|
|
101
|
+
if (open.length === 0) break;
|
|
102
|
+
const largest = open.reduce((a, b) => (out.sizes[b] > out.sizes[a] ? b : a));
|
|
103
|
+
const current = limits[largest];
|
|
104
|
+
// An unlimited section is first cut to what it actually printed, so halving means something.
|
|
105
|
+
const next = Number.isFinite(current) ? Math.floor(current / 2) : Math.max(0, Math.floor(out.sizes[largest] / 2));
|
|
106
|
+
limits = { ...limits, [largest]: next };
|
|
107
|
+
out = build(limits);
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The last resort, which makes the bound total: whatever a section's lists were cut to, text over
|
|
114
|
+
* `max` is cut again here, at a line boundary, so no input can exceed it. Any `<details>` the cut
|
|
115
|
+
* left open is closed, and the end says how many characters were cut and from what, so even this
|
|
116
|
+
* is never silent. Reached only by a section that is not a list, or by lists whose cut-to-nothing
|
|
117
|
+
* form is still too long.
|
|
118
|
+
*/
|
|
119
|
+
export function clamp(text: string, max: number, what: string): string {
|
|
120
|
+
if (text.length <= max) return text;
|
|
121
|
+
const notice = (cut: number): string =>
|
|
122
|
+
`<sub>${cut} character${cut === 1 ? "" : "s"} of ${what} omitted to keep the comment under GitHub's ${REPORT_LIMIT}-character limit.</sub>`;
|
|
123
|
+
let keep = Math.max(0, max - 400);
|
|
124
|
+
for (;;) {
|
|
125
|
+
let prefix = text.slice(0, keep);
|
|
126
|
+
const newline = prefix.lastIndexOf("\n");
|
|
127
|
+
if (newline > 0) prefix = prefix.slice(0, newline);
|
|
128
|
+
const open = Math.max(0, (prefix.match(/<details>/g) ?? []).length - (prefix.match(/<\/details>/g) ?? []).length);
|
|
129
|
+
const out = `${prefix}\n\n${"</details>\n\n".repeat(open)}${notice(text.length - prefix.length)}`;
|
|
130
|
+
if (out.length <= max) return out;
|
|
131
|
+
if (keep === 0) return notice(text.length).slice(0, max);
|
|
132
|
+
keep = Math.max(0, keep - Math.max(1, out.length - max));
|
|
133
|
+
}
|
|
134
|
+
}
|
package/src/callsites.ts
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// Call sites for a symbol: where in the tree its name occurs, with file and line
|
|
2
|
+
// (flyingrobots/footprint#10).
|
|
3
|
+
//
|
|
4
|
+
// Why footprint computes this itself. graft 0.14.0 has reference analysis, but no CLI contract of
|
|
5
|
+
// its own answers with a call site's *line*: `symbol blame --json` carries a `referenceCount` and
|
|
6
|
+
// no per-reference list, and `struct review --json`, `symbol difficulty --json` and
|
|
7
|
+
// `struct test-coverage --json` carry referencing *file paths* only. All four read the WARP index
|
|
8
|
+
// rather than the tree. So this is footprint's own scan, and it is deliberately the most
|
|
9
|
+
// conservative one that can be checked by an independent oracle:
|
|
10
|
+
//
|
|
11
|
+
// a call site is a whole-word occurrence of the symbol's name in a tracked, non-binary file.
|
|
12
|
+
//
|
|
13
|
+
// "Whole-word" is `grep -w`'s own rule -- the match may not be preceded or followed by a word
|
|
14
|
+
// constituent character -- which is what lets the test compare footprint's answer with real
|
|
15
|
+
// `grep -I -n -w` over `git ls-files` line for line, instead of with a hand-written expectation.
|
|
16
|
+
// A word character is a letter, a digit or an underscore, in any script: `[\p{L}\p{N}_]`. The
|
|
17
|
+
// Unicode part is not decoration. A differential fuzz against real grep caught an ASCII-only
|
|
18
|
+
// `[A-Za-z0-9_]` reporting a call site for `r` inside `rünïcode`, which grep does not and which an
|
|
19
|
+
// identifier scan must not either: in a UTF-8 locale grep's word characters are the locale's
|
|
20
|
+
// alphanumerics, and `ü` is one of them. The limits are stated everywhere the result is: it counts the declaration itself, a
|
|
21
|
+
// mention in a comment, a string or prose, and it cannot see a call through dynamic dispatch or a
|
|
22
|
+
// re-export under another name. It is evidence a reviewer can check, not a call graph.
|
|
23
|
+
//
|
|
24
|
+
// Matching is pure (`matchLines`), so most of the suite needs neither git nor a filesystem.
|
|
25
|
+
|
|
26
|
+
import { $ } from "bun";
|
|
27
|
+
import { join } from "node:path";
|
|
28
|
+
|
|
29
|
+
export interface CallSite {
|
|
30
|
+
readonly file: string;
|
|
31
|
+
readonly line: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** What the scan calls itself on the wire, so a reader knows what kind of evidence this is. */
|
|
35
|
+
export const SCAN_METHOD = "identifier_scan";
|
|
36
|
+
|
|
37
|
+
/** The word boundary, named once: `grep -w`'s rule, and the schema and the docs quote this. */
|
|
38
|
+
export const WORD_CHARACTER = "[\\p{L}\\p{N}_]";
|
|
39
|
+
|
|
40
|
+
const DECODER = new TextDecoder();
|
|
41
|
+
|
|
42
|
+
/** How much of a file is read to decide it is binary. GNU grep decides on its first buffer too. */
|
|
43
|
+
const BINARY_SNIFF_BYTES = 8192;
|
|
44
|
+
|
|
45
|
+
export type ScanUnavailableCode = "scan.not_run" | "scan.ls_files_failed";
|
|
46
|
+
|
|
47
|
+
export interface SkippedFiles {
|
|
48
|
+
/** Files holding a NUL byte: skipped, exactly as `grep -I` skips them. */
|
|
49
|
+
readonly binary: number;
|
|
50
|
+
/** Tracked files that could not be read, a file deleted from the working tree among them. */
|
|
51
|
+
readonly unreadable: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type CallSiteScan =
|
|
55
|
+
| {
|
|
56
|
+
readonly available: true;
|
|
57
|
+
/** One entry per symbol asked for, including the ones nothing references. */
|
|
58
|
+
readonly sites: ReadonlyMap<string, readonly CallSite[]>;
|
|
59
|
+
readonly filesScanned: number;
|
|
60
|
+
readonly skipped: SkippedFiles;
|
|
61
|
+
}
|
|
62
|
+
| {
|
|
63
|
+
readonly available: false;
|
|
64
|
+
readonly reason: { readonly code: ScanUnavailableCode; readonly detail: string };
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export function unavailableScan(code: ScanUnavailableCode, detail: string): CallSiteScan {
|
|
68
|
+
return { available: false, reason: { code, detail } };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* No scan was run. A distinct state from a scan that found nothing: an empty list means "looked,
|
|
73
|
+
* found none", and this means "did not look". Collapsing the two would report a figure that was
|
|
74
|
+
* never computed.
|
|
75
|
+
*/
|
|
76
|
+
export function notRunScan(): CallSiteScan {
|
|
77
|
+
return unavailableScan(
|
|
78
|
+
"scan.not_run",
|
|
79
|
+
"the export surface was parsed without a call-site scan, so no tree was read; this is not an empty call-site list",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The sites for one symbol, or null when the scan did not run -- never a misleading `[]`. */
|
|
84
|
+
export function sitesFor(scan: CallSiteScan, symbol: string): readonly CallSite[] | null {
|
|
85
|
+
if (!scan.available) return null;
|
|
86
|
+
return scan.sites.get(symbol) ?? null;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A NUL byte in the first buffer. The same heuristic `grep -I` uses, so the oracle agrees. */
|
|
90
|
+
export function isBinary(bytes: Uint8Array): boolean {
|
|
91
|
+
const end = Math.min(bytes.length, BINARY_SNIFF_BYTES);
|
|
92
|
+
for (let i = 0; i < end; i += 1) if (bytes[i] === 0) return true;
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function wordPattern(symbol: string): RegExp {
|
|
97
|
+
const escaped = symbol.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
98
|
+
return new RegExp(`(?<!${WORD_CHARACTER})${escaped}(?!${WORD_CHARACTER})`, "gu");
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The line numbers, 1-based and ascending, where each symbol occurs as a whole word.
|
|
103
|
+
*
|
|
104
|
+
* One entry per line however many times the symbol occurs on it, because that is what `grep -n`
|
|
105
|
+
* reports and the oracle has to be comparable. Every symbol asked for gets a key, so a symbol
|
|
106
|
+
* nothing references is an empty list rather than a missing one.
|
|
107
|
+
*/
|
|
108
|
+
export function matchLines(text: string, symbols: readonly string[]): Map<string, number[]> {
|
|
109
|
+
const out = new Map<string, number[]>();
|
|
110
|
+
for (const symbol of symbols) out.set(symbol, []);
|
|
111
|
+
|
|
112
|
+
let starts: number[] | null = null;
|
|
113
|
+
for (const symbol of symbols) {
|
|
114
|
+
if (symbol === "" || !text.includes(symbol)) continue; // the cheap filter before the regex
|
|
115
|
+
if (starts === null) starts = lineStarts(text);
|
|
116
|
+
const lines = out.get(symbol)!;
|
|
117
|
+
let last = -1;
|
|
118
|
+
for (const m of text.matchAll(wordPattern(symbol))) {
|
|
119
|
+
const line = lineOf(starts, m.index);
|
|
120
|
+
if (line !== last) {
|
|
121
|
+
lines.push(line);
|
|
122
|
+
last = line;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function lineStarts(text: string): number[] {
|
|
130
|
+
const starts = [0];
|
|
131
|
+
for (let i = text.indexOf("\n"); i !== -1; i = text.indexOf("\n", i + 1)) starts.push(i + 1);
|
|
132
|
+
return starts;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** Binary search: the 1-based line holding `offset`. */
|
|
136
|
+
function lineOf(starts: readonly number[], offset: number): number {
|
|
137
|
+
let lo = 0;
|
|
138
|
+
let hi = starts.length - 1;
|
|
139
|
+
while (lo < hi) {
|
|
140
|
+
const mid = (lo + hi + 1) >> 1;
|
|
141
|
+
if (starts[mid]! <= offset) lo = mid;
|
|
142
|
+
else hi = mid - 1;
|
|
143
|
+
}
|
|
144
|
+
return lo + 1;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** The tracked files, from git and nothing else, or null when git could not list them. */
|
|
148
|
+
export async function trackedFiles(repo: string): Promise<readonly string[] | null> {
|
|
149
|
+
const ls = await $`git -C ${repo} ls-files -z`.quiet().nothrow();
|
|
150
|
+
if (ls.exitCode !== 0) return null;
|
|
151
|
+
return ls.text().split("\0").filter((f) => f !== "");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Scan the tracked files of the working tree at `repo` for every symbol named.
|
|
156
|
+
*
|
|
157
|
+
* The working tree, not a ref: a run reports the call sites a reviewer would find in the checkout
|
|
158
|
+
* in front of them. With no symbol to look up, nothing is read at all -- an empty surface costs no
|
|
159
|
+
* git call and the scan honestly reports zero files scanned.
|
|
160
|
+
*/
|
|
161
|
+
export async function scanCallSites(repo: string, symbols: readonly string[]): Promise<CallSiteScan> {
|
|
162
|
+
const wanted = [...new Set(symbols)].filter((s) => s !== "");
|
|
163
|
+
if (wanted.length === 0) {
|
|
164
|
+
return { available: true, sites: new Map(), filesScanned: 0, skipped: { binary: 0, unreadable: 0 } };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const files = await trackedFiles(repo);
|
|
168
|
+
if (files === null) {
|
|
169
|
+
return unavailableScan(
|
|
170
|
+
"scan.ls_files_failed",
|
|
171
|
+
`git ls-files -z exited non-zero in ${repo}, so footprint has no file list to scan and will not guess at one`,
|
|
172
|
+
);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const found = new Map<string, CallSite[]>();
|
|
176
|
+
for (const s of wanted) found.set(s, []);
|
|
177
|
+
let filesScanned = 0;
|
|
178
|
+
let binary = 0;
|
|
179
|
+
let unreadable = 0;
|
|
180
|
+
|
|
181
|
+
for (const file of files) {
|
|
182
|
+
let bytes: Uint8Array;
|
|
183
|
+
try {
|
|
184
|
+
bytes = new Uint8Array(await Bun.file(join(repo, file)).arrayBuffer());
|
|
185
|
+
} catch {
|
|
186
|
+
unreadable += 1; // tracked but not on disk: deleted in the working tree, or unreadable
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (isBinary(bytes)) {
|
|
190
|
+
binary += 1;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
filesScanned += 1;
|
|
194
|
+
const text = DECODER.decode(bytes);
|
|
195
|
+
for (const [symbol, lines] of matchLines(text, wanted)) {
|
|
196
|
+
const sites = found.get(symbol)!;
|
|
197
|
+
for (const line of lines) sites.push({ file, line });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// By file then line, so the digest follows the tree rather than the order git listed it in.
|
|
202
|
+
for (const sites of found.values()) sites.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line);
|
|
203
|
+
return { available: true, sites: found, filesScanned, skipped: { binary, unreadable } };
|
|
204
|
+
}
|
package/src/cell.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// One Markdown table cell, for the text footprint did not write: the Changelog's Change, Reason
|
|
2
|
+
// and Remarks, and the agent's Impact and Data flow (flyingrobots/footprint#59).
|
|
3
|
+
|
|
4
|
+
/** What a cell says when it was given nothing: a dash, never a blank cell. */
|
|
5
|
+
const EMPTY_CELL = "—";
|
|
6
|
+
|
|
7
|
+
/** `|` escaped, every newline a `<br>`, never empty. */
|
|
8
|
+
export function cellText(text: string): string {
|
|
9
|
+
const t = text.trim().replace(/\r\n|\r|\n/g, "<br>").replace(/\|/g, "\\|");
|
|
10
|
+
return t === "" ? EMPTY_CELL : t;
|
|
11
|
+
}
|
package/src/classify.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// What kind of file is this? The categories exist to answer one question at a glance:
|
|
2
|
+
// "is this change shaped the way I think it is?"
|
|
3
|
+
|
|
4
|
+
export type Category =
|
|
5
|
+
| "source"
|
|
6
|
+
| "test"
|
|
7
|
+
| "docs"
|
|
8
|
+
| "config"
|
|
9
|
+
| "ci"
|
|
10
|
+
| "generated"
|
|
11
|
+
| "assets";
|
|
12
|
+
|
|
13
|
+
/** Ordered: the first matching rule wins, so a test fixture counts as a test, not a source file. */
|
|
14
|
+
const RULES: ReadonlyArray<readonly [Category, RegExp]> = [
|
|
15
|
+
["generated", /(^|\/)(dist|build|out|target|node_modules)\//],
|
|
16
|
+
["generated", /(^|\/)(package-lock\.json|bun\.lock|bun\.lockb|yarn\.lock|pnpm-lock\.yaml|Cargo\.lock|go\.sum)$/],
|
|
17
|
+
["generated", /\.(min\.js|min\.css|map)$/],
|
|
18
|
+
["ci", /(^|\/)\.github\//],
|
|
19
|
+
["ci", /(^|\/)(Dockerfile|compose\.ya?ml|docker-compose\.ya?ml|\.dockerignore)$/],
|
|
20
|
+
["test", /(^|\/)(tests?|__tests__|fixtures?|testdata)\//],
|
|
21
|
+
// `spec/` holds RSpec-style test code, and also a specification-first repository's prose
|
|
22
|
+
// (flyingrobots/footprint#42): a documentation extension there falls through to the docs rules.
|
|
23
|
+
["test", /(^|\/)spec\/(?!.*\.(md|mdx|rst|adoc|txt)$)/i],
|
|
24
|
+
["test", /\.(test|spec)\.[cm]?[jt]sx?$/],
|
|
25
|
+
["test", /_test\.(go|py|rs)$/],
|
|
26
|
+
// Agent skills are instructions an agent executes, not documentation: Markdown under a `skills/`
|
|
27
|
+
// path component, and any SKILL.md. `config` rather than a new category, which would change the
|
|
28
|
+
// shipped footprint.composition schema's enum (flyingrobots/footprint#34). Before the docs rules,
|
|
29
|
+
// after the test rules, so a skill fixture stays a test.
|
|
30
|
+
["config", /(^|\/)skills\/(.+\/)?[^/]+\.mdx?$/i],
|
|
31
|
+
["config", /(^|\/)SKILL\.md$/],
|
|
32
|
+
// Licence and notice texts, by whole file name: extensionless, so without this they fall through
|
|
33
|
+
// to source and read as code in an unanalysed language (flyingrobots/footprint#36).
|
|
34
|
+
["docs", /(^|\/)(LICENSE(\.[^/]+)?|NOTICE|COPYING)$/],
|
|
35
|
+
["docs", /(^|\/)docs?\//],
|
|
36
|
+
["docs", /\.(md|mdx|rst|adoc|txt)$/i],
|
|
37
|
+
["assets", /\.(png|jpe?g|gif|svg|webp|avif|ico|woff2?|ttf|otf|mp4|webm|pdf)$/i],
|
|
38
|
+
["config", /\.(json|ya?ml|toml|ini|env|conf|cfg)$/i],
|
|
39
|
+
["config", /(^|\/)\.[^/]+rc(\.[a-z]+)?$/],
|
|
40
|
+
["config", /(^|\/)(\.gitignore|\.gitattributes|\.editorconfig|tsconfig[^/]*\.json)$/],
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
export function classify(path: string): Category {
|
|
44
|
+
for (const [category, pattern] of RULES) {
|
|
45
|
+
if (pattern.test(path)) return category;
|
|
46
|
+
}
|
|
47
|
+
return "source";
|
|
48
|
+
}
|