From b085e371b230b304fb0446040dee98793dc2e545 Mon Sep 17 00:00:00 2001 From: eust-w Date: Tue, 1 Sep 2026 02:40:27 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Complete=20Fanyu=20Capture=20operat?= =?UTF-8?q?ions=20gaps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/operators/builtin/package/operator.py | 84 ++++++++++- docs/fanyu-v5-workflow.md | 2 +- internal/api/annotation_labels.go | 19 +++ internal/api/capture_automation.go | 6 + internal/api/capture_automation_test.go | 41 +++++ internal/api/human_loop.go | 8 +- internal/api/human_slice_materialization.go | 11 +- internal/api/project_members.go | 61 ++++++-- internal/api/project_members_test.go | 74 +++++++++ internal/api/server.go | 14 +- internal/api/tenant.go | 23 ++- internal/api/workflow_runtime_policy_test.go | 13 ++ internal/domain/annotation_labels.go | 25 +++- internal/domain/annotation_operations.go | 2 + internal/domain/capture_automation.go | 1 + internal/worker/annotation_gate.go | 53 ++++++- internal/worker/annotation_supplier_test.go | 15 ++ .../builtin/package/src/operator.py | 84 ++++++++++- src/App.tsx | 7 +- src/components/AnnotationLabelsPanel.tsx | 140 ++++++++++++++---- src/components/AnnotationWorkbenchEditor.tsx | 71 ++++++++- src/components/Sidebar.tsx | 2 +- src/context/UserContext.tsx | 2 +- src/pages/AnnotationOperations.tsx | 8 + src/pages/DataAssets.tsx | 70 +++++---- src/pages/ProjectDetail.tsx | 14 +- src/services/api/human-loop.ts | 3 +- src/types.ts | 16 ++ .../test_fanyu_annotation_operators.py | 17 ++- tests/frontend/annotation-operations.test.mjs | 8 + tests/frontend/data-assets.test.mjs | 8 +- .../frontend/fanyu-comment-followup.test.mjs | 7 +- .../project-membership-cascade.test.mjs | 1 + 33 files changed, 796 insertions(+), 114 deletions(-) diff --git a/backend/operators/builtin/package/operator.py b/backend/operators/builtin/package/operator.py index 4c24a048..92d7a8d5 100644 --- a/backend/operators/builtin/package/operator.py +++ b/backend/operators/builtin/package/operator.py @@ -401,6 +401,53 @@ def _read_imu(path: pathlib.Path) -> list[dict[str, Any]]: return result +def _read_materialized_pts(path: pathlib.Path) -> list[int]: + header, rows = _read_rows(path) + normalized = {name.lower(): index for index, name in enumerate(header)} + timestamp_column = normalized.get("timestamp_ms") + if timestamp_column is None: + raise RuntimeError(f"materialized PTS CSV must contain timestamp_ms: {path.name}") + values: list[int] = [] + for row in rows: + try: + value = int(round(float(row[timestamp_column]) * 1000)) + except (IndexError, ValueError) as exc: + raise RuntimeError(f"materialized PTS CSV contains an invalid timestamp_ms: {path.name}") from exc + if values and value <= values[-1]: + raise RuntimeError(f"materialized PTS CSV timestamp_ms must be strictly increasing: {path.name}") + values.append(value) + if not values: + raise RuntimeError(f"materialized PTS CSV has no frame timestamps: {path.name}") + return values + + +def _validate_materialized_imu(path: pathlib.Path, start_us: int, end_us: int) -> int: + header, rows = _read_rows(path) + indexes = {name.lower(): index for index, name in enumerate(header)} + required = {"timestamp_ms", "clip_time_ms", "x", "y", "z", "type"} + if not required.issubset(indexes): + raise RuntimeError(f"materialized IMU CSV is missing required columns: {path.name}") + previous = -1 + seen_types: dict[int, set[str]] = {} + for row in rows: + try: + timestamp_us = int(round(float(row[indexes["timestamp_ms"]]) * 1000)) + kind = row[indexes["type"]].lower() + for key in ("x", "y", "z"): + float(row[indexes[key]]) + except (IndexError, ValueError) as exc: + raise RuntimeError(f"materialized IMU CSV contains an invalid row: {path.name}") from exc + if timestamp_us < previous or kind not in {"accel", "gyro"}: + raise RuntimeError(f"materialized IMU ordering/type is invalid: {path.name}") + if timestamp_us < start_us or timestamp_us >= end_us: + raise RuntimeError(f"materialized IMU row is outside the synchronized slice window: {path.name}") + previous = timestamp_us + seen_types.setdefault(timestamp_us, set()).add(kind) + if not rows or any(types != {"accel", "gyro"} for types in seen_types.values()): + raise RuntimeError(f"materialized IMU must contain paired accel/gyro samples: {path.name}") + return len(rows) + + def _ffprobe_frame_count(path: pathlib.Path) -> int: completed = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-count_frames", "-show_entries", "stream=nb_read_frames", "-of", "default=nokey=1:noprint_wrappers=1", str(path)], capture_output=True, text=True, check=False) try: @@ -1051,6 +1098,27 @@ def _run_accepted_slice_directory( shutil.copytree(source_slice, target_slice, dirs_exist_ok=True) start = float(raw.get("startSecond") or 0.0) end = float(raw.get("endSecond") or 0.0) + start_us = int(raw.get("startTimestampUs") or 0) + end_us = int(raw.get("endTimestampUs") or 0) + required_files = ["left.mp4", "right.mp4", "left_pts.csv", "right_pts.csv", "imu.csv", "annotation.json", "synchronized-slice.json"] + missing_files = [name for name in required_files if not (target_slice / name).is_file()] + if missing_files: + raise RuntimeError(f"accepted slice {slice_id} is missing required files: {', '.join(missing_files)}") + left_pts = _read_materialized_pts(target_slice / "left_pts.csv") + right_pts = _read_materialized_pts(target_slice / "right_pts.csv") + if _ffprobe_frame_count(target_slice / "left.mp4") != len(left_pts): + raise RuntimeError(f"accepted slice {slice_id} left video/PTS frame count mismatch") + if _ffprobe_frame_count(target_slice / "right.mp4") != len(right_pts): + raise RuntimeError(f"accepted slice {slice_id} right video/PTS frame count mismatch") + _validate_materialized_imu(target_slice / "imu.csv", start_us, end_us) + declared_files = raw.get("files") if isinstance(raw.get("files"), dict) else {} + for declared in declared_files.values(): + if not isinstance(declared, dict): + continue + name = str(declared.get("name") or "").strip() + expected_sha = str(declared.get("sha256") or "").strip() + if name and expected_sha and (target_slice / name).is_file() and _sha256_file(target_slice / name) != expected_sha: + raise RuntimeError(f"accepted slice {slice_id} checksum mismatch: {name}") if pose_policy.get("headpose") == "include": cropped = _crop_pose(headpose, start, end, "headpose") if headpose else None if cropped is None or cropped.get("frameCount", 0) <= 0: @@ -1089,7 +1157,21 @@ def _run_accepted_slice_directory( "randomValue": final_acceptance.get("samplingRandomValue"), "decidedAt": final_acceptance.get("samplingDecidedAt"), } - manifest = {"schemaVersion": "droboticflow.delivery.manifest/v5", "datasetType": "fanyu-accepted-slice-directories", "revision": revision_manifest.get("revision"), "sliceCount": len(package_items), "coverage": coverage, "acceptanceSampling": sampling, "poseDelivery": pose_policy, "calibration": calibration, "lineage": {"acceptedSliceDirectoryArtifactId": directory_artifact.get("id"), "humanItemId": revision_manifest.get("humanItemId"), "taskId": revision_manifest.get("taskId"), "sourceRunId": revision_manifest.get("runId")}, "items": package_items} + capture_record = summary_payload.get("capture") if isinstance(summary_payload.get("capture"), dict) else revision_manifest.get("capture") if isinstance(revision_manifest.get("capture"), dict) else {} + quality_reviews = summary_payload.get("qualityReviews") if isinstance(summary_payload.get("qualityReviews"), list) else [] + capture_manifest = {"schemaVersion": "droboticflow.capture-delivery/v1", "capture": capture_record, "revision": revision_manifest.get("revision"), "coverage": coverage, "sliceCount": len(package_items), "lineage": {"acceptedSliceDirectoryArtifactId": directory_artifact.get("id"), "humanItemId": revision_manifest.get("humanItemId"), "taskId": revision_manifest.get("taskId"), "sourceRunId": revision_manifest.get("runId")}} + evidence_payloads = { + "capture-manifest.json": capture_manifest, + "quality-review.json": {"schemaVersion": "droboticflow.capture-quality-review/v1", "reviews": quality_reviews}, + "acceptance-record.json": {"schemaVersion": "droboticflow.capture-acceptance/v1", "finalAcceptance": final_acceptance}, + "sampling-evidence.json": {"schemaVersion": "droboticflow.capture-sampling/v1", **sampling}, + } + root_evidence = [] + for name, payload in evidence_payloads.items(): + path = delivery_root / name + path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") + root_evidence.append({"name": name, "sha256": _sha256_file(path), "bytes": path.stat().st_size}) + manifest = {"schemaVersion": "droboticflow.delivery.manifest/v5", "datasetType": "fanyu-accepted-slice-directories", "capture": capture_record, "revision": revision_manifest.get("revision"), "sliceCount": len(package_items), "coverage": coverage, "acceptanceSampling": sampling, "poseDelivery": pose_policy, "calibration": calibration, "rootEvidence": root_evidence, "lineage": {"acceptedSliceDirectoryArtifactId": directory_artifact.get("id"), "humanItemId": revision_manifest.get("humanItemId"), "taskId": revision_manifest.get("taskId"), "sourceRunId": revision_manifest.get("runId")}, "items": package_items} manifest_binding = _output(node_data, helpers, "deliveryManifest") directory_binding = _output(node_data, helpers, "deliveryDirectory") result = [helpers["write_json_artifact"](run_id, node_id, delivery_root, "packages-manifest.json", manifest, output=manifest_binding)] diff --git a/docs/fanyu-v5-workflow.md b/docs/fanyu-v5-workflow.md index 02438ae5..a4dfc1ca 100644 --- a/docs/fanyu-v5-workflow.md +++ b/docs/fanyu-v5-workflow.md @@ -4,7 +4,7 @@ `项目 → Capture 目录 → 切片` -工作流只是 Capture 的处理配置,不是数据层级。泛宇不使用“15 分钟数据包”或 Episode 概念;平台中 LeRobot、RLDS、DROID 等通用算子的行业术语不受影响。 +工作流只是 Capture 的处理配置,不是数据层级。泛宇全部页面、接口、目录和交付文件统一使用“项目、Capture、切片”三个业务概念;通用数据集算子的行业数据结构不受影响。 ## 生产流程 diff --git a/internal/api/annotation_labels.go b/internal/api/annotation_labels.go index ae0dd40e..9439ea80 100644 --- a/internal/api/annotation_labels.go +++ b/internal/api/annotation_labels.go @@ -22,6 +22,15 @@ type annotationLabelInput struct { Status domain.AnnotationLabelStatus `json:"status"` } +func annotationLabelRevision(item domain.AnnotationLabelRecord) domain.AnnotationLabelRevision { + return domain.AnnotationLabelRevision{ + Version: item.Version, Code: item.Code, Name: item.Name, Category: item.Category, + Description: item.Description, Example: item.Example, Color: item.Color, + Aliases: append([]string(nil), item.Aliases...), Status: item.Status, + UpdatedByUserID: item.UpdatedByUserID, UpdatedAt: item.UpdatedAt, + } +} + func normalizeAnnotationLabelInput(input annotationLabelInput) annotationLabelInput { input.Code = strings.TrimSpace(input.Code) input.Name = strings.TrimSpace(input.Name) @@ -69,6 +78,11 @@ func (server *server) handleAnnotationLabelsList(writer http.ResponseWriter, req renderError(writer, http.StatusInternalServerError, err) return } + for index := range items { + if len(items[index].History) == 0 { + items[index].History = []domain.AnnotationLabelRevision{annotationLabelRevision(items[index])} + } + } renderJSON(writer, http.StatusOK, map[string]any{"items": items}) } @@ -108,6 +122,7 @@ func (server *server) handleAnnotationLabelCreate(writer http.ResponseWriter, re Aliases: payload.Aliases, Status: payload.Status, Version: 1, CreatedByUserID: actorID, UpdatedByUserID: actorID, CreatedAt: now, UpdatedAt: now, } + item.History = []domain.AnnotationLabelRevision{annotationLabelRevision(item)} if err := server.store.UpsertAnnotationLabel(request.Context(), item); err != nil { renderError(writer, http.StatusInternalServerError, err) return @@ -152,6 +167,10 @@ func (server *server) handleAnnotationLabelPatch(writer http.ResponseWriter, req item.Aliases, item.Status = payload.Aliases, payload.Status item.Version++ item.UpdatedByUserID, item.UpdatedAt = principal.User.ID, nowISO() + item.History = append(item.History, annotationLabelRevision(*item)) + if len(item.History) > 100 { + item.History = append([]domain.AnnotationLabelRevision(nil), item.History[len(item.History)-100:]...) + } if err := server.store.UpsertAnnotationLabel(request.Context(), *item); err != nil { renderError(writer, http.StatusInternalServerError, err) return diff --git a/internal/api/capture_automation.go b/internal/api/capture_automation.go index 9036fb3d..43bf3204 100644 --- a/internal/api/capture_automation.go +++ b/internal/api/capture_automation.go @@ -127,6 +127,12 @@ func (server *server) captureAutomationPayload(ctx context.Context, ctxWorkflow record.NextRetryAt = run.AvailableAt } record.ParentRunID = run.ParentRunID + if run.Failure != nil { + record.FailedNodeID = strings.TrimSpace(run.Failure.NodeID) + if strings.TrimSpace(record.Error) == "" { + record.Error = strings.TrimSpace(run.Failure.Message) + } + } record.BusinessStage = server.captureBusinessStage(ctx, *record, run) } return captureAutomationResponse{Config: config, Counts: captureAutomationCounts(records), Records: records} diff --git a/internal/api/capture_automation_test.go b/internal/api/capture_automation_test.go index d816489c..a22daee6 100644 --- a/internal/api/capture_automation_test.go +++ b/internal/api/capture_automation_test.go @@ -95,6 +95,47 @@ func TestCaptureAutomationCountsSeparateFailuresAndCancellations(t *testing.T) { } } +func TestCaptureAutomationReleasedWorkflowRejectsManualProductionRootRun(t *testing.T) { + ctx := t.Context() + dataStore := store.NewMemoryStore() + project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: domain.DefaultOrganizationID, Name: "Fanyu"}) + if err != nil { + t.Fatal(err) + } + workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{ + OrganizationID: domain.DefaultOrganizationID, + ProjectID: project.ID, + Name: "Capture listener", + Type: domain.WorkflowTypeBatch, + WorkflowInputs: []domain.WorkflowInputDefinition{{ + ID: "sourceDirectory", Name: "sourceDirectory", Type: domain.PortTypeFile, + Container: domain.PortContainerDirectory, PayloadMode: domain.ArtifactTransferModeRef, Required: true, + }}, + }) + if err != nil { + t.Fatal(err) + } + workflow.DefinitionState = domain.WorkflowDefinitionStateReleased + workflow.CaptureAutomation = &domain.CaptureAutomationConfig{Enabled: true, WorkflowInputID: "sourceDirectory"} + if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil { + t.Fatal(err) + } + + handler := NewHandler(dataStore, nil) + body := bytes.NewBufferString(`{"trigger":"manual","mode":"production","inputs":{"sourceDirectory":{"mode":"ref","type":"file","ref":{"provider":"bos","bucket":"drobodata-fanyu","prefix":"test","kind":"directory"}}}}`) + request := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/run", body) + request.Header.Set("Content-Type", "application/json") + recorder := httptest.NewRecorder() + handler.ServeHTTP(recorder, request) + if recorder.Code != http.StatusConflict || !strings.Contains(recorder.Body.String(), `"code":"capture_automation_manual_run_forbidden"`) { + t.Fatalf("manual Capture root run status=%d body=%s", recorder.Code, recorder.Body.String()) + } + runs, err := dataStore.ListRuns(ctx, "") + if err != nil || len(runs) != 0 { + t.Fatalf("manual root request created runs: %#v err=%v", runs, err) + } +} + func TestCaptureRecordCancelStopsRunCancelsHumanWorkAndKeepsRetryMetadata(t *testing.T) { ctx := t.Context() dataStore := store.NewMemoryStore() diff --git a/internal/api/human_loop.go b/internal/api/human_loop.go index 86d81696..1f102435 100644 --- a/internal/api/human_loop.go +++ b/internal/api/human_loop.go @@ -308,11 +308,15 @@ func (server *server) handleHumanItemSubjectContent(writer http.ResponseWriter, if !ok { return } - if strings.TrimSpace(item.RunID) == "" || strings.TrimSpace(item.Subject.ArtifactID) == "" { + artifactID := strings.TrimSpace(item.Subject.ArtifactID) + if strings.EqualFold(strings.TrimSpace(request.URL.Query().Get("side")), "right") && item.Subject.Payload != nil { + artifactID = strings.TrimSpace(fmt.Sprint(item.Subject.Payload["rightArtifactId"])) + } + if strings.TrimSpace(item.RunID) == "" || artifactID == "" { renderJSON(writer, http.StatusNotFound, map[string]string{"error": "Human item subject artifact not found"}) return } - server.serveRunArtifactContent(writer, request, item.RunID, item.Subject.ArtifactID) + server.serveRunArtifactContent(writer, request, item.RunID, artifactID) } func humanEvidenceArtifactRunIDs(value any, inheritedRunID string, destination map[string]string) { diff --git a/internal/api/human_slice_materialization.go b/internal/api/human_slice_materialization.go index 36d02bc9..873380c4 100644 --- a/internal/api/human_slice_materialization.go +++ b/internal/api/human_slice_materialization.go @@ -714,7 +714,16 @@ func (server *server) materializeCombinedSliceSubmission(ctx context.Context, ta return nil, fmt.Errorf("slice reconstruction coverage mismatch: left=%d/%d right=%d/%d imu=%d/%d", deliveredLeftFrames, expectedLeftFrames, deliveredRightFrames, expectedRightFrames, deliveredIMURows, imuCommonCount) } coverage := map[string]any{"mode": "full_common_range", "contiguous": true, "gapMicroseconds": 0, "overlapMicroseconds": 0, "startSecond": commonMediaStart, "endSecond": commonMediaEnd, "durationSeconds": commonMediaEnd - commonMediaStart, "startTimestampUs": commonStart, "endTimestampUs": commonEnd, "ptsToMediaScale": ptsToMediaScale, "sliceCount": len(manifestItems), "reconstruction": map[string]any{"leftFrameCount": deliveredLeftFrames, "rightFrameCount": deliveredRightFrames, "imuRowCount": deliveredIMURows}} - revisionManifest := map[string]any{"schemaVersion": "droboticflow.slice-materialization/v2", "revision": revision, "humanItemId": item.ID, "taskId": task.ID, "runId": task.RunID, "collectionId": collectionID, "status": "materialized", "sliceCount": len(manifestItems), "coverage": coverage, "items": manifestItems} + captureEvidence := map[string]any{} + if capture, captureErr := server.store.GetCaptureRecordByRunID(ctx, task.RunID); captureErr != nil { + return nil, captureErr + } else if capture != nil { + captureEvidence = map[string]any{ + "captureRecordId": capture.ID, "captureId": capture.CaptureID, "prefix": capture.Prefix, + "fingerprint": capture.Fingerprint, "contractRevision": capture.ContractRevision, + } + } + revisionManifest := map[string]any{"schemaVersion": "droboticflow.slice-materialization/v2", "revision": revision, "humanItemId": item.ID, "taskId": task.ID, "runId": task.RunID, "collectionId": collectionID, "capture": captureEvidence, "status": "materialized", "sliceCount": len(manifestItems), "coverage": coverage, "items": manifestItems} manifestPath := filepath.Join(root, "revision-manifest.json") manifestSHA, manifestBytes, err := writeJSONFile(manifestPath, revisionManifest) if err != nil { diff --git a/internal/api/project_members.go b/internal/api/project_members.go index 2a792315..3fe2a2d2 100644 --- a/internal/api/project_members.go +++ b/internal/api/project_members.go @@ -37,6 +37,21 @@ func isOrganizationAdmin(principal *authenticatedPrincipal) bool { return principal != nil && (principal.User.IsSuperAdmin || principal.User.Role == domain.UserRoleAdmin || principal.User.Role == domain.UserRoleOrganizationAdmin) } +func isSupplierManager(principal *authenticatedPrincipal) bool { + return principal != nil && principal.User.Role == domain.UserRoleSupplierManager && strings.TrimSpace(principal.User.SupplierID) != "" +} + +func projectMemberManager(principal *authenticatedPrincipal) bool { + return isOrganizationAdmin(principal) || isSupplierManager(principal) +} + +func projectMemberSupplierAllowed(principal *authenticatedPrincipal, supplierID string) bool { + if isOrganizationAdmin(principal) { + return true + } + return isSupplierManager(principal) && strings.TrimSpace(principal.User.SupplierID) == strings.TrimSpace(supplierID) +} + func normalizedMemberRoles(values []domain.AnnotationAssignmentRole) ([]domain.AnnotationAssignmentRole, bool) { roles := domain.NormalizeProjectMemberRoles(values) return roles, len(roles) > 0 @@ -124,7 +139,7 @@ func (server *server) handleProjectMembersList(writer http.ResponseWriter, reque renderJSON(writer, http.StatusOK, map[string]any{"items": filtered}) } -func (server *server) saveProjectMember(writer http.ResponseWriter, request *http.Request, project *domain.ProjectRecord, member *domain.ProjectMemberRecord, payload projectMemberInput, created bool) { +func (server *server) saveProjectMember(writer http.ResponseWriter, request *http.Request, principal *authenticatedPrincipal, project *domain.ProjectRecord, member *domain.ProjectMemberRecord, payload projectMemberInput, created bool) { payload.UserID = strings.TrimSpace(payload.UserID) payload.SupplierID = strings.TrimSpace(payload.SupplierID) roles, valid := normalizedMemberRoles(payload.Roles) @@ -144,6 +159,10 @@ func (server *server) saveProjectMember(writer http.ResponseWriter, request *htt if payload.SupplierID == "" { payload.SupplierID = strings.TrimSpace(user.SupplierID) } + if !projectMemberSupplierAllowed(principal, payload.SupplierID) || (isSupplierManager(principal) && strings.TrimSpace(user.SupplierID) != strings.TrimSpace(principal.User.SupplierID)) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "supplier manager may only maintain users and project members in their own supplier", "code": "supplier_scope_forbidden"}) + return + } if payload.SupplierID != "" { supplier, supplierErr := server.store.GetSupplier(request.Context(), payload.SupplierID) if supplierErr != nil { @@ -173,8 +192,8 @@ func (server *server) saveProjectMember(writer http.ResponseWriter, request *htt func (server *server) handleProjectMemberCreate(writer http.ResponseWriter, request *http.Request) { principal, _ := currentPrincipal(request) - if !isOrganizationAdmin(principal) { - renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization admin is required"}) + if !projectMemberManager(principal) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization admin or supplier manager is required"}) return } projectID := strings.TrimSpace(chi.URLParam(request, "id")) @@ -200,13 +219,13 @@ func (server *server) handleProjectMemberCreate(writer http.ResponseWriter, requ renderJSON(writer, http.StatusConflict, map[string]any{"error": "project member already exists", "member": existing}) return } - server.saveProjectMember(writer, request, project, nil, payload, true) + server.saveProjectMember(writer, request, principal, project, nil, payload, true) } func (server *server) handleProjectMemberUpdate(writer http.ResponseWriter, request *http.Request) { principal, _ := currentPrincipal(request) - if !isOrganizationAdmin(principal) { - renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization admin is required"}) + if !projectMemberManager(principal) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization admin or supplier manager is required"}) return } member, err := server.store.GetProjectMember(request.Context(), strings.TrimSpace(chi.URLParam(request, "memberId"))) @@ -218,6 +237,10 @@ func (server *server) handleProjectMemberUpdate(writer http.ResponseWriter, requ renderJSON(writer, http.StatusNotFound, map[string]string{"error": "project member not found"}) return } + if !projectMemberSupplierAllowed(principal, member.SupplierID) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "supplier manager may only maintain project members in their own supplier", "code": "supplier_scope_forbidden"}) + return + } project, err := server.store.GetProject(request.Context(), member.ProjectID) if err != nil || project == nil { if err != nil { @@ -232,7 +255,7 @@ func (server *server) handleProjectMemberUpdate(writer http.ResponseWriter, requ return } payload.UserID = member.UserID - server.saveProjectMember(writer, request, project, member, payload, false) + server.saveProjectMember(writer, request, principal, project, member, payload, false) } func (server *server) releaseProjectMemberTasks(ctx context.Context, organizationID string, member domain.ProjectMemberRecord, actorID string) (int, error) { @@ -267,8 +290,8 @@ func (server *server) releaseProjectMemberTasks(ctx context.Context, organizatio func (server *server) handleProjectMemberDelete(writer http.ResponseWriter, request *http.Request) { principal, _ := currentPrincipal(request) - if !isOrganizationAdmin(principal) { - renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization admin is required"}) + if !projectMemberManager(principal) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization admin or supplier manager is required"}) return } member, err := server.store.GetProjectMember(request.Context(), strings.TrimSpace(chi.URLParam(request, "memberId"))) @@ -280,6 +303,10 @@ func (server *server) handleProjectMemberDelete(writer http.ResponseWriter, requ renderJSON(writer, http.StatusNotFound, map[string]string{"error": "project member not found"}) return } + if !projectMemberSupplierAllowed(principal, member.SupplierID) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "supplier manager may only maintain project members in their own supplier", "code": "supplier_scope_forbidden"}) + return + } releaseTasks := strings.EqualFold(strings.TrimSpace(request.URL.Query().Get("releaseTasks")), "true") if releaseTasks { if _, err := server.releaseProjectMemberTasks(request.Context(), member.OrganizationID, *member, principal.User.ID); err != nil { @@ -301,8 +328,8 @@ func (server *server) handleProjectMemberDelete(writer http.ResponseWriter, requ func (server *server) handleProjectMembersBulk(writer http.ResponseWriter, request *http.Request) { principal, _ := currentPrincipal(request) - if !isOrganizationAdmin(principal) { - renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization admin is required"}) + if !projectMemberManager(principal) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "organization admin or supplier manager is required"}) return } projectID := strings.TrimSpace(chi.URLParam(request, "id")) @@ -321,7 +348,7 @@ func (server *server) handleProjectMembersBulk(writer http.ResponseWriter, reque } payload.Action = strings.ToLower(strings.TrimSpace(payload.Action)) if payload.Action == "add" { - server.handleProjectMembersBulkAdd(writer, request, project, payload.Entries) + server.handleProjectMembersBulkAdd(writer, request, principal, project, payload.Entries) return } roles, valid := normalizedMemberRoles(payload.Roles) @@ -335,6 +362,10 @@ func (server *server) handleProjectMembersBulk(writer http.ResponseWriter, reque if err != nil || member == nil || member.ProjectID != projectID || !server.filterByTenant(request, member.OrganizationID) { continue } + if !projectMemberSupplierAllowed(principal, member.SupplierID) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "supplier manager may only maintain project members in their own supplier", "code": "supplier_scope_forbidden"}) + return + } if payload.Action == "remove" { if _, err = server.releaseProjectMemberTasks(request.Context(), member.OrganizationID, *member, principal.User.ID); err == nil { _, err = server.store.DeleteProjectMember(request.Context(), member.ID) @@ -352,7 +383,7 @@ func (server *server) handleProjectMembersBulk(writer http.ResponseWriter, reque renderJSON(writer, http.StatusOK, projectMemberBulkResult{Updated: updated}) } -func (server *server) handleProjectMembersBulkAdd(writer http.ResponseWriter, request *http.Request, project *domain.ProjectRecord, entries []projectMemberInput) { +func (server *server) handleProjectMembersBulkAdd(writer http.ResponseWriter, request *http.Request, principal *authenticatedPrincipal, project *domain.ProjectRecord, entries []projectMemberInput) { if len(entries) == 0 { renderValidationError(writer, validationIssue{Field: "entries", Message: "at least one project member entry is required"}) return @@ -411,6 +442,10 @@ func (server *server) handleProjectMembersBulkAdd(writer http.ResponseWriter, re if entry.SupplierID == "" { entry.SupplierID = strings.TrimSpace(user.SupplierID) } + if !projectMemberSupplierAllowed(principal, entry.SupplierID) || (isSupplierManager(principal) && strings.TrimSpace(user.SupplierID) != strings.TrimSpace(principal.User.SupplierID)) { + renderJSON(writer, http.StatusForbidden, map[string]string{"error": "supplier manager may only maintain users and project members in their own supplier", "code": "supplier_scope_forbidden"}) + return + } if entry.SupplierID != "" { supplier, cached := supplierCache[entry.SupplierID] if !cached { diff --git a/internal/api/project_members_test.go b/internal/api/project_members_test.go index c642044f..198d8377 100644 --- a/internal/api/project_members_test.go +++ b/internal/api/project_members_test.go @@ -186,6 +186,80 @@ func TestBulkProjectMemberAddValidatesWholeRequestBeforeWriting(t *testing.T) { } } +func TestSupplierManagerMaintainsOnlyOwnSupplierProjectMembers(t *testing.T) { + t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true") + ctx := context.Background() + dataStore := store.NewMemoryStore() + handler := NewHandler(dataStore, nil) + rootCookie := bootstrapSuperAdminSession(t, handler) + organization := adminCreateOrganization(t, handler, rootCookie, "Supplier member scope", "supplier-member-scope") + supplierA := domain.SupplierRecord{ID: "supplier-a", OrganizationID: organization.ID, Name: "Supplier A", Status: "active"} + supplierB := domain.SupplierRecord{ID: "supplier-b", OrganizationID: organization.ID, Name: "Supplier B", Status: "active"} + if err := dataStore.UpsertSupplier(ctx, supplierA); err != nil { + t.Fatal(err) + } + if err := dataStore.UpsertSupplier(ctx, supplierB); err != nil { + t.Fatal(err) + } + manager := adminCreateUser(t, handler, rootCookie, organization.ID, "Supplier Manager", "supplier-manager@example.com", domain.UserRoleSupplierManager, "manager-password") + storedManager, err := dataStore.GetUser(ctx, manager.ID) + if err != nil || storedManager == nil { + t.Fatalf("load manager: %#v err=%v", storedManager, err) + } + storedManager.SupplierID = supplierA.ID + if err := dataStore.UpsertUser(ctx, *storedManager); err != nil { + t.Fatal(err) + } + workerA := adminCreateUser(t, handler, rootCookie, organization.ID, "Worker A", "supplier-worker-a@example.com", domain.UserRoleMember, "worker-password") + storedWorkerA, err := dataStore.GetUser(ctx, workerA.ID) + if err != nil || storedWorkerA == nil { + t.Fatalf("load worker A: %#v err=%v", storedWorkerA, err) + } + storedWorkerA.SupplierID = supplierA.ID + if err := dataStore.UpsertUser(ctx, *storedWorkerA); err != nil { + t.Fatal(err) + } + workerB := adminCreateUser(t, handler, rootCookie, organization.ID, "Worker B", "supplier-worker-b@example.com", domain.UserRoleMember, "worker-password") + storedWorkerB, err := dataStore.GetUser(ctx, workerB.ID) + if err != nil || storedWorkerB == nil { + t.Fatalf("load worker B: %#v err=%v", storedWorkerB, err) + } + storedWorkerB.SupplierID = supplierB.ID + if err := dataStore.UpsertUser(ctx, *storedWorkerB); err != nil { + t.Fatal(err) + } + managerCookie := loginSession(t, handler, manager.Email, "manager-password") + project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: organization.ID, Name: "Supplier project"}) + if err != nil { + t.Fatal(err) + } + + ownBody, _ := json.Marshal(map[string]any{"userId": workerA.ID, "supplierId": supplierA.ID, "roles": []string{"annotator"}}) + created := serveJSONWithCookie(handler, http.MethodPost, "/api/projects/"+project.ID+"/members", ownBody, managerCookie) + if created.Code != http.StatusCreated { + t.Fatalf("supplier manager create own member=%d body=%s", created.Code, created.Body.String()) + } + var ownMember domain.ProjectMemberRecord + if err := json.Unmarshal(created.Body.Bytes(), &ownMember); err != nil { + t.Fatal(err) + } + updateBody, _ := json.Marshal(map[string]any{"supplierId": supplierA.ID, "roles": []string{"annotator", "quality_reviewer"}}) + updated := serveJSONWithCookie(handler, http.MethodPatch, "/api/projects/"+project.ID+"/members/"+ownMember.ID, updateBody, managerCookie) + if updated.Code != http.StatusOK || !jsonContains(updated.Body.String(), "quality_reviewer") { + t.Fatalf("supplier manager update own member=%d body=%s", updated.Code, updated.Body.String()) + } + + otherBody, _ := json.Marshal(map[string]any{"userId": workerB.ID, "supplierId": supplierB.ID, "roles": []string{"annotator"}}) + forbidden := serveJSONWithCookie(handler, http.MethodPost, "/api/projects/"+project.ID+"/members", otherBody, managerCookie) + if forbidden.Code != http.StatusForbidden || !jsonContains(forbidden.Body.String(), `"code":"supplier_scope_forbidden"`) { + t.Fatalf("supplier manager crossed supplier scope=%d body=%s", forbidden.Code, forbidden.Body.String()) + } + otherMember, _ := dataStore.GetProjectMemberByProjectUser(ctx, organization.ID, project.ID, workerB.ID) + if otherMember != nil { + t.Fatalf("cross-supplier member was created: %#v", otherMember) + } +} + func jsonContains(value string, expected string) bool { return len(expected) == 0 || (len(value) >= len(expected) && containsText(value, expected)) } diff --git a/internal/api/server.go b/internal/api/server.go index 68997853..cfdd5b85 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -1459,7 +1459,7 @@ func NewHandlerWithConfig(dataStore store.Store, aiService ai.Service, appConfig router.Post("/api/scan-configs/{id}/run", instance.handleScanConfigsRun) }) router.Group(func(router chi.Router) { - router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleOrganizationAdmin)) + router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleOrganizationAdmin, domain.UserRoleSupplierManager)) router.Post("/api/suppliers", instance.handleSupplierCreate) router.Post("/api/projects/{id}/members", instance.handleProjectMemberCreate) router.Patch("/api/projects/{id}/members/{memberId}", instance.handleProjectMemberUpdate) @@ -1567,8 +1567,11 @@ func NewHandlerWithConfig(dataStore store.Store, aiService ai.Service, appConfig router.Put("/api/users/{id}/storage-path-favorites", instance.handleUserStoragePathFavoritesUpdate) router.Put("/api/users/me", instance.handleCurrentUserProfileUpdate) router.Group(func(router chi.Router) { - router.Use(instance.requireBusinessRole(domain.UserRoleAdmin)) + router.Use(instance.requireBusinessRole(domain.UserRoleAdmin, domain.UserRoleSupplierManager)) router.Get("/api/users", instance.handleUsersList) + }) + router.Group(func(router chi.Router) { + router.Use(instance.requireBusinessRole(domain.UserRoleAdmin)) router.Post("/api/users", instance.handleUsersCreate) router.Put("/api/users/{id}", instance.handleUsersUpdate) router.Delete("/api/users/{id}", instance.handleUsersDelete) @@ -4869,6 +4872,13 @@ func (server *server) handleWorkflowRun(writer http.ResponseWriter, request *htt }) return } + if mode == domain.RunModeProduction && payload.Trigger == domain.RunTriggerManual && workflow.CaptureAutomation != nil && workflow.CaptureAutomation.Enabled { + renderJSON(writer, http.StatusConflict, map[string]string{ + "error": "持续 Capture 工作流由目录监听器逐条创建 Run,不能手工运行整个监听根目录;请等待 Capture 自动进入流程,或在 Capture 列表中使用重新处理", + "code": "capture_automation_manual_run_forbidden", + }) + return + } // Validate node overrides and debug target against workflow nodes nodeMap := make(map[string]domain.WorkflowNode, len(workflow.Nodes)) diff --git a/internal/api/tenant.go b/internal/api/tenant.go index d12af389..c75a2fab 100644 --- a/internal/api/tenant.go +++ b/internal/api/tenant.go @@ -247,6 +247,20 @@ func (server *server) projectVisibleToRequest(request *http.Request, organizatio switch principal.User.Role { case domain.UserRoleAdmin, domain.UserRolePipelineEngineer, domain.UserRoleOperator: return true + case domain.UserRoleSupplierManager: + if strings.TrimSpace(principal.User.SupplierID) == "" { + return false + } + members, err := server.store.ListProjectMembers(request.Context(), organizationID, projectID) + if err != nil { + return false + } + for _, member := range members { + if strings.TrimSpace(member.SupplierID) == strings.TrimSpace(principal.User.SupplierID) { + return true + } + } + return false case domain.UserRoleCutter, domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer: // Standard RoboData roles are project scoped below. default: @@ -341,7 +355,14 @@ func (server *server) filterSamplingTasksForRequest(request *http.Request, items func (server *server) filterUsersForRequest(request *http.Request, items []domain.UserRecord) []domain.UserRecord { return filterTenantRecords(items, func(item domain.UserRecord) bool { - return server.filterByTenant(request, item.OrganizationID) + if !server.filterByTenant(request, item.OrganizationID) { + return false + } + principal, ok := currentPrincipal(request) + if !ok || principal == nil || principal.User.Role != domain.UserRoleSupplierManager { + return true + } + return strings.TrimSpace(principal.User.SupplierID) != "" && strings.TrimSpace(item.SupplierID) == strings.TrimSpace(principal.User.SupplierID) }) } diff --git a/internal/api/workflow_runtime_policy_test.go b/internal/api/workflow_runtime_policy_test.go index a973a82f..8b4dbe9d 100644 --- a/internal/api/workflow_runtime_policy_test.go +++ b/internal/api/workflow_runtime_policy_test.go @@ -155,6 +155,19 @@ func TestOrganizationLabelDictionaryAndProjectSelection(t *testing.T) { if err := json.Unmarshal(created.Body.Bytes(), &label); err != nil { t.Fatal(err) } + if label.Version != 1 || len(label.History) != 1 { + t.Fatalf("created label has no first immutable revision: %#v", label) + } + patched := serveJSONWithCookie(handler, http.MethodPatch, "/api/annotation-labels/"+label.ID, []byte(`{"code":"pick","name":"拿取物品","category":"动作","description":"拿起目标物","example":"右手拿起水瓶","status":"active"}`), adminCookie) + if patched.Code != http.StatusOK { + t.Fatalf("patch label=%d body=%s", patched.Code, patched.Body.String()) + } + if err := json.Unmarshal(patched.Body.Bytes(), &label); err != nil { + t.Fatal(err) + } + if label.Version != 2 || len(label.History) != 2 || label.History[1].Example != "右手拿起水瓶" { + t.Fatalf("label revision history=%#v", label) + } selectedBody, _ := json.Marshal(map[string]any{"labelIds": []string{label.ID}}) selected := serveJSONWithCookie(handler, http.MethodPut, "/api/projects/"+project.ID+"/annotation-labels", selectedBody, adminCookie) if selected.Code != http.StatusOK || !jsonContains(selected.Body.String(), label.ID) { diff --git a/internal/domain/annotation_labels.go b/internal/domain/annotation_labels.go index 1a0f066d..53ec6440 100644 --- a/internal/domain/annotation_labels.go +++ b/internal/domain/annotation_labels.go @@ -8,8 +8,26 @@ const ( ) type AnnotationLabelRecord struct { - ID string `json:"id" bson:"id"` - OrganizationID string `json:"organizationId" bson:"organizationId"` + ID string `json:"id" bson:"id"` + OrganizationID string `json:"organizationId" bson:"organizationId"` + Code string `json:"code" bson:"code"` + Name string `json:"name" bson:"name"` + Category string `json:"category,omitempty" bson:"category,omitempty"` + Description string `json:"description,omitempty" bson:"description,omitempty"` + Example string `json:"example,omitempty" bson:"example,omitempty"` + Color string `json:"color,omitempty" bson:"color,omitempty"` + Aliases []string `json:"aliases,omitempty" bson:"aliases,omitempty"` + Status AnnotationLabelStatus `json:"status" bson:"status"` + Version int `json:"version" bson:"version"` + CreatedByUserID string `json:"createdByUserId" bson:"createdByUserId"` + UpdatedByUserID string `json:"updatedByUserId" bson:"updatedByUserId"` + CreatedAt string `json:"createdAt" bson:"createdAt"` + UpdatedAt string `json:"updatedAt" bson:"updatedAt"` + History []AnnotationLabelRevision `json:"history,omitempty" bson:"history,omitempty"` +} + +type AnnotationLabelRevision struct { + Version int `json:"version" bson:"version"` Code string `json:"code" bson:"code"` Name string `json:"name" bson:"name"` Category string `json:"category,omitempty" bson:"category,omitempty"` @@ -18,10 +36,7 @@ type AnnotationLabelRecord struct { Color string `json:"color,omitempty" bson:"color,omitempty"` Aliases []string `json:"aliases,omitempty" bson:"aliases,omitempty"` Status AnnotationLabelStatus `json:"status" bson:"status"` - Version int `json:"version" bson:"version"` - CreatedByUserID string `json:"createdByUserId" bson:"createdByUserId"` UpdatedByUserID string `json:"updatedByUserId" bson:"updatedByUserId"` - CreatedAt string `json:"createdAt" bson:"createdAt"` UpdatedAt string `json:"updatedAt" bson:"updatedAt"` } diff --git a/internal/domain/annotation_operations.go b/internal/domain/annotation_operations.go index adedd63b..f90a36d8 100644 --- a/internal/domain/annotation_operations.go +++ b/internal/domain/annotation_operations.go @@ -160,6 +160,8 @@ type AnnotationTaskRecord struct { NodeID string `json:"nodeId,omitempty" bson:"nodeId,omitempty"` ArtifactID string `json:"artifactId,omitempty" bson:"artifactId,omitempty"` SourceVideo string `json:"sourceVideo" bson:"sourceVideo"` + RightArtifactID string `json:"rightArtifactId,omitempty" bson:"rightArtifactId,omitempty"` + RightSourceVideo string `json:"rightSourceVideo,omitempty" bson:"rightSourceVideo,omitempty"` ClipStart float64 `json:"clipStart" bson:"clipStart"` ClipEnd float64 `json:"clipEnd" bson:"clipEnd"` SourceIsFullVideo bool `json:"sourceIsFullVideo,omitempty" bson:"sourceIsFullVideo,omitempty"` diff --git a/internal/domain/capture_automation.go b/internal/domain/capture_automation.go index ecd9aac7..4e6f1fb4 100644 --- a/internal/domain/capture_automation.go +++ b/internal/domain/capture_automation.go @@ -77,6 +77,7 @@ type CaptureRecord struct { MaxAttempts int `json:"maxAttempts,omitempty" bson:"-"` NextRetryAt string `json:"nextRetryAt,omitempty" bson:"-"` ParentRunID string `json:"parentRunId,omitempty" bson:"-"` + FailedNodeID string `json:"failedNodeId,omitempty" bson:"-"` BusinessStage CaptureBusinessStage `json:"businessStage,omitempty" bson:"businessStage,omitempty"` ContractRevision string `json:"contractRevision,omitempty" bson:"contractRevision,omitempty"` RuntimePolicyRevision int `json:"runtimePolicyRevision,omitempty" bson:"runtimePolicyRevision,omitempty"` diff --git a/internal/worker/annotation_gate.go b/internal/worker/annotation_gate.go index 6414f5b8..ceffedee 100644 --- a/internal/worker/annotation_gate.go +++ b/internal/worker/annotation_gate.go @@ -530,6 +530,22 @@ func bindingArtifacts(bindings []domain.NodeInputBinding, targetPortID string) [ return nil } +func matchingCollectionArtifact(source domain.ExecutionArtifact, candidates []domain.ExecutionArtifact) *domain.ExecutionArtifact { + collectionID := strings.TrimSpace(fmt.Sprint(source.Data["collectionId"])) + for index := range candidates { + candidateID := strings.TrimSpace(fmt.Sprint(candidates[index].Data["collectionId"])) + if collectionID != "" && candidateID == collectionID { + copy := candidates[index] + return © + } + } + if len(candidates) == 1 { + copy := candidates[0] + return © + } + return nil +} + func annotationSourceDuration(artifact domain.ExecutionArtifact, windows []map[string]any) float64 { for _, key := range []string{"sourceDurationSeconds", "duration"} { if value, ok := annotationNumber(artifact.Data[key]); ok && value > 0 { @@ -747,6 +763,7 @@ func (runtime *Runtime) createAnnotationGateTasks( } if nodeHasInputPort(node, "roughCutWindows") { leftVideos := bindingArtifacts(inputBindings, "fullLeftVideos") + rightVideos := bindingArtifacts(inputBindings, "fullRightVideos") if len(leftVideos) == 0 { return nil, fmt.Errorf("timestamp-window human gate requires fullLeftVideos") } @@ -755,6 +772,7 @@ func (runtime *Runtime) createAnnotationGateTasks( poseSummary := annotationPoseSummary(poseInputs) tasks := make([]domain.AnnotationTaskRecord, 0, len(leftVideos)) for _, artifact := range leftVideos { + rightArtifact := matchingCollectionArtifact(artifact, rightVideos) collectionID := strings.TrimSpace(fmt.Sprint(artifact.Data["collectionId"])) matchedWindows := make([]map[string]any, 0) for _, window := range windows { @@ -768,6 +786,10 @@ func (runtime *Runtime) createAnnotationGateTasks( return nil, fmt.Errorf("human gate cannot resolve full source duration for %s", artifact.Name) } task := domain.AnnotationTaskRecord{ID: domain.NewID("annotation-task"), OrganizationID: run.OrganizationID, ProjectID: run.ProjectID, DataTaskID: run.TaskID, WorkflowID: run.WorkflowID, RunID: run.ID, NodeID: node.ID, ArtifactID: artifact.ID, SourceVideo: fmt.Sprintf("/api/runs/%s/artifacts/%s/content", url.PathEscape(run.ID), url.PathEscape(artifact.ID)), ClipStart: 0, ClipEnd: duration, SourceIsFullVideo: true, SourceDurationSeconds: duration, CoverageStartSecond: 0, CoverageEndSecond: duration, CoverageMode: "full_source_pending_common_range", TemplateID: template.ID, TemplateVersion: template.Version, LabelSnapshot: append([]domain.SemanticLabelDefinition(nil), labelSnapshot...), SupplierID: config.SupplierID, Status: domain.AnnotationTaskStatusPendingAnnotation, PoseSummary: poseSummary, CreatedAt: nowISO(), UpdatedAt: nowISO()} + if rightArtifact != nil { + task.RightArtifactID = rightArtifact.ID + task.RightSourceVideo = fmt.Sprintf("/api/runs/%s/artifacts/%s/content", url.PathEscape(run.ID), url.PathEscape(rightArtifact.ID)) + } if err := runtime.store.UpsertAnnotationTask(ctx, task); err != nil { return nil, err } @@ -998,6 +1020,8 @@ func (runtime *Runtime) ensureAnnotationHumanBatch( "clipEnd": task.ClipEnd, "sourceIsFullVideo": task.SourceIsFullVideo, "sourceDurationSeconds": task.SourceDurationSeconds, + "rightArtifactId": task.RightArtifactID, + "rightSourceVideo": task.RightSourceVideo, "templateId": task.TemplateID, "poseSummary": task.PoseSummary, }, @@ -1213,7 +1237,34 @@ func (runtime *Runtime) releasePartitionArtifacts( materialized.PortName = directoryPortName artifacts = append(artifacts, *materialized) } - summary := map[string]any{"schemaVersion": "droboticflow.acceptance-summary/v1", "decision": "approved", "partitionId": partition.ID, "itemCount": len(selected), "finalAcceptance": acceptanceEvidence} + qualityReviews := make([]map[string]any, 0, len(selected)) + for _, itemID := range partition.ItemIDs { + item := selected[itemID] + if strings.TrimSpace(item.LegacyTaskID) == "" { + continue + } + reviews, reviewErr := runtime.store.ListAnnotationReviews(ctx, batch.OrganizationID, item.LegacyTaskID) + if reviewErr != nil { + return nil, reviewErr + } + for _, review := range reviews { + qualityReviews = append(qualityReviews, map[string]any{ + "taskId": review.TaskID, "revision": review.Revision, "decision": review.Decision, + "reasonCodes": review.ReasonCodes, "problemSliceIds": review.ProblemSliceIDs, + "comment": review.Comment, "reviewerUserId": review.ReviewerUserID, "createdAt": review.CreatedAt, + }) + } + } + captureEvidence := map[string]any{} + if capture, captureErr := runtime.store.GetCaptureRecordByRunID(ctx, run.ID); captureErr != nil { + return nil, captureErr + } else if capture != nil { + captureEvidence = map[string]any{ + "captureRecordId": capture.ID, "captureId": capture.CaptureID, "prefix": capture.Prefix, + "fingerprint": capture.Fingerprint, "contractRevision": capture.ContractRevision, + } + } + summary := map[string]any{"schemaVersion": "droboticflow.acceptance-summary/v2", "decision": "approved", "partitionId": partition.ID, "itemCount": len(selected), "capture": captureEvidence, "qualityReviews": qualityReviews, "finalAcceptance": acceptanceEvidence} encoded, _ := json.Marshal(summary) artifacts = append(artifacts, domain.ExecutionArtifact{ID: fmt.Sprintf("%s-%s-acceptance-summary", run.ID, node.ID), RunID: run.ID, NodeID: node.ID, PortID: summaryPortID, PortName: summaryPortName, Name: "acceptance-summary.json", Type: domain.PortTypeJSON, Size: annotationArtifactSize(int64(len(encoded))), SizeBytes: int64(len(encoded)), Data: summary, Payload: &domain.ArtifactPayload{Mode: domain.ArtifactTransferModeInline, Type: domain.PortTypeJSON, Inline: summary}}) return artifacts, nil diff --git a/internal/worker/annotation_supplier_test.go b/internal/worker/annotation_supplier_test.go index c0fc5f18..12360af8 100644 --- a/internal/worker/annotation_supplier_test.go +++ b/internal/worker/annotation_supplier_test.go @@ -68,3 +68,18 @@ func TestAnnotationGateSupplierResolutionIsFailClosed(t *testing.T) { } }) } + +func TestMatchingCollectionArtifactKeepsStereoCaptureTogether(t *testing.T) { + left := domain.ExecutionArtifact{ID: "left-a", Data: map[string]any{"collectionId": "capture-a"}} + rights := []domain.ExecutionArtifact{ + {ID: "right-b", Data: map[string]any{"collectionId": "capture-b"}}, + {ID: "right-a", Data: map[string]any{"collectionId": "capture-a"}}, + } + matched := matchingCollectionArtifact(left, rights) + if matched == nil || matched.ID != "right-a" { + t.Fatalf("matched right artifact=%#v", matched) + } + if ambiguous := matchingCollectionArtifact(domain.ExecutionArtifact{ID: "left-unknown"}, rights); ambiguous != nil { + t.Fatalf("unscoped left artifact must not guess among multiple right videos: %#v", ambiguous) + } +} diff --git a/operator-repos/builtin/package/src/operator.py b/operator-repos/builtin/package/src/operator.py index 4c24a048..92d7a8d5 100644 --- a/operator-repos/builtin/package/src/operator.py +++ b/operator-repos/builtin/package/src/operator.py @@ -401,6 +401,53 @@ def _read_imu(path: pathlib.Path) -> list[dict[str, Any]]: return result +def _read_materialized_pts(path: pathlib.Path) -> list[int]: + header, rows = _read_rows(path) + normalized = {name.lower(): index for index, name in enumerate(header)} + timestamp_column = normalized.get("timestamp_ms") + if timestamp_column is None: + raise RuntimeError(f"materialized PTS CSV must contain timestamp_ms: {path.name}") + values: list[int] = [] + for row in rows: + try: + value = int(round(float(row[timestamp_column]) * 1000)) + except (IndexError, ValueError) as exc: + raise RuntimeError(f"materialized PTS CSV contains an invalid timestamp_ms: {path.name}") from exc + if values and value <= values[-1]: + raise RuntimeError(f"materialized PTS CSV timestamp_ms must be strictly increasing: {path.name}") + values.append(value) + if not values: + raise RuntimeError(f"materialized PTS CSV has no frame timestamps: {path.name}") + return values + + +def _validate_materialized_imu(path: pathlib.Path, start_us: int, end_us: int) -> int: + header, rows = _read_rows(path) + indexes = {name.lower(): index for index, name in enumerate(header)} + required = {"timestamp_ms", "clip_time_ms", "x", "y", "z", "type"} + if not required.issubset(indexes): + raise RuntimeError(f"materialized IMU CSV is missing required columns: {path.name}") + previous = -1 + seen_types: dict[int, set[str]] = {} + for row in rows: + try: + timestamp_us = int(round(float(row[indexes["timestamp_ms"]]) * 1000)) + kind = row[indexes["type"]].lower() + for key in ("x", "y", "z"): + float(row[indexes[key]]) + except (IndexError, ValueError) as exc: + raise RuntimeError(f"materialized IMU CSV contains an invalid row: {path.name}") from exc + if timestamp_us < previous or kind not in {"accel", "gyro"}: + raise RuntimeError(f"materialized IMU ordering/type is invalid: {path.name}") + if timestamp_us < start_us or timestamp_us >= end_us: + raise RuntimeError(f"materialized IMU row is outside the synchronized slice window: {path.name}") + previous = timestamp_us + seen_types.setdefault(timestamp_us, set()).add(kind) + if not rows or any(types != {"accel", "gyro"} for types in seen_types.values()): + raise RuntimeError(f"materialized IMU must contain paired accel/gyro samples: {path.name}") + return len(rows) + + def _ffprobe_frame_count(path: pathlib.Path) -> int: completed = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-count_frames", "-show_entries", "stream=nb_read_frames", "-of", "default=nokey=1:noprint_wrappers=1", str(path)], capture_output=True, text=True, check=False) try: @@ -1051,6 +1098,27 @@ def _run_accepted_slice_directory( shutil.copytree(source_slice, target_slice, dirs_exist_ok=True) start = float(raw.get("startSecond") or 0.0) end = float(raw.get("endSecond") or 0.0) + start_us = int(raw.get("startTimestampUs") or 0) + end_us = int(raw.get("endTimestampUs") or 0) + required_files = ["left.mp4", "right.mp4", "left_pts.csv", "right_pts.csv", "imu.csv", "annotation.json", "synchronized-slice.json"] + missing_files = [name for name in required_files if not (target_slice / name).is_file()] + if missing_files: + raise RuntimeError(f"accepted slice {slice_id} is missing required files: {', '.join(missing_files)}") + left_pts = _read_materialized_pts(target_slice / "left_pts.csv") + right_pts = _read_materialized_pts(target_slice / "right_pts.csv") + if _ffprobe_frame_count(target_slice / "left.mp4") != len(left_pts): + raise RuntimeError(f"accepted slice {slice_id} left video/PTS frame count mismatch") + if _ffprobe_frame_count(target_slice / "right.mp4") != len(right_pts): + raise RuntimeError(f"accepted slice {slice_id} right video/PTS frame count mismatch") + _validate_materialized_imu(target_slice / "imu.csv", start_us, end_us) + declared_files = raw.get("files") if isinstance(raw.get("files"), dict) else {} + for declared in declared_files.values(): + if not isinstance(declared, dict): + continue + name = str(declared.get("name") or "").strip() + expected_sha = str(declared.get("sha256") or "").strip() + if name and expected_sha and (target_slice / name).is_file() and _sha256_file(target_slice / name) != expected_sha: + raise RuntimeError(f"accepted slice {slice_id} checksum mismatch: {name}") if pose_policy.get("headpose") == "include": cropped = _crop_pose(headpose, start, end, "headpose") if headpose else None if cropped is None or cropped.get("frameCount", 0) <= 0: @@ -1089,7 +1157,21 @@ def _run_accepted_slice_directory( "randomValue": final_acceptance.get("samplingRandomValue"), "decidedAt": final_acceptance.get("samplingDecidedAt"), } - manifest = {"schemaVersion": "droboticflow.delivery.manifest/v5", "datasetType": "fanyu-accepted-slice-directories", "revision": revision_manifest.get("revision"), "sliceCount": len(package_items), "coverage": coverage, "acceptanceSampling": sampling, "poseDelivery": pose_policy, "calibration": calibration, "lineage": {"acceptedSliceDirectoryArtifactId": directory_artifact.get("id"), "humanItemId": revision_manifest.get("humanItemId"), "taskId": revision_manifest.get("taskId"), "sourceRunId": revision_manifest.get("runId")}, "items": package_items} + capture_record = summary_payload.get("capture") if isinstance(summary_payload.get("capture"), dict) else revision_manifest.get("capture") if isinstance(revision_manifest.get("capture"), dict) else {} + quality_reviews = summary_payload.get("qualityReviews") if isinstance(summary_payload.get("qualityReviews"), list) else [] + capture_manifest = {"schemaVersion": "droboticflow.capture-delivery/v1", "capture": capture_record, "revision": revision_manifest.get("revision"), "coverage": coverage, "sliceCount": len(package_items), "lineage": {"acceptedSliceDirectoryArtifactId": directory_artifact.get("id"), "humanItemId": revision_manifest.get("humanItemId"), "taskId": revision_manifest.get("taskId"), "sourceRunId": revision_manifest.get("runId")}} + evidence_payloads = { + "capture-manifest.json": capture_manifest, + "quality-review.json": {"schemaVersion": "droboticflow.capture-quality-review/v1", "reviews": quality_reviews}, + "acceptance-record.json": {"schemaVersion": "droboticflow.capture-acceptance/v1", "finalAcceptance": final_acceptance}, + "sampling-evidence.json": {"schemaVersion": "droboticflow.capture-sampling/v1", **sampling}, + } + root_evidence = [] + for name, payload in evidence_payloads.items(): + path = delivery_root / name + path.write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") + root_evidence.append({"name": name, "sha256": _sha256_file(path), "bytes": path.stat().st_size}) + manifest = {"schemaVersion": "droboticflow.delivery.manifest/v5", "datasetType": "fanyu-accepted-slice-directories", "capture": capture_record, "revision": revision_manifest.get("revision"), "sliceCount": len(package_items), "coverage": coverage, "acceptanceSampling": sampling, "poseDelivery": pose_policy, "calibration": calibration, "rootEvidence": root_evidence, "lineage": {"acceptedSliceDirectoryArtifactId": directory_artifact.get("id"), "humanItemId": revision_manifest.get("humanItemId"), "taskId": revision_manifest.get("taskId"), "sourceRunId": revision_manifest.get("runId")}, "items": package_items} manifest_binding = _output(node_data, helpers, "deliveryManifest") directory_binding = _output(node_data, helpers, "deliveryDirectory") result = [helpers["write_json_artifact"](run_id, node_id, delivery_root, "packages-manifest.json", manifest, output=manifest_binding)] diff --git a/src/App.tsx b/src/App.tsx index ce7e45f6..8bdd67c2 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -119,7 +119,12 @@ function RoleRouteGuard({ children }: { children: React.ReactNode }) { : currentUser?.role === "pipeline_engineer" || currentUser?.role === "operator" ? "admin" : currentUser?.role; - if (effectiveRole === "supplier_manager" && path !== "/") { + if ( + effectiveRole === "supplier_manager" && + path !== "/" && + path !== "/projects" && + !path.startsWith("/project/") + ) { return ; } if ( diff --git a/src/components/AnnotationLabelsPanel.tsx b/src/components/AnnotationLabelsPanel.tsx index 572a9afc..ed24358a 100644 --- a/src/components/AnnotationLabelsPanel.tsx +++ b/src/components/AnnotationLabelsPanel.tsx @@ -1,5 +1,5 @@ -import React, { useEffect, useMemo, useState } from "react"; -import { Loader2, Plus, Save, Tags } from "lucide-react"; +import React, { useEffect, useState } from "react"; +import { Edit3, Loader2, Plus, Save, Tags, X } from "lucide-react"; import { apiRequest } from "../services/api/client"; import { getActionErrorMessage } from "../lib/action-errors.js"; import type { AnnotationLabelRecord, ProjectAnnotationLabelsRecord } from "../types"; @@ -10,7 +10,15 @@ export function AnnotationLabelsPanel({ projectId, canManage }: { projectId: str const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); - const [draft, setDraft] = useState({ code: "", name: "", category: "", description: "" }); + const [editingLabelId, setEditingLabelId] = useState(""); + const [draft, setDraft] = useState({ + code: "", + name: "", + category: "", + description: "", + example: "", + status: "active" as "active" | "inactive", + }); const load = async () => { setLoading(true); @@ -33,25 +41,43 @@ export function AnnotationLabelsPanel({ projectId, canManage }: { projectId: str void load(); }, [projectId]); - const activeLabels = useMemo(() => labels.filter((item) => item.status === "active"), [labels]); + const resetDraft = () => { + setEditingLabelId(""); + setDraft({ code: "", name: "", category: "", description: "", example: "", status: "active" }); + }; - const createLabel = async () => { + const editLabel = (label: AnnotationLabelRecord) => { + setEditingLabelId(label.id); + setDraft({ + code: label.code, + name: label.name, + category: label.category || "", + description: label.description || "", + example: label.example || "", + status: label.status, + }); + }; + + const saveLabel = async () => { if (!draft.code.trim() || !draft.name.trim()) return; setSaving(true); setError(""); try { - const created = await apiRequest( - "/api/annotation-labels", - { method: "POST" }, - { - ...draft, - status: "active", - }, + const saved = await apiRequest( + editingLabelId ? `/api/annotation-labels/${editingLabelId}` : "/api/annotation-labels", + { method: editingLabelId ? "PATCH" : "POST" }, + draft, ); - setLabels((items) => [...items, created].sort((a, b) => a.code.localeCompare(b.code))); - setDraft({ code: "", name: "", category: "", description: "" }); + setLabels((items) => { + const next = editingLabelId ? items.map((item) => (item.id === saved.id ? saved : item)) : [...items, saved]; + return next.sort((a, b) => a.code.localeCompare(b.code)); + }); + if (saved.status === "inactive") { + setEnabled((items) => items.filter((id) => id !== saved.id)); + } + resetDraft(); } catch (saveError) { - setError(getActionErrorMessage(saveError, "创建全局标签失败")); + setError(getActionErrorMessage(saveError, editingLabelId ? "更新全局标签失败" : "创建全局标签失败")); } finally { setSaving(false); } @@ -119,21 +145,46 @@ export function AnnotationLabelsPanel({ projectId, canManage }: { projectId: str placeholder="分类" className="rounded-md border border-[#2a2b2f] bg-black px-3 py-2 text-sm text-white" /> -