feat(mcp): 授权闭环 + 流程模板 + 基线适配器(P1 主体完成)
- 授权闭环 4 工具:authorize.grant / revoke / effective(四源并集 + 应用过滤两组数)+ user.create —— effective 是「授权了但看不见」的唯一诊断入口;user.create 建非管理员账号让闭环可自证 - prompts 3 模板:new_business_module / diagnose_menu_not_visible / verify_all —— 测试核对模板里引用的工具 id 是否存在(防腐烂) - 新 crate sspf-adapter-baseline:PlatformAdapter 第二实现,5 个只读 op + 形状归一化 —— 用 hyper::client::conn::http1,只支持 http://,口令传密文 - 新增 AR6 与 baseline-smoke.sh(10 条);mcp-smoke 23 → 32 条 - 影响:测试 282 → 306;十一闸全绿;工具 15 → 19;内存仍 7 MB
This commit is contained in:
1 parent
330436bf01
commit
df63306b24
20 files changed
+3287
-29
No files matched your search
Executable
+202
@@ -0,0 +1,202 @@
|
||||
#!/usr/bin/env bash
|
||||
# ============================================================================
|
||||
# baseline-smoke —— 基线平台 HTTP 适配器的契约尺子(P1-d)
|
||||
#
|
||||
# 验证什么:
|
||||
# B1 自动登录(账号 + 密文口令)能拿到 token
|
||||
# B2 describe_env 能读到 tables / version(health 是裸对象,不是信封)
|
||||
# B3 menu.tree 的 `data.list` 被投影成 native 的 `menus`
|
||||
# B4 user.list 的 `organize` → `organizeId`,缺的 `roleId` 补空串
|
||||
# B5 user.list 裁剪掉 native 没有的键(creatorTime/fullName…)
|
||||
# B6 dict.type.list 可用(空库返回 0 条也不算失败)
|
||||
# B7 不支持的 op 在**发请求前**被拦下(CAPABILITY_UNSUPPORTED)
|
||||
#
|
||||
# 🔴 为什么打自己而不是打 Java 后端:
|
||||
# 本仓内核实现的 HTTP 端点就是契约本身(docs/contract.md)。
|
||||
# 用适配器打自己 = 跨进程走一遍契约,能验证端点映射与形状归一化;
|
||||
# 等真实基线平台接入时,把 BASE_URL 换掉即可(P4 对账的预演)。
|
||||
#
|
||||
# 用法: bash scripts/baseline-smoke.sh [端口]
|
||||
# 退出码: 0 全过 / 1 有 ❌
|
||||
# ============================================================================
|
||||
|
||||
set -u
|
||||
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$ROOT" || exit 1
|
||||
|
||||
PORT="${1:-30993}"
|
||||
BIN="$ROOT/target/debug/ssw-platform"
|
||||
PROBE="$ROOT/target/debug/examples/probe"
|
||||
|
||||
PASS=0
|
||||
FAIL=0
|
||||
|
||||
ok() { PASS=$((PASS + 1)); printf '✅ %s\n' "$1"; }
|
||||
bad() { FAIL=$((FAIL + 1)); printf '❌ %s\n' "$1"; }
|
||||
|
||||
if [ ! -x "$BIN" ]; then
|
||||
echo "❌ 找不到 $BIN(先 cargo build)"
|
||||
exit 1
|
||||
fi
|
||||
if [ ! -x "$PROBE" ]; then
|
||||
echo "❌ 找不到 $PROBE(先 cargo build -p sspf-adapter-baseline --example probe)"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
TMP="$(mktemp -d /tmp/baseline-smoke.XXXXXX)" || exit 1
|
||||
SVC_PID=""
|
||||
|
||||
cleanup() {
|
||||
[ -n "$SVC_PID" ] && kill "$SVC_PID" 2>/dev/null
|
||||
sleep 1
|
||||
[ -n "$SVC_PID" ] && kill -9 "$SVC_PID" 2>/dev/null
|
||||
rm -rf "$TMP"
|
||||
}
|
||||
trap cleanup EXIT
|
||||
|
||||
# ---- 起一个 HTTP 后端(临时库,不污染开发数据)----
|
||||
PGLITE_DIR="$TMP/db" PORT="$PORT" "$BIN" > "$TMP/svc.log" 2>&1 &
|
||||
SVC_PID=$!
|
||||
|
||||
# 等就绪(最多 10 秒)
|
||||
for _ in $(seq 1 20); do
|
||||
if curl -s --noproxy '*' --max-time 1 "http://127.0.0.1:$PORT/api/health" > /dev/null 2>&1; then
|
||||
break
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
|
||||
if ! curl -s --noproxy '*' --max-time 2 "http://127.0.0.1:$PORT/api/health" | grep -q '"ok":true'; then
|
||||
echo "❌ 后端没起来;日志末尾:"
|
||||
tail -5 "$TMP/svc.log"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# ---- 跑探针 ----
|
||||
ENC="$("$BIN" --enc 123456)"
|
||||
if [ -z "$ENC" ]; then
|
||||
echo "❌ 取不到密文口令"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
"$PROBE" "http://127.0.0.1:$PORT" admin "$ENC" > "$TMP/probe.ndjson" 2> "$TMP/probe.err"
|
||||
if [ ! -s "$TMP/probe.ndjson" ]; then
|
||||
echo "❌ 探针没有输出;stderr:"
|
||||
cat "$TMP/probe.err"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# ---- 断言 ----
|
||||
python3 - "$TMP/probe.ndjson" <<'PY'
|
||||
import json, sys
|
||||
|
||||
lines = [l for l in open(sys.argv[1], encoding='utf-8') if l.strip()]
|
||||
rows = {}
|
||||
for line in lines:
|
||||
try:
|
||||
m = json.loads(line)
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"❌ 输出不是合法 JSON 行: {e}\n {line[:120]}")
|
||||
sys.exit(1)
|
||||
rows[m["op"]] = m
|
||||
|
||||
PASS = [0]
|
||||
FAIL = [0]
|
||||
|
||||
def ok(msg):
|
||||
PASS[0] += 1
|
||||
print(f"✅ {msg}")
|
||||
|
||||
def bad(msg):
|
||||
FAIL[0] += 1
|
||||
print(f"❌ {msg}")
|
||||
|
||||
# B1 / B2 环境与登录
|
||||
env = rows.get("describe_env", {})
|
||||
if env.get("ok") and env.get("data", {}).get("backend") == "baseline-http":
|
||||
d = env["data"]
|
||||
ok(f"B1 自动登录成功(账号+密文 ⇒ token),backend={d['backend']}")
|
||||
if d.get("tables", 0) > 0 and d.get("version"):
|
||||
ok(f"B2 describe_env 读到 health 裸对象:tables={d['tables']} version={d['version']}")
|
||||
else:
|
||||
bad(f"B2 describe_env 没读到 tables/version: {json.dumps(d, ensure_ascii=False)[:160]}")
|
||||
else:
|
||||
bad(f"B1 describe_env 失败: {json.dumps(env, ensure_ascii=False)[:200]}")
|
||||
|
||||
# B3 menu.tree 形状
|
||||
mt = rows.get("menu.tree", {})
|
||||
md = mt.get("data", {}) if mt.get("ok") else {}
|
||||
menus = md.get("menus") or []
|
||||
if mt.get("ok") and md.get("count", 0) > 0 and isinstance(menus, list):
|
||||
# native 形状:有 menus 键、每项有 urlAddress/parentId;baseline 的 list 键必须消失
|
||||
first = menus[0] if menus else {}
|
||||
if "list" not in md and {"id", "fullName", "urlAddress", "parentId"} <= set(first):
|
||||
ok(f"B3 menu.tree 投影成 native 形状({md['count']} 条,键为 menus 而非 list)")
|
||||
else:
|
||||
bad(f"B3 menu.tree 形状不对: {json.dumps(md, ensure_ascii=False)[:200]}")
|
||||
else:
|
||||
bad(f"B3 menu.tree 失败: {json.dumps(mt, ensure_ascii=False)[:200]}")
|
||||
|
||||
# B4 / B5 user.list 字段改名与裁剪
|
||||
ul = rows.get("permission.user.list", {})
|
||||
ud = ul.get("data", {}) if ul.get("ok") else {}
|
||||
users = ud.get("users") or []
|
||||
if ul.get("ok") and users:
|
||||
u0 = users[0]
|
||||
if "organizeId" in u0 and "organize" not in u0:
|
||||
ok("B4 user.list 的 organize → organizeId 改名生效")
|
||||
else:
|
||||
bad(f"B4 organizeId 改名失效: {json.dumps(u0, ensure_ascii=False)[:160]}")
|
||||
if "roleId" in u0:
|
||||
ok("B4b baseline 缺失的 roleId 补了空串(形状对齐 native)")
|
||||
else:
|
||||
bad("B4b roleId 没补默认值")
|
||||
leaked = {"creatorTime", "fullName", "description", "headIcon"} & set(u0)
|
||||
if not leaked:
|
||||
ok("B5 user.list 已裁剪掉 native 没有的键(否则对账永远不等)")
|
||||
else:
|
||||
bad(f"B5 有多余键未裁剪: {sorted(leaked)}")
|
||||
else:
|
||||
bad(f"B4 user.list 失败: {json.dumps(ul, ensure_ascii=False)[:200]}")
|
||||
|
||||
# B6 role / dict
|
||||
rl = rows.get("permission.role.list", {})
|
||||
if rl.get("ok") and rl.get("data", {}).get("count", 0) >= 2:
|
||||
ok(f"B6 role.list 投影正常({rl['data']['count']} 个角色)")
|
||||
else:
|
||||
bad(f"B6 role.list 失败: {json.dumps(rl, ensure_ascii=False)[:160]}")
|
||||
|
||||
dt = rows.get("dict.type.list", {})
|
||||
if dt.get("ok") and isinstance(dt.get("data", {}).get("types"), list):
|
||||
ok(f"B6b dict.type.list 可用({dt['data'].get('count', 0)} 条,空库不算失败)")
|
||||
else:
|
||||
bad(f"B6b dict.type.list 失败: {json.dumps(dt, ensure_ascii=False)[:160]}")
|
||||
|
||||
# B7 不支持的 op 必须在发请求前被拦下
|
||||
st = rows.get("schema.table.list", {})
|
||||
if st.get("ok") is False and st.get("error", {}).get("code") == "CAPABILITY_UNSUPPORTED":
|
||||
hints = " ".join(st["error"].get("hints") or [])
|
||||
if "native" in hints:
|
||||
ok("B7 不支持的 op ⇒ CAPABILITY_UNSUPPORTED,且指引切 native")
|
||||
else:
|
||||
bad("B7 错误没有可执行的 hint")
|
||||
if st["error"].get("next_actions"):
|
||||
ok("B7b 错误带 next_actions(AI 能自纠)")
|
||||
else:
|
||||
bad("B7b 错误缺 next_actions")
|
||||
else:
|
||||
bad(f"B7 schema.table.list 应该被拦下: {json.dumps(st, ensure_ascii=False)[:200]}")
|
||||
|
||||
print()
|
||||
print(f"PASS={PASS[0]} FAIL={FAIL[0]}")
|
||||
sys.exit(1 if FAIL[0] else 0)
|
||||
PY
|
||||
RC=$?
|
||||
|
||||
echo
|
||||
if [ "$RC" -ne 0 ]; then
|
||||
echo "❌ baseline-smoke 有失败项(见上)"
|
||||
exit 1
|
||||
fi
|
||||
echo "✅ baseline-smoke 全过"
|
||||
+19
-1
@@ -23,6 +23,9 @@
|
||||
# AR5 `crates/sspf-mcp`(若已存在)依赖 ⊆ {sspf-contract, serde, serde_json, tokio, async-trait}
|
||||
# —— MCP 层要能同时接「进程内内核」和「远端 Java 后端」⇒ **不许依赖内核内部**
|
||||
# —— tokio / async-trait 是协议实现(stdio 异步读写)所需,不是业务依赖
|
||||
# AR6 `crates/sspf-adapter-baseline` 依赖 ⊆ {sspf-mcp, sspf-contract, serde, serde_json,
|
||||
# tokio, http, hyper, hyper-util, http-body-util, bytes}
|
||||
# —— 适配器只认工具契约层;依赖内核 store 就等于把第二种后端钉死在本进程旁
|
||||
# MEM1 非注释代码里不许出现 `SELECT *`
|
||||
# —— 终端产品内存预算 ≤ 120MB(PLAN.md 判断 ⑥)⇒ 全表读进 Vec 直接爆
|
||||
# MEM2 `crates/**` 与 `src/backend/src/platform/**` 里不许出现无界 `HashMap` 缓存
|
||||
@@ -46,7 +49,7 @@ skip() { SKIP=$((SKIP + 1)); printf '⏭ %s\n' "$1"; }
|
||||
|
||||
usage() {
|
||||
echo "用法: bash scripts/check-arch.sh [--self-test]"
|
||||
echo " 静态检查 crate 依赖纪律(AR1–AR5)与内存红线(MEM1–MEM2)"
|
||||
echo " 静态检查 crate 依赖纪律(AR1–AR6)与内存红线(MEM1–MEM2)"
|
||||
echo "退出码: 0 全过 / 1 有 ❌ / 2 用法错"
|
||||
}
|
||||
|
||||
@@ -140,6 +143,20 @@ check_ar5() {
|
||||
fi
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------- AR6
|
||||
check_ar6() {
|
||||
local f="$CRATES/sspf-adapter-baseline/Cargo.toml"
|
||||
[ -f "$f" ] || { skip "AR6 sspf-adapter-baseline 不存在"; return; }
|
||||
local deps; deps="$(deps_of "$f" | tr '\n' ' ')"
|
||||
# 🔴 适配器只许依赖「工具契约层 + HTTP 客户端」。
|
||||
# 一旦依赖 sspf-store / axum,第二种后端就只能跑在同一个内核旁边 —— 双轨失效。
|
||||
if subset_of "$deps" "sspf-mcp sspf-contract serde serde_json tokio http hyper hyper-util http-body-util bytes"; then
|
||||
ok "AR6 sspf-adapter-baseline 依赖干净([$deps])"
|
||||
else
|
||||
bad "AR6 sspf-adapter-baseline 依赖越界:[$deps] —— 只许工具契约层 + HTTP 客户端,不许依赖内核 store"
|
||||
fi
|
||||
}
|
||||
|
||||
# ---------------------------------------------------------------- MEM1
|
||||
# 只看**代码行**:去掉 `//` 之后的注释、`//!` / `///` 文档注释、以及字符串里的反引号示例。
|
||||
check_mem1() {
|
||||
@@ -263,6 +280,7 @@ check_ar2
|
||||
check_ar3
|
||||
check_ar4
|
||||
check_ar5
|
||||
check_ar6
|
||||
check_mem1
|
||||
check_mem2
|
||||
|
||||
|
||||
+75
-3
@@ -79,6 +79,15 @@ cat > "$REQ" <<'NDJSON'
|
||||
{"jsonrpc":"2.0","id":18,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"menu.create","args":{"fullName":"冒烟菜单","enCode":"permission.smoke.page","urlAddress":"smoke/Page"}}}}
|
||||
{"jsonrpc":"2.0","id":19,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"menu.create","args":{"fullName":"重复","enCode":"permission.smoke.page","urlAddress":"smoke/Other"}}}}
|
||||
{"jsonrpc":"2.0","id":20,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"menu.create","args":{"fullName":"坏路径","enCode":"permission.smoke.bad","urlAddress":"/bad/Path"}}}}
|
||||
{"jsonrpc":"2.0","id":21,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.authorize.grant","args":{"objectType":"role","objectId":"role_user","menuEnCode":"permission.smoke.page"}}}}
|
||||
{"jsonrpc":"2.0","id":22,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.user.create","args":{"account":"smoke_user","realName":"冒烟用户","password":"Smoke@123","roleIds":["role_user"]}}}}
|
||||
{"jsonrpc":"2.0","id":23,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.effective","args":{"account":"smoke_user","withMenuDetail":true}}}}
|
||||
{"jsonrpc":"2.0","id":24,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.authorize.revoke","args":{"objectType":"role","objectId":"role_user","menuEnCode":"permission.smoke.page"}}}}
|
||||
{"jsonrpc":"2.0","id":25,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.effective","args":{"account":"smoke_user"}}}}
|
||||
{"jsonrpc":"2.0","id":26,"method":"tools/call","params":{"name":"sspf_call","arguments":{"tool_id":"permission.authorize.revoke","args":{"objectType":"role","objectId":"role_user","menuEnCode":"permission.smoke.page"}}}}
|
||||
{"jsonrpc":"2.0","id":27,"method":"prompts/list","params":{}}
|
||||
{"jsonrpc":"2.0","id":28,"method":"prompts/get","params":{"name":"new_business_module","arguments":{"module_name":"冒烟","module_code":"smoke"}}}
|
||||
{"jsonrpc":"2.0","id":29,"method":"prompts/get","params":{"name":"diagnose_menu_not_visible","arguments":{}}}
|
||||
NDJSON
|
||||
|
||||
echo "── MCP 工具层契约尺子(4 元工具 + 分层加载)──"
|
||||
@@ -156,10 +165,10 @@ else:
|
||||
ok("M3 业务工具不在 tools/list(只能经 discover 访问)")
|
||||
|
||||
# ---- M4 通知不回复 ----
|
||||
if len(parsed) == 20:
|
||||
ok("M4 20 条带 id 的请求各一个回复;通知无回复")
|
||||
if len(parsed) == 29:
|
||||
ok("M4 29 条带 id 的请求各一个回复;通知无回复")
|
||||
else:
|
||||
bad(f"M4 回复数 {len(parsed)},期望 20(通知不该有回复)")
|
||||
bad(f"M4 回复数 {len(parsed)},期望 29(通知不该有回复)")
|
||||
|
||||
# ---- M5 context ----
|
||||
sc = (result(3) or {}).get("structuredContent", {})
|
||||
@@ -285,6 +294,69 @@ if w3.get("ok") is False and w3e.get("code") == "BAD_URL_ADDRESS":
|
||||
else:
|
||||
bad(f"M22 坏路径未被拒: {json.dumps(w3, ensure_ascii=False)[:140]}")
|
||||
|
||||
# ---- M23~M30 授权闭环(建菜单后的最后一公里)----
|
||||
g = (result(21) or {}).get("structuredContent", {})
|
||||
if g.get("ok") is True and g.get("data", {}).get("granted") is True:
|
||||
ok("M23 permission.authorize.grant 授给角色成功")
|
||||
else:
|
||||
bad(f"M23 授权失败: {json.dumps(g, ensure_ascii=False)[:160]}")
|
||||
|
||||
u = (result(22) or {}).get("structuredContent", {})
|
||||
if u.get("ok") is True and u.get("data", {}).get("isAdministrator") is False:
|
||||
ok("M24 user.create 建出的是**非管理员**账号(能测出授权问题)")
|
||||
else:
|
||||
bad(f"M24 建用户失败: {json.dumps(u, ensure_ascii=False)[:160]}")
|
||||
|
||||
e1 = (result(23) or {}).get("structuredContent", {}).get("data", {})
|
||||
vis1 = [m.get("urlAddress") for m in (e1.get("visible") or {}).get("menus", [])
|
||||
if isinstance(m, dict)]
|
||||
src1 = (e1.get("sources") or {}).get("role", {}).get("menuIds") or []
|
||||
if e1.get("isAdmin") is False and "smoke/Page" in vis1 and src1:
|
||||
ok(f"M25 effective 看到授权的菜单(visible={e1['visible']['count']},来源=role)")
|
||||
else:
|
||||
bad(f"M25 授权后仍看不到: visible={vis1} isAdmin={e1.get('isAdmin')}")
|
||||
|
||||
rv = (result(24) or {}).get("structuredContent", {})
|
||||
if rv.get("ok") is True and rv.get("data", {}).get("revoked") is True:
|
||||
ok("M26 revoke 撤权成功(软删)")
|
||||
else:
|
||||
bad(f"M26 撤权失败: {json.dumps(rv, ensure_ascii=False)[:160]}")
|
||||
|
||||
e2 = (result(25) or {}).get("structuredContent", {}).get("data", {})
|
||||
vis2 = [m.get("urlAddress") for m in (e2.get("visible") or {}).get("menus", [])
|
||||
if isinstance(m, dict)]
|
||||
if "smoke/Page" not in vis2:
|
||||
ok("M27 撤权后 effective 立刻看不到(四源并集真的变了)")
|
||||
else:
|
||||
bad(f"M27 撤权后仍可见: {vis2}")
|
||||
|
||||
e3 = (result(26) or {}).get("structuredContent", {})
|
||||
if e3.get("ok") is False and e3.get("error", {}).get("code") == "AUTHORIZE_NOT_FOUND":
|
||||
ok("M28 重复撤权 ⇒ AUTHORIZE_NOT_FOUND(不静默成功)")
|
||||
else:
|
||||
bad(f"M28 重复撤权未被拒: {json.dumps(e3, ensure_ascii=False)[:160]}")
|
||||
|
||||
pl = result(27) or {}
|
||||
names = [x.get("name") for x in (pl.get("prompts") or [])]
|
||||
if len(names) >= 3 and "new_business_module" in names:
|
||||
ok(f"M29 prompts/list 返回 {len(names)} 个模板(含 new_business_module)")
|
||||
else:
|
||||
bad(f"M29 prompts/list 异常: {names}")
|
||||
|
||||
pg = result(28) or {}
|
||||
ptext = ((pg.get("messages") or [{}])[0].get("content") or {}).get("text", "")
|
||||
if "非管理员" in ptext and "menu.create" in ptext and "permission.authorize.grant" in ptext:
|
||||
ok("M30 prompts/get 正文含关键步骤(建菜单 + 授权 + 非管理员验证)")
|
||||
else:
|
||||
bad(f"M30 模板正文缺关键步骤(长度 {len(ptext)})")
|
||||
|
||||
# 缺参时是 JSON-RPC error(没有 result),所以要拿整条响应
|
||||
pm = parsed.get(29, {})
|
||||
if pm.get("error", {}).get("code") == -32602:
|
||||
ok("M31 模板缺必填参数 ⇒ INVALID_PARAMS(不渲染带占位符的正文)")
|
||||
else:
|
||||
bad(f"M31 缺参未被拦: {json.dumps(pm, ensure_ascii=False)[:140]}")
|
||||
|
||||
print()
|
||||
print(f"PASS={PASS} FAIL={FAIL}")
|
||||
sys.exit(0 if FAIL == 0 else 1)
|
||||
|
||||
Reference in new issue
Block a user