annotationReadCache handed the same *HumanItemRecord to every lookup in a request, and write handlers mutate the item they load (version, status, assignment, payload keys) before the versioned write. After a failed or conflicting write, later lookups in that request would see unsaved state. Keep the single store read per request but return a copy per lookup, with the payload, assignment and processing-job input maps cloned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
60 lines
2.4 KiB
Go
60 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
type countingHumanItemStore struct {
|
|
store.Store
|
|
lists int
|
|
}
|
|
|
|
func (s *countingHumanItemStore) ListHumanItems(ctx context.Context, org string, query domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
|
s.lists++
|
|
return s.Store.ListHumanItems(ctx, org, query)
|
|
}
|
|
|
|
// Review 2026-10-18: the request cache returned one shared pointer, so a
|
|
// handler's unsaved changes (before a failed versioned write) leaked into every
|
|
// later lookup in the same request. Lookups must stay cached but independent.
|
|
func TestAnnotationReadCacheReturnsIndependentCopies(t *testing.T) {
|
|
ctx := context.Background()
|
|
counting := &countingHumanItemStore{Store: store.NewMemoryStore()}
|
|
item := domain.HumanItemRecord{ID: "item", OrganizationID: "org", PluginID: "annotation-qa", ProjectID: "project", LegacyTaskID: "task", Version: 3, Status: domain.HumanItemClaimed,
|
|
Subject: domain.HumanSubjectRef{Payload: map[string]any{"annotationWorkspace": map[string]any{"saved": true}}}}
|
|
if err := counting.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &server{store: counting}
|
|
ctx = context.WithValue(ctx, annotationReadCacheKey{}, &annotationRequestReads{items: map[string]*domain.HumanItemRecord{}})
|
|
task := domain.AnnotationTaskRecord{ID: "task", OrganizationID: "org", ProjectID: "project"}
|
|
|
|
first, err := s.humanItemForLegacyTask(ctx, task)
|
|
if err != nil || first == nil {
|
|
t.Fatalf("first lookup = %v, %v", first, err)
|
|
}
|
|
first.Version++
|
|
first.Status = domain.HumanItemSubmitted
|
|
first.Subject.Payload["annotationWorkspace"] = map[string]any{"saved": false}
|
|
|
|
second, err := s.humanItemForLegacyTask(ctx, task)
|
|
if err != nil || second == nil {
|
|
t.Fatalf("second lookup = %v, %v", second, err)
|
|
}
|
|
if second.Version != 3 || second.Status != domain.HumanItemClaimed || mapValue(second.Subject.Payload["annotationWorkspace"])["saved"] != true {
|
|
t.Fatalf("cached lookup saw unsaved changes: version=%d status=%s payload=%v", second.Version, second.Status, second.Subject.Payload)
|
|
}
|
|
second.Subject.Payload["extra"] = true
|
|
third, _ := s.humanItemForLegacyTask(ctx, task)
|
|
if _, leaked := third.Subject.Payload["extra"]; leaked {
|
|
t.Fatal("payload key added to one lookup leaked into the next")
|
|
}
|
|
if counting.lists != 1 {
|
|
t.Fatalf("store read %d times, want the request cache to keep it at 1", counting.lists)
|
|
}
|
|
}
|