feat(research): isolate candidates and add reviewed paper promotion
This commit is contained in:
parent
26b97265c6
commit
25b5cbf481
128 changed files with 13038 additions and 1388 deletions
119
scripts/build-candidate-sandbox.ts
Normal file
119
scripts/build-candidate-sandbox.ts
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
/** Build only trusted host/runtime files. No candidate source is evaluated by this script. */
|
||||
import { execFile } from 'node:child_process';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { copyFile, mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { dirname, join, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { promisify } from 'node:util';
|
||||
import {
|
||||
candidateExecutionSchema,
|
||||
candidateSandboxLimits,
|
||||
} from '../apps/stock/config/src/contracts/candidate-execution';
|
||||
|
||||
const execute = promisify(execFile);
|
||||
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
|
||||
const dist = join(root, 'apps/stock/orchestrator/dist');
|
||||
const digest = async (path: string) =>
|
||||
createHash('sha256')
|
||||
.update(await readFile(path))
|
||||
.digest('hex');
|
||||
if (process.platform !== 'linux' || process.arch !== 'x64') {
|
||||
throw new Error('Candidate sandbox currently requires Linux x86_64');
|
||||
}
|
||||
await mkdir(dist, { recursive: true });
|
||||
const host = join(dist, 'candidate-host.js');
|
||||
const guardian = join(dist, 'candidate-guardian');
|
||||
const built = await Bun.build({
|
||||
entrypoints: [join(root, 'apps/stock/orchestrator/src/backtest/candidate-host.ts')],
|
||||
target: 'bun',
|
||||
minify: false,
|
||||
outdir: dist,
|
||||
naming: '[name].js',
|
||||
});
|
||||
if (!built.success) {
|
||||
throw new Error(`Candidate host build failed: ${built.logs.join('\n')}`);
|
||||
}
|
||||
await execute('rustc', [
|
||||
join(root, 'apps/stock/orchestrator/src/backtest/sandbox/guardian.rs'),
|
||||
'--edition=2021',
|
||||
'-O',
|
||||
'-o',
|
||||
guardian,
|
||||
]);
|
||||
const bun = await realpath(process.execPath);
|
||||
const context = await mkdtemp(join(tmpdir(), 'stock-bot-candidate-image-'));
|
||||
try {
|
||||
const copy = async (source: string, destination: string) => {
|
||||
const path = join(context, destination);
|
||||
await mkdir(dirname(path), { recursive: true });
|
||||
await copyFile(source, path);
|
||||
};
|
||||
await copy(bun, 'usr/bin/bun');
|
||||
await copy(guardian, 'usr/bin/guardian');
|
||||
await copy(host, 'app/candidate-host.js');
|
||||
const libraries = new Set<string>();
|
||||
for (const binary of [bun, guardian]) {
|
||||
const output = (await execute('/usr/bin/ldd', [binary])).stdout;
|
||||
for (const line of output.split('\n')) {
|
||||
const path = /(?:=>\s+)?(\/[^\s]+)\s+\(/.exec(line)?.[1];
|
||||
if (!path) {
|
||||
if (line.includes('not found')) {
|
||||
throw new Error('Required runtime library is missing');
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!/^\/(?:lib(?:64)?|usr\/lib)\//.test(path)) {
|
||||
throw new Error('Runtime library lies outside system libraries');
|
||||
}
|
||||
libraries.add(path);
|
||||
}
|
||||
}
|
||||
for (const library of libraries) {
|
||||
await copy(library, library.replace(/^\//, ''));
|
||||
}
|
||||
await writeFile(
|
||||
join(context, 'Dockerfile'),
|
||||
'FROM scratch\nCOPY usr/ /usr/\nCOPY lib/ /lib/\nCOPY lib64/ /lib64/\nCOPY app/ /app/\nENTRYPOINT ["/usr/bin/guardian"]\n'
|
||||
);
|
||||
const imageFile = join(context, 'image-id');
|
||||
const dockerConfig = join(context, 'docker-config');
|
||||
await mkdir(dockerConfig, { mode: 0o700 });
|
||||
await execute(
|
||||
'/usr/bin/docker',
|
||||
[
|
||||
'--host',
|
||||
'unix:///var/run/docker.sock',
|
||||
'build',
|
||||
'--network=none',
|
||||
'--pull=false',
|
||||
'--iidfile',
|
||||
imageFile,
|
||||
context,
|
||||
],
|
||||
{
|
||||
env: { PATH: '/usr/bin:/bin', DOCKER_CONFIG: dockerConfig },
|
||||
timeout: 120000,
|
||||
maxBuffer: 1024 * 1024,
|
||||
}
|
||||
);
|
||||
const descriptor = candidateExecutionSchema.parse({
|
||||
kind: 'typescript-isolated-candidate/v1',
|
||||
imageId: (await readFile(imageFile, 'utf8')).trim(),
|
||||
profile: {
|
||||
version: 1,
|
||||
policy: 'linux-docker-guardian/v1',
|
||||
protocol: 'candidate-controller/v1',
|
||||
platform: 'linux',
|
||||
arch: 'x64',
|
||||
hostSha256: await digest(host),
|
||||
guardianSha256: await digest(guardian),
|
||||
bunSha256: await digest(bun),
|
||||
limits: candidateSandboxLimits,
|
||||
},
|
||||
});
|
||||
await writeFile(join(dist, 'candidate-sandbox.json'), JSON.stringify(descriptor, null, 2) + '\n');
|
||||
console.log(JSON.stringify(descriptor));
|
||||
} finally {
|
||||
await rm(context, { recursive: true, force: true });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue