基底:#251 头 174b631(堆叠于 #242 feat-ci-expand 之上), 基线 origin/main aebc6238;本轨不改 #251 已定语义、不动 workflow 门槛、不删检查、不加 skip。 lint(ruff E,F@120):109 -> 10 - F401 43 -> 0:删未用导入;try/except 双绑定的名字出现 “打地鼠”(删 except 分支后 try 分支又被标出),两分支 均已核对正文无引用后清掉;MCP 三模块的 Error 名不删, 改走 importlib 属性绑定(见下),两路径绑定都保留。 - E702 40 -> 0:全在 host.py main() 的 argparse 段,纯分行, tokenize 按分号 token 拆(含 2377 行多行语句只断首个分号); 一次 5 空格缩进失误已回滚重做并以 ast + 全量 host 测试验证。 - E501 13 -> 0:host.py 5 项随 E702 分行连带清掉;mcp_server 3 项把描述拆成相邻字面量(AST 比对 24 条工具描述与原值 完全一致,注意 ruff 按东亚宽度计长,CJK 算 2);test_host 5 项断开 git commit 参数表。 - E402 8 -> 10:生产侧 2 项清掉(db.py _re、github_app _base64 移到顶部,之前无引用、无重名);剩余 10 项全是 tests/ 的 sys.path 前置 import(6 项旧 + 4 个新测试文件 沿用同一既定模式),移上去会破坏单文件直跑,有意保留。 - F841 4 -> 0:只去绑定、不删调用(run_task、DB 入队、 tarfile 上下文、git 提交),各有注释与原测试/新测试覆盖; host dispatch_from_branch 新增真实 git 仓库回归测试。 - F811 1 -> 0:server 本地 _row_get 与 peer_messages.row_get 逐行相同,删本地定义,全部调用点解析到同一 import; 新增 test_server_row_get(同一对象 + 三路行为)。 mypy(生产范围):24 -> 0,零新增 type: ignore - no-redef 9 -> 0:MCP 三模块 try/except 两次 from-import 改 importlib.import_module + 属性绑定(cloud 优先、flat copy 回落、内层 ImportError 同样回落,语义与原来一致; flat-copy 子进程测试证实回落后 __module__ 为顶层名); server _row_get 随 F811 消除。 - arg-type 10 -> 0:harness2_tasks shared 改 TypedDict 逐键精确类型;CodexProbeAdapter 补 no-op validate_tree (_adapter_validate 本就跳过缺失方法,行为一致)。 - var-annotated 3 -> 0:sub2api _SHARED_ADMIN/_login_slots、 verify-multiuser RESULTS 加精确注解。 - assignment/misc 2 -> 0:marker_tools ZoneInfo 先声明 Any 再 try import(草稿先验 mypy 通过)。 bandit:总数 286 不变,High 仍 4 项,全在 host.py,本轨不改 (属第 3 项白名单轨范围),只做 triage: - 1319 run_verify:无插值,整串验测命令,管道/&& 须 shell 语义(新增 test_host_run_verify 断言管道与 && 真实生效)。 - 1497 notify:模板来自算子配置,三处插值全部 shlex.quote, 证据为既有 test_notify_failure_quotes_shell_metacharacters (; touch 与 $(touch ) 不执行)。 - 2099/2103 goal verify:无插值,任务配置验测串,同 run_verify。 - 无新增 High;pip-audit 0 漏洞。 覆盖率(同口径三套件 coverage 计量):75.96% -> 77.87% (22392 行/缺 5385 -> 22437 行/缺 4963,显示 76% -> 78%) - client.py 0% -> 90%(pipe 假子进程 + main 分支) - run_parallel.py 0% -> 97%、migrate 包装 0% -> 95% - playwright_mcp 37% -> 62%、imagegen_mcp 47% -> 79%、 websearch_mcp 51% -> 86%、deploy.py 56% -> 77% - verify-multiuser 0% -> 27%(仅 check/req helper;main() 是主机侧真实多用户实弹验证,按门禁不建生产任务、 不以 mock 冒充,仍为缺口) - host.py 55% -> 57%(文件 2020 -> 2060 行系 E702 分行 的计量口径变化,非代码膨胀) - 未到 85%:门槛保持 75% 不动,不删低覆盖模块出统计口径; 缺口主力仍是 server.py / worker.py / host.py 大文件, 下一阶段须针对性深测,非本轨快赢可及。 验证:三套件 1576 -> 1629 项全过(tests 127、cloud/tests 1449 skipped=4、deploy/tests 53);ruff E9 通过;mypy 0; bandit High 4(同上);pip-audit 0 漏洞;试跑产物已删, 提交后工作树干净。
194 lines
8.7 KiB
Python
194 lines
8.7 KiB
Python
"""实时链路(host 侧)单测:通知摘要器 + 链路文件追加。
|
||
|
||
不启动 app-server:Server 实例用 object.__new__ 绕开 __init__
|
||
(__init__ 会拉起 codex 子进程),只测与进程无关的方法。
|
||
"""
|
||
|
||
import json
|
||
import os
|
||
import shutil
|
||
import sys
|
||
import tempfile
|
||
import unittest
|
||
from pathlib import Path
|
||
|
||
try:
|
||
from host import Server, TRACE_MAX_LINES, _summarize_trace_item
|
||
except ImportError:
|
||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||
from host import Server, TRACE_MAX_LINES, _summarize_trace_item
|
||
|
||
|
||
def _msg(item, ts=1700000000000):
|
||
return {"method": "item/completed",
|
||
"params": {"item": item, "completedAtMs": ts}}
|
||
|
||
|
||
class SummarizeTraceItemTestCase(unittest.TestCase):
|
||
def test_exec_truncates_command(self):
|
||
entries = _summarize_trace_item(
|
||
"item/completed",
|
||
{"item": {"type": "commandExecution", "id": "c1",
|
||
"command": "x" * 600, "cwd": "/w",
|
||
"exitCode": 0, "durationMs": 12},
|
||
"completedAtMs": 1700000000000})
|
||
self.assertEqual(len(entries), 1)
|
||
self.assertEqual(entries[0]["kind"], "exec")
|
||
self.assertEqual(len(entries[0]["command"]), 500)
|
||
self.assertEqual(entries[0]["exitCode"], 0)
|
||
self.assertEqual(entries[0]["ts"], 1700000000000)
|
||
|
||
def test_file_change_split_per_change(self):
|
||
entries = _summarize_trace_item(
|
||
"item/completed",
|
||
{"item": {"type": "fileChange", "id": "f1", "status": "completed",
|
||
"changes": [{"path": "a.py", "kind": "update"},
|
||
{"path": "b.py", "kind": "add"}]},
|
||
"startedAtMs": 1700000000001})
|
||
self.assertEqual([e["kind"] for e in entries], ["file", "file"])
|
||
self.assertEqual(entries[0]["id"], "f1:a.py")
|
||
self.assertEqual(entries[1]["path"], "b.py")
|
||
|
||
def test_message_reasoning_tool_call_plan(self):
|
||
cases = [
|
||
({"type": "agentMessage", "id": "m1", "phase": "final",
|
||
"text": "done"}, "message"),
|
||
({"type": "reasoning", "id": "r1",
|
||
"summary": ["a", "b"]}, "reasoning"),
|
||
({"type": "mcpToolCall", "id": "t1", "server": "s",
|
||
"tool": "read", "status": "completed"}, "tool"),
|
||
({"type": "functionCallOutput", "id": "o1",
|
||
"name": "read_file"}, "call"),
|
||
({"type": "plan", "id": "p1", "text": "step"}, "plan"),
|
||
]
|
||
for item, kind in cases:
|
||
entries = _summarize_trace_item("item/completed",
|
||
{"item": item})
|
||
self.assertEqual(len(entries), 1)
|
||
self.assertEqual(entries[0]["kind"], kind)
|
||
|
||
def test_message_truncation_is_flagged(self):
|
||
# 2026-10-29 实弹:468 字 final_answer 在链路里只剩前
|
||
# 300 字、断在“fu”后,控制台照原样当助手回复展示,
|
||
# 用户误以为模型没写完。截断本身是设计(防链路文件
|
||
# 写爆),但必须带标记,短消息不带标记。
|
||
long_entries = _summarize_trace_item(
|
||
"item/completed",
|
||
{"item": {"type": "agentMessage", "id": "m-long",
|
||
"phase": "final_answer", "text": "x" * 468}})
|
||
self.assertEqual(len(long_entries[0]["text"]), 300)
|
||
self.assertTrue(long_entries[0].get("truncated"))
|
||
short_entries = _summarize_trace_item(
|
||
"item/completed",
|
||
{"item": {"type": "agentMessage", "id": "m-short",
|
||
"phase": "final_answer", "text": "done"}})
|
||
self.assertNotIn("truncated", short_entries[0])
|
||
|
||
def test_skipped_notifications(self):
|
||
self.assertEqual(_summarize_trace_item(
|
||
"item/completed", {"item": {"type": "userMessage", "id": "u1",
|
||
"text": "hi"}}), [])
|
||
self.assertEqual(_summarize_trace_item(
|
||
"item/completed", {"item": {"type": "agentMessage", "id": "m2",
|
||
"text": ""}}), [])
|
||
self.assertEqual(_summarize_trace_item("item/completed", {}), [])
|
||
self.assertEqual(_summarize_trace_item(
|
||
"item/completed", {"item": {"type": "unknownThing",
|
||
"id": "z"}}), [])
|
||
|
||
|
||
class TraceFileTestCase(unittest.TestCase):
|
||
def _bare_server(self):
|
||
srv = object.__new__(Server)
|
||
srv.trace_path = None
|
||
srv._trace_lines = 0
|
||
return srv
|
||
|
||
def test_set_trace_file_and_append(self):
|
||
with tempfile.TemporaryDirectory() as d:
|
||
state_path = os.path.join(d, "host-state.json")
|
||
srv = self._bare_server()
|
||
srv.set_trace_file(state_path, "abc/def?id=1")
|
||
self.assertEqual(
|
||
srv.trace_path,
|
||
os.path.join(d, "host-traces", "abc-def-id-1.jsonl"))
|
||
srv._append_trace(_msg({"type": "commandExecution", "id": "c1",
|
||
"command": "ls", "cwd": "/w",
|
||
"exitCode": 0, "durationMs": 3}))
|
||
srv._append_trace(_msg({"type": "userMessage", "id": "u1",
|
||
"text": "hi"}))
|
||
with open(srv.trace_path, encoding="utf-8") as f:
|
||
lines = f.read().strip().split("\n")
|
||
self.assertEqual(len(lines), 1)
|
||
self.assertEqual(json.loads(lines[0])["kind"], "exec")
|
||
|
||
def test_set_trace_file_failure_keeps_none(self):
|
||
with tempfile.TemporaryDirectory() as d:
|
||
# host-traces 已被普通文件占住,建目录失败时不抛、不影响回合。
|
||
blocker = os.path.join(d, "host-traces")
|
||
with open(blocker, "w", encoding="utf-8") as f:
|
||
f.write("x")
|
||
srv = self._bare_server()
|
||
srv.set_trace_file(os.path.join(d, "host-state.json"), "t1")
|
||
self.assertIsNone(srv.trace_path)
|
||
|
||
def test_append_stops_at_max_lines(self):
|
||
with tempfile.TemporaryDirectory() as d:
|
||
srv = self._bare_server()
|
||
srv.trace_path = os.path.join(d, "t.jsonl")
|
||
srv._trace_lines = TRACE_MAX_LINES - 1
|
||
srv._append_trace(_msg({"type": "commandExecution", "id": "c1",
|
||
"command": "a"}))
|
||
srv._append_trace(_msg({"type": "commandExecution", "id": "c2",
|
||
"command": "b"}))
|
||
with open(srv.trace_path, encoding="utf-8") as f:
|
||
lines = f.read().strip().split("\n")
|
||
self.assertEqual(len(lines), 1)
|
||
self.assertEqual(json.loads(lines[0])["id"], "c1")
|
||
|
||
def test_summarize_carries_thread(self):
|
||
# 模型内子代理与主任务同文件时,服务端凭 thread 归属条目。
|
||
entries = _summarize_trace_item(
|
||
"item/completed",
|
||
{"item": {"type": "commandExecution", "id": "c1",
|
||
"command": "ls"},
|
||
"threadId": "th-sub", "completedAtMs": 1700000000000})
|
||
self.assertEqual(entries[0]["thread"], "th-sub")
|
||
entries = _summarize_trace_item(
|
||
"item/completed",
|
||
{"item": {"type": "commandExecution", "id": "c2",
|
||
"command": "ls"},
|
||
"completedAtMs": 1700000000000})
|
||
self.assertIsNone(entries[0]["thread"])
|
||
|
||
def test_append_trace_marker_to_parent_file(self):
|
||
# delegate 向父链路报到:路径清洗与 set_trace_file 一致,
|
||
# 写失败不抛(尽力而为,不影响回合)。
|
||
from host import _append_trace_marker, _trace_file_path
|
||
with tempfile.TemporaryDirectory() as d:
|
||
state_path = os.path.join(d, "host-state.json")
|
||
self.assertEqual(
|
||
_trace_file_path(state_path, "abc/def?id=1"),
|
||
os.path.join(d, "host-traces", "abc-def-id-1.jsonl"))
|
||
_append_trace_marker(
|
||
state_path, "parent task!",
|
||
{"kind": "agent-start", "id": "agent:rev",
|
||
"name": "rev", "prompt": "review"})
|
||
path = os.path.join(d, "host-traces", "parent-task-.jsonl")
|
||
with open(path, encoding="utf-8") as f:
|
||
entry = json.loads(f.read().strip())
|
||
self.assertEqual(entry["kind"], "agent-start")
|
||
self.assertEqual(entry["name"], "rev")
|
||
# 目录被文件占住时也不抛。
|
||
blocker = os.path.join(d, "host-traces")
|
||
shutil.rmtree(blocker)
|
||
with open(blocker, "w", encoding="utf-8") as f:
|
||
f.write("x")
|
||
_append_trace_marker(state_path, "parent task!",
|
||
{"kind": "agent-done", "id": "agent:rev:done",
|
||
"name": "rev", "status": "completed"})
|
||
|
||
|
||
if __name__ == "__main__":
|
||
unittest.main()
|