diff --git a/docs/annotation-operations.md b/docs/annotation-operations.md index aa5f4176..b5cc4e0e 100644 --- a/docs/annotation-operations.md +++ b/docs/annotation-operations.md @@ -122,3 +122,9 @@ v5.8 打包以绝对 `timestamp_us` 为权威时钟,按同一个半开窗口 ### 质检领取与本人标注隔离 质检领取资格与正式审核保持职责隔离:任务的最近标注作者为当前用户时,后端返回不可领取,直接调用领取接口也被拒绝。账号从标注员调整为质检员不豁免此规则。连续审查与审核提交沿用现有本人隔离检查。历史已经领取的本人任务不在此次改动中自动释放,仍不能提交自审决定;不修改已有标注、历史审核或客户记录。 + +### 退回历史按需分页 + +工作台首屏不再请求退回历史;只有打开拒绝区才请求,每页读取最多 30 条候选记录,随后沿用服务端权限过滤。页面可能不足 30 条或为空但仍有下一页,此时继续翻页,不代表历史结束。本页待处置数量不是全部历史总量。筛选变化清空旧页,失败可重试。 + +`GET /api/human-rejects` 新增可选 `limit=1..100`、`cursor`,返回 `items` 和 `nextCursor`。按创建时间、ID 倒序游标分页,权限过滤不改变游标推进,不漏掉后续有权限的记录;本页关联任务批量查询,项目权限按请求复用。未传 limit 的旧调用及内部按任务读取仍保持全量行为,无数据迁移。此优化降低首屏和历史列表查询压力,不宣称已消除所有数据库超时。 diff --git a/internal/api/human_loop.go b/internal/api/human_loop.go index 12221d3b..c405735a 100644 --- a/internal/api/human_loop.go +++ b/internal/api/human_loop.go @@ -2037,16 +2037,73 @@ func (server *server) routeV58AcceptanceRejection(ctx context.Context, source *d } func (server *server) handleHumanRejectsList(writer http.ResponseWriter, request *http.Request) { - items, err := server.store.ListHumanRejects(request.Context(), server.requestOrganizationID(request), parseHumanRejectQuery(request)) + query := parseHumanRejectQuery(request) + paged := request.URL.Query().Get("limit") != "" + limit := 0 + if paged { + var err error + limit, err = strconv.Atoi(request.URL.Query().Get("limit")) + if err != nil || limit < 1 || limit > 100 { + renderJSON(writer, 400, map[string]string{"error": "limit must be between 1 and 100"}) + return + } + query.Limit = limit + 1 + } + if cursor := request.URL.Query().Get("cursor"); cursor != "" { + var key []string + raw, err := base64.RawURLEncoding.DecodeString(cursor) + if !paged || err != nil || json.Unmarshal(raw, &key) != nil || len(key) != 2 || key[0] == "" || key[1] == "" { + renderJSON(writer, 400, map[string]string{"error": "invalid cursor"}) + return + } + query.BeforeTime, query.BeforeID = key[0], key[1] + } + items, err := server.store.ListHumanRejects(request.Context(), server.requestOrganizationID(request), query) if err != nil { renderError(writer, http.StatusInternalServerError, err) return } + nextCursor := "" + if paged && len(items) > limit { + items = items[:limit] + last := items[len(items)-1] + raw, _ := json.Marshal([]string{last.CreatedAt, last.ID}) + nextCursor = base64.RawURLEncoding.EncodeToString(raw) + } + // Resolve the page's tasks and memberships once; retain legacy package fallback. + ids := []string{} + for _, reject := range items { + ids = append(ids, reject.ItemID) + } + tasks := map[string]domain.HumanItemRecord{} + if len(ids) > 0 { + rows, e := server.store.ListHumanItems(request.Context(), server.requestOrganizationID(request), domain.HumanItemQuery{IDs: ids, ListProjection: true}) + if e != nil { + renderError(writer, 500, e) + return + } + ctx, e := server.withHumanListReads(request.Context(), server.requestOrganizationID(request), query.ProjectID, rows) + if e != nil { + renderError(writer, 500, e) + return + } + request = request.WithContext(ctx) + for _, row := range rows { + tasks[row.ID] = row + } + } principal, _ := currentPrincipal(request) if !annotationManager(principal) { filtered := make([]domain.HumanRejectRecord, 0, len(items)) for _, reject := range items { - if server.canAccessHumanReject(request, reject) { + task, found := tasks[reject.ItemID] + allowed := false + if found { + allowed = server.canAccessHumanItem(request.Context(), request, task, "") + } else { + allowed = server.canAccessHumanReject(request, reject) + } + if allowed { filtered = append(filtered, reject) } } @@ -2054,12 +2111,19 @@ func (server *server) handleHumanRejectsList(writer http.ResponseWriter, request } resolver := newHumanDisplayResolver(server) for index := range items { - if err := resolver.enrichReject(request.Context(), &items[index]); err != nil { + var err error + if task, found := tasks[items[index].ItemID]; found { + err = resolver.enrichItem(request.Context(), &task) + items[index].Display = task.Display + } else { + err = resolver.enrichReject(request.Context(), &items[index]) + } + if err != nil { renderError(writer, http.StatusInternalServerError, err) return } } - renderJSON(writer, http.StatusOK, map[string]any{"items": items}) + renderJSON(writer, http.StatusOK, map[string]any{"items": items, "nextCursor": nextCursor}) } func (server *server) canAccessHumanReject(request *http.Request, reject domain.HumanRejectRecord) bool { diff --git a/internal/api/human_reject_paging_test.go b/internal/api/human_reject_paging_test.go new file mode 100644 index 00000000..ff319382 --- /dev/null +++ b/internal/api/human_reject_paging_test.go @@ -0,0 +1,94 @@ +package api + +import ( + "droboticflow/internal/domain" + "encoding/json" + "fmt" + "net/http" + "net/url" + "testing" +) + +func TestHumanRejectCursorPage(t *testing.T) { + f := newCombinedDraftFixture(t) + for i := 0; i < 5; i++ { + if err := f.store.UpsertHumanReject(f.ctx, domain.HumanRejectRecord{ID: fmt.Sprintf("reject-%d", i), OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID, ItemID: f.item.ID, CreatedAt: "2026-10-28T00:00:00Z"}); err != nil { + t.Fatal(err) + } + } + cursor := "" + seen := map[string]bool{} + for page := 0; page < 4; page++ { + r := f.request(http.MethodGet, "/api/human-rejects?limit=2&cursor="+url.QueryEscape(cursor), f.reviewer, nil) + if r.Code != 200 { + t.Fatalf("%d %s", r.Code, r.Body.String()) + } + var out struct { + Items []domain.HumanRejectRecord `json:"items"` + Next string `json:"nextCursor"` + } + if err := json.Unmarshal(r.Body.Bytes(), &out); err != nil { + t.Fatal(err) + } + if len(out.Items) > 2 { + t.Fatal("unbounded page") + } + for _, v := range out.Items { + if seen[v.ID] { + t.Fatal("duplicate", v.ID) + } + seen[v.ID] = true + } + cursor = out.Next + if cursor == "" { + break + } + } + if len(seen) != 5 { + t.Fatalf("lost records: %v", seen) + } + for _, q := range []string{"limit=0", "limit=101", "limit=2&cursor=invalid"} { + r := f.request("GET", "/api/human-rejects?"+q, f.reviewer, nil) + if r.Code != 400 { + t.Fatalf("accepted %s: %d", q, r.Code) + } + } + r := f.request("GET", "/api/human-rejects", f.reviewer, nil) + var legacy struct { + Items []domain.HumanRejectRecord `json:"items"` + } + json.Unmarshal(r.Body.Bytes(), &legacy) + if r.Code != 200 || len(legacy.Items) != 5 { + t.Fatalf("legacy compatibility: %d %s", r.Code, r.Body.String()) + } +} + +func TestHumanRejectFilteredPageStillAdvances(t *testing.T) { + f := newCombinedDraftFixture(t) + for _, r := range []domain.HumanRejectRecord{ + {ID: "z-hidden", ItemID: "missing-item", OrganizationID: f.item.OrganizationID, CreatedAt: "2026-10-28T00:00:00Z"}, + {ID: "a-visible", ItemID: f.item.ID, OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID, CreatedAt: "2026-10-28T00:00:00Z"}, + } { + if e := f.store.UpsertHumanReject(f.ctx, r); e != nil { + t.Fatal(e) + } + } + first := f.request("GET", "/api/human-rejects?limit=1", f.reviewer, nil) + var page struct { + Items []domain.HumanRejectRecord `json:"items"` + Next string `json:"nextCursor"` + } + if e := json.Unmarshal(first.Body.Bytes(), &page); e != nil { + t.Fatal(e) + } + if first.Code != 200 || len(page.Items) != 0 || page.Next == "" { + t.Fatalf("filtered first page %d %s", first.Code, first.Body.String()) + } + next := f.request("GET", "/api/human-rejects?limit=1&cursor="+url.QueryEscape(page.Next), f.reviewer, nil) + if e := json.Unmarshal(next.Body.Bytes(), &page); e != nil { + t.Fatal(e) + } + if next.Code != 200 || len(page.Items) != 1 || page.Items[0].ID != "a-visible" || page.Next != "" { + t.Fatalf("next page %d %s", next.Code, next.Body.String()) + } +} diff --git a/internal/domain/human_loop.go b/internal/domain/human_loop.go index f47cdd1f..b71d7e7c 100644 --- a/internal/domain/human_loop.go +++ b/internal/domain/human_loop.go @@ -363,6 +363,9 @@ type HumanItemQuery struct { } type HumanRejectQuery struct { + Limit int + BeforeTime string + BeforeID string BatchID string ItemID string ProjectID string diff --git a/internal/store/human_loop.go b/internal/store/human_loop.go index e17bcd4e..507d13f1 100644 --- a/internal/store/human_loop.go +++ b/internal/store/human_loop.go @@ -454,11 +454,19 @@ func (store *MemoryStore) ListHumanRejects(_ context.Context, organizationID str defer store.mu.Unlock() items := make([]domain.HumanRejectRecord, 0) for _, item := range store.humanRejects { - if humanOrganizationMatches(item.OrganizationID, organizationID) && humanRejectMatches(item, query) { + 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 { return items[i].CreatedAt > items[j].CreatedAt }) + 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 } @@ -861,7 +869,19 @@ func humanRejectMongoFilter(organizationID string, query domain.HumanRejectQuery } func (store *MongoStore) ListHumanRejects(ctx context.Context, organizationID string, query domain.HumanRejectQuery) ([]domain.HumanRejectRecord, error) { - return mongoList[domain.HumanRejectRecord](ctx, store.humanRejects, humanRejectMongoFilter(organizationID, query), bson.D{{Key: "createdAt", Value: -1}}) + 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) { diff --git a/internal/store/human_reject_paging_test.go b/internal/store/human_reject_paging_test.go new file mode 100644 index 00000000..dd25d682 --- /dev/null +++ b/internal/store/human_reject_paging_test.go @@ -0,0 +1,63 @@ +package store + +import ( + "context" + "droboticflow/internal/domain" + "fmt" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" + "os" + "testing" + "time" +) + +func TestHumanRejectPagingMongoReplica(t *testing.T) { + if os.Getenv("TEST_REJECT_MONGO") != "1" { + t.Skip("local synthetic replica test") + } + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) + defer cancel() + client, e := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018")) + if e != nil { + t.Fatal(e) + } + defer client.Disconnect(context.Background()) + db := client.Database(fmt.Sprintf("reject_paging_test_%d", time.Now().UnixNano())) + defer db.Drop(context.Background()) + s := NewMongoStore(db) + for i := 0; i < 7; i++ { + e = s.UpsertHumanReject(ctx, domain.HumanRejectRecord{ID: fmt.Sprintf("r%d", i), OrganizationID: "test-org", CreatedAt: "2026-10-28T00:00:00Z"}) + if e != nil { + t.Fatal(e) + } + } + q := domain.HumanRejectQuery{Limit: 2} + seen := map[string]bool{} + for n := 0; n < 5; n++ { + rows, e := s.ListHumanRejects(ctx, "test-org", q) + if e != nil { + t.Fatal(e) + } + if len(rows) > 2 { + t.Fatal("unbounded") + } + if len(rows) == 0 { + break + } + for _, r := range rows { + if seen[r.ID] { + t.Fatal("duplicate") + } + seen[r.ID] = true + } + last := rows[len(rows)-1] + q.BeforeTime, q.BeforeID = last.CreatedAt, last.ID + } + if len(seen) != 7 { + t.Fatalf("lost records %v", seen) + } + rows, e := s.ListHumanRejects(ctx, "other-org", domain.HumanRejectQuery{Limit: 2}) + if e != nil || len(rows) != 0 { + t.Fatal("organization scope", e) + } +} diff --git a/src/pages/AnnotationOperations.tsx b/src/pages/AnnotationOperations.tsx index 5aa81f43..fa64881a 100644 --- a/src/pages/AnnotationOperations.tsx +++ b/src/pages/AnnotationOperations.tsx @@ -738,6 +738,10 @@ export default function AnnotationOperations({ workPage = false }: { workPage?: }) as TaskPoolView; const [taskPoolView, setTaskPoolView] = useState(defaultTaskPoolView); const [workload, setWorkload] = useState({ items: [], eventCount: 0 }); + const [rejectCursor, setRejectCursor] = useState(""); + const [rejectNextCursor, setRejectNextCursor] = useState(""); + const [rejectError, setRejectError] = useState(""); + const [rejectLoading, setRejectLoading] = useState(false); const [rejects, setRejects] = useState([]); const [deliveryPackages, setDeliveryPackages] = useState([]); const [expandedDeliveryPackageId, setExpandedDeliveryPackageId] = useState(""); @@ -1280,50 +1284,35 @@ export default function AnnotationOperations({ workPage = false }: { workPage?: }, signal: adminPoolAbortRef.current?.signal, }); - const [ - nextProjectMembers, - nextWorkflows, - nextTemplates, - poolPage, - nextWorkload, - nextRejects, - nextPackages, - nextScopedRun, - ] = await Promise.all([ - projectId - ? projectsApi.listMembers(projectId) - : fanOutProjectScope - ? Promise.allSettled(knownProjects.map((p) => projectsApi.listMembers(p.id))).then((results) => - results.flatMap((r) => (r.status === "fulfilled" ? r.value : [])), - ) - : Promise.resolve([] as ProjectMember[]), - projectId - ? projectsApi.listWorkflows(projectId) - : fanOutProjectScope - ? Promise.allSettled(knownProjects.map((p) => projectsApi.listWorkflows(p.id))).then((results) => - results.flatMap((r) => (r.status === "fulfilled" ? r.value : [])), - ) - : Promise.resolve([] as Workflow[]), - annotationOperationsApi.listTemplates(projectId || undefined), - poolsRequest, - deferReviewReports ? Promise.resolve(null) : annotationOperationsApi.workloadSummary(projectId || undefined), - deferReviewReports - ? Promise.resolve(null) - : humanLoopApi.listRejects({ - projectId: projectId || undefined, - workflowId: requestedWorkflowId || undefined, - runId: requestedRunId || undefined, - }), - reviewOnly && workspaceMode !== "acceptance" - ? Promise.resolve([] as DeliveryPackage[]) - : deliveryPackagesApi.list({ - projectId: projectId || undefined, - workflowId: requestedWorkflowId || undefined, - runId: requestedRunId || undefined, - status: projectPoolView === "all" ? undefined : "pending_acceptance", - }), - isManager && requestedRunId ? runsApi.get(requestedRunId) : Promise.resolve(null), - ]); + const [nextProjectMembers, nextWorkflows, nextTemplates, poolPage, nextWorkload, nextPackages, nextScopedRun] = + await Promise.all([ + projectId + ? projectsApi.listMembers(projectId) + : fanOutProjectScope + ? Promise.allSettled(knownProjects.map((p) => projectsApi.listMembers(p.id))).then((results) => + results.flatMap((r) => (r.status === "fulfilled" ? r.value : [])), + ) + : Promise.resolve([] as ProjectMember[]), + projectId + ? projectsApi.listWorkflows(projectId) + : fanOutProjectScope + ? Promise.allSettled(knownProjects.map((p) => projectsApi.listWorkflows(p.id))).then((results) => + results.flatMap((r) => (r.status === "fulfilled" ? r.value : [])), + ) + : Promise.resolve([] as Workflow[]), + annotationOperationsApi.listTemplates(projectId || undefined), + poolsRequest, + deferReviewReports ? Promise.resolve(null) : annotationOperationsApi.workloadSummary(projectId || undefined), + reviewOnly && workspaceMode !== "acceptance" + ? Promise.resolve([] as DeliveryPackage[]) + : deliveryPackagesApi.list({ + projectId: projectId || undefined, + workflowId: requestedWorkflowId || undefined, + runId: requestedRunId || undefined, + status: projectPoolView === "all" ? undefined : "pending_acceptance", + }), + isManager && requestedRunId ? runsApi.get(requestedRunId) : Promise.resolve(null), + ]); if (!current()) return; const { tasks: nextTasks, pools: nextHumanPools } = poolPage; taskPoolCursorRef.current = poolPage.taskCursor; @@ -1348,7 +1337,6 @@ export default function AnnotationOperations({ workPage = false }: { workPage?: reviewed: nextHumanPools.reviewed, }); if (nextWorkload) setWorkload(nextWorkload); - if (nextRejects) setRejects(nextRejects); setDeliveryPackages(nextPackages); setScopedRun(nextScopedRun); loadedProjectScope.current = projectDataScope; @@ -1416,19 +1404,7 @@ export default function AnnotationOperations({ workPage = false }: { workPage?: let disposed = false; setReviewAuxiliaryError(""); setReviewAuxiliaryLoading(true); - const filters = { - projectId: projectId || undefined, - workflowId: requestedWorkflowId || undefined, - runId: requestedRunId || undefined, - }; const reports = []; - // Acceptance does not use rejection history to render a current task. - if (!acceptanceWorkspace || tab === "rejects") - reports.push( - humanLoopApi.listRejects(filters).then((value) => { - if (!disposed) setRejects(value); - }), - ); if (tab === "workload") reports.push( annotationOperationsApi.workloadSummary(projectId || undefined).then((value) => { @@ -1439,13 +1415,49 @@ export default function AnnotationOperations({ workPage = false }: { workPage?: if (disposed) return; setReviewAuxiliaryLoading(false); if (results.some((r) => r.status === "rejected")) - setReviewAuxiliaryError("退回记录或工作记录暂未加载完成,请刷新重试;当前任务可继续查看。"); + setReviewAuxiliaryError("工作记录暂未加载完成,请刷新重试;当前任务可继续查看。"); }); return () => { disposed = true; }; }, [deferReviewReports, acceptanceWorkspace, projectDataScope, tab, projectLoadRetryVersion]); + useEffect(() => { + setRejectCursor(""); + setRejectNextCursor(""); + setRejects([]); + }, [projectDataScope]); + useEffect(() => { + if (tab !== "rejects") return; + let active = true; + setRejectLoading(true); + setRejectError(""); + setRejects([]); + setRejectNextCursor(""); + humanLoopApi + .listRejectsPage({ + projectId: projectId || undefined, + workflowId: requestedWorkflowId || undefined, + runId: requestedRunId || undefined, + cursor: rejectCursor || undefined, + }) + .then((result) => { + if (active) { + setRejects(result.items); + setRejectNextCursor(result.nextCursor); + } + }) + .catch(() => { + if (active) setRejectError("退回记录加载失败,请重试。"); + }) + .finally(() => { + if (active) setRejectLoading(false); + }); + return () => { + active = false; + }; + }, [tab, projectDataScope, projectId, requestedWorkflowId, requestedRunId, rejectCursor, projectLoadRetryVersion]); + const applyReviewClaim = (item: HumanItem) => { // The claim response is authoritative. Do not start a queue reload that // would compete with detail/evidence requests for browser connections. @@ -6705,6 +6717,27 @@ export default function AnnotationOperations({ workPage = false }: { workPage?: {tab === "rejects" ? (
+ {rejectLoading &&

正在加载退回记录…

} + {rejectError && ( +

+ {rejectError} + +

+ )} + +
拒绝区
@@ -6713,7 +6746,7 @@ export default function AnnotationOperations({ workPage = false }: { workPage?:
- {rejects.filter((item) => item.status === "open").length} 待处置 + 本页 {rejects.filter((item) => item.status === "open").length} 待处置
diff --git a/src/services/api/human-loop.ts b/src/services/api/human-loop.ts index ee5f63db..fd0a4af1 100644 --- a/src/services/api/human-loop.ts +++ b/src/services/api/human-loop.ts @@ -203,6 +203,10 @@ export const humanLoopApi = { id: string, payload: { version: number; action: "reopen" | "close"; targetStageId?: string; comment: string }, ) => apiRequest(`/api/human-items/${encodeURIComponent(id)}/escalation`, { method: "POST" }, payload), + listRejectsPage: (filters: { projectId?: string; workflowId?: string; runId?: string; cursor?: string }) => + apiRequest<{ items: HumanReject[]; nextCursor: string }>( + `/api/human-rejects${queryString({ ...filters, limit: 30 })}`, + ), listRejects: async ( filters: { projectId?: string; workflowId?: string; runId?: string; status?: HumanReject["status"] } = {}, ) => (await apiRequest<{ items: HumanReject[] }>(`/api/human-rejects${queryString(filters)}`)).items,