Since we are officially freezing V1, let's build the V2 Supervisor. We will
design this exactly like a traditional UNIX daemonit sits above your existing
tools, orchestrating them without altering their source code.
Here is the blueprint for supervisor.js. It will act as your automated QA
Engineer. It takes your .ast-md file, triggers your V1 compiler, generates a
test suite using your local Gemma mesh, and then uses DeepSeek to recursively
fix any bugs if the test crashes.
The Supervisor Script (supervisor.js)
Save this in your root directory. It requires no changes to your existing
files.
const fs = require('fs'); const path = require('path'); const { execSync } =
require('child_process'); // ========================================== // 1.
CONFIGURATION // ========================================== const MESH_URL =
'http://100.111.184.107:8080/v1/chat/completions'; const MAX_RETRIES = 3; //
========================================== // 2. TELEMETRY LOGGER //
========================================== function logTelemetry(event,
details) { const logFile = path.join(__dirname, 'run_report.log'); const
timestamp = new Date().toISOString(); const entry = `[${timestamp}] ${event}
${details}
${'-'.repeat(40)}
`; fs.appendFileSync(logFile, entry, 'utf-8'); console.log(`[SUPERVISOR]
${event}`); } // ========================================== // 3. AI
INTEGRATIONS // ========================================== function
cleanMarkdown(text) { let code = text.trim(); code = code.replace(/^```[a-z]*\s
*
/i, ''); code = code.replace(/
?\s*```\s*$/i, ''); return code.replace(/<end_of_turn>/g, '').trim(); } async
function generateTestEnvironment(astContent) { logTelemetry("TEST_GENERATION",
"Asking Gemma to write test_runner.js"); const prompt = `You are a QA
Automation Engineer. Read this AST-MD specification and write a Node.js test
script that requires the final entry-point module and executes it to prove it
works. If it requires mock files (like a JSON list of IPs), use the 'fs'
module to dynamically create them in the same directory before running the
test. Output ONLY valid, executable JavaScript. No explanations.
SPECIFICATION:
${astContent}`; const response = await fetch(MESH_URL, { method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer
sk-local-no-key' }, body: JSON.stringify({ model: 'gemma-2-9b-it', messages: [
{ role: 'system', content: 'Output ONLY executable JS code.' }, { role:
'user', content: prompt } ], temperature: 0.1 }) }); const data = await
response.json(); return cleanMarkdown(data.choices[0].message.content); }
async function repairCode(brokenCod
e, errorMessage) { logTelemetry("INITIATING_REPAIR", `Sending stack trace to
DeepSeek`); const apiKey = process.env.DEEPSEEK_API_KEY; const prompt = `The
following Node.js code crashed with this error. Fix the code. Output ONLY the
fully corrected JavaScript code.
--- ERROR LOG ---
${errorMessage}
--- BROKEN CODE ---
${brokenCode}`; const response = await fetch('https://api.deepseek.com/chat/com
pletions', { method: 'POST', headers: { 'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}` }, body: JSON.stringify({ model:
'deepseek-coder', messages: [ { role: 'system', content: 'You are an
autonomous debugging engine. Output ONLY executable code.' }, { role: 'user',
content: prompt } ], temperature: 0.1, max_tokens: 8192 }) }); const data =
await response.json(); return cleanMarkdown(data.choices[0].message.content);
} // ========================================== // 4. THE QA LOOP //
========================================== async function main() { const
astFile = process.argv[2]; if (!astFile) { console.error("Usage: node
supervisor.js <spec.ast-md>"); process.exit(1); } const astPath =
path.resolve(astFile); const astContent = fs.readFileSync(astPath, 'utf-8');
// STEP 1: Run the V1 Compiler logTelemetry("COMPILATION_START", `Triggering
V1 compiler for ${astFile}`); try { // Inherit stdio so we can see the V1 logs
in real-time execSync(`node index.js ${astFile}`, { stdio: 'inherit' }); }
catch (err) { logTelemetry("COMPILATION_FAILED", "V1 compiler threw a fatal
error. Supervisor halting."); process.exit(1); } // STEP 2: Generate Tests
const testCode = await generateTestEnvironment(astContent); const testPath =
path.join(__dirname, 'build', 'test_runner.js'); fs.writeFileSync(testPath,
testCode, 'utf-8'); logTelemetry("TEST_SAVED", `Test environment generated at
${testPath}`); // STEP 3: The Execution & Repair Loop let attempt = 1; let
passed = false; while (attempt <= MAX_RETRIES && !passed) { logTelemetry("TEST_
RUN", `Executing test_runner.js (Attempt ${attempt}/${MAX_RETRIES})`); tr
y { // Run the test. If it exits with 0, we drop down to passed = true const
output = execSync(`node ${testPath}`, { encoding: 'utf-8', cwd:
path.join(__dirname, 'build') }); logTelemetry("TEST_PASSED", `Output:
${output}`); passed = true; console.log("
========================================"); console.log(" SUPERVISOR
SIGN-OFF: ALL TESTS PASSED!"); console.log("===================================
=====
"); } catch (error) { // execSync throws if exit code is not 0 const
stackTrace = error.stderr || error.stdout || error.message; logTelemetry("TEST_
FAILED", `Crash detected:
${stackTrace}`); console.log(`
[!] Test crashed. Engaging self-repair protocol (Attempt ${attempt}/${MAX_RETRI
ES})...`); // NOTE: In a more advanced version, we would parse the stack trace
to find exactly WHICH file crashed. // For this V2 MVP, we will ask DeepSeek
to fix the test_runner itself or the main executor. const mainExecutorPath =
path.join(__dirname, 'build', 'PING_EXECUTOR.js'); // Hardcoded for this
specific project example const brokenCode = fs.readFileSync(mainExecutorPath,
'utf-8'); const fixedCode = await repairCode(brokenCode, stackTrace);
fs.writeFileSync(mainExecutorPath, fixedCode, 'utf-8'); logTelemetry("REPAIR_AP
PLIED", `Overwrote ${mainExecutorPath} with AI fixes.`); attempt++; } } if
(!passed) { logTelemetry("SUPERVISOR_ABORT", "Max repair retries reached.
Human intervention required."); console.log("
[FATAL] System could not self-heal. Check run_report.log for telemetry."); } }
main();
How it adheres to UNIX
Wrapper Design: Notice how Step 1 literally just runs execSync('node
index.js'). It treats your V1 compiler as a black box command-line utility. If
you ever upgrade V1, the Supervisor won't care, as long as V1 still builds
files into the ./build folder.
Stateful Logging: The run_report.log will now act as your daily telemetry
file. It records every generation, every test pass, and every stack trace if a
repair is needed.
The Auto-Repair Logic
In this MVP Supervisor, if a crash occurs, it captures the raw stderr output
from the terminal, reads the broken source code, and fires them both to
DeepSeek with a strict instruction to return the fixed version. It then
forcefully overwrites the broken file and loops the test again.