wishforge-ide/scripts/local-services.test.mjs
eust-w 3623d318af ✨ Sandbox the local POC's protected checks and installations on macOS, by operator opt-in
In the development host mode, protected checks and dependency
installations ran directly as the operator. They run the agent's code:
a test it wrote could read the home directory's secrets, reach the
network, or plant Git configuration that the trusted Git commands after
it would execute.

With WF_HOST_CHECK_SANDBOX=true they run under Seatbelt instead. File
contents in the home directory are unreadable except the command's own
directory and the paths the operator lists; it may write only there,
in the caches the operator lists and in a temporary directory of its
own, never in a .git directory. Only installations reach the network;
checks may use loopback ports by opt-in, for test suites that start
local servers. The platform's own ports (control plane, Temporal and
its web UI, database, object storage, provisioner, Git service, IDE,
console) stay blocked in any case: the local start lists them from
their configured addresses. Checks get no answer from the keychain,
and a command whose home is the executor's (holding its credentials)
gets a private one. A sandbox that cannot start leaves the run waiting
on infrastructure; verification evidence records checkSandbox.

scripts/check-host-check-sandbox.ts checks all of this on the target
Mac, from outside the sandbox, then runs the POC's installation and
checks sandboxed on a recent droboticflow source. Enabled on the local
POC, a real wish passed through both, including a repair round for the
agent's own failing tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-26 10:10:47 +08:00

93 lines
4.1 KiB
JavaScript

import assert from 'node:assert/strict';
import { mkdtemp, readFile, rm } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { platformPorts, startServices, webListener } from './local-services.mjs';
const alive = pid => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
};
async function gone(pid) {
for (let attempt = 0; attempt < 40 && alive(pid); attempt++) await new Promise(resolve => setTimeout(resolve, 50));
return !alive(pid);
}
test('stopping takes down each service with everything it spawned, even a service ignoring SIGTERM', async t => {
const directory = await mkdtemp(path.join(os.tmpdir(), 'wishforge-services-'));
t.after(() => rm(directory, { recursive: true, force: true }));
const pids = path.join(directory, 'pids.json');
// A service that ignores SIGTERM and leaves a child of its own running, like a dev server's workers.
const service = `
const { spawn } = require('node:child_process');
process.on('SIGTERM', () => {});
const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' });
require('node:fs').writeFileSync(${JSON.stringify(pids)}, JSON.stringify([process.pid, child.pid]));
setInterval(() => {}, 1000);
`;
const services = await startServices(
[{ name: 'stubborn', cwd: directory, args: ['-e', service], env: process.env }],
'',
{
graceMs: 300,
},
);
const [leader, grandchild] = JSON.parse(await readFile(pids, 'utf8'));
assert.ok(alive(leader) && alive(grandchild));
await services.stop();
assert.ok(await gone(leader), 'the service itself stopped');
assert.ok(await gone(grandchild), 'the process it spawned stopped too');
process.exitCode = undefined;
});
test('under the supervisor, a service already answering is refused instead of adopted', async t => {
const original = globalThis.fetch;
t.after(() => {
globalThis.fetch = original;
process.exitCode = undefined;
});
globalThis.fetch = async () => Response.json({ service: 'wishforge-control-plane' });
const api = { name: 'api', url: 'http://127.0.0.1:4000/health', identity: 'wishforge-control-plane', args: [] };
await assert.rejects(startServices([api], '', { reuse: false }), /already answers; stop that process/);
// A developer's own start may still reuse it.
await startServices([api], '');
});
test('only the production web build listens beyond loopback, and readiness is checked where it listens', () => {
assert.deepEqual(webListener({}), { host: '127.0.0.1', port: 3000, probe: '127.0.0.1' });
const lan = { WF_LOCAL_ACCESS: 'true', WF_WEB_SERVE_MODE: 'production' };
assert.deepEqual(webListener({ ...lan, WF_WEB_HOST: '0.0.0.0', WF_WEB_PORT: '3100' }), {
host: '0.0.0.0',
port: 3100,
probe: '127.0.0.1',
});
// A listener bound to one address answers only there.
assert.equal(webListener({ ...lan, WF_WEB_HOST: '192.168.1.20' }).probe, '192.168.1.20');
assert.throws(() => webListener({ WF_WEB_HOST: '0.0.0.0', WF_WEB_SERVE_MODE: 'production' }), /local sign-in/);
// The development server hands visitors its sources and hot-reload channel.
assert.throws(() => webListener({ WF_WEB_HOST: '0.0.0.0', WF_LOCAL_ACCESS: 'true' }), /production web build/);
assert.equal(webListener({ WF_WEB_HOST: 'localhost', WF_WEB_SERVE_MODE: 'development' }).host, 'localhost');
assert.throws(() => webListener({ WF_WEB_PORT: '70000' }), /TCP port/);
});
test("the platform's own ports come from its configured addresses", () => {
assert.deepEqual(
platformPorts({
WF_API_URL: 'http://127.0.0.1:4000',
DATABASE_URL: 'postgresql://user:password@127.0.0.1:55432/wishforge',
TEMPORAL_ADDRESS: '127.0.0.1:7233',
S3_ENDPOINT: 'http://127.0.0.1:49000',
WF_PROVISIONER_URL: 'http://127.0.0.1:4450',
WF_IDE_ALLOWED_UPSTREAM_ORIGINS: 'http://127.0.0.1:4300, http://localhost:4301',
WF_HOST_CHECK_BLOCKED_PORTS: '6379',
WF_IDE_PUBLIC_URL: 'not an address',
}),
[3000, 4000, 4300, 4301, 4450, 4504, 6379, 7233, 8233, 49000, 55432],
);
});