Brings the branch up to the current base (executor settings, project skills, Chinese run messages, cloud IDE removal, wish attachments) and resolves the ten conflicted files, keeping both sides: - wish-refiner: the tenant's executor settings pick the models first, then the Copilot model setting; the tenant credential still goes before the operator's login - worker providers: executor settings apply, and a run with its own credential still gets an empty home - main.ts: Copilot screenshot uploads and wish attachments each keep their own body limit - contracts, http, web types: default budget and skill auto-activation, Copilot and attachment entity kinds, ./text and ./attachments exports - globals.css: Copilot styles and attachment styles, one after the other - tests: the canceled-release reason is Chinese; the refiner's default models have no [1m] suffix, as this branch names them Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
247 lines
9.3 KiB
JavaScript
247 lines
9.3 KiB
JavaScript
import test from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { EventEmitter } from 'node:events';
|
||
import { createHmac } from 'node:crypto';
|
||
import { mkdtemp, rm, stat } from 'node:fs/promises';
|
||
import os from 'node:os';
|
||
import path from 'node:path';
|
||
import { fileURLToPath } from 'node:url';
|
||
import { createLocalBackend, readLocalConfig } from '../local.mjs';
|
||
import { resourceGrantPayload } from '../resource-grants.mjs';
|
||
|
||
const secret = 'local-resource-grant-secret-for-tests';
|
||
const now = Math.floor(Date.now() / 1000);
|
||
function grant(overrides = {}) {
|
||
const payload = {
|
||
schemaVersion: 1,
|
||
tenantId: 'demo',
|
||
runId: 'run-1',
|
||
generation: 'generation-1',
|
||
planId: 'legacy',
|
||
planVersion: 'legacy-v1',
|
||
image: 'local-host',
|
||
cpuMilli: 2000,
|
||
memoryMiB: 4096,
|
||
storageMiB: 10240,
|
||
issuedAt: now,
|
||
expiresAt: now + 300,
|
||
...overrides,
|
||
};
|
||
return {
|
||
...payload,
|
||
signature: createHmac('sha256', secret).update(resourceGrantPayload(payload)).digest('base64url'),
|
||
};
|
||
}
|
||
const request = (overrides = {}) => ({
|
||
tenantId: 'demo',
|
||
runId: 'run-1',
|
||
generation: 'generation-1',
|
||
executorToken: 'executor-token',
|
||
verifierToken: 'verifier-token',
|
||
resourceGrant: grant(),
|
||
...overrides,
|
||
});
|
||
/** A child process double that records how it was started and exits when signalled. */
|
||
function fakeSpawn(started) {
|
||
return (command, args, options) => {
|
||
const child = new EventEmitter();
|
||
Object.assign(child, {
|
||
command,
|
||
args,
|
||
options,
|
||
exitCode: null,
|
||
signalCode: null,
|
||
stdout: new EventEmitter(),
|
||
stderr: new EventEmitter(),
|
||
});
|
||
child.kill = signal => {
|
||
child.signalCode = signal;
|
||
queueMicrotask(() => child.emit('exit', null, signal));
|
||
};
|
||
started.push(child);
|
||
return child;
|
||
};
|
||
}
|
||
async function fixture(env = {}) {
|
||
const root = await mkdtemp(path.join(os.tmpdir(), 'wishforge-local-provisioner-'));
|
||
const config = readLocalConfig(
|
||
{
|
||
WF_DEPLOYMENT_MODE: 'development',
|
||
WF_TENANT_ID: 'demo',
|
||
WF_RESOURCE_GRANT_SECRET: secret,
|
||
WF_LOCAL_RUN_ROOT: path.join(root, 'runs'),
|
||
PATH: '/usr/bin',
|
||
WF_GIT_ALLOW_LOCAL_HTTP: 'true',
|
||
DATABASE_URL: 'postgresql://must-not-leak',
|
||
WF_PROVISIONER_TOKEN: 'must-not-leak-either',
|
||
...env,
|
||
},
|
||
root,
|
||
);
|
||
const started = [];
|
||
const logs = [];
|
||
const backend = createLocalBackend(config, { spawnProcess: fakeSpawn(started), log: line => logs.push(line) });
|
||
return { root, config, backend, started, logs, cleanup: () => rm(root, { recursive: true, force: true }) };
|
||
}
|
||
|
||
test('refuses to run outside development and requires the grant secret', () => {
|
||
assert.throws(
|
||
() => readLocalConfig({ WF_DEPLOYMENT_MODE: 'production', WF_TENANT_ID: 'demo', WF_RESOURCE_GRANT_SECRET: secret }),
|
||
/development/,
|
||
);
|
||
assert.throws(
|
||
() => readLocalConfig({ WF_DEPLOYMENT_MODE: 'development', WF_TENANT_ID: 'demo' }),
|
||
/WF_RESOURCE_GRANT_SECRET/,
|
||
);
|
||
});
|
||
|
||
test('hands both spellings of the proxy settings to the workers', () => {
|
||
// A host usually exports only the lower-case pair; a worker without the proxy cannot reach a
|
||
// gateway that lives outside this machine, and its CLI then hangs with nothing to say.
|
||
const { passthrough } = readLocalConfig(
|
||
{
|
||
WF_DEPLOYMENT_MODE: 'development',
|
||
WF_TENANT_ID: 'demo',
|
||
WF_RESOURCE_GRANT_SECRET: secret,
|
||
http_proxy: 'http://proxy.local:7890',
|
||
https_proxy: 'http://proxy.local:7890',
|
||
no_proxy: 'localhost,127.0.0.1',
|
||
NO_PROXY: 'localhost',
|
||
DATABASE_URL: 'postgresql://must-not-leak',
|
||
},
|
||
os.tmpdir(),
|
||
);
|
||
assert.deepEqual(passthrough, {
|
||
NO_PROXY: 'localhost',
|
||
http_proxy: 'http://proxy.local:7890',
|
||
https_proxy: 'http://proxy.local:7890',
|
||
no_proxy: 'localhost,127.0.0.1',
|
||
});
|
||
});
|
||
|
||
test('places default workspaces outside platform dependencies and rejects nested roots', () => {
|
||
const env = { WF_DEPLOYMENT_MODE: 'development', WF_TENANT_ID: 'demo', WF_RESOURCE_GRANT_SECRET: secret };
|
||
const source = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..');
|
||
assert.equal(readLocalConfig(env, source).runRoot, path.join(os.homedir(), '.local/share/wishforge/runs'));
|
||
for (const runRoot of [source, path.join(source, '.wishforge/runs'), '.wishforge/runs'])
|
||
assert.throws(
|
||
() => readLocalConfig({ ...env, WF_LOCAL_RUN_ROOT: runRoot }, source),
|
||
/outside the WishForge source tree/,
|
||
);
|
||
assert.equal(readLocalConfig({ ...env, WF_LOCAL_RUN_ROOT: `${source}-runs` }, source).runRoot, `${source}-runs`);
|
||
const temporary = path.join(os.tmpdir(), 'standalone-local-test');
|
||
assert.equal(readLocalConfig({ ...env, WF_LOCAL_RUN_ROOT: 'runs' }, temporary).runRoot, path.join(temporary, 'runs'));
|
||
});
|
||
|
||
test('starts one executor and one verifier per generation with only their own credentials', async () => {
|
||
const f = await fixture({ WF_CLAUDE_LOCAL_AUTH_HOME: '/home/operator' });
|
||
try {
|
||
assert.deepEqual(await f.backend.provisionRun(request()), {
|
||
status: 'provisioned',
|
||
tenantId: 'demo',
|
||
runId: 'run-1',
|
||
generation: 'generation-1',
|
||
});
|
||
// A repeated request after an ambiguous response starts nothing new.
|
||
await f.backend.provisionRun(request());
|
||
assert.equal(f.started.length, 2);
|
||
const [executor, verifier] = f.started;
|
||
assert.equal(executor.options.env.WF_WORKER_ROLE, 'executor');
|
||
assert.equal(executor.options.env.WF_WORKER_TOKEN, 'executor-token');
|
||
assert.equal(executor.options.env.WF_VERIFIER_TOKEN, undefined);
|
||
assert.equal(verifier.options.env.WF_VERIFIER_TOKEN, 'verifier-token');
|
||
assert.equal(verifier.options.env.WF_WORKER_TOKEN, undefined);
|
||
assert.equal(executor.options.env.WF_CLAUDE_LOCAL_AUTH_HOME, '/home/operator');
|
||
assert.equal(verifier.options.env.WF_CLAUDE_LOCAL_AUTH_HOME, undefined);
|
||
for (const child of f.started) {
|
||
assert.equal(child.options.env.WF_SANDBOX_MODE, 'host');
|
||
assert.equal(child.options.env.WF_SANDBOX_GENERATION, 'generation-1');
|
||
assert.equal(child.options.env.WF_GIT_ALLOW_LOCAL_HTTP, 'true');
|
||
assert.equal(child.options.env.DATABASE_URL, undefined);
|
||
assert.equal(child.options.env.WF_PROVISIONER_TOKEN, undefined);
|
||
assert.ok((await stat(child.options.env.WF_WORKSPACE_ROOT)).isDirectory());
|
||
}
|
||
assert.notEqual(executor.options.env.WF_WORKSPACE_ROOT, verifier.options.env.WF_WORKSPACE_ROOT);
|
||
executor.stdout.emit('data', Buffer.from('worker ready\n'));
|
||
assert.ok(f.logs.some(line => line.includes('[executor run-1/generati] worker ready')));
|
||
|
||
const released = await f.backend.releaseRun({
|
||
tenantId: 'demo',
|
||
runId: 'run-1',
|
||
generation: 'generation-1',
|
||
resourceGrant: grant(),
|
||
});
|
||
assert.deepEqual(released, {
|
||
tenantId: 'demo',
|
||
runId: 'run-1',
|
||
generation: 'generation-1',
|
||
status: 'released',
|
||
computeReleased: true,
|
||
persistentStorageRetained: true,
|
||
});
|
||
assert.deepEqual(
|
||
f.started.map(child => child.signalCode),
|
||
['SIGTERM', 'SIGTERM'],
|
||
);
|
||
} finally {
|
||
await f.cleanup();
|
||
}
|
||
});
|
||
|
||
test('removes a run’s directories only once none of its generations is running', async () => {
|
||
const f = await fixture();
|
||
try {
|
||
await f.backend.provisionRun(request());
|
||
const purge = { tenantId: 'demo', runId: 'run-1', generation: 'generation-1', resourceGrant: grant() };
|
||
assert.equal((await f.backend.purgeRun(purge)).status, 'busy');
|
||
await f.backend.releaseRun(purge);
|
||
const directory = path.join(f.config.runRoot, 'demo', 'run-1');
|
||
assert.ok((await stat(directory)).isDirectory());
|
||
assert.deepEqual(await f.backend.purgeRun(purge), {
|
||
tenantId: 'demo',
|
||
runId: 'run-1',
|
||
generation: 'generation-1',
|
||
status: 'purged',
|
||
persistentStorageRetained: false,
|
||
});
|
||
await assert.rejects(stat(directory), { code: 'ENOENT' });
|
||
await assert.rejects(f.backend.purgeRun({ ...purge, runId: '../escape' }));
|
||
} finally {
|
||
await f.cleanup();
|
||
}
|
||
});
|
||
|
||
test('lets a run’s directories go once its workers stopped by themselves', async () => {
|
||
const f = await fixture();
|
||
try {
|
||
await f.backend.provisionRun(request());
|
||
const purge = { tenantId: 'demo', runId: 'run-1', generation: 'generation-1', resourceGrant: grant() };
|
||
const [executor, verifier] = f.started;
|
||
executor.exitCode = 0;
|
||
executor.emit('exit', 0, null);
|
||
assert.equal((await f.backend.purgeRun(purge)).status, 'busy');
|
||
verifier.exitCode = 0;
|
||
verifier.emit('exit', 0, null);
|
||
assert.equal((await f.backend.purgeRun(purge)).status, 'purged');
|
||
} finally {
|
||
await f.cleanup();
|
||
}
|
||
});
|
||
|
||
test('rejects forged, foreign or malformed requests before starting anything', async () => {
|
||
const f = await fixture();
|
||
try {
|
||
await assert.rejects(
|
||
f.backend.provisionRun(request({ resourceGrant: { ...grant(), signature: 'forged' } })),
|
||
/RESOURCE_GRANT_INVALID/,
|
||
);
|
||
await assert.rejects(f.backend.provisionRun(request({ tenantId: 'other' })), /INVALID_RUN_REQUEST/);
|
||
await assert.rejects(f.backend.provisionRun(request({ runId: '../escape' })), /INVALID_RUN_REQUEST/);
|
||
await assert.rejects(f.backend.provisionRun(request({ verifierToken: 'executor-token' })), /INVALID_RUN_REQUEST/);
|
||
await assert.rejects(f.backend.provisionRun({ ...request(), command: 'rm -rf /' }), /INVALID_RUN_REQUEST/);
|
||
assert.equal(f.started.length, 0);
|
||
} finally {
|
||
await f.cleanup();
|
||
}
|
||
});
|