Skip to content

Commit 05484c7

Browse files
committed
Enforce repair contract at every checkpoint
1 parent 471bf80 commit 05484c7

2 files changed

Lines changed: 144 additions & 6 deletions

File tree

‎src/repair/execute-fix-artifact.ts‎

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,9 +2145,11 @@ function editValidatePrepareMerge({
21452145
});
21462146
}
21472147

2148-
const firstCheckpoint = commitCheckpointIfNeeded({
2148+
const firstCheckpoint = commitRepairCheckpointIfNeeded({
2149+
fixArtifact,
21492150
targetDir,
21502151
message: fixArtifact.pr_title,
2152+
phase: "initial",
21512153
trailers: mode === "replacement" ? coAuthorTrailers(contributorCredits) : [],
21522154
});
21532155
if (firstCheckpoint) {
@@ -2176,9 +2178,11 @@ function editValidatePrepareMerge({
21762178
baseBranch,
21772179
sourceHead: repairDeltaBaseHead,
21782180
onReviewFix: (reviewAttempt: JsonValue) => {
2179-
const checkpoint = commitCheckpointIfNeeded({
2181+
const checkpoint = commitRepairCheckpointIfNeeded({
2182+
fixArtifact,
21802183
targetDir,
21812184
message: `fix(clawsweeper): address review for ${result.cluster_id} (${reviewAttempt})`,
2185+
phase: `review-fix-${reviewAttempt}`,
21822186
trailers: mode === "replacement" ? coAuthorTrailers(contributorCredits) : [],
21832187
});
21842188
if (checkpoint) {
@@ -2207,9 +2211,11 @@ function editValidatePrepareMerge({
22072211
headSha: currentHead(targetDir),
22082212
});
22092213
if (sync.status === "already-current") break;
2210-
const checkpoint = commitCheckpointIfNeeded({
2214+
const checkpoint = commitRepairCheckpointIfNeeded({
2215+
fixArtifact,
22112216
targetDir,
22122217
message: `fix(clawsweeper): reconcile ${result.cluster_id} with main (${attempt})`,
2218+
phase: `base-sync-${attempt}`,
22132219
trailers: mode === "replacement" ? coAuthorTrailers(contributorCredits) : [],
22142220
});
22152221
if (checkpoint) {
@@ -2227,9 +2233,11 @@ function editValidatePrepareMerge({
22272233
break;
22282234
}
22292235
}
2230-
const finalCheckpoint = commitCheckpointIfNeeded({
2236+
const finalCheckpoint = commitRepairCheckpointIfNeeded({
2237+
fixArtifact,
22312238
targetDir,
22322239
message: `fix(clawsweeper): finalize ${result.cluster_id}`,
2240+
phase: "finalize",
22332241
trailers: mode === "replacement" ? coAuthorTrailers(contributorCredits) : [],
22342242
});
22352243
if (finalCheckpoint) {
@@ -3300,15 +3308,87 @@ function checkoutRecoverableReplacementBranch({
33003308
return { resumed: false, branch };
33013309
}
33023310

3303-
function commitCheckpointIfNeeded({ targetDir, message, trailers = [] }: LooseRecord) {
3304-
if (!run("git", ["status", "--porcelain"], { cwd: targetDir }).trim()) return "";
3311+
function commitRepairCheckpointIfNeeded({
3312+
fixArtifact,
3313+
targetDir,
3314+
message,
3315+
phase,
3316+
trailers = [],
3317+
}: LooseRecord) {
3318+
const status = run("git", ["status", "--porcelain"], { cwd: targetDir }).trim();
3319+
if (!status) return "";
3320+
enforceRepairCheckpointContract({ fixArtifact, phase, status });
33053321
run("git", ["add", "--all"], { cwd: targetDir });
33063322
const args = ["commit", "-m", message];
33073323
for (const trailer of uniqueStrings(trailers)) args.push("-m", trailer);
33083324
runGitNetwork(args, targetDir);
33093325
return run("git", ["rev-parse", "HEAD"], { cwd: targetDir }).trim();
33103326
}
33113327

3328+
function enforceRepairCheckpointContract({ fixArtifact, phase, status }: LooseRecord) {
3329+
const mustTouch = repairCheckpointMustTouchFiles(fixArtifact);
3330+
if (mustTouch.length === 0) return;
3331+
3332+
const changedFiles = changedFilesFromPorcelainStatus(String(status));
3333+
if (
3334+
changedFiles.some((file) =>
3335+
mustTouch.some((expected) => changedFileMatchesContract(file, expected)),
3336+
)
3337+
) {
3338+
return;
3339+
}
3340+
3341+
throw new Error(
3342+
[
3343+
`repair checkpoint contract rejected ${String(phase || "checkpoint")}: no must_touch file changed`,
3344+
`must_touch=${mustTouch.join(", ")}`,
3345+
`changed_files=${changedFiles.join(", ") || "none"}`,
3346+
].join("; "),
3347+
);
3348+
}
3349+
3350+
function repairCheckpointMustTouchFiles(fixArtifact: LooseRecord): string[] {
3351+
const candidates = [
3352+
...jsonStringArray(fixArtifact.must_touch),
3353+
...jsonStringArray(fixArtifact.must_touch_files),
3354+
];
3355+
return uniqueStrings(candidates.map(normalizeRepairContractPath).filter(Boolean));
3356+
}
3357+
3358+
function jsonStringArray(value: JsonValue): string[] {
3359+
return Array.isArray(value) ? value.map((entry) => String(entry)) : [];
3360+
}
3361+
3362+
function changedFilesFromPorcelainStatus(status: string): string[] {
3363+
return uniqueStrings(
3364+
status
3365+
.split(/\r?\n/)
3366+
.flatMap((line) => porcelainChangedPaths(line))
3367+
.map(normalizeRepairContractPath)
3368+
.filter(Boolean),
3369+
);
3370+
}
3371+
3372+
function porcelainChangedPaths(line: string): string[] {
3373+
if (line.length < 4) return [];
3374+
const body = line.slice(3).trim();
3375+
if (!body) return [];
3376+
const renamed = body.split(" -> ");
3377+
return renamed.length === 2 ? [renamed[1] ?? ""] : [body];
3378+
}
3379+
3380+
function changedFileMatchesContract(changedFile: string, expected: string) {
3381+
return changedFile === expected || changedFile.startsWith(`${expected.replace(/\/$/, "")}/`);
3382+
}
3383+
3384+
function normalizeRepairContractPath(value: JsonValue): string {
3385+
const pathValue = String(value ?? "").trim();
3386+
if (!pathValue || pathValue.startsWith("/") || pathValue.includes("\0")) return "";
3387+
if (/[`$;&|<>()[\]{}*?~]/.test(pathValue)) return "";
3388+
if (pathValue.split(/[\\/]/).includes("..")) return "";
3389+
return pathValue.replace(/^\.\//, "");
3390+
}
3391+
33123392
function pushRecoverableBranch({ targetDir, branch }: LooseRecord) {
33133393
assertIssueImplementationNotPaused();
33143394
const remoteSha = remoteBranchSha({ targetDir, branch });

‎test/repair/execute-fix-artifact-source.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,61 @@ test("issue implementation rechecks opt-out labels immediately before branch pus
148148
assert.match(source.slice(helperStart, helperEnd), /repairPauseLabel\(issue\.labels\)/);
149149
assert.match(source.slice(helperStart, helperEnd), /refusing to push or open a PR/);
150150
});
151+
152+
test("repair contract checkpoints use one helper for every checkpoint path", () => {
153+
const source = fs.readFileSync(
154+
path.join(process.cwd(), "src/repair/execute-fix-artifact.ts"),
155+
"utf8",
156+
);
157+
assert.equal(
158+
[...source.matchAll(/commitCheckpointIfNeeded\(/g)].length,
159+
0,
160+
"legacy unguarded checkpoint helper must not remain",
161+
);
162+
assert.equal(
163+
[...source.matchAll(/commitRepairCheckpointIfNeeded\(/g)].length,
164+
5,
165+
"four checkpoint call sites plus the helper definition should use the guarded helper",
166+
);
167+
assert.match(source, /phase: "initial"/);
168+
assert.match(source, /phase: `review-fix-\$\{reviewAttempt\}`/);
169+
assert.match(source, /phase: `base-sync-\$\{attempt\}`/);
170+
assert.match(source, /phase: "finalize"/);
171+
});
172+
173+
test("repair checkpoint contract is explicit must_touch and bypasses incomplete likely_files", () => {
174+
const source = fs.readFileSync(
175+
path.join(process.cwd(), "src/repair/execute-fix-artifact.ts"),
176+
"utf8",
177+
);
178+
const helperStart = source.indexOf("function repairCheckpointMustTouchFiles(");
179+
const helperEnd = source.indexOf("function changedFilesFromPorcelainStatus(", helperStart);
180+
assert.notEqual(helperStart, -1);
181+
assert.notEqual(helperEnd, -1);
182+
const helper = source.slice(helperStart, helperEnd);
183+
assert.match(helper, /fixArtifact\.must_touch/);
184+
assert.match(helper, /fixArtifact\.must_touch_files/);
185+
assert.doesNotMatch(helper, /likely_files/);
186+
187+
const enforceStart = source.indexOf("function enforceRepairCheckpointContract(");
188+
const enforceEnd = source.indexOf("function repairCheckpointMustTouchFiles(", enforceStart);
189+
assert.notEqual(enforceStart, -1);
190+
const enforce = source.slice(enforceStart, enforceEnd);
191+
assert.match(enforce, /if \(mustTouch\.length === 0\) return;/);
192+
assert.match(enforce, /repair checkpoint contract rejected/);
193+
});
194+
195+
test("repair contract parser preserves git porcelain path offsets", () => {
196+
const source = fs.readFileSync(
197+
path.join(process.cwd(), "src/repair/execute-fix-artifact.ts"),
198+
"utf8",
199+
);
200+
const parserStart = source.indexOf("function porcelainChangedPaths(");
201+
const parserEnd = source.indexOf("function changedFileMatchesContract(", parserStart);
202+
assert.notEqual(parserStart, -1);
203+
assert.notEqual(parserEnd, -1);
204+
const parser = source.slice(parserStart, parserEnd);
205+
assert.match(parser, /line\.slice\(3\)/);
206+
assert.doesNotMatch(parser, /status\.slice\(3\)|text\.slice\(3\)/);
207+
assert.match(parser, /body\.split\(" -> "\)/);
208+
});

0 commit comments

Comments
 (0)