feat(joinir): Phase 53 - SELFHOST-NORM-DEV-EXPAND implementation

Expanded selfhost dev Normalized target with 2 practical P2/P3 loop variations,
strengthened structural signature axis, and implemented two-stage detection.

Key Changes:

1. Documentation (phase49-selfhost-joinir-depth2-design.md +128 lines):
   - Added Phase 53 section with candidate selection rationale
   - Documented two-stage detector strategy (structural primary + dev-only name guard)
   - Defined structural axis strengthening (carrier count/type, branch patterns)

2. Fixtures (+210 lines):
   - selfhost_args_parse_p2.program.json (60 lines): P2 with String carrier + conditional branching
   - selfhost_stmt_count_p3.program.json (150 lines): P3 with 5 carriers + multi-branch if-else

3. Structured Builders (fixtures.rs +48 lines):
   - build_selfhost_args_parse_p2_structured_for_normalized_dev()
   - build_selfhost_stmt_count_p3_structured_for_normalized_dev()

4. ShapeGuard Two-Stage Detection (shape_guard.rs +80 lines):
   - Added SelfhostArgsParseP2/SelfhostStmtCountP3 to NormalizedDevShape enum
   - Implemented is_selfhost_args_parse_p2(): P2 core family + name guard
   - Implemented is_selfhost_stmt_count_p3(): 2-10 carrier check + name guard
   - Updated capability_for_shape() mappings

5. Bridge Integration (bridge.rs +8 lines, normalized.rs +10 lines):
   - Added shape handlers delegating to existing normalizers
   - Added roundtrip reconstruction handlers

6. Entry Point Registration (ast_lowerer/mod.rs +2 lines):
   - Registered selfhost_args_parse_p2/selfhost_stmt_count_p3 as LoopFrontend routes

7. Dev VM Comparison Tests (normalized_joinir_min.rs +40 lines):
   - normalized_selfhost_args_parse_p2_vm_bridge_direct_matches_structured()
   - normalized_selfhost_stmt_count_p3_vm_bridge_direct_matches_structured()

8. Test Context Fix (dev_env.rs):
   - Added thread-local test context depth counter
   - Fixed deadlock in nested test_ctx() calls via reentrant with_dev_env_if_unset()

Structural Axis Growth:

P2 family:
- Carrier count: 1-3 (unchanged)
- NEW: Type diversity (Integer/String mixed)
- NEW: Conditional branching patterns (Eq-heavy comparisons)

P3 family:
- NEW: Carrier count upper bound: 2-10 (was 2-4)
- NEW: Multi-branch if-else (5+ branches with nested structure)
- NEW: Complex conditional patterns

Test Results:
- normalized_dev: 40/40 PASS (including 2 new tests)
- lib regression: 939 PASS, 56 ignored
- Existing behavior unchanged (normalized_dev feature-gated)

Phase 53 Achievements:
 P2/P3 each gained 1 practical variation (2 total)
 Two-stage detection: structural primary + dev-only name guard
 Structural axis expanded: 4 axes (carrier count/type/Compare/branch patterns)
 All tests PASS, no regressions
 Test context deadlock fixed (0.04s for 29 tests)

Files Modified: 14 files
Lines Added: ~516 lines (net)
Implementation: Pure additive (feature-gated)

Next Phase (54+):
- Accumulate 6+ loops per P2/P3 family
- Achieve 5+ stable structural axes
- Target < 5% false positive rate
- Then shrink/remove name guard scope
This commit is contained in:
nyash-codex
2025-12-12 16:40:20 +09:00
parent 386cbc1915
commit 7b0db59100
14 changed files with 1580 additions and 64 deletions

View File

@ -1416,7 +1416,7 @@ Pattern3 (if-sum) ループを Normalized JoinIR に対応させる。P2 と同
**スコープ外**: P4 (continue) 対応NORM-P4 フェーズで実施、Complex P3 patterns後続フェーズ
### 3.25 Phase 48-NORM-P4 Normalized P4 (Continue) 🏗️ DESIGN + PHASE 48-A MINIMAL DEV COMPLETE (2025-12-12)
### 3.25 Phase 48-NORM-P4 Normalized P4 (Continue) 🏗️ DESIGN + PHASE 48-A/B/C CANONICAL COMPLETE (2025-12-12 → 2026-01-XX)
**設計詳細 / 実装サマリ**: [phase48-norm-p4-design.md](./phase48-norm-p4-design.md)
@ -1442,5 +1442,31 @@ P4 (continue) は P1/P2/P3 と同じ `loop_step(env, k_exit)` 骨格を使う設
Structured→Normalized→MIR(direct) vs Structured→MIR / runner / VM bridge
- `cargo test --release` ベースで **939/939 tests PASS**Phase 48-A 実装時点)
**Phase 48 doc is SSOT** for P4 Normalized design + 48-A 実装サマリだよ。
Phase 48-Bmulti-carrier / string ops 拡張)と 48-Ccanonical 昇格)は今後のフェーズで扱う。
**Phase 48-BJsonParser continue skip_ws、dev-only実装ステータス**:
- Fixtures: `jsonparser_parse_array_continue_skip_ws.program.json` / `jsonparser_parse_object_continue_skip_ws.program.json`
- ShapeGuard: JsonParser continue ループ用の shape を追加array/object 両方)
- Normalized lowering: `normalize_jsonparser_parse_array_continue_skip_ws` / `_parse_object_...` で Structured→Normalized→MIR(direct) を dev 比較
- テスト: normalized dev スイートに VM bridge 比較テストを追加Structured 直経路と stdout 一致を確認)
**Phase 48-Ccanonical 昇格)実装ステータス**:
- Canonical set 拡張: Pattern4 continue minimal / JsonParser skip_ws array/object を `is_canonical_shape()` に追加
- Bridge/runner: P4 canonical shapes は env 無しでも Normalized→MIR(direct) を必ず通るStructured fallback 無し、fail-fast
- テスト: canonical ルートenv OFFと Structured 直経路の stdout 一致を比較するテストを追加
**Phase 48 doc is SSOT** for P4 Normalized design + 48-A/B/C サマリだよ。
P1〜P4 の代表ループがすべて canonical Normalized パイプラインに載った状態になった。
### 3.26 Phase 49-SELFHOST-NORM-DEPTH2 Selfhost depth2 Normalized 設計フェーズdocsのみ
- SSOT: [phase49-selfhost-joinir-depth2-design.md](./phase49-selfhost-joinir-depth2-design.md)
- 目的: selfhost ラインでも `.hako → Program/MIR JSON → JoinIR(Structured) → Normalized → MIR → VM/LLVM` の depth2 パイプラインを踏めるように、対象ループ(軽い P2/P3 を 1〜2 本)と適用方針を設計で固定する。
- スコープ: selfhost の P2/P3 軽量ループのみ(トークン走査系 P2・if-sum 系 P3 を候補化。heavy ループや P5/Trim 系は Phase 50+ に回す。
- 設計アウトプット: 対象ループ↔Pattern/shape マッピング表、Program JSON/fixture/test 計画、depth2 パイプラインの責務整理(コード変更なし)。
### 3.27 Phase 50-SELFHOST-NORM-DEV selfhost P2/P3 の dev Normalized 実装
- 対象: selfhost_token_scan_p2P2 break カウンタループ)/ selfhost_if_sum_p3P3 if-sum sum+countの 2 本に限定。
- Fixtures: `selfhost_token_scan_p2.program.json` / `selfhost_if_sum_p3.program.json` を normalized_dev フィクスチャ群に追加。
- ShapeGuard: `SelfhostTokenScanP2` / `SelfhostIfSumP3` 形状を追加し、canonical P2/P3 とは分離Pattern2/3 minimal に吸われないようガード)。
- Normalizer/Bridge: 既存 Pattern2/3 normalizer を流用して Structured→Normalized→MIR(direct) を dev 実行、構造/VM 出力を Structured 直経路と比較。
- テスト: normalized_joinir_min.rs に selfhost P2/P3 の VM ブリッジ比較テストを追加normalized_dev 前提、shape_guard の検出テストも拡張。