1732 lines
69 KiB
Go
1732 lines
69 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
type annotationGateConfig struct {
|
|
TemplateID string
|
|
SupplierID string
|
|
ReleasePolicy domain.HumanReleasePolicy
|
|
PartitionMaxItems int
|
|
PartitionMaxWaitSeconds int
|
|
MaxReworkRounds int
|
|
MaxReworkRoundsExplicit bool
|
|
HumanStages []domain.HumanStageDefinition
|
|
RequirePrePackageAcceptance bool
|
|
}
|
|
|
|
func positiveAnnotationGateInt(value string, fallback int) int {
|
|
parsed, err := strconv.Atoi(strings.TrimSpace(value))
|
|
if err != nil || parsed <= 0 {
|
|
return fallback
|
|
}
|
|
return parsed
|
|
}
|
|
|
|
func annotationPluginInt(value any, fallback int) int {
|
|
switch typed := value.(type) {
|
|
case int:
|
|
if typed > 0 {
|
|
return typed
|
|
}
|
|
case int32:
|
|
if typed > 0 {
|
|
return int(typed)
|
|
}
|
|
case int64:
|
|
if typed > 0 {
|
|
return int(typed)
|
|
}
|
|
case float64:
|
|
if typed > 0 {
|
|
return int(typed)
|
|
}
|
|
case string:
|
|
return positiveAnnotationGateInt(typed, fallback)
|
|
}
|
|
return fallback
|
|
}
|
|
|
|
func applyAnnotationPluginConfig(config annotationGateConfig, values map[string]any) annotationGateConfig {
|
|
if policy := domain.HumanReleasePolicy(strings.TrimSpace(fmt.Sprint(values["releasePolicy"]))); policy == domain.HumanReleaseAcceptedPartition || policy == domain.HumanReleaseAllAccepted {
|
|
config.ReleasePolicy = policy
|
|
}
|
|
config.PartitionMaxItems = annotationPluginInt(values["partitionMaxItems"], config.PartitionMaxItems)
|
|
config.PartitionMaxWaitSeconds = annotationPluginInt(values["partitionMaxWaitSeconds"], config.PartitionMaxWaitSeconds)
|
|
if !config.MaxReworkRoundsExplicit {
|
|
config.MaxReworkRounds = annotationPluginInt(values["maxReworkRounds"], config.MaxReworkRounds)
|
|
}
|
|
return config
|
|
}
|
|
|
|
func annotationGateConfigFromNode(node domain.WorkflowNode) annotationGateConfig {
|
|
params := nodeHyperparamsMap(node)
|
|
releasePolicy := domain.HumanReleasePolicy(strings.TrimSpace(params["RELEASE_POLICY"]))
|
|
if releasePolicy != domain.HumanReleaseAllAccepted {
|
|
releasePolicy = domain.HumanReleaseAcceptedPartition
|
|
}
|
|
return annotationGateConfig{
|
|
TemplateID: strings.TrimSpace(params["ANNOTATION_TEMPLATE_ID"]),
|
|
SupplierID: strings.TrimSpace(params["ANNOTATION_SUPPLIER_ID"]),
|
|
ReleasePolicy: releasePolicy,
|
|
PartitionMaxItems: positiveAnnotationGateInt(params["PARTITION_MAX_ITEMS"], 32),
|
|
PartitionMaxWaitSeconds: positiveAnnotationGateInt(params["PARTITION_MAX_WAIT_SECONDS"], 60),
|
|
MaxReworkRounds: positiveAnnotationGateInt(params["MAX_REWORK_ROUNDS"], 5),
|
|
MaxReworkRoundsExplicit: strings.TrimSpace(params["MAX_REWORK_ROUNDS"]) != "",
|
|
RequirePrePackageAcceptance: strings.EqualFold(strings.TrimSpace(params["REQUIRE_PREPACKAGE_ACCEPTANCE"]), "true") || strings.TrimSpace(params["REQUIRE_PREPACKAGE_ACCEPTANCE"]) == "1",
|
|
}
|
|
}
|
|
|
|
func (runtime *Runtime) annotationGateSupplierID(ctx context.Context, run *domain.RunRecord, config *annotationGateConfig) (string, error) {
|
|
if config == nil || run == nil {
|
|
return "", fmt.Errorf("annotation supplier resolution requires a run and configuration")
|
|
}
|
|
requested := strings.TrimSpace(config.SupplierID)
|
|
members, err := runtime.store.ListProjectMembers(ctx, run.OrganizationID, run.ProjectID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
requiredRoles := map[domain.AnnotationAssignmentRole]struct{}{}
|
|
stages := config.HumanStages
|
|
if len(stages) == 0 {
|
|
stages = domain.DefaultAnnotationHumanStages()
|
|
}
|
|
for _, stage := range stages {
|
|
role := domain.AnnotationAssignmentRole(strings.TrimSpace(stage.RequiredRole))
|
|
if role != "" {
|
|
requiredRoles[role] = struct{}{}
|
|
}
|
|
}
|
|
strictCapturePool := domain.IsFanyuV58ThreeRoleHumanLoop(stages)
|
|
if strictCapturePool && run.ProcessingProfile == domain.ProcessingProfileRawHuman {
|
|
// Existing project workflow instances may still carry a historical
|
|
// single-supplier parameter. Newly generated Fanyu human work always
|
|
// joins the shared pool; ownership is chosen at claim time.
|
|
config.SupplierID = ""
|
|
return "", nil
|
|
}
|
|
if config.RequirePrePackageAcceptance && !strictCapturePool {
|
|
requiredRoles[domain.AnnotationRoleAcceptanceReviewer] = struct{}{}
|
|
}
|
|
if requested == "" && strictCapturePool {
|
|
// v5.8 Capture work enters one project-wide pool. The supplier is bound
|
|
// atomically when an annotator claims the item, then quality review is
|
|
// constrained to that supplier. Platform acceptance remains anonymized.
|
|
return "", nil
|
|
}
|
|
ids := []string{}
|
|
for _, m := range members {
|
|
ids = append(ids, m.UserID)
|
|
}
|
|
users, err := runtime.store.ListProjectMemberUsers(ctx, run.OrganizationID, ids)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
directory := map[string]domain.UserRecord{}
|
|
for _, user := range users {
|
|
directory[user.ID] = user
|
|
}
|
|
supplierCovered := func(supplierID string) bool {
|
|
covered := map[domain.AnnotationAssignmentRole]struct{}{}
|
|
for _, member := range members {
|
|
if strings.TrimSpace(member.SupplierID) != supplierID {
|
|
continue
|
|
}
|
|
user, ok := directory[member.UserID]
|
|
if !ok || user.Status == domain.UserStatusDisabled || user.SupplierID != supplierID {
|
|
continue
|
|
}
|
|
for _, role := range domain.AccountProductionRoles(user) {
|
|
covered[role] = struct{}{}
|
|
}
|
|
}
|
|
for role := range requiredRoles {
|
|
if _, ok := covered[role]; !ok {
|
|
return false
|
|
}
|
|
}
|
|
return len(requiredRoles) > 0
|
|
}
|
|
validateSupplier := func(supplierID string) error {
|
|
supplier, supplierErr := runtime.store.GetSupplier(ctx, supplierID)
|
|
if supplierErr != nil {
|
|
return supplierErr
|
|
}
|
|
if supplier == nil || strings.TrimSpace(supplier.OrganizationID) != strings.TrimSpace(run.OrganizationID) || !strings.EqualFold(strings.TrimSpace(supplier.Status), "active") {
|
|
return fmt.Errorf("supplier %s must be active and belong to the run organization", supplierID)
|
|
}
|
|
if !supplierCovered(supplierID) {
|
|
return fmt.Errorf("supplier %s project members do not cover every configured human stage role", supplierID)
|
|
}
|
|
return nil
|
|
}
|
|
if requested != "" {
|
|
if err := validateSupplier(requested); err != nil {
|
|
return "", err
|
|
}
|
|
config.SupplierID = requested
|
|
return requested, nil
|
|
}
|
|
candidates := map[string]struct{}{}
|
|
for _, member := range members {
|
|
supplierID := strings.TrimSpace(member.SupplierID)
|
|
if supplierID == "" || !supplierCovered(supplierID) {
|
|
continue
|
|
}
|
|
if err := validateSupplier(supplierID); err == nil {
|
|
candidates[supplierID] = struct{}{}
|
|
}
|
|
}
|
|
if len(candidates) != 1 {
|
|
return "", fmt.Errorf("annotation workflow requires exactly one active supplier whose project members cover every configured human stage role; found %d", len(candidates))
|
|
}
|
|
for supplierID := range candidates {
|
|
config.SupplierID = supplierID
|
|
return supplierID, nil
|
|
}
|
|
return "", fmt.Errorf("annotation supplier could not be resolved")
|
|
}
|
|
|
|
func annotationNumber(value any) (float64, bool) {
|
|
switch typed := value.(type) {
|
|
case float64:
|
|
return typed, true
|
|
case float32:
|
|
return float64(typed), true
|
|
case int:
|
|
return float64(typed), true
|
|
case int64:
|
|
return float64(typed), true
|
|
case json.Number:
|
|
parsed, err := typed.Float64()
|
|
return parsed, err == nil
|
|
case string:
|
|
parsed, err := strconv.ParseFloat(strings.TrimSpace(typed), 64)
|
|
return parsed, err == nil
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|
|
|
|
func annotationArtifactPayload(artifact domain.ExecutionArtifact) map[string]any {
|
|
path := strings.TrimSpace(artifact.Path)
|
|
if path != "" && !strings.Contains(path, "://") && strings.HasSuffix(strings.ToLower(path), ".json") {
|
|
if payload, err := os.ReadFile(path); err == nil {
|
|
var decoded map[string]any
|
|
decoder := json.NewDecoder(strings.NewReader(string(payload)))
|
|
decoder.UseNumber()
|
|
if err := decoder.Decode(&decoded); err == nil {
|
|
return decoded
|
|
}
|
|
}
|
|
}
|
|
if len(artifact.Data) > 0 {
|
|
return artifact.Data
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func annotationClipItems(inputs []domain.ExecutionArtifact) map[string]map[string]any {
|
|
items := map[string]map[string]any{}
|
|
for _, artifact := range inputs {
|
|
// v5.4 carries virtual clips as a video collection port. Their compact
|
|
// per-artifact metadata is the business payload; a manifest is only an
|
|
// internal index and is intentionally not required on the graph edge.
|
|
rawClipID, hasClipID := artifact.Data["clipId"]
|
|
clipID := ""
|
|
if hasClipID && rawClipID != nil {
|
|
clipID = strings.TrimSpace(fmt.Sprint(rawClipID))
|
|
}
|
|
if artifact.Type == domain.PortTypeVideo && clipID != "" {
|
|
copy := map[string]any{}
|
|
for key, value := range artifact.Data {
|
|
copy[key] = value
|
|
}
|
|
copy["clipName"] = artifact.Name
|
|
items[artifact.Name] = copy
|
|
}
|
|
if artifact.Name != "clips-manifest.json" {
|
|
continue
|
|
}
|
|
payload := annotationArtifactPayload(artifact)
|
|
for _, raw := range anySlice(payload["items"]) {
|
|
item, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
name := strings.TrimSpace(fmt.Sprint(item["clipName"]))
|
|
if name != "" {
|
|
items[name] = item
|
|
}
|
|
}
|
|
}
|
|
return items
|
|
}
|
|
|
|
func anySlice(value any) []any {
|
|
switch typed := value.(type) {
|
|
case []any:
|
|
return typed
|
|
case []map[string]any:
|
|
items := make([]any, 0, len(typed))
|
|
for _, item := range typed {
|
|
items = append(items, item)
|
|
}
|
|
return items
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func annotationLabel(template domain.AnnotationTemplateRecord, label string) (string, string) {
|
|
normalized := strings.TrimSpace(label)
|
|
for _, item := range template.Labels {
|
|
if strings.EqualFold(strings.TrimSpace(item.Name), normalized) || strings.EqualFold(strings.TrimSpace(item.ID), normalized) {
|
|
return item.ID, item.Name
|
|
}
|
|
for _, alias := range item.Aliases {
|
|
if strings.EqualFold(strings.TrimSpace(alias), normalized) {
|
|
return item.ID, item.Name
|
|
}
|
|
}
|
|
}
|
|
if normalized == "" {
|
|
return "unlabeled", "未标注"
|
|
}
|
|
id := strings.ToLower(normalized)
|
|
id = strings.NewReplacer(" ", "-", "_", "-", "/", "-").Replace(id)
|
|
return id, normalized
|
|
}
|
|
|
|
func annotationSegmentsFromActions(actions any, template domain.AnnotationTemplateRecord) []domain.AnnotationSegment {
|
|
segments := make([]domain.AnnotationSegment, 0)
|
|
for index, raw := range anySlice(actions) {
|
|
item, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
start, startOK := annotationNumber(item["start"])
|
|
end, endOK := annotationNumber(item["end"])
|
|
if !startOK || !endOK || end <= start {
|
|
continue
|
|
}
|
|
labelID, label := annotationLabel(template, fmt.Sprint(item["label"]))
|
|
confidence, _ := annotationNumber(item["confidence"])
|
|
phrases := make([]string, 0)
|
|
for _, phrase := range anySlice(item["phrases"]) {
|
|
if text := strings.TrimSpace(fmt.Sprint(phrase)); text != "" {
|
|
phrases = append(phrases, text)
|
|
}
|
|
}
|
|
segments = append(segments, domain.AnnotationSegment{
|
|
ID: fmt.Sprintf("prelabel-%03d", index+1),
|
|
LabelID: labelID,
|
|
Label: label,
|
|
Start: start,
|
|
End: end,
|
|
Confidence: confidence,
|
|
Phrases: phrases,
|
|
Source: "workflow-prelabel",
|
|
})
|
|
}
|
|
return segments
|
|
}
|
|
|
|
func annotationPrelabels(inputs []domain.ExecutionArtifact, template domain.AnnotationTemplateRecord) map[string][]domain.AnnotationSegment {
|
|
result := map[string][]domain.AnnotationSegment{}
|
|
for _, artifact := range inputs {
|
|
payload := annotationArtifactPayload(artifact)
|
|
if payload == nil {
|
|
continue
|
|
}
|
|
if actions := payload["actions"]; actions != nil {
|
|
clipID := strings.TrimSpace(fmt.Sprint(payload["clipId"]))
|
|
if clipID == "" {
|
|
clipID = strings.TrimSuffix(artifact.Name, filepath.Ext(artifact.Name))
|
|
clipID = strings.TrimSuffix(clipID, "_annotation")
|
|
}
|
|
result[clipID] = annotationSegmentsFromActions(actions, template)
|
|
}
|
|
for _, raw := range anySlice(payload["items"]) {
|
|
item, ok := raw.(map[string]any)
|
|
if !ok {
|
|
continue
|
|
}
|
|
clipID := strings.TrimSpace(fmt.Sprint(item["clipId"]))
|
|
annotation, _ := item["annotation"].(map[string]any)
|
|
if clipID != "" && annotation != nil {
|
|
result[clipID] = annotationSegmentsFromActions(annotation["actions"], template)
|
|
}
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
// annotationPoseSummary keeps scoped prelabel evidence references/counts in the
|
|
// human task. The full manifests remain artifacts and are later cropped by the
|
|
// package operator; reviewers receive browser-renderable evidence rather than
|
|
// an unbounded pose stream.
|
|
func annotationPoseSummary(inputs []domain.ExecutionArtifact) map[string]any {
|
|
summary := map[string]any{
|
|
"headposeEnabled": false, "headposeAvailable": false, "headposeFrameCount": 0,
|
|
"handposeEnabled": false, "handposeAvailable": false, "handposeFrameCount": 0,
|
|
"handPrelabelEnabled": false, "handPrelabelAvailable": false, "handPrelabelFrameCount": 0,
|
|
"headposeInvalid": false,
|
|
"headposeArtifacts": []map[string]any{}, "handposeArtifacts": []map[string]any{},
|
|
"handPrelabelArtifacts": []map[string]any{},
|
|
"headposeRenderArtifacts": []map[string]any{}, "handPrelabelRenderArtifacts": []map[string]any{},
|
|
}
|
|
seenHeadposeArtifacts := map[string]struct{}{}
|
|
seenHandposeArtifacts := map[string]struct{}{}
|
|
for _, artifact := range inputs {
|
|
identity := strings.ToLower(strings.TrimSpace(artifact.NodeID + " " + artifact.Name + " " + artifact.Path))
|
|
mimeType := ""
|
|
if artifact.Metadata != nil {
|
|
mimeType = strings.ToLower(strings.TrimSpace(artifact.Metadata.MimeType))
|
|
}
|
|
isVideo := artifact.Type == domain.PortTypeVideo || strings.HasPrefix(mimeType, "video/") || strings.HasSuffix(identity, ".mp4")
|
|
if isVideo && (strings.Contains(identity, "headpose") || strings.Contains(identity, "head-pose")) {
|
|
ref := map[string]any{"artifactId": artifact.ID, "name": artifact.Name, "path": artifact.Path, "kind": "rendered_video"}
|
|
if artifact.Metadata != nil && strings.TrimSpace(artifact.Metadata.SHA256) != "" {
|
|
ref["sha256"] = artifact.Metadata.SHA256
|
|
}
|
|
summary["headposeEnabled"] = true
|
|
summary["headposeRenderArtifacts"] = append(summary["headposeRenderArtifacts"].([]map[string]any), ref)
|
|
}
|
|
if isVideo && (strings.Contains(identity, "wilor") || strings.Contains(identity, "handpose") || strings.Contains(identity, "hand-pose")) {
|
|
ref := map[string]any{"artifactId": artifact.ID, "name": artifact.Name, "path": artifact.Path, "kind": "rendered_video"}
|
|
if artifact.Metadata != nil && strings.TrimSpace(artifact.Metadata.SHA256) != "" {
|
|
ref["sha256"] = artifact.Metadata.SHA256
|
|
}
|
|
summary["handposeEnabled"] = true
|
|
summary["handPrelabelEnabled"] = true
|
|
summary["handPrelabelRenderArtifacts"] = append(summary["handPrelabelRenderArtifacts"].([]map[string]any), ref)
|
|
}
|
|
payload := annotationArtifactPayload(artifact)
|
|
if payload == nil {
|
|
continue
|
|
}
|
|
schema := strings.TrimSpace(fmt.Sprint(payload["schemaVersion"]))
|
|
ref := map[string]any{"artifactId": artifact.ID, "name": artifact.Name, "path": artifact.Path}
|
|
if artifact.Metadata != nil && strings.TrimSpace(artifact.Metadata.SHA256) != "" {
|
|
ref["sha256"] = artifact.Metadata.SHA256
|
|
}
|
|
switch schema {
|
|
case "droboticflow.headpose/v1":
|
|
summary["headposeEnabled"] = true
|
|
if enabled, exists := payload["enabled"].(bool); exists && !enabled {
|
|
continue
|
|
}
|
|
declaredCount, countOK := annotationNumber(payload["frameCount"])
|
|
if !countOK || math.IsNaN(declaredCount) || declaredCount <= 0 {
|
|
summary["headposeInvalid"] = true
|
|
continue
|
|
}
|
|
framesValue, includesFrames := payload["frames"]
|
|
frames := anySlice(framesValue)
|
|
if includesFrames && (int(declaredCount) != len(frames) || len(frames) == 0) {
|
|
summary["headposeInvalid"] = true
|
|
continue
|
|
}
|
|
// Durable ref artifacts are compacted before they are persisted in a Run:
|
|
// frameCount and schema stay queryable, while the potentially large
|
|
// frames array remains in the checksum-protected JSON object. Accept that
|
|
// compact projection without weakening validation of a full manifest.
|
|
if !includesFrames && !annotationPoseArtifactHasDurableChecksum(artifact) {
|
|
summary["headposeInvalid"] = true
|
|
continue
|
|
}
|
|
if _, seen := seenHeadposeArtifacts[artifact.ID]; seen {
|
|
continue
|
|
}
|
|
seenHeadposeArtifacts[artifact.ID] = struct{}{}
|
|
frameCount := int(declaredCount)
|
|
ref["frameCount"] = frameCount
|
|
summary["headposeAvailable"] = true
|
|
summary["headposeFrameCount"] = summary["headposeFrameCount"].(int) + frameCount
|
|
summary["headposeArtifacts"] = append(summary["headposeArtifacts"].([]map[string]any), ref)
|
|
case "droboticflow.wilor.hand_pose_result/v1":
|
|
summary["handposeEnabled"] = true
|
|
summary["handPrelabelEnabled"] = true
|
|
if enabled, exists := payload["enabled"].(bool); exists && !enabled {
|
|
continue
|
|
}
|
|
if _, seen := seenHandposeArtifacts[artifact.ID]; seen {
|
|
continue
|
|
}
|
|
seenHandposeArtifacts[artifact.ID] = struct{}{}
|
|
summary["handposeAvailable"] = true
|
|
result, _ := payload["result"].(map[string]any)
|
|
count, ok := annotationNumber(payload["frameCount"])
|
|
if !ok {
|
|
count, ok = annotationNumber(payload["framesCount"])
|
|
}
|
|
if !ok {
|
|
count, ok = annotationNumber(result["frameCount"])
|
|
}
|
|
if !ok {
|
|
if frames, present := payload["frames"]; present {
|
|
count, ok = float64(len(anySlice(frames))), true
|
|
}
|
|
}
|
|
if ok && !math.IsNaN(count) && !math.IsInf(count, 0) && count >= 0 {
|
|
frameCount := int(count)
|
|
ref["frameCount"] = frameCount
|
|
summary["handposeFrameCount"] = summary["handposeFrameCount"].(int) + frameCount
|
|
}
|
|
summary["handposeArtifacts"] = append(summary["handposeArtifacts"].([]map[string]any), ref)
|
|
}
|
|
}
|
|
summary["handPrelabelAvailable"] = summary["handposeAvailable"]
|
|
summary["handPrelabelFrameCount"] = summary["handposeFrameCount"]
|
|
summary["handPrelabelArtifacts"] = summary["handposeArtifacts"]
|
|
return summary
|
|
}
|
|
|
|
func annotationPoseArtifactHasDurableChecksum(artifact domain.ExecutionArtifact) bool {
|
|
if artifact.Metadata == nil || strings.TrimSpace(artifact.Metadata.SHA256) == "" {
|
|
return false
|
|
}
|
|
if artifact.Payload != nil && artifact.Payload.Ref != nil {
|
|
return true
|
|
}
|
|
return strings.TrimSpace(artifact.Path) != ""
|
|
}
|
|
|
|
func annotationPoseSummaryHasRequiredHeadpose(summary map[string]any) bool {
|
|
available, _ := summary["headposeAvailable"].(bool)
|
|
invalid, _ := summary["headposeInvalid"].(bool)
|
|
count, countOK := annotationNumber(summary["headposeFrameCount"])
|
|
refs := anySlice(summary["headposeArtifacts"])
|
|
return available && !invalid && countOK && count > 0 && len(refs) > 0
|
|
}
|
|
|
|
func annotationClipRange(item map[string]any, artifact domain.ExecutionArtifact) (float64, float64) {
|
|
start, _ := annotationNumber(item["startSecond"])
|
|
end, endOK := annotationNumber(item["endSecond"])
|
|
if !endOK {
|
|
if duration, ok := annotationNumber(item["duration"]); ok {
|
|
end = start + duration
|
|
endOK = true
|
|
}
|
|
}
|
|
if !endOK && artifact.Data != nil {
|
|
if preview, ok := artifact.Data["preview"].(map[string]any); ok {
|
|
if duration, durationOK := annotationNumber(preview["duration"]); durationOK {
|
|
end = start + duration
|
|
}
|
|
}
|
|
}
|
|
return start, end
|
|
}
|
|
|
|
func nodeHasInputPort(node domain.WorkflowNode, portID string) bool {
|
|
for _, input := range node.Data.Inputs {
|
|
if input.ID == portID {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func bindingArtifacts(bindings []domain.NodeInputBinding, targetPortID string) []domain.ExecutionArtifact {
|
|
for _, binding := range bindings {
|
|
if binding.TargetPortID == targetPortID {
|
|
return append([]domain.ExecutionArtifact(nil), binding.Artifacts...)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func matchingCollectionArtifact(source domain.ExecutionArtifact, candidates []domain.ExecutionArtifact) *domain.ExecutionArtifact {
|
|
collectionID := strings.TrimSpace(fmt.Sprint(source.Data["collectionId"]))
|
|
for index := range candidates {
|
|
candidateID := strings.TrimSpace(fmt.Sprint(candidates[index].Data["collectionId"]))
|
|
if collectionID != "" && candidateID == collectionID {
|
|
copy := candidates[index]
|
|
return ©
|
|
}
|
|
}
|
|
if len(candidates) == 1 {
|
|
copy := candidates[0]
|
|
return ©
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func annotationSourceDuration(artifact domain.ExecutionArtifact, windows []map[string]any) float64 {
|
|
for _, key := range []string{"sourceDurationSeconds", "duration"} {
|
|
if value, ok := annotationNumber(artifact.Data[key]); ok && value > 0 {
|
|
return value
|
|
}
|
|
}
|
|
for _, key := range []string{"metrics", "preview"} {
|
|
if nested, ok := artifact.Data[key].(map[string]any); ok {
|
|
if value, valueOK := annotationNumber(nested["duration"]); valueOK && value > 0 {
|
|
return value
|
|
}
|
|
}
|
|
}
|
|
if privacy, ok := artifact.Data["privacyTransform"].(map[string]any); ok {
|
|
if metrics, metricsOK := privacy["metrics"].(map[string]any); metricsOK {
|
|
for _, key := range []string{"duration", "videoDurationSeconds"} {
|
|
if value, valueOK := annotationNumber(metrics[key]); valueOK && value > 0 {
|
|
return value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
maximum := 0.0
|
|
for _, window := range windows {
|
|
if end, ok := annotationNumber(window["endSecond"]); ok && end > maximum {
|
|
maximum = end
|
|
}
|
|
if duration, ok := annotationNumber(window["sourceDurationSeconds"]); ok && duration > maximum {
|
|
maximum = duration
|
|
}
|
|
}
|
|
return maximum
|
|
}
|
|
|
|
func roughCutWindowItems(inputs []domain.ExecutionArtifact) []map[string]any {
|
|
items := make([]map[string]any, 0)
|
|
for _, artifact := range inputs {
|
|
payload := annotationArtifactPayload(artifact)
|
|
if strings.TrimSpace(fmt.Sprint(payload["schemaVersion"])) != "droboticflow.rough-cut-windows/v1" {
|
|
continue
|
|
}
|
|
rawWindows := anySlice(payload["windows"])
|
|
if len(rawWindows) == 0 {
|
|
rawWindows = anySlice(payload["items"])
|
|
}
|
|
for _, raw := range rawWindows {
|
|
if item, ok := raw.(map[string]any); ok {
|
|
items = append(items, item)
|
|
}
|
|
}
|
|
}
|
|
return items
|
|
}
|
|
|
|
func fullCoverageSliceSuggestions(duration float64, windows []map[string]any) []domain.SliceSegment {
|
|
if !math.IsNaN(duration) && !math.IsInf(duration, 0) && duration > 0 {
|
|
// Continue below.
|
|
} else {
|
|
return nil
|
|
}
|
|
type interval struct {
|
|
start float64
|
|
end float64
|
|
sourceIDs []string
|
|
}
|
|
valid := make([]interval, 0, len(windows))
|
|
for index, window := range windows {
|
|
start, startOK := annotationNumber(window["startSecond"])
|
|
end, endOK := annotationNumber(window["endSecond"])
|
|
if !startOK || !endOK || math.IsNaN(start) || math.IsNaN(end) || math.IsInf(start, 0) || math.IsInf(end, 0) {
|
|
continue
|
|
}
|
|
start = max(0, min(duration, start))
|
|
end = max(0, min(duration, end))
|
|
if end <= start {
|
|
continue
|
|
}
|
|
windowID := strings.TrimSpace(fmt.Sprint(window["id"]))
|
|
if windowID == "" || windowID == "<nil>" {
|
|
windowID = fmt.Sprintf("window-%06d", index+1)
|
|
}
|
|
valid = append(valid, interval{start: start, end: end, sourceIDs: []string{windowID}})
|
|
}
|
|
sort.Slice(valid, func(left, right int) bool {
|
|
if valid[left].start == valid[right].start {
|
|
return valid[left].end < valid[right].end
|
|
}
|
|
return valid[left].start < valid[right].start
|
|
})
|
|
merged := make([]interval, 0, len(valid))
|
|
for _, candidate := range valid {
|
|
if len(merged) == 0 || candidate.start >= merged[len(merged)-1].end {
|
|
merged = append(merged, candidate)
|
|
continue
|
|
}
|
|
last := &merged[len(merged)-1]
|
|
last.end = max(last.end, candidate.end)
|
|
last.sourceIDs = append(last.sourceIDs, candidate.sourceIDs...)
|
|
}
|
|
slices := make([]domain.SliceSegment, 0, len(merged)*2+1)
|
|
cursor := 0.0
|
|
appendSlice := func(start float64, end float64, sourceIDs []string) {
|
|
if end <= start {
|
|
return
|
|
}
|
|
kind := "coverage"
|
|
if len(sourceIDs) > 0 {
|
|
kind = "suggested"
|
|
}
|
|
slices = append(slices, domain.SliceSegment{
|
|
ID: fmt.Sprintf("%s-%06d", kind, len(slices)+1),
|
|
Start: start,
|
|
End: end,
|
|
SourceIDs: append([]string(nil), sourceIDs...),
|
|
})
|
|
}
|
|
for _, candidate := range merged {
|
|
appendSlice(cursor, candidate.start, nil)
|
|
appendSlice(candidate.start, candidate.end, candidate.sourceIDs)
|
|
cursor = candidate.end
|
|
}
|
|
appendSlice(cursor, duration, nil)
|
|
if len(slices) == 0 {
|
|
appendSlice(0, duration, nil)
|
|
}
|
|
return slices
|
|
}
|
|
|
|
func annotationGateTasksForRun(ctx context.Context, runtime *Runtime, run *domain.RunRecord, node domain.WorkflowNode) ([]domain.AnnotationTaskRecord, error) {
|
|
if runtime == nil || runtime.store == nil || run == nil {
|
|
return nil, fmt.Errorf("annotation gate runtime is not initialized")
|
|
}
|
|
items, err := runtime.store.ListAnnotationTasks(ctx, run.OrganizationID, run.ProjectID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
filtered := make([]domain.AnnotationTaskRecord, 0)
|
|
for _, item := range items {
|
|
if item.RunID == run.ID && item.NodeID == node.ID {
|
|
filtered = append(filtered, item)
|
|
}
|
|
}
|
|
sort.Slice(filtered, func(left, right int) bool { return filtered[left].ArtifactID < filtered[right].ArtifactID })
|
|
return filtered, nil
|
|
}
|
|
|
|
func (runtime *Runtime) annotationLabelsForProject(ctx context.Context, organizationID, projectID string, _ []domain.SemanticLabelDefinition) ([]domain.SemanticLabelDefinition, error) {
|
|
selection, err := runtime.store.GetProjectAnnotationLabels(ctx, projectID)
|
|
if err != nil || selection == nil || len(selection.LabelIDs) == 0 {
|
|
return []domain.SemanticLabelDefinition{}, err
|
|
}
|
|
labels, err := runtime.store.ListAnnotationLabels(ctx, organizationID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
byID := make(map[string]domain.AnnotationLabelRecord, len(labels))
|
|
for _, label := range labels {
|
|
byID[label.ID] = label
|
|
}
|
|
result := make([]domain.SemanticLabelDefinition, 0, len(selection.LabelIDs))
|
|
for _, id := range selection.LabelIDs {
|
|
label, exists := byID[id]
|
|
if !exists || label.Status != domain.AnnotationLabelActive {
|
|
continue
|
|
}
|
|
result = append(result, domain.SemanticLabelDefinition{ID: label.Code, Name: label.Name, Description: label.Description, Aliases: label.Aliases, Color: label.Color})
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (runtime *Runtime) createAnnotationGateTasks(
|
|
ctx context.Context,
|
|
run *domain.RunRecord,
|
|
node domain.WorkflowNode,
|
|
inputs []domain.ExecutionArtifact,
|
|
inputBindings []domain.NodeInputBinding,
|
|
) ([]domain.AnnotationTaskRecord, error) {
|
|
config := annotationGateConfigFromNode(node)
|
|
if pluginConfig, pluginErr := runtime.store.GetOrganizationPlugin(ctx, run.OrganizationID, "annotation-qa"); pluginErr != nil {
|
|
return nil, pluginErr
|
|
} else if pluginConfig != nil && pluginConfig.Enabled {
|
|
config = applyAnnotationPluginConfig(config, pluginConfig.Config)
|
|
}
|
|
if config.TemplateID == "" {
|
|
return nil, fmt.Errorf("annotation gate requires ANNOTATION_TEMPLATE_ID")
|
|
}
|
|
template, err := runtime.store.GetAnnotationTemplate(ctx, config.TemplateID)
|
|
if err != nil || template == nil {
|
|
return nil, fmt.Errorf("load annotation template %s: %w", config.TemplateID, err)
|
|
}
|
|
config.HumanStages = template.HumanStages
|
|
labelSnapshot, labelErr := runtime.annotationLabelsForProject(ctx, run.OrganizationID, run.ProjectID, template.Labels)
|
|
if labelErr != nil {
|
|
return nil, labelErr
|
|
}
|
|
template.Labels = append([]domain.SemanticLabelDefinition(nil), labelSnapshot...)
|
|
if (template.ProjectID != "" && template.ProjectID != run.ProjectID) || template.Status != domain.AnnotationTemplatePublished || template.OrganizationID != run.OrganizationID {
|
|
return nil, fmt.Errorf("annotation gate template does not belong to the run project and organization")
|
|
}
|
|
if _, err := runtime.annotationGateSupplierID(ctx, run, &config); err != nil {
|
|
return nil, fmt.Errorf("resolve annotation supplier: %w", err)
|
|
}
|
|
existing, err := annotationGateTasksForRun(ctx, runtime, run, node)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(existing) > 0 {
|
|
if err := runtime.ensureAnnotationHumanBatch(ctx, run, node, config, existing); err != nil {
|
|
return nil, err
|
|
}
|
|
return existing, nil
|
|
}
|
|
if nodeHasInputPort(node, "roughCutWindows") || run.ProcessingProfile == domain.ProcessingProfileRawHuman {
|
|
leftVideos := bindingArtifacts(inputBindings, "fullLeftVideos")
|
|
rightVideos := bindingArtifacts(inputBindings, "fullRightVideos")
|
|
if len(leftVideos) == 0 {
|
|
return nil, fmt.Errorf("timestamp-window human gate requires fullLeftVideos")
|
|
}
|
|
windows := roughCutWindowItems(bindingArtifacts(inputBindings, "roughCutWindows"))
|
|
poseInputs := append(bindingArtifacts(inputBindings, "headposeEvidence"), bindingArtifacts(inputBindings, "handposeEvidence")...)
|
|
poseSummary := annotationPoseSummary(poseInputs)
|
|
tasks := make([]domain.AnnotationTaskRecord, 0, len(leftVideos))
|
|
for _, artifact := range leftVideos {
|
|
rightArtifact := matchingCollectionArtifact(artifact, rightVideos)
|
|
collectionID := strings.TrimSpace(fmt.Sprint(artifact.Data["collectionId"]))
|
|
matchedWindows := make([]map[string]any, 0)
|
|
for _, window := range windows {
|
|
windowCollection := strings.TrimSpace(fmt.Sprint(window["collectionId"]))
|
|
if windowCollection == "" || collectionID == "" || windowCollection == collectionID {
|
|
matchedWindows = append(matchedWindows, window)
|
|
}
|
|
}
|
|
duration := annotationSourceDuration(artifact, matchedWindows)
|
|
if duration <= 0 {
|
|
return nil, fmt.Errorf("human gate cannot resolve full source duration for %s", artifact.Name)
|
|
}
|
|
task := domain.AnnotationTaskRecord{ID: domain.NewID("annotation-task"), OrganizationID: run.OrganizationID, ProjectID: run.ProjectID, DataTaskID: run.TaskID, WorkflowID: run.WorkflowID, RunID: run.ID, NodeID: node.ID, ArtifactID: artifact.ID, SourceVideo: fmt.Sprintf("/api/runs/%s/artifacts/%s/content", url.PathEscape(run.ID), url.PathEscape(artifact.ID)), ClipStart: 0, ClipEnd: duration, SourceIsFullVideo: true, SourceDurationSeconds: duration, CoverageStartSecond: 0, CoverageEndSecond: duration, CoverageMode: "full_source_pending_common_range", TemplateID: template.ID, TemplateVersion: template.Version, LabelSnapshot: append([]domain.SemanticLabelDefinition(nil), labelSnapshot...), SupplierID: config.SupplierID, Status: domain.AnnotationTaskStatusPendingAnnotation, PoseSummary: poseSummary, CreatedAt: nowISO(), UpdatedAt: nowISO()}
|
|
task.ProcessingProfile = run.ProcessingProfile
|
|
if rightArtifact != nil {
|
|
task.RightArtifactID = rightArtifact.ID
|
|
task.RightSourceVideo = fmt.Sprintf("/api/runs/%s/artifacts/%s/content", url.PathEscape(run.ID), url.PathEscape(rightArtifact.ID))
|
|
}
|
|
if err := runtime.store.UpsertAnnotationTask(ctx, task); err != nil {
|
|
return nil, err
|
|
}
|
|
slices := fullCoverageSliceSuggestions(duration, matchedWindows)
|
|
sliceReason := "Qwen timestamp-window suggestions"
|
|
if run.ProcessingProfile == domain.ProcessingProfileRawHuman {
|
|
sliceReason = "Initial full-coverage slice for raw human annotation"
|
|
}
|
|
segments := make([]domain.AnnotationSegment, 0, len(matchedWindows))
|
|
for index, window := range matchedWindows {
|
|
start, startOK := annotationNumber(window["startSecond"])
|
|
end, endOK := annotationNumber(window["endSecond"])
|
|
if !startOK || !endOK || start < 0 || end <= start || end > duration {
|
|
continue
|
|
}
|
|
windowID := strings.TrimSpace(fmt.Sprint(window["id"]))
|
|
if windowID == "" {
|
|
windowID = fmt.Sprintf("window-%06d", index+1)
|
|
}
|
|
labelHint := strings.TrimSpace(fmt.Sprint(window["suggestedLabelId"]))
|
|
if labelHint == "" || labelHint == "<nil>" {
|
|
labelHint = strings.TrimSpace(fmt.Sprint(window["suggestedLabel"]))
|
|
}
|
|
labelID, label := annotationLabel(*template, labelHint)
|
|
confidence, _ := annotationNumber(window["confidence"])
|
|
segments = append(segments, domain.AnnotationSegment{ID: "prelabel-" + windowID, LabelID: labelID, Label: label, Start: start, End: end, Confidence: confidence, Source: "qwen"})
|
|
}
|
|
if err := runtime.store.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{ID: domain.NewID("slice-revision"), OrganizationID: run.OrganizationID, TaskID: task.ID, AnnotationEpoch: task.AnnotationEpoch, Revision: 0, Segments: slices, AuthorUserID: "system", ChangeReason: sliceReason, CreatedAt: nowISO()}); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(segments) > 0 {
|
|
if err := runtime.store.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: domain.NewID("annotation-revision"), OrganizationID: run.OrganizationID, TaskID: task.ID, AnnotationEpoch: task.AnnotationEpoch, Revision: 0, TemplateVersion: template.Version, Segments: segments, AuthorUserID: "system", ChangeReason: "Qwen timestamp-window suggestions", CountsAsOutput: false, CreatedAt: nowISO()}); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
tasks = append(tasks, task)
|
|
}
|
|
if err := runtime.ensureAnnotationHumanBatch(ctx, run, node, config, tasks); err != nil {
|
|
return nil, err
|
|
}
|
|
return tasks, nil
|
|
}
|
|
clips := annotationClipItems(inputs)
|
|
prelabels := annotationPrelabels(inputs, *template)
|
|
poseSummary := annotationPoseSummary(inputs)
|
|
tasks := make([]domain.AnnotationTaskRecord, 0)
|
|
for _, artifact := range inputs {
|
|
if artifact.Type != domain.PortTypeVideo {
|
|
continue
|
|
}
|
|
clipItem, expectedClip := clips[artifact.Name]
|
|
if len(clips) > 0 && !expectedClip {
|
|
continue
|
|
}
|
|
clipID := strings.TrimSpace(fmt.Sprint(clipItem["clipId"]))
|
|
if clipID == "" {
|
|
clipID = strings.TrimSuffix(artifact.Name, filepath.Ext(artifact.Name))
|
|
}
|
|
clipStart, clipEnd := annotationClipRange(clipItem, artifact)
|
|
if clipEnd <= clipStart {
|
|
return nil, fmt.Errorf("annotation gate cannot resolve clip duration for %s", artifact.Name)
|
|
}
|
|
task := domain.AnnotationTaskRecord{
|
|
ID: domain.NewID("annotation-task"),
|
|
OrganizationID: run.OrganizationID,
|
|
ProjectID: run.ProjectID,
|
|
DataTaskID: run.TaskID,
|
|
WorkflowID: run.WorkflowID,
|
|
RunID: run.ID,
|
|
NodeID: node.ID,
|
|
ArtifactID: artifact.ID,
|
|
SourceVideo: fmt.Sprintf("/api/runs/%s/artifacts/%s/content", url.PathEscape(run.ID), url.PathEscape(artifact.ID)),
|
|
ClipStart: clipStart,
|
|
ClipEnd: clipEnd,
|
|
SourceIsFullVideo: func() bool {
|
|
value, _ := clipItem["sourceIsFullVideo"].(bool)
|
|
return value
|
|
}(),
|
|
SourceDurationSeconds: func() float64 {
|
|
value, _ := annotationNumber(clipItem["sourceDurationSeconds"])
|
|
return value
|
|
}(),
|
|
TemplateID: template.ID,
|
|
TemplateVersion: template.Version,
|
|
LabelSnapshot: append([]domain.SemanticLabelDefinition(nil), labelSnapshot...),
|
|
SupplierID: config.SupplierID,
|
|
AssignedUserID: "",
|
|
Status: domain.AnnotationTaskStatusPendingAnnotation,
|
|
CurrentRevision: 0,
|
|
PoseSummary: poseSummary,
|
|
CreatedAt: nowISO(),
|
|
}
|
|
if err := runtime.store.UpsertAnnotationTask(ctx, task); err != nil {
|
|
return nil, err
|
|
}
|
|
segments := prelabels[clipID]
|
|
if len(segments) > 0 {
|
|
if err := runtime.store.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{
|
|
ID: domain.NewID("annotation-revision"), OrganizationID: run.OrganizationID, TaskID: task.ID, AnnotationEpoch: task.AnnotationEpoch,
|
|
Revision: 0, TemplateVersion: template.Version, Segments: segments,
|
|
AuthorUserID: "system", ChangeReason: "workflow prelabel", IsRework: false, CountsAsOutput: false, CreatedAt: nowISO(),
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
eventMode := run.Mode
|
|
if eventMode == "" {
|
|
eventMode = domain.RunModeProduction
|
|
}
|
|
_ = runtime.store.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{
|
|
ID: domain.NewID("annotation-worklog"), OrganizationID: run.OrganizationID, ProjectID: run.ProjectID,
|
|
TaskID: task.ID, UserID: "system", SupplierID: config.SupplierID,
|
|
EventType: "created", Mode: eventMode, Temporary: eventMode == domain.RunModeDebug, TemplateVersion: template.Version, OutputDelta: 0, CreatedAt: nowISO(),
|
|
})
|
|
tasks = append(tasks, task)
|
|
}
|
|
if len(tasks) == 0 {
|
|
return nil, fmt.Errorf("annotation gate requires at least one upstream clip video artifact")
|
|
}
|
|
if run.Metadata == nil {
|
|
run.Metadata = &domain.RunMetadata{}
|
|
}
|
|
if run.Metadata.AnnotationTaskIDsByNode == nil {
|
|
run.Metadata.AnnotationTaskIDsByNode = map[string][]string{}
|
|
}
|
|
taskIDs := make([]string, 0, len(tasks))
|
|
for _, task := range tasks {
|
|
taskIDs = append(taskIDs, task.ID)
|
|
}
|
|
run.Metadata.AnnotationTaskIDsByNode[node.ID] = taskIDs
|
|
if err := runtime.ensureAnnotationHumanBatch(ctx, run, node, config, tasks); err != nil {
|
|
return nil, err
|
|
}
|
|
return tasks, nil
|
|
}
|
|
|
|
func (runtime *Runtime) ensureAnnotationHumanBatch(
|
|
ctx context.Context,
|
|
run *domain.RunRecord,
|
|
node domain.WorkflowNode,
|
|
config annotationGateConfig,
|
|
tasks []domain.AnnotationTaskRecord,
|
|
) error {
|
|
batches, err := runtime.store.ListHumanBatches(ctx, run.OrganizationID, domain.HumanBatchQuery{RunID: run.ID})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var batch *domain.HumanBatchRecord
|
|
for index := range batches {
|
|
if batches[index].GateNodeID == node.ID {
|
|
copy := batches[index]
|
|
batch = ©
|
|
break
|
|
}
|
|
}
|
|
if batch == nil {
|
|
created := domain.HumanBatchRecord{
|
|
ProcessingProfile: run.ProcessingProfile,
|
|
OrganizationID: run.OrganizationID,
|
|
PluginID: "annotation-qa",
|
|
ProjectID: run.ProjectID,
|
|
SupplierID: config.SupplierID,
|
|
WorkflowID: run.WorkflowID,
|
|
RunID: run.ID,
|
|
ContractRevision: domain.FanyuV58CaptureContractRevision,
|
|
GateNodeID: node.ID,
|
|
Status: domain.HumanBatchActive,
|
|
Stages: config.HumanStages,
|
|
ReleasePolicy: config.ReleasePolicy,
|
|
PartitionMaxItems: config.PartitionMaxItems,
|
|
PartitionMaxWaitSeconds: config.PartitionMaxWaitSeconds,
|
|
MaxReworkRounds: config.MaxReworkRounds,
|
|
RequirePrePackageAcceptance: config.RequirePrePackageAcceptance,
|
|
ItemCount: len(tasks),
|
|
}
|
|
if len(created.Stages) == 0 {
|
|
created.Stages = domain.DefaultAnnotationHumanStages()
|
|
}
|
|
if err := runtime.store.UpsertHumanBatch(ctx, created); err != nil {
|
|
return err
|
|
}
|
|
batches, err = runtime.store.ListHumanBatches(ctx, run.OrganizationID, domain.HumanBatchQuery{RunID: run.ID})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for index := range batches {
|
|
if batches[index].GateNodeID == node.ID {
|
|
copy := batches[index]
|
|
batch = ©
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if batch == nil {
|
|
return fmt.Errorf("annotation human batch was not persisted for run %s node %s", run.ID, node.ID)
|
|
}
|
|
existingItems, err := runtime.store.ListHumanItems(ctx, run.OrganizationID, domain.HumanItemQuery{BatchID: batch.ID})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
byLegacyTask := make(map[string]struct{}, len(existingItems))
|
|
for _, item := range existingItems {
|
|
byLegacyTask[item.LegacyTaskID] = struct{}{}
|
|
}
|
|
for _, task := range tasks {
|
|
if _, exists := byLegacyTask[task.ID]; exists {
|
|
continue
|
|
}
|
|
itemKey := strings.TrimSpace(task.ArtifactID)
|
|
if itemKey == "" {
|
|
itemKey = task.ID
|
|
}
|
|
initialStageID := "split_annotation"
|
|
if len(batch.Stages) > 0 && strings.TrimSpace(batch.Stages[0].ID) != "" {
|
|
initialStageID = batch.Stages[0].ID
|
|
}
|
|
item := domain.HumanItemRecord{
|
|
ProcessingProfile: batch.ProcessingProfile,
|
|
OrganizationID: run.OrganizationID,
|
|
PluginID: "annotation-qa",
|
|
BatchID: batch.ID,
|
|
ProjectID: run.ProjectID,
|
|
WorkflowID: run.WorkflowID,
|
|
RunID: run.ID,
|
|
GateNodeID: node.ID,
|
|
LegacyTaskID: task.ID,
|
|
Subject: domain.HumanSubjectRef{
|
|
ArtifactID: task.ArtifactID,
|
|
ItemKey: itemKey,
|
|
MediaType: "video/mp4",
|
|
Payload: map[string]any{
|
|
"sourceVideo": task.SourceVideo,
|
|
"clipStart": task.ClipStart,
|
|
"clipEnd": task.ClipEnd,
|
|
"sourceIsFullVideo": task.SourceIsFullVideo,
|
|
"sourceDurationSeconds": task.SourceDurationSeconds,
|
|
"rightArtifactId": task.RightArtifactID,
|
|
"rightSourceVideo": task.RightSourceVideo,
|
|
"templateId": task.TemplateID,
|
|
"poseSummary": task.PoseSummary,
|
|
},
|
|
},
|
|
CurrentStageID: initialStageID,
|
|
Status: domain.HumanItemReady,
|
|
AssignedUserID: "",
|
|
SupplierID: task.SupplierID,
|
|
MaxReworkRounds: batch.MaxReworkRounds,
|
|
Version: 1,
|
|
}
|
|
if err := runtime.store.UpsertHumanItem(ctx, item); err != nil {
|
|
return err
|
|
}
|
|
items, err := runtime.store.ListHumanItems(ctx, run.OrganizationID, domain.HumanItemQuery{BatchID: batch.ID})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, stored := range items {
|
|
if stored.LegacyTaskID == task.ID {
|
|
_ = runtime.store.AppendHumanEvent(ctx, domain.HumanEventRecord{
|
|
OrganizationID: run.OrganizationID, BatchID: batch.ID, ItemID: stored.ID,
|
|
StageID: stored.CurrentStageID, EventType: "created", ActorUserID: "system", ItemVersion: stored.Version,
|
|
Payload: map[string]any{"legacyTaskId": task.ID},
|
|
})
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func annotationRevisionActions(revision domain.AnnotationRevisionRecord) ([]map[string]any, []string) {
|
|
actions := make([]map[string]any, 0, len(revision.Segments))
|
|
objects := make([]string, 0)
|
|
seenObjects := map[string]struct{}{}
|
|
for _, segment := range revision.Segments {
|
|
actions = append(actions, map[string]any{
|
|
"id": segment.ID, "labelId": segment.LabelID, "label": segment.Label,
|
|
"description": segment.Description, "handedness": segment.Handedness,
|
|
"start": segment.Start, "end": segment.End, "confidence": segment.Confidence,
|
|
"phrases": segment.Phrases, "source": segment.Source,
|
|
})
|
|
for _, phrase := range segment.Phrases {
|
|
if _, exists := seenObjects[phrase]; exists || strings.TrimSpace(phrase) == "" {
|
|
continue
|
|
}
|
|
seenObjects[phrase] = struct{}{}
|
|
objects = append(objects, phrase)
|
|
}
|
|
}
|
|
return actions, objects
|
|
}
|
|
|
|
func annotationGateOutputPortNamed(node domain.WorkflowNode, requested string) (string, string) {
|
|
for _, output := range node.Data.Outputs {
|
|
if output.ID == requested {
|
|
return output.ID, output.Name
|
|
}
|
|
}
|
|
return "", ""
|
|
}
|
|
|
|
func annotationGateOutputPort(node domain.WorkflowNode) (string, string) {
|
|
if portID, portName := annotationGateOutputPortNamed(node, "approvedVideos"); portID != "" {
|
|
return portID, portName
|
|
}
|
|
if portID, portName := annotationGateOutputPortNamed(node, "approved"); portID != "" {
|
|
return portID, portName
|
|
}
|
|
if len(node.Data.Outputs) > 0 {
|
|
return node.Data.Outputs[0].ID, node.Data.Outputs[0].Name
|
|
}
|
|
return "approved", "approved"
|
|
}
|
|
|
|
func annotationGateUsesMultiportContract(node domain.WorkflowNode) bool {
|
|
approved, _ := annotationGateOutputPortNamed(node, "approvedVideos")
|
|
annotations, _ := annotationGateOutputPortNamed(node, "annotations")
|
|
return approved != "" && annotations != ""
|
|
}
|
|
|
|
func annotationGateUsesAcceptedDirectoryContract(node domain.WorkflowNode) bool {
|
|
directory, _ := annotationGateOutputPortNamed(node, "acceptedSliceDirectory")
|
|
summary, _ := annotationGateOutputPortNamed(node, "acceptanceSummary")
|
|
return directory != "" && summary != ""
|
|
}
|
|
|
|
func fanyuV5PrePackageAcceptanceEvidence(ctx context.Context, runtime *Runtime, sourceBatch *domain.HumanBatchRecord) (map[string]any, error) {
|
|
if sourceBatch == nil || !sourceBatch.RequirePrePackageAcceptance {
|
|
return map[string]any{"confirmed": true, "legacy": true}, nil
|
|
}
|
|
capture, captureErr := runtime.store.GetCaptureRecordByRunID(ctx, sourceBatch.RunID)
|
|
if captureErr != nil {
|
|
return nil, captureErr
|
|
} else if capture != nil && capture.SamplingDecision != nil && capture.SamplingDecision.Decision == domain.CaptureSamplingNotSelected {
|
|
if sourceBatch.ProcessingProfile == domain.ProcessingProfileRawHuman {
|
|
return map[string]any{"confirmed": true, "sampled": false, "samplingDecision": capture.SamplingDecision.Decision, "samplingProbability": capture.SamplingDecision.Probability, "samplingPolicyRevision": capture.SamplingDecision.PolicyRevision, "samplingRandomValue": capture.SamplingDecision.RandomValue, "samplingDecidedAt": capture.SamplingDecision.DecidedAt, "processingProfile": domain.ProcessingProfileRawHuman}, nil
|
|
}
|
|
return map[string]any{
|
|
"confirmed": true, "sampled": false, "samplingDecision": capture.SamplingDecision.Decision,
|
|
"samplingProbability": capture.SamplingDecision.Probability,
|
|
"samplingPolicyRevision": capture.SamplingDecision.PolicyRevision,
|
|
"samplingRandomValue": capture.SamplingDecision.RandomValue,
|
|
"samplingDecidedAt": capture.SamplingDecision.DecidedAt,
|
|
"poseDelivery": map[string]any{
|
|
"headpose": domain.PoseDeliveryInclude,
|
|
"handpose": domain.PoseDeliveryInclude,
|
|
},
|
|
}, nil
|
|
}
|
|
acceptanceBatchID := "human-batch-fanyu-v5-prepackage-acceptance-" + strings.TrimSpace(sourceBatch.ID)
|
|
acceptanceItemID := "human-item-fanyu-v5-prepackage-acceptance-" + strings.TrimSpace(sourceBatch.ID)
|
|
acceptanceBatch, err := runtime.store.GetHumanBatch(ctx, acceptanceBatchID)
|
|
if err != nil || acceptanceBatch == nil {
|
|
return nil, fmt.Errorf("v5 pre-package acceptance batch is not ready")
|
|
}
|
|
item, err := runtime.store.GetHumanItem(ctx, acceptanceItemID)
|
|
if err != nil || item == nil || item.Status != domain.HumanItemAccepted || strings.TrimSpace(item.AcceptedAt) == "" {
|
|
return nil, fmt.Errorf("v5 pre-package acceptance has not been confirmed")
|
|
}
|
|
payload := map[string]any{}
|
|
if item.Subject.Payload != nil {
|
|
payload = item.Subject.Payload
|
|
}
|
|
if strings.TrimSpace(item.AcceptedByUserID) == "" {
|
|
return nil, fmt.Errorf("v5 pre-package acceptance has no confirmer identity")
|
|
}
|
|
canonical, marshalErr := json.Marshal(map[string]any{"batchId": acceptanceBatch.ID, "itemId": item.ID, "version": item.Version, "acceptedAt": item.AcceptedAt, "confirmedByUserId": item.AcceptedByUserID, "statistics": payload["statistics"], "poseDelivery": payload["poseDelivery"], "sourceBatchId": sourceBatch.ID})
|
|
if marshalErr != nil {
|
|
return nil, marshalErr
|
|
}
|
|
digest := sha256.Sum256(canonical)
|
|
evidence := map[string]any{"confirmed": true, "sampled": true, "samplingDecision": domain.CaptureSamplingSelected, "humanBatchId": acceptanceBatch.ID, "humanItemId": item.ID, "humanItemVersion": item.Version, "acceptedAt": item.AcceptedAt, "confirmedByUserId": item.AcceptedByUserID, "statistics": payload["statistics"], "poseDelivery": payload["poseDelivery"], "canonical": string(canonical), "sha256": hex.EncodeToString(digest[:])}
|
|
if sourceBatch.ProcessingProfile == domain.ProcessingProfileRawHuman {
|
|
evidence["processingProfile"] = domain.ProcessingProfileRawHuman
|
|
}
|
|
if capture != nil && capture.SamplingDecision != nil {
|
|
evidence["samplingProbability"] = capture.SamplingDecision.Probability
|
|
evidence["samplingPolicyRevision"] = capture.SamplingDecision.PolicyRevision
|
|
evidence["samplingRandomValue"] = capture.SamplingDecision.RandomValue
|
|
evidence["samplingDecidedAt"] = capture.SamplingDecision.DecidedAt
|
|
}
|
|
return evidence, nil
|
|
}
|
|
|
|
func annotationArtifactSize(size int64) string {
|
|
if size < 1024 {
|
|
return fmt.Sprintf("%d B", size)
|
|
}
|
|
return fmt.Sprintf("%.1f KiB", float64(size)/1024)
|
|
}
|
|
|
|
// fanyuV5FinalSliceCoordinates keeps the two timeline spaces explicit: the
|
|
// final MP4 is cut from the already-sliced clip (clip-relative seconds), while
|
|
// Headpose/Handpose manifests still use original source-video seconds.
|
|
func fanyuV5FinalSliceCoordinates(task domain.AnnotationTaskRecord, finalSlice domain.SliceSegment) (clipStart, clipEnd, sourceStart, sourceEnd float64, err error) {
|
|
allowedStart, allowedEnd := task.ClipStart, task.ClipEnd
|
|
if task.SourceIsFullVideo {
|
|
allowedStart, allowedEnd = 0, task.SourceDurationSeconds
|
|
}
|
|
if allowedEnd <= allowedStart || finalSlice.End <= finalSlice.Start || finalSlice.Start < allowedStart || finalSlice.End > allowedEnd {
|
|
return 0, 0, 0, 0, fmt.Errorf("final slice is outside annotation task clip range")
|
|
}
|
|
if task.SourceIsFullVideo {
|
|
return finalSlice.Start, finalSlice.End, finalSlice.Start, finalSlice.End, nil
|
|
}
|
|
return finalSlice.Start - task.ClipStart, finalSlice.End - task.ClipStart, finalSlice.Start, finalSlice.End, nil
|
|
}
|
|
|
|
func (runtime *Runtime) releasePartitionArtifacts(
|
|
ctx context.Context,
|
|
run *domain.RunRecord,
|
|
node domain.WorkflowNode,
|
|
execution domain.NodeExecutionRecord,
|
|
partition domain.ReleasePartitionRecord,
|
|
) ([]domain.ExecutionArtifact, error) {
|
|
batch, err := runtime.store.GetHumanBatch(ctx, partition.BatchID)
|
|
if err != nil || batch == nil {
|
|
return nil, fmt.Errorf("load human batch %s: %w", partition.BatchID, err)
|
|
}
|
|
acceptanceEvidence := map[string]any(nil)
|
|
if batch.RequirePrePackageAcceptance {
|
|
var acceptanceErr error
|
|
acceptanceEvidence, acceptanceErr = fanyuV5PrePackageAcceptanceEvidence(ctx, runtime, batch)
|
|
if acceptanceErr != nil {
|
|
return nil, acceptanceErr
|
|
}
|
|
}
|
|
items, err := runtime.store.ListHumanItems(ctx, batch.OrganizationID, domain.HumanItemQuery{BatchID: batch.ID})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
selected := make(map[string]domain.HumanItemRecord, len(partition.ItemIDs))
|
|
for _, item := range items {
|
|
for _, itemID := range partition.ItemIDs {
|
|
if item.ID == itemID {
|
|
selected[item.ID] = item
|
|
}
|
|
}
|
|
}
|
|
if annotationGateUsesAcceptedDirectoryContract(node) {
|
|
directoryPortID, directoryPortName := annotationGateOutputPortNamed(node, "acceptedSliceDirectory")
|
|
summaryPortID, summaryPortName := annotationGateOutputPortNamed(node, "acceptanceSummary")
|
|
artifacts := make([]domain.ExecutionArtifact, 0, len(selected)+1)
|
|
acceptedMaterializations := []map[string]any{}
|
|
for _, itemID := range partition.ItemIDs {
|
|
item, ok := selected[itemID]
|
|
if !ok {
|
|
return nil, fmt.Errorf("release partition item %s is unavailable", itemID)
|
|
}
|
|
materialization, _ := item.Subject.Payload["materialization"].(map[string]any)
|
|
acceptedMaterializations = append(acceptedMaterializations, map[string]any{"artifactId": materialization["artifactId"], "revision": materialization["revision"], "manifestSha256": materialization["manifestSha256"], "humanItemId": item.ID})
|
|
artifactID := strings.TrimSpace(fmt.Sprint(materialization["artifactId"]))
|
|
if artifactID == "" {
|
|
return nil, fmt.Errorf("accepted item %s has no materialized slice directory", item.ID)
|
|
}
|
|
var materialized *domain.ExecutionArtifact
|
|
for index := range run.Artifacts {
|
|
if run.Artifacts[index].ID == artifactID {
|
|
copy := cloneArtifact(run.Artifacts[index])
|
|
materialized = ©
|
|
break
|
|
}
|
|
}
|
|
if materialized == nil {
|
|
return nil, fmt.Errorf("materialized slice artifact %s is unavailable", artifactID)
|
|
}
|
|
materialized.PortID = directoryPortID
|
|
materialized.PortName = directoryPortName
|
|
artifacts = append(artifacts, *materialized)
|
|
}
|
|
qualityReviews := make([]map[string]any, 0, len(selected))
|
|
for _, itemID := range partition.ItemIDs {
|
|
item := selected[itemID]
|
|
if strings.TrimSpace(item.LegacyTaskID) == "" {
|
|
continue
|
|
}
|
|
reviews, reviewErr := runtime.store.ListAnnotationReviews(ctx, batch.OrganizationID, item.LegacyTaskID)
|
|
if reviewErr != nil {
|
|
return nil, reviewErr
|
|
}
|
|
for _, review := range reviews {
|
|
qualityReviews = append(qualityReviews, map[string]any{
|
|
"id": review.ID, "reviewRound": review.ReviewRound, "stage": review.Stage, "confirmations": review.Confirmations, "nextRevision": review.NextRevision, "rejectTargets": review.RejectTargets,
|
|
"taskId": review.TaskID, "revision": review.Revision, "decision": review.Decision,
|
|
"reasonCodes": review.ReasonCodes, "problemSliceIds": review.ProblemSliceIDs,
|
|
"comment": review.Comment, "reviewerUserId": review.ReviewerUserID, "createdAt": review.CreatedAt,
|
|
})
|
|
}
|
|
}
|
|
captureEvidence := map[string]any{}
|
|
if capture, captureErr := runtime.store.GetCaptureRecordByRunID(ctx, run.ID); captureErr != nil {
|
|
return nil, captureErr
|
|
} else if capture != nil {
|
|
captureEvidence = map[string]any{
|
|
"captureRecordId": capture.ID, "captureId": capture.CaptureID, "prefix": capture.Prefix,
|
|
"fingerprint": capture.Fingerprint, "contractRevision": capture.ContractRevision,
|
|
}
|
|
}
|
|
summary := map[string]any{"schemaVersion": "droboticflow.acceptance-summary/v2", "decision": "approved", "partitionId": partition.ID, "itemCount": len(selected), "capture": captureEvidence, "qualityReviews": qualityReviews, "finalAcceptance": acceptanceEvidence}
|
|
classificationMode, err := runtime.classificationDeliveryEnabled(ctx, run.WorkflowID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if classificationMode {
|
|
auditItems := []domain.HumanItemRecord{}
|
|
for _, itemID := range partition.ItemIDs {
|
|
auditItems = append(auditItems, selected[itemID])
|
|
}
|
|
if acceptanceID, ok := acceptanceEvidence["humanItemId"].(string); ok && acceptanceID != "" {
|
|
acceptedItem, err := runtime.store.GetHumanItem(ctx, acceptanceID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if acceptedItem == nil {
|
|
return nil, fmt.Errorf("acceptance audit item missing")
|
|
}
|
|
auditItems = append(auditItems, *acceptedItem)
|
|
}
|
|
trace, err := runtime.deliveryAuditTrace(ctx, batch.OrganizationID, auditItems)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
summary["trace"] = trace
|
|
}
|
|
summary["acceptedMaterializations"] = acceptedMaterializations
|
|
if batch.ProcessingProfile == domain.ProcessingProfileRawHuman {
|
|
summary["processingProfile"] = domain.ProcessingProfileRawHuman
|
|
}
|
|
encoded, _ := json.Marshal(summary)
|
|
summaryArtifact := domain.ExecutionArtifact{ID: fmt.Sprintf("%s-%s-acceptance-summary", run.ID, node.ID), RunID: run.ID, NodeID: node.ID, PortID: summaryPortID, PortName: summaryPortName, Name: "acceptance-summary.json", Type: domain.PortTypeJSON, Size: annotationArtifactSize(int64(len(encoded))), SizeBytes: int64(len(encoded)), Data: summary, Payload: &domain.ArtifactPayload{Mode: domain.ArtifactTransferModeInline, Type: domain.PortTypeJSON, Inline: summary}}
|
|
if classificationMode {
|
|
summaryArtifact, err = runtime.persistClassificationSummary(ctx, run, node, partition.ID, summaryArtifact, encoded)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
artifacts = append(artifacts, summaryArtifact)
|
|
return artifacts, nil
|
|
}
|
|
clips := annotationClipItems(execution.InputArtifacts)
|
|
manifestItems := make([]map[string]any, 0, len(selected))
|
|
videoPortID, videoPortName := annotationGateOutputPort(node)
|
|
annotationPortID, annotationPortName := videoPortID, videoPortName
|
|
summaryPortID, summaryPortName := "", ""
|
|
multiport := annotationGateUsesMultiportContract(node)
|
|
if multiport {
|
|
videoPortID, videoPortName = annotationGateOutputPortNamed(node, "approvedVideos")
|
|
annotationPortID, annotationPortName = annotationGateOutputPortNamed(node, "annotations")
|
|
summaryPortID, summaryPortName = annotationGateOutputPortNamed(node, "aggregateSummary")
|
|
}
|
|
artifacts := make([]domain.ExecutionArtifact, 0, len(selected)+1)
|
|
emittedSourceArtifacts := map[string]bool{}
|
|
for _, itemID := range partition.ItemIDs {
|
|
item, ok := selected[itemID]
|
|
if !ok || item.LegacyTaskID == "" {
|
|
return nil, fmt.Errorf("release partition item %s is unavailable", itemID)
|
|
}
|
|
task, err := runtime.store.GetAnnotationTask(ctx, item.LegacyTaskID)
|
|
if err != nil || task == nil {
|
|
return nil, fmt.Errorf("load annotation task %s: %w", item.LegacyTaskID, err)
|
|
}
|
|
revisions, err := runtime.store.ListAnnotationRevisions(ctx, task.OrganizationID, task.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var acceptedRevision *domain.AnnotationRevisionRecord
|
|
for index := range revisions {
|
|
if revisions[index].Revision == task.CurrentRevision {
|
|
copy := revisions[index]
|
|
acceptedRevision = ©
|
|
}
|
|
}
|
|
if acceptedRevision == nil {
|
|
return nil, fmt.Errorf("release partition task %s has no accepted revision", task.ID)
|
|
}
|
|
var sourceArtifact *domain.ExecutionArtifact
|
|
for index := range execution.InputArtifacts {
|
|
if execution.InputArtifacts[index].ID == task.ArtifactID {
|
|
copy := cloneArtifact(execution.InputArtifacts[index])
|
|
sourceArtifact = ©
|
|
break
|
|
}
|
|
}
|
|
if sourceArtifact == nil {
|
|
return nil, fmt.Errorf("release partition source artifact %s is unavailable", task.ArtifactID)
|
|
}
|
|
base := map[string]any{}
|
|
for key, value := range clips[sourceArtifact.Name] {
|
|
base[key] = value
|
|
}
|
|
clipID := strings.TrimSpace(fmt.Sprint(base["clipId"]))
|
|
if clipID == "" {
|
|
clipID = strings.TrimSuffix(sourceArtifact.Name, filepath.Ext(sourceArtifact.Name))
|
|
}
|
|
slices, sliceErr := runtime.store.ListSliceRevisions(ctx, task.OrganizationID, task.ID)
|
|
if sliceErr != nil {
|
|
return nil, sliceErr
|
|
}
|
|
finalSlices := []domain.SliceSegment{{ID: "original", Start: task.ClipStart, End: task.ClipEnd}}
|
|
if batch.RequirePrePackageAcceptance {
|
|
if len(slices) == 0 {
|
|
return nil, fmt.Errorf("v5 release task %s has no final slice revision", task.ID)
|
|
}
|
|
finalSlices = slices[len(slices)-1].Segments
|
|
}
|
|
for index, finalSlice := range finalSlices {
|
|
clipStart, clipEnd, sourceStart, sourceEnd, coordinateErr := fanyuV5FinalSliceCoordinates(*task, finalSlice)
|
|
if coordinateErr != nil {
|
|
return nil, fmt.Errorf("invalid final slice for task %s: %w", task.ID, coordinateErr)
|
|
}
|
|
// Qwen-VL semantic rough-cut items are virtual clips backed by the
|
|
// complete sanitized source video. Their final cut coordinates are
|
|
// therefore already source-relative and must not be shifted back to a
|
|
// pre-cut clip timeline before the package operator materializes MP4s.
|
|
if sourceIsFullVideo, _ := base["sourceIsFullVideo"].(bool); sourceIsFullVideo {
|
|
clipStart, clipEnd = sourceStart, sourceEnd
|
|
}
|
|
finalClipID := clipID
|
|
if batch.RequirePrePackageAcceptance {
|
|
finalClipID = fmt.Sprintf("%s_final_%03d", clipID, index+1)
|
|
}
|
|
finalActions := make([]map[string]any, 0)
|
|
for _, action := range acceptedRevision.Segments {
|
|
start := math.Max(action.Start, finalSlice.Start)
|
|
end := math.Min(action.End, finalSlice.End)
|
|
if end <= start {
|
|
continue
|
|
}
|
|
finalActions = append(finalActions, map[string]any{"id": action.ID, "labelId": action.LabelID, "label": action.Label, "description": action.Description, "handedness": action.Handedness, "start": start - finalSlice.Start, "end": end - finalSlice.Start, "confidence": action.Confidence, "phrases": action.Phrases, "source": action.Source})
|
|
}
|
|
objects := make([]string, 0)
|
|
seenObjects := map[string]struct{}{}
|
|
for _, action := range finalActions {
|
|
for _, raw := range anySlice(action["phrases"]) {
|
|
phrase := strings.TrimSpace(fmt.Sprint(raw))
|
|
if phrase != "" {
|
|
if _, exists := seenObjects[phrase]; !exists {
|
|
seenObjects[phrase] = struct{}{}
|
|
objects = append(objects, phrase)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
itemBase := map[string]any{}
|
|
for key, value := range base {
|
|
itemBase[key] = value
|
|
}
|
|
itemBase["clipId"] = finalClipID
|
|
itemBase["clipName"] = finalClipID + ".mp4"
|
|
itemBase["clipPath"] = sourceArtifact.Path
|
|
itemBase["sourceClipPath"] = sourceArtifact.Path
|
|
itemBase["startSecond"] = clipStart
|
|
itemBase["endSecond"] = clipEnd
|
|
itemBase["duration"] = clipEnd - clipStart
|
|
itemBase["sourceStartSecond"] = sourceStart
|
|
itemBase["sourceEndSecond"] = sourceEnd
|
|
itemBase["finalSlice"] = finalSlice
|
|
itemBase["finalSegment"] = batch.RequirePrePackageAcceptance
|
|
itemBase["annotationFile"] = finalClipID + "_annotation.json"
|
|
itemBase["annotation"] = map[string]any{"schemaVersion": "droboticflow.annotation.revision/v2", "taskId": task.ID, "revision": acceptedRevision.Revision, "templateVersion": acceptedRevision.TemplateVersion, "clipId": finalClipID, "sourceVideo": sourceArtifact.Name, "duration": clipEnd - clipStart, "actions": finalActions, "objects": objects}
|
|
if batch.RequirePrePackageAcceptance {
|
|
itemBase["finalAcceptanceConfirmed"] = true
|
|
itemBase["finalAcceptance"] = acceptanceEvidence
|
|
}
|
|
manifestItems = append(manifestItems, itemBase)
|
|
}
|
|
if !emittedSourceArtifacts[sourceArtifact.ID] {
|
|
sourceArtifact.PortID = videoPortID
|
|
sourceArtifact.PortName = videoPortName
|
|
artifacts = append(artifacts, *sourceArtifact)
|
|
emittedSourceArtifacts[sourceArtifact.ID] = true
|
|
}
|
|
}
|
|
// HeadPose and HandPose remain independent evidence inputs on the workflow.
|
|
// humanReleaseBaseline preserves their producing node executions, so the
|
|
// package node receives them through the direct pose edges. Duplicating pose
|
|
// JSON here and relabelling it as approvedVideos corrupts the port contract
|
|
// and makes the read-only canvas show non-video evidence as human-approved
|
|
// video output.
|
|
manifest := map[string]any{
|
|
"schemaVersion": "droboticflow.annotation.accepted-partition/v1", "datasetType": "x-platform-annotations",
|
|
"partitionId": partition.ID, "partitionSequence": partition.Sequence, "annotationCount": len(manifestItems),
|
|
"projectId": run.ProjectID, "workflowId": run.WorkflowID, "runId": run.ID, "nodeId": node.ID, "items": manifestItems,
|
|
}
|
|
workspace, err := prepareNodeWorkspace(runtime.workspaceRoot, run.ID, node.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
payload, err := json.MarshalIndent(manifest, "", " ")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
payload = append(payload, '\n')
|
|
name := fmt.Sprintf("annotations-partition-%06d.json", partition.Sequence)
|
|
manifestPath := filepath.Join(workspace.OutputDir, name)
|
|
if err := os.WriteFile(manifestPath, payload, 0o644); err != nil {
|
|
return nil, err
|
|
}
|
|
digest := sha256.Sum256(payload)
|
|
artifacts = append(artifacts, domain.ExecutionArtifact{
|
|
ID: fmt.Sprintf("%s-%s-%s", run.ID, node.ID, name), RunID: run.ID, NodeID: node.ID,
|
|
PortID: annotationPortID, PortName: annotationPortName, Name: "annotations-manifest.json", Type: domain.PortTypeJSON,
|
|
Size: annotationArtifactSize(int64(len(payload))), SizeBytes: int64(len(payload)), Path: manifestPath,
|
|
Metadata: &domain.ArtifactMetadata{SHA256: hex.EncodeToString(digest[:]), MimeType: "application/json"}, Data: manifest,
|
|
})
|
|
if multiport && summaryPortID != "" {
|
|
summary := map[string]any{"annotationCount": len(manifestItems), "partitionSequence": partition.Sequence, "aggregateConfirmed": batch.RequirePrePackageAcceptance}
|
|
encoded, _ := json.Marshal(summary)
|
|
artifacts = append(artifacts, domain.ExecutionArtifact{
|
|
ID: fmt.Sprintf("%s-%s-aggregate-summary", run.ID, node.ID), RunID: run.ID, NodeID: node.ID,
|
|
PortID: summaryPortID, PortName: summaryPortName, Name: "annotation-aggregate-summary.json", Type: domain.PortTypeJSON,
|
|
Size: annotationArtifactSize(int64(len(encoded))), SizeBytes: int64(len(encoded)), Data: summary,
|
|
Payload: &domain.ArtifactPayload{Mode: domain.ArtifactTransferModeInline, Type: domain.PortTypeJSON, Inline: summary},
|
|
})
|
|
}
|
|
return artifacts, nil
|
|
}
|
|
|
|
func (runtime *Runtime) acceptedAnnotationGateArtifacts(
|
|
ctx context.Context,
|
|
run *domain.RunRecord,
|
|
node domain.WorkflowNode,
|
|
execution domain.NodeExecutionRecord,
|
|
) ([]domain.ExecutionArtifact, bool, error) {
|
|
tasks, err := annotationGateTasksForRun(ctx, runtime, run, node)
|
|
if err != nil || len(tasks) == 0 {
|
|
return nil, false, err
|
|
}
|
|
for _, task := range tasks {
|
|
if task.Status != domain.AnnotationTaskStatusAccepted {
|
|
return nil, false, nil
|
|
}
|
|
}
|
|
batches, batchErr := runtime.store.ListHumanBatches(ctx, run.OrganizationID, domain.HumanBatchQuery{RunID: run.ID})
|
|
if batchErr != nil {
|
|
return nil, false, batchErr
|
|
}
|
|
for index := range batches {
|
|
if batches[index].GateNodeID != node.ID || !batches[index].RequirePrePackageAcceptance {
|
|
continue
|
|
}
|
|
if _, acceptanceErr := fanyuV5PrePackageAcceptanceEvidence(ctx, runtime, &batches[index]); acceptanceErr != nil {
|
|
// A pending aggregate acceptance is a normal paused state, not an
|
|
// execution error. The item remains claimable in the acceptance tab.
|
|
return nil, false, nil
|
|
}
|
|
}
|
|
clips := annotationClipItems(execution.InputArtifacts)
|
|
items := make([]map[string]any, 0, len(tasks))
|
|
for _, task := range tasks {
|
|
revisions, err := runtime.store.ListAnnotationRevisions(ctx, task.OrganizationID, task.ID)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
var acceptedRevision *domain.AnnotationRevisionRecord
|
|
for index := range revisions {
|
|
if revisions[index].Revision == task.CurrentRevision {
|
|
acceptedRevision = &revisions[index]
|
|
}
|
|
}
|
|
if acceptedRevision == nil || acceptedRevision.Revision <= 0 {
|
|
return nil, false, fmt.Errorf("accepted annotation task %s has no accepted revision", task.ID)
|
|
}
|
|
var sourceArtifact *domain.ExecutionArtifact
|
|
for index := range execution.InputArtifacts {
|
|
if execution.InputArtifacts[index].ID == task.ArtifactID {
|
|
sourceArtifact = &execution.InputArtifacts[index]
|
|
break
|
|
}
|
|
}
|
|
if sourceArtifact == nil {
|
|
return nil, false, fmt.Errorf("annotation task %s source artifact is unavailable", task.ID)
|
|
}
|
|
base := map[string]any{}
|
|
for key, value := range clips[sourceArtifact.Name] {
|
|
base[key] = value
|
|
}
|
|
clipID := strings.TrimSpace(fmt.Sprint(base["clipId"]))
|
|
if clipID == "" {
|
|
clipID = strings.TrimSuffix(sourceArtifact.Name, filepath.Ext(sourceArtifact.Name))
|
|
}
|
|
actions, objects := annotationRevisionActions(*acceptedRevision)
|
|
base["clipId"] = clipID
|
|
base["clipName"] = sourceArtifact.Name
|
|
base["clipPath"] = sourceArtifact.Path
|
|
base["startSecond"] = task.ClipStart
|
|
base["endSecond"] = task.ClipEnd
|
|
base["duration"] = task.ClipEnd - task.ClipStart
|
|
base["annotationFile"] = clipID + "_annotation.json"
|
|
base["annotation"] = map[string]any{
|
|
"schemaVersion": "droboticflow.annotation.revision/v1",
|
|
"taskId": task.ID, "revision": acceptedRevision.Revision, "templateVersion": acceptedRevision.TemplateVersion,
|
|
"clipId": clipID, "sourceVideo": sourceArtifact.Name, "duration": task.ClipEnd - task.ClipStart,
|
|
"actions": actions, "objects": objects,
|
|
}
|
|
items = append(items, base)
|
|
}
|
|
manifest := map[string]any{
|
|
"schemaVersion": "droboticflow.annotation.accepted/v1",
|
|
"datasetType": "x-platform-annotations",
|
|
"annotationCount": len(items),
|
|
"projectId": run.ProjectID, "dataTaskId": run.TaskID, "workflowId": run.WorkflowID, "runId": run.ID, "nodeId": node.ID,
|
|
"items": items,
|
|
}
|
|
workspace, err := prepareNodeWorkspace(runtime.workspaceRoot, run.ID, node.ID)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
payload, err := json.MarshalIndent(manifest, "", " ")
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
payload = append(payload, '\n')
|
|
manifestPath := filepath.Join(workspace.OutputDir, "annotations-manifest.json")
|
|
if err := os.WriteFile(manifestPath, payload, 0o644); err != nil {
|
|
return nil, false, err
|
|
}
|
|
digest := sha256.Sum256(payload)
|
|
videoPortID, videoPortName := annotationGateOutputPort(node)
|
|
annotationPortID, annotationPortName := videoPortID, videoPortName
|
|
summaryPortID, summaryPortName := "", ""
|
|
multiport := annotationGateUsesMultiportContract(node)
|
|
if multiport {
|
|
videoPortID, videoPortName = annotationGateOutputPortNamed(node, "approvedVideos")
|
|
annotationPortID, annotationPortName = annotationGateOutputPortNamed(node, "annotations")
|
|
summaryPortID, summaryPortName = annotationGateOutputPortNamed(node, "aggregateSummary")
|
|
}
|
|
accepted := make([]domain.ExecutionArtifact, 0, len(execution.InputArtifacts)+1)
|
|
for _, artifact := range execution.InputArtifacts {
|
|
if artifact.Name == "annotations-manifest.json" || strings.HasSuffix(artifact.Name, "_annotation.json") {
|
|
continue
|
|
}
|
|
if multiport && artifact.Type != domain.PortTypeVideo {
|
|
continue
|
|
}
|
|
copy := cloneArtifact(artifact)
|
|
copy.PortID = videoPortID
|
|
copy.PortName = videoPortName
|
|
accepted = append(accepted, copy)
|
|
}
|
|
accepted = append(accepted, domain.ExecutionArtifact{
|
|
ID: fmt.Sprintf("%s-%s-%s-annotations-manifest.json", run.ID, node.ID, annotationPortID),
|
|
RunID: run.ID, NodeID: node.ID, PortID: annotationPortID, PortName: annotationPortName,
|
|
Name: "annotations-manifest.json", Type: domain.PortTypeJSON,
|
|
Size: annotationArtifactSize(int64(len(payload))), SizeBytes: int64(len(payload)), Path: manifestPath,
|
|
Metadata: &domain.ArtifactMetadata{SHA256: hex.EncodeToString(digest[:]), MimeType: "application/json"},
|
|
Data: manifest,
|
|
})
|
|
if multiport && summaryPortID != "" {
|
|
summary := map[string]any{"annotationCount": len(items), "aggregateConfirmed": false}
|
|
encoded, _ := json.Marshal(summary)
|
|
accepted = append(accepted, domain.ExecutionArtifact{
|
|
ID: fmt.Sprintf("%s-%s-aggregate-summary", run.ID, node.ID), RunID: run.ID, NodeID: node.ID,
|
|
PortID: summaryPortID, PortName: summaryPortName, Name: "annotation-aggregate-summary.json", Type: domain.PortTypeJSON,
|
|
Size: annotationArtifactSize(int64(len(encoded))), SizeBytes: int64(len(encoded)), Data: summary,
|
|
Payload: &domain.ArtifactPayload{Mode: domain.ArtifactTransferModeInline, Type: domain.PortTypeJSON, Inline: summary},
|
|
})
|
|
}
|
|
return accepted, true, nil
|
|
}
|
|
|
|
func (runtime *Runtime) resolveAcceptedAnnotationGate(
|
|
ctx context.Context,
|
|
workflow domain.WorkflowRecord,
|
|
run *domain.RunRecord,
|
|
node domain.WorkflowNode,
|
|
executionMap map[string]domain.NodeExecutionRecord,
|
|
allowed map[string]struct{},
|
|
) (bool, []string, *domain.NodeExecutionRecord, error) {
|
|
batches, batchErr := runtime.store.ListHumanBatches(ctx, run.OrganizationID, domain.HumanBatchQuery{RunID: run.ID})
|
|
if batchErr != nil {
|
|
return false, nil, nil, batchErr
|
|
}
|
|
for index := range batches {
|
|
if batches[index].GateNodeID == node.ID && batches[index].RequirePrePackageAcceptance {
|
|
// v5 must release a partition child run after the single aggregate
|
|
// acceptance confirmation. Resuming the parent here would bypass final
|
|
// slice MP4 generation and pre-package evidence injection.
|
|
return false, nil, nil, nil
|
|
}
|
|
}
|
|
execution := executionMap[node.ID]
|
|
artifacts, accepted, err := runtime.acceptedAnnotationGateArtifacts(ctx, run, node, execution)
|
|
if err != nil || !accepted {
|
|
return false, nil, nil, err
|
|
}
|
|
execution.NodeID = node.ID
|
|
execution.NodeLabel = node.Data.Label
|
|
execution.Status = domain.NodeExecutionStatusCompleted
|
|
execution.EndedAt = nowISO()
|
|
execution.Artifacts = artifacts
|
|
execution.Logs = append(execution.Logs, logEntry("INFO", "All annotation tasks accepted; resuming workflow", node.Data.Label))
|
|
tasks, err := annotationGateTasksForRun(ctx, runtime, run, node)
|
|
if err != nil {
|
|
return false, nil, nil, err
|
|
}
|
|
count := int64(len(tasks))
|
|
duration := 0.0
|
|
for _, task := range tasks {
|
|
duration += task.ClipEnd - task.ClipStart
|
|
}
|
|
execution.DataMetrics = &domain.DataMetricsRecord{
|
|
SchemaVersion: 1,
|
|
Stage: domain.DataMetricStageAnnotate,
|
|
Input: domain.DataVolume{TrajectoryCount: &count},
|
|
Output: domain.DataVolume{TrajectoryCount: &count, DurationSeconds: &duration},
|
|
TrainReady: domain.DataVolume{TrajectoryCount: &count, DurationSeconds: &duration},
|
|
Quality: domain.DataMetricQualityExact,
|
|
OccurredAt: execution.EndedAt,
|
|
}
|
|
decisionEdges := domain.GetDecisionOutgoingEdges(workflow.Edges, node.ID, domain.AuditStatusApproved)
|
|
executionMap[node.ID] = execution
|
|
applyExecution(run, execution)
|
|
markUnselectedAuditBranchesCompleted(workflow, executionMap, run, node.ID, decisionEdges, domain.AuditStatusApproved, allowed)
|
|
run.Status = domain.RunStatusRunning
|
|
syncCurrentNodeID(run)
|
|
run.UpdatedAt = nowISO()
|
|
return true, edgeTargets(decisionEdges), &execution, nil
|
|
}
|