droboticflow/internal/api/annotation_metadata_revision.go

293 lines
12 KiB
Go

package api
import (
"context"
"crypto/sha256"
"droboticflow/internal/domain"
"encoding/hex"
"encoding/json"
"fmt"
"golang.org/x/sync/errgroup"
"log"
"math"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
)
func readVerifiedAnnotationJSON(path, sha string) (map[string]any, error) {
b, e := os.ReadFile(path)
if e != nil {
return nil, e
}
sum := sha256.Sum256(b)
if sha == "" || hex.EncodeToString(sum[:]) != sha {
return nil, fmt.Errorf("annotation manifest checksum mismatch")
}
var m map[string]any
e = json.Unmarshal(b, &m)
return m, e
}
// No source IMU/PTS reads, video decoding or application-side media transfer
// on the BOS fast path. New generations own independent server-side copies.
func (s *server) tryAnnotationMetadataRevision(ctx context.Context, task domain.AnnotationTaskRecord, item domain.HumanItemRecord, run *domain.RunRecord, payload combinedAnnotationSubmissionInput, revision int, fingerprint string) (*humanSliceMaterialization, error) {
prefix := fmt.Sprintf("%s-%s-%s-materialized-revision-", task.RunID, task.NodeID, task.ID)
var prior *domain.ExecutionArtifact
highest := 0
slices := append([]domain.SliceSegment(nil), payload.SliceSegments...)
sort.Slice(slices, func(i, j int) bool { return slices[i].Start < slices[j].Start })
for i := range run.Artifacts {
a := &run.Artifacts[i]
n, e := strconv.Atoi(strings.TrimPrefix(a.ID, prefix))
if e != nil || !strings.HasPrefix(a.ID, prefix) || n >= revision || n <= highest || stringValue(a.Data["sourceFingerprint"]) != fingerprint {
continue
}
old := decodeMaterializationValue[domain.SliceSegment](a.Data["normalizedSlices"])
if len(old) != len(slices) || len(old) == 0 {
continue
}
same := true
for j := range old {
if math.Abs(old[j].Start-slices[j].Start) > 1e-9 || math.Abs(old[j].End-slices[j].End) > 1e-9 {
same = false
}
}
if same {
prior = a
highest = n
}
}
if prior == nil || prior.Metadata == nil {
return nil, nil
}
base := filepath.Join(s.workspaceMountPath, "system", "runs", task.RunID, "human-materializations", item.ID, task.ID)
if prior.Path != filepath.Join(base, fmt.Sprintf("revision-%06d", highest)) {
return nil, nil
}
manifest, e := readVerifiedAnnotationJSON(filepath.Join(prior.Path, "revision-manifest.json"), prior.Metadata.SHA256)
if e != nil {
return nil, nil
}
if manifest["originalSource"] == nil {
if record, err := s.store.GetCaptureRecordByRunID(ctx, task.RunID); err != nil {
return nil, err
} else if original, err := annotationRevisionOriginalSource(run, record); err != nil {
return nil, err
} else if original != nil {
manifest["originalSource"] = original
}
}
entries := listValue(manifest["items"])
if len(entries) != len(slices) {
return nil, nil
}
timeline, e := s.annotationFrameTimelineForTask(ctx, task, "left")
if e != nil {
return nil, e
}
if stringValue(timeline["sourceFingerprint"]) != fingerprint {
return nil, fmt.Errorf("source changed during annotation update")
}
grid := annotationBoundaryGrid{Seconds: timeline["boundariesSeconds"].([]float64), Timestamps: timeline["boundaryTimestampUs"].([]int64)}
segments := append([]domain.AnnotationSegment{}, payload.Segments...)
for i := range segments {
a, e := grid.resolve(segments[i].Start)
if e != nil {
return nil, e
}
b, e := grid.resolve(segments[i].End)
if e != nil || b <= a {
return nil, fmt.Errorf("invalid annotation boundary")
}
segments[i].Start, segments[i].End = grid.Seconds[a], grid.Seconds[b]
segments[i].StartTimestampUS, segments[i].EndTimestampUS = grid.Timestamps[a], grid.Timestamps[b]
}
if issues := validateDescriptionIntervals(slices, segments, (!payload.sliceOnly && s.continuousActionsForItem(ctx, &item)) || task.TemplateVersion < 6); len(issues) > 0 {
return nil, fmt.Errorf("%s", issues[0].Message)
}
stage, e := os.MkdirTemp("", "annotation-metadata-")
if e != nil {
return nil, e
}
defer os.RemoveAll(stage)
destination := filepath.Join(base, fmt.Sprintf("revision-%06d", revision))
kept, discarded := 0, 0
keptDuration, discardedDuration := 0.0, 0.0
for i, raw := range entries {
entry := mapValue(raw)
entries[i] = entry
slice := &slices[i]
oldDir := stringValue(entry["directory"])
if oldDir != filepath.ToSlash(filepath.Join("slices", fmt.Sprintf("slice-%06d", i+1))) {
return nil, nil
}
files := mapValue(entry["files"])
entry["files"] = files
syncFile := mapValue(files["synchronization"])
synchronized, e := readVerifiedAnnotationJSON(filepath.Join(prior.Path, oldDir, "synchronized-slice.json"), stringValue(syncFile["sha256"]))
if e != nil {
return nil, nil
}
slice.StartTimestampUS = int64Value(entry["startTimestampUs"])
slice.EndTimestampUS = int64Value(entry["endTimestampUs"])
if slice.IsDiscarded() {
discarded++
discardedDuration += slice.End - slice.Start
} else {
kept++
keptDuration += slice.End - slice.Start
}
entry["sourceSliceId"], entry["sliceName"], entry["deliveryDisposition"] = slice.ID, slice.Name, sliceDisposition(*slice)
synchronized["sliceName"], synchronized["deliveryDisposition"] = slice.Name, sliceDisposition(*slice)
entry["dictionary"], synchronized["dictionary"] = slice.Dictionary, slice.Dictionary
delete(entry, "discardEvidence")
delete(synchronized, "discardEvidence")
if slice.IsDiscarded() {
evidence := discardReasonEvidence(*slice)
entry["discardEvidence"] = evidence
synchronized["discardEvidence"] = evidence
}
actions := []map[string]any{}
for _, segment := range segments {
if slice.IsDiscarded() || segment.End <= slice.Start || segment.Start >= slice.End {
continue
}
actions = append(actions, map[string]any{"id": segment.ID, "status": effectiveActionStatus(segment), "isInvalid": segment.IsInvalid, "description": segment.Description, "descriptionTemplateId": segment.LabelID, "dictionary": segment.Dictionary, "phrases": segment.Phrases, "source": segment.Source, "confidence": segment.Confidence, "clipStartMs": (segment.Start - slice.Start) * 1000, "clipEndMs": (segment.End - slice.Start) * 1000, "startTimestampUs": segment.StartTimestampUS, "endTimestampUs": segment.EndTimestampUS})
}
annotation := map[string]any{"schemaVersion": "droboticflow.timestamped-description/v3", "boundaryContract": "hardware-frame-v1", "sliceId": entry["sliceId"], "sliceName": slice.Name, "dictionary": slice.Dictionary, "window": map[string]any{"startTimestampUs": slice.StartTimestampUS, "endTimestampUs": slice.EndTimestampUS, "semantics": "half-open"}, "descriptions": actions, "actions": actions}
if e = os.MkdirAll(filepath.Join(stage, oldDir), 0755); e != nil {
return nil, e
}
for name, value := range map[string]map[string]any{"annotation": annotation, "synchronization": synchronized} {
filename := "annotation.json"
if name == "synchronization" {
filename = "synchronized-slice.json"
}
sha, size, e := writeJSONFile(filepath.Join(stage, oldDir, filename), value)
if e != nil {
return nil, e
}
files[name] = map[string]any{"name": filename, "sha256": sha, "bytes": size}
}
}
// Verify all old metadata before copying any members. A legacy generation
// without BOS digest/ETag metadata safely falls back to normal generation.
var copiedVersions sync.Map
preflight, pctx := errgroup.WithContext(ctx)
preflight.SetLimit(4)
for _, raw := range entries {
entry := mapValue(raw)
dir := stringValue(entry["directory"])
files := mapValue(entry["files"])
for _, kind := range []string{"leftVideo", "rightVideo", "leftPts", "rightPts", "imu"} {
file := mapValue(files[kind])
name := stringValue(file["name"])
if name == "" || filepath.Base(name) != name {
return nil, nil
}
preflight.Go(func() error {
return s.annotationMediaCopyReady(pctx, task.OrganizationID, filepath.Join(prior.Path, dir, name), file)
})
}
}
if e = preflight.Wait(); e != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
return nil, nil
}
group, gctx := errgroup.WithContext(ctx)
group.SetLimit(4)
for _, raw := range entries {
entry := mapValue(raw)
dir := stringValue(entry["directory"])
files := mapValue(entry["files"])
for _, kind := range []string{"leftVideo", "rightVideo", "leftPts", "rightPts", "imu"} {
file := mapValue(files[kind])
name := stringValue(file["name"])
if name == "" || filepath.Base(name) != name {
return nil, nil
}
group.Go(func() error {
target := filepath.Join(destination, dir, name)
meta, e := s.copyAnnotationObject(gctx, task.OrganizationID, filepath.Join(prior.Path, dir, name), target, file)
if e == nil {
copiedVersions.Store(target, meta.ETag)
}
return e
})
}
}
if e = group.Wait(); e != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
log.Printf("[api] metadata reuse fallback task=%s: %v", task.ID, e)
return nil, nil
}
// New copy ETags may differ for multipart/server-side copies. Pin them in
// the next manifest, without downloading media or parsing the original IMU.
for i, raw := range entries {
entry := mapValue(raw)
entries[i] = entry
files := mapValue(entry["files"])
entry["files"] = files
dir := stringValue(entry["directory"])
for _, kind := range []string{"leftVideo", "rightVideo", "leftPts", "rightPts", "imu"} {
file := mapValue(files[kind])
files[kind] = file
if tag, ok := copiedVersions.Load(filepath.Join(destination, dir, stringValue(file["name"]))); ok && tag.(string) != "" {
file["etag"] = tag.(string)
}
}
for _, kind := range []string{"annotation", "synchronization"} {
file := mapValue(files[kind])
files[kind] = file
name := stringValue(file["name"])
meta, e := s.publishAnnotationFile(ctx, task.OrganizationID, filepath.Join(stage, dir, name), filepath.Join(destination, dir, name), stringValue(file["sha256"]))
if e != nil {
return nil, e
}
if meta.ETag != "" {
file["etag"] = meta.ETag
}
}
}
selection := map[string]any{"keptSliceCount": kept, "discardedSliceCount": discarded, "keptDurationSeconds": keptDuration, "discardedDurationSeconds": discardedDuration}
manifest["items"], manifest["revision"], manifest["schemaVersion"] = entries, revision, "droboticflow.slice-materialization/v2"
delete(manifest, "deliverySelection")
if discarded > 0 {
manifest["schemaVersion"] = "droboticflow.slice-materialization/v3"
manifest["deliverySelection"] = selection
}
sha, size, e := writeJSONFile(filepath.Join(stage, "revision-manifest.json"), manifest)
if e != nil {
return nil, e
}
if e = s.assertAnnotationLease(ctx); e != nil {
return nil, e
}
if _, e = s.publishAnnotationFile(ctx, task.OrganizationID, filepath.Join(stage, "revision-manifest.json"), filepath.Join(destination, "revision-manifest.json"), sha); e != nil {
return nil, e
}
artifact := *prior
artifact.ID = prefix + fmt.Sprintf("%06d", revision)
artifact.Name = fmt.Sprintf("revision-%06d.directory.index", revision)
artifact.Path = destination
artifact.SizeBytes = size
artifact.Metadata = &domain.ArtifactMetadata{SHA256: sha, MimeType: "application/json"}
artifact.Data = mapValue(prior.Data)
artifact.Data["rootPath"], artifact.Data["manifestPath"], artifact.Data["revision"] = destination, filepath.Join(destination, "revision-manifest.json"), revision
artifact.Data["normalizedSlices"], artifact.Data["normalizedSegments"], artifact.Data["deliverySelection"], artifact.Data["reusedSliceCount"], artifact.Data["metadataOnly"] = slices, segments, selection, len(slices), true
if e = s.storeAnnotationMaterializationArtifact(ctx, task, run, artifact); e != nil {
return nil, e
}
coverage := mapValue(manifest["coverage"])
log.Printf("[api] metadata-only revision task=%s revision=%d slices=%d", task.ID, revision, len(slices))
return &humanSliceMaterialization{Artifact: artifact, Payload: map[string]any{"status": "completed", "revision": revision, "sliceCount": len(slices), "coverage": coverage, "deliverySelection": selection, "runId": task.RunID, "artifactId": artifact.ID, "manifestSha256": sha, "metadataOnly": true}, Slices: slices, Segments: segments, CoverageStartSecond: floatValue(coverage["startSecond"]), CoverageEndSecond: floatValue(coverage["endSecond"])}, nil
}