package api import ( "context" "fmt" "log" "os" "path/filepath" "strconv" "strings" "time" "droboticflow/internal/domain" ) // Keep one published data set. Generation IDs remain private fencing/cache // tokens, not selectable business versions. Never clean before publication. func (s *server) retainCurrentAnnotationData(ctx context.Context, task domain.AnnotationTaskRecord, itemID string, generation int) error { item, err := s.store.GetHumanItem(ctx, itemID) if err != nil || item == nil { return err } if item.LegacyTaskID != task.ID || item.OrganizationID != task.OrganizationID || item.RunID != task.RunID { return fmt.Errorf("annotation retention scope mismatch") } // Slice-only regeneration has not published a new semantic revision. Its // generation must never prune the last reviewed or failed-submission data. if item.CurrentStageID != domain.FanyuV58StageQualityReview || item.CurrentRevision != generation || (item.ProcessingJob != nil && item.ProcessingJob.Status == "failed") { return nil } revisions, err := s.store.ListAnnotationRevisions(ctx, task.OrganizationID, task.ID) if err != nil { return err } published := false for _, r := range revisions { if r.Revision == generation { published = true break } } if !published { return nil } // A lossless consolidation pins its original evidence for future reslicing // and submissions, not just the first post-consolidation cleanup. if item.Subject.Payload["preserveAnnotationRevisionHistory"] == true { return nil } // Disposition history is review evidence. Keep its private prior generations; // this does not add selectable delivery versions or alter project deletion. if history, ok := item.Subject.Payload["sliceDispositionHistory"]; ok && history != nil { return nil } current := mapValue(item.Subject.Payload["materialization"]) if intValue(current["revision"]) != generation || stringValue(current["status"]) != "completed" { return nil } if stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["status"]) == "processing" || item.PendingCombinedSubmissionID != "" { return nil } if err := s.store.PruneAnnotationDataBefore(ctx, task.OrganizationID, task.ID, generation); err != nil { return err } run, err := s.store.GetRun(ctx, task.RunID) if err != nil || run == nil { return err } if run.OrganizationID != task.OrganizationID { return fmt.Errorf("annotation run scope mismatch") } // Only API-generated directories under this exact Capture are eligible. // Original inputs, model output and other Captures are never candidates. for _, id := range []string{task.RunID, item.ID, task.ID} { if id == "" || filepath.Base(id) != id || id == "." || id == ".." { return fmt.Errorf("unsafe annotation data identifier") } } if !filepath.IsAbs(s.workspaceMountPath) || filepath.Clean(s.workspaceMountPath) == "/" { return fmt.Errorf("annotation workspace is unavailable") } base := filepath.Join(s.workspaceMountPath, "system", "runs", task.RunID, "human-materializations", item.ID, task.ID) prefix := fmt.Sprintf("%s-%s-%s-materialized-revision-", task.RunID, task.NodeID, task.ID) ids := []string{} for _, a := range run.Artifacts { if !strings.HasPrefix(a.ID, prefix) || a.ID == stringValue(current["artifactId"]) { continue } old, parseErr := strconv.Atoi(strings.TrimPrefix(a.ID, prefix)) if parseErr != nil || old < 1 || old >= generation { continue } dir := filepath.Join(base, fmt.Sprintf("revision-%06d", old)) if filepath.Clean(a.Path) != dir || a.PortID != "materializedSliceDirectory" { continue } resolved, pathErr := filepath.EvalSymlinks(dir) if pathErr != nil && !os.IsNotExist(pathErr) { return pathErr } if pathErr == nil { rootReal, rootErr := filepath.EvalSymlinks(s.workspaceMountPath) relative, relErr := filepath.Rel(s.workspaceMountPath, dir) if rootErr != nil || relErr != nil || resolved != filepath.Join(rootReal, relative) { return fmt.Errorf("refusing symlinked annotation directory") } } if err := s.removeAnnotationGeneration(ctx, task.OrganizationID, dir); err != nil { return err } ids = append(ids, a.ID) } return s.store.RemoveRunArtifacts(ctx, task.OrganizationID, task.RunID, ids) } func (s *server) cleanupReplacedAnnotationData(ctx context.Context, task domain.AnnotationTaskRecord, itemID string, generation int) { ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 30*time.Second) defer cancel() if err := s.retainCurrentAnnotationData(ctx, task, itemID, generation); err != nil { log.Printf("[api] current annotation cleanup task=%s: %v", task.ID, err) } }