fix: 实现 Agent 修复验证闭环

This commit is contained in:
cnbugs
2026-07-25 13:10:21 +08:00
parent 47abfef238
commit f2254646c1
6 changed files with 264 additions and 30 deletions
+13 -1
View File
@@ -7,7 +7,7 @@ from backend.diagnosis import KUBECTL
READ_ONLY_VERBS = {
"api-resources", "api-versions", "auth", "cluster-info", "describe",
"api-resources", "api-versions", "auth", "cluster-info", "config", "describe",
"diff", "explain", "get", "logs", "top", "version", "wait",
}
WRITE_OR_INTERACTIVE_VERBS = {
@@ -69,9 +69,21 @@ def _find_verb(args: list[str]) -> str:
raise ValueError("无法识别 kubectl 操作类型")
READ_ONLY_CONFIG_SUBCOMMANDS = {"current-context", "get-clusters", "get-contexts", "get-users", "view"}
def classify_kubectl_command(command: str) -> CommandDecision:
args = parse_kubectl_command(command)
verb = _find_verb(args)
if verb == "config":
try:
verb_index = args.index(next(arg for arg in args if arg.lower() == "config"))
subcommand = args[verb_index + 1].lower()
except (StopIteration, ValueError, IndexError):
return CommandDecision("write", True, verb, args)
if subcommand in READ_ONLY_CONFIG_SUBCOMMANDS:
return CommandDecision("read", False, verb, args)
return CommandDecision("write", True, verb, args)
if verb in READ_ONLY_VERBS:
return CommandDecision("read", False, verb, args)
if verb in WRITE_OR_INTERACTIVE_VERBS:
+79 -3
View File
@@ -3,6 +3,7 @@ import json
import re
import uuid
from collections import Counter
from typing import Optional
import httpx
from backend.config import AI_API_BASE, AI_API_KEY, AI_MODEL
from backend.agent_tools import classify_kubectl_command, execute_kubectl
@@ -40,7 +41,9 @@ AGENT_SYSTEM_PROMPT = """你是 Kubernetes 自主诊断 Agent。你的首要职
每轮必须只返回一个 JSON 对象,不要使用 Markdown 代码块:
- 继续调查:{"action":"command","command":"kubectl ...","reason":"为什么执行"}
- 完成分析:{"action":"final","analysis":"中文 Markdown 分析结论"}
- 完成分析:{"action":"final","analysis":"中文 Markdown 分析结论","verified":true,"remaining_issues":0}
只有完成修复后的全量复检,并确认 remaining_issues 为 0 时才能返回 final。只定位根因、执行命令成功或部分资源恢复都不能结束。
"""
_PENDING_APPROVALS: dict[str, dict] = {}
@@ -72,6 +75,9 @@ def _parse_agent_action(content: str) -> dict:
raise ValueError("AI 命令动作缺少 command")
if action["action"] == "final" and not action.get("analysis"):
raise ValueError("AI 最终动作缺少 analysis")
if action["action"] == "final":
if action.get("verified") is not True or action.get("remaining_issues") != 0:
raise ValueError("AI 最终动作必须确认 verified=true 且 remaining_issues=0")
return action
@@ -130,17 +136,58 @@ async def run_diagnostic_agent(
"content": "AI 执行模式已自动采集第一份现场证据:\n" + json.dumps(initial_result, ensure_ascii=False),
})
for step in range(1, max_steps + 1):
async for event in _run_agent_loop(
messages=messages,
execution_mode=execution_mode,
max_steps=max_steps,
start_step=1,
command_counts=command_counts,
report=report,
question=question,
):
yield event
async def _run_agent_loop(
messages: list[dict],
execution_mode: str,
max_steps: int,
start_step: int,
command_counts: Counter,
report: str,
question: str,
):
for step in range(start_step, max_steps + 1):
yield {"type": "thinking", "step": step, "message": f"AI 正在规划第 {step} 轮诊断"}
try:
content = await _complete_chat(messages)
action = _parse_agent_action(content)
except ValueError as exc:
if "最终动作必须确认" in str(exc):
messages.extend([
{"role": "assistant", "content": content},
{
"role": "user",
"content": (
"不能结束诊断:尚未通过修复后的全量复检确认所有问题清零。"
"请继续返回 command,检查节点、Pod、Deployment、事件以及原故障资源;"
"只有 verified=true 且 remaining_issues=0 时才可返回 final。"
),
},
])
yield {"type": "verification_required", "message": "尚未确认所有问题清零,Agent 继续执行诊断验证"}
continue
yield {"type": "error", "message": str(exc)}
return
except Exception as exc:
yield {"type": "error", "message": str(exc)}
return
if action["action"] == "final":
yield {"type": "final", "content": action["analysis"], "model": AI_MODEL}
yield {
"type": "final", "content": action["analysis"], "model": AI_MODEL,
"verified": True, "remaining_issues": 0,
}
return
command = action["command"]
@@ -191,6 +238,13 @@ async def run_diagnostic_agent(
"reason": reason,
"created_by": AI_MODEL,
"run_id": "",
"report": report,
"question": question,
"execution_mode": execution_mode,
"messages": messages + [{"role": "assistant", "content": content}],
"next_step": step + 1,
"max_steps": max_steps,
"command_counts": dict(command_counts),
}
yield {
"type": "approval_required", "approval_id": approval_id,
@@ -214,6 +268,28 @@ async def run_diagnostic_agent(
}
async def resume_diagnostic_agent(pending: dict, result: dict):
"""审批命令执行后,把真实结果交回原 Agent 上下文并继续诊断。"""
messages = list(pending.get("messages") or [])
messages.append({
"role": "user",
"content": (
"用户已批准命令,以下是实际执行结果。请继续自主诊断并验证问题是否解决:\n"
+ json.dumps(result, ensure_ascii=False)
),
})
async for event in _run_agent_loop(
messages=messages,
execution_mode=pending.get("execution_mode", "ai"),
max_steps=int(pending.get("max_steps", 30)),
start_step=int(pending.get("next_step", 1)),
command_counts=Counter(pending.get("command_counts") or {}),
report=pending.get("report", ""),
question=pending.get("question", ""),
):
yield event
def take_pending_approval(approval_id: str) -> dict | None:
return _PENDING_APPROVALS.pop(approval_id, None)
+36 -7
View File
@@ -22,7 +22,7 @@ from backend.diagnosis import (
from backend.ai_agent import (
analyze_with_ai, chat_with_ai,
analyze_with_ai_stream, chat_with_ai_stream,
run_diagnostic_agent, take_pending_approval,
run_diagnostic_agent, resume_diagnostic_agent, take_pending_approval,
)
from backend.agent_tools import execute_kubectl
from backend.history_store import HistoryStore
@@ -371,12 +371,41 @@ async def execute_approved_agent_command(req: AgentApprovalRequest):
history_store.finish_run(pending["run_id"], "rejected", result["message"])
return result
result = await execute_kubectl(pending["command"])
response = {"approved": True, "reason": pending["reason"], **result}
if pending.get("run_id"):
history_store.append_event(pending["run_id"], "approved_result", response)
status = "completed" if result.get("returncode") == 0 else "failed"
history_store.finish_run(pending["run_id"], status, result.get("stdout") or result.get("stderr", ""))
return response
approved_event = {"approved": True, "reason": pending["reason"], **result}
async def event_generator():
yield f"data: {json.dumps({'type': 'approved_result', **approved_event}, ensure_ascii=False)}\n\n"
run_id = pending.get("run_id", "")
if run_id:
history_store.append_event(run_id, "approved_result", approved_event)
final_status = "completed"
summary = result.get("stdout") or result.get("stderr", "")
async for event in resume_diagnostic_agent(pending, result):
if run_id:
history_store.append_event(run_id, event.get("type", "event"), {k: v for k, v in event.items() if k != "type"})
if event.get("type") == "final":
summary = event.get("content", "")
elif event.get("type") == "error":
final_status = "failed"
summary = event.get("message", "")
elif event.get("type") == "approval_required":
final_status = "waiting"
from backend.ai_agent import _PENDING_APPROVALS
next_pending = _PENDING_APPROVALS.get(event.get("approval_id"))
if next_pending is not None:
next_pending["run_id"] = run_id
yield f"data: {json.dumps(event, ensure_ascii=False)}\n\n"
if run_id:
history_store.finish_run(run_id, final_status, summary)
yield f"data: {json.dumps({'type': 'done', 'run_id': run_id}, ensure_ascii=False)}\n\n"
return StreamingResponse(
event_generator(),
media_type="text/event-stream",
headers={"Cache-Control": "no-cache", "X-Accel-Buffering": "no"},
)
@app.post("/api/chat")