bookworm-smart-assistant/scripts/patches/fix-cold-cap-override-0427.js

38 lines
1.7 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
// Fix: session-memory boost 覆盖冷启动 confidence cap 的 bug
// route-interceptor-bundle.js:443 用 candidates[0].confidence 覆盖了 route-engine 返回的 capped 值
// 修复: 在 A/B test 块之后重新应用 cap
const fs = require('fs');
const path = require('path');
const SENTINEL = '// COLD_START_CAP_REAPPLY_v1';
const fp = path.join(__dirname, '..', '..', 'hooks', 'route-interceptor-bundle.js');
let code = fs.readFileSync(fp, 'utf8');
if (code.includes(SENTINEL)) { console.log('SKIP: already patched'); process.exit(0); }
// 定位 A/B test 块结束位置: "} catch {}" 后的 "}"
const abMarker = "routing.experiment = experiment;";
const idx = code.indexOf(abMarker);
if (idx === -1) { console.error('FAIL: cannot find A/B test marker'); process.exit(1); }
// 找到 A/B test 的闭合 catch 块
const afterAB = code.indexOf('} catch {}', idx);
if (afterAB === -1) { console.error('FAIL: cannot find A/B catch block'); process.exit(1); }
const closeBrace = code.indexOf('}', afterAB + '} catch {}'.length);
if (closeBrace === -1) { console.error('FAIL: cannot find closing brace'); process.exit(1); }
const patch = `
${SENTINEL}
if (routing._coldStartApplied && routing.candidates && routing.candidates.length >= 2) {
const _capGap = routing.candidates[0].confidence - routing.candidates[1].confidence;
if (_capGap < 0.15 && routing.confidence > 0.65) routing.confidence = 0.65;
}`;
// 在 A/B test 块的闭合 } 之后插入
const insertPos = closeBrace + 1;
fs.copyFileSync(fp, fp + '.bak');
code = code.slice(0, insertPos) + patch + code.slice(insertPos);
fs.writeFileSync(fp, code, 'utf8');
console.log('PATCHED: cold-start cap re-apply after session-memory + A/B test');