Skip to content

Commit 5f769e3

Browse files
donjor-agent[bot]donjor
authored andcommitted
fix(fork): scan skips restored upstream test openers; folds may split a cited mover (#1506)
## Problem Three tooling gaps block paying down the grandfathered scan findings. - **Restore reads as a fork block:** `dc56af095d6` restored upstream cases, such as `it("only lists server-known sessions that are running or starting (plus current)", …)`; the scan reports each restore as "gains N fork test block(s)" - **Mover split refused:** `prove` refuses a non-repair commit owned by several new commits, so Fold A cannot split a mover back into the owners whose blocks it moved - **Stale doc:** `fork-development.md` lists two harness deferrals the scan no longer implements ## Change | Part | Change | | --- | --- | | B0a | block rule skips an added opener the target file already has, unless the head holds more copies than the target | | B0b | `memberFindings` accepts an old commit with several owners when every owner cites it under `Squashes:` | | B0c | drop the `DesktopWindow.test.ts` / `server.test.ts` deferral text | | Docs | fork-fold skill states the relaxed guard; the sibling table gains the restore row | The count check keeps a fork block that reuses an upstream title counted. Under B0b, a split mover's remainder commit must cite the mover too; an owner that does not cite it still refuses. ## Scope and approval Part of #1493 (plan batch B0, host decisions D1 and D2). ## Verification - `vp test run scripts/fork-scan-gates.test.ts scripts/fork-scan.test.ts` → exit 0, 44 passed - `vp test run scripts/fork-fold.test.ts` → exit 0, 33 passed - Both new tests fail with the old rule - `vp run typecheck` (scripts) → exit 0 - `vp run fork:ci` → exit 0 Inventory scan from the issue against merge base `b33eda13998b`: ```diff - historical: 172 finding(s) already on origin/hyprws + historical: 167 finding(s) already on origin/hyprws ``` <details> <summary>Cleared findings (all <code>dc56af095d6</code>)</summary> | File | Blocks | | --- | --- | | `apps/desktop/src/app/DesktopClerk.test.ts` | 1 | | `apps/mobile/src/features/terminal/terminalMenu.test.ts` | 1 | | `apps/server/src/pullRequest/PullRequestService.test.ts` | 1 | | `apps/web/src/components/pullRequest/pullRequestProjectFilter.logic.test.ts` | 7 | | `apps/web/src/localApi.test.ts` | 2 | One count shrinks: `175d4928166` `ClaudeAdapter.test.ts` 2 → 1 block; its bare `it.effect(` opener matches upstream's, and a later mover took the block out. Fold A clears that finding. </details> Claude Opus 5.5 via Claude Code. ## Fork trailers Fork-Domain: fork-meta Fork-Tier: core Fork-Upstreamable: no Co-authored-by: donjor <[email protected]>
1 parent 2299d18 commit 5f769e3

7 files changed

Lines changed: 110 additions & 31 deletions

File tree

‎.agents/skills/fork-fold/SKILL.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Split each one on a detached copy of the head (`git worktree add --detach <dir>
8383

8484
Run the fold with `--head <split head>`: each fixup joins its owner's plan line and squashes there, so the owner's `Squashes:` lists its own old sha, the repair sha, and the repair's references.
8585
`prove` and `publish` take the pre-split tip as `<old>`; a tree-equal fold needs no retest.
86-
`prove` also refuses a dropped PR link, a commit naming no `<old>` member or touching a path none of its members touched, and an old commit owned by no new commit or, unless it carries `Fork-Repair`, by several.
86+
`prove` also refuses a dropped PR link, a commit naming no `<old>` member or touching a path none of its members touched, and an old commit owned by no new commit or by several, unless it carries `Fork-Repair` or every owner cites it under `Squashes:`.
8787

8888
## Stops
8989

‎docs/fork/internals/fork-development.md‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,7 @@ Otherwise the replay conflicts at one seam on every upstream append.
186186
| Additive gate | A dropped line in the replay is a finding |
187187
| Ownership | The selected upstream target tree |
188188
| Recognized | `it`, `test`, `describe`, `effectIt`, Effect variants |
189-
190-
Two harness deferrals, exact paths, never widened:
191-
192-
```text
193-
apps/desktop/src/window/DesktopWindow.test.ts
194-
apps/server/src/server.test.ts
195-
```
196-
197-
Both build the harness in-module; a sibling would re-register every test.
189+
| Restore | An opener the target already has is no fork block |
198190

199191
### Diverging from an upstream expectation
200192

‎scripts/fork-fold.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,26 @@ it("lets a Fork-Repair commit split across owners but not vanish", () => {
410410
]);
411411
});
412412

413+
it("lets a mover split across owners only when every owner cites it", () => {
414+
const mover = commit("eeee0000", { subject: "test: move blocks", files: ["a.ts", "b.ts"] });
415+
const cites = (short: string, subject: string, file: string) =>
416+
commit(short, {
417+
subject,
418+
message: `${subject}\n\nSquashes:\n\n- eeee0000 test: move blocks\n`,
419+
files: [file],
420+
});
421+
const remainder = cites("bbbb2222", "test: move blocks", "b.ts");
422+
assert.deepStrictEqual(
423+
memberFindings(stackOf(mover), stackOf(cites("aaaa1111", "feat: a", "a.ts"), remainder)),
424+
[],
425+
);
426+
const silent = commit("cccc3333", { subject: "test: move blocks", files: ["b.ts"] });
427+
assert.deepStrictEqual(
428+
memberFindings(stackOf(mover), stackOf(cites("aaaa1111", "feat: a", "a.ts"), silent)),
429+
["eeee0000 test: move blocks: belongs to several new commits: aaaa1111, cccc3333"],
430+
);
431+
});
432+
413433
it("reads fork PR refs from a subject marker, a fork pull URL, and earlier Squashes lines only", () => {
414434
const message = [
415435
"feat(web): first (#1355)",

‎scripts/fork-fold.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,15 @@ export const replay = (
531531
* `Squashes:` lines name (shas outside the old range cite earlier folds) plus
532532
* the old commit sharing its subject. Finds a new commit with no member, a new
533533
* commit touching a path no member touched, and an old commit owned by no new
534-
* commit or — unless it is a Fork-Repair split across owners — by several.
534+
* commit or by several. A Fork-Repair commit may split across owners, and so
535+
* may any old commit every owner cites under `Squashes:`: a mover split back
536+
* into the owners whose blocks it moved.
535537
*/
536538
export const memberFindings = (old: FoldStack, next: FoldStack): ReadonlyArray<string> => {
537539
const findings: string[] = [];
538-
const owners = new Map<string, string[]>(old.commits.map((commit) => [commit.sha, []]));
540+
const owners = new Map<string, { short: string; cites: boolean }[]>(
541+
old.commits.map((commit) => [commit.sha, []]),
542+
);
539543
for (const commit of next.commits) {
540544
const cited = squashesLines(commit.message).map((line) => line.slice(2).split(/\s/)[0] ?? "");
541545
const members = old.commits.filter(
@@ -547,7 +551,10 @@ export const memberFindings = (old: FoldStack, next: FoldStack): ReadonlyArray<s
547551
findings.push(`${label}: names no commit in the old range`);
548552
continue;
549553
}
550-
for (const member of members) owners.get(member.sha)?.push(commit.short);
554+
for (const member of members) {
555+
const cites = cited.some((sha) => member.sha.startsWith(sha));
556+
owners.get(member.sha)?.push({ short: commit.short, cites });
557+
}
551558
const touched = new Set(members.flatMap((member) => member.files));
552559
const stray = commit.files.filter((path) => !touched.has(path));
553560
if (stray.length > 0) {
@@ -558,8 +565,13 @@ export const memberFindings = (old: FoldStack, next: FoldStack): ReadonlyArray<s
558565
const owned = owners.get(commit.sha) ?? [];
559566
const label = `${commit.short} ${commit.subject}`;
560567
if (owned.length === 0) findings.push(`${label}: belongs to no new commit`);
561-
else if (owned.length > 1 && commit.repair === undefined) {
562-
findings.push(`${label}: belongs to several new commits: ${owned.join(", ")}`);
568+
else if (
569+
owned.length > 1 &&
570+
commit.repair === undefined &&
571+
!owned.every((owner) => owner.cites)
572+
) {
573+
const shorts = owned.map((owner) => owner.short).join(", ");
574+
findings.push(`${label}: belongs to several new commits: ${shorts}`);
563575
}
564576
}
565577
return findings;

‎scripts/fork-scan-authoring.ts‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export interface ExportDeclaration {
3939

4040
export interface TestBlockHunk {
4141
readonly path: string;
42-
readonly added: number;
42+
// Trimmed, so the block rule can tell a restored upstream opener from a fork one.
43+
readonly addedOpeners: ReadonlyArray<string>;
4344
readonly removed: number;
4445
}
4546

@@ -177,14 +178,18 @@ export const parseCommitPatches = (raw: string): ReadonlyMap<string, CommitPatch
177178
// source side and additions to the target side rather than to one path.
178179
let sourcePath: string | null = null;
179180
let targetPath: string | null = null;
180-
let hunkAddedTestBlocks = 0;
181+
let hunkAddedOpeners: Array<string> = [];
181182
let hunkRemovedTestBlocks = 0;
182183
const flushTestBlockHunk = () => {
183184
const path = targetPath ?? sourcePath;
184-
if (path !== null && (hunkAddedTestBlocks > 0 || hunkRemovedTestBlocks > 0)) {
185-
testBlockHunks.push({ path, added: hunkAddedTestBlocks, removed: hunkRemovedTestBlocks });
185+
if (path !== null && (hunkAddedOpeners.length > 0 || hunkRemovedTestBlocks > 0)) {
186+
testBlockHunks.push({
187+
path,
188+
addedOpeners: hunkAddedOpeners,
189+
removed: hunkRemovedTestBlocks,
190+
});
186191
}
187-
hunkAddedTestBlocks = 0;
192+
hunkAddedOpeners = [];
188193
hunkRemovedTestBlocks = 0;
189194
};
190195
for (const line of lines) {
@@ -226,7 +231,7 @@ export const parseCommitPatches = (raw: string): ReadonlyMap<string, CommitPatch
226231
});
227232
}
228233
if (TEST_BLOCK.test(content)) {
229-
if (added) hunkAddedTestBlocks += 1;
234+
if (added) hunkAddedOpeners.push(content.trim());
230235
else hunkRemovedTestBlocks += 1;
231236
}
232237
}
@@ -257,6 +262,12 @@ const FORK_TEST_FILE = /\.fork\.test\.tsx?$/;
257262
const TITLE_OF =
258263
/^\s*(?:it|test|effectIt)\s*(?:\.[\w$]+)*\s*(?:<[^>]*>)?\s*\(\s*(?:"((?:\\.|[^"\\])*)"|'((?:\\.|[^'\\])*)'|`((?:\\.|[^`\\])*)`)/;
259264

265+
/** The paths whose target-tree text the upstream-test rule measures this patch against. */
266+
export const upstreamTestPaths = (patch: CommitPatch): ReadonlyArray<string> => [
267+
...patch.removedTestLines.keys(),
268+
...patch.testBlockHunks.filter((hunk) => hunk.addedOpeners.length > 0).map((hunk) => hunk.path),
269+
];
270+
260271
/** Trimmed, without blank and comment-only lines: the case-body lines the rule counts. */
261272
const significant = (content: string): boolean => {
262273
const trimmed = content.trim();
@@ -351,9 +362,28 @@ export const collectAuthoringWarnings = (
351362
found.push({ rule, commit: commit.short, domain: commit.domain, detail });
352363
};
353364

365+
const occurrences = (text: string, line: string) =>
366+
significantLines(text).filter((candidate) => candidate === line).length;
367+
// An added opener the target already carries restores an upstream case
368+
// rather than gaining a fork block, unless the head holds more copies of
369+
// that line than the target: a fork block that reuses an upstream title
370+
// still counts.
371+
const restoresUpstreamOpener = (path: string, line: string): boolean => {
372+
if (input.upstreamTestLines.get(path)?.has(line) !== true) return false;
373+
const targetText = input.upstreamTestTexts.get(path);
374+
const headText = input.headTestTexts?.get(path);
375+
return (
376+
targetText === undefined ||
377+
headText === undefined ||
378+
occurrences(headText, line) <= occurrences(targetText, line)
379+
);
380+
};
354381
const appendedTestBlocks = new Map<string, number>();
355382
for (const hunk of patch.testBlockHunks) {
356-
const count = Math.max(0, hunk.added - hunk.removed);
383+
const forkOpeners = hunk.addedOpeners.filter(
384+
(line) => !restoresUpstreamOpener(hunk.path, line),
385+
).length;
386+
const count = Math.min(forkOpeners, Math.max(0, hunk.addedOpeners.length - hunk.removed));
357387
if (count === 0) continue;
358388
appendedTestBlocks.set(hunk.path, (appendedTestBlocks.get(hunk.path) ?? 0) + count);
359389
}
@@ -379,8 +409,6 @@ export const collectAuthoringWarnings = (
379409
const upstreamLines = input.upstreamTestLines.get(path);
380410
const targetText = input.upstreamTestTexts.get(path);
381411
const headText = input.headTestTexts?.get(path);
382-
const occurrences = (text: string, line: string) =>
383-
significantLines(text).filter((candidate) => candidate === line).length;
384412
const upstream =
385413
upstreamLines === undefined
386414
? lines

‎scripts/fork-scan-gates.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,32 @@ it("fails an upstream-test addition in the since range", () => {
358358
assert.isTrue(failures.some((failure) => failure.startsWith("upstream-test:")));
359359
});
360360

361+
it("passes a restored upstream case and still counts a fork block reusing its title", () => {
362+
const path = "apps/web/src/other.test.ts";
363+
const opener = 'it("lists running sessions", () => {';
364+
const raw = ["abc1234", `--- a/${path}`, `+++ b/${path}`, "@@ -0,0 +1 @@", `+${opener}`, ""].join(
365+
"\n",
366+
);
367+
const scan = (headText: string) =>
368+
scanFailures(
369+
buildScanResult(
370+
baseInput({
371+
commits: [{ sha: "abc1234", short: "abc1234", domain: "example" }],
372+
filesBySha: new Map([["abc1234", [path]]]),
373+
patchesBySha: parseCommitPatches(raw),
374+
upstreamFiles: new Set([path]),
375+
upstreamTestFiles: new Set([path]),
376+
upstreamTestLines: new Map([[path, significantTestLines(`${opener}\n});\n`)]]),
377+
upstreamTestTexts: new Map([[path, `${opener}\n});\n`]]),
378+
headTestTexts: new Map([[path, headText]]),
379+
siblingTexts: new Map(),
380+
}),
381+
),
382+
).filter((failure) => failure.startsWith("upstream-test:"));
383+
assert.deepStrictEqual(scan(`${opener}\n});\n`), []);
384+
assert.lengthOf(scan(`${opener}\n});\n${opener}\n});\n`), 1);
385+
});
386+
361387
it("passes a declared-superseded deletion and still fails an undeclared one", () => {
362388
// RSI-Software/t3code-hyprws#1208: the sibling declares the upstream case the fork
363389
// contradicts, so deleting the fork's in-place rewrite from the upstream copy is

‎scripts/fork-scan.ts‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
renderAuthoringWarnings,
1919
significantTestLines,
2020
upstreamSourceLines,
21+
upstreamTestPaths,
2122
type CommitPatch,
2223
type ScanAuthoringWarning,
2324
} from "./fork-scan-authoring.ts";
@@ -890,7 +891,7 @@ const buildGuardInput = (
890891
range.target,
891892
patchesBySha,
892893
upstreamTestFiles,
893-
(patch) => patch.removedTestLines.keys(),
894+
upstreamTestPaths,
894895
() => true,
895896
significantTestLines,
896897
),
@@ -958,10 +959,11 @@ const readUpstreamLines = (
958959

959960
/**
960961
* The target-tree text of every upstream test file a warned commit removes a
961-
* test line from — the same path set `readUpstreamLines` selects for the
962-
* test side, plus the full text. The declared-superseded exemption compares
963-
* structurally by case, so it needs the text, not just the line set
964-
* (RSI-Software/t3code-hyprws#1208).
962+
* test line from or adds a test opener to — the same path set
963+
* `readUpstreamLines` selects for the test side, plus the full text. The
964+
* declared-superseded exemption compares structurally by case, and the
965+
* restored-opener exemption by count, so both need the text, not just the
966+
* line set (RSI-Software/t3code-hyprws#1208).
965967
*/
966968
const readUpstreamTestTexts = (
967969
git: GitReader,
@@ -971,8 +973,7 @@ const readUpstreamTestTexts = (
971973
): ReadonlyMap<string, string> => {
972974
const paths = new Set<string>();
973975
for (const patch of patchesBySha.values())
974-
for (const path of patch.removedTestLines.keys())
975-
if (upstreamTestFiles.has(path)) paths.add(path);
976+
for (const path of upstreamTestPaths(patch)) if (upstreamTestFiles.has(path)) paths.add(path);
976977
const texts = new Map<string, string>();
977978
for (const path of [...paths].toSorted()) {
978979
try {

0 commit comments

Comments
 (0)