forked from longtao.wu/droboticflow
285 lines
11 KiB
Go
285 lines
11 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"reflect"
|
|
)
|
|
|
|
func (s *MemoryStore) ClearReworkRoutingPending(_ context.Context, id string, version int) (bool, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
item, ok := s.humanItems[id]
|
|
if !ok || item.Version != version {
|
|
return false, nil
|
|
}
|
|
item.ReworkRoutingPending = false
|
|
s.humanItems[id] = item
|
|
return true, nil
|
|
}
|
|
func (s *MongoStore) ClearReworkRoutingPending(ctx context.Context, id string, version int) (bool, error) {
|
|
r, err := s.humanItems.UpdateOne(ctx, bson.M{"id": id, "version": version}, bson.M{"$set": bson.M{"reworkRoutingPending": false}})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return r.MatchedCount == 1, nil
|
|
}
|
|
|
|
// A reset never reuses a generation number or deletes formal audit history.
|
|
// Old jobs may finish, but cannot publish a task from an earlier editing epoch.
|
|
func (s *MemoryStore) ResetDeletedAnnotatorTask(_ context.Context, task domain.AnnotationTaskRecord) error {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
current, exists := s.annotationTasks[task.ID]
|
|
if exists {
|
|
if current.AnnotationEpoch > task.AnnotationEpoch {
|
|
return ErrHumanClaimConflict
|
|
}
|
|
task.AnnotationRevisionFloor = max(task.AnnotationRevisionFloor, current.AnnotationRevisionFloor, current.CurrentRevision)
|
|
}
|
|
for _, revision := range s.annotationRevisions {
|
|
if revision.TaskID == task.ID {
|
|
task.AnnotationRevisionFloor = max(task.AnnotationRevisionFloor, revision.Revision)
|
|
}
|
|
}
|
|
for _, revision := range s.sliceRevisions {
|
|
if revision.TaskID == task.ID {
|
|
task.AnnotationRevisionFloor = max(task.AnnotationRevisionFloor, revision.Revision)
|
|
}
|
|
}
|
|
if !exists || current.AnnotationEpoch < task.AnnotationEpoch {
|
|
if err := s.upsertAnnotationTaskLocked(task); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
for id, revision := range s.annotationRevisions {
|
|
if revision.TaskID == task.ID && revision.AnnotationEpoch < task.AnnotationEpoch {
|
|
revision.SupersededByAccountDeletion = true
|
|
s.annotationRevisions[id] = revision
|
|
}
|
|
}
|
|
for id, revision := range s.sliceRevisions {
|
|
if revision.TaskID == task.ID && revision.AnnotationEpoch < task.AnnotationEpoch {
|
|
revision.SupersededByAccountDeletion = true
|
|
s.sliceRevisions[id] = revision
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *MongoStore) ResetDeletedAnnotatorTask(ctx context.Context, task domain.AnnotationTaskRecord) error {
|
|
_, err := s.withSupplierRecordWriteLock(ctx, task.ProjectID, "annotationTasks", task.ID, func(ctx context.Context) (any, error) {
|
|
current, err := s.GetAnnotationTask(ctx, task.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if current != nil {
|
|
if current.AnnotationEpoch > task.AnnotationEpoch {
|
|
return nil, ErrHumanClaimConflict
|
|
}
|
|
task.AnnotationRevisionFloor = max(task.AnnotationRevisionFloor, current.AnnotationRevisionFloor, current.CurrentRevision)
|
|
}
|
|
filter := bson.M{"organizationId": normalizedOrganizationID(task.OrganizationID), "taskId": task.ID}
|
|
for _, col := range []mongoCollection{s.annotationRevisions, s.sliceRevisions} {
|
|
var last struct {
|
|
Revision int `bson:"revision"`
|
|
}
|
|
err := col.FindOne(ctx, filter, options.FindOne().SetSort(bson.D{{Key: "revision", Value: -1}}).SetProjection(bson.M{"revision": 1})).Decode(&last)
|
|
if err != nil && err != mongo.ErrNoDocuments {
|
|
return nil, err
|
|
}
|
|
task.AnnotationRevisionFloor = max(task.AnnotationRevisionFloor, last.Revision)
|
|
}
|
|
// A second recoverer may arrive after release and after the next
|
|
// annotator has saved. Replaying this epoch must not rewrite their task.
|
|
if current == nil || current.AnnotationEpoch < task.AnnotationEpoch {
|
|
task.OrganizationID = normalizedOrganizationID(task.OrganizationID)
|
|
task.UpdatedAt = nowISO()
|
|
if err := upsertAnnotationEpoch(ctx, s.annotationTasks, task.ID, task, task.AnnotationEpoch); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
filter["$or"] = bson.A{bson.M{"annotationEpoch": bson.M{"$lt": task.AnnotationEpoch}}, bson.M{"annotationEpoch": bson.M{"$exists": false}}}
|
|
for _, col := range []mongoCollection{s.annotationRevisions, s.sliceRevisions} {
|
|
if _, err := col.UpdateMany(ctx, filter, bson.M{"$set": bson.M{"supersededByAccountDeletion": true}}); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nil, nil
|
|
})
|
|
return err
|
|
}
|
|
|
|
func upsertAnnotationEpoch(ctx context.Context, col mongoCollection, id string, value any, epoch int) error {
|
|
_, err := upsertAnnotationEpochInserted(ctx, col, id, value, epoch)
|
|
return err
|
|
}
|
|
|
|
// Use the write result, rather than an unlocked existence read: concurrent
|
|
// projection updates must not be mistaken for task creation.
|
|
func upsertAnnotationEpochInserted(ctx context.Context, col mongoCollection, id string, value any, epoch int) (bool, error) {
|
|
inserted := false
|
|
err := supplierGuardedMongoWrite(ctx, col, id, value, func(ctx context.Context, d bson.M) error {
|
|
result, err := col.ReplaceOne(ctx, bson.M{"id": id, "$or": bson.A{bson.M{"annotationEpoch": bson.M{"$lte": epoch}}, bson.M{"annotationEpoch": bson.M{"$exists": false}}}}, d, options.Replace().SetUpsert(true))
|
|
if mongo.IsDuplicateKeyError(err) {
|
|
return ErrHumanClaimConflict
|
|
}
|
|
if err == nil {
|
|
inserted = result.UpsertedCount == 1
|
|
}
|
|
return err
|
|
})
|
|
return inserted, err
|
|
}
|
|
|
|
func (s *MongoStore) upsertActiveAnnotationRevision(ctx context.Context, col mongoCollection, id, taskID string, revision, epoch int, value any) error {
|
|
return supplierGuardedMongoWrite(ctx, col, id, value, func(ctx context.Context, d bson.M) error {
|
|
// This read and write hold the same task stripe as reset; a pre-lock
|
|
// parent read alone would leave a check/write race.
|
|
task, err := s.GetAnnotationTask(ctx, taskID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if task != nil && (epoch != task.AnnotationEpoch || (task.AnnotationEpoch > 0 && revision <= task.AnnotationRevisionFloor)) {
|
|
return ErrHumanClaimConflict
|
|
}
|
|
_, err = col.ReplaceOne(ctx, bson.M{"id": id, "supersededByAccountDeletion": bson.M{"$ne": true}}, d, options.Replace().SetUpsert(true))
|
|
if mongo.IsDuplicateKeyError(err) {
|
|
return ErrHumanClaimConflict
|
|
}
|
|
return err
|
|
})
|
|
}
|
|
|
|
// Start an inherited rework from the frozen reviewed version, never another
|
|
// account's private, unfinished draft. The baseline and review records remain.
|
|
func applyInheritedReworkWorkspace(item *domain.HumanItemRecord, baseline domain.ReworkBaselineRecord, user string) {
|
|
item.Subject = clone(item.Subject)
|
|
if item.Subject.Payload == nil {
|
|
item.Subject.Payload = map[string]any{}
|
|
}
|
|
delete(item.Subject.Payload, "annotationDraft")
|
|
statuses := map[string]domain.SliceAnnotationStatus{}
|
|
for _, slice := range baseline.Slices {
|
|
status := slice.AnnotationStatus
|
|
if status == "" {
|
|
status = "pending"
|
|
}
|
|
statuses[slice.ID] = status
|
|
}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": baseline.Slices, "segments": baseline.Actions, "operations": []domain.SliceOperation{}, "statuses": statuses, "userId": user, "savedAt": nowISO()}
|
|
}
|
|
|
|
func inheritedBaselineFromRecords(item domain.HumanItemRecord, saved *domain.ReworkBaselineRecord, annotations []domain.AnnotationRevisionRecord, slices []domain.SliceRevisionRecord) (*domain.ReworkBaselineRecord, error) {
|
|
if saved != nil {
|
|
if saved.OrganizationID != item.OrganizationID || saved.ProjectID != item.ProjectID || saved.ItemID != item.ID || saved.Round != item.ReviewRound || len(saved.Slices) == 0 {
|
|
return nil, ErrHumanClaimConflict
|
|
}
|
|
return saved, nil
|
|
}
|
|
// Older reviewed tasks predate independent baselines. Recover only their
|
|
// exact submitted revision, never a newer private workspace or draft.
|
|
if item.CurrentRevision <= 0 {
|
|
return nil, ErrHumanClaimConflict
|
|
}
|
|
result := &domain.ReworkBaselineRecord{OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, ItemID: item.ID, Round: item.ReviewRound}
|
|
foundAnnotation, foundSlices := false, false
|
|
for _, rev := range annotations {
|
|
if !rev.SupersededByAccountDeletion && rev.OrganizationID == item.OrganizationID && rev.TaskID == item.LegacyTaskID && rev.Revision == item.CurrentRevision {
|
|
result.Actions = rev.Segments
|
|
foundAnnotation = true
|
|
}
|
|
}
|
|
for _, rev := range slices {
|
|
if !rev.SupersededByAccountDeletion && rev.OrganizationID == item.OrganizationID && rev.TaskID == item.LegacyTaskID && rev.Revision == item.CurrentRevision {
|
|
result.Slices = rev.Segments
|
|
foundSlices = true
|
|
}
|
|
}
|
|
if !foundAnnotation || !foundSlices || len(result.Slices) == 0 {
|
|
return nil, ErrHumanClaimConflict
|
|
}
|
|
return result, nil
|
|
}
|
|
func (s *MemoryStore) inheritedReworkBaselineLocked(item domain.HumanItemRecord) (*domain.ReworkBaselineRecord, error) {
|
|
var saved *domain.ReworkBaselineRecord
|
|
if v, ok := s.reworkBaselines[ReworkBaselineID(item.ID, item.ReviewRound)]; ok {
|
|
saved = &v
|
|
}
|
|
annotations := []domain.AnnotationRevisionRecord{}
|
|
slices := []domain.SliceRevisionRecord{}
|
|
if saved == nil {
|
|
for _, rev := range s.annotationRevisions {
|
|
if rev.TaskID == item.LegacyTaskID {
|
|
annotations = append(annotations, rev)
|
|
}
|
|
}
|
|
for _, rev := range s.sliceRevisions {
|
|
if rev.TaskID == item.LegacyTaskID {
|
|
slices = append(slices, rev)
|
|
}
|
|
}
|
|
}
|
|
return inheritedBaselineFromRecords(item, saved, annotations, slices)
|
|
}
|
|
func (s *MemoryStore) LoadInheritedReworkBaseline(_ context.Context, item domain.HumanItemRecord) (*domain.ReworkBaselineRecord, error) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
return s.inheritedReworkBaselineLocked(item)
|
|
}
|
|
func (s *MongoStore) LoadInheritedReworkBaseline(ctx context.Context, item domain.HumanItemRecord) (*domain.ReworkBaselineRecord, error) {
|
|
saved, err := s.GetReworkBaseline(ctx, ReworkBaselineID(item.ID, item.ReviewRound))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if saved != nil {
|
|
return inheritedBaselineFromRecords(item, saved, nil, nil)
|
|
}
|
|
annotations, err := s.ListAnnotationRevisions(ctx, item.OrganizationID, item.LegacyTaskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
slices, err := s.ListSliceRevisions(ctx, item.OrganizationID, item.LegacyTaskID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return inheritedBaselineFromRecords(item, nil, annotations, slices)
|
|
}
|
|
|
|
// Refuse the periodic routing scan if its bounded lookup indexes are absent.
|
|
// Index creation is an explicitly authorized operation, never API startup work.
|
|
func (s *MongoStore) validateDeletedAuthorRoutingIndexes(ctx context.Context) error {
|
|
cur, err := s.humanItems.Indexes().List(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cur.Close(ctx)
|
|
wanted := map[string]bson.D{
|
|
"deleted_author_routing_v1": {{Key: "organizationId", Value: int32(1)}, {Key: "currentStageId", Value: int32(1)}, {Key: "reworkRoutingPending", Value: int32(1)}, {Key: "status", Value: int32(1)}},
|
|
"deleted_author_unassigned_v1": {{Key: "organizationId", Value: int32(1)}, {Key: "currentStageId", Value: int32(1)}, {Key: "inheritedRework", Value: int32(1)}, {Key: "assignedUserId", Value: int32(1)}, {Key: "status", Value: int32(1)}},
|
|
}
|
|
for cur.Next(ctx) {
|
|
var index struct {
|
|
Name string `bson:"name"`
|
|
Key bson.D `bson:"key"`
|
|
}
|
|
if err := cur.Decode(&index); err != nil {
|
|
return err
|
|
}
|
|
if key, ok := wanted[index.Name]; ok && reflect.DeepEqual(key, index.Key) {
|
|
delete(wanted, index.Name)
|
|
}
|
|
}
|
|
if err := cur.Err(); err != nil {
|
|
return err
|
|
}
|
|
if len(wanted) > 0 {
|
|
return fmt.Errorf("deleted author routing indexes are not ready")
|
|
}
|
|
return nil
|
|
}
|