🐛 Fix platform pool reads and recoverable rework handoff
This commit is contained in:
parent
6b42476962
commit
08bbd632e7
74
cmd/rework-handoff-plan/main.go
Normal file
74
cmd/rework-handoff-plan/main.go
Normal file
@ -0,0 +1,74 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/reworkrepair"
|
||||
"droboticflow/internal/store"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
database := flag.String("database", "", "exact Mongo database")
|
||||
org := flag.String("organization", "", "organization ID for a read-only plan")
|
||||
ids := flag.String("items", "", "comma-separated exact item IDs (at most 100)")
|
||||
planFile := flag.String("plan", "", "previously reviewed plan JSON")
|
||||
apply := flag.Bool("apply", false, "explicitly apply the confirmed metadata-only plan")
|
||||
confirm := flag.String("confirm-sha256", "", "exact reviewed plan digest; required for apply")
|
||||
actor := flag.String("actor", "", "active platform administrator ID; required for apply")
|
||||
flag.Parse()
|
||||
if *database == "" || os.Getenv("MONGO_URI") == "" {
|
||||
fmt.Fprintln(os.Stderr, "database and MONGO_URI are required")
|
||||
os.Exit(2)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI")))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Mongo connection unavailable")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer client.Disconnect(context.Background())
|
||||
s := store.NewMongoStore(client.Database(*database))
|
||||
encoder := json.NewEncoder(os.Stdout)
|
||||
encoder.SetIndent("", " ")
|
||||
if !*apply {
|
||||
if *org == "" || *ids == "" || *planFile != "" || *confirm != "" || *actor != "" {
|
||||
fmt.Fprintln(os.Stderr, "read-only preparation requires organization and exact items only")
|
||||
os.Exit(2)
|
||||
}
|
||||
p, err := reworkrepair.Prepare(ctx, s, *database, *org, strings.Split(*ids, ","))
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "plan unavailable: source state or formal evidence needs review")
|
||||
os.Exit(1)
|
||||
}
|
||||
_ = encoder.Encode(p)
|
||||
return
|
||||
}
|
||||
if *planFile == "" || *confirm == "" || *actor == "" || *ids != "" || *org != "" {
|
||||
fmt.Fprintln(os.Stderr, "apply requires reviewed plan, confirmation digest and actor only")
|
||||
os.Exit(2)
|
||||
}
|
||||
bytes, err := os.ReadFile(*planFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "plan file unavailable")
|
||||
os.Exit(2)
|
||||
}
|
||||
var p reworkrepair.Plan
|
||||
if json.Unmarshal(bytes, &p) != nil {
|
||||
fmt.Fprintln(os.Stderr, "invalid plan")
|
||||
os.Exit(2)
|
||||
}
|
||||
result, err := reworkrepair.Apply(ctx, s, *database, p, *confirm, *actor)
|
||||
_ = encoder.Encode(result)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "handoff incomplete; inspect partial result and re-read task state before retry; annotations were not overwritten")
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
82
docs/operations/platform-query-closure.md
Normal file
82
docs/operations/platform-query-closure.md
Normal file
@ -0,0 +1,82 @@
|
||||
# 2026-11-11 平台问题收口
|
||||
|
||||
状态:本地修复与相关隔离专项验证已完成,生产待实施、验收。未执行部署、索引写入、历史任务补处理或开启删除交接开关。
|
||||
|
||||
## 根因与处理
|
||||
|
||||
| 问题 | 根因与本批处理 | 生产完成条件 |
|
||||
| --- | --- | --- |
|
||||
| 领取、统计和搜索超时 | 候选未提前限定标注阶段;过滤统计每页重复发现和关联;广范围列表先关联全部任务;无过滤统计仍读取大文档。本批下推必要阶段范围、单次穷尽统计、每批 200 条关联、覆盖索引扫描统计。权限仍由 API 实时校验,旧数据仍按原匹配和恢复路径处理。 | 部署兼容代码,批准索引后真实请求只读复测。 |
|
||||
| 管理员生产总览超时、容量预警 | 已有本地 `6bf0b766` 的有界查询和分片统计快照修复;生产仍为旧实现。之前批准的两个统计索引不能替代应用升级。 | 新代码上线后对冷/热查询及容量重新验收,不能以建好索引宣称解决。 |
|
||||
| Worker 轮询及审查恢复读取重 | 缺少状态优先的统计覆盖索引和恢复定位索引。维持原聚合/恢复语义,仅显式选择新索引;索引缺失回到原查询。 | 批准建设索引,所有组件按兼容要求升级,再做资源对照。 |
|
||||
| 切片生成/保存失败被持续阻止 | 已有本地 `48106027` 将暂时依赖读取失败从不可重试校验错误改为 503/504;已有标注保留。 | 部署后验收真实故障恢复,不能通过清空草稿绕过。 |
|
||||
| 删除标注员后的返工只堆给新人 | 已有本地 `6b424769` 实现供应商内负载优先、同负载随机、跨项目每人最多接手一条;本批修复正式质检决定的窄字段更新遗漏路由标记,避免重启后丢失待分配意图。 | 全部写入者兼容、两个路由索引获批建设后,单独批准开关。 |
|
||||
| 已删除作者的历史未分配返工 | 新规则不会擅自重写历史任务。新增只读计划与显式确认工具,核对正式冻结基线、版本、供应商和删除状态后,仅 CAS 标记待交接并审计;不改正文、不直接指定接手人。 | 精确任务清单另行授权;版本变化必须重读,不以旧快照覆盖。 |
|
||||
|
||||
## 兼容与失败恢复
|
||||
|
||||
- 搜索仍使用原有完整匹配器、租户范围、旧数据回退和稳定分页规则;无命中先做一个有界探测,再由服务端穷尽剩余范围,避免把全部身份传回 API。发现的小 ID 集直接关联,避免重复引用读取。
|
||||
- 统计覆盖路径不用于额外字段不在索引内的交接扫描和盲标可见性查询;缺少新索引时保留原投影读取。每页最多 1000 条,取消/超时返回错误,不发布部分总数。
|
||||
- 所有新索引由运维显式建设;启动过程不自动创建。索引缺失保持结果兼容,但不保证原路径性能。新索引损坏或键不符不能静默当成成功。
|
||||
- 正式质检记录、送检修订、切片及草稿不因本次查询优化删除。历史工具只标记未分配、已退回且原作者已删除的任务;已被领取、版本/基线改变或缺少证据时拒绝执行。
|
||||
|
||||
## 索引实施方案(待授权)
|
||||
|
||||
[查询索引清单](platform-query-indexes.json) 与 `store.PlatformReadIndexes()` 由测试比对,共六个非唯一索引:
|
||||
|
||||
1. `humanItems.pool_count_candidates_cover_v1`:权限判断所需的小字段覆盖统计;不含正文、草稿和批注。按组织和 ID 有界翻页,匹配条件在覆盖投影之后执行,保留 null/缺失字段语义。
|
||||
2. `runs.worker_run_counts_cover_v1`:状态在前,保留原分组统计,不读取运行日志大载荷。
|
||||
3. `runs.human_release_parent_v1`:按父运行定位旧交付来源,保留原恢复路径。
|
||||
4. `reviewOperations.review_operation_lookup_v1`:组织、操作者、操作身份精确定位,保留历史回执 ID 及任务/条目核对。
|
||||
5. `reviewSessions.review_continuation_admission_v1`:只索引有待恢复准入记录的会话,不更改恢复状态。
|
||||
6. `captureRecords.capture_run_lookup_v1`:按运行定位采集记录,历史父运行回退保留。
|
||||
|
||||
另有 [两个删除交接索引](deleted-annotator-indexes.json),与本次六个索引分别列明审批。既有搜索覆盖索引方案不在本次自动建设范围内。
|
||||
|
||||
建设前:确认泛宇数据库、现有索引名称/键/部分条件、磁盘余量、CPU/IO 和复制延迟;若同名不同键则停止,不自动删除替换。每次仅建一个,观察完成及写延迟后再继续。建设会读取集合、消耗 IO/CPU/磁盘,并增加未来写入维护成本;统计覆盖索引字段多,必须记录实测尺寸与收益。
|
||||
|
||||
建设后:只读解释候选查询,记录文档/键扫描、冷/热耗时及资源;缺少收益或持续增加写延迟时,仅删除本批新建的相应索引回退。不得删除旧索引、修改客户记录或用更长超时替代验收。回退索引后应用仍兼容原查询,性能收益随之失效。
|
||||
|
||||
## 历史返工工具(待授权)
|
||||
|
||||
`cmd/rework-handoff-plan` 默认只读。连接信息仅从 `MONGO_URI` 读取,不写入计划或日志。
|
||||
|
||||
```sh
|
||||
go run ./cmd/rework-handoff-plan --database <exact-database> \
|
||||
--organization <organization-id> --items <exact-id-1>,<exact-id-2> > /private/tmp/rework-plan.json
|
||||
```
|
||||
|
||||
审核计划后,写入操作必须再取得明确授权,并提供计划摘要及有效平台管理员:
|
||||
|
||||
```sh
|
||||
go run ./cmd/rework-handoff-plan --database <exact-database> \
|
||||
--apply --plan /private/tmp/rework-plan.json \
|
||||
--confirm-sha256 <reviewed-digest> --actor <admin-id>
|
||||
```
|
||||
|
||||
最多 100 条显式 ID,执行前整体预检,每条仍做版本 CAS 窄更新,不替换任务文档,保留同时发生的不提升版本的草稿及后台进度更新。先幂等写入“请求交接”审计,再公开路由标记,避免进程中断后任务立即被接手而缺少处置依据;审计不冒充交接已完成。失败返回已处理数量;重试只接受原计划精确状态或本计划刚标记的状态,审计使用固定 ID。状态已推进则重新盘点,不盲目重试。工具不负责开启自动分配;交接开关关闭时任务保持待分配。
|
||||
|
||||
## 已完成的生产只读核对
|
||||
|
||||
- `FYLX_B17_AZEC864012K_20260925_102740/part0003` 已到质检 `ready`、正式修订 3,提交后台作业及切片均完成。不得将已提交任务没有个人草稿误判为内容丢失,更不能回填旧草稿。
|
||||
- 盘点发现两个原作者已删除且未分配的返工任务;已核对正式基线并生成只读计划,没有执行生产标记或分配。计划与原始诊断保存在忽略目录,执行前须刷新版本。
|
||||
- `FYLX_B15_AZEC86400WS_20260924_141701/part0001` 已按采集记录和运行关联定位:质检 `ready`、正式修订 3、提交和切片均完成,任务未丢失。受影响账号的线上搜索性能/可见性仍需发布后按其权限验收,不能以管理员可见替代。
|
||||
- 生产 API/处理器为 `e99febda`,Worker 为 `0fd7e367`(本次盘点时);本地修改不会自动生效。
|
||||
|
||||
## 验证证据与边界
|
||||
|
||||
原始证据在 `local-data/diagnostics/platform-closure-20261111/`,仅限本地,不含连接凭证和客户正文:
|
||||
|
||||
- 真实质检回执路径先复现路由标记丢失,再验证修复与既有返工回归。
|
||||
- 标注候选同数据集:42 条结果不变,读取文档从 1042 降至 42;广范围首屏关联从 5000 降至 200,首屏 25 条与排序保持一致。
|
||||
- 统计覆盖扫描:2108 条、空/缺失归属、冻结验收归属及多种筛选对照一致;解释计划读取任务文档为 0。
|
||||
- 1.2 万条合成记录真实 HTTP 统计初测:管理员 1.75 秒到 0.33 秒,Mongo 回包约 112.8 MB 到 12.4 MB;验收员 0.86 秒到 0.17 秒,56.4 MB 到 6.2 MB。此为数据库回包,不是公网 EIP 流量。
|
||||
- 同规模带关键词统计先测得管理员约 2.3 秒未达标;消除列表分块重复准备后,三次最慢广匹配 1.59 秒、无命中 1.59 秒;验收员相应约 0.80 秒。数量仍为 12000/0,无截断或部分总数。
|
||||
- Worker 保留原分组结果,覆盖索引下 300 个排队运行统计读取文档 0;审查回执、准入恢复、采集定位在 3000 条样本中从全扫降为索引定位。
|
||||
- 管理员任务池、质检搜索、保存/切片失败恢复的浏览器专项通过;历史处置工具的真实 Mongo、权限拒绝、并发领取、独立后台更新、审计失败和标记响应丢失恢复通过竞态检测。
|
||||
- 81 人隔离读取冒烟为 60 标注、12 质检、8 验收、1 管理员,975 个请求、0 错误、0 Mongo 写命令;只读短测,不代替持续容量验收。
|
||||
- 官方基线门禁:Go 68 项、前端 13 项失败均为既有记录,无新增失败,未扩大基线。
|
||||
|
||||
统一任务报告为 `local-data/test-results/verify-1794402790672492000/report.json`。报告保留未完成状态:混合工作树中已有的生产专用只读诊断和元数据计时 opt-in 等检查被跳过,执行期间又补强了历史修复工具,不能将整份报告标为全绿。相应最新工具专项证据为 `historical-repair-durable-intent.log`;未改动或提交另一项管理员诊断任务的两个文件。生产业务验收及正式发布门禁仍待执行。
|
||||
|
||||
以上是隔离环境专项证据,不构成生产容量、81 人稳态并发或长时间资源增长验收。发布必须使用同步后的 dev,通过发布门禁、单独确认最终计划,再执行线上只读业务验收。
|
||||
75
docs/operations/platform-query-indexes.json
Normal file
75
docs/operations/platform-query-indexes.json
Normal file
@ -0,0 +1,75 @@
|
||||
[
|
||||
{
|
||||
"collection": "humanItems",
|
||||
"name": "pool_count_candidates_cover_v1",
|
||||
"keys": {
|
||||
"organizationId": 1,
|
||||
"id": 1,
|
||||
"pluginId": 1,
|
||||
"currentStageId": 1,
|
||||
"status": 1,
|
||||
"assignedUserId": 1,
|
||||
"projectId": 1,
|
||||
"batchId": 1,
|
||||
"legacyTaskId": 1,
|
||||
"supplierId": 1,
|
||||
"processingScope": 1,
|
||||
"processingProfile": 1,
|
||||
"workgroupId": 1,
|
||||
"vendorId": 1,
|
||||
"latestAuthorUserId": 1,
|
||||
"reviewRound": 1,
|
||||
"claimedAt": 1,
|
||||
"acceptanceClaim": 1,
|
||||
"inheritedRework": 1,
|
||||
"workflowId": 1,
|
||||
"runId": 1,
|
||||
"createdAt": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"collection": "runs",
|
||||
"name": "worker_run_counts_cover_v1",
|
||||
"keys": {
|
||||
"status": 1,
|
||||
"workflowId": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"collection": "runs",
|
||||
"name": "human_release_parent_v1",
|
||||
"keys": {
|
||||
"trigger": 1,
|
||||
"parentRunId": 1,
|
||||
"id": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"collection": "reviewOperations",
|
||||
"name": "review_operation_lookup_v1",
|
||||
"keys": {
|
||||
"organizationId": 1,
|
||||
"userId": 1,
|
||||
"operationId": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"collection": "reviewSessions",
|
||||
"name": "review_continuation_admission_v1",
|
||||
"keys": {
|
||||
"continuationAdmission.continuationId": 1
|
||||
},
|
||||
"partialFilterExpression": {
|
||||
"continuationAdmission.continuationId": {
|
||||
"$exists": true
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"collection": "captureRecords",
|
||||
"name": "capture_run_lookup_v1",
|
||||
"keys": {
|
||||
"runId": 1
|
||||
}
|
||||
}
|
||||
]
|
||||
@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@ -121,6 +122,10 @@ func TestHumanPoolCountMongoTransport(t *testing.T) {
|
||||
var baselineBody string
|
||||
var baselineFinds, baselineBytes int
|
||||
for index, handler := range handlers {
|
||||
if index == 1 {
|
||||
_, err := db.Collection("humanItems").Indexes().CreateMany(ctx, store.PlatformReadIndexes()["humanItems"])
|
||||
must(err)
|
||||
}
|
||||
mu.Lock()
|
||||
finds, bytes = 0, 0
|
||||
mu.Unlock()
|
||||
@ -144,6 +149,39 @@ func TestHumanPoolCountMongoTransport(t *testing.T) {
|
||||
if w.Body.String() != baselineBody || calls*2 >= baselineFinds || replyBytes*2 >= baselineBytes {
|
||||
t.Fatalf("count/transport regression: finds %d -> %d bytes %d -> %d", baselineFinds, calls, baselineBytes, replyBytes)
|
||||
}
|
||||
if elapsed > 2*time.Second {
|
||||
t.Fatalf("candidate count exceeded 2s: %s", elapsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Exercise the real HTTP filtered-count path at the same scale, including
|
||||
// broad matches and exhaustive no-hit queries; do not substitute a page size
|
||||
// for the authorized total. These are short local measurements, not a
|
||||
// production concurrency/capacity acceptance.
|
||||
for _, user := range []domain.UserRecord{f.admin, f.reviewer} {
|
||||
for _, query := range []string{"", "&search=count-transport-", "&search=no-such-capture"} {
|
||||
var samples []time.Duration
|
||||
for n := 0; n < 3; n++ {
|
||||
r := httptest.NewRequest("GET", "/api/human-item-counts?pluginId=annotation-qa"+query, nil)
|
||||
r = r.WithContext(context.WithValue(ctx, authContextKey{}, &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: user.OrganizationID, Status: domain.OrganizationStatusActive}}))
|
||||
w := httptest.NewRecorder()
|
||||
start := time.Now()
|
||||
handlers[1].ServeHTTP(w, r)
|
||||
samples = append(samples, time.Since(start))
|
||||
var body struct{ Counts map[string]int }
|
||||
want := count
|
||||
if strings.Contains(query, "no-such") {
|
||||
want = 0
|
||||
}
|
||||
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &body) != nil || body.Counts["available"] != want {
|
||||
t.Fatalf("filtered count role=%s query=%q code=%d body=%s", user.Role, query, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
sort.Slice(samples, func(i, j int) bool { return samples[i] < samples[j] })
|
||||
t.Logf("role=%s query=%q local worst-of-3=%s", user.Role, query, samples[2])
|
||||
if samples[2] > 2*time.Second {
|
||||
t.Fatalf("count query exceeded local 2s target: %s", samples[2])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ func TestHumanPoolCountScanMatchesAuthorizedPages(t *testing.T) {
|
||||
}
|
||||
}
|
||||
for _, actor := range []domain.UserRecord{f.reviewer, f.admin} {
|
||||
for _, scope := range []string{"", "&stageIds=acceptance,prepackage_acceptance&reviewedStageIds=acceptance,prepackage_acceptance", "&projectId=" + f.item.ProjectID} {
|
||||
for _, scope := range []string{"", "&stageIds=acceptance,prepackage_acceptance&reviewedStageIds=acceptance,prepackage_acceptance", "&projectId=" + f.item.ProjectID, "&search=scan-count-", "&search=no-such-item", "&category=available", "&category=pending_acceptance&reviewedStageIds=acceptance,prepackage_acceptance"} {
|
||||
counts := f.request("GET", "/api/human-item-counts?pluginId=annotation-qa"+scope, actor, nil)
|
||||
var body struct{ Counts map[string]int }
|
||||
if counts.Code != 200 || json.Unmarshal(counts.Body.Bytes(), &body) != nil {
|
||||
|
||||
@ -151,7 +151,7 @@ func (s *server) handleHumanPoolSummaries(w http.ResponseWriter, r *http.Request
|
||||
renderJSON(w, 200, map[string]any{"summaries": summaries, "filtersApplied": true, "hasMore": page.HasMore, "nextCursor": page.NextCursor})
|
||||
}
|
||||
|
||||
// Count every authorized page, without payload hydration or display joins.
|
||||
// Count every authorized candidate, without public-page hydration or display joins.
|
||||
// Errors/cancellation return an error, never a partial total. No cross-request
|
||||
// cache: revocation and self-review rules are checked by the same list filter.
|
||||
func (s *server) handleHumanPoolCounts(w http.ResponseWriter, r *http.Request) {
|
||||
@ -166,74 +166,30 @@ func (s *server) handleHumanPoolCounts(w http.ResponseWriter, r *http.Request) {
|
||||
pools = append(pools, "all")
|
||||
}
|
||||
for _, pool := range pools {
|
||||
// Unfiltered counts need every authorized row, not sorted public pages.
|
||||
// Keep search/category on the original exhaustive list path.
|
||||
v := r.URL.Query()
|
||||
if v.Get("search") == "" && v.Get("capture") == "" && (v.Get("category") == "" || v.Get("category") == "all") {
|
||||
next := r.Clone(context.WithValue(r.Context(), humanPoolReadModeKey{}, humanPoolReadCount))
|
||||
u := *r.URL
|
||||
next.URL = &u
|
||||
query := next.URL.Query()
|
||||
if pool == "reviewed" {
|
||||
query.Del("stageId")
|
||||
query.Del("stageIds")
|
||||
if stages := query.Get("reviewedStageIds"); stages != "" {
|
||||
query.Set("stageIds", stages)
|
||||
}
|
||||
next := r.Clone(context.WithValue(r.Context(), humanPoolReadModeKey{}, humanPoolReadCount))
|
||||
u := *r.URL
|
||||
next.URL = &u
|
||||
query := next.URL.Query()
|
||||
if pool == "reviewed" {
|
||||
query.Del("stageId")
|
||||
query.Del("stageIds")
|
||||
if stages := query.Get("reviewedStageIds"); stages != "" {
|
||||
query.Set("stageIds", stages)
|
||||
}
|
||||
query.Del("reviewedStageIds")
|
||||
query.Set("pool", pool)
|
||||
query.Set("cursor", "")
|
||||
query.Set("limit", "1000")
|
||||
next.URL.RawQuery = query.Encode()
|
||||
total, readRequest, ok := s.countHumanPoolCandidates(w, next)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
r = r.WithContext(readRequest.Context())
|
||||
counts[pool] = total
|
||||
continue
|
||||
}
|
||||
count := 0
|
||||
cursor := ""
|
||||
for {
|
||||
if r.Context().Err() != nil {
|
||||
renderError(w, 500, r.Context().Err())
|
||||
return
|
||||
}
|
||||
next := r.Clone(context.WithValue(r.Context(), humanPoolReadModeKey{}, humanPoolReadCount))
|
||||
u := *r.URL
|
||||
next.URL = &u
|
||||
query := next.URL.Query()
|
||||
if pool == "reviewed" {
|
||||
query.Del("stageId")
|
||||
query.Del("stageIds")
|
||||
if stages := query.Get("reviewedStageIds"); stages != "" {
|
||||
query.Set("stageIds", stages)
|
||||
}
|
||||
}
|
||||
query.Del("reviewedStageIds")
|
||||
query.Set("pool", pool)
|
||||
query.Set("limit", "1000")
|
||||
query.Set("cursor", cursor)
|
||||
next.URL.RawQuery = query.Encode()
|
||||
page, readRequest := s.readHumanItemsList(w, next)
|
||||
if page == nil {
|
||||
return
|
||||
}
|
||||
r = r.WithContext(readRequest.Context())
|
||||
count += len(page.Items)
|
||||
if !page.HasMore {
|
||||
break
|
||||
}
|
||||
if page.NextCursor == "" || page.NextCursor == cursor {
|
||||
renderJSON(w, 500, map[string]string{"error": "任务数量暂时无法核对,请重试。"})
|
||||
return
|
||||
}
|
||||
cursor = page.NextCursor
|
||||
query.Del("reviewedStageIds")
|
||||
query.Set("pool", pool)
|
||||
query.Set("cursor", "")
|
||||
query.Set("limit", "1000")
|
||||
next.URL.RawQuery = query.Encode()
|
||||
total, readRequest, ok := s.countHumanPoolCandidates(w, next)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
counts[pool] = count
|
||||
r = r.WithContext(readRequest.Context())
|
||||
counts[pool] = total
|
||||
}
|
||||
|
||||
renderJSON(w, 200, map[string]any{"counts": counts, "filtersApplied": true})
|
||||
}
|
||||
|
||||
@ -252,6 +208,11 @@ func (s *server) countHumanPoolCandidates(w http.ResponseWriter, r *http.Request
|
||||
return err
|
||||
}
|
||||
r = r.WithContext(ctx)
|
||||
if reviewedAcceptanceCategory(query) {
|
||||
if err := s.projectReviewedAcceptanceStatuses(r.Context(), s.requestOrganizationID(r), items, batches); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, item := range items {
|
||||
if err := r.Context().Err(); err != nil {
|
||||
return err
|
||||
|
||||
73
internal/api/platform_closure_test.go
Normal file
73
internal/api/platform_closure_test.go
Normal file
@ -0,0 +1,73 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Exercise the real receipt decision path: a recovery marker that exists only
|
||||
// on the in-memory plan disappears on restart and strands deleted-author work.
|
||||
func TestReviewRejectionPersistsDeletedAuthorRoutingIntent(t *testing.T) {
|
||||
t.Setenv("DELETED_ANNOTATOR_HANDOFF", "true")
|
||||
f := decisionFixture(t)
|
||||
f.item.LatestAuthorUserID = f.annotator.ID
|
||||
f.item.SupplierID = f.annotator.SupplierID
|
||||
f.item.MaxReworkRounds = 5
|
||||
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
version, headers := openReviewDraftSession(t, f)
|
||||
input := map[string]any{"stage": "quality", "decision": "rejected", "reasonCodes": []string{"manual_review_reject"}, "comment": "请修正", "problemSliceIds": []string{"left"}, "operationId": "closure-routing", "draftVersion": 0, "resultVersion": version}
|
||||
r := reviewSessionRequest(f, http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/reviews", f.reviewer, input, headers)
|
||||
if r.Code != 201 {
|
||||
t.Fatal(r.Code, r.Body.String())
|
||||
}
|
||||
item, err := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if item.CurrentStageID != domain.FanyuV58StageAnnotationCombined || !item.ReworkRoutingPending {
|
||||
t.Fatal("formal rejection lost durable routing intent", item.CurrentStageID, item.ReworkRoutingPending)
|
||||
}
|
||||
}
|
||||
|
||||
type filteredCountClosureStore struct {
|
||||
store.Store
|
||||
filteredVisits int
|
||||
filteredLists int
|
||||
}
|
||||
|
||||
func (s *filteredCountClosureStore) VisitHumanPoolCountCandidates(ctx context.Context, org string, q domain.HumanItemQuery, visit func([]domain.HumanItemRecord) error) error {
|
||||
if q.Search != "" || q.Capture != "" || q.Category != "" && q.Category != "all" {
|
||||
s.filteredVisits++
|
||||
}
|
||||
return s.Store.VisitHumanPoolCountCandidates(ctx, org, q, visit)
|
||||
}
|
||||
func (s *filteredCountClosureStore) ListHumanItems(ctx context.Context, org string, q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
||||
if q.Search != "" || q.Capture != "" || q.Category != "" && q.Category != "all" {
|
||||
s.filteredLists++
|
||||
}
|
||||
return s.Store.ListHumanItems(ctx, org, q)
|
||||
}
|
||||
func TestFilteredPoolCountsUseOneExhaustiveVisitorPerPool(t *testing.T) {
|
||||
f := newCombinedDraftFixture(t)
|
||||
probe := &filteredCountClosureStore{Store: f.store}
|
||||
f.handler = NewHandler(probe, nil)
|
||||
r := f.request(http.MethodGet, "/api/human-item-counts?pluginId=annotation-qa&search=capture-custom", f.admin, nil)
|
||||
if r.Code != 200 {
|
||||
t.Fatal(r.Code, r.Body.String())
|
||||
}
|
||||
var body struct {
|
||||
Counts map[string]int `json:"counts"`
|
||||
}
|
||||
if err := json.Unmarshal(r.Body.Bytes(), &body); err != nil || body.Counts["all"] != 1 {
|
||||
t.Fatal("wrong filtered count", body, err)
|
||||
}
|
||||
if probe.filteredVisits != 4 || probe.filteredLists != 0 {
|
||||
t.Fatalf("filtered counts repeated paged list reads: visitors=%d lists=%d", probe.filteredVisits, probe.filteredLists)
|
||||
}
|
||||
}
|
||||
193
internal/reworkrepair/repair.go
Normal file
193
internal/reworkrepair/repair.go
Normal file
@ -0,0 +1,193 @@
|
||||
// Package reworkrepair prepares a bounded, reviewable handoff of historical
|
||||
// unassigned rework. It never resets annotations or assigns an annotator.
|
||||
package reworkrepair
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrPlanChanged = errors.New("rework handoff plan changed; no overwrite allowed")
|
||||
|
||||
type Entry struct {
|
||||
ItemID string `json:"itemId"`
|
||||
Version int `json:"version"`
|
||||
ProjectID string `json:"projectId"`
|
||||
SupplierID string `json:"supplierId"`
|
||||
AuthorID string `json:"authorId"`
|
||||
TaskID string `json:"taskId"`
|
||||
Round int `json:"round"`
|
||||
Revision int `json:"revision"`
|
||||
BaselineDigest string `json:"baselineDigest"`
|
||||
}
|
||||
type Plan struct {
|
||||
Schema int `json:"schema"`
|
||||
Database string `json:"database"`
|
||||
OrganizationID string `json:"organizationId"`
|
||||
Entries []Entry `json:"entries"`
|
||||
Digest string `json:"digest"`
|
||||
}
|
||||
type Result struct {
|
||||
Marked int `json:"marked"`
|
||||
AlreadyQueued int `json:"alreadyQueued"`
|
||||
AuditEvents int `json:"auditEvents"`
|
||||
}
|
||||
|
||||
func digest(v any) string {
|
||||
b, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%x", sha256.Sum256(b))
|
||||
}
|
||||
func planDigest(p Plan) string { p.Digest = ""; return digest(p) }
|
||||
func load(ctx context.Context, s store.Store, org, id string) (*domain.HumanItemRecord, Entry, error) {
|
||||
item, err := s.GetHumanItem(ctx, id)
|
||||
if err != nil {
|
||||
return nil, Entry{}, err
|
||||
}
|
||||
if item == nil || item.OrganizationID != org || item.CurrentStageID != domain.FanyuV58StageAnnotationCombined || item.Status != domain.HumanItemRework || item.AssignedUserID != "" || item.SupplierID == "" || item.LatestAuthorUserID == "" || item.ReviewRound < 1 || item.Version < 1 || item.AccountHandoff != nil && item.AccountHandoff.Pending {
|
||||
return nil, Entry{}, ErrPlanChanged
|
||||
}
|
||||
user, err := s.GetUser(ctx, item.LatestAuthorUserID)
|
||||
if err != nil {
|
||||
return nil, Entry{}, err
|
||||
}
|
||||
if user == nil || user.OrganizationID != org || (user.DeletedAt == "" && user.DeletionRequestedAt == "") {
|
||||
return nil, Entry{}, ErrPlanChanged
|
||||
}
|
||||
task, err := s.GetAnnotationTask(ctx, item.LegacyTaskID)
|
||||
if err != nil {
|
||||
return nil, Entry{}, err
|
||||
}
|
||||
if task == nil || task.OrganizationID != org || task.ProjectID != item.ProjectID || task.SupplierID != "" && task.SupplierID != item.SupplierID {
|
||||
return nil, Entry{}, ErrPlanChanged
|
||||
}
|
||||
baseline, err := s.LoadInheritedReworkBaseline(ctx, *item)
|
||||
if err != nil {
|
||||
return nil, Entry{}, err
|
||||
}
|
||||
if baseline == nil || len(baseline.Slices) == 0 {
|
||||
return nil, Entry{}, ErrPlanChanged
|
||||
}
|
||||
e := Entry{ItemID: item.ID, Version: item.Version, ProjectID: item.ProjectID, SupplierID: item.SupplierID, AuthorID: item.LatestAuthorUserID, TaskID: item.LegacyTaskID, Round: item.ReviewRound, Revision: item.CurrentRevision, BaselineDigest: digest(baseline)}
|
||||
if e.BaselineDigest == "" {
|
||||
return nil, Entry{}, ErrPlanChanged
|
||||
}
|
||||
return item, e, nil
|
||||
}
|
||||
|
||||
// Prepare only reads the explicitly selected IDs. There is no production-wide
|
||||
// implicit scan and no attempt to reconstruct missing formal evidence.
|
||||
func Prepare(ctx context.Context, s store.Store, database, org string, ids []string) (Plan, error) {
|
||||
p := Plan{Schema: 1, Database: database, OrganizationID: org, Entries: []Entry{}}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return p, err
|
||||
}
|
||||
if database == "" || org == "" || len(ids) == 0 || len(ids) > 100 {
|
||||
return p, ErrPlanChanged
|
||||
}
|
||||
ids = append([]string(nil), ids...)
|
||||
sort.Strings(ids)
|
||||
for i, id := range ids {
|
||||
if id == "" || i > 0 && ids[i-1] == id {
|
||||
return p, ErrPlanChanged
|
||||
}
|
||||
item, e, err := load(ctx, s, org, id)
|
||||
if err != nil {
|
||||
return p, err
|
||||
}
|
||||
if item.InheritedRework || item.ReworkRoutingPending {
|
||||
return p, ErrPlanChanged
|
||||
}
|
||||
p.Entries = append(p.Entries, e)
|
||||
}
|
||||
p.Digest = planDigest(p)
|
||||
if p.Digest == "" {
|
||||
return p, ErrPlanChanged
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
// Apply requires the exact plan digest and an active platform administrator.
|
||||
// Every entry is validated before writes. Each write still uses its own CAS;
|
||||
// partial failure is explicit, and retry never replaces annotations or drafts.
|
||||
func Apply(ctx context.Context, s store.Store, database string, p Plan, confirmation, actorID string) (Result, error) {
|
||||
result := Result{}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if p.Schema != 1 || p.Database != database || p.OrganizationID == "" || p.Digest == "" || p.Digest != confirmation || p.Digest != planDigest(p) || len(p.Entries) == 0 || len(p.Entries) > 100 {
|
||||
return result, ErrPlanChanged
|
||||
}
|
||||
actor, err := s.GetUser(ctx, actorID)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if actor == nil || actor.Status != domain.UserStatusActive || actor.DeletedAt != "" || actor.DeletionRequestedAt != "" || actor.Role != domain.UserRoleAdmin || !actor.IsSuperAdmin && actor.OrganizationID != p.OrganizationID {
|
||||
return result, ErrPlanChanged
|
||||
}
|
||||
items := make([]*domain.HumanItemRecord, len(p.Entries))
|
||||
already := make([]bool, len(items))
|
||||
seen := map[string]bool{}
|
||||
for i, e := range p.Entries {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
if seen[e.ItemID] {
|
||||
return result, ErrPlanChanged
|
||||
}
|
||||
seen[e.ItemID] = true
|
||||
item, current, err := load(ctx, s, p.OrganizationID, e.ItemID)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
already[i] = item.Version == e.Version+1 && item.InheritedRework && item.ReworkRoutingPending
|
||||
if already[i] {
|
||||
current.Version = e.Version
|
||||
}
|
||||
if current != e || !already[i] && (item.InheritedRework || item.ReworkRoutingPending) {
|
||||
return result, ErrPlanChanged
|
||||
}
|
||||
items[i] = item
|
||||
}
|
||||
for i, item := range items {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return result, err
|
||||
}
|
||||
e := p.Entries[i]
|
||||
// Persist an intent before exposing the marker to the normal router.
|
||||
// The router may assign immediately; recording only afterwards would
|
||||
// leave no repair audit if this process crashed and the task advanced.
|
||||
at := time.Now().UTC().Format(time.RFC3339Nano)
|
||||
event := domain.HumanEventRecord{ID: "historical-rework-handoff:" + p.Digest + ":" + item.ID, OrganizationID: item.OrganizationID, BatchID: item.BatchID, ItemID: item.ID, StageID: item.CurrentStageID, Round: item.ReviewRound, EventType: "deleted_author_rework_handoff_requested", ActorUserID: actorID, ItemVersion: e.Version, CreatedAt: at, Payload: map[string]any{"repairPlan": p.Digest, "originalAuthorUserId": e.AuthorID, "preservesReviewedContent": true, "expectedItemVersion": e.Version}}
|
||||
if err := s.AppendHumanEventOnce(ctx, event); err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.AuditEvents++
|
||||
if !already[i] {
|
||||
item.InheritedRework = true
|
||||
item.ReworkRoutingPending = true
|
||||
item.Version++
|
||||
item.UpdatedAt = at
|
||||
saved, err := s.MarkInheritedReworkPending(ctx, *item, e.Version)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if !saved {
|
||||
return result, ErrPlanChanged
|
||||
}
|
||||
result.Marked++
|
||||
} else {
|
||||
result.AlreadyQueued++
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
119
internal/reworkrepair/repair_mongo_test.go
Normal file
119
internal/reworkrepair/repair_mongo_test.go
Normal file
@ -0,0 +1,119 @@
|
||||
package reworkrepair
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func TestHistoricalReworkPlanMongoCASAndPreservation(t *testing.T) {
|
||||
uri := os.Getenv("POOL_LOCAL_MONGO_URI")
|
||||
if uri == "" {
|
||||
t.Skip("isolated local Mongo opt-in")
|
||||
}
|
||||
u, err := url.Parse(uri)
|
||||
if err != nil || u.Hostname() != "127.0.0.1" && u.Hostname() != "localhost" {
|
||||
t.Fatal("local Mongo only")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 20*time.Second)
|
||||
defer cancel()
|
||||
client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri).SetServerSelectionTimeout(3*time.Second))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db := client.Database(fmt.Sprintf("historical_rework_plan_%d", time.Now().UnixNano()))
|
||||
t.Cleanup(func() {
|
||||
cleanup, stop := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer stop()
|
||||
if err := db.Drop(cleanup); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_ = client.Disconnect(cleanup)
|
||||
})
|
||||
s := store.NewMongoStore(db)
|
||||
mem, item := fixture(t)
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for _, id := range []string{"admin", "deleted"} {
|
||||
user, err := mem.GetUser(ctx, id)
|
||||
must(err)
|
||||
must(s.UpsertUser(ctx, *user))
|
||||
}
|
||||
task, err := mem.GetAnnotationTask(ctx, item.LegacyTaskID)
|
||||
must(err)
|
||||
must(s.UpsertAnnotationTask(ctx, *task))
|
||||
must(s.UpsertHumanItem(ctx, item))
|
||||
baseline, err := mem.GetReworkBaseline(ctx, store.ReworkBaselineID(item.ID, 1))
|
||||
must(err)
|
||||
must(s.UpsertReworkBaseline(ctx, *baseline))
|
||||
before, err := s.GetHumanItem(ctx, item.ID)
|
||||
must(err)
|
||||
plan, err := Prepare(ctx, s, db.Name(), item.OrganizationID, []string{item.ID})
|
||||
must(err)
|
||||
afterPlan, err := s.GetHumanItem(ctx, item.ID)
|
||||
must(err)
|
||||
if !reflect.DeepEqual(before, afterPlan) {
|
||||
t.Fatal("Mongo preparation mutated customer state")
|
||||
}
|
||||
var background *domain.HumanItemRecord
|
||||
writer := &faultStore{Store: s, beforeWrite: func() {
|
||||
// Model an independent progress/draft writer that does not bump the
|
||||
// workflow version. Metadata repair must not replace its fresh data.
|
||||
_, err := db.Collection("humanItems").UpdateOne(ctx, bson.M{"id": item.ID}, bson.M{"$set": bson.M{"subject.payload.annotationDraft": bson.M{"marker": "new independent write"}, "processingJob": bson.M{"id": "background", "kind": "slice", "status": "failed"}}})
|
||||
must(err)
|
||||
background, err = s.GetHumanItem(ctx, item.ID)
|
||||
must(err)
|
||||
}}
|
||||
result, err := Apply(ctx, writer, db.Name(), plan, plan.Digest, "admin")
|
||||
must(err)
|
||||
if result.Marked != 1 {
|
||||
t.Fatal(result)
|
||||
}
|
||||
after, err := s.GetHumanItem(ctx, item.ID)
|
||||
must(err)
|
||||
if background == nil || !reflect.DeepEqual(background.Subject, after.Subject) || !reflect.DeepEqual(background.ProcessingJob, after.ProcessingJob) || after.CurrentRevision != before.CurrentRevision || !after.ReworkRoutingPending {
|
||||
t.Fatal("Mongo repair changed reviewed contents")
|
||||
}
|
||||
result, err = Apply(ctx, s, db.Name(), plan, plan.Digest, "admin")
|
||||
must(err)
|
||||
if result.AlreadyQueued != 1 {
|
||||
t.Fatal(result)
|
||||
}
|
||||
events, err := s.ListHumanEvents(ctx, item.OrganizationID, item.ID)
|
||||
must(err)
|
||||
if len(events) != 1 {
|
||||
t.Fatal("duplicate audit", len(events))
|
||||
}
|
||||
// A newer claimant wins: the reviewed old plan cannot revert their edits.
|
||||
after.AssignedUserID = "new-annotator"
|
||||
after.Version++
|
||||
saved, err := s.UpdateHumanItemVersioned(ctx, *after, after.Version-1)
|
||||
must(err)
|
||||
if !saved {
|
||||
t.Fatal("fixture claim CAS failed")
|
||||
}
|
||||
claimed, err := s.GetHumanItem(ctx, item.ID)
|
||||
must(err)
|
||||
if _, err := Apply(ctx, s, db.Name(), plan, plan.Digest, "admin"); err == nil {
|
||||
t.Fatal("accepted obsolete plan")
|
||||
}
|
||||
final, err := s.GetHumanItem(ctx, item.ID)
|
||||
must(err)
|
||||
if !reflect.DeepEqual(final, claimed) {
|
||||
t.Fatal("repair overwrote new claimant")
|
||||
}
|
||||
}
|
||||
191
internal/reworkrepair/repair_test.go
Normal file
191
internal/reworkrepair/repair_test.go
Normal file
@ -0,0 +1,191 @@
|
||||
package reworkrepair
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"droboticflow/internal/store"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func fixture(t *testing.T) (*store.MemoryStore, domain.HumanItemRecord) {
|
||||
t.Helper()
|
||||
s := store.NewMemoryStore()
|
||||
ctx := t.Context()
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
must(s.UpsertOrganization(ctx, domain.OrganizationRecord{ID: "org", Status: domain.OrganizationStatusActive}))
|
||||
must(s.UpsertProject(ctx, domain.ProjectRecord{ID: "project", OrganizationID: "org"}))
|
||||
must(s.UpsertSupplier(ctx, domain.SupplierRecord{ID: "supplier", OrganizationID: "org", Status: "active"}))
|
||||
must(s.UpsertUser(ctx, domain.UserRecord{ID: "admin", OrganizationID: "org", Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}))
|
||||
must(s.UpsertUser(ctx, domain.UserRecord{ID: "deleted", OrganizationID: "org", Role: domain.UserRoleAnnotator, Status: domain.UserStatusDisabled, DeletedAt: "2026-11-01T00:00:00Z"}))
|
||||
must(s.UpsertAnnotationTask(ctx, domain.AnnotationTaskRecord{ID: "task", OrganizationID: "org", ProjectID: "project", SupplierID: "supplier", CurrentRevision: 3, Status: domain.AnnotationTaskStatusRework}))
|
||||
item := domain.HumanItemRecord{ID: "item", OrganizationID: "org", ProjectID: "project", SupplierID: "supplier", LegacyTaskID: "task", BatchID: "batch", PluginID: "annotation-qa", CurrentStageID: domain.FanyuV58StageAnnotationCombined, Status: domain.HumanItemRework, ReviewRound: 1, CurrentRevision: 3, LatestAuthorUserID: "deleted", Version: 9, Subject: domain.HumanSubjectRef{Payload: map[string]any{"annotationWorkspace": map[string]any{"marker": "retain private history"}, "annotationDraft": map[string]any{"marker": "do not clear"}}}}
|
||||
must(s.UpsertHumanItem(ctx, item))
|
||||
must(s.UpsertReworkBaseline(ctx, domain.ReworkBaselineRecord{ID: store.ReworkBaselineID(item.ID, 1), OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, ItemID: item.ID, Round: 1, Slices: []domain.SliceSegment{{ID: "slice", Start: 0, End: 10}}, Actions: []domain.AnnotationSegment{{ID: "action", Start: 0, End: 10, Description: "formal description"}}, Problems: []domain.ReviewProblem{{ID: "review", Text: "formal quality comment"}}}))
|
||||
return s, item
|
||||
}
|
||||
func TestPrepareAndApplyPreserveReviewedContentsAndRetry(t *testing.T) {
|
||||
s, item := fixture(t)
|
||||
ctx := t.Context()
|
||||
before, _ := s.GetHumanItem(ctx, item.ID)
|
||||
baseline, _ := s.GetReworkBaseline(ctx, store.ReworkBaselineID(item.ID, 1))
|
||||
p, err := Prepare(ctx, s, "isolated", "org", []string{item.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
afterPlan, _ := s.GetHumanItem(ctx, item.ID)
|
||||
if !reflect.DeepEqual(before, afterPlan) {
|
||||
t.Fatal("dry run changed a task")
|
||||
}
|
||||
if _, err := Apply(ctx, s, "isolated", p, "wrong", "admin"); err == nil {
|
||||
t.Fatal("accepted unconfirmed plan")
|
||||
}
|
||||
result, err := Apply(ctx, s, "isolated", p, p.Digest, "admin")
|
||||
if err != nil || result.Marked != 1 || result.AuditEvents != 1 {
|
||||
t.Fatal(result, err)
|
||||
}
|
||||
after, _ := s.GetHumanItem(ctx, item.ID)
|
||||
if !after.InheritedRework || !after.ReworkRoutingPending || after.Version != before.Version+1 || after.AssignedUserID != "" || !reflect.DeepEqual(before.Subject, after.Subject) || after.CurrentRevision != before.CurrentRevision {
|
||||
t.Fatal("repair overwrote reviewed work")
|
||||
}
|
||||
baselineAfter, _ := s.GetReworkBaseline(ctx, store.ReworkBaselineID(item.ID, 1))
|
||||
if !reflect.DeepEqual(baseline, baselineAfter) {
|
||||
t.Fatal("quality comments changed")
|
||||
}
|
||||
again, err := Apply(ctx, s, "isolated", p, p.Digest, "admin")
|
||||
if err != nil || again.Marked != 0 || again.AlreadyQueued != 1 {
|
||||
t.Fatal("retry not idempotent", again, err)
|
||||
}
|
||||
events, err := s.ListHumanEvents(ctx, "org", item.ID)
|
||||
if err != nil || len(events) != 1 {
|
||||
t.Fatal("duplicate audit", len(events), err)
|
||||
}
|
||||
}
|
||||
|
||||
type faultStore struct {
|
||||
store.Store
|
||||
beforeWrite func()
|
||||
failAudit bool
|
||||
lostMarkerReply bool
|
||||
}
|
||||
|
||||
func (s *faultStore) MarkInheritedReworkPending(ctx context.Context, i domain.HumanItemRecord, v int) (bool, error) {
|
||||
if s.beforeWrite != nil {
|
||||
fn := s.beforeWrite
|
||||
s.beforeWrite = nil
|
||||
fn()
|
||||
}
|
||||
saved, err := s.Store.MarkInheritedReworkPending(ctx, i, v)
|
||||
if err == nil && saved && s.lostMarkerReply {
|
||||
s.lostMarkerReply = false
|
||||
return false, errors.New("synthetic crash after durable marker")
|
||||
}
|
||||
return saved, err
|
||||
}
|
||||
func (s *faultStore) AppendHumanEventOnce(ctx context.Context, e domain.HumanEventRecord) error {
|
||||
if s.failAudit {
|
||||
s.failAudit = false
|
||||
return errors.New("synthetic interruption after marker")
|
||||
}
|
||||
return s.Store.AppendHumanEventOnce(ctx, e)
|
||||
}
|
||||
func TestHandoffConcurrentClaimAndLostAuditNeverOverwrite(t *testing.T) {
|
||||
for _, race := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "audit-restart", true: "concurrent-claim"}[race], func(t *testing.T) {
|
||||
s, item := fixture(t)
|
||||
ctx := t.Context()
|
||||
p, err := Prepare(ctx, s, "isolated", "org", []string{item.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := &faultStore{Store: s, failAudit: !race}
|
||||
if race {
|
||||
f.beforeWrite = func() {
|
||||
current, _ := s.GetHumanItem(ctx, item.ID)
|
||||
current.AssignedUserID = "next-worker"
|
||||
current.Version++
|
||||
current.Subject.Payload["annotationDraft"] = map[string]any{"marker": "new edits"}
|
||||
if err := s.UpsertHumanItem(ctx, *current); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
result, err := Apply(ctx, f, "isolated", p, p.Digest, "admin")
|
||||
if err == nil {
|
||||
t.Fatal("fault was not surfaced")
|
||||
}
|
||||
current, _ := s.GetHumanItem(ctx, item.ID)
|
||||
if race {
|
||||
if result.Marked != 0 || current.AssignedUserID != "next-worker" || current.InheritedRework {
|
||||
t.Fatal("claim overwritten")
|
||||
}
|
||||
return
|
||||
}
|
||||
if result.Marked != 0 || current.InheritedRework {
|
||||
t.Fatal("marker published without durable intent")
|
||||
}
|
||||
repaired, err := Apply(ctx, s, "isolated", p, p.Digest, "admin")
|
||||
if err != nil || repaired.Marked != 1 || repaired.AuditEvents != 1 {
|
||||
t.Fatal("audit restart failed", repaired, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandoffLostMarkerReplyRestartsWithDurableAudit(t *testing.T) {
|
||||
s, item := fixture(t)
|
||||
ctx := t.Context()
|
||||
p, err := Prepare(ctx, s, "isolated", "org", []string{item.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f := &faultStore{Store: s, lostMarkerReply: true}
|
||||
if _, err := Apply(ctx, f, "isolated", p, p.Digest, "admin"); err == nil {
|
||||
t.Fatal("lost response not surfaced")
|
||||
}
|
||||
events, err := s.ListHumanEvents(ctx, item.OrganizationID, item.ID)
|
||||
if err != nil || len(events) != 1 || events[0].EventType != "deleted_author_rework_handoff_requested" {
|
||||
t.Fatal("marker has no durable repair intent", err)
|
||||
}
|
||||
r, err := Apply(ctx, s, "isolated", p, p.Digest, "admin")
|
||||
if err != nil || r.Marked != 0 || r.AlreadyQueued != 1 {
|
||||
t.Fatal("restart did not recognize durable marker", r, err)
|
||||
}
|
||||
}
|
||||
func TestHandoffRejectsScopeTamperingPermissionAndCancellation(t *testing.T) {
|
||||
s, item := fixture(t)
|
||||
ctx := t.Context()
|
||||
p, err := Prepare(ctx, s, "isolated", "org", []string{item.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, actor := range []string{"deleted", "missing"} {
|
||||
if _, err := Apply(ctx, s, "isolated", p, p.Digest, actor); err == nil {
|
||||
t.Fatal("unauthorized operator accepted")
|
||||
}
|
||||
}
|
||||
if _, err := Prepare(ctx, s, "isolated", "foreign", []string{item.ID}); err == nil {
|
||||
t.Fatal("foreign task accepted")
|
||||
}
|
||||
tampered := p
|
||||
tampered.Entries = append([]Entry(nil), p.Entries...)
|
||||
tampered.Entries[0].SupplierID = "other"
|
||||
if _, err := Apply(ctx, s, "isolated", tampered, p.Digest, "admin"); err == nil {
|
||||
t.Fatal("tampered plan accepted")
|
||||
}
|
||||
canceled, stop := context.WithCancel(ctx)
|
||||
stop()
|
||||
if _, err := Apply(canceled, s, "isolated", p, p.Digest, "admin"); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("canceled mutation accepted", err)
|
||||
}
|
||||
current, _ := s.GetHumanItem(ctx, item.ID)
|
||||
if current.Version != item.Version || current.InheritedRework {
|
||||
t.Fatal("refusal changed original data")
|
||||
}
|
||||
}
|
||||
@ -47,10 +47,17 @@ func (s *MongoStore) readSourceReadyClaimCandidates(ctx context.Context, filter
|
||||
// The candidate predicates have several competing organization-prefixed
|
||||
// indexes. Avoid spending the read budget on multi-plan trials; this
|
||||
// existing queue index covers the shared count/claim routing fields.
|
||||
read := options.Find().SetMaxTime(8 * time.Second).SetProjection(projection).SetBatchSize(200).SetHint("review_queue_candidates")
|
||||
hint := "review_queue_candidates"
|
||||
if _, scoped := filter["currentStageId"]; scoped {
|
||||
hint = "pool_stage_available_sorted"
|
||||
}
|
||||
read := options.Find().SetMaxTime(8 * time.Second).SetProjection(projection).SetBatchSize(200).SetHint(hint)
|
||||
cursor, err := s.humanItems.Find(ctx, filter, read)
|
||||
// Legacy installations and isolated fixtures may not have the index yet.
|
||||
// Keep their original query instead of requiring an index migration here.
|
||||
if poolSearchHintMissing(err) && ctx.Err() == nil && hint != "review_queue_candidates" {
|
||||
cursor, err = s.humanItems.Find(ctx, filter, read.SetHint("review_queue_candidates"))
|
||||
}
|
||||
if poolSearchHintMissing(err) && ctx.Err() == nil {
|
||||
cursor, err = s.humanItems.Find(ctx, filter, read.SetHint(nil))
|
||||
}
|
||||
|
||||
47
internal/store/annotation_claim_stage_read.go
Normal file
47
internal/store/annotation_claim_stage_read.go
Normal file
@ -0,0 +1,47 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// This is a necessary stage scope, not a replacement permission predicate.
|
||||
// Keep the first definition of duplicate stage IDs, as the domain lookup does;
|
||||
// unknown/custom IDs and historical annotator-role aliases remain supported.
|
||||
func (s *MongoStore) annotationCandidateStageScope(ctx context.Context, org, project string, filter bson.M) (bson.M, error) {
|
||||
cur, err := s.humanBatches.Find(ctx, bson.M{"organizationId": normalizedOrganizationID(org), "projectId": bson.M{"$in": bson.A{project, "", nil}}, "status": bson.M{"$ne": domain.HumanBatchCompleted}}, options.Find().SetProjection(bson.M{"_id": 0, "stages": 1}))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
stages := map[string]bool{}
|
||||
for cur.Next(ctx) {
|
||||
var b domain.HumanBatchRecord
|
||||
if err := cur.Decode(&b); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, stage := range b.Stages {
|
||||
if seen[stage.ID] {
|
||||
continue
|
||||
}
|
||||
seen[stage.ID] = true
|
||||
if stage.Kind == domain.HumanStageAnnotation && domain.AnnotationRoleForHumanStage(stage) == domain.AnnotationRoleAnnotator {
|
||||
stages[stage.ID] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := cur.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids := make([]string, 0, len(stages))
|
||||
for id := range stages {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
filter["currentStageId"] = bson.M{"$in": ids}
|
||||
return filter, nil
|
||||
}
|
||||
@ -28,12 +28,20 @@ func (s *MemoryStore) VisitAnnotationClaimCandidates(ctx context.Context, org, p
|
||||
}
|
||||
func (s *MongoStore) VisitAnnotationClaimCandidates(ctx context.Context, org, project string, visit func(domain.HumanItemRecord) (bool, error)) (bool, error) {
|
||||
filter := bson.M{"reworkRoutingPending": bson.M{"$ne": true}, "inheritedRework": bson.M{"$ne": true}, "organizationId": org, "projectId": project, "pluginId": "annotation-qa", "legacyTaskId": bson.M{"$exists": true, "$ne": ""}, "assignedUserId": bson.M{"$in": bson.A{"", nil}}, "status": bson.M{"$in": bson.A{domain.HumanItemReady, domain.HumanItemRework, domain.HumanItemSubmitted}}}
|
||||
filter, err := s.annotationCandidateStageScope(ctx, org, project, filter)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
projection := annotationSourceAdmissionProjection()
|
||||
projection["_id"] = 0
|
||||
pipeline := mongo.Pipeline{{{Key: "$sample", Value: bson.M{"size": 64}}}, {{Key: "$match", Value: filter}}, {{Key: "$project", Value: projection}}}
|
||||
pipeline := mongo.Pipeline{{{Key: "$match", Value: filter}}, {{Key: "$sample", Value: bson.M{"size": 64}}}, {{Key: "$project", Value: projection}}}
|
||||
seen := map[string]bool{}
|
||||
for attempt := 0; attempt < 4; attempt++ {
|
||||
cursor, err := s.humanItems.Aggregate(ctx, pipeline, options.Aggregate().SetMaxTime(5*time.Second).SetBatchSize(64))
|
||||
for attempt := 0; attempt < 1; attempt++ {
|
||||
read := options.Aggregate().SetMaxTime(5 * time.Second).SetBatchSize(64).SetHint("pool_stage_available_sorted")
|
||||
cursor, err := s.humanItems.Aggregate(ctx, pipeline, read)
|
||||
if poolSearchHintMissing(err) && ctx.Err() == nil {
|
||||
cursor, err = s.humanItems.Aggregate(ctx, pipeline, read.SetHint(nil))
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
@ -214,7 +214,11 @@ func (store *MongoStore) GetCaptureRecordByWorkflowPrefix(ctx context.Context, w
|
||||
|
||||
func (store *MongoStore) GetCaptureRecordByRunID(ctx context.Context, runID string) (*domain.CaptureRecord, error) {
|
||||
var item domain.CaptureRecord
|
||||
err := store.captureRecords.FindOne(ctx, bson.M{"runId": runID}).Decode(&item)
|
||||
read := options.FindOne().SetHint(captureRunLookupIndex)
|
||||
err := store.captureRecords.FindOne(ctx, bson.M{"runId": runID}, read).Decode(&item)
|
||||
if poolSearchHintMissing(err) && ctx.Err() == nil {
|
||||
err = store.captureRecords.FindOne(ctx, bson.M{"runId": runID}, read.SetHint(nil)).Decode(&item)
|
||||
}
|
||||
if err == mongo.ErrNoDocuments {
|
||||
cursor, lookupErr := store.runs.Find(ctx, bson.M{"trigger": domain.RunTriggerHumanRelease, "$or": bson.A{bson.M{"id": runID}, bson.M{"parentRunId": runID}}})
|
||||
if lookupErr != nil {
|
||||
|
||||
43
internal/store/historical_rework_handoff.go
Normal file
43
internal/store/historical_rework_handoff.go
Normal file
@ -0,0 +1,43 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// Historical repair owns routing metadata only. In particular, background
|
||||
// progress and customer payload writes may not increment the item version;
|
||||
// a full-record replacement would overwrite such concurrent writes.
|
||||
func (s *MongoStore) MarkInheritedReworkPending(ctx context.Context, item domain.HumanItemRecord, expected int) (bool, error) {
|
||||
if expected < 1 || item.ID == "" || item.OrganizationID == "" || item.SupplierID == "" {
|
||||
return false, ErrHumanClaimConflict
|
||||
}
|
||||
saved := false
|
||||
err := supplierGuardedMongoWrite(ctx, s.humanItems, item.ID, item, func(ctx context.Context, _ bson.M) error {
|
||||
filter := bson.M{"id": item.ID, "organizationId": item.OrganizationID, "version": expected, "supplierId": item.SupplierID, "latestAuthorUserId": item.LatestAuthorUserID, "legacyTaskId": item.LegacyTaskID, "reviewRound": item.ReviewRound, "currentRevision": item.CurrentRevision, "currentStageId": domain.FanyuV58StageAnnotationCombined, "status": domain.HumanItemRework, "assignedUserId": bson.M{"$in": bson.A{"", nil}}, "reworkRoutingPending": bson.M{"$ne": true}, "inheritedRework": bson.M{"$ne": true}, "accountHandoff.pending": bson.M{"$ne": true}}
|
||||
r, err := s.humanItems.UpdateOne(ctx, filter, bson.M{"$set": bson.M{"inheritedRework": true, "reworkRoutingPending": true, "version": expected + 1, "updatedAt": item.UpdatedAt}})
|
||||
if err == nil {
|
||||
saved = r.MatchedCount == 1
|
||||
}
|
||||
return err
|
||||
})
|
||||
return saved, err
|
||||
}
|
||||
|
||||
func (s *MemoryStore) MarkInheritedReworkPending(ctx context.Context, item domain.HumanItemRecord, expected int) (bool, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
old, ok := s.humanItems[item.ID]
|
||||
if !ok || expected < 1 || old.Version != expected || old.OrganizationID != item.OrganizationID || old.SupplierID == "" || old.SupplierID != item.SupplierID || old.LatestAuthorUserID != item.LatestAuthorUserID || old.LegacyTaskID != item.LegacyTaskID || old.ReviewRound != item.ReviewRound || old.CurrentRevision != item.CurrentRevision || old.CurrentStageID != domain.FanyuV58StageAnnotationCombined || old.Status != domain.HumanItemRework || old.AssignedUserID != "" || old.InheritedRework || old.ReworkRoutingPending || old.AccountHandoff != nil && old.AccountHandoff.Pending {
|
||||
return false, nil
|
||||
}
|
||||
old.InheritedRework, old.ReworkRoutingPending = true, true
|
||||
old.Version, old.UpdatedAt = expected+1, item.UpdatedAt
|
||||
s.humanItems[item.ID] = old
|
||||
return true, nil
|
||||
}
|
||||
@ -56,6 +56,10 @@ func (s *MemoryStore) ListAnnotationClaimCandidates(_ context.Context, org, proj
|
||||
}
|
||||
func (s *MongoStore) ListAnnotationClaimCandidates(ctx context.Context, org, project string) ([]domain.HumanItemRecord, error) {
|
||||
filter := bson.M{"reworkRoutingPending": bson.M{"$ne": true}, "inheritedRework": bson.M{"$ne": true}, "organizationId": org, "projectId": project, "pluginId": "annotation-qa", "legacyTaskId": bson.M{"$exists": true, "$ne": ""}, "assignedUserId": bson.M{"$in": bson.A{"", nil}}, "status": bson.M{"$in": bson.A{domain.HumanItemReady, domain.HumanItemRework, domain.HumanItemSubmitted}}}
|
||||
filter, err := s.annotationCandidateStageScope(ctx, org, project, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.listSourceReadyClaimCandidates(ctx, filter)
|
||||
}
|
||||
func (s *MemoryStore) GetHumanClaimReceipt(_ context.Context, key string) (*HumanClaimReceipt, error) {
|
||||
@ -572,7 +576,11 @@ func (s *MongoStore) CountAnnotationClaimCandidates(ctx context.Context, org str
|
||||
if len(q.Memberships) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
items, err := s.listSourceReadyClaimCandidates(ctx, annotationClaimCountCandidateFilter(org, q))
|
||||
filter, err := s.annotationCandidateStageScope(ctx, org, q.ProjectID, annotationClaimCountCandidateFilter(org, q))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
items, err := s.listSourceReadyClaimCandidates(ctx, filter)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
129
internal/store/human_pool_count_cover_test.go
Normal file
129
internal/store/human_pool_count_cover_test.go
Normal file
@ -0,0 +1,129 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/event"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPlatformPoolCountCoveredScanParityAndBounds(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var commands []bson.M
|
||||
ctx, s := poolLocalMongo(t, &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
||||
if e.CommandName == "aggregate" && e.Command.Lookup("aggregate").StringValue() == "humanItems" {
|
||||
var c bson.M
|
||||
if bson.Unmarshal(e.Command, &c) == nil {
|
||||
mu.Lock()
|
||||
commands = append(commands, c)
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
}})
|
||||
docs := make([]any, 2108)
|
||||
for i := range docs {
|
||||
d := bson.M{"id": fmt.Sprintf("count-%05d", i), "organizationId": "cover-org", "pluginId": "annotation-qa", "projectId": "project", "batchId": "batch", "workflowId": "workflow", "runId": "run", "legacyTaskId": "task", "currentStageId": "prepackage_acceptance", "status": "ready", "supplierId": "supplier", "createdAt": "2026-11-01T00:00:00Z", "subject": bson.M{"payload": bson.M{"large": strings.Repeat("x", 16<<10)}}}
|
||||
switch i % 5 {
|
||||
case 0:
|
||||
d["assignedUserId"] = "actor"
|
||||
d["latestAuthorUserId"] = "author"
|
||||
case 1:
|
||||
d["assignedUserId"] = nil
|
||||
case 2:
|
||||
d["assignedUserId"] = ""
|
||||
case 3:
|
||||
d["inheritedRework"] = true
|
||||
d["assignedUserId"] = "actor"
|
||||
d["acceptanceClaim"] = bson.M{"userId": "actor", "supplierId": "supplier", "stageId": "prepackage_acceptance", "round": 1, "claimedAt": "now"}
|
||||
d["claimedAt"] = "now"
|
||||
d["reviewRound"] = 1
|
||||
case 4:
|
||||
// Keep assignedUserId genuinely absent, including inherited work.
|
||||
d["inheritedRework"] = true
|
||||
d["currentStageId"] = "annotation_combined"
|
||||
d["status"] = "rework"
|
||||
}
|
||||
docs[i] = d
|
||||
}
|
||||
if _, err := s.humanItems.InsertMany(ctx, docs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
scopes := []domain.HumanItemQuery{
|
||||
{PluginID: "annotation-qa"},
|
||||
{PluginID: "annotation-qa", UnassignedOnly: true, StageIDs: []string{"prepackage_acceptance"}, Statuses: []domain.HumanItemStatus{domain.HumanItemReady}},
|
||||
{PluginID: "annotation-qa", ActorUserID: "actor", MinePoolOnly: true},
|
||||
{PluginID: "annotation-qa", WorkflowID: "workflow", RunID: "run", AvailableProjectIDs: []string{"project"}},
|
||||
{PluginID: "annotation-qa", IDs: []string{}},
|
||||
{PluginID: "annotation-qa", InheritedUnassigned: true},
|
||||
{PluginID: "annotation-qa", Cursor: &domain.HumanItemCursor{CreatedAt: "2026-11-01T00:00:00Z", ID: "count-00800"}},
|
||||
}
|
||||
expected := make([][]domain.HumanItemRecord, len(scopes))
|
||||
collect := func(q domain.HumanItemQuery) ([]domain.HumanItemRecord, error) {
|
||||
rows := []domain.HumanItemRecord{}
|
||||
err := s.VisitHumanPoolCountCandidates(ctx, "cover-org", q, func(page []domain.HumanItemRecord) error {
|
||||
if len(page) > humanPoolCountBatchSize {
|
||||
t.Fatal("unbounded count page")
|
||||
}
|
||||
for _, v := range page {
|
||||
v.WorkflowID = ""
|
||||
v.RunID = ""
|
||||
v.CreatedAt = ""
|
||||
rows = append(rows, v)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
sort.Slice(rows, func(i, j int) bool { return rows[i].ID < rows[j].ID })
|
||||
return rows, err
|
||||
}
|
||||
for i, q := range scopes {
|
||||
rows, err := collect(q)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected[i] = rows
|
||||
}
|
||||
if _, err := s.humanItems.Indexes().CreateMany(ctx, PlatformReadIndexes()["humanItems"]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i, q := range scopes {
|
||||
rows, err := collect(q)
|
||||
if err != nil || !reflect.DeepEqual(rows, expected[i]) {
|
||||
t.Fatal("covered count changed routing or frozen claim metadata", i, len(rows), len(expected[i]), err)
|
||||
}
|
||||
}
|
||||
mu.Lock()
|
||||
reads := commands
|
||||
mu.Unlock()
|
||||
if len(reads) == 0 {
|
||||
t.Fatal("count still fetched large task bodies")
|
||||
}
|
||||
for _, c := range reads {
|
||||
for _, k := range []string{"$db", "lsid", "$clusterTime", "readConcern", "$readPreference"} {
|
||||
delete(c, k)
|
||||
}
|
||||
var result bson.M
|
||||
if err := s.db.RunCommand(ctx, poolTestExplainCommand(c)).Decode(&result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var stats bson.M
|
||||
stats = deletionDoc(result["executionStats"])
|
||||
if stats == nil {
|
||||
for _, stage := range deletionArray(result["stages"]) {
|
||||
if cur := deletionDoc(deletionDoc(stage)["$cursor"]); cur != nil {
|
||||
stats = deletionDoc(cur["executionStats"])
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if stats == nil || supplierNumber(stats["totalDocsExamined"]) != 0 {
|
||||
t.Fatal("count covering plan FETCHed customer annotations", stats["totalDocsExamined"])
|
||||
}
|
||||
}
|
||||
t.Logf("covered count rows=%d bounded Mongo reads=%d; documents fetched=0", len(expected[0]), len(reads))
|
||||
}
|
||||
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
@ -15,10 +16,10 @@ const humanPoolCountBatchSize = 1000
|
||||
// Counts have no ordering contract. Scan one cursor instead of re-running a
|
||||
// sorted list query for every authorized page. Authorization stays in the API's
|
||||
// existing filter; this visitor is not an authorized total or a public list.
|
||||
// Search/category queries retain the list path until their joins are exhausted.
|
||||
// Search/category counts use the same exhaustive matcher without detail hydration.
|
||||
func (s *MongoStore) VisitHumanPoolCountCandidates(ctx context.Context, org string, q domain.HumanItemQuery, visit func([]domain.HumanItemRecord) error) error {
|
||||
if humanPoolFiltered(q) {
|
||||
return fmt.Errorf("filtered counts require the search list path")
|
||||
return s.visitFilteredHumanPoolCountCandidates(ctx, org, q, visit)
|
||||
}
|
||||
filter := humanItemMongoFilter(org, q)
|
||||
if q.BlindAnnotationActorID != "" {
|
||||
@ -28,6 +29,15 @@ func (s *MongoStore) VisitHumanPoolCountCandidates(ctx context.Context, org stri
|
||||
}
|
||||
filter = bson.M{"$and": bson.A{filter, visibility}}
|
||||
}
|
||||
// A facet boundary keeps null/missing-value predicates above the covered
|
||||
// projection. Limit each facet to one bounded page, never the full pool.
|
||||
// Routing scans use additional fields and retain their existing path.
|
||||
if q.BlindAnnotationActorID == "" && !q.NeedsReworkRouting {
|
||||
used, err := s.visitCoveredHumanPoolCounts(ctx, org, q, filter, visit)
|
||||
if used || err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
projection := bson.M{"_id": 0}
|
||||
for _, field := range []string{"inheritedRework", "id", "organizationId", "pluginId", "projectId", "batchId", "legacyTaskId", "supplierId", "processingScope", "processingProfile", "workgroupId", "vendorId", "currentStageId", "status", "assignedUserId", "latestAuthorUserId", "acceptanceClaim", "reviewRound", "claimedAt"} {
|
||||
projection[field] = 1
|
||||
@ -61,13 +71,64 @@ func (s *MongoStore) VisitHumanPoolCountCandidates(ctx context.Context, org stri
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MongoStore) visitCoveredHumanPoolCounts(ctx context.Context, org string, q domain.HumanItemQuery, filter bson.M, visit func([]domain.HumanItemRecord) error) (bool, error) {
|
||||
projection := bson.M{"_id": 0}
|
||||
for _, field := range humanPoolCountFields {
|
||||
projection[field] = 1
|
||||
}
|
||||
lastID := ""
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return true, err
|
||||
}
|
||||
anchor := bson.M{"organizationId": normalizedOrganizationID(org)}
|
||||
if q.PluginID != "" {
|
||||
anchor["pluginId"] = q.PluginID
|
||||
}
|
||||
if lastID != "" {
|
||||
anchor["id"] = bson.M{"$gt": lastID}
|
||||
}
|
||||
pipeline := mongo.Pipeline{
|
||||
{{Key: "$match", Value: anchor}},
|
||||
{{Key: "$sort", Value: bson.D{{Key: "id", Value: 1}}}},
|
||||
{{Key: "$project", Value: projection}},
|
||||
{{Key: "$facet", Value: bson.M{"rows": bson.A{bson.M{"$match": filter}, bson.M{"$limit": humanPoolCountBatchSize}}}}},
|
||||
{{Key: "$unwind", Value: "$rows"}},
|
||||
{{Key: "$replaceRoot", Value: bson.M{"newRoot": "$rows"}}},
|
||||
}
|
||||
cur, err := s.humanItems.Aggregate(ctx, pipeline, options.Aggregate().SetHint(humanPoolCountIndex).SetMaxTime(8*time.Second).SetBatchSize(humanPoolCountBatchSize))
|
||||
if err != nil {
|
||||
if lastID == "" && poolSearchHintMissing(err) {
|
||||
return false, nil
|
||||
}
|
||||
return true, err
|
||||
}
|
||||
var rows []domain.HumanItemRecord
|
||||
err = cur.All(ctx, &rows)
|
||||
if err != nil {
|
||||
return true, err
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return true, nil
|
||||
}
|
||||
next := rows[len(rows)-1].ID
|
||||
if next == "" || next <= lastID {
|
||||
return true, fmt.Errorf("pool count cursor did not advance")
|
||||
}
|
||||
if err := visit(rows); err != nil {
|
||||
return true, err
|
||||
}
|
||||
if len(rows) < humanPoolCountBatchSize {
|
||||
return true, nil
|
||||
}
|
||||
lastID = next
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MemoryStore) VisitHumanPoolCountCandidates(ctx context.Context, org string, q domain.HumanItemQuery, visit func([]domain.HumanItemRecord) error) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if humanPoolFiltered(q) {
|
||||
return fmt.Errorf("filtered counts require the search list path")
|
||||
}
|
||||
q.Limit = 0
|
||||
q.PoolCandidateProjection = true
|
||||
rows, err := s.ListHumanItems(ctx, org, q)
|
||||
@ -86,3 +147,41 @@ func (s *MemoryStore) VisitHumanPoolCountCandidates(ctx context.Context, org str
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
// Search counts stream matched metadata once, instead of restarting discovery
|
||||
// and foreign joins for every public page. Authorization remains in the API.
|
||||
func (s *MongoStore) visitFilteredHumanPoolCountCandidates(ctx context.Context, org string, q domain.HumanItemQuery, visit func([]domain.HumanItemRecord) error) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
||||
defer cancel()
|
||||
q.Limit = 0
|
||||
q.Cursor = nil
|
||||
filter, empty, err := s.prepareHumanPoolSearchFilter(ctx, org, q)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if empty {
|
||||
return nil
|
||||
}
|
||||
page := make([]domain.HumanItemRecord, 0, humanPoolSearchJoinPageSize)
|
||||
// Counts must exhaust the entire scope. A single streaming join avoids
|
||||
// repeating root reads and query setup for every list-sized chunk.
|
||||
err = s.visitJoinedHumanPoolSearchRows(ctx, org, q, filter, humanPoolSearchHasBoundedIDs(filter), func(row adminPoolRow) (bool, error) {
|
||||
item := row.HumanItemRecord
|
||||
item.Subject.Payload = nil
|
||||
page = append(page, item)
|
||||
if len(page) == humanPoolSearchJoinPageSize {
|
||||
if err := visit(page); err != nil {
|
||||
return false, err
|
||||
}
|
||||
page = make([]domain.HumanItemRecord, 0, humanPoolSearchJoinPageSize)
|
||||
}
|
||||
return false, nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(page) > 0 {
|
||||
return visit(page)
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
@ -88,81 +87,51 @@ func (s *MongoStore) listHumanPoolSearch(ctx context.Context, org string, q doma
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if q.IDs != nil && len(q.IDs) == 0 || q.AvailableProjectIDs != nil && len(q.AvailableProjectIDs) == 0 {
|
||||
filter, empty, err := s.prepareHumanPoolSearchFilter(ctx, org, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if empty {
|
||||
return []domain.HumanItemRecord{}, nil
|
||||
}
|
||||
ids := []string{}
|
||||
err = s.visitHumanPoolSearchRows(ctx, org, q, filter, func(row adminPoolRow) (bool, error) {
|
||||
ids = append(ids, row.ID)
|
||||
return q.Limit > 0 && len(ids) >= q.Limit, nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.hydrateHumanPoolSearch(ctx, org, q, ids)
|
||||
}
|
||||
|
||||
func (s *MongoStore) prepareHumanPoolSearchFilter(ctx context.Context, org string, q domain.HumanItemQuery) (bson.M, bool, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if q.IDs != nil && len(q.IDs) == 0 || q.AvailableProjectIDs != nil && len(q.AvailableProjectIDs) == 0 {
|
||||
return nil, true, nil
|
||||
}
|
||||
filter := humanItemMongoFilter(org, q)
|
||||
if q.BlindAnnotationActorID != "" {
|
||||
visibility, err := s.blindAnnotationMongoVisibility(ctx, org, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
filter = bson.M{"$and": bson.A{filter, visibility}}
|
||||
}
|
||||
candidates, err := s.humanPoolSearchCandidates(ctx, org, q, filter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, false, err
|
||||
}
|
||||
if candidates != nil {
|
||||
// Candidate discovery already applied the original root scope. An empty
|
||||
// set cannot produce a visible row; don't execute empty joins/sorts for
|
||||
// the other tabs of a count request.
|
||||
if ids, ok := candidates["id"].(bson.M); ok {
|
||||
if values, ok := ids["$in"].([]string); ok && len(values) == 0 {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []domain.HumanItemRecord{}, nil
|
||||
return nil, true, nil
|
||||
}
|
||||
}
|
||||
filter = bson.M{"$and": bson.A{filter, candidates}}
|
||||
}
|
||||
pipeline := humanPoolSearchPipeline(org, q, filter)
|
||||
for _, text := range []string{q.Search, q.Capture} {
|
||||
if text != "" {
|
||||
if searchFilter := humanPoolSearchPrefilter(text); searchFilter != nil {
|
||||
pipeline = append(pipeline, bson.D{{Key: "$match", Value: searchFilter}})
|
||||
}
|
||||
}
|
||||
}
|
||||
if category := humanPoolCategoryPrefilter(q.Category, q.ReviewedPool); category != nil {
|
||||
pipeline = append(pipeline, bson.D{{Key: "$match", Value: category}})
|
||||
}
|
||||
cur, err := s.humanItems.Aggregate(ctx, pipeline, options.Aggregate().SetAllowDiskUse(false).SetMaxTime(8*time.Second).SetBatchSize(200))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
ids := []string{}
|
||||
for {
|
||||
rows := []adminPoolRow{}
|
||||
for len(rows) < 200 && cur.Next(ctx) {
|
||||
var row adminPoolRow
|
||||
if err := cur.Decode(&row); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
if err := cur.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
break
|
||||
}
|
||||
if err := s.humanPoolSearchLegacyPayloads(ctx, org, q, rows); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, row := range rows {
|
||||
if humanPoolSearchRowMatches(row, q) {
|
||||
ids = append(ids, row.ID)
|
||||
}
|
||||
if q.Limit > 0 && len(ids) >= q.Limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
if q.Limit > 0 && len(ids) >= q.Limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
return s.hydrateHumanPoolSearch(ctx, org, q, ids)
|
||||
return filter, false, nil
|
||||
}
|
||||
|
||||
@ -14,6 +14,11 @@ func humanPoolSearchPipeline(org string, q domain.HumanItemQuery, filter bson.M)
|
||||
// Only search/category metadata participates in global sorts and joins.
|
||||
// Hydrate matched IDs afterwards; the public record shape stays unchanged.
|
||||
projection := bson.M{"version": 1, "updatedAt": 1, "_id": 0, "id": 1, "organizationId": 1, "pluginId": 1, "projectId": 1, "runId": 1, "legacyTaskId": 1, "createdAt": 1, "currentStageId": 1, "status": 1, "reviewRound": 1, "pendingCombinedSubmissionId": 1, "assignedUserId": 1, "subject.itemKey": 1, "subject.artifactId": 1, "subject.payload.sliceWorkflow.status": 1, "subject.payload.sliceWorkflow.phase": 1}
|
||||
// The same rows feed exhaustive metadata counts; retain every field the
|
||||
// existing server visibility predicate reads, never infer permissions here.
|
||||
for _, field := range []string{"batchId", "supplierId", "processingScope", "processingProfile", "workgroupId", "vendorId", "latestAuthorUserId", "acceptanceClaim", "inheritedRework", "reworkRoutingPending", "accountHandoff", "claimedAt", "annotationEpoch"} {
|
||||
projection[field] = 1
|
||||
}
|
||||
// Reverse lookup validity was checked before projection. The Go matcher
|
||||
// needs display/legacy evidence, not duplicated regex text/source snapshots.
|
||||
for _, field := range []string{"schema", "sourceVersion", "sourceUpdatedAt", "sourceItemKey", "sourceArtifactId", "payloadDirectories", "durationSeconds"} {
|
||||
|
||||
170
internal/store/human_pool_search_pages.go
Normal file
170
internal/store/human_pool_search_pages.go
Normal file
@ -0,0 +1,170 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"time"
|
||||
)
|
||||
|
||||
const humanPoolSearchJoinPageSize = 200
|
||||
|
||||
// Read ordered root identities before joining. A page request must not sort and
|
||||
// join every task in a broad pool before it can return its first 25 results.
|
||||
// No inventory is truncated: exhausted chunks advance until enough matches or
|
||||
// the entire cursor is consumed. Each join reapplies the original scope.
|
||||
func (s *MongoStore) visitHumanPoolSearchRows(ctx context.Context, org string, q domain.HumanItemQuery, filter bson.M, visit func(adminPoolRow) (bool, error)) error {
|
||||
// Reverse discovery may already have a small exact identity set. Join it
|
||||
// directly rather than reading the same identities a second time.
|
||||
if humanPoolSearchHasBoundedIDs(filter) {
|
||||
return s.visitJoinedHumanPoolSearchRows(ctx, org, q, filter, true, visit)
|
||||
}
|
||||
read := options.Find().SetProjection(bson.M{"_id": 0, "id": 1, "createdAt": 1}).SetSort(bson.D{{Key: "createdAt", Value: -1}, {Key: "id", Value: -1}}).SetBatchSize(humanPoolSearchJoinPageSize).SetMaxTime(8 * time.Second).SetHint(poolGuardIndex)
|
||||
roots, err := s.humanItems.Find(ctx, filter, read)
|
||||
if poolSearchHintMissing(err) && ctx.Err() == nil {
|
||||
roots, err = s.humanItems.Find(ctx, filter, read.SetHint("organizationId_createdAt_id"))
|
||||
}
|
||||
if poolSearchHintMissing(err) && ctx.Err() == nil {
|
||||
roots, err = s.humanItems.Find(ctx, filter, read.SetHint(nil))
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer roots.Close(ctx)
|
||||
firstChunk := true
|
||||
for {
|
||||
ids := make([]string, 0, humanPoolSearchJoinPageSize)
|
||||
for len(ids) < humanPoolSearchJoinPageSize && roots.Next(ctx) {
|
||||
var root struct {
|
||||
ID string `bson:"id"`
|
||||
}
|
||||
if err := roots.Decode(&root); err != nil {
|
||||
return err
|
||||
}
|
||||
ids = append(ids, root.ID)
|
||||
}
|
||||
if err := roots.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return ctx.Err()
|
||||
}
|
||||
scoped := bson.M{"$and": bson.A{filter, bson.M{"id": bson.M{"$in": ids}}}}
|
||||
cur, err := s.readJoinedHumanPoolSearchRows(ctx, org, q, scoped, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var rows []adminPoolRow
|
||||
err = cur.All(ctx, &rows)
|
||||
_ = cur.Close(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.humanPoolSearchLegacyPayloads(ctx, org, q, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
matched := false
|
||||
for _, row := range rows {
|
||||
if !humanPoolSearchRowMatches(row, q) {
|
||||
continue
|
||||
}
|
||||
matched = true
|
||||
done, err := visit(row)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if done {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
// A sparse/no-hit query should not transfer every root identity to the
|
||||
// API. After one bounded probe, retain the server-side exhaustive path
|
||||
// for the remainder. Excluding IDs preserves missing-date semantics.
|
||||
if firstChunk && !matched {
|
||||
_ = roots.Close(ctx)
|
||||
remaining := bson.M{"$and": bson.A{filter, bson.M{"id": bson.M{"$nin": ids}}}}
|
||||
return s.visitJoinedHumanPoolSearchRows(ctx, org, q, remaining, false, visit)
|
||||
}
|
||||
firstChunk = false
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func humanPoolSearchHasBoundedIDs(filter bson.M) bool {
|
||||
if id, ok := filter["id"].(bson.M); ok {
|
||||
if ids, ok := id["$in"].([]string); ok && len(ids) <= humanPoolSearchJoinPageSize {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if and, ok := filter["$and"].(bson.A); ok {
|
||||
for _, clause := range and {
|
||||
if m, ok := clause.(bson.M); ok && humanPoolSearchHasBoundedIDs(m) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *MongoStore) readJoinedHumanPoolSearchRows(ctx context.Context, org string, q domain.HumanItemQuery, filter bson.M, bounded bool) (*mongo.Cursor, error) {
|
||||
pipeline := humanPoolSearchPipeline(org, q, filter)
|
||||
for _, text := range []string{q.Search, q.Capture} {
|
||||
if text != "" {
|
||||
if f := humanPoolSearchPrefilter(text); f != nil {
|
||||
pipeline = append(pipeline, bson.D{{Key: "$match", Value: f}})
|
||||
}
|
||||
}
|
||||
}
|
||||
if category := humanPoolCategoryPrefilter(q.Category, q.ReviewedPool); category != nil {
|
||||
pipeline = append(pipeline, bson.D{{Key: "$match", Value: category}})
|
||||
}
|
||||
read := options.Aggregate().SetAllowDiskUse(false).SetMaxTime(8 * time.Second).SetBatchSize(humanPoolSearchJoinPageSize)
|
||||
if bounded {
|
||||
read.SetHint(bson.D{{Key: "id", Value: 1}})
|
||||
}
|
||||
cur, err := s.humanItems.Aggregate(ctx, pipeline, read)
|
||||
if bounded && poolSearchHintMissing(err) && ctx.Err() == nil {
|
||||
cur, err = s.humanItems.Aggregate(ctx, pipeline, read.SetHint(nil))
|
||||
}
|
||||
return cur, err
|
||||
}
|
||||
func (s *MongoStore) visitJoinedHumanPoolSearchRows(ctx context.Context, org string, q domain.HumanItemQuery, filter bson.M, bounded bool, visit func(adminPoolRow) (bool, error)) error {
|
||||
cur, err := s.readJoinedHumanPoolSearchRows(ctx, org, q, filter, bounded)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cur.Close(ctx)
|
||||
for {
|
||||
rows := make([]adminPoolRow, 0, humanPoolSearchJoinPageSize)
|
||||
for len(rows) < humanPoolSearchJoinPageSize && cur.Next(ctx) {
|
||||
var row adminPoolRow
|
||||
if err := cur.Decode(&row); err != nil {
|
||||
return err
|
||||
}
|
||||
rows = append(rows, row)
|
||||
}
|
||||
if err := cur.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return ctx.Err()
|
||||
}
|
||||
if err := s.humanPoolSearchLegacyPayloads(ctx, org, q, rows); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, row := range rows {
|
||||
if !humanPoolSearchRowMatches(row, q) {
|
||||
continue
|
||||
}
|
||||
done, err := visit(row)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if done {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -52,13 +52,17 @@ func testHumanPoolSearchReferenceScope(t *testing.T, cover bool) {
|
||||
if !measuring.Load() {
|
||||
return
|
||||
}
|
||||
if e.CommandName == "aggregate" && e.Command.Lookup("aggregate").StringValue() == "humanItems" && e.Command.Lookup("hint").Type == 0 {
|
||||
// Page joins may now use the unique id key pattern. Only the scalar
|
||||
// discovery commands carry string hints and promise zero document reads.
|
||||
hint := e.Command.Lookup("hint")
|
||||
_, scalarHint := hint.StringValueOK()
|
||||
if e.CommandName == "aggregate" && e.Command.Lookup("aggregate").StringValue() == "humanItems" && !scalarHint {
|
||||
joinedScans.Add(1)
|
||||
}
|
||||
if e.CommandName == "aggregate" && e.Command.Lookup("aggregate").StringValue() == "humanItems" && e.Command.Lookup("hint").Type != 0 {
|
||||
if e.CommandName == "aggregate" && e.Command.Lookup("aggregate").StringValue() == "humanItems" && scalarHint {
|
||||
guardScans.Add(1)
|
||||
}
|
||||
if cover && (e.CommandName == "find" || e.CommandName == "aggregate") && e.Command.Lookup("hint").Type != 0 {
|
||||
if cover && (e.CommandName == "find" || e.CommandName == "aggregate") && scalarHint {
|
||||
var command bson.M
|
||||
if bson.Unmarshal(e.Command, &command) == nil {
|
||||
mu.Lock()
|
||||
|
||||
@ -1565,7 +1565,11 @@ func (store *MongoStore) CountWorkerRuns(ctx context.Context) (WorkerRunCounts,
|
||||
"count": bson.M{"$sum": 1},
|
||||
}}},
|
||||
}
|
||||
cursor, err := store.runs.Aggregate(ctx, pipeline)
|
||||
read := options.Aggregate().SetHint(workerRunCountIndex)
|
||||
cursor, err := store.runs.Aggregate(ctx, pipeline, read)
|
||||
if poolSearchHintMissing(err) && ctx.Err() == nil {
|
||||
cursor, err = store.runs.Aggregate(ctx, pipeline, read.SetHint(nil))
|
||||
}
|
||||
if err != nil {
|
||||
return WorkerRunCounts{}, err
|
||||
}
|
||||
|
||||
273
internal/store/platform_query_regression_test.go
Normal file
273
internal/store/platform_query_regression_test.go
Normal file
@ -0,0 +1,273 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"droboticflow/internal/domain"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/event"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPlatformClaimStageReadDoesNotFetchReviewBacklog(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var query bson.M
|
||||
ctx, s := poolLocalMongo(t, &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
||||
if e.CommandName == "find" && e.Command.Lookup("find").StringValue() == "humanItems" {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
_ = bson.Unmarshal(e.Command, &query)
|
||||
}
|
||||
}})
|
||||
if err := s.EnsureIndexes(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const org, project = "closure-org", "closure-project"
|
||||
batch := domain.HumanBatchRecord{ID: "batch", OrganizationID: org, ProjectID: project, Stages: domain.DefaultFanyuV58HumanStages(), Status: domain.HumanBatchActive}
|
||||
if err := s.UpsertHumanBatch(ctx, batch); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
docs := make([]any, 1042)
|
||||
for i := range docs {
|
||||
stage := domain.FanyuV58StageQualityReview
|
||||
if i < 42 {
|
||||
stage = domain.FanyuV58StageAnnotationCombined
|
||||
}
|
||||
docs[i] = bson.M{"id": fmt.Sprintf("item-%d", i), "organizationId": org, "projectId": project, "pluginId": "annotation-qa", "batchId": batch.ID, "legacyTaskId": fmt.Sprintf("task-%d", i), "currentStageId": stage, "status": "ready", "assignedUserId": "", "supplierId": "supplier", "subject": bson.M{"itemKey": fmt.Sprintf("source-%d", i), "payload": bson.M{"large": strings.Repeat("x", 16<<10)}}}
|
||||
}
|
||||
if _, err := s.humanItems.InsertMany(ctx, docs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
n, err := s.CountAnnotationClaimCandidates(ctx, org, AnnotationClaimCountQuery{ProjectID: project, ActiveSupplierIDs: []string{"supplier"}, Memberships: []AnnotationClaimMembership{{SupplierID: "supplier", Legacy: true}}})
|
||||
if err != nil || n != 42 {
|
||||
t.Fatal("wrong inventory", n, err)
|
||||
}
|
||||
mu.Lock()
|
||||
captured := query
|
||||
mu.Unlock()
|
||||
find := bson.D{{Key: "find", Value: "humanItems"}, {Key: "filter", Value: captured["filter"]}, {Key: "projection", Value: captured["projection"]}}
|
||||
if h := captured["hint"]; h != nil {
|
||||
find = append(find, bson.E{Key: "hint", Value: h})
|
||||
}
|
||||
var result bson.M
|
||||
if err := s.db.RunCommand(ctx, bson.D{{Key: "explain", Value: find}, {Key: "verbosity", Value: "executionStats"}}).Decode(&result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stats := deletionDoc(result["executionStats"])
|
||||
examined := supplierNumber(stats["totalDocsExamined"])
|
||||
t.Logf("claim candidates=%d examined=%d keys=%v", n, examined, stats["totalKeysExamined"])
|
||||
if examined > 50 {
|
||||
t.Fatal("annotation inventory fetched unrelated review payloads", examined)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlatformWorkerRunCountsDoNotFetchRunPayloads(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var query bson.M
|
||||
ctx, s := poolLocalMongo(t, &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
||||
if (e.CommandName == "aggregate" && e.Command.Lookup("aggregate").StringValue() == "runs") || (e.CommandName == "find" && e.Command.Lookup("find").StringValue() == "runs") {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
_ = bson.Unmarshal(e.Command, &query)
|
||||
}
|
||||
}})
|
||||
if err := s.EnsureIndexes(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := s.runs.Indexes().CreateOne(ctx, PlatformReadIndexes()["runs"][0]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
docs := make([]any, 2300)
|
||||
for i := range docs {
|
||||
status := "completed"
|
||||
if i < 300 {
|
||||
status = "queued"
|
||||
}
|
||||
docs[i] = bson.M{"id": fmt.Sprintf("run-%d", i), "workflowId": fmt.Sprintf("workflow-%d", i%7), "status": status, "logs": strings.Repeat("x", 16<<10)}
|
||||
}
|
||||
if _, err := s.runs.InsertMany(ctx, docs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
n, err := s.CountWorkerRuns(ctx)
|
||||
if err != nil || n.QueuedTotal != 300 {
|
||||
t.Fatal("wrong worker counts", n, err)
|
||||
}
|
||||
mu.Lock()
|
||||
captured := query
|
||||
mu.Unlock()
|
||||
|
||||
command := bson.D{{Key: "aggregate", Value: "runs"}, {Key: "pipeline", Value: captured["pipeline"]}, {Key: "cursor", Value: bson.M{}}}
|
||||
if _, ok := captured["find"]; ok {
|
||||
command = bson.D{{Key: "find", Value: "runs"}, {Key: "filter", Value: captured["filter"]}, {Key: "projection", Value: captured["projection"]}}
|
||||
}
|
||||
if h := captured["hint"]; h != nil {
|
||||
command = append(command, bson.E{Key: "hint", Value: h})
|
||||
}
|
||||
var result bson.M
|
||||
if err := s.db.RunCommand(ctx, bson.D{{Key: "explain", Value: command}, {Key: "verbosity", Value: "executionStats"}}).Decode(&result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
stats := deletionDoc(result["executionStats"])
|
||||
if stats == nil {
|
||||
for _, raw := range deletionArray(result["stages"]) {
|
||||
if cursor := deletionDoc(deletionDoc(raw)["$cursor"]); cursor != nil {
|
||||
stats = deletionDoc(cursor["executionStats"])
|
||||
}
|
||||
}
|
||||
}
|
||||
t.Logf("worker queued=%d examined=%v keys=%v", n.QueuedTotal, stats["totalDocsExamined"], stats["totalKeysExamined"])
|
||||
if stats == nil || supplierNumber(stats["totalDocsExamined"]) != 0 {
|
||||
t.Fatalf("worker polling fetched large run bodies: docs=%v", stats["totalDocsExamined"])
|
||||
}
|
||||
}
|
||||
|
||||
// A broad text match must not build and sort every foreign join before the
|
||||
// first page can be returned. The original matcher still decides each row.
|
||||
func TestPlatformBroadPoolSearchBoundsFirstPageJoins(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var joined []bson.M
|
||||
ctx, s := poolLocalMongo(t, &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
||||
if e.CommandName != "aggregate" || e.Command.Lookup("aggregate").StringValue() != "humanItems" {
|
||||
return
|
||||
}
|
||||
var q bson.M
|
||||
if bson.Unmarshal(e.Command, &q) != nil {
|
||||
return
|
||||
}
|
||||
raw, _ := bson.Marshal(q)
|
||||
if strings.Contains(string(raw), "$lookup") {
|
||||
mu.Lock()
|
||||
joined = append(joined, q)
|
||||
mu.Unlock()
|
||||
}
|
||||
}})
|
||||
if err := s.EnsureIndexes(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
docs := make([]any, 5000)
|
||||
for i := range docs {
|
||||
docs[i] = bson.M{"id": fmt.Sprintf("pool-%05d", i), "organizationId": "broad-org", "pluginId": "annotation-qa", "currentStageId": "prepackage_acceptance", "status": "ready", "assignedUserId": "", "createdAt": fmt.Sprintf("2026-11-%05d", i), "subject": bson.M{"itemKey": fmt.Sprintf("FYLX_BROAD_%05d/part0001", i)}}
|
||||
}
|
||||
if _, err := s.humanItems.InsertMany(ctx, docs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rows, err := s.ListHumanItems(ctx, "broad-org", domain.HumanItemQuery{PluginID: "annotation-qa", CurrentStageID: "prepackage_acceptance", Search: "FYLX_BROAD", Limit: 25, IdentityProjection: true})
|
||||
if err != nil || len(rows) != 25 {
|
||||
t.Fatal("first page", len(rows), err)
|
||||
}
|
||||
if rows[0].ID != "pool-04999" || rows[24].ID != "pool-04975" {
|
||||
t.Fatal("order changed")
|
||||
}
|
||||
mu.Lock()
|
||||
queries := joined
|
||||
mu.Unlock()
|
||||
if len(queries) == 0 {
|
||||
t.Fatal("no actual join query observed")
|
||||
}
|
||||
for _, q := range queries {
|
||||
var result bson.M
|
||||
cmd := poolTestExplainCommand(q)
|
||||
for _, key := range []string{"$db", "lsid", "$clusterTime", "readConcern", "$readPreference"} {
|
||||
delete(q, key)
|
||||
}
|
||||
cmd = poolTestExplainCommand(q)
|
||||
if err := s.db.RunCommand(ctx, cmd).Decode(&result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var examined int64
|
||||
if stats := deletionDoc(result["executionStats"]); stats != nil {
|
||||
examined = int64(supplierNumber(stats["totalDocsExamined"]))
|
||||
}
|
||||
for _, raw := range deletionArray(result["stages"]) {
|
||||
if c := deletionDoc(deletionDoc(raw)["$cursor"]); c != nil {
|
||||
examined = int64(supplierNumber(deletionDoc(c["executionStats"])["totalDocsExamined"]))
|
||||
}
|
||||
}
|
||||
t.Logf("first-page joins=%d root documents=%d", len(queries), examined)
|
||||
if examined > 250 {
|
||||
t.Fatal("first-page join materialized whole inventory", examined)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlatformRecoveryLookupIndexesPreserveLegacyReceipts(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
reads := map[string]bson.M{}
|
||||
ctx, s := poolLocalMongo(t, &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
||||
if e.CommandName != "find" {
|
||||
return
|
||||
}
|
||||
name := e.Command.Lookup("find").StringValue()
|
||||
if name != "reviewOperations" && name != "reviewSessions" && name != "captureRecords" {
|
||||
return
|
||||
}
|
||||
var q bson.M
|
||||
if bson.Unmarshal(e.Command, &q) != nil {
|
||||
return
|
||||
}
|
||||
mu.Lock()
|
||||
reads[name] = q
|
||||
mu.Unlock()
|
||||
}})
|
||||
ops, sessions, captures := []any{}, []any{}, []any{}
|
||||
for i := 0; i < 3000; i++ {
|
||||
ops = append(ops, bson.M{"_id": fmt.Sprintf("legacy-receipt-%d", i), "organizationId": "recovery-org", "userId": "reviewer", "operationId": fmt.Sprintf("op-%d", i), "itemId": "item", "taskId": "task", "status": "completed"})
|
||||
sessions = append(sessions, bson.M{"_id": fmt.Sprintf("session-%d", i), "revision": 1})
|
||||
captures = append(captures, bson.M{"id": fmt.Sprintf("capture-%d", i), "runId": fmt.Sprintf("run-%d", i)})
|
||||
}
|
||||
for name, docs := range map[string][]any{"reviewOperations": ops, "reviewSessions": sessions, "captureRecords": captures} {
|
||||
if _, err := s.db.Collection(name).InsertMany(ctx, docs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
explainLast := func(name string) int64 {
|
||||
mu.Lock()
|
||||
q := reads[name]
|
||||
mu.Unlock()
|
||||
for _, k := range []string{"$db", "lsid", "$clusterTime", "readConcern", "$readPreference"} {
|
||||
delete(q, k)
|
||||
}
|
||||
var result bson.M
|
||||
if err := s.db.RunCommand(ctx, poolTestExplainCommand(q)).Decode(&result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return supplierNumber(deletionDoc(result["executionStats"])["totalDocsExamined"])
|
||||
}
|
||||
for _, indexed := range []bool{false, true} {
|
||||
if indexed {
|
||||
for name, models := range PlatformReadIndexes() {
|
||||
if _, err := s.db.Collection(name).Indexes().CreateMany(ctx, models); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
op, err := s.FindReviewOperation(ctx, "recovery-org", "reviewer", "item", "op-2999")
|
||||
if err != nil || op == nil || op.ID != "legacy-receipt-2999" {
|
||||
t.Fatal("legacy item receipt lookup changed", err)
|
||||
}
|
||||
itemDocs := explainLast("reviewOperations")
|
||||
alias, err := s.FindReviewOperation(ctx, "recovery-org", "reviewer", "task", "op-2999")
|
||||
if err != nil || alias == nil || alias.ID != op.ID {
|
||||
t.Fatal("task alias receipt lookup changed", err)
|
||||
}
|
||||
if denied, err := s.FindReviewOperation(ctx, "foreign-org", "reviewer", "item", "op-2999"); err != nil || denied != nil {
|
||||
t.Fatal("receipt tenant scope widened", err)
|
||||
}
|
||||
if q, err := s.GetReviewContinuation(ctx, "queue-with-no-pending-admission"); err != nil || q != nil {
|
||||
t.Fatal("missing queue recovery changed", err)
|
||||
}
|
||||
sessionDocs := explainLast("reviewSessions")
|
||||
capture, err := s.GetCaptureRecordByRunID(ctx, "run-2999")
|
||||
if err != nil || capture == nil || capture.ID != "capture-2999" {
|
||||
t.Fatal("capture run relation changed", err)
|
||||
}
|
||||
captureDocs := explainLast("captureRecords")
|
||||
t.Logf("indexed=%t receiptDocs=%d sessionDocs=%d captureDocs=%d", indexed, itemDocs, sessionDocs, captureDocs)
|
||||
if indexed && (itemDocs > 1 || sessionDocs > 0 || captureDocs > 1) {
|
||||
t.Fatal("recovery lookup still scans unrelated records")
|
||||
}
|
||||
}
|
||||
reviewContinuationAdmissionContract(t, s)
|
||||
}
|
||||
37
internal/store/platform_read_indexes.go
Normal file
37
internal/store/platform_read_indexes.go
Normal file
@ -0,0 +1,37 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
const humanPoolCountIndex = "pool_count_candidates_cover_v1"
|
||||
|
||||
var humanPoolCountFields = []string{"organizationId", "id", "pluginId", "currentStageId", "status", "assignedUserId", "projectId", "batchId", "legacyTaskId", "supplierId", "processingScope", "processingProfile", "workgroupId", "vendorId", "latestAuthorUserId", "reviewRound", "claimedAt", "acceptanceClaim", "inheritedRework", "workflowId", "runId", "createdAt"}
|
||||
|
||||
const workerRunCountIndex = "worker_run_counts_cover_v1"
|
||||
const reviewOperationLookupIndex = "review_operation_lookup_v1"
|
||||
const reviewAdmissionLookupIndex = "review_continuation_admission_v1"
|
||||
const captureRunLookupIndex = "capture_run_lookup_v1"
|
||||
|
||||
// Opt-in only: used by isolated fixtures and explicit operator plans. Never
|
||||
// add these builds to startup; production index writes require authorization.
|
||||
func PlatformReadIndexes() map[string][]mongo.IndexModel {
|
||||
model := func(name string, fields ...string) mongo.IndexModel {
|
||||
keys := bson.D{}
|
||||
for _, f := range fields {
|
||||
keys = append(keys, bson.E{Key: f, Value: 1})
|
||||
}
|
||||
return mongo.IndexModel{Keys: keys, Options: options.Index().SetName(name)}
|
||||
}
|
||||
admission := model(reviewAdmissionLookupIndex, "continuationAdmission.continuationId")
|
||||
admission.Options.SetPartialFilterExpression(bson.M{"continuationAdmission.continuationId": bson.M{"$exists": true}})
|
||||
return map[string][]mongo.IndexModel{
|
||||
"humanItems": {model(humanPoolCountIndex, humanPoolCountFields...)},
|
||||
"runs": {model(workerRunCountIndex, "status", "workflowId"), model("human_release_parent_v1", "trigger", "parentRunId", "id")},
|
||||
"reviewOperations": {model(reviewOperationLookupIndex, "organizationId", "userId", "operationId")},
|
||||
"reviewSessions": {admission},
|
||||
"captureRecords": {model(captureRunLookupIndex, "runId")},
|
||||
}
|
||||
}
|
||||
68
internal/store/platform_read_indexes_test.go
Normal file
68
internal/store/platform_read_indexes_test.go
Normal file
@ -0,0 +1,68 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func TestPlatformReadIndexPlanMatchesImplementation(t *testing.T) {
|
||||
data, err := os.ReadFile("../../docs/operations/platform-query-indexes.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var rows []struct {
|
||||
Collection, Name string
|
||||
Keys json.RawMessage
|
||||
PartialFilterExpression json.RawMessage
|
||||
}
|
||||
if err := json.Unmarshal(data, &rows); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
models := PlatformReadIndexes()
|
||||
seen := map[string]bool{}
|
||||
for _, row := range rows {
|
||||
var keys bson.D
|
||||
if err := bson.UnmarshalExtJSON(row.Keys, false, &keys); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, m := range models[row.Collection] {
|
||||
if *m.Options.Name != row.Name {
|
||||
continue
|
||||
}
|
||||
id := row.Collection + "/" + row.Name
|
||||
planned, _ := bson.Marshal(keys)
|
||||
actual, _ := bson.Marshal(m.Keys)
|
||||
if seen[id] || !bytes.Equal(planned, actual) {
|
||||
t.Fatal("index plan differs", id)
|
||||
}
|
||||
seen[id], found = true, true
|
||||
if len(row.PartialFilterExpression) > 0 {
|
||||
var partial bson.M
|
||||
if err := bson.UnmarshalExtJSON(row.PartialFilterExpression, false, &partial); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(partial, m.Options.PartialFilterExpression) {
|
||||
t.Fatal("partial index differs", id)
|
||||
}
|
||||
} else if m.Options.PartialFilterExpression != nil {
|
||||
t.Fatal("missing partial index plan", id)
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("unknown planned index", row.Name)
|
||||
}
|
||||
}
|
||||
for collection, list := range models {
|
||||
for _, m := range list {
|
||||
if !seen[collection+"/"+*m.Options.Name] {
|
||||
t.Fatal("undocumented index", *m.Options.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ import (
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var ErrReviewContinuationOrigin = errors.New("review continuation origin changed")
|
||||
@ -128,7 +129,12 @@ func (s *MongoStore) applyReviewContinuationAdmission(ctx context.Context, sessi
|
||||
func (s *MongoStore) GetReviewContinuation(ctx context.Context, id string) (*domain.ReviewContinuation, error) {
|
||||
// A crash between the session CAS and queue projection is recovered by the
|
||||
// next ordinary queue request, even if the source page has already changed.
|
||||
cursor, err := s.db.Collection("reviewSessions").Find(ctx, bson.M{"continuationAdmission.continuationId": id})
|
||||
filter := bson.M{"continuationAdmission.continuationId": id}
|
||||
read := options.Find().SetHint(reviewAdmissionLookupIndex)
|
||||
cursor, err := s.db.Collection("reviewSessions").Find(ctx, filter, read)
|
||||
if poolSearchHintMissing(err) && ctx.Err() == nil {
|
||||
cursor, err = s.db.Collection("reviewSessions").Find(ctx, filter, read.SetHint(nil))
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
func cloneReviewOperation(v domain.ReviewOperation) domain.ReviewOperation {
|
||||
@ -62,7 +63,12 @@ func (s *MongoStore) GetReviewOperation(ctx context.Context, id string) (*domain
|
||||
}
|
||||
func (s *MongoStore) FindReviewOperation(ctx context.Context, org, user, resource, operation string) (*domain.ReviewOperation, error) {
|
||||
var v domain.ReviewOperation
|
||||
e := s.db.Collection("reviewOperations").FindOne(ctx, bson.M{"organizationId": org, "userId": user, "operationId": operation, "$or": bson.A{bson.M{"itemId": resource}, bson.M{"taskId": resource}}}).Decode(&v)
|
||||
filter := bson.M{"organizationId": org, "userId": user, "operationId": operation, "$or": bson.A{bson.M{"itemId": resource}, bson.M{"taskId": resource}}}
|
||||
read := options.FindOne().SetHint(reviewOperationLookupIndex)
|
||||
e := s.db.Collection("reviewOperations").FindOne(ctx, filter, read).Decode(&v)
|
||||
if poolSearchHintMissing(e) && ctx.Err() == nil {
|
||||
e = s.db.Collection("reviewOperations").FindOne(ctx, filter, read.SetHint(nil)).Decode(&v)
|
||||
}
|
||||
if errors.Is(e, mongo.ErrNoDocuments) {
|
||||
return nil, nil
|
||||
}
|
||||
@ -87,7 +93,7 @@ func (s *MongoStore) CompareAndSwapReviewOperation(ctx context.Context, v domain
|
||||
|
||||
// Review decisions own workflow state, never the customer's annotation/evidence.
|
||||
func humanReviewFields(v domain.HumanItemRecord) bson.M {
|
||||
return bson.M{"currentStageId": v.CurrentStageID, "status": v.Status,
|
||||
return bson.M{"reworkRoutingPending": v.ReworkRoutingPending, "inheritedRework": v.InheritedRework, "currentStageId": v.CurrentStageID, "status": v.Status,
|
||||
"assignedUserId": v.AssignedUserID, "claimedAt": v.ClaimedAt,
|
||||
"reviewRound": v.ReviewRound, "version": v.Version,
|
||||
"acceptedAt": v.AcceptedAt, "acceptedByUserId": v.AcceptedByUserID,
|
||||
@ -106,6 +112,7 @@ func (s *MemoryStore) UpdateHumanItemForReview(_ context.Context, next domain.Hu
|
||||
return false, nil
|
||||
}
|
||||
current.CurrentStageID, current.Status = next.CurrentStageID, next.Status
|
||||
current.ReworkRoutingPending, current.InheritedRework = next.ReworkRoutingPending, next.InheritedRework
|
||||
current.AssignedUserID, current.ClaimedAt = next.AssignedUserID, next.ClaimedAt
|
||||
current.ReviewRound, current.Version = next.ReviewRound, next.Version
|
||||
current.AcceptedAt, current.AcceptedByUserID = next.AcceptedAt, next.AcceptedByUserID
|
||||
|
||||
@ -23,6 +23,8 @@ func reviewPatchContract(t *testing.T, s Store) {
|
||||
next.Version = 6
|
||||
next.Status = domain.HumanItemSubmitted
|
||||
next.ReviewOperationID = "decision"
|
||||
next.ReworkRoutingPending = true
|
||||
next.InheritedRework = true
|
||||
if ok, e := s.UpdateHumanItemForReview(ctx, next, 4); ok || e != nil {
|
||||
t.Fatalf("stale write: %v %v", ok, e)
|
||||
}
|
||||
@ -33,7 +35,7 @@ func reviewPatchContract(t *testing.T, s Store) {
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
if got.Subject.Payload["history"] != "keep" || got.Subject.Payload["annotation"] != "original" || got.Version != 6 || got.ReviewOperationID != "decision" {
|
||||
if got.Subject.Payload["history"] != "keep" || got.Subject.Payload["annotation"] != "original" || got.Version != 6 || got.ReviewOperationID != "decision" || !got.ReworkRoutingPending || !got.InheritedRework {
|
||||
t.Fatal("lost evidence or transition")
|
||||
}
|
||||
if ok, e := s.UpdateHumanItemForReview(ctx, next, 5); ok || e != nil {
|
||||
|
||||
@ -326,6 +326,7 @@ type Store interface {
|
||||
AnnotationWorkBufferSummary(context.Context, string, string, string, int) (AnnotationWorkBuffer, error)
|
||||
ResetDeletedAnnotatorTask(context.Context, domain.AnnotationTaskRecord) error
|
||||
ClearReworkRoutingPending(context.Context, string, int) (bool, error)
|
||||
MarkInheritedReworkPending(context.Context, domain.HumanItemRecord, int) (bool, error)
|
||||
LoadInheritedReworkBaseline(context.Context, domain.HumanItemRecord) (*domain.ReworkBaselineRecord, error)
|
||||
CommitHumanClaim(context.Context, HumanClaimCommit) (*domain.HumanItemRecord, error)
|
||||
GetHumanItem(context.Context, string) (*domain.HumanItemRecord, error)
|
||||
|
||||
@ -734,6 +734,64 @@
|
||||
"warnings": [
|
||||
"新规则默认关闭;启用前须完成所有写入组件兼容升级及单独授权的路由索引建设。生产数据补处理不包含在本地验证中。"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "platform-query-closure",
|
||||
"paths": [
|
||||
"^internal/store/(annotation_claim_stage_read|historical_rework_handoff|human_pool_count_cover_test|platform_query_regression_test|platform_read_indexes|platform_read_indexes_test|review_continuation|review_decision_patch_test)\\.go$",
|
||||
"^internal/api/platform_closure_test\\.go$",
|
||||
"^internal/reworkrepair/",
|
||||
"^cmd/rework-handoff-plan/",
|
||||
"^docs/operations/platform-query-.*"
|
||||
],
|
||||
"frontend": [],
|
||||
"go": {
|
||||
"./internal/store": [
|
||||
"^TestPlatform",
|
||||
"^TestHumanPoolSearchScopeFlights",
|
||||
"^TestHumanPoolSearchMongoCoveredDiscovery"
|
||||
],
|
||||
"./internal/api": [
|
||||
"^TestFilteredPoolCountsUseOneExhaustiveVisitorPerPool$",
|
||||
"^TestReviewRejectionPersistsDeletedAuthorRoutingIntent$"
|
||||
],
|
||||
"./internal/reworkrepair": ["^Test"]
|
||||
},
|
||||
"task": [
|
||||
{
|
||||
"argv": ["go", "test", "./internal/store", "-run", "^TestPlatform", "-count=1", "-v"],
|
||||
"env": {
|
||||
"POOL_LOCAL_MONGO_URI": "mongodb://127.0.0.1:27018"
|
||||
},
|
||||
"tests": true
|
||||
},
|
||||
{
|
||||
"argv": ["go", "test", "./cmd/rework-handoff-plan"]
|
||||
},
|
||||
{
|
||||
"argv": [
|
||||
"go",
|
||||
"test",
|
||||
"./internal/store",
|
||||
"-run",
|
||||
"^(TestReviewPatchLocalMongo|TestReviewContinuationMongoStandaloneAdmission)$",
|
||||
"-count=1",
|
||||
"-v"
|
||||
],
|
||||
"env": {
|
||||
"REVIEW_LOCAL_MONGO_TEST": "1"
|
||||
},
|
||||
"tests": true
|
||||
},
|
||||
{
|
||||
"argv": ["go", "test", "./internal/reworkrepair", "-count=1", "-race", "-v"],
|
||||
"env": {
|
||||
"POOL_LOCAL_MONGO_URI": "mongodb://127.0.0.1:27018"
|
||||
},
|
||||
"tests": true
|
||||
}
|
||||
],
|
||||
"warnings": ["索引建设、历史返工元数据补处理和开关启用均须单独批准;本地性能对照不代表生产容量通过。"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user