限制无关历史补查并保留跨跳过步骤的证据顺序

This commit is contained in:
tfarcraw 2026-11-19 01:47:09 +08:00
parent 3027db91f7
commit 1862aeb352
3 changed files with 20 additions and 6 deletions

View File

@ -4,13 +4,20 @@ namespace SopHelper.Engine.Sop;
public static class InspectionFrameSelector
{
public static VisionEvidenceFrame? Select(IReadOnlyList<VisionEvidenceFrame> candidates,
IReadOnlyDictionary<long, double> inspected, IReadOnlyList<VisionEvidenceFrame> boundaries)
IReadOnlyDictionary<long, double> inspected, IReadOnlyList<VisionEvidenceFrame> boundaries,
double bufferedSpanSeconds = double.PositiveInfinity)
{
var available = candidates.DistinctBy(f => f.FrameId).Where(f => !inspected.ContainsKey(f.FrameId))
.OrderBy(ObservationEvidenceWindow.Time).ToArray();
if (available.Length == 0) return null;
if (inspected.Count == 0)
{
var start = ObservationEvidenceWindow.Time(available[0]);
var end = ObservationEvidenceWindow.Time(available[^1]);
if (end - start >= bufferedSpanSeconds)
return available.MinBy(f => Math.Abs(ObservationEvidenceWindow.Time(f) - (start + end) / 2));
return available.LastOrDefault(f => boundaries.Any(b => b.FrameId == f.FrameId)) ?? available[^1];
}
var first = candidates.Min(ObservationEvidenceWindow.Time);
var last = candidates.Max(ObservationEvidenceWindow.Time);
var times = inspected.Values.Where(t => t >= first && t <= last).Append(first).Append(last).Distinct().Order().ToArray();

View File

@ -5,6 +5,9 @@ namespace SopHelper.Engine.Sop;
public partial class SopEngine
{
private readonly Dictionary<int, ContinuousCheck> _continuousChecks = [];
public event Action<int, IReadOnlyList<VisionEvidenceFrame>>? DecisionCandidatesObserved;
private double PriorCompletedEvidenceTime() => _acceptedEvidenceTimes.Where(p => p.Key < _index)
.Select(p => p.Value).DefaultIfEmpty(double.NegativeInfinity).Max();
private sealed class ContinuousCheck(StepDef step, CaptureGate gate)
{
public StepDef Step = step;
@ -159,8 +162,10 @@ public partial class SopEngine
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 frames = state.Window.Frames.Where(f => !_acceptedEvidenceTimes.TryGetValue(_index - 1, out var prior)
|| ObservationEvidenceWindow.Time(f) >= prior).ToArray();
var prior = PriorCompletedEvidenceTime();
if (options.EvidenceMode == VisionEvidenceMode.SingleFrame && state.Attempts > 0)
prior = Math.Max(prior, state.LatestSeconds - options.CheckWindowMs / 1000d);
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)
{
@ -170,9 +175,9 @@ public partial class SopEngine
var current = CaptureDecision(step, frame, state.Gate.HandRegion, capture, crop, out _, notify: false);
if (current is not null) candidates = candidates.Append(new(frame.FrameId, frame.Timestamp, current.Request.Jpeg, frame.SourceSeconds));
}
var valid = candidates.Where(f => !_acceptedEvidenceTimes.TryGetValue(_index - 1, out var prior)
|| ObservationEvidenceWindow.Time(f) >= prior).ToArray();
var selected = InspectionFrameSelector.Select(valid, state.Inspected, state.Boundaries);
var valid = candidates.Where(f => ObservationEvidenceWindow.Time(f) >= prior).ToArray();
DecisionCandidatesObserved?.Invoke(step.Id, valid);
var selected = InspectionFrameSelector.Select(valid, state.Inspected, state.Boundaries, options.ObservationMinMs / 1000d);
frames = selected is null ? [] : [selected];
}
if (frames.Length == 0 || state.Inspected.ContainsKey(frames[^1].FrameId)

View File

@ -38,6 +38,8 @@ internal static class ContinuousCheckTests
public static async Task RunAsync()
{
var source = Enumerable.Range(0, 7).Select(i => new VisionEvidenceFrame(i, DateTime.UnixEpoch.AddSeconds(i), [(byte)i], i)).ToArray();
Check(InspectionFrameSelector.Select(source, new Dictionary<long, double>(), [source[^1]], 2.4)?.FrameId == 3,
"已缓存较长操作片段的首次检查应覆盖中段,避免只检查动作结束后的尾图");
var coverage = new Dictionary<long, double> { [6] = 6 };
var recovered = InspectionFrameSelector.Select(source, coverage, []);
Check(recovered?.FrameId == 3, "已经检查过较晚帧后,重试须能补查缓存中间帧,不能永远只检查更晚画面");