bookworm-smart-assistant/scripts/patches/patch-route-precision-10x-batch-b1.js
Bookworm Admin 34f304881f fix: strip session-continuity-mcp hooks from Portable template
export.mjs now removes hooks referencing npm packages not included
in the Portable distribution (session-continuity-mcp).
Eliminates MODULE_NOT_FOUND errors on Portable installations.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-27 22:15:39 +08:00

80 lines
2.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
/**
* patch-route-precision-10x-batch-b1.js
* 路由精度10项改进 — Batch B1: route-engine.js
* Item 1: 冷启动置信度上限 — coldStartApplied=true 且 gap_1_2 < 0.15 时 confidence 上限 0.65
*
* 安全性: .bak 备份 + sentinel 注释检查 + UTF-8 无 BOM 写入
*/
'use strict';
const fs = require('fs');
const path = require('path');
const SENTINEL = '// COLD_START_CONFIDENCE_CAP_v1_APPLIED';
const TARGET = path.join(__dirname, '..', 'route-engine.js');
const BAK = TARGET + '.bak';
if (!fs.existsSync(TARGET)) {
console.error('[ERROR] route-engine.js not found:', TARGET);
process.exit(1);
}
const src = fs.readFileSync(TARGET, 'utf8');
if (src.includes(SENTINEL)) {
console.log('[SKIP] Patch already applied (sentinel found).');
process.exit(0);
}
// 找到短查询置信度上限 patch 注释后面的正确插入点
// 我们需要在 _finalConfidence 计算结束后短查询cap之后插入冷启动cap
// 目标: 在 "CONFIDENCE_CAP_SHORT_QUERY_PATCH_2026_04_20" 块之后、
// "ALIAS_RESOLVER_INJECTED" 注释之前插入
const INSERT_AFTER = ` // === ALIAS_RESOLVER_INJECTED_PHASE2_2026_04_25 ===`;
if (!src.includes(INSERT_AFTER)) {
console.error('[ERROR] Anchor "ALIAS_RESOLVER_INJECTED_PHASE2_2026_04_25" not found. Cannot patch safely.');
process.exit(1);
}
const CAP_CODE = `
${SENTINEL}
// 冷启动置信度上限: coldStartApplied=true 且 rank1/rank2 分差 < 0.15 → cap 0.65
// 防止冷启动 boost 后 gap 较小时系统过度自信
if (coldStartApplied && normalized.length >= 2) {
const _n0 = normalized[0] ? (normalized[0].confidence || 0) : 0;
const _n1 = normalized[1] ? (normalized[1].confidence || 0) : 0;
const gap_1_2 = _n0 - _n1;
if (gap_1_2 < 0.15 && _finalConfidence > 0.65) {
_finalConfidence = 0.65;
try {
const _capLog = JSON.stringify({
t: Date.now(), event: 'cold_start_confidence_cap',
gap: Math.round(gap_1_2 * 1000) / 1000,
original: confidence, capped: 0.65,
primary: normalized[0] && normalized[0].name,
}) + '\\n';
fs.appendFileSync(path.join(DEBUG_DIR, 'confidence-cap.log'), _capLog);
} catch {}
}
}
`;
fs.writeFileSync(BAK, src, 'utf8');
console.log('[BAK] Backed up to', BAK);
const patched = src.replace(INSERT_AFTER, CAP_CODE + INSERT_AFTER);
if (patched === src) {
console.error('[ERROR] String replacement produced no change. Aborting.');
process.exit(1);
}
fs.writeFileSync(TARGET, patched, 'utf8');
console.log('[DONE] Item 1: cold-start confidence cap injected into route-engine.js');
console.log(' Sentinel:', SENTINEL);