feat(reconciliation): 实现导入对账后端
为什么:T6a 需要一张批次/差异表、全量只读扫描、R1-R8 判定和 finding 级处置;现有解析监控只转述台账状态,无法回答库内是否一致。 做什么:迁移 000084(upload_md5、runs/findings/actions、共享 removal_candidates 视图);WeKnora 字段与 1000/页扫描;runtime 只读客户端(0400 Key、503 降级);扫描/矩阵/动作/定时/僵死回收;9 条接口与 RBAC;overview 队列与删两张常量卡。 边界:只对测试库;对账零写请求;R1/R2 单条确认;R6 复用 /removal;R2 只管理员;生产链路未改。 验证:make verify 全绿(17 项);新增矩阵/扫描 R1-R8/零写/失败整批/动作/运行时/密钥权限测试。
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
DROP VIEW IF EXISTS removal_candidates;
|
||||
DROP TABLE IF EXISTS reconciliation_finding_actions;
|
||||
DROP TABLE IF EXISTS reconciliation_findings;
|
||||
DROP TABLE IF EXISTS reconciliation_runs;
|
||||
ALTER TABLE test_publications DROP COLUMN IF EXISTS upload_md5;
|
||||
@@ -0,0 +1,119 @@
|
||||
-- T6 一期导入对账:批次、差异、处置审计与上传 MD5。
|
||||
-- 只新增,不改台账既有状态语义;findings 可按 run 整批清理,actions 永久保留。
|
||||
|
||||
ALTER TABLE test_publications
|
||||
ADD COLUMN IF NOT EXISTS upload_md5 CHAR(32) NOT NULL DEFAULT ''
|
||||
CHECK (upload_md5 = '' OR upload_md5 ~ '^[0-9a-f]{32}$');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS reconciliation_runs (
|
||||
id VARCHAR(36) PRIMARY KEY,
|
||||
test_kb_id VARCHAR(255) NOT NULL,
|
||||
state VARCHAR(16) NOT NULL DEFAULT 'running'
|
||||
CHECK (state IN ('running', 'succeeded', 'failed')),
|
||||
trigger VARCHAR(16) NOT NULL
|
||||
CHECK (trigger IN ('schedule', 'manual')),
|
||||
started_at TIMESTAMPTZ NOT NULL,
|
||||
finished_at TIMESTAMPTZ,
|
||||
ledger_count INTEGER NOT NULL DEFAULT 0 CHECK (ledger_count >= 0),
|
||||
kb_count INTEGER NOT NULL DEFAULT 0 CHECK (kb_count >= 0),
|
||||
counts JSONB NOT NULL DEFAULT '{}'::jsonb
|
||||
CHECK (jsonb_typeof(counts) = 'object'),
|
||||
previous_counts JSONB
|
||||
CHECK (previous_counts IS NULL OR jsonb_typeof(previous_counts) = 'object'),
|
||||
window_overlapped BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
error_code VARCHAR(64) NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- One running batch per test knowledge base. The application 409 is only a
|
||||
-- friendly hint; this index is the real concurrency guard.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS reconciliation_runs_one_running
|
||||
ON reconciliation_runs (test_kb_id) WHERE state = 'running';
|
||||
CREATE INDEX IF NOT EXISTS idx_reconciliation_runs_started
|
||||
ON reconciliation_runs (started_at DESC, id);
|
||||
CREATE INDEX IF NOT EXISTS idx_reconciliation_runs_kb_started
|
||||
ON reconciliation_runs (test_kb_id, started_at DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS reconciliation_findings (
|
||||
id VARCHAR(36) PRIMARY KEY,
|
||||
run_id VARCHAR(36) NOT NULL
|
||||
REFERENCES reconciliation_runs(id) ON DELETE CASCADE,
|
||||
finding_type VARCHAR(2) NOT NULL
|
||||
CHECK (finding_type IN ('R1','R2','R3','R4','R5','R6','R7','R8')),
|
||||
publication_id VARCHAR(36) NOT NULL DEFAULT '',
|
||||
knowledge_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
path TEXT NOT NULL DEFAULT '',
|
||||
source TEXT NOT NULL DEFAULT '',
|
||||
target_scope VARCHAR(16) NOT NULL DEFAULT '',
|
||||
ledger_state VARCHAR(20) NOT NULL DEFAULT '',
|
||||
weknora_parse_status VARCHAR(32) NOT NULL DEFAULT '',
|
||||
normalized_state VARCHAR(32) NOT NULL DEFAULT '',
|
||||
ledger_sha256 CHAR(64) NOT NULL DEFAULT '',
|
||||
declared_sha256 CHAR(64) NOT NULL DEFAULT '',
|
||||
weknora_file_md5 CHAR(32) NOT NULL DEFAULT '',
|
||||
evidence_level VARCHAR(16) NOT NULL DEFAULT 'none'
|
||||
CHECK (evidence_level IN ('independent','declared_only','none')),
|
||||
confirmation_state VARCHAR(16) NOT NULL DEFAULT 'confirmed'
|
||||
CHECK (confirmation_state IN ('confirmed','pending')),
|
||||
detail TEXT NOT NULL DEFAULT '',
|
||||
suggested_action VARCHAR(32) NOT NULL DEFAULT 'none'
|
||||
CHECK (suggested_action IN ('requeue_upload','remove_from_test','writeback_state','enter_removal','none')),
|
||||
action_state VARCHAR(16) NOT NULL DEFAULT 'open'
|
||||
CHECK (action_state IN ('open','done')),
|
||||
weknora_url TEXT NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (run_id, finding_type, publication_id, knowledge_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_reconciliation_findings_run
|
||||
ON reconciliation_findings (run_id, finding_type, id);
|
||||
CREATE INDEX IF NOT EXISTS idx_reconciliation_findings_publication
|
||||
ON reconciliation_findings (publication_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_reconciliation_findings_knowledge
|
||||
ON reconciliation_findings (knowledge_id);
|
||||
|
||||
-- Durable action audit. Deliberedly not FK-bound to findings: findings are
|
||||
-- deleted by retention, the audit is not.
|
||||
CREATE TABLE IF NOT EXISTS reconciliation_finding_actions (
|
||||
id VARCHAR(36) PRIMARY KEY,
|
||||
finding_id VARCHAR(36) NOT NULL,
|
||||
run_id VARCHAR(36) NOT NULL,
|
||||
publication_id VARCHAR(36) NOT NULL DEFAULT '',
|
||||
knowledge_id VARCHAR(255) NOT NULL DEFAULT '',
|
||||
action VARCHAR(32) NOT NULL
|
||||
CHECK (action IN ('writeback','requeue','remove')),
|
||||
actor_ref VARCHAR(128) NOT NULL DEFAULT '',
|
||||
outcome VARCHAR(32) NOT NULL,
|
||||
detail TEXT NOT NULL DEFAULT '',
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_reconciliation_actions_finding
|
||||
ON reconciliation_finding_actions (finding_id, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_reconciliation_actions_publication
|
||||
ON reconciliation_finding_actions (publication_id, created_at DESC);
|
||||
|
||||
-- R6 and /monitor/overview must never disagree about "需下架": both read this
|
||||
-- view. The 2026-10-10 acceptance found the old lane permanently zero because
|
||||
-- two different predicates were maintained by hand.
|
||||
CREATE OR REPLACE VIEW removal_candidates AS
|
||||
SELECT publication.id AS publication_id,
|
||||
publication.test_kb_id,
|
||||
publication.knowledge_id,
|
||||
publication.target_scope,
|
||||
version.id AS source_item_version_id,
|
||||
version.source_item_id,
|
||||
item.title,
|
||||
item.normalized_path,
|
||||
item.source_connection_id,
|
||||
connection.name AS source_name
|
||||
FROM test_publications publication
|
||||
JOIN source_item_versions version ON version.id = publication.source_item_version_id
|
||||
JOIN source_items item ON item.id = version.source_item_id
|
||||
JOIN source_connections connection ON connection.id = item.source_connection_id
|
||||
WHERE publication.superseded_at IS NULL
|
||||
AND publication.knowledge_id <> ''
|
||||
AND publication.processing_state = 'completed'
|
||||
AND publication.retrieval_state NOT IN ('disabling', 'disabled')
|
||||
AND item.policy_state = 'blocked';
|
||||
Reference in New Issue
Block a user