@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/src/systems.ts ADDED
@@ -0,0 +1,175 @@
1
+ // The system a call site lives in, and the mitigation its row carries
2
+ // (flyingrobots/footprint#11).
3
+ //
4
+ // footprint#2 §3 asks for one blast-radius table per system, a row per call site, each with a
5
+ // containment mitigation, allowing "None needed" so a row is never padded. Two things have to be
6
+ // defined precisely for that to be a report rather than an opinion, and both are here.
7
+ //
8
+ // 1. WHAT A SYSTEM IS. The first path segment of a call site's repository-relative path -- the
9
+ // directory immediately under the repository root. A path with no directory component, a file
10
+ // at the repository root, belongs to no system and is grouped under `(unassigned)`: present and
11
+ // named, never dropped.
12
+ //
13
+ // This is the simplest rule that groups anything, and it needs no configuration and no list to
14
+ // keep. `src/` descending one more level was considered and rejected: `src/` in this repository
15
+ // is flat, so the branch would have no effect on the run that proves the feature, and a rule
16
+ // whose extra clause never fires is a rule nobody can check. A configurable list was rejected
17
+ // for the same reason -- it would make the grouping depend on a setting rather than on the
18
+ // tree, and a reviewer could not tell from the output which systems were even considered.
19
+ //
20
+ // 2. WHAT A MITIGATION IS. `AGENTS.md`: report, never judge. So a mitigation here is not advice.
21
+ // It is one of three sentences, chosen by two facts about the call site and nothing else:
22
+ // whether the call site is in the very file the symbol is exported from, and what
23
+ // `classify()` calls that file. Four bases, three forms, nothing derived from a judgement
24
+ // about importance, risk or quality:
25
+ //
26
+ // declaring_file the call site is in the file the symbol is exported from -> None needed
27
+ // test_file `classify()` calls the call site's file a test -> None needed
28
+ // other_system anything else, inside a system -> the system, named
29
+ // unassigned anything else, outside every system -> said as such
30
+ //
31
+ // Both "None needed" bases are facts a reviewer can check in one line, not an assessment: the
32
+ // declaring file is the file whose export surface moved, so a call site in it is the change
33
+ // itself rather than something the change reaches; and a test file is what `classify()` says it
34
+ // is, by the documented ordered rules in `src/classify.ts`.
35
+ //
36
+ // What this deliberately does NOT say: whether a call site matters, whether it will break,
37
+ // whether it is covered, or what anyone should do about it. The moment a mitigation says
38
+ // "consider adding a test" it is a verdict, and footprint stops being a report. OUT-14e holds
39
+ // the output to the three forms with an allowlist the test declares for itself.
40
+ //
41
+ // Everything here is pure, so the whole of OUT-14 except the end-to-end case runs without git.
42
+
43
+ import { classify } from "./classify.ts";
44
+ import type { CallSite } from "./callsites.ts";
45
+
46
+ /** What the grouping calls itself on the wire, so a reader knows which rule produced it. */
47
+ export const SYSTEM_RULE = "path_prefix" as const;
48
+
49
+ /** The system of a call site that is in no system. A name, not a null: a group, not a gap. */
50
+ export const UNASSIGNED_SYSTEM = "(unassigned)" as const;
51
+
52
+ /** The one mitigation that is not a statement about a path. Spelled exactly as the issue asks. */
53
+ export const NONE_NEEDED = "None needed" as const;
54
+
55
+ /** The grouping rule, once, quoted onto every record so the output explains itself. */
56
+ export const SYSTEM_RULE_DETAIL =
57
+ "A system is the first path segment of a call site's repository-relative path: the directory immediately under the repository root, however deep the file sits inside it. A file at the repository root has no such segment and is grouped under (unassigned), which is listed last and never dropped. Systems are ordered by name so the grouping follows the tree rather than the order the scan found the sites in.";
58
+
59
+ /** The mitigation derivation, once, quoted onto every record for the same reason. */
60
+ export const MITIGATION_RULE =
61
+ "Every row's mitigation is derived from two reported facts about that call site and nothing else: whether it is in the very file the symbol is exported from, and what classify() calls that file. A call site in the symbol's own declaring file, or in a test file, is \"None needed\"; any other call site states the system it is in, or that it is outside every known system. No mitigation is a judgement about importance, risk or coverage, and none is advice: footprint reports and does not judge (AGENTS.md).";
62
+
63
+ /** Which of the two facts chose a row's mitigation, on the wire, so the derivation is checkable. */
64
+ export type MitigationBasis = "declaring_file" | "test_file" | "other_system" | "unassigned";
65
+
66
+ /** A call site with the system that contains it and the mitigation its row carries. */
67
+ export interface AssignedCallSite extends CallSite {
68
+ readonly system: string;
69
+ readonly mitigation: string;
70
+ readonly mitigationBasis: MitigationBasis;
71
+ }
72
+
73
+ /**
74
+ * The system containing `path`: its first path segment, or `(unassigned)` when it has none.
75
+ *
76
+ * A leading slash is treated as no segment too. `git ls-files` never prints one, so this is a
77
+ * guard rather than a case: a path footprint cannot place is grouped, not dropped.
78
+ */
79
+ export function systemOf(path: string): string {
80
+ const slash = path.indexOf("/");
81
+ return slash <= 0 ? UNASSIGNED_SYSTEM : path.slice(0, slash);
82
+ }
83
+
84
+ /**
85
+ * Which fact decides this row's mitigation. Ordered, and the order is load-bearing: the declaring
86
+ * file wins over everything, because a symbol declared in a test file is still its own
87
+ * declaration, and `classify()` wins over the system, because a test fixture at the repository
88
+ * root is a test file before it is a file in no system.
89
+ */
90
+ export function mitigationBasis(symbolFile: string, siteFile: string): MitigationBasis {
91
+ if (siteFile === symbolFile) return "declaring_file";
92
+ if (classify(siteFile) === "test") return "test_file";
93
+ return systemOf(siteFile) === UNASSIGNED_SYSTEM ? "unassigned" : "other_system";
94
+ }
95
+
96
+ /** The sentence for a basis. Three forms, and no fourth: see OUT-14e. */
97
+ export function mitigationFor(basis: MitigationBasis, system: string): string {
98
+ switch (basis) {
99
+ case "declaring_file":
100
+ case "test_file":
101
+ return NONE_NEEDED;
102
+ case "other_system":
103
+ return `Call site in ${system}, outside the changed file.`;
104
+ case "unassigned":
105
+ return "Call site outside every known system, outside the changed file.";
106
+ }
107
+ }
108
+
109
+ /** A call site, placed in its system and given its row's mitigation. */
110
+ export function assignCallSite(symbolFile: string, site: CallSite): AssignedCallSite {
111
+ const system = systemOf(site.file);
112
+ const basis = mitigationBasis(symbolFile, site.file);
113
+ return { file: site.file, line: site.line, system, mitigation: mitigationFor(basis, system), mitigationBasis: basis };
114
+ }
115
+
116
+ /** One row of one table: which symbol's reach this call site is. */
117
+ export interface SystemRow {
118
+ readonly symbol: string;
119
+ readonly site: AssignedCallSite;
120
+ }
121
+
122
+ /** One table: a system and the rows in it. */
123
+ export interface SystemGroup {
124
+ readonly system: string;
125
+ readonly rows: readonly SystemRow[];
126
+ }
127
+
128
+ /** A system and how many call sites are in it, which is what the result carries. */
129
+ export interface SystemCount {
130
+ readonly system: string;
131
+ readonly callSites: number;
132
+ }
133
+
134
+ /**
135
+ * Systems before `(unassigned)`, and otherwise by name. Comparing by name and nothing else keeps
136
+ * the grouping a function of the tree: two runs over the same sites group identically whatever
137
+ * order the scan found them in, which is what lets the grouping sit inside the digest.
138
+ */
139
+ function bySystem(a: string, b: string): number {
140
+ if (a === b) return 0;
141
+ if (a === UNASSIGNED_SYSTEM) return 1;
142
+ if (b === UNASSIGNED_SYSTEM) return -1;
143
+ return a.localeCompare(b);
144
+ }
145
+
146
+ /**
147
+ * Group rows into one group per system. Rows inside a group are ordered by symbol, then file, then
148
+ * line, so a symbol's call sites in one system are contiguous -- which is what lets `--human` cap
149
+ * a long symbol and name the remainder once, rather than interleaving.
150
+ */
151
+ export function groupBySystem(rows: Iterable<SystemRow>): readonly SystemGroup[] {
152
+ const groups = new Map<string, SystemRow[]>();
153
+ for (const row of rows) {
154
+ const list = groups.get(row.site.system);
155
+ if (list === undefined) groups.set(row.site.system, [row]);
156
+ else list.push(row);
157
+ }
158
+ return [...groups.keys()].sort(bySystem).map((system) => ({
159
+ system,
160
+ rows: groups.get(system)!.sort(
161
+ (a, b) => a.symbol.localeCompare(b.symbol) || a.site.file.localeCompare(b.site.file) || a.site.line - b.site.line,
162
+ ),
163
+ }));
164
+ }
165
+
166
+ /**
167
+ * How many call sites each system holds, in the same order. These sum to `counts.call_sites` by
168
+ * construction -- every call site is in exactly one system, `(unassigned)` included -- and a test
169
+ * asserts the sum rather than trusting the construction.
170
+ */
171
+ export function systemCounts(sites: Iterable<AssignedCallSite>): readonly SystemCount[] {
172
+ const counts = new Map<string, number>();
173
+ for (const site of sites) counts.set(site.system, (counts.get(site.system) ?? 0) + 1);
174
+ return [...counts.keys()].sort(bySystem).map((system) => ({ system, callSites: counts.get(system)! }));
175
+ }