droboticflow/internal/api/annotation_task_list_optimization_test.go
d-robotics ba5c9909e5 ⚡ Fix annotation-center slow loads across roles: filter pushdown, per-task draft join, manager fast path, workload cache, slow-request tracing
- Task pool: push status/supplier/workflow/run/node/artifact equality filters into ListAnnotationTasksFiltered (new store method, backed by organizationId+createdAt indexes) instead of post-filtering an organization-wide scan
- Human items join: ListHumanItemsByLegacyTaskIDs fetches only the listed tasks' items via the organizationId+legacyTaskId index; TaskListProjection now drops annotationDraft too
- Draft progress: ListHumanItemDraftProgress loads slice/segment drafts per filtered task only (batched $in, new projection reads just legacyTaskId + draft payload); draft overrides revision only when the human item was touched after the latest slice revision
- Manager fast path: admin views skip the request-scoped batch snapshot, membership and supplier-pool reads entirely
- Workload summary: 45s per-user response cache (any /api write invalidates), mirroring the dashboard workforce route
- Tracing: any /api request over 1s now logs method/path/duration so page-level latency regressions surface without per-handler instrumentation; task list log gains drafts_ms segment
- Tests: draft-progress override semantics, manager scan-free path, store filter pushdown; v5 fixture users get SupplierID to match the supplier-unified account model (pre-existing red since c92bca16, 18 stock failures recovered green)

Co-authored-by: AgentTeams slow-page-fix-round3
2026-10-11 22:14:43 +05:30

175 lines
7.5 KiB
Go

package api
import (
"context"
"encoding/json"
"net/http"
"testing"
"time"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
// TestTaskListDraftProgressStillDrivesPoolProgress verifies the per-task draft
// join: an in-progress draft newer than the latest slice revision overrides
// revision-derived progress, while an older draft does not resurrect stale
// slice counts.
func TestTaskListDraftProgressStillDrivesPoolProgress(t *testing.T) {
fixture := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticReview, domain.HumanItemReady, "", 1)
if err := fixture.store.UpsertSliceRevision(fixture.ctx, domain.SliceRevisionRecord{
ID: "draft-progress-slices-v1", OrganizationID: domain.DefaultOrganizationID, TaskID: fixture.task.ID, Revision: 1,
Segments: []domain.SliceSegment{{ID: "stale", Start: 0, End: 10, AnnotationStatus: domain.SliceAnnotationLabeled}},
CreatedAt: time.Now().UTC().Add(-2 * time.Hour).Format(time.RFC3339),
}); err != nil {
t.Fatal(err)
}
// Draft with two slices, one labeled — the live workbench state.
item := fixture.item
item.UpdatedAt = time.Now().UTC().Format(time.RFC3339)
payload := map[string]any{}
for key, value := range fixture.item.Subject.Payload {
payload[key] = value
}
payload["annotationDraft"] = map[string]any{
"sliceSegments": []any{
map[string]any{"id": "draft-a", "start": 0.0, "end": 5.0, "annotationStatus": "labeled"},
map[string]any{"id": "draft-b", "start": 5.0, "end": 10.0},
},
"segments": []any{map[string]any{"id": "draft-event", "labelId": "01", "start": 1.0, "end": 2.0}},
}
item.Subject.Payload = payload
if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil {
t.Fatal(err)
}
recorder := fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks?projectId="+fixture.task.ProjectID, fixture.quality, nil)
if recorder.Code != http.StatusOK {
t.Fatalf("task list status=%d body=%s", recorder.Code, recorder.Body.String())
}
var response struct {
Items []struct {
SliceCount int `json:"sliceCount"`
CompletedSliceCount int `json:"completedSliceCount"`
PendingSliceCount int `json:"pendingSliceCount"`
AnnotationEventCount int `json:"annotationEventCount"`
} `json:"items"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Items) != 1 {
t.Fatalf("expected 1 task, got %#v", response.Items)
}
got := response.Items[0]
if got.SliceCount != 2 || got.CompletedSliceCount != 1 || got.PendingSliceCount != 1 || got.AnnotationEventCount != 1 {
t.Fatalf("draft-derived progress mismatch: slices=%d completed=%d pending=%d events=%d", got.SliceCount, got.CompletedSliceCount, got.PendingSliceCount, got.AnnotationEventCount)
}
// An older draft (human item untouched since the revision) must defer to
// the revision: progress stays revision-derived, and no draft read happens.
stale := fixture.item
stale.UpdatedAt = time.Now().UTC().Add(-3 * time.Hour).Format(time.RFC3339)
stalePayload := map[string]any{}
for key, value := range fixture.item.Subject.Payload {
stalePayload[key] = value
}
stalePayload["annotationDraft"] = map[string]any{
"sliceSegments": []any{map[string]any{"id": "old-draft", "start": 0.0, "end": 10.0, "annotationStatus": "labeled"}},
}
stale.Subject.Payload = stalePayload
if err := fixture.store.UpsertHumanItem(fixture.ctx, stale); err != nil {
t.Fatal(err)
}
recorder = fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks?projectId="+fixture.task.ProjectID, fixture.quality, nil)
if recorder.Code != http.StatusOK {
t.Fatalf("stale task list status=%d body=%s", recorder.Code, recorder.Body.String())
}
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Items) != 1 || response.Items[0].SliceCount != 1 || response.Items[0].CompletedSliceCount != 1 {
t.Fatalf("revision-derived progress mismatch: %#v", response.Items)
}
}
// TestTaskListManagerSkipsMembershipScan pins the manager fast path: the
// admin view performs no batch snapshot, membership or supplier-pool reads,
// because the manager branch never consults them.
func TestTaskListManagerSkipsMembershipScan(t *testing.T) {
fixture := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticReview, domain.HumanItemReady, "", 1)
admin := domain.UserRecord{ID: "admin-task-list", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
if err := fixture.store.UpsertUser(fixture.ctx, admin); err != nil {
t.Fatal(err)
}
counter := &taskListReadCounter{Store: fixture.store}
fixture.handler = NewHandler(counter, nil)
recorder := fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks?projectId="+fixture.task.ProjectID, admin, nil)
if recorder.Code != http.StatusOK {
t.Fatalf("manager task list status=%d body=%s", recorder.Code, recorder.Body.String())
}
if counter.listedAllHumanItems || counter.memberships != 0 || counter.batches != 0 {
t.Fatalf("manager scan leak: orgWideHumanItems=%v memberships=%d batches=%d", counter.listedAllHumanItems, counter.memberships, counter.batches)
}
}
// TestTaskListStoreFilterPushedIntoTaskQuery asserts the handler pushes the
// status filter to the store query: tasks filtered out by the database never
// reach the response, and no organization-wide human-item scan runs for any
// caller role.
func TestTaskListStoreFilterPushedIntoTaskQuery(t *testing.T) {
fixture := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticReview, domain.HumanItemReady, "", 1)
other := fixture.task
other.ID = "filtered-out-task"
other.Status = domain.AnnotationTaskStatusAccepted
if err := fixture.store.UpsertAnnotationTask(fixture.ctx, other); err != nil {
t.Fatal(err)
}
counter := &taskListReadCounter{Store: fixture.store}
fixture.handler = NewHandler(counter, nil)
recorder := fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks?projectId="+fixture.task.ProjectID+"&status="+string(fixture.task.Status), fixture.quality, nil)
if recorder.Code != http.StatusOK {
t.Fatalf("filtered task list status=%d body=%s", recorder.Code, recorder.Body.String())
}
var response struct {
Items []struct {
ID string `json:"id"`
} `json:"items"`
}
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
t.Fatal(err)
}
if len(response.Items) != 1 || response.Items[0].ID != fixture.task.ID {
t.Fatalf("status filter must exclude the completed task: %#v", response.Items)
}
if counter.listedAllHumanItems {
t.Fatal("task list must not run an organization-wide human items scan")
}
}
// taskListReadCounter wraps the MemoryStore to detect scans the task list
// must never perform on the optimized paths.
type taskListReadCounter struct {
store.Store
listedAllHumanItems bool
memberships int
batches int
}
func (s *taskListReadCounter) ListHumanItems(ctx context.Context, organizationID string, query domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
// The by-task join is a different method; any ListHumanItems call here is
// a scan to flag.
s.listedAllHumanItems = true
return s.Store.ListHumanItems(ctx, organizationID, query)
}
func (s *taskListReadCounter) ListProjectMembers(ctx context.Context, organizationID string, projectID string) ([]domain.ProjectMemberRecord, error) {
s.memberships++
return s.Store.ListProjectMembers(ctx, organizationID, projectID)
}
func (s *taskListReadCounter) GetHumanBatch(ctx context.Context, id string) (*domain.HumanBatchRecord, error) {
s.batches++
return s.Store.GetHumanBatch(ctx, id)
}