Phase 22.1 WIP: SSOT resolver + TLV infrastructure + Hako MIR builder setup

Setup infrastructure for Phase 22.1 (TLV C shim & Resolver SSOT):

Core changes:
- Add nyash_tlv, nyash_c_core, nyash_kernel_min_c crates (opt-in)
- Implement SSOT resolver bridge (src/using/ssot_bridge.rs)
- Add HAKO_USING_SSOT=1 / HAKO_USING_SSOT_HAKO=1 env support
- Add HAKO_TLV_SHIM=1 infrastructure (requires --features tlv-shim)

MIR builder improvements:
- Fix using/alias consistency in Hako MIR builder
- Add hako.mir.builder.internal.{prog_scan,pattern_util} to nyash.toml
- Normalize LLVM extern calls: nyash.console.* → nyash_console_*

Smoke tests:
- Add phase2211 tests (using_ssot_hako_parity_canary_vm.sh)
- Add phase2220, phase2230, phase2231 test structure
- Add phase2100 S3 backend selector tests
- Improve test_runner.sh with quiet/timeout controls

Documentation:
- Add docs/ENV_VARS.md (Phase 22.1 env vars reference)
- Add docs/development/runtime/C_CORE_ABI.md
- Update de-rust-roadmap.md with Phase 22.x details

Tools:
- Add tools/hakorune_emit_mir.sh (Hako-first MIR emission wrapper)
- Add tools/tlv_roundtrip_smoke.sh placeholder
- Improve ny_mir_builder.sh with better backend selection

Known issues (to be fixed):
- Parser infinite loop in static method parameter parsing
- Stage-B output contamination with "RC: 0" (needs NYASH_JSON_ONLY=1)
- phase2211/using_ssot_hako_parity_canary_vm.sh fork bomb (needs recursion guard)

Next steps: Fix parser infinite loop + Stage-B quiet mode for green tests
This commit is contained in:
nyash-codex
2025-11-09 15:11:18 +09:00
parent 5d2cd5bad0
commit 981ddd890c
62 changed files with 1981 additions and 103 deletions

View File

@ -17,3 +17,4 @@ pub mod spec;
pub mod policy;
pub mod errors;
pub mod simple_registry;
pub mod ssot_bridge;

107
src/using/ssot_bridge.rs Normal file
View File

@ -0,0 +1,107 @@
//! SSOT bridge — thin callable shim from Rust to Hako resolver (Phase 22.1)
//!
//! MVP: does not invoke Hako VM yet. It mirrors the Hako box logic for modules-only
//! resolution, returning the mapped path when present. Callers must keep behavior
//! identical to existing resolver and use this only under an explicit env toggle.
use std::collections::HashMap;
use std::io::Write;
use std::process::Command;
#[derive(Default, Debug, Clone)]
pub struct SsotCtx {
pub modules: HashMap<String, String>,
pub using_paths: Vec<String>,
pub cwd: Option<String>,
}
/// Attempt to resolve via SSOT bridge. Returns Some(path) if found; otherwise None.
///
/// Behavior (MVP):
/// - Only consults `modules` map (exact match).
/// - Does not access filesystem nor invoke Hako VM.
pub fn call_using_resolve_ssot(name: &str, ctx: &SsotCtx) -> Option<String> {
if name.is_empty() { return None; }
// Optional: delegate to Hako resolver when explicitly requested.
if std::env::var("HAKO_USING_SSOT_HAKO").ok().as_deref() == Some("1") {
if let Some(hit) = call_hako_box(name, ctx) { return Some(hit); }
}
// MVP: modules-only
ctx.modules.get(name).cloned()
}
/// Try resolving via Hako `UsingResolveSSOTBox.resolve(name, ctx)` by spawning the nyash VM.
/// Guarded by `HAKO_USING_SSOT_HAKO=1`. Returns Some(path) on success; otherwise None.
fn call_hako_box(name: &str, ctx: &SsotCtx) -> Option<String> {
// Build inline Hako code that constructs a minimal ctx with modules map.
let mut code = String::new();
code.push_str("using hako.using.resolve.ssot as UsingResolveSSOTBox\n");
code.push_str("static box Main {\n main() {\n local modules = new MapBox()\n");
for (k, v) in ctx.modules.iter() {
// Escape quotes conservatively
let kk = k.replace('\"', "\\\"");
let vv = v.replace('\"', "\\\"");
code.push_str(&format!(" modules.set(\"{}\", \"{}\")\n", kk, vv));
}
code.push_str(" local ctx = new MapBox()\n ctx.set(\"modules\", modules)\n");
// relative_hint: opt-in via parent env HAKO_USING_SSOT_RELATIVE=1
if std::env::var("HAKO_USING_SSOT_RELATIVE").ok().as_deref() == Some("1") {
code.push_str(" ctx.set(\\\"relative_hint\\\", \\\"1\\\")\\n");
}
// using_paths
if !ctx.using_paths.is_empty() {
code.push_str(" local ups = new ArrayBox()\n");
for up in ctx.using_paths.iter() {
let upq = up.replace('\"', "\\\"");
code.push_str(&format!(" ups.push(\"{}\")\n", upq));
}
code.push_str(" ctx.set(\\\"using_paths\\\", ups)\n");
}
// cwd
if let Some(cwd) = &ctx.cwd {
let cwq = cwd.replace('\"', "\\\"");
code.push_str(&format!(" ctx.set(\\\"cwd\\\", \"{}\")\n", cwq));
}
let nn = name.replace('\"', "\\\"");
code.push_str(&format!(
" local r = UsingResolveSSOTBox.resolve(\"{}\", ctx)\n if r == null {{ return 0 }}\n print(r)\n return 0\n }}\n",
nn
));
// Write to a temp file
// Write ephemeral file; any failure → None (delegate to legacy)
let mut tf = tempfile::Builder::new()
.prefix("ny_ssot_")
.suffix(".hako")
.tempfile()
.ok()?;
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() }
});
// Stage3 + tolerance (matches smokes wrappers)
let mut cmd = Command::new(bin);
cmd.arg("--backend").arg("vm").arg(&path)
// Parser/entry tolerances (same as smokes "safe" mode)
.env("NYASH_PARSER_STAGE3", "1")
.env("HAKO_PARSER_STAGE3", "1")
.env("NYASH_PARSER_ALLOW_SEMICOLON", "1")
.env("NYASH_ENTRY_ALLOW_TOPLEVEL_MAIN", "1")
// Disable inline compiler for stability
.env("NYASH_DISABLE_NY_COMPILER", "1")
.env("HAKO_DISABLE_NY_COMPILER", "1")
// Hard-disable SSOT in the child to avoid recursion; mark invoking guard
.env("HAKO_USING_SSOT", "0")
.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; }
let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
if s.is_empty() { None } else { Some(s) }
}