Files
ssw-ontology/docker-compose.ontology.yml
T
admin f6ae259574 feat(ontology): 对象类型梳理 - 健康度扫描 + AI 同义判定 + 合并向导
让平台自己就能诊断同义/零实例/孤立/低区分度 4 类问题, 由 manager 审核后执行合并.

后端:
- 新建 server/handlers_ontology_cleanup.go, 4 个接口 (analyze / merge-preview /
  merge-execute / delete-zero-inst), 均走 managerAuth 仅 manager 可调.
- 新表 ontology_audit_log (actor / action / target_type / details / affected_rows),
  analyze / merge / delete-zero-inst 都写审计.
- ai.Provider 接口加 Complete(ctx, prompt) 轻量补全 (anthropic / openai / heuristic 实现),
  ExtractJSON 改为导出.
- db.go DDL 幂等 CREATE TABLE / INDEX.

前端:
- api.js 4 个新 wrapper.
- OntologyView 第 6 个 Tab「类型梳理」: 一键扫描 + 4 类问题清单 +
  同义组展开 (radio 选 target) + 合并预览 dialog + 一键删除按钮.

合并事务要点:
- 先 pk 加 _m<src_id> 后缀再 UPDATE object_type_id, 避免 UNIQUE(object_type_id,pk).
- 同时 UPDATE object_type_id + type_api_name, 保持两字段一致 (否则连接器查不到).
- link.source_type / target_type / link_types.source_type / link_types.target_type 同步改写.
- action_types 没有 target_type 字段 (在 params jsonb), 跳过.

健康度判定:
- 启发式候选: 名称规范化后相同 / Levenshtein ≤2 / HasPrefix 双向 (含
  TrustedCryptoCard vs TrustedCryptographyCard) / 描述 Jaccard ≥ 0.6.
- AI 用 anthropic MiniMax-M3 复核, 给 confidence + reason + 建议 target.
- AI 未明确表态的启发式候选不强行过滤, 标 conf=0.5 让 manager 自己决定.

辅助:
- 提供 docker-compose.ontology.yml 片段给 workspace compose 挂载使用
  (二进制 + archive + env + healthcheck + autoheal 自愈).

测试: 54→39 个对象类型, 13 个零实例一键删 + 2 组同义合并
(FunctionNode→FunctionalNode, TrustedCryptoCard→TrustedCryptographyCard),
审计日志 12 条, AI 实际接入 anthropic:MiniMax-M3.
2026-08-07 19:16:13 +08:00

109 lines
5.4 KiB
YAML

# =============================================================================
# 尚数网本体管理平台 · compose service 片段 (v2: 假死自愈版)
# 用法: 把下面整个 `ontology:` 块 + `autoheal:` 块复制到 ~/Desktop/workspace/docker-compose.yml
# 的 `services:` 下(建议放在 jnpf 服务之后),然后 `docker compose up -d`
#
# 依赖:
# - 复用 workspace 已有的 postgres 服务 (服务名 postgres, 容器端口 5432)
# - 加入 jnpf-net 网络, 容器间用服务名 `postgres` 互通
# - 二进制已交叉编译: /Users/xs/Desktop/know/ssw-ontology/bin/server-linux
# - 知识库目录: /Users/xs/Desktop/know/ssw-ontology/archive
#
# 关键设计:
# * restart: unless-stopped -> 进程挂掉 / docker daemon 重启后自动拉起
# * healthcheck -> 主动探活! 每 30s 打真实业务接口, 连 3 次失败标 unhealthy
# (这是 restart:unless-stopped 做不到的: 进程活着但服务假死)
# * autoheal 容器 -> 监听带 label=autoheal 的 unhealthy 容器并自动 docker restart
# 从而 "进程假死 → 标 unhealthy → autoheal 重启" 闭环自愈
# * HTTP_PORT=8081 -> 覆盖本体 .env 里的 8080 (8080 被 Trustee KBS 占用)
# * DATABASE_URL 用 postgres:5432/shangshu (shangshu 库已存在于 PG 卷)
# * alpine 默认无 CA, command 里 apk add ca-certificates 保证 MiniMax HTTPS 可用
# * 不需要挂 .env, env 全部写在下面 environment 段
# =============================================================================
ontology:
image: alpine:3.20
container_name: shangshu-ontology
# autoheal 只重启带此 label 的容器 (精准, 不影响 workspace 其他服务)
labels:
- "autoheal: true"
depends_on:
postgres:
condition: service_healthy
ports:
- "8081:8081"
volumes:
# 已编译的 Linux 静态二进制 (只读挂载)
- /Users/xs/Desktop/know/ssw-ontology/bin/server-linux:/app/server:ro
# 知识文件根目录 (读写, 支持上传提取)
- /Users/xs/Desktop/know/ssw-ontology/archive:/app/archive
environment:
DATABASE_URL: postgres://dacli:dacli123@postgres:5432/shangshu?sslmode=disable
HTTP_PORT: "8081"
ARCHIVE_DIR: /app/archive
SEED_ON_START: "true"
ALLOW_CORS: "true"
AI_ENABLED: "true"
AI_BASE_URL: https://api.minimaxi.com/anthropic
AI_KEY: sk-cp-pI_Yq-u5HrclWaesyc1R98_yJfUC6c_Mwv7711VQ_p7NeFof96qSLJNlzxsNz0ahi6-LxunFQ5mqkW9exBsyxX-JX9egz9P84edge8j-WMTAXaJVFxEVYRY
AI_MODEL: MiniMax-M3
networks:
- jnpf-net
restart: unless-stopped
# 启动: 装 CA 证书后前台跑 server
command: ["/bin/sh", "-c", "apk add --no-cache ca-certificates >/dev/null 2>&1; exec /app/server"]
# ---- 健康检查: 探测真实业务接口, 不是只 ping 端口 ----
healthcheck:
test: ["CMD-SHELL", "wget -q -O /dev/null http://localhost:8081/api/v1/connector/object-types || exit 1"]
interval: 30s # 每 30 秒探一次
timeout: 10s # 单次探活超时 10 秒
retries: 3 # 连续 3 次失败 => unhealthy (约 90 秒后触发)
start_period: 60s # 启动宽限期: 给 DB 建表 + seed 留足时间, 期间不计失败
# ---------------------------------------------------------------------------
# autoheal: 监听 unhealthy 容器并自动重启 (解决 restart:unless-stopped 不检测假死)
# 需要挂载 docker.sock (宿主 docker 守护进程), 只动带 autoheal label 的容器
# 首次 up 会从 Docker Hub 拉一次镜像 (willfarrell/autoheal)
# ---------------------------------------------------------------------------
autoheal:
image: willfarrell/autoheal:latest
container_name: autoheal
environment:
# 只处理带 "autoheal: true" label 的容器 (精准, 不影响 jnpf/postgres)
- AUTOHEAL_CONTAINER_LABEL=autoheal
# 重启前先等容器停止, 默认 true
- AUTOHEAL_START_PERIOD=30
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: unless-stopped
networks:
- jnpf-net
# =============================================================================
# 备选方案 (不想拉 autoheal 镜像 / 不想挂 docker.sock 时用):
# 把上面的 healthcheck 保留, 删掉 autoheal 块, 然后改用单容器 watchdog ——
# 把下面脚本存为 /Users/xs/Desktop/know/ssw-ontology/docker/watchdog.sh 并 chmod +x,
# 把 ontology 的 command 改成 ["/app/watchdog.sh"], 并挂载该脚本:
#
# # docker/watchdog.sh
# #!/bin/sh
# /app/server &
# PID=$!
# # 主进程退出则杀掉容器 (PID1), 让 unless-stopped 重启
# ( wait $PID; echo "[watchdog] server exited, killing container"; kill -9 1 ) &
# # 探活循环: 假死则自杀让容器退出
# while true; do
# sleep 30
# if ! wget -q -O /dev/null "http://localhost:8081/api/v1/connector/object-types" 2>/dev/null; then
# echo "[watchdog] healthcheck failed -> restart"
# kill -9 $PID 2>/dev/null
# exit 1
# fi
# done
#
# volumes 追加:
# - /Users/xs/Desktop/know/ssw-ontology/docker/watchdog.sh:/app/watchdog.sh:ro
# command: ["/app/watchdog.sh"]
# 注意: 此方案 watchdog 须是 PID1 (用 command: ["/app/watchdog.sh"] 即满足)
# =============================================================================