feat(governance): policy-driven ingestion from catalog annotations

Annotation in 来源与目录 is now the only import decision surface:
- migration 000033: catalog_import_intents (durable, rescan-proof per-file
  intent with locator snapshot + fetch lifecycle), source_items.policy_state,
  planned publication state, ui_setting_revisions.ingestion_concurrency
- marking fans a folder decision out to per-file intents, materializes
  versioned items, reuses already-verified local bytes (compare, don't pull),
  and plans one publication per routed KB; blocked retires planned rows
  while live publications stay untouched (需下架 shows in the KB diff)
- artifact.fetch worker lane (scripts/artifact_fetch.py): DingTalk export
  tree/live box API, AList /api/fs/get direct download with SSO-redirect
  fail-closed, PP ledger via locator hash; content-level dedupe on sha256
- 开始处理 promotes planned->pending inside the claim-time concurrency budget
  from system settings; claims enforce it in SQL
- monitor overview rebuilt as knowledge-base diff: planned/needs_removal per
  KB + fetch pipeline summary; frontend lanes 待入库/需下架 replace the
  deprecated snapshot change-set confirmation
- spool: images (png/jpg/gif/bmp/webp) accepted, ppt/pptx rejected in favour
  of PDF twins; DingTalk PPT resolves its converted PDF pair
This commit is contained in:
姚章浩
2026-09-13 22:17:12 +08:00
parent f6cb90cd70
commit 63f3b2c503
38 changed files with 1597 additions and 307 deletions
@@ -0,0 +1,9 @@
ALTER TABLE ui_setting_revisions DROP COLUMN IF EXISTS ingestion_concurrency;
DROP INDEX IF EXISTS idx_test_publications_planned;
ALTER TABLE test_publications DROP CONSTRAINT IF EXISTS test_publications_processing_state_check;
ALTER TABLE test_publications ADD CONSTRAINT test_publications_processing_state_check
CHECK (processing_state IN ('pending', 'processing', 'finalizing', 'completed', 'failed', 'stalled'));
DROP INDEX IF EXISTS idx_source_items_policy;
ALTER TABLE source_items DROP COLUMN IF EXISTS policy_state;
DROP INDEX IF EXISTS idx_catalog_intents_fetch;
DROP TABLE IF EXISTS catalog_import_intents;
@@ -0,0 +1,62 @@
-- Policy-driven ingestion: the ONLY import decision surface is the catalog
-- annotation (internal / external / both / blocked). Marking a file or folder
-- records a durable per-file INTENT that survives rescans; unannotated files
-- stay "unset" and are never ingested (cold start imports nothing).
-- A saved intent immediately materializes a versioned source item and an
-- artifact.fetch job so the artifact is pulled from the live source (or
-- matched against an already-present local copy); once content is verified a
-- 'planned' publication appears in the knowledge-base diff as 待入库. The one
-- human batch action "开始处理" promotes planned -> queued within the
-- concurrency budget configured in system settings.
-- Per-file decision, keyed by the connection-stable catalog node id
-- (node ids are derived from source_key + relative path, identical across
-- snapshots, so intents are rescan-proof).
CREATE TABLE IF NOT EXISTS catalog_import_intents (
source_connection_id VARCHAR(36) NOT NULL
REFERENCES source_connections(id) ON DELETE CASCADE,
node_id VARCHAR(64) NOT NULL,
visibility VARCHAR(20) NOT NULL
CHECK (visibility IN ('internal', 'external', 'both', 'blocked')),
source_item_id VARCHAR(36),
decided_by VARCHAR(128) NOT NULL,
reason VARCHAR(500) NOT NULL DEFAULT '',
-- Locator snapshot: the fetch worker needs path/name metadata without
-- re-walking a snapshot that may already have rotated.
relative_path TEXT NOT NULL DEFAULT '',
name TEXT NOT NULL DEFAULT '',
size_bytes BIGINT,
modified_at TIMESTAMPTZ,
content_sha256 CHAR(64),
locator_hash CHAR(64) NOT NULL DEFAULT '',
-- Content materialization lifecycle driven by the artifact.fetch job.
fetch_state VARCHAR(20) NOT NULL DEFAULT 'pending'
CHECK (fetch_state IN ('pending', 'fetching', 'ready', 'missing_local', 'failed')),
last_error TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (source_connection_id, node_id)
);
CREATE INDEX IF NOT EXISTS idx_catalog_intents_item
ON catalog_import_intents (source_item_id) WHERE source_item_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_catalog_intents_fetch
ON catalog_import_intents (fetch_state) WHERE fetch_state <> 'ready';
-- Materialized items carry the policy so list/diff queries never re-walk rules.
ALTER TABLE source_items ADD COLUMN IF NOT EXISTS policy_state VARCHAR(20) NOT NULL DEFAULT 'unset'
CHECK (policy_state IN ('unset', 'internal', 'external', 'both', 'blocked'));
CREATE INDEX IF NOT EXISTS idx_source_items_policy
ON source_items (policy_state) WHERE policy_state <> 'unset';
-- The knowledge base is the ingestion target of a planned publication; make
-- "planned" explicit so the KB-diff view never reuses queued semantics.
ALTER TABLE test_publications DROP CONSTRAINT IF EXISTS test_publications_processing_state_check;
ALTER TABLE test_publications ADD CONSTRAINT test_publications_processing_state_check
CHECK (processing_state IN ('planned', 'pending', 'processing', 'finalizing', 'completed', 'failed', 'stalled'));
CREATE INDEX IF NOT EXISTS idx_test_publications_planned
ON test_publications (test_kb_id, processing_state) WHERE processing_state = 'planned';
-- 开始处理's concurrency budget lives in system settings (validated revisions).
ALTER TABLE ui_setting_revisions ADD COLUMN IF NOT EXISTS ingestion_concurrency INTEGER NOT NULL DEFAULT 2
CHECK (ingestion_concurrency BETWEEN 1 AND 16);