新增 mcpcfg.txt 丢失自动重建防御(30s 轮询,OC 更新删文件后自愈)
This commit is contained in:
@@ -148,6 +148,36 @@ function healShadowPath(activePath: string, missingVal: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
// ── 一手防御:mcpcfg.txt 实时监测 + 自动重建(2026-10-12)─────────────────
|
||||
// OC 升级/更新会删掉 ~/.config/opencode 下的非官方文件(实测 mcpcfg.txt 被删
|
||||
// 导致 /mcpon /mcpoff 整个不注册)。server 插件常驻,这里做 30s 轮询兜底:
|
||||
// 文件丢失 → 探测同目录 opencode.json / 影子配置 → 重建(模式=自动)。
|
||||
// 只在缺失时动手,绝不覆盖用户手改内容。
|
||||
function ensureMcpcfgAlive(): boolean {
|
||||
try {
|
||||
if (existsSync(CFG_TXT)) return true
|
||||
const active = join(LOG_DIR, "opencode.json")
|
||||
if (!existsSync(active)) {
|
||||
fileLog("防御: mcpcfg.txt 与 opencode.json 均缺失,无法自动重建")
|
||||
return false
|
||||
}
|
||||
const has = activeHasMcp(active)
|
||||
const shadow = healShadowPath(active, "")
|
||||
if (!shadow || !existsSync(shadow)) {
|
||||
fileLog("防御: mcpcfg.txt 丢失且未找到影子配置,无法自动重建(shadow=" + shadow + ")")
|
||||
return false
|
||||
}
|
||||
const onPath = has ? active : shadow
|
||||
const quietPath = has ? shadow : active
|
||||
if (!writeMcpCfg("auto", onPath, quietPath)) return false
|
||||
fileLog("防御: mcpcfg.txt 丢失已自动重建(模式=自动 on=" + onPath + " quiet=" + quietPath + ")")
|
||||
return true
|
||||
} catch (e: any) {
|
||||
fileLog("ensureMcpcfg 异常(已吞掉): " + (e && e.message ? e.message : String(e)))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
function readMcpCfg() {
|
||||
try {
|
||||
if (!existsSync(CFG_TXT)) {
|
||||
@@ -681,6 +711,15 @@ export default {
|
||||
"(JSON Schema input + options.codemode=false + execute 返回 {content})" +
|
||||
(registration ? ",registration 已持有" : ""),
|
||||
)
|
||||
// 一手防御轮询:mcpcfg.txt 被 OC 更新删除时自动重建(30s;健康时仅一次 existsSync)
|
||||
try {
|
||||
ensureMcpcfgAlive()
|
||||
setInterval(() => {
|
||||
try {
|
||||
ensureMcpcfgAlive()
|
||||
} catch {}
|
||||
}, 30000)
|
||||
} catch {}
|
||||
// 不返回任何值(返回 cleanup 函数是允许的,但我们没有需要清理的资源;
|
||||
// 返回 hooks 对象 = 触发 Ae is not a function 整批崩溃)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user