droboticflow/internal/api/slice_edit_boundary_archive.go
d-robotics fde4d2737e
Some checks failed
web-ci / checks (push) Waiting to run
web-ci / checks (pull_request) Has been cancelled
🩹 Resolve October 16 annotation and rework feedback
2026-10-19 11:05:43 +08:00

70 lines
2.4 KiB
Go

package api
import "droboticflow/internal/domain"
// A boundary move may move active action pieces into an already discarded
// slice without changing its disposition. Archive only the newly covered area;
// actions in the old discarded area are still rejected by the normal guard.
// The canonical slices carry server-owned archives, never client archives.
func archiveSliceEditBoundaryActions(item *domain.HumanItemRecord, previous, slices []domain.SliceSegment, actions []domain.AnnotationSegment) []domain.AnnotationSegment {
remaining := append([]domain.AnnotationSegment{}, actions...)
edits := currentSliceArchiveEdits(item)
savedEditCount := len(edits)
for i := range slices {
target := &slices[i]
if !target.IsDiscarded() {
continue
}
var old *domain.SliceSegment
for j := range previous {
if previous[j].ID == target.ID {
old = &previous[j]
break
}
}
if old == nil {
for _, sourceID := range target.SourceIDs {
for j := range previous {
if previous[j].ID == sourceID {
old = &previous[j]
break
}
}
if old != nil {
break
}
}
}
if old == nil || !old.IsDiscarded() || (old.Start == target.Start && old.End == target.End) {
continue
}
var archived []domain.AnnotationSegment
var windows []domain.SliceSegment
kept := make([]domain.AnnotationSegment, 0, len(remaining))
for _, action := range remaining {
inside := action.Start >= target.Start && action.End <= target.End
newlyCovered := action.End <= old.Start || action.Start >= old.End
if !inside || !newlyCovered {
kept = append(kept, action)
continue
}
archived = append(archived, action)
windows = append(windows, domain.SliceSegment{Start: action.Start, End: action.End})
}
if len(archived) == 0 {
continue
}
target.DiscardedAnnotations = append(archiveOutsideIntervals(target.DiscardedAnnotations, target.ID, windows...), archived...)
edits = append(edits, sliceArchiveEdit{
Owner: domain.SliceSegment{ID: target.ID, SourceIDs: append([]string(nil), target.SourceIDs...)},
Intervals: windows, Annotations: archived, Version: item.Version + 1,
Revision: item.CurrentRevision, Phase: stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["phase"]),
})
remaining = kept
}
if len(edits) != savedEditCount {
item.Subject.Payload[sliceArchiveEditsKey] = edits
}
return remaining
}