63 lines
2.1 KiB
JavaScript
63 lines
2.1 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
// patch-x01-pre-agent-gate-regex.js — P0: restore from backup + fix \\b → \b
|
||
|
|
const fs = require('fs');
|
||
|
|
const path = require('path');
|
||
|
|
|
||
|
|
const target = path.join(__dirname, '..', '..', 'hooks', 'pre-agent-gate.js');
|
||
|
|
const bakOrig = target + '.bak.2026-04-26';
|
||
|
|
|
||
|
|
// Step 1: Restore from original backup if it exists (undo broken fix)
|
||
|
|
if (fs.existsSync(bakOrig)) {
|
||
|
|
const origBuf = fs.readFileSync(bakOrig);
|
||
|
|
// Verify backup has the double-backslash pattern (5c 5c 62)
|
||
|
|
let hasDoubleBS = false;
|
||
|
|
for (let i = 0; i < origBuf.length - 2; i++) {
|
||
|
|
if (origBuf[i] === 0x5c && origBuf[i+1] === 0x5c && origBuf[i+2] === 0x62) {
|
||
|
|
hasDoubleBS = true; break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (hasDoubleBS) {
|
||
|
|
fs.writeFileSync(target, origBuf);
|
||
|
|
process.stdout.write('[RESTORE] Restored from backup (has \\\\b pattern)\n');
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Step 2: Read target, do byte-level \\b → \b replacement on lines 15-16
|
||
|
|
const content = fs.readFileSync(target, 'utf8');
|
||
|
|
const lines = content.split('\n');
|
||
|
|
|
||
|
|
let fixCount = 0;
|
||
|
|
for (let i = 14; i <= 15; i++) {
|
||
|
|
if (!lines[i]) continue;
|
||
|
|
const lineBuf = Buffer.from(lines[i], 'utf8');
|
||
|
|
const result = [];
|
||
|
|
for (let j = 0; j < lineBuf.length; j++) {
|
||
|
|
if (j + 2 < lineBuf.length &&
|
||
|
|
lineBuf[j] === 0x5c && lineBuf[j+1] === 0x5c &&
|
||
|
|
(lineBuf[j+2] === 0x62 || lineBuf[j+2] === 0x73)) {
|
||
|
|
// \\b (5c 5c 62) → \b (5c 62) or \\s (5c 5c 73) → \s (5c 73)
|
||
|
|
result.push(0x5c);
|
||
|
|
result.push(lineBuf[j+2]);
|
||
|
|
j += 2;
|
||
|
|
fixCount++;
|
||
|
|
} else {
|
||
|
|
result.push(lineBuf[j]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
lines[i] = Buffer.from(result).toString('utf8');
|
||
|
|
}
|
||
|
|
|
||
|
|
if (fixCount === 0) {
|
||
|
|
process.stdout.write('[SKIP] No \\\\b patterns found on lines 15-16\n');
|
||
|
|
process.exit(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
fs.writeFileSync(target, lines.join('\n'), 'utf8');
|
||
|
|
|
||
|
|
// Step 3: Verify
|
||
|
|
const vBuf = Buffer.from(fs.readFileSync(target, 'utf8').split('\n')[14], 'utf8');
|
||
|
|
const hex = [];
|
||
|
|
for (let i = 2; i < 14 && i < vBuf.length; i++) hex.push(vBuf[i].toString(16).padStart(2, '0'));
|
||
|
|
process.stdout.write('[DONE] ' + fixCount + ' replacements. Line 15 hex: ' + hex.join(' ') + '\n');
|
||
|
|
// Expected: 5c 62 66 69 6e 64 5c 62 2f 69 2c 20 → \bfind\b/i,
|