Refactor MCP tree server: dispatch table, caching, validation, subprocess cleanup

Break up the 1735-line handle_tool match into 45 individual handler functions
with hashtable-based dispatch. Add mtime-based file parse caching (AST + CST),
consolidated run_command helper replacing 9 bare open_process_in patterns,
require_file/require_dir input validation, and pagination (limit/offset) for
sx_find_across, sx_comp_list, sx_comp_usage. Also includes pending VM changes:
rest-arity support, hyperscript parser, compiler/transpiler updates.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-08 10:12:57 +00:00
parent 4d1079aa5e
commit 387a6cb49e
19 changed files with 1353 additions and 966 deletions

View File

@@ -936,8 +936,8 @@ let register_jit_hook env =
| Lambda l ->
(match l.l_compiled with
| Some cl when not (Sx_vm.is_jit_failed cl) ->
(* Skip during compilation — compiled helpers loop on complex ASTs.
Normal execution uses bytecode (fast). *)
(* Skip during CEK-based compilation — helpers are called inside
the VM when compile has bytecode, no need for the hook. *)
if !(Sx_vm._jit_compiling) then None
else
(try Some (Sx_vm.call_closure cl args cl.vm_env_ref)
@@ -954,7 +954,6 @@ let register_jit_hook env =
None)
| Some _ -> None
| None ->
(* Only block NEW compilations during _jit_compiling, not execution *)
let fn_name = match l.l_name with Some n -> n | None -> "?" in
if !(Sx_vm._jit_compiling) then None
else if Hashtbl.mem _jit_warned fn_name then None
@@ -1185,9 +1184,12 @@ let rec dispatch env cmd =
register_jit_hook env;
let t0 = Unix.gettimeofday () in
let count = ref 0 in
(* Pre-compile helpers, NOT "compile" itself (loops on complex ASTs) *)
(* Pre-compile compiler helpers AND compile itself.
When compile has bytecode, jit_compile_lambda calls it directly via
the VM — all helper calls happen inside the same VM execution with
no per-call overhead. This is 10-100x faster than CEK dispatch. *)
let compiler_names = [
"compile-module"; "compile-expr"; "compile-symbol";
"compile"; "compile-module"; "compile-expr"; "compile-symbol";
"compile-dict"; "compile-list"; "compile-if"; "compile-when";
"compile-and"; "compile-or"; "compile-begin"; "compile-let";
"compile-letrec"; "compile-lambda"; "compile-define"; "compile-set";
@@ -1209,15 +1211,16 @@ let rec dispatch env cmd =
| None -> ())
| _ -> ()
) compiler_names;
(* Mark "compile" as jit-failed — loops on complex ASTs as bytecode *)
(try match Hashtbl.find_opt env.bindings (Sx_types.intern "compile") with
| Some (Lambda l) -> l.l_compiled <- Some Sx_vm.jit_failed_sentinel
| _ -> ()
with _ -> ());
let dt = Unix.gettimeofday () -. t0 in
Printf.eprintf "[jit] Pre-compiled %d compiler functions in %.3fs (lazy JIT active for all)\n%!" !count dt;
send_ok ()
| List [Symbol "jit-reset-name"; String name] ->
(* Reset a function's JIT state back to uncompiled *)
(match Hashtbl.find_opt env.bindings (Sx_types.intern name) with
| Some (Lambda l) -> l.l_compiled <- None; send_ok_raw (Printf.sprintf "reset %s" name)
| _ -> send_ok_raw (Printf.sprintf "not-found %s" name))
| List [Symbol "set-request-cookies"; Dict cookies] ->
(* Set request cookies for get-cookie primitive.
Called by Python bridge before each page render. *)
@@ -2670,12 +2673,11 @@ let http_mode port =
| _ -> raise (Eval_error "component-source: expected (name)"));
let jt0 = Unix.gettimeofday () in
let count = ref 0 in
(* Pre-compile compiler HELPERS but NOT "compile" itself.
"compile" runs via CEK (correct for all AST sizes).
Its internal calls to compile-expr/emit-byte/etc use bytecode (fast).
Pre-compiling "compile" causes it to loop on complex nested forms. *)
(* Pre-compile the entire compiler — compile + helpers.
jit_compile_lambda calls compile directly via the VM when it has
bytecode, so all helper calls happen in one VM execution. *)
let compiler_names = [
"compile-module"; "compile-expr"; "compile-symbol";
"compile"; "compile-module"; "compile-expr"; "compile-symbol";
"compile-dict"; "compile-list"; "compile-if"; "compile-when";
"compile-and"; "compile-or"; "compile-begin"; "compile-let";
"compile-letrec"; "compile-lambda"; "compile-define"; "compile-set";
@@ -2699,12 +2701,6 @@ let http_mode port =
| _ -> ()
with _ -> ()
) compiler_names;
(* Mark "compile" as jit-failed — its compiled bytecode loops on complex
ASTs. It runs via CEK (correct), while its helpers run as bytecode (fast). *)
(try match env_get env "compile" with
| Lambda l -> l.l_compiled <- Some Sx_vm.jit_failed_sentinel
| _ -> ()
with _ -> ());
let jt1 = Unix.gettimeofday () in
Printf.eprintf "[sx-http] JIT pre-compiled %d compiler fns in %.3fs\n%!" !count (jt1 -. jt0);
(* Re-bind native primitives that stdlib.sx may have overwritten with