|
| 1 | +import type { JsonValue, LooseRecord } from "./json-types.js"; |
| 2 | + |
| 3 | +export interface RepairContract { |
| 4 | + mustTouch: string[]; |
| 5 | + match: "any" | "all"; |
| 6 | +} |
| 7 | + |
| 8 | +export function repairContract(fixArtifact: LooseRecord): RepairContract | null { |
| 9 | + const raw = fixArtifact.repair_contract; |
| 10 | + if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null; |
| 11 | + if (validateRepairContractShape(fixArtifact).length > 0) return null; |
| 12 | + const mustTouch = uniqueStrings(raw.must_touch.map(normalizeRepairContractPath)); |
| 13 | + return { |
| 14 | + mustTouch, |
| 15 | + match: raw.match, |
| 16 | + }; |
| 17 | +} |
| 18 | + |
| 19 | +export function enforceRepairContract({ |
| 20 | + fixArtifact, |
| 21 | + changedFiles: rawChangedFiles, |
| 22 | +}: { |
| 23 | + fixArtifact: LooseRecord; |
| 24 | + changedFiles: readonly string[]; |
| 25 | +}): void { |
| 26 | + const contract = repairContract(fixArtifact); |
| 27 | + if (!contract) return; |
| 28 | + |
| 29 | + const changedFiles = uniqueStrings( |
| 30 | + rawChangedFiles.map(normalizeRepairContractPath).filter(Boolean), |
| 31 | + ); |
| 32 | + const matched = contract.mustTouch.filter((expected) => |
| 33 | + changedFiles.some((file) => changedFileMatchesContract(file, expected)), |
| 34 | + ); |
| 35 | + const ok = |
| 36 | + contract.match === "all" ? matched.length === contract.mustTouch.length : matched.length > 0; |
| 37 | + if (ok) return; |
| 38 | + |
| 39 | + const missing = contract.mustTouch.filter((expected) => !matched.includes(expected)); |
| 40 | + throw new Error( |
| 41 | + [ |
| 42 | + "repair contract rejected final repair tree: required paths are missing from the final branch delta", |
| 43 | + `match=${contract.match}`, |
| 44 | + `must_touch=${contract.mustTouch.join(", ")}`, |
| 45 | + `matched=${matched.join(", ") || "none"}`, |
| 46 | + `missing=${missing.join(", ") || "none"}`, |
| 47 | + `changed_files=${changedFiles.join(", ") || "none"}`, |
| 48 | + ].join("; "), |
| 49 | + ); |
| 50 | +} |
| 51 | + |
| 52 | +export function changedFilesFromNameOnlyZ(diff: string): string[] { |
| 53 | + return uniqueStrings(diff.split("\0").map(normalizeRepairContractPath).filter(Boolean)); |
| 54 | +} |
| 55 | + |
| 56 | +export function validateRepairContractShape(fixArtifact: LooseRecord): string[] { |
| 57 | + const raw = fixArtifact.repair_contract; |
| 58 | + if (raw === undefined || raw === null) return []; |
| 59 | + if (typeof raw !== "object" || Array.isArray(raw)) { |
| 60 | + return ["fix_artifact.repair_contract must be an object when present"]; |
| 61 | + } |
| 62 | + |
| 63 | + const errors: string[] = []; |
| 64 | + const allowedKeys = new Set(["must_touch", "match"]); |
| 65 | + for (const key of Object.keys(raw)) { |
| 66 | + if (!allowedKeys.has(key)) { |
| 67 | + errors.push(`fix_artifact.repair_contract.${key} is not allowed`); |
| 68 | + } |
| 69 | + } |
| 70 | + if (!Array.isArray(raw.must_touch) || raw.must_touch.length === 0) { |
| 71 | + errors.push("fix_artifact.repair_contract.must_touch must be a non-empty list"); |
| 72 | + } |
| 73 | + for (const value of Array.isArray(raw.must_touch) ? raw.must_touch : []) { |
| 74 | + if (typeof value !== "string") { |
| 75 | + errors.push("fix_artifact.repair_contract.must_touch entries must be strings"); |
| 76 | + continue; |
| 77 | + } |
| 78 | + if (!normalizeRepairContractPath(value)) { |
| 79 | + errors.push( |
| 80 | + `fix_artifact.repair_contract.must_touch contains an unsafe path: ${String(value)}`, |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + if (raw.match !== "any" && raw.match !== "all") { |
| 85 | + errors.push("fix_artifact.repair_contract.match must be any or all"); |
| 86 | + } |
| 87 | + if (fixArtifact.deterministic_rebase_only === true) { |
| 88 | + errors.push( |
| 89 | + "fix_artifact.repair_contract is incompatible with deterministic_rebase_only because a pure base sync has no repair delta", |
| 90 | + ); |
| 91 | + } |
| 92 | + return errors; |
| 93 | +} |
| 94 | + |
| 95 | +function changedFileMatchesContract(changedFile: string, expected: string): boolean { |
| 96 | + const prefix = expected.replace(/\/$/, ""); |
| 97 | + return changedFile === expected || changedFile.startsWith(`${prefix}/`); |
| 98 | +} |
| 99 | + |
| 100 | +function normalizeRepairContractPath(value: JsonValue): string { |
| 101 | + const pathValue = String(value ?? "").trim(); |
| 102 | + if (!pathValue || pathValue.startsWith("/") || pathValue.includes("\0")) return ""; |
| 103 | + if (/[`$;&|<>()[\]{}*?~]/.test(pathValue)) return ""; |
| 104 | + if (pathValue.split(/[\\/]/).includes("..")) return ""; |
| 105 | + return pathValue.replace(/^\.\//, ""); |
| 106 | +} |
| 107 | + |
| 108 | +function uniqueStrings(values: readonly string[]): string[] { |
| 109 | + return [...new Set(values)]; |
| 110 | +} |
0 commit comments