---
name: Phase 3-1 backend integration design
description: 2026-05-07 ユーザー入力 invention の backend full engine 接続設計. 4 path 比較 (precomputed / Cloudflare Functions / frontend port / selective import). Phase 2 (4 engine MVP) → Phase 3-1 で backend 接続. Phase 3-2 永続化は別 path
type: project
originSessionId: ae11b294-84f2-4adc-a145-a3103fa436db
---
# Phase 3-1 backend integration design

## 背景

Phase 2a-e 完了 (commit 67942346):
- 🔮 lens MVP (5 lens, frontend heuristic)
- 📡 radar MVP (D-FUMT₈ + 14 OSS source + SEED 1573)
- ⚡ circuit MVP (8 ノード unified gate)
- 🧠 NNUE MVP (6 軸 heuristic 評価)
- 🌐 統合 Hub (4 engine 1 viewport, controlled mode)

未接続:
- mandala-supreme-engine.ts SUPREME_CLUSTERS (20+ cluster, E1-E19 engine)
- C0_MANDALA 10 engine (現在 frontend は 5 lens のみ実装, 残 5 は metadata only)
- 真の D-FUMT NNUE 評価 (2,560-dim backend, 既存)
- invention duplicate detection (build-invention.ts pipeline)

## 4 path 比較

### A. Precomputed JSON

**design**: 1573 SEED 全部に対し 10 lens 出力を build 時に pre-compute → JSON 保存. frontend は JSON 読み込みのみ.

| 軸 | 評価 |
|---|---|
| 工数 | 1 日 (pre-compute script + JSON serialize) |
| 動作 | ✅ 既存 1573 のみ |
| ユーザー新理論 | ❌ 不可 (precomputed 範囲外) |
| infra | ✅ 不要 (静的 hosting で OK) |
| build size | ⚠ +5-15 MB (1573 × 10 lens) |
| 保守 | ⚠ SEED 更新ごとに re-compute |

**判定**: Phase 3-1 推奨 path として **不採用** (ユーザー新理論不可は致命的).

### B. Cloudflare Pages Functions

**design**: Cloudflare Workers runtime で backend 実行. `functions/api/mandala-observe.ts` に entry. frontend → fetch → backend 計算 → JSON return.

| 軸 | 評価 |
|---|---|
| 工数 | 2-3 日 (Functions setup + backend port + test) |
| 動作 | ✅ ユーザー新理論対応 |
| infra | ⚠ Cloudflare Functions 設定必要 (free tier 100K req/day) |
| Node 互換 | ❌ fs / better-sqlite3 / native module 不可 |
| backend port | ⚠ pure function 部分のみ |
| Latency | ✅ Edge runtime ~50ms |

**判定**: 推奨 path. 但し fs / sqlite 依存は port 不可 → backend を browser-safe pure function に refactor 必要.

### C. Frontend port (full)

**design**: 既存 backend logic を frontend TypeScript に full 移植. Browser 内で実行.

| 軸 | 評価 |
|---|---|
| 工数 | 3-5 日 (各 engine 移植 + test) |
| 動作 | ✅ ユーザー新理論対応 |
| infra | ✅ 不要 |
| 同期保守 | ❌ backend 更新時 frontend も同期必須 (drift risk) |
| bundle size | ❌ +500KB-2MB |
| performance | ⚠ heavy compute は browser で遅い |

**判定**: 推奨しない (drift risk + bundle size).

### D. Selective import (今 phase で partial 実施)

**design**: backend module から **browser-safe pure function + metadata** だけ import. fs / Node API 依存箇所は除外.

| 軸 | 評価 |
|---|---|
| 工数 | 1-2 日 (browser-safe layer 設計) |
| 動作 | ⚠ 部分のみ (full engine は不可) |
| infra | ✅ 不要 |
| bundle size | ✅ 限定的 (metadata 中心) |
| 同期 | ✅ 同 repo 内で integration |

**判定**: Phase 3-1 partial で selective import を実施. SUPREME_CLUSTERS metadata は browser-safe → 直接 import 可能 (`src/renderer/data/mandala-c0-lenses.ts` 経由).

## 推奨 plan

### Phase 3-1 partial (今 turn 完了 / commit-pending)

- ✅ `src/renderer/data/mandala-c0-lenses.ts` 作成 (10 lens metadata)
- ✅ YourTheoryMandala に L6-L10 metadata 表示 (activation heuristic)
- ✅ 各 lens に "Phase 3-1 で full 実装予定" badge

### Phase 3-1 full (将来 STEP, ~2-3 日)

**path B (Cloudflare Functions)** で実装:

1. `functions/api/mandala-observe.ts` entry 作成
2. backend `mandala-supreme-engine.ts` を refactor:
   - fs / sqlite 依存箇所を identify
   - browser-safe pure function に分離
3. frontend → fetch /api/mandala-observe { theoryName, theoryDetail } → backend 計算 → JSON return
4. YourTheoryMandala で fetch + 結果表示
5. 同様 path で circuit / NNUE backend 接続

**Latency 目標**: Cloudflare Edge ~50-200ms / 真の D-FUMT NNUE 2,560-dim も可能 (model file < 25 MB なら edge 配信可)

### Phase 3-2 永続化 (今 turn 完了 / commit-pending)

- ✅ `InventionExportButton.tsx` 作成
- ✅ JSON ダウンロード (invented-{date}-userdef-{slug}.json)
- ✅ GitHub Issue link 自動生成 (pre-filled title + body)
- ✅ 認証不要 (両 path)

### Phase 3-2 自動 PR (将来 STEP)

Cloudflare Functions + GitHub API で:
1. ユーザー submit → Functions endpoint
2. PAT で GitHub API 呼び出し
3. `data/invention/pending/*.json` に branch + PR 自動作成
4. 藤本さん review → merge → 自動 SEED 追加 trigger

## 技術 risk

| risk | likelihood | mitigation |
|---|---|---|
| Cloudflare Functions の Node 互換 limit | high | pure function に refactor |
| backend module の fs 依存度 | medium | identify + 分離 (browser-safe layer) |
| bundle size 爆発 | low | selective import で制御 |
| performance regression | medium | Edge runtime + selective compute |
| invention duplicate check の collision | low | backend で実施 (memory feedback_invention_duplicate_prevention.md) |

## 関連 memory

- `feedback_dictionary_datacenter_dual_vision.md` ★★★★★ — 質+量 両立 vision (Phase 3-1 で full 実装が両立に近づく)
- `feedback_invention_duplicate_prevention.md` ★★★ — invention pipeline duplicate check
- `feedback_phase_c_silicon_existence_claim.md` ★★★★★ — computational vs physical reality 区別 (Phase 3-1 は computational only)
- `feedback_index_html_bundle_sync.md` ★★★★★ — Cloudflare Pages deploy pattern

## process check

Phase 3-1 full 着手前:
- [ ] backend module の fs / sqlite 依存度 audit
- [ ] browser-safe layer 設計 document
- [ ] Cloudflare Functions setup (新 directory `functions/`)
- [ ] free tier limit 確認 (100K req/day で十分か)
- [ ] performance baseline 測定 (Edge runtime latency)
