fix(host): _load_config_file 与 init 的 open() 改用 with 关闭

行号纠正:摘要的 2199/2443 是另一基线行号;origin/main
aebc623 上两处是 _load_config_file 的 json.load(open(path))
与 main() init 分支的 json.dump(init_cfg, open(...)),
2443 行在 aebc623 上是 if not args.repo: 并无 open。
只改资源关闭:open 参数、读写内容、错误语义(坏 JSON 仍
转 RuntimeError)均不变,不加 encoding、不改其它裸 open()。
回归:ConfigFileCloseTest(3 项),旧代码上 3 项全 fail、
新代码上全 pass(warnings 过滤 + gc 证明无 ResourceWarning)。
This commit is contained in:
muse authored and host committed 2026-11-03 02:49:25 +00:00
1 parent e768959c93
commit 1156d3c754
2 files changed
+77 -3

No files matched your search

+4 -3
View File
@@ -2210,7 +2210,8 @@ def run_goal_loop(server, state, state_path, task, goal_text, verify,
def _load_config_file(path):
if path and os.path.exists(path):
try:
return json.load(open(path))
with open(path) as f:
return json.load(f)
except Exception as e:
raise RuntimeError(f"配置文件读失败 {path}: {e}")
return {}
@@ -2454,8 +2455,8 @@ def main():
"max_daily_turns 是按 turn 数算的预算(协议不暴露 token 数);"
"notify_command 是失败时跑的本地 shell 命令,可用 {task} {status}",
}
json.dump(init_cfg, open(config_path, "w"), indent=2,
ensure_ascii=False)
with open(config_path, "w") as f:
json.dump(init_cfg, f, indent=2, ensure_ascii=False)
print(f"配置文件已写:{config_path},请按需改 repo/model 后再用")
return
# 有效配置(init/config 之外的命令都用它)