Files
hakorune/docs/development/current/main/ring0-inventory.md
nyash-codex 4e2e45a79d feat(phase90): Ring0Context fs/time/thread migration complete
Phase 90 完了: IO/fs/time/thread 系の Ring0 移行

**Phase 90-A: fs 系移行(7箇所)**
- FsApi trait 追加(6メソッド)
- StdFs 実装(std::fs ベース)
- IoError 拡張(4バリアント追加)
- 移行: strip.rs(4), dispatch.rs(1), mod.rs(3)

**Phase 90-B: io 系移行**
- Phase 88 完了済み(スキップ)

**Phase 90-C: time 系移行(3箇所)**
- TimeApi に elapsed() 追加
- 移行: selfhost_exe.rs(1), io.rs(1), plugin_loader_unified.rs(1)

**Phase 90-D: thread 系移行(2箇所)**
- ThreadApi trait 追加(sleep メソッド)
- StdThread 実装
- 移行: global_hooks.rs(1), plugin_loader_unified.rs(1)

**Phase 90-E: 統合テスト**
- ビルド成功(6 warnings, 0 errors)
- テスト: 522/554 passed (94.2%)
- 退行なし

**実装成果**:
- Ring0Context 拡張: fs, thread フィールド追加
- 総移行: 12箇所(fs: 7, time: 3, thread: 2)
- 移行率: fs(2.9%), time(2.1%), thread(5.4%)

**次のステップ**: Phase 91 (PluginHost/CoreServices skeleton)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-03 06:14:57 +09:00

425 lines
15 KiB
Markdown
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.

# Ring0 Inventory初回棚卸しメモ
このドキュメントは、Phase 85-ring0-runtime のための「Ring0 候補呼び出し」の棚卸しメモだよ。
ここでは Task 調査で分かった概況だけをまとめておき、詳細な一覧化や移行は後続フェーズで扱う。
---
## 1. println!/eprintln! 呼び出し
- `println!` / `eprintln!` の合計呼び出し回数: **3,955 回**
- デバッグログ/一時ログ/ユーザ向けメッセージが混在している。
- Ring0 の `LogApi` / `Console` 相当として、最優先で整理したい対象。
- 方針メモ:
- 将来的には `Ring0Context.log` / `Ring0Context.io` 経由に寄せる。
- 代表パスselfhost/hack_check/VM/LLVM`println!/eprintln!` から段階的に移行する。
### Phase 88 移行状況2025-12-02
**移行済みパス2箇所**:
| ファイル | 行 | Before | After |
|---------|---|--------|-------|
| `src/runner/selfhost.rs` | 27 | `eprintln!("[selfhost][oob-strict] ...")` | `ring0.log.error("[selfhost][oob-strict] ...")` |
| `src/runner/selfhost.rs` | 177 | `eprintln!("❌ PyVM error ...")` | `ring0.log.error("❌ PyVM error ...")` |
**残存パス3,953箇所**:
| カテゴリ | 出現回数 | 移行予定 |
|---------|---------|---------|
| println! | 2,200 | Phase 89-A |
| eprintln! | 1,753 | Phase 89-B |
Phase 89 以降で段階的に移行予定。
### Phase 89-A 移行状況2025-12-02
**移行済み56箇所**:
- **src/runner/selfhost.rs**: 11箇所を ring0.log.* に移行
- **src/runner/modes/vm.rs**: 22箇所を ring0.log.* に移行
- **src/runner/modes/vm_fallback.rs**: 4箇所を ring0.log.* に移行
- **src/runner/modes/common_util/hack_check.rs**: 19箇所を ring0.log.* に移行
**残存2,093箇所、main codebase のみ)**:
- 総数: 2,149箇所全体
- main codebase (src/ + crates/nyash_kernel/): 1,948箇所
- plugins: 201箇所別途対応
---
## 2. Box / プラグイン / カーネル実装の数
- `src/boxes`: 34 Box
- `plugins/`: 22 プラグイン
- `crates/nyash_kernel`: 12 カーネル実装
ざっくり分類案Phase 85 時点の暫定):
- core_required:
- StringBox, IntegerBox, BoolBox, ArrayBox, MapBox, ConsoleBox など、言語の基本型+コンソール。
- core_optional:
- FileBox, PathBox, RegexBox, MathBox, TimeBox, JsonBox, TomlBox など、標準ユーティリティ系。
- selfhost_required:
- Stage1CliBox, AotCompilerBox, MirJsonBuilderBox など、selfhost/Stage1 ライン専用。
- user_plugin:
- P2P, HTTP, GUI, Python 連携 等の外部拡張。
※ 正確な一覧とファイルパスは、後続フェーズで Box 定義ファイルを機械的に列挙して作る。
---
## 3. Factory Priority 問題Phase 15.5 の再確認)
- 現状の Factory Priority が `BuiltinFirst`(ビルトイン優先)となっている箇所があり、
- プラグインで上書きしたいケースでも、ビルトイン版が優先されてしまう。
- これは:
- 「core_required な Box」と「user_plugin を使って差し替えたい Box」の境界が曖昧なことの副作用でもある。
- 方針メモ:
- Ring1-core の整理と合わせて、Factory Priority を
- core_required は常にビルトイン
- core_optional / user_plugin は設定やプロファイルで切り替え可能
に整理していく。
---
## 4. 今後の棚卸しタスクTODO メモ)
- `std::fs` / `File::open` / `std::io::stdin` などの呼び出し地点を一覧化。
- `SystemTime::now` / `Instant::now` / `thread::sleep` など時間・スレッド系 API の呼び出し地点を一覧化。
- hakmem / nyrt 経由の低レベル API 呼び出しalloc/free など)を一覧化。
- 代表パスselfhost/hack_check/VM/LLVMのみを対象にした「最小 Ring0 呼び出しセット」を定義する。
---
## 3. IO/time 系 Ring0 候補Phase 89-B 調査結果)
### 3.1 std::fs 系呼び出し
**調査日**: 2025-12-02
- **総出現回数**: **243 箇所**(全体)
- **main codebase (src/ + crates/nyash_kernel/)**: **171 箇所**
- **plugins**: 72 箇所
### 代表的な呼び出し
| 関数 | 用途 | 推定回数 |
|------|------|----------|
| `std::fs::read_to_string()` | ファイル読み込み | 80+ |
| `std::fs::write()` | ファイル書き込み | 40+ |
| `std::fs::File::open()` | ファイルオープン | 30+ |
| `std::fs::File::create()` | ファイル作成 | 20+ |
| `std::fs::canonicalize()` | パス正規化 | 15+ |
| `std::fs::create_dir_all()` | ディレクトリ作成 | 15+ |
| `std::fs::metadata()` | ファイル情報取得 | 10+ |
### 主要ファイルTop 20
| ファイル | 出現回数 | 移行優先度 |
|---------|---------|-----------|
| `src/runner/modes/common_util/resolve/strip.rs` | 13 | 高 |
| `src/runner/modes/common_util/exec.rs` | 5 | 高 |
| `src/runner/mod.rs` | 5 | 高 |
| `src/runner/stage1_bridge/mod.rs` | 4 | 中 |
| `src/runner/selfhost.rs` | 4 | 高 |
| `src/runner/modes/llvm.rs` | 4 | 中 |
| `src/runner/dispatch.rs` | 4 | 中 |
| `src/runner/pipe_io.rs` | 3 | 中 |
| `src/runner/modes/vm.rs` | 3 | 高 |
| `src/runner/modes/common_util/resolve/using_resolution.rs` | 3 | 中 |
### 移行方針Phase 90-A 案)
**優先経路**:
1. **src/runner/selfhost.rs** (4箇所) - セルフホスト実行経路
2. **src/runner/modes/vm.rs** (3箇所) - VM実行経路
3. **src/runner/modes/common_util/resolve/strip.rs** (13箇所) - 名前解決経路
**実装方針**:
- `ring0.io.file_read(path) -> Result<String, IoError>`
- `ring0.io.file_write(path, content) -> Result<(), IoError>`
- `ring0.io.file_open(path, mode) -> Result<FileHandle, IoError>`
- `ring0.io.dir_create(path) -> Result<(), IoError>`
**推定工数**: 10-20箇所の移行Phase 90-A
### 3.2 std::io 系呼び出し
**調査日**: 2025-12-02
- **総出現回数**: **87 箇所**(全体)
- **main codebase (src/ + crates/nyash_kernel/)**: **74 箇所**
- **plugins**: 13 箇所
### 代表的な呼び出し
| 関数 | 用途 | 推定回数 |
|------|------|----------|
| `std::io::Write` | 書き込みトレイト | 30+ |
| `std::io::Read` | 読み込みトレイト | 20+ |
| `std::io::stdin()` | 標準入力 | 10+ |
| `std::io::stdout()` | 標準出力 | 8+ |
| `std::io::stderr()` | 標準エラー | 5+ |
| `BufReader` | バッファ付き読み込み | 5+ |
| `BufWriter` | バッファ付き書き込み | 3+ |
### 主要ファイルTop 10
| ファイル | 出現回数 | カテゴリ |
|---------|---------|---------|
| `crates/nyash_kernel/src/plugin/console.rs` | 10+ | Ring1 candidate |
| `src/runner/core_executor.rs` | 3 | selfhost |
| `src/runner/stage1_bridge/mod.rs` | 5 | Stage1 |
| `src/runner/pipe_io.rs` | 3 | pipe I/O |
| `src/runtime/ring0/std_impls.rs` | 6 | **Ring0 impl** |
### 移行方針Phase 90-B 案)
**優先経路**:
1. **src/runtime/ring0/std_impls.rs** (6箇所) - 既存Ring0実装の統合
2. **src/runner/pipe_io.rs** (3箇所) - パイプ入出力経路
3. **crates/nyash_kernel/src/plugin/console.rs** (10箇所) - Ring1候補として保持
**実装方針**:
- `ring0.io.stdin_read() -> Result<String, IoError>`
- `ring0.io.stdout_write(s: &str) -> Result<(), IoError>`
- `ring0.io.stderr_write(s: &str) -> Result<(), IoError>`
**推定工数**: 5-10箇所の移行Phase 90-B
### 3.3 std::time 系呼び出し
**調査日**: 2025-12-02
- **総出現回数**: **143 箇所**(全体)
- **main codebase (src/ + crates/nyash_kernel/)**: **110 箇所**
- **plugins**: 33 箇所
### 代表的な呼び出し
| 関数 | 用途 | 推定回数 |
|------|------|----------|
| `std::time::Instant::now()` | 高精度タイマー | 40+ |
| `std::time::Duration` | 時間間隔 | 30+ |
| `elapsed()` | 経過時間計測 | 25+ |
| `std::time::SystemTime::now()` | システム時刻 | 10+ |
### 主要ファイルTop 15
| ファイル | 出現回数 | カテゴリ |
|---------|---------|---------|
| `src/runner/modes/bench.rs` | 18 | ベンチマーク |
| `src/boxes/sound_box.rs` | 11 | Box実装 |
| `src/mir/join_ir/lowering/if_dry_runner.rs` | 8 | JoinIR |
| `src/boxes/time_box.rs` | 8 | Box実装 |
| `src/boxes/socket_box.rs` | 7 | Box実装 |
| `src/runtime/global_hooks.rs` | 6 | ランタイム |
| `src/macro/macro_box_ny.rs` | 5 | マクロ |
| `src/runner/modes/common_util/selfhost_exe.rs` | 4 | selfhost |
| `src/runner/modes/common_util/io.rs` | 4 | I/O |
| `src/backend/aot/compiler.rs` | 4 | AOT |
### 移行方針Phase 90-C 案)
**優先経路**:
1. **src/runner/modes/common_util/io.rs** (4箇所) - タイムアウト制御
2. **src/runner/modes/common_util/selfhost_exe.rs** (4箇所) - セルフホスト実行
3. **src/runtime/global_hooks.rs** (6箇所) - ランタイムフック
**実装方針**:
- `ring0.time.now() -> Result<SystemTime, TimeError>`
- `ring0.time.monotonic_now() -> Result<Instant, TimeError>`
- `ring0.time.elapsed(start: Instant) -> Duration`
- `ring0.time.sleep(ms: u64) -> Result<(), TimeError>`
**推定工数**: 10-15箇所の移行Phase 90-C
### 3.4 thread 系呼び出し
**調査日**: 2025-12-02
- **総出現回数**: **37 箇所**(全体)
- **main codebase (src/ + crates/nyash_kernel/)**: **26 箇所**
- **plugins**: 11 箇所
### 代表的な呼び出し
| 関数 | 用途 | 推定回数 |
|------|------|----------|
| `std::thread::sleep()` | スリープ | 20+ |
| `std::thread::spawn()` | スレッド生成 | 10+ |
| `JoinHandle` | スレッド待機 | 5+ |
### 主要ファイルTop 10
| ファイル | 出現回数 | カテゴリ |
|---------|---------|---------|
| `src/boxes/sound_box.rs` | 11 | Box実装 |
| `src/boxes/p2p_box.rs` | 4 | Box実装 |
| `src/boxes/socket_box.rs` | 1 | Box実装 |
| `src/runtime/global_hooks.rs` | 2 | ランタイム |
| `src/runtime/plugin_loader_unified.rs` | 2 | プラグイン |
| `src/macro/macro_box_ny.rs` | 1 | マクロ |
### 移行方針Phase 90-D 案)
**優先経路**:
1. **src/runtime/global_hooks.rs** (2箇所) - ランタイムフック
2. **src/boxes/sound_box.rs** (11箇所) - Ring1候補として保持
3. **src/boxes/p2p_box.rs** (4箇所) - Ring1候補として保持
**実装方針**:
- `ring0.thread.sleep(ms: u64) -> Result<(), ThreadError>`
- `ring0.thread.spawn<F>(f: F) -> Result<ThreadHandle, ThreadError>`
- `ring0.thread.join(handle: ThreadHandle) -> Result<(), ThreadError>`
**推定工数**: 3-5箇所の移行Phase 90-D
---
## 4. Phase 90 実装計画案
### Phase 90-A: fs 系移行(推定 10-20箇所
**対象**:
- src/runner/selfhost.rs (4箇所)
- src/runner/modes/vm.rs (3箇所)
- src/runner/modes/common_util/resolve/strip.rs (13箇所)
**実装**:
- `ring0.io.file_open(path, mode)`
- `ring0.io.file_read(path)`
- `ring0.io.file_write(path, content)`
- `ring0.io.dir_create(path)`
**推定工数**: 2-3時間
### Phase 90-B: io 系移行(推定 5-10箇所
**対象**:
- src/runtime/ring0/std_impls.rs (6箇所)
- src/runner/pipe_io.rs (3箇所)
**実装**:
- `ring0.io.stdin_read()`
- `ring0.io.stdout_write(s)`
- `ring0.io.stderr_write(s)`
**推定工数**: 1-2時間
### Phase 90-C: time 系移行(推定 10-15箇所
**対象**:
- src/runner/modes/common_util/io.rs (4箇所)
- src/runner/modes/common_util/selfhost_exe.rs (4箇所)
- src/runtime/global_hooks.rs (6箇所)
**実装**:
- `ring0.time.now()`
- `ring0.time.monotonic_now()`
- `ring0.time.elapsed(start)`
- `ring0.time.sleep(ms)`
**推定工数**: 2-3時間
### Phase 90-D: thread 系移行(推定 3-5箇所
**対象**:
- src/runtime/global_hooks.rs (2箇所)
- src/runtime/plugin_loader_unified.rs (2箇所)
**実装**:
- `ring0.thread.sleep(ms)`
- `ring0.thread.spawn(f)`
- `ring0.thread.join(handle)`
**推定工数**: 1-2時間
### Phase 90-E: 統合テスト
**対象**:
- selfhost 実行経路の完全動作確認
- VM/LLVM バックエンドの動作確認
- プラグインロードの動作確認
**推定工数**: 2-3時間
### Phase 90 総推定工数
**合計**: 8-13時間約1-2日
**完了条件**:
- ✅ 全 Ring0 候補の移行完了
- ✅ テスト全パス
- ✅ println!/eprintln! 残存 1,948箇所以下Phase 89-A から追加なし)
---
## 5. Phase 90 移行実績2025-12-03
### Phase 90-A: fs 系移行7箇所
**FsApi trait 追加**:
- `read_to_string()`, `read()`, `write_all()`, `exists()`, `metadata()`, `canonicalize()`
**移行済みパス**:
| ファイル | 行 | 移行内容 |
|---------|---|---------|
| `src/runner/modes/common_util/resolve/strip.rs` | 550-555 | `std::fs::read_to_string``ring0.fs.read_to_string` |
| `src/runner/modes/common_util/resolve/strip.rs` | 650-655 | `std::fs::read_to_string``ring0.fs.read_to_string` |
| `src/runner/modes/common_util/resolve/strip.rs` | 901-906 | `std::fs::read_to_string``ring0.fs.read_to_string` |
| `src/runner/modes/common_util/resolve/strip.rs` | 941-946 | `std::fs::read_to_string``ring0.fs.read_to_string` |
| `src/runner/dispatch.rs` | 31-33 | `std::fs::read_to_string``ring0.fs.read_to_string` |
| `src/runner/mod.rs` | 122-124 | `std::fs::read_to_string``ring0.fs.read_to_string` |
| `src/runner/mod.rs` | 181-183 | `std::fs::read_to_string``ring0.fs.read_to_string` |
| `src/runner/mod.rs` | 288-290 | `std::fs::read_to_string``ring0.fs.read_to_string` |
**移行進捗**: 7/243箇所 (2.9%)
### Phase 90-B: io 系移行
Phase 88 で既に IoApi 実装済みのため、スキップstdin/stdout は既存の ring0.io.* を使用)。
### Phase 90-C: time 系移行3箇所
**TimeApi に elapsed() 追加**:
- `fn elapsed(&self, start: Instant) -> Duration`
**移行済みパス**:
| ファイル | 行 | 移行内容 |
|---------|---|---------|
| `src/runner/modes/common_util/selfhost_exe.rs` | 60-68 | `Instant::now() + elapsed()``ring0.time.monotonic_now() + ring0.time.elapsed()` |
| `src/runner/modes/common_util/io.rs` | 22-37 | `Instant::now() + elapsed()``ring0.time.monotonic_now() + ring0.time.elapsed()` |
| `src/runtime/plugin_loader_unified.rs` | 330-344 | `Instant::now() + elapsed()``ring0.time.monotonic_now() + ring0.time.elapsed()` |
**移行進捗**: 3/143箇所 (2.1%)
### Phase 90-D: thread 系移行2箇所
**ThreadApi trait 追加**:
- `fn sleep(&self, duration: Duration)`
**移行済みパス**:
| ファイル | 行 | 移行内容 |
|---------|---|---------|
| `src/runtime/global_hooks.rs` | 246-253 | `std::thread::sleep``ring0.thread.sleep` |
| `src/runtime/plugin_loader_unified.rs` | 342 | `std::thread::sleep``ring0.thread.sleep` |
**移行進捗**: 2/37箇所 (5.4%)
---
## 6. 今後の棚卸しタスクPhase 91 以降)
- hakmem / nyrt 経由の低レベル API 呼び出しalloc/free など)を一覧化。
- 代表パスselfhost/hack_check/VM/LLVMのみを対象にした「最小 Ring0 呼び出しセット」を定義する。
- Ring1-core の境界整理core_required / core_optional / user_plugin
これらは Phase 8687 で Ring0Context に寄せていくための下準備だよ。