🐛 Freeze acceptance trace cutoff with evidence

This commit is contained in:
d-robotics 2026-11-19 14:24:12 +08:00
parent 5678afcd55
commit a6b2325862
3 changed files with 117 additions and 8 deletions

View File

@ -2639,6 +2639,7 @@ func parseISOTime(value string) time.Time {
}
const fanyuV5PrePackageAcceptanceStageID = "prepackage_acceptance"
const acceptanceAssembledAtKey = "assembledAt"
func fanyuV5PrePackageAcceptanceBatchID(sourceBatchID string) string {
return "human-batch-fanyu-v5-prepackage-acceptance-" + strings.TrimSpace(sourceBatchID)
@ -2894,6 +2895,11 @@ func (server *server) ensureFanyuV5PrePackageAcceptance(ctx context.Context, sou
})
}
now := nowISO()
payload := map[string]any{
"sourceBatchId": source.ID, "items": aggregate, "statistics": statistics,
"strictThreeRole": domain.IsFanyuV58ThreeRoleHumanLoop(source.Stages),
acceptanceAssembledAtKey: now,
}
if acceptanceItem == nil {
supplierID := items[0].SupplierID
legacyAssignment := items[0].LegacyAssignment
@ -2907,13 +2913,13 @@ func (server *server) ensureFanyuV5PrePackageAcceptance(ctx context.Context, sou
ProcessingProfile: source.ProcessingProfile,
ID: acceptanceID, OrganizationID: source.OrganizationID, PluginID: source.PluginID, BatchID: batchID,
ProjectID: source.ProjectID, WorkflowID: source.WorkflowID, RunID: source.RunID, GateNodeID: acceptanceBatch.GateNodeID,
Subject: domain.HumanSubjectRef{ItemKey: source.ID, MediaType: "application/json", Payload: map[string]any{"sourceBatchId": source.ID, "items": aggregate, "statistics": statistics, "strictThreeRole": domain.IsFanyuV58ThreeRoleHumanLoop(source.Stages)}},
Subject: domain.HumanSubjectRef{ItemKey: source.ID, MediaType: "application/json", Payload: payload},
CurrentStageID: fanyuV5PrePackageAcceptanceStageID, Status: domain.HumanItemReady,
SupplierID: supplierID, LegacyAssignment: legacyAssignment,
WorkgroupID: workgroupID, VendorID: vendorID, MaxReworkRounds: source.MaxReworkRounds, Version: 1, CreatedAt: now, UpdatedAt: now,
}
} else {
acceptanceItem.Subject.Payload = map[string]any{"sourceBatchId": source.ID, "items": aggregate, "statistics": statistics, "strictThreeRole": domain.IsFanyuV58ThreeRoleHumanLoop(source.Stages)}
acceptanceItem.Subject.Payload = payload
acceptanceItem.Status = domain.HumanItemReady
acceptanceItem.AssignedUserID = ""
acceptanceItem.ClaimedAt = ""

View File

@ -177,12 +177,15 @@ func (s *server) addAcceptanceSourceTrace(ctx context.Context, item domain.Human
if err != nil || len(sources) == 0 {
return err
}
// Each assembly (including reassembly after a return) records a created
// event. Claim and decision timestamps can be later than the frozen data.
cutoff := parseISOTime(item.CreatedAt)
for _, event := range events {
if at := parseISOTime(event.CreatedAt); event.EventType == "created" && at.After(cutoff) {
cutoff = at
// The cutoff is saved with the evidence. The separate audit event can fail
// after the evidence is already persisted.
cutoff := parseISOTime(stringValue(item.Subject.Payload[acceptanceAssembledAtKey]))
if cutoff.IsZero() {
cutoff = parseISOTime(item.CreatedAt)
for _, event := range events {
if at := parseISOTime(event.CreatedAt); event.EventType == "created" && at.After(cutoff) {
cutoff = at
}
}
}
if cutoff.IsZero() {

View File

@ -9,6 +9,7 @@ import (
"net/http"
"strings"
"testing"
"time"
)
func TestReviewTraceExistingEvidenceAndMissingHistory(t *testing.T) {
@ -186,3 +187,102 @@ func TestReviewTraceAcceptanceProjectsOnlySelectedSource(t *testing.T) {
}
}
}
type acceptanceAssemblyEventFailure struct {
store.Store
itemID string
err error
}
func (s acceptanceAssemblyEventFailure) AppendHumanEvent(ctx context.Context, event domain.HumanEventRecord) error {
if event.ItemID == s.itemID && event.EventType == "created" {
return s.err
}
return s.Store.AppendHumanEvent(ctx, event)
}
func TestReviewTraceAcceptanceReassemblySurvivesMissingCreatedEvent(t *testing.T) {
data, handler, quality, acceptance, source, aggregate := acceptanceProjectionFixture(t)
ctx := t.Context()
must := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
batch, err := data.GetHumanBatch(ctx, source.BatchID)
must(err)
task, err := data.GetAnnotationTask(ctx, source.LegacyTaskID)
must(err)
path := "/api/human-items/" + aggregate.ID + "/review-trace"
read := func() reviewTrace {
t.Helper()
res := acceptanceProjectionRequest(handler, http.MethodGet, path, acceptance, nil)
var trace reviewTrace
if res.Code != http.StatusOK || json.Unmarshal(res.Body.Bytes(), &trace) != nil {
t.Fatalf("trace status=%d body=%s", res.Code, res.Body.String())
}
return trace
}
add := func(id, kind, at string, revision int) {
t.Helper()
must(data.AppendHumanEvent(ctx, domain.HumanEventRecord{
ID: id, ItemID: source.ID, OrganizationID: source.OrganizationID, EventType: kind,
ActorUserID: quality.ID, CreatedAt: at, Payload: map[string]any{"annotationRevision": revision},
}))
}
add("source-created", "created", "2026-01-01T00:00:00Z", 0)
add("source-original-submit", "combined_submitted", "2026-01-02T00:00:00Z", 1)
add("source-original-approval", "combined_quality_approved", "2026-01-03T00:00:00Z", 1)
if original := read(); original.Revision != 1 || len(original.Events) != 2 {
t.Fatalf("unexpected original trace: %+v", original)
}
aggregate.Status = domain.HumanItemRejected
must(data.UpsertHumanItem(ctx, *aggregate))
task.CurrentRevision = 2
must(data.UpsertAnnotationTask(ctx, *task))
source.Subject.Payload = map[string]any{"materialization": map[string]any{"artifactId": "reassembled-artifact", "revision": 2}}
must(data.UpsertHumanItem(ctx, *source))
must(data.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{
ID: "reassembled-revision", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 2,
}))
add("source-rework-rejected", "combined_quality_rejected", nowISO(), 1)
add("source-rework-submit", "combined_submitted", nowISO(), 2)
add("source-rework-approval", "combined_quality_approved", nowISO(), 2)
writeFailure := errors.New("injected failure after saving assembled acceptance payload")
assembly := &server{store: acceptanceAssemblyEventFailure{Store: data, itemID: aggregate.ID, err: writeFailure}}
if _, err := assembly.ensureFanyuV5PrePackageAcceptance(ctx, batch); !errors.Is(err, writeFailure) {
t.Fatalf("expected injected event failure, got %v", err)
}
// The restarted assembly sees a complete ready payload and returns without
// appending another event or overwriting the saved snapshots.
restarted := &server{store: data}
confirmed, err := restarted.ensureFanyuV5PrePackageAcceptance(ctx, batch)
must(err)
if confirmed {
t.Fatal("reassembly must leave the item awaiting acceptance")
}
persisted, err := data.GetHumanItem(ctx, aggregate.ID)
must(err)
if persisted.Status != domain.HumanItemReady || persisted.Version != aggregate.Version+1 {
t.Fatalf("reassembly was not persisted exactly once: %+v", persisted)
}
events, err := data.ListHumanTraceEvents(ctx, aggregate.OrganizationID, aggregate.ID)
must(err)
if len(events) != 1 || events[0].EventType != "created" || events[0].CreatedAt != aggregate.CreatedAt {
t.Fatalf("expected only the original assembly event: %+v", events)
}
later := parseISOTime(persisted.UpdatedAt).Add(time.Minute).Format(time.RFC3339Nano)
add("source-post-assembly", "combined_quality_approved", later, 3)
persisted.UpdatedAt = later
must(data.UpsertHumanItem(ctx, *persisted))
got := read()
if got.Revision != 2 || got.QualityRejections == nil || *got.QualityRejections != 1 ||
len(got.Events) != 5 || got.Events[0].ID != "source-rework-approval" ||
got.Events[1].ID != "source-rework-submit" || got.Events[2].ID != "source-rework-rejected" {
t.Fatalf("trace diverged from the saved assembly: %+v", got)
}
}