Compare commits

...

2 Commits

Author SHA1 Message Date
diegosouzapw
a321a94ee7 fix(scripts): restore a claim with the base's own text in the strip pass
restoreClaims wrote the parsed base value back as an ASCII number. A
translated mirror that writes the count in native digits (Persian
README: "۲۳۳ مهاجرت") got "233" instead, so the line still differed from
the merge base and the PR kept conflicting on that mirror.

Use the base's exact substring for the claim span. Regression test with
a Persian-digit claim fails on the old code and passes now.
2026-11-15 04:35:20 -03:00
diegosouzapw
cb2af4b242 fix(scripts): restore derived files byte for byte in the strip pass
The strip pass read merge-base and head contents through a helper that
trims its output, so every restored file lost its final newline. The
"restored" blob still differed from the merge base and the PR kept
conflicting on it (stryker.conf.json in the first batch).

Read blobs with a dedicated readBlob() that keeps the exact bytes, and
add a regression test that builds the restore commit from a real git
repository and compares blob ids with the merge base.
2026-11-15 03:58:06 -03:00
2 changed files with 97 additions and 3 deletions

View File

@@ -52,6 +52,8 @@ export function classifyFile(file, { claimFiles, instructionFiles }) {
/**
* Each managed value the head changed goes back to the base value of the same claim (same check,
* same position). A check whose claim count differs between base and head is left to a human.
* The replacement is the base's own text for that claim, not the parsed value: a mirror that
* writes "۲۳۳" must get "۲۳۳" back, or the line still differs from the base and keeps conflicting.
*/
export function restoreClaims(base, head, checks) {
const edits = [];
@@ -72,6 +74,7 @@ export function restoreClaims(base, head, checks) {
label: check.label,
from: hv[i],
to: bv[i],
text: b[i].start >= 0 ? (base ?? "").slice(b[i].start, b[i].end) : bv[i],
start: claim.start,
end: claim.end,
});
@@ -84,7 +87,7 @@ export function restoreClaims(base, head, checks) {
const key = `${e.start}:${e.end}`;
if (seen.has(key)) continue;
seen.add(key);
content = content.slice(0, e.start) + e.to + content.slice(e.end);
content = content.slice(0, e.start) + e.text + content.slice(e.end);
}
for (const check of checks) {
if (!edits.some((e) => e.label === check.label)) continue;
@@ -279,6 +282,24 @@ function tryGit(args, opts) {
}
}
/**
* A file's exact bytes at `ref` (null when the path does not exist there). Never trimmed: the
* restore must reproduce the merge-base blob byte for byte — a trimmed read drops the final
* newline, leaves the file "modified" and the PR still conflicting.
*/
export function readBlob(ref, file, { cwd = ROOT } = {}) {
try {
return execFileSync("git", ["show", `${ref}:${file}`], {
cwd,
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
maxBuffer: 256 << 20,
});
} catch {
return null;
}
}
export const RESTORE_MESSAGE = [
"chore: drop derived values the release refresh now owns",
"",
@@ -363,8 +384,8 @@ async function main() {
const changed = git(["diff", "--name-only", mb, ref]).split("\n").filter(Boolean);
const plan = planPrRestore({
changed,
readBase: (f) => tryGit(["show", `${mb}:${f}`]),
readHead: (f) => tryGit(["show", `${ref}:${f}`]),
readBase: (f) => readBlob(mb, f),
readHead: (f) => readBlob(ref, f),
ctx,
});
const entry = {

View File

@@ -30,6 +30,7 @@ const strip = (await import("../../scripts/ad-hoc/strip-derived-from-pr.mjs")) a
ctx: Ctx;
}) => { files: PlannedFile[]; manual: string[] };
loadContext: () => Promise<Ctx>;
readBlob: (ref: string, file: string, opts?: { cwd?: string }) => string | null;
buildRestoreCommit: (args: {
cwd: string;
head: string;
@@ -83,6 +84,28 @@ test("claims: each value the PR changed goes back to the base value; prose edits
assert.deepEqual(r.manual, []);
});
test("claims: the base's own digits come back, not a re-rendered number", () => {
// A translated mirror writes the count in Persian digits; the head rewrote it in ASCII.
const toAscii = (t: string) => t.replace(/[۰-۹]/g, (d) => String("۰۱۲۳۴۵۶۷۸۹".indexOf(d)));
const nativeCheck: Check = {
label: "DB migrations count (README mirrors)",
extract: (content) =>
[...content.matchAll(/([0-9۰-۹]+) مهاجرت/dg)].map((m) => ({
value: Number(toAscii(m[1])),
start: (m as unknown as { indices: [number, number][] }).indices[1][0],
end: (m as unknown as { indices: [number, number][] }).indices[1][1],
})),
};
const base = "<td>۱۴۷ ماژول، ۲۳۳ مهاجرت</td>\n";
const head = "<td>۱۴۷ ماژول، 234 مهاجرت</td>\n";
const r = strip.restoreClaims(base, head, [nativeCheck]);
assert.equal(r.content, base, "byte-identical to the base line");
assert.deepEqual(r.edits, [
{ label: "DB migrations count (README mirrors)", from: "234", to: "233" },
]);
assert.deepEqual(r.manual, []);
});
test("claims: a PR that adds or removes a claim is left to a human", () => {
const r = strip.restoreClaims("387 providers.\n", "388 providers. Also 388 providers.\n", [
providerCheck,
@@ -210,3 +233,53 @@ test("buildRestoreCommit: one commit on top of the head, only the planned files
fs.rmSync(dir, { recursive: true, force: true });
}
});
test("restore from real git blobs is byte-exact: the final newline survives", () => {
// Regression: contents read through a trimming helper lost the last "\n", so the "restored"
// file still differed from the merge base and the PR stayed conflicting on it.
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "strip-bytes-"));
const git = (...args: string[]) =>
execFileSync("git", args, { cwd: dir, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
const write = (file: string, content: string) => fs.writeFileSync(path.join(dir, file), content);
try {
git("init", "-q");
git("config", "user.name", "test");
git("config", "user.email", "test@example.invalid");
write("stryker.conf.json", '{\n "testFiles": ["a"]\n}\n');
write("AGENTS.md", "# Agents\nOne endpoint, 387 providers.\n");
git("add", "-A");
git("commit", "-q", "-m", "merge base");
const base = git("rev-parse", "HEAD").trim();
write("stryker.conf.json", '{\n "testFiles": ["a", "b"]\n}\n');
write("AGENTS.md", "# Agents\nOne endpoint, 388 providers.\nNew rule.\n");
git("commit", "-q", "-am", "PR head");
const head = git("rev-parse", "HEAD").trim();
assert.equal(
strip.readBlob(base, "AGENTS.md", { cwd: dir }),
"# Agents\nOne endpoint, 387 providers.\n"
);
assert.equal(strip.readBlob(base, "missing.txt", { cwd: dir }), null);
const plan = strip.planPrRestore({
changed: ["stryker.conf.json", "AGENTS.md"],
readBase: (f) => strip.readBlob(base, f, { cwd: dir }),
readHead: (f) => strip.readBlob(head, f, { cwd: dir }),
ctx,
});
const commit = strip.buildRestoreCommit({ cwd: dir, head, files: plan.files }).trim();
assert.equal(
git("rev-parse", `${commit}:stryker.conf.json`),
git("rev-parse", `${base}:stryker.conf.json`),
"the restored blob is the merge-base blob, byte for byte"
);
assert.equal(git("diff", "--name-only", base, commit, "--", "stryker.conf.json"), "");
assert.equal(
git("show", `${commit}:AGENTS.md`),
"# Agents\nOne endpoint, 387 providers.\nNew rule.\n",
"digit-only revert keeps the PR's prose and the final newline"
);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});