From df044f6c5972b191fad05c34080c9accb12d986a Mon Sep 17 00:00:00 2001
From: tfarcraw <123211521@163.com>
Date: Wed, 18 Nov 2026 14:07:30 +0800
Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E4=BF=9D=E5=AD=98Agent?=
=?UTF-8?q?=E5=88=86=E6=9E=90=E5=85=A8=E8=BF=87=E7=A8=8B=E6=97=A5=E5=BF=97?=
=?UTF-8?q?=E5=B9=B6=E6=98=BE=E7=A4=BA=E5=85=B7=E4=BD=93=E5=A4=B1=E8=B4=A5?=
=?UTF-8?q?=E5=8E=9F=E5=9B=A0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
每次分析创建独立JSONL日志,实时记录模型请求与原始响应、工具调用、视频时间点、上下文压缩、回测结果及异常;实际读取和送检截图独立落盘。聊天显示日志位置,鉴权密钥与内联图片数据脱敏。取消和失败保留完整已写日志,额外错误快照关联会话日志。
修复tool_calls为空时的响应兼容问题,检查空choices及服务返回错误,避免吞掉具体解析异常。
验证:SopVideoAgent.Smoke、VideoFlowTest.Smoke通过,涵盖日志实时写入、图片证据、脱敏、异常与取消。真实接口独立分析完成三轮回测,未复现通用异常,草稿实际停在第二步;报告见scratch/agent-failure-live。额外Avalonia.Smoke的旧侧栏数量断言在进入Agent检查前失败,本次未涉及侧栏。
---
.../VVMs/RecipeEditor/SopAgentErrors.cs | 45 +++++++++++
.../VVMs/RecipeEditor/SopAgentRunLog.cs | 66 ++++++++++++++++
.../RecipeEditor/SopVideoAgentViewModel.cs | 45 ++++++++---
.../SopAgent/SopVideoAgentClient.cs | 78 +++++++++++++++----
.../SopAgent/SopVideoDraftClient.cs | 4 +-
tests/SopVideoAgent.Smoke/Program.cs | 1 +
.../ResponseDiagnosticsChecks.cs | 35 +++++++++
.../VerificationLoopChecks.cs | 6 +-
.../AgentDraftVerificationChecks.cs | 40 +++++++++-
9 files changed, 290 insertions(+), 30 deletions(-)
create mode 100644 src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentErrors.cs
create mode 100644 src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentRunLog.cs
create mode 100644 tests/SopVideoAgent.Smoke/ResponseDiagnosticsChecks.cs
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentErrors.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentErrors.cs
new file mode 100644
index 0000000..5c53c02
--- /dev/null
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentErrors.cs
@@ -0,0 +1,45 @@
+using System.IO;
+using System.Net.Http;
+using System.Text.Json;
+using SopHelper.Engine.SopAgent;
+using SopHelper.Presentation;
+
+namespace SopHelper.VVMs.RecipeEditor;
+
+public partial class RecipeEditorViewModel
+{
+ private void ReportAgentFailure(Exception error, SopAgentConnection connection, SopAgentRunLog? runLog)
+ {
+ var stage = AgentStatus;
+ var reason = error switch
+ {
+ HttpRequestException => "模型接口连接失败:" + error.Message,
+ JsonException => "模型接口返回的 JSON 无法解析:" + error.Message,
+ KeyNotFoundException or InvalidOperationException or IndexOutOfRangeException
+ => "处理模型响应或草稿时发生异常:" + error.Message,
+ InvalidDataException => error.Message,
+ IOException => "读取视频或本地文件失败:" + error.Message,
+ _ => error.Message
+ };
+ if (error.GetBaseException() is { } cause && !ReferenceEquals(cause, error)) reason += ";根因:" + cause.Message;
+ AgentStatus = "分析失败:" + SopAgentRunLog.Redact(reason, connection.ApiKey, 1600) + ";当前配方未修改。";
+ AgentChatMessages.Add(new("系统", AgentStatus));
+ try
+ {
+ runLog?.Write(new("exception", 0, $"阶段:{stage}\n{error}"));
+ var folder = Path.Combine(DesktopPlatform.WorkspaceRoot, "workspace", "agent-diagnostics");
+ Directory.CreateDirectory(folder);
+ var path = Path.Combine(folder, "error-" + DateTime.Now.ToString("yyyyMMdd-HHmmss-fff") + "-" + Guid.NewGuid().ToString("N")[..8] + ".json");
+ File.WriteAllText(path, JsonSerializer.Serialize(new
+ {
+ Time = DateTimeOffset.Now, Stage = stage, Model = connection.Model, SessionLog = runLog?.Path,
+ connection.ContextLength, connection.MaxTokens, Video = AgentVideoPath,
+ Error = SopAgentRunLog.Redact(error.ToString(), connection.ApiKey, 16000)
+ }, new JsonSerializerOptions { WriteIndented = true, Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping }));
+ AgentChatMessages.Add(new("错误详情", $"失败阶段:{stage}\n异常类型:{error.GetType().Name}\n诊断记录:{path}"));
+ }
+ catch (Exception logError) when (logError is IOException or UnauthorizedAccessException)
+ { AgentChatMessages.Add(new("错误详情", "诊断文件无法写入,具体原因已显示在上一条消息中。")); }
+ }
+
+}
diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentRunLog.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentRunLog.cs
new file mode 100644
index 0000000..c5392fa
--- /dev/null
+++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentRunLog.cs
@@ -0,0 +1,66 @@
+using System.IO;
+using System.Text.Json;
+using System.Text.RegularExpressions;
+using SopHelper.Engine.SopAgent;
+
+namespace SopHelper.VVMs.RecipeEditor;
+
+/// 按事件立即落盘,异常、取消和关闭程序后仍可诊断。图片单独保存,不把 Base64 写入日志。
+internal sealed class SopAgentRunLog : IDisposable
+{
+ private readonly object _gate = new();
+ private readonly StreamWriter _writer;
+ private readonly string _apiKey;
+ private bool _disposed;
+ private int _imageIndex;
+ public string Path { get; }
+
+ public SopAgentRunLog(string workspace, string apiKey)
+ {
+ _apiKey = apiKey;
+ var folder = System.IO.Path.Combine(workspace, "workspace", "agent-logs");
+ Directory.CreateDirectory(folder);
+ Path = System.IO.Path.Combine(folder, "analysis-" + DateTime.Now.ToString("yyyyMMdd-HHmmss-fff") + "-" + Guid.NewGuid().ToString("N")[..8] + ".jsonl");
+ _writer = new StreamWriter(new FileStream(Path, FileMode.CreateNew, FileAccess.Write, FileShare.Read)) { AutoFlush = true };
+ }
+
+ public void Write(SopAgentTrace trace)
+ {
+ lock (_gate)
+ {
+ if (_disposed) return;
+ var images = new List