feat(monitor): 卡片概况与统一文件处理表
build / build (push) Successful in 1m17s

This commit is contained in:
姚章浩
2026-10-27 12:14:19 +08:00
parent d8d143eca0
commit 1b09f7d35b
13 changed files with 2238 additions and 559 deletions
+102
View File
@@ -0,0 +1,102 @@
package httpapi
import (
"context"
"errors"
postgresstore "knowledge-governance/internal/platform/postgres"
"net/http"
"strconv"
"strings"
"time"
)
type monitorRemovalRetrier interface {
RetryMonitorRemoval(context.Context, string, string, time.Time) error
}
func retryMonitorRemoval(w http.ResponseWriter, r *http.Request, config Config) {
identity, ok := requireSessionIdentity(w, r)
if !ok {
return
}
if identity.Role != roleEngineer && identity.Role != roleAdmin {
writeJSON(w, 403, map[string]string{"error": "forbidden"})
return
}
if r.Header.Get("X-Governance-Action") != "monitor-removal-retry" {
writeJSON(w, 403, map[string]string{"error": "write intent required"})
return
}
id := r.PathValue("id")
if id == "" || len(id) > 128 {
writeJSON(w, 400, map[string]string{"error": "invalid publication"})
return
}
store, ok := config.Store.(monitorRemovalRetrier)
if !ok {
writeJSON(w, 501, map[string]string{"error": "removal retry not configured"})
return
}
err := store.RetryMonitorRemoval(r.Context(), id, actorFor(r), time.Now().UTC())
if errors.Is(err, postgresstore.ErrPublicationStateConflict) {
writeJSON(w, 409, map[string]string{"error": "状态或治理规则已变化,不能重复提交"})
return
}
if err != nil {
writeJSON(w, 500, map[string]string{"error": "测试库移除重试失败"})
return
}
writeJSON(w, 202, map[string]string{"status": "accepted"})
}
type monitorTaskReader interface {
ListMonitorTasks(context.Context, postgresstore.MonitorTaskFilter) (postgresstore.MonitorTaskPage, error)
}
func listMonitorTasks(w http.ResponseWriter, r *http.Request, config Config) {
if _, ok := requireSessionIdentity(w, r); !ok {
return
}
q := r.URL.Query()
f := postgresstore.MonitorTaskFilter{Search: q.Get("q"), Source: q.Get("source"), Scope: q.Get("scope"), Action: q.Get("action"), Status: q.Get("status"), Stage: q.Get("stage"), ID: q.Get("id"), Target: q.Get("target"), Since: q.Get("since"), Until: q.Get("until"), Limit: 20}
for k, p := range map[string]*int{"limit": &f.Limit, "offset": &f.Offset} {
if q.Get(k) != "" {
n, e := strconv.Atoi(q.Get(k))
if e != nil {
writeJSON(w, 400, map[string]string{"error": "invalid pagination"})
return
}
*p = n
}
}
if f.Limit < 1 || f.Limit > 100 || f.Offset < 0 || len(f.Search) > 512 || len(f.Source) > 128 || len(f.ID) > 128 {
writeJSON(w, 400, map[string]string{"error": "invalid filter"})
return
}
for _, pair := range [][2]string{{f.Scope, "|internal|external"}, {f.Action, "|add|update|remove"}, {f.Status, "|active|waiting|running|retrying|attention|completed|cancelled"}, {f.Stage, "|waiting|fetch|upload|parsing|indexing|removal|finished"}} {
if !strings.Contains("|"+pair[1]+"|", "|"+pair[0]+"|") {
writeJSON(w, 400, map[string]string{"error": "invalid filter"})
return
}
}
for _, v := range []string{f.Since, f.Until} {
if v != "" {
if _, e := time.Parse(time.RFC3339, v); e != nil {
writeJSON(w, 400, map[string]string{"error": "invalid date"})
return
}
}
}
store, ok := config.Store.(monitorTaskReader)
if !ok {
writeJSON(w, 501, map[string]string{"error": "monitor tasks not configured"})
return
}
page, e := store.ListMonitorTasks(r.Context(), f)
if e != nil {
writeJSON(w, 500, map[string]string{"error": "无法读取文件处理状态"})
return
}
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, 200, map[string]any{"data": page})
}
+53
View File
@@ -0,0 +1,53 @@
package httpapi
import (
"context"
postgresstore "knowledge-governance/internal/platform/postgres"
"net/http/httptest"
"testing"
)
type monitorTasksTestStore struct {
fakeJobStore
filter postgresstore.MonitorTaskFilter
}
func (s *monitorTasksTestStore) ListMonitorTasks(_ context.Context, f postgresstore.MonitorTaskFilter) (postgresstore.MonitorTaskPage, error) {
s.filter = f
return postgresstore.MonitorTaskPage{Items: []postgresstore.MonitorTask{}, Total: 145, Limit: f.Limit, Offset: f.Offset}, nil
}
func TestMonitorTasksRoute(t *testing.T) {
s := &monitorTasksTestStore{}
req := httptest.NewRequest("GET", "/api/v1/monitor/tasks?status=active&limit=20&offset=40", nil)
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleViewer}))
w := httptest.NewRecorder()
h, err := NewHandler(Config{Store: s})
if err != nil {
t.Fatal(err)
}
h.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
if s.filter.Status != "active" || s.filter.Offset != 40 {
t.Fatalf("filter %#v", s.filter)
}
for _, query := range []string{"status=garbage", "stage=internal&scope=internal", "limit=101", "offset=-1", "since=bad"} {
r := httptest.NewRequest("GET", "/api/v1/monitor/tasks?"+query, nil).WithContext(req.Context())
rr := httptest.NewRecorder()
h.ServeHTTP(rr, r)
if rr.Code != 400 {
t.Fatalf("invalid %s: %d", query, rr.Code)
}
}
unauth := httptest.NewRecorder()
h.ServeHTTP(unauth, httptest.NewRequest("GET", "/api/v1/monitor/tasks", nil))
if unauth.Code != 401 {
t.Fatalf("unauthenticated %d", unauth.Code)
}
removal := httptest.NewRecorder()
h.ServeHTTP(removal, httptest.NewRequest("POST", "/api/v1/test-publications/example/removal-retry", nil).WithContext(req.Context()))
if removal.Code != 403 {
t.Fatalf("viewer removal %d", removal.Code)
}
}
+6
View File
@@ -398,6 +398,9 @@ func NewHandler(config Config) (http.Handler, error) {
mux.HandleFunc("GET /api/v1/monitor/knowledge-bases/{id}/files", func(w http.ResponseWriter, r *http.Request) {
monitorFiles(w, r, config.Monitor)
})
mux.HandleFunc("GET /api/v1/monitor/tasks", func(w http.ResponseWriter, r *http.Request) {
listMonitorTasks(w, r, config)
})
mux.HandleFunc("GET /api/v1/monitor/metrics", func(w http.ResponseWriter, r *http.Request) {
monitorMetrics(w, r, config.Monitor)
})
@@ -557,6 +560,9 @@ func NewHandler(config Config) (http.Handler, error) {
})
// 需下架条目的移除入口:把「被禁止导入但仍存活在测试库」的知识从测试库移除
// (置 disabling + 入队既有 test.remove 工作流)。与开始处理同级:engineer 即可。
mux.HandleFunc("POST /api/v1/test-publications/{id}/removal-retry", func(w http.ResponseWriter, r *http.Request) {
retryMonitorRemoval(w, r, config)
})
mux.HandleFunc("POST /api/v1/test-publications/{id}/removal", func(w http.ResponseWriter, r *http.Request) {
startTestPublicationRemoval(w, r, config)
})
@@ -0,0 +1,37 @@
package postgres
import (
"context"
"time"
)
func (s *Store) RetryMonitorRemoval(ctx context.Context, id, actor string, now time.Time) error {
var count int
err := s.db.QueryRow(ctx, `WITH candidate AS (
SELECT p.id,j.id AS job_id FROM test_publications p
JOIN source_item_versions v ON v.id=p.source_item_version_id
JOIN source_items si ON si.id=v.source_item_id
JOIN jobs j ON j.payload->>'publication_id'=p.id AND j.type='test.remove' AND j.state='failed'
WHERE p.id=$1 AND p.superseded_at IS NULL AND p.knowledge_id<>'' AND p.retrieval_state IN ('failed','disabling') AND si.policy_state='blocked'
AND NOT EXISTS(SELECT 1 FROM jobs live WHERE live.payload->>'publication_id'=p.id AND live.state IN ('pending','running','retry'))
ORDER BY j.updated_at DESC,j.id LIMIT 1 FOR UPDATE OF p,si,j
), retried AS (
UPDATE jobs SET state='pending',attempt=0,available_at=$2,lease_owner='',lease_until=NULL,last_error='',last_error_code='',updated_at=$2
FROM candidate c WHERE jobs.id=c.job_id AND jobs.state='failed' RETURNING c.id,jobs.id AS job_id
), changed AS (
UPDATE test_publications SET retrieval_state='disabling',last_error='',retry_count=retry_count+1,updated_at=$2
FROM retried r WHERE test_publications.id=r.id RETURNING test_publications.id,r.job_id
), closed AS (
UPDATE job_failures SET state='requeued',resolved_at=$2,resolution_actor_ref=$4,resolution_reason='测试库移除重试' FROM changed c WHERE job_failures.job_id=c.job_id AND job_failures.state='open'
), audited AS (
INSERT INTO job_retry_operations(id,batch_key,publication_id,actor_ref,reason,outcome,created_at)
SELECT $3,$3,c.id,$4,'测试库移除重试','started',$2 FROM changed c
) SELECT count(*) FROM changed`, id, now, orchestrationJobID("monitor-removal-retry", id, now.String()), actor).Scan(&count)
if err != nil {
return err
}
if count != 1 {
return ErrPublicationStateConflict
}
return nil
}
+140
View File
@@ -0,0 +1,140 @@
package postgres
import (
"context"
"encoding/json"
"errors"
)
type MonitorTaskFilter struct {
Search, Source, Scope, Action, Status, Stage, ID, Target, Since, Until string
Limit, Offset int
}
type MonitorTaskEvent struct {
At string `json:"at"`
Kind string `json:"kind"`
Detail string `json:"detail"`
}
type MonitorTask struct {
History []MonitorTaskEvent `json:"history"`
ID string `json:"id"`
Title string `json:"title"`
Path string `json:"path"`
SourceID string `json:"source_id"`
SourceName string `json:"source_name"`
TargetScope string `json:"target_scope"`
TargetKB string `json:"target_kb"`
Action string `json:"action"`
Stage string `json:"stage"`
Status string `json:"status"`
Detail string `json:"detail"`
UpdatedAt string `json:"updated_at"`
RetryKind string `json:"retry_kind"`
FailureID string `json:"failure_id"`
RetryCount int `json:"retry_count"`
VersionID string `json:"version_id"`
}
type MonitorTaskPage struct {
Items []MonitorTask `json:"items"`
Total int `json:"total"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Sources []MonitorOption `json:"sources"`
Targets []MonitorOption `json:"targets"`
Retryable int `json:"retryable"`
}
type MonitorOption struct {
ID string `json:"id"`
Name string `json:"name"`
}
// Reduce publication versions before filtering; totals and pages share one snapshot.
const monitorTasksCTE = `WITH history_publications AS (
SELECT hp.* FROM test_publications hp JOIN source_item_versions hv ON hv.id=hp.source_item_version_id
WHERE $7<>'' AND (hv.source_item_id,hp.test_kb_id)=(SELECT v.source_item_id,p.test_kb_id FROM test_publications p JOIN source_item_versions v ON v.id=p.source_item_version_id WHERE p.id=$7)
), history_events AS (
SELECT updated_at AS at,'版本状态' AS kind,source_item_version_id || ' · ' || processing_state || ' / ' || retrieval_state AS detail FROM history_publications
UNION ALL SELECT f.created_at,'失败记录',f.job_type || ' · ' || f.error_code || ' · 尝试 ' || f.attempt FROM job_failures f WHERE f.publication_id IN(SELECT id FROM history_publications) OR (f.job_type='artifact.fetch' AND f.business_version_id IN(SELECT source_item_version_id FROM history_publications))
UNION ALL SELECT r.created_at,'重试记录',r.reason || ' · ' || r.outcome FROM job_retry_operations r WHERE r.publication_id IN(SELECT id FROM history_publications)
), latest AS (
SELECT DISTINCT ON(v.source_item_id,p.test_kb_id) p.*,v.source_item_id,v.content_object_id
FROM test_publications p JOIN source_item_versions v ON v.id=p.source_item_version_id
WHERE p.superseded_at IS NULL
ORDER BY v.source_item_id,p.test_kb_id,p.created_at DESC,p.id DESC
), base AS (
SELECT p.id,si.title,si.normalized_path AS path,c.id AS source_id,c.name AS source_name,
p.target_scope,p.test_kb_id AS target_kb,p.source_item_version_id AS version_id,p.retry_count,
GREATEST(p.updated_at,j.updated_at,f.created_at) AS updated_at,
p.processing_state,p.ingest_state,p.retrieval_state,p.last_error,p.knowledge_id,
co.content_state,co.local_spool_ref,si.policy_state,
CASE WHEN p.retrieval_state IN ('disabling','disabled') OR (p.knowledge_id<>'' AND si.policy_state='blocked') OR j.type='test.remove' THEN 'remove'
WHEN EXISTS(SELECT 1 FROM test_publications prev JOIN source_item_versions pv ON pv.id=prev.source_item_version_id WHERE pv.source_item_id=si.id AND prev.test_kb_id=p.test_kb_id AND prev.id<>p.id AND prev.created_at<p.created_at) THEN 'update' ELSE 'add' END AS action,
COALESCE(j.type,'') AS job_type,COALESCE(j.state,'') AS job_state,COALESCE(j.attempt,0) AS job_attempt,j.available_at,
COALESCE(f.id,'') AS failure_id,COALESCE(f.job_type,'') AS failure_type,COALESCE(f.error_code,'') AS failure_error
FROM latest p JOIN source_items si ON si.id=p.source_item_id JOIN source_connections c ON c.id=si.source_connection_id
LEFT JOIN content_objects co ON co.id=p.content_object_id
LEFT JOIN LATERAL(SELECT j.type,j.state,j.attempt,j.available_at,j.updated_at FROM jobs j WHERE
(j.payload->>'publication_id'=p.id OR (j.type='artifact.fetch' AND j.payload->>'source_item_version_id'=p.source_item_version_id))
AND j.type IN ('artifact.fetch','test.upload','test.poll','test.remove')
ORDER BY CASE WHEN j.state IN ('pending','running','retry') THEN 0 ELSE 1 END,j.updated_at DESC,j.id DESC LIMIT 1) j ON TRUE
LEFT JOIN LATERAL(SELECT f.id,f.job_type,f.error_code,f.created_at FROM job_failures f WHERE f.state='open' AND (f.publication_id=p.id OR (f.job_type='artifact.fetch' AND f.business_version_id=p.source_item_version_id)) ORDER BY f.created_at DESC,f.id DESC LIMIT 1) f ON TRUE
), classified AS (
SELECT *,
CASE WHEN action='remove' AND retrieval_state='disabled' THEN 'completed'
WHEN action<>'remove' AND processing_state='completed' THEN 'completed'
WHEN job_state IN ('pending','retry') AND job_attempt>0 THEN 'retrying'
WHEN job_state='running' THEN 'running'
WHEN job_state IN ('pending','retry') THEN 'waiting'
WHEN failure_id<>'' OR processing_state IN ('failed','stalled') OR ingest_state='failed' OR retrieval_state='failed' THEN 'attention'
WHEN processing_state='cancelled' THEN 'cancelled'
WHEN action='remove' THEN 'waiting'
WHEN processing_state IN ('processing','finalizing') THEN 'running'
ELSE 'waiting' END AS status,
CASE WHEN action='remove' AND retrieval_state='disabled' THEN 'finished'
WHEN action='remove' THEN 'removal'
WHEN processing_state IN ('completed','cancelled') THEN 'finished'
WHEN job_type='artifact.fetch' AND job_state IN ('pending','running','retry') THEN 'fetch'
WHEN processing_state='finalizing' THEN 'indexing'
WHEN processing_state='processing' OR job_type='test.poll' THEN 'parsing'
WHEN ingest_state='uploading' OR job_type='test.upload' THEN 'upload'
WHEN failure_type='artifact.fetch' OR COALESCE(content_state,'')<>'verified' THEN 'fetch'
ELSE 'waiting' END AS stage
FROM base
), projected AS (
SELECT id,title,path,source_id,source_name,target_scope,target_kb,version_id,retry_count,updated_at,action,stage,status,failure_id,
CASE WHEN status<>'attention' THEN ''
WHEN action='remove' THEN CASE WHEN job_type='test.remove' AND job_state='failed' AND retrieval_state IN ('failed','disabling') AND policy_state='blocked' THEN 'remove' ELSE '' END
WHEN failure_type='artifact.fetch' THEN 'fetch'
WHEN content_state='verified' AND local_spool_ref<>'' AND processing_state IN ('failed','stalled','pending') THEN 'upload' ELSE '' END AS retry_kind,
CASE WHEN status='completed' THEN CASE WHEN action='remove' THEN '已从测试库移除;来源文件保留' ELSE '测试库处理完成;未代表已发布生产' END
WHEN status='cancelled' THEN '处理已取消,记录保留'
WHEN status='retrying' THEN '等待自动重试;下次调度 ' || COALESCE(to_char(available_at AT TIME ZONE 'Asia/Shanghai','YYYY-MM-DD HH24:MI:SS'),'未提供')
WHEN status='attention' THEN COALESCE(NULLIF(last_error,''),NULLIF(failure_error,''),'处理异常,请查看详情')
WHEN stage='removal' THEN CASE WHEN status='running' THEN '正在移除测试库知识' ELSE '等待测试库移除任务' END
WHEN stage='fetch' THEN '等待或正在获取源文件'
WHEN stage='waiting' THEN '文件已就绪,等待后台处理调度'
WHEN stage='upload' THEN '等待或正在上传测试知识库'
WHEN stage='indexing' THEN '正在索引与增强'
ELSE '正在解析处理' END AS detail
FROM classified
), filtered AS (
SELECT * FROM projected WHERE ($1='' OR strpos(lower(title || ' ' || path),lower($1))>0)
AND ($2='' OR source_id=$2) AND ($3='' OR target_scope=$3) AND ($4='' OR action=$4)
AND ($5='' OR ($5='active' AND status NOT IN ('completed','cancelled')) OR status=$5)
AND ($6='' OR stage=$6) AND ($7='' OR id=$7) AND ($10='' OR target_kb=$10)
AND ($11='' OR updated_at>=NULLIF($11,'')::timestamptz) AND ($12='' OR updated_at<=NULLIF($12,'')::timestamptz)
) `
func (s *Store) ListMonitorTasks(ctx context.Context, f MonitorTaskFilter) (MonitorTaskPage, error) {
if f.Limit < 1 || f.Limit > 100 || f.Offset < 0 {
return MonitorTaskPage{}, errors.New("invalid monitor pagination")
}
var raw []byte
err := s.db.QueryRow(ctx, monitorTasksCTE+`SELECT jsonb_build_object('items',COALESCE((SELECT jsonb_agg(to_jsonb(x)||jsonb_build_object('history',COALESCE((SELECT jsonb_agg(e ORDER BY e.at DESC,e.kind,e.detail) FROM (SELECT * FROM history_events ORDER BY at DESC,kind,detail LIMIT 50)e),'[]'::jsonb)) ORDER BY x.updated_at DESC,x.id) FROM (SELECT * FROM filtered ORDER BY updated_at DESC,id LIMIT $8 OFFSET $9)x),'[]'::jsonb),'total',(SELECT count(*) FROM filtered),'limit',$8::int,'offset',$9::int,'retryable',(SELECT count(*) FROM filtered WHERE retry_kind<>''),'sources',COALESCE((SELECT jsonb_agg(x ORDER BY x.name) FROM (SELECT DISTINCT source_id AS id,source_name AS name FROM projected)x),'[]'::jsonb),'targets',COALESCE((SELECT jsonb_agg(x ORDER BY x.name) FROM (SELECT DISTINCT target_kb AS id,target_kb AS name FROM projected)x),'[]'::jsonb))`, f.Search, f.Source, f.Scope, f.Action, f.Status, f.Stage, f.ID, f.Limit, f.Offset, f.Target, f.Since, f.Until).Scan(&raw)
if err != nil {
return MonitorTaskPage{}, err
}
var result MonitorTaskPage
err = json.Unmarshal(raw, &result)
return result, err
}
@@ -0,0 +1,105 @@
package postgres
import (
"context"
"github.com/jackc/pgx/v5/pgxpool"
"os"
"testing"
"time"
)
func TestMonitorTasksUnifiedPaginationIntegration(t *testing.T) {
url := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if url == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, url)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err = ApplyMigrations(ctx, pool); err != nil {
t.Fatal(err)
}
exec := func(q string) {
t.Helper()
if _, e := pool.Exec(ctx, q); e != nil {
t.Fatal(e)
}
}
exec(`INSERT INTO source_connections (id,source_type,name) VALUES ('conn-manual','manual','Manual')`)
now := time.Date(2026, 10, 27, 12, 0, 0, 0, time.UTC)
insertReviewFixture(t, ctx, pool, 4, "mt-ready", now)
insertReviewFixture(t, ctx, pool, 5, "mt-remove", now)
insertReviewFixture(t, ctx, pool, 6, "mt-removed", now)
exec(`UPDATE source_items SET policy_state='blocked' WHERE id='si-review-5'`)
exec(`UPDATE test_publications SET retrieval_state='disabled' WHERE id='mt-removed'`)
store := NewStore(pool)
page, err := store.ListMonitorTasks(ctx, MonitorTaskFilter{Limit: 1})
if err != nil {
t.Fatal(err)
}
if page.Total != 3 || len(page.Items) != 1 {
t.Fatalf("global pagination: %#v", page)
}
active, err := store.ListMonitorTasks(ctx, MonitorTaskFilter{Status: "active", Limit: 100})
if err != nil {
t.Fatal(err)
}
if active.Total != 1 || active.Items[0].ID != "mt-remove" || active.Items[0].Action != "remove" || active.Items[0].Status != "waiting" {
t.Fatalf("removal is not completed: %#v", active)
}
done, err := store.ListMonitorTasks(ctx, MonitorTaskFilter{Action: "remove", Status: "completed", Limit: 100})
if err != nil {
t.Fatal(err)
}
if done.Total != 1 || done.Items[0].ID != "mt-removed" || done.Items[0].RetryKind != "" {
t.Fatalf("removed is not ready/retryable: %#v", done)
}
empty, err := store.ListMonitorTasks(ctx, MonitorTaskFilter{Limit: 1, Offset: 999})
if err != nil || empty.Total != 3 || len(empty.Items) != 0 {
t.Fatalf("empty last page lost total: %#v %v", empty, err)
}
exec(`UPDATE test_publications SET retrieval_state='failed' WHERE id='mt-remove'`)
exec(`INSERT INTO jobs(id,type,idempotency_key,payload,state) VALUES('mt-removal-job','test.remove','mt-removal-key','{"publication_id":"mt-remove"}','failed')`)
exec(`INSERT INTO job_failures(id,job_id,job_type,publication_id,error_code,attempt,max_attempts_at_failure,last_error) VALUES('mt-failure','mt-removal-job','test.remove','mt-remove','removal_failed',8,8,'removal_failed')`)
if err = store.RetryMonitorRemoval(ctx, "mt-remove", "tester", now); err != nil {
t.Fatalf("retry removal: %v", err)
}
if err = store.RetryMonitorRemoval(ctx, "mt-remove", "tester", now); err != ErrPublicationStateConflict {
t.Fatalf("duplicate retry accepted: %v", err)
}
detail, e := store.ListMonitorTasks(ctx, MonitorTaskFilter{ID: "mt-remove", Limit: 1})
if e != nil || len(detail.Items) != 1 || len(detail.Items[0].History) < 3 {
t.Fatalf("missing version/failure/retry history: %#v %v", detail, e)
}
var n int
if err = pool.QueryRow(ctx, `SELECT count(*) FROM jobs WHERE type='test.upload'`).Scan(&n); err != nil || n != 0 {
t.Fatalf("removal created upload: %d %v", n, err)
}
if err = pool.QueryRow(ctx, `SELECT count(*) FROM jobs WHERE id='mt-removal-job' AND state='pending'`).Scan(&n); err != nil || n != 1 {
t.Fatalf("not enqueued: %d %v", n, err)
}
exec(`UPDATE jobs SET state='failed' WHERE id='mt-removal-job'`)
exec(`UPDATE source_items SET policy_state='internal' WHERE id='si-review-5'`)
// A new version replaces the main row; routing that version to another KB adds one row.
exec(`INSERT INTO source_item_versions(id,source_item_id,revision,content_state,raw_record_summary) VALUES('mt-v-new','si-review-4','new-revision','verified','{}')`)
exec(`INSERT INTO test_publications(id,source_item_version_id,test_kb_id,target_scope,processing_state,created_at) SELECT 'mt-new','mt-v-new',test_kb_id,'internal','processing',created_at+interval '1 second' FROM test_publications WHERE id='mt-ready'`)
exec(`INSERT INTO test_publications(id,source_item_version_id,test_kb_id,target_scope,processing_state) VALUES('mt-external','mt-v-new','external-kb','external','planned')`)
versions, e := store.ListMonitorTasks(ctx, MonitorTaskFilter{Search: "mt-ready", Limit: 20})
if e != nil || versions.Total != 2 {
t.Fatalf("file-target identity: %#v %v", versions, e)
}
for _, v := range versions.Items {
if v.ID == "mt-ready" {
t.Fatal("old version escaped dedup")
}
if v.ID == "mt-new" && (v.Action != "update" || v.Stage != "parsing") {
t.Fatalf("latest action/stage: %#v", v)
}
}
if err = store.RetryMonitorRemoval(ctx, "mt-remove", "tester", now); err != ErrPublicationStateConflict {
t.Fatalf("changed policy accepted: %v", err)
}
}