forked from longtao.wu/droboticflow
1188 lines
46 KiB
Go
1188 lines
46 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
|
|
WorkgroupID string
|
|
ReleasePolicy domain.HumanReleasePolicy
|
|
PartitionMaxItems int
|
|
PartitionMaxWaitSeconds int
|
|
MaxReworkRounds int
|
|
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)
|
|
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"]),
|
|
WorkgroupID: strings.TrimSpace(params["ANNOTATION_WORKGROUP_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),
|
|
RequirePrePackageAcceptance: strings.EqualFold(strings.TrimSpace(params["REQUIRE_PREPACKAGE_ACCEPTANCE"]), "true") || strings.TrimSpace(params["REQUIRE_PREPACKAGE_ACCEPTANCE"]) == "1",
|
|
}
|
|
}
|
|
|
|
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.
|
|
if artifact.Type == domain.PortTypeVideo && strings.TrimSpace(fmt.Sprint(artifact.Data["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, _ := payload["enabled"].(bool); !enabled {
|
|
continue
|
|
}
|
|
frames := anySlice(payload["frames"])
|
|
declaredCount, countOK := annotationNumber(payload["frameCount"])
|
|
if !countOK || math.IsNaN(declaredCount) || declaredCount <= 0 || int(declaredCount) != len(frames) || len(frames) == 0 {
|
|
summary["headposeInvalid"] = true
|
|
continue
|
|
}
|
|
if _, seen := seenHeadposeArtifacts[artifact.ID]; seen {
|
|
continue
|
|
}
|
|
seenHeadposeArtifacts[artifact.ID] = struct{}{}
|
|
ref["frameCount"] = len(frames)
|
|
summary["headposeAvailable"] = true
|
|
summary["headposeFrameCount"] = summary["headposeFrameCount"].(int) + len(frames)
|
|
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)
|
|
if count, ok := annotationNumber(result["frameCount"]); ok {
|
|
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 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 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) createAnnotationGateTasks(
|
|
ctx context.Context,
|
|
run *domain.RunRecord,
|
|
node domain.WorkflowNode,
|
|
inputs []domain.ExecutionArtifact,
|
|
) ([]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 == "" || config.WorkgroupID == "" {
|
|
return nil, fmt.Errorf("annotation gate requires ANNOTATION_TEMPLATE_ID and ANNOTATION_WORKGROUP_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
|
|
group, err := runtime.store.GetAnnotationWorkgroup(ctx, config.WorkgroupID)
|
|
if err != nil || group == nil {
|
|
return nil, fmt.Errorf("load annotation workgroup %s: %w", config.WorkgroupID, err)
|
|
}
|
|
if (template.ProjectID != "" && template.ProjectID != run.ProjectID) || template.Status != domain.AnnotationTemplatePublished || template.OrganizationID != run.OrganizationID || group.OrganizationID != run.OrganizationID {
|
|
return nil, fmt.Errorf("annotation gate template/workgroup does not belong to the run project and organization")
|
|
}
|
|
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
|
|
}
|
|
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,
|
|
WorkgroupID: group.ID,
|
|
VendorID: group.VendorID,
|
|
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,
|
|
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", WorkgroupID: group.ID, VendorID: group.VendorID,
|
|
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{
|
|
OrganizationID: run.OrganizationID,
|
|
PluginID: "annotation-qa",
|
|
ProjectID: run.ProjectID,
|
|
WorkflowID: run.WorkflowID,
|
|
RunID: run.ID,
|
|
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{
|
|
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,
|
|
"templateId": task.TemplateID,
|
|
"poseSummary": task.PoseSummary,
|
|
},
|
|
},
|
|
CurrentStageID: initialStageID,
|
|
Status: domain.HumanItemReady,
|
|
AssignedUserID: "",
|
|
WorkgroupID: task.WorkgroupID,
|
|
VendorID: task.VendorID,
|
|
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,
|
|
"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 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
|
|
}
|
|
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"], "sourceBatchId": sourceBatch.ID})
|
|
if marshalErr != nil {
|
|
return nil, marshalErr
|
|
}
|
|
digest := sha256.Sum256(canonical)
|
|
return map[string]any{"confirmed": true, "humanBatchId": acceptanceBatch.ID, "humanItemId": item.ID, "humanItemVersion": item.Version, "acceptedAt": item.AcceptedAt, "confirmedByUserId": item.AcceptedByUserID, "statistics": payload["statistics"], "canonical": string(canonical), "sha256": hex.EncodeToString(digest[:])}, 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
|
|
}
|
|
}
|
|
}
|
|
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, "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
|
|
}
|
|
}
|
|
// Preserve scoped pose manifests through the human-release child run. The
|
|
// package operator receives these as evidence and crops their frames to each
|
|
// final segment; it never receives a copied full pose JSON per segment.
|
|
for _, artifact := range execution.InputArtifacts {
|
|
payload := annotationArtifactPayload(artifact)
|
|
if payload == nil {
|
|
continue
|
|
}
|
|
schema := strings.TrimSpace(fmt.Sprint(payload["schemaVersion"]))
|
|
if schema != "droboticflow.headpose/v1" && schema != "droboticflow.wilor.hand_pose_result/v1" {
|
|
continue
|
|
}
|
|
if emittedSourceArtifacts[artifact.ID] {
|
|
continue
|
|
}
|
|
copy := cloneArtifact(artifact)
|
|
copy.PortID = videoPortID
|
|
copy.PortName = videoPortName
|
|
artifacts = append(artifacts, copy)
|
|
emittedSourceArtifacts[artifact.ID] = true
|
|
}
|
|
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
|
|
}
|