86 lines
6.2 KiB
C#
86 lines
6.2 KiB
C#
using System.Net;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using SopHelper.Engine.SopAgent;
|
|
|
|
internal static class DirectDraftVerificationChecks
|
|
{
|
|
public static async Task RunAsync(SopVideoSample video)
|
|
{
|
|
var frames = SopVideoSampler.ReadFrames(video, [1], 0, default);
|
|
var draft = new SopVideoDraft { Steps = [new() { Name = "当前步骤", Description = "当前手改判据", Region = "work", StartSeconds = 0, EndSeconds = 4, EvidenceFrame = 0 }] };
|
|
foreach (var mode in new[] { "pass", "repair", "never", "infrastructure", "cancel" })
|
|
{
|
|
using var handler = new DraftHttp(draft);
|
|
using var http = new HttpClient(handler);
|
|
using var cancellation = new CancellationTokenSource();
|
|
var attempts = 0;
|
|
var prompts = new List<SopAgentTrace>();
|
|
var request = new SopVideoAnalysisRequest("实测", "保留用户工艺", video with { Frames = frames }, ["work"],
|
|
UserMessage: "按当前草稿回测", CurrentDraft: draft, VerifyCurrentDraftFirst: true,
|
|
Conversation: mode == "pass" ? Enumerable.Range(0, 10).Select(i => new SopAgentChatTurn(i % 2 == 0 ? "user" : "assistant", "历史" + i)).ToArray() : null,
|
|
Trace: prompts.Add, VerifyDraft: (current, _, token) =>
|
|
{
|
|
attempts++;
|
|
if (mode == "cancel") { cancellation.Cancel(); token.ThrowIfCancellationRequested(); }
|
|
if (attempts == 1 && current.Steps[0].Description != "当前手改判据") throw new Exception("首次必须直接验证当前草稿");
|
|
var passed = mode == "pass" || mode == "repair" && attempts == 2;
|
|
return Task.FromResult(new SopDraftVerification(passed, "真实失败反馈" + attempts,
|
|
[new(1, current.Steps[0].Name, passed, passed ? "通过" : "触发过早")],
|
|
[new(1, 10, 1, "work", passed ? .99 : .1, passed, "真实送检结果", frames[0].Jpeg)], InfrastructureFailure: mode == "infrastructure"));
|
|
});
|
|
try
|
|
{
|
|
var result = await new SopVideoAgentClient(http).ChatAsync(request,
|
|
new("https://direct.invalid/v1", "test", "", ContextLength: 32768, MaxTokens: 2048), cancellation.Token);
|
|
Check(mode != "cancel" && result.Draft?.Verification?.Passed == (mode is "pass" or "repair"), "完成状态只能来自真实回测");
|
|
Check(attempts == (mode == "repair" ? 2 : mode == "never" ? 3 : 1), "直接回测也必须遵守三轮上限");
|
|
}
|
|
catch (OperationCanceledException) when (mode == "cancel") { }
|
|
var expected = mode == "repair" ? 1 : mode == "never" ? 2 : 0;
|
|
Check(handler.Bodies.Count == expected && prompts.Count(t => t.Kind == "model_request") == expected,
|
|
"通过、服务故障或取消不请求模型,失败后才请求修订且不产生伪模型日志");
|
|
if (expected > 0)
|
|
{
|
|
using var body = JsonDocument.Parse(handler.Bodies[0]);
|
|
Check(body.RootElement.GetProperty("messages").EnumerateArray().Any(m => m.GetProperty("content").ToString().Contains("verification_feedback")),
|
|
"首个真正的模型请求就必须包含实际失败反馈");
|
|
}
|
|
}
|
|
var budgetFrames = SopVideoSampler.ReadFrames(video, [0, 1], 0, default);
|
|
var budgetDraft = new SopVideoDraft { Steps = [new() { Name = "预算校准", Description = "主体完整达到目标状态",
|
|
StartSeconds = 0, EndSeconds = 4, EvidenceFrame = 1, IncompleteEvidenceFrame = 0, Region = "work", ImageMode = "full_frame",
|
|
Inspection = new() { EvidenceMode = "sequence", CheckWindowMs = 12000 } }] };
|
|
using (var handler = new DraftHttp(budgetDraft))
|
|
using (var http = new HttpClient(handler))
|
|
{
|
|
var attempts = 0;
|
|
var result = await new SopVideoAgentClient(http).ChatAsync(new("预算测试", "保留判据和对照", video with { Frames = budgetFrames }, ["work"],
|
|
UserMessage: "回测并修订", CurrentDraft: budgetDraft, VerifyCurrentDraftFirst: true, SequenceAvailable: true,
|
|
VerifyDraft: (current, _, _) =>
|
|
{
|
|
attempts++;
|
|
Check(current.Steps[0].Description == budgetDraft.Steps[0].Description && current.Steps[0].IncompleteEvidenceFrame == 0,
|
|
"预算校准不能改变工艺判据或未完成对照");
|
|
if (attempts == 2) Check(current.Steps[0].Inspection!.CheckWindowMs == 32900, "候选窗口须覆盖真实单请求预算及采集余量");
|
|
return Task.FromResult(new SopDraftVerification(attempts == 2, "实际服务预算反馈", [new(1, "预算校准", attempts == 2, "结果")], [],
|
|
InfrastructureFailure: attempts == 1, BudgetHints: attempts == 1 ? [new(1, 12000, 30000, 32900)] : null));
|
|
}), new("https://budget.invalid/v1", "model", "", ContextLength: 32768, MaxTokens: 2048), default);
|
|
Check(attempts == 2 && result.Draft?.Verification?.Passed == true && handler.Bodies.Count == 0,
|
|
"窗口提前截断请求时仅调整预算并真实复测,无须模型改写判据或产生伪请求日志");
|
|
}
|
|
Console.WriteLine("PASS: 直接回测手改草稿、通过零模型请求、预算不匹配无语义重写复测、三轮上限、基础设施故障与取消保护");
|
|
}
|
|
private static void Check(bool value, string message) { if (!value) throw new Exception(message); }
|
|
private sealed class DraftHttp(SopVideoDraft draft) : HttpMessageHandler
|
|
{
|
|
public List<string> Bodies { get; } = [];
|
|
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
|
|
{
|
|
Bodies.Add(await request.Content!.ReadAsStringAsync(token));
|
|
return new(HttpStatusCode.OK) { Content = new StringContent(JsonSerializer.Serialize(new
|
|
{ choices = new[] { new { finish_reason = "stop", message = new { content = JsonSerializer.Serialize(new { reply = "根据反馈修订", draft }) } } } }), Encoding.UTF8, "application/json") };
|
|
}
|
|
}
|
|
}
|