Daemon: supervise the worker child, drop WSL, Docker, Eve and CORS
vili-daemon.mjs spawns the Python worker itself (no shell, port 0 from the
ready line, nice 10, one child at a time, respawn after exit, group kill on
SIGTERM) with a closed environment. WSL/Docker, the Eve surfaces and deck
WebSocket, the provider advertisement, /smoke, CORS, JSON sidecars and the
pid file are deleted. POSTs with an Origin or a non-JSON body are refused
403 cross-site. cultcache-ts comes from the vendor/CultLib submodule at
b549d731; ws is dropped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:14:10 +02:00
// Stands in for kimodo-resident-worker.py in the daemon's tests. It binds port 0, prints the ready line the
2026-10-23 13:16:50 +02:00
// daemon waits for, and answers /health and /generate with what the daemon gave it. The --model value picks a
// misbehaviour: crash-before-ready, hang-before-ready, sick, slow-health, stubborn, fail-generate,
2026-10-24 23:04:36 +02:00
// refuse-generate, drop-generate, slow-generate, writer (appends to <output>.writes every 50 ms, never answers, ignores SIGTERM), slow-to-stop (no answer; prints a line and exits 1.5 s after SIGTERM), chatty (prints a line the moment it is healthy), encoder (a grandchild, then no answer), graceful (exits 0.5 s after SIGTERM), die-after-ready, bvh-outside (lists a BVH outside the job's directory), bvh-link (lists a symlink as its BVH), plant-link (a normal answer with a file symlink and a directory symlink planted beside the BVH). A request with bvh writes <output>.bvh (motion/motion_NN.bvh for several samples) and lists it. Any other value is just the model the worker serves.
Daemon: supervise the worker child, drop WSL, Docker, Eve and CORS
vili-daemon.mjs spawns the Python worker itself (no shell, port 0 from the
ready line, nice 10, one child at a time, respawn after exit, group kill on
SIGTERM) with a closed environment. WSL/Docker, the Eve surfaces and deck
WebSocket, the provider advertisement, /smoke, CORS, JSON sidecars and the
pid file are deleted. POSTs with an Origin or a non-JSON body are refused
403 cross-site. cultcache-ts comes from the vendor/CultLib submodule at
b549d731; ws is dropped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:14:10 +02:00
import http from "node:http" ;
import os from "node:os" ;
import path from "node:path" ;
2026-10-23 13:16:50 +02:00
import { spawn } from "node:child_process" ;
2026-10-24 20:26:54 +02:00
import { appendFileSync , mkdirSync , symlinkSync , writeFileSync } from "node:fs" ;
Daemon: supervise the worker child, drop WSL, Docker, Eve and CORS
vili-daemon.mjs spawns the Python worker itself (no shell, port 0 from the
ready line, nice 10, one child at a time, respawn after exit, group kill on
SIGTERM) with a closed environment. WSL/Docker, the Eve surfaces and deck
WebSocket, the provider advertisement, /smoke, CORS, JSON sidecars and the
pid file are deleted. POSTs with an Origin or a non-JSON body are refused
403 cross-site. cultcache-ts comes from the vendor/CultLib submodule at
b549d731; ws is dropped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:14:10 +02:00
const argv = process . argv . slice ( 2 ) ;
2026-10-23 13:16:50 +02:00
const flag = ( name ) => {
const index = argv . indexOf ( name ) ;
return index < 0 ? undefined : argv [ index + 1 ] ;
} ;
const model = flag ( "--model" ) ;
const spawnLog = path . join ( process . env . VILI _EMBEDDINGS _DIR , "stub-spawns.log" ) ;
Daemon: supervise the worker child, drop WSL, Docker, Eve and CORS
vili-daemon.mjs spawns the Python worker itself (no shell, port 0 from the
ready line, nice 10, one child at a time, respawn after exit, group kill on
SIGTERM) with a closed environment. WSL/Docker, the Eve surfaces and deck
WebSocket, the provider advertisement, /smoke, CORS, JSON sidecars and the
pid file are deleted. POSTs with an Origin or a non-JSON body are refused
403 cross-site. cultcache-ts comes from the vendor/CultLib submodule at
b549d731; ws is dropped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:14:10 +02:00
let requests = 0 ;
2026-10-23 13:16:50 +02:00
let healthCalls = 0 ;
Daemon: supervise the worker child, drop WSL, Docker, Eve and CORS
vili-daemon.mjs spawns the Python worker itself (no shell, port 0 from the
ready line, nice 10, one child at a time, respawn after exit, group kill on
SIGTERM) with a closed environment. WSL/Docker, the Eve surfaces and deck
WebSocket, the provider advertisement, /smoke, CORS, JSON sidecars and the
pid file are deleted. POSTs with an Origin or a non-JSON body are refused
403 cross-site. cultcache-ts comes from the vendor/CultLib submodule at
b549d731; ws is dropped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:14:10 +02:00
2026-10-23 13:16:50 +02:00
appendFileSync ( spawnLog , ` ${ process . pid } \n ` ) ;
console . log ( "stub-stdout-line" ) ;
console . error ( "stub-stderr-line" ) ;
// decoys: neither is the ready line
console . log ( JSON . stringify ( { event : "progress" , port : 1 } ) ) ;
console . log ( JSON . stringify ( { event : "kimodo-worker-ready" , port : "not-a-port" } ) ) ;
Daemon: supervise the worker child, drop WSL, Docker, Eve and CORS
vili-daemon.mjs spawns the Python worker itself (no shell, port 0 from the
ready line, nice 10, one child at a time, respawn after exit, group kill on
SIGTERM) with a closed environment. WSL/Docker, the Eve surfaces and deck
WebSocket, the provider advertisement, /smoke, CORS, JSON sidecars and the
pid file are deleted. POSTs with an Origin or a non-JSON body are refused
403 cross-site. cultcache-ts comes from the vendor/CultLib submodule at
b549d731; ws is dropped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:14:10 +02:00
2026-10-23 13:16:50 +02:00
if ( model === "crash-before-ready" ) process . exit ( 3 ) ;
if ( model === "stubborn" ) {
process . on ( "SIGTERM" , ( ) => { } ) ;
2026-10-24 00:23:07 +02:00
// the grandchild reports itself once its handler is armed, so a test never reads a pid that would die of the default SIGTERM
spawn ( process . execPath , [ "-e" , ` process.on('SIGTERM', () => {}); require('fs').appendFileSync( ${ JSON . stringify ( spawnLog ) } , 'grandchild ' + process.pid + ' \\ n'); setInterval(() => {}, 1000) ` ] , { stdio : "ignore" } ) ;
2026-10-23 13:16:50 +02:00
}
2026-10-24 05:19:08 +02:00
if ( model === "slow-to-stop" ) process . on ( "SIGTERM" , ( ) => {
console . log ( "stopping-line" ) ; // output while it stops
setTimeout ( ( ) => process . exit ( 0 ) , 1500 ) ;
} ) ;
if ( model === "writer" ) process . on ( "SIGTERM" , ( ) => { } ) ; // a worker that carries on writing through SIGTERM
2026-10-24 05:15:43 +02:00
Daemon: drop the unread worker.model; tests for grace, host default, relative models root, early death
Mutation triage (StrykerJS, 81 survivors on the first full run) showed worker.model was
written and never read. The new tests pin the survivors that were behaviour: the loopback
default, --host, stray flags, the models-root resolution, the SIGTERM grace, a worker that
dies while the daemon waits for its health, the POST route, and lastError after a
requested stop.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:07:15 +02:00
if ( model === "graceful" ) {
process . on ( "SIGTERM" , ( ) => {
setTimeout ( ( ) => {
appendFileSync ( spawnLog , "graceful-exit\n" ) ;
process . exit ( 0 ) ;
} , 500 ) ;
} ) ;
}
2026-10-23 13:16:50 +02:00
if ( model === "hang-before-ready" ) {
setInterval ( ( ) => { } , 1000 ) ;
} else {
const server = http . createServer ( ( request , response ) => {
const chunks = [ ] ;
request . on ( "data" , ( chunk ) => chunks . push ( chunk ) ) ;
request . on ( "end" , ( ) => {
const send = ( status , body ) => {
response . writeHead ( status , { "content-type" : "application/json" } ) ;
response . end ( JSON . stringify ( body ) ) ;
} ;
if ( request . method === "GET" && request . url === "/health" ) {
healthCalls += 1 ;
Daemon: drop the unread worker.model; tests for grace, host default, relative models root, early death
Mutation triage (StrykerJS, 81 survivors on the first full run) showed worker.model was
written and never read. The new tests pin the survivors that were behaviour: the loopback
default, --host, stray flags, the models-root resolution, the SIGTERM grace, a worker that
dies while the daemon waits for its health, the POST route, and lastError after a
requested stop.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:07:15 +02:00
if ( model === "sick" || model === "die-after-ready" || ( model === "slow-health" && healthCalls <= 2 ) ) return send ( 200 , { ok : false } ) ;
2026-10-24 03:57:06 +02:00
writeFileSync ( path . join ( process . env . VILI _EMBEDDINGS _DIR , "stub-healthy" ) , "" ) ; // a test can act the moment the worker is healthy
2026-10-24 05:15:43 +02:00
if ( model === "chatty" ) console . log ( "after-healthy-line" ) ; // after stub-healthy exists, which a test can make the loss of the lease
2026-10-23 13:16:50 +02:00
send ( 200 , { ok : true , model : model || "stub-model" , displayModel : model || "Stub Model" , pid : process . pid , requests } ) ;
} else if ( request . method === "POST" && request . url === "/generate" ) {
requests += 1 ;
const payload = JSON . parse ( Buffer . concat ( chunks ) . toString ( "utf8" ) ) ;
if ( model === "fail-generate" ) return send ( 500 , { ok : false } ) ;
if ( model === "refuse-generate" ) return send ( 200 , { ok : false } ) ;
if ( model === "drop-generate" ) return request . socket . destroy ( ) ;
2026-10-24 05:15:43 +02:00
if ( model === "slow-generate" || model === "slow-to-stop" ) return ;
if ( model === "writer" ) {
setInterval ( ( ) => appendFileSync ( ` ${ payload . output } .writes ` , ` ${ Date . now ( ) } \n ` ) , 50 ) ;
return ;
}
2026-10-23 17:20:01 +02:00
if ( model === "encoder" ) {
// an encoder child of the real worker: a grandchild in the worker's group, then a request that never answers
2026-10-24 00:23:07 +02:00
spawn ( process . execPath , [ "-e" , ` process.on('SIGTERM', () => {}); require('fs').appendFileSync( ${ JSON . stringify ( spawnLog ) } , 'grandchild ' + process.pid + ' \\ n'); setInterval(() => {}, 1000) ` ] , { stdio : "ignore" } ) ;
2026-10-23 17:20:01 +02:00
return ;
}
2026-10-24 20:26:54 +02:00
let files ;
if ( payload . bvh ) {
const outside = path . join ( path . dirname ( payload . output ) , ".." , "outside.bvh" ) ;
if ( model === "bvh-outside" ) {
writeFileSync ( outside , "BVH-OUTSIDE\n" ) ;
files = [ outside ] ;
} else if ( model === "bvh-link" ) {
writeFileSync ( outside , "BVH-OUTSIDE\n" ) ;
symlinkSync ( outside , ` ${ payload . output } .bvh ` ) ;
files = [ ` ${ payload . output } .bvh ` ] ;
} else if ( payload . numSamples > 1 ) {
mkdirSync ( payload . output , { recursive : true } ) ;
files = Array . from ( { length : payload . numSamples } , ( _ , index ) => path . join ( payload . output , ` motion_ ${ String ( index ) . padStart ( 2 , "0" ) } .bvh ` ) ) ;
for ( const [ index , file ] of files . entries ( ) ) writeFileSync ( file , ` BVH-STUB sample ${ index } \n ` ) ;
2026-10-24 21:00:22 +02:00
files . reverse ( ) ; // the worker's listing order is not the daemon's clip order
2026-10-24 20:26:54 +02:00
} else {
writeFileSync ( ` ${ payload . output } .npz ` , "NPZ-STUB\n" ) ;
writeFileSync ( ` ${ payload . output } .bvh ` , "BVH-STUB\n" ) ;
files = [ ` ${ payload . output } .npz ` , ` ${ payload . output } .bvh ` ] ;
}
}
2026-10-24 23:04:36 +02:00
if ( model === "plant-link" ) {
const outside = path . join ( path . dirname ( payload . output ) , ".." , "outside.bvh" ) ;
2026-10-24 23:15:55 +02:00
writeFileSync ( outside , "BVH-OUTSIDE\n" ) ;
2026-10-24 23:04:36 +02:00
symlinkSync ( outside , ` ${ payload . output } .link ` ) ;
symlinkSync ( path . dirname ( outside ) , ` ${ payload . output } -linkdir ` ) ;
}
2026-10-23 13:16:50 +02:00
send ( 200 , {
ok : true ,
2026-10-24 20:26:54 +02:00
files ,
seed : payload . seed ,
metaSeed : payload . meta ? . seed ,
bvh : payload . bvh ,
bvhStandardTpose : payload . bvhStandardTpose ,
2026-10-23 13:16:50 +02:00
model : model || "stub-model" ,
pid : process . pid ,
requests ,
Daemon: drop the unread worker.model; tests for grace, host default, relative models root, early death
Mutation triage (StrykerJS, 81 survivors on the first full run) showed worker.model was
written and never read. The new tests pin the survivors that were behaviour: the loopback
default, --host, stray flags, the models-root resolution, the SIGTERM grace, a worker that
dies while the daemon waits for its health, the POST route, and lastError after a
requested stop.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:07:15 +02:00
contentType : request . headers [ "content-type" ] ,
2026-10-23 13:16:50 +02:00
output : payload . output ,
prompt : payload . prompt ,
constraints : payload . constraints ,
priority : os . getPriority ( ) ,
env : {
OMP _NUM _THREADS : process . env . OMP _NUM _THREADS ,
MKL _NUM _THREADS : process . env . MKL _NUM _THREADS ,
VILI _THREADS : process . env . VILI _THREADS ,
HF _HUB _OFFLINE : process . env . HF _HUB _OFFLINE ,
TRANSFORMERS _OFFLINE : process . env . TRANSFORMERS _OFFLINE ,
HF _HOME : process . env . HF _HOME ,
PYTHONPATH : process . env . PYTHONPATH ,
PYTHONDONTWRITEBYTECODE : process . env . PYTHONDONTWRITEBYTECODE ,
PYTHONUNBUFFERED : process . env . PYTHONUNBUFFERED ,
VILI _KIMODO _COMMIT : process . env . VILI _KIMODO _COMMIT ,
VILI _PYTHON _ENV : process . env . VILI _PYTHON _ENV ,
PATH : process . env . PATH ,
HOME : process . env . HOME ,
LANG : process . env . LANG ,
LEAK _CHECK : process . env . LEAK _CHECK ,
} ,
2026-10-24 23:04:36 +02:00
envKeys : Object . keys ( process . env ) . sort ( ) ,
2026-10-23 13:16:50 +02:00
argv ,
} ) ;
} else {
response . writeHead ( 404 ) ;
response . end ( ) ;
}
} ) ;
Daemon: supervise the worker child, drop WSL, Docker, Eve and CORS
vili-daemon.mjs spawns the Python worker itself (no shell, port 0 from the
ready line, nice 10, one child at a time, respawn after exit, group kill on
SIGTERM) with a closed environment. WSL/Docker, the Eve surfaces and deck
WebSocket, the provider advertisement, /smoke, CORS, JSON sidecars and the
pid file are deleted. POSTs with an Origin or a non-JSON body are refused
403 cross-site. cultcache-ts comes from the vendor/CultLib submodule at
b549d731; ws is dropped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:14:10 +02:00
} ) ;
2026-10-23 13:16:50 +02:00
server . listen ( Number ( flag ( "--port" ) ) , flag ( "--host" ) , ( ) => {
console . log ( JSON . stringify ( { event : "kimodo-worker-ready" , port : server . address ( ) . port } ) ) ;
Daemon: drop the unread worker.model; tests for grace, host default, relative models root, early death
Mutation triage (StrykerJS, 81 survivors on the first full run) showed worker.model was
written and never read. The new tests pin the survivors that were behaviour: the loopback
default, --host, stray flags, the models-root resolution, the SIGTERM grace, a worker that
dies while the daemon waits for its health, the POST route, and lastError after a
requested stop.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 15:07:15 +02:00
if ( model === "die-after-ready" ) setTimeout ( ( ) => process . exit ( 4 ) , 300 ) ;
2026-10-23 13:16:50 +02:00
} ) ;
}