forked from longtao.wu/droboticflow
Preserve legacy system suggestions and unreadable saved content across workspace writers. Compare explicit dictionary snapshots and normalize empty optional evidence during save readback; keep transient autosave failures retryable. Cover recovery entry points, source shapes, failed saves and stale-snapshot rejection with isolated HTTP and browser regressions.
73 lines
3.3 KiB
Go
73 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"errors"
|
|
)
|
|
|
|
var errInitialSliceSnapshotConflict = errors.New("切片内容尚未保存,请先保存当前进度;已有标注仍保留")
|
|
|
|
// An initial autosave may contain pending actions even before generation. It
|
|
// must not be mistaken for a request to wipe a submitted/reworked revision.
|
|
// Use the owned saved snapshot; never downgrade the preservation guard.
|
|
func (s *server) initialSliceConfirmationSnapshot(ctx context.Context, task domain.AnnotationTaskRecord, item *domain.HumanItemRecord, requested sliceConfirmationInput) (sliceConfirmationInput, combinedAnnotationDraftInput, bool, error) {
|
|
var empty combinedAnnotationDraftInput
|
|
if task.CurrentRevision > 0 || item.CurrentRevision > 0 || item.ReviewRound > 0 || task.Status == domain.AnnotationTaskStatusRework || item.PendingCombinedSubmissionID != "" {
|
|
return requested, empty, false, nil
|
|
}
|
|
flow := mapValue(item.Subject.Payload["sliceWorkflow"])
|
|
if status := stringValue(flow["status"]); status != "" && status != "editing" {
|
|
return requested, empty, false, nil
|
|
}
|
|
// Explicit reset starts a new editing epoch but retains materialization and
|
|
// revision history as evidence. Only its cancelled, empty slice job is safe.
|
|
reset := stringValue(flow["status"]) == "editing" && stringValue(flow["resetAt"]) != ""
|
|
job := item.ProcessingJob
|
|
resetJob := job != nil && job.Kind == "slice" && job.Status == "cancelled" && len(job.Input) == 0
|
|
if (!reset && len(mapValue(item.Subject.Payload["materialization"])) > 0) || (job != nil && !(reset && resetJob)) {
|
|
return requested, empty, false, nil
|
|
}
|
|
slices, e := s.store.ListSliceRevisions(ctx, task.OrganizationID, task.ID)
|
|
if e != nil {
|
|
return requested, empty, false, e
|
|
}
|
|
for _, revision := range slices {
|
|
if revision.Revision == 0 && revision.AuthorUserID == "system" {
|
|
continue
|
|
}
|
|
if !reset {
|
|
return requested, empty, false, nil
|
|
}
|
|
}
|
|
annotations, e := s.store.ListAnnotationRevisions(ctx, task.OrganizationID, task.ID)
|
|
if e != nil {
|
|
return requested, empty, false, e
|
|
}
|
|
for _, revision := range annotations {
|
|
if revision.Revision == 0 && revision.AuthorUserID == "system" {
|
|
continue
|
|
}
|
|
return requested, empty, false, nil
|
|
}
|
|
selected, saved, e := retainedSliceRecoveryInput(item, item.Version)
|
|
if e != nil || (stringValue(selected.RetryWorkspace["userId"]) != "" && stringValue(selected.RetryWorkspace["userId"]) != item.AssignedUserID) {
|
|
return requested, empty, false, errInitialSliceSnapshotConflict
|
|
}
|
|
// Draft saving fills server-owned disposition and dictionary evidence.
|
|
// Compare the same canonical representation, not raw UI transport fields.
|
|
canonical, issues := s.canonicalSliceDispositions(ctx, annotationRequestItemCopy(item), requested.SliceSegments, requested.SliceOperations)
|
|
if len(issues) > 0 {
|
|
return requested, empty, false, errInitialSliceSnapshotConflict
|
|
}
|
|
left, leftErr := json.Marshal(descriptionOnlySlices(canonical))
|
|
right, rightErr := json.Marshal(descriptionOnlySlices(selected.SliceSegments))
|
|
if leftErr != nil || rightErr != nil || string(left) != string(right) {
|
|
return requested, empty, false, errInitialSliceSnapshotConflict
|
|
}
|
|
selected.RecoverExisting = true
|
|
selected.ChangeReason = "首次确认切片并保留已保存内容"
|
|
return selected, saved, true, nil
|
|
}
|