From 8aaa50fe3fca259b23b81bf3fed4edc19de918cf Mon Sep 17 00:00:00 2001 From: eust-w Date: Tue, 18 Aug 2026 12:22:11 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Connect=20native=20annotation=20wor?= =?UTF-8?q?kflow=20gate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/annotation-operations.md | 29 + internal/api/annotation_operations.go | 68 ++- internal/api/data_operations_test.go | 55 ++ internal/domain/annotation_operations.go | 2 + internal/domain/types.go | 1 + internal/domain/workflow.go | 15 + internal/domain/workflow_annotation_test.go | 19 + internal/worker/annotation_gate.go | 528 ++++++++++++++++++ internal/worker/runtime.go | 166 +++--- internal/worker/runtime_test.go | 142 +++++ src/components/AnnotationTimelineEditor.tsx | 248 ++++++++ src/lib/annotation-operations.js | 45 ++ src/pages/AnnotationOperations.tsx | 110 ++-- src/pages/Monitoring.tsx | 12 + src/pages/ProjectDetail.tsx | 12 + src/services/api/annotation-operations.ts | 15 +- src/types.ts | 3 + tests/frontend/annotation-operations.test.mjs | 29 + 18 files changed, 1387 insertions(+), 112 deletions(-) create mode 100644 internal/domain/workflow_annotation_test.go create mode 100644 internal/worker/annotation_gate.go create mode 100644 src/components/AnnotationTimelineEditor.tsx diff --git a/docs/annotation-operations.md b/docs/annotation-operations.md index 6092792a..b339e5fc 100644 --- a/docs/annotation-operations.md +++ b/docs/annotation-operations.md @@ -22,6 +22,35 @@ 模板必须发布后才能创建任务。时间段必须位于 clip 范围内、不能重叠,置信度必须位于 `[0, 1]`。质检或验收拒绝会把任务转为 `rework` 并指向下一 revision,不在工作流 DAG 中建立环。 +## 原生工作流 Annotation Gate + +人工审核节点配置 `REVIEW_MODE=annotation` 后成为原生标注门,另外需要: + +- `ANNOTATION_TEMPLATE_ID`:当前项目中已发布的模板。 +- `ANNOTATION_WORKGROUP_ID`:目标供应商分组。 +- `ANNOTATION_ASSIGNED_USER_ID`:可选的首轮标注员。 + +Worker 到达该节点后不会启动 Kubernetes Job,也不会创建通用 AuditTask。它会从上游 `clips-manifest.json`、视频 artifact 和预标 JSON 自动按片段创建 AnnotationTask,并写入 `projectId`、`dataTaskId`、`workflowId`、`runId`、`nodeId`、`artifactId`。Run 保持 `waiting_for_audit`,直到该节点下所有任务完成首轮 revision、质检和最终验收。 + +最终验收通过后,Worker 生成 `droboticflow.annotation.accepted/v1` 的 `annotations-manifest.json`,记录模板版本、人工 revision、片段、artifact 和 Run lineage,然后从节点 `approved` 输出恢复同一个 Run。未选择的分支标记为跳过,不启动空上传 Job;质检或验收打回仅创建新 revision。 + +`/annotation-operations` 支持通过以下参数从项目或监控页深链进入: + +`projectId`、`dataTaskId`、`workflowId`、`runId`、`nodeId`、`artifactId`、`taskId`。 + +## 时间轴交互 + +标注任务使用真实 artifact 视频预览,并提供: + +- 拖动整个语义片段; +- 拖动左右把手调整起止时间; +- 默认 0.1 秒吸附、方向键微调、Shift 加速; +- 播放头与视频时间同步; +- 1–8 倍横向缩放; +- 选中后独立删除,避免点击片段即删除。 + +拖动和缩放在相邻片段及 clip 边界内自动限位,提交 revision 时仍由后端执行范围、重叠和置信度校验。 + ## 工作量口径 首次 revision 产生 `outputDelta=1`;返工 revision 记录工时和返工次数,但 `outputDelta=0`,因此不会重复增加正式产出。质检、验收及拒绝原因分别记录为不可变工作量事件,汇总可按用户、供应商、分组和模板版本查询。 diff --git a/internal/api/annotation_operations.go b/internal/api/annotation_operations.go index f9e26bc5..5497d950 100644 --- a/internal/api/annotation_operations.go +++ b/internal/api/annotation_operations.go @@ -405,8 +405,10 @@ func (server *server) handleAnnotationTemplatePublish(writer http.ResponseWriter type annotationTaskInput struct { ProjectID string `json:"projectId"` + DataTaskID string `json:"dataTaskId,omitempty"` WorkflowID string `json:"workflowId,omitempty"` RunID string `json:"runId,omitempty"` + NodeID string `json:"nodeId,omitempty"` ArtifactID string `json:"artifactId,omitempty"` SourceVideo string `json:"sourceVideo"` ClipStart float64 `json:"clipStart"` @@ -421,6 +423,10 @@ func (server *server) handleAnnotationTasksList(writer http.ResponseWriter, requ status := domain.AnnotationTaskStatus(strings.TrimSpace(request.URL.Query().Get("status"))) workgroupID := strings.TrimSpace(request.URL.Query().Get("workgroupId")) vendorID := strings.TrimSpace(request.URL.Query().Get("vendorId")) + workflowID := strings.TrimSpace(request.URL.Query().Get("workflowId")) + runID := strings.TrimSpace(request.URL.Query().Get("runId")) + nodeID := strings.TrimSpace(request.URL.Query().Get("nodeId")) + artifactID := strings.TrimSpace(request.URL.Query().Get("artifactId")) items, err := server.store.ListAnnotationTasks(request.Context(), server.requestOrganizationID(request), projectID) if err != nil { renderError(writer, http.StatusInternalServerError, err) @@ -446,6 +452,18 @@ func (server *server) handleAnnotationTasksList(writer http.ResponseWriter, requ if vendorID != "" && item.VendorID != vendorID { continue } + if workflowID != "" && item.WorkflowID != workflowID { + continue + } + if runID != "" && item.RunID != runID { + continue + } + if nodeID != "" && item.NodeID != nodeID { + continue + } + if artifactID != "" && item.ArtifactID != artifactID { + continue + } if !annotationManager(principal) && !membershipAllowsTask(memberships, item) { continue } @@ -482,6 +500,11 @@ func (server *server) handleAnnotationTaskCreate(writer http.ResponseWriter, req return } payload.ProjectID = strings.TrimSpace(payload.ProjectID) + payload.DataTaskID = strings.TrimSpace(payload.DataTaskID) + payload.WorkflowID = strings.TrimSpace(payload.WorkflowID) + payload.RunID = strings.TrimSpace(payload.RunID) + payload.NodeID = strings.TrimSpace(payload.NodeID) + payload.ArtifactID = strings.TrimSpace(payload.ArtifactID) payload.SourceVideo = strings.TrimSpace(payload.SourceVideo) payload.TemplateID = strings.TrimSpace(payload.TemplateID) payload.WorkgroupID = strings.TrimSpace(payload.WorkgroupID) @@ -508,9 +531,11 @@ func (server *server) handleAnnotationTaskCreate(writer http.ResponseWriter, req ID: domain.NewID("annotation-task"), OrganizationID: project.OrganizationID, ProjectID: project.ID, - WorkflowID: strings.TrimSpace(payload.WorkflowID), - RunID: strings.TrimSpace(payload.RunID), - ArtifactID: strings.TrimSpace(payload.ArtifactID), + DataTaskID: payload.DataTaskID, + WorkflowID: payload.WorkflowID, + RunID: payload.RunID, + NodeID: payload.NodeID, + ArtifactID: payload.ArtifactID, SourceVideo: payload.SourceVideo, ClipStart: payload.ClipStart, ClipEnd: payload.ClipEnd, @@ -646,6 +671,39 @@ type annotationReviewInput struct { DurationSeconds int64 `json:"durationSeconds,omitempty"` } +func (server *server) queueAcceptedAnnotationGateRun(ctx context.Context, task domain.AnnotationTaskRecord) error { + if task.Status != domain.AnnotationTaskStatusAccepted || strings.TrimSpace(task.RunID) == "" || strings.TrimSpace(task.NodeID) == "" { + return nil + } + tasks, err := server.store.ListAnnotationTasks(ctx, task.OrganizationID, task.ProjectID) + if err != nil { + return err + } + matched := 0 + for _, candidate := range tasks { + if candidate.RunID != task.RunID || candidate.NodeID != task.NodeID { + continue + } + matched++ + if candidate.Status != domain.AnnotationTaskStatusAccepted { + return nil + } + } + if matched == 0 { + return nil + } + run, err := server.store.GetRun(ctx, task.RunID) + if err != nil || run == nil { + return err + } + if run.Status != domain.RunStatusWaitingForAudit || run.CurrentNodeID != task.NodeID { + return nil + } + run.Status = domain.RunStatusQueued + run.UpdatedAt = nowISO() + return server.store.UpsertRun(ctx, *run) +} + func (server *server) handleAnnotationReviewCreate(writer http.ResponseWriter, request *http.Request) { task, err := server.store.GetAnnotationTask(request.Context(), chi.URLParam(request, "id")) if err != nil { @@ -716,6 +774,10 @@ func (server *server) handleAnnotationReviewCreate(writer http.ResponseWriter, r renderError(writer, http.StatusInternalServerError, err) return } + if err := server.queueAcceptedAnnotationGateRun(request.Context(), *task); err != nil { + renderError(writer, http.StatusInternalServerError, fmt.Errorf("resume annotation workflow: %w", err)) + return + } eventType := string(payload.Stage) + "_" + string(payload.Decision) _ = server.store.UpsertAnnotationWorklogEvent(request.Context(), domain.AnnotationWorklogEventRecord{ ID: domain.NewID("annotation-worklog"), OrganizationID: task.OrganizationID, ProjectID: task.ProjectID, diff --git a/internal/api/data_operations_test.go b/internal/api/data_operations_test.go index bb459563..d04c1774 100644 --- a/internal/api/data_operations_test.go +++ b/internal/api/data_operations_test.go @@ -112,6 +112,61 @@ func TestCollectionTaskAcceptsFileCountOnlyTarget(t *testing.T) { } } +func TestAcceptedAnnotationGateQueuesRunOnlyAfterAllTasksAccepted(t *testing.T) { + ctx := context.Background() + dataStore := store.NewMemoryStore() + project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{Name: "Annotation Gate"}) + if err != nil { + t.Fatalf("create project: %v", err) + } + workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{ + ProjectID: project.ID, + Name: "Annotation Gate", + Type: domain.WorkflowTypeBatch, + Nodes: []domain.WorkflowNode{{ID: "annotation-gate", Type: "operator", Data: domain.WorkflowNodeData{Label: "逐段语义标注"}}}, + }) + if err != nil { + t.Fatalf("create workflow: %v", err) + } + run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual) + if err != nil { + t.Fatalf("create run: %v", err) + } + run.Status = domain.RunStatusWaitingForAudit + run.CurrentNodeID = "annotation-gate" + if err := dataStore.UpsertRun(ctx, *run); err != nil { + t.Fatalf("upsert waiting run: %v", err) + } + tasks := []domain.AnnotationTaskRecord{ + {ID: "annotation-1", OrganizationID: project.OrganizationID, ProjectID: project.ID, RunID: run.ID, NodeID: "annotation-gate", Status: domain.AnnotationTaskStatusAccepted}, + {ID: "annotation-2", OrganizationID: project.OrganizationID, ProjectID: project.ID, RunID: run.ID, NodeID: "annotation-gate", Status: domain.AnnotationTaskStatusPendingAcceptance}, + } + for _, item := range tasks { + if err := dataStore.UpsertAnnotationTask(ctx, item); err != nil { + t.Fatalf("upsert annotation task: %v", err) + } + } + server := &server{store: dataStore} + if err := server.queueAcceptedAnnotationGateRun(ctx, tasks[0]); err != nil { + t.Fatalf("queue with pending sibling: %v", err) + } + stillWaiting, _ := dataStore.GetRun(ctx, run.ID) + if stillWaiting.Status != domain.RunStatusWaitingForAudit { + t.Fatalf("run should wait for every task, got %s", stillWaiting.Status) + } + tasks[1].Status = domain.AnnotationTaskStatusAccepted + if err := dataStore.UpsertAnnotationTask(ctx, tasks[1]); err != nil { + t.Fatalf("accept final annotation task: %v", err) + } + if err := server.queueAcceptedAnnotationGateRun(ctx, tasks[1]); err != nil { + t.Fatalf("queue accepted gate: %v", err) + } + queued, _ := dataStore.GetRun(ctx, run.ID) + if queued.Status != domain.RunStatusQueued { + t.Fatalf("all accepted tasks should queue the run, got %s", queued.Status) + } +} + func TestLegacySourceCollectionsProduceEstimatedTrajectoryMetrics(t *testing.T) { metrics := legacyMetricsFromExecution( domain.RunRecord{CreatedAt: "2026-07-23T01:00:00Z"}, diff --git a/internal/domain/annotation_operations.go b/internal/domain/annotation_operations.go index f65e1fea..f13d4bb5 100644 --- a/internal/domain/annotation_operations.go +++ b/internal/domain/annotation_operations.go @@ -111,8 +111,10 @@ type AnnotationTaskRecord struct { ID string `json:"id" bson:"id"` OrganizationID string `json:"organizationId,omitempty" bson:"organizationId,omitempty"` ProjectID string `json:"projectId" bson:"projectId"` + DataTaskID string `json:"dataTaskId,omitempty" bson:"dataTaskId,omitempty"` WorkflowID string `json:"workflowId,omitempty" bson:"workflowId,omitempty"` RunID string `json:"runId,omitempty" bson:"runId,omitempty"` + NodeID string `json:"nodeId,omitempty" bson:"nodeId,omitempty"` ArtifactID string `json:"artifactId,omitempty" bson:"artifactId,omitempty"` SourceVideo string `json:"sourceVideo" bson:"sourceVideo"` ClipStart float64 `json:"clipStart" bson:"clipStart"` diff --git a/internal/domain/types.go b/internal/domain/types.go index 10b8cae0..aab72f03 100644 --- a/internal/domain/types.go +++ b/internal/domain/types.go @@ -608,6 +608,7 @@ type LightweightOperatorCodeManifest struct { type RunMetadata struct { LightweightOperatorCodeManifests map[string]LightweightOperatorCodeManifest `json:"lightweightOperatorCodeManifests,omitempty" bson:"lightweightOperatorCodeManifests,omitempty"` TrainingInputs []TrainingInputRecord `json:"trainingInputs,omitempty" bson:"trainingInputs,omitempty"` + AnnotationTaskIDsByNode map[string][]string `json:"annotationTaskIdsByNode,omitempty" bson:"annotationTaskIdsByNode,omitempty"` } // RunFailure is the machine-readable counterpart to RunRecord.Error. Error is diff --git a/internal/domain/workflow.go b/internal/domain/workflow.go index 21ec90b3..c4528737 100644 --- a/internal/domain/workflow.go +++ b/internal/domain/workflow.go @@ -299,7 +299,22 @@ func GetDecisionOutgoingEdges(edges []WorkflowEdge, nodeID string, decision Audi return outgoing } +func NodeRequiresAnnotation(node WorkflowNode) bool { + if strings.EqualFold(strings.TrimSpace(node.Data.OperatorID), "operator-builtin-annotation-gate") { + return true + } + for _, item := range node.Data.Hyperparams { + if strings.EqualFold(strings.TrimSpace(item.Key), "REVIEW_MODE") && strings.EqualFold(strings.TrimSpace(item.Value), "annotation") { + return true + } + } + return false +} + func NodeRequiresAudit(node WorkflowNode) bool { + if NodeRequiresAnnotation(node) { + return false + } label := strings.ToLower(node.Data.Label) return node.Data.Category == "HUMAN" || strings.Contains(label, "human review") || strings.Contains(node.Data.Label, "人工审核") } diff --git a/internal/domain/workflow_annotation_test.go b/internal/domain/workflow_annotation_test.go new file mode 100644 index 00000000..5e2f0cd2 --- /dev/null +++ b/internal/domain/workflow_annotation_test.go @@ -0,0 +1,19 @@ +package domain + +import "testing" + +func TestAnnotationReviewModeUsesNativeAnnotationGate(t *testing.T) { + node := WorkflowNode{Data: WorkflowNodeData{ + Label: "逐段语义标注", + Category: "HUMAN", + Hyperparams: []HyperParam{ + {Key: "REVIEW_MODE", Value: "annotation"}, + }, + }} + if !NodeRequiresAnnotation(node) { + t.Fatal("annotation review mode should be handled by the native annotation gate") + } + if NodeRequiresAudit(node) { + t.Fatal("annotation gate must not create a generic audit task") + } +} diff --git a/internal/worker/annotation_gate.go b/internal/worker/annotation_gate.go new file mode 100644 index 00000000..f7b39861 --- /dev/null +++ b/internal/worker/annotation_gate.go @@ -0,0 +1,528 @@ +package worker + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "net/url" + "os" + "path/filepath" + "sort" + "strconv" + "strings" + + "droboticflow/internal/domain" +) + +type annotationGateConfig struct { + TemplateID string + WorkgroupID string + AssignedUserID string +} + +func annotationGateConfigFromNode(node domain.WorkflowNode) annotationGateConfig { + params := nodeHyperparamsMap(node) + return annotationGateConfig{ + TemplateID: strings.TrimSpace(params["ANNOTATION_TEMPLATE_ID"]), + WorkgroupID: strings.TrimSpace(params["ANNOTATION_WORKGROUP_ID"]), + AssignedUserID: strings.TrimSpace(params["ANNOTATION_ASSIGNED_USER_ID"]), + } +} + +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 { + 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 +} + +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 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) + } + 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 != 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 { + return existing, nil + } + clips := annotationClipItems(inputs) + prelabels := annotationPrelabels(inputs, *template) + 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, + TemplateID: template.ID, + TemplateVersion: template.Version, + WorkgroupID: group.ID, + VendorID: group.VendorID, + AssignedUserID: config.AssignedUserID, + Status: domain.AnnotationTaskStatusPendingAnnotation, + CurrentRevision: 0, + 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 + } + } + _ = runtime.store.UpsertAnnotationWorklogEvent(ctx, domain.AnnotationWorklogEventRecord{ + ID: domain.NewID("annotation-worklog"), OrganizationID: run.OrganizationID, ProjectID: run.ProjectID, + TaskID: task.ID, UserID: config.AssignedUserID, WorkgroupID: group.ID, VendorID: group.VendorID, + EventType: "workflow_assigned", 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 + return tasks, 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 annotationGateOutputPort(node domain.WorkflowNode) (string, string) { + for _, output := range node.Data.Outputs { + if output.ID == "approved" { + return output.ID, output.Name + } + } + if len(node.Data.Outputs) > 0 { + return node.Data.Outputs[0].ID, node.Data.Outputs[0].Name + } + return "approved", "approved" +} + +func annotationArtifactSize(size int64) string { + if size < 1024 { + return fmt.Sprintf("%d B", size) + } + return fmt.Sprintf("%.1f KiB", float64(size)/1024) +} + +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 + } + } + 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) + portID, portName := annotationGateOutputPort(node) + 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 + } + copy := cloneArtifact(artifact) + copy.PortID = portID + copy.PortName = portName + accepted = append(accepted, copy) + } + accepted = append(accepted, domain.ExecutionArtifact{ + ID: fmt.Sprintf("%s-%s-%s-annotations-manifest.json", run.ID, node.ID, portID), + RunID: run.ID, NodeID: node.ID, PortID: portID, PortName: portName, + 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, + }) + 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) { + 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 +} diff --git a/internal/worker/runtime.go b/internal/worker/runtime.go index 14be59dc..d3d8f572 100644 --- a/internal/worker/runtime.go +++ b/internal/worker/runtime.go @@ -2634,57 +2634,76 @@ func (runtime *Runtime) processRun(ctx context.Context, claimed *domain.RunRecor auditQueue := make([]string, 0) if currentRun.CurrentNodeID != "" { - tasks, err := runtime.store.ListAuditTasks(ctx, "all") - if err != nil { - return nil, err - } - for _, task := range tasks { - if task.RunID == currentRun.ID && task.NodeID == currentRun.CurrentNodeID && task.Status != domain.AuditStatusPending { - node := domain.GetNodeByID(workflow.Nodes, currentRun.CurrentNodeID) - if node != nil { - execution := executionMap[node.ID] - execution.NodeID = node.ID - execution.NodeLabel = node.Data.Label - execution.Status = domain.NodeExecutionStatusCompleted - execution.EndedAt = nowISO() - execution.Logs = append(execution.Logs, logEntry("INFO", fmt.Sprintf("Audit decision: %s", task.Status), node.Data.Label)) - resolvedNode := runtime.resolveNodeBusinessStage(ctx, currentRun, *node) - if resolvedNode.Data.BusinessStage == domain.DataMetricStageAudit { - volume := upstreamBusinessVolume(*workflow, executionMap, node.ID) - execution.DataMetrics = &domain.DataMetricsRecord{ - SchemaVersion: 1, - Stage: domain.DataMetricStageAudit, - Input: volume, - Output: volume, - Quality: domain.DataMetricQualityExact, - OccurredAt: execution.EndedAt, - } - if task.Status == domain.AuditStatusApproved { - execution.DataMetrics.TrainReady = volume - } else { - execution.DataMetrics.Output = domain.DataVolume{} - execution.DataMetrics.Rejected = volume - execution.DataMetrics.RejectionReasons = map[string]int64{"人工审核拒绝": 1} - } - } - decisionEdges := domain.GetDecisionOutgoingEdges(workflow.Edges, node.ID, task.Status) - execution.Artifacts = routeAuditArtifacts(*node, execution.InputArtifacts, decisionEdges) - executionMap[node.ID] = execution - applyExecution(currentRun, execution) - markUnselectedAuditBranchesCompleted(*workflow, executionMap, currentRun, node.ID, decisionEdges, task.Status, allowedNodes) - for _, target := range edgeTargets(decisionEdges) { - queueNodeIfReady(*workflow, executionMap, &readyQueue, readySet, allowedNodes, target) - } - currentRun.Status = domain.RunStatusRunning - syncCurrentNodeID(currentRun) - currentRun.UpdatedAt = nowISO() - if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil { - return nil, err - } - runtime.materializeNodeMetricsBestEffort(ctx, currentRun, &execution) - executionMap[node.ID] = execution + node := domain.GetNodeByID(workflow.Nodes, currentRun.CurrentNodeID) + if node != nil && domain.NodeRequiresAnnotation(*node) { + resolved, targets, execution, resolveErr := runtime.resolveAcceptedAnnotationGate(ctx, *workflow, currentRun, *node, executionMap, allowedNodes) + if resolveErr != nil { + return nil, resolveErr + } + if resolved { + for _, target := range targets { + queueNodeIfReady(*workflow, executionMap, &readyQueue, readySet, allowedNodes, target) + } + if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil { + return nil, err + } + if execution != nil { + runtime.materializeNodeMetricsBestEffort(ctx, currentRun, execution) + executionMap[node.ID] = *execution + } + } + } else { + tasks, err := runtime.store.ListAuditTasks(ctx, "all") + if err != nil { + return nil, err + } + for _, task := range tasks { + if task.RunID == currentRun.ID && task.NodeID == currentRun.CurrentNodeID && task.Status != domain.AuditStatusPending { + if node != nil { + execution := executionMap[node.ID] + execution.NodeID = node.ID + execution.NodeLabel = node.Data.Label + execution.Status = domain.NodeExecutionStatusCompleted + execution.EndedAt = nowISO() + execution.Logs = append(execution.Logs, logEntry("INFO", fmt.Sprintf("Audit decision: %s", task.Status), node.Data.Label)) + resolvedNode := runtime.resolveNodeBusinessStage(ctx, currentRun, *node) + if resolvedNode.Data.BusinessStage == domain.DataMetricStageAudit { + volume := upstreamBusinessVolume(*workflow, executionMap, node.ID) + execution.DataMetrics = &domain.DataMetricsRecord{ + SchemaVersion: 1, + Stage: domain.DataMetricStageAudit, + Input: volume, + Output: volume, + Quality: domain.DataMetricQualityExact, + OccurredAt: execution.EndedAt, + } + if task.Status == domain.AuditStatusApproved { + execution.DataMetrics.TrainReady = volume + } else { + execution.DataMetrics.Output = domain.DataVolume{} + execution.DataMetrics.Rejected = volume + execution.DataMetrics.RejectionReasons = map[string]int64{"人工审核拒绝": 1} + } + } + decisionEdges := domain.GetDecisionOutgoingEdges(workflow.Edges, node.ID, task.Status) + execution.Artifacts = routeAuditArtifacts(*node, execution.InputArtifacts, decisionEdges) + executionMap[node.ID] = execution + applyExecution(currentRun, execution) + markUnselectedAuditBranchesCompleted(*workflow, executionMap, currentRun, node.ID, decisionEdges, task.Status, allowedNodes) + for _, target := range edgeTargets(decisionEdges) { + queueNodeIfReady(*workflow, executionMap, &readyQueue, readySet, allowedNodes, target) + } + currentRun.Status = domain.RunStatusRunning + syncCurrentNodeID(currentRun) + currentRun.UpdatedAt = nowISO() + if err := runtime.store.UpsertRun(ctx, *currentRun); err != nil { + return nil, err + } + runtime.materializeNodeMetricsBestEffort(ctx, currentRun, &execution) + executionMap[node.ID] = execution + } + break } - break } } } @@ -2802,7 +2821,7 @@ func (runtime *Runtime) processRun(ctx context.Context, claimed *domain.RunRecor if !dependenciesComplete(*workflow, executionMap, node.ID) { continue } - if domain.NodeRequiresAudit(*node) { + if domain.NodeRequiresAudit(*node) || domain.NodeRequiresAnnotation(*node) { auditQueue = append(auditQueue, node.ID) continue } @@ -2927,30 +2946,39 @@ func (runtime *Runtime) processRun(ctx context.Context, claimed *domain.RunRecor } } applyNodeExecutionSnapshot(&execution, *node, inputArtifacts, inputLineage) - task := domain.AuditTaskRecord{ - ID: domain.NewID("audit"), - OrganizationID: currentRun.OrganizationID, - RunID: currentRun.ID, - WorkflowID: workflow.ID, - ProjectID: workflow.ProjectID, - NodeID: node.ID, - WorkflowName: workflow.Name, - ProjectName: currentRun.ProjectName, - OperatorName: node.Data.Label, - CreatedAt: nowISO(), - UpdatedAt: nowISO(), - Status: domain.AuditStatusPending, - Data: createAuditPayload(*node, inputArtifacts), - } - if _, err := runtime.store.CreateAuditTask(ctx, task); err != nil { - return nil, err + waitingMessage := "Node is waiting for human review" + if domain.NodeRequiresAnnotation(*node) { + tasks, createErr := runtime.createAnnotationGateTasks(ctx, currentRun, *node, inputArtifacts) + if createErr != nil { + return nil, createErr + } + waitingMessage = fmt.Sprintf("Node is waiting for %d annotation task(s)", len(tasks)) + } else { + task := domain.AuditTaskRecord{ + ID: domain.NewID("audit"), + OrganizationID: currentRun.OrganizationID, + RunID: currentRun.ID, + WorkflowID: workflow.ID, + ProjectID: workflow.ProjectID, + NodeID: node.ID, + WorkflowName: workflow.Name, + ProjectName: currentRun.ProjectName, + OperatorName: node.Data.Label, + CreatedAt: nowISO(), + UpdatedAt: nowISO(), + Status: domain.AuditStatusPending, + Data: createAuditPayload(*node, inputArtifacts), + } + if _, err := runtime.store.CreateAuditTask(ctx, task); err != nil { + return nil, err + } } execution.Status = domain.NodeExecutionStatusWaitingForAudit execution.EndedAt = nowISO() execution.Artifacts = cloneArtifacts(inputArtifacts) execution.InputArtifacts = cloneArtifacts(inputArtifacts) execution.InputLineage = cloneInputLineage(inputLineage) - execution.Logs = append(execution.Logs, logEntry("WARN", "Node is waiting for human review", node.Data.Label)) + execution.Logs = append(execution.Logs, logEntry("WARN", waitingMessage, node.Data.Label)) executionMap[node.ID] = execution applyExecution(currentRun, execution) currentRun.Status = domain.RunStatusWaitingForAudit diff --git a/internal/worker/runtime_test.go b/internal/worker/runtime_test.go index da4ec30f..37376c6b 100644 --- a/internal/worker/runtime_test.go +++ b/internal/worker/runtime_test.go @@ -2028,6 +2028,148 @@ func TestRuntimeRoutesArtifactsAcrossSequentialHumanReviews(t *testing.T) { } } +func TestRuntimeAnnotationGateCreatesTasksAndReturnsAcceptedRevisionArtifact(t *testing.T) { + ctx := context.Background() + dataStore := store.NewMemoryStore() + project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: domain.DefaultOrganizationID, Name: "Native Annotation"}) + if err != nil { + t.Fatalf("create project: %v", err) + } + template := domain.AnnotationTemplateRecord{ + ID: "template-1", OrganizationID: project.OrganizationID, ProjectID: project.ID, + Name: "动作模板", Version: 1, Status: domain.AnnotationTemplatePublished, + Labels: []domain.SemanticLabelDefinition{{ID: "steady", Name: "平稳活动"}, {ID: "fast", Name: "快速移动"}}, + } + group := domain.AnnotationWorkgroupRecord{ID: "group-1", OrganizationID: project.OrganizationID, Name: "供应商 A", VendorID: "vendor-a", Status: "active"} + if err := dataStore.UpsertAnnotationTemplate(ctx, template); err != nil { + t.Fatalf("upsert template: %v", err) + } + if err := dataStore.UpsertAnnotationWorkgroup(ctx, group); err != nil { + t.Fatalf("upsert group: %v", err) + } + workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{ + OrganizationID: project.OrganizationID, + ProjectID: project.ID, + Name: "Native Annotation", + Type: domain.WorkflowTypeBatch, + Nodes: []domain.WorkflowNode{ + {ID: "source", Type: "operator", Data: domain.WorkflowNodeData{Label: "source", Outputs: []domain.PortDefinition{{ID: "out-1", Name: "clips", Type: domain.PortTypeJSON}}}}, + {ID: "annotation-gate", Type: "operator", Data: domain.WorkflowNodeData{ + Label: "逐段语义标注", Category: "HUMAN", + Inputs: []domain.PortDefinition{{ID: "in-1", Name: "clips", Type: domain.PortTypeJSON}}, + Outputs: []domain.PortDefinition{{ID: "approved", Name: "approved", Type: domain.PortTypeJSON}}, + Hyperparams: []domain.HyperParam{ + {Key: "REVIEW_MODE", Value: "annotation"}, + {Key: "ANNOTATION_TEMPLATE_ID", Value: template.ID}, + {Key: "ANNOTATION_WORKGROUP_ID", Value: group.ID}, + }, + }}, + }, + Edges: []domain.WorkflowEdge{{ID: "source-gate", Source: "source", SourceHandle: "out-1", Target: "annotation-gate", TargetHandle: "in-1"}}, + }) + if err != nil { + t.Fatalf("create workflow: %v", err) + } + run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerManual) + if err != nil { + t.Fatalf("create run: %v", err) + } + root := t.TempDir() + videoPath := filepath.Join(root, "clip001.mp4") + if err := os.WriteFile(videoPath, []byte("real-clip-placeholder"), 0o644); err != nil { + t.Fatalf("write video fixture: %v", err) + } + clipsPath := filepath.Join(root, "clips-manifest.json") + clipsPayload := map[string]any{ + "clipCount": 1, + "items": []any{map[string]any{ + "clipId": "clip001", "clipName": "clip001.mp4", "clipPath": videoPath, + "startSecond": 0.0, "endSecond": 10.0, "duration": 10.0, + }}, + } + clipsBytes, _ := json.Marshal(clipsPayload) + if err := os.WriteFile(clipsPath, clipsBytes, 0o644); err != nil { + t.Fatalf("write clips manifest: %v", err) + } + prelabelPath := filepath.Join(root, "clip001_annotation.json") + prelabelPayload := map[string]any{ + "clipId": "clip001", + "actions": []any{map[string]any{"label": "平稳活动", "start": 0.0, "end": 10.0, "confidence": 0.8}}, + } + prelabelBytes, _ := json.Marshal(prelabelPayload) + if err := os.WriteFile(prelabelPath, prelabelBytes, 0o644); err != nil { + t.Fatalf("write prelabel: %v", err) + } + run.Status = domain.RunStatusQueued + run.CurrentNodeID = "" + run.NodeExecutions = []domain.NodeExecutionRecord{{ + NodeID: "source", NodeLabel: "source", Attempt: 1, Status: domain.NodeExecutionStatusCompleted, + StartedAt: nowISO(), EndedAt: nowISO(), Logs: []domain.LogEntryRecord{}, ResourceSeries: []domain.ResourceSample{}, + Artifacts: []domain.ExecutionArtifact{ + {ID: "artifact-video", RunID: run.ID, NodeID: "source", PortID: "out-1", Name: "clip001.mp4", Type: domain.PortTypeVideo, Path: videoPath, Data: map[string]any{"preview": map[string]any{"duration": 10.0}}}, + {ID: "artifact-clips", RunID: run.ID, NodeID: "source", PortID: "out-1", Name: "clips-manifest.json", Type: domain.PortTypeJSON, Path: clipsPath, Data: clipsPayload}, + {ID: "artifact-prelabel", RunID: run.ID, NodeID: "source", PortID: "out-1", Name: "clip001_annotation.json", Type: domain.PortTypeJSON, Path: prelabelPath, Data: prelabelPayload}, + }, + }} + if err := dataStore.UpsertRun(ctx, *run); err != nil { + t.Fatalf("upsert seeded run: %v", err) + } + runtime := newTestRuntimeWithLauncher(t, dataStore, testLauncher{launch: func(context.Context, k8s.LaunchInput) (*k8s.LaunchResult, error) { + return nil, fmt.Errorf("annotation gate should not launch a Kubernetes job") + }}) + runtime.workspaceRoot = root + if _, err := runtime.ProcessOnce(ctx); err != nil { + t.Fatalf("process to annotation gate: %v", err) + } + waiting, err := dataStore.GetRun(ctx, run.ID) + if err != nil || waiting == nil || waiting.Status != domain.RunStatusWaitingForAudit { + t.Fatalf("annotation gate did not pause run: run=%#v err=%v", waiting, err) + } + tasks, err := dataStore.ListAnnotationTasks(ctx, project.OrganizationID, project.ID) + if err != nil || len(tasks) != 1 { + t.Fatalf("annotation tasks = %#v err=%v", tasks, err) + } + if tasks[0].RunID != run.ID || tasks[0].NodeID != "annotation-gate" || tasks[0].ArtifactID != "artifact-video" || tasks[0].DataTaskID != run.TaskID { + t.Fatalf("annotation task linkage = %#v", tasks[0]) + } + revisions, err := dataStore.ListAnnotationRevisions(ctx, project.OrganizationID, tasks[0].ID) + if err != nil || len(revisions) != 1 || revisions[0].Revision != 0 || len(revisions[0].Segments) != 1 { + t.Fatalf("prelabel revision = %#v err=%v", revisions, err) + } + acceptedSegments := []domain.AnnotationSegment{{ID: "segment-1", LabelID: "fast", Label: "快速移动", Start: 0, End: 10, Confidence: 1, Source: "manual"}} + if err := dataStore.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ + ID: "revision-accepted", OrganizationID: project.OrganizationID, TaskID: tasks[0].ID, + Revision: 1, TemplateVersion: 1, Segments: acceptedSegments, AuthorUserID: "annotator", CountsAsOutput: true, CreatedAt: nowISO(), + }); err != nil { + t.Fatalf("upsert accepted revision: %v", err) + } + tasks[0].CurrentRevision = 1 + tasks[0].Status = domain.AnnotationTaskStatusAccepted + if err := dataStore.UpsertAnnotationTask(ctx, tasks[0]); err != nil { + t.Fatalf("accept annotation task: %v", err) + } + waiting.Status = domain.RunStatusQueued + if err := dataStore.UpsertRun(ctx, *waiting); err != nil { + t.Fatalf("queue accepted annotation run: %v", err) + } + if _, err := runtime.ProcessOnce(ctx); err != nil { + t.Fatalf("resume accepted annotation run: %v", err) + } + completed, err := dataStore.GetRun(ctx, run.ID) + if err != nil || completed == nil || completed.Status != domain.RunStatusCompleted { + t.Fatalf("completed annotation run = %#v err=%v", completed, err) + } + gateExecution := mustNodeExecution(t, completed, "annotation-gate") + manifest := mustArtifact(t, gateExecution, "annotations-manifest.json") + if manifest.Metadata == nil || manifest.Metadata.SHA256 == "" || manifest.PortID != "approved" { + t.Fatalf("accepted annotation artifact = %#v", manifest) + } + manifestItems := anySlice(manifest.Data["items"]) + if len(manifestItems) != 1 { + t.Fatalf("accepted manifest items = %#v", manifest.Data["items"]) + } +} + func TestRuntimeRoutesArtifactsByOutputHandle(t *testing.T) { ctx := context.Background() dataStore := store.NewMemoryStore() diff --git a/src/components/AnnotationTimelineEditor.tsx b/src/components/AnnotationTimelineEditor.tsx new file mode 100644 index 00000000..1f7a317b --- /dev/null +++ b/src/components/AnnotationTimelineEditor.tsx @@ -0,0 +1,248 @@ +import React, { useMemo, useRef, useState } from "react"; +import { Minus, Plus, Trash2, ZoomIn } from "lucide-react"; +import type { AnnotationSegment, SemanticLabelDefinition } from "../types"; +import { + annotationSegmentPosition, + editAnnotationSegment, +} from "../lib/annotation-operations.js"; +import { buildBrowserPlayableVideoURL } from "../lib/video-preview.js"; + +type DragMode = "move" | "resize-start" | "resize-end"; + +type DragState = { + segmentId: string; + mode: DragMode; + pointerId: number; + originX: number; + initialSegments: AnnotationSegment[]; +}; + +type AnnotationTimelineEditorProps = { + videoSource: string; + clipStart: number; + clipEnd: number; + segments: AnnotationSegment[]; + labels: SemanticLabelDefinition[]; + onChange: (segments: AnnotationSegment[]) => void; + readOnly?: boolean; + snapSeconds?: number; +}; + +const formatSeconds = (value: number) => `${Math.max(0, value).toFixed(2)}s`; + +export default function AnnotationTimelineEditor({ + videoSource, + clipStart, + clipEnd, + segments, + labels, + onChange, + readOnly = false, + snapSeconds = 0.1, +}: AnnotationTimelineEditorProps) { + const videoRef = useRef(null); + const timelineRef = useRef(null); + const dragRef = useRef(null); + const [selectedSegmentId, setSelectedSegmentId] = useState(""); + const [currentTime, setCurrentTime] = useState(clipStart); + const [zoom, setZoom] = useState(1); + const duration = Math.max(clipEnd - clipStart, 0.001); + const playableSource = buildBrowserPlayableVideoURL(videoSource); + const labelColors = useMemo(() => new Map(labels.map((label) => [label.id, label.color || "#2563eb"])), [labels]); + const selectedSegment = segments.find((segment) => segment.id === selectedSegmentId); + + const beginDrag = (event: React.PointerEvent, segmentId: string, mode: DragMode) => { + if (readOnly) return; + event.preventDefault(); + event.stopPropagation(); + event.currentTarget.setPointerCapture(event.pointerId); + dragRef.current = { + segmentId, + mode, + pointerId: event.pointerId, + originX: event.clientX, + initialSegments: segments.map((segment) => ({ ...segment })), + }; + setSelectedSegmentId(segmentId); + }; + + const moveDrag = (event: React.PointerEvent) => { + const drag = dragRef.current; + const timeline = timelineRef.current; + if (!drag || !timeline || drag.pointerId !== event.pointerId) return; + const rect = timeline.getBoundingClientRect(); + const deltaSeconds = ((event.clientX - drag.originX) / Math.max(rect.width, 1)) * duration; + onChange( + editAnnotationSegment( + drag.initialSegments, + drag.segmentId, + drag.mode, + deltaSeconds, + clipStart, + clipEnd, + snapSeconds, + ), + ); + }; + + const endDrag = (event: React.PointerEvent) => { + if (dragRef.current?.pointerId === event.pointerId) dragRef.current = null; + }; + + const seek = (event: React.PointerEvent) => { + if (dragRef.current || event.target !== event.currentTarget) return; + const rect = event.currentTarget.getBoundingClientRect(); + const next = clipStart + ((event.clientX - rect.left) / Math.max(rect.width, 1)) * duration; + const clamped = Math.max(clipStart, Math.min(clipEnd, next)); + setCurrentTime(clamped); + if (videoRef.current) videoRef.current.currentTime = Math.max(0, clamped - clipStart); + }; + + const editSelected = (mode: DragMode, delta: number) => { + if (!selectedSegmentId || readOnly) return; + onChange(editAnnotationSegment(segments, selectedSegmentId, mode, delta, clipStart, clipEnd, snapSeconds)); + }; + + const handleSegmentKey = (event: React.KeyboardEvent, segmentId: string) => { + if (readOnly || (event.key !== "ArrowLeft" && event.key !== "ArrowRight")) return; + event.preventDefault(); + const direction = event.key === "ArrowLeft" ? -1 : 1; + const amount = direction * snapSeconds * (event.shiftKey ? 10 : 1); + onChange(editAnnotationSegment(segments, segmentId, "move", amount, clipStart, clipEnd, snapSeconds)); + }; + + const removeSelected = () => { + if (!selectedSegmentId || readOnly) return; + onChange(segments.filter((segment) => segment.id !== selectedSegmentId)); + setSelectedSegmentId(""); + }; + + const playhead = annotationSegmentPosition( + { start: currentTime, end: currentTime + Math.max(duration / 1000, 0.001) }, + clipStart, + clipEnd, + ); + + return ( +
+
+ {playableSource ? ( +
+ +
+
+ 播放头 {formatSeconds(currentTime)} · 吸附 {snapSeconds.toFixed(1)}s +
+ +
+ +
+
+
+ {formatSeconds(clipStart)} + {formatSeconds(clipEnd)} +
+ +
+ + {selectedSegment ? ( +
+ {selectedSegment.label} + + + {selectedSegment.start.toFixed(2)}–{selectedSegment.end.toFixed(2)}s + {!readOnly ? ( + + ) : null} +
+ ) : null} +
+ ); +} diff --git a/src/lib/annotation-operations.js b/src/lib/annotation-operations.js index 1fa777c4..5edbe4e1 100644 --- a/src/lib/annotation-operations.js +++ b/src/lib/annotation-operations.js @@ -33,6 +33,51 @@ export function annotationSegmentPosition(segment, clipStart, clipEnd) { return { left, width }; } +function clamp(value, minimum, maximum) { + return Math.max(minimum, Math.min(maximum, value)); +} + +function snap(value, clipStart, step) { + const size = Math.max(Number(step) || 0.1, 0.001); + return Number((Number(clipStart || 0) + Math.round((value - Number(clipStart || 0)) / size) * size).toFixed(6)); +} + +export function editAnnotationSegment( + segments, + segmentId, + mode, + deltaSeconds, + clipStart, + clipEnd, + stepSeconds = 0.1, +) { + const normalized = normalizeAnnotationSegments(segments, clipStart, clipEnd); + const index = normalized.findIndex((item) => item.id === segmentId); + if (index < 0) return normalized; + const segment = normalized[index]; + const minimum = Number(clipStart) || 0; + const maximum = Number(clipEnd) || minimum; + const step = Math.max(Number(stepSeconds) || 0.1, 0.001); + const previousEnd = index > 0 ? normalized[index - 1].end : minimum; + const nextStart = index < normalized.length - 1 ? normalized[index + 1].start : maximum; + const delta = Number(deltaSeconds) || 0; + let start = segment.start; + let end = segment.end; + + if (mode === "move") { + const duration = segment.end - segment.start; + start = clamp(snap(segment.start + delta, minimum, step), previousEnd, nextStart - duration); + end = start + duration; + } else if (mode === "resize-start") { + start = clamp(snap(segment.start + delta, minimum, step), previousEnd, segment.end - step); + } else if (mode === "resize-end") { + end = clamp(snap(segment.end + delta, minimum, step), segment.start + step, nextStart); + } + + normalized[index] = { ...segment, start: Number(start.toFixed(6)), end: Number(end.toFixed(6)), source: "manual" }; + return normalized; +} + export function summarizeAnnotationWorkload(items) { return (Array.isArray(items) ? items : []).reduce( (summary, item) => ({ diff --git a/src/pages/AnnotationOperations.tsx b/src/pages/AnnotationOperations.tsx index f35e1bb6..32edd8eb 100644 --- a/src/pages/AnnotationOperations.tsx +++ b/src/pages/AnnotationOperations.tsx @@ -1,4 +1,5 @@ import React, { useCallback, useEffect, useMemo, useState } from "react"; +import { Link, useSearchParams } from "react-router-dom"; import { CheckCircle2, ClipboardCheck, @@ -28,12 +29,12 @@ import { useTimeZone } from "../context/TimeZoneContext"; import { annotationOperationsApi } from "../services/api/annotation-operations"; import { projectsApi } from "../services/api/projects"; import { - annotationSegmentPosition, normalizeAnnotationSegments, summarizeAnnotationWorkload, } from "../lib/annotation-operations.js"; import { formatCompactDateTime } from "../lib/datetime"; import { getActionErrorMessage } from "../lib/action-errors.js"; +import AnnotationTimelineEditor from "../components/AnnotationTimelineEditor"; type Tab = "queue" | "templates" | "groups" | "workload"; @@ -69,6 +70,14 @@ const inputClass = export default function AnnotationOperations() { const { users, role } = useUser(); const { timeZone } = useTimeZone(); + const [searchParams, setSearchParams] = useSearchParams(); + const requestedProjectId = searchParams.get("projectId")?.trim() || ""; + const requestedDataTaskId = searchParams.get("dataTaskId")?.trim() || ""; + const requestedWorkflowId = searchParams.get("workflowId")?.trim() || ""; + const requestedRunId = searchParams.get("runId")?.trim() || ""; + const requestedNodeId = searchParams.get("nodeId")?.trim() || ""; + const requestedArtifactId = searchParams.get("artifactId")?.trim() || ""; + const requestedTaskId = searchParams.get("taskId")?.trim() || ""; const [tab, setTab] = useState("queue"); const [projects, setProjects] = useState([]); const [projectId, setProjectId] = useState(""); @@ -106,13 +115,15 @@ export default function AnnotationOperations() { ]); setProjects(nextProjects); setWorkgroups(nextGroups); - setProjectId((current) => current || nextProjects[0]?.id || ""); + setProjectId((current) => + current || nextProjects.find((item) => item.id === requestedProjectId)?.id || nextProjects[0]?.id || "", + ); } catch (loadError) { setError(getActionErrorMessage(loadError, "加载标注运营基础数据失败")); } finally { setLoading(false); } - }, []); + }, [requestedProjectId]); const loadProject = useCallback(async () => { if (!projectId) { @@ -128,22 +139,30 @@ export default function AnnotationOperations() { const [nextMemberships, nextTemplates, nextTasks, nextWorkload] = await Promise.all([ annotationOperationsApi.listMemberships(projectId), annotationOperationsApi.listTemplates(projectId), - annotationOperationsApi.listTasks({ projectId }), + annotationOperationsApi.listTasks({ + projectId, + workflowId: requestedWorkflowId || undefined, + runId: requestedRunId || undefined, + nodeId: requestedNodeId || undefined, + artifactId: requestedArtifactId || undefined, + }), annotationOperationsApi.workloadSummary(projectId), ]); setMemberships(nextMemberships); setTemplates(nextTemplates); setTasks(nextTasks); setWorkload(nextWorkload); - setSelectedTaskId((current) => - nextTasks.some((item) => item.id === current) ? current : nextTasks[0]?.id || "", - ); + setSelectedTaskId((current) => { + if (nextTasks.some((item) => item.id === current)) return current; + if (nextTasks.some((item) => item.id === requestedTaskId)) return requestedTaskId; + return nextTasks[0]?.id || ""; + }); } catch (loadError) { setError(getActionErrorMessage(loadError, "加载项目标注运营数据失败")); } finally { setLoading(false); } - }, [projectId]); + }, [projectId, requestedArtifactId, requestedNodeId, requestedRunId, requestedTaskId, requestedWorkflowId]); useEffect(() => { void loadBase(); @@ -295,6 +314,11 @@ export default function AnnotationOperations() { try { await annotationOperationsApi.createTask({ projectId, + dataTaskId: requestedDataTaskId || undefined, + workflowId: requestedWorkflowId || undefined, + runId: requestedRunId || undefined, + nodeId: requestedNodeId || undefined, + artifactId: requestedArtifactId || undefined, sourceVideo: taskDraft.sourceVideo, clipStart: Number(taskDraft.clipStart), clipEnd: Number(taskDraft.clipEnd), @@ -311,6 +335,23 @@ export default function AnnotationOperations() { } }; + const selectProject = (nextProjectId: string) => { + setProjectId(nextProjectId); + const next = new URLSearchParams(searchParams); + if (nextProjectId) next.set("projectId", nextProjectId); + else next.delete("projectId"); + next.delete("taskId"); + setSearchParams(next, { replace: true }); + }; + + const selectTask = (taskId: string) => { + setSelectedTaskId(taskId); + const next = new URLSearchParams(searchParams); + if (taskId) next.set("taskId", taskId); + else next.delete("taskId"); + setSearchParams(next, { replace: true }); + }; + return (
@@ -322,7 +363,7 @@ export default function AnnotationOperations() {

- selectProject(event.target.value)}> {projects.map((project) => (
+ {task.runId ?
Run {task.runId}
: null} ))} {tasks.length === 0 ?
暂无可见任务
: null} @@ -470,44 +512,34 @@ export default function AnnotationOperations() {
-
{detail.task.sourceVideo}
+
{detail.task.artifactId || detail.task.sourceVideo}
模板 v{detail.task.templateVersion} · {statusLabels[detail.task.status]}
+
+ {detail.task.runId ? ( + + 返回 Run 监控 + + ) : null} + {detail.task.workflowId ? Workflow {detail.task.workflowId} : null} + {detail.task.dataTaskId ? DataTask {detail.task.dataTaskId} : null} +
Revision {detail.task.currentRevision}
-
-
- {detail.task.clipStart.toFixed(2)}s - {detail.task.clipEnd.toFixed(2)}s -
-
- {segments.map((segment) => { - const position = annotationSegmentPosition(segment, detail.task.clipStart, detail.task.clipEnd); - const color = - selectedTemplate?.labels.find((item) => item.id === segment.labelId)?.color || "#2563eb"; - return ( - - ); - })} -
-
+