acf053faf3
背景:发布管理把"逐条核验"的仪式强加给每一份文档。实测一份文档要 9~11 次交互,
且有两处顺序陷阱(冻结后批准 → 建批 422;生成即固化 → 批准被永久钉住)。
P0-1 建批时冻结:
- CreateReleaseBatch 在未指定版本时,自己把当前实时计划冻结成一份**已确认**版本并绑定
(同一事务;同摘要复用最近版本,planned 或 confirmed 都可复用,因此同一份计划不堆版本)。
- 语义不变:批一旦建立,清单/摘要/验收轮次/目标库绑定全部不可变、回滚按它走;变的只是
"谁在什么时候按固化键"——从操作员的手动两步变成建批这一下。
- 效果:去掉「生成新版本」「确认冻结」两步;消灭"批准晚于冻结 → 422"这类顺序陷阱;
前端不再传版本 id,也不再有"所选知识不在冻结清单里"的阻断。
- 计划→清单项的映射抽成 ChangeSetItemsFromPlan 供两处共用,否则两处算出的摘要不同,
"同一计划复用同一版本"永远不命中。
P0-2 验收通过自动批准:
- 验收三项结论达发布门槛(knowledge_exists/chunks_present=passed,retrieval_hit 与门槛同
口径)时,由 worker 写入 reviewer_ref='system:auto' 的批准记录,替代"每份文档点一次批准"。
- 策略 TEST_AUTO_REVIEW_POLICY = off | internal(默认,仅对内)| all;值不认识拒绝启动。
只影响审核这一步:生产授权、双闸与发布前快照门禁不受影响。界面上只读展示当前策略。
- 安全边界(写死在审核 SQL 里):系统只允许写"第一条"审核——人工一旦表过态,机器不得用一条
更晚的 approved 把人工结论翻回去;人工随时可再审。集成测试去掉该约束即 FAIL(已实测)。
- 自动批准自身的任何失败都不影响验收结论,只上报。
顺带(P1-1 前半,已被 P0-1 取代的部分已删除):
- 写操作失败把服务端 {"error": ...} 的原因带出来,并对已知原因给出下一步(不再只给 HTTP 状态码)。
- 当前的"不在已冻结清单里"标注与就地修复按钮随 P0-1 一起移除(该失败模式已不存在)。
测试:bind_time_freeze_integration_test.go(绑定已确认版本/同计划复用/冻结后不可变/
动作不符仍 NotEligible)、auto_review_integration_test.go(系统只写第一条、人工可再审)、
validate_test.go(对内自动/对外不自动/策略关闭/验收未过不批/自身失败不影响验收)、
auto_review_test.go、config_test 及前端能力解析测试。
实测耗时:本机 Go 全量 26 包通过、前端 178 条通过。
260 lines
13 KiB
Go
260 lines
13 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"knowledge-governance/internal/governance"
|
|
)
|
|
|
|
var (
|
|
ErrInvalidReviewFilter = errors.New("invalid review candidate filter")
|
|
ErrInvalidPublicationReview = errors.New("invalid publication review")
|
|
ErrPublicationReviewNotEligible = errors.New("publication review is not eligible")
|
|
)
|
|
|
|
const reviewCandidatesCTE = `
|
|
WITH acceptance_run_summary AS (
|
|
SELECT publication_id, run_id, MAX(observed_at) AS observed_at,
|
|
COALESCE(MAX(state) FILTER (WHERE check_type = 'knowledge_exists'), '') AS knowledge_exists,
|
|
COALESCE(MAX(state) FILTER (WHERE check_type = 'chunks_present'), '') AS chunks_present,
|
|
COALESCE(MAX(state) FILTER (WHERE check_type = 'retrieval_hit'), '') AS retrieval_hit
|
|
FROM acceptance_results
|
|
GROUP BY publication_id, run_id
|
|
), latest_acceptance AS (
|
|
SELECT DISTINCT ON (publication_id)
|
|
publication_id, run_id, knowledge_exists, chunks_present, retrieval_hit, observed_at
|
|
FROM acceptance_run_summary
|
|
ORDER BY publication_id, observed_at DESC, run_id DESC
|
|
), candidates AS (
|
|
SELECT p.id AS publication_id, i.title, connection.source_type,
|
|
COALESCE(item_scope.visibility, directory_scope.visibility, i.visibility) AS current_visibility,
|
|
acceptance.run_id AS acceptance_run_id,
|
|
acceptance.knowledge_exists, acceptance.chunks_present, acceptance.retrieval_hit,
|
|
CASE
|
|
WHEN p.ingest_state <> 'uploaded' OR p.processing_state <> 'completed' OR p.retrieval_state <> 'unpublished' THEN 'blocked'
|
|
WHEN version.revision <> i.revision OR i.source_change IN ('deleted', 'unavailable') THEN 'blocked'
|
|
WHEN acceptance.knowledge_exists = 'failed' OR acceptance.chunks_present = 'failed' OR acceptance.retrieval_hit = 'failed' THEN 'blocked'
|
|
WHEN acceptance.knowledge_exists <> 'passed' OR acceptance.chunks_present <> 'passed'
|
|
OR acceptance.retrieval_hit NOT IN ('passed', 'manual_required') THEN 'blocked'
|
|
ELSE 'ready'
|
|
END AS gate_status,
|
|
CASE
|
|
WHEN p.ingest_state <> 'uploaded' OR p.processing_state <> 'completed' OR p.retrieval_state <> 'unpublished' THEN 'publication_not_ready'
|
|
WHEN version.revision <> i.revision OR i.source_change IN ('deleted', 'unavailable') THEN 'source_not_current'
|
|
WHEN acceptance.knowledge_exists = 'failed' OR acceptance.chunks_present = 'failed' OR acceptance.retrieval_hit = 'failed' THEN 'acceptance_failed'
|
|
WHEN acceptance.knowledge_exists <> 'passed' OR acceptance.chunks_present <> 'passed'
|
|
OR acceptance.retrieval_hit NOT IN ('passed', 'manual_required') THEN 'acceptance_incomplete'
|
|
ELSE 'ready'
|
|
END AS gate_code,
|
|
CASE
|
|
WHEN review.id IS NOT NULL AND review.acceptance_run_id = acceptance.run_id
|
|
AND review.publication_updated_at = p.updated_at THEN review.decision
|
|
ELSE 'pending'
|
|
END AS review_state,
|
|
CASE WHEN review.id IS NOT NULL AND review.acceptance_run_id = acceptance.run_id
|
|
AND review.publication_updated_at = p.updated_at THEN review.decided_visibility ELSE '' END AS decided_visibility,
|
|
CASE WHEN review.id IS NOT NULL AND review.acceptance_run_id = acceptance.run_id
|
|
AND review.publication_updated_at = p.updated_at THEN review.decision_code ELSE '' END AS decision_code,
|
|
CASE WHEN review.id IS NOT NULL AND review.acceptance_run_id = acceptance.run_id
|
|
AND review.publication_updated_at = p.updated_at THEN review.reviewer_ref ELSE '' END AS reviewer_ref,
|
|
CASE WHEN review.id IS NOT NULL AND review.acceptance_run_id = acceptance.run_id
|
|
AND review.publication_updated_at = p.updated_at THEN review.created_at ELSE NULL END AS reviewed_at,
|
|
p.updated_at
|
|
FROM test_publications AS p
|
|
JOIN source_item_versions AS version ON version.id = p.source_item_version_id
|
|
JOIN source_items AS i ON i.id = version.source_item_id
|
|
JOIN source_connections AS connection ON connection.id = i.source_connection_id
|
|
LEFT JOIN source_item_scope_overrides AS item_scope ON item_scope.source_item_id = i.id
|
|
LEFT JOIN LATERAL (
|
|
SELECT rule.visibility
|
|
FROM directory_scope_rules AS rule
|
|
WHERE rule.source_connection_id = i.source_connection_id
|
|
AND (
|
|
rule.directory_path = ''
|
|
OR i.normalized_path = rule.directory_path
|
|
OR LEFT(i.normalized_path, LENGTH(rule.directory_path) + 1) = rule.directory_path || '/'
|
|
)
|
|
ORDER BY LENGTH(rule.directory_path) DESC
|
|
LIMIT 1
|
|
) AS directory_scope ON TRUE
|
|
JOIN latest_acceptance AS acceptance ON acceptance.publication_id = p.id
|
|
LEFT JOIN LATERAL (
|
|
SELECT id, decision, decided_visibility, decision_code, reviewer_ref,
|
|
acceptance_run_id, publication_updated_at, created_at
|
|
FROM publication_reviews
|
|
WHERE publication_id = p.id
|
|
ORDER BY created_at DESC, id DESC
|
|
LIMIT 1
|
|
) AS review ON TRUE
|
|
)
|
|
`
|
|
|
|
const countReviewCandidatesSQL = reviewCandidatesCTE + `
|
|
SELECT COUNT(*)::INTEGER
|
|
FROM candidates
|
|
WHERE $1 = '' OR review_state = $1
|
|
`
|
|
|
|
const listReviewCandidatesSQL = reviewCandidatesCTE + `
|
|
SELECT publication_id, title, source_type, current_visibility,
|
|
acceptance_run_id, knowledge_exists, chunks_present, retrieval_hit,
|
|
gate_status, gate_code, review_state, decided_visibility,
|
|
decision_code, reviewer_ref, reviewed_at, updated_at
|
|
FROM candidates
|
|
WHERE $1 = '' OR review_state = $1
|
|
ORDER BY updated_at DESC, publication_id
|
|
LIMIT $2 OFFSET $3
|
|
`
|
|
|
|
const reviewPublicationSQL = reviewCandidatesCTE + `
|
|
INSERT INTO publication_reviews (
|
|
id, publication_id, decision, decided_visibility, reviewer_ref,
|
|
decision_code, acceptance_run_id, publication_updated_at, created_at
|
|
)
|
|
SELECT $1::varchar, publication_id, $3::varchar, $4::varchar, $5::varchar, $6::varchar,
|
|
acceptance_run_id, updated_at, $7::timestamptz
|
|
FROM candidates
|
|
WHERE publication_id = $2::varchar
|
|
AND ($3::varchar = 'rejected' OR gate_status = 'ready')
|
|
-- 系统自动批准(reviewer_ref='system:auto')只允许写"第一条"审核:人工一旦表过态,
|
|
-- 机器不得再用一条更晚的 approved 把人工结论静默翻回去(重跑验收时尤其危险)。
|
|
-- 人工审核不受此限,随时可再审。
|
|
AND ($5::varchar <> 'system:auto' OR NOT EXISTS (
|
|
SELECT 1 FROM publication_reviews existing
|
|
WHERE existing.publication_id = candidates.publication_id))
|
|
ON CONFLICT (
|
|
publication_id, acceptance_run_id, publication_updated_at,
|
|
reviewer_ref, decision, decided_visibility, decision_code
|
|
) DO UPDATE SET id = publication_reviews.id
|
|
RETURNING id, publication_id, decision, decided_visibility, reviewer_ref,
|
|
decision_code, acceptance_run_id, publication_updated_at, created_at
|
|
`
|
|
|
|
func (s *Store) ListReviewCandidates(ctx context.Context, filter governance.ReviewCandidateFilter) (governance.ReviewCandidatePage, error) {
|
|
if filter.Limit < 1 || filter.Limit > 100 || filter.Offset < 0 || filter.Offset > 1000000 ||
|
|
filter.ReviewState != "" && filter.ReviewState != string(governance.ReviewPending) &&
|
|
filter.ReviewState != string(governance.ReviewApproved) && filter.ReviewState != string(governance.ReviewRejected) {
|
|
return governance.ReviewCandidatePage{}, ErrInvalidReviewFilter
|
|
}
|
|
page := governance.ReviewCandidatePage{Items: []governance.ReviewCandidate{}, Limit: filter.Limit, Offset: filter.Offset}
|
|
if err := s.db.QueryRow(ctx, countReviewCandidatesSQL, filter.ReviewState).Scan(&page.Total); err != nil {
|
|
return governance.ReviewCandidatePage{}, fmt.Errorf("count review candidates: %w", err)
|
|
}
|
|
rows, err := s.db.Query(ctx, listReviewCandidatesSQL, filter.ReviewState, filter.Limit, filter.Offset)
|
|
if err != nil {
|
|
return governance.ReviewCandidatePage{}, fmt.Errorf("list review candidates: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var item governance.ReviewCandidate
|
|
var sourceType, visibility, knowledgeState, chunksState, retrievalState, gateStatus, reviewState string
|
|
if err := rows.Scan(
|
|
&item.PublicationID, &item.Title, &sourceType, &visibility,
|
|
&item.AcceptanceRunID, &knowledgeState, &chunksState, &retrievalState,
|
|
&gateStatus, &item.GateCode, &reviewState, &item.DecidedVisibility,
|
|
&item.DecisionCode, &item.ReviewerRef, &item.ReviewedAt, &item.PublicationUpdated,
|
|
); err != nil {
|
|
return governance.ReviewCandidatePage{}, fmt.Errorf("scan review candidate: %w", err)
|
|
}
|
|
item.SourceType = governance.SourceType(sourceType)
|
|
item.CurrentVisibility = governance.Visibility(visibility)
|
|
item.KnowledgeExists = governance.AcceptanceState(knowledgeState)
|
|
item.ChunksPresent = governance.AcceptanceState(chunksState)
|
|
item.RetrievalHit = governance.AcceptanceState(retrievalState)
|
|
item.GateStatus = governance.ReviewGateStatus(gateStatus)
|
|
item.ReviewState = governance.ReviewState(reviewState)
|
|
if !item.SourceType.Valid() || !item.GateStatus.Valid() || !item.ReviewState.Valid() ||
|
|
!item.KnowledgeExists.Valid() || !item.ChunksPresent.Valid() || !item.RetrievalHit.Valid() ||
|
|
item.CurrentVisibility != governance.VisibilityInternal && item.CurrentVisibility != governance.VisibilityExternal {
|
|
return governance.ReviewCandidatePage{}, errors.New("invalid review candidate row")
|
|
}
|
|
page.Items = append(page.Items, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return governance.ReviewCandidatePage{}, fmt.Errorf("iterate review candidates: %w", err)
|
|
}
|
|
return page, nil
|
|
}
|
|
|
|
func (s *Store) ReviewPublicationWithOperation(ctx context.Context, request governance.ReviewPublicationRequest, operationID string) (governance.PublicationReview, bool, error) {
|
|
fingerprint, err := operationFingerprint(struct {
|
|
PublicationID string `json:"publication_id"`
|
|
Decision governance.ReviewState `json:"decision"`
|
|
Visibility governance.Visibility `json:"decided_visibility"`
|
|
ReviewerRef string `json:"reviewer_ref"`
|
|
Code string `json:"decision_code"`
|
|
}{request.PublicationID, request.Decision, request.DecidedVisibility, request.ReviewerRef, request.DecisionCode})
|
|
if err != nil {
|
|
return governance.PublicationReview{}, false, err
|
|
}
|
|
raw, replayed, err := s.executeOperation(ctx, "publication.review", operationID, fingerprint, request.Now, func(tx pgx.Tx) (json.RawMessage, error) {
|
|
review, err := NewStore(tx).ReviewPublication(ctx, request)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(review)
|
|
})
|
|
if err != nil {
|
|
return governance.PublicationReview{}, false, err
|
|
}
|
|
var review governance.PublicationReview
|
|
if err := json.Unmarshal(raw, &review); err != nil {
|
|
return governance.PublicationReview{}, false, fmt.Errorf("decode stored review result: %w", err)
|
|
}
|
|
return review, replayed, nil
|
|
}
|
|
|
|
func (s *Store) ReviewPublication(ctx context.Context, request governance.ReviewPublicationRequest) (governance.PublicationReview, error) {
|
|
if !validPublicationReview(request) {
|
|
return governance.PublicationReview{}, ErrInvalidPublicationReview
|
|
}
|
|
reviewID, err := newOpaqueID("rv-")
|
|
if err != nil {
|
|
return governance.PublicationReview{}, err
|
|
}
|
|
var review governance.PublicationReview
|
|
var decision, visibility string
|
|
err = s.db.QueryRow(ctx, reviewPublicationSQL,
|
|
reviewID, request.PublicationID, string(request.Decision), string(request.DecidedVisibility),
|
|
request.ReviewerRef, request.DecisionCode, request.Now,
|
|
).Scan(
|
|
&review.ID, &review.PublicationID, &decision, &visibility, &review.ReviewerRef,
|
|
&review.DecisionCode, &review.AcceptanceRunID, &review.PublicationUpdated, &review.CreatedAt,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return governance.PublicationReview{}, ErrPublicationReviewNotEligible
|
|
}
|
|
if err != nil {
|
|
return governance.PublicationReview{}, fmt.Errorf("review publication: %w", err)
|
|
}
|
|
review.Decision = governance.ReviewState(decision)
|
|
review.DecidedVisibility = governance.Visibility(visibility)
|
|
return review, nil
|
|
}
|
|
|
|
func validPublicationReview(request governance.ReviewPublicationRequest) bool {
|
|
if !validShortID(request.PublicationID) || request.Now.IsZero() ||
|
|
request.DecidedVisibility != governance.VisibilityInternal && request.DecidedVisibility != governance.VisibilityExternal ||
|
|
!validReviewerRef(request.ReviewerRef) {
|
|
return false
|
|
}
|
|
switch request.Decision {
|
|
case governance.ReviewApproved:
|
|
return request.DecisionCode == "approved_for_release"
|
|
case governance.ReviewRejected:
|
|
return request.DecisionCode == "needs_correction" || request.DecisionCode == "classification_changed" || request.DecisionCode == "acceptance_rejected"
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
func validReviewerRef(value string) bool {
|
|
return value == strings.TrimSpace(value) && value != "" && len(value) <= 128 && !strings.ContainsAny(value, "\x00\r\n ")
|
|
}
|