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