Skip to content

Commit 89308f3

Browse files
committed
fix(codemod-split-engine): prove a --hand-docs slice against a base an earlier slice landed in
The proof now undoes every move in the head and in the base and compares the two, so the second slice's in-run proof holds on the first slice's output and against the unsliced base alike. One self-test case pins it. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Utp6CYHBS7P5hkUQrNHkHZ
1 parent df94c71 commit 89308f3

1 file changed

Lines changed: 64 additions & 31 deletions

File tree

‎scripts/codemod-split-engine.mjs‎

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2719,10 +2719,11 @@ async function mainScoped(tm, argv) {
27192719
* The proof is the undo: putting every moved docblock back on its stub
27202720
* (re-indented), the placeholder back on its function, and taking the marker
27212721
* phrase and the header note out rebuilds the base `engine.ts` and every base
2722-
* hand module byte-for-byte. A one-character edit in a moved docblock, a block
2723-
* dropped on either side, a public docblock moved, or any other byte changed
2724-
* in those files fails it. The public `.d.ts` bar is a report reading on two
2725-
* built dists, as for the first two passes.
2722+
* hand module byte-for-byte (a base an earlier slice landed in is undone the
2723+
* same way first, so a slice is proved on its own). A one-character edit in a
2724+
* moved docblock, a block dropped on either side, a public docblock moved, or
2725+
* any other byte changed in those files fails it. The public `.d.ts` bar is a
2726+
* report reading on two built dists, as for the first two passes.
27262727
*/
27272728
const HAND_DOCS_CARD = '#30053';
27282729

@@ -2871,57 +2872,81 @@ function generateHandDocs(tm, engineText, moduleTexts, planned) {
28712872
}
28722873

28732874
/**
2874-
* The proof. Undo every move the head's markers record and require the base
2875-
* `engine.ts` and the base hand modules back, byte-for-byte. `baseModules` and
2876-
* `headModules` map a hand module file to its text (a file absent from one
2877-
* side fails).
2875+
* Undo every move a tree's markers record: each moved docblock back on its
2876+
* stub (re-indented), the placeholder back on its function, the marker phrase
2877+
* and the header note out. `moduleTexts` maps a hand module file to its text.
2878+
* Returns the undone texts, the moves it undid, and every way the tree breaks
2879+
* the rule (a public docblock moved, a marker over a docblock still on the
2880+
* class, a function with no moved docblock, a header with no note).
28782881
*/
2879-
function verifyHandDocs(tm, baseText, headText, baseModules, headModules, { className = CLASS_NAME } = {}) {
2882+
function undoHandDocs(tm, engineText, moduleTexts, className = CLASS_NAME) {
28802883
const failures = [];
28812884
const moved = [];
28822885
const hostEdits = [];
28832886
const perModule = new Map();
2884-
for (const s of handStubs(tm, headText, className)) {
2887+
for (const s of handStubs(tm, engineText, className)) {
28852888
if (!s.moved) continue;
28862889
if (s.scope !== 'private') failures.push(`${s.name}: a ${s.scope} method's docblock moved off the class (only a private one may)`);
28872890
if (s.lead !== '') failures.push(`${s.name}: its stub says the docblock moved, but the class still carries leading text`);
2888-
const text = headModules.get(s.module);
2891+
const text = moduleTexts.get(s.module);
28892892
const fn = text == null ? null : handFunction(tm, s.module, text, s.name);
28902893
if (!fn) { failures.push(`${s.name}: ${s.module} exports no function of that name`); continue; }
28912894
const sole = soleDocblock(fn.lead);
28922895
if (!sole || sole.indent !== '' || fn.lead === `${placeholderDoc(s.name, className)}\n`) {
28932896
failures.push(`${s.name}: its stub says the docblock moved, but its function in ${s.module} carries no moved docblock`);
28942897
continue;
28952898
}
2896-
const indent = /^ */.exec(headText.slice(headText.lastIndexOf('\n', s.memberStart) + 1, s.memberStart))[0];
2899+
const indent = /^ */.exec(engineText.slice(engineText.lastIndexOf('\n', s.memberStart) + 1, s.memberStart))[0];
28972900
hostEdits.push({ start: s.memberStart, end: s.memberStart, text: `${reindentDoc(sole.doc)}\n${indent}` });
28982901
hostEdits.push({ start: s.phraseAt, end: s.phraseAt + HAND_DOC_MOVED.length, text: '' });
28992902
const edits = perModule.get(s.module) ?? [];
29002903
edits.push({ start: fn.leadStart, end: fn.start, text: `${placeholderDoc(s.name, className)}\n` });
29012904
perModule.set(s.module, edits);
29022905
moved.push({ name: s.name, module: s.module, lines: sole.doc.split('\n').length });
29032906
}
2907+
const modules = new Map();
2908+
for (const [file, text] of moduleTexts) {
2909+
const edits = perModule.get(file) ?? [];
2910+
if (edits.length > 0 || text.includes(HAND_DOCS_NOTE)) {
2911+
const at = text.indexOf(`\n${HAND_DOCS_NOTE}\n */\n`);
2912+
if (at < 0) failures.push(`${file}: a docblock moved into it, but its header carries no note saying so above its closing line`);
2913+
else edits.push({ start: at, end: at + HAND_DOCS_NOTE.length + 1, text: '' });
2914+
}
2915+
modules.set(file, applyEdits(text, edits));
2916+
}
2917+
return { host: applyEdits(engineText, hostEdits), modules, moved, failures };
2918+
}
2919+
2920+
/**
2921+
* The proof: undoing every move in the head lands on exactly what undoing
2922+
* every move in the base lands on, `engine.ts` and each hand module byte for
2923+
* byte. A base no slice has touched undoes to itself, so against it this is
2924+
* "the undo rebuilds the base"; against a base an earlier slice landed in, it
2925+
* proves this slice without re-proving that one's text. `baseModules` and
2926+
* `headModules` map a hand module file to its text (a file absent from one
2927+
* side fails).
2928+
*/
2929+
function verifyHandDocs(tm, baseText, headText, baseModules, headModules, { className = CLASS_NAME } = {}) {
2930+
const head = undoHandDocs(tm, headText, headModules, className);
2931+
const base = undoHandDocs(tm, baseText, baseModules, className);
2932+
const failures = [...head.failures, ...base.failures.map((f) => `base: ${f}`)];
29042933
const firstDiff = (a, b) => {
29052934
const x = a.split('\n'); const y = b.split('\n');
29062935
let i = 0; while (i < Math.max(x.length, y.length) && x[i] === y[i]) i += 1;
2907-
return `line ${i + 1}: base ${JSON.stringify(x[i] ?? null)} | undone head ${JSON.stringify(y[i] ?? null)}`;
2936+
return `line ${i + 1}: undone base ${JSON.stringify(x[i] ?? null)} | undone head ${JSON.stringify(y[i] ?? null)}`;
29082937
};
2909-
const rebuilt = applyEdits(headText, hostEdits);
2910-
if (rebuilt !== baseText) failures.push(`undoing the moves does NOT rebuild the base ${HOST_BASENAME} (${firstDiff(baseText, rebuilt)})`);
2938+
if (head.host !== base.host) failures.push(`undoing the moves does NOT rebuild the base ${HOST_BASENAME} (${firstDiff(base.host, head.host)})`);
29112939
const files = [...new Set([...baseModules.keys(), ...headModules.keys()])].sort();
29122940
for (const file of files) {
2913-
const base = baseModules.get(file); const head = headModules.get(file);
2914-
if (base == null || head == null) { failures.push(`${file}: only in the ${base == null ? 'head' : 'base'}`); continue; }
2915-
const edits = perModule.get(file) ?? [];
2916-
if (edits.length > 0 || head.includes(HAND_DOCS_NOTE)) {
2917-
const at = head.indexOf(`\n${HAND_DOCS_NOTE}\n */\n`);
2918-
if (at < 0) failures.push(`${file}: a docblock moved into it, but its header carries no note saying so before its closing line`);
2919-
else edits.push({ start: at, end: at + HAND_DOCS_NOTE.length + 1, text: '' });
2920-
}
2921-
const undone = applyEdits(head, edits);
2922-
if (undone !== base) failures.push(`undoing the moves does NOT rebuild the base ${file} (${firstDiff(base, undone)})`);
2923-
}
2924-
return { failures, moved, modules: files.length, linesBefore: lineCount(baseText), linesAfter: lineCount(headText) };
2941+
const b = base.modules.get(file); const h = head.modules.get(file);
2942+
if (b == null || h == null) { failures.push(`${file}: only in the ${b == null ? 'head' : 'base'}`); continue; }
2943+
if (h !== b) failures.push(`undoing the moves does NOT rebuild the base ${file} (${firstDiff(b, h)})`);
2944+
}
2945+
const baseMoved = new Set(base.moved.map((m) => m.name));
2946+
return {
2947+
failures, moved: head.moved.filter((m) => !baseMoved.has(m.name)), baseMoved: base.moved.length,
2948+
modules: files.length, linesBefore: lineCount(baseText), linesAfter: lineCount(headText),
2949+
};
29252950
}
29262951

29272952
function planReportHandDocs(planned) {
@@ -2943,9 +2968,9 @@ function planReportHandDocs(planned) {
29432968

29442969
function printVerifyHandDocs(v) {
29452970
for (const f of v.failures) console.error(` ✗ ${f}`);
2946-
console.log(`hand docblocks: ${v.moved.length} moved (${v.moved.reduce((n, m) => n + m.lines, 0)} lines) across ${new Set(v.moved.map((m) => m.module)).size} module(s); ` +
2947-
`${HOST_BASENAME} ${v.linesBefore} -> ${v.linesAfter} lines; undoing the moves ${v.failures.some((f) => f.startsWith('undoing')) ? 'does NOT' : 'does'} rebuild the base ` +
2948-
`${HOST_BASENAME} and its ${v.modules} hand module(s) byte-for-byte`);
2971+
console.log(`hand docblocks: ${v.moved.length} moved (${v.moved.reduce((n, m) => n + m.lines, 0)} lines) across ${new Set(v.moved.map((m) => m.module)).size} module(s), ${v.baseMoved} already moved at base; ` +
2972+
`${HOST_BASENAME} ${v.linesBefore} -> ${v.linesAfter} lines; undoing every move ${v.failures.some((f) => f.startsWith('undoing')) ? 'does NOT rebuild' : 'rebuilds'} the base, ` +
2973+
`undone the same way, for ${HOST_BASENAME} and its ${v.modules} hand module(s) byte-for-byte`);
29492974
}
29502975

29512976
/** The hand modules `engine.ts`'s hand stubs name, read through `read(file)` (null: absent). */
@@ -3104,7 +3129,7 @@ const SELF_TEST_BATTERIES = Object.freeze({
31043129
'import prune': 4,
31053130
'refusals': 6,
31063131
'scoped classes (second pass)': 16,
3107-
'hand-series docblocks (third pass)': 15,
3132+
'hand-series docblocks (third pass)': 16,
31083133
});
31093134
const SELF_TEST_BATTERY_FLOOR = 5;
31103135

@@ -3710,6 +3735,14 @@ async function selfTest() {
37103735
!slice.modules.has('engine-beta.ts') && slice.host.includes('A beta docblock') &&
37113736
rest.host === out.host && (rest.modules.get('engine-beta.ts') ?? '') === out.modules.get('engine-beta.ts') &&
37123737
planHandDocs(tm, slice.host, sliceMods).records.find((r) => r.name === 'privateDoc')?.reason === 'already-moved');
3738+
{
3739+
const restMods = new Map([...sliceMods, ...rest.modules]);
3740+
const onSlice = verifyHandDocs(tm, slice.host, rest.host, sliceMods, restMods);
3741+
const onBase = verifyHandDocs(tm, input, rest.host, mods, restMods);
3742+
check('the next slice proves against the earlier slice\'s output, counting only its own moves, and against the unsliced base',
3743+
onSlice.failures.length === 0 && onSlice.moved.length === 1 && onSlice.baseMoved === 1 && onBase.failures.length === 0 && onBase.moved.length === 2,
3744+
[...onSlice.failures, ...onBase.failures].join(' | '));
3745+
}
37133746
const fails = (host, edited) => verifyHandDocs(tm, input, host, mods, new Map([...mods, ...out.modules, ...edited])).failures.length > 0;
37143747
check('one edited character in a moved docblock fails', fails(out.host, [['engine-alpha.ts', alpha.replace('Second paragraph.', 'Second paragraph!')]]));
37153748
check('a moved docblock dropped from its function fails', fails(out.host, [['engine-alpha.ts', alpha.replace(/\/\*\*\n \* A private docblock[\s\S]*?\*\/\n/, '')]]));

0 commit comments

Comments
 (0)