muse-agent-example/数据库/迁移/V0022__知识方法.sql
zizi cf0f4fb985 W20–W24:保存知识方法、迁移框架、审校修订与案例行为的集成成果
接续 88dd570,保存 W20–W24 已实现的共享接口、业务入口、迁移、工作台、测试与文档。
W20/W22/W23 保持 in_progress,W21/W24 保持 verified;此提交不宣称方法或规则正式启用、多轮返修、真实角色评测完成。

W25 新增实验、标定、逐调用交付与角色执行及其迁移/测试/索引留在实施工作树,原有私人和旧实现保留项不纳入。

验证:离线 571、前端 33 通过;PG 469 项通过、2 项浏览器未启用,2 项误带入的 W25 用例已移出本提交;最终任务与交付边界 37 项通过。make 检查、最终类型、84 项资源及 diff 检查通过。未重跑浏览器或 Pi 宿主,不以合成调用认证外部模型效果。
独立整体审查四维通过;证据保存在 R2-20260909/提交W20-W24。
2026-09-13 16:24:42 +08:00

130 lines
6.1 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- W20(B04):方法候选与版本、证据反例、固定绑定、不可变消费与检索索引。
-- 方法是跨作品正式内容:版本只追加;绑定记具体版本不追随;消费不可变;索引可重建不动正式内容。
-- 裁决 A:向量不依赖 pgvector,存 double precision[],相似度在应用层计算。
DO $migration$
BEGIN
-- 方法:跨作品范式卡;内容哈希按 owner 去重,同哈希不同 owner 由服务层拒绝。
CREATE TABLE public.muse_method (
method_id uuid PRIMARY KEY,
owner text NOT NULL,
type_id text NOT NULL,
title text NOT NULL,
state text NOT NULL CHECK (state IN ('proposed','confirmed','enabled','disabled','withdrawn')),
content_hash text NOT NULL,
current_version bigint NOT NULL DEFAULT 0 CHECK (current_version >= 0),
created_by text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (owner, content_hash),
CHECK (title <> '')
);
CREATE INDEX ON public.muse_method (type_id, state);
-- 方法版本:append-only;绑定与消费都指向具体版本。
CREATE TABLE public.muse_method_version (
version_id uuid PRIMARY KEY,
method_id uuid NOT NULL REFERENCES public.muse_method(method_id),
version bigint NOT NULL CHECK (version > 0),
content jsonb NOT NULL CHECK (jsonb_typeof(content) = 'object'),
content_hash text NOT NULL,
参数上限 jsonb NOT NULL CHECK (jsonb_typeof(参数上限) = 'object'),
schema_id text NOT NULL,
schema_version int NOT NULL,
effective_schema_hash text NOT NULL,
proposal_id uuid,
confirmed_by text NOT NULL,
confirmed_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (method_id, version),
UNIQUE (method_id, content_hash)
);
-- 方法提案:审阅对象;确认后版本入 append-only 表,提案保留可追溯。
CREATE TABLE public.muse_method_proposal (
proposal_id uuid PRIMARY KEY,
method_id uuid NOT NULL,
revision bigint NOT NULL CHECK (revision >= 1),
request jsonb NOT NULL CHECK (jsonb_typeof(request) = 'object'),
proposal_hash text NOT NULL,
created_by text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (method_id, revision)
);
-- 证据与反例:来源(B03 版本钉住)、样例、反例、例外与静态方法资源引用(不复制正文)。
CREATE TABLE public.muse_method_evidence (
evidence_id uuid PRIMARY KEY,
method_id uuid NOT NULL REFERENCES public.muse_method(method_id),
kind text NOT NULL CHECK (kind IN ('source','sample','counter','exception','static_ref')),
ref jsonb NOT NULL CHECK (jsonb_typeof(ref) = 'object'),
note text NOT NULL DEFAULT '',
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE INDEX ON public.muse_method_evidence (method_id, kind);
-- 方法绑定:作品或规划目标固定到具体版本;active 唯一;重绑留痕可回查。
CREATE TABLE public.muse_method_binding (
binding_id uuid PRIMARY KEY,
work_id text NOT NULL,
target_kind text NOT NULL CHECK (target_kind IN ('work','planning')),
target_ref text NOT NULL,
method_id uuid NOT NULL REFERENCES public.muse_method(method_id),
version_id uuid NOT NULL REFERENCES public.muse_method_version(version_id),
bound_by text NOT NULL,
bound_at timestamptz NOT NULL DEFAULT clock_timestamp(),
released_at timestamptz,
CHECK (target_ref <> '')
);
CREATE UNIQUE INDEX muse_method_binding_active
ON public.muse_method_binding (work_id, target_ref, method_id)
WHERE released_at IS NULL;
-- 方法消费:不可变;kind 区分生成/审阅/规划/启用验证;上下文与结果都可定位。
CREATE TABLE public.muse_method_usage (
usage_id uuid PRIMARY KEY,
method_version_id uuid NOT NULL REFERENCES public.muse_method_version(version_id),
kind text NOT NULL CHECK (kind IN ('generation','review','planning','enablement')),
context_ref text NOT NULL,
fragment_hash text NOT NULL,
result_ref text,
task_id uuid,
created_by text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
UNIQUE (method_version_id, kind, context_ref, fragment_hash)
);
CREATE INDEX ON public.muse_method_usage (method_version_id, created_at);
-- 方法检索索引:每版本一向量;embedder 版本随行;重建整表重写,不动方法本体。
CREATE TABLE public.muse_method_index (
method_version_id uuid PRIMARY KEY REFERENCES public.muse_method_version(version_id),
检索文本 text NOT NULL,
检索文本哈希 text NOT NULL,
vector double precision[] NOT NULL CHECK (array_length(vector, 1) > 0),
embedder text NOT NULL,
built_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
-- 索引版本台账:当前有效索引身份;检索时对不上即拒绝为陈旧。
CREATE TABLE public.muse_method_index_version (
ledger_id bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
索引身份 text NOT NULL,
embedder text NOT NULL,
method_count int NOT NULL CHECK (method_count >= 0),
built_at timestamptz NOT NULL DEFAULT clock_timestamp(),
active boolean NOT NULL DEFAULT false
);
CREATE UNIQUE INDEX muse_method_index_active ON public.muse_method_index_version (active)
WHERE active;
-- 文档派生索引:承接章后流程登记的摘要绑定(W16 接缝),关键词可查。
CREATE TABLE public.muse_document_index (
document_id text NOT NULL,
revision bigint NOT NULL CHECK (revision >= 0),
document_hash text NOT NULL,
结构哈希 text NOT NULL,
summary text NOT NULL DEFAULT '',
registered_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (document_id, revision)
);
END
$migration$;