Files
clash-party/src/main/core/smartModel.ts
MOMO0302-02 3b296bf54b fix: apply Smart core settings on hot reload and stop re-downloading the model
* fix: apply Smart core settings on hot reload

The Smart core switches (LightGBM, data collection, strategy, collector
size) are delivered to the kernel through a generated global override
script, but that script was only regenerated inside prepareCore(). The
settings page persists the change and then hot reloads, so the reload
re-applied the previous script and the new values silently did not reach
the kernel until the core happened to be restarted.

Regenerate the override before generating the profile on hot reload, and
skip the write when the generated content is unchanged so routine hot
reloads no longer churn the override file.

* fix: reuse the downloaded Smart model when checking a profile

The Smart core downloads Model.bin when it is missing from its working
directory. Profile checks run the core with `-d <test dir>`, which never
receives the model, so every check starts a fresh download.

That download runs after the previous core has already been stopped and
the system proxy torn down, so it times out instead of completing, and a
failed download leaves nothing behind - the next restart repeats it. On a
restart here it cost 20s, against 0.5s once the model is in place.

Copy the model over from the working directory before running the check,
mirroring how the geo databases are already shared with the test dir.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-31 17:04:14 +08:00

35 lines
1.4 KiB
TypeScript
Raw 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.

import { copyFile, stat } from 'fs/promises'
import { existsSync } from 'fs'
import path from 'path'
import { mihomoWorkDir, mihomoTestDir } from '../utils/dirs'
import { managerLogger } from '../utils/logger'
const MODEL_FILE = 'Model.bin'
async function isSourceNewer(sourcePath: string, targetPath: string): Promise<boolean> {
try {
const [sourceStats, targetStats] = await Promise.all([stat(sourcePath), stat(targetPath)])
return sourceStats.mtime > targetStats.mtime
} catch {
return true
}
}
// Smart 内核在工作目录找不到 Model.bin 时会联网下载模型。而配置检查mihomo -t跑在独立的
// test 目录里,该目录从不包含模型;这次检查又发生在旧内核已经停止、系统代理已经撤下之后,
// 于是下载必然超时失败,每次重启白白多等约 20 秒,且失败不留文件,下次重启重演一遍。
// 正式工作目录已有模型时先同步过去,跳过这次注定失败的下载。
export async function syncSmartModelToTestDir(): Promise<void> {
const source = path.join(mihomoWorkDir(), MODEL_FILE)
if (!existsSync(source)) return
const target = path.join(mihomoTestDir(), MODEL_FILE)
if (existsSync(target) && !(await isSourceNewer(source, target))) return
try {
await copyFile(source, target)
} catch (error) {
managerLogger.warn('Failed to sync Model.bin into test dir', error)
}
}