am-i-done 0.3.1 → 0.4.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/CLAUDE.md CHANGED
@@ -23,6 +23,8 @@ Nothing is left uncovered or excluded to reach it; if a line cannot be covered,
23
23
  - `src/fingerprint.ts`: hashing and git, shared by the caches. `src/cache.ts`: the local skip. `src/spec-cache.ts`: the per-spec cache the `test` stage uses. `src/tmp.ts`, `src/tmp-preload.ts`: the temp-file helper.
24
24
  - `src/cli/`: the commands; `bin/` the two-line entry points.
25
25
  - `templates/`: what `am-i-done init` copies.
26
+ - `samples/`: rules and stages to copy, not run on this tree; `samples.spec.ts` holds them to their
27
+ fixtures and loads the stages. Samples import only from `am-i-done`. `docs/ideas.md`: the rest.
26
28
 
27
29
  ## Conventions
28
30
 
@@ -33,4 +35,5 @@ Nothing is left uncovered or excluded to reach it; if a line cannot be covered,
33
35
  - A parser upgrade that changes the tree fails `ast.spec.ts`; fix the normalizer in `ast.ts` with the
34
36
  measured difference, never by loosening the comparison.
35
37
  - Temp files come from `src/tmp.ts`, including in specs. Never `mkdtemp` or `tmpdir()`.
36
- - Comments say why, not what. No issue numbers or names from other projects.
38
+ - Comments say why, not what. No issue numbers or names from other projects, in samples too: they
39
+ use `@example/*`, `example.com`, "someone".
package/README.md CHANGED
@@ -203,7 +203,10 @@ fails loudly instead of passing as if the tree were clean. A rule with nothing t
203
203
  optionally `// @path <path>` as comment lines of their own in its first lines. The path must be one
204
204
  the rule looks at. They run on every run, so a rule that no longer catches what it was written for
205
205
  fails. The `.fixture` suffix keeps
206
- the type checker, linter and test runner away from code that is wrong on purpose.
206
+ the type checker, linter and test runner away from code that is wrong on purpose. A rule that looks
207
+ across files gets a scenario: a directory `<rule-id>__<scenario>/` holding a small project, each
208
+ file at its path with `.fixture` appended and its own `@expect`. Each of a rule's `anchors` must
209
+ find its marker in the rule's fixtures.
207
210
 
208
211
  **Suppression.** An exception says why, and where its removal is tracked:
209
212
 
@@ -225,6 +228,13 @@ default), and a suppression that no longer suppresses anything are each a violat
225
228
  Turn them all off with `[rules] recommended = false`, or one with
226
229
  `skip = [{ id = "no-as-any", reason = "… (#123)" }]`.
227
230
 
231
+ ## Samples
232
+
233
+ [`samples/`](samples/README.md) holds rules and stages to copy into a project: tests that pass
234
+ without testing anything, workspace import boundaries, leaked home paths, a coverage ratchet, dead
235
+ modules, specs that registered no tests, and more. [docs/ideas.md](docs/ideas.md) lists more that
236
+ are not written yet.
237
+
228
238
  ## Temp files: `am-i-done/tmp`
229
239
 
230
240
  Temp directories made by hand leak: a spec that throws before its cleanup, or a run that is killed,
@@ -331,7 +341,7 @@ every commit. It runs typecheck, lint, the architecture rules and the tests.
331
341
 
332
342
  ## Status
333
343
 
334
- 0.2: the runner, stage autoloading, the rule engine with syntax trees and the import graph, the local
344
+ 0.4: the runner, stage autoloading, the rule engine with syntax trees and the import graph, the local
335
345
  skip, the spec cache, the temp-file helper. Planned: a host-wide lease so parallel worktrees do not
336
346
  overload a machine, a CI skip for trees that passed before push, and more built-in rules.
337
347
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "am-i-done",
3
- "version": "0.3.1",
3
+ "version": "0.4.0",
4
4
  "description": "A definition of done for agentic projects: one command that says whether the work is finished.",
5
5
  "license": "MIT",
6
6
  "author": "theshadow27 (https://github.com/theshadow27)",
@@ -1,9 +1,9 @@
1
- import { existsSync, readdirSync, readFileSync } from "node:fs";
1
+ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
2
2
  import { join } from "node:path";
3
3
  import type { Parser } from "./ast.ts";
4
- import { evaluate, wantsTests, workspace } from "./evaluate.ts";
4
+ import { type Evaluation, evaluate, wantsTests, workspace } from "./evaluate.ts";
5
5
  import { fileMeta, matchesAny } from "./files.ts";
6
- import type { Rule } from "./rule.ts";
6
+ import type { FileMeta, Rule } from "./rule.ts";
7
7
 
8
8
  /**
9
9
  * Rule fixtures: one file per scenario, `<rulesDir>/fixtures/<rule-id>__<scenario>.<ext>.fixture`.
@@ -13,6 +13,13 @@ import type { Rule } from "./rule.ts";
13
13
  * A directive is a comment line of its own (`// @expect 2`, ` * @path src/a.ts`), blanked before the
14
14
  * rule runs so it cannot match it; one written after code on the same line is refused, since
15
15
  * blanking it would hide that code.
16
+ *
17
+ * A rule that looks across files gets a scenario instead: a directory `<rule-id>__<scenario>/`
18
+ * holding a small project, each file at its path with the suffix added
19
+ * (`packages/app/package.json.fixture`). The rule sees every file of the scenario, and each file it
20
+ * looks at must report its own `@expect`. A file it does not look at is context and expects nothing.
21
+ * The workspace is in memory, so the import graph resolves relative imports only: a rule that
22
+ * follows package names reads them from the scenario's package.json files.
16
23
  */
17
24
 
18
25
  export const FIXTURE_DIR = "fixtures";
@@ -30,6 +37,8 @@ export interface Fixture {
30
37
  content: string;
31
38
  /** 1-based lines in the directive area that mention `@expect` or `@path` but are not directives. */
32
39
  stray: number[];
40
+ /** The scenario directory this file belongs to; its files are checked together. */
41
+ scenario?: string;
33
42
  }
34
43
 
35
44
  export function parseFixture(source: string, name: string, text: string): Fixture {
@@ -52,15 +61,34 @@ export function parseFixture(source: string, name: string, text: string): Fixtur
52
61
  return { ruleId: stem.slice(0, split), source, path, expect: expected, content: lines.join("\n"), stray };
53
62
  }
54
63
 
55
- /** Every fixture in `dirs`, by the rule it names. Files without `__` or the suffix are not fixtures. */
64
+ /**
65
+ * Every fixture in `dirs`, by the rule it names: single files, and the files of each scenario
66
+ * directory. Names without `__`, and files without the suffix, are not fixtures.
67
+ */
56
68
  export function loadFixtures(dirs: readonly string[]): Fixture[] {
57
69
  return dirs.flatMap((dir) => {
58
70
  const fixtures = join(dir, FIXTURE_DIR);
59
71
  if (!existsSync(fixtures)) return [];
60
72
  return readdirSync(fixtures)
61
- .filter((name) => name.endsWith(FIXTURE_SUFFIX) && name.includes("__"))
73
+ .filter((name) => name.includes("__"))
62
74
  .sort()
63
- .map((name) => parseFixture(join(fixtures, name), name, readFileSync(join(fixtures, name), "utf8")));
75
+ .flatMap((name) => {
76
+ const source = join(fixtures, name);
77
+ if (statSync(source).isDirectory()) return loadScenario(source, name);
78
+ return name.endsWith(FIXTURE_SUFFIX) ? [parseFixture(source, name, readFileSync(source, "utf8"))] : [];
79
+ });
80
+ });
81
+ }
82
+
83
+ /** A scenario directory's files, each at its path inside the directory. */
84
+ function loadScenario(dir: string, name: string): Fixture[] {
85
+ const ruleId = name.slice(0, name.indexOf("__"));
86
+ const scenario = dir;
87
+ const paths = [...new Bun.Glob(`**/*${FIXTURE_SUFFIX}`).scanSync({ cwd: dir, dot: true })].sort();
88
+ if (paths.length === 0) throw new Error(`${dir}: a scenario with no *${FIXTURE_SUFFIX} files`);
89
+ return paths.map((path) => {
90
+ const source = join(dir, path);
91
+ return { ...parseFixture(source, `${ruleId}__${path}`, readFileSync(source, "utf8")), scenario };
64
92
  });
65
93
  }
66
94
 
@@ -73,9 +101,11 @@ export interface FixtureOptions {
73
101
 
74
102
  /**
75
103
  * Problems with the rules' fixtures: a rule with none, or with none that fires (a rule that never
76
- * reports anything passes every tree, so one fixture must show it catching something); a fixture
77
- * for a rule that does not exist (or was skipped, which `known` says); a fixture at a path the rule
78
- * would never look at; a misplaced directive; and every count that disagrees with its `@expect`.
104
+ * reports anything passes every tree, so one fixture must show it catching something); an anchor
105
+ * that finds its marker in none of the rule's fixtures (they are the tree it was written for); a
106
+ * fixture for a rule that does not exist (or was skipped, which `known` says); a fixture at a path
107
+ * the rule would never look at; a misplaced directive; and every count that disagrees with its
108
+ * `@expect`.
79
109
  */
80
110
  export function checkFixtures(
81
111
  rules: readonly Rule[],
@@ -91,19 +121,58 @@ export function checkFixtures(
91
121
  ? []
92
122
  : [`rule "${rule.id}" has no fixture that fires: give one an \`@expect\` of 1 or more`];
93
123
  });
94
- const each = fixtures.flatMap((fixture) => {
95
- const rule = byId.get(fixture.ruleId);
96
- if (rule !== undefined) return checkFixture(rule, fixture, options);
97
- return known.has(fixture.ruleId) ? [] : [`${fixture.source}: no rule "${fixture.ruleId}"`];
124
+ const anchors = rules.flatMap((rule) => {
125
+ const own = fixtures.filter((f) => f.ruleId === rule.id);
126
+ const tree = new Map(own.map((f) => [f.path, fileMeta("/fixture", f.path, f.content)]));
127
+ return (rule.anchors ?? []).flatMap((anchor, i) =>
128
+ own.length === 0 || anchor(tree)
129
+ ? []
130
+ : [`rule "${rule.id}": anchor ${i + 1} finds its marker in none of the rule's fixtures`],
131
+ );
132
+ });
133
+ const groups = Map.groupBy(fixtures, (f) => f.scenario ?? f.source);
134
+ const each = [...groups.entries()].flatMap(([name, group]) => {
135
+ const first = group[0] as Fixture;
136
+ const rule = byId.get(first.ruleId);
137
+ if (rule === undefined) return known.has(first.ruleId) ? [] : [`${name}: no rule "${first.ruleId}"`];
138
+ return first.scenario === undefined
139
+ ? checkFixture(rule, first, options)
140
+ : checkScenario(rule, name, group, options);
98
141
  });
99
- return [...coverage, ...each];
142
+ return [...coverage, ...anchors, ...each];
143
+ }
144
+
145
+ const strayLines = (fixture: Fixture): string[] =>
146
+ fixture.stray.map((line) => `${fixture.source}:${line}: put \`@expect\` and \`@path\` on comment lines of their own`);
147
+
148
+ /** A scenario's files against its rule, in one workspace. */
149
+ function checkScenario(rule: Rule, name: string, group: readonly Fixture[], options: FixtureOptions): string[] {
150
+ const globs = rule.files ?? options.include;
151
+ // A real run loads only `[rules] include` and the rules' own `files`, so a scenario file outside
152
+ // both would let the rule see something it never sees in a project.
153
+ const loadable = [...options.include, ...(rule.files ?? [])];
154
+ const all = group.map((fixture) => ({ fixture, file: fileMeta("/fixture", fixture.path, fixture.content) }));
155
+ const metas = all.filter(({ file }) => matchesAny(loadable, file.relativePath));
156
+ const ws = workspace("/fixture", new Map(metas.map(({ file }) => [file.relativePath, file])), options);
157
+ const seen = (file: FileMeta) => wantsTests(rule, file) && matchesAny(globs, file.relativePath);
158
+ const problems = group.flatMap(strayLines);
159
+ for (const { fixture } of all.filter((m) => !metas.includes(m)))
160
+ problems.push(
161
+ `${fixture.source}: a run never loads ${fixture.path} for rule "${rule.id}": it matches neither [rules] include nor the rule's files`,
162
+ );
163
+ if (!metas.some(({ file }) => seen(file)))
164
+ problems.push(`${name}: rule "${rule.id}" looks at none of its files (files: ${globs.join(", ")})`);
165
+ for (const { fixture, file } of metas) {
166
+ if (seen(file)) problems.push(...compare(rule, fixture, evaluate(rule, file, ws)));
167
+ else if (fixture.expect > 0)
168
+ problems.push(`${fixture.source}: rule "${rule.id}" never looks at ${fixture.path}, so it can expect nothing`);
169
+ }
170
+ return problems;
100
171
  }
101
172
 
102
173
  /** One fixture against its rule. */
103
174
  function checkFixture(rule: Rule, fixture: Fixture, options: FixtureOptions): string[] {
104
- const stray = fixture.stray.map(
105
- (line) => `${fixture.source}:${line}: put \`@expect\` and \`@path\` on comment lines of their own`,
106
- );
175
+ const stray = strayLines(fixture);
107
176
  const file = fileMeta("/fixture", fixture.path, fixture.content);
108
177
  const globs = rule.files ?? options.include;
109
178
  if (!wantsTests(rule, file) || !matchesAny(globs, fixture.path)) {
@@ -111,9 +180,13 @@ function checkFixture(rule: Rule, fixture: Fixture, options: FixtureOptions): st
111
180
  return [...stray, `${fixture.source}: rule "${rule.id}" never looks at ${fixture.path} (${why})`];
112
181
  }
113
182
  const ws = workspace("/fixture", new Map([[file.relativePath, file]]), options);
114
- const { violations, error } = evaluate(rule, file, ws);
115
- if (error !== undefined) return [...stray, `${fixture.source}: rule "${rule.id}" threw: ${error}`];
116
- if (violations.length === fixture.expect) return stray;
183
+ return [...stray, ...compare(rule, fixture, evaluate(rule, file, ws))];
184
+ }
185
+
186
+ /** What one evaluation got wrong against its fixture's `@expect`. */
187
+ function compare(rule: Rule, fixture: Fixture, { violations, error }: Evaluation): string[] {
188
+ if (error !== undefined) return [`${fixture.source}: rule "${rule.id}" threw: ${error}`];
189
+ if (violations.length === fixture.expect) return [];
117
190
  const found = violations.map((v) => `\n ${v.line}:${v.column} ${v.rule.id} ${v.snippet}`).join("");
118
- return [...stray, `${fixture.source}: expected ${fixture.expect} violation(s), found ${violations.length}${found}`];
191
+ return [`${fixture.source}: expected ${fixture.expect} violation(s), found ${violations.length}${found}`];
119
192
  }