wishforge-ide/scripts/local-services.mjs
eust-w a9411f586b feat: run the whole loop locally with a local provisioner
Local development had no provisioner, so every wish stopped at "sandbox not
ready", and dev:all started neither the coordinator nor the deployer.

- The provisioner moves from apps/ide to apps/provisioner and gains a backend
  switch: `local` starts an executor and a verifier process per run generation
  on this host (WF_SANDBOX_MODE=host, own directories and run-scoped tokens,
  development mode only) and points workspaces at the local Theia; `kubernetes`
  keeps the Kata Jobs, now started with the image entrypoint instead of the npm dev
  script and allowed to reach the platform Git service.
- In development the resource grant needs no runtime image digest.
- setup:local is one idempotent script that creates or completes .env.local,
  including the deployment mode, provisioner and grant settings.
- dev:all starts Git, API, provisioner, coordinator, deployer and web; dev:ide
  starts Theia and the IDE gateway; ide:install and ide:build wrap the separate
  Theia package. npm test also runs the provisioner tests.
- Project settings accept the target branch and dependency install commands.
- Web development conveniences require WF_DEPLOYMENT_MODE=development.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 15:36:12 +08:00

100 lines
3.3 KiB
JavaScript

// Starts local development processes, reuses ones that are already running and
// stops everything it started when one of them exits.
import { spawn } from 'node:child_process';
import { setTimeout as delay } from 'node:timers/promises';
export function loadLocalEnvironment() {
try {
process.loadEnvFile('.env.local');
} catch {
throw new Error('.env.local is missing; run npm run setup:local first');
}
if (process.env.WF_DEPLOYMENT_MODE !== 'development')
throw new Error('Local services need WF_DEPLOYMENT_MODE=development in .env.local; run npm run setup:local');
}
const inherited = () =>
Object.fromEntries(
['PATH', 'HOME', 'TMPDIR', 'LANG', 'TERM', 'HTTP_PROXY', 'HTTPS_PROXY', 'NO_PROXY']
.filter(key => process.env[key])
.map(key => [key, process.env[key]]),
);
/** Each process sees only the settings matching its patterns, plus the deployment mode. */
export function environment(patterns, extra = {}) {
return {
...inherited(),
...Object.fromEntries(Object.entries(process.env).filter(([key]) => patterns.some(pattern => pattern.test(key)))),
WF_DEPLOYMENT_MODE: 'development',
NODE_ENV: 'development',
...extra,
};
}
async function healthy(service) {
try {
const response = await fetch(service.url, { signal: AbortSignal.timeout(1000) });
if (!response.ok) return false;
if (!service.identity) return true;
return (await response.json()).service === service.identity;
} catch {
return false;
}
}
/**
* `url` is a readiness check; a service that answers there already is reused. Services
* without one (Temporal workers) count as ready once they stay up for a moment.
*/
export async function startServices(services, banner) {
const children = [];
let stopping = false;
const stop = (code = 0) => {
if (stopping) return;
stopping = true;
for (const child of children) child.kill('SIGTERM');
process.exitCode = code;
};
for (const signal of ['SIGINT', 'SIGTERM']) process.once(signal, () => stop());
for (const service of services) {
if (stopping) return;
if (service.url && (await healthy(service))) {
console.log(`${service.name}: reusing the service already running at ${service.url}`);
continue;
}
const child = spawn(process.execPath, service.args, {
cwd: service.cwd,
env: service.env,
stdio: ['ignore', 'pipe', 'pipe'],
});
children.push(child);
for (const output of [child.stdout, child.stderr])
output.on('data', bytes =>
String(bytes)
.split('\n')
.filter(Boolean)
.forEach(line => console.log(`[${service.name}] ${line}`)),
);
child.once('exit', code => {
if (!stopping) {
console.error(`${service.name} exited (${code}); stopping the services this command started.`);
stop(code || 1);
}
});
let ready = false;
for (let attempt = 0; attempt < (service.url ? 90 : 2) && !stopping; attempt++) {
if (service.url && (await healthy(service))) {
ready = true;
break;
}
await delay(1000);
}
if (!service.url && !stopping) ready = true;
if (!ready) {
stop(1);
throw new Error(`${service.name} did not become ready; check its output above.`);
}
}
console.log(banner);
if (!children.length) console.log('Every service was already running; nothing was started.');
}