85 lines
3.2 KiB
Go
85 lines
3.2 KiB
Go
package api
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
var errSliceSnapshotConflict = errors.New("当前保存内容与原失败快照不同,请核对后按当前保存内容重新确认生成;两份内容仍保留")
|
|
|
|
// Compare annotation content, not autosave timestamps or compacted operation
|
|
// history. Frame identities, dictionaries, disposition and actions all matter.
|
|
func sliceWorkspaceContentSHA(workspace map[string]any) (string, error) {
|
|
_, saved, err := retainedSliceInput(workspace, 0)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
content := struct {
|
|
Slices []domain.SliceSegment `json:"slices"`
|
|
Segments []domain.AnnotationSegment `json:"segments"`
|
|
Statuses map[string]domain.SliceAnnotationStatus `json:"statuses"`
|
|
}{saved.normalizedSliceSegments(), saved.normalizedSegments(), saved.normalizedStatuses()}
|
|
body, err := json.Marshal(content)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
sum := sha256.Sum256(body)
|
|
return hex.EncodeToString(sum[:]), nil
|
|
}
|
|
|
|
func sliceConfirmationWorkspace(payload sliceConfirmationInput) map[string]any {
|
|
if payload.RetryFailed {
|
|
return payload.RetryWorkspace
|
|
}
|
|
return map[string]any{"sliceSegments": payload.SliceSegments, "segments": []domain.AnnotationSegment{}, "statuses": pendingSliceStatuses(payload.SliceSegments)}
|
|
}
|
|
|
|
// This gate also applies to pre-upgrade queued jobs lacking a content hash.
|
|
// A valid frame in a different saved snapshot is still the wrong annotation.
|
|
func validateSliceCompletionSnapshot(item *domain.HumanItemRecord, payload sliceConfirmationInput) error {
|
|
selectedSHA, err := sliceWorkspaceContentSHA(sliceConfirmationWorkspace(payload))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if payload.SnapshotContentSHA != "" && payload.SnapshotContentSHA != selectedSHA {
|
|
return fmt.Errorf("切片生成快照校验失败,原内容仍保留")
|
|
}
|
|
flowSHA := stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["snapshotContentSha256"])
|
|
if flowSHA != "" && flowSHA != selectedSHA {
|
|
return fmt.Errorf("切片生成确认版本已变化,原内容仍保留")
|
|
}
|
|
current, _, err := retainedSliceRecoveryInput(item, item.Version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
currentSHA, err := sliceWorkspaceContentSHA(current.RetryWorkspace)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if currentSHA != selectedSHA {
|
|
return errSliceSnapshotConflict
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func failedSliceReconfirmationAvailable(item *domain.HumanItemRecord) bool {
|
|
if item == nil || stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["status"]) != "failed" {
|
|
return false
|
|
}
|
|
_, _, err := retainedSliceRetryInput(item, item.Version)
|
|
return errors.Is(err, errSliceSnapshotConflict)
|
|
}
|
|
|
|
func compactSliceWorkspaceForArchive(workspace map[string]any) (map[string]any, error) {
|
|
_, saved, err := retainedSliceInput(workspace, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return map[string]any{"sliceSegments": saved.normalizedSliceSegments(), "segments": saved.normalizedSegments(), "statuses": saved.normalizedStatuses(), "operations": saved.normalizedOperations(), "userId": workspace["userId"], "savedAt": workspace["savedAt"], "confirmedAt": workspace["confirmedAt"]}, nil
|
|
}
|