Repository navigation
🤖 feat: DRY gate (make dry-check, Quality / DRY) on unclebob/dryer #7147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
181865b
feat: DRY gate (make dry-check, Quality / DRY) on unclebob/dryer
ThomasK33 97c4132
fix: remove the base tree when writing it fails
ThomasK33 f5c0e9e
fix: DRY gate finds a function's base version by git hunks, not by name
ThomasK33 ee0389e
fix: DRY gate treats moved functions as old, and a split function as new
ThomasK33 1f0ffc3
fix: DRY gate scores every function of a file renamed into production
ThomasK33 55c0ad1
fix: DRY gate pairs moved duplicates with distinct bases; diff parser…
ThomasK33 62700a5
fix: DRY gate assigns removed forms to moved forms one to one
ThomasK33 8692e50
fix: diff parser counts context lines per side; gates pin --inter-hun…
ThomasK33 28ba71f
refactor: the DRY gate no longer tracks moves across files
ThomasK33 f6507c1
fix: a function rewritten line for line keeps its DRY base form
ThomasK33 59d666b
Merge remote-tracking branch 'origin/main' into gauntlet/dry-gate
ThomasK33 22475b6
Merge remote-tracking branch 'origin/main' into gauntlet/dry-gate
ThomasK33 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| --- | ||
| name: gauntlet | ||
| description: Read and fix DRY gate findings (make dry-check, Quality / DRY). Use when a dry-check report names a new near-duplicate, or before you copy a function and edit the copy. | ||
| --- | ||
|
|
||
| # Gauntlet gates | ||
|
|
||
| These gates sit next to the CRAP gate (`crap-gate` skill). They only warn: | ||
| the reader of the finding decides. They score only the functions that your | ||
| change touched, since the merge-base with `origin/main`. | ||
|
|
||
| ## DRY gate | ||
|
|
||
| `make dry-check` (or `bun scripts/dry/check.ts --base <ref>`) compares each | ||
| changed function with every production function in `src/`. The scorer is | ||
| [dryer](https://github.com/unclebob/dryer): it drops names, literals and | ||
| locals, then scores the Jaccard similarity of the structure. Exit 0 no | ||
| findings, 1 findings, 2 tool error. CI shows the same report in the | ||
| `Quality / DRY` job summary. | ||
|
|
||
| ``` | ||
| warning new-duplicate score 1.00 src/x.ts:41-46 feeBucket | ||
| matches src/x.ts:34-39 centsBucket | ||
| ``` | ||
|
|
||
| - A finding needs a score of 0.82 or more (the dryer default), at least 4 | ||
| lines and 20 syntax nodes. | ||
| - Only pairs that the change made are findings: a new function that copies | ||
| another one, or an edit that makes two functions match. An edit to one side | ||
| of a pair that already matched on main is quiet. The gate does not track | ||
| moves across files: a function moved into another file reports its old | ||
| pairs again. Say so in the PR; it is not a new duplicate. | ||
| - Files under test, story and fixture paths do not count (the CRAP gate's | ||
| production-file rule). | ||
|
|
||
| ### Repair | ||
|
|
||
| 1. If both functions must change together (one rule, one format, one | ||
| protocol), extract the shared shape into one function and call it from both | ||
| places. | ||
| 2. If they only look alike and must change apart (two different domains that | ||
| happen to share a shape today), keep both. Say why in the PR. | ||
|
|
||
| Never merge two functions only to clear a DRY finding. A wrong abstraction | ||
| costs more than a copy. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // The files and lines changed since a base commit, shared by the quality | ||
| // gates (crap/check.ts, dry/check.ts, mutate/check.ts) so they agree on what | ||
| // "changed" means: the working tree against `base`, with renames paired. | ||
| import { execFileSync } from "node:child_process"; | ||
| import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; | ||
| import { tmpdir } from "node:os"; | ||
| import { dirname, join } from "node:path"; | ||
| import { diffHeaders, parseChangedLines, type ChangedLines, type Hunk } from "./policy"; | ||
|
|
||
| export function git(root: string, args: string[]): string { | ||
| return execFileSync("git", args, { cwd: root, encoding: "utf8", maxBuffer: 256 * 1024 * 1024 }); | ||
| } | ||
|
|
||
| // Cheap pre-filter so binaries and lockfiles never reach a scorer. The | ||
| // adapters apply the full production-file rule (crapper_report.py). | ||
| export const SCORED = /^src\/.*\.[cm]?[jt]sx?$/; | ||
|
|
||
| /** | ||
| * One changed file since `base`. `basePath` is null for an added file, | ||
| * `headPath` null for a deleted one. `-M` pairs a renamed file's two paths so | ||
| * its functions keep their identity (the policy keys on the path). | ||
| */ | ||
| export interface ChangedFile { | ||
| basePath: string | null; | ||
| headPath: string | null; | ||
| } | ||
|
|
||
| export function parseNameStatus(output: string): ChangedFile[] { | ||
| const fields = output.split("\0"); | ||
| const files: ChangedFile[] = []; | ||
| let i = 0; | ||
| while (i < fields.length && fields[i] !== "") { | ||
| const status = fields[i]; | ||
| // `-M` without `-C` reports renames (R<similarity>) but never copies. | ||
| if (status.startsWith("R")) { | ||
| files.push({ basePath: fields[i + 1], headPath: fields[i + 2] }); | ||
| i += 3; | ||
| } else { | ||
| const path = fields[i + 1]; | ||
| files.push({ | ||
| basePath: status === "A" ? null : path, | ||
| headPath: status === "D" ? null : path, | ||
| }); | ||
| i += 2; | ||
| } | ||
| } | ||
| return files.filter((file) => [file.basePath, file.headPath].some((p) => p && SCORED.test(p))); | ||
| } | ||
|
|
||
| /** | ||
| * Hunks per head path, both sides, so a gate can map a head line back to its | ||
| * base line (dry/dryer_report.py). Paths follow parseChangedLines in policy.ts. | ||
| */ | ||
| export function parseHunks(diff: string): Map<string, Hunk[]> { | ||
| const hunks = new Map<string, Hunk[]>(); | ||
| let current: Hunk[] | null = null; | ||
| for (const header of diffHeaders(diff)) { | ||
| if (header.kind === "file") { | ||
| current = header.path === null ? null : []; | ||
| if (current && header.path !== null) hunks.set(header.path, current); | ||
| } else if (current) { | ||
| current.push(header.hunk); | ||
| } | ||
| } | ||
| return hunks; | ||
| } | ||
|
|
||
| export interface Changes { | ||
| root: string; | ||
| /** The resolved base commit SHA. */ | ||
| base: string; | ||
| files: ChangedFile[]; | ||
| changed: ChangedLines; | ||
| hunks: Map<string, Hunk[]>; | ||
| } | ||
|
|
||
| export function changesSince(cwd: string, baseRef: string): Changes { | ||
| const root = git(cwd, ["rev-parse", "--show-toplevel"]).trim(); | ||
| const base = git(root, [ | ||
| "rev-parse", | ||
| "--verify", | ||
| "--end-of-options", | ||
| `${baseRef}^{commit}`, | ||
| ]).trim(); | ||
| // The working tree is the head: unstaged edits count, untracked files do not. | ||
| // Pin the output format against user config (diff.mnemonicPrefix, | ||
| // diff.noprefix, core.quotePath) so paths match the scorer's. | ||
| const diffArgs = [ | ||
| "-c", | ||
| "core.quotePath=false", | ||
| "diff", | ||
| "-M", | ||
| "--default-prefix", | ||
| "--no-color", | ||
| "--no-ext-diff", | ||
| // diff.interHunkContext would merge nearby hunks with context lines. | ||
| "--inter-hunk-context=0", | ||
| base, | ||
| ]; | ||
| const files = parseNameStatus(git(root, [...diffArgs, "--name-status", "-z"])).filter( | ||
| (file) => file.headPath === null || existsSync(join(root, file.headPath)) | ||
| ); | ||
| const pathspec = files.flatMap((file) => | ||
| [file.basePath, file.headPath].filter((p) => p !== null) | ||
| ); | ||
| const diff = | ||
| pathspec.length === 0 ? "" : git(root, [...diffArgs, "-U0", "--", ...new Set(pathspec)]); | ||
|
ThomasK33 marked this conversation as resolved.
|
||
| return { root, base, files, changed: parseChangedLines(diff), hunks: parseHunks(diff) }; | ||
| } | ||
|
|
||
| /** | ||
| * Writes the base content of every changed file that existed at `base` into a | ||
| * new temp tree, at its base path. The caller removes the tree. | ||
| */ | ||
| export function writeBaseTree( | ||
| root: string, | ||
| base: string, | ||
| files: readonly ChangedFile[] | ||
| ): { tree: string; paths: string[] } { | ||
| const tree = mkdtempSync(join(tmpdir(), "gate-base-")); | ||
| const paths: string[] = []; | ||
| try { | ||
| for (const { basePath } of files) { | ||
| if (basePath === null) continue; | ||
| mkdirSync(dirname(join(tree, basePath)), { recursive: true }); | ||
| const blob = execFileSync("git", ["show", `${base}:${basePath}`], { | ||
| cwd: root, | ||
| maxBuffer: 256 * 1024 * 1024, | ||
| }); | ||
| writeFileSync(join(tree, basePath), blob); | ||
| paths.push(basePath); | ||
| } | ||
| } catch (error) { | ||
| // The caller owns the tree only once this returns. | ||
| rmSync(tree, { recursive: true, force: true }); | ||
| throw error; | ||
| } | ||
| return { tree, paths }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.