@ftrdotdev/pi-tracker 0.6.0 → 0.8.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.
Files changed (36) hide show
  1. package/README.md +183 -60
  2. package/package.json +23 -11
  3. package/src/compaction/candidate.test.ts +9 -6
  4. package/src/compaction/candidate.ts +32 -2
  5. package/src/compaction/classifier.test.ts +67 -60
  6. package/src/compaction/classifier.ts +24 -22
  7. package/src/compaction/completion.test.ts +127 -49
  8. package/src/compaction/completion.ts +1 -3
  9. package/src/compaction/decision-record.test.ts +91 -0
  10. package/src/compaction/decision-record.ts +101 -0
  11. package/src/compaction/digest.test.ts +104 -9
  12. package/src/compaction/digest.ts +96 -9
  13. package/src/compaction/footprint.test.ts +135 -0
  14. package/src/compaction/footprint.ts +387 -0
  15. package/src/compaction/observations.test.ts +199 -0
  16. package/src/compaction/observations.ts +348 -0
  17. package/src/compaction/rules.test.ts +253 -0
  18. package/src/compaction/rules.ts +188 -0
  19. package/src/compaction/settings.test.ts +105 -49
  20. package/src/compaction/settings.ts +53 -31
  21. package/src/compaction/settle.test.ts +370 -0
  22. package/src/compaction/settle.ts +109 -53
  23. package/src/core/deps.test.ts +1 -1
  24. package/src/core/domain.test.ts +42 -9
  25. package/src/core/domain.ts +73 -8
  26. package/src/core/persistence.test.ts +7 -7
  27. package/src/core/store.test.ts +116 -50
  28. package/src/core/store.ts +92 -31
  29. package/src/index.test.ts +209 -68
  30. package/src/index.ts +170 -48
  31. package/src/presentation/tool-metadata.test.ts +263 -48
  32. package/src/presentation/tool-metadata.ts +325 -102
  33. package/src/presentation/ui.test.ts +122 -7
  34. package/src/presentation/ui.ts +81 -18
  35. package/src/presentation/vim-settings.test.ts +98 -0
  36. package/src/presentation/vim-settings.ts +59 -0
@@ -0,0 +1,348 @@
1
+ /**
2
+ * The observation vocabulary for the reliance rules.
3
+ *
4
+ * Two kinds of fact live here. The declaration side is authored metadata on an
5
+ * item: the files, symbols, regions, and decisions it expects to read, and the
6
+ * documents it will write. The footprint side is what the batch actually did,
7
+ * recorded by the extension: paths written with their touched spans and a
8
+ * content fingerprint, paths read, symbols deleted, and commands run.
9
+ *
10
+ * The resolver turns a declaration and the footprint against the disk into a
11
+ * small typed state. It reads through an injected `FileProbe`, so the product
12
+ * passes a real filesystem and the tests pass a map. No rule decides anything
13
+ * here; `rules.ts` owns the policy rows.
14
+ *
15
+ * The path grammar and the two text projections are kept from the pre-revert
16
+ * rule branch, because the corpus parity test scores the renamed rows against
17
+ * the old measured behavior.
18
+ */
19
+ import type { DeclaredProduct, DeclaredReference } from "../core/domain.ts";
20
+
21
+ export type { DeclaredProduct, DeclaredReference };
22
+
23
+ // --------------------------------------------------------------------------
24
+ // The legacy path grammar and text facts
25
+ // --------------------------------------------------------------------------
26
+
27
+ /**
28
+ * File extensions worth naming, longest first. The order matters: `.jsonc` must
29
+ * win over `.json`, and `.json` must not match the start of `.jsonx`.
30
+ */
31
+ const EXTENSIONS = [
32
+ "tsbuildinfo",
33
+ "jsonc",
34
+ "json",
35
+ "tsx",
36
+ "jsx",
37
+ "mdx",
38
+ "mts",
39
+ "cts",
40
+ "toml",
41
+ "yaml",
42
+ "html",
43
+ "mjs",
44
+ "cjs",
45
+ "css",
46
+ "sql",
47
+ "yml",
48
+ "txt",
49
+ "md",
50
+ "ts",
51
+ "js",
52
+ "sh",
53
+ "rs",
54
+ "py",
55
+ "go",
56
+ ] as const;
57
+
58
+ /** Any file-like token, with an extension the work is likely to touch. */
59
+ export const PATH_TOKEN = new RegExp(
60
+ `(?:\\.{0,2}\\/)?(?:[\\w.@-]+\\/)*[\\w.@-]+\\.(?:${EXTENSIONS.join("|")})(?!\\w)`,
61
+ "g",
62
+ );
63
+
64
+ /** A QRSPI plan, design, or outline document. */
65
+ export const QRSPI_PLAN_DOCUMENT = /\.qrspi\/(?:plans|designs|outlines)\//;
66
+
67
+ /** Every file path named in a string, distinct and in match order. */
68
+ export const documentPathsOf = (text: string): readonly string[] => [
69
+ ...new Set(text.match(PATH_TOKEN) ?? []),
70
+ ];
71
+
72
+ /** The fields the text projections read from a recorded work entry. */
73
+ export interface RecordText {
74
+ readonly kind?: string;
75
+ readonly text: string;
76
+ readonly name?: string;
77
+ readonly args?: {
78
+ readonly path?: string;
79
+ readonly command?: string;
80
+ };
81
+ }
82
+
83
+ /** The tool names that produce a file. */
84
+ export const PRODUCING_TOOLS: ReadonlySet<string> = new Set(["write", "edit"]);
85
+
86
+ /** One entry's text plus its bounded path and command. */
87
+ const entryTexts = (entry: RecordText): readonly string[] => [
88
+ entry.text,
89
+ ...(entry.args?.path === undefined ? [] : [entry.args.path]),
90
+ ...(entry.args?.command === undefined ? [] : [entry.args.command]),
91
+ ];
92
+
93
+ /** The batch plus the record, joined. A document named here was named at some point. */
94
+ export const knownTextOf = (batch: readonly string[], record: readonly RecordText[]): string =>
95
+ [...batch, ...record.flatMap(entryTexts)].join("\n");
96
+
97
+ /**
98
+ * The `write` and `edit` call paths, joined. A path named here was produced.
99
+ * Only the call's `path` argument counts: the call's full JSON can mention
100
+ * other files, and a tool result repeats a path without being the call.
101
+ */
102
+ export const producedTextOf = (record: readonly RecordText[]): string =>
103
+ record
104
+ .filter((entry) => entry.kind === "tool-call" && PRODUCING_TOOLS.has(entry.name ?? ""))
105
+ .flatMap((entry) => (entry.args?.path === undefined ? [] : [entry.args.path]))
106
+ .join("\n");
107
+
108
+ // --------------------------------------------------------------------------
109
+ // Footprint
110
+ // --------------------------------------------------------------------------
111
+
112
+ /** One path the batch wrote or edited. */
113
+ export interface WrittenPath {
114
+ readonly path: string;
115
+ /** New-side line ranges the batch touched, e.g. `12-14,30`. */
116
+ readonly spans: readonly string[];
117
+ /** Fingerprint of the written region after the write. */
118
+ readonly fingerprint?: string;
119
+ }
120
+
121
+ /** A path or symbol the batch removed. */
122
+ export interface DeletedReference {
123
+ readonly path: string;
124
+ readonly symbol?: string;
125
+ }
126
+
127
+ /** What the batch did between two settle decisions. */
128
+ export interface BatchFootprint {
129
+ readonly wrote: readonly WrittenPath[];
130
+ readonly read: readonly string[];
131
+ readonly deleted: readonly DeletedReference[];
132
+ readonly commands: readonly string[];
133
+ }
134
+
135
+ /** An empty footprint, for a decision with no recorded work. */
136
+ export const emptyFootprint = (): BatchFootprint => ({
137
+ wrote: [],
138
+ read: [],
139
+ deleted: [],
140
+ commands: [],
141
+ });
142
+
143
+ /**
144
+ * What the product can see at settle: the batch item texts plus the footprint,
145
+ * with no transcript prose. One projection, shared by the eval report and the
146
+ * live settle decision, so the measured facts and the live facts cannot drift.
147
+ *
148
+ * `knownText` joins the batch texts, every read path, every written path, every
149
+ * deleted path, and every command. `producedText` joins the written paths only:
150
+ * a command never marks a path produced.
151
+ */
152
+ export const productionFacts = (
153
+ batchTexts: readonly string[],
154
+ footprint: BatchFootprint,
155
+ candidateText: string,
156
+ ): {
157
+ readonly knownText: string;
158
+ readonly producedText: string;
159
+ readonly candidatePaths: readonly string[];
160
+ readonly planDocument: boolean;
161
+ } => {
162
+ const knownText = [
163
+ ...batchTexts,
164
+ ...footprint.read,
165
+ ...footprint.wrote.map((entry) => entry.path),
166
+ ...footprint.deleted.map((entry) => entry.path),
167
+ ...footprint.commands,
168
+ ].join("\n");
169
+ return {
170
+ knownText,
171
+ producedText: footprint.wrote.map((entry) => entry.path).join("\n"),
172
+ candidatePaths: documentPathsOf(candidateText),
173
+ planDocument: QRSPI_PLAN_DOCUMENT.test(knownText),
174
+ };
175
+ };
176
+
177
+ /**
178
+ * The disk probe the resolver reads through. The product resolves a relative
179
+ * path against the workspace root; the tests pass a map. Everything a probe
180
+ * returns is already bounded by the caller.
181
+ */
182
+ export interface FileProbe {
183
+ /** The file's text, or undefined when the path does not exist. */
184
+ readonly read: (path: string) => string | undefined;
185
+ /** Every path that contains the symbol, workspace wide. */
186
+ readonly findSymbol: (symbol: string) => readonly string[];
187
+ }
188
+
189
+ // --------------------------------------------------------------------------
190
+ // Resolution
191
+ // --------------------------------------------------------------------------
192
+
193
+ /** How a declared reference resolves against the disk now. */
194
+ export type ReferenceState = "readable" | "lost" | "drifted" | "unresolved";
195
+
196
+ /** How a declared product resolves against the disk now. */
197
+ export type ProductState = "exists" | "missing";
198
+
199
+ export interface ResolvedReference {
200
+ readonly reference: DeclaredReference;
201
+ readonly state: ReferenceState;
202
+ }
203
+
204
+ export interface ResolvedProduct {
205
+ readonly product: DeclaredProduct;
206
+ readonly state: ProductState;
207
+ }
208
+
209
+ /**
210
+ * A small, deterministic fingerprint of a text region: FNV-1a over UTF-16
211
+ * units. The recorder and the resolver must use this one function, or a drift
212
+ * check would compare two different scales.
213
+ */
214
+ export const fingerprintOf = (text: string): string => {
215
+ let hash = 0x811c9dc5;
216
+ for (let index = 0; index < text.length; index += 1) {
217
+ hash ^= text.charCodeAt(index);
218
+ hash = Math.imul(hash, 0x01000193) >>> 0;
219
+ }
220
+ return hash.toString(16).padStart(8, "0");
221
+ };
222
+
223
+ /** One parsed `start-end` or `start` line range, 1-based and inclusive. */
224
+ interface LineRange {
225
+ readonly start: number;
226
+ readonly end: number;
227
+ }
228
+
229
+ /** Parse `12-14,30`; undefined when any part is malformed or inverted. */
230
+ const parseSpan = (span: string): readonly LineRange[] | undefined => {
231
+ const ranges: LineRange[] = [];
232
+ for (const part of span.split(",")) {
233
+ const match = /^(\d+)(?:-(\d+))?$/.exec(part.trim());
234
+ if (match === null) return undefined;
235
+ const start = Number(match[1]);
236
+ const end = match[2] === undefined ? start : Number(match[2]);
237
+ if (start < 1 || end < start) return undefined;
238
+ ranges.push({ start, end });
239
+ }
240
+ return ranges.length === 0 ? undefined : ranges;
241
+ };
242
+
243
+ /** The text the declared span covers, or undefined when the file is too short. */
244
+ export const spanTextOf = (text: string, span: string): string | undefined => {
245
+ const ranges = parseSpan(span);
246
+ if (ranges === undefined) return undefined;
247
+ const lines = text.split("\n");
248
+ const parts: string[] = [];
249
+ for (const range of ranges) {
250
+ if (range.end > lines.length) return undefined;
251
+ parts.push(lines.slice(range.start - 1, range.end).join("\n"));
252
+ }
253
+ return parts.join("\n");
254
+ };
255
+
256
+ /** True when the symbol appears as a token, not as part of a longer word. */
257
+ export const containsSymbol = (text: string, symbol: string): boolean => {
258
+ if (symbol.length === 0) return false;
259
+ const escaped = symbol.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
260
+ return new RegExp(`(^|[^\\w$])${escaped}([^\\w$]|$)`).test(text);
261
+ };
262
+
263
+ const deletedPath = (footprint: BatchFootprint, path: string): boolean =>
264
+ footprint.deleted.some((entry) => entry.path === path && entry.symbol === undefined);
265
+
266
+ const deletedSymbol = (footprint: BatchFootprint, symbol: string): boolean =>
267
+ footprint.deleted.some((entry) => entry.symbol === symbol);
268
+
269
+ /**
270
+ * The span state: drifted when the region is gone or the batch wrote that exact
271
+ * span and its content no longer matches the recorded fingerprint; readable
272
+ * otherwise. A drift we cannot check is not called a drift.
273
+ */
274
+ const spanState = (
275
+ text: string,
276
+ path: string,
277
+ span: string,
278
+ footprint: BatchFootprint,
279
+ ): ReferenceState => {
280
+ const current = spanTextOf(text, span);
281
+ if (current === undefined) return "drifted";
282
+ const written = footprint.wrote.find(
283
+ (entry) => entry.path === path && entry.spans.includes(span) && entry.fingerprint !== undefined,
284
+ );
285
+ if (written?.fingerprint !== undefined && written.fingerprint !== fingerprintOf(current)) {
286
+ return "drifted";
287
+ }
288
+ return "readable";
289
+ };
290
+
291
+ /** Resolve one declared reference against the footprint and the disk. */
292
+ export const resolveReference = (
293
+ reference: DeclaredReference,
294
+ footprint: BatchFootprint,
295
+ probe: FileProbe,
296
+ ): ResolvedReference => {
297
+ if (reference.kind === "decision") {
298
+ // A decision is a semantic claim. No deterministic check exists, so it
299
+ // stays unresolved and the rule layer routes it to the classifier.
300
+ return { reference, state: "unresolved" };
301
+ }
302
+ const text = probe.read(reference.path);
303
+ if (text === undefined) {
304
+ return { reference, state: deletedPath(footprint, reference.path) ? "lost" : "unresolved" };
305
+ }
306
+ if (reference.symbol !== undefined) {
307
+ if (containsSymbol(text, reference.symbol)) {
308
+ return {
309
+ reference,
310
+ state:
311
+ reference.span === undefined
312
+ ? "readable"
313
+ : spanState(text, reference.path, reference.span, footprint),
314
+ };
315
+ }
316
+ // The named file no longer carries it. The declared scope is named file
317
+ // first, then the workspace, so one moved symbol is still readable. Two or
318
+ // more matches do not name one file, so the reference abstains.
319
+ const matches = probe.findSymbol(reference.symbol);
320
+ if (matches.length === 1) return { reference, state: "readable" };
321
+ if (matches.length > 1) return { reference, state: "unresolved" };
322
+ return { reference, state: deletedSymbol(footprint, reference.symbol) ? "lost" : "unresolved" };
323
+ }
324
+ if (reference.span !== undefined) {
325
+ return { reference, state: spanState(text, reference.path, reference.span, footprint) };
326
+ }
327
+ return { reference, state: "readable" };
328
+ };
329
+
330
+ /** Resolve every declared reference, in declaration order. */
331
+ export const resolveReferences = (
332
+ references: readonly DeclaredReference[],
333
+ footprint: BatchFootprint,
334
+ probe: FileProbe,
335
+ ): readonly ResolvedReference[] =>
336
+ references.map((reference) => resolveReference(reference, footprint, probe));
337
+
338
+ /** Resolve one declared product: it exists on disk or it does not. */
339
+ export const resolveProduct = (product: DeclaredProduct, probe: FileProbe): ResolvedProduct => ({
340
+ product,
341
+ state: probe.read(product.path) === undefined ? "missing" : "exists",
342
+ });
343
+
344
+ /** Resolve every declared product, in declaration order. */
345
+ export const resolveProducts = (
346
+ products: readonly DeclaredProduct[],
347
+ probe: FileProbe,
348
+ ): readonly ResolvedProduct[] => products.map((product) => resolveProduct(product, probe));
@@ -0,0 +1,253 @@
1
+ import { assert, it } from "@effect/vitest";
2
+ import type { CompactionCandidate } from "./candidate.ts";
3
+ import {
4
+ containsSymbol,
5
+ emptyFootprint,
6
+ productionFacts,
7
+ type BatchFootprint,
8
+ type FileProbe,
9
+ type ResolvedProduct,
10
+ type ResolvedReference,
11
+ } from "./observations.ts";
12
+ import {
13
+ acceptedRoute,
14
+ buildRuleInput,
15
+ resolveCandidates,
16
+ route,
17
+ type RuleInput,
18
+ type RuleVerdict,
19
+ } from "./rules.ts";
20
+
21
+ /** The four facts the legacy rows read. */
22
+ interface LegacyFacts {
23
+ readonly writes: boolean;
24
+ readonly reads: boolean;
25
+ readonly dependent: boolean;
26
+ readonly planDocument: boolean;
27
+ }
28
+
29
+ /** Build an input that realizes the facts, with no declarations. */
30
+ const legacyInput = (facts: LegacyFacts): RuleInput => ({
31
+ references: [],
32
+ products: [],
33
+ candidatePaths: facts.writes ? ["src/missing.ts"] : facts.reads ? ["src/produced.ts"] : [],
34
+ knownText: facts.reads ? "src/produced.ts" : "",
35
+ producedText: facts.reads ? "src/produced.ts" : "",
36
+ dependent: facts.dependent,
37
+ planDocument: facts.planDocument,
38
+ });
39
+
40
+ /**
41
+ * The frozen pre-revert rule table, kept here with its old rule ids so the
42
+ * parity test proves the rename changed no verdict. Do not edit this to match
43
+ * the new table; that would make the test circular.
44
+ */
45
+ const frozenRoute = (facts: LegacyFacts): { verdict: RuleVerdict; rule: string } => {
46
+ if (facts.writes) return { verdict: "keep", rule: "writes-missing-document" };
47
+ if (facts.reads && !facts.dependent) {
48
+ return { verdict: "compact", rule: "reads-produced-document" };
49
+ }
50
+ if (!facts.reads && !facts.dependent && !facts.planDocument) {
51
+ return { verdict: "compact", rule: "nothing-in-play" };
52
+ }
53
+ if (facts.dependent && facts.planDocument) {
54
+ return { verdict: "abstain", rule: "document-and-dependency" };
55
+ }
56
+ if (facts.dependent) return { verdict: "abstain", rule: "dependent-successor" };
57
+ return { verdict: "abstain", rule: "plan-document" };
58
+ };
59
+
60
+ /** The two renamed compact rows; every other id is unchanged. */
61
+ const RENAMED: Readonly<Record<string, string>> = {
62
+ "writes-missing-document": "names-unknown-path",
63
+ "reads-produced-document": "names-batch-output",
64
+ "nothing-in-play": "no-visible-link",
65
+ };
66
+
67
+ const reference = (
68
+ state: ResolvedReference["state"],
69
+ value: ResolvedReference["reference"] = { kind: "path", path: "src/a.ts" },
70
+ ): ResolvedReference => ({ reference: value, state });
71
+
72
+ const product = (state: ResolvedProduct["state"], path = "src/out.ts"): ResolvedProduct => ({
73
+ product: { path },
74
+ state,
75
+ });
76
+
77
+ const declaredInput = (
78
+ references: readonly ResolvedReference[],
79
+ products: readonly ResolvedProduct[] = [],
80
+ ): RuleInput => ({
81
+ references,
82
+ products,
83
+ candidatePaths: [],
84
+ knownText: "",
85
+ producedText: "",
86
+ dependent: false,
87
+ planDocument: false,
88
+ });
89
+
90
+ it("the legacy rows reproduce the frozen table on all sixteen fact combinations", () => {
91
+ for (const writes of [false, true]) {
92
+ for (const reads of [false, true]) {
93
+ for (const dependent of [false, true]) {
94
+ for (const planDocument of [false, true]) {
95
+ const facts = { writes, reads, dependent, planDocument };
96
+ const frozen = frozenRoute(facts);
97
+ const renamed = { verdict: frozen.verdict, rule: RENAMED[frozen.rule] ?? frozen.rule };
98
+ assert.deepStrictEqual(route(legacyInput(facts)), renamed, JSON.stringify(facts));
99
+ }
100
+ }
101
+ }
102
+ }
103
+ });
104
+
105
+ it("a lost reference keeps the context before any other row", () => {
106
+ const decision = route(
107
+ declaredInput([reference("lost"), reference("readable", { kind: "path", path: "src/b.ts" })]),
108
+ );
109
+ assert.deepStrictEqual(decision, { verdict: "keep", rule: "reference-is-lost" });
110
+ });
111
+
112
+ it("an unwritten plan product keeps the context; a non-plan product does not", () => {
113
+ assert.deepStrictEqual(route(declaredInput([], [product("missing", ".qrspi/plans/router.md")])), {
114
+ verdict: "keep",
115
+ rule: "product-is-unwritten-plan",
116
+ });
117
+ assert.deepStrictEqual(route(declaredInput([], [product("missing", "src/out.ts")])), {
118
+ verdict: "abstain",
119
+ rule: "declarations-unresolved",
120
+ });
121
+ });
122
+
123
+ it("a declared decision routes to the classifier", () => {
124
+ const decision = route(
125
+ declaredInput([reference("unresolved", { kind: "decision", topic: "storage shape" })]),
126
+ );
127
+ assert.deepStrictEqual(decision, { verdict: "abstain", rule: "decision-is-declared" });
128
+ });
129
+
130
+ it("a drifted reference routes to the classifier", () => {
131
+ assert.deepStrictEqual(route(declaredInput([reference("drifted")])), {
132
+ verdict: "abstain",
133
+ rule: "reference-has-drifted",
134
+ });
135
+ });
136
+
137
+ it("all readable references compact", () => {
138
+ assert.deepStrictEqual(route(declaredInput([reference("readable"), reference("readable")])), {
139
+ verdict: "compact",
140
+ rule: "reference-is-readable",
141
+ });
142
+ });
143
+
144
+ it("an unresolved reference routes to the classifier, never to a legacy compact row", () => {
145
+ const decision = route(declaredInput([reference("unresolved")]));
146
+ assert.deepStrictEqual(decision, { verdict: "abstain", rule: "reference-is-unresolved" });
147
+ });
148
+
149
+ it("a declaration without a verdict does not fall through to no-visible-link", () => {
150
+ const decision = route(declaredInput([], [product("exists", "docs/plan.md")]));
151
+ assert.deepStrictEqual(decision, { verdict: "abstain", rule: "declarations-unresolved" });
152
+ });
153
+
154
+ it("acceptedRoute lets only the gated rows decide", () => {
155
+ assert.deepStrictEqual(
156
+ acceptedRoute(
157
+ legacyInput({
158
+ writes: false,
159
+ reads: false,
160
+ dependent: false,
161
+ planDocument: false,
162
+ }),
163
+ ),
164
+ { verdict: "compact", rule: "no-visible-link" },
165
+ );
166
+ assert.deepStrictEqual(
167
+ acceptedRoute(
168
+ legacyInput({
169
+ writes: true,
170
+ reads: false,
171
+ dependent: false,
172
+ planDocument: false,
173
+ }),
174
+ ),
175
+ { verdict: "abstain", rule: "names-unknown-path" },
176
+ );
177
+ assert.deepStrictEqual(acceptedRoute(declaredInput([reference("readable")])), {
178
+ verdict: "abstain",
179
+ rule: "reference-is-readable",
180
+ });
181
+ });
182
+
183
+ // ---------------------------------------------------------------------------
184
+ // resolveCandidates and buildRuleInput
185
+ // ---------------------------------------------------------------------------
186
+
187
+ const candidateOf = (overrides: Partial<CompactionCandidate>): CompactionCandidate => ({
188
+ class: "ready-queue",
189
+ listName: "Work",
190
+ id: 2,
191
+ text: "second step",
192
+ completed: [],
193
+ ...overrides,
194
+ });
195
+
196
+ const probeOf = (files: Readonly<Record<string, string>>): FileProbe => ({
197
+ read: (path) => files[path],
198
+ findSymbol: (symbol) =>
199
+ Object.entries(files)
200
+ .filter(([, text]) => containsSymbol(text, symbol))
201
+ .map(([path]) => path),
202
+ });
203
+
204
+ const footprintWith = (overrides: Partial<BatchFootprint>): BatchFootprint => ({
205
+ ...emptyFootprint(),
206
+ ...overrides,
207
+ });
208
+
209
+ it("resolveCandidates abstains every reference without a footprint", () => {
210
+ const candidate = candidateOf({ refs: [{ kind: "path", path: "src/a.ts" }] });
211
+ const probe = probeOf({ "src/a.ts": "export {};" });
212
+ assert.deepStrictEqual(resolveCandidates([candidate], emptyFootprint(), probe)[0]?.references, [
213
+ { reference: { kind: "path", path: "src/a.ts" }, state: "unresolved" },
214
+ ]);
215
+ assert.deepStrictEqual(
216
+ resolveCandidates([candidate], footprintWith({ read: ["src/a.ts"] }), probe)[0]?.references,
217
+ [{ reference: { kind: "path", path: "src/a.ts" }, state: "readable" }],
218
+ );
219
+ });
220
+
221
+ it("resolveCandidates checks a product against disk without a footprint", () => {
222
+ const candidate = candidateOf({ produces: [{ path: "docs/plan.md" }] });
223
+ assert.deepStrictEqual(
224
+ resolveCandidates([candidate], emptyFootprint(), probeOf({ "docs/plan.md": "# Plan" }))[0]
225
+ ?.products,
226
+ [{ product: { path: "docs/plan.md" }, state: "exists" }],
227
+ );
228
+ });
229
+
230
+ it("a candidate that names a batch output routes to names-batch-output", () => {
231
+ const candidate = candidateOf({ text: "wire src/router.ts" });
232
+ const footprint = footprintWith({ wrote: [{ path: "src/router.ts", spans: [] }] });
233
+ const facts = resolveCandidates([candidate], footprint, probeOf({}))[0]!;
234
+ const production = productionFacts([], footprint, candidate.text);
235
+ assert.deepStrictEqual(acceptedRoute(buildRuleInput(facts, production)), {
236
+ verdict: "compact",
237
+ rule: "names-batch-output",
238
+ });
239
+ });
240
+
241
+ it("a restored readable reference routes to reference-is-unresolved", () => {
242
+ const candidate = candidateOf({ refs: [{ kind: "path", path: "src/a.ts" }] });
243
+ const facts = resolveCandidates(
244
+ [candidate],
245
+ emptyFootprint(),
246
+ probeOf({ "src/a.ts": "export {};" }),
247
+ )[0]!;
248
+ const production = productionFacts([], emptyFootprint(), candidate.text);
249
+ assert.deepStrictEqual(acceptedRoute(buildRuleInput(facts, production)), {
250
+ verdict: "abstain",
251
+ rule: "reference-is-unresolved",
252
+ });
253
+ });