Here is the Node.js wrapper script. It uses the built-in readline module to
create a persistent, non-blocking REPL loop.
Save the previous code as machine.js, and save this script as runner.js.
const readline = require('readline');
const fs = require('fs');
const { parse_input, state } = require('./machine.js');
const ENV_FILE = 'state-machine-environment.json';
/ --- 1. Startup Initialization ---
console.log("==========================================");
console.log(" Hallucination-Tolerant State Machine (PoC)");
console.log("==========================================");
/ Attempt to seed the environment if the configuration file exists
if (fs.existsSync(ENV_FILE)) {
console.log(`[Startup] Found ${ENV_FILE}. Merging into local cache...`);
try {
const envData = JSON.parse(fs.readFileSync(ENV_FILE, 'utf8'));
if (envData.environment) state.environment = envData.environment;
if (envData.local_cache) Object.assign(state.local_cache,
envData.local_cache);
console.log(`[Startup] Environment seeded successfully.`);
} catch (e) {
console.log(`[Startup] Error reading ${ENV_FILE}: ${e.message}`);
}
} else {
console.log(`[Startup] No ${ENV_FILE} found. Starting in VOID.`);
}
console.log(`[Startup] Identity established: ${state.identity}`);
/ --- 2. REPL Setup ---
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
/ We use a dynamic prompt so if you use `/load` and change
/ the machine's identity, the CLI immediately reflects it.
const getPrompt = () => `
${state.identity} > `;
/ --- 3. The Execution Loop ---
rl.setPrompt(getPrompt());
rl.prompt();
rl.on('line', (line) => {
const input = line.trim();
// Intercept standard terminal exit commands
if (input.toLowerCase() === '/exit' || input.toLowerCase() === '/quit') {
console.log("[Shutdown] Terminating state machine...");
process.exit(0);
}
// Feed non-empty input into the core command router
if (input) {
try {
parse_input(input);
} catch (err) {
console.error(`[Fatal Router Error]: ${err.message}`);
}
}
// Refresh the prompt and wait for the next command
rl.setPrompt(getPrompt());
rl.prompt();
}).on('close', () => {
console.log("
[Shutdown] Connection closed.");
process.exit(0);
});
How to use this PoC:
* Run node runner.js in your terminal.
* Type /mission macro Build the mailbox file watcher to set an objective.
* Type /concept MAILBOX_ROUTER to let the system hallucinate a component.
* Type /mint MAILBOX_ROUTER to convert it into a physically tracked inventory
object.
* Type /tick to generate the exact payload you would feed into an LLM to
decide the next step.
* Type /save to write your session to disk, then /exit. You can start it back
up later and type /load [filename] to resume exactly where you left off.