分离候选采集与检查预算并分散复检时机

This commit is contained in:
tfarcraw 2026-11-19 01:26:01 +08:00
parent 70c04d9e94
commit be050f8b85
3 changed files with 25 additions and 15 deletions

View File

@ -14,16 +14,17 @@ public partial class SopEngine
ObservationPreRollMs = step.AiConfirmation!.ObservationPreRollMs,
ObservationSampleMs = step.AiConfirmation.ObservationSampleMs,
ObservationMinMs = step.AiConfirmation.ObservationMinMs,
ObservationMaxMs = Math.Max(step.AiConfirmation.ObservationMaxMs, step.AiConfirmation.CheckWindowMs),
// 候选证据可能早于前序判定;采集上限独立于本步检查预算,仍受帧数和字节预算约束。
ObservationMaxMs = 60000,
ObservationMaxFrames = step.AiConfirmation.ObservationMaxFrames,
});
public bool Triggered, SawQuiet;
public bool Triggered, SawQuiet, BudgetStarted;
public long LastFrame = -1, SentFrame = -1, StartedAt, LastSubmittedAt, FinishedAt;
public double TriggerSeconds, LatestSeconds, LastMotionSeconds;
public double BudgetSeconds, LatestSeconds, LastMotionSeconds;
public int Attempts, SentVersion;
public Dictionary<HandType, (double X, double Y)> Anchors = [];
public string Reason = "尚未获得检查结果";
public double ElapsedMs => Math.Max((LatestSeconds - TriggerSeconds) * 1000, Environment.TickCount64 - StartedAt);
public double ElapsedMs => BudgetStarted ? Math.Max((LatestSeconds - BudgetSeconds) * 1000, Environment.TickCount64 - StartedAt) : 0;
}
private ContinuousCheck EnsureContinuousStep(int index, StepDef step, CaptureGate gate)
{
@ -67,9 +68,9 @@ public partial class SopEngine
var trigger = EvaluateCaptureGate(state.Step, frame, state.Gate, "continuous");
if (!state.Triggered && trigger.IsMet)
{
state.Triggered = true; state.TriggerSeconds = seconds; state.StartedAt = Environment.TickCount64;
state.Triggered = true;
state.LastMotionSeconds = seconds; state.Window.Open(seconds);
Log("info", $"步骤 {state.Step.Id} 操作触发,打开 {state.Step.AiConfirmation!.CheckWindowMs}ms 检查窗口");
Log("info", $"步骤 {state.Step.Id} 操作事件触发,开始采集候选证据;轮到本步后限时检查");
// 跟踪立即向后推进,不等检查结束或模型返回;同一次区域停留仍受 RequireRelease 保护。
if (_nextCaptureIndex == index) ArmFollowingStep(frame, state.Step);
}
@ -91,6 +92,11 @@ public partial class SopEngine
var state = EnsureContinuousStep(_index, step, gate);
ObserveContinuousStep(_index, state, frame, capture, crop);
if (!state.Triggered) return new() { Message = "等待操作事件,保留触发前画面" };
if (!state.BudgetStarted)
{
state.BudgetStarted = true; state.BudgetSeconds = state.LatestSeconds; state.StartedAt = Environment.TickCount64;
Log("info", $"步骤 {step.Id} 开始 {options.CheckWindowMs}ms 限时检查,已保留先前连续操作证据");
}
var expired = state.ElapsedMs >= options.CheckWindowMs;
if (_pendingDecision is { } pending && pending.Task.IsCompleted)
{
@ -117,6 +123,7 @@ public partial class SopEngine
}
if (expired)
{
if (_pendingDecision is not null) state.Reason = "已发送的检查未在预算内返回,结果仍未确认";
InvalidateDecision(discardBuffered: false);
return new() { IsSkipped = true, Message = $"检查窗口到期,{state.Attempts} 次送检未确认通过,已跳过并继续后续步骤:{state.Reason}" };
}
@ -127,9 +134,11 @@ public partial class SopEngine
if (state.Window.Error is { } error) { state.Reason = error; return new() { Message = error }; }
if (state.Attempts >= options.MaxChecks) return new() { Message = $"已检查 {state.Attempts} 次仍未通过,窗口结束后继续后续步骤:{state.Reason}" };
var quiet = (state.LatestSeconds - state.LastMotionSeconds) * 1000 >= options.CheckSettleMs;
var interval = Math.Max(500, options.RetryDelayMs);
// 把有限次数分布到窗口内,避免一开始密集失败就耗尽全部机会。
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 timeToCheck = state.Attempts == 0 ? quiet || sinceRequest >= interval : sinceRequest >= interval;
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();
if (!timeToCheck) return new() { Message = $"等待下一次检查时机(已送检 {state.Attempts}/{options.MaxChecks} 次)" };

View File

@ -31,15 +31,16 @@ internal static class SequenceTrialChecks
test.DecisionSubmitted += request =>
{
var number = ++sequence;
foreach (var frame in request.Frames ?? [])
var collected = request.Frames ?? [new VisionEvidenceFrame(request.FrameId, request.CapturedAt, request.Jpeg, request.SourceSeconds)];
foreach (var frame in collected)
{
var file = Path.Combine(reportPath, $"frame-{frame.FrameId:D6}.jpg");
if (!File.Exists(file)) File.WriteAllBytes(file, frame.Jpeg);
}
var frames = request.Frames?.Select(f => new { f.FrameId, f.SourceSeconds, Bytes = f.Jpeg.Length }).ToArray();
var frames = collected.Select(f => new { f.FrameId, f.SourceSeconds, Bytes = f.Jpeg.Length }).ToArray();
requests.Add(new { Number = number, WallSeconds = watch.Elapsed.TotalSeconds, CurrentVideoSeconds = latestSeconds,
request.StepId, request.EvidenceMode, Frames = frames });
Console.WriteLine($"提交 {number}: 视频 {latestSeconds:F2}s,{frames?.Length} 张真实过程画面");
Console.WriteLine($"提交 {number}: 步骤 {request.StepId},视频 {latestSeconds:F2}s,{frames.Length} 张真实画面");
};
test.DecisionEvaluated += (request, result, accepted) =>
{

View File

@ -55,14 +55,14 @@ internal static class ContinuousCheckTests
Check(retry.Request.StepId == 1 && retry.Request.FrameId > first.Request.FrameId, "窗口内重试应使用新证据");
retry.Result.SetResult(new(false, .9, "仍未完成"));
await Until(() => { engine.ProcessFrame(Frame(5, .6, 120)); return evaluated == 2; });
Feed(6, 2.1, 120);
Feed(6, 3.1, 120);
Check(engine.CurrentStep?.Id == 2 && snapshots.Any(s => s.StepId == 1 && s.IsSkipped && !s.IsMet)
&& alarms == 1, "限时多次未通过必须标记跳过、告警并继续下一步");
engine.ProcessFrame(Frame(6, 2.1, 120)); await Until(() => client.Calls.Count == 3);
engine.ProcessFrame(Frame(6, 3.1, 120)); await Until(() => client.Calls.Count == 3);
var next = client.Calls.ElementAt(2);
Check(next.Request.StepId == 2 && next.Request.FrameId is 5 or 6, "下一步须保留第一步等待期间取得的连续动作证据");
Check(next.Request.StepId == 2 && next.Request.FrameId is 5 or 6, "下一步须保留等待期间的连续动作证据,提前触发不能消耗轮到本步后的检查预算");
next.Result.SetResult(new(true, .99, "第二步结果可见"));
await Until(() => { engine.ProcessFrame(Frame(6, 2.1, 120)); return engine.State == EngineState.Completed; });
await Until(() => { engine.ProcessFrame(Frame(6, 3.1, 120)); return engine.State == EngineState.Completed; });
Check(passed == false && !engine.RoundPassed && engine.Statistics.CompletedCount == 0 && engine.SkippedSteps.Contains(1),
"跳过不能算合格完成或增加合格产量");
engine.Reset(); Check(engine.SkippedSteps.Count == 0 && !engine.HasPendingDecision, "复位必须清空跳过状态和所有窗口");