Files
kbdb-graph-plugin/.claude/hooks/pre-write-guard-no-table.sh
Leo efe8e165cf feat: KBDB-graph 插件獨立 — 全面改寫成走基本盤 API(API-as-Wall)
按 leo 鐵律(2026-06-14)把插件從「直接 SQL 操作基本盤表」改寫成
「只透過基本盤 arcrun/kbdb HTTP API 讀寫」。零建表、零 migration、零 SQL。

- 新增 src/lib/kbdb-client.ts:唯一對外通道,封裝 entries/templates/records API
- 新增 src/lib/templates.ts:triplet/entity template 定義(替代建表)
- 改寫 21 個違規 action(triplet/graph/entity/search)→ 走 client,圖在插件層記憶體組裝
- 移除所有 migrations、D1/Vectorize/AI 綁定;embedding/語意搜尋歸基本盤 optional 模組
- index.ts 只掛 triplets/graph/entities/search 路由;基本盤路由歸 arcrun/kbdb
- 測試改走 mock client(純 node);裁剪 CLAUDE.md 只留 graph 插件 + 鐵律
- 修正 SDD design.md「讀現狀推翻鐵律」的錯誤判斷(共用 D1 → API-as-Wall)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-14 20:59:41 +08:00

66 lines
2.7 KiB
Bash
Executable File
Raw Permalink 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.
#!/bin/bash
# .claude/hooks/pre-write-guard-no-table.sh
# KBDB-graph PreToolUse guard for Write / Edit / MultiEdit
#
# 鐵律(leo 2026-06-14):任何人都不准動表。插件不准直接接觸表,全走基本盤 API,禁 SQL。
# 退出 code0 = 允許 / 2 = 擋下(stderr 回傳給 CC
# 依賴:jq
set -o pipefail
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
CONTENT=$(echo "$INPUT" | jq -r '
.tool_input.content
// .tool_input.new_string
// (.tool_input.edits // [] | map(.new_string // "") | join("\n"))
// ""
')
block() {
cat >&2 <<EOF
❌ BLOCKED by KBDB 鐵律:任何人都不准動表
檔案:${FILE_PATH}
違反:${1}
原因:${2}
正確做法:${3}
參考:InkStoneCo/.agents/specs/matrix-rearrange/DECISION-kbdb-v3-baseplane.md
EOF
exit 2
}
# 跳過文件/markdown(規則說明本身會出現這些字)
case "$FILE_PATH" in
*.md|*/docs/*|*/.claude/*) exit 0 ;;
esac
# ── 規則 1:禁 CREATE / ALTER / DROP TABLE ──────────────────────────────
if echo "$CONTENT" | grep -iqE '(CREATE|ALTER|DROP)[[:space:]]+TABLE'; then
block "CREATE/ALTER/DROP TABLE" \
"任何人都不准動表。3 張基本盤表鎖死,只有基本盤維護者能改。" \
"新資料類型=建 template(調基本盤 templates API),永不建表。"
fi
# ── 規則 2:禁插件直接寫表(INSERT/UPDATE/DELETE 基本盤表)─────────────
if echo "$CONTENT" | grep -iqE '(INSERT[[:space:]]+INTO|UPDATE|DELETE[[:space:]]+FROM)[[:space:]]+(entries|templates|entry_values|blocks|triplets)'; then
block "直接寫基本盤表" \
"插件不准直接接觸表(API-as-Wall)。" \
"改調基本盤 API:寫 entry/record 走 POST /entries、/records(帶 template)。"
fi
# ── 規則 3:禁插件直接讀表(SELECT/JOIN 基本盤表)──────────────────────
if echo "$CONTENT" | grep -iqE '(SELECT[[:space:]].*FROM|JOIN)[[:space:]]+(entries|templates|entry_values|blocks|triplets)'; then
block "直接讀基本盤表" \
"插件不准直接接觸表(API-as-Wall),讀也要走 API。" \
"改調基本盤 API:查 records/search 端點取回資料,在插件層組裝成圖。"
fi
# ── 規則 4:禁 D1 .prepare(...sql...) 這類繞 API 的直接 SQL ─────────────
if echo "$CONTENT" | grep -iqE '\.(prepare|exec)\([^)]*(entries|templates|entry_values|blocks|triplets)'; then
block "繞過 API 直接 D1 SQL" \
"插件層全程禁 SQL。" \
"所有 DB 操作走基本盤 HTTP API,插件不直連 D1。"
fi
exit 0