feat(phase21.5/22.1): MirBuilder JsonFrag refactor + FileBox ring-1 + registry tests

Phase 21.5 (AOT/LLVM Optimization Prep)
- FileBox ring-1 (core-ro) provider: priority=-100, always available, no panic path
  - src/runner/modes/common_util/provider_registry.rs: CoreRoFileProviderFactory
  - Auto-registers at startup, eliminates fallback panic structurally
- StringBox fast path prototypes (length/size optimization)
- Performance benchmarks (C/Python/Hako comparison baseline)

Phase 22.1 (JsonFrag Unification)
- JsonFrag.last_index_of_from() for backward search (VM fallback)
- Replace hand-written lastIndexOf in lower_loop_sum_bc_box.hako
- SentinelExtractorBox for Break/Continue pattern extraction

MirBuilder Refactor (Box → JsonFrag Migration)
- 20+ lower_*_box.hako: Box-heavy → JsonFrag text assembly
- MirBuilderMinBox: lightweight using set for dev env
- Registry-only fast path with [registry:*] tag observation
- pattern_util_box.hako: enhanced pattern matching

Dev Environment & Testing
- Dev toggles: SMOKES_DEV_PREINCLUDE=1 (point-enable), HAKO_MIR_BUILDER_SKIP_LOOPS=1
- phase2160: registry opt-in tests (array/map get/set/push/len) - content verification
- phase2034: rc-dependent → token grep (grep -F based validation)
- run_quick.sh: fast smoke testing harness
- ENV documentation: docs/ENV_VARS.md

Test Results
 quick phase2034: ALL GREEN (MirBuilder internal patterns)
 registry phase2160: ALL GREEN (array/map get/set/push/len)
 rc-dependent tests → content token verification complete
 PREINCLUDE policy: default OFF, point-enable only where needed

Technical Notes
- No INCLUDE by default (maintain minimalism)
- FAIL_FAST=0 in Bring-up contexts only (explicit dev toggles)
- Tag-based route observation ([mirbuilder/min:*], [registry:*])
- MIR structure validation (not just rc parity)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-10 19:42:42 +09:00
parent fc5706e3f2
commit 6055d53eff
135 changed files with 3983 additions and 1150 deletions

View File

@ -13,6 +13,31 @@ impl MirInterpreter {
if let Err(e) = crate::runtime::provider_lock::guard_before_new_box(box_type) {
return Err(self.err_invalid(e));
}
// Fast path (bench/profile-only): new StringBox("const") without registry roundtrip
if std::env::var("NYASH_VM_FAST").ok().as_deref() == Some("1") && box_type == "StringBox" {
if args.len() == 1 {
let v0 = self.reg_load(args[0])?;
let s_opt: Option<String> = match v0.clone() {
VMValue::String(s) => Some(s),
VMValue::BoxRef(b) => {
if let Some(sb) = b.as_any().downcast_ref::<crate::boxes::basic::StringBox>() {
Some(sb.value.clone())
} else {
None
}
}
_ => None,
};
if let Some(s) = s_opt {
let boxed: Box<dyn crate::box_trait::NyashBox> = Box::new(crate::boxes::basic::StringBox::new(s));
let created_vm = VMValue::from_nyash_box(boxed);
self.regs.insert(dst, created_vm);
if Self::box_trace_enabled() { self.box_trace_emit_new(box_type, args.len()); }
return Ok(());
}
}
}
let converted = self.load_args_as_boxes(args)?;
let reg = crate::runtime::unified_registry::get_global_unified_registry();
let created = reg

View File

@ -13,6 +13,17 @@ pub(super) fn try_handle_string_box(
eprintln!("[vm-trace] try_handle_string_box(method={})", method);
}
let recv = this.reg_load(box_val)?;
// Ultra-fast path: raw VM string receiver for length/size (no boxing at all)
if (method == "length" || method == "size")
&& std::env::var("NYASH_VM_FAST").ok().as_deref() == Some("1")
{
if let VMValue::String(ref raw) = recv {
let use_cp = std::env::var("NYASH_STR_CP").ok().as_deref() == Some("1");
let n = if use_cp { raw.chars().count() as i64 } else { raw.len() as i64 };
this.write_result(dst, VMValue::Integer(n));
return Ok(true);
}
}
// Handle ONLY when the receiver is actually a string.
// Do NOT coerce arbitrary boxes to StringBox (e.g., ArrayBox.length()).
let sb_norm_opt: Option<crate::box_trait::StringBox> = match recv.clone() {
@ -30,6 +41,13 @@ pub(super) fn try_handle_string_box(
// Only handle known string methods here (receiver is confirmed string)
match method {
"length" | "size" => {
// Bench/profile fast path: return VMValue::Integer directly (avoid boxing overhead)
if std::env::var("NYASH_VM_FAST").ok().as_deref() == Some("1") {
let use_cp = std::env::var("NYASH_STR_CP").ok().as_deref() == Some("1");
let n = if use_cp { sb_norm.value.chars().count() as i64 } else { sb_norm.value.len() as i64 };
this.write_result(dst, VMValue::Integer(n));
return Ok(true);
}
let ret = sb_norm.length();
this.write_result(dst, VMValue::from_nyash_box(ret));
return Ok(true);

View File

@ -165,6 +165,11 @@ pub fn env_bool_default(key: &str, default: bool) -> bool {
}
}
/// Global fail-fast policy for runtime fallbacks.
/// Default: ON (true) to prohibit silent/different-route fallbacks in Rust layer.
/// Set NYASH_FAIL_FAST=0 to temporarily allow legacy fallbacks during bring-up.
pub fn fail_fast() -> bool { env_bool_default("NYASH_FAIL_FAST", true) }
// ---- Phase 11.8 MIR cleanup toggles ----
/// Core-13 minimal MIR mode toggle
/// Default: ON (unless explicitly disabled with NYASH_MIR_CORE13=0)

View File

@ -118,7 +118,26 @@ def lower_boxcall(
# Minimal method bridging for strings and console
if method_name in ("length", "len"):
# Any.length_h: Array/String/Map に対応
# Fast path (opt-in): pointer-based string length → nyash.string.length_si(i8*, i64 mode)
try:
import os
fast_on = os.environ.get('NYASH_LLVM_FAST') == '1'
except Exception:
fast_on = False
if fast_on and resolver is not None and hasattr(resolver, 'string_ptrs'):
try:
ptr = resolver.string_ptrs.get(int(box_vid))
except Exception:
ptr = None
if ptr is not None:
mode = 1 if os.environ.get('NYASH_STR_CP') == '1' else 0
mode_c = ir.Constant(i64, mode)
callee = _declare(module, "nyash.string.length_si", i64, [i8p, i64])
result = builder.call(callee, [ptr, mode_c], name="strlen_si")
if dst_vid is not None:
vmap[dst_vid] = result
return
# Default: Any.length_h(handle) → i64
recv_h = _ensure_handle(builder, module, recv_val)
callee = _declare(module, "nyash.any.length_h", i64, [i64])
result = builder.call(callee, [recv_h], name="any_length_h")

View File

@ -28,6 +28,27 @@ pub fn register_provider_factory(factory: Arc<dyn ProviderFactory>) {
registry.lock().unwrap().push(factory);
}
/// Builtin ring1 FileBox provider (corero) — always available, lowest priority
struct CoreRoFileProviderFactory;
impl ProviderFactory for CoreRoFileProviderFactory {
fn box_name(&self) -> &str { "FileBox" }
fn create_provider(&self) -> Arc<dyn FileIo> { Arc::new(CoreRoFileIo::new()) }
fn is_available(&self) -> bool { true }
fn priority(&self) -> i32 { -100 } // ring1: lower than any plugin/provider
}
/// Ensure ring1 (corero) provider is present in the registry
fn ensure_builtin_file_provider_registered() {
let reg = PROVIDER_FACTORIES.get_or_init(|| Mutex::new(Vec::new()));
let mut guard = reg.lock().unwrap();
// If at least one FileBox provider exists, we still keep ring1 present for safety; avoid duplicates by checking any corero present by priority
let has_core_ro = guard.iter().any(|f| f.box_name() == "FileBox" && f.priority() <= -100);
if !has_core_ro {
guard.push(Arc::new(CoreRoFileProviderFactory));
}
}
/// Read FileBox mode from environment variables
#[allow(dead_code)]
pub fn read_filebox_mode_from_env() -> FileBoxMode {
@ -45,8 +66,10 @@ pub fn read_filebox_mode_from_env() -> FileBoxMode {
/// Select provider based on mode and registered factories (SSOT)
#[allow(dead_code)]
pub fn select_file_provider(mode: FileBoxMode) -> Arc<dyn FileIo> {
let registry = PROVIDER_FACTORIES.get();
let quiet_pipe = crate::config::env::env_bool("NYASH_JSON_ONLY");
// Always ensure ring1 (corero) exists before inspecting registry
ensure_builtin_file_provider_registered();
let registry = PROVIDER_FACTORIES.get();
match mode {
FileBoxMode::Auto => {
@ -69,11 +92,27 @@ pub fn select_file_provider(mode: FileBoxMode) -> Arc<dyn FileIo> {
}
}
// Fallback to core-ro
if !quiet_pipe {
eprintln!("[provider-registry] FileBox: using core-ro fallback");
// Fallback policy
// Allow a narrow, explicit carveout:
// - When JSONonly pipeline is active (quiet structured I/O), or
// - When NYASH_FILEBOX_ALLOW_FALLBACK=1 is set,
// always use corero provider even if FailFast is ON.
let allow_fb_override =
crate::config::env::env_bool("NYASH_JSON_ONLY") ||
crate::config::env::env_bool("NYASH_FILEBOX_ALLOW_FALLBACK");
if crate::config::env::fail_fast() && !allow_fb_override {
eprintln!("[failfast/provider/filebox:auto-fallback-blocked]");
panic!("Fail-Fast: FileBox provider fallback is disabled (NYASH_FAIL_FAST=0 or NYASH_FILEBOX_ALLOW_FALLBACK=1 to override)");
} else {
if !quiet_pipe {
eprintln!(
"[provider-registry] FileBox: using core-ro fallback{}",
if allow_fb_override { " (override)" } else { "" }
);
}
Arc::new(CoreRoFileIo::new())
}
Arc::new(CoreRoFileIo::new())
}
FileBoxMode::PluginOnly => {
// Try only registered providers, Fail-Fast if none available
@ -105,4 +144,3 @@ pub fn select_file_provider(mode: FileBoxMode) -> Arc<dyn FileIo> {
}
}
}

View File

@ -68,20 +68,32 @@ fn call_hako_box(name: &str, ctx: &SsotCtx) -> Option<String> {
nn
));
// Write to a temp file
// Write ephemeral file; any failure → None (delegate to legacy)
let mut tf = tempfile::Builder::new()
// Write to a temp file (Fail-Fast aware)
let mut tf = match tempfile::Builder::new()
.prefix("ny_ssot_")
.suffix(".hako")
.tempfile()
.ok()?;
.tempfile() {
Ok(f) => f,
Err(e) => {
if crate::config::env::fail_fast() {
eprintln!("[failfast/ssot/tempfile] {}", e);
panic!("Fail-Fast: SSOT tempfile creation failed");
}
return None;
}
};
let _ = write!(tf, "{}", code);
let path = tf.path().to_path_buf();
// Resolve nyash binary; fallback to current exe or default path on failure
let bin = std::env::var("NYASH_BIN").ok().unwrap_or_else(|| {
if let Ok(p) = std::env::current_exe() { p.to_string_lossy().to_string() }
else { "target/release/nyash".to_string() }
});
// Resolve nyash binary; Fail-Fast aware fallback
let bin = if let Ok(b) = std::env::var("NYASH_BIN") { b } else {
if let Ok(p) = std::env::current_exe() { p.to_string_lossy().to_string() } else {
if crate::config::env::fail_fast() {
eprintln!("[failfast/ssot/nyash-bin] unable to resolve NYASH_BIN/current_exe");
panic!("Fail-Fast: cannot resolve nyash binary for SSOT child");
}
"target/release/nyash".to_string()
}
};
// Stage3 + tolerance (matches smokes wrappers)
let mut cmd = Command::new(bin);
@ -99,9 +111,30 @@ fn call_hako_box(name: &str, ctx: &SsotCtx) -> Option<String> {
.env("HAKO_USING_SSOT_HAKO", "0")
.env("HAKO_USING_SSOT_RELATIVE", "0")
.env("HAKO_USING_SSOT_INVOKING", "1");
// Any spawn/IO error → None (fail-safe to legacy)
let out = cmd.output().ok()?;
if !out.status.success() { return None; }
// Any spawn/IO error → Fail-Fast or None
let out = match cmd.output() {
Ok(o) => o,
Err(e) => {
if crate::config::env::fail_fast() {
eprintln!("[failfast/ssot/hako-spawn] {}", e);
panic!("Fail-Fast: SSOT child spawn failed");
}
return None;
}
};
if !out.status.success() {
if crate::config::env::fail_fast() {
eprintln!("[failfast/ssot/hako-exit] status={}", out.status);
panic!("Fail-Fast: SSOT child exited with error");
}
return None;
}
let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
if s.is_empty() { None } else { Some(s) }
if s.is_empty() {
if crate::config::env::fail_fast() {
eprintln!("[failfast/ssot/hako-empty]");
panic!("Fail-Fast: SSOT child produced empty output");
}
None
} else { Some(s) }
}