droboticflow/internal/store/annotation_operations.go

1037 lines
39 KiB
Go

package store
import (
"context"
"encoding/json"
"sort"
"strings"
"droboticflow/internal/domain"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func annotationOrganizationMatches(actual string, expected string) bool {
return normalizedOrganizationID(actual) == normalizedOrganizationID(expected)
}
func sortAnnotationRecords[T any](items []T, createdAt func(T) string) {
sort.Slice(items, func(left int, right int) bool { return createdAt(items[left]) > createdAt(items[right]) })
}
func (store *MemoryStore) ListAnnotationWorkgroups(_ context.Context, organizationID string) ([]domain.AnnotationWorkgroupRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationWorkgroupRecord, 0)
for _, item := range store.annotationWorkgroups {
if annotationOrganizationMatches(item.OrganizationID, organizationID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationWorkgroupRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) GetAnnotationWorkgroup(_ context.Context, id string) (*domain.AnnotationWorkgroupRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.annotationWorkgroups[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertAnnotationWorkgroup(_ context.Context, item domain.AnnotationWorkgroupRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-workgroup")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Status == "" {
item.Status = "active"
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
store.annotationWorkgroups[item.ID] = item
return nil
}
func (store *MemoryStore) ListProjectAnnotationMemberships(_ context.Context, organizationID string, projectID string) ([]domain.ProjectAnnotationMembershipRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.ProjectAnnotationMembershipRecord, 0)
for _, item := range store.annotationMemberships {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (projectID == "" || item.ProjectID == projectID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.ProjectAnnotationMembershipRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) UpsertProjectAnnotationMembership(_ context.Context, item domain.ProjectAnnotationMembershipRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-membership")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
store.annotationMemberships[item.ID] = item
// Test and migration compatibility only: active authorization reads
// ProjectMember. Any legacy fixture written through this deprecated method
// is immediately mirrored into the production model.
memberID := "project-member-legacy:" + item.ProjectID + ":" + item.UserID
member := store.projectMembers[memberID]
if member.ID == "" {
member = domain.ProjectMemberRecord{ID: memberID, OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, UserID: item.UserID, SupplierID: item.VendorID, CreatedAt: item.CreatedAt}
}
member.Roles = domain.NormalizeProjectMemberRoles(append(member.Roles, item.AssignmentRole))
member.UpdatedAt = now
store.projectMembers[member.ID] = clone(member)
return nil
}
func (store *MemoryStore) DeleteProjectAnnotationMembership(_ context.Context, id string) (bool, error) {
store.mu.Lock()
defer store.mu.Unlock()
id = strings.TrimSpace(id)
if _, ok := store.annotationMemberships[id]; !ok {
return false, nil
}
delete(store.annotationMemberships, id)
return true, nil
}
func (store *MemoryStore) ListAnnotationTemplates(_ context.Context, organizationID string, projectID string) ([]domain.AnnotationTemplateRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationTemplateRecord, 0)
for _, item := range store.annotationTemplates {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (projectID == "" || item.ProjectID == "" || item.ProjectID == projectID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationTemplateRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) GetAnnotationTemplate(_ context.Context, id string) (*domain.AnnotationTemplateRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.annotationTemplates[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertAnnotationTemplate(_ context.Context, item domain.AnnotationTemplateRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-template")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Version <= 0 {
item.Version = 1
}
if item.Status == "" {
item.Status = domain.AnnotationTemplateDraft
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
store.annotationTemplates[item.ID] = item
return nil
}
func (store *MemoryStore) ListAnnotationTasks(_ context.Context, organizationID string, projectID string) ([]domain.AnnotationTaskRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationTaskRecord, 0)
for _, item := range store.annotationTasks {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (projectID == "" || item.ProjectID == projectID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationTaskRecord) string { return item.CreatedAt })
return items, nil
}
// ListAnnotationTasksFiltered mirrors the MongoStore variant so the local
// replica and in-process tests exercise the same filter-push semantics.
func (store *MemoryStore) ListAnnotationTasksFiltered(_ context.Context, organizationID string, filter AnnotationTaskFilter) ([]domain.AnnotationTaskRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
matches := func(value string, expected string) bool { return expected == "" || value == expected }
// An explicit ID set serves pool-item joins (see MongoStore).
if len(filter.IDs) > 0 {
wanted := make(map[string]bool, len(filter.IDs))
for _, id := range filter.IDs {
wanted[id] = true
}
items := make([]domain.AnnotationTaskRecord, 0, len(wanted))
for _, item := range store.annotationTasks {
if wanted[item.ID] && annotationOrganizationMatches(item.OrganizationID, organizationID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationTaskRecord) string { return item.CreatedAt })
return items, nil
}
items := make([]domain.AnnotationTaskRecord, 0)
for _, item := range store.annotationTasks {
if annotationOrganizationMatches(item.OrganizationID, organizationID) &&
matches(item.ProjectID, filter.ProjectID) &&
matches(string(item.Status), filter.Status) &&
matches(item.SupplierID, filter.SupplierID) &&
matches(item.WorkflowID, filter.WorkflowID) &&
matches(item.RunID, filter.RunID) &&
matches(item.NodeID, filter.NodeID) &&
matches(item.ArtifactID, filter.ArtifactID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationTaskRecord) string { return item.CreatedAt })
// Descending (createdAt, id) cursor semantics matching the MongoStore.
if filter.CursorID != "" {
kept := items[:0]
for _, item := range items {
if item.CreatedAt < filter.CursorCreatedAt || (item.CreatedAt == filter.CursorCreatedAt && item.ID < filter.CursorID) {
kept = append(kept, item)
}
}
items = kept
}
if filter.Limit > 0 && int64(len(items)) > filter.Limit {
items = items[:filter.Limit]
}
return items, nil
}
// ListHumanItemsByLegacyTaskIDs mirrors the MongoStore join for MemoryStore
// consumers, applying the same task-list projection clone.
func (store *MemoryStore) ListHumanItemsByLegacyTaskIDs(_ context.Context, organizationID string, taskIDs []string) ([]domain.HumanItemRecord, error) {
if len(taskIDs) == 0 {
return []domain.HumanItemRecord{}, nil
}
store.mu.Lock()
defer store.mu.Unlock()
wanted := make(map[string]bool, len(taskIDs))
for _, id := range taskIDs {
wanted[id] = true
}
items := make([]domain.HumanItemRecord, 0, len(taskIDs))
for _, item := range store.humanItems {
if item.PluginID == "annotation-qa" && wanted[item.LegacyTaskID] && annotationOrganizationMatches(item.OrganizationID, organizationID) {
items = append(items, humanItemListProjectionClone(item, humanTaskListPayloadKeysRemoved))
}
}
return items, nil
}
// ListHumanItemDraftProgress mirrors the MongoStore draft read: only items in
// the requested task-ID set contribute, and payloads without a draft are
// skipped.
func (store *MemoryStore) ListHumanItemDraftProgress(_ context.Context, organizationID string, taskIDs []string) ([]domain.HumanItemDraftProgress, error) {
if len(taskIDs) == 0 {
return []domain.HumanItemDraftProgress{}, nil
}
store.mu.Lock()
defer store.mu.Unlock()
wanted := make(map[string]bool, len(taskIDs))
for _, id := range taskIDs {
wanted[id] = true
}
results := make([]domain.HumanItemDraftProgress, 0, len(taskIDs))
for _, item := range store.humanItems {
if !wanted[item.LegacyTaskID] || !annotationOrganizationMatches(item.OrganizationID, organizationID) || item.LegacyTaskID == "" {
continue
}
draft, _ := item.Subject.Payload["annotationDraft"].(map[string]any)
if draft == nil {
continue
}
progress := domain.HumanItemDraftProgress{LegacyTaskID: item.LegacyTaskID}
progress.SliceSegments = decodeDraftSegments[domain.SliceSegment](draft["sliceSegments"])
progress.Segments = decodeDraftSegments[domain.AnnotationSegment](draft["segments"])
results = append(results, progress)
}
return results, nil
}
func (store *MemoryStore) GetAnnotationTask(_ context.Context, id string) (*domain.AnnotationTaskRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
item, ok := store.annotationTasks[id]
if !ok {
return nil, nil
}
copy := item
return &copy, nil
}
func (store *MemoryStore) UpsertAnnotationTask(_ context.Context, item domain.AnnotationTaskRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
return store.upsertAnnotationTaskLocked(item)
}
func (store *MemoryStore) upsertAnnotationTaskLocked(item domain.AnnotationTaskRecord) error {
if old, ok := store.annotationTasks[item.ID]; ok && old.AnnotationEpoch > item.AnnotationEpoch {
return ErrHumanClaimConflict
}
if store.supplierRecordBlockedLocked("annotationTasks", item.ID, item) {
return nil
}
if _, deleting := store.projectDeletions["project-deletion-"+item.ProjectID]; deleting {
if _, exists := store.annotationTasks[item.ID]; !exists {
return nil
}
}
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-task")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Status == "" {
item.Status = domain.AnnotationTaskStatusPendingAnnotation
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
_, existed := store.annotationTasks[item.ID]
store.annotationTasks[item.ID] = item
if !existed && AnnotationSourceValidationRequired(item) {
if store.annotationIndexJobs == nil {
store.annotationIndexJobs = map[string]domain.AnnotationIndexJob{}
}
if _, exists := store.annotationIndexJobs[item.ID]; !exists {
store.annotationIndexJobs[item.ID] = annotationIndexJob(item)
}
}
return nil
}
func (store *MemoryStore) ListAnnotationRevisions(_ context.Context, organizationID string, taskID string) ([]domain.AnnotationRevisionRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationRevisionRecord, 0)
for _, item := range store.annotationRevisions {
if !item.SupersededByAccountDeletion && annotationOrganizationMatches(item.OrganizationID, organizationID) && (taskID == "" || item.TaskID == taskID) {
items = append(items, item)
}
}
sort.Slice(items, func(left int, right int) bool {
if items[left].TaskID != items[right].TaskID {
return items[left].TaskID < items[right].TaskID
}
return items[left].Revision < items[right].Revision
})
return items, nil
}
// ListAnnotationRevisionsByTaskIDs implements Store.ListAnnotationRevisionsByTaskIDs:
// only revisions whose TaskID is in taskIDs are returned (empty slice means no
// matches), so task-scoped consumers no longer pull the whole organization.
func (store *MemoryStore) ListAnnotationRevisionsByTaskIDs(_ context.Context, organizationID string, taskIDs []string) ([]domain.AnnotationRevisionRecord, error) {
allowed := annotationTaskIDSet(taskIDs)
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationRevisionRecord, 0, len(allowed))
for _, item := range store.annotationRevisions {
if !item.SupersededByAccountDeletion && annotationOrganizationMatches(item.OrganizationID, organizationID) && allowed[item.TaskID] {
items = append(items, item)
}
}
sort.Slice(items, func(left int, right int) bool {
if items[left].TaskID != items[right].TaskID {
return items[left].TaskID < items[right].TaskID
}
return items[left].Revision < items[right].Revision
})
return items, nil
}
func annotationTaskIDSet(taskIDs []string) map[string]bool {
allowed := make(map[string]bool, len(taskIDs))
for _, id := range taskIDs {
if trimmed := strings.TrimSpace(id); trimmed != "" {
allowed[trimmed] = true
}
}
return allowed
}
func (store *MemoryStore) UpsertAnnotationRevision(_ context.Context, item domain.AnnotationRevisionRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
if task, ok := store.annotationTasks[item.TaskID]; ok && (item.AnnotationEpoch != task.AnnotationEpoch || (task.AnnotationEpoch > 0 && item.Revision <= task.AnnotationRevisionFloor)) {
return ErrHumanClaimConflict
}
if old, ok := store.annotationRevisions[item.ID]; ok && old.SupersededByAccountDeletion {
return ErrHumanClaimConflict
}
if store.supplierRecordBlockedLocked("annotationRevisions", item.ID, item) {
return nil
}
if item.ID == "" {
item.ID = domain.NewID("annotation-revision")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
store.annotationRevisions[item.ID] = item
return nil
}
func (store *MemoryStore) DeleteAnnotationRevisions(_ context.Context, organizationID string, taskID string) error {
store.mu.Lock()
defer store.mu.Unlock()
for id, item := range store.annotationRevisions {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (taskID == "" || item.TaskID == taskID) {
delete(store.annotationRevisions, id)
}
}
return nil
}
func (store *MemoryStore) ListSliceRevisions(_ context.Context, organizationID string, taskID string) ([]domain.SliceRevisionRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.SliceRevisionRecord, 0)
for _, item := range store.sliceRevisions {
if !item.SupersededByAccountDeletion && annotationOrganizationMatches(item.OrganizationID, organizationID) && (taskID == "" || item.TaskID == taskID) {
items = append(items, clone(item))
}
}
sort.Slice(items, func(i, j int) bool { return items[i].Revision < items[j].Revision })
return items, nil
}
// ListSliceRevisionsByTaskIDs implements Store.ListSliceRevisionsByTaskIDs:
// only revisions whose TaskID is in taskIDs are returned (empty slice means no
// matches).
func (store *MemoryStore) ListSliceRevisionsByTaskIDs(_ context.Context, organizationID string, taskIDs []string) ([]domain.SliceRevisionRecord, error) {
allowed := annotationTaskIDSet(taskIDs)
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.SliceRevisionRecord, 0, len(allowed))
for _, item := range store.sliceRevisions {
if !item.SupersededByAccountDeletion && annotationOrganizationMatches(item.OrganizationID, organizationID) && allowed[item.TaskID] {
items = append(items, clone(item))
}
}
sort.Slice(items, func(i, j int) bool { return items[i].Revision < items[j].Revision })
return items, nil
}
func (store *MemoryStore) UpsertSliceRevision(_ context.Context, item domain.SliceRevisionRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
if task, ok := store.annotationTasks[item.TaskID]; ok && (item.AnnotationEpoch != task.AnnotationEpoch || (task.AnnotationEpoch > 0 && item.Revision <= task.AnnotationRevisionFloor)) {
return ErrHumanClaimConflict
}
if old, ok := store.sliceRevisions[item.ID]; ok && old.SupersededByAccountDeletion {
return ErrHumanClaimConflict
}
if store.supplierRecordBlockedLocked("sliceRevisions", item.ID, item) {
return nil
}
if item.ID == "" {
item.ID = domain.NewID("slice-revision")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
store.sliceRevisions[item.ID] = clone(item)
return nil
}
func (store *MemoryStore) DeleteSliceRevisions(_ context.Context, organizationID string, taskID string) error {
store.mu.Lock()
defer store.mu.Unlock()
for id, item := range store.sliceRevisions {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (taskID == "" || item.TaskID == taskID) {
delete(store.sliceRevisions, id)
}
}
return nil
}
func (store *MemoryStore) ListAnnotationReviews(_ context.Context, organizationID string, taskID string) ([]domain.AnnotationReviewRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationReviewRecord, 0)
for _, item := range store.annotationReviews {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (taskID == "" || item.TaskID == taskID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationReviewRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) UpsertAnnotationReview(_ context.Context, item domain.AnnotationReviewRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
if store.supplierRecordBlockedLocked("annotationReviews", item.ID, item) {
return nil
}
if item.ID == "" {
item.ID = domain.NewID("annotation-review")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
store.annotationReviews[item.ID] = item
return nil
}
func (store *MemoryStore) ListAnnotationWorklogEvents(_ context.Context, organizationID string, projectID string) ([]domain.AnnotationWorklogEventRecord, error) {
store.mu.Lock()
defer store.mu.Unlock()
items := make([]domain.AnnotationWorklogEventRecord, 0)
for _, item := range store.annotationWorklogEvents {
if annotationOrganizationMatches(item.OrganizationID, organizationID) && (projectID == "" || item.ProjectID == projectID) {
items = append(items, item)
}
}
sortAnnotationRecords(items, func(item domain.AnnotationWorklogEventRecord) string { return item.CreatedAt })
return items, nil
}
func (store *MemoryStore) UpsertAnnotationWorklogEvent(_ context.Context, item domain.AnnotationWorklogEventRecord) error {
store.mu.Lock()
defer store.mu.Unlock()
if store.supplierRecordBlockedLocked("annotationWorklogEvents", item.ID, item) {
return nil
}
if item.ID == "" {
item.ID = domain.NewID("annotation-worklog")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
if item.Mode == "" {
item.Mode = domain.RunModeProduction
}
if item.SubmissionID != "" {
if _, exists := store.annotationWorklogEvents[item.ID]; exists {
return nil // Retry must not move a successful submission into another day.
}
}
store.annotationWorklogEvents[item.ID] = item
return nil
}
func mongoAnnotationFilter(organizationID string, extra bson.M) bson.M {
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
for key, value := range extra {
filter[key] = value
}
return filter
}
func mongoGetByID[T any](ctx context.Context, collection mongoCollection, id string) (*T, error) {
var item T
if err := collection.FindOne(ctx, bson.M{"id": strings.TrimSpace(id)}).Decode(&item); err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
return nil, err
}
return &item, nil
}
func mongoList[T any](ctx context.Context, collection mongoCollection, filter bson.M, sortFields bson.D) ([]T, error) {
cursor, err := collection.Find(ctx, filter, options.Find().SetSort(sortFields))
if err != nil {
return nil, err
}
return decodeMany[T](ctx, cursor)
}
func mongoUpsert(ctx context.Context, collection mongoCollection, id string, item any) error {
return supplierGuardedMongoWrite(ctx, collection, id, item, func(ctx context.Context, d bson.M) error {
allow, e := projectAllowsUpsert(ctx, collection.Database(), deletionString(d, "projectId"))
if e != nil {
return e
}
_, e = collection.UpdateOne(ctx, bson.M{"id": id}, bson.M{"$set": d}, options.Update().SetUpsert(allow))
return e
})
}
func (store *MongoStore) ListAnnotationWorkgroups(ctx context.Context, organizationID string) ([]domain.AnnotationWorkgroupRecord, error) {
return mongoList[domain.AnnotationWorkgroupRecord](ctx, store.annotationWorkgroups, mongoAnnotationFilter(organizationID, bson.M{}), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) GetAnnotationWorkgroup(ctx context.Context, id string) (*domain.AnnotationWorkgroupRecord, error) {
return mongoGetByID[domain.AnnotationWorkgroupRecord](ctx, store.annotationWorkgroups, id)
}
func (store *MongoStore) UpsertAnnotationWorkgroup(ctx context.Context, item domain.AnnotationWorkgroupRecord) error {
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-workgroup")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Status == "" {
item.Status = "active"
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
return mongoUpsert(ctx, store.annotationWorkgroups, item.ID, item)
}
func (store *MongoStore) ListProjectAnnotationMemberships(ctx context.Context, organizationID string, projectID string) ([]domain.ProjectAnnotationMembershipRecord, error) {
extra := bson.M{}
if projectID != "" {
extra["projectId"] = projectID
}
return mongoList[domain.ProjectAnnotationMembershipRecord](ctx, store.annotationMemberships, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) UpsertProjectAnnotationMembership(ctx context.Context, item domain.ProjectAnnotationMembershipRecord) error {
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-membership")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
return mongoUpsert(ctx, store.annotationMemberships, item.ID, item)
}
func (store *MongoStore) DeleteProjectAnnotationMembership(ctx context.Context, id string) (bool, error) {
result, err := store.annotationMemberships.DeleteOne(ctx, bson.M{"id": strings.TrimSpace(id)})
if err != nil {
return false, err
}
return result.DeletedCount == 1, nil
}
func (store *MongoStore) ListAnnotationTemplates(ctx context.Context, organizationID string, projectID string) ([]domain.AnnotationTemplateRecord, error) {
extra := bson.M{}
if projectID != "" {
extra["$or"] = bson.A{
bson.M{"projectId": projectID},
bson.M{"projectId": ""},
bson.M{"projectId": bson.M{"$exists": false}},
}
}
return mongoList[domain.AnnotationTemplateRecord](ctx, store.annotationTemplates, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) GetAnnotationTemplate(ctx context.Context, id string) (*domain.AnnotationTemplateRecord, error) {
return mongoGetByID[domain.AnnotationTemplateRecord](ctx, store.annotationTemplates, id)
}
func (store *MongoStore) UpsertAnnotationTemplate(ctx context.Context, item domain.AnnotationTemplateRecord) error {
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-template")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Version <= 0 {
item.Version = 1
}
if item.Status == "" {
item.Status = domain.AnnotationTemplateDraft
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
return mongoUpsert(ctx, store.annotationTemplates, item.ID, item)
}
func (store *MongoStore) ListAnnotationTasks(ctx context.Context, organizationID string, projectID string) ([]domain.AnnotationTaskRecord, error) {
extra := bson.M{}
if projectID != "" {
extra["projectId"] = projectID
}
return mongoList[domain.AnnotationTaskRecord](ctx, store.annotationTasks, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
// AnnotationTaskFilter pushes the annotation tasks list's equality filters
// into the store query. Every field is optional; empty values widen the
// result like the corresponding query parameter.
type AnnotationTaskFilter struct {
ProjectID string
Status string
SupplierID string
WorkflowID string
RunID string
NodeID string
ArtifactID string
// IDs requests a specific task-ID set (pool-item joins). When set, the
// other equality filters and the cursor are ignored and the id_unique
// index serves the lookup directly.
IDs []string
// Limit caps the page size (0 = unbounded, the legacy behavior).
Limit int64
// Cursor resumes strictly after (CreatedAt, ID) in the descending order.
CursorCreatedAt string
CursorID string
}
// ListAnnotationTasksFiltered is the task-pool variant of ListAnnotationTasks:
// the handler's status/supplier/workflow/run/node/artifact equality filters
// are evaluated by the database instead of a post-fetch in-memory loop, and
// the organizationId+createdAt indexes back the createdAt-descending sort.
func (store *MongoStore) ListAnnotationTasksFiltered(ctx context.Context, organizationID string, filter AnnotationTaskFilter) ([]domain.AnnotationTaskRecord, error) {
extra := bson.M{}
if filter.ProjectID != "" {
extra["projectId"] = filter.ProjectID
}
if filter.Status != "" {
extra["status"] = filter.Status
}
if filter.SupplierID != "" {
extra["supplierId"] = filter.SupplierID
}
if filter.WorkflowID != "" {
extra["workflowId"] = filter.WorkflowID
}
if filter.RunID != "" {
extra["runId"] = filter.RunID
}
if filter.NodeID != "" {
extra["nodeId"] = filter.NodeID
}
if filter.ArtifactID != "" {
extra["artifactId"] = filter.ArtifactID
}
query := mongoAnnotationFilter(organizationID, extra)
// An explicit ID set serves pool-item joins: the caller's pool page
// decides which tasks it needs, so fetch exactly those (id_unique index)
// instead of re-deriving them through the cursor window.
if len(filter.IDs) > 0 {
ids := make(bson.A, 0, len(filter.IDs))
for _, id := range filter.IDs {
ids = append(ids, id)
}
query["id"] = bson.M{"$in": ids}
delete(query, "$or")
}
if len(filter.IDs) == 0 && filter.CursorID != "" {
cursorTail := bson.A{bson.M{"createdAt": bson.M{"$lt": filter.CursorCreatedAt}}, bson.M{"createdAt": filter.CursorCreatedAt, "id": bson.M{"$lt": filter.CursorID}}}
query["$or"] = cursorTail
}
options := options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}})
if filter.Limit > 0 {
options = options.SetLimit(filter.Limit)
}
cursor, err := store.annotationTasks.Find(ctx, query, options)
if err != nil {
return nil, err
}
return decodeMany[domain.AnnotationTaskRecord](ctx, cursor)
}
// ListHumanItemsByLegacyTaskIDs joins human items to an explicit task-ID set
// via the organizationId+legacyTaskId index instead of scanning the whole
// organization. The task-list projection applies, so annotationDraft payloads
// are never decoded on this path.
func (store *MongoStore) ListHumanItemsByLegacyTaskIDs(ctx context.Context, organizationID string, taskIDs []string) ([]domain.HumanItemRecord, error) {
if len(taskIDs) == 0 {
return []domain.HumanItemRecord{}, nil
}
filtered := make([]domain.HumanItemRecord, 0, len(taskIDs))
projection := humanItemListProjection(domain.HumanItemQuery{TaskListProjection: true})
for start := 0; start < len(taskIDs); start += mongoTaskIDBatchSize {
end := min(start+mongoTaskIDBatchSize, len(taskIDs))
cursor, err := store.humanItems.Find(ctx, bson.M{
"organizationId": normalizedOrganizationID(organizationID),
"pluginId": "annotation-qa",
"legacyTaskId": bson.M{"$in": taskIDs[start:end]},
}, options.Find().SetProjection(projection))
if err != nil {
return nil, err
}
batch, decodeErr := decodeMany[domain.HumanItemRecord](ctx, cursor)
if decodeErr != nil {
return nil, decodeErr
}
filtered = append(filtered, batch...)
}
return filtered, nil
}
// humanItemDraftProjection decodes only legacyTaskId plus the draft payload
// sub-document; the projection never touches other fields.
type humanItemDraftProjection struct {
LegacyTaskID string `bson:"legacyTaskId"`
Subject struct {
Payload struct {
AnnotationDraft map[string]any `bson:"annotationDraft"`
} `bson:"payload"`
} `bson:"subject"`
}
// ListHumanItemDraftProgress loads the progress-relevant draft slices for the
// given task IDs. Items without a draft are skipped. Batches of 500 keep the
// $in arrays bounded like the revision reads.
func (store *MongoStore) ListHumanItemDraftProgress(ctx context.Context, organizationID string, taskIDs []string) ([]domain.HumanItemDraftProgress, error) {
if len(taskIDs) == 0 {
return []domain.HumanItemDraftProgress{}, nil
}
results := make([]domain.HumanItemDraftProgress, 0, len(taskIDs))
for start := 0; start < len(taskIDs); start += mongoTaskIDBatchSize {
end := min(start+mongoTaskIDBatchSize, len(taskIDs))
cursor, err := store.humanItems.Find(ctx, bson.M{
"organizationId": normalizedOrganizationID(organizationID),
"pluginId": "annotation-qa",
"legacyTaskId": bson.M{"$in": taskIDs[start:end]},
}, options.Find().SetProjection(bson.M{"_id": 0, "legacyTaskId": 1, "subject.payload.annotationDraft": 1}))
if err != nil {
return nil, err
}
for cursor.Next(ctx) {
var doc humanItemDraftProjection
if err := cursor.Decode(&doc); err != nil {
cursor.Close(ctx)
return nil, err
}
if doc.LegacyTaskID == "" || doc.Subject.Payload.AnnotationDraft == nil {
continue
}
progress := domain.HumanItemDraftProgress{LegacyTaskID: doc.LegacyTaskID}
progress.SliceSegments = decodeDraftSegments[domain.SliceSegment](doc.Subject.Payload.AnnotationDraft["sliceSegments"])
progress.Segments = decodeDraftSegments[domain.AnnotationSegment](doc.Subject.Payload.AnnotationDraft["segments"])
results = append(results, progress)
}
if err := cursor.Err(); err != nil {
cursor.Close(ctx)
return nil, err
}
cursor.Close(ctx)
}
return results, nil
}
// decodeDraftSegments mirrors the API layer's materialization decoding: a JSON
// round-trip converts bson.M/bson.A documents into the typed segment structs.
func decodeDraftSegments[T any](value any) []T {
if value == nil {
return nil
}
encoded, err := json.Marshal(value)
if err != nil {
return nil
}
var items []T
if err := json.Unmarshal(encoded, &items); err != nil {
return nil
}
return items
}
func (store *MongoStore) GetAnnotationTask(ctx context.Context, id string) (*domain.AnnotationTaskRecord, error) {
return mongoGetByID[domain.AnnotationTaskRecord](ctx, store.annotationTasks, id)
}
func (store *MongoStore) UpsertAnnotationTask(ctx context.Context, item domain.AnnotationTaskRecord) error {
now := nowISO()
if item.ID == "" {
item.ID = domain.NewID("annotation-task")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.Status == "" {
item.Status = domain.AnnotationTaskStatusPendingAnnotation
}
if item.CreatedAt == "" {
item.CreatedAt = now
}
item.UpdatedAt = now
inserted, err := upsertAnnotationEpochInserted(ctx, store.annotationTasks, item.ID, item, item.AnnotationEpoch)
if err != nil {
return err
}
// Existing task projections are also updated during detail GET recovery.
// Missing historical evidence requires explicit repair, never a read-triggered
// full decode/source copy. New task creation alone bootstraps validation.
if inserted && AnnotationSourceValidationRequired(item) {
if err := store.EnqueueAnnotationIndex(ctx, item); err != nil {
return err
}
}
return nil
}
func (store *MongoStore) ListAnnotationRevisions(ctx context.Context, organizationID string, taskID string) ([]domain.AnnotationRevisionRecord, error) {
extra := bson.M{"supersededByAccountDeletion": bson.M{"$ne": true}}
if taskID != "" {
extra["taskId"] = taskID
}
return mongoList[domain.AnnotationRevisionRecord](ctx, store.annotationRevisions, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "taskId", Value: 1}, {Key: "revision", Value: 1}})
}
// mongoTaskIDBatchSize caps the $in array length for task-scoped revision
// queries; Mongo degrades badly past a few hundred keys per $in.
const mongoTaskIDBatchSize = 500
// ListAnnotationRevisionsByTaskIDs implements Store.ListAnnotationRevisionsByTaskIDs:
// revisions are queried by {organizationId, taskId $in taskIDs} in batches of
// mongoTaskIDBatchSize, backed by the organizationId_taskId_revision_unique
// index. Results preserve the ListAnnotationRevisions ordering (taskId,
// revision).
func (store *MongoStore) ListAnnotationRevisionsByTaskIDs(ctx context.Context, organizationID string, taskIDs []string) ([]domain.AnnotationRevisionRecord, error) {
items := make([]domain.AnnotationRevisionRecord, 0, len(taskIDs))
for start := 0; start < len(taskIDs); start += mongoTaskIDBatchSize {
end := min(start+mongoTaskIDBatchSize, len(taskIDs))
batch, err := mongoList[domain.AnnotationRevisionRecord](ctx, store.annotationRevisions,
mongoAnnotationFilter(organizationID, bson.M{"taskId": bson.M{"$in": taskIDs[start:end]}, "supersededByAccountDeletion": bson.M{"$ne": true}}),
bson.D{{Key: "taskId", Value: 1}, {Key: "revision", Value: 1}})
if err != nil {
return nil, err
}
items = append(items, batch...)
}
return items, nil
}
func (store *MongoStore) UpsertAnnotationRevision(ctx context.Context, item domain.AnnotationRevisionRecord) error {
if item.ID == "" {
item.ID = domain.NewID("annotation-revision")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
return store.upsertActiveAnnotationRevision(ctx, store.annotationRevisions, item.ID, item.TaskID, item.Revision, item.AnnotationEpoch, item)
}
func (store *MongoStore) DeleteAnnotationRevisions(ctx context.Context, organizationID string, taskID string) error {
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
if taskID != "" {
filter["taskId"] = taskID
}
_, err := store.annotationRevisions.DeleteMany(ctx, filter)
return err
}
func (store *MongoStore) ListSliceRevisions(ctx context.Context, organizationID string, taskID string) ([]domain.SliceRevisionRecord, error) {
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID), "supersededByAccountDeletion": bson.M{"$ne": true}}
if taskID != "" {
filter["taskId"] = taskID
}
return mongoList[domain.SliceRevisionRecord](ctx, store.sliceRevisions, filter, bson.D{{Key: "revision", Value: 1}})
}
// ListSliceRevisionsByTaskIDs implements Store.ListSliceRevisionsByTaskIDs:
// same batching contract as ListAnnotationRevisionsByTaskIDs; results preserve
// the ListSliceRevisions ordering (revision).
func (store *MongoStore) ListSliceRevisionsByTaskIDs(ctx context.Context, organizationID string, taskIDs []string) ([]domain.SliceRevisionRecord, error) {
items := make([]domain.SliceRevisionRecord, 0, len(taskIDs))
for start := 0; start < len(taskIDs); start += mongoTaskIDBatchSize {
end := min(start+mongoTaskIDBatchSize, len(taskIDs))
batch, err := mongoList[domain.SliceRevisionRecord](ctx, store.sliceRevisions,
mongoAnnotationFilter(organizationID, bson.M{"taskId": bson.M{"$in": taskIDs[start:end]}, "supersededByAccountDeletion": bson.M{"$ne": true}}),
bson.D{{Key: "revision", Value: 1}})
if err != nil {
return nil, err
}
items = append(items, batch...)
}
return items, nil
}
func (store *MongoStore) UpsertSliceRevision(ctx context.Context, item domain.SliceRevisionRecord) error {
if item.ID == "" {
item.ID = domain.NewID("slice-revision")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
return store.upsertActiveAnnotationRevision(ctx, store.sliceRevisions, item.ID, item.TaskID, item.Revision, item.AnnotationEpoch, item)
}
func (store *MongoStore) DeleteSliceRevisions(ctx context.Context, organizationID string, taskID string) error {
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
if taskID != "" {
filter["taskId"] = taskID
}
_, err := store.sliceRevisions.DeleteMany(ctx, filter)
return err
}
func (store *MongoStore) ListAnnotationReviews(ctx context.Context, organizationID string, taskID string) ([]domain.AnnotationReviewRecord, error) {
extra := bson.M{}
if taskID != "" {
extra["taskId"] = taskID
}
return mongoList[domain.AnnotationReviewRecord](ctx, store.annotationReviews, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) UpsertAnnotationReview(ctx context.Context, item domain.AnnotationReviewRecord) error {
if item.ID == "" {
item.ID = domain.NewID("annotation-review")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
return mongoUpsert(ctx, store.annotationReviews, item.ID, item)
}
func (store *MongoStore) ListAnnotationWorklogEvents(ctx context.Context, organizationID string, projectID string) ([]domain.AnnotationWorklogEventRecord, error) {
extra := bson.M{}
if projectID != "" {
extra["projectId"] = projectID
}
return mongoList[domain.AnnotationWorklogEventRecord](ctx, store.annotationWorklogEvents, mongoAnnotationFilter(organizationID, extra), bson.D{{Key: "createdAt", Value: -1}})
}
func (store *MongoStore) UpsertAnnotationWorklogEvent(ctx context.Context, item domain.AnnotationWorklogEventRecord) error {
if item.ID == "" {
item.ID = domain.NewID("annotation-worklog")
}
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
if item.CreatedAt == "" {
item.CreatedAt = nowISO()
}
if item.Mode == "" {
item.Mode = domain.RunModeProduction
}
if item.SubmissionID != "" {
return supplierGuardedMongoWrite(ctx, store.annotationWorklogEvents, item.ID, item, func(ctx context.Context, document bson.M) error {
allow, err := projectAllowsUpsert(ctx, store.db, item.ProjectID)
if err != nil {
return err
}
_, err = store.annotationWorklogEvents.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$setOnInsert": document}, options.Update().SetUpsert(allow))
return err
})
}
return mongoUpsert(ctx, store.annotationWorklogEvents, item.ID, item)
}