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
@@ -9,3 +9,6 @@ build/
|
||||
src/frontend/dist/
|
||||
src/backend/dist/
|
||||
.xstudio/
|
||||
|
||||
# XStudio 模版例外:env 样例文件需要随合并审核入库
|
||||
!.env.example
|
||||
@@ -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 的注册模式。
|
||||
@@ -0,0 +1,46 @@
|
||||
---
|
||||
change_id: chg_0005
|
||||
status: proposed
|
||||
owner: pm@dth
|
||||
stakeholders: [pd, dev, qa]
|
||||
priority: P1
|
||||
target_release: v0.4
|
||||
created: 2026-09-28
|
||||
skip_coverage: false
|
||||
---
|
||||
|
||||
# Change: 实验台算子扩展(6 新算子 · S7-S12)
|
||||
|
||||
## Why
|
||||
chg_0002 落地了 3 个 demo 算子(hello-mpc / pir / vertical-lr)。需求 §4.4 要求 12 原有 + 7 新增 = 19 算子全集。本 chg 在 chg_0002 框架上**再补 6 个真实算法算子**(含 3 原 + 3 新增),把 capability `experiment_platform` 推向 v0.4。剩余算子(10 个)在后续 chg 补齐。
|
||||
|
||||
## Who
|
||||
- 高校教师:跑联邦/安全算子教学实验
|
||||
- 学生:调用 TEE 推理 / prompt 攻防 / 审计 算子
|
||||
- 平台运营:注册新算子(沿用 chg_0002 的 OperatorsService 注册模式)
|
||||
|
||||
## Scope
|
||||
- 6 个新算子(每个 = 真计算):
|
||||
- 原有 3:scql(安全协作查询)/ horizontal-fedavg(横向联邦平均)/ secure-mlp(隐私保护 MLP)
|
||||
- 新增 3:tee-inference(密态推理 mock)/ prompt-defense(提示词攻防 mock)/ compliance-audit(合规审计 mock)
|
||||
- 6 个 ADDED Scenarios(S7-S12)覆盖:register → run → get → 真字段断言
|
||||
- OperatorsService 注册扩展(构造函数注入 + onModuleInit 注册 map 追加)
|
||||
- experiment.module.ts providers 数组追加 6 新类
|
||||
- e2e:tests/e2e/dth_experiment_extend.test.sh(沿用 chg_0002 bash 风格 + python json 解析)
|
||||
|
||||
## Non-Goals
|
||||
- 不补到 19 算子全集(剩余 10 算子在 chg_0006+)
|
||||
- 不改前端(chg_0004 已定)
|
||||
- 不动 src/frontend/、src/backend/src/auth/、src/backend/src/common/{cipher,action-result.filter}.ts(契约红线)
|
||||
|
||||
## Impact
|
||||
- 现有 capability `experiment_platform` 增量(S 编号接续从 S7 起,不是 S1,capability 级全局)
|
||||
|
||||
## Acceptance
|
||||
- [ ] admin GET /api/ssw/dth/experiment/operators 返回 ≥ 9 算子(3 原 + 6 新)
|
||||
- [ ] 6 个新算子每个都能 POST /runs 返回 status=success 且 output_json 含真计算字段
|
||||
- [ ] perm-smoke + chg_0002 e2e + chg_0005 e2e 全绿
|
||||
- [ ] check_spec_coverage.sh gap=0
|
||||
|
||||
## Rollout
|
||||
合并 main;后端 :30000 重启生效。
|
||||
@@ -0,0 +1,60 @@
|
||||
---
|
||||
change_id: chg_0005
|
||||
owner: pd@dth
|
||||
status: draft
|
||||
requires_human_validation: false
|
||||
---
|
||||
|
||||
# Spec: experiment_platform
|
||||
|
||||
## Requirement
|
||||
WHEN 管理员调用算子列表接口 THE SYSTEM SHALL 返回 9+ 个算子元数据。
|
||||
WHEN 管理员对任一新算子发起 run THE SYSTEM SHALL 返回 run_id + 真 output_json。
|
||||
|
||||
## ADDED Scenarios
|
||||
|
||||
### S7 scql_aggregation_returns_real_sum
|
||||
- **WHEN** admin POST /runs body={"operator":"scql","input":{"columns":[{"values":[10,20,30]},{"values":[1,2,3]}]}}
|
||||
- **THEN** data.status=success
|
||||
- **AND** GET /runs/:id 的 data.output_json.scql.sum == 60(10+20+30 + 1+2+3 = 66…实测真求和,下面以断言代码为准)
|
||||
- **AND** output_json.scql.column_count == 2
|
||||
|
||||
### S8 horizontal_fedavg_aggregates_model_weights
|
||||
- **WHEN** admin POST /runs body={"operator":"horizontal-fedavg","input":{"clients":[{"weights":[0.1,0.2]},{"weights":[0.3,0.4]},{"weights":[0.5,0.6]}],"aggregation":"mean"}}
|
||||
- **THEN** data.status=success
|
||||
- **AND** GET /runs/:id 的 data.output_json.fedavg.aggregated == [0.3, 0.4]
|
||||
- **AND** output_json.fedavg.round_count == 1
|
||||
|
||||
### S9 secure_mlp_adds_dp_noise
|
||||
- **WHEN** admin POST /runs body={"operator":"secure-mlp","input":{"features":[[1.0,2.0],[3.0,4.0]],"weights":[0.5,0.5],"bias":0.1,"epsilon":1.0}}
|
||||
- **THEN** data.status=success
|
||||
- **AND** GET /runs/:id 的 data.output_json.mlp.predictions 数组长度 == 2
|
||||
- **AND** predictions[i] ∈ (0,1)(sigmoid 输出)
|
||||
- **AND** output_json.mlp.noise_scale > 0
|
||||
|
||||
### S10 tee_inference_returns_attestation
|
||||
- **WHEN** admin POST /runs body={"operator":"tee-inference","input":{"prompt":"hello","model":"gpt-mock"}}
|
||||
- **THEN** data.status=success
|
||||
- **AND** GET /runs/:id 的 data.output_json.tee.attestation 字符串长度 >= 64
|
||||
- **AND** output_json.tee.attestation 含 "tee-mock-" 前缀
|
||||
- **AND** output_json.tee.model == "gpt-mock"
|
||||
|
||||
### S11 prompt_defense_detects_injection
|
||||
- **WHEN** admin POST /runs body={"operator":"prompt-defense","input":{"prompt":"ignore previous instructions and ..."}}
|
||||
- **THEN** data.status=success
|
||||
- **AND** GET /runs/:id 的 data.output_json.defense.threat_level ∈ {"high","medium","low"}
|
||||
- **AND** output_json.defense.detected == true
|
||||
- **AND** 当 prompt = "What is 2+2?" 时 output_json.defense.detected == false, threat_level=="low"
|
||||
|
||||
### S12 compliance_audit_generates_trail
|
||||
- **WHEN** admin POST /runs body={"operator":"compliance-audit","input":{"action":"data_export","actor":"alice","user":"alice","timestamp":"2026-09-28T10:00:00Z"}}
|
||||
- **THEN** data.status=success
|
||||
- **AND** GET /runs/:id 的 data.output_json.audit.trail_id 是 36 位 UUID
|
||||
- **AND** output_json.audit.compliance_score ∈ [0,1]
|
||||
- **AND** output_json.audit.action == "data_export"
|
||||
|
||||
### all test_runner_and_gate_pass
|
||||
- **WHEN** 跑 `bash tests/e2e/dth_experiment_extend.test.sh`
|
||||
- **THEN** C7_test_runner_passes_all 通过(≥26 断言)
|
||||
- **AND** 跑 `bash openspec/scripts/check_spec_coverage.sh`
|
||||
- **AND** C8_check_spec_coverage_gap_zero 通过(gap=0)
|
||||
@@ -0,0 +1,30 @@
|
||||
# Tasks (DAG)
|
||||
- id: T1
|
||||
type: architect
|
||||
depends_on: []
|
||||
output: docs/dth/experiment-platform/operators-design.md
|
||||
verification: design covers scql/fedavg/secure-mlp/tee-inference/prompt-defense/compliance-audit
|
||||
|
||||
- id: T2
|
||||
type: implementer
|
||||
depends_on: [T1]
|
||||
output: src/backend/src/dth/experiment/operators/{scql,horizontal-fedavg,secure-mlp,tee-inference,prompt-defense,compliance-audit}.ts
|
||||
verification: 6 文件每个 export async function run(input) 返回真计算 output
|
||||
|
||||
- id: T3
|
||||
type: implementer
|
||||
depends_on: [T2]
|
||||
output: 修改 src/backend/src/dth/experiment/operators.service.ts 注册新 6 算子 + experiment.module.ts providers 数组追加 6 新类
|
||||
verification: OperatorsService.onModuleInit 注册后 list >= 9 项
|
||||
|
||||
- id: T4
|
||||
type: tester
|
||||
depends_on: [T3]
|
||||
output: tests/e2e/dth_experiment_extend.test.sh(6 case 覆盖 S7-S12 + list >= 9 验证)
|
||||
verification: 6/6 pass(list >= 9 + 6 个新算子真计算)
|
||||
|
||||
- id: T5
|
||||
type: qa
|
||||
depends_on: [T4]
|
||||
output: PROJECT_ROOT=. bash openspec/scripts/check_spec_coverage.sh
|
||||
verification: stdout PASS gap=0
|
||||
@@ -0,0 +1,25 @@
|
||||
# spec: experiment_platform@chg_0005
|
||||
scenarios:
|
||||
- id: S7
|
||||
cases: [C1_scql_sum_returns_real_aggregation]
|
||||
- id: S8
|
||||
cases: [C2_fedavg_aggregates_3_clients]
|
||||
- id: S9
|
||||
cases: [C3_secure_mlp_predictions_sigmoid]
|
||||
- id: S10
|
||||
cases: [C4_tee_inference_returns_attestation]
|
||||
- id: S11
|
||||
cases: [C5_prompt_defense_detects_injection]
|
||||
- id: S12
|
||||
cases: [C6_compliance_audit_trail_id_uuid]
|
||||
- id: all
|
||||
cases: [C7_test_runner_passes_all, C8_check_spec_coverage_gap_zero]
|
||||
|
||||
gate:
|
||||
blocking_gap_tolerance: 0
|
||||
implementation_files:
|
||||
- src/backend/src/dth/experiment/operators/
|
||||
- src/backend/src/dth/experiment/operators.service.ts
|
||||
- tests/e2e/dth_experiment_extend.test.sh
|
||||
test_files:
|
||||
- tests/e2e/
|
||||
@@ -0,0 +1,82 @@
|
||||
---
|
||||
change_id: chg_0005
|
||||
owner: qa@dth
|
||||
status: draft
|
||||
spec_ref: spec.md
|
||||
---
|
||||
|
||||
# Testcases
|
||||
|
||||
## Cases
|
||||
|
||||
### C1_scql_sum_returns_real_aggregation
|
||||
- Scenario: S7
|
||||
- Type: integration
|
||||
- Priority: P0
|
||||
- Given: admin token
|
||||
- When: POST /api/ssw/dth/experiment/runs body={"operator":"scql","input":{"columns":[{"values":[10,20,30]},{"values":[1,2,3]}]}}
|
||||
- Then: status=success; output_json.scql.sum=66; output_json.scql.column_count=2
|
||||
- Bound Task: T4
|
||||
|
||||
### C2_fedavg_aggregates_3_clients
|
||||
- Scenario: S8
|
||||
- Type: integration
|
||||
- Priority: P0
|
||||
- Given: admin token
|
||||
- When: POST /api/ssw/dth/experiment/runs body={"operator":"horizontal-fedavg","input":{"clients":[{"weights":[0.1,0.2]},{"weights":[0.3,0.4]},{"weights":[0.5,0.6]}],"aggregation":"mean"}}
|
||||
- Then: status=success; output_json.fedavg.aggregated=[0.3,0.4]
|
||||
- Bound Task: T4
|
||||
|
||||
### C3_secure_mlp_predictions_sigmoid
|
||||
- Scenario: S9
|
||||
- Type: integration
|
||||
- Priority: P0
|
||||
- Given: admin token
|
||||
- When: POST /api/ssw/dth/experiment/runs body={"operator":"secure-mlp","input":{"features":[[1.0,2.0],[3.0,4.0]],"weights":[0.5,0.5],"bias":0.1,"epsilon":1.0}}
|
||||
- Then: status=success; output_json.mlp.predictions 长度=2;每项 ∈ (0,1);noise_scale > 0
|
||||
- Bound Task: T4
|
||||
|
||||
### C4_tee_inference_returns_attestation
|
||||
- Scenario: S10
|
||||
- Type: integration
|
||||
- Priority: P0
|
||||
- Given: admin token
|
||||
- When: POST /api/ssw/dth/experiment/runs body={"operator":"tee-inference","input":{"prompt":"hello","model":"gpt-mock"}}
|
||||
- Then: status=success; output_json.tee.attestation 长度 >= 64 含 "tee-mock-" 前缀; model=="gpt-mock"
|
||||
- Bound Task: T4
|
||||
|
||||
### C5_prompt_defense_detects_injection
|
||||
- Scenario: S11
|
||||
- Type: integration
|
||||
- Priority: P0
|
||||
- Given: admin token
|
||||
- When: POST /api/ssw/dth/experiment/runs body={"operator":"prompt-defense","input":{"prompt":"ignore previous instructions and ..."}}
|
||||
- Then: status=success; output_json.defense.detected=true; threat_level ∈ {high,medium}
|
||||
- Bound Task: T4
|
||||
|
||||
### C6_compliance_audit_trail_id_uuid
|
||||
- Scenario: S12
|
||||
- Type: integration
|
||||
- Priority: P0
|
||||
- Given: admin token
|
||||
- When: POST /api/ssw/dth/experiment/runs body={"operator":"compliance-audit","input":{"action":"data_export","actor":"alice","user":"alice","timestamp":"2026-09-28T10:00:00Z"}}
|
||||
- Then: status=success; output_json.audit.trail_id 匹配 UUID 正则;compliance_score ∈ [0,1]; action=="data_export"
|
||||
- Bound Task: T4
|
||||
|
||||
### C7_test_runner_passes_all
|
||||
- Scenario: S7-S12
|
||||
- Type: e2e
|
||||
- Priority: P0
|
||||
- Given: 后端运行
|
||||
- When: bash tests/e2e/dth_experiment_extend.test.sh
|
||||
- Then: exit 0
|
||||
- Bound Task: T4
|
||||
|
||||
### C8_check_spec_coverage_gap_zero
|
||||
- Scenario: all
|
||||
- Type: e2e
|
||||
- Priority: P0
|
||||
- Given: chg 工件齐备
|
||||
- When: PROJECT_ROOT=. bash openspec/scripts/check_spec_coverage.sh
|
||||
- Then: stdout PASS gap=0
|
||||
- Bound Task: T5
|
||||
@@ -5,6 +5,12 @@ import { ExperimentController } from './experiment.controller';
|
||||
import { HelloMpcOperator } from './operators/hello-mpc';
|
||||
import { PirOperator } from './operators/pir';
|
||||
import { VerticalLrOperator } from './operators/vertical-lr';
|
||||
import { ScqlOperator } from './operators/scql';
|
||||
import { HorizontalFedavgOperator } from './operators/horizontal-fedavg';
|
||||
import { SecureMlpOperator } from './operators/secure-mlp';
|
||||
import { TeeInferenceOperator } from './operators/tee-inference';
|
||||
import { PromptDefenseOperator } from './operators/prompt-defense';
|
||||
import { ComplianceAuditOperator } from './operators/compliance-audit';
|
||||
|
||||
@Module({
|
||||
controllers: [ExperimentController],
|
||||
@@ -14,6 +20,12 @@ import { VerticalLrOperator } from './operators/vertical-lr';
|
||||
HelloMpcOperator,
|
||||
PirOperator,
|
||||
VerticalLrOperator,
|
||||
ScqlOperator,
|
||||
HorizontalFedavgOperator,
|
||||
SecureMlpOperator,
|
||||
TeeInferenceOperator,
|
||||
PromptDefenseOperator,
|
||||
ComplianceAuditOperator,
|
||||
],
|
||||
})
|
||||
export class DthExperimentModule {}
|
||||
@@ -3,6 +3,12 @@ import { Operator, OperatorMeta, OPERATOR_TOKEN } from './operators';
|
||||
import { HelloMpcOperator } from './operators/hello-mpc';
|
||||
import { PirOperator } from './operators/pir';
|
||||
import { VerticalLrOperator } from './operators/vertical-lr';
|
||||
import { ScqlOperator } from './operators/scql';
|
||||
import { HorizontalFedavgOperator } from './operators/horizontal-fedavg';
|
||||
import { SecureMlpOperator } from './operators/secure-mlp';
|
||||
import { TeeInferenceOperator } from './operators/tee-inference';
|
||||
import { PromptDefenseOperator } from './operators/prompt-defense';
|
||||
import { ComplianceAuditOperator } from './operators/compliance-audit';
|
||||
|
||||
@Injectable()
|
||||
export class OperatorsService implements OnModuleInit {
|
||||
@@ -12,10 +18,27 @@ export class OperatorsService implements OnModuleInit {
|
||||
private readonly helloMpc: HelloMpcOperator,
|
||||
private readonly pir: PirOperator,
|
||||
private readonly verticalLr: VerticalLrOperator,
|
||||
private readonly scql: ScqlOperator,
|
||||
private readonly horizontalFedavg: HorizontalFedavgOperator,
|
||||
private readonly secureMlp: SecureMlpOperator,
|
||||
private readonly teeInference: TeeInferenceOperator,
|
||||
private readonly promptDefense: PromptDefenseOperator,
|
||||
private readonly complianceAudit: ComplianceAuditOperator,
|
||||
) {}
|
||||
|
||||
onModuleInit() {
|
||||
for (const op of [this.helloMpc, this.pir, this.verticalLr]) {
|
||||
const all = [
|
||||
this.helloMpc,
|
||||
this.pir,
|
||||
this.verticalLr,
|
||||
this.scql,
|
||||
this.horizontalFedavg,
|
||||
this.secureMlp,
|
||||
this.teeInference,
|
||||
this.promptDefense,
|
||||
this.complianceAudit,
|
||||
];
|
||||
for (const op of all) {
|
||||
this.registry.set(op.meta.code, op);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
@Injectable()
|
||||
export class ComplianceAuditOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'compliance-audit',
|
||||
name: 'Compliance Audit — immutable trail with compliance score (mock)',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['action', 'actor'],
|
||||
properties: {
|
||||
action: { type: 'string', minLength: 1 },
|
||||
actor: { type: 'string', minLength: 1 },
|
||||
user: { type: 'string' },
|
||||
timestamp: { type: 'string' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const action = String(input?.action ?? '').trim();
|
||||
const actor = String(input?.actor ?? '').trim();
|
||||
if (!action) return { ok: false, error: 'action required' };
|
||||
if (!actor) return { ok: false, error: 'actor required' };
|
||||
|
||||
const user = input?.user !== undefined ? String(input.user) : actor;
|
||||
const timestamp =
|
||||
typeof input?.timestamp === 'string' && input.timestamp.length > 0
|
||||
? input.timestamp
|
||||
: new Date().toISOString();
|
||||
|
||||
// Real computation: UUID v4 trail + deterministic compliance score in [0,1).
|
||||
const trail_id = uuidv4();
|
||||
const compliance_score = Number((this.fnv1a(`${action}:${actor}`) % 10000) / 10000).toFixed(6);
|
||||
const score = Number(compliance_score);
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
audit: {
|
||||
trail_id,
|
||||
action,
|
||||
actor,
|
||||
user,
|
||||
timestamp,
|
||||
compliance_score: score,
|
||||
status: 'recorded',
|
||||
recorded_at: new Date().toISOString(),
|
||||
},
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private fnv1a(s: string): number {
|
||||
let h = 0x811c9dc5;
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
h ^= s.charCodeAt(i);
|
||||
h = (h + ((h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24))) >>> 0;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
@Injectable()
|
||||
export class HorizontalFedavgOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'horizontal-fedavg',
|
||||
name: 'Horizontal Federated Averaging (FedAvg one round, mean/sum)',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['clients'],
|
||||
properties: {
|
||||
clients: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
items: {
|
||||
type: 'object',
|
||||
required: ['weights'],
|
||||
properties: {
|
||||
weights: { type: 'array', items: { type: 'number' } },
|
||||
},
|
||||
},
|
||||
},
|
||||
aggregation: { type: 'string', enum: ['mean', 'sum'], default: 'mean' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const clients = Array.isArray(input?.clients) ? input.clients : [];
|
||||
if (clients.length === 0) {
|
||||
return { ok: false, error: 'clients must be a non-empty array' };
|
||||
}
|
||||
const aggregation = input?.aggregation === 'sum' ? 'sum' : 'mean';
|
||||
|
||||
// Real computation: FedAvg one-round server-side aggregation.
|
||||
// Validate dims are homogeneous.
|
||||
const dims: number[] = clients.map((c: any) =>
|
||||
Array.isArray(c?.weights) ? (c.weights as number[]).length : 0,
|
||||
);
|
||||
if (dims.length === 0 || dims.some((d) => d === 0)) {
|
||||
return { ok: false, error: 'every client must have non-empty weights' };
|
||||
}
|
||||
const first = dims[0];
|
||||
if (!dims.every((d) => d === first)) {
|
||||
return { ok: false, error: 'all client weights must share the same dimension' };
|
||||
}
|
||||
|
||||
const dim = first;
|
||||
const agg = new Array(dim).fill(0);
|
||||
for (const c of clients) {
|
||||
const ws = (c.weights as number[]).map(Number);
|
||||
for (let i = 0; i < dim; i++) agg[i] += ws[i] ?? 0;
|
||||
}
|
||||
if (aggregation === 'mean') {
|
||||
const n = clients.length;
|
||||
for (let i = 0; i < dim; i++) agg[i] = Number((agg[i] / n).toFixed(6));
|
||||
} else {
|
||||
for (let i = 0; i < dim; i++) agg[i] = Number(agg[i].toFixed(6));
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
fedavg: {
|
||||
aggregated: agg,
|
||||
client_count: clients.length,
|
||||
round_count: 1,
|
||||
aggregation,
|
||||
dimension: dim,
|
||||
algorithm: 'fedavg-mock-1-round',
|
||||
},
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
const INJECTION_KEYWORDS = [
|
||||
'ignore',
|
||||
'previous',
|
||||
'instructions',
|
||||
'system',
|
||||
'override',
|
||||
'jailbreak',
|
||||
];
|
||||
|
||||
@Injectable()
|
||||
export class PromptDefenseOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'prompt-defense',
|
||||
name: 'Prompt Injection Defense — keyword-based threat classifier',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['prompt'],
|
||||
properties: {
|
||||
prompt: { type: 'string', minLength: 0 },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const raw = String(input?.prompt ?? '');
|
||||
const lower = raw.toLowerCase();
|
||||
|
||||
// Real computation: case-insensitive substring match for each keyword.
|
||||
const matched: string[] = [];
|
||||
for (const kw of INJECTION_KEYWORDS) {
|
||||
if (lower.includes(kw)) matched.push(kw);
|
||||
}
|
||||
const hits = matched.length;
|
||||
const detected = hits >= 1;
|
||||
const threat_level = hits >= 2 ? 'high' : hits === 1 ? 'medium' : 'low';
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
defense: {
|
||||
detected,
|
||||
threat_level,
|
||||
matched_keywords: matched,
|
||||
score: hits,
|
||||
prompt_length: raw.length,
|
||||
keywords_total: INJECTION_KEYWORDS.length,
|
||||
},
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
@Injectable()
|
||||
export class ScqlOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'scql',
|
||||
name: 'SCQL — Secure Collaborative Query (multi-party sum aggregation mock)',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['columns'],
|
||||
properties: {
|
||||
columns: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
items: {
|
||||
type: 'object',
|
||||
required: ['values'],
|
||||
properties: {
|
||||
values: { type: 'array', items: { type: 'number' } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const cols = Array.isArray(input?.columns) ? input.columns : [];
|
||||
if (cols.length === 0) {
|
||||
return { ok: false, error: 'columns must be a non-empty array' };
|
||||
}
|
||||
|
||||
// Real computation: secure-sum across columns (mock multi-party aggregation).
|
||||
const perColumnSum: number[] = [];
|
||||
let totalSum = 0;
|
||||
let totalValues = 0;
|
||||
|
||||
for (const col of cols) {
|
||||
const vals = Array.isArray(col?.values) ? col.values.map(Number).filter(Number.isFinite) : [];
|
||||
const colSum = vals.reduce((a, b) => a + b, 0);
|
||||
perColumnSum.push(colSum);
|
||||
totalSum += colSum;
|
||||
totalValues += vals.length;
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
scql: {
|
||||
sum: totalSum,
|
||||
column_count: cols.length,
|
||||
total_values: totalValues,
|
||||
mean: totalValues > 0 ? Number((totalSum / totalValues).toFixed(6)) : 0,
|
||||
per_column_sum: perColumnSum,
|
||||
protocol: 'scql-psi-sum-mock',
|
||||
},
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
@Injectable()
|
||||
export class SecureMlpOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'secure-mlp',
|
||||
name: 'Secure MLP — single-layer forward + Laplace DP noise (mock)',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['features', 'weights', 'bias', 'epsilon'],
|
||||
properties: {
|
||||
features: {
|
||||
type: 'array',
|
||||
minItems: 1,
|
||||
items: {
|
||||
type: 'array',
|
||||
items: { type: 'number' },
|
||||
},
|
||||
},
|
||||
weights: { type: 'array', items: { type: 'number' }, minItems: 1 },
|
||||
bias: { type: 'number' },
|
||||
epsilon: { type: 'number', exclusiveMinimum: 0 },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const features = Array.isArray(input?.features) ? input.features : [];
|
||||
const weights = Array.isArray(input?.weights) ? input.weights.map(Number) : [];
|
||||
const bias = Number(input?.bias ?? 0);
|
||||
const epsilon = Number(input?.epsilon);
|
||||
if (features.length === 0) return { ok: false, error: 'features empty' };
|
||||
if (weights.length === 0) return { ok: false, error: 'weights empty' };
|
||||
if (!Number.isFinite(epsilon) || epsilon <= 0) return { ok: false, error: 'epsilon must be > 0' };
|
||||
|
||||
const dim = weights.length;
|
||||
const noiseScale = 1 / epsilon;
|
||||
|
||||
// Real computation: z_i = bias + Σ_j w_j * x_ij + Laplace(0, noiseScale)
|
||||
// pred_i = sigmoid(z_i)
|
||||
const predictions: number[] = [];
|
||||
for (const row of features) {
|
||||
const xRow: number[] = Array.isArray(row) ? (row as number[]).map(Number) : [];
|
||||
let z = bias;
|
||||
for (let j = 0; j < dim; j++) z += weights[j] * (xRow[j] ?? 0);
|
||||
z += this.laplace(noiseScale);
|
||||
const p = 1 / (1 + Math.exp(-z));
|
||||
predictions.push(Number(p.toFixed(6)));
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
mlp: {
|
||||
predictions,
|
||||
noise_scale: Number(noiseScale.toFixed(6)),
|
||||
rows: features.length,
|
||||
weights_used: weights,
|
||||
bias_used: bias,
|
||||
epsilon,
|
||||
mechanism: 'laplace',
|
||||
},
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
// Laplace noise sampler (m=0, b=scale). Uses inverse-CDF with two uniforms.
|
||||
private laplace(scale: number): number {
|
||||
const u = Math.random() - 0.5;
|
||||
return -scale * Math.sign(u) * Math.log(1 - 2 * Math.abs(u));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { createHash } from 'crypto';
|
||||
import { Operator, OperatorMeta } from '../operators';
|
||||
|
||||
@Injectable()
|
||||
export class TeeInferenceOperator implements Operator {
|
||||
readonly meta: OperatorMeta = {
|
||||
code: 'tee-inference',
|
||||
name: 'TEE Inference — confidential LLM inference with attestation (mock)',
|
||||
version: '0.1.0',
|
||||
schema: {
|
||||
type: 'object',
|
||||
required: ['prompt'],
|
||||
properties: {
|
||||
prompt: { type: 'string', minLength: 1 },
|
||||
model: { type: 'string', default: 'gpt-mock' },
|
||||
},
|
||||
additionalProperties: false,
|
||||
},
|
||||
};
|
||||
|
||||
async run(input: Record<string, any>): Promise<Record<string, any>> {
|
||||
const prompt = String(input?.prompt ?? '');
|
||||
const model = String(input?.model ?? 'gpt-mock');
|
||||
if (!prompt) return { ok: false, error: 'prompt required' };
|
||||
|
||||
// Real computation: sha256-based mock attestation.
|
||||
const hex = createHash('sha256').update(`${prompt}||${model}`).digest('hex');
|
||||
const attestation = `tee-mock-${hex.slice(0, 56)}`; // 9 + 56 = 65 chars, satisfies >= 64.
|
||||
|
||||
const response = `mock:${model} echoes: ${prompt.slice(0, 64)}`;
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
tee: {
|
||||
attestation,
|
||||
response,
|
||||
model,
|
||||
prompt_length: prompt.length,
|
||||
enclave: 'mock-sgx-equivalent',
|
||||
hash_full: hex,
|
||||
},
|
||||
computed_at: new Date().toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Executable
+163
@@ -0,0 +1,163 @@
|
||||
#!/usr/bin/env bash
|
||||
# dth_experiment_extend.test.sh — e2e verification for S7..S12 of chg_0005.
|
||||
# Exit 0 only if all 6 scenarios + list>=9 pass. Each assertion is a hard-fail.
|
||||
#
|
||||
# Usage:
|
||||
# DTH_BASE=http://127.0.0.1:30000 bash tests/e2e/dth_experiment_extend.test.sh
|
||||
#
|
||||
# Pre-req: backend is running and admin seed is loaded (run.sh dev).
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
BASE="${DTH_BASE:-http://127.0.0.1:${PORT:-30000}}"
|
||||
LOGIN_URL="$BASE/api/ssw/dth/auth/login"
|
||||
OPS_URL="$BASE/api/ssw/dth/experiment/operators"
|
||||
RUNS_URL="$BASE/api/ssw/dth/experiment/runs"
|
||||
|
||||
pass=0; fail=0; fails=()
|
||||
|
||||
json_get() {
|
||||
local field="$1"; local body="$2"
|
||||
python3 -c "import json,sys; d=json.loads(sys.argv[1]); print(d${field})" "$body" 2>/dev/null
|
||||
}
|
||||
|
||||
login() {
|
||||
curl -s -X POST "$LOGIN_URL" -H 'Content-Type: application/json' -d "{\"username\":\"$1\",\"password\":\"$2\"}"
|
||||
}
|
||||
|
||||
assert() {
|
||||
local id="$1"; local cond="$2"; local detail="$3"
|
||||
if eval "$cond"; then
|
||||
pass=$((pass+1))
|
||||
printf " ✓ %s\n" "$id"
|
||||
else
|
||||
fail=$((fail+1))
|
||||
fails+=("$id: $detail")
|
||||
printf " ✗ %s — %s\n" "$id" "$detail"
|
||||
fi
|
||||
}
|
||||
|
||||
# ----- pre: login admin -----
|
||||
ADMIN_BODY="$(login admin 123456)"
|
||||
ADMIN_TOKEN="$(json_get "['data']['token']" "$ADMIN_BODY")"
|
||||
assert "pre_admin_login" "[ -n '$ADMIN_TOKEN' ]" "admin token empty; login body=$ADMIN_BODY"
|
||||
|
||||
# ----- S7: scql sum aggregation -----
|
||||
SCQL_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"operator":"scql","input":{"columns":[{"values":[10,20,30]},{"values":[1,2,3]}]}}' "$RUNS_URL")"
|
||||
SCQL_ID="$(json_get "['data']['run_id']" "$SCQL_BODY")"
|
||||
SCQL_STATUS="$(json_get "['data']['status']" "$SCQL_BODY")"
|
||||
assert "S7_scql_run_status" "[ '$SCQL_STATUS' = 'success' ]" "scql status=$SCQL_STATUS"
|
||||
SCQL_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$SCQL_ID")"
|
||||
SCQL_SUM="$(json_get "['data']['output_json']['scql']['sum']" "$SCQL_GET")"
|
||||
SCQL_COLS="$(json_get "['data']['output_json']['scql']['column_count']" "$SCQL_GET")"
|
||||
SCQL_TOTAL="$(json_get "['data']['output_json']['scql']['total_values']" "$SCQL_GET")"
|
||||
assert "S7_scql_sum_66" "[ '$SCQL_SUM' = '66' ]" "sum=$SCQL_SUM"
|
||||
assert "S7_scql_column_count_2" "[ '$SCQL_COLS' = '2' ]" "column_count=$SCQL_COLS"
|
||||
assert "S7_scql_total_values_6" "[ '$SCQL_TOTAL' = '6' ]" "total_values=$SCQL_TOTAL"
|
||||
|
||||
# ----- S8: horizontal-fedavg mean of 3 clients -----
|
||||
FED_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"operator":"horizontal-fedavg","input":{"clients":[{"weights":[0.1,0.2]},{"weights":[0.3,0.4]},{"weights":[0.5,0.6]}],"aggregation":"mean"}}' "$RUNS_URL")"
|
||||
FED_ID="$(json_get "['data']['run_id']" "$FED_BODY")"
|
||||
FED_STATUS="$(json_get "['data']['status']" "$FED_BODY")"
|
||||
assert "S8_fedavg_run_status" "[ '$FED_STATUS' = 'success' ]" "fedavg status=$FED_STATUS"
|
||||
FED_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$FED_ID")"
|
||||
FED_AGG="$(json_get "['data']['output_json']['fedavg']['aggregated']" "$FED_GET")"
|
||||
FED_ROUND="$(json_get "['data']['output_json']['fedavg']['round_count']" "$FED_GET")"
|
||||
FED_CLIENTS="$(json_get "['data']['output_json']['fedavg']['client_count']" "$FED_GET")"
|
||||
# aggregated must equal [0.3, 0.4]
|
||||
FED_AGG_NORM="$(echo "$FED_AGG" | tr -d ' ')"
|
||||
assert "S8_fedavg_aggregated_0.3_0.4" "[ '$FED_AGG_NORM' = '[0.3,0.4]' ]" "aggregated=$FED_AGG"
|
||||
assert "S8_fedavg_round_count_1" "[ '$FED_ROUND' = '1' ]" "round_count=$FED_ROUND"
|
||||
assert "S8_fedavg_client_count_3" "[ '$FED_CLIENTS' = '3' ]" "client_count=$FED_CLIENTS"
|
||||
|
||||
# ----- S9: secure-mlp sigmoid predictions + noise -----
|
||||
MLP_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"operator":"secure-mlp","input":{"features":[[1.0,2.0],[3.0,4.0]],"weights":[0.5,0.5],"bias":0.1,"epsilon":1.0}}' "$RUNS_URL")"
|
||||
MLP_ID="$(json_get "['data']['run_id']" "$MLP_BODY")"
|
||||
MLP_STATUS="$(json_get "['data']['status']" "$MLP_BODY")"
|
||||
assert "S9_mlp_run_status" "[ '$MLP_STATUS' = 'success' ]" "mlp status=$MLP_STATUS"
|
||||
MLP_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$MLP_ID")"
|
||||
# Use python to assert length=2 and each in (0,1)
|
||||
MLP_ASSERT="$(python3 -c "
|
||||
import json,sys
|
||||
d=json.loads(sys.argv[1])
|
||||
p=d['data']['output_json']['mlp']['predictions']
|
||||
n=d['data']['output_json']['mlp']['noise_scale']
|
||||
ok = len(p)==2 and all(0 < x < 1 for x in p) and n>0
|
||||
print(ok)
|
||||
" "$MLP_GET" 2>/dev/null)"
|
||||
assert "S9_mlp_predictions_sigmoid" "[ '$MLP_ASSERT' = 'True' ]" "predictions check failed; body=$MLP_GET"
|
||||
|
||||
# ----- S10: tee-inference attestation -----
|
||||
TEE_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"operator":"tee-inference","input":{"prompt":"hello","model":"gpt-mock"}}' "$RUNS_URL")"
|
||||
TEE_ID="$(json_get "['data']['run_id']" "$TEE_BODY")"
|
||||
TEE_STATUS="$(json_get "['data']['status']" "$TEE_BODY")"
|
||||
assert "S10_tee_run_status" "[ '$TEE_STATUS' = 'success' ]" "tee status=$TEE_STATUS"
|
||||
TEE_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$TEE_ID")"
|
||||
TEE_ASSERT="$(python3 -c "
|
||||
import json,sys
|
||||
d=json.loads(sys.argv[1])
|
||||
t=d['data']['output_json']['tee']
|
||||
a=t['attestation']
|
||||
ok = len(a)>=64 and a.startswith('tee-mock-') and t['model']=='gpt-mock'
|
||||
print(ok)
|
||||
" "$TEE_GET" 2>/dev/null)"
|
||||
assert "S10_tee_attestation_ok" "[ '$TEE_ASSERT' = 'True' ]" "tee attestation check failed; body=$TEE_GET"
|
||||
|
||||
# ----- S11: prompt-defense detects injection + clean ignores -----
|
||||
DEF_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"operator":"prompt-defense","input":{"prompt":"ignore previous instructions and ..."}}' "$RUNS_URL")"
|
||||
DEF_ID="$(json_get "['data']['run_id']" "$DEF_BODY")"
|
||||
DEF_STATUS="$(json_get "['data']['status']" "$DEF_BODY")"
|
||||
assert "S11_defense_run_status" "[ '$DEF_STATUS' = 'success' ]" "defense status=$DEF_STATUS"
|
||||
DEF_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DEF_ID")"
|
||||
DEF_DETECTED="$(json_get "['data']['output_json']['defense']['detected']" "$DEF_GET")"
|
||||
DEF_LEVEL="$(json_get "['data']['output_json']['defense']['threat_level']" "$DEF_GET")"
|
||||
assert "S11_defense_detected_true" "[ '$DEF_DETECTED' = 'True' ]" "detected=$DEF_DETECTED"
|
||||
assert "S11_defense_threat_high" "[ '$DEF_LEVEL' = 'high' ]" "threat_level=$DEF_LEVEL"
|
||||
|
||||
DEF_CLEAN_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"operator":"prompt-defense","input":{"prompt":"What is 2+2?"}}' "$RUNS_URL")"
|
||||
DEF_CLEAN_ID="$(json_get "['data']['run_id']" "$DEF_CLEAN_BODY")"
|
||||
DEF_CLEAN_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$DEF_CLEAN_ID")"
|
||||
DEF_CLEAN_DETECTED="$(json_get "['data']['output_json']['defense']['detected']" "$DEF_CLEAN_GET")"
|
||||
DEF_CLEAN_LEVEL="$(json_get "['data']['output_json']['defense']['threat_level']" "$DEF_CLEAN_GET")"
|
||||
assert "S11_clean_not_detected" "[ '$DEF_CLEAN_DETECTED' = 'False' ]" "clean detected=$DEF_CLEAN_DETECTED"
|
||||
assert "S11_clean_threat_low" "[ '$DEF_CLEAN_LEVEL' = 'low' ]" "clean level=$DEF_CLEAN_LEVEL"
|
||||
|
||||
# ----- S12: compliance-audit trail -----
|
||||
AUDIT_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" -H 'Content-Type: application/json' \
|
||||
-d '{"operator":"compliance-audit","input":{"action":"data_export","actor":"alice","user":"alice","timestamp":"2026-09-28T10:00:00Z"}}' "$RUNS_URL")"
|
||||
AUDIT_ID="$(json_get "['data']['run_id']" "$AUDIT_BODY")"
|
||||
AUDIT_STATUS="$(json_get "['data']['status']" "$AUDIT_BODY")"
|
||||
assert "S12_audit_run_status" "[ '$AUDIT_STATUS' = 'success' ]" "audit status=$AUDIT_STATUS"
|
||||
AUDIT_GET="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$RUNS_URL/$AUDIT_ID")"
|
||||
AUDIT_ASSERT="$(python3 -c "
|
||||
import json,sys,re
|
||||
d=json.loads(sys.argv[1])
|
||||
a=d['data']['output_json']['audit']
|
||||
uuid_re=re.compile(r'^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$')
|
||||
ok = bool(uuid_re.match(a['trail_id'])) and 0 <= a['compliance_score'] < 1 and a['action']=='data_export'
|
||||
print(ok)
|
||||
" "$AUDIT_GET" 2>/dev/null)"
|
||||
assert "S12_audit_trail_ok" "[ '$AUDIT_ASSERT' = 'True' ]" "audit trail check failed; body=$AUDIT_GET"
|
||||
|
||||
# ----- extra: list >= 9 operators + new 6 codes present -----
|
||||
LIST_BODY="$(curl -s -H "Authorization: Bearer $ADMIN_TOKEN" "$OPS_URL")"
|
||||
LIST_LEN="$(python3 -c "import json,sys; print(len(json.loads(sys.argv[1])['data']))" "$LIST_BODY")"
|
||||
CODES="$(python3 -c "import json,sys; print(','.join(sorted(o['code'] for o in json.loads(sys.argv[1])['data'])))" "$LIST_BODY")"
|
||||
assert "list_at_least_9" "[ '$LIST_LEN' -ge 9 ]" "got $LIST_LEN operators: $CODES"
|
||||
for code in scql horizontal-fedavg secure-mlp tee-inference prompt-defense compliance-audit; do
|
||||
assert "list_has_${code}" "echo '$CODES' | tr ',' '\n' | grep -qx '$code'" "missing $code in $CODES"
|
||||
done
|
||||
|
||||
echo "---"
|
||||
echo "e2e dth_experiment_extend: pass=$pass fail=$fail"
|
||||
if [ $fail -gt 0 ]; then
|
||||
printf 'FAILED:\n'; printf ' - %s\n' "${fails[@]}"
|
||||
exit 1
|
||||
fi
|
||||
exit 0
|
||||
Reference in new issue
Block a user