eust-w 13ceca5c8f
Some checks failed
CI / Format, types and tests (pull_request) Has been cancelled
CI / PostgreSQL migrations and integration tests (pull_request) Has been cancelled
🔀 Merge review/overhaul into feat/wiki-copilot-diagnostics
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>
2026-11-12 00:39:05 +08:00

234 lines
9.7 KiB
JavaScript

import { spawn } from 'node:child_process';
import { mkdir, rm } from 'node:fs/promises';
import { homedir } from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { verifyResourceGrant } from './resource-grants.mjs';
const id = /^[A-Za-z0-9_-]{1,128}$/;
const SOURCE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..');
/** Process environment a worker inherits from this host; nothing else from the provisioner's own environment. */
const PASSTHROUGH = [
'PATH',
'HOME',
'LANG',
'TMPDIR',
// Both spellings, because a host usually exports only the lower-case pair: without the proxy the
// executor's CLI cannot reach a gateway outside this machine. Kept in step with PROXY_VARIABLES.
'HTTP_PROXY',
'HTTPS_PROXY',
'NO_PROXY',
'http_proxy',
'https_proxy',
'no_proxy',
'WF_GIT_ALLOW_LOCAL_HTTP',
'WF_LOCAL_POC_BINDING_FILE',
'WF_CODEBUDDY_BIN',
'WF_CLAUDE_BIN',
'WF_CLAUDE_COMMAND_SANDBOX',
'WF_CLAUDE_COMMAND_READ_PATHS',
'WF_CLAUDE_COMMAND_ENV',
'WF_HOST_CHECK_SANDBOX',
'WF_HOST_CHECK_READ_PATHS',
'WF_HOST_CHECK_WRITE_PATHS',
'WF_HOST_CHECK_LOOPBACK',
'WF_HOST_CHECK_BLOCKED_PORTS',
'WF_HOST_CHECK_KUBECONFIG',
'WF_HOST_CHECK_KUBECTL',
'WF_HOST_CHECK_POD_NAMESPACE',
'WF_HOST_CHECK_POD_IMAGE',
'WF_HOST_CHECK_POD_CLAIM',
'WF_HOST_CHECK_POD_ROOT',
'WF_HOST_CHECK_POD_CPU',
'WF_HOST_CHECK_POD_MEMORY',
'WF_DOCKER_CONFIG',
'TEMPORAL_NAMESPACE_PREFIX',
'TEMPORAL_TLS_CA',
'TEMPORAL_TLS_CERT',
'TEMPORAL_TLS_KEY',
'TEMPORAL_TLS_SERVER_NAME',
];
function required(env, name) {
if (!env[name]) throw new Error(`unconfigured: ${name} is required`);
return env[name];
}
/**
* Development backend: each run generation gets an executor and a verifier process on
* this host, with their own directories and run-scoped tokens. There is no isolation,
* so it refuses to start outside the development deployment mode.
*/
export function readLocalConfig(env = process.env, cwd = process.cwd()) {
if (env.WF_DEPLOYMENT_MODE !== 'development')
throw new Error('The local provisioner runs project code on this host and requires WF_DEPLOYMENT_MODE=development');
const config = {
tenantId: required(env, 'WF_TENANT_ID'),
resourceGrantSecret: required(env, 'WF_RESOURCE_GRANT_SECRET'),
controlPlaneUrl: env.WF_CONTROL_PLANE_URL ?? 'http://127.0.0.1:4000',
temporalAddress: env.TEMPORAL_ADDRESS ?? '127.0.0.1:7233',
temporalNamespace: env.TEMPORAL_NAMESPACE ?? 'default',
runRoot: path.resolve(cwd, env.WF_LOCAL_RUN_ROOT ?? path.join(homedir(), '.local/share/wishforge/runs')),
cwd,
workerEntry: path.resolve(cwd, env.WF_LOCAL_WORKER_ENTRY ?? 'apps/worker/src/main.ts'),
claudeAuthHome: env.WF_CLAUDE_LOCAL_AUTH_HOME,
passthrough: Object.fromEntries(PASSTHROUGH.filter(name => env[name]).map(name => [name, env[name]])),
// Grants are verified like in production, but the host has no image and no fixed caps.
acceptAnyImage: true,
cpuMilliCap: 64000,
memoryMiBCap: 262144,
storageSize: '1024Gi',
};
// Node and TypeScript search ancestor node_modules. A nested project would silently
// inherit WishForge's React/types instead of checking only its own locked dependencies.
const relativeToSource = path.relative(SOURCE_ROOT, config.runRoot);
if (
!relativeToSource ||
(!relativeToSource.startsWith(`..${path.sep}`) && relativeToSource !== '..' && !path.isAbsolute(relativeToSource))
)
throw new Error(
'WF_LOCAL_RUN_ROOT must be outside the WishForge source tree; use ~/.local/share/wishforge/runs and recover old attempts from persisted checkpoints',
);
if (!id.test(config.tenantId)) throw new Error('Invalid WF_TENANT_ID');
if (config.claudeAuthHome && !path.isAbsolute(config.claudeAuthHome))
throw new Error('WF_CLAUDE_LOCAL_AUTH_HOME must be an absolute path');
if (config.resourceGrantSecret.length < 32)
throw new Error('WF_RESOURCE_GRANT_SECRET must be at least 32 characters');
return config;
}
function scope(config, request, keys) {
if (
!request ||
Object.keys(request).some(key => !keys.includes(key)) ||
request.tenantId !== config.tenantId ||
!id.test(request.runId ?? '') ||
!id.test(request.generation ?? '')
)
throw new Error('INVALID_RUN_REQUEST');
return `${request.tenantId}\0${request.runId}\0${request.generation}`;
}
function stopProcess(child, timeoutMs = 10_000) {
if (child.exitCode !== null || child.signalCode !== null) return Promise.resolve();
return new Promise(resolve => {
const timer = setTimeout(() => child.kill('SIGKILL'), timeoutMs);
child.once('exit', () => {
clearTimeout(timer);
resolve();
});
child.kill('SIGTERM');
});
}
export function createLocalBackend(config, { spawnProcess = spawn, log = line => console.log(line) } = {}) {
/** Live worker processes per tenant, run and generation. */
const generations = new Map();
return {
tenantId: config.tenantId,
async provisionRun(request) {
const key = scope(config, request, [
'tenantId',
'runId',
'generation',
'executorToken',
'verifierToken',
'resourceGrant',
]);
verifyResourceGrant(config, request);
if (typeof request.executorToken !== 'string' || typeof request.verifierToken !== 'string')
throw new Error('INVALID_RUN_REQUEST');
if (request.executorToken === request.verifierToken) throw new Error('INVALID_RUN_REQUEST');
// A repeated request for the same generation (an ambiguous earlier response) starts nothing new.
if (!generations.has(key)) {
const directory = path.join(config.runRoot, request.tenantId, request.runId, request.generation);
const children = [];
for (const role of ['executor', 'verifier']) {
const workspaceRoot = path.join(directory, role);
await mkdir(workspaceRoot, { recursive: true, mode: 0o700 });
const child = spawnProcess(process.execPath, ['--import', 'tsx', config.workerEntry], {
cwd: config.cwd,
stdio: ['ignore', 'pipe', 'pipe'],
env: {
...config.passthrough,
WF_DEPLOYMENT_MODE: 'development',
WF_WORKER_ROLE: role,
...(role === 'executor'
? {
WF_WORKER_TOKEN: request.executorToken,
...(config.claudeAuthHome ? { WF_CLAUDE_LOCAL_AUTH_HOME: config.claudeAuthHome } : {}),
}
: { WF_VERIFIER_TOKEN: request.verifierToken }),
WF_SANDBOX_MODE: 'host',
WF_SANDBOX_TENANT_ID: request.tenantId,
WF_SANDBOX_RUN_ID: request.runId,
WF_SANDBOX_GENERATION: request.generation,
WF_WORKER_ID: `${role}-${request.generation.slice(0, 8)}`,
WF_WORKSPACE_ROOT: workspaceRoot,
WF_PROVIDER_HOME_ROOT: path.join(directory, 'provider-home'),
WF_CONTROL_PLANE_URL: config.controlPlaneUrl,
TEMPORAL_ADDRESS: config.temporalAddress,
TEMPORAL_NAMESPACE: config.temporalNamespace,
},
});
const label = `[${role} ${request.runId.slice(0, 8)}/${request.generation.slice(0, 8)}]`;
for (const stream of [child.stdout, child.stderr])
stream?.on('data', bytes =>
String(bytes)
.split('\n')
.filter(Boolean)
.forEach(line => log(`${label} ${line}`)),
);
child.once('exit', code => {
log(`${label} exited (${code ?? 'signal'})`);
// A generation whose workers all stopped by themselves no longer holds the run.
if (
generations.get(key) === children &&
children.every(other => other.exitCode !== null || other.signalCode !== null)
)
generations.delete(key);
});
children.push(child);
}
generations.set(key, children);
}
return {
status: 'provisioned',
tenantId: request.tenantId,
runId: request.runId,
generation: request.generation,
};
},
/** Removes the directories of every generation of a run once none of them is running. */
async purgeRun(request) {
scope(config, request, ['tenantId', 'runId', 'generation', 'resourceGrant']);
verifyResourceGrant(config, request, { allowExpired: true, allowRetiredImage: true, cleanup: true });
const receipt = { tenantId: request.tenantId, runId: request.runId, generation: request.generation };
const prefix = `${request.tenantId}\0${request.runId}\0`;
if ([...generations.keys()].some(key => key.startsWith(prefix)))
return { ...receipt, status: 'busy', persistentStorageRetained: true };
await rm(path.join(config.runRoot, request.tenantId, request.runId), { recursive: true, force: true });
return { ...receipt, status: 'purged', persistentStorageRetained: false };
},
async releaseRun(request) {
const key = scope(config, request, ['tenantId', 'runId', 'generation', 'resourceGrant']);
verifyResourceGrant(config, request, { allowExpired: true, allowRetiredImage: true, cleanup: true });
await Promise.all((generations.get(key) ?? []).map(child => stopProcess(child)));
generations.delete(key);
// Run directories stay on disk like the retained volumes of the Kubernetes backend.
return {
tenantId: request.tenantId,
runId: request.runId,
generation: request.generation,
status: 'released',
computeReleased: true,
persistentStorageRetained: true,
};
},
async close() {
await Promise.all([...generations.values()].flat().map(child => stopProcess(child, 3000)));
generations.clear();
},
};
}