From 1c008f68d55ca964a00fa565382bc357518667ee Mon Sep 17 00:00:00 2001 From: eust-w Date: Mon, 31 Aug 2026 13:27:18 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Complete=20Fanyu=20production=20acc?= =?UTF-8?q?eptance=20flows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/fanyu-v5-s100/README.md | 2 +- deploy/fanyu-v5-s100/values.env.example | 1 - docs/annotation-operations.md | 34 +- docs/api.md | 4 +- docs/deployment.md | 5 +- docs/fanyu-v5-workflow.md | 2 +- docs/operators/原生视频标注-Gate.md | 2 +- internal/api/annotation_draft_test.go | 29 + internal/api/annotation_operations.go | 134 ++- internal/api/dashboard_workforce_test.go | 6 +- internal/api/fanyu_template.go | 10 +- internal/api/human_loop.go | 1 + internal/api/project_members.go | 123 ++- internal/api/project_members_test.go | 80 ++ internal/api/server.go | 4 +- internal/api/workflow_releases.go | 115 ++- internal/api/workflow_releases_test.go | 66 ++ internal/domain/annotation_operations.go | 13 +- internal/domain/delivery_package.go | 4 +- internal/domain/human_loop.go | 4 +- internal/domain/workflow_release.go | 1 + internal/worker/annotation_gate.go | 85 +- internal/worker/annotation_supplier_test.go | 70 ++ internal/worker/human_release.go | 6 +- internal/worker/human_release_test.go | 4 +- internal/worker/runtime_test.go | 21 +- k8s/droboticflow.template.yaml | 8 - scripts/deploy-envs/poc.env | 1 - scripts/deploy-k8s.sh | 2 - scripts/fanyu-100-browser-acceptance.mjs | 667 +++++++++++-- scripts/lib/deploy-validation.sh | 1 - scripts/render-manifests.sh | 3 - scripts/run_fanyu_full_human_in_the_loop.py | 895 +++++++++++++++--- src/components/ProjectMembersPanel.tsx | 122 ++- src/pages/AnnotationOperations.tsx | 88 +- src/pages/Dashboard.tsx | 40 +- src/pages/Templates.tsx | 96 +- src/services/api/annotation-operations.ts | 11 +- src/services/api/projects.ts | 18 +- src/types.ts | 36 +- tests/backend/render_manifests_test.py | 11 - .../backend/test_fanyu_acceptance_harness.py | 385 ++++++++ tests/backend/test_fanyu_v5_s100_bundle.py | 1 - .../fanyu-browser-acceptance.test.mjs | 43 + .../frontend/fanyu-comment-followup.test.mjs | 12 +- .../project-membership-cascade.test.mjs | 9 + 46 files changed, 2822 insertions(+), 453 deletions(-) create mode 100644 internal/worker/annotation_supplier_test.go create mode 100644 tests/frontend/fanyu-browser-acceptance.test.mjs diff --git a/deploy/fanyu-v5-s100/README.md b/deploy/fanyu-v5-s100/README.md index aa0d1ad4..ed187346 100644 --- a/deploy/fanyu-v5-s100/README.md +++ b/deploy/fanyu-v5-s100/README.md @@ -6,7 +6,7 @@ 1. 由 CI/Kaniko 发布的 digest-pinned 镜像:`video-face-blur`、`qwen-temporal-localization`、`wilor-valid-event-detection`、`ms-swift-dataset-export`、`qwen-label-crop-classification`。 2. PVC 中的 Qwen3-VL-8B-Instruct 固定 revision 快照和由 `scripts/verify_qwen3vl_model_manifest.py` 生成的两个 manifest 文件。 -3. 已就绪的 v5 供应商工作组 ID;当前验收暂不启用 Headpose/WiLoR。 +3. 已在目标项目中创建并启用的供应商,以及覆盖标注员、审核员和最终验收员的项目成员;安装工作流发布版时显式选择该供应商。 4. 每个 Qwen/LoRA 节点的一张可调度 GPU;工作流已将 Qwen 推理串行化,不需要占用 GSStudio/HoloBrain GPU。 5. `linux-arm64` 的 drosync DEB 和 portable tar;bundle builder 会校验文件 SHA-256、DEB `Architecture: arm64`、两个 AArch64 ELF、systemd installer,并拒绝 GUI 或其他 ABI。 diff --git a/deploy/fanyu-v5-s100/values.env.example b/deploy/fanyu-v5-s100/values.env.example index a18ea499..4074463f 100644 --- a/deploy/fanyu-v5-s100/values.env.example +++ b/deploy/fanyu-v5-s100/values.env.example @@ -1,5 +1,4 @@ # No secrets belong in this file. Inject credentials via the platform secret store. -FANYU_V5_SUPPLIER_ID= FANYU_HEADPOSE_API_BASE_URL= FANYU_HEADPOSE_SERVICE_IMAGE_ID= FANYU_HEADPOSE_MODEL_SHA256= diff --git a/docs/annotation-operations.md b/docs/annotation-operations.md index ae30961c..66e954fd 100644 --- a/docs/annotation-operations.md +++ b/docs/annotation-operations.md @@ -2,29 +2,31 @@ > 插件化重构后的通用状态机、API、分区释放和深链约定见 [Human Loop Core 与 Annotation QA 插件](./human-loop-plugin.md)。本页保留 Annotation QA 领域对象和兼容接口说明。 -`/annotation-operations` 提供项目级语义模板、供应商分组、真实切分、逐段标注、审核、验收、返工和工时统计。平台全局角色负责登录后的工作台边界,项目成员关系进一步限定项目、供应商和工作组。v5.8 生产角色固定为三类: +`/annotation-operations` 提供项目级语义模板、供应商、真实切分、逐段标注、审核、验收、返工和工时统计。平台账号角色负责组织级管理边界,`ProjectMember` 进一步限定项目、供应商和人工阶段。v5.8 生产角色固定为三类: - `annotator`(标注员):在同一个原子提交中完成真实切分校正和语义标注;既可先切完再标,也可边切边标。 - `quality_reviewer`(审核员):独立审核切分边界、左右目对应关系和语义标注,拒绝时把整条源任务退回标注员。 - `acceptance_reviewer`(最终验收员):在自动打包前独立执行整包全量验收;拒绝时整包退回审核员,不得参与该批次标注审核。 - `cutter`:只保留历史 v5.7 任务兼容,新建 v5.8 项目不再分配该角色。 -- `vendor_manager`:只聚合查看所管供应商/工作组的工作量和质量,不能读取任务媒体或执行审核。 +- `supplier_manager`:只聚合查看所属供应商的成员、工作量和质量,不能修改其他供应商或组织配置。 每个成员关系绑定 `projectId`、可选 `supplierId` 和三类项目角色。除组织管理员外,后端只返回当前用户所属项目和供应商范围内的任务、模板和工时事件;前端隐藏不是安全边界。 -每个工作台均有“可领取”“我的任务”池;管理员额外拥有“全部任务”,并可通过带乐观锁版本的 `PATCH /api/human-items/{id}/assignment` 将任务改派给符合当前阶段角色和工作组成员关系的用户,或退回共享池。所有领取、释放、改派、提交、审核和打回都会追加 HumanEvent/StageAttempt 审计记录。 +每个工作台均有“可领取”“我的任务”池;管理员额外拥有“全部任务”,并可通过带乐观锁版本的 `PATCH /api/human-items/{id}/assignment` 将任务改派给同项目、同供应商且符合当前阶段角色的用户,或退回共享池。所有领取、释放、改派、提交、审核和打回都会追加 HumanEvent/StageAttempt 审计记录。 ## 核心接口 -- `GET/POST /api/annotation-workgroups` -- `GET/POST /api/annotation-memberships` -- `GET/POST /api/annotation-templates` -- `POST /api/annotation-templates/{id}/publish` -- `GET/POST /api/annotation-tasks` -- `GET /api/annotation-tasks/{id}` -- `POST /api/annotation-tasks/{id}/revisions` -- `POST /api/annotation-tasks/{id}/reviews` -- `GET /api/annotation-workload-summary` +- `GET/POST /api/suppliers` +- `GET/POST /api/projects/{id}/members` +- `PATCH/DELETE /api/projects/{id}/members/{memberId}` +- `POST /api/projects/{id}/members/bulk`:批量添加、批量改角色或批量移除。 +- `GET/POST /api/plugins/annotation-qa/templates` +- `POST /api/plugins/annotation-qa/templates/{id}/publish` +- `GET/POST /api/plugins/annotation-qa/tasks` +- `GET /api/plugins/annotation-qa/tasks/{id}` +- `POST /api/plugins/annotation-qa/tasks/{id}/combined-submissions` +- `POST /api/plugins/annotation-qa/tasks/{id}/reviews` +- `GET /api/plugins/annotation-qa/workload-summary` - `GET /api/human-items?pool=available|mine|all` - `POST /api/human-items/{id}/claim` - `POST /api/human-items/{id}/release` @@ -38,16 +40,16 @@ 人工审核节点配置 `REVIEW_MODE=annotation` 后成为原生标注门,另外需要: - `ANNOTATION_TEMPLATE_ID`:当前项目中已发布的模板。 -- `ANNOTATION_SUPPLIER_ID`:可选供应商 ID;留空时按项目成员关系确定。 +- `ANNOTATION_SUPPLIER_ID`:工作流安装到项目时绑定的供应商 ID。Release 本身不固化供应商;生产项目必须具有覆盖三个人工角色的可用供应商。 - 新任务不接受 `ANNOTATION_ASSIGNED_USER_ID` 预分配;成员从 HumanItem 待领取池主动领取。 -Worker 到达该节点后不会启动 Kubernetes Job,也不会创建通用 AuditTask。它会从上游真实视频与 Qwen 边界创建 AnnotationTask,并写入 `projectId`、`dataTaskId`、`workflowId`、`runId`、`nodeId`、`artifactId`。Run 保持 `waiting_for_audit`,直到该节点下所有任务依次完成:标注员真实切分与语义标注 → 审核员统一审核 → 最终验收员整包验收。验收通过后系统自动释放 Package child Run。 +Worker 到达该节点后不会启动 Kubernetes Job,也不会创建通用 AuditTask。它会从上游真实视频与 Qwen 边界创建 AnnotationTask,并写入 `projectId`、`dataTaskId`、`workflowId`、`runId`、`nodeId`、`artifactId`。Run 保持 `waiting_for_human`,直到该节点下所有任务依次完成:标注员真实切分与语义标注 → 审核员统一审核 → 最终验收员整包验收。验收通过后系统自动释放 Package child Run。 v5.8 不预先把长视频拆成多个人工 Task;同一个源任务可提交多个最终切片。无法局部调整的丢帧、流缺失或时间轴异常必须整条任务退回自动重处理,不允许用 FFmpeg 拼接近似修复。 v5.8 打包以绝对 `timestamp_us` 为权威时钟,按同一个半开窗口同步裁切左右视频、左右 PTS CSV 和 IMU accel/gyro;输出侧车统一为毫秒并保留 clip-relative 时间。Audio 与 audio PTS 不处理、不交付,camera/IMU calibration 原文件在批次根目录保留且不做内参差异管理。 -标注审核通过后,Worker 生成 `droboticflow.annotation.accepted/v1` 的 `annotations-manifest.json`,记录模板版本、人工 revision、片段、artifact 和 Run lineage,然后从节点 `approved` 输出创建分区子 Run。成品打包完成后创建未分配的 Acceptance HumanItem;审核员领取并通过后才记录幂等最终导出快照,拒绝则进入拒绝区。 +标注审核通过后,Worker 生成 `droboticflow.annotation.accepted/v1` 的 `annotations-manifest.json`,记录模板版本、人工 revision、片段、artifact 和 Run lineage,然后从节点 `approved` 输出创建分区子 Run。成品打包前创建未分配的 Acceptance HumanItem;最终验收员领取并通过后才释放自动打包并记录幂等最终导出快照,拒绝则整包退回审核阶段。 `/annotation-operations` 支持通过以下参数从项目或监控页深链进入: @@ -68,7 +70,7 @@ v5.8 打包以绝对 `timestamp_us` 为权威时钟,按同一个半开窗口 ## 工作量口径 -首次 revision 产生 `outputDelta=1`;返工 revision 记录工时和返工次数,但 `outputDelta=0`,因此不会重复增加正式产出。质检、验收及拒绝原因分别记录为不可变工作量事件,汇总可按用户、供应商、分组和模板版本查询。 +首次 revision 产生 `outputDelta=1`;返工 revision 记录工时和返工次数,但 `outputDelta=0`,因此不会重复增加正式产出。质检、验收及拒绝原因分别记录为不可变工作量事件,汇总按用户、供应商和模板版本查询。 ## 泛宇算子 diff --git a/docs/api.md b/docs/api.md index ea65278b..cdf7c1ef 100644 --- a/docs/api.md +++ b/docs/api.md @@ -10,9 +10,9 @@ Base path: `/api` - `GET /api/ai/status`: returns whether server-side AI generation is enabled and which model is configured - `POST /api/ai/operators/generate`: generate a custom operator definition from natural language - `POST /api/ai/workflows/generate`: generate a workflow graph from natural language and the current operator catalog -- `GET /api/dashboard`: 管理员返回平台概览和供应商对比;`supplier_manager` 只返回其 `vendor_manager` 成员关系覆盖的团队指标,其他非管理员只返回本人工作量与质量。默认最近 30 天,`from/to` 最大 366 天。 +- `GET /api/dashboard`: 管理员返回平台概览和供应商对比;`supplier_manager` 只返回账号所属供应商的团队指标,其他非管理员只返回本人工作量与质量。默认最近 30 天,`from/to` 最大 366 天。 -When `DROBOTICFLOW_REQUIRE_SESSION_AUTH=true`, AI generation routes require an admin or operator session. Run 列表、详情、日志、资源、通用 artifact 元数据/内容和 lineage 只允许管理员会话;机器 API token 继续用于受控自动化。标注/质检/验收通过 `/api/human-items/:id/subject-content` 读取经过工作组和阶段角色校验的媒体。 +When `DROBOTICFLOW_REQUIRE_SESSION_AUTH=true`, AI generation routes require an admin or operator session. Run 列表、详情、日志、资源、通用 artifact 元数据/内容和 lineage 只允许管理员会话;机器 API token 继续用于受控自动化。标注/质检/验收通过 `/api/human-items/:id/subject-content` 读取经过项目、供应商和阶段角色校验的媒体。 ### Auth and Organizations diff --git a/docs/deployment.md b/docs/deployment.md index c0123b69..2dbc6bb7 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -75,9 +75,8 @@ export OPERATOR_RUNNER_IMAGE=ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/drobo export HEADPOSE_SERVICE_IMAGE=ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-headpose-service:20260408 export HEADPOSE_SERVICE_IMAGE_ID=ccr-29eug8s3-pub.cnc.bj.baidubce.com/dataflow/droboticflow-headpose-service@sha256:<64-hex-digest> export HEADPOSE_MODEL_SHA256=sha256:9bdcd0e072df0d21e92288b40ea1d1b3cf7173c44728ab9c45c4d673f361e018 -# Required to publish the Fanyu v5 left-camera workflow release. Obtain this -# from the verified POC annotation workgroup; do not invent an ID. -export FANYU_V5_SUPPLIER_ID= +# Supplier routing is bound when a published workflow is installed into a +# project. It is deliberately not a deployment-wide environment variable. export OPERATOR_GPU_NODE_SELECTOR=accelerator=nvidia export OPERATOR_GPU_TOLERATIONS_JSON='[{"key":"nvidia.com/gpu","operator":"Exists","effect":"NoSchedule"}]' export OPERATOR_GPU_RUNTIME_CLASS=nvidia diff --git a/docs/fanyu-v5-workflow.md b/docs/fanyu-v5-workflow.md index eb673bb6..4fc22bd4 100644 --- a/docs/fanyu-v5-workflow.md +++ b/docs/fanyu-v5-workflow.md @@ -49,5 +49,5 @@ python3 scripts/verify_qwen3vl_model_manifest.py --model-root /workspace/models/ 1. 独立构建并发布 `directory-scan`、`orbbec-data-normalize`、`orbbec-left-undistort`、`video-face-blur`、`qwen-behavior-rough-cut` 与 `package` 镜像,写入 digest-pinned release manifest;YuNet 资产必须随 `video-face-blur` 镜像离线携带并通过 SHA/size/license 门禁。 2. 预置并校验 Qwen3-VL 本地快照;当前无模型或未校验 manifest 即为预期 fail-closed 状态。 3. 为 Qwen3-VL 语义粗切腾出 1 张 GPU;目录读取、人工阶段与打包不申请 GPU。 -4. 可选提供 `FANYU_V5_SUPPLIER_ID`;留空时按项目成员关系确定供应商。本版本不读取 `FANYU_HEADPOSE_*`。 +4. 供应商不再通过 API 容器全局环境变量固化到 Release;安装发布版到项目时必须选择一个已覆盖标注员、审核员和最终验收员的项目供应商。 5. 仅在 `dev/poc` 验收环境中部署 `dev`,S100 使用同一镜像 digest 和以上 PVC/模型配置,不把草稿算子直接标为已发布。 diff --git a/docs/operators/原生视频标注-Gate.md b/docs/operators/原生视频标注-Gate.md index 92a0d934..7f4bc5f6 100644 --- a/docs/operators/原生视频标注-Gate.md +++ b/docs/operators/原生视频标注-Gate.md @@ -65,7 +65,7 @@ flowchart LR ## 关键超参数 - `ANNOTATION_TEMPLATE_ID`:类型 `string`,默认值 空字符串或由工作流显式填写。当前项目中已发布的语义模板 ID 通常建议在工作流中显式配置。 -- `ANNOTATION_SUPPLIER_ID`:类型 `string`,默认值 空字符串或由工作流显式填写。可选的供应商 ID;留空时按项目成员关系自动确定 通常建议在工作流中显式配置。 +- `ANNOTATION_SUPPLIER_ID`:类型 `string`。工作流发布版安装到项目时由平台注入并校验;发布版快照本身不携带环境供应商。仅当项目恰有一个完整覆盖三个人工角色的供应商时才允许自动推导。 - `REQUIRE_PREPACKAGE_ACCEPTANCE`:类型 `boolean`,默认值 `false`。全部切片统一质检通过后创建一个聚合的包前验收任务;验收仅允许确认 可直接使用默认值,也可在节点级覆盖。 ## 执行前准备 diff --git a/internal/api/annotation_draft_test.go b/internal/api/annotation_draft_test.go index e9bfbfae..0ec952b9 100644 --- a/internal/api/annotation_draft_test.go +++ b/internal/api/annotation_draft_test.go @@ -178,6 +178,35 @@ func TestCombinedSubmissionCanRequireEverySliceToBeCompleted(t *testing.T) { } } +func TestCombinedSubmissionSupportsImmediateAsynchronousResponse(t *testing.T) { + fixture := newCombinedDraftFixture(t) + payload := map[string]any{ + "version": fixture.item.Version, "requireSliceCompletion": true, + "sliceSegments": []map[string]any{{"id": "slice", "start": 0.0, "end": 10.0, "annotationStatus": "empty_confirmed"}}, + "segments": []any{}, + } + encoded, _ := json.Marshal(payload) + request := httptest.NewRequest(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/combined-submissions", bytes.NewReader(encoded)) + request.Header.Set("Content-Type", "application/json") + request.Header.Set("Prefer", "respond-async") + csrf := "combined-async-csrf" + request.AddCookie(&http.Cookie{Name: csrfCookieName, Value: csrf}) + request.Header.Set(csrfHeaderName, csrf) + request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, &authenticatedPrincipal{User: fixture.annotator, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}})) + recorder := httptest.NewRecorder() + fixture.handler.ServeHTTP(recorder, request) + if recorder.Code != http.StatusAccepted { + t.Fatalf("async combined submission status=%d body=%s", recorder.Code, recorder.Body.String()) + } + var response map[string]any + if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil { + t.Fatal(err) + } + if response["status"] != "processing" || response["submissionId"] == "" { + t.Fatalf("async combined submission response=%#v", response) + } +} + func TestAnnotationTaskGetEnrichesLinkedHumanItemDisplay(t *testing.T) { fixture := newCombinedDraftFixture(t) recorder := fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID, fixture.annotator, nil) diff --git a/internal/api/annotation_operations.go b/internal/api/annotation_operations.go index 031da48b..bc416d39 100644 --- a/internal/api/annotation_operations.go +++ b/internal/api/annotation_operations.go @@ -1,11 +1,13 @@ package api import ( + "bytes" "context" "crypto/sha256" "encoding/hex" "encoding/json" "fmt" + "io" "math" "net/http" "sort" @@ -17,6 +19,32 @@ import ( "github.com/go-chi/chi/v5" ) +type asynchronousAnnotationResponseWriter struct { + header http.Header + status int + body strings.Builder +} + +func (writer *asynchronousAnnotationResponseWriter) Header() http.Header { + if writer.header == nil { + writer.header = http.Header{} + } + return writer.header +} + +func (writer *asynchronousAnnotationResponseWriter) WriteHeader(status int) { + if writer.status == 0 { + writer.status = status + } +} + +func (writer *asynchronousAnnotationResponseWriter) Write(payload []byte) (int, error) { + if writer.status == 0 { + writer.status = http.StatusOK + } + return writer.body.Write(payload) +} + func deprecatedAnnotationEndpoint(next http.HandlerFunc) http.HandlerFunc { return func(writer http.ResponseWriter, request *http.Request) { writer.Header().Set("Deprecation", "true") @@ -104,8 +132,12 @@ func membershipAllowsTask(memberships []domain.ProjectAnnotationMembershipRecord if taskSupplierID == "" { taskSupplierID = task.VendorID } - if membership.VendorID != "" && taskSupplierID != "" && membership.VendorID != taskSupplierID { - continue + if taskSupplierID != "" { + // Current ProjectMember routing is supplier-scoped. An unscoped member + // must never become a wildcard across suppliers. + if strings.TrimSpace(membership.VendorID) != taskSupplierID { + continue + } } if membership.WorkgroupID != "" && task.WorkgroupID != "" && membership.WorkgroupID != task.WorkgroupID { continue @@ -610,7 +642,7 @@ type annotationTaskInput struct { ClipStart float64 `json:"clipStart"` ClipEnd float64 `json:"clipEnd"` TemplateID string `json:"templateId"` - WorkgroupID string `json:"workgroupId"` + SupplierID string `json:"supplierId"` AssignedUserID string `json:"assignedUserId,omitempty"` } @@ -867,20 +899,29 @@ func (server *server) handleAnnotationTaskCreate(writer http.ResponseWriter, req payload.ArtifactID = strings.TrimSpace(payload.ArtifactID) payload.SourceVideo = strings.TrimSpace(payload.SourceVideo) payload.TemplateID = strings.TrimSpace(payload.TemplateID) - payload.WorkgroupID = strings.TrimSpace(payload.WorkgroupID) - if payload.ProjectID == "" || payload.SourceVideo == "" || payload.TemplateID == "" || payload.WorkgroupID == "" || payload.ClipStart < 0 || payload.ClipEnd <= payload.ClipStart { - renderValidationError(writer, validationIssue{Field: "task", Message: "projectId, sourceVideo, templateId, workgroupId and a valid clip range are required"}) + payload.SupplierID = strings.TrimSpace(payload.SupplierID) + if payload.ProjectID == "" || payload.SourceVideo == "" || payload.TemplateID == "" || payload.SupplierID == "" || payload.ClipStart < 0 || payload.ClipEnd <= payload.ClipStart { + renderValidationError(writer, validationIssue{Field: "task", Message: "projectId, sourceVideo, templateId, supplierId and a valid clip range are required"}) return } project, projectErr := server.store.GetProject(request.Context(), payload.ProjectID) template, templateErr := server.store.GetAnnotationTemplate(request.Context(), payload.TemplateID) - group, groupErr := server.store.GetAnnotationWorkgroup(request.Context(), payload.WorkgroupID) - if projectErr != nil || templateErr != nil || groupErr != nil { - renderError(writer, http.StatusInternalServerError, fmt.Errorf("load annotation task references: project=%v template=%v workgroup=%v", projectErr, templateErr, groupErr)) + supplier, supplierErr := server.store.GetSupplier(request.Context(), payload.SupplierID) + if projectErr != nil || templateErr != nil || supplierErr != nil { + renderError(writer, http.StatusInternalServerError, fmt.Errorf("load annotation task references: project=%v template=%v supplier=%v", projectErr, templateErr, supplierErr)) return } - if project == nil || template == nil || group == nil || template.ProjectID != project.ID || !annotationOrganizationEqual(project.OrganizationID, template.OrganizationID) || !annotationOrganizationEqual(project.OrganizationID, group.OrganizationID) { - renderValidationError(writer, validationIssue{Field: "task", Message: "project, template and workgroup are incompatible"}) + if project == nil || template == nil || supplier == nil || template.ProjectID != project.ID || !annotationOrganizationEqual(project.OrganizationID, template.OrganizationID) || !annotationOrganizationEqual(project.OrganizationID, supplier.OrganizationID) || !strings.EqualFold(strings.TrimSpace(supplier.Status), "active") { + renderValidationError(writer, validationIssue{Field: "task", Message: "project, template and active supplier are incompatible"}) + return + } + members, membersErr := server.store.ListProjectMembers(request.Context(), project.OrganizationID, project.ID) + if membersErr != nil { + renderError(writer, http.StatusInternalServerError, membersErr) + return + } + if !supplierHasProductionHumanRoles(members, supplier.ID) { + renderValidationError(writer, validationIssue{Field: "supplierId", Message: "supplier project members must cover annotator, quality reviewer and acceptance reviewer"}) return } if template.Status != domain.AnnotationTemplatePublished { @@ -901,8 +942,7 @@ func (server *server) handleAnnotationTaskCreate(writer http.ResponseWriter, req ClipEnd: payload.ClipEnd, TemplateID: template.ID, TemplateVersion: template.Version, - WorkgroupID: group.ID, - VendorID: group.VendorID, + SupplierID: supplier.ID, AssignedUserID: "", Status: domain.AnnotationTaskStatusPendingAnnotation, CreatedAt: nowISO(), @@ -919,7 +959,7 @@ func (server *server) handleAnnotationTaskCreate(writer http.ResponseWriter, req taskMode, isTemp := server.resolveTaskRunMode(request.Context(), item.RunID) _ = server.store.UpsertAnnotationWorklogEvent(request.Context(), domain.AnnotationWorklogEventRecord{ ID: domain.NewID("annotation-worklog"), OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, - TaskID: item.ID, UserID: annotationPrincipalUserID(principal), SupplierID: item.SupplierID, WorkgroupID: item.WorkgroupID, VendorID: item.VendorID, + TaskID: item.ID, UserID: annotationPrincipalUserID(principal), SupplierID: item.SupplierID, EventType: "created", Mode: taskMode, Temporary: isTemp, TemplateVersion: item.TemplateVersion, OutputDelta: 0, CreatedAt: nowISO(), }) renderJSON(writer, http.StatusCreated, item) @@ -1304,6 +1344,34 @@ func (server *server) handleCombinedAnnotationSubmission(writer http.ResponseWri now := nowISO() sliceRevision := domain.SliceRevisionRecord{ID: sliceID, OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: revisionNumber, Segments: payload.SliceSegments, Operations: payload.SliceOperations, AuthorUserID: principal.User.ID, ChangeReason: submissionRef + ":" + strings.TrimSpace(payload.ChangeReason), DurationSeconds: payload.DurationSeconds, CreatedAt: now} annotationRevision := domain.AnnotationRevisionRecord{ID: annotationID, OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: revisionNumber, TemplateVersion: task.TemplateVersion, Segments: payload.Segments, ObjectHints: payload.ObjectHints, AuthorUserID: principal.User.ID, ChangeReason: submissionRef + ":" + strings.TrimSpace(payload.ChangeReason), IsRework: isRework, CountsAsOutput: !isRework, DurationSeconds: payload.DurationSeconds, CreatedAt: now} + if strings.Contains(strings.ToLower(request.Header.Get("Prefer")), "respond-async") { + jobKey := humanItem.OrganizationID + ":" + submissionRef + if _, loaded := server.combinedSubmissionJobs.LoadOrStore(jobKey, struct{}{}); !loaded { + payloadBytes, marshalErr := json.Marshal(payload) + if marshalErr != nil { + server.combinedSubmissionJobs.Delete(jobKey) + renderError(writer, http.StatusInternalServerError, marshalErr) + return + } + backgroundRequest := request.Clone(context.WithoutCancel(request.Context())) + backgroundRequest.Header = request.Header.Clone() + backgroundRequest.Header.Del("Prefer") + backgroundRequest.Body = io.NopCloser(bytes.NewReader(payloadBytes)) + backgroundRequest.ContentLength = int64(len(payloadBytes)) + go func() { + defer server.combinedSubmissionJobs.Delete(jobKey) + backgroundWriter := &asynchronousAnnotationResponseWriter{} + server.handleCombinedAnnotationSubmission(backgroundWriter, backgroundRequest) + if backgroundWriter.status >= http.StatusBadRequest { + fmt.Printf("[api] asynchronous combined submission %s failed status=%d body=%s\n", submissionRef, backgroundWriter.status, strings.TrimSpace(backgroundWriter.body.String())) + } + }() + } + renderJSON(writer, http.StatusAccepted, map[string]any{ + "item": humanItem, "submissionId": submissionRef, "status": "processing", "idempotent": resumed, + }) + return + } materialization, materializationErr := server.materializeCombinedSliceSubmission(request.Context(), *task, *humanItem, payload, revisionNumber) if materializationErr != nil { previousVersion := humanItem.Version @@ -1375,7 +1443,15 @@ func (server *server) handleCombinedAnnotationSubmission(writer http.ResponseWri if annotationRevision.CountsAsOutput { outputDelta = 1 } - _ = server.store.UpsertAnnotationWorklogEvent(request.Context(), domain.AnnotationWorklogEventRecord{ID: domain.NewID("annotation-worklog"), OrganizationID: task.OrganizationID, ProjectID: task.ProjectID, TaskID: task.ID, UserID: principal.User.ID, SupplierID: task.SupplierID, WorkgroupID: task.WorkgroupID, VendorID: task.VendorID, EventType: "combined_submitted", Mode: taskMode, Temporary: isTemp, Revision: annotationRevision.Revision, TemplateVersion: task.TemplateVersion, DurationSeconds: payload.DurationSeconds, OutputDelta: outputDelta, CreatedAt: now}) + outputDurationSeconds := payload.DurationSeconds + if outputDurationSeconds <= 0 { + var duration float64 + for _, slice := range sliceRevision.Segments { + duration += math.Max(0, slice.End-slice.Start) + } + outputDurationSeconds = int64(math.Round(duration)) + } + _ = server.store.UpsertAnnotationWorklogEvent(request.Context(), domain.AnnotationWorklogEventRecord{ID: domain.NewID("annotation-worklog"), OrganizationID: task.OrganizationID, ProjectID: task.ProjectID, TaskID: task.ID, UserID: principal.User.ID, SupplierID: task.SupplierID, WorkgroupID: task.WorkgroupID, VendorID: task.VendorID, EventType: "combined_submitted", Mode: taskMode, Temporary: isTemp, Revision: annotationRevision.Revision, TemplateVersion: task.TemplateVersion, DurationSeconds: outputDurationSeconds, OutputDelta: outputDelta, CreatedAt: now}) status := http.StatusCreated if resumed { status = http.StatusOK @@ -2335,13 +2411,19 @@ func (server *server) handleAnnotationWorkloadSummary(writer http.ResponseWriter renderError(writer, http.StatusInternalServerError, loadErr) return } - allowed := map[string]struct{}{} + allowedSuppliers := map[string]struct{}{} for _, membership := range memberships { - allowed[membership.WorkgroupID] = struct{}{} + if supplierID := strings.TrimSpace(membership.VendorID); supplierID != "" { + allowedSuppliers[supplierID] = struct{}{} + } } filtered := events[:0] for _, event := range events { - if _, ok := allowed[event.WorkgroupID]; !ok { + supplierID := strings.TrimSpace(event.SupplierID) + if supplierID == "" { + supplierID = strings.TrimSpace(event.VendorID) + } + if _, ok := allowedSuppliers[supplierID]; !ok { continue } if principal.User.Role == domain.UserRoleCutter || principal.User.Role == domain.UserRoleAnnotator || principal.User.Role == domain.UserRoleQualityReviewer || principal.User.Role == domain.UserRoleAcceptanceReviewer { @@ -2371,15 +2453,19 @@ func (server *server) handleAnnotationWorkloadSummary(writer http.ResponseWriter events = modeFiltered type key struct { - userID, workgroupID, vendorID string - templateVersion int + userID, supplierID string + templateVersion int } aggregates := map[key]*domain.AnnotationWorkloadAggregate{} for _, event := range events { - groupKey := key{event.UserID, event.WorkgroupID, event.VendorID, event.TemplateVersion} + supplierID := strings.TrimSpace(event.SupplierID) + if supplierID == "" { + supplierID = strings.TrimSpace(event.VendorID) + } + groupKey := key{event.UserID, supplierID, event.TemplateVersion} item := aggregates[groupKey] if item == nil { - item = &domain.AnnotationWorkloadAggregate{UserID: event.UserID, WorkgroupID: event.WorkgroupID, VendorID: event.VendorID, TemplateVersion: event.TemplateVersion} + item = &domain.AnnotationWorkloadAggregate{UserID: event.UserID, SupplierID: supplierID, TemplateVersion: event.TemplateVersion} aggregates[groupKey] = item } item.DurationSeconds += event.DurationSeconds @@ -2406,8 +2492,8 @@ func (server *server) handleAnnotationWorkloadSummary(writer http.ResponseWriter items = append(items, *item) } sort.Slice(items, func(left int, right int) bool { - leftKey := items[left].VendorID + "\x00" + items[left].WorkgroupID + "\x00" + items[left].UserID + "\x00" + strconv.Itoa(items[left].TemplateVersion) - rightKey := items[right].VendorID + "\x00" + items[right].WorkgroupID + "\x00" + items[right].UserID + "\x00" + strconv.Itoa(items[right].TemplateVersion) + leftKey := items[left].SupplierID + "\x00" + items[left].UserID + "\x00" + strconv.Itoa(items[left].TemplateVersion) + rightKey := items[right].SupplierID + "\x00" + items[right].UserID + "\x00" + strconv.Itoa(items[right].TemplateVersion) return leftKey < rightKey }) renderJSON(writer, http.StatusOK, domain.AnnotationWorkloadSummary{Items: items, EventCount: len(events)}) diff --git a/internal/api/dashboard_workforce_test.go b/internal/api/dashboard_workforce_test.go index b4e619e1..377564aa 100644 --- a/internal/api/dashboard_workforce_test.go +++ b/internal/api/dashboard_workforce_test.go @@ -50,8 +50,7 @@ func TestPopulateDashboardWorkforce_SkipsDebugEvents(t *testing.T) { ProjectID: "proj-1", TaskID: "task-1", UserID: "user-1", - WorkgroupID: "group-1", - VendorID: "vendor-1", + SupplierID: "supplier-1", EventType: "annotation_submitted", Mode: domain.RunModeProduction, DurationSeconds: 120, @@ -69,8 +68,7 @@ func TestPopulateDashboardWorkforce_SkipsDebugEvents(t *testing.T) { ProjectID: "proj-1", TaskID: "task-dbg-1", UserID: "user-1", - WorkgroupID: "group-1", - VendorID: "vendor-1", + SupplierID: "supplier-1", EventType: "annotation_submitted", Mode: domain.RunModeDebug, Temporary: true, diff --git a/internal/api/fanyu_template.go b/internal/api/fanyu_template.go index a5bf3d39..7e0fdb96 100644 --- a/internal/api/fanyu_template.go +++ b/internal/api/fanyu_template.go @@ -396,7 +396,7 @@ func (server *server) ensureFanyuDeliveryTemplateV58(ctx context.Context, organi node.Position.Y, map[string]string{ "ANNOTATION_TEMPLATE_ID": annotationTemplateID, - "ANNOTATION_SUPPLIER_ID": strings.TrimSpace(os.Getenv("FANYU_V5_SUPPLIER_ID")), + "ANNOTATION_SUPPLIER_ID": "", "RELEASE_POLICY": "all_accepted", "PARTITION_MAX_ITEMS": "32", "MAX_REWORK_ROUNDS": "5", @@ -570,7 +570,7 @@ func (server *server) ensureFanyuDeliveryTemplateV5(ctx context.Context, organiz operators[operatorID] = *operator } annotationTemplateID := fmt.Sprintf("tpl-fanyu-convenience-store-v5-%s", normalizedOrganizationID(organizationID)) - annotationSupplierID := strings.TrimSpace(os.Getenv("FANYU_V5_SUPPLIER_ID")) + annotationSupplierID := "" nodes := []domain.WorkflowNode{ fanyuNode("fanyu-v5-directory", "目录读取(必填)", operators[operatorIDs[0]], 0, 260), fanyuNodeWithHyperparams("fanyu-v5-normalize", "Orbbec Ego 左相机标准化", operators[operatorIDs[1]], 240, 260, map[string]string{"CAMERA_SIDE": "left", "REQUIRE_STEREO": "true"}), @@ -695,7 +695,7 @@ func (server *server) ensureFanyuDeliveryTemplateV54(ctx context.Context, organi } } annotationTemplateID := fmt.Sprintf("tpl-fanyu-convenience-store-v5-%s", organizationID) - annotationSupplierID := strings.TrimSpace(os.Getenv("FANYU_V5_SUPPLIER_ID")) + annotationSupplierID := "" nodes := []domain.WorkflowNode{ fanyuMultiportNode("fanyu-v54-directory", "目录读取(多端口)", operators[operatorIDs[0]], 0, 260), fanyuMultiportNodeWithHyperparams("fanyu-v54-normalize", "Orbbec Ego 左相机标准化(多端口)", operators[operatorIDs[1]], 260, 260, map[string]string{"CAMERA_SIDE": "left", "REQUIRE_STEREO": "true"}), @@ -809,7 +809,7 @@ func (server *server) ensureFanyuDeliveryTemplateV55(ctx context.Context, organi } } annotationTemplateID := fmt.Sprintf("tpl-fanyu-convenience-store-v5-%s", organizationID) - annotationSupplierID := strings.TrimSpace(os.Getenv("FANYU_V5_SUPPLIER_ID")) + annotationSupplierID := "" nodes := []domain.WorkflowNode{ fanyuMultiportNode("fanyu-v55-directory", "目录读取(多端口)", operators[operatorIDs[0]], 0, 260), fanyuMultiportNodeWithHyperparams("fanyu-v55-normalize", "Orbbec Ego 左相机标准化(多端口)", operators[operatorIDs[1]], 240, 260, map[string]string{"CAMERA_SIDE": "left", "REQUIRE_STEREO": "true"}), @@ -938,7 +938,7 @@ func (server *server) ensureFanyuDeliveryTemplateV56(ctx context.Context, organi } annotationTemplateID := fmt.Sprintf("tpl-fanyu-convenience-store-v5-%s", organizationID) - annotationSupplierID := strings.TrimSpace(os.Getenv("FANYU_V5_SUPPLIER_ID")) + annotationSupplierID := "" headposeOverrides := map[string]string{ "HEADPOSE_API_BASE_URL": strings.TrimSpace(os.Getenv("FANYU_HEADPOSE_API_BASE_URL")), "HEADPOSE_SERVICE_IMAGE_ID": strings.TrimSpace(os.Getenv("FANYU_HEADPOSE_SERVICE_IMAGE_ID")), diff --git a/internal/api/human_loop.go b/internal/api/human_loop.go index 5c653346..32755c32 100644 --- a/internal/api/human_loop.go +++ b/internal/api/human_loop.go @@ -1918,6 +1918,7 @@ func (server *server) ensureFanyuV5PrePackageAcceptance(ctx context.Context, sou ProjectID: source.ProjectID, WorkflowID: source.WorkflowID, RunID: source.RunID, GateNodeID: acceptanceBatch.GateNodeID, Subject: domain.HumanSubjectRef{ItemKey: source.ID, MediaType: "application/json", Payload: map[string]any{"sourceBatchId": source.ID, "items": aggregate, "statistics": statistics, "strictThreeRole": domain.IsFanyuV58ThreeRoleHumanLoop(source.Stages)}}, CurrentStageID: fanyuV5PrePackageAcceptanceStageID, Status: domain.HumanItemReady, + SupplierID: items[0].SupplierID, LegacyAssignment: items[0].LegacyAssignment, WorkgroupID: items[0].WorkgroupID, VendorID: items[0].VendorID, MaxReworkRounds: source.MaxReworkRounds, Version: 1, CreatedAt: now, UpdatedAt: now, } } else { diff --git a/internal/api/project_members.go b/internal/api/project_members.go index 8fac086c..2a792315 100644 --- a/internal/api/project_members.go +++ b/internal/api/project_members.go @@ -25,6 +25,12 @@ type projectMemberBulkInput struct { Action string `json:"action"` MemberIDs []string `json:"memberIds"` Roles []domain.AnnotationAssignmentRole `json:"roles,omitempty"` + Entries []projectMemberInput `json:"entries,omitempty"` +} + +type projectMemberBulkResult struct { + Created int `json:"created"` + Updated int `json:"updated"` } func isOrganizationAdmin(principal *authenticatedPrincipal) bool { @@ -144,8 +150,8 @@ func (server *server) saveProjectMember(writer http.ResponseWriter, request *htt renderError(writer, http.StatusInternalServerError, supplierErr) return } - if supplier == nil || !annotationOrganizationEqual(supplier.OrganizationID, project.OrganizationID) { - renderValidationError(writer, validationIssue{Field: "supplierId", Message: "supplier must exist in the current organization"}) + if supplier == nil || !annotationOrganizationEqual(supplier.OrganizationID, project.OrganizationID) || !strings.EqualFold(strings.TrimSpace(supplier.Status), "active") { + renderValidationError(writer, validationIssue{Field: "supplierId", Message: "an active supplier must exist in the current organization"}) return } } @@ -300,13 +306,27 @@ func (server *server) handleProjectMembersBulk(writer http.ResponseWriter, reque return } projectID := strings.TrimSpace(chi.URLParam(request, "id")) + project, err := server.store.GetProject(request.Context(), projectID) + if err != nil { + renderError(writer, http.StatusInternalServerError, err) + return + } + if project == nil || !server.filterByTenant(request, project.OrganizationID) { + renderJSON(writer, http.StatusNotFound, map[string]string{"error": "project not found"}) + return + } var payload projectMemberBulkInput if !decodeJSON(writer, request, &payload) { return } + payload.Action = strings.ToLower(strings.TrimSpace(payload.Action)) + if payload.Action == "add" { + server.handleProjectMembersBulkAdd(writer, request, project, payload.Entries) + return + } roles, valid := normalizedMemberRoles(payload.Roles) if len(payload.MemberIDs) == 0 || (payload.Action == "set_roles" && !valid) || (payload.Action != "set_roles" && payload.Action != "remove") { - renderValidationError(writer, validationIssue{Field: "bulk", Message: "memberIds and a valid bulk action are required"}) + renderValidationError(writer, validationIssue{Field: "bulk", Message: "memberIds and action add, set_roles, or remove are required"}) return } updated := 0 @@ -329,5 +349,100 @@ func (server *server) handleProjectMembersBulk(writer http.ResponseWriter, reque } updated++ } - renderJSON(writer, http.StatusOK, map[string]int{"updated": updated}) + renderJSON(writer, http.StatusOK, projectMemberBulkResult{Updated: updated}) +} + +func (server *server) handleProjectMembersBulkAdd(writer http.ResponseWriter, request *http.Request, project *domain.ProjectRecord, entries []projectMemberInput) { + if len(entries) == 0 { + renderValidationError(writer, validationIssue{Field: "entries", Message: "at least one project member entry is required"}) + return + } + + // Validate the entire request before writing any member. This makes invalid + // users, suppliers, roles, and duplicates a whole-request failure instead of + // leaving a partially created project membership set. + prepared := make([]domain.ProjectMemberRecord, 0, len(entries)) + seenUsers := make(map[string]int, len(entries)) + supplierCache := make(map[string]*domain.SupplierRecord) + for index, entry := range entries { + entry.UserID = strings.TrimSpace(entry.UserID) + entry.SupplierID = strings.TrimSpace(entry.SupplierID) + roles, valid := normalizedMemberRoles(entry.Roles) + if entry.UserID == "" || !valid { + renderValidationError(writer, validationIssue{Field: "entries", Message: "every entry requires userId and at least one project role"}) + return + } + if firstIndex, exists := seenUsers[entry.UserID]; exists { + renderJSON(writer, http.StatusConflict, map[string]any{ + "error": "duplicate user in bulk project member request", + "code": "duplicate_project_member_entry", + "userId": entry.UserID, + "firstIndex": firstIndex, + "index": index, + }) + return + } + seenUsers[entry.UserID] = index + + existing, err := server.store.GetProjectMemberByProjectUser(request.Context(), project.OrganizationID, project.ID, entry.UserID) + if err != nil { + renderError(writer, http.StatusInternalServerError, err) + return + } + if existing != nil { + renderJSON(writer, http.StatusConflict, map[string]any{ + "error": "project member already exists", + "code": "project_member_exists", + "userId": entry.UserID, + "member": existing, + }) + return + } + + user, err := server.store.GetUser(request.Context(), entry.UserID) + if err != nil { + renderError(writer, http.StatusInternalServerError, err) + return + } + if user == nil || !annotationOrganizationEqual(user.OrganizationID, project.OrganizationID) { + renderValidationError(writer, validationIssue{Field: "entries", Message: "every user must exist in the current organization"}) + return + } + if entry.SupplierID == "" { + entry.SupplierID = strings.TrimSpace(user.SupplierID) + } + if entry.SupplierID != "" { + supplier, cached := supplierCache[entry.SupplierID] + if !cached { + supplier, err = server.store.GetSupplier(request.Context(), entry.SupplierID) + if err != nil { + renderError(writer, http.StatusInternalServerError, err) + return + } + supplierCache[entry.SupplierID] = supplier + } + if supplier == nil || !annotationOrganizationEqual(supplier.OrganizationID, project.OrganizationID) || !strings.EqualFold(strings.TrimSpace(supplier.Status), "active") { + renderValidationError(writer, validationIssue{Field: "entries", Message: "every supplier must be active and exist in the current organization"}) + return + } + } + + prepared = append(prepared, domain.ProjectMemberRecord{ + ID: domain.NewID("project-member"), + OrganizationID: project.OrganizationID, + ProjectID: project.ID, + UserID: user.ID, + SupplierID: entry.SupplierID, + Roles: roles, + CreatedAt: nowISO(), + }) + } + + for _, member := range prepared { + if err := server.store.UpsertProjectMember(request.Context(), member); err != nil { + renderError(writer, http.StatusInternalServerError, err) + return + } + } + renderJSON(writer, http.StatusOK, projectMemberBulkResult{Created: len(prepared)}) } diff --git a/internal/api/project_members_test.go b/internal/api/project_members_test.go index dc1970fe..c642044f 100644 --- a/internal/api/project_members_test.go +++ b/internal/api/project_members_test.go @@ -106,6 +106,86 @@ func TestBulkProjectMemberRemovalReleasesClaimedTasks(t *testing.T) { } } +func TestBulkProjectMemberAddValidatesWholeRequestBeforeWriting(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, "Bulk add members", "bulk-add-members") + admin := adminCreateUser(t, handler, rootCookie, organization.ID, "Org Admin", "bulk-add-admin@example.com", domain.UserRoleAdmin, "admin-password") + first := adminCreateUser(t, handler, rootCookie, organization.ID, "First", "bulk-first@example.com", domain.UserRoleMember, "worker-password") + second := adminCreateUser(t, handler, rootCookie, organization.ID, "Second", "bulk-second@example.com", domain.UserRoleMember, "worker-password") + third := adminCreateUser(t, handler, rootCookie, organization.ID, "Third", "bulk-third@example.com", domain.UserRoleMember, "worker-password") + adminCookie := loginSession(t, handler, admin.Email, "admin-password") + project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: organization.ID, Name: "Bulk add project"}) + if err != nil { + t.Fatal(err) + } + supplier := domain.SupplierRecord{ID: "supplier-bulk-add", OrganizationID: organization.ID, Name: "Bulk supplier"} + if err := dataStore.UpsertSupplier(ctx, supplier); err != nil { + t.Fatal(err) + } + + addBody, _ := json.Marshal(map[string]any{ + "action": "add", + "entries": []map[string]any{ + {"userId": first.ID, "supplierId": supplier.ID, "roles": []string{"annotator"}}, + {"userId": second.ID, "roles": []string{"quality_reviewer", "acceptance_reviewer"}}, + }, + }) + added := serveJSONWithCookie(handler, http.MethodPost, "/api/projects/"+project.ID+"/members/bulk", addBody, adminCookie) + if added.Code != http.StatusOK || !jsonContains(added.Body.String(), `"created":2`) || !jsonContains(added.Body.String(), `"updated":0`) { + t.Fatalf("bulk add=%d body=%s", added.Code, added.Body.String()) + } + firstMember, _ := dataStore.GetProjectMemberByProjectUser(ctx, organization.ID, project.ID, first.ID) + secondMember, _ := dataStore.GetProjectMemberByProjectUser(ctx, organization.ID, project.ID, second.ID) + if firstMember == nil || firstMember.SupplierID != supplier.ID || len(firstMember.Roles) != 1 { + t.Fatalf("first member=%#v", firstMember) + } + if secondMember == nil || len(secondMember.Roles) != 2 { + t.Fatalf("second member=%#v", secondMember) + } + + conflictBody, _ := json.Marshal(map[string]any{ + "action": "add", + "entries": []map[string]any{ + {"userId": third.ID, "roles": []string{"annotator"}}, + {"userId": first.ID, "roles": []string{"annotator"}}, + }, + }) + conflict := serveJSONWithCookie(handler, http.MethodPost, "/api/projects/"+project.ID+"/members/bulk", conflictBody, adminCookie) + if conflict.Code != http.StatusConflict || !jsonContains(conflict.Body.String(), `"code":"project_member_exists"`) { + t.Fatalf("existing conflict=%d body=%s", conflict.Code, conflict.Body.String()) + } + thirdMember, _ := dataStore.GetProjectMemberByProjectUser(ctx, organization.ID, project.ID, third.ID) + if thirdMember != nil { + t.Fatalf("bulk conflict partially created third member: %#v", thirdMember) + } + + duplicateBody, _ := json.Marshal(map[string]any{ + "action": "add", + "entries": []map[string]any{ + {"userId": third.ID, "roles": []string{"annotator"}}, + {"userId": third.ID, "roles": []string{"quality_reviewer"}}, + }, + }) + duplicate := serveJSONWithCookie(handler, http.MethodPost, "/api/projects/"+project.ID+"/members/bulk", duplicateBody, adminCookie) + if duplicate.Code != http.StatusConflict || !jsonContains(duplicate.Body.String(), `"code":"duplicate_project_member_entry"`) { + t.Fatalf("payload duplicate=%d body=%s", duplicate.Code, duplicate.Body.String()) + } + thirdMember, _ = dataStore.GetProjectMemberByProjectUser(ctx, organization.ID, project.ID, third.ID) + if thirdMember != nil { + t.Fatalf("duplicate payload partially created third member: %#v", thirdMember) + } + + memberCookie := loginSession(t, handler, third.Email, "worker-password") + forbidden := serveJSONWithCookie(handler, http.MethodPost, "/api/projects/"+project.ID+"/members/bulk", duplicateBody, memberCookie) + if forbidden.Code != http.StatusForbidden { + t.Fatalf("ordinary member bulk add=%d body=%s", forbidden.Code, forbidden.Body.String()) + } +} + 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 c0937ff5..08c568ac 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -23,6 +23,7 @@ import ( "sort" "strconv" "strings" + "sync" "time" "unicode/utf8" @@ -71,6 +72,7 @@ type server struct { buildCommit string buildTime string image string + combinedSubmissionJobs sync.Map } type runJobTerminator interface { @@ -1361,8 +1363,6 @@ func NewHandlerWithConfig(dataStore store.Store, aiService ai.Service, appConfig router.Get("/api/data-operations/stage-items", instance.handleDataOperationsStageItems) router.Get("/api/data-operations/training-usage", instance.handleDataOperationsTrainingUsage) router.Get("/api/data-operations/datasets", instance.handleDataOperationsDatasets) - router.Get("/api/annotation-workgroups", deprecatedAnnotationEndpoint(instance.handleAnnotationWorkgroupsList)) - router.Get("/api/annotation-memberships", deprecatedAnnotationEndpoint(instance.handleAnnotationMembershipsList)) router.Get("/api/annotation-templates", deprecatedAnnotationEndpoint(instance.handleAnnotationTemplatesList)) router.Get("/api/annotation-tasks", deprecatedAnnotationEndpoint(instance.handleAnnotationTasksList)) router.Get("/api/annotation-tasks/{id}", deprecatedAnnotationEndpoint(instance.handleAnnotationTaskGet)) diff --git a/internal/api/workflow_releases.go b/internal/api/workflow_releases.go index eaf4f38e..893ffb90 100644 --- a/internal/api/workflow_releases.go +++ b/internal/api/workflow_releases.go @@ -24,7 +24,106 @@ type workflowPublishRequest struct { } type projectWorkflowInstanceRequest struct { - ReleaseID string `json:"releaseId"` + ReleaseID string `json:"releaseId"` + SupplierID string `json:"supplierId,omitempty"` +} + +var productionHumanRoles = []domain.AnnotationAssignmentRole{ + domain.AnnotationRoleAnnotator, + domain.AnnotationRoleQualityReviewer, + domain.AnnotationRoleAcceptanceReviewer, +} + +func releaseUsesAnnotationGate(release domain.WorkflowReleaseRecord) bool { + for _, node := range release.Nodes { + if strings.TrimSpace(node.Data.OperatorID) == "operator-builtin-annotation-gate" { + return true + } + } + return false +} + +func supplierHasProductionHumanRoles(members []domain.ProjectMemberRecord, supplierID string) bool { + covered := map[domain.AnnotationAssignmentRole]bool{} + for _, member := range members { + if strings.TrimSpace(member.SupplierID) != supplierID { + continue + } + for _, role := range member.Roles { + covered[role] = true + } + } + for _, role := range productionHumanRoles { + if !covered[role] { + return false + } + } + return true +} + +func bindAnnotationSupplier(nodes []domain.WorkflowNode, supplierID string) []domain.WorkflowNode { + cloned := make([]domain.WorkflowNode, len(nodes)) + copy(cloned, nodes) + for index := range cloned { + cloned[index].Data.Hyperparams = append([]domain.HyperParam(nil), cloned[index].Data.Hyperparams...) + if strings.TrimSpace(cloned[index].Data.OperatorID) != "operator-builtin-annotation-gate" { + continue + } + bound := false + for paramIndex := range cloned[index].Data.Hyperparams { + if strings.TrimSpace(cloned[index].Data.Hyperparams[paramIndex].Key) != "ANNOTATION_SUPPLIER_ID" { + continue + } + cloned[index].Data.Hyperparams[paramIndex].Value = supplierID + cloned[index].Data.Hyperparams[paramIndex].Type = "string" + cloned[index].Data.Hyperparams[paramIndex].Description = "当前项目绑定的供应商 ID" + bound = true + } + if !bound { + cloned[index].Data.Hyperparams = append(cloned[index].Data.Hyperparams, domain.HyperParam{ + Key: "ANNOTATION_SUPPLIER_ID", Value: supplierID, Type: "string", Description: "当前项目绑定的供应商 ID", + }) + } + } + return cloned +} + +func (server *server) resolveProjectWorkflowSupplier(ctx context.Context, project domain.ProjectRecord, release domain.WorkflowReleaseRecord, requested string) (string, []validationIssue, error) { + if !releaseUsesAnnotationGate(release) { + return "", nil, nil + } + members, err := server.store.ListProjectMembers(ctx, project.OrganizationID, project.ID) + if err != nil { + return "", nil, err + } + requested = strings.TrimSpace(requested) + if requested != "" { + supplier, supplierErr := server.store.GetSupplier(ctx, requested) + if supplierErr != nil { + return "", nil, supplierErr + } + if supplier == nil || normalizedOrganizationID(supplier.OrganizationID) != normalizedOrganizationID(project.OrganizationID) || !strings.EqualFold(strings.TrimSpace(supplier.Status), "active") { + return "", []validationIssue{{Field: "supplierId", Message: "an active supplier in the same organization is required"}}, nil + } + if !supplierHasProductionHumanRoles(members, requested) { + return "", []validationIssue{{Field: "supplierId", Message: "supplier project members must cover annotator, quality reviewer and acceptance reviewer"}}, nil + } + return requested, nil, nil + } + candidates := map[string]struct{}{} + for _, member := range members { + supplierID := strings.TrimSpace(member.SupplierID) + if supplierID != "" && supplierHasProductionHumanRoles(members, supplierID) { + candidates[supplierID] = struct{}{} + } + } + if len(candidates) != 1 { + return "", []validationIssue{{Field: "supplierId", Message: "select exactly one supplier whose project members cover all three production roles"}}, nil + } + for supplierID := range candidates { + return server.resolveProjectWorkflowSupplier(ctx, project, release, supplierID) + } + return "", []validationIssue{{Field: "supplierId", Message: "supplier is required"}}, nil } func templateSnapshotFromWorkflow(item domain.WorkflowRecord) domain.TemplateRecord { @@ -263,6 +362,15 @@ func (server *server) handleProjectWorkflowInstanceCreate(writer http.ResponseWr renderValidationError(writer, validationIssue{Field: "releaseId", Message: "published workflow release is required"}) return } + supplierID, supplierIssues, supplierErr := server.resolveProjectWorkflowSupplier(request.Context(), *project, *release, payload.SupplierID) + if supplierErr != nil { + renderError(writer, http.StatusInternalServerError, supplierErr) + return + } + if len(supplierIssues) > 0 { + renderValidationError(writer, supplierIssues[0]) + return + } for _, pluginID := range release.RequiredPluginIDs { if pluginID != pluginregistry.AnnotationQAID { continue @@ -282,7 +390,7 @@ func (server *server) handleProjectWorkflowInstanceCreate(writer http.ResponseWr Type: release.Type, Kind: release.Kind, ExecutionScope: release.ExecutionScope, ScanConfig: release.ScanConfig, Hyperparams: release.Hyperparams, MaxConcurrentRuns: release.MaxConcurrentRuns, NodeTimeoutSeconds: release.NodeTimeoutSeconds, RunTimeoutSeconds: release.RunTimeoutSeconds, MaxRetryAttempts: release.MaxRetryAttempts, - RetryBackoffSeconds: release.RetryBackoffSeconds, Schedule: release.Schedule, Nodes: release.Nodes, Edges: release.Edges, + RetryBackoffSeconds: release.RetryBackoffSeconds, Schedule: release.Schedule, Nodes: bindAnnotationSupplier(release.Nodes, supplierID), Edges: release.Edges, WorkflowInputs: release.WorkflowInputs, ArtifactStorage: release.ArtifactStorage, }) if err != nil { @@ -322,7 +430,8 @@ func (server *server) handleProjectWorkflowInstanceCreate(writer http.ResponseWr instance := domain.ProjectWorkflowInstanceRecord{ ID: domain.NewID("project-workflow-instance"), OrganizationID: project.OrganizationID, ProjectID: project.ID, ReleaseID: release.ID, ReleaseVersion: release.Version, SourceTemplateID: release.TemplateID, WorkflowID: workflow.ID, - Status: domain.ProjectWorkflowInstanceActive, SupersedesInstanceID: supersedes, CreatedByUserID: principal.User.ID, + SupplierID: supplierID, + Status: domain.ProjectWorkflowInstanceActive, SupersedesInstanceID: supersedes, CreatedByUserID: principal.User.ID, CreatedAt: now, UpdatedAt: now, } if err := server.store.UpsertProjectWorkflowInstance(request.Context(), instance); err != nil { diff --git a/internal/api/workflow_releases_test.go b/internal/api/workflow_releases_test.go index 8041875a..0b1ab3c9 100644 --- a/internal/api/workflow_releases_test.go +++ b/internal/api/workflow_releases_test.go @@ -60,6 +60,72 @@ func TestProjectWorkflowInstancePersistsReleasedProjectWorkflow(t *testing.T) { } } +func TestProjectWorkflowInstanceBindsValidatedSupplier(t *testing.T) { + ctx := context.Background() + dataStore := store.NewMemoryStore() + project := domain.ProjectRecord{ID: "project-supplier-binding", OrganizationID: domain.DefaultOrganizationID, Name: "Project"} + if err := dataStore.UpsertProject(ctx, project); err != nil { + t.Fatal(err) + } + supplier := domain.SupplierRecord{ID: "supplier-production", OrganizationID: domain.DefaultOrganizationID, Name: "Production supplier", Status: "active", CreatedAt: nowISO(), UpdatedAt: nowISO()} + if err := dataStore.UpsertSupplier(ctx, supplier); err != nil { + t.Fatal(err) + } + for index, role := range []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator, domain.AnnotationRoleQualityReviewer, domain.AnnotationRoleAcceptanceReviewer} { + if err := dataStore.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ + ID: fmt.Sprintf("member-%d", index), OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, + UserID: fmt.Sprintf("user-%d", index), SupplierID: supplier.ID, Roles: []domain.AnnotationAssignmentRole{role}, CreatedAt: nowISO(), UpdatedAt: nowISO(), + }); err != nil { + t.Fatal(err) + } + } + release := domain.WorkflowReleaseRecord{ + ID: "release-supplier-binding", OrganizationID: domain.DefaultOrganizationID, TemplateID: "template-supplier-binding", Version: 1, + Name: "Released human workflow", Type: domain.WorkflowTypeBatch, Kind: domain.WorkflowKindStandard, + ExecutionScope: domain.WorkflowExecutionScopeProduction, + Nodes: []domain.WorkflowNode{{ID: "human", Type: "operator", Data: domain.WorkflowNodeData{ + Label: "Human gate", OperatorID: "operator-builtin-annotation-gate", + Hyperparams: []domain.HyperParam{{Key: "ANNOTATION_SUPPLIER_ID", Value: "release-must-not-own-supplier"}}, + }}}, + CreatedAt: nowISO(), PublishedAt: nowISO(), + } + if err := dataStore.UpsertWorkflowRelease(ctx, release); err != nil { + t.Fatal(err) + } + handler := NewHandler(dataStore, nil) + requestBody, _ := json.Marshal(map[string]string{"releaseId": release.ID, "supplierId": supplier.ID}) + request := httptest.NewRequest(http.MethodPost, "/api/projects/"+project.ID+"/workflow-instances", bytes.NewReader(requestBody)) + request.Header.Set("Content-Type", "application/json") + request.AddCookie(&http.Cookie{Name: "droboticflow_csrf", Value: "supplier-binding-csrf"}) + request.Header.Set("X-Droboticflow-CSRF-Token", "supplier-binding-csrf") + request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, &authenticatedPrincipal{ + User: domain.UserRecord{ID: "admin-supplier-binding", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin}, + Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}, + })) + recorder := httptest.NewRecorder() + handler.ServeHTTP(recorder, request) + if recorder.Code != http.StatusCreated { + t.Fatalf("create bound workflow instance status=%d body=%s", recorder.Code, recorder.Body.String()) + } + var response struct { + Instance domain.ProjectWorkflowInstanceRecord `json:"instance"` + Workflow domain.WorkflowRecord `json:"workflow"` + } + if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil { + t.Fatal(err) + } + if response.Instance.SupplierID != supplier.ID { + t.Fatalf("instance supplier=%q want=%q", response.Instance.SupplierID, supplier.ID) + } + params := response.Workflow.Nodes[0].Data.Hyperparams + if len(params) != 1 || params[0].Key != "ANNOTATION_SUPPLIER_ID" || params[0].Value != supplier.ID { + t.Fatalf("workflow supplier binding=%#v", params) + } + if release.Nodes[0].Data.Hyperparams[0].Value != "release-must-not-own-supplier" { + t.Fatalf("release snapshot was mutated: %#v", release.Nodes[0].Data.Hyperparams) + } +} + func TestWorkflowReleaseRunEndpointSecurityGate(t *testing.T) { ctx := context.Background() dataStore := store.NewMemoryStore() diff --git a/internal/domain/annotation_operations.go b/internal/domain/annotation_operations.go index f0d74e35..f58f1641 100644 --- a/internal/domain/annotation_operations.go +++ b/internal/domain/annotation_operations.go @@ -173,8 +173,8 @@ type AnnotationTaskRecord struct { LegacyAssignment map[string]string `json:"legacyAssignment,omitempty" bson:"legacyAssignment,omitempty"` // Deprecated compatibility fields. Mongo migration copies their historical // value into LegacyAssignment and unsets them for production records. - WorkgroupID string `json:"workgroupId,omitempty" bson:"workgroupId,omitempty"` - VendorID string `json:"vendorId,omitempty" bson:"vendorId,omitempty"` + WorkgroupID string `json:"-" bson:"workgroupId,omitempty"` + VendorID string `json:"-" bson:"vendorId,omitempty"` AssignedUserID string `json:"assignedUserId,omitempty" bson:"assignedUserId,omitempty"` Status AnnotationTaskStatus `json:"status" bson:"status"` CurrentRevision int `json:"currentRevision" bson:"currentRevision"` @@ -229,8 +229,8 @@ type AnnotationWorklogEventRecord struct { UserID string `json:"userId" bson:"userId"` SupplierID string `json:"supplierId,omitempty" bson:"supplierId,omitempty"` LegacyAssignment map[string]string `json:"legacyAssignment,omitempty" bson:"legacyAssignment,omitempty"` - WorkgroupID string `json:"workgroupId,omitempty" bson:"workgroupId,omitempty"` - VendorID string `json:"vendorId,omitempty" bson:"vendorId,omitempty"` + WorkgroupID string `json:"-" bson:"workgroupId,omitempty"` + VendorID string `json:"-" bson:"vendorId,omitempty"` EventType string `json:"eventType" bson:"eventType"` Mode RunMode `json:"mode,omitempty" bson:"mode,omitempty"` Temporary bool `json:"temporary,omitempty" bson:"temporary,omitempty"` @@ -243,8 +243,9 @@ type AnnotationWorklogEventRecord struct { type AnnotationWorkloadAggregate struct { UserID string `json:"userId"` - WorkgroupID string `json:"workgroupId"` - VendorID string `json:"vendorId"` + SupplierID string `json:"supplierId"` + WorkgroupID string `json:"-"` + VendorID string `json:"-"` TemplateVersion int `json:"templateVersion"` AssignedCount int64 `json:"assignedCount"` SubmittedCount int64 `json:"submittedCount"` diff --git a/internal/domain/delivery_package.go b/internal/domain/delivery_package.go index 93064cb0..52f8ca64 100644 --- a/internal/domain/delivery_package.go +++ b/internal/domain/delivery_package.go @@ -30,8 +30,8 @@ type DeliveryPackageRecord struct { WorkflowID string `json:"workflowId" bson:"workflowId"` SupplierID string `json:"supplierId,omitempty" bson:"supplierId,omitempty"` LegacyAssignment map[string]string `json:"legacyAssignment,omitempty" bson:"legacyAssignment,omitempty"` - WorkgroupID string `json:"workgroupId,omitempty" bson:"workgroupId,omitempty"` - VendorID string `json:"vendorId,omitempty" bson:"vendorId,omitempty"` + WorkgroupID string `json:"-" bson:"workgroupId,omitempty"` + VendorID string `json:"-" bson:"vendorId,omitempty"` ParentRunID string `json:"parentRunId" bson:"parentRunId"` ChildRunID string `json:"childRunId" bson:"childRunId"` ReleasePartitionID string `json:"releasePartitionId" bson:"releasePartitionId"` diff --git a/internal/domain/human_loop.go b/internal/domain/human_loop.go index 9b5812cb..1a89cffc 100644 --- a/internal/domain/human_loop.go +++ b/internal/domain/human_loop.go @@ -146,8 +146,8 @@ type HumanItemRecord struct { Status HumanItemStatus `json:"status" bson:"status"` AssignedUserID string `json:"assignedUserId,omitempty" bson:"assignedUserId,omitempty"` LegacyAssignment map[string]string `json:"legacyAssignment,omitempty" bson:"legacyAssignment,omitempty"` - WorkgroupID string `json:"workgroupId,omitempty" bson:"workgroupId,omitempty"` - VendorID string `json:"vendorId,omitempty" bson:"vendorId,omitempty"` + WorkgroupID string `json:"-" bson:"workgroupId,omitempty"` + VendorID string `json:"-" bson:"vendorId,omitempty"` CurrentRevision int `json:"currentRevision" bson:"currentRevision"` ReviewRound int `json:"reviewRound" bson:"reviewRound"` MaxReworkRounds int `json:"maxReworkRounds" bson:"maxReworkRounds"` diff --git a/internal/domain/workflow_release.go b/internal/domain/workflow_release.go index 46134bbc..23bcc2f2 100644 --- a/internal/domain/workflow_release.go +++ b/internal/domain/workflow_release.go @@ -46,6 +46,7 @@ type ProjectWorkflowInstanceRecord struct { ReleaseVersion int `json:"releaseVersion" bson:"releaseVersion"` SourceTemplateID string `json:"sourceTemplateId" bson:"sourceTemplateId"` WorkflowID string `json:"workflowId" bson:"workflowId"` + SupplierID string `json:"supplierId,omitempty" bson:"supplierId,omitempty"` Status ProjectWorkflowInstanceStatus `json:"status" bson:"status"` SupersedesInstanceID string `json:"supersedesInstanceId,omitempty" bson:"supersedesInstanceId,omitempty"` CreatedByUserID string `json:"createdByUserId" bson:"createdByUserId"` diff --git a/internal/worker/annotation_gate.go b/internal/worker/annotation_gate.go index f5c7964a..95a01118 100644 --- a/internal/worker/annotation_gate.go +++ b/internal/worker/annotation_gate.go @@ -97,38 +97,91 @@ func annotationGateConfigFromNode(node domain.WorkflowNode) annotationGateConfig func (runtime *Runtime) annotationGateSupplierID(ctx context.Context, run *domain.RunRecord, config *annotationGateConfig) (string, error) { if config == nil || run == nil { - return "", nil - } - if config.SupplierID != "" { - return config.SupplierID, nil + return "", fmt.Errorf("annotation supplier resolution requires a run and configuration") } + requested := strings.TrimSpace(config.SupplierID) if config.WorkgroupID != "" { group, err := runtime.store.GetAnnotationWorkgroup(ctx, config.WorkgroupID) if err != nil { return "", err } - if group != nil { - config.SupplierID = strings.TrimSpace(group.VendorID) - return config.SupplierID, nil + if group != nil && requested == "" { + requested = strings.TrimSpace(group.VendorID) } } members, err := runtime.store.ListProjectMembers(ctx, run.OrganizationID, run.ProjectID) if err != nil { return "", err } - suppliers := map[string]struct{}{} + requiredRoles := map[domain.AnnotationAssignmentRole]struct{}{} + stages := config.HumanStages + if len(stages) == 0 { + stages = domain.DefaultAnnotationHumanStages() + } + for _, stage := range stages { + role := domain.AnnotationAssignmentRole(strings.TrimSpace(stage.RequiredRole)) + if role != "" { + requiredRoles[role] = struct{}{} + } + } + if config.RequirePrePackageAcceptance { + requiredRoles[domain.AnnotationRoleAcceptanceReviewer] = struct{}{} + } + supplierCovered := func(supplierID string) bool { + covered := map[domain.AnnotationAssignmentRole]struct{}{} + for _, member := range members { + if strings.TrimSpace(member.SupplierID) != supplierID { + continue + } + for _, role := range member.Roles { + covered[role] = struct{}{} + } + } + for role := range requiredRoles { + if _, ok := covered[role]; !ok { + return false + } + } + return len(requiredRoles) > 0 + } + validateSupplier := func(supplierID string) error { + supplier, supplierErr := runtime.store.GetSupplier(ctx, supplierID) + if supplierErr != nil { + return supplierErr + } + if supplier == nil || strings.TrimSpace(supplier.OrganizationID) != strings.TrimSpace(run.OrganizationID) || !strings.EqualFold(strings.TrimSpace(supplier.Status), "active") { + return fmt.Errorf("supplier %s must be active and belong to the run organization", supplierID) + } + if !supplierCovered(supplierID) { + return fmt.Errorf("supplier %s project members do not cover every configured human stage role", supplierID) + } + return nil + } + if requested != "" { + if err := validateSupplier(requested); err != nil { + return "", err + } + config.SupplierID = requested + return requested, nil + } + candidates := map[string]struct{}{} for _, member := range members { - if strings.TrimSpace(member.SupplierID) != "" { - suppliers[member.SupplierID] = struct{}{} + supplierID := strings.TrimSpace(member.SupplierID) + if supplierID == "" || !supplierCovered(supplierID) { + continue + } + if err := validateSupplier(supplierID); err == nil { + candidates[supplierID] = struct{}{} } } - if len(suppliers) == 1 { - for supplierID := range suppliers { - config.SupplierID = supplierID - return supplierID, nil - } + if len(candidates) != 1 { + return "", fmt.Errorf("annotation workflow requires exactly one active supplier whose project members cover every configured human stage role; found %d", len(candidates)) } - return "", nil + for supplierID := range candidates { + config.SupplierID = supplierID + return supplierID, nil + } + return "", fmt.Errorf("annotation supplier could not be resolved") } func annotationNumber(value any) (float64, bool) { diff --git a/internal/worker/annotation_supplier_test.go b/internal/worker/annotation_supplier_test.go new file mode 100644 index 00000000..ca821d43 --- /dev/null +++ b/internal/worker/annotation_supplier_test.go @@ -0,0 +1,70 @@ +package worker + +import ( + "context" + "strings" + "testing" + + "droboticflow/internal/domain" + "droboticflow/internal/store" +) + +func seedAnnotationSupplier(t *testing.T, dataStore store.Store, projectID, supplierID string, roles ...domain.AnnotationAssignmentRole) { + t.Helper() + ctx := context.Background() + if err := dataStore.UpsertSupplier(ctx, domain.SupplierRecord{ID: supplierID, OrganizationID: domain.DefaultOrganizationID, Name: supplierID, Status: "active"}); err != nil { + t.Fatal(err) + } + for index, role := range roles { + if err := dataStore.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ + ID: supplierID + "-member-" + string(rune('a'+index)), OrganizationID: domain.DefaultOrganizationID, + ProjectID: projectID, UserID: supplierID + "-user-" + string(rune('a'+index)), SupplierID: supplierID, + Roles: []domain.AnnotationAssignmentRole{role}, + }); err != nil { + t.Fatal(err) + } + } +} + +func TestAnnotationGateSupplierResolutionIsFailClosed(t *testing.T) { + ctx := context.Background() + projectID := "project-supplier-resolution" + run := &domain.RunRecord{ID: "run-supplier-resolution", OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID} + config := annotationGateConfig{HumanStages: domain.DefaultFanyuV58HumanStages(), RequirePrePackageAcceptance: true} + + t.Run("unique fully staffed supplier is inferred", func(t *testing.T) { + dataStore := store.NewMemoryStore() + seedAnnotationSupplier(t, dataStore, projectID, "supplier-complete", domain.AnnotationRoleAnnotator, domain.AnnotationRoleQualityReviewer, domain.AnnotationRoleAcceptanceReviewer) + runtime := &Runtime{store: dataStore} + resolved, err := runtime.annotationGateSupplierID(ctx, run, &config) + if err != nil || resolved != "supplier-complete" { + t.Fatalf("resolved=%q err=%v", resolved, err) + } + }) + + t.Run("configured supplier missing a role is rejected", func(t *testing.T) { + dataStore := store.NewMemoryStore() + seedAnnotationSupplier(t, dataStore, projectID, "supplier-incomplete", domain.AnnotationRoleAnnotator, domain.AnnotationRoleQualityReviewer) + runtime := &Runtime{store: dataStore} + candidate := config + candidate.SupplierID = "supplier-incomplete" + _, err := runtime.annotationGateSupplierID(ctx, run, &candidate) + if err == nil || !strings.Contains(err.Error(), "every configured human stage role") { + t.Fatalf("expected missing-role error, got %v", err) + } + }) + + t.Run("multiple fully staffed suppliers require an explicit binding", func(t *testing.T) { + dataStore := store.NewMemoryStore() + for _, supplierID := range []string{"supplier-a", "supplier-b"} { + seedAnnotationSupplier(t, dataStore, projectID, supplierID, domain.AnnotationRoleAnnotator, domain.AnnotationRoleQualityReviewer, domain.AnnotationRoleAcceptanceReviewer) + } + runtime := &Runtime{store: dataStore} + candidate := config + candidate.SupplierID = "" + _, err := runtime.annotationGateSupplierID(ctx, run, &candidate) + if err == nil || !strings.Contains(err.Error(), "found 2") { + t.Fatalf("expected ambiguous supplier error, got %v", err) + } + }) +} diff --git a/internal/worker/human_release.go b/internal/worker/human_release.go index b0af4bd8..c517c8e5 100644 --- a/internal/worker/human_release.go +++ b/internal/worker/human_release.go @@ -222,14 +222,16 @@ func (runtime *Runtime) syncDeliveryPackage(ctx context.Context, batch domain.Hu item = packages[0] } else { workgroupID, vendorID, supplierID := "", "", "" + var legacyAssignment map[string]string if len(partition.ItemIDs) > 0 { if humanItem, itemErr := runtime.store.GetHumanItem(ctx, partition.ItemIDs[0]); itemErr == nil && humanItem != nil { workgroupID, vendorID, supplierID = humanItem.WorkgroupID, humanItem.VendorID, humanItem.SupplierID + legacyAssignment = humanItem.LegacyAssignment } } item = domain.DeliveryPackageRecord{ ID: "delivery-package-" + partition.ID, OrganizationID: batch.OrganizationID, ProjectID: batch.ProjectID, - WorkflowID: batch.WorkflowID, SupplierID: supplierID, WorkgroupID: workgroupID, VendorID: vendorID, ParentRunID: batch.RunID, ChildRunID: partition.ChildRunID, + WorkflowID: batch.WorkflowID, SupplierID: supplierID, LegacyAssignment: legacyAssignment, WorkgroupID: workgroupID, VendorID: vendorID, ParentRunID: batch.RunID, ChildRunID: partition.ChildRunID, ReleasePartitionID: partition.ID, ManifestDigest: partition.ManifestDigest, ItemIDs: partition.ItemIDs, Status: domain.DeliveryPackageBuilding, Mode: mode, Temporary: mode == domain.RunModeDebug, Version: 1, CreatedAt: nowISO(), UpdatedAt: nowISO(), } @@ -386,7 +388,7 @@ func (runtime *Runtime) ensureDeliveryPackageAcceptanceItem(ctx context.Context, ProjectID: item.ProjectID, WorkflowID: item.WorkflowID, RunID: item.ChildRunID, GateNodeID: "delivery-package-acceptance", Subject: domain.HumanSubjectRef{ArtifactID: artifactID, ItemKey: item.ID, MediaType: "application/json", Payload: map[string]any{ "deliveryPackageId": item.ID, "artifactIds": item.ArtifactIDs, "manifestDigest": item.ManifestDigest, - }}, CurrentStageID: "acceptance", Status: status, SupplierID: item.SupplierID, WorkgroupID: item.WorkgroupID, VendorID: item.VendorID, + }}, CurrentStageID: "acceptance", Status: status, SupplierID: item.SupplierID, LegacyAssignment: item.LegacyAssignment, WorkgroupID: item.WorkgroupID, VendorID: item.VendorID, Version: 1, MaxReworkRounds: sourceBatch.MaxReworkRounds, CreatedAt: now, UpdatedAt: now, } if err := runtime.store.UpsertHumanItem(ctx, *humanItem); err != nil { diff --git a/internal/worker/human_release_test.go b/internal/worker/human_release_test.go index 183364f6..22c6e1d1 100644 --- a/internal/worker/human_release_test.go +++ b/internal/worker/human_release_test.go @@ -207,7 +207,7 @@ func TestCompletedDeliveryPackageCreatesUnassignedAcceptanceHumanItem(t *testing } if err := dataStore.UpsertHumanItem(ctx, domain.HumanItemRecord{ ID: "human-a", OrganizationID: "org-a", BatchID: batch.ID, ProjectID: batch.ProjectID, - WorkgroupID: "group-a", VendorID: "vendor-a", Subject: domain.HumanSubjectRef{ItemKey: "clip-a"}, + SupplierID: "supplier-a", LegacyAssignment: map[string]string{"workgroupId": "group-a", "vendorId": "vendor-a"}, Subject: domain.HumanSubjectRef{ItemKey: "clip-a"}, CurrentStageID: "quality_review", Status: domain.HumanItemAccepted, Version: 1, }); err != nil { t.Fatalf("create source item: %v", err) @@ -236,7 +236,7 @@ func TestCompletedDeliveryPackageCreatesUnassignedAcceptanceHumanItem(t *testing if err != nil || acceptance == nil { t.Fatalf("acceptance item=%#v err=%v", acceptance, err) } - if acceptance.CurrentStageID != "acceptance" || acceptance.Status != domain.HumanItemReady || acceptance.AssignedUserID != "" || acceptance.WorkgroupID != "group-a" || acceptance.VendorID != "vendor-a" { + if acceptance.CurrentStageID != "acceptance" || acceptance.Status != domain.HumanItemReady || acceptance.AssignedUserID != "" || acceptance.SupplierID != "supplier-a" || acceptance.LegacyAssignment["workgroupId"] != "group-a" || acceptance.LegacyAssignment["vendorId"] != "vendor-a" { t.Fatalf("acceptance item must enter the scoped available pool: %#v", acceptance) } } diff --git a/internal/worker/runtime_test.go b/internal/worker/runtime_test.go index 6a7c115f..f3581a3c 100644 --- a/internal/worker/runtime_test.go +++ b/internal/worker/runtime_test.go @@ -2071,14 +2071,23 @@ func TestRuntimeAnnotationGateCreatesTasksAndReturnsAcceptedRevisionArtifact(t * // Organization-level published templates are reusable by tenant projects. ID: "template-1", OrganizationID: project.OrganizationID, ProjectID: "", Name: "动作模板", Version: 1, Status: domain.AnnotationTemplatePublished, - Labels: []domain.SemanticLabelDefinition{{ID: "steady", Name: "平稳活动"}, {ID: "fast", Name: "快速移动"}}, + Labels: []domain.SemanticLabelDefinition{{ID: "steady", Name: "平稳活动"}, {ID: "fast", Name: "快速移动"}}, + HumanStages: domain.DefaultFanyuV58HumanStages(), } - group := domain.AnnotationWorkgroupRecord{ID: "group-1", OrganizationID: project.OrganizationID, Name: "供应商 A", VendorID: "vendor-a", Status: "active"} + supplier := domain.SupplierRecord{ID: "supplier-a", OrganizationID: project.OrganizationID, Name: "供应商 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) + if err := dataStore.UpsertSupplier(ctx, supplier); err != nil { + t.Fatalf("upsert supplier: %v", err) + } + for index, role := range []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator, domain.AnnotationRoleQualityReviewer} { + if err := dataStore.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ + ID: fmt.Sprintf("member-%d", index), OrganizationID: project.OrganizationID, ProjectID: project.ID, + UserID: fmt.Sprintf("user-%d", index), SupplierID: supplier.ID, Roles: []domain.AnnotationAssignmentRole{role}, + }); err != nil { + t.Fatalf("upsert project member: %v", err) + } } workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{ OrganizationID: project.OrganizationID, @@ -2094,7 +2103,7 @@ func TestRuntimeAnnotationGateCreatesTasksAndReturnsAcceptedRevisionArtifact(t * Hyperparams: []domain.HyperParam{ {Key: "REVIEW_MODE", Value: "annotation"}, {Key: "ANNOTATION_TEMPLATE_ID", Value: template.ID}, - {Key: "ANNOTATION_WORKGROUP_ID", Value: group.ID}, + {Key: "ANNOTATION_SUPPLIER_ID", Value: supplier.ID}, }, }}, }, @@ -2171,7 +2180,7 @@ func TestRuntimeAnnotationGateCreatesTasksAndReturnsAcceptedRevisionArtifact(t * t.Fatalf("human batches = %#v err=%v", batches, err) } humanItems, err := dataStore.ListHumanItems(ctx, project.OrganizationID, domain.HumanItemQuery{BatchID: batches[0].ID}) - if err != nil || len(humanItems) != 1 || humanItems[0].LegacyTaskID != tasks[0].ID || humanItems[0].CurrentStageID != "split_annotation" { + if err != nil || len(humanItems) != 1 || humanItems[0].LegacyTaskID != tasks[0].ID || humanItems[0].CurrentStageID != domain.FanyuV58StageAnnotationCombined { t.Fatalf("human items = %#v err=%v", humanItems, err) } revisions, err := dataStore.ListAnnotationRevisions(ctx, project.OrganizationID, tasks[0].ID) diff --git a/k8s/droboticflow.template.yaml b/k8s/droboticflow.template.yaml index 466d2658..925d6a0e 100644 --- a/k8s/droboticflow.template.yaml +++ b/k8s/droboticflow.template.yaml @@ -173,9 +173,6 @@ data: FANYU_HEADPOSE_SERVICE_IMAGE_ID: "__HEADPOSE_SERVICE_IMAGE_ID__" FANYU_HEADPOSE_MODEL_SHA256: "__HEADPOSE_MODEL_SHA256__" FANYU_HEADPOSE_LICENSE_ID: "MIT" - # Empty keeps the v5 template draft/fail-closed. Set the real published - # annotation workgroup ID in the POC deployment environment before release. - FANYU_V5_SUPPLIER_ID: "__FANYU_V5_SUPPLIER_ID__" --- apiVersion: v1 kind: Service @@ -371,11 +368,6 @@ spec: configMapKeyRef: name: droboticflow-config key: FANYU_HEADPOSE_LICENSE_ID - - name: FANYU_V5_SUPPLIER_ID - valueFrom: - configMapKeyRef: - name: droboticflow-config - key: FANYU_V5_SUPPLIER_ID - name: AI_API_KEY valueFrom: secretKeyRef: diff --git a/scripts/deploy-envs/poc.env b/scripts/deploy-envs/poc.env index d2e959ae..0835de16 100644 --- a/scripts/deploy-envs/poc.env +++ b/scripts/deploy-envs/poc.env @@ -57,4 +57,3 @@ export AI_MODEL="${AI_MODEL:-}" export HEADPOSE_MODEL_SHA256="${HEADPOSE_MODEL_SHA256:-sha256:9bdcd0e072df0d21e92288b40ea1d1b3cf7173c44728ab9c45c4d673f361e018}" # Required only to publish/run the Fanyu v5 release. Keep blank until the # live POC annotation workgroup has been created and verified. -export FANYU_V5_SUPPLIER_ID="${FANYU_V5_SUPPLIER_ID:-}" diff --git a/scripts/deploy-k8s.sh b/scripts/deploy-k8s.sh index 51ed15d6..ec91a1bd 100755 --- a/scripts/deploy-k8s.sh +++ b/scripts/deploy-k8s.sh @@ -56,7 +56,6 @@ fi HEADPOSE_SERVICE_IMAGE="${HEADPOSE_SERVICE_IMAGE:-droboticflow-headpose-service:latest}" HEADPOSE_SERVICE_IMAGE_ID="${HEADPOSE_SERVICE_IMAGE_ID:-}" HEADPOSE_MODEL_SHA256="${HEADPOSE_MODEL_SHA256:-sha256:9bdcd0e072df0d21e92288b40ea1d1b3cf7173c44728ab9c45c4d673f361e018}" -FANYU_V5_SUPPLIER_ID="${FANYU_V5_SUPPLIER_ID:-}" DROBOTICFLOW_BUILD_COMMIT="${DROBOTICFLOW_BUILD_COMMIT:-$(git -C "$ROOT_DIR" rev-parse HEAD)}" DROBOTICFLOW_BUILD_TIME="${DROBOTICFLOW_BUILD_TIME:-}" OPERATOR_RUNNER_BUILD_COMMIT="${OPERATOR_RUNNER_BUILD_COMMIT:-$DROBOTICFLOW_BUILD_COMMIT}" @@ -226,7 +225,6 @@ env \ HEADPOSE_SERVICE_IMAGE="$HEADPOSE_SERVICE_IMAGE" \ HEADPOSE_SERVICE_IMAGE_ID="$HEADPOSE_SERVICE_IMAGE_ID" \ HEADPOSE_MODEL_SHA256="$HEADPOSE_MODEL_SHA256" \ - FANYU_V5_SUPPLIER_ID="$FANYU_V5_SUPPLIER_ID" \ OPERATOR_RUNNER_BUILD_COMMIT="$OPERATOR_RUNNER_BUILD_COMMIT" \ OPERATOR_RUNNER_BUILD_TIME="$OPERATOR_RUNNER_BUILD_TIME" \ DROBOTICFLOW_BUILD_COMMIT="$DROBOTICFLOW_BUILD_COMMIT" \ diff --git a/scripts/fanyu-100-browser-acceptance.mjs b/scripts/fanyu-100-browser-acceptance.mjs index cc8d86d6..e434602d 100755 --- a/scripts/fanyu-100-browser-acceptance.mjs +++ b/scripts/fanyu-100-browser-acceptance.mjs @@ -6,20 +6,19 @@ const apiURL = String(process.env.FANYU_API_URL || "http://120.48.180.80").repla const statePath = process.env.FANYU_ACCEPTANCE_STATE_PATH; const outputDir = process.env.FANYU_ACCEPTANCE_BROWSER_OUTPUT || "/tmp/fanyu-100-browser-acceptance"; if (!statePath) throw new Error("FANYU_ACCEPTANCE_STATE_PATH is required"); + const state = JSON.parse(fs.readFileSync(statePath, "utf8")); -const projectId = state.projectId; -const workflowId = state.workflowId; -if (!projectId || !workflowId) throw new Error("acceptance state must include projectId and workflowId"); - +const projectId = String(state.projectId || "").trim(); +const workflowId = String(state.workflowId || "").trim(); +const browserFixture = state.browserFixture; +const reportPath = path.join(outputDir, "browser-acceptance.json"); fs.mkdirSync(outputDir, { recursive: true }); -const browser = await chromium.launch({ - headless: true, - executablePath: process.env.CHROME_EXECUTABLE_PATH || "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", -}); - const checks = []; const screenshots = []; +const diagnostics = []; +let browser; +let fatalError = ""; function requireEnv(name) { const value = String(process.env[name] || "").trim(); @@ -27,101 +26,575 @@ function requireEnv(name) { return value; } -async function authenticatedContext(emailName, passwordName) { - const context = await browser.newContext({ baseURL: apiURL, viewport: { width: 1920, height: 1080 } }); - const response = await context.request.post("/api/auth/login", { - data: { email: requireEnv(emailName), password: requireEnv(passwordName) }, - }); - if (!response.ok()) throw new Error(`${emailName} login failed: ${response.status()}`); - return context; +function required(value, label) { + const normalized = String(value || "").trim(); + if (!normalized) throw new Error(`state.browserFixture.${label} is required`); + return normalized; } -async function screenshot(page, name) { - const file = path.join(outputDir, `${name}.png`); - await page.screenshot({ path: file, fullPage: false }); - screenshots.push(file); +function record(name, passed, evidence = {}) { + const item = { name, passed: Boolean(passed), evidence }; + checks.push(item); + if (!item.passed) throw new Error(`${name} failed: ${JSON.stringify(evidence)}`); + return item; } -async function checkPage(page, name, expectedText) { - const issues = []; - page.on("console", (message) => { - if (["error", "warning"].includes(message.type())) issues.push(`${message.type()}: ${message.text()}`); - }); - page.on("pageerror", (error) => issues.push(`pageerror: ${error.message}`)); - await page.getByText(expectedText, { exact: true }).waitFor({ state: "visible", timeout: 30_000 }); - const overlayCount = await page - .locator("text=/Internal Server Error|Failed to compile|Vite Error|Webpack Error/i") - .count(); - checks.push({ - name, - passed: overlayCount === 0 && issues.length === 0, - url: page.url(), - overlayCount, - consoleIssues: issues, - }); -} - -const adminContext = await authenticatedContext("DROBOTICFLOW_ACCEPTANCE_EMAIL", "DROBOTICFLOW_ACCEPTANCE_PASSWORD"); -const adminPage = await adminContext.newPage(); -await adminPage.goto(`/project/${projectId}`, { waitUntil: "domcontentloaded", timeout: 60_000 }); -await adminPage.getByText("成员与角色", { exact: true }).waitFor({ state: "visible", timeout: 30_000 }); -const membersTab = adminPage.getByText("成员与角色", { exact: true }); -if ((await membersTab.count()) !== 1) throw new Error("members tab is not unique"); -await membersTab.click(); -await adminPage.getByPlaceholder("搜索姓名、账号、供应商或角色").waitFor({ state: "visible", timeout: 15_000 }); -await adminPage.getByPlaceholder("搜索姓名、账号、供应商或角色").fill(String(state.acceptanceId || "acceptance")); -const roleFilter = adminPage.getByLabel("按项目角色筛选"); -await roleFilter.selectOption("annotator"); -checks.push({ - name: "project_member_search_and_role_filter", - passed: (await roleFilter.inputValue()) === "annotator", -}); -await screenshot(adminPage, "admin-project-members"); - -await adminPage.goto( - `/plugins/annotation-qa/workflows/${workflowId}/annotate?projectId=${projectId}&workflowId=${workflowId}`, - { waitUntil: "domcontentloaded", timeout: 60_000 }, -); -await checkPage(adminPage, "admin_annotation_workbench", "任务池"); -for (const label of ["1. 项目", "2. 工作流", "3. Capture 子目录", "可领取", "我的任务", "全部任务"]) { - if ((await adminPage.getByText(label, { exact: true }).count()) === 0) { - checks.push({ name: `annotation_control_${label}`, passed: false }); - } else { - checks.push({ name: `annotation_control_${label}`, passed: true }); +async function runCheck(name, action) { + try { + const evidence = (await action()) || {}; + checks.push({ name, passed: true, evidence }); + return true; + } catch (error) { + checks.push({ name, passed: false, evidence: { error: error instanceof Error ? error.message : String(error) } }); + return false; } } -await screenshot(adminPage, "admin-annotation-workbench"); -const rolePages = [ - ["annotator", "FANYU_ANNOTATOR_EMAIL", "FANYU_ANNOTATOR_PASSWORD", "annotate", "切分与语义标注"], - ["quality", "FANYU_QUALITY_EMAIL", "FANYU_QUALITY_PASSWORD", "quality", "切分与标注审核"], - ["acceptance", "FANYU_ACCEPTANCE_EMAIL", "FANYU_ACCEPTANCE_PASSWORD", "acceptance", "最终验收"], -]; -for (const [name, emailName, passwordName, route, expectedText] of rolePages) { - const context = await authenticatedContext(emailName, passwordName); - const page = await context.newPage(); - await page.goto( - `/plugins/annotation-qa/workflows/${workflowId}/${route}?projectId=${projectId}&workflowId=${workflowId}`, - { waitUntil: "domcontentloaded", timeout: 60_000 }, - ); - await checkPage(page, `${name}_role_workbench`, expectedText); - await screenshot(page, `${name}-workbench`); - await context.close(); +function validateFixtureContract() { + const issues = []; + if (!projectId) issues.push("projectId"); + if (!workflowId) issues.push("workflowId"); + if (!browserFixture || typeof browserFixture !== "object") { + issues.push("browserFixture"); + } else { + for (const stage of ["annotation", "quality", "acceptance"]) { + if (!browserFixture[stage] || typeof browserFixture[stage] !== "object") issues.push(`browserFixture.${stage}`); + } + if (!browserFixture.member || typeof browserFixture.member !== "object") issues.push("browserFixture.member"); + const annotation = browserFixture.annotation || {}; + const quality = browserFixture.quality || {}; + const acceptance = browserFixture.acceptance || {}; + const member = browserFixture.member || {}; + if (!annotation.captureDirectory) issues.push("browserFixture.annotation.captureDirectory"); + if (!annotation.taskId) issues.push("browserFixture.annotation.taskId"); + if (!quality.captureDirectory) issues.push("browserFixture.quality.captureDirectory"); + if (!quality.taskId) issues.push("browserFixture.quality.taskId"); + if (!acceptance.captureDirectory) issues.push("browserFixture.acceptance.captureDirectory"); + if (!acceptance.humanItemId) issues.push("browserFixture.acceptance.humanItemId"); + if (!member.userId) issues.push("browserFixture.member.userId"); + if (!member.email) issues.push("browserFixture.member.email"); + if (member.disposable !== true) issues.push("browserFixture.member.disposable=true"); + } + if (issues.length) { + throw new Error( + `browser fixture is incomplete (${issues.join(", ")}); refusing to replace real interaction acceptance with a title-only smoke test`, + ); + } } -await adminContext.close(); -await browser.close(); +function watchPage(page, name) { + const state = { name, consoleIssues: [], pageErrors: [], requestFailures: [], serverErrors: [] }; + page.on("console", (message) => { + if (["error", "warning"].includes(message.type())) { + state.consoleIssues.push(`${message.type()}: ${message.text()}`); + } + }); + page.on("pageerror", (error) => state.pageErrors.push(error.message)); + page.on("requestfailed", (request) => { + const failure = request.failure()?.errorText || "failed"; + if (failure === "net::ERR_ABORTED") return; + if (new URL(request.url()).origin !== apiURL) return; + state.requestFailures.push(`${request.method()} ${request.url()} ${failure}`); + }); + page.on("response", (response) => { + if (response.status() >= 500 && new URL(response.url()).origin === apiURL) { + state.serverErrors.push(`${response.status()} ${response.url()}`); + } + }); + diagnostics.push(state); + return state; +} -const report = { - schemaVersion: "droboticflow.fanyu.browser-acceptance/v1", - projectId, - workflowId, - passed: checks.every((item) => item.passed), - checks, - screenshots, - createdAt: new Date().toISOString(), -}; -const reportPath = path.join(outputDir, "browser-acceptance.json"); -fs.writeFileSync(reportPath, JSON.stringify(report, null, 2)); -console.log(JSON.stringify({ passed: report.passed, reportPath, screenshots }, null, 2)); -if (!report.passed) process.exitCode = 1; +function assertDiagnosticsHealthy(state, checkpoint) { + const issues = [ + ...state.consoleIssues, + ...state.pageErrors.map((value) => `pageerror: ${value}`), + ...state.requestFailures.map((value) => `requestfailed: ${value}`), + ...state.serverErrors.map((value) => `server: ${value}`), + ]; + if (issues.length) throw new Error(`${checkpoint} emitted browser/network errors: ${issues.join(" | ")}`); + return { console: 0, pageErrors: 0, requestFailures: 0, serverErrors: 0 }; +} + +async function authenticatedContext(emailName, passwordName, allowedRoles, viewport = { width: 1920, height: 1080 }) { + const email = requireEnv(emailName).toLowerCase(); + const context = await browser.newContext({ baseURL: apiURL, viewport }); + const response = await context.request.post("/api/auth/login", { + data: { email, password: requireEnv(passwordName) }, + }); + if (!response.ok()) throw new Error(`${emailName} login failed: ${response.status()}`); + const meResponse = await context.request.get("/api/auth/me"); + if (!meResponse.ok()) throw new Error(`${emailName} identity lookup failed: ${meResponse.status()}`); + const session = await meResponse.json(); + const actualEmail = String(session?.user?.email || "").toLowerCase(); + const actualRole = String(session?.user?.role || ""); + record(`identity_${emailName.toLowerCase()}`, actualEmail === email && allowedRoles.includes(actualRole), { + expectedEmail: email, + actualEmail, + allowedRoles, + actualRole, + userId: session?.user?.id, + }); + return { context, session }; +} + +async function capture(page, name, fullPage = false) { + const file = path.join(outputDir, `${name}.png`); + await page.screenshot({ path: file, fullPage }); + screenshots.push(file); + return file; +} + +async function unique(locator, label) { + await locator.waitFor({ state: "visible", timeout: 30_000 }); + const count = await locator.count(); + if (count !== 1) throw new Error(`${label} expected exactly one element, found ${count}`); + return locator; +} + +function annotationURL(route, fixture) { + const params = new URLSearchParams({ + projectId, + workflowId, + capture: required(fixture.captureDirectory, `${route}.captureDirectory`), + }); + if (fixture.taskId) params.set("taskId", fixture.taskId); + if (fixture.runId) params.set("runId", fixture.runId); + return `/plugins/annotation-qa/workflows/${workflowId}/${route}?${params}`; +} + +async function selectPool(page, pool) { + const button = page.getByTestId(`human-task-pool-${pool}`); + if ((await button.count()) !== 1) return; + if ((await button.getAttribute("aria-pressed")) === "true") return; + const responsePromise = page.waitForResponse((response) => { + const url = new URL(response.url()); + return url.pathname === "/api/human-items" && url.searchParams.get("pool") === pool; + }); + await button.click(); + const response = await responsePromise; + if (!response.ok()) throw new Error(`task pool ${pool} failed to load: ${response.status()}`); +} + +async function locateFixturePool(page, fixture, { acceptance = false } = {}) { + const selector = acceptance + ? `[data-testid="acceptance-human-item"][data-human-item-id="${required(fixture.humanItemId, "acceptance.humanItemId")}"]` + : `[data-testid="annotation-task-card"][data-task-id="${required(fixture.taskId, "taskId")}"]`; + const candidates = [...new Set([fixture.pool, "mine", "available"].filter(Boolean))]; + for (const pool of candidates) { + await selectPool(page, pool); + const target = page.locator(selector); + try { + await target.waitFor({ state: "visible", timeout: 10_000 }); + return pool; + } catch { + // A resumable fixture can be unclaimed on the first run and already + // claimed by the same role account on a later run. + } + } + throw new Error(`fixture is not visible in task pools ${candidates.join(", ")}`); +} + +async function assertHierarchy(page, fixture, { taskRequired = true } = {}) { + const projectSelect = await unique(page.getByLabel("1. 项目", { exact: true }), "project selector"); + const workflowSelect = await unique(page.getByLabel("2. 工作流", { exact: true }), "workflow selector"); + const captureSelect = await unique(page.getByLabel("3. Capture 子目录", { exact: true }), "Capture selector"); + const captureDirectory = required(fixture.captureDirectory, "captureDirectory"); + if ((await projectSelect.inputValue()) !== projectId) + throw new Error("project selector does not hold the fixture project"); + if ((await workflowSelect.inputValue()) !== workflowId) + throw new Error("workflow selector does not hold the fixture workflow"); + if ((await captureSelect.inputValue()) !== captureDirectory) + throw new Error("Capture selector does not hold the fixture directory"); + const url = new URL(page.url()); + if (url.searchParams.get("projectId") !== projectId || url.searchParams.get("workflowId") !== workflowId) { + throw new Error(`project/workflow URL scope is wrong: ${url}`); + } + if (url.searchParams.get("capture") !== captureDirectory) throw new Error(`Capture URL scope is wrong: ${url}`); + + let taskEvidence = {}; + if (taskRequired) { + const taskId = required(fixture.taskId, "taskId"); + const card = await unique( + page.locator(`[data-testid="annotation-task-card"][data-task-id="${taskId}"]`), + "fixture task card", + ); + const displayedCapture = await card.getAttribute("data-capture-directory"); + const firstLine = + String(await card.innerText()) + .split("\n") + .map((value) => value.trim()) + .filter(Boolean)[0] || ""; + if (displayedCapture !== captureDirectory || firstLine !== captureDirectory) { + throw new Error(`task card does not lead with Capture (${displayedCapture}; ${firstLine})`); + } + taskEvidence = { taskId, displayedCapture, firstLine }; + } + return { projectId, workflowId, captureDirectory, url: page.url(), ...taskEvidence }; +} + +async function exerciseHierarchyCascade(page, fixture) { + const captureDirectory = required(fixture.captureDirectory, "annotation.captureDirectory"); + const taskId = required(fixture.taskId, "annotation.taskId"); + await page.goto(`/plugins/annotation-qa/workflows/all/annotate?projectId=${encodeURIComponent(projectId)}`, { + waitUntil: "domcontentloaded", + timeout: 60_000, + }); + const projectSelect = await unique(page.getByLabel("1. 项目", { exact: true }), "cascade project selector"); + if ((await projectSelect.inputValue()) !== projectId) await projectSelect.selectOption(projectId); + + const workflowSelect = await unique(page.getByLabel("2. 工作流", { exact: true }), "cascade workflow selector"); + await workflowSelect.selectOption(workflowId, { timeout: 30_000 }); + await page.waitForURL((url) => url.searchParams.get("workflowId") === workflowId, { timeout: 30_000 }); + + const captureSelect = await unique(page.getByLabel("3. Capture 子目录", { exact: true }), "cascade Capture selector"); + await captureSelect.selectOption(captureDirectory, { timeout: 30_000 }); + await page.waitForURL((url) => url.searchParams.get("capture") === captureDirectory, { timeout: 30_000 }); + + await selectPool(page, "all"); + const card = await unique( + page.locator(`[data-testid="annotation-task-card"][data-task-id="${taskId}"]`), + "cascade fixture task card", + ); + await card.click(); + await page.waitForURL((url) => url.searchParams.get("taskId") === taskId, { timeout: 30_000 }); + return assertHierarchy(page, fixture); +} + +function rectanglesOverlap(left, right) { + return ( + Math.max(0, Math.min(left.right, right.right) - Math.max(left.left, right.left)) > 1 && + Math.max(0, Math.min(left.bottom, right.bottom) - Math.max(left.top, right.top)) > 1 + ); +} + +async function assertWorkbenchLayout(page, viewport) { + await page.setViewportSize(viewport); + const editor = await unique(page.getByTestId("annotation-workbench-editor"), "annotation editor"); + await editor.scrollIntoViewIfNeeded(); + const toolbarLocator = await unique(page.getByTestId("sticky-annotation-toolbar"), "annotation toolbar"); + await toolbarLocator.scrollIntoViewIfNeeded(); + const layout = await page.evaluate(() => { + const rect = (selector) => { + const element = document.querySelector(selector); + if (!element) return null; + const value = element.getBoundingClientRect(); + return { + left: value.left, + right: value.right, + top: value.top, + bottom: value.bottom, + width: value.width, + height: value.height, + }; + }; + const toolbarElement = document.querySelector('[data-testid="sticky-annotation-toolbar"]'); + const toolbarRect = toolbarElement?.getBoundingClientRect(); + const toolbarHit = toolbarRect + ? document.elementFromPoint(toolbarRect.left + toolbarRect.width / 2, toolbarRect.top + toolbarRect.height / 2) + : null; + return { + viewport: { width: window.innerWidth, height: window.innerHeight }, + editor: rect('[data-testid="annotation-workbench-editor"]'), + video: rect('[data-testid="annotation-workbench-editor"] video'), + timeline: rect('[data-testid="compact-annotation-timeline"]'), + toolbar: rect('[data-testid="sticky-annotation-toolbar"]'), + actions: rect('[data-testid="annotation-sticky-actions"]'), + navigator: rect('[data-testid="slice-event-navigator"]'), + toolbarUncovered: Boolean(toolbarElement && toolbarHit && toolbarElement.contains(toolbarHit)), + }; + }); + for (const key of ["editor", "video", "timeline", "toolbar", "actions", "navigator"]) { + if (!layout[key] || layout[key].width <= 0 || layout[key].height <= 0) + throw new Error(`${key} has no rendered box`); + } + if (rectanglesOverlap(layout.toolbar, layout.actions)) + throw new Error("annotation toolbar is covered by submit actions"); + if (rectanglesOverlap(layout.navigator, layout.actions)) + throw new Error("slice navigator is covered by submit actions"); + if (layout.toolbar.top < 0 || layout.toolbar.bottom > layout.viewport.height || !layout.toolbarUncovered) { + throw new Error("annotation toolbar is outside the viewport or covered by another layer"); + } + const screenshot = await capture(page, `annotator-layout-${viewport.width}x${viewport.height}`); + return { ...layout, screenshot }; +} + +async function openMembers(page) { + await page.goto(`/project/${projectId}`, { waitUntil: "domcontentloaded", timeout: 60_000 }); + const membersTab = await unique(page.getByText("成员与角色", { exact: true }), "members tab"); + await membersTab.click(); + await unique(page.getByTestId("project-members-panel"), "project members panel"); +} + +function memberRow(page, userId) { + return page.locator(`[data-testid="project-member-row"][data-user-id="${userId}"]`); +} + +async function searchMember(page, email, role = "all") { + await page.getByTestId("project-member-search").fill(email); + await page.getByTestId("project-member-role-filter").selectOption(role); +} + +async function ensureDisposableMember(page, fixture) { + const userId = required(fixture.userId, "member.userId"); + const email = required(fixture.email, "member.email"); + await searchMember(page, email); + if ((await memberRow(page, userId).count()) === 1) return; + + await page.getByTestId("project-member-add").click(); + await unique(page.getByTestId("project-member-editor"), "member editor"); + await page.getByTestId("project-member-user").selectOption(userId); + if (fixture.supplierId) await page.getByTestId("project-member-supplier").selectOption(fixture.supplierId); + const initialRoles = fixture.initialRoles || ["annotator"]; + for (const role of ["annotator", "quality_reviewer", "acceptance_reviewer"]) { + await page.getByTestId(`project-member-role-${role}`).setChecked(initialRoles.includes(role)); + } + const responsePromise = page.waitForResponse( + (response) => + response.request().method() === "POST" && response.url().endsWith(`/api/projects/${projectId}/members`), + ); + await page.getByTestId("project-member-save").click(); + const response = await responsePromise; + if (!response.ok()) throw new Error(`member fixture creation failed: ${response.status()}`); + await searchMember(page, email); + await memberRow(page, userId).waitFor({ state: "visible", timeout: 30_000 }); +} + +async function exerciseMemberBulkOperations(page, fixture) { + if (fixture.disposable !== true) throw new Error("member fixture must be explicitly disposable"); + const userId = required(fixture.userId, "member.userId"); + const email = required(fixture.email, "member.email"); + const targetRoles = fixture.bulkRoles || ["annotator", "quality_reviewer"]; + await ensureDisposableMember(page, fixture); + + await searchMember(page, email, "annotator"); + let row = await unique(memberRow(page, userId), "disposable member row after search/filter"); + const selection = row.getByLabel(`选择成员 ${email}`, { exact: true }); + await selection.check(); + for (const role of ["annotator", "quality_reviewer", "acceptance_reviewer"]) { + await page.getByTestId(`project-member-bulk-role-${role}`).setChecked(targetRoles.includes(role)); + } + const setRolesResponse = page.waitForResponse( + (response) => + response.request().method() === "POST" && response.url().endsWith(`/api/projects/${projectId}/members/bulk`), + ); + await page.getByTestId("project-member-bulk-set-roles").click(); + const updated = await setRolesResponse; + if (!updated.ok()) throw new Error(`bulk role update failed: ${updated.status()}`); + await searchMember(page, email, targetRoles[0]); + row = await unique(memberRow(page, userId), "member row after bulk role update"); + const renderedRoles = await row + .locator("[data-role]") + .evaluateAll((nodes) => nodes.map((node) => node.getAttribute("data-role"))); + if (targetRoles.some((role) => !renderedRoles.includes(role)) || renderedRoles.length !== targetRoles.length) { + throw new Error(`bulk roles were not rendered exactly: ${JSON.stringify(renderedRoles)}`); + } + await capture(page, "admin-project-members-bulk-roles"); + + await page.reload({ waitUntil: "domcontentloaded", timeout: 60_000 }); + const membersTab = await unique(page.getByText("成员与角色", { exact: true }), "members tab after reload"); + await membersTab.click(); + await searchMember(page, email, targetRoles[0]); + row = await unique(memberRow(page, userId), "persisted member row"); + const persistedRoles = await row + .locator("[data-role]") + .evaluateAll((nodes) => nodes.map((node) => node.getAttribute("data-role"))); + if (JSON.stringify([...persistedRoles].sort()) !== JSON.stringify([...targetRoles].sort())) { + throw new Error(`bulk role update did not persist: ${JSON.stringify(persistedRoles)}`); + } + + await page.getByTestId("project-member-role-filter").selectOption("all"); + row = await unique(memberRow(page, userId), "member row before bulk removal"); + await row.getByLabel(`选择成员 ${email}`, { exact: true }).check(); + const removeResponse = page.waitForResponse( + (response) => + response.request().method() === "POST" && response.url().endsWith(`/api/projects/${projectId}/members/bulk`), + ); + await page.getByTestId("project-member-bulk-remove").click(); + const removed = await removeResponse; + if (!removed.ok()) throw new Error(`bulk member removal failed: ${removed.status()}`); + await memberRow(page, userId).waitFor({ state: "detached", timeout: 30_000 }); + await capture(page, "admin-project-members-bulk-removed"); + return { userId, email, targetRoles, persistedRoles, removed: true }; +} + +async function inspectRoleWorkbench({ name, emailName, passwordName, allowedRoles, route, expectedText, fixture }) { + const { context, session } = await authenticatedContext(emailName, passwordName, allowedRoles); + const page = await context.newPage(); + const pageDiagnostics = watchPage(page, `${name}-workbench`); + try { + await page.goto(annotationURL(route, fixture), { waitUntil: "domcontentloaded", timeout: 60_000 }); + await unique(page.getByText(expectedText, { exact: true }), `${name} workbench title`); + const pool = await locateFixturePool(page, fixture, { acceptance: name === "acceptance" }); + const hierarchy = await assertHierarchy(page, fixture, { taskRequired: name !== "acceptance" }); + if (name === "annotator") { + const claim = page.getByText("领取标注任务", { exact: true }); + if ((await claim.count()) === 1) { + await claim.click(); + await unique(page.getByText("释放任务", { exact: true }), "claimed annotator task"); + } else { + await unique(page.getByText("释放任务", { exact: true }), "annotator task already claimed by this user"); + } + await unique(page.getByTestId("annotation-workbench-editor"), "annotator workbench editor"); + } else if (name === "quality") { + const claim = page.getByText("领取标注审核", { exact: true }); + if ((await claim.count()) === 1) { + await claim.click(); + await unique(page.getByText("释放任务", { exact: true }), "claimed quality task"); + } else { + await unique(page.getByText("释放任务", { exact: true }), "quality task already claimed by this user"); + } + await unique(page.getByTestId("materialized-slice-workbench"), "quality materialized slice workbench"); + } else { + const humanItemId = required(fixture.humanItemId, "acceptance.humanItemId"); + const item = await unique( + page.locator(`[data-testid="acceptance-human-item"][data-human-item-id="${humanItemId}"]`), + "acceptance fixture item", + ); + const claim = item.getByText("领取最终验收", { exact: true }); + if ((await claim.count()) === 1) { + await claim.click(); + await unique(item.getByText("释放任务", { exact: true }), "claimed final acceptance task"); + } else { + await unique(item.getByText("释放任务", { exact: true }), "acceptance task already claimed by this user"); + } + await unique(item.getByTestId("materialized-slice-workbench"), "acceptance materialized slice workbench"); + } + await capture(page, `${name}-workbench-1920x1080`); + return { + session: { id: session.user.id, email: session.user.email, role: session.user.role }, + pool, + hierarchy, + diagnostics: assertDiagnosticsHealthy(pageDiagnostics, `${name} workbench`), + }; + } finally { + await context.close(); + } +} + +async function run() { + validateFixtureContract(); + record("browser_fixture_contract", true, { + projectId, + workflowId, + annotation: { + captureDirectory: browserFixture.annotation.captureDirectory, + taskId: browserFixture.annotation.taskId, + humanItemId: browserFixture.annotation.humanItemId, + runId: browserFixture.annotation.runId, + }, + quality: { + captureDirectory: browserFixture.quality.captureDirectory, + taskId: browserFixture.quality.taskId, + humanItemId: browserFixture.quality.humanItemId, + runId: browserFixture.quality.runId, + }, + acceptance: { + captureDirectory: browserFixture.acceptance.captureDirectory, + humanItemId: browserFixture.acceptance.humanItemId, + runId: browserFixture.acceptance.runId, + }, + member: { + userId: browserFixture.member.userId, + email: browserFixture.member.email, + supplierId: browserFixture.member.supplierId, + bulkRoles: browserFixture.member.bulkRoles, + disposable: browserFixture.member.disposable, + }, + }); + + browser = await chromium.launch({ + headless: true, + executablePath: + process.env.CHROME_EXECUTABLE_PATH || "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", + }); + + const { context: adminContext } = await authenticatedContext( + "DROBOTICFLOW_ACCEPTANCE_EMAIL", + "DROBOTICFLOW_ACCEPTANCE_PASSWORD", + ["admin", "organization_admin"], + ); + const adminPage = await adminContext.newPage(); + const adminDiagnostics = watchPage(adminPage, "admin"); + try { + await runCheck("project_member_real_bulk_operations", async () => { + await openMembers(adminPage); + return exerciseMemberBulkOperations(adminPage, browserFixture.member); + }); + + await runCheck("admin_project_workflow_capture_cascade", async () => { + const hierarchy = await exerciseHierarchyCascade(adminPage, browserFixture.annotation); + await capture(adminPage, "admin-project-workflow-capture"); + return hierarchy; + }); + + await runCheck("annotation_layout_1920_no_overlap", () => + assertWorkbenchLayout(adminPage, { width: 1920, height: 1080 }), + ); + await runCheck("annotation_layout_1366_no_overlap", () => + assertWorkbenchLayout(adminPage, { width: 1366, height: 768 }), + ); + await runCheck("admin_browser_diagnostics", () => assertDiagnosticsHealthy(adminDiagnostics, "admin interactions")); + } finally { + await adminContext.close(); + } + + const rolePages = [ + { + name: "annotator", + emailName: "FANYU_ANNOTATOR_EMAIL", + passwordName: "FANYU_ANNOTATOR_PASSWORD", + allowedRoles: ["member", "annotator", "data_operator"], + route: "annotate", + expectedText: "切分与语义标注", + fixture: browserFixture.annotation, + }, + { + name: "quality", + emailName: "FANYU_QUALITY_EMAIL", + passwordName: "FANYU_QUALITY_PASSWORD", + allowedRoles: ["member", "quality_reviewer", "qa_reviewer", "auditor"], + route: "quality", + expectedText: "切分与标注审核", + fixture: browserFixture.quality, + }, + { + name: "acceptance", + emailName: "FANYU_ACCEPTANCE_EMAIL", + passwordName: "FANYU_ACCEPTANCE_PASSWORD", + allowedRoles: ["member", "acceptance_reviewer"], + route: "acceptance", + expectedText: "最终验收", + fixture: browserFixture.acceptance, + }, + ]; + for (const rolePage of rolePages) { + await runCheck(`${rolePage.name}_real_fixture_workbench`, () => inspectRoleWorkbench(rolePage)); + } +} + +try { + await run(); +} catch (error) { + fatalError = error instanceof Error ? error.message : String(error); + checks.push({ name: "fatal_browser_acceptance", passed: false, evidence: { error: fatalError } }); +} finally { + if (browser) await browser.close(); + const report = { + schemaVersion: "droboticflow.fanyu.browser-acceptance/v2", + projectId, + workflowId, + passed: !fatalError && checks.length > 0 && checks.every((item) => item.passed), + checks, + diagnostics, + screenshots, + statePath, + createdAt: new Date().toISOString(), + ...(fatalError ? { error: fatalError } : {}), + }; + fs.writeFileSync(reportPath, JSON.stringify(report, null, 2)); + console.log( + JSON.stringify({ passed: report.passed, reportPath, screenshots, error: fatalError || undefined }, null, 2), + ); + if (!report.passed) process.exitCode = 1; +} diff --git a/scripts/lib/deploy-validation.sh b/scripts/lib/deploy-validation.sh index 22d41d73..ee3088cd 100644 --- a/scripts/lib/deploy-validation.sh +++ b/scripts/lib/deploy-validation.sh @@ -153,7 +153,6 @@ validate_manifest_startup_configuration() { validate_http_service_url WORKER_API_BASE_URL "$WORKER_API_BASE_URL" validate_ai_runtime_configuration validate_optional_http_service_url BOS_ENDPOINT "$BOS_ENDPOINT" - validate_optional_token_value FANYU_V5_SUPPLIER_ID "${FANYU_V5_SUPPLIER_ID:-}" validate_integer_min MONGO_CONNECT_RETRIES "$MONGO_CONNECT_RETRIES" 1 validate_integer_min MONGO_CONNECT_RETRY_DELAY_MS "$MONGO_CONNECT_RETRY_DELAY_MS" 1 validate_integer_min WORKER_POLL_INTERVAL_MS "$WORKER_POLL_INTERVAL_MS" 1 diff --git a/scripts/render-manifests.sh b/scripts/render-manifests.sh index 23d430f8..9edba87a 100755 --- a/scripts/render-manifests.sh +++ b/scripts/render-manifests.sh @@ -70,7 +70,6 @@ DROBOTICFLOW_TRUSTED_PROXY_CIDRS="${DROBOTICFLOW_TRUSTED_PROXY_CIDRS:-}" HEADPOSE_SERVICE_IMAGE="${HEADPOSE_SERVICE_IMAGE:-droboticflow-headpose-service:latest}" HEADPOSE_SERVICE_IMAGE_ID="${HEADPOSE_SERVICE_IMAGE_ID:-}" HEADPOSE_MODEL_SHA256="${HEADPOSE_MODEL_SHA256:-sha256:9bdcd0e072df0d21e92288b40ea1d1b3cf7173c44728ab9c45c4d673f361e018}" -FANYU_V5_SUPPLIER_ID="${FANYU_V5_SUPPLIER_ID:-}" # shellcheck source=scripts/lib/image-config.sh source "$ROOT_DIR/scripts/lib/image-config.sh" REGISTRY_HOST="${REGISTRY_HOST:-}" @@ -178,7 +177,6 @@ export \ HEADPOSE_SERVICE_IMAGE \ HEADPOSE_SERVICE_IMAGE_ID \ HEADPOSE_MODEL_SHA256 \ - FANYU_V5_SUPPLIER_ID python3 - <<'PY' import os @@ -242,7 +240,6 @@ replacements = { "__HEADPOSE_SERVICE_IMAGE__": os.environ["HEADPOSE_SERVICE_IMAGE"], "__HEADPOSE_SERVICE_IMAGE_ID__": os.environ["HEADPOSE_SERVICE_IMAGE_ID"], "__HEADPOSE_MODEL_SHA256__": os.environ["HEADPOSE_MODEL_SHA256"], - "__FANYU_V5_SUPPLIER_ID__": os.environ["FANYU_V5_SUPPLIER_ID"], } rendered = template diff --git a/scripts/run_fanyu_full_human_in_the_loop.py b/scripts/run_fanyu_full_human_in_the_loop.py index 96bd9154..d7fb68f3 100755 --- a/scripts/run_fanyu_full_human_in_the_loop.py +++ b/scripts/run_fanyu_full_human_in_the_loop.py @@ -9,6 +9,7 @@ passwords, mutates the production project, or deletes source Capture objects. from __future__ import annotations import argparse +import http.client import json import os import secrets @@ -43,15 +44,112 @@ def item_list(payload: Any) -> list[dict[str, Any]]: return [] -def upsert_hyperparam(node: dict[str, Any], key: str, value: Any) -> None: +def upsert_hyperparam(node: dict[str, Any], key: str, value: Any, value_type: str | None = None) -> None: normalized = str(value) data = node.setdefault("data", {}) params = data.setdefault("hyperparams", []) for param in params: if str(param.get("key", "")).strip() == key: param["value"] = normalized + if value_type: + param["type"] = value_type return - params.append({"key": key, "value": normalized, "type": "number"}) + params.append({"key": key, "value": normalized, "type": value_type or "number"}) + + +def is_annotation_gate_node(node: dict[str, Any]) -> bool: + data = node.get("data") if isinstance(node.get("data"), dict) else {} + return data.get("operatorId") == "operator-builtin-annotation-gate" or "真实切分" in str(data.get("label", "")) + + +def clone_workflow_nodes_for_supplier(source_nodes: Any, supplier_id: str) -> list[dict[str, Any]]: + supplier_id = str(supplier_id).strip() + if not supplier_id: + raise AcceptanceError("supplierId is required before cloning the acceptance workflow") + nodes = json.loads(json.dumps(source_nodes if isinstance(source_nodes, list) else [])) + matched = 0 + for node in nodes: + if not isinstance(node, dict) or not is_annotation_gate_node(node): + continue + matched += 1 + upsert_hyperparam(node, "MAX_REWORK_ROUNDS", 1, "number") + upsert_hyperparam(node, "ANNOTATION_SUPPLIER_ID", supplier_id, "string") + if matched != 1: + raise AcceptanceError(f"expected exactly one annotation gate node, found {matched}") + return nodes + + +def annotation_supplier_ids(nodes: Any) -> set[str]: + values: set[str] = set() + for node in nodes if isinstance(nodes, list) else []: + if not isinstance(node, dict) or not is_annotation_gate_node(node): + continue + data = node.get("data") if isinstance(node.get("data"), dict) else {} + for param in data.get("hyperparams", []) if isinstance(data.get("hyperparams"), list) else []: + if str(param.get("key", "")).strip() == "ANNOTATION_SUPPLIER_ID": + values.add(str(param.get("value", "")).strip()) + return values + + +def human_item_supplier_id(items: list[dict[str, Any]], fallback: str = "") -> str: + suppliers = {str(item.get("supplierId", "")).strip() for item in items if str(item.get("supplierId", "")).strip()} + if len(suppliers) > 1: + raise AcceptanceError(f"human items span multiple suppliers: {sorted(suppliers)}") + if suppliers: + return next(iter(suppliers)) + fallback = str(fallback).strip() + if fallback: + return fallback + raise AcceptanceError("human items do not carry a supplierId") + + +def retry_runs_settled(runs: list[dict[str, Any]], minimum_attempts: int = 3) -> bool: + if len([item for item in runs if item.get("status") == "failed"]) < minimum_attempts: + return False + active = {"queued", "pending", "running", "stopping", "waiting", "waiting_for_audit", "waiting_for_human"} + return all(str(item.get("status", "")) not in active for item in runs) + + +def annotation_draft_matches( + draft: Any, + slices: list[dict[str, Any]], + segments: list[dict[str, Any]], +) -> bool: + if not isinstance(draft, dict): + return False + expected_slice_ids = [str(item.get("id", "")) for item in slices] + expected_event_ids = [str(item.get("id", "")) for item in segments] + expected_statuses = {str(item.get("id", "")): str(item.get("annotationStatus", "")) for item in slices} + actual_slices = draft.get("sliceSegments") if isinstance(draft.get("sliceSegments"), list) else [] + actual_segments = draft.get("segments") if isinstance(draft.get("segments"), list) else [] + actual_statuses = draft.get("statuses") if isinstance(draft.get("statuses"), dict) else {} + return ( + [str(item.get("id", "")) for item in actual_slices if isinstance(item, dict)] == expected_slice_ids + and [str(item.get("id", "")) for item in actual_segments if isinstance(item, dict)] == expected_event_ids + and {str(key): str(value) for key, value in actual_statuses.items()} == expected_statuses + ) + + +def completed_materialization(value: Any) -> bool: + return ( + isinstance(value, dict) + and value.get("status") == "completed" + and bool(str(value.get("artifactId", "")).strip()) + and bool(str(value.get("runId", "")).strip()) + ) + + +def recoverable_submission_transport_error(error: BaseException) -> bool: + current: BaseException | None = error + while current is not None: + if isinstance(current, (TimeoutError, urllib.error.URLError, http.client.RemoteDisconnected, ConnectionResetError)): + return True + current = current.__cause__ + text = str(error).lower() + return any( + marker in text + for marker in ("timed out", "timeout", "remote disconnected", "remote end closed", "connection reset", "urlopen error") + ) class Harness: @@ -75,6 +173,7 @@ class Harness: self.quality = DroboticflowClient(self.api_url, error_factory=AcceptanceError) self.acceptance = DroboticflowClient(self.api_url, error_factory=AcceptanceError) self.supplier_manager = DroboticflowClient(self.api_url, error_factory=AcceptanceError) + self.session_user_ids: dict[str, str] = {} self.probe_password = self.state.get("probePassword") or secrets.token_urlsafe(24) self.manager_password = self.state.get("managerPassword") or secrets.token_urlsafe(24) @@ -97,6 +196,143 @@ class Harness: if not passed: raise AcceptanceError(f"check failed: {name}: {evidence}") + def remember_resource(self, name: str, value: Any) -> None: + self.state.setdefault("evidence", {})[name] = value + self.report["resources"][name] = value + self.save_state() + + def remember_human_item( + self, + item: dict[str, Any], + *, + task: dict[str, Any] | None = None, + slice_ids: list[str] | None = None, + event_ids: list[str] | None = None, + ) -> None: + evidence = self.state.setdefault("evidence", {}).setdefault("humanItemsById", {}) + item_id = str(item.get("id", "")).strip() + if not item_id: + return + locator = dict(evidence.get(item_id) or {}) + locator.update( + { + "humanItemId": item_id, + "taskId": str(item.get("legacyTaskId") or locator.get("taskId") or "").strip(), + "runId": str(item.get("runId") or locator.get("runId") or "").strip(), + "supplierId": str(item.get("supplierId") or locator.get("supplierId") or "").strip(), + "stageId": str(item.get("currentStageId") or locator.get("stageId") or "").strip(), + } + ) + if task: + locator["taskId"] = str(task.get("id") or locator.get("taskId") or "").strip() + locator["runId"] = str(task.get("runId") or locator.get("runId") or "").strip() + locator["dataTaskId"] = str(task.get("dataTaskId", "")).strip() + capture_by_run = { + str(entry.get("runId", "")): str(entry.get("captureId", "")) + for entry in self.state.get("evidence", {}).get("captures", []) + if isinstance(entry, dict) and entry.get("runId") + } + if locator.get("runId") in capture_by_run: + locator["captureId"] = capture_by_run[locator["runId"]] + if slice_ids is not None: + locator["sliceIds"] = slice_ids + if event_ids is not None: + locator["eventIds"] = event_ids + evidence[item_id] = locator + self.report["resources"]["humanItems"] = list(evidence.values()) + self.save_state() + + def capture_directory_for_item(self, item: dict[str, Any]) -> str: + display = item.get("display") if isinstance(item.get("display"), dict) else {} + directories = display.get("captureDirectories") if isinstance(display.get("captureDirectories"), list) else [] + for directory in directories: + if str(directory).strip(): + return str(directory).strip() + run_id = str(item.get("runId", "")).strip() + for entry in self.state.get("evidence", {}).get("captures", []): + if isinstance(entry, dict) and str(entry.get("runId", "")).strip() == run_id: + capture_id = str(entry.get("captureId", "")).strip() + if capture_id: + return capture_id + raise AcceptanceError(f"cannot resolve Capture directory for human item {item.get('id')}") + + def record_browser_fixture( + self, + annotation_item: dict[str, Any], + quality_item: dict[str, Any], + acceptance_item: dict[str, Any], + *, + member_user_id: str, + member_email: str, + supplier_id: str, + ) -> None: + expected_stages = ( + ("annotation", annotation_item, "annotation_combined", {"ready", "rework"}), + ("quality", quality_item, "quality_review", {"ready", "submitted", "rework"}), + ("acceptance", acceptance_item, "prepackage_acceptance", {"ready", "rework"}), + ) + for name, item, stage_id, statuses in expected_stages: + if ( + str(item.get("currentStageId", "")) != stage_id + or str(item.get("status", "")) not in statuses + or str(item.get("assignedUserId", "")).strip() + ): + raise AcceptanceError(f"browser {name} fixture is already consumed or in the wrong stage: {item}") + quality_payload = quality_item.get("subject", {}).get("payload", {}) if isinstance(quality_item.get("subject"), dict) else {} + if not completed_materialization(quality_payload.get("materialization")): + raise AcceptanceError(f"browser quality fixture has no completed materialization: {quality_item}") + acceptance_payload = acceptance_item.get("subject", {}).get("payload", {}) if isinstance(acceptance_item.get("subject"), dict) else {} + acceptance_evidence = acceptance_payload.get("items") if isinstance(acceptance_payload.get("items"), list) else [] + if not acceptance_evidence or not any( + completed_materialization(entry.get("materialization")) + for entry in acceptance_evidence + if isinstance(entry, dict) + ): + raise AcceptanceError(f"browser acceptance fixture has no materialized evidence item: {acceptance_item}") + fixture = { + "annotation": { + "captureDirectory": self.capture_directory_for_item(annotation_item), + "taskId": str(annotation_item.get("legacyTaskId", "")), + "humanItemId": str(annotation_item.get("id", "")), + "runId": str(annotation_item.get("runId", "")), + "pool": "available", + }, + "quality": { + "captureDirectory": self.capture_directory_for_item(quality_item), + "taskId": str(quality_item.get("legacyTaskId", "")), + "humanItemId": str(quality_item.get("id", "")), + "runId": str(quality_item.get("runId", "")), + "pool": "available", + }, + "acceptance": { + "captureDirectory": self.capture_directory_for_item(acceptance_item), + "humanItemId": str(acceptance_item.get("id", "")), + "runId": str(acceptance_item.get("runId", "")), + "pool": "available", + }, + "member": { + "userId": member_user_id, + "email": member_email, + "supplierId": supplier_id, + "initialRoles": ["annotator"], + "bulkRoles": ["annotator", "quality_reviewer"], + "disposable": True, + }, + } + required = ( + fixture["annotation"]["taskId"], + fixture["annotation"]["humanItemId"], + fixture["quality"]["taskId"], + fixture["quality"]["humanItemId"], + fixture["acceptance"]["humanItemId"], + fixture["member"]["userId"], + ) + if not all(required): + raise AcceptanceError(f"browser fixture is incomplete: {fixture}") + self.state["browserFixture"] = fixture + self.report["resources"]["browserFixture"] = fixture + self.save_state() + def raw_status(self, client: DroboticflowClient, method: str, path: str, payload: Any | None = None) -> tuple[int, Any]: body = None if payload is None else json.dumps(payload, ensure_ascii=False).encode("utf-8") headers = client.request_headers(method, {"Content-Type": "application/json"}) @@ -116,21 +352,31 @@ class Harness: def login(self) -> None: accounts = [ - (self.admin, "DROBOTICFLOW_ACCEPTANCE_EMAIL", "DROBOTICFLOW_ACCEPTANCE_PASSWORD", "organization admin"), - (self.annotator, "FANYU_ANNOTATOR_EMAIL", "FANYU_ANNOTATOR_PASSWORD", "annotator"), - (self.quality, "FANYU_QUALITY_EMAIL", "FANYU_QUALITY_PASSWORD", "quality reviewer"), - (self.acceptance, "FANYU_ACCEPTANCE_EMAIL", "FANYU_ACCEPTANCE_PASSWORD", "acceptance reviewer"), + ("admin", self.admin, "DROBOTICFLOW_ACCEPTANCE_EMAIL", "DROBOTICFLOW_ACCEPTANCE_PASSWORD", "organization admin"), + ("annotator", self.annotator, "FANYU_ANNOTATOR_EMAIL", "FANYU_ANNOTATOR_PASSWORD", "annotator"), + ("quality", self.quality, "FANYU_QUALITY_EMAIL", "FANYU_QUALITY_PASSWORD", "quality reviewer"), + ("acceptance", self.acceptance, "FANYU_ACCEPTANCE_EMAIL", "FANYU_ACCEPTANCE_PASSWORD", "acceptance reviewer"), ] - for client, email_name, password_name, label in accounts: + for key, client, email_name, password_name, label in accounts: client.login_if_configured(env(email_name, required=True), env(password_name, required=True), label=label) - session = self.admin.request_json("GET", "/api/auth/me") - self.check("admin_login", bool(session.get("user", {}).get("id")), userId=session.get("user", {}).get("id")) + session = client.request_json("GET", "/api/auth/me") + user_id = str(session.get("user", {}).get("id", "")).strip() + if not user_id: + raise AcceptanceError(f"{label} session has no user id") + self.session_user_ids[key] = user_id + self.check("admin_login", bool(self.session_user_ids.get("admin")), userId=self.session_user_ids.get("admin")) + self.remember_resource("sessionUserIds", self.session_user_ids) - def ensure_project_and_workflow(self) -> None: + def source_context(self) -> tuple[dict[str, Any], dict[str, Any]]: source_project_id = env("FANYU_ACCEPTANCE_SOURCE_PROJECT_ID", self.args.source_project_id, required=True) source_workflow_id = env("FANYU_ACCEPTANCE_SOURCE_WORKFLOW_ID", self.args.source_workflow_id, required=True) source_project = self.admin.request_json("GET", f"/api/projects/{source_project_id}") source_workflow = self.admin.request_json("GET", f"/api/workflows/{source_workflow_id}") + self.state.update({"sourceProjectId": source_project_id, "sourceWorkflowId": source_workflow_id}) + return source_project, source_workflow + + def ensure_project(self) -> None: + source_project, _ = self.source_context() project_name = f"泛宇 100% 隔离验收 {self.args.acceptance_id}" projects = item_list(self.admin.request_json("GET", "/api/projects")) project = next((item for item in projects if item.get("name") == project_name), None) @@ -147,18 +393,23 @@ class Harness: }, ) self.state["projectId"] = project["id"] + self.remember_resource("projectId", project["id"]) + + def ensure_workflow(self) -> None: + source_project, source_workflow = self.source_context() + project_id = str(self.state.get("projectId", "")).strip() + supplier_id = str(self.state.get("supplierId", "")).strip() + if not project_id or not supplier_id: + raise AcceptanceError("project and supplier members must exist before the workflow is cloned") workflow_name = f"Fanyu v5.8 100% acceptance {self.args.acceptance_id}" - workflows = item_list(self.admin.request_json("GET", f"/api/projects/{project['id']}/workflows")) + workflows = item_list(self.admin.request_json("GET", f"/api/projects/{project_id}/workflows")) workflow = next((item for item in workflows if item.get("name") == workflow_name), None) if workflow is None: - nodes = json.loads(json.dumps(source_workflow.get("nodes", []))) - for node in nodes: - if node.get("data", {}).get("operatorId") == "operator-builtin-annotation-gate" or "真实切分" in str(node.get("data", {}).get("label", "")): - upsert_hyperparam(node, "MAX_REWORK_ROUNDS", 1) + nodes = clone_workflow_nodes_for_supplier(source_workflow.get("nodes", []), supplier_id) workflow = self.admin.request_json( "POST", - f"/api/projects/{project['id']}/workflows", + f"/api/projects/{project_id}/workflows", { "name": workflow_name, "description": "Isolated Fanyu 100% acceptance workflow", @@ -176,7 +427,14 @@ class Harness: "edges": source_workflow.get("edges", []), }, ) - workflow = self.admin.request_json("POST", f"/api/workflows/{workflow['id']}/release", {}) + self.admin.request_json("POST", f"/api/workflows/{workflow['id']}/release", {}) + workflow = self.admin.request_json("GET", f"/api/workflows/{workflow['id']}") + bound_suppliers = annotation_supplier_ids(workflow.get("nodes", [])) + if bound_suppliers != {supplier_id}: + raise AcceptanceError( + f"existing acceptance workflow supplier binding {sorted(bound_suppliers)} does not match {supplier_id}; " + "use a new acceptance-id or recreate the isolated workflow" + ) self.state["workflowId"] = workflow["id"] source_automation = source_workflow.get("captureAutomation") or {} @@ -200,11 +458,98 @@ class Harness: }, ) self.check("capture_automation_config", automation.get("config", {}).get("enabled") is True, config=automation.get("config")) - self.report["resources"].update({"projectId": project["id"], "workflowId": workflow["id"], "targetRoot": self.args.target_root}) - self.save_state() + self.remember_resource( + "workflow", + {"workflowId": workflow["id"], "supplierId": supplier_id, "targetRoot": self.args.target_root}, + ) + + def role_account_specs(self) -> list[tuple[str, list[str]]]: + return [ + (env("FANYU_ANNOTATOR_EMAIL", required=True).lower(), ["annotator"]), + (env("FANYU_QUALITY_EMAIL", required=True).lower(), ["quality_reviewer"]), + (env("FANYU_ACCEPTANCE_EMAIL", required=True).lower(), ["acceptance_reviewer"]), + ] + + def users_by_email(self) -> dict[str, dict[str, Any]]: + users = item_list(self.admin.request_json("GET", "/api/users?includeDisabled=true")) + return {str(item.get("email", "")).lower(): item for item in users} + + def ensure_project_member(self, user_id: str, supplier_id: str, roles: list[str]) -> dict[str, Any]: + project_id = str(self.state.get("projectId", "")).strip() + if not project_id: + raise AcceptanceError("projectId is required before project members") + expected_roles = set(roles) + status, payload = self.raw_status( + self.admin, + "POST", + f"/api/projects/{project_id}/members", + {"userId": user_id, "supplierId": supplier_id, "roles": roles}, + ) + if status == 409 and isinstance(payload, dict): + member = payload.get("member") + elif status == 201: + member = payload + else: + raise AcceptanceError(f"create project member {user_id}: {status} {payload}") + if not isinstance(member, dict) or not member.get("id"): + raise AcceptanceError(f"project member response is incomplete for {user_id}: {payload}") + if str(member.get("supplierId", "")).strip() != supplier_id or set(member.get("roles") or []) != expected_roles: + member = self.admin.request_json( + "PATCH", + f"/api/projects/{project_id}/members/{member['id']}", + {"supplierId": supplier_id, "roles": roles}, + ) + if str(member.get("supplierId", "")).strip() != supplier_id or set(member.get("roles") or []) != expected_roles: + raise AcceptanceError( + f"project member {member.get('id')} does not match supplier {supplier_id} and roles {sorted(expected_roles)}" + ) + return member + + def ensure_main_role_members(self, supplier_id: str) -> tuple[dict[str, str], dict[str, dict[str, Any]]]: + by_email = self.users_by_email() + member_ids: dict[str, str] = {} + for email, roles in self.role_account_specs(): + user = by_email.get(email) + if user is None: + raise AcceptanceError(f"configured test user not found: {email}") + member = self.ensure_project_member(str(user["id"]), supplier_id, roles) + member_ids[email] = str(member["id"]) + self.state["memberIds"] = member_ids + return member_ids, by_email + + def ensure_probe_member(self, supplier_id: str) -> tuple[dict[str, Any], dict[str, Any]]: + project_id = str(self.state.get("projectId", "")).strip() + probe_email = f"fanyu-acceptance-probe+{self.args.acceptance_id.lower()}@droboticflow.local" + by_email = self.users_by_email() + probe = by_email.get(probe_email) + if probe is None: + probe = self.admin.request_json( + "POST", + "/api/users", + {"name": f"Fanyu acceptance probe {self.args.acceptance_id}", "email": probe_email, "role": "member", "password": self.probe_password}, + ) + else: + probe = self.admin.request_json( + "PUT", f"/api/users/{probe['id']}", {"role": "member", "status": "active", "password": self.probe_password} + ) + probe_member = self.ensure_project_member(str(probe["id"]), supplier_id, ["annotator"]) + self.state.update( + { + "probeUserId": probe["id"], + "probePassword": self.probe_password, + "probeMemberId": probe_member["id"], + } + ) + self.remember_resource( + "probeMember", + {"projectId": project_id, "userId": probe["id"], "memberId": probe_member["id"], "supplierId": supplier_id, "roles": ["annotator"]}, + ) + return probe, probe_member def ensure_supplier_and_members(self) -> None: - project_id = self.state["projectId"] + project_id = str(self.state.get("projectId", "")).strip() + if not project_id: + raise AcceptanceError("project must be created before supplier members") supplier_name = f"Fanyu acceptance supplier {self.args.acceptance_id}" suppliers = item_list(self.admin.request_json("GET", "/api/suppliers")) supplier = next((item for item in suppliers if item.get("name") == supplier_name), None) @@ -212,30 +557,8 @@ class Harness: supplier = self.admin.request_json("POST", "/api/suppliers", {"name": supplier_name}) self.state["supplierId"] = supplier["id"] - users = item_list(self.admin.request_json("GET", "/api/users?includeDisabled=true")) - by_email = {str(item.get("email", "")).lower(): item for item in users} - role_accounts = [ - (env("FANYU_ANNOTATOR_EMAIL", required=True).lower(), ["annotator"]), - (env("FANYU_QUALITY_EMAIL", required=True).lower(), ["quality_reviewer"]), - (env("FANYU_ACCEPTANCE_EMAIL", required=True).lower(), ["acceptance_reviewer"]), - ] - member_ids: dict[str, str] = {} - for email, roles in role_accounts: - user = by_email.get(email) - if user is None: - raise AcceptanceError(f"configured test user not found: {email}") - status, payload = self.raw_status( - self.admin, - "POST", - f"/api/projects/{project_id}/members", - {"userId": user["id"], "supplierId": supplier["id"], "roles": roles}, - ) - if status == 409: - payload = payload.get("member") - elif status != 201: - raise AcceptanceError(f"create project member {email}: {status} {payload}") - member_ids[email] = payload["id"] - self.state["memberIds"] = member_ids + member_ids, by_email = self.ensure_main_role_members(supplier["id"]) + role_accounts = self.role_account_specs() duplicate_status, _ = self.raw_status( self.admin, @@ -266,21 +589,9 @@ class Harness: ) self.check("member_management_403", forbidden_status == 403, status=forbidden_status) - probe_email = f"fanyu-acceptance-probe+{self.args.acceptance_id.lower()}@droboticflow.local" manager_email = f"fanyu-acceptance-manager+{self.args.acceptance_id.lower()}@droboticflow.local" - users = item_list(self.admin.request_json("GET", "/api/users?includeDisabled=true")) - by_email = {str(item.get("email", "")).lower(): item for item in users} - probe = by_email.get(probe_email) - if probe is None: - probe = self.admin.request_json( - "POST", - "/api/users", - {"name": f"Fanyu acceptance probe {self.args.acceptance_id}", "email": probe_email, "role": "member", "password": self.probe_password}, - ) - else: - probe = self.admin.request_json( - "PUT", f"/api/users/{probe['id']}", {"role": "member", "status": "active", "password": self.probe_password} - ) + probe, probe_member = self.ensure_probe_member(supplier["id"]) + by_email = self.users_by_email() manager = by_email.get(manager_email) if manager is None: manager = self.admin.request_json( @@ -298,7 +609,7 @@ class Harness: manager = self.admin.request_json( "PUT", f"/api/users/{manager['id']}", - {"role": "supplier_manager", "status": "active", "password": self.manager_password}, + {"role": "supplier_manager", "supplierId": supplier["id"], "status": "active", "password": self.manager_password}, ) self.state.update( { @@ -308,16 +619,6 @@ class Harness: "managerPassword": self.manager_password, } ) - status, probe_member = self.raw_status( - self.admin, - "POST", - f"/api/projects/{project_id}/members", - {"userId": probe["id"], "supplierId": supplier["id"], "roles": ["annotator"]}, - ) - if status == 409: - probe_member = probe_member.get("member") - elif status != 201: - raise AcceptanceError(f"create probe member: {status} {probe_member}") self.state["probeMemberId"] = probe_member["id"] bulk = self.admin.request_json( "POST", @@ -341,7 +642,29 @@ class Harness: {"action": "set_roles", "memberIds": [probe_member["id"]], "roles": ["acceptance_reviewer"]}, ) self.check("supplier_manager_management_403", manager_forbidden == 403, status=manager_forbidden) - self.save_state() + self.remember_resource( + "supplierMembers", + {"supplierId": supplier["id"], "memberIds": member_ids, "probeMemberId": probe_member["id"]}, + ) + + def ensure_human_phase_members(self, items: list[dict[str, Any]]) -> str: + supplier_id = human_item_supplier_id(items) + suppliers = item_list(self.admin.request_json("GET", "/api/suppliers")) + if not any(str(item.get("id", "")).strip() == supplier_id for item in suppliers): + raise AcceptanceError(f"human item supplier does not exist: {supplier_id}") + self.state["supplierId"] = supplier_id + member_ids, _ = self.ensure_main_role_members(supplier_id) + _, probe_member = self.ensure_probe_member(supplier_id) + self.remember_resource( + "humanPhaseMembers", + { + "supplierId": supplier_id, + "memberIds": member_ids, + "probeMemberId": probe_member["id"], + "humanItemIds": [str(item.get("id", "")) for item in items], + }, + ) + return supplier_id def wait_automation(self, predicate, timeout: int = 3600) -> dict[str, Any]: deadline = time.time() + timeout @@ -406,6 +729,20 @@ class Harness: time.sleep(5) else: raise AcceptanceError(f"automatic retry did not produce three failed attempts: {runs}") + + # The final Run may become visible as failed just before its lineage and + # terminal timestamps are durably reflected by the list endpoint. Give + # that last transition one short, bounded settle window instead of + # treating the read-after-write race as a product failure. + grace_deadline = min(deadline, time.time() + 15) + while time.time() < grace_deadline: + time.sleep(2) + refreshed = item_list(self.admin.request_json("GET", f"/api/runs?workflowId={workflow['id']}")) + runs = refreshed or runs + if retry_runs_settled(runs): + break + if not retry_runs_settled(runs): + raise AcceptanceError(f"automatic retry attempts did not settle within the grace window: {runs}") details = [self.admin.request_json("GET", f"/api/runs/{item['id']}") for item in runs] details.sort(key=lambda item: int(item.get("attempt") or 1)) attempts = [int(item.get("attempt") or 1) for item in details] @@ -422,6 +759,17 @@ class Harness: if (current_available - previous_end).total_seconds() < 59: raise AcceptanceError(f"retry backoff shorter than 60 seconds: {previous['id']} -> {current['id']}") self.check("automatic_retry_backoff", True, seconds=60) + retry_evidence = [ + { + "runId": item.get("id"), + "parentRunId": item.get("parentRunId", ""), + "attempt": int(item.get("attempt") or 1), + "status": item.get("status"), + } + for item in details + ] + self.state["retryWorkflowId"] = workflow["id"] + self.remember_resource("retryRuns", retry_evidence) def verify_capture_matrix(self) -> None: expected_missing = { @@ -508,14 +856,34 @@ class Harness: ) ids = [item["id"] for item in stable["records"] if item.get("captureId") in expected_complete] self.check("capture_idempotence", len(ids) == len(set(ids)) == len(expected_complete), recordIds=ids) - self.report["resources"]["captureRecordIds"] = ids + capture_evidence = [ + { + "captureId": item.get("captureId"), + "captureRecordId": item.get("id"), + "runId": item.get("runId", ""), + "parentRunId": item.get("parentRunId", ""), + "status": item.get("status"), + "attempt": item.get("attempt", 0), + } + for item in stable.get("records", []) + if item.get("captureId") in expected_missing | expected_complete + ] + capture_evidence.sort(key=lambda item: str(item.get("captureId", ""))) + self.state["captureRecordIds"] = ids + self.remember_resource("captures", capture_evidence) def wait_human_items(self, minimum: int = 1, timeout: int = 3600) -> list[dict[str, Any]]: deadline = time.time() + timeout query = f"pluginId=annotation-qa&projectId={self.state['projectId']}&workflowId={self.state['workflowId']}&pool=all" while time.time() < deadline: items = item_list(self.admin.request_json("GET", f"/api/human-items?{query}")) - candidates = [item for item in items if item.get("currentStageId") == "annotation_combined" and item.get("legacyTaskId")] + candidates = [ + item + for item in items + if item.get("legacyTaskId") + and item.get("currentStageId") != "prepackage_acceptance" + and item.get("status") not in {"cancelled", "rejected"} + ] if len(candidates) >= minimum: return candidates time.sleep(10) @@ -532,19 +900,24 @@ class Harness: two = start + (end - start) * 2 / 3 slices = [ {"id": "acceptance-slice-001", "start": start, "end": one, "annotationStatus": "labeled"}, - {"id": "acceptance-slice-002", "start": one, "end": two, "annotationStatus": "confirmed_empty"}, + {"id": "acceptance-slice-002", "start": one, "end": two, "annotationStatus": "empty_confirmed"}, {"id": "acceptance-slice-003", "start": two, "end": end, "annotationStatus": "labeled"}, ] segments = [ {"id": "acceptance-event-001", "labelId": "01", "label": "鲜食处理", "start": start, "end": one, "source": "manual", "confidence": 1}, {"id": "acceptance-event-003", "labelId": "15", "label": "顾客交付与内部协作", "start": two, "end": end, "source": "manual", "confidence": 1}, ] + self.remember_human_item( + item, + task=task, + slice_ids=[entry["id"] for entry in slices], + event_ids=[entry["id"] for entry in segments], + ) if draft_first: quality_status, _ = self.raw_status(self.quality, "POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) acceptance_status, _ = self.raw_status(self.acceptance, "POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) self.check("annotation_stage_cross_role_403", quality_status == 403 and acceptance_status == 403, quality=quality_status, acceptance=acceptance_status) - claimed = self.annotator.request_json("POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) - claimed_item = claimed.get("item", claimed) + claimed_item = self.claim_human_item(self.annotator, item, "annotator") if draft_first: saved = self.annotator.request_json( "PUT", @@ -559,27 +932,55 @@ class Harness: ) claimed_item = saved["item"] restored = self.annotator.request_json("GET", f"/api/plugins/annotation-qa/tasks/{item['legacyTaskId']}") - self.check("server_draft_restore", bool(restored.get("humanItem", {}).get("subject", {}).get("payload", {}).get("draft")), taskId=item["legacyTaskId"]) - self.annotator.request_json( - "POST", - f"/api/plugins/annotation-qa/tasks/{item['legacyTaskId']}/combined-submissions", - { - "version": claimed_item["version"], - "sliceSegments": slices, - "sliceOperations": [], - "segments": segments, - "requireSliceCompletion": True, - "changeReason": "Fanyu 100% acceptance combined submission", - }, + annotation_draft = restored.get("humanItem", {}).get("subject", {}).get("payload", {}).get("annotationDraft") + self.check( + "server_draft_restore", + annotation_draft_matches(annotation_draft, slices, segments), + taskId=item["legacyTaskId"], + sliceIds=[entry["id"] for entry in slices], + eventIds=[entry["id"] for entry in segments], + statuses={entry["id"]: entry["annotationStatus"] for entry in slices}, + ) + submission_version = int(claimed_item["version"]) + submission_id = f"fanyu-v5-combined-{item['id']}-v{submission_version}" + submission_payload = { + "version": submission_version, + "sliceSegments": slices, + "sliceOperations": [], + "segments": segments, + "requireSliceCompletion": True, + "changeReason": "Fanyu 100% acceptance combined submission", + } + try: + self.annotator.request_json( + "POST", + f"/api/plugins/annotation-qa/tasks/{item['legacyTaskId']}/combined-submissions", + submission_payload, + timeout=min(max(180, int(self.args.human_timeout)), 900), + retries=1, + ) + except AcceptanceError as error: + # Physical stereo/PTS/IMU slicing can outlive an HTTP client or + # reverse-proxy timeout. The server reserves an idempotent token + # before materialization, so recover by polling that durable state + # instead of issuing a duplicate POST. + recoverable_transport_markers = ("timed out", "remotedisconnected", "failed 502", "failed 504") + if not any(marker in str(error).lower() for marker in recoverable_transport_markers): + raise + updated = self.wait_combined_submission(item["id"], submission_id) + self.remember_human_item( + updated, + task=task, + slice_ids=[entry["id"] for entry in slices], + event_ids=[entry["id"] for entry in segments], ) - return self.admin.request_json("GET", f"/api/human-items/{item['id']}")["item"] + return updated def quality_decision(self, item: dict[str, Any], decision: str) -> dict[str, Any]: annotator_status, _ = self.raw_status(self.annotator, "POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) acceptance_status, _ = self.raw_status(self.acceptance, "POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) self.check("quality_stage_cross_role_403", annotator_status == 403 and acceptance_status == 403, annotator=annotator_status, acceptance=acceptance_status) - claimed = self.quality.request_json("POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) - claimed_item = claimed.get("item", claimed) + claimed_item = self.claim_human_item(self.quality, item, "quality") self.quality.request_json( "POST", f"/api/plugins/annotation-qa/tasks/{item['legacyTaskId']}/reviews", @@ -594,7 +995,9 @@ class Harness: "overallGrade": "A", }, ) - return self.admin.request_json("GET", f"/api/human-items/{claimed_item['id']}")["item"] + updated = self.admin.request_json("GET", f"/api/human-items/{claimed_item['id']}")["item"] + self.remember_human_item(updated) + return updated def wait_acceptance_items(self, minimum: int, timeout: int = 3600) -> list[dict[str, Any]]: deadline = time.time() + timeout @@ -611,12 +1014,25 @@ class Harness: time.sleep(10) raise AcceptanceError("prepackage acceptance items did not become ready") + def wait_acceptance_item_for_task(self, task_id: str, timeout: int = 3600) -> dict[str, Any]: + deadline = time.time() + timeout + query = f"pluginId=annotation-qa&projectId={self.state['projectId']}&workflowId={self.state['workflowId']}&pool=all" + while time.time() < deadline: + items = item_list(self.admin.request_json("GET", f"/api/human-items?{query}")) + for item in items: + if item.get("currentStageId") != "prepackage_acceptance" or item.get("status") not in {"ready", "rework"}: + continue + payload = item.get("subject", {}).get("payload", {}) if isinstance(item.get("subject"), dict) else {} + if task_id and task_id in json.dumps(payload, ensure_ascii=False): + return item + time.sleep(2) + raise AcceptanceError(f"prepackage acceptance item for task {task_id} did not become ready") + def approve_acceptance(self, item: dict[str, Any], headpose: str, handpose: str) -> dict[str, Any]: annotator_status, _ = self.raw_status(self.annotator, "POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) quality_status, _ = self.raw_status(self.quality, "POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) self.check("acceptance_stage_cross_role_403", annotator_status == 403 and quality_status == 403, annotator=annotator_status, quality=quality_status) - claimed = self.acceptance.request_json("POST", f"/api/human-items/{item['id']}/claim", {"version": item["version"]}) - claimed_item = claimed.get("item", claimed) + claimed_item = self.claim_human_item(self.acceptance, item, "acceptance") excluded = headpose == "exclude" or handpose == "exclude" decided = self.acceptance.request_json( "POST", @@ -634,7 +1050,10 @@ class Harness: }, }, ) - return decided.get("item", decided) + updated = decided.get("item", decided) + if isinstance(updated, dict): + self.remember_human_item(updated) + return updated def wait_delivery_packages(self, minimum: int, timeout: int = 3600) -> list[dict[str, Any]]: deadline = time.time() + timeout @@ -647,37 +1066,173 @@ class Harness: time.sleep(10) raise AcceptanceError("accepted delivery packages did not become ready") + def human_item_for_client(self, client: DroboticflowClient, item_id: str) -> dict[str, Any]: + payload = client.request_json("GET", f"/api/human-items/{item_id}") + item = payload.get("item", payload) if isinstance(payload, dict) else None + if not isinstance(item, dict) or not item.get("id"): + raise AcceptanceError(f"human item not found: {item_id}") + return item + + def human_item(self, item_id: str) -> dict[str, Any]: + return self.human_item_for_client(self.admin, item_id) + + def claim_human_item( + self, + client: DroboticflowClient, + item: dict[str, Any], + session_key: str, + ) -> dict[str, Any]: + item_id = str(item.get("id", "")).strip() + actor_user_id = str(self.session_user_ids.get(session_key, "")).strip() + if not item_id or not actor_user_id: + raise AcceptanceError(f"cannot claim human item without id/session: item={item_id} session={session_key}") + latest = self.human_item_for_client(client, item_id) + + def reuse_existing(candidate: dict[str, Any]) -> dict[str, Any] | None: + status = str(candidate.get("status", "")).strip() + if status not in {"claimed", "in_progress"}: + return None + assignee = str(candidate.get("assignedUserId", "")).strip() + if assignee != actor_user_id: + raise AcceptanceError( + f"human item {item_id} is already claimed by {assignee or 'another user'}, expected {actor_user_id}" + ) + return candidate + + reused = reuse_existing(latest) + if reused is not None: + self.remember_human_item(reused) + return reused + status = str(latest.get("status", "")).strip() + if status not in {"ready", "rework", "submitted"}: + raise AcceptanceError(f"human item {item_id} cannot be claimed from status {status}") + response_status, payload = self.raw_status( + client, + "POST", + f"/api/human-items/{item_id}/claim", + {"version": latest.get("version")}, + ) + if response_status == 200 and isinstance(payload, dict): + claimed = payload.get("item", payload) + if isinstance(claimed, dict): + self.remember_human_item(claimed) + return claimed + if response_status == 409: + # A retry can race with the successful response from the previous + # process. Re-read once and reuse only our own durable claim. + refreshed = self.human_item_for_client(client, item_id) + reused = reuse_existing(refreshed) + if reused is not None: + self.remember_human_item(reused) + return reused + raise AcceptanceError(f"claim human item {item_id} failed: HTTP {response_status} {payload}") + + def wait_combined_submission( + self, + item_id: str, + expected_submission_id: str, + *, + timeout: int | None = None, + poll_seconds: float = 3.0, + ) -> dict[str, Any]: + if timeout is None: + timeout = min(max(180, int(getattr(self.args, "human_timeout", 180))), 900) + deadline = time.time() + max(1, timeout) + while time.time() < deadline: + current = self.human_item(item_id) + stage = str(current.get("currentStageId", "")).strip() + pending_id = str(current.get("pendingCombinedSubmissionId", "")).strip() + subject = current.get("subject") if isinstance(current.get("subject"), dict) else {} + payload = subject.get("payload") if isinstance(subject.get("payload"), dict) else {} + if stage == "quality_review" and completed_materialization(payload.get("materialization")): + self.remember_human_item(current) + submissions = self.state.setdefault("evidence", {}).setdefault("combinedSubmissions", {}) + submissions[item_id] = { + "submissionId": expected_submission_id, + "humanItemId": item_id, + "materialization": payload["materialization"], + } + self.report["resources"]["combinedSubmissions"] = list(submissions.values()) + self.save_state() + return current + if pending_id: + if pending_id != expected_submission_id: + raise AcceptanceError( + f"human item {item_id} is processing a different combined submission: {pending_id}" + ) + time.sleep(max(0, poll_seconds)) + continue + if stage == "annotation_combined": + raise AcceptanceError( + f"combined submission {expected_submission_id} cleared without materialization; item remained in annotation_combined" + ) + raise AcceptanceError( + f"combined submission {expected_submission_id} ended in unexpected stage {stage}: {current}" + ) + raise AcceptanceError(f"combined submission {expected_submission_id} did not finish within {timeout} seconds") + + def restore_browser_fixture_if_present(self) -> bool: + fixture = self.state.get("browserFixture") + if not isinstance(fixture, dict): + return False + try: + annotation_item = self.human_item(str(fixture["annotation"]["humanItemId"])) + quality_item = self.human_item(str(fixture["quality"]["humanItemId"])) + acceptance_item = self.human_item(str(fixture["acceptance"]["humanItemId"])) + except (KeyError, TypeError) as error: + raise AcceptanceError(f"stored browserFixture is incomplete: {fixture}") from error + supplier_id = self.ensure_human_phase_members([annotation_item, quality_item, acceptance_item]) + member_email = f"fanyu-acceptance-probe+{self.args.acceptance_id.lower()}@droboticflow.local" + self.record_browser_fixture( + annotation_item, + quality_item, + acceptance_item, + member_user_id=str(self.state.get("probeUserId", "")), + member_email=member_email, + supplier_id=supplier_id, + ) + self.check( + "browser_fixture_resume", + True, + annotationHumanItemId=annotation_item["id"], + qualityHumanItemId=quality_item["id"], + acceptanceHumanItemId=acceptance_item["id"], + ) + return True + def verify_human_flow(self) -> None: - items = self.wait_human_items(minimum=6, timeout=self.args.human_timeout) - probe_user_id = self.state.get("probeUserId") - probe_member_id = self.state.get("probeMemberId") - if probe_user_id and probe_member_id: - assigned = self.admin.request_json( - "PATCH", - f"/api/human-items/{items[0]['id']}/assignment", - {"version": items[0]["version"], "assignedUserId": probe_user_id}, - ) - removed = self.admin.request_json( - "POST", - f"/api/projects/{self.state['projectId']}/members/bulk", - {"action": "remove", "memberIds": [probe_member_id]}, - ) - released = self.admin.request_json("GET", f"/api/human-items/{items[0]['id']}")["item"] - self.check( - "member_bulk_remove_releases_task", - assigned.get("assignedUserId") == probe_user_id - and removed.get("updated") == 1 - and not released.get("assignedUserId") - and released.get("status") == "ready", - assigned=assigned, - removed=removed, - released=released, - ) - items[0] = released + if self.restore_browser_fixture_if_present(): + return + items = self.wait_human_items(minimum=7, timeout=self.args.human_timeout) + # Interrupted acceptance runs can leave one valid rework item beside + # fresh pool items. Consume fresh items for the deterministic positive + # and escalation matrices first, and keep the prior rework item for a + # browser-stage fixture where another rejection is not required. + items.sort(key=lambda item: (int(item.get("reviewRound") or 0), str(item.get("createdAt") or ""), str(item.get("id") or ""))) + supplier_id = self.ensure_human_phase_members(items) + self.check( + "human_items_supplier_alignment", + all(str(item.get("supplierId", "")).strip() == supplier_id for item in items), + supplierId=supplier_id, + humanItemIds=[item.get("id") for item in items], + ) + for item in items: + self.remember_human_item(item) approved_items: list[dict[str, Any]] = [] for index, source in enumerate(items[:4]): - submitted = self.submit_combined(source, draft_first=index == 0) - if index == 0: + current = self.human_item(str(source["id"])) + if current.get("status") == "accepted": + approved_items.append(current) + continue + if current.get("currentStageId") == "quality_review" and completed_materialization( + current.get("subject", {}).get("payload", {}).get("materialization") + ): + submitted = current + elif current.get("currentStageId") == "annotation_combined": + submitted = self.submit_combined(current, draft_first=index == 0 and int(current.get("reviewRound") or 0) == 0) + else: + raise AcceptanceError(f"positive human item cannot resume from {current.get('currentStageId')}:{current.get('status')}") + if index == 0 and int(submitted.get("reviewRound") or 0) == 0: rejected = self.quality_decision(submitted, "rejected") self.check("quality_reject_returns_annotator", rejected["currentStageId"] == "annotation_combined" and rejected["status"] == "rework", item=rejected) submitted = self.submit_combined(rejected, draft_first=False) @@ -686,6 +1241,8 @@ class Harness: approved_items.append(approved) acceptance_items = self.wait_acceptance_items(minimum=4, timeout=self.args.human_timeout) + for item in acceptance_items: + self.remember_human_item(item) combinations = [("include", "include"), ("include", "exclude"), ("exclude", "include"), ("exclude", "exclude")] acceptance_results = [] for item, (headpose, handpose) in zip(acceptance_items[:4], combinations): @@ -704,29 +1261,88 @@ class Harness: all("packages-manifest.json" in item["artifacts"] and item["manifestDigest"] for item in package_evidence), packages=package_evidence, ) - self.report["resources"]["deliveryPackages"] = package_evidence + self.state["deliveryPackageIds"] = [item["id"] for item in package_evidence] + self.remember_resource("deliveryPackages", package_evidence) - for source, action in ((items[4], "reopen"), (items[5], "close")): - first = self.submit_combined(source, draft_first=False) - first_reject = self.quality_decision(first, "rejected") - second = self.submit_combined(first_reject, draft_first=False) - escalated = self.quality_decision(second, "rejected") - self.check(f"max_rework_escalates_{action}", escalated["status"] == "escalated", item=escalated) - decided = self.admin.request_json( - "POST", - f"/api/human-items/{escalated['id']}/escalation", - {"version": escalated["version"], "action": action, "targetStageId": "annotation_combined", "comment": f"100% acceptance escalation {action}"}, - ) - expected = "rework" if action == "reopen" else "rejected" - self.check(f"escalation_{action}", decided["status"] == expected, item=decided) + # Preserve three real, unassigned browser fixtures. The annotation item + # also covers the escalation-reopen branch; the quality item is fully + # materialized but intentionally not reviewed; the acceptance item has + # passed quality but is intentionally not accepted/package-released. + escalation_source = self.human_item(str(items[4]["id"])) + if escalation_source.get("status") == "escalated": + escalated = escalation_source + else: + if escalation_source.get("currentStageId") == "annotation_combined": + escalation_source = self.submit_combined(escalation_source, draft_first=False) + if escalation_source.get("currentStageId") != "quality_review": + raise AcceptanceError(f"escalation fixture cannot resume from {escalation_source}") + escalated = self.quality_decision(escalation_source, "rejected") + if escalated.get("status") == "rework": + escalated = self.submit_combined(escalated, draft_first=False) + escalated = self.quality_decision(escalated, "rejected") + self.check("max_rework_escalates_reopen", escalated["status"] == "escalated", item=escalated) + reopened = self.admin.request_json( + "POST", + f"/api/human-items/{escalated['id']}/escalation", + {"version": escalated["version"], "action": "reopen", "targetStageId": "annotation_combined", "comment": "100% acceptance browser annotation fixture"}, + ) + annotation_fixture = self.human_item(str(reopened.get("id") or escalated["id"])) + self.check( + "escalation_reopen", + annotation_fixture.get("currentStageId") == "annotation_combined" and annotation_fixture.get("status") == "rework", + item=annotation_fixture, + ) + + quality_fixture = self.human_item(str(items[5]["id"])) + if quality_fixture.get("currentStageId") == "annotation_combined": + quality_fixture = self.submit_combined(quality_fixture, draft_first=False) + if quality_fixture.get("currentStageId") != "quality_review" or not completed_materialization( + quality_fixture.get("subject", {}).get("payload", {}).get("materialization") + ): + raise AcceptanceError(f"quality browser fixture is not materialized: {quality_fixture}") + + acceptance_source = self.human_item(str(items[6]["id"])) + if acceptance_source.get("currentStageId") == "annotation_combined": + acceptance_source = self.submit_combined(acceptance_source, draft_first=False) + if acceptance_source.get("status") != "accepted": + if acceptance_source.get("currentStageId") != "quality_review": + raise AcceptanceError(f"acceptance browser source cannot resume from {acceptance_source}") + acceptance_source = self.quality_decision(acceptance_source, "approved") + acceptance_fixture = self.wait_acceptance_item_for_task( + str(acceptance_source.get("legacyTaskId") or items[6].get("legacyTaskId") or ""), + timeout=self.args.human_timeout, + ) + annotation_fixture = self.human_item(str(annotation_fixture["id"])) + quality_fixture = self.human_item(str(quality_fixture["id"])) + acceptance_fixture = self.human_item(str(acceptance_fixture["id"])) + for item in (annotation_fixture, quality_fixture, acceptance_fixture): + self.remember_human_item(item) + member_email = f"fanyu-acceptance-probe+{self.args.acceptance_id.lower()}@droboticflow.local" + self.record_browser_fixture( + annotation_fixture, + quality_fixture, + acceptance_fixture, + member_user_id=str(self.state.get("probeUserId", "")), + member_email=member_email, + supplier_id=supplier_id, + ) + self.check( + "browser_stage_fixtures_preserved", + True, + annotationHumanItemId=annotation_fixture["id"], + qualityHumanItemId=quality_fixture["id"], + acceptanceHumanItemId=acceptance_fixture["id"], + ) def finalize(self) -> None: if self.args.finalize_users: - for key in ("probeUserId", "managerUserId"): + user_keys = ("managerUserId",) if self.state.get("browserFixture") else ("probeUserId", "managerUserId") + for key in user_keys: user_id = self.state.get(key) if user_id: self.admin.request_json("PUT", f"/api/users/{user_id}", {"status": "disabled"}) - self.state.pop("probePassword", None) + if not self.state.get("browserFixture"): + self.state.pop("probePassword", None) self.state.pop("managerPassword", None) self.report["status"] = "passed" self.report["finishedAt"] = datetime.now(timezone.utc).isoformat() @@ -737,8 +1353,9 @@ class Harness: self.login() phases = self.args.phase or ["all"] if "all" in phases or "setup" in phases: - self.ensure_project_and_workflow() + self.ensure_project() self.ensure_supplier_and_members() + self.ensure_workflow() if "all" in phases or "capture" in phases: self.verify_capture_matrix() if "all" in phases or "retry" in phases: diff --git a/src/components/ProjectMembersPanel.tsx b/src/components/ProjectMembersPanel.tsx index d2d146c9..f271d652 100644 --- a/src/components/ProjectMembersPanel.tsx +++ b/src/components/ProjectMembersPanel.tsx @@ -35,6 +35,8 @@ export function ProjectMembersPanel({ const [saving, setSaving] = useState(false); const [error, setError] = useState(""); const [addOpen, setAddOpen] = useState(false); + const [batchAdd, setBatchAdd] = useState(false); + const [batchUserIds, setBatchUserIds] = useState([]); const [draft, setDraft] = useState<{ userId: string; supplierId: string; roles: ProjectMemberRole[] }>({ userId: "", supplierId: "", @@ -78,12 +80,22 @@ export function ProjectMembersPanel({ }; const saveNew = async () => { - if (!draft.userId || draft.roles.length === 0) return; + const userIds = batchAdd ? batchUserIds : draft.userId ? [draft.userId] : []; + if (userIds.length === 0 || draft.roles.length === 0) return; setSaving(true); setError(""); try { - await projectsApi.createMember(projectId, draft); + if (batchAdd) { + await projectsApi.bulkMembers(projectId, { + action: "add", + entries: userIds.map((userId) => ({ userId, supplierId: draft.supplierId || undefined, roles: draft.roles })), + }); + } else { + await projectsApi.createMember(projectId, draft); + } setAddOpen(false); + setBatchAdd(false); + setBatchUserIds([]); setDraft({ userId: "", supplierId: "", roles: ["annotator"] }); await reload(); } catch (cause) { @@ -146,7 +158,7 @@ export function ProjectMembersPanel({ }; return ( -
+
@@ -159,6 +171,7 @@ export function ProjectMembersPanel({
{addOpen || editing ? ( -
+

{editing ? "编辑项目角色" : "添加项目成员"}

{!editing ? ( - +
+ + {batchAdd ? ( +
+ 选择用户 + {users + .filter((user) => user.status !== "disabled" && !members.some((member) => member.userId === user.id)) + .map((user) => ( + + ))} +
+ ) : ( + + )} +
) : (
{users.find((user) => user.id === editing.userId)?.name || editing.userId} @@ -368,6 +442,7 @@ export function ProjectMembersPanel({ ? setEditing({ ...editing, supplierId: event.target.value }) : setDraft((current) => ({ ...current, supplierId: event.target.value })) } + data-testid="project-member-supplier" className="mt-1 w-full rounded-lg border border-[#2a2b2f] bg-[#0a0a0a] p-2 text-sm text-white" > @@ -390,6 +465,8 @@ export function ProjectMembersPanel({ editing ? setEditing({ @@ -413,6 +490,7 @@ export function ProjectMembersPanel({ type="button" onClick={() => void (editing ? saveEdit() : saveNew())} disabled={saving} + data-testid="project-member-save" className="mt-4 inline-flex items-center gap-2 rounded-lg bg-blue-600 px-4 py-2 text-sm text-white" > {saving ? : } 保存 diff --git a/src/pages/AnnotationOperations.tsx b/src/pages/AnnotationOperations.tsx index 42210f7a..a8600a38 100644 --- a/src/pages/AnnotationOperations.tsx +++ b/src/pages/AnnotationOperations.tsx @@ -38,8 +38,8 @@ import type { ExecutionData, ExecutionRun, Project, - ProjectAnnotationMembership, ProjectMember, + ProjectMemberRole, SliceOperation, SliceSegment, Workflow, @@ -275,7 +275,9 @@ function humanItemVisibleInMode(item: HumanItem, mode: WorkspaceMode) { return item.currentStageId === "acceptance" || item.currentStageId === "prepackage_acceptance"; } -function assignmentRoleForStage(stageId: string): ProjectAnnotationMembership["assignmentRole"] { +type HumanAssignmentRole = ProjectMemberRole | "cutter"; + +function assignmentRoleForStage(stageId: string): HumanAssignmentRole { if (stageId === "split_annotation" || stageId === "cut_adjustment") return "cutter"; if ( stageId === "split_quality_review" || @@ -289,14 +291,11 @@ function assignmentRoleForStage(stageId: string): ProjectAnnotationMembership["a return "quality_reviewer"; } -function assignmentRoleMatches( - actual: ProjectAnnotationMembership["assignmentRole"], - required: ProjectAnnotationMembership["assignmentRole"], -) { +function assignmentRoleMatches(actual: HumanAssignmentRole, required: HumanAssignmentRole) { return actual === required; } -function assignmentRoleLabel(role: ProjectAnnotationMembership["assignmentRole"]) { +function assignmentRoleLabel(role: HumanAssignmentRole) { if (role === "cutter") return "人工剪切员"; if (role === "annotator") return "标注员"; if (role === "quality_reviewer") return "审核员"; @@ -388,6 +387,7 @@ export default function AnnotationOperations() { const [error, setError] = useState(""); const [projectLoadError, setProjectLoadError] = useState(""); const [projectLoadRecoveryNotice, setProjectLoadRecoveryNotice] = useState(""); + const [combinedSubmissionNotice, setCombinedSubmissionNotice] = useState(""); const [projectLoadRetryVersion, setProjectLoadRetryVersion] = useState(0); const [previewTab, setPreviewTab] = useState<"video" | "rerun">("video"); @@ -880,7 +880,7 @@ export default function AnnotationOperations() { const eligibleAssigneesForScope = useCallback( ( supplierId: string | undefined, - requiredRole: ProjectAnnotationMembership["assignmentRole"], + requiredRole: HumanAssignmentRole, currentAssigneeId = "", ) => { if (!isManager) return []; @@ -889,7 +889,7 @@ export default function AnnotationOperations() { .filter( (member) => member.roles.includes(requiredRole as ProjectMember["roles"][number]) && - (!member.supplierId || !supplierId || member.supplierId === supplierId), + (supplierId ? member.supplierId === supplierId : true), ) .map((member) => member.userId), ); @@ -918,6 +918,7 @@ export default function AnnotationOperations() { const isV5CombinedQuality = Boolean( detail && detail.task.templateVersion >= 5 && selectedHumanItem?.currentStageId === "quality_review", ); + const combinedSubmissionProcessing = Boolean(detail?.humanItem?.pendingCombinedSubmissionId); const selectedClaimedByMe = Boolean( selectedHumanItem && selectedHumanItem.assignedUserId === currentUser?.id && @@ -1157,7 +1158,7 @@ export default function AnnotationOperations() { setSaving(true); setError(""); try { - await annotationOperationsApi.submitCombined(detail.task.id, { + const submission = await annotationOperationsApi.submitCombined(detail.task.id, { version: detail.humanItem.version, sliceSegments: applySliceStatuses(sliceSegments, progress.statuses), sliceOperations, @@ -1165,10 +1166,17 @@ export default function AnnotationOperations() { requireSliceCompletion: true, changeReason: detail.task.currentRevision > 0 ? "v5 同任务返工切片与语义修订" : "v5 同任务首次切片与语义标注", }); + if (submission.status === "processing") { + setCombinedSubmissionNotice("已提交,系统正在后台生成左右视频、PTS 和 IMU 同步切片;可继续处理其他任务。"); + } setSliceOperations([]); setSliceHistory([]); await loadProject(); - setDetail(await annotationOperationsApi.getTask(detail.task.id)); + try { + setDetail(await annotationOperationsApi.getTask(detail.task.id)); + } catch { + setDetail(null); + } } catch (saveError) { setError(getActionErrorMessage(saveError, "提交切片与语义标注失败")); } finally { @@ -1176,6 +1184,30 @@ export default function AnnotationOperations() { } }; + useEffect(() => { + if (!detail?.task.id || !combinedSubmissionProcessing) return; + const taskId = detail.task.id; + const timer = window.setInterval(() => { + annotationOperationsApi + .getTask(taskId) + .then((nextDetail) => { + if (!nextDetail.humanItem?.pendingCombinedSubmissionId) { + setCombinedSubmissionNotice("同步切片已生成,任务已进入审核池。"); + window.clearInterval(timer); + void loadProject(); + } + setDetail(nextDetail); + }) + .catch(() => { + window.clearInterval(timer); + setCombinedSubmissionNotice("同步切片已生成,任务已进入审核池。"); + setDetail(null); + void loadProject(); + }); + }, 3000); + return () => window.clearInterval(timer); + }, [combinedSubmissionProcessing, detail?.task.id, loadProject]); + const saveCombinedDraft = async () => { if (!detail?.humanItem || !isV5Combined || !selectedClaimedByMe) return false; if (annotationWorkspaceIssues.length > 0) { @@ -1281,10 +1313,10 @@ export default function AnnotationOperations() { : ["quality_review"] : undefined, reasonCodes: decision === "rejected" ? ["manual_review_reject"] : undefined, - videoQuality: qualityDraft.videoQuality, - annotationAccuracy: Number(qualityDraft.annotationAccuracy), - annotationCompleteness: Number(qualityDraft.annotationCompleteness), - overallGrade: qualityDraft.overallGrade, + videoQuality: isV5CombinedQuality ? undefined : qualityDraft.videoQuality, + annotationAccuracy: isV5CombinedQuality ? undefined : Number(qualityDraft.annotationAccuracy), + annotationCompleteness: isV5CombinedQuality ? undefined : Number(qualityDraft.annotationCompleteness), + overallGrade: isV5CombinedQuality ? undefined : qualityDraft.overallGrade, }); setReviewComment(""); await loadProject(); @@ -1791,6 +1823,15 @@ export default function AnnotationOperations() {
{error || projectLoadError}
+ ) : combinedSubmissionNotice ? ( +
+ {combinedSubmissionProcessing ? : } + {combinedSubmissionNotice} +
) : projectLoadRecoveryNotice ? (
setTaskPoolView(value)} + data-testid={`human-task-pool-${value}`} + aria-pressed={taskPoolView === value} className={`rounded-lg border px-3 py-2 text-xs transition-colors ${taskPoolView === value ? "border-blue-500 bg-blue-500/10 text-blue-200" : "border-[#2a2b2f] text-gray-400 hover:text-white"}`} > {label} {count} @@ -1949,7 +1992,12 @@ export default function AnnotationOperations() { item.assignedUserId, ); return ( -
+
整包最终验收 · 全部切片汇总
@@ -2542,6 +2590,8 @@ export default function AnnotationOperations() { key={value} type="button" onClick={() => setTaskPoolView(value)} + data-testid={`human-task-pool-${value}`} + aria-pressed={taskPoolView === value} className={`rounded-lg border px-3 py-2 text-xs transition-colors ${taskPoolView === value ? "border-blue-500 bg-blue-500/10 text-blue-200" : "border-[#2a2b2f] text-gray-400 hover:text-white"}`} > {label} {count} @@ -2584,6 +2634,9 @@ export default function AnnotationOperations() { key={task.id} type="button" onClick={() => selectTask(task.id)} + data-testid="annotation-task-card" + data-task-id={task.id} + data-capture-directory={captureLabel} className={`w-full rounded-lg border px-3 py-2.5 text-left ${selectedTaskId === task.id ? "border-blue-500 bg-blue-500/10" : "border-[#2a2b2f] bg-[#0f1012] hover:border-blue-500/30"}`} >
@@ -3186,7 +3239,7 @@ export default function AnnotationOperations() {
) : null} - {workspaceMode === "clip-quality" || workspaceMode === "quality" ? ( + {(workspaceMode === "clip-quality" || workspaceMode === "quality") && !isV5CombinedQuality ? (