feat: 扩充 shell 命令白名单(含 containerd/ctr/nerdctl),解除 AI Agent 轮数上限

- 新增 ~50 个 Linux 基础命令(du/top/iostat/sar/curl/tcpdump 等)
- 新增 containerd 系列: ctr (containers/images/tasks/snapshots),
  nerdctl (ps/logs/inspect/stats 等)
- ip 命令补充 maddr/monitor/tunnel/tuntap 只读操作
- _run_agent_loop 改为 while True 循环,max_steps=0 表示不限轮数
- main.py 传入 max_steps=0,AI Agent 持续诊断直到问题解决
- 更新 Agent system prompt 第7条:明确无轮数上限
This commit is contained in:
Your Name
2026-07-25 14:17:56 +08:00
parent 1f37cd3268
commit 4b54e49a0d
3 changed files with 30 additions and 12 deletions
+16 -9
View File
@@ -44,7 +44,7 @@ AGENT_SYSTEM_PROMPT = """你是 Kubernetes 自主诊断 Agent。你的首要职
4. apply、delete、patch、scale、rollout、exec 等修改或交互命令必须暂停并请求人工批准。
5. systemctl restart/stop/start 等系统修改命令必须暂停并请求人工批准。
6. 不要猜测命令输出;必须依据实际工具结果继续判断。
7. 最多执行有限轮次,优先使用 namespace 和资源名缩小范围
7. 只要问题没有完全解决(verified=true 且 remaining_issues=0),就要持续执行命令诊断,没有轮数上限
每轮必须只返回一个 JSON 对象,不要使用 Markdown 代码块:
- 继续调查:{"action":"command","command":"kubectl ...","reason":"为什么执行"}
@@ -165,7 +165,10 @@ async def _run_agent_loop(
question: str,
):
invalid_response_count = 0
for step in range(start_step, max_steps + 1):
step = start_step
while True:
if max_steps > 0 and step > max_steps:
break
yield {"type": "thinking", "step": step, "message": f"AI 正在规划第 {step} 轮诊断"}
try:
content = await _complete_chat(messages)
@@ -293,13 +296,17 @@ async def _run_agent_loop(
{"role": "user", "content": "命令实际执行结果:\n" + json.dumps(result, ensure_ascii=False)},
])
yield {
"type": "error",
"message": (
f"自主诊断已达到安全上限({max_steps} 轮),但 Agent 仍未形成有效结论。"
"系统已停止继续执行以避免无限循环,请检查是否重复执行相同命令或 AI 服务响应异常。"
),
}
step += 1
if max_steps > 0:
yield {
"type": "error",
"message": (
f"自主诊断已达到安全上限({max_steps} 轮),但 Agent 仍未形成有效结论。"
"系统已停止继续执行以避免无限循环,请检查是否重复执行相同命令或 AI 服务响应异常。"
),
}
# max_steps=0 表示不限轮数,不输出错误 — Agent 会持续执行直到完成或遇到致命错误
async def resume_diagnostic_agent(pending: dict, result: dict):