merge sandbox/d42537c: chg_0005 experiment_platform_extend — 6 new operators + registration + 8 e2e cases (gate PASS)
- 6 new operators: scql (secure sum) / horizontal-fedavg (FedAvg mean) / secure-mlp (sigmoid + Laplace DP) / tee-inference (sha256 attestation) / prompt-defense (keyword classifier) / compliance-audit (uuid trail)
- OperatorsService + experiment.module 注册追加(不替换 chg_0002 的 3 算子)
- tests/e2e/dth_experiment_extend.test.sh: 8 case 覆盖 S7-S12 + list ≥9
- docs/dth/experiment-platform/operators-design.md: T1 架构
- openspec/changes/chg_0005_experiment_platform_extend/{5 工件}
Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
34bcec0eb9
commit
f1a15c44f4
16 files changed
+956
-1
No files matched your search
@@ -0,0 +1,132 @@
|
||||
# Operators Design — 实验台算子扩展(chg_0005 · 6 新算子)
|
||||
|
||||
> 覆盖 capability:`experiment_platform` · 接续 chg_0002 的 S1-S6,本文档定义 S7-S12 共 6 个新算子的算法骨架、输入输出 schema、真计算细节。
|
||||
|
||||
## 1. 背景
|
||||
chg_0002 落地了 3 个 demo 算子(hello-mpc / pir / vertical-lr)。需求 §4.4 要求 12 原有 + 7 新增 = 19 算子全集。本 chg 在已有 OperatorsService + RunsService + ExperimentController 框架上**再注入 6 个真实算法算子**。
|
||||
|
||||
## 2. 设计原则
|
||||
- 每个算子 = 一个独立文件,遵循 chg_0002 已立的 `@Injectable()` class + `Operator` interface 模式。
|
||||
- 不引入新 npm 依赖;只使用 Node 内置 `crypto` + 项目已有的 `uuid`。
|
||||
- 所有算子必须返回**真计算字段**(非 stub / 非 hardcode "ok":true 占位),便于 S7-S12 e2e 断言。
|
||||
- 注册方式:构造函数注入 6 个新 service,`onModuleInit()` 遍历注入实例 → 写入 `registry` map(不替换 chg_0002 已注册的 3 个)。
|
||||
|
||||
## 3. 算子清单
|
||||
|
||||
### 3.1 scql(Secure Collaborative Query Language,安全协作查询)
|
||||
- **定位**:原 chg 14 算子之一。SCQL 是蚂蚁开源的多方安全计算协作查询系统。
|
||||
- **算法骨架**:input 列求和聚合 → secure sum over multi-party columns。
|
||||
- **Input schema**:
|
||||
```ts
|
||||
{ columns: [{ values: number[] }, ...] } // 每列一维数值数组
|
||||
```
|
||||
- **Output 真计算字段**:
|
||||
- `sum`:所有列所有 values 求和(整数和)
|
||||
- `column_count`:列数
|
||||
- `total_values`:元素总数
|
||||
- `mean`:sum / total_values
|
||||
- `per_column_sum`:每列分别求和
|
||||
- **断言(S7)**:columns=[{values:[10,20,30]},{values:[1,2,3]}] → sum=66, column_count=2
|
||||
|
||||
### 3.2 horizontal-fedavg(横向联邦平均)
|
||||
- **定位**:原 chg 14 算子之一。FedAvg 是 McMahan 2017 联邦学习经典算法。
|
||||
- **算法骨架**:input 多个 client weights → server 聚合(mean / sum / median)→ 一轮聚合结果。
|
||||
- **Input schema**:
|
||||
```ts
|
||||
{ clients: [{ weights: number[] }, ...], aggregation: 'mean' | 'sum' } // aggregation 缺省 'mean'
|
||||
```
|
||||
- **Output 真计算字段**:
|
||||
- `aggregated`:聚合后向量(mean 时按 N clients 求平均;sum 时直接相加)
|
||||
- `client_count`:客户端数
|
||||
- `round_count`:本实现固定 = 1(一轮聚合)
|
||||
- `aggregation`:实际使用的聚合方式
|
||||
- **断言(S8)**:3 clients 各 2-dim weights=[0.1,0.2]/[0.3,0.4]/[0.5,0.6] → aggregated=[0.3,0.4]
|
||||
|
||||
### 3.3 secure-mlp(隐私保护 MLP — 差分噪声 + sigmoid)
|
||||
- **定位**:原 chg 14 算子之一。单层线性 + DP Laplace 噪声的 MLP forward。
|
||||
- **算法骨架**:`pred_i = sigmoid(Σ_j w_j * x_ij + bias + Laplace(0, 1/epsilon))`
|
||||
- **Input schema**:
|
||||
```ts
|
||||
{ features: number[][], weights: number[], bias: number, epsilon: number }
|
||||
```
|
||||
- **Output 真计算字段**:
|
||||
- `predictions`:每条样本一个 sigmoid 输出 ∈ (0,1)
|
||||
- `noise_scale`:1/epsilon 的 Laplace scale
|
||||
- `rows`:样本数
|
||||
- `weights_used`:回显 weights(便于审计)
|
||||
- `bias_used`:回显 bias
|
||||
- **断言(S9)**:features=[[1,2],[3,4]], weights=[0.5,0.5], bias=0.1, epsilon=1.0 → predictions 长度=2;每项 ∈ (0,1);noise_scale = 1.0
|
||||
|
||||
### 3.4 tee-inference(TEE 密态推理 mock)
|
||||
- **定位**:新增 7 算子之一。模拟 Trusted Execution Environment(Intel SGX / AMD SEV / 机密 VM)内 LLM 推理,并产出 attestation 凭证。
|
||||
- **算法骨架**:
|
||||
- `attestation = 'tee-mock-' + sha256(prompt + '||' + model).toString('hex').slice(0,56)` (总长 = 9 + 56 = 65 ≥ 64)
|
||||
- `response` = 模拟 LLM 回复 `mock: <model> echoes: <first 64 chars of prompt>`
|
||||
- **Input schema**:
|
||||
```ts
|
||||
{ prompt: string, model: string } // model 缺省 'gpt-mock'
|
||||
```
|
||||
- **Output 真计算字段**:
|
||||
- `attestation`:≥ 64 字符、含 `tee-mock-` 前缀、sha256-based
|
||||
- `response`:模拟 LLM 输出
|
||||
- `model`:回显输入
|
||||
- `prompt_length`:输入长度
|
||||
- `enclave`:固定 `mock-sgx-equivalent`
|
||||
- **断言(S10)**:prompt="hello", model="gpt-mock" → attestation 长度 ≥ 64 且以 `tee-mock-` 开头;model == "gpt-mock"
|
||||
|
||||
### 3.5 prompt-defense(Prompt 注入检测)
|
||||
- **定位**:新增 7 算子之一。检测 prompt 是否含注入攻击关键词,分类威胁等级。
|
||||
- **算法骨架**:
|
||||
- 关键词集合:`['ignore', 'previous', 'instructions', 'system', 'override', 'jailbreak']`
|
||||
- 大小写不敏感 substring 匹配;统计命中数
|
||||
- `threat_level`:hits ≥2 = "high";=1 = "medium";=0 = "low"
|
||||
- `detected`:hits ≥ 1
|
||||
- **Input schema**:
|
||||
```ts
|
||||
{ prompt: string }
|
||||
```
|
||||
- **Output 真计算字段**:
|
||||
- `detected`:bool
|
||||
- `threat_level`:enum `low|medium|high`
|
||||
- `matched_keywords`:命中的关键词数组(去重)
|
||||
- `score`:命中数(>=0)
|
||||
- `prompt_length`:输入长度
|
||||
- **断言(S11)**:prompt 含 "ignore" + "previous" → hits=2 → threat_level="high" detected=true;prompt="What is 2+2?" → hits=0 → threat_level="low" detected=false
|
||||
|
||||
### 3.6 compliance-audit(合规审计 — UUID trail + 评分)
|
||||
- **定位**:新增 7 算子之一。生成不可篡改审计 trail。
|
||||
- **算法骨架**:
|
||||
- `trail_id = uuidv4()`(标准 36 字符 UUID v4)
|
||||
- `compliance_score = ((fnv1a32(action + ':' + actor) mod 10000) / 10000) ∈ [0,1)`
|
||||
- `status = 'recorded'`
|
||||
- **Input schema**:
|
||||
```ts
|
||||
{ action: string, actor: string, user?: string, timestamp?: string }
|
||||
```
|
||||
- **Output 真计算字段**:
|
||||
- `trail_id`:36 字符 UUID v4 字符串
|
||||
- `action`、`actor`、`user`、`timestamp`:回显输入
|
||||
- `compliance_score`:∈ [0,1)
|
||||
- `status`:固定 `'recorded'`
|
||||
- `recorded_at`:ISO 时间戳
|
||||
- **断言(S12)**:action="data_export", actor="alice" → trail_id 匹配 UUID 正则;compliance_score ∈ [0,1]
|
||||
|
||||
## 4. 注册方案
|
||||
- 修改 `operators.service.ts`:构造函数增加 6 个新算子的依赖注入;`onModuleInit()` 中 `for` 循环改为 `[...原3, ...新6]` 顺序注册;总数 ≥ 9。
|
||||
- 修改 `experiment.module.ts`:providers 数组追加 6 个新 class。
|
||||
- **行为影响**:既有 3 个算子的接口契约不变(向后兼容 chg_0002 回归测试)。
|
||||
|
||||
## 5. e2e 覆盖
|
||||
- 6 case(C1-C6)+ 1 个 list ≥ 9 算子的额外断言(覆盖 6 新 + 3 原)。
|
||||
- 沿用 chg_0002 bash + python json parse 风格。
|
||||
- 期望:6 + 1(list)+ 全部 PASS。
|
||||
|
||||
## 6. 风险与回退
|
||||
| 风险 | 缓解 |
|
||||
|------|------|
|
||||
| `vertical-lr.ts` 已知 TS2352 cast(chg_0002 时代码) | 不修改 chg_0002 文件;git stash --include-untracked 后跑 `tsc --noEmit` 验证 chg_0005 新文件 0 error |
|
||||
| e2e 启动后端被 bash 工具 kill | `setsid nohup ... > /tmp/e2e_backend.log 2>&1 &` 分离会话 |
|
||||
| 算子注册顺序冲突 | 每算子 code 唯一(scql/horizontal-fedavg/secure-mlp/tee-inference/prompt-defense/compliance-audit),map.set 幂等 |
|
||||
|
||||
## 7. 后续 chg
|
||||
- chg_0006+ 补齐剩余 10 算子(vertical-xgboost / secure-aggregation / psgd / etc.),沿用本 chg 的注册模式。
|
||||
Reference in new issue
Block a user