reset_ontology.py
3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""清空本体(四元组改造后旧数据不兼容,用它重建)。
会删除(只限 --workspace 指定的本体):元素、绑定、证据、发布版本、写回层、骨架与关系边、数据管道、建模记录、
审核队列(AI 草案 / 变更提案)、流程卡及其运行、动作审批单与待办,以及旧能力表里的技能 / 动作定义。
保留:数据源与内省结果、文档与图谱、外部端点、用户与角色、审计日志(审计只追加,永不删除)。
整库重置(连数据源、文档、审计一起清)用 scripts/wipe_all.py。
./.venv/Scripts/python.exe scripts/reset_ontology.py --yes
./.venv/Scripts/python.exe scripts/reset_ontology.py --yes --template generic_starter # 顺带导入起步包
"""
from __future__ import annotations
import argparse
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent))
sys.stdout.reconfigure(encoding="utf-8")
def main() -> None:
ap = argparse.ArgumentParser(description="清空本体")
ap.add_argument("--yes", action="store_true", help="确认清空")
ap.add_argument("--workspace", default="default")
ap.add_argument("--template", default="", help="清空后导入的模板 id(templates/*.yaml)")
args = ap.parse_args()
if not args.yes:
raise SystemExit("这会清空本体的所有元素与发布版本。确认请加 --yes")
from sqlalchemy import delete, select
from app import bootstrap
from app.capability.models import ActionRun, ApprovalTask, CapabilityDef, Task
from app.governance.models import ReviewItem
from app.sop.models import SopDefinition, SopStepRun, SopRun, SopVersion
from app.core import db as core_db
from app.core import identity
from app.core.dbx import session
from app.governance import audit
from app.kernel import models as km
from app.kernel import service, templates
core_db.init_db()
bootstrap.setup()
identity.set_current(identity.Actor(workspace=args.workspace))
with session() as s:
n = 0
runs = select(SopRun.id).where(SopRun.workspace == args.workspace)
n += s.execute(delete(SopStepRun).where(SopStepRun.run_id.in_(runs))).rowcount or 0
defs = select(SopDefinition.id).where(SopDefinition.workspace == args.workspace)
n += s.execute(delete(SopVersion).where(SopVersion.def_id.in_(defs))).rowcount or 0
model_runs = select(km.ModelRun.id).where(km.ModelRun.workspace == args.workspace)
n += s.execute(delete(km.ModelCall).where(km.ModelCall.run_id.in_(model_runs))).rowcount or 0
actions = select(ActionRun.id).where(ActionRun.workspace == args.workspace)
n += s.execute(delete(ApprovalTask).where(ApprovalTask.run_id.in_(actions))).rowcount or 0
for table in (km.Concept, km.Binding, km.Evidence, km.Revision, km.ObjectEdit, km.InstanceCache,
km.LinkEdge, km.Pipeline, km.ModelRun, ReviewItem, SopRun, SopDefinition,
ActionRun, Task):
n += s.execute(delete(table).where(table.workspace == args.workspace)).rowcount or 0
n += s.execute(delete(CapabilityDef).where(CapabilityDef.workspace == args.workspace,
CapabilityDef.kind.in_(("skill", "action")))).rowcount or 0
s.commit()
service.invalidate_runtime()
audit.record("ontology.reset", "ontology", "", f"清空本体({n} 行)")
print(f"已清空 {n} 行")
if args.template:
out = templates.apply_template(args.template)
print(f"已导入模板 {args.template}:新增 {len(out['created'])},错误 {len(out['errors'])}")
for e in out["errors"]:
print(" ", e)
if __name__ == "__main__":
main()