forked from longtao.wu/droboticflow
1227 lines
46 KiB
Go
1227 lines
46 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"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 humanOrganizationMatches(actual string, expected string) bool {
|
|
return normalizedOrganizationID(actual) == normalizedOrganizationID(expected)
|
|
}
|
|
|
|
func humanBatchMatches(item domain.HumanBatchRecord, query domain.HumanBatchQuery) bool {
|
|
return (query.PluginID == "" || item.PluginID == query.PluginID) &&
|
|
(query.ProjectID == "" || item.ProjectID == query.ProjectID) &&
|
|
(query.WorkflowID == "" || item.WorkflowID == query.WorkflowID) &&
|
|
(query.RunID == "" || item.RunID == query.RunID) &&
|
|
(query.Status == "" || item.Status == query.Status)
|
|
}
|
|
|
|
func matchesAnnotationView(item domain.HumanItemRecord, query domain.HumanItemQuery) bool {
|
|
switch query.AnnotationView {
|
|
case "active":
|
|
return item.AssignedUserID == query.ActorUserID && (item.CurrentStageID == "annotation_combined" || item.CurrentStageID == "semantic_annotation" || item.CurrentStageID == "split_annotation") && (item.Status == domain.HumanItemClaimed || item.Status == domain.HumanItemInProgress || item.Status == domain.HumanItemRework)
|
|
case "submitted":
|
|
return item.LatestAuthorUserID == query.ActorUserID && item.CurrentStageID != "annotation_combined" && item.CurrentStageID != "semantic_annotation" && item.CurrentStageID != "split_annotation"
|
|
}
|
|
return true
|
|
}
|
|
|
|
// Only the HTTP mine pool opts in. Other actor queries and dedicated annotator
|
|
// views must still retain their historical ownership semantics.
|
|
func matchesHumanMinePool(item domain.HumanItemRecord, query domain.HumanItemQuery) bool {
|
|
if !query.MinePoolOnly || query.ActorUserID == "" || query.AnnotationView != "" {
|
|
return true
|
|
}
|
|
return item.AssignedUserID == query.ActorUserID || (item.LatestAuthorUserID == query.ActorUserID && item.CurrentStageID == "quality_review" && (item.Status == domain.HumanItemReady || item.Status == domain.HumanItemSubmitted))
|
|
}
|
|
|
|
func humanItemMatches(item domain.HumanItemRecord, query domain.HumanItemQuery) bool {
|
|
return (!query.NeedsReworkRouting || (item.CurrentStageID == domain.FanyuV58StageAnnotationCombined && (item.Status == domain.HumanItemRework || item.Status == domain.HumanItemClaimed || item.Status == domain.HumanItemInProgress) && (item.ReworkRoutingPending || (item.InheritedRework && item.AssignedUserID == "")))) && (!query.InheritedUnassigned || (item.InheritedRework && item.AssignedUserID == "" && item.CurrentStageID == domain.FanyuV58StageAnnotationCombined && item.Status == domain.HumanItemRework)) && (query.AvailableSupplierID == nil || item.SupplierID == "" || item.SupplierID == *query.AvailableSupplierID) && (query.AvailableProjectIDs == nil || containsHumanItemID(query.AvailableProjectIDs, item.ProjectID)) && matchesHumanMinePool(item, query) && matchesAnnotationView(item, query) && (query.IDs == nil || containsHumanItemID(query.IDs, item.ID)) && (query.LegacyTaskID == "" || item.LegacyTaskID == query.LegacyTaskID) &&
|
|
(query.ActorUserID == "" || item.AssignedUserID == query.ActorUserID || item.LatestAuthorUserID == query.ActorUserID) &&
|
|
(query.PluginID == "" || item.PluginID == query.PluginID) &&
|
|
(query.BatchID == "" || item.BatchID == query.BatchID) &&
|
|
(query.ProjectID == "" || item.ProjectID == query.ProjectID) &&
|
|
(query.WorkflowID == "" || item.WorkflowID == query.WorkflowID) &&
|
|
(query.RunID == "" || item.RunID == query.RunID) &&
|
|
(query.CurrentStageID == "" || len(query.StageIDs) > 0 || item.CurrentStageID == query.CurrentStageID) &&
|
|
(query.Status == "" || item.Status == query.Status) &&
|
|
(!query.UnassignedOnly || item.AssignedUserID == "") &&
|
|
(len(query.StageIDs) == 0 || containsHumanItemID(query.StageIDs, item.CurrentStageID)) &&
|
|
(query.Statuses == nil || humanItemStatusMatched(query.Statuses, item.Status))
|
|
}
|
|
|
|
func humanItemStatusMatched(statuses []domain.HumanItemStatus, status domain.HumanItemStatus) bool {
|
|
for _, candidate := range statuses {
|
|
if status == candidate {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// humanItemListPayloadKeysRemoved mirrors humanItemListProjection: payload
|
|
// sub-documents that are only needed on the detail/workbench paths are dropped
|
|
// from list responses to keep pages small.
|
|
var humanItemListPayloadKeysRemoved = []string{
|
|
"annotationWorkspace",
|
|
"annotationDraft",
|
|
"sliceDispositionHistory",
|
|
"sliceResetHistory",
|
|
"sliceArchiveEdits",
|
|
"sliceRestoreEvidence",
|
|
"currentDataPreview",
|
|
}
|
|
|
|
// humanTaskListPayloadKeysRemoved is the annotation-tasks-list variant of
|
|
// humanItemListPayloadKeysRemoved. annotationDraft is also excluded: the
|
|
// tasks handler fetches drafts per filtered task (ListHumanItemDraftProgress)
|
|
// instead of deriving progress from an organization-wide scan, so list joins
|
|
// never decode draft payloads.
|
|
var humanTaskListPayloadKeysRemoved = []string{
|
|
"annotationWorkspace",
|
|
"annotationDraft",
|
|
"sliceDispositionHistory",
|
|
"sliceResetHistory",
|
|
"sliceArchiveEdits",
|
|
"sliceRestoreEvidence",
|
|
"currentDataPreview",
|
|
}
|
|
|
|
func humanItemPayloadKeysRemoved(query domain.HumanItemQuery) []string {
|
|
if query.TaskListProjection {
|
|
return humanTaskListPayloadKeysRemoved
|
|
}
|
|
return humanItemListPayloadKeysRemoved
|
|
}
|
|
|
|
func humanItemListProjectionClone(item domain.HumanItemRecord, keysRemoved []string) domain.HumanItemRecord {
|
|
if item.ProcessingJob != nil {
|
|
summary := *item.ProcessingJob
|
|
summary.Input = nil
|
|
item.ProcessingJob = &summary
|
|
}
|
|
if item.Subject.Payload == nil {
|
|
return item
|
|
}
|
|
payload := make(map[string]any, len(item.Subject.Payload))
|
|
for key, value := range item.Subject.Payload {
|
|
payload[key] = value
|
|
}
|
|
for _, key := range keysRemoved {
|
|
delete(payload, key)
|
|
}
|
|
item.Subject.Payload = payload
|
|
return item
|
|
}
|
|
|
|
func humanItemAfterCursor(item domain.HumanItemRecord, cursor *domain.HumanItemCursor) bool {
|
|
if cursor == nil {
|
|
return true
|
|
}
|
|
if item.CreatedAt != cursor.CreatedAt {
|
|
return item.CreatedAt < cursor.CreatedAt
|
|
}
|
|
return item.ID < cursor.ID
|
|
}
|
|
|
|
func (store *MemoryStore) ListHumanBatches(_ context.Context, organizationID string, query domain.HumanBatchQuery) ([]domain.HumanBatchRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.HumanBatchRecord, 0)
|
|
for _, item := range store.humanBatches {
|
|
if humanOrganizationMatches(item.OrganizationID, organizationID) && humanBatchMatches(item, query) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool { return items[i].CreatedAt > items[j].CreatedAt })
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) HasRawHumanAnnotationBatch(_ context.Context, organizationID string, projectID string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
for _, item := range store.humanBatches {
|
|
if humanOrganizationMatches(item.OrganizationID, organizationID) && item.ProjectID == projectID &&
|
|
item.PluginID == "annotation-qa" && item.ProcessingProfile == domain.ProcessingProfileRawHuman &&
|
|
domain.IsFanyuV58ThreeRoleHumanLoop(item.Stages) && item.Status != domain.HumanBatchCompleted {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func humanBatchCanSweepRelease(batch domain.HumanBatchRecord) bool {
|
|
if batch.Status == domain.HumanBatchCompleted || batch.Status == domain.HumanBatchBlocked || batch.ReleasePolicy == "" {
|
|
return false
|
|
}
|
|
if strings.TrimSpace(batch.SourceBatchID) != "" && len(batch.Stages) == 1 && batch.Stages[0].ID == "prepackage_acceptance" {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// ListHumanBatchIDsAwaitingRelease implements
|
|
// Store.ListHumanBatchIDsAwaitingRelease (see the Mongo version's comment).
|
|
func (store *MemoryStore) ListHumanBatchIDsAwaitingRelease(_ context.Context) ([]string, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
ids := make([]string, 0)
|
|
for _, item := range store.humanItems {
|
|
if item.Status == domain.HumanItemAccepted && strings.TrimSpace(item.ReleasePartitionID) == "" && strings.TrimSpace(item.BatchID) != "" {
|
|
if batch, ok := store.humanBatches[strings.TrimSpace(item.BatchID)]; ok && !humanBatchCanSweepRelease(batch) {
|
|
continue
|
|
}
|
|
ids = append(ids, item.BatchID)
|
|
}
|
|
}
|
|
sort.Strings(ids)
|
|
// Distinct: a batch with several awaiting items must appear once.
|
|
deduped := ids[:0]
|
|
previous := ""
|
|
for _, id := range ids {
|
|
if id != previous {
|
|
deduped = append(deduped, id)
|
|
previous = id
|
|
}
|
|
}
|
|
return deduped, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetHumanBatch(_ context.Context, id string) (*domain.HumanBatchRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.humanBatches[strings.TrimSpace(id)]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
copy := clone(item)
|
|
return ©, nil
|
|
}
|
|
|
|
func normalizeHumanBatch(item domain.HumanBatchRecord) domain.HumanBatchRecord {
|
|
now := nowISO()
|
|
if item.ID == "" {
|
|
item.ID = domain.NewID("human-batch")
|
|
}
|
|
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
|
|
if item.Status == "" {
|
|
item.Status = domain.HumanBatchActive
|
|
}
|
|
if item.ReleasePolicy == "" {
|
|
item.ReleasePolicy = domain.HumanReleaseAcceptedPartition
|
|
}
|
|
if item.PartitionMaxItems <= 0 {
|
|
item.PartitionMaxItems = 32
|
|
}
|
|
if item.PartitionMaxWaitSeconds <= 0 {
|
|
item.PartitionMaxWaitSeconds = 60
|
|
}
|
|
if item.MaxReworkRounds <= 0 {
|
|
item.MaxReworkRounds = 5
|
|
}
|
|
if len(item.Stages) == 0 {
|
|
item.Stages = domain.DefaultAnnotationHumanStages()
|
|
}
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
item.UpdatedAt = now
|
|
return item
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertHumanBatch(_ context.Context, item domain.HumanBatchRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if store.supplierRecordBlockedLocked("humanBatches", item.ID, item) {
|
|
return nil
|
|
}
|
|
if e := store.supplierScrubLocked("humanBatches", item.ID, &item); e != nil {
|
|
return e
|
|
}
|
|
|
|
item = normalizeHumanBatch(item)
|
|
store.humanBatches[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListHumanItems(_ context.Context, organizationID string, query domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.HumanItemRecord, 0)
|
|
for _, item := range store.humanItems {
|
|
if humanOrganizationMatches(item.OrganizationID, organizationID) && humanItemMatches(item, query) && humanItemAfterCursor(item, query.Cursor) && blindAnnotationVisible(item, store.humanBatches[item.BatchID], query.BlindAnnotationActorID) {
|
|
if humanPoolFiltered(query) && !humanPoolSearchRowMatches(store.humanPoolSearchRowLocked(item, organizationID), query) {
|
|
continue
|
|
}
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool {
|
|
if items[i].CreatedAt != items[j].CreatedAt {
|
|
return items[i].CreatedAt > items[j].CreatedAt
|
|
}
|
|
return items[i].ID > items[j].ID
|
|
})
|
|
if query.Limit > 0 && len(items) > query.Limit {
|
|
items = items[:query.Limit]
|
|
}
|
|
if query.IdentityProjection {
|
|
for i := range items {
|
|
items[i] = domain.HumanItemRecord{ID: items[i].ID}
|
|
}
|
|
} else if query.PoolCandidateProjection {
|
|
for i := range items {
|
|
items[i] = items[i].PoolCandidate()
|
|
}
|
|
} else if query.BufferSummaryProjection {
|
|
for i := range items {
|
|
items[i] = annotationBufferProjectionClone(items[i])
|
|
}
|
|
} else if query.ListProjection || query.TaskListProjection {
|
|
keysRemoved := humanItemPayloadKeysRemoved(query)
|
|
for index := range items {
|
|
items[index] = humanItemListProjectionClone(items[index], keysRemoved)
|
|
}
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetHumanItem(_ context.Context, id string) (*domain.HumanItemRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.humanItems[strings.TrimSpace(id)]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
copy := clone(item)
|
|
return ©, nil
|
|
}
|
|
|
|
func normalizeHumanItem(item domain.HumanItemRecord) domain.HumanItemRecord {
|
|
now := nowISO()
|
|
if item.ID == "" {
|
|
item.ID = domain.NewID("human-item")
|
|
}
|
|
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
|
|
if item.Status == "" {
|
|
item.Status = domain.HumanItemReady
|
|
}
|
|
if item.CurrentStageID == "" {
|
|
item.CurrentStageID = "split_annotation"
|
|
}
|
|
if item.MaxReworkRounds <= 0 {
|
|
item.MaxReworkRounds = 5
|
|
}
|
|
if item.Version <= 0 {
|
|
item.Version = 1
|
|
}
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
item.UpdatedAt = now
|
|
return item
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertHumanItem(_ context.Context, item domain.HumanItemRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if store.supplierRecordBlockedLocked("humanItems", item.ID, item) {
|
|
return nil
|
|
}
|
|
if e := store.supplierScrubLocked("humanItems", item.ID, &item); e != nil {
|
|
return e
|
|
}
|
|
|
|
if _, deleting := store.projectDeletions["project-deletion-"+item.ProjectID]; deleting {
|
|
if _, exists := store.humanItems[item.ID]; !exists {
|
|
return nil
|
|
}
|
|
}
|
|
item = normalizeHumanItem(item)
|
|
store.humanItems[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpdateHumanItemVersioned(_ context.Context, item domain.HumanItemRecord, expectedVersion int) (bool, error) {
|
|
if err := prepareAnnotationRecord(&item); err != nil {
|
|
return false, err
|
|
}
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
existing, ok := store.humanItems[item.ID]
|
|
if !ok || existing.Version != expectedVersion {
|
|
return false, nil
|
|
}
|
|
if j := item.ProcessingJob; j != nil && j.Owner != "" && (existing.ProcessingJob == nil || existing.ProcessingJob.ID != j.ID || existing.ProcessingJob.Owner != j.Owner) {
|
|
return false, nil
|
|
}
|
|
item = normalizeHumanItem(item)
|
|
store.humanItems[item.ID] = clone(item)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListHumanStageAttempts(_ context.Context, organizationID string, itemID string) ([]domain.HumanStageAttemptRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.HumanStageAttemptRecord, 0)
|
|
for _, item := range store.humanStageAttempts {
|
|
if humanOrganizationMatches(item.OrganizationID, organizationID) && (itemID == "" || item.ItemID == itemID) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool {
|
|
if items[i].Round != items[j].Round {
|
|
return items[i].Round < items[j].Round
|
|
}
|
|
return items[i].CreatedAt < items[j].CreatedAt
|
|
})
|
|
return items, nil
|
|
}
|
|
|
|
func normalizeHumanAttempt(item domain.HumanStageAttemptRecord) domain.HumanStageAttemptRecord {
|
|
now := nowISO()
|
|
if item.ID == "" {
|
|
if item.ItemID != "" && item.StageID != "" {
|
|
item.ID = "human-attempt-" + item.ItemID + "-" + item.StageID + "-" + fmt.Sprint(item.Round)
|
|
} else {
|
|
item.ID = domain.NewID("human-attempt")
|
|
}
|
|
}
|
|
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
|
|
if item.Version <= 0 {
|
|
item.Version = 1
|
|
}
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
item.UpdatedAt = now
|
|
return item
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertHumanStageAttempt(_ context.Context, item domain.HumanStageAttemptRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if store.supplierRecordBlockedLocked("humanStageAttempts", item.ID, item) {
|
|
return nil
|
|
}
|
|
item = normalizeHumanAttempt(item)
|
|
// Attempts for the same (organization, item, stage, round) are one logical
|
|
// row even when their ids differ (review operations use operation-scoped
|
|
// ids). Replace the existing row so the unique-index semantics match the
|
|
// MongoStore tuple upsert.
|
|
for id, existing := range store.humanStageAttempts {
|
|
if existing.OrganizationID == item.OrganizationID && existing.ItemID == item.ItemID && existing.StageID == item.StageID && existing.Round == item.Round {
|
|
delete(store.humanStageAttempts, id)
|
|
}
|
|
}
|
|
store.humanStageAttempts[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListHumanEvents(_ context.Context, organizationID string, itemID string) ([]domain.HumanEventRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.HumanEventRecord, 0)
|
|
for _, item := range store.humanEvents {
|
|
if humanOrganizationMatches(item.OrganizationID, organizationID) && (itemID == "" || item.ItemID == itemID) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool { return items[i].CreatedAt < items[j].CreatedAt })
|
|
return items, nil
|
|
}
|
|
|
|
func normalizeHumanEvent(item domain.HumanEventRecord) domain.HumanEventRecord {
|
|
if item.ID == "" {
|
|
item.ID = domain.NewID("human-event")
|
|
}
|
|
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = nowISO()
|
|
}
|
|
return item
|
|
}
|
|
|
|
// AppendHumanEventOnce preserves the first durable recovery evidence on retries.
|
|
func (store *MemoryStore) AppendHumanEventOnce(_ context.Context, item domain.HumanEventRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item = normalizeHumanEvent(item)
|
|
if old, ok := store.humanEvents[item.ID]; ok {
|
|
if old.OrganizationID != item.OrganizationID || old.ItemID != item.ItemID {
|
|
return fmt.Errorf("human event recovery scope mismatch")
|
|
}
|
|
return nil
|
|
}
|
|
store.humanEvents[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) AppendHumanEvent(_ context.Context, item domain.HumanEventRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item = normalizeHumanEvent(item)
|
|
store.humanEvents[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func humanRejectMatches(item domain.HumanRejectRecord, query domain.HumanRejectQuery) bool {
|
|
return (query.BatchID == "" || item.BatchID == query.BatchID) &&
|
|
(query.ItemID == "" || item.ItemID == query.ItemID) &&
|
|
(query.ProjectID == "" || item.ProjectID == query.ProjectID) &&
|
|
(query.WorkflowID == "" || item.WorkflowID == query.WorkflowID) &&
|
|
(query.RunID == "" || item.RunID == query.RunID) &&
|
|
(query.Status == "" || item.Status == query.Status)
|
|
}
|
|
|
|
func normalizeHumanReject(item domain.HumanRejectRecord) domain.HumanRejectRecord {
|
|
now := nowISO()
|
|
if item.ID == "" {
|
|
item.ID = domain.NewID("human-reject")
|
|
}
|
|
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
|
|
if item.Status == "" {
|
|
item.Status = domain.HumanRejectOpen
|
|
}
|
|
if item.Version <= 0 {
|
|
item.Version = 1
|
|
}
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
item.UpdatedAt = now
|
|
return item
|
|
}
|
|
|
|
func (store *MemoryStore) ListHumanRejects(_ context.Context, organizationID string, query domain.HumanRejectQuery) ([]domain.HumanRejectRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.HumanRejectRecord, 0)
|
|
for _, item := range store.humanRejects {
|
|
if humanOrganizationMatches(item.OrganizationID, organizationID) && humanRejectMatches(item, query) && (query.BeforeTime == "" || item.CreatedAt < query.BeforeTime || (item.CreatedAt == query.BeforeTime && item.ID < query.BeforeID)) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool {
|
|
if items[i].CreatedAt == items[j].CreatedAt {
|
|
return items[i].ID > items[j].ID
|
|
}
|
|
return items[i].CreatedAt > items[j].CreatedAt
|
|
})
|
|
if query.Limit > 0 && len(items) > query.Limit {
|
|
items = items[:query.Limit]
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetHumanReject(_ context.Context, id string) (*domain.HumanRejectRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.humanRejects[strings.TrimSpace(id)]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
copy := clone(item)
|
|
return ©, nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertHumanReject(_ context.Context, item domain.HumanRejectRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item = normalizeHumanReject(item)
|
|
store.humanRejects[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func (store *MemoryStore) UpdateHumanRejectVersioned(_ context.Context, item domain.HumanRejectRecord, expectedVersion int) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
existing, ok := store.humanRejects[item.ID]
|
|
if !ok || existing.Version != expectedVersion {
|
|
return false, nil
|
|
}
|
|
item = normalizeHumanReject(item)
|
|
store.humanRejects[item.ID] = clone(item)
|
|
return true, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ListReleasePartitions(_ context.Context, organizationID string, batchID string) ([]domain.ReleasePartitionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
items := make([]domain.ReleasePartitionRecord, 0)
|
|
for _, item := range store.releasePartitions {
|
|
if humanOrganizationMatches(item.OrganizationID, organizationID) && (batchID == "" || item.BatchID == batchID) {
|
|
items = append(items, clone(item))
|
|
}
|
|
}
|
|
sort.Slice(items, func(i, j int) bool { return items[i].Sequence < items[j].Sequence })
|
|
return items, nil
|
|
}
|
|
|
|
func (store *MemoryStore) GetReleasePartitionByUniqueKey(_ context.Context, organizationID string, uniqueKey string) (*domain.ReleasePartitionRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
for _, item := range store.releasePartitions {
|
|
if humanOrganizationMatches(item.OrganizationID, organizationID) && item.UniqueKey == uniqueKey {
|
|
copy := clone(item)
|
|
return ©, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (store *MemoryStore) ClaimReleasePartition(_ context.Context, organizationID string, id string) (bool, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
item, ok := store.releasePartitions[id]
|
|
if !ok || !humanOrganizationMatches(item.OrganizationID, organizationID) ||
|
|
(item.Status != domain.ReleasePartitionPending && item.Status != domain.ReleasePartitionFailed) {
|
|
return false, nil
|
|
}
|
|
item.Status = domain.ReleasePartitionDispatching
|
|
item.UpdatedAt = nowISO()
|
|
store.releasePartitions[id] = clone(item)
|
|
return true, nil
|
|
}
|
|
|
|
func normalizeReleasePartition(item domain.ReleasePartitionRecord) domain.ReleasePartitionRecord {
|
|
now := nowISO()
|
|
if item.ID == "" {
|
|
item.ID = domain.NewID("human-partition")
|
|
}
|
|
item.OrganizationID = normalizedOrganizationID(item.OrganizationID)
|
|
if item.Status == "" {
|
|
item.Status = domain.ReleasePartitionPending
|
|
}
|
|
if item.CreatedAt == "" {
|
|
item.CreatedAt = now
|
|
}
|
|
item.UpdatedAt = now
|
|
return item
|
|
}
|
|
|
|
func (store *MemoryStore) UpsertReleasePartition(_ context.Context, item domain.ReleasePartitionRecord) error {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
if store.supplierRecordBlockedLocked("humanReleasePartitions", item.ID, item) {
|
|
return nil
|
|
}
|
|
if e := store.supplierScrubLocked("humanReleasePartitions", item.ID, &item); e != nil {
|
|
return e
|
|
}
|
|
|
|
item = normalizeReleasePartition(item)
|
|
store.releasePartitions[item.ID] = clone(item)
|
|
return nil
|
|
}
|
|
|
|
func humanBatchMongoFilter(organizationID string, query domain.HumanBatchQuery) bson.M {
|
|
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
|
|
if query.PluginID != "" {
|
|
filter["pluginId"] = query.PluginID
|
|
}
|
|
if query.ProjectID != "" {
|
|
filter["projectId"] = query.ProjectID
|
|
}
|
|
if query.WorkflowID != "" {
|
|
filter["workflowId"] = query.WorkflowID
|
|
}
|
|
if query.RunID != "" {
|
|
filter["runId"] = query.RunID
|
|
}
|
|
if query.Status != "" {
|
|
filter["status"] = query.Status
|
|
}
|
|
return filter
|
|
}
|
|
|
|
func humanItemMongoFilter(organizationID string, query domain.HumanItemQuery) bson.M {
|
|
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
|
|
if query.AvailableSupplierID != nil {
|
|
filter["supplierId"] = bson.M{"$in": bson.A{"", nil, *query.AvailableSupplierID}}
|
|
}
|
|
if query.AvailableProjectIDs != nil {
|
|
filter["projectId"] = bson.M{"$in": query.AvailableProjectIDs}
|
|
}
|
|
if query.IDs != nil {
|
|
filter["id"] = bson.M{"$in": query.IDs}
|
|
}
|
|
if query.LegacyTaskID != "" {
|
|
filter["legacyTaskId"] = query.LegacyTaskID
|
|
}
|
|
if query.NeedsReworkRouting {
|
|
filter["status"] = bson.M{"$in": bson.A{domain.HumanItemRework, domain.HumanItemClaimed, domain.HumanItemInProgress}}
|
|
filter["currentStageId"] = domain.FanyuV58StageAnnotationCombined
|
|
filter["$or"] = bson.A{bson.M{"reworkRoutingPending": true}, bson.M{"inheritedRework": true, "assignedUserId": ""}}
|
|
}
|
|
if query.InheritedUnassigned {
|
|
filter["inheritedRework"] = true
|
|
filter["assignedUserId"] = ""
|
|
filter["currentStageId"] = domain.FanyuV58StageAnnotationCombined
|
|
filter["status"] = domain.HumanItemRework
|
|
}
|
|
if query.ActorUserID != "" && query.AnnotationView == "" {
|
|
filter["$or"] = bson.A{bson.M{"assignedUserId": query.ActorUserID}, bson.M{"latestAuthorUserId": query.ActorUserID}}
|
|
if query.MinePoolOnly {
|
|
filter["$or"] = bson.A{bson.M{"assignedUserId": query.ActorUserID}, bson.M{"latestAuthorUserId": query.ActorUserID, "currentStageId": "quality_review", "status": bson.M{"$in": []string{"ready", "submitted"}}}}
|
|
}
|
|
}
|
|
if query.PluginID != "" {
|
|
filter["pluginId"] = query.PluginID
|
|
}
|
|
if query.BatchID != "" {
|
|
filter["batchId"] = query.BatchID
|
|
}
|
|
if query.ProjectID != "" {
|
|
if query.AvailableProjectIDs == nil || containsHumanItemID(query.AvailableProjectIDs, query.ProjectID) {
|
|
filter["projectId"] = query.ProjectID
|
|
} else {
|
|
filter["projectId"] = bson.M{"$in": []string{}}
|
|
}
|
|
}
|
|
if query.WorkflowID != "" {
|
|
filter["workflowId"] = query.WorkflowID
|
|
}
|
|
if query.RunID != "" {
|
|
filter["runId"] = query.RunID
|
|
}
|
|
if query.CurrentStageID != "" {
|
|
filter["currentStageId"] = query.CurrentStageID
|
|
}
|
|
if len(query.StageIDs) > 0 {
|
|
stages := make(bson.A, 0, len(query.StageIDs))
|
|
for _, stage := range query.StageIDs {
|
|
stages = append(stages, stage)
|
|
}
|
|
filter["currentStageId"] = bson.M{"$in": stages}
|
|
}
|
|
if query.Status != "" {
|
|
filter["status"] = query.Status
|
|
}
|
|
if query.UnassignedOnly {
|
|
filter["assignedUserId"] = bson.M{"$in": bson.A{"", nil}}
|
|
}
|
|
if len(query.Statuses) > 0 {
|
|
statuses := make(bson.A, 0, len(query.Statuses))
|
|
for _, status := range query.Statuses {
|
|
statuses = append(statuses, status)
|
|
}
|
|
filter["status"] = bson.M{"$in": statuses}
|
|
}
|
|
if query.Cursor != nil {
|
|
// Copy the base conditions before nesting them under $and: embedding
|
|
// the same map inside itself makes BSON encoding recurse forever and
|
|
// crashes the process (t8 blocker v1-blocker-cursor-stack-overflow).
|
|
base := make(bson.M, len(filter))
|
|
for key, value := range filter {
|
|
base[key] = value
|
|
}
|
|
filter["$and"] = bson.A{base, bson.M{"$or": bson.A{
|
|
bson.M{"createdAt": bson.M{"$lt": query.Cursor.CreatedAt}},
|
|
bson.M{"createdAt": query.Cursor.CreatedAt, "id": bson.M{"$lt": query.Cursor.ID}},
|
|
}}}
|
|
}
|
|
|
|
if query.AnnotationView != "" {
|
|
view := bson.M{}
|
|
if query.AnnotationView == "active" {
|
|
view = bson.M{"assignedUserId": query.ActorUserID, "currentStageId": bson.M{"$in": []string{"annotation_combined", "semantic_annotation", "split_annotation"}}, "status": bson.M{"$in": []string{"claimed", "in_progress", "rework"}}}
|
|
} else {
|
|
view = bson.M{"latestAuthorUserId": query.ActorUserID, "currentStageId": bson.M{"$nin": []string{"annotation_combined", "semantic_annotation", "split_annotation"}}}
|
|
}
|
|
return bson.M{"$and": bson.A{filter, view}}
|
|
}
|
|
return filter
|
|
}
|
|
|
|
// humanItemListProjection drops payload sub-documents that only the
|
|
// detail/workbench paths need (annotation drafts, disposition histories,
|
|
// restore evidence). Everything the list page and its enrichment read —
|
|
// sliceWorkflow status, acceptance aggregates (items/statistics), source
|
|
// video/artifact locators, duration — stays in the document.
|
|
func humanItemListProjection(query domain.HumanItemQuery) bson.M {
|
|
if query.PoolCandidateProjection {
|
|
return bson.M{"subject.payload": 0, "processingJob.input": 0, "poolSearch.text": 0, "poolSearch.source": 0}
|
|
}
|
|
if query.IdentityProjection {
|
|
return bson.M{"_id": 0, "id": 1}
|
|
}
|
|
if query.BufferSummaryProjection {
|
|
return bson.M{"_id": 0, "inheritedRework": 1, "id": 1, "organizationId": 1, "projectId": 1, "workflowId": 1, "runId": 1, "batchId": 1, "pluginId": 1, "legacyTaskId": 1, "assignedUserId": 1, "currentStageId": 1, "status": 1, "version": 1, "reviewRound": 1, "claimedAt": 1, "pendingCombinedSubmissionId": 1, "processingJob.sourceFingerprint": 1, "processingJob.sourceIndex": 1, "subject.payload.materialization.sourceIndex": 1, "processingJob.status": 1, "processingJob.kind": 1, "processingJob.error": 1, "subject.payload.sliceWorkflow.status": 1, "subject.payload.sliceWorkflow.phase": 1}
|
|
}
|
|
projection := bson.M{"processingJob.input": 0, "poolSearch.text": 0, "poolSearch.source": 0}
|
|
for _, key := range humanItemPayloadKeysRemoved(query) {
|
|
projection["subject.payload."+key] = 0
|
|
}
|
|
return projection
|
|
}
|
|
|
|
func (store *MongoStore) ListHumanBatches(ctx context.Context, organizationID string, query domain.HumanBatchQuery) ([]domain.HumanBatchRecord, error) {
|
|
return mongoList[domain.HumanBatchRecord](ctx, store.humanBatches, humanBatchMongoFilter(organizationID, query), bson.D{{Key: "createdAt", Value: -1}})
|
|
}
|
|
|
|
// HasRawHumanAnnotationBatch answers the work-buffer enablement question.
|
|
// The (organizationId, projectId, pluginId) index bounds the scan, and only
|
|
// the fields the strict three-role predicate reads are projected, so even a
|
|
// 12k-batch legacy project avoids decoding full batch documents.
|
|
func (store *MongoStore) HasRawHumanAnnotationBatch(ctx context.Context, organizationID string, projectID string) (bool, error) {
|
|
filter := bson.M{
|
|
"organizationId": normalizedOrganizationID(organizationID),
|
|
"projectId": projectID,
|
|
"pluginId": "annotation-qa",
|
|
"status": bson.M{"$ne": domain.HumanBatchCompleted},
|
|
"processingProfile": domain.ProcessingProfileRawHuman,
|
|
}
|
|
cursor, err := store.humanBatches.Find(ctx, filter, options.Find().SetProjection(bson.M{"stages": 1}))
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
for cursor.Next(ctx) {
|
|
var batch struct {
|
|
Stages []domain.HumanStageDefinition `bson:"stages"`
|
|
}
|
|
if err := cursor.Decode(&batch); err != nil {
|
|
return false, err
|
|
}
|
|
if domain.IsFanyuV58ThreeRoleHumanLoop(batch.Stages) {
|
|
return true, nil
|
|
}
|
|
}
|
|
return false, cursor.Err()
|
|
}
|
|
|
|
// ListHumanBatchIDsAwaitingRelease implements
|
|
// Store.ListHumanBatchIDsAwaitingRelease: distinct batch IDs that still have
|
|
// accepted items without a release partition — exactly the batches the
|
|
// background release sweeper (formerly the GET /api/human-batches flush) can
|
|
// act on.
|
|
func (store *MongoStore) ListHumanBatchIDsAwaitingRelease(ctx context.Context) ([]string, error) {
|
|
values, err := store.humanItems.Distinct(ctx, "batchId", bson.M{
|
|
"status": string(domain.HumanItemAccepted),
|
|
"releasePartitionId": bson.M{"$in": bson.A{nil, ""}},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
candidateIDs := make([]string, 0, len(values))
|
|
for _, value := range values {
|
|
if id, ok := value.(string); ok && id != "" {
|
|
candidateIDs = append(candidateIDs, id)
|
|
}
|
|
}
|
|
if len(candidateIDs) == 0 {
|
|
return []string{}, nil
|
|
}
|
|
cursor, err := store.humanBatches.Find(ctx, bson.M{
|
|
"id": bson.M{"$in": candidateIDs},
|
|
"status": bson.M{"$nin": bson.A{string(domain.HumanBatchCompleted), string(domain.HumanBatchBlocked)}},
|
|
"releasePolicy": bson.M{"$nin": bson.A{nil, ""}},
|
|
"sourceBatchId": bson.M{"$in": bson.A{nil, ""}},
|
|
}, options.Find().SetProjection(bson.M{"_id": 0, "id": 1}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
ids := make([]string, 0, len(candidateIDs))
|
|
for cursor.Next(ctx) {
|
|
var row struct {
|
|
ID string `bson:"id"`
|
|
}
|
|
if err := cursor.Decode(&row); err != nil {
|
|
return nil, err
|
|
}
|
|
if row.ID != "" {
|
|
ids = append(ids, row.ID)
|
|
}
|
|
}
|
|
if err := cursor.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
sort.Strings(ids)
|
|
return ids, nil
|
|
}
|
|
|
|
func (store *MongoStore) GetHumanBatch(ctx context.Context, id string) (*domain.HumanBatchRecord, error) {
|
|
return mongoGetByID[domain.HumanBatchRecord](ctx, store.humanBatches, id)
|
|
}
|
|
|
|
func (store *MongoStore) UpsertHumanBatch(ctx context.Context, item domain.HumanBatchRecord) error {
|
|
item = normalizeHumanBatch(item)
|
|
return mongoUpsert(ctx, store.humanBatches, item.ID, item)
|
|
}
|
|
|
|
func (store *MongoStore) ListHumanItems(ctx context.Context, organizationID string, query domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
|
// Personal pages must include a just-claimed/returned task before async
|
|
// discovery catches up. These indexed actor/history scopes stay live.
|
|
if query.PoolCandidateProjection && query.ActorUserID == "" && !query.ReviewedPool && !query.MinePoolOnly {
|
|
if target, err := store.poolReplica(ctx); err != nil {
|
|
return nil, err
|
|
} else if target != nil {
|
|
return store.replicaPoolCandidates(ctx, target, organizationID, query)
|
|
}
|
|
}
|
|
if query.NeedsReworkRouting {
|
|
if err := store.validateDeletedAuthorRoutingIndexes(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if humanPoolFiltered(query) {
|
|
return store.listHumanPoolSearch(ctx, organizationID, query)
|
|
}
|
|
findOptions := options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}})
|
|
if query.Limit > 0 {
|
|
findOptions.SetLimit(int64(query.Limit))
|
|
}
|
|
if query.PoolCandidateProjection || query.IdentityProjection || query.BufferSummaryProjection || query.ListProjection || query.TaskListProjection {
|
|
findOptions.SetProjection(humanItemListProjection(query))
|
|
}
|
|
filter := humanItemMongoFilter(organizationID, query)
|
|
if query.BlindAnnotationActorID != "" {
|
|
visibility, err := store.blindAnnotationMongoVisibility(ctx, organizationID, query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
filter = bson.M{"$and": bson.A{filter, visibility}}
|
|
}
|
|
cursor, err := store.humanItems.Find(ctx, filter, findOptions)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.HumanItemRecord](ctx, cursor)
|
|
}
|
|
|
|
func (store *MongoStore) GetHumanItem(ctx context.Context, id string) (*domain.HumanItemRecord, error) {
|
|
return mongoGetByID[domain.HumanItemRecord](ctx, store.humanItems, id)
|
|
}
|
|
|
|
func (store *MongoStore) UpsertHumanItem(ctx context.Context, item domain.HumanItemRecord) error {
|
|
item = normalizeHumanItem(item)
|
|
return mongoUpsert(ctx, store.humanItems, item.ID, item)
|
|
}
|
|
|
|
func (store *MongoStore) UpdateHumanItemVersioned(ctx context.Context, item domain.HumanItemRecord, expectedVersion int) (bool, error) {
|
|
if err := prepareAnnotationRecord(&item); err != nil {
|
|
return false, err
|
|
}
|
|
updated := false
|
|
e := supplierGuardedMongoWrite(ctx, store.humanItems, item.ID, item, func(ctx context.Context, d bson.M) error {
|
|
filter := bson.M{"id": item.ID, "version": expectedVersion}
|
|
if j := item.ProcessingJob; j != nil && j.Owner != "" {
|
|
filter["processingJob.id"] = j.ID
|
|
filter["processingJob.owner"] = j.Owner
|
|
}
|
|
r, e := store.humanItems.ReplaceOne(ctx, filter, d)
|
|
if e == nil {
|
|
updated = r.MatchedCount == 1
|
|
}
|
|
return e
|
|
})
|
|
return updated, e
|
|
}
|
|
|
|
func (store *MongoStore) ListHumanStageAttempts(ctx context.Context, organizationID string, itemID string) ([]domain.HumanStageAttemptRecord, error) {
|
|
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
|
|
if itemID != "" {
|
|
filter["itemId"] = itemID
|
|
}
|
|
return mongoList[domain.HumanStageAttemptRecord](ctx, store.humanStageAttempts, filter, bson.D{{Key: "round", Value: 1}, {Key: "createdAt", Value: 1}})
|
|
}
|
|
|
|
func (store *MongoStore) UpsertHumanStageAttempt(ctx context.Context, item domain.HumanStageAttemptRecord) error {
|
|
item = normalizeHumanAttempt(item)
|
|
// Upsert on the logical tuple (organization, item, stage, round) instead of
|
|
// the record id: review operations create attempt rows with operation-
|
|
// scoped ids, and an id-only upsert would insert a second row for the same
|
|
// tuple and fail on organizationId_itemId_stageId_round_unique (E11000),
|
|
// which surfaced as a 500 on the reviewer claim path.
|
|
tuple := bson.M{"organizationId": item.OrganizationID, "itemId": item.ItemID, "stageId": item.StageID, "round": item.Round}
|
|
return supplierGuardedMongoWrite(ctx, store.humanStageAttempts, item.ID, item, func(ctx context.Context, d bson.M) error {
|
|
if _, e := store.humanStageAttempts.UpdateOne(ctx, tuple, bson.M{"$set": d}, options.Update().SetUpsert(true)); e != nil {
|
|
// The id-first path (identical id, different tuple spelling) stays valid.
|
|
if mongo.IsDuplicateKeyError(e) {
|
|
if _, e2 := store.humanStageAttempts.UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": d}, options.Update().SetUpsert(true)); e2 != nil {
|
|
return e2
|
|
}
|
|
if _, e3 := store.humanStageAttempts.UpdateOne(ctx, tuple, bson.M{"$set": d}, options.Update().SetUpsert(true)); e3 != nil {
|
|
return e3
|
|
}
|
|
return nil
|
|
}
|
|
return e
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (store *MongoStore) ListHumanEvents(ctx context.Context, organizationID string, itemID string) ([]domain.HumanEventRecord, error) {
|
|
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
|
|
if itemID != "" {
|
|
filter["itemId"] = itemID
|
|
}
|
|
return mongoList[domain.HumanEventRecord](ctx, store.humanEvents, filter, bson.D{{Key: "createdAt", Value: 1}})
|
|
}
|
|
|
|
func (store *MongoStore) AppendHumanEventOnce(ctx context.Context, item domain.HumanEventRecord) error {
|
|
item = normalizeHumanEvent(item)
|
|
return supplierGuardedMongoWrite(ctx, store.humanEvents, item.ID, item, func(ctx context.Context, d bson.M) error {
|
|
_, e := store.humanEvents.UpdateOne(ctx, bson.M{"id": item.ID, "organizationId": item.OrganizationID, "itemId": item.ItemID}, bson.M{"$setOnInsert": d}, options.Update().SetUpsert(true))
|
|
return e
|
|
})
|
|
}
|
|
|
|
func (store *MongoStore) AppendHumanEvent(ctx context.Context, item domain.HumanEventRecord) error {
|
|
item = normalizeHumanEvent(item)
|
|
return supplierGuardedMongoWrite(ctx, store.humanEvents, item.ID, item, func(ctx context.Context, d bson.M) error { _, e := store.humanEvents.InsertOne(ctx, d); return e })
|
|
}
|
|
|
|
func humanRejectMongoFilter(organizationID string, query domain.HumanRejectQuery) bson.M {
|
|
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
|
|
if query.BatchID != "" {
|
|
filter["batchId"] = query.BatchID
|
|
}
|
|
if query.ItemID != "" {
|
|
filter["itemId"] = query.ItemID
|
|
}
|
|
if query.ProjectID != "" {
|
|
filter["projectId"] = query.ProjectID
|
|
}
|
|
if query.WorkflowID != "" {
|
|
filter["workflowId"] = query.WorkflowID
|
|
}
|
|
if query.RunID != "" {
|
|
filter["runId"] = query.RunID
|
|
}
|
|
if query.Status != "" {
|
|
filter["status"] = query.Status
|
|
}
|
|
return filter
|
|
}
|
|
|
|
func (store *MongoStore) ListHumanRejects(ctx context.Context, organizationID string, query domain.HumanRejectQuery) ([]domain.HumanRejectRecord, error) {
|
|
filter := humanRejectMongoFilter(organizationID, query)
|
|
if query.BeforeTime != "" {
|
|
filter["$or"] = bson.A{bson.M{"createdAt": bson.M{"$lt": query.BeforeTime}}, bson.M{"createdAt": query.BeforeTime, "id": bson.M{"$lt": query.BeforeID}}}
|
|
}
|
|
opts := options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}})
|
|
if query.Limit > 0 {
|
|
opts.SetLimit(int64(query.Limit))
|
|
}
|
|
cur, err := store.humanRejects.Find(ctx, filter, opts)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return decodeMany[domain.HumanRejectRecord](ctx, cur)
|
|
}
|
|
|
|
func (store *MongoStore) GetHumanReject(ctx context.Context, id string) (*domain.HumanRejectRecord, error) {
|
|
return mongoGetByID[domain.HumanRejectRecord](ctx, store.humanRejects, id)
|
|
}
|
|
|
|
func (store *MongoStore) UpsertHumanReject(ctx context.Context, item domain.HumanRejectRecord) error {
|
|
item = normalizeHumanReject(item)
|
|
return mongoUpsert(ctx, store.humanRejects, item.ID, item)
|
|
}
|
|
|
|
func (store *MongoStore) UpdateHumanRejectVersioned(ctx context.Context, item domain.HumanRejectRecord, expectedVersion int) (bool, error) {
|
|
item = normalizeHumanReject(item)
|
|
result, err := store.humanRejects.ReplaceOne(ctx, bson.M{"id": item.ID, "version": expectedVersion}, item)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.MatchedCount == 1, nil
|
|
}
|
|
|
|
func (store *MongoStore) ListReleasePartitions(ctx context.Context, organizationID string, batchID string) ([]domain.ReleasePartitionRecord, error) {
|
|
filter := bson.M{"organizationId": normalizedOrganizationID(organizationID)}
|
|
if batchID != "" {
|
|
filter["batchId"] = batchID
|
|
}
|
|
return mongoList[domain.ReleasePartitionRecord](ctx, store.releasePartitions, filter, bson.D{{Key: "sequence", Value: 1}})
|
|
}
|
|
|
|
func (store *MongoStore) GetReleasePartitionByUniqueKey(ctx context.Context, organizationID string, uniqueKey string) (*domain.ReleasePartitionRecord, error) {
|
|
var item domain.ReleasePartitionRecord
|
|
err := store.releasePartitions.FindOne(ctx, bson.M{"organizationId": normalizedOrganizationID(organizationID), "uniqueKey": uniqueKey}).Decode(&item)
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
func (store *MongoStore) ClaimReleasePartition(ctx context.Context, organizationID string, id string) (bool, error) {
|
|
result, err := store.releasePartitions.UpdateOne(
|
|
ctx,
|
|
bson.M{
|
|
"id": id,
|
|
"organizationId": normalizedOrganizationID(organizationID),
|
|
"status": bson.M{"$in": bson.A{
|
|
string(domain.ReleasePartitionPending),
|
|
string(domain.ReleasePartitionFailed),
|
|
}},
|
|
},
|
|
bson.M{"$set": bson.M{"status": string(domain.ReleasePartitionDispatching), "updatedAt": nowISO()}},
|
|
)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return result.ModifiedCount == 1, nil
|
|
}
|
|
|
|
func (store *MongoStore) UpsertReleasePartition(ctx context.Context, item domain.ReleasePartitionRecord) error {
|
|
item = normalizeReleasePartition(item)
|
|
return supplierGuardedMongoWrite(ctx, store.releasePartitions, item.ID, item, func(ctx context.Context, d bson.M) error {
|
|
_, e := store.releasePartitions.UpdateOne(ctx, bson.M{"organizationId": item.OrganizationID, "uniqueKey": item.UniqueKey}, bson.M{"$set": d}, options.Update().SetUpsert(true))
|
|
return e
|
|
})
|
|
}
|
|
|
|
// ListHumanBatchAccessRecords omits large batch payloads. The projection is only
|
|
// for list authorization/stage checks, never for updating a batch.
|
|
func (store *MongoStore) ListHumanBatchAccessRecords(ctx context.Context, organizationID string, ids []string) ([]domain.HumanBatchRecord, error) {
|
|
if b, ok := ctx.Value(queryReplicaPoolReadKey{}).(*queryReplicaPoolBarrier); ok && b.counts {
|
|
if target, err := store.poolReplica(ctx); err != nil {
|
|
return nil, err
|
|
} else if target != nil {
|
|
return store.replicaBatchAccess(ctx, target, organizationID, ids)
|
|
}
|
|
}
|
|
return store.listAuthoritativeBatchAccess(ctx, organizationID, ids)
|
|
}
|
|
func (store *MongoStore) listAuthoritativeBatchAccess(ctx context.Context, organizationID string, ids []string) ([]domain.HumanBatchRecord, error) {
|
|
if store.queryReplicaFactStore {
|
|
return store.cachedReplicaBatchAccess(ctx, organizationID, ids)
|
|
}
|
|
return store.readBatchAccess(ctx, organizationID, ids)
|
|
}
|
|
func (store *MongoStore) readBatchAccess(ctx context.Context, organizationID string, ids []string) ([]domain.HumanBatchRecord, error) {
|
|
if len(ids) == 0 {
|
|
return []domain.HumanBatchRecord{}, nil
|
|
}
|
|
cursor, err := store.humanBatches.Find(ctx, bson.M{"organizationId": normalizedOrganizationID(organizationID), "id": bson.M{"$in": ids}}, options.Find().SetProjection(bson.M{"_id": 0, "id": 1, "organizationId": 1, "projectId": 1, "stages": 1, "processingProfile": 1, "status": 1, "sourceBatchId": 1, "processingScope": 1, "requirePrePackageAcceptance": 1}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
var items []domain.HumanBatchRecord
|
|
if err := cursor.All(ctx, &items); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
func (store *MemoryStore) ListHumanBatchAccessRecords(ctx context.Context, organizationID string, ids []string) ([]domain.HumanBatchRecord, error) {
|
|
items := make([]domain.HumanBatchRecord, 0, len(ids))
|
|
for _, id := range ids {
|
|
b, err := store.GetHumanBatch(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if b != nil && b.OrganizationID == normalizedOrganizationID(organizationID) {
|
|
items = append(items, domain.HumanBatchRecord{ID: b.ID, OrganizationID: b.OrganizationID, ProjectID: b.ProjectID, Stages: b.Stages, ProcessingProfile: b.ProcessingProfile, Status: b.Status, SourceBatchID: b.SourceBatchID, ProcessingScope: b.ProcessingScope, RequirePrePackageAcceptance: b.RequirePrePackageAcceptance})
|
|
}
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
func containsHumanItemID(ids []string, id string) bool {
|
|
for _, candidate := range ids {
|
|
if candidate == id {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// Query decision events, not current assignments: completion releases ownership.
|
|
func (store *MemoryStore) ListReviewedHumanItemIDs(_ context.Context, organizationID, actorID string) ([]string, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
ids := []string{}
|
|
seen := map[string]bool{}
|
|
for _, event := range store.humanEvents {
|
|
if humanOrganizationMatches(event.OrganizationID, organizationID) && event.ActorUserID == actorID && (event.EventType == "approved" || event.EventType == "rejected" || event.EventType == "combined_quality_approved" || event.EventType == "combined_quality_rejected") && !seen[event.ItemID] {
|
|
seen[event.ItemID] = true
|
|
ids = append(ids, event.ItemID)
|
|
}
|
|
}
|
|
return ids, nil
|
|
}
|
|
func (store *MongoStore) ListReviewedHumanItemIDs(ctx context.Context, organizationID, actorID string) ([]string, error) {
|
|
if target, err := store.poolReplica(ctx); err != nil {
|
|
return nil, err
|
|
} else if target != nil {
|
|
return target.ListReviewedHumanItemIDs(ctx, organizationID, actorID)
|
|
}
|
|
values, err := store.humanEvents.Distinct(ctx, "itemId", bson.M{"organizationId": normalizedOrganizationID(organizationID), "actorUserId": actorID, "eventType": bson.M{"$in": []string{"approved", "rejected", "combined_quality_approved", "combined_quality_rejected"}}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ids := []string{}
|
|
for _, value := range values {
|
|
if id, ok := value.(string); ok {
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
// Only enrich the visible page; the caller still applies normal task access
|
|
// checks. One aggregate for the page, never one history query per task.
|
|
func (store *MongoStore) ListLatestReviewEvents(ctx context.Context, org, actor string, ids []string) (map[string]domain.HumanEventRecord, error) {
|
|
result := map[string]domain.HumanEventRecord{}
|
|
if len(ids) == 0 {
|
|
return result, nil
|
|
}
|
|
cursor, err := store.humanEvents.Aggregate(ctx, mongo.Pipeline{
|
|
{{Key: "$match", Value: bson.M{"organizationId": normalizedOrganizationID(org), "actorUserId": actor, "itemId": bson.M{"$in": ids}, "eventType": bson.M{"$in": []string{"approved", "rejected", "combined_quality_approved", "combined_quality_rejected"}}}}},
|
|
{{Key: "$project", Value: bson.M{"_id": 0, "id": 1, "itemId": 1, "eventType": 1, "stageId": 1, "round": 1, "createdAt": 1, "itemVersion": 1, "payload.reviewedRevision": 1}}},
|
|
{{Key: "$sort", Value: bson.D{{Key: "itemId", Value: 1}, {Key: "itemVersion", Value: -1}, {Key: "createdAt", Value: -1}, {Key: "id", Value: -1}}}},
|
|
{{Key: "$group", Value: bson.M{"_id": "$itemId", "event": bson.M{"$first": "$$ROOT"}}}},
|
|
{{Key: "$replaceRoot", Value: bson.M{"newRoot": "$event"}}},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer cursor.Close(ctx)
|
|
for cursor.Next(ctx) {
|
|
var event domain.HumanEventRecord
|
|
if err := cursor.Decode(&event); err != nil {
|
|
return nil, err
|
|
}
|
|
result[event.ItemID] = event
|
|
}
|
|
return result, cursor.Err()
|
|
}
|
|
func (store *MemoryStore) ListLatestReviewEvents(_ context.Context, org, actor string, ids []string) (map[string]domain.HumanEventRecord, error) {
|
|
store.mu.Lock()
|
|
defer store.mu.Unlock()
|
|
result := map[string]domain.HumanEventRecord{}
|
|
for _, event := range store.humanEvents {
|
|
if !humanOrganizationMatches(event.OrganizationID, org) || event.ActorUserID != actor || !containsHumanItemID(ids, event.ItemID) {
|
|
continue
|
|
}
|
|
switch event.EventType {
|
|
case "approved", "rejected", "combined_quality_approved", "combined_quality_rejected":
|
|
default:
|
|
continue
|
|
}
|
|
old, found := result[event.ItemID]
|
|
if !found || event.ItemVersion > old.ItemVersion || (event.ItemVersion == old.ItemVersion && (event.CreatedAt > old.CreatedAt || (event.CreatedAt == old.CreatedAt && event.ID > old.ID))) {
|
|
result[event.ItemID] = event
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|