#!/usr/bin/env node /** * Patch W3: log-rotator 扩展覆盖范围 * - 修复 route-2- 正则要求紧跟 dash 的 bug (实际文件 route-2026-*) * - 扩展匹配 ab-experiments / hook-timing / skill-outcome / pre-agent-gate 日 TTL * 幂等: sentinel W3_LOG_EXTEND */ const fs = require('fs'); const path = require('path'); const TARGET = path.join(__dirname, '..', '..', 'hooks', 'log-rotator.js'); const SENTINEL = 'W3_LOG_EXTEND_v1'; function main() { if (!fs.existsSync(TARGET)) { console.error('[patch-w3] missing target'); process.exit(2); } const src = fs.readFileSync(TARGET, 'utf8'); if (src.includes(SENTINEL)) { console.log('[patch-w3] already applied (sentinel found), skip'); process.exit(0); } const oldRe = /\/\^\(activity\|trace\|outcome\|compliance\|route-2\|security\|route-stats-daily\)-\/\.test\(f\)/; if (!oldRe.test(src)) { console.error('[patch-w3] anchor not found, abort'); process.exit(3); } // 新正则: 兼容 route-2026-XX-XX 格式 (route-\d{4}-);新增 ab-experiments / hook-timing / skill-outcome / pre-agent-gate const patched = src.replace( oldRe, "/* " + SENTINEL + " */ (/^(activity|trace|outcome|compliance|security|route-stats-daily|ab-experiments|hook-timing|skill-outcome|pre-agent-gate)-?/.test(f) || /^route-\\d{4}-\\d{2}-\\d{2}\\.jsonl$/.test(f))" ); if (patched === src) { console.error('[patch-w3] replace failed'); process.exit(4); } // 备份 + 原子写 const bakDir = path.join(path.dirname(TARGET), 'archive'); if (!fs.existsSync(bakDir)) fs.mkdirSync(bakDir, { recursive: true }); const bak = path.join(bakDir, 'log-rotator.js.bak.w3.' + Date.now()); fs.writeFileSync(bak, src); const tmp = TARGET + '.tmp.' + process.pid; fs.writeFileSync(tmp, patched); fs.renameSync(tmp, TARGET); console.log('[patch-w3] applied. backup:', bak); } main();