---
name: mandala-supreme-engine.ts backend audit (Phase 3-1 full 準備)
description: 2026-05-07 Phase 3-1 full backend 接続前の fs/sqlite 依存度 audit summary. mandala-supreme-engine.ts + 4 engine deps の Cloudflare Workers 互換性評価. browser-safe layer 分離 plan
type: project
originSessionId: ae11b294-84f2-4adc-a145-a3103fa436db
---
# mandala-supreme-engine.ts backend audit

## 対象 file

`src/axiom-os/mandala-supreme-engine.ts` (1505+ lines)

## import 依存度 audit (Edge runtime 互換性)

### ✅ Browser-safe (Cloudflare Workers OK)

| line | import | status | 備考 |
|---|---|---|---|
| 78 | `EightLogicValue from './seven-logic'` | ✅ | type only / pure |
| 79 | `SeedTheory from './seed-kernel'` | ⚠ | type は OK / 但し seed-kernel.ts に fs 依存ある可能性 (要確認) |
| 144 | `export const SUPREME_CLUSTERS: ClusterDefinition[]` | ✅ | 静的 metadata / Phase 3-1 partial で流用済 |
| 710 | `export const ALL_SHADE_FEATURES` | ✅ | 静的 |
| 729 | `export const ZFC_AXIOMS` | ✅ | 静的 |

### ❌ Node 専用 (Edge runtime 不可)

| line | import | 用途推定 | mitigation |
|---|---|---|---|
| 513 | `execSync from 'child_process'` | shell command 実行 | ❌ Edge 完全不可. Functions では使わない layer に |
| 514 | `import * as fs from 'fs'` | file 読み書き | ⚠ data file inline 化 or KV store で代替 |
| 515 | `import * as path from 'path'` | path manipulation | ✅ pure function `path` 部分は Web 互換 (URL API で代替可) |
| 516 | `PortfolioRacingEngine` | 並列 solver | ⚠ 依存 audit 必要 |
| 517 | `AlphaEvolveEngine` | 進化的探索 | ⚠ 依存 audit 必要 |
| 518 | `runCEGIS from './cegis-lyapunov-ulam-engine'` | Lyapunov 降下判定 | ⚠ 依存 audit 必要 |
| 519 | `computeSiegelNumen, productSpaceOrbit from './siegel-padic-spectral-engine'` | Siegel χ_H + p-adic | ⚠ 依存 audit 必要 |

### ⚠ Pure function 候補 (要詳細 audit)

| line | export | browser-safe? |
|---|---|---|
| 745 | `function classifyAxiomActivations` | 要内部確認 (fs 使わない なら ✅) |
| 808 | `function formatActivationLog` | 要内部確認 |
| 823 | `function problemAwareShade` | 要内部確認 (4 engine 呼ぶ なら ❌ — 依存先 Node 専用) |
| 1481 | `function weightedMajorityVote` | 要内部確認 (純 算術 なら ✅) |
| 1505 | `class MandalaSupremeEngine` | constructor で fs 使うなら ❌ |

## audit verdict

honest 結論:

**mandala-supreme-engine.ts を full Cloudflare Functions 移植は重い** (~1-2 日 refactor).

理由:
1. `child_process` `fs` `path` 直接依存
2. 4 engine (PortfolioRacing / AlphaEvolve / CEGIS / Siegel) の transitive dependency も Node 専用の可能性高
3. SUPREME_CLUSTERS 静的 metadata は Phase 3-1 partial で **既に流用済** (重複作業避ける)

## 段階的 refactor plan

### Step 1 (Turn N+1): browser-safe layer 抽出

新 file: `src/axiom-os/mandala-browser-safe.ts`
- `classifyAxiomActivations` (fs 使わない部分) を copy
- `weightedMajorityVote` (純算術) を copy
- `EightLogicValue` `SeedTheory` type re-export
- 4 engine import なし (Node 依存)

**SeedTheory data**:
- 現状 `data/seed-kernel-summary.json` (~625 KB) として既に JSON 化済
- frontend では fetch + parse で取得 (browser fetch API で OK)

### Step 2 (Turn N+1 後半): functions/api/mandala-observe.ts 実装

```ts
import { classifyAxiomActivations, weightedMajorityVote } from '../../src/axiom-os/mandala-browser-safe';

export const onRequestPost: PagesFunction = async (context) => {
  const { theoryName, theoryDetail, theoryKeywords } = await context.request.json();
  // 1. SEED summary を fetch (Cloudflare Pages 静的配信から)
  const seedRes = await fetch(new URL('/data/seed-kernel-summary.json', context.request.url).toString());
  const seeds = await seedRes.json();
  // 2. browser-safe function で 評価
  const result = classifyAxiomActivations({ theoryName, theoryDetail, theoryKeywords, seeds });
  return new Response(JSON.stringify(result), { ... });
};
```

### Step 3 (Turn N+2): frontend 接続 + fallback

YourTheoryMandala で:
```ts
const [backendResult, setBackendResult] = useState(null);
useEffect(() => {
  if (!submitted) return;
  fetch('/api/mandala-observe', { method: 'POST', body: JSON.stringify({...}) })
    .then(r => r.ok ? r.json() : null)
    .then(setBackendResult)
    .catch(() => { /* fallback to heuristic */ });
}, [submitted, theoryName, theoryDetail]);
```

backend 成功 → backend 結果使用 / 失敗 → 既存 heuristic (Phase 2a) fallback.

### Step 4 (Turn N+3): 4 engine の deeper integration

- PortfolioRacing / AlphaEvolve / CEGIS / Siegel の各 engine を同 pattern で audit
- pure function 部分のみ抽出
- Cloudflare Workers で動かない部分は **fallback メッセージ** で正直開示
- 完全 backend 移植は long-term work

### Step 5 (Turn N+4): invention-submit + GitHub PAT (Phase 3-2 full)

- 別 endpoint `/api/invention-submit`
- backend audit と独立 (GitHub API 呼ぶだけ)
- audit 結果に依存しないので並行可能

## next turn での実装

Turn N+1 (推奨実装順):
1. `src/axiom-os/mandala-browser-safe.ts` 作成 (pure function 抽出)
2. `functions/api/mandala-observe.ts` 実装 (skeleton)
3. frontend YourTheoryMandala で fetch + fallback 実装
4. test (curl /api/mandala-observe で動作確認)

## risk

| risk | likelihood | mitigation |
|---|---|---|
| pure function 抽出時に依存漏れ | high | TypeScript build で error 出る → 段階的 fix |
| Cloudflare bundle size > 1MB | medium | data file は static 配信 / function は logic only |
| Edge runtime 不対応 API | medium | Web standard だけ使う / Node API は polyfill しない |
| 4 engine 全 port 不能 | high | 部分 port + 残りは Phase 4 (long-term) で local backend or wasm |

## 関連 memory

- `project_phase_3_backend_integration_design.md` ★★★★ — 4 path 比較
- `feedback_visitor_analytics_stance.md` ★★★★ — Cloudflare Pages 採用根拠 (Functions と同じ infra)
- `feedback_phase_c_silicon_existence_claim.md` ★★★★★ — computational vs physical reality 区別

## 公開可否

この audit 結果は **public memory として公開可能** (codebase 構造 audit は OUKC commons 性質と整合).
