From c4a831a55c9e3b9873b44a38d13bce20e458c19b Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Wed, 18 Nov 2026 10:04:11 +0800 Subject: [PATCH] =?UTF-8?q?Agent=E7=94=9F=E6=88=90=E6=AD=A5=E9=AA=A4?= =?UTF-8?q?=E5=90=8E=E8=87=AA=E5=8A=A8=E5=9B=9E=E6=94=BE=E6=A0=87=E5=87=86?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E5=B9=B6=E4=BE=9D=E6=8D=AE=E7=9C=9F=E5=AE=9E?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=E4=BF=AE=E8=AE=A2=E8=8D=89=E7=A8=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 默认执行生成、回测、失败图像反馈与最多三轮修订;支持独立触发与送检区域、防抖参数和有画面证据的区域校正提案。保护AI阈值和步骤顺序;应用与回测共用配置映射,手改草稿或标定后需重新验证,不自动保存配方。 验证通过:SopVideoAgent.Smoke、VideoFlowTest.Smoke、VisionDecision.Smoke、Avalonia.Smoke --vision-only;新的草稿验证器对本地装盒标准视频真实回放通过4/4。 完整生成端到端实测在Agent接口第5轮返回HTTP 503,未宣称通过。全量Avalonia.Smoke既有time_elapsed过程图用例失败,专项回归通过。 --- .../Training/SopDraftVideoVerifier.cs | 90 +++++++++++++ .../Training/SopVideoFlowTest.cs | 9 +- .../VVMs/RecipeEditor/EditorNodes.cs | 4 +- .../VVMs/RecipeEditor/RecipeEditor.axaml | 12 +- .../SopAgentVerificationViewModel.cs | 77 ++++++++++++ .../RecipeEditor/SopVideoAgentViewModel.cs | 98 ++++++++++----- .../Sop/SopEngine.BufferedEvidence.cs | 3 +- .../Sop/SopEngine.cs | 3 +- .../SopAgent/SopDraftVerification.cs | 47 +++++++ .../SopAgent/SopStepDesignPrompt.cs | 2 +- .../SopAgent/SopVideoAgentClient.cs | 50 +++++++- .../SopAgent/SopVideoDraftClient.cs | 45 ++++++- tests/Avalonia.Smoke/Program.cs | 1 + tests/SopVideoAgent.Smoke/Program.cs | 1 + .../VerificationLoopChecks.cs | 119 ++++++++++++++++++ .../AgentDraftVerificationChecks.cs | 109 ++++++++++++++++ .../LiveVideoFlowChecks.cs | 75 +++++++++++ tests/VideoFlowTest.Smoke/Program.cs | 1 + 18 files changed, 700 insertions(+), 46 deletions(-) create mode 100644 src/SopHelper.Avalonia/Training/SopDraftVideoVerifier.cs create mode 100644 src/SopHelper.Avalonia/VVMs/RecipeEditor/SopAgentVerificationViewModel.cs create mode 100644 src/SopHelper.CrossPlatform.Engine/SopAgent/SopDraftVerification.cs create mode 100644 tests/SopVideoAgent.Smoke/VerificationLoopChecks.cs create mode 100644 tests/VideoFlowTest.Smoke/AgentDraftVerificationChecks.cs diff --git a/src/SopHelper.Avalonia/Training/SopDraftVideoVerifier.cs b/src/SopHelper.Avalonia/Training/SopDraftVideoVerifier.cs new file mode 100644 index 0000000..88396a4 --- /dev/null +++ b/src/SopHelper.Avalonia/Training/SopDraftVideoVerifier.cs @@ -0,0 +1,90 @@ +using Avalonia.Threading; +using OpenCvSharp; +using SopHelper.Engine.Sop; +using SopHelper.Engine.SopAgent; + +namespace SopHelper.Training; + +public interface ISopDraftVideoVerifier +{ + Task VerifyAsync(SopVideoDraft draft, SopVideoSample video, SopConfig template, + IProgress? progress, CancellationToken token); +} + +/// 通过与“测试流程”相同的播放器和检测引擎回放草稿,不改写配方或生产统计。 +public sealed class SopDraftVideoVerifier : ISopDraftVideoVerifier +{ + public async Task VerifyAsync(SopVideoDraft draft, SopVideoSample video, SopConfig template, + IProgress? progress, CancellationToken token) + { + if (Dispatcher.UIThread.CheckAccess()) return await VerifyCoreAsync(draft, video, template, progress, token); + return await Dispatcher.UIThread.InvokeAsync(() => VerifyCoreAsync(draft, video, template, progress, token)); + } + + private static async Task VerifyCoreAsync(SopVideoDraft draft, SopVideoSample video, SopConfig template, + IProgress? progress, CancellationToken token) + { + var config = SopDraftConfigBuilder.Build(template, draft); + var fingerprint = SopDraftConfigBuilder.Fingerprint(config, video); + using var budget = CancellationTokenSource.CreateLinkedTokenSource(token); + budget.CancelAfter(TimeSpan.FromSeconds(video.DurationSeconds + 210)); + var images = new System.Collections.Concurrent.ConcurrentDictionary(); + var playback = new RecordedVideoPlayback(); + using var test = new SopVideoFlowTest(config); + string? failure = null; + var infrastructureFailure = false; + double fps = 0; + test.DecisionSubmitted += request => + { + var step = config.Sop.Steps[request.StepId - 1]; + var options = step.AiConfirmation ?? new(); + var scope = VisionImageMode.Resolve(options) == VisionImageMode.FixedRegion ? options.ImageRegion : VisionImageMode.Resolve(options); + images[request.StepId] = new(request.StepId, request.FrameId, fps > 0 ? request.FrameId / fps : 0, + scope, null, null, "等待判定", request.Jpeg); + }; + test.DecisionEvaluated += (request, decision, accepted) => + { + if (images.TryGetValue(request.StepId, out var image)) + images[request.StepId] = image with { Confidence = decision.Confidence, Accepted = accepted, Reason = decision.Reason }; + if (decision.Reason.Contains("接口请求失败") || decision.Reason.Contains("超时或已取消")) infrastructureFailure = true; + }; + test.Changed += (status, _) => progress?.Report("标准视频回测:" + status); + playback.Failed += message => failure = message; + try + { + EnsureSource(); + fps = await Task.Run(() => { using var source = new VideoCapture(video.Path); return source.Fps; }, budget.Token); + await Task.Run(test.LoadModels, budget.Token); + test.Start(); + playback.FrameAnalyzer = test.AnalyzeFrame; + await playback.PlayAsync(video.Path); + while (!playback.IsAtEnd && failure is null) await Task.Delay(100, budget.Token); + if (failure is not null) throw new IOException(failure); + await test.FinishPendingDecisionAsync(budget.Token); + EnsureSource(); + return new(test.IsCompleted, test.Finish("标准视频草稿回测结束"), test.StepResults, + images.Values.OrderBy(i => i.StepId).ToArray(), fingerprint, infrastructureFailure); + } + catch (OperationCanceledException) when (!token.IsCancellationRequested) + { + return new(false, "标准视频回测超时,请检查视觉服务响应速度", test.StepResults, images.Values.ToArray(), fingerprint, true); + } + catch (Exception error) when (error is not OperationCanceledException) + { + return new(false, "标准视频回测无法完成:" + error.Message, test.StepResults, images.Values.ToArray(), fingerprint, true); + } + finally + { + await playback.StopAsync(); + playback.FrameAnalyzer = null; + } + + void EnsureSource() + { + budget.Token.ThrowIfCancellationRequested(); + var current = new FileInfo(video.Path); + if (!current.Exists || video.SourceLength > 0 && (current.Length != video.SourceLength || current.LastWriteTimeUtc != video.SourceModifiedUtc)) + throw new IOException("回测期间标准视频已变化,请重新生成"); + } + } +} diff --git a/src/SopHelper.Avalonia/Training/SopVideoFlowTest.cs b/src/SopHelper.Avalonia/Training/SopVideoFlowTest.cs index 5b17806..3b20350 100644 --- a/src/SopHelper.Avalonia/Training/SopVideoFlowTest.cs +++ b/src/SopHelper.Avalonia/Training/SopVideoFlowTest.cs @@ -21,6 +21,12 @@ public sealed class SopVideoFlowTest : IDisposable private readonly Dictionary _lastAiFailures = []; public event Action? Changed; public event Action? DecisionImageCaptured; + public event Action? DecisionSubmitted; + public event Action? DecisionEvaluated; + public IReadOnlyList StepResults + { + get { lock (_gate) return _steps.Values.OrderBy(s => s.StepId).Select(s => new SopHelper.Engine.SopAgent.SopDraftVerificationStep(s.StepId, s.Name, s.IsMet, s.Message)).ToArray(); } + } public bool IsCompleted => _engine.State == EngineState.Completed; public IReadOnlyDictionary Regions => _engine.Config.Sop.Regions; public string Details @@ -40,7 +46,8 @@ public sealed class SopVideoFlowTest : IDisposable snapshot.Sop.Model = null; _stride = Math.Max(1, snapshot.Sop.Settings.FrameStride); _engine.VisionDecisionClient = visionClient ?? new ConfiguredVisionDecisionClient(VisionHttp); - _engine.DecisionSubmitted += request => DecisionImageCaptured?.Invoke(request.Jpeg, request.FrameId); + _engine.DecisionSubmitted += request => { DecisionImageCaptured?.Invoke(request.Jpeg, request.FrameId); DecisionSubmitted?.Invoke(request); }; + _engine.DecisionEvaluated += (request, result, accepted) => DecisionEvaluated?.Invoke(request, result, accepted); _engine.StepSnapshotChanged += step => { if (_steps.TryGetValue(step.StepId, out var previous) && previous.IsMet && !step.IsMet) return; diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs b/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs index c91f414..7c65636 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/EditorNodes.cs @@ -261,11 +261,11 @@ public class RecipeNode : EditorNode } /// 加区域 - public RegionNode AddRegion() + public RegionNode AddRegion(string? key = null) { var node = new RegionNode { - Key = $"region{Regions.Count + 1}", + Key = key ?? $"region{Regions.Count + 1}", Name = "新区域", X1 = 100, Y1 = 100, diff --git a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml index e733726..e4075fc 100644 --- a/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml +++ b/src/SopHelper.Avalonia/VVMs/RecipeEditor/RecipeEditor.axaml @@ -496,7 +496,7 @@ - + @@ -512,11 +512,21 @@