diff --git a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs index 634099d..bf2cc05 100644 --- a/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs +++ b/src/SopHelper.CrossPlatform.Engine/Sop/SopEngine.ContinuousChecks.cs @@ -21,12 +21,13 @@ public partial class SopEngine ObservationMaxMs = 60000, ObservationMaxFrames = step.AiConfirmation.ObservationMaxFrames, }); - public bool Triggered, SawQuiet, BudgetStarted, StoredBoundary, StoredOutside, FollowCurrentResult; + public bool Triggered, SawQuiet, BudgetStarted, StoredBoundary, StoredOutside, FollowCurrentResult, GateWasMet; public double? OutsideSince; public List Boundaries = []; public Dictionary Inspected = []; public long LastFrame = -1, SentFrame = -1, StartedAt, LastSubmittedAt, FinishedAt; public double BudgetSeconds, LatestSeconds, LastMotionSeconds; + public double LatestTriggerSeconds = double.NegativeInfinity, LastSubmittedSourceSeconds = double.NegativeInfinity; public int Attempts, SentVersion; public Dictionary Anchors = []; public string Reason = "尚未获得检查结果"; @@ -73,6 +74,8 @@ public partial class SopEngine else if ((seconds - state.LastMotionSeconds) * 1000 >= state.Step.AiConfirmation!.CheckSettleMs) state.SawQuiet = true; var trigger = EvaluateCaptureGate(state.Step, frame, state.Gate, "continuous"); + if (trigger.IsMet && !state.GateWasMet) state.LatestTriggerSeconds = seconds; + state.GateWasMet = trigger.IsMet; if (!state.Triggered && trigger.IsMet) { state.Triggered = true; @@ -115,6 +118,7 @@ public partial class SopEngine var state = EnsureContinuousStep(_index, step, gate); ObserveContinuousStep(_index, state, frame, capture, crop); if (!state.Triggered) return new() { Message = "等待操作事件,保留触发前画面" }; + var prior = PriorCompletedEvidenceTime(); if (!state.BudgetStarted) { state.BudgetStarted = true; state.BudgetSeconds = state.LatestSeconds; state.StartedAt = Environment.TickCount64; @@ -157,6 +161,12 @@ public partial class SopEngine return new() { IsSkipped = true, Message = $"检查窗口到期,{state.Attempts} 次送检未确认通过,已跳过并继续后续步骤:{state.Reason}" }; } if (_pendingDecision is not null) return new() { Message = $"第 {state.Attempts} 次检查中,后续操作持续采集;到期自动继续" }; + var discrete = step.Detection?.Method.ToLowerInvariant() is ConditionMethod.HandLeaveRegion or ConditionMethod.HandMove; + if (discrete && state.LatestTriggerSeconds < prior) + { + state.Reason = "已有离开或跨区事件早于前序结果,等待本步新的操作事件"; + return new() { Message = state.Reason }; + } var client = VisionDecisionClient; if (client is null || options.EvidenceMode == VisionEvidenceMode.Sequence && !client.SupportsSequences) { state.Reason = "检查服务未配置或不支持所需证据"; return new() { Message = state.Reason + ";窗口到期后记录未通过并继续" }; } @@ -167,8 +177,11 @@ public partial class SopEngine var interval = Math.Max(Math.Max(500, options.RetryDelayMs), options.CheckWindowMs / options.MaxChecks); var sinceRequest = state.Attempts == 0 ? state.ElapsedMs : Environment.TickCount64 - state.LastSubmittedAt; var firstWait = Math.Min(interval, Math.Max(500, options.CheckSettleMs)); - var timeToCheck = state.Attempts == 0 ? quiet || sinceRequest >= firstWait : sinceRequest >= interval; - var prior = PriorCompletedEvidenceTime(); + var freshEvent = discrete && state.LatestTriggerSeconds > state.LastSubmittedSourceSeconds + && sinceRequest >= Math.Max(500, options.RetryDelayMs); + var timeToCheck = state.Attempts == 0 ? quiet || sinceRequest >= firstWait : sinceRequest >= interval || freshEvent; + if (discrete && options.EvidenceMode == VisionEvidenceMode.SingleFrame) + prior = Math.Max(prior, state.LatestTriggerSeconds); var frames = state.Window.Frames.Where(f => ObservationEvidenceWindow.Time(f) >= prior).ToArray(); if (!timeToCheck) return new() { Message = $"等待下一次检查时机(已送检 {state.Attempts}/{options.MaxChecks} 次)" }; if (options.EvidenceMode == VisionEvidenceMode.SingleFrame) @@ -204,6 +217,7 @@ public partial class SopEngine var remaining = Math.Max(1, options.CheckWindowMs - state.ElapsedMs); var cancellation = new CancellationTokenSource(TimeSpan.FromMilliseconds(Math.Min(options.TimeoutSeconds * 1000d, remaining))); state.Attempts++; state.SentVersion = state.Window.Version; state.SentFrame = last.FrameId; state.LastSubmittedAt = Environment.TickCount64; + state.LastSubmittedSourceSeconds = state.LatestSeconds; state.Inspected[last.FrameId] = ObservationEvidenceWindow.Time(last); state.FinishedAt = long.MaxValue; async Task CheckAsync() diff --git a/tests/VisionDecision.Smoke/ContinuousCheckTests.cs b/tests/VisionDecision.Smoke/ContinuousCheckTests.cs index 649484b..6cd4896 100644 --- a/tests/VisionDecision.Smoke/ContinuousCheckTests.cs +++ b/tests/VisionDecision.Smoke/ContinuousCheckTests.cs @@ -135,6 +135,21 @@ internal static class ContinuousCheckTests var effective = temporalClient.Calls.ElementAt(1).Request.Frames!; Check(effective[^1].SourceSeconds - effective[0].SourceSeconds >= 2.4, "首次过程检查必须覆盖配置要求的实际有效片段时长"); temporal.Stop(); + var leaveConfig = Config(); leaveConfig.Sop.Steps[1].Detection!.Method = ConditionMethod.HandLeaveRegion; + var leaveClient = new Client(); var leave = new SopEngine { VisionDecisionClient = leaveClient }; + leave.Load(leaveConfig); leave.Start(); + void LeaveFeed(int id, double seconds, int x) => leave.ProcessFrame(Frame(id, seconds, x), () => [(byte)id]); + LeaveFeed(0, 0, 10); LeaveFeed(1, .1, 120); LeaveFeed(2, .3, 10); LeaveFeed(3, .5, 10); + await Until(() => leaveClient.Calls.Count == 1); + leaveClient.Calls.ElementAt(0).Result.SetResult(new(true, .99, "前序结果")); + await Until(() => { leave.ProcessFrame(Frame(3, .5, 10)); return leave.CurrentStep?.Id == 2; }); + LeaveFeed(4, .7, 10); + Check(leaveClient.Calls.Count == 1, "离散动作不能复用早于前序完成的旧离开事件"); + LeaveFeed(5, .9, 120); LeaveFeed(6, 1.1, 10); LeaveFeed(7, 1.3, 10); + await Until(() => leaveClient.Calls.Count == 2); + Check(leaveClient.Calls.ElementAt(1).Request.SourceSeconds >= 1.1, + "必须用前序结果之后新发生的离开事件触发检查,已采集的后续证据仍须保留"); + leave.Stop(); Console.WriteLine("PASS: 停稳触发、新帧重试、限时跳过、后续动作持续取证、挂起硬截止、迟到结果隔离和非合格整轮统计"); } }