🐛 Preserve submitted history when changing annotator roles

This commit is contained in:
Codex 2026-11-12 19:03:21 -08:00
parent 8b33f38e59
commit 5c6023ecd3
9 changed files with 359 additions and 25 deletions

View File

@ -31,6 +31,7 @@ MongoDB 通过数据库持久互斥保护供应商变更与所属账号创建;
- `set_roles` 批量操作明确拒绝;角色变更从账号管理进行。
- API 标注/质检/验收权限、随机领取、普通领取/任务分配,以及 Worker 的供应商生产角色覆盖检查,都依据账号唯一角色。旧项目角色数组不能增加权限。
- 有未完成的已领取人工任务或关联标注任务时拒绝修改角色。MongoDB 角色变更、普通领取与随机领取使用同一个账号级持久互斥;支持事务时共同写账号文档,避免“检查后又被领取”的竞态。
- 标注员已送检的旧标注记录处于 `pending_quality` 或 `pending_acceptance` 时,保留原领取人作为历史归属,不再据此阻止标注员修改角色。本人仍领取的人工任务、切片失败、待标注、返工及状态不明的旧任务继续阻断;切换为审核员后仍不能审核本人提交的数据。此规则只调整角色变更判断,不清空领取字段、草稿、版本或审核记录。
- `defaultProjectRoles` 和旧项目 `roles` 保留用于历史审计,不再作为额外授权,不自动覆写历史。
## 历史检查与上线边界

View File

@ -30,7 +30,7 @@ func (s *server) ensureRoleChangeIdle(w http.ResponseWriter, r *http.Request, us
return false
}
for _, task := range tasks {
if task.AssignedUserID == user.ID && task.Status != domain.AnnotationTaskStatusAccepted && task.Status != domain.AnnotationTaskStatusRejected {
if task.AssignedUserID == user.ID && domain.AnnotationTaskBlocksRoleChange(task.Status, user.Role) {
renderJSON(w, 409, map[string]string{"code": "account_has_pending_tasks", "error": "该账号有关联标注任务尚未完成,请先处理任务"})
return false
}

View File

@ -0,0 +1,95 @@
package api
import (
"droboticflow/internal/domain"
"droboticflow/internal/store"
"errors"
"net/http"
"reflect"
"testing"
)
func TestRoleChangePreservesSubmittedAnnotationHistory(t *testing.T) {
for _, status := range []domain.AnnotationTaskStatus{domain.AnnotationTaskStatusPendingQuality, domain.AnnotationTaskStatusPendingAcceptance} {
t.Run(string(status), func(t *testing.T) {
f := newCombinedDraftFixture(t)
task := f.task
task.Status, task.AssignedUserID = status, f.annotator.ID
if err := f.store.UpsertAnnotationTask(f.ctx, task); err != nil {
t.Fatal(err)
}
item := f.item
item.CurrentStageID, item.Status, item.AssignedUserID = "quality_review", domain.HumanItemReady, ""
item.LatestAuthorUserID = f.annotator.ID
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
t.Fatal(err)
}
review := domain.AnnotationReviewRecord{ID: "retained-review", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewRejected, ReviewerUserID: f.reviewer.ID}
if err := f.store.UpsertAnnotationReview(f.ctx, review); err != nil {
t.Fatal(err)
}
beforeTask, _ := f.store.GetAnnotationTask(f.ctx, task.ID)
beforeItem, _ := f.store.GetHumanItem(f.ctx, item.ID)
beforeReviews, _ := f.store.ListAnnotationReviews(f.ctx, task.OrganizationID, task.ID)
r := f.request(http.MethodPut, "/api/users/"+f.annotator.ID, f.admin, map[string]any{"role": "quality_reviewer"})
if r.Code != http.StatusOK {
t.Fatalf("submitted history blocks role change: %d %s", r.Code, r.Body)
}
afterTask, _ := f.store.GetAnnotationTask(f.ctx, task.ID)
afterItem, _ := f.store.GetHumanItem(f.ctx, item.ID)
afterReviews, _ := f.store.ListAnnotationReviews(f.ctx, task.OrganizationID, task.ID)
if !reflect.DeepEqual(beforeTask, afterTask) || !reflect.DeepEqual(beforeItem, afterItem) || !reflect.DeepEqual(beforeReviews, afterReviews) {
t.Fatal("role change rewrote work or audit history")
}
promoted, err := f.store.GetUser(f.ctx, f.annotator.ID)
if err != nil || promoted == nil {
t.Fatal("updated account unavailable", err)
}
claim := f.request(http.MethodPost, "/api/human-items/"+item.ID+"/claim", *promoted, map[string]any{"version": item.Version})
if claim.Code != http.StatusForbidden {
t.Fatalf("promotion allowed review of own output: %d %s", claim.Code, claim.Body)
}
})
}
}
func TestRoleChangeStillRejectsOwnedAndLegacyUnfinishedWork(t *testing.T) {
for _, kind := range []string{"claimed", "failed-slice", "legacy-pending", "legacy-rework", "legacy-unknown"} {
t.Run(kind, func(t *testing.T) {
f := newCombinedDraftFixture(t)
task := f.task
task.AssignedUserID = f.annotator.ID
task.Status = domain.AnnotationTaskStatusPendingQuality
item := f.item
if kind == "failed-slice" {
item.ProcessingJob = &domain.AnnotationProcessingJob{Kind: "slice", Status: "failed", Error: "retained failure"}
}
if kind == "legacy-pending" || kind == "legacy-rework" || kind == "legacy-unknown" {
item.Status = domain.HumanItemAccepted
item.AssignedUserID = ""
task.Status = domain.AnnotationTaskStatusPendingAnnotation
if kind == "legacy-rework" {
task.Status = domain.AnnotationTaskStatusRework
}
if kind == "legacy-unknown" {
task.Status = "unknown-legacy-state"
}
}
if e := f.store.UpsertAnnotationTask(f.ctx, task); e != nil {
t.Fatal(e)
}
if e := f.store.UpsertHumanItem(f.ctx, item); e != nil {
t.Fatal(e)
}
r := f.request(http.MethodPut, "/api/users/"+f.annotator.ID, f.admin, map[string]any{"role": "quality_reviewer"})
if r.Code != 409 {
t.Fatalf("unfinished work accepted: %d %s", r.Code, r.Body)
}
next := f.annotator
next.Role = domain.UserRoleQualityReviewer
if e := f.store.UpdateUser(f.ctx, next, f.annotator); !errors.Is(e, store.ErrAccountPendingTasks) {
t.Fatalf("store bypassed pending guard: %v", e)
}
})
}
}

View File

@ -0,0 +1,108 @@
package api
import (
"context"
"droboticflow/internal/domain"
"net/http"
"reflect"
"testing"
"time"
)
// Reproduces the incident shape without copying customer annotations/media:
// an old cadence failure, an older five-slice workspace and a newer two-slice
// owned draft. Reconfirmation must use the draft, not an empty/old snapshot.
func TestS07SavedSliceRecoveryPreservesNewerDraftAcrossRestart(t *testing.T) {
t.Setenv("ANNOTATION_SOURCE_VALIDATION_MODE", "advisory")
t.Setenv("ANNOTATION_SOURCE_INDEX", "true")
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
f, root := annotationProcessorFixture(t)
makeCadenceAuditFailure(t, root)
engine := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true, externalAnnotationProcessor: true}
timeline, err := engine.annotationFrameTimelineForTask(f.ctx, f.task, "left")
if err != nil {
t.Fatal(err)
}
points := timeline["boundariesSeconds"].([]float64)
end, middle := points[len(points)-1], points[len(points)/2]
h := seedFailedSliceRetry(t, f)
old := mapValue(h.Subject.Payload["annotationWorkspace"])
var oldSlices []domain.SliceSegment
for i := 0; i < 5; i++ {
oldSlices = append(oldSlices, domain.SliceSegment{ID: domain.NewID("old-slice"), Name: "测试切片", Start: end * float64(i) / 5, End: end * float64(i+1) / 5})
}
old["sliceSegments"] = oldSlices
h.Subject.Payload["annotationWorkspace"] = old
draft := map[string]any{"sliceSegments": []domain.SliceSegment{{ID: "new-a", Name: "测试切片", Start: 0, End: middle}, {ID: "new-b", Name: "测试切片", Start: middle, End: end}}, "segments": []domain.AnnotationSegment{{ID: "new-action-a", Start: 0, End: middle, Status: "pending"}, {ID: "new-action-b", Start: middle, End: end, Status: "pending"}}, "userId": f.annotator.ID, "savedAt": nowISO(), "savedVersion": h.Version}
h.Subject.Payload["annotationDraft"] = draft
h.ProcessingJob = &domain.AnnotationProcessingJob{ID: "old-cadence-failure", Kind: "slice", Status: "failed", Error: "source video duration does not match its fixed frame cadence"}
if err = f.store.UpsertHumanItem(f.ctx, h); err != nil {
t.Fatal(err)
}
stored, err := f.store.GetHumanItem(f.ctx, h.ID)
if err != nil || stored == nil {
t.Fatal("saved draft unavailable", err)
}
draft = mapValue(stored.Subject.Payload["annotationDraft"])
review := domain.AnnotationReviewRecord{ID: "retained-history", OrganizationID: h.OrganizationID, TaskID: f.task.ID, Stage: domain.AnnotationReviewStageQuality, ReviewerUserID: f.reviewer.ID, Decision: domain.AnnotationReviewRejected, Revision: 0}
if err = f.store.UpsertAnnotationReview(f.ctx, review); err != nil {
t.Fatal(err)
}
beforeReviews, _ := f.store.ListAnnotationReviews(f.ctx, h.OrganizationID, f.task.ID)
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-confirmations", f.annotator, map[string]any{"version": h.Version, "reconfirmSaved": true})
if r.Code != http.StatusAccepted {
t.Fatalf("saved-content recovery blocked: %d %s", r.Code, r.Body)
}
queued, _ := f.store.GetHumanItem(f.ctx, h.ID)
if !reflect.DeepEqual(mapValue(queued.Subject.Payload["annotationDraft"]), draft) {
t.Fatal("queue replaced current draft")
}
now := time.Now().UTC()
_, err = f.store.ClaimAnnotationProcessing(f.ctx, "interrupted", now.Format(time.RFC3339Nano), now.Add(-time.Second).Format(time.RFC3339Nano))
if err != nil {
t.Fatal(err)
}
claimed, err := f.store.ClaimAnnotationProcessing(f.ctx, "restarted", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
if err != nil || claimed == nil {
t.Fatal("durable retry unavailable", err)
}
engine.executeAnnotationJob(context.Background(), *claimed)
done, _ := f.store.GetHumanItem(f.ctx, h.ID)
if done.ProcessingJob.Status != "completed" {
t.Fatalf("cadence recovery failed: %+v", done.ProcessingJob)
}
workspace := annotationWorkspacePayload(done)
actualSlices := decodeMaterializationValue[domain.SliceSegment](workspace["sliceSegments"])
savedSlices := decodeMaterializationValue[domain.SliceSegment](draft["sliceSegments"])
if len(actualSlices) != len(savedSlices) {
t.Fatal("recovery used older slice structure")
}
for i, saved := range savedSlices {
actual := actualSlices[i]
// Generation enriches dictionary/frame identities and pending status;
// it must not move the user's current boundaries or replace slice IDs.
if actual.ID != saved.ID || actual.Name != saved.Name || actual.Start != saved.Start || actual.End != saved.End {
t.Fatalf("saved slice changed: %+v -> %+v", saved, actual)
}
}
if !reflect.DeepEqual(decodeMaterializationValue[domain.AnnotationSegment](workspace["segments"]), decodeMaterializationValue[domain.AnnotationSegment](draft["segments"])) {
t.Fatal("recovery changed saved actions")
}
afterReviews, _ := f.store.ListAnnotationReviews(f.ctx, h.OrganizationID, f.task.ID)
if !reflect.DeepEqual(beforeReviews, afterReviews) {
t.Fatal("recovery changed formal review history")
}
events, err := f.store.ListHumanEvents(f.ctx, h.OrganizationID, h.ID)
if err != nil {
t.Fatal(err)
}
archived := false
for _, e := range events {
if e.EventType == "slice_reconfirmation_snapshot_prepared" && len(listValue(mapValue(e.Payload["previousWorkspace"])["sliceSegments"])) == 5 {
archived = true
}
}
if !archived {
t.Fatal("older workspace was not retained as recovery evidence")
}
}

View File

@ -0,0 +1,19 @@
package domain
import "slices"
// Submitted annotation tasks retain their original annotator for attribution.
// That historical identity is not an outstanding assignment to the annotator.
// Live HumanItem assignments are checked separately, including failed jobs and
// work returned for rework. Unknown legacy states remain blocking.
func AnnotationRoleChangeNonBlockingStatuses(role UserRole) []AnnotationTaskStatus {
statuses := []AnnotationTaskStatus{AnnotationTaskStatusAccepted, AnnotationTaskStatusRejected}
if role == UserRoleAnnotator {
statuses = append(statuses, AnnotationTaskStatusPendingQuality, AnnotationTaskStatusPendingAcceptance)
}
return statuses
}
func AnnotationTaskBlocksRoleChange(status AnnotationTaskStatus, role UserRole) bool {
return !slices.Contains(AnnotationRoleChangeNonBlockingStatuses(role), status)
}

View File

@ -34,7 +34,7 @@ func (s *MongoStore) updateAccountRole(ctx context.Context, item, expected domai
if n > 0 {
return nil, ErrAccountPendingTasks
}
n, e = s.annotationTasks.CountDocuments(sc, bson.M{"assignedUserId": item.ID, "status": bson.M{"$nin": bson.A{domain.AnnotationTaskStatusAccepted, domain.AnnotationTaskStatusRejected}}})
n, e = s.annotationTasks.CountDocuments(sc, bson.M{"assignedUserId": item.ID, "status": bson.M{"$nin": domain.AnnotationRoleChangeNonBlockingStatuses(expected.Role)}})
if e != nil {
return nil, e
}

View File

@ -0,0 +1,88 @@
package store
import (
"context"
"droboticflow/internal/domain"
"errors"
"fmt"
"os"
"reflect"
"testing"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func TestAccountRoleHandoffMongo(t *testing.T) {
if os.Getenv("ACCOUNT_ROLE_LOCAL_MONGO") != "1" {
t.Skip("isolated loopback Mongo opt-in")
}
ctx, cancel := context.WithTimeout(t.Context(), 60*time.Second)
defer cancel()
c, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018/?directConnection=true").SetServerSelectionTimeout(3*time.Second))
if err != nil {
t.Fatal(err)
}
defer c.Disconnect(context.Background())
db := c.Database(fmt.Sprintf("account_role_handoff_%d", time.Now().UnixNano()))
defer db.Drop(context.Background())
s := NewMongoStore(db)
for i, scenario := range []struct {
name string
status domain.AnnotationTaskStatus
active bool
allow bool
}{
{"submitted-quality", domain.AnnotationTaskStatusPendingQuality, false, true},
{"submitted-acceptance", domain.AnnotationTaskStatusPendingAcceptance, false, true},
{"pending", domain.AnnotationTaskStatusPendingAnnotation, false, false},
{"rework", domain.AnnotationTaskStatusRework, false, false},
{"unknown", "unknown", false, false},
{"owned-human-task", domain.AnnotationTaskStatusPendingQuality, true, false},
} {
t.Run(scenario.name, func(t *testing.T) {
old := domain.UserRecord{ID: fmt.Sprintf("worker-%d", i), OrganizationID: "test-org", SupplierID: "test-supplier", Role: domain.UserRoleAnnotator, Status: domain.UserStatusActive, Phone: fmt.Sprintf("13900009%03d", i)}
if _, err := s.users.InsertOne(ctx, old); err != nil {
t.Fatal(err)
}
task := domain.AnnotationTaskRecord{ID: fmt.Sprintf("task-%d", i), OrganizationID: old.OrganizationID, AssignedUserID: old.ID, Status: scenario.status}
if _, err := s.annotationTasks.InsertOne(ctx, task); err != nil {
t.Fatal(err)
}
if scenario.active {
if _, err := s.humanItems.InsertOne(ctx, domain.HumanItemRecord{ID: "owned", OrganizationID: old.OrganizationID, AssignedUserID: old.ID, Status: domain.HumanItemClaimed}); err != nil {
t.Fatal(err)
}
}
var before bson.M
if err := s.annotationTasks.FindOne(ctx, bson.M{"id": task.ID}).Decode(&before); err != nil {
t.Fatal(err)
}
next := old
next.Role = domain.UserRoleQualityReviewer
err := s.UpdateUser(ctx, next, old)
if scenario.allow && err != nil {
t.Fatalf("submitted attribution blocked: %v", err)
}
if !scenario.allow && !errors.Is(err, ErrAccountPendingTasks) {
t.Fatalf("unfinished work not fenced: %v", err)
}
var after bson.M
if err := s.annotationTasks.FindOne(ctx, bson.M{"id": task.ID}).Decode(&after); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(before, after) {
t.Fatal("role mutation changed historical task")
}
stored, err := s.GetUser(ctx, old.ID)
if err != nil {
t.Fatal(err)
}
if (stored.Role == domain.UserRoleQualityReviewer) != scenario.allow {
t.Fatal("role state inconsistent with result")
}
})
}
}

View File

@ -2403,7 +2403,7 @@ func (store *MemoryStore) upsertUser(_ context.Context, item domain.UserRecord,
}
}
for _, task := range store.annotationTasks {
if task.AssignedUserID == item.ID && task.Status != domain.AnnotationTaskStatusAccepted && task.Status != domain.AnnotationTaskStatusRejected {
if task.AssignedUserID == item.ID && domain.AnnotationTaskBlocksRoleChange(task.Status, expected.Role) {
return ErrAccountPendingTasks
}
}

View File

@ -1,6 +1,45 @@
{
"version": 1,
"scopes": [
{
"name": "account-role-handoff",
"paths": [
"^internal/domain/account_role_work\\.go$",
"^internal/api/account_(hierarchy|role_handoff_test)\\.go$",
"^internal/store/account_role_(change|handoff_mongo_test)\\.go$",
"^internal/api/s07_saved_slice_recovery_test\\.go$"
],
"frontend": [],
"go": {
"./internal/api": [
"^TestRoleChange",
"^TestAccountHierarchySupplierLifecycleAndSingleRole$",
"^TestS07SavedSliceRecovery",
"^TestReconfirmSavedSlices",
"^TestFailedSliceRetry"
],
"./internal/store": [
"^TestAccountUpdateRejectsStaleAuthorization$",
"^TestConcurrentLastAdministratorProtection$"
]
},
"task": [
{
"argv": [
"go",
"test",
"./internal/store",
"-run",
"^TestAccountRoleHandoffMongo$",
"-count=1",
"-timeout=90s",
"-v"
],
"env": { "ACCOUNT_ROLE_LOCAL_MONGO": "1" },
"tests": true
}
]
},
{
"name": "legacy-v4-release",
"paths": [
@ -900,28 +939,14 @@
],
"frontend": [],
"go": {
"./internal/queryserving": [
"^TestQueryServing"
],
"./internal/store": [
"^TestQueryReadModel",
"^TestQuerySearch"
],
"./internal/outputsnapshot": [
"^TestQueryServing"
],
"./internal/api": [
"^TestQueryServing"
]
"./internal/queryserving": ["^TestQueryServing"],
"./internal/store": ["^TestQueryReadModel", "^TestQuerySearch"],
"./internal/outputsnapshot": ["^TestQueryServing"],
"./internal/api": ["^TestQueryServing"]
},
"task": [
{
"argv": [
"python3",
"scripts/test_query_serving.py",
"--start",
"--race"
],
"argv": ["python3", "scripts/test_query_serving.py", "--start", "--race"],
"tests": true,
"env": {}
},
@ -939,9 +964,7 @@
"tests": true
}
],
"warnings": [
"真实三组件仅使用固定本机合成夹具;完整规模三轮、故障恢复容量及生产部署独立验收。"
]
"warnings": ["真实三组件仅使用固定本机合成夹具;完整规模三轮、故障恢复容量及生产部署独立验收。"]
}
]
}