This 10-step protocol is a near-perfect blueprint for a Hot-Swappable Proxy
Pattern. It allows the engine to seamlessly convert interpreted bytecodes into
native machine code without the surrounding ecosystem ever knowing the object
changed forms.
However, looking at your architecture, we need to adjust Step 3 to prevent a
catastrophic bottleneck, while leaning into the green-thread scheduler you've
already built to handle the rest.
Here is an analysis and refinement of your 10-step protocol to fit the
HAUKI-X86 microkernel:
1. The Initiation (sys_offload)
You can expose a new system call, sys_offload(int target_oid). Whether
triggered heuristically by the VM (e.g., instruction count exceeding a
threshold) or manually by the LPC script, this flips a bit in the Master
Object Index (MOI) flags (e.g., this.moi[base + 3] |= 0x8) marking it as
FLAG_OFFLOADING.
2. The Transit Wait Queue
Because you built the Actor-Model mailbox system, incoming calls don't crash
the system; they simply queue up. Any post_mail sent to an offloading object
is held in its mailbox.
3. The Pause (Refined: Do NOT Pause the VM)
We absolutely must avoid pausing the entire VM. Compiling C code or invoking
an LLM synthesis harness can take anywhere from 50 milliseconds to 5 seconds.
If the VM halts, the entire MUD freezes for all players.
* The Fix: Instead of halting the VM, we use your existing scheduler logic.
We pull the target object's CTX out of the runQueue and drop it into a new map
called offloadTable. The rest of the game world continues ticking normally,
while the target object enters a localized state of suspended animation.
4. The Resynthesis Queue (File-Based IPC)
Writing to a local directory is the perfect MVP. The VM serializes the
object's AST, its current memory state (the raw tagged integers from the
stateArena), and dumps a JSON file into /tmp/hauki_resynth/. An external
watcher process (your synthesis engine) detects the file and begins compiling.
5. The High-Speed Comm Channel
To keep it simple, the synthesis worker can spawn a dedicated Unix Domain
Socket (UDS) or a local TCP port specifically for this object.
6 & 7. The Wait and Timeout
The engine sets a standard Node.js setTimeout. If the timeout fires before the
synthesis engine replies, the VM strips the FLAG_OFFLOADING from the MOI,
pulls the CTX from the offloadTable back into the runQueue, and logs a
compilation failure. The object simply processes its queued mail via standard
interpreted LPC.
8. The Proxy Swap (Success)
If the synthesis succeeds, the external worker sends a "READY" packet
containing the new comm socket address. The VM changes the MOI flag to
FLAG_NATIVE.
The object is now a "Hollow Shell." Its local CTX is effectively destroyed or
permanently parked. Whenever the engine attempts to route mail or execute a
function on this OID, the engine's router intercepts the call and forwards the
JSON payload across the socket to the native worker.
9. Bidirectional Updates
The native worker acts as a standalone node in your mesh. It processes its
high-frequency ticks internally. When it needs to interact with the world
(e.g., moving to a new coordinate, attacking), it sends a JSON payload back
over the socket: {"action": "post_mail", "target": 5001, "msg":
"thermal_radiation"}. The VM receives this and injects it into the standard
simulation loop.
10. Fallback and Re-Integration
If the socket drops (the native process crashes or yields), the VM catches the
ECONNRESET event. It gracefully intercepts the failure, pulls the last known
state snapshot, clears the FLAG_NATIVE bit, and drops the original LPC context
back into the runQueue. The MUD barely stutters.
To prototype this, the very first thing we need is the State Serializer.
Before we can offload an object, we need a function inside LPCVirtualMachine.js
that can look up an OID, extract its active variables from the stateArena,
dereference any tagged strings/arrays from the GlobalHandleLedger, and pack
them into a clean JSON object for the external queue.
Would you like to draft the serialize_state(oid) function to see what an
object looks like right before it gets pushed to disk?