Skip to content

Commit bb85595

Browse files
donjor-agent[bot]donjor
authored andcommitted
fix(fork): folds from a split head cite the old trunk shas (#1600)
## Problem `fork:fold apply --head <split head>` cites members by their split-head shas. A repair split replays every commit after the first repair, so those shas sit on no trunk; `prove` passed them as earlier folds and `fork:scan --replay-of` then dropped the owner's exemption. ## Change - **Apply:** new `--old <pre-split tip>` - **Cite:** a replayed member cites its `<old>` twin - **Match:** subject, oldest first (`fork:scan`) - **Guard:** split head without `--old` refuses - **No twin:** a fold refuses that member - **Prove:** refuses an unnamed `Squashes:` sha The `fork-fold` skill documents `--old`. Closes #1599 ## Refresh onto the V2 trunk The old head `b9ecfaa5639` sat on the pre-V2 trunk, so against today's `hyprws` the PR showed 100 commits and 100 files. The branch is now `hyprws` `4899869dfff` plus this one commit. - **Patch:** byte-identical to `b9ecfaa5639` - **Size:** 4 files, +172/-19 - **Base:** the 4 files match the old parent - **Still needed:** new tests fail on trunk ## Scope and approval Bug tracked as #1599; both close conditions covered. Jordon approved the refresh. ## Verification - **Fixture:** a replayed member cites its old sha - **Unit:** member guard finds an unnamed sha - **Unit:** accepts an earlier fold's listed member - **Existing split test:** now passes `--old` ```text head 794da44, base hyprws 4899869 vp test run scripts/fork-fold.test.ts → exit 0 (38 passed) without the fix: exit 1 (5 failed, 33 passed) scripts: vp run typecheck → exit 0 vp run fork:ci → exit 0 ``` <details> <summary>Fresh review at 794da44: nothing blocking</summary> All four predate the refresh; left as follow-ups to keep scope. ### Refused by prove - **Shared subject:** remainder takes a twin's sha - **No split pieces:** apply guard skipped ### Refused by apply - **Folded remainder:** skill does not say so yet - **Landed `fixup!`:** asks for `--old` </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 180745f commit bb85595

4 files changed

Lines changed: 172 additions & 19 deletions

File tree

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ Split each one on a detached copy of the head (`git worktree add --detach <dir>
8181
3. One inseparable remainder: a single `upstream-fixes` commit naming the upstream change it adapts to, without `Fork-Repair`.
8282
4. `git rebase --continue`. The split head is tree-equal to the old tip.
8383

84-
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.
84+
Run the fold with `--head <split head> --old <old>`: 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.
85+
A replayed member cites its `<old>` counterpart by subject, since its split sha sits on no trunk.
8586
`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 by several, unless it carries `Fork-Repair` or every owner cites it under `Squashes:`.
87+
`prove` also refuses a dropped PR link, a `Squashes:` sha `<old>` never names, 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:`.
8788

8889
## Stops
8990

@@ -92,6 +93,7 @@ Run the fold with `--head <split head>`: each fixup joins its owner's plan line
9293
| `does not apply at its plan position` | Reorder or split that line |
9394
| `plan does not cover the stack` | Fix the listed shas |
9495
| `a fixup! member carries no repair line` | Copy `list`'s fixup line into that piece |
96+
| `--head is a split head` | Pass the pre-split tip as `--old` |
9597
| `publish` refuses | Read the reason; nothing pushed |
9698

9799
## Cadence

‎scripts/fork-fold.test.ts‎

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ it("finds a memberless commit, stray paths, and orphaned or doubly-owned old com
423423
const none = commit("ffff0003", { subject: "feat: none" });
424424
assert.deepStrictEqual(memberFindings(stackOf(a, b, orphan), stackOf(stray, twice, none)), [
425425
"ffff0001 feat: aaaa1111: touches paths no member touched: residue.ts",
426+
"ffff0002 feat: twice: cites 9999aaaa, which the old range never names",
426427
"ffff0003 feat: none: names no commit in the old range",
427428
"aaaa1111 feat: aaaa1111: belongs to several new commits: ffff0001, ffff0002",
428429
"cccc3333 feat: cccc3333: belongs to no new commit",
@@ -464,6 +465,38 @@ it("lets a mover split across owners only when every owner cites it", () => {
464465
);
465466
});
466467

468+
it("accepts an earlier fold's member the old range lists, and refuses a sha it never names", () => {
469+
const earlier = commit("aaaa1111", {
470+
message: "feat: aaaa1111\n\nSquashes:\n\n- 0123456789 feat: an older member\n",
471+
files: ["a.ts"],
472+
});
473+
const kept = commit("ffff0001", { subject: earlier.subject, message: earlier.message });
474+
assert.deepStrictEqual(memberFindings(stackOf(earlier), stackOf(kept)), []);
475+
// A split head's replayed sha: on no published trunk, so no old commit names it.
476+
const split = commit("ffff0002", {
477+
subject: earlier.subject,
478+
message: "feat: aaaa1111\n\nSquashes:\n\n- 4290da0ce0 feat: aaaa1111\n",
479+
});
480+
assert.deepStrictEqual(memberFindings(stackOf(earlier), stackOf(split)), [
481+
"ffff0002 feat: aaaa1111: cites 4290da0ce0, which the old range never names",
482+
]);
483+
});
484+
485+
it("cites a replayed member by its old sha, and refuses one the old range lacks", () => {
486+
const lead = commit("aaaa1111");
487+
const replayed = commit("bbbb2222", { cite: "cccc3333" });
488+
assert.include(foldMessage({ members: [lead, replayed] }), "- cccc3333 feat: bbbb2222");
489+
assert.throws(
490+
() => foldMessage({ members: [lead, commit("dddd4444", { cite: null })] }),
491+
/--old holds no commit with this subject/,
492+
);
493+
// A lone member keeps its message verbatim and cites nothing.
494+
assert.strictEqual(
495+
foldMessage({ members: [commit("dddd4444", { cite: null })] }),
496+
"feat: dddd4444",
497+
);
498+
});
499+
467500
it("reads fork PR refs from a subject marker, a fork pull URL, and earlier Squashes lines only", () => {
468501
const message = [
469502
"feat(web): first (#1355)",
@@ -1064,7 +1097,16 @@ it("folds a split repair into its owners, provenance and residue included", () =
10641097
NodePath.join(root, "plan.tsv"),
10651098
[`${alpha}\t${alphaFixup}`, `${beta}\t${betaFixup}`, residue].join("\n"),
10661099
);
1067-
const applied = cli(root, ["apply", "plan.tsv", "--base", base, "--head", splitHead]);
1100+
const applied = cli(root, [
1101+
"apply",
1102+
"plan.tsv",
1103+
"--base",
1104+
base,
1105+
"--head",
1106+
splitHead,
1107+
"--old",
1108+
head,
1109+
]);
10681110
assert.strictEqual(applied.status, 0, applied.stderr);
10691111
const tip = applied.stdout.trim();
10701112
assert.strictEqual(
@@ -1097,6 +1139,54 @@ it("folds a split repair into its owners, provenance and residue included", () =
10971139
assert.notInclude(residueBody, "Fork-Repair");
10981140
}));
10991141

1142+
it("folds from a split head cite the old trunk shas of every replayed member", () =>
1143+
withSplitStack(({ root, base, alpha, beta, repair }) => {
1144+
// A commit after the repair: the split replays it onto a sha no trunk carries.
1145+
write(root, "alpha2.txt", "alpha follow-up\n");
1146+
const followUp = commitAll(
1147+
root,
1148+
"feat: alpha follow-up",
1149+
"Fork-Domain: fold-a\nFork-Tier: qol",
1150+
);
1151+
const old = followUp;
1152+
const listed = cli(root, ["list", "--base", base]);
1153+
const repairLine = listed.stdout.split("fixup line: ")[1]?.split("\n")[0] ?? "";
1154+
const { alphaFixup, betaFixup, residue } = splitRepair(root, repair, repairLine);
1155+
git(root, ["cherry-pick", followUp]);
1156+
const replayed = git(root, ["rev-parse", "HEAD"]);
1157+
assert.notStrictEqual(replayed, followUp);
1158+
assert.strictEqual(
1159+
git(root, ["rev-parse", `${replayed}^{tree}`]),
1160+
git(root, ["rev-parse", `${old}^{tree}`]),
1161+
);
1162+
1163+
NodeFS.writeFileSync(
1164+
NodePath.join(root, "plan.tsv"),
1165+
[`${alpha}\t${alphaFixup}\t${replayed}`, `${beta}\t${betaFixup}`, residue].join("\n"),
1166+
);
1167+
const apply = (extra: ReadonlyArray<string>) =>
1168+
cli(root, ["apply", "plan.tsv", "--base", base, "--head", replayed, ...extra]);
1169+
const refused = apply([]);
1170+
assert.strictEqual(refused.status, 1);
1171+
assert.include(refused.stderr, "pass --old <pre-split tip>");
1172+
1173+
const applied = apply(["--old", old]);
1174+
assert.strictEqual(applied.status, 0, applied.stderr);
1175+
const tip = applied.stdout.trim();
1176+
assert.strictEqual(
1177+
git(root, ["rev-parse", `${tip}^{tree}`]),
1178+
git(root, ["rev-parse", `${old}^{tree}`]),
1179+
);
1180+
const alphaBody = git(root, ["log", "-1", "--format=%B", `${tip}~2`]);
1181+
assert.include(alphaBody, `- ${followUp.slice(0, 7)} feat: alpha follow-up`);
1182+
assert.notInclude(alphaBody, replayed.slice(0, 7));
1183+
assert.deepStrictEqual(squashedMembers(alphaBody), [
1184+
alpha.slice(0, 7),
1185+
repair.slice(0, 7),
1186+
followUp.slice(0, 7),
1187+
]);
1188+
}));
1189+
11001190
it("proves tree equality, the delta check, and the replay scan, failing on any", () =>
11011191
withStack(({ root, base, head }) => {
11021192
const calls: Array<string> = [];

‎scripts/fork-fold.ts‎

Lines changed: 72 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Commands:
4545
Options:
4646
--base <ref> Upstream base (default: upstream/main)
4747
--head <ref> Stack head for list and apply (default: HEAD)
48+
--old <ref> apply: the pre-split tip whose shas a split head's folds cite;
49+
required when --head holds split repair pieces
4850
--json list: print JSON instead of the table
4951
-h, --help Show help
5052
@@ -57,7 +59,9 @@ and a line's members never mix Fork-Domain values.
5759
5860
A Fork-Repair commit is split before the run: each 'fixup! <owner subject>'
5961
piece joins its owner's line, and list prints the repair line to copy into
60-
every piece. .agents/skills/fork-fold/SKILL.md owns the procedure.
62+
every piece. The split replays every later commit, so apply takes the pre-split
63+
tip as --old and each fold cites a replayed member by its <old> sha.
64+
.agents/skills/fork-fold/SKILL.md owns the procedure.
6165
6266
apply replays with merge-tree until a block refuses there; from that block on
6367
the remaining plan replays as one git rebase --autosquash in a throwaway
@@ -338,12 +342,47 @@ const FIXUP_SUBJECT = "fixup! ";
338342
*/
339343
const firstSquashesLine = (message: string): string | undefined => squashesLines(message)[0];
340344

345+
/** The shas a message's `Squashes:` member lines cite. */
346+
const squashesShas = (message: string): ReadonlyArray<string> =>
347+
squashesLines(message).map((line) => line.slice(2).split(/\s/)[0] ?? "");
348+
349+
/** A split piece of a repair: a `fixup!` carrying the repair's line under `Squashes:`. */
350+
const isSplitPiece = (commit: FoldCommit): boolean =>
351+
commit.subject.startsWith(FIXUP_SUBJECT) && firstSquashesLine(commit.message) !== undefined;
352+
353+
/**
354+
* A repair split replays every commit after its first repair, so a split head
355+
* carries shas no published trunk does. Each replayed commit cites its `<old>`
356+
* counterpart instead: the same subject, oldest first, as fork:scan's replay
357+
* match reads it. A commit `<old>` carries keeps its sha; a split piece already
358+
* renders its repair line; a commit with no counterpart cites null, which a
359+
* fold refuses to render.
360+
*/
361+
export const citeOld = (stack: FoldStack, old: FoldStack): FoldStack => {
362+
const onHead = new Set(stack.commits.map(({ sha }) => sha));
363+
const onOld = new Set(old.commits.map(({ sha }) => sha));
364+
const counterparts = new Map<string, Array<FoldCommit>>();
365+
for (const commit of old.commits) {
366+
if (onHead.has(commit.sha)) continue;
367+
counterparts.set(commit.subject, [...(counterparts.get(commit.subject) ?? []), commit]);
368+
}
369+
return {
370+
...stack,
371+
commits: stack.commits.map((commit) =>
372+
onOld.has(commit.sha) || isSplitPiece(commit)
373+
? commit
374+
: { ...commit, cite: counterparts.get(commit.subject)?.shift()?.short ?? null },
375+
),
376+
};
377+
};
378+
341379
/**
342380
* One member line: the sha first (squashedMembers reads it), then the subject,
343381
* then every fork pull request the member cites, rendered full so the link
344382
* always stays on the fork. A split piece of a repair renders as the repair it
345383
* carries — never as the throwaway piece. A squash-landed fixup is a landing of
346384
* its own, so it renders like any member and its pull request link survives.
385+
* A commit a split head replayed renders under its `<old>` sha.
347386
*/
348387
const memberLine = (member: FoldCommit): string => {
349388
if (member.subject.startsWith(FIXUP_SUBJECT)) {
@@ -355,11 +394,16 @@ const memberLine = (member: FoldCommit): string => {
355394
);
356395
}
357396
}
397+
if (member.cite === null) {
398+
throw new FoldError(
399+
`${member.short} ${member.subject}: --old holds no commit with this subject, so a fold cannot cite it; keep it a lone member without a plan link`,
400+
);
401+
}
358402
const links = forkPullRequests(member.message)
359403
.filter((ref) => !member.subject.includes(ref))
360404
.map((ref) => ` (${ref})`)
361405
.join("");
362-
return `- ${member.short} ${member.subject}${links}`;
406+
return `- ${member.cite ?? member.short} ${member.subject}${links}`;
363407
};
364408

365409
/**
@@ -535,25 +579,32 @@ export const replay = (
535579

536580
/**
537581
* Per-commit member guard: each new commit's members are the old commits its
538-
* `Squashes:` lines name (shas outside the old range cite earlier folds) plus
539-
* the old commit sharing its subject. Finds a new commit with no member, a new
540-
* commit touching a path no member touched, and an old commit owned by no new
541-
* commit or by several. A Fork-Repair commit may split across owners, and so
542-
* may any old commit every owner cites under `Squashes:`: a mover split back
543-
* into the owners whose blocks it moved.
582+
* `Squashes:` lines name plus the old commit sharing its subject. Finds a
583+
* `Squashes:` sha the old range never names — neither an old commit nor an
584+
* earlier fold's member an old commit lists — such as a split head's replayed
585+
* sha, a new commit with no member, a new commit touching a path no member
586+
* touched, and an old commit owned by no new commit or by several. A
587+
* Fork-Repair commit may split across owners, and so may any old commit every
588+
* owner cites under `Squashes:`: a mover split back into the owners whose
589+
* blocks it moved.
544590
*/
545591
export const memberFindings = (old: FoldStack, next: FoldStack): ReadonlyArray<string> => {
546592
const findings: string[] = [];
547593
const owners = new Map<string, { short: string; cites: boolean }[]>(
548594
old.commits.map((commit) => [commit.sha, []]),
549595
);
596+
const named = old.commits.flatMap((commit) => [commit.sha, ...squashesShas(commit.message)]);
550597
for (const commit of next.commits) {
551-
const cited = squashesLines(commit.message).map((line) => line.slice(2).split(/\s/)[0] ?? "");
598+
const cited = squashesShas(commit.message);
599+
const label = `${commit.short} ${commit.subject}`;
600+
for (const sha of cited) {
601+
if (named.some((name) => name.startsWith(sha) || sha.startsWith(name))) continue;
602+
findings.push(`${label}: cites ${sha}, which the old range never names`);
603+
}
552604
const members = old.commits.filter(
553605
(member) =>
554606
member.subject === commit.subject || cited.some((sha) => member.sha.startsWith(sha)),
555607
);
556-
const label = `${commit.short} ${commit.subject}`;
557608
if (members.length === 0) {
558609
findings.push(`${label}: names no commit in the old range`);
559610
continue;
@@ -757,12 +808,17 @@ export const run = (
757808
return 0;
758809
}
759810
if (subcommand === "apply") {
760-
const args = parseArgs(rest, { values: ["--base", "--head"], positionals: 1 });
761-
const stack = readStack(
762-
git,
763-
args.values.get("--base") ?? "upstream/main",
764-
args.values.get("--head") ?? "HEAD",
765-
);
811+
const args = parseArgs(rest, { values: ["--base", "--head", "--old"], positionals: 1 });
812+
const base = args.values.get("--base") ?? "upstream/main";
813+
const head = readStack(git, base, args.values.get("--head") ?? "HEAD");
814+
const oldRef = args.values.get("--old");
815+
const piece = head.commits.find(isSplitPiece);
816+
if (oldRef === undefined && piece !== undefined) {
817+
throw new FoldError(
818+
`${piece.short} ${piece.subject}: --head is a split head; pass --old <pre-split tip> so its folds cite <old> shas`,
819+
);
820+
}
821+
const stack = oldRef === undefined ? head : citeOld(head, readStack(git, base, oldRef));
766822
const planPath = args.positionals[0] ?? "";
767823
const planText = readPlan(NodePath.resolve(cwd, planPath));
768824
const plan = parsePlan(planText);

‎scripts/lib/fork-fold-rebase.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export interface FoldCommit extends ParsedForkCommit {
2222
readonly files: ReadonlyArray<string>;
2323
/** Full commit message (`%B`), so fold messages can carry each member's PR links. */
2424
readonly message: string;
25+
/**
26+
* The `<old>` short sha a fold names a commit that a split head replayed,
27+
* or null when `<old>` holds no counterpart; absent, a fold names the commit itself.
28+
*/
29+
readonly cite?: string | null;
2530
}
2631

2732
export interface FoldStack {

0 commit comments

Comments
 (0)