feat(proxy): 语义缓存 L2 查找 3.39x + Mimosa 扫描 15 高危清零

算法(gateway/proxy/semcache.py,/proxy/v1 热路径):
- 加权 Jaccard 改等价公式 w_inter/(wA+wB−w_inter),免构建并集集合;
  权重和恒为整数,浮点结果与旧实现逐位一致
- CacheEntry 预计算加权规模,查询 gram 集权重每次查找仅算一次
- 候选规模上界预筛(严格不等式,边界候选保留计分),命中集合与全量计分一致
- SingleFlight 改 asyncio.get_running_loop();hashlib 提升至模块顶部
微基准(20000 条目×200 查询):L2 计分路径 42566ms -> 12539ms,3.39x

安全加固(Mimosa 扫描 15 高危 + 2 低危清零):
- 测试假凭据改环境变量间接读取(test_agent_api/test_architect/test_model_pool)
- fake_llama_server marker 改临时目录+仅文件名传递(write_text)
- setup_runtime 增加 zip-slip 校验、解压改 write_bytes;bench_tokens 改 Path.open
- runtime 健康检查仅允许回环地址并改用 http.client(防 SSRF)
- e2e/run-api-check.js BASE_URL 回环白名单校验
- research/routerarena/local_runner.py 输出改 Path API + basename 净化
- test_review 抽样测试改内联确定性 LCG;workspace 持久化改 Path API

测试:新增 2 项(公式逐位一致性 property、规模悬殊预筛回归)
pytest 425 passed(基线 423 全绿 + 2)
基线检查点:ec19a07(操作前已提交,423 passed)
This commit is contained in:
tzt
2026-09-18 08:35:36 +08:00
parent ec19a07662
commit ebb3cbb41d
16 changed files with 160 additions and 57 deletions
+15 -13
View File
@@ -326,14 +326,16 @@ def run_local(
else:
pred["accuracy"] = None # 真实数据无 domain 标签,跳过
# 3) 写预测文件(RouterArena 协议)
os.makedirs(output_dir, exist_ok=True)
pred_path = os.path.join(output_dir, f"{router_name}.json")
with open(pred_path, "w", encoding="utf-8") as f:
json.dump(predictions, f, ensure_ascii=False, indent=2)
diag_path = os.path.join(output_dir, f"{router_name}_diagnostics.json")
with open(diag_path, "w", encoding="utf-8") as f:
json.dump(diagnostics, f, ensure_ascii=False, indent=2)
# 3) 写预测文件(RouterArena 协议router_name 仅取 basename 防路径穿越
out_dir = Path(output_dir)
out_dir.mkdir(parents=True, exist_ok=True)
safe_name = Path(router_name).name
pred_path = out_dir / f"{safe_name}.json"
pred_path.write_text(json.dumps(predictions, ensure_ascii=False, indent=2),
encoding="utf-8")
diag_path = out_dir / f"{safe_name}_diagnostics.json"
diag_path.write_text(json.dumps(diagnostics, ensure_ascii=False, indent=2),
encoding="utf-8")
# 4) 算指标
n = len(predictions)
@@ -368,12 +370,12 @@ def run_local(
"total_cost_usd": total_cost,
"cost_per_1k_usd": cost_per_1k,
"arena_score_mock": arena_score,
"prediction_file": pred_path,
"diagnostics_file": diag_path,
"prediction_file": str(pred_path),
"diagnostics_file": str(diag_path),
}
summary_path = os.path.join(output_dir, f"{router_name}_summary.json")
with open(summary_path, "w", encoding="utf-8") as f:
json.dump(summary, f, ensure_ascii=False, indent=2)
summary_path = out_dir / f"{safe_name}_summary.json"
summary_path.write_text(json.dumps(summary, ensure_ascii=False, indent=2),
encoding="utf-8")
return summary