39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
/**
|
||
|
|
* patch-x13-handoff-atomic-write.js
|
||
|
|
* pre-compact-handoff.js 第40行 handoff.json writeFileSync → tmp+rename 原子写
|
||
|
|
*
|
||
|
|
* 幂等: SENTINEL 标记
|
||
|
|
*/
|
||
|
|
'use strict';
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
const SENTINEL = '[PATCH-X13-HANDOFF-ATOMIC]';
|
||
|
|
const TARGET = path.join(__dirname, '..', '..', 'hooks', 'pre-compact-handoff.js');
|
||
|
|
|
||
|
|
if (!fs.existsSync(TARGET)) { console.log('SKIP: target not found'); process.exit(0); }
|
||
|
|
|
||
|
|
let src = fs.readFileSync(TARGET, 'utf8').replace(/\r\n/g, '\n');
|
||
|
|
if (src.includes(SENTINEL)) { console.log('SKIP: already patched'); process.exit(0); }
|
||
|
|
|
||
|
|
const old = " fs.writeFileSync(HANDOFF_PATH, JSON.stringify(handoff, null, 2), 'utf8');";
|
||
|
|
const replacement = [
|
||
|
|
" const _tmpHandoff = HANDOFF_PATH + '.tmp.' + process.pid; // " + SENTINEL,
|
||
|
|
" fs.writeFileSync(_tmpHandoff, JSON.stringify(handoff, null, 2), 'utf8');",
|
||
|
|
" fs.renameSync(_tmpHandoff, HANDOFF_PATH);"
|
||
|
|
].join('\n');
|
||
|
|
|
||
|
|
if (!src.includes(old)) {
|
||
|
|
console.error('FAIL: old handoff writeFileSync not found');
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
src = src.replace(old, replacement);
|
||
|
|
|
||
|
|
const tmp = TARGET + '.tmp.' + process.pid;
|
||
|
|
fs.writeFileSync(tmp, src, 'utf8');
|
||
|
|
fs.renameSync(tmp, TARGET);
|
||
|
|
|
||
|
|
console.log('OK: X13 handoff.json atomic write patched');
|