136 lines
5.7 KiB
Go
136 lines
5.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// A retry reads the retained server workspace. Client-supplied slices must never
|
|
// replace it, and malformed historic containers must not become empty content.
|
|
func retainedSliceRetryInput(item *domain.HumanItemRecord, version int) (sliceConfirmationInput, combinedAnnotationDraftInput, error) {
|
|
selected, saved, err := retainedSliceRecoveryInput(item, version)
|
|
if err != nil {
|
|
return selected, saved, err
|
|
}
|
|
expected := stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["snapshotContentSha256"])
|
|
if expected == "" {
|
|
var frozen map[string]any
|
|
if job := item.ProcessingJob; job != nil && job.Kind == "slice" && len(job.Input) > 0 {
|
|
encoded, marshalErr := json.Marshal(job.Input)
|
|
var prior sliceConfirmationInput
|
|
if marshalErr != nil {
|
|
return selected, saved, marshalErr
|
|
}
|
|
if decodeErr := json.Unmarshal(encoded, &prior); decodeErr != nil {
|
|
return selected, saved, decodeErr
|
|
}
|
|
frozen = sliceConfirmationWorkspace(prior)
|
|
} else {
|
|
frozen = mapValue(item.Subject.Payload["annotationWorkspace"])
|
|
}
|
|
if len(frozen) == 0 {
|
|
return selected, saved, errSliceSnapshotConflict
|
|
}
|
|
expected, err = sliceWorkspaceContentSHA(frozen)
|
|
if err != nil {
|
|
return selected, saved, err
|
|
}
|
|
}
|
|
actual, err := sliceWorkspaceContentSHA(selected.RetryWorkspace)
|
|
if err != nil {
|
|
return selected, saved, err
|
|
}
|
|
if actual != expected {
|
|
return selected, saved, errSliceSnapshotConflict
|
|
}
|
|
return selected, saved, nil
|
|
}
|
|
|
|
// Legacy recovery must retain the same newer, owned autosave restored by the
|
|
// editor. Ordinary retries must additionally match their frozen content hash.
|
|
func retainedSliceRecoveryInput(item *domain.HumanItemRecord, version int) (sliceConfirmationInput, combinedAnnotationDraftInput, error) {
|
|
workspace := annotationWorkspacePayload(item)
|
|
baseline := mapValue(item.Subject.Payload["annotationWorkspace"])
|
|
if raw := item.Subject.Payload["annotationDraft"]; raw != nil {
|
|
draft := mapValue(raw)
|
|
if len(draft) == 0 || item.AssignedUserID == "" || stringValue(draft["userId"]) != item.AssignedUserID || intValue(draft["savedVersion"]) > item.Version {
|
|
return sliceConfirmationInput{}, combinedAnnotationDraftInput{}, fmt.Errorf("自动保存草稿的归属或版本无法核对,请先保存当前进度;原内容仍保留")
|
|
}
|
|
savedAt, savedErr := time.Parse(time.RFC3339Nano, stringValue(draft["savedAt"]))
|
|
if savedErr != nil {
|
|
return sliceConfirmationInput{}, combinedAnnotationDraftInput{}, fmt.Errorf("自动保存草稿缺少可靠保存时间,请先保存当前进度;原内容仍保留")
|
|
}
|
|
if len(baseline) == 0 {
|
|
return retainedSliceInput(draft, version)
|
|
}
|
|
baselineTime := stringValue(baseline["confirmedAt"])
|
|
if baselineTime == "" {
|
|
baselineTime = stringValue(baseline["savedAt"])
|
|
}
|
|
confirmedAt, confirmedErr := time.Parse(time.RFC3339Nano, baselineTime)
|
|
if confirmedErr != nil {
|
|
return sliceConfirmationInput{}, combinedAnnotationDraftInput{}, fmt.Errorf("无法核对自动保存草稿与已确认内容的先后,请先核对保存记录;原内容仍保留")
|
|
}
|
|
if !savedAt.Before(confirmedAt) {
|
|
workspace = draft
|
|
}
|
|
}
|
|
return retainedSliceInput(workspace, version)
|
|
}
|
|
|
|
func retainedSliceInput(workspace map[string]any, version int) (sliceConfirmationInput, combinedAnnotationDraftInput, error) {
|
|
var saved combinedAnnotationDraftInput
|
|
encoded, err := json.Marshal(workspace)
|
|
if err == nil {
|
|
err = json.Unmarshal(encoded, &saved)
|
|
}
|
|
if err != nil || len(saved.SliceSegments) == 0 {
|
|
return sliceConfirmationInput{}, saved, fmt.Errorf("没有完整可读取的已保存切片,请先核对或调整切片后重新确认;原内容仍保留")
|
|
}
|
|
return sliceConfirmationInput{
|
|
Version: version, RetryFailed: true, RetryWorkspace: workspace, SliceSegments: saved.SliceSegments,
|
|
SliceOperations: saved.normalizedOperations(), ChangeReason: "保留已保存内容重试失败切片",
|
|
}, saved, nil
|
|
}
|
|
|
|
func retainedSliceRetryIssues(saved combinedAnnotationDraftInput, slices []domain.SliceSegment, task domain.AnnotationTaskRecord, timeline map[string]any) []validationIssue {
|
|
issues := validateDraftCombinedAnnotationEvents(saved.Segments, task)
|
|
issues = append(issues, validateDraftStatuses(saved.normalizedStatuses(), slices)...)
|
|
issues = append(issues, validateDescriptionIntervals(slices, saved.Segments, false)...)
|
|
grid := annotationBoundaryGrid{
|
|
Seconds: timeline["boundariesSeconds"].([]float64), Timestamps: timeline["boundaryTimestampUs"].([]int64),
|
|
}
|
|
for _, slice := range slices {
|
|
for _, boundary := range []float64{slice.Start, slice.End} {
|
|
if _, err := grid.resolve(boundary); err != nil {
|
|
issues = append(issues, validationIssue{Field: "sliceSegments", Message: "已保存切片边界与当前源视频不符,请先调整切片:" + err.Error()})
|
|
}
|
|
}
|
|
}
|
|
for _, segment := range saved.Segments {
|
|
for _, boundary := range []float64{segment.Start, segment.End} {
|
|
if _, err := grid.resolve(boundary); err != nil {
|
|
issues = append(issues, validationIssue{Field: "segments", Message: "已保存动作边界与当前源视频不符,内容已保留:" + err.Error()})
|
|
}
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func sliceRetryFailureEvidence(item *domain.HumanItemRecord) map[string]any {
|
|
previous := mapValue(item.Subject.Payload["sliceWorkflow"])
|
|
result := map[string]any{}
|
|
for _, field := range []string{"requestId", "revision", "errorCode", "error", "startedAt", "heartbeatAt", "leaseExpiresAt", "updatedAt"} {
|
|
if value, exists := previous[field]; exists {
|
|
result[field] = value
|
|
}
|
|
}
|
|
if job := item.ProcessingJob; job != nil {
|
|
result["jobId"], result["jobError"] = job.ID, job.Error
|
|
}
|
|
return result
|
|
}
|