Files
ssw-platform/crates/sspf-adapter-baseline/examples/probe.rs
T
admin df63306b24 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
2026-10-31 11:23:16 +08:00

119 lines
3.9 KiB
Rust
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.
//! probe —— 用基线适配器打一个**真实 HTTP 后端**,对每个 op 打印结果。
//!
//! ## 为什么需要它
//!
//! `BaselineHttpAdapter` 的单元测试只能验证「形状归一化」这半边;
//! 「端点映射对不对、登录流程通不通」必须打真实服务才能证明。
//!
//! 同时这也是 **P4 影子对账的探针雏形**:
//! 同一个 op 分别用 native 与 baseline 跑,输出可直接 diff。
//!
//! ## 用法
//!
//! ```bash
//! # 1. 起一个 HTTP 后端(它就是契约的实现,可以是被测的基线平台,也可以是本仓内核)
//! PGLITE_DIR=/tmp/db PORT=30994 ./target/debug/ssw-platform &
//!
//! # 2. 取密文口令
//! ENC=$(./target/debug/ssw-platform --enc 123456)
//!
//! # 3. 探针(账号 + 密文 ⇒ 自动登录)
//! cargo run -p sspf-adapter-baseline --example probe -- \
//! http://127.0.0.1:30994 admin "$ENC"
//! ```
//!
//! 输出是一行一个 JSON(NDJSON),便于 `jq` / python 消费:
//!
//! ```json
//! {"op":"permission.role.list","ok":true,"data":{"count":2,...}}
//! {"op":"schema.table.list","ok":false,"error":{"code":"CAPABILITY_UNSUPPORTED",...}}
//! ```
use sspf_adapter_baseline::BaselineHttpAdapter;
use sspf_mcp::adapter::PlatformAdapter;
use serde_json::json;
/// 逐个探测的 op(含一个**预期不支持**的,用来验证边界声明是诚实的)。
const OPS: &[&str] = &[
"context.get",
"menu.tree",
"permission.user.list",
"permission.role.list",
"dict.type.list",
// 下面这条应当返回 CAPABILITY_UNSUPPORTED —— 必须在**发请求前**拦下
"schema.table.list",
];
#[tokio::main]
async fn main() {
let mut args = std::env::args().skip(1);
let base = match args.next() {
Some(v) => v,
None => {
eprintln!("用法: probe <base_url> <account> <password_cipher>");
eprintln!("示例: probe http://127.0.0.1:30994 admin \"$(ssw-platform --enc 123456)\"");
std::process::exit(2);
}
};
let account = args.next().unwrap_or_else(|| "admin".to_string());
let cipher = match args.next() {
Some(v) => v,
None => {
eprintln!("缺少 password_cipher(用 `ssw-platform --enc <明文>` 生成)");
std::process::exit(2);
}
};
let adapter = match BaselineHttpAdapter::with_credentials(&base, &account, &cipher) {
Ok(a) => a,
Err(e) => {
eprintln!("构造适配器失败: {e}");
std::process::exit(2);
}
};
// 环境信息(登录也在这一步发生)
let env = adapter.describe_env().await;
println!(
"{}",
json!({
"op": "describe_env",
"ok": true,
"data": {
"backend": env.backend,
"version": env.version,
"app_code": env.app_code,
"system_id": env.system_id,
"user": env.user,
"tables": env.tables,
"capabilities": env.capabilities,
}
})
);
for op in OPS {
let args = match *op {
"permission.user.list" => json!({ "limit": 5 }),
_ => json!({}),
};
match adapter.invoke(op, args).await {
Ok(data) => println!("{}", json!({ "op": op, "ok": true, "data": data })),
Err(e) => println!(
"{}",
json!({
"op": op,
"ok": false,
"error": {
"code": e.code,
"message": e.message,
"hints": e.hints,
"next_actions": e.next_actions.iter()
.map(|n| json!({"tool": n.tool, "why": n.why}))
.collect::<Vec<_>>(),
}
})
),
}
}
}