diff --git a/src/SopHelper.Avalonia/Training/SopDraftReferenceVerifier.cs b/src/SopHelper.Avalonia/Training/SopDraftReferenceVerifier.cs index 925f9d5..1df3766 100644 --- a/src/SopHelper.Avalonia/Training/SopDraftReferenceVerifier.cs +++ b/src/SopHelper.Avalonia/Training/SopDraftReferenceVerifier.cs @@ -22,8 +22,8 @@ public sealed class SopDraftReferenceVerifier(IVisionDecisionClient? vision = nu foreach (var kind in new[] { "completion", "previous_state" }) { token.ThrowIfCancellationRequested(); - var evidence = kind == "completion" ? source.EvidenceFrame : id > 1 ? draft.Steps[id - 2].EvidenceFrame - : video.Frames.FirstOrDefault(f => f.Seconds == 0)?.Index ?? -1; + var evidence = kind == "completion" ? source.EvidenceFrame : source.IncompleteEvidenceFrame ?? (id > 1 ? draft.Steps[id - 2].EvidenceFrame + : video.Frames.FirstOrDefault(f => f.Seconds == 0)?.Index ?? -1); var seconds = evidence < 0 ? 0 : video.Frames[evidence].Seconds; if (options.EvidenceMode == VisionEvidenceMode.Sequence && !client.SupportsSequences) { diff --git a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopStepDesignPrompt.cs b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopStepDesignPrompt.cs index 79c264d..f6046a9 100644 --- a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopStepDesignPrompt.cs +++ b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopStepDesignPrompt.cs @@ -8,7 +8,7 @@ public static class SopStepDesignPrompt 判定方式: - 每步 inspection.evidence_mode 默认 single_frame:适合稳定、清晰且能区分前序的可见结果。D3-lite 只看当前送检图,看不到过程、历史或区外状态;description 必须是可直接验证的静态状态,不能写无依据的“符合要求”。 - inspection_capabilities.sequence=true 时可新选 sequence:适合必须看到状态变化、单帧正反例难区分、结果短暂或易被后续动作遮挡的操作。description 保留关键变化及最终结果,模型检查按序真实画面,返回完成/未完成/证据不足。已有时序步骤保留过程要求。服务不可用时说明能力缺口,不能假装配置成功或改成宽松外观来凑通过。 - - 不机械统一模式。name 简述动作;description 采用最小充分判据:操作主体及关键结果关系。仅工艺明确要求时附加颜色、左右手、部件、质量等细节;用户明确要求必须保留。触发条件不自动成为验收要求;用户未要求时,不能额外要求手离开或整理收尾。手出现或接触不能代替主体完成。operation_requirements 保留最初工艺意图,不能逐轮漂移;相邻步骤必须可区分。 + - 不机械统一模式。name 简述动作;description 采用最小充分判据:操作主体及关键结果关系。仅工艺明确要求时附加颜色、左右手、部件、质量等细节;用户明确要求必须保留。触发条件不自动成为验收要求;用户未要求时,不能额外要求手离开或整理收尾。手出现或接触不能代替主体完成。operation_requirements 保留最初工艺意图,不能逐轮漂移;相邻步骤必须可区分。主体须完整达到目标关系,部分进入、仅开始覆盖或半成品不能算完成,除非用户明确接受该状态。 检查策略 inspection(可省略字段沿用原值): - evidence_mode=single_frame/sequence;check_window_ms=500–60000;max_checks=1–20;check_settle_ms=100–3000且小于窗口;observation_min_ms=200–30000;observation_max_ms>=min且<=60000;observation_max_frames和observation_request_max_frames各3–32。 @@ -23,7 +23,7 @@ public static class SopStepDesignPrompt 回测修订: - 先核对实际送检帧采集时间、范围和内容,再用同服务同裁剪的完成参考及前序未完成对照定位。正例通过而运行失败:检查触发、错过瞬态和选帧;清晰正例也失败:检查判据、范围或服务能力;前序也通过:判据过宽,不得放行。时序参考必须用真实片段,不能用单帧代判。 - 后续动作可能遮住结果,须查看缓存中段/边界,避免反复尾图。单帧无法区分时可按可用能力选时序,保留要求并重做对照及真实回放。参考通过不代表整轮通过;不能降低阈值、删步、更换动作或用跳过掩盖失败。 - - 每步 evidence_frame 引用实际读取的结果画面,时间段包含它。不得从遮挡猜颜色、数量、型号、质量,不因抽样未见断言不存在,也不能编造次数、受力等不可见过程。 + - 每步 evidence_frame 引用实际读取的结果画面,时间段包含它;尽量附 incomplete_evidence_frame,引用尽可能接近完成但仍未完成的真实已读画面,须早于正例,不能编造负例。可用部分完成状态作对照,不能只用相差很大的空白起始状态;反例也通过须修订判据并重新回放。不得从遮挡猜颜色、数量、型号、质量,不因抽样未见断言不存在,也不能编造次数、受力等不可见过程。 - 提交前核对模式可用、证据充分、范围完整、触发合理、相邻可区分、区域有效;仅为具体缺口补看,避免重复读图。note 简述选择依据或缺口;无法解决则明确阻塞,工艺含义不清先澄清。聊天修改同样遵守本规则。 """; } diff --git a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVerificationContext.cs b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVerificationContext.cs index 04e9f4b..8b0536b 100644 --- a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVerificationContext.cs +++ b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVerificationContext.cs @@ -24,7 +24,7 @@ internal static class SopVerificationContext var failed = report is null ? null : FailedStepIds(report); var indices = draft.Steps.Select((step, index) => (step, index)) .Where(x => failed is null || failed.Count == 0 || failed.Contains(x.index + 1) || failed.Contains(x.index) || failed.Contains(x.index + 2)) - .Select(x => x.step.EvidenceFrame) + .SelectMany(x => new[] { x.step.EvidenceFrame, x.step.IncompleteEvidenceFrame ?? -1 }) .Concat(draft.RegionAdjustments.Select(r => r.EvidenceFrame)).ToHashSet(); foreach (var frame in observed.Where(f => indices.Contains(f.Index))) Add($"frame_index={frame.Index}, time={frame.Seconds.ToString("F3", CultureInfo.InvariantCulture)}s", frame.Jpeg); diff --git a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoAgentClient.cs b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoAgentClient.cs index cef2b45..92c2606 100644 --- a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoAgentClient.cs +++ b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoAgentClient.cs @@ -40,6 +40,7 @@ public sealed class SopVideoAgentClient(HttpClient http) : ISopVideoDraftClient var referenceBlockingId = 1; var runtimeBlockingId = 1; var confirmedReferences = new Dictionary(); + var protectedControls = request.VerifyDraft is null ? null : request.CurrentDraft; var maxTurns = request.CheckReference is null ? 12 : 20; var pendingCompression = ""; List? verificationBase = null; @@ -258,6 +259,7 @@ public sealed class SopVideoAgentClient(HttpClient http) : ISopVideoDraftClient if (observed.Count == 0) throw new FormatException("Agent 未读取视频,未接受步骤草稿"); SopVideoDraftClient.Validate(draft, request.Video with { Frames = observed }, request.Regions, request.RegionDefinitions); SopVideoDraftClient.ValidateCapabilities(draft, request); + if (request.VerifyDraft is not null) PreserveIncompleteControls(draft, protectedControls, observed); draft.ObservedFrames = observed; } var current = new FileInfo(request.Video.Path); @@ -269,6 +271,7 @@ public sealed class SopVideoAgentClient(HttpClient http) : ISopVideoDraftClient if (verificationStepNames is not null && !verificationStepNames.SequenceEqual(draft.Steps.Select(s => s.Name))) throw new FormatException("自动回测修订必须保留每步名称和顺序,不能删步、合并或更换工艺来通过;只修订有视频依据的判据、触发、取图和防抖参数"); verificationStepNames ??= draft.Steps.Select(s => s.Name).ToArray(); + protectedControls = JsonSerializer.Deserialize(SopAgentJson.Serialize(draft)); verificationAttempts++; request.Progress?.Report($"标准视频自动回测(第 {verificationAttempts}/3 轮)…"); var report = await verify(draft, request.Video with { Frames = observed }, token).ConfigureAwait(false); @@ -429,6 +432,7 @@ public sealed class SopVideoAgentClient(HttpClient http) : ISopVideoDraftClient if (idToCheck < referenceBlockingId || idToCheck > referenceBlockingId + 1) throw new ArgumentException($"先解决当前阻塞步骤 {referenceBlockingId} 及紧邻下一步,再回放验证;不要无目标检查后续步骤"); SopVideoDraftClient.Validate(candidate, request.Video with { Frames = observed }, request.Regions, request.RegionDefinitions); + PreserveIncompleteControls(candidate, protectedControls, observed); var names = verificationStepNames ?? request.CurrentDraft?.Steps.Select(s => s.Name).ToArray(); if (names is not null && !names.SequenceEqual(candidate.Steps.Select(s => s.Name))) throw new ArgumentException("参考检查须保留当前工艺的步骤名称、数量和顺序"); @@ -487,12 +491,23 @@ public sealed class SopVideoAgentClient(HttpClient http) : ISopVideoDraftClient { var step = draft.Steps[id - 1]; var keys = new[] { step.Region, step.ToRegion, step.ImageRegion }; - return SopAgentJson.Serialize(new { step.Description, step.EvidenceFrame, step.ImageMode, step.ImageRegion, + return SopAgentJson.Serialize(new { step.Description, step.EvidenceFrame, step.IncompleteEvidenceFrame, step.ImageMode, step.ImageRegion, step.Region, step.ToRegion, step.Inspection, PreviousEvidence = id > 1 ? draft.Steps[id - 2].EvidenceFrame : -1, Regions = draft.RegionAdjustments.Where(r => keys.Contains(r.Key, StringComparer.OrdinalIgnoreCase)) .OrderBy(r => r.Key, StringComparer.OrdinalIgnoreCase).Select(r => new { r.Key, r.X1, r.Y1, r.X2, r.Y2 }) }); } + private static void PreserveIncompleteControls(SopVideoDraft candidate, SopVideoDraft? original, IReadOnlyList frames) + { + // 用户在本次聊天首版明确拆分/合并可改变步骤;后续自动修订另受步骤名称及顺序锁定。 + if (original is null || !original.Steps.Select(s => s.Name).SequenceEqual(candidate.Steps.Select(s => s.Name))) return; + for (var index = 0; index < original.Steps.Count; index++) + if (original.Steps[index].IncompleteEvidenceFrame is { } before + && (candidate.Steps[index].IncompleteEvidenceFrame is not { } after + || frames[after].Seconds < frames[before].Seconds)) + throw new FormatException("自动修订不能删除或前移已有未完成边界对照来掩盖误放行;保留工艺要求修订判据、范围或模式,标签含义有疑问时说明原因"); + } + private async Task SummarizeHistoryAsync(object[] history, SopVideoAnalysisRequest request, SopAgentConnection connection, CancellationToken token) { diff --git a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoDraftClient.cs b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoDraftClient.cs index 6a7d6da..7a33a66 100644 --- a/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoDraftClient.cs +++ b/src/SopHelper.CrossPlatform.Engine/SopAgent/SopVideoDraftClient.cs @@ -45,6 +45,7 @@ public sealed class SopDraftStep [JsonPropertyName("to_region")] public string ToRegion { get; set; } = ""; public string Hand { get; set; } = "any"; [JsonPropertyName("evidence_frame")] public int EvidenceFrame { get; set; } = -1; + [JsonPropertyName("incomplete_evidence_frame")] public int? IncompleteEvidenceFrame { get; set; } public string Note { get; set; } = ""; [JsonPropertyName("stable_frames")] public int? StableFrames { get; set; } public float? Tolerance { get; set; } @@ -232,6 +233,9 @@ public sealed class SopVideoDraftClient(HttpClient http) : ISopVideoDraftClient else if (string.IsNullOrWhiteSpace(step.Region)) throw new FormatException($"步骤 {number}:手部触发规则缺少主区域"); var evidence = video.Frames[step.EvidenceFrame]; + if (step.IncompleteEvidenceFrame is { } incomplete && (incomplete < 0 || incomplete >= video.Frames.Count + || video.Frames[incomplete].Seconds >= evidence.Seconds)) + throw new FormatException("未完成对照须引用实际读取且早于完成参考的画面,不能与正例相同或伪造编号"); if (evidence.Seconds < step.StartSeconds - .1 || evidence.Seconds > step.EndSeconds + .1) throw new FormatException($"步骤 {number}:证据画面不在本步骤时间段内"); previousEnd = step.EndSeconds; diff --git a/tests/SopVideoAgent.Smoke/Program.cs b/tests/SopVideoAgent.Smoke/Program.cs index 264da52..5838603 100644 --- a/tests/SopVideoAgent.Smoke/Program.cs +++ b/tests/SopVideoAgent.Smoke/Program.cs @@ -38,7 +38,9 @@ try step=>{step.Method="hand_move";step.FromRegion="";step.ToRegion="";}, step=>{step.Method="hand_leave_region";step.Region="";}, step=>{step.Method="hand_in_region";step.Region="";}, - step=>{step.EndSeconds=1;} + step=>{step.EndSeconds=1;}, + step=>{step.IncompleteEvidenceFrame=step.EvidenceFrame;}, + step=>{step.IncompleteEvidenceFrame=99;} }) { var invalid=JsonSerializer.Deserialize(JsonSerializer.Serialize(draft))!; diff --git a/tests/SopVideoAgent.Smoke/ReferenceToolChecks.cs b/tests/SopVideoAgent.Smoke/ReferenceToolChecks.cs index b375aeb..38c8619 100644 --- a/tests/SopVideoAgent.Smoke/ReferenceToolChecks.cs +++ b/tests/SopVideoAgent.Smoke/ReferenceToolChecks.cs @@ -10,9 +10,13 @@ internal static class ReferenceToolChecks var sample = video with { Frames = SopVideoSampler.ReadFrames(video, [.5, 1.5, 2.5, 3.5], 0, default) }; var current = new SopVideoDraft { Steps = Enumerable.Range(0, 4).Select(i => new SopDraftStep { Name = "保留工艺" + i, Description = "主体与可见完成关系", Region = "work", StartSeconds = i, EndSeconds = i + 1, EvidenceFrame = i }).ToList() }; - foreach (var mode in new[] { "valid", "renamed", "bad_evidence", "extra_argument", "out_of_focus", "advance", "advance_changed" }) + foreach (var mode in new[] { "valid", "renamed", "bad_evidence", "extra_argument", "out_of_focus", "advance", "advance_changed", "drop_negative", "earlier_negative" }) { + if (mode is "drop_negative" or "earlier_negative") current.Steps[2].IncompleteEvidenceFrame = 1; + else current.Steps[2].IncompleteEvidenceFrame = null; var candidate = JsonSerializer.Deserialize(JsonSerializer.Serialize(current))!; + if (mode == "drop_negative") candidate.Steps[2].IncompleteEvidenceFrame = null; + if (mode == "earlier_negative") candidate.Steps[2].IncompleteEvidenceFrame = 0; if (mode == "renamed") candidate.Steps[0].Name = "另一工艺"; if (mode == "bad_evidence") candidate.Steps[0].EvidenceFrame = 99; using var handler = new ProbeHttp(current, candidate, mode); diff --git a/tests/VideoFlowTest.Smoke/AgentBenchmarkChecks.cs b/tests/VideoFlowTest.Smoke/AgentBenchmarkChecks.cs index 3567c9f..d714c42 100644 --- a/tests/VideoFlowTest.Smoke/AgentBenchmarkChecks.cs +++ b/tests/VideoFlowTest.Smoke/AgentBenchmarkChecks.cs @@ -11,7 +11,7 @@ internal static class AgentBenchmarkChecks { public static async Task RunAsync(string configPath, string videoPath, string settingsPath, string reportPath, string? sourceReportPath = null, bool verifyCurrentFirst = false, bool disableThinking = false, string? draftPath = null, int referenceStep = 0, bool replayOnly = false, - string? instructions = null) + string? instructions = null, string? boundaryManifest = null) { Directory.CreateDirectory(reportPath); using var settings = JsonDocument.Parse(File.ReadAllText(settingsPath)); @@ -85,6 +85,26 @@ internal static class AgentBenchmarkChecks SopVideoDraftClient.Validate(draft, video with { Frames = draft.ObservedFrames }, template.Sop.Regions.Keys.ToArray(), template.Sop.Regions); Log("复用同一份生成草稿及原始观察帧,排除重复生成的耗时和差异"); } + if (boundaryManifest is not null) + { + var controls = JsonSerializer.Deserialize(File.ReadAllText(boundaryManifest), + new JsonSerializerOptions { PropertyNameCaseInsensitive = true }) ?? throw new ArgumentException("缺少边界对照清单"); + var frames = draft.ObservedFrames.ToList(); + foreach (var group in controls.Where(c => !c.ExpectedCompleted).GroupBy(c => c.StepId)) + { + var seconds = group.SelectMany(c => c.Times).Max(); + var frame = frames.FirstOrDefault(f => Math.Abs(f.Seconds - seconds) < .0001); + if (frame is null) + { + if (frames.Count >= 60) throw new InvalidOperationException("边界对照超出已读画面预算"); + frame = SopVideoSampler.ReadFrames(video, [seconds], frames.Count, cancellation.Token).Single(); frames.Add(frame); + } + draft.Steps[group.Key - 1].IncompleteEvidenceFrame = frame.Index; + } + draft.ObservedFrames = frames; + SopVideoDraftClient.Validate(draft, video with { Frames = frames }, template.Sop.Regions.Keys.ToArray(), template.Sop.Regions); + Log("已载入显式测试清单中的真实未完成边界作为草稿对照;期望标签不发送给视觉服务"); + } Save("generated-draft.json", draft); Save("observed-frames.json", draft.ObservedFrames.Select(f => new { f.Index, f.Seconds })); foreach (var frame in draft.ObservedFrames) File.WriteAllBytes(Path.Combine(reportPath, $"observed-{frame.Index}.jpg"), frame.Jpeg); diff --git a/tests/VideoFlowTest.Smoke/LiveVideoFlowChecks.cs b/tests/VideoFlowTest.Smoke/LiveVideoFlowChecks.cs index 262e81e..dcbbad5 100644 --- a/tests/VideoFlowTest.Smoke/LiveVideoFlowChecks.cs +++ b/tests/VideoFlowTest.Smoke/LiveVideoFlowChecks.cs @@ -39,7 +39,7 @@ internal static class LiveVideoFlowChecks Value("--agent-settings") ?? throw new ArgumentException("需要 --agent-settings"), Value("--report") ?? throw new ArgumentException("需要 --report"), Value("--reuse-draft"), args.Contains("--verify-current-first"), args.Contains("--disable-thinking"), Value("--draft-file"), - int.Parse(Value("--reference-step") ?? "0"), args.Contains("--replay-only"), Value("--instructions")))); + int.Parse(Value("--reference-step") ?? "0"), args.Contains("--replay-only"), Value("--instructions"), Value("--boundary-manifest")))); return; } if (args.Contains("--agent-generate")) diff --git a/tests/VideoFlowTest.Smoke/ReferenceEvidenceChecks.cs b/tests/VideoFlowTest.Smoke/ReferenceEvidenceChecks.cs index f4589c2..4ee48fe 100644 --- a/tests/VideoFlowTest.Smoke/ReferenceEvidenceChecks.cs +++ b/tests/VideoFlowTest.Smoke/ReferenceEvidenceChecks.cs @@ -22,6 +22,11 @@ internal static class ReferenceEvidenceChecks Check(probe.FrameIds.SequenceEqual(new long[] { 5, 0, 15, 5 }) && checks[0].EvidenceFrame == 0 && checks[0].Seconds == .5, "参考证据索引、视频原始帧号和时间须区分"); Check(before == SopConfigLoader.ToYaml(config), "参考判据检查不修改原配置或推进流程"); + var boundaryVideo = video with { Frames = [.. video.Frames, .. SopVideoSampler.ReadFrames(video, [.2], 2, default)] }; + draft.Steps[1].IncompleteEvidenceFrame = 2; + var boundaryChecks = new SopDraftReferenceVerifier(probe).CheckAsync(config, draft, boundaryVideo, 2, default).GetAwaiter().GetResult(); + Check(probe.FrameIds[^1] == 2 && boundaryChecks[^1].EvidenceFrame == 2, "显式未完成边界须替代较远的前序状态并使用真实帧检查"); + draft.Steps[1].IncompleteEvidenceFrame = null; Check(checks[0].Contradicts && checks[3].Contradicts && !checks[1].Contradicts && !checks[2].Contradicts, "正例失败或前序状态也通过均须标记矛盾,不能把前序误判为本步完成"); config.Sop.Steps[0].AiConfirmation!.ImageMode = "hand";