forked from longtao.wu/droboticflow
216 lines
6.9 KiB
Go
216 lines
6.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math/rand/v2"
|
|
"sort"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
// Recheck after the formal decision is committed. If deletion won while the
|
|
// reviewer was submitting, its durable routing marker still finds this task.
|
|
func (s *server) resolveReworkAuthor(ctx context.Context, item domain.HumanItemRecord) error {
|
|
user, err := s.store.GetUser(ctx, item.LatestAuthorUserID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
deleted := user != nil && (user.DeletedAt != "" || user.DeletionRequestedAt != "")
|
|
if !deleted {
|
|
ok, err := s.store.ClearReworkRoutingPending(ctx, item.ID, item.Version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return store.ErrHumanClaimConflict
|
|
}
|
|
return nil
|
|
}
|
|
previous := item.Version
|
|
item.ReworkRoutingPending = false
|
|
if deleted {
|
|
item.InheritedRework = true
|
|
if item.AssignedUserID == user.ID {
|
|
item.AssignedUserID = ""
|
|
item.ClaimedAt = ""
|
|
item.Status = domain.HumanItemRework
|
|
}
|
|
}
|
|
item.Version++
|
|
item.UpdatedAt = nowISO()
|
|
ok, err := s.store.UpdateHumanItemVersioned(ctx, item, previous)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !ok {
|
|
return store.ErrHumanClaimConflict
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type reworkCandidate struct {
|
|
user domain.UserRecord
|
|
load int
|
|
}
|
|
type reworkCandidateCacheKey struct{}
|
|
type reworkCandidateCache struct {
|
|
byProject map[string][]reworkCandidate
|
|
reserved map[string]bool
|
|
}
|
|
|
|
func (s *server) dispatchDeletedAuthorReworks(ctx context.Context) error {
|
|
ctx = context.WithValue(ctx, reworkCandidateCacheKey{}, &reworkCandidateCache{byProject: map[string][]reworkCandidate{}, reserved: map[string]bool{}})
|
|
orgs, err := s.store.ListOrganizations(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var first error
|
|
for _, org := range orgs {
|
|
if org.Status != domain.OrganizationStatusActive {
|
|
continue
|
|
}
|
|
items, err := s.store.ListHumanItems(ctx, org.ID, domain.HumanItemQuery{NeedsReworkRouting: true})
|
|
if err != nil {
|
|
if first == nil {
|
|
first = err
|
|
}
|
|
continue
|
|
}
|
|
for _, item := range items {
|
|
if item.AccountHandoff != nil && item.AccountHandoff.Pending {
|
|
continue
|
|
}
|
|
if item.ReworkRoutingPending {
|
|
if err := s.resolveReworkAuthor(ctx, item); err != nil {
|
|
if first == nil {
|
|
first = err
|
|
}
|
|
continue
|
|
}
|
|
fresh, err := s.store.GetHumanItem(ctx, item.ID)
|
|
if err != nil || fresh == nil {
|
|
continue
|
|
}
|
|
item = *fresh
|
|
}
|
|
if !item.InheritedRework || item.AssignedUserID != "" || item.Status != domain.HumanItemRework {
|
|
continue
|
|
}
|
|
if err := s.assignDeletedAuthorRework(ctx, item); err != nil && first == nil {
|
|
first = err
|
|
}
|
|
}
|
|
}
|
|
return first
|
|
}
|
|
|
|
func (s *server) assignDeletedAuthorRework(ctx context.Context, item domain.HumanItemRecord) error {
|
|
if item.SupplierID == "" {
|
|
return nil
|
|
} // Never guess a supplier for legacy data.
|
|
supplier, err := s.store.GetSupplier(ctx, item.SupplierID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if supplier == nil || supplier.OrganizationID != item.OrganizationID || supplier.Status != "active" {
|
|
return nil
|
|
}
|
|
users, err := s.store.ListUsersByOrganization(ctx, item.OrganizationID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
batch, err := s.store.GetHumanBatch(ctx, item.BatchID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if batch == nil || batch.Status == domain.HumanBatchCompleted {
|
|
return nil
|
|
}
|
|
baseline, err := s.store.LoadInheritedReworkBaseline(ctx, item)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if baseline == nil || len(baseline.Slices) == 0 {
|
|
return nil
|
|
}
|
|
legacy := domain.AnnotationTaskRecord{ID: item.LegacyTaskID, OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, SupplierID: item.SupplierID, ProcessingScope: item.ProcessingScope, ProcessingProfile: item.ProcessingProfile}
|
|
legacy.SupplierPool = batch.ProcessingProfile == domain.ProcessingProfileRawHuman && domain.IsFanyuV58ThreeRoleHumanLoop(batch.Stages)
|
|
eligible := []reworkCandidate{}
|
|
memo, _ := ctx.Value(reworkCandidateCacheKey{}).(*reworkCandidateCache)
|
|
cacheKey := item.OrganizationID + "/" + item.ProjectID + "/" + item.SupplierID
|
|
cached := false
|
|
if memo != nil {
|
|
eligible, cached = memo.byProject[cacheKey]
|
|
}
|
|
if !cached {
|
|
for _, user := range users {
|
|
if user.Role != domain.UserRoleAnnotator || user.Status != domain.UserStatusActive || user.DeletedAt != "" || user.DeletionRequestedAt != "" || user.SupplierID != item.SupplierID {
|
|
continue
|
|
}
|
|
members, err := s.annotationMembershipsForUser(ctx, item.OrganizationID, item.ProjectID, user.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !membershipHasRole(members, legacy, domain.AnnotationRoleAnnotator) {
|
|
continue
|
|
}
|
|
load, inherited := 0, 0
|
|
err = s.store.VisitHumanPoolCountCandidates(ctx, item.OrganizationID, domain.HumanItemQuery{ActorUserID: user.ID, MinePoolOnly: true, CurrentStageID: domain.FanyuV58StageAnnotationCombined}, func(page []domain.HumanItemRecord) error {
|
|
for _, owned := range page {
|
|
if owned.AssignedUserID == user.ID && humanItemPending(owned.Status) {
|
|
load++
|
|
if owned.InheritedRework {
|
|
inherited++
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if inherited > 0 {
|
|
continue
|
|
}
|
|
eligible = append(eligible, reworkCandidate{user, load})
|
|
}
|
|
|
|
if memo != nil {
|
|
memo.byProject[cacheKey] = eligible
|
|
}
|
|
}
|
|
rand.Shuffle(len(eligible), func(i, j int) { eligible[i], eligible[j] = eligible[j], eligible[i] })
|
|
sort.SliceStable(eligible, func(i, j int) bool { return eligible[i].load < eligible[j].load })
|
|
for _, candidate := range eligible {
|
|
if memo != nil && memo.reserved[candidate.user.ID] {
|
|
continue
|
|
}
|
|
next := item
|
|
next.Subject = item.Subject
|
|
next.AssignedUserID = candidate.user.ID
|
|
next.Status = domain.HumanItemClaimed
|
|
next.ClaimedAt = nowISO()
|
|
next.UpdatedAt = next.ClaimedAt
|
|
next.Version++
|
|
id := fmt.Sprintf("deleted-author:%s:%d:%s", item.ID, item.Version, candidate.user.ID)
|
|
_, err := s.store.CommitHumanClaim(ctx, store.HumanClaimCommit{EnforceBuffer: legacy.SupplierPool, Receipt: store.HumanClaimReceipt{ID: id, ItemID: item.ID, UserID: candidate.user.ID, ProjectID: item.ProjectID, OrganizationID: item.OrganizationID}, Item: next, ExpectedVersion: item.Version, TaskStatus: domain.AnnotationTaskStatusRework, Attempt: domain.HumanStageAttemptRecord{OrganizationID: item.OrganizationID, ItemID: item.ID, StageID: item.CurrentStageID, Round: item.ReviewRound, Status: domain.HumanItemClaimed, AssigneeUserID: candidate.user.ID}, Event: domain.HumanEventRecord{ID: id, OrganizationID: item.OrganizationID, BatchID: item.BatchID, ItemID: item.ID, StageID: item.CurrentStageID, Round: item.ReviewRound, EventType: "deleted_author_rework_assigned", ActorUserID: candidate.user.ID, ItemVersion: next.Version, CreatedAt: next.ClaimedAt}})
|
|
if err == nil {
|
|
if memo != nil {
|
|
memo.reserved[candidate.user.ID] = true
|
|
}
|
|
return nil
|
|
}
|
|
if errors.Is(err, store.ErrAnnotationBufferFull) {
|
|
if memo != nil {
|
|
memo.reserved[candidate.user.ID] = true
|
|
}
|
|
continue
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|