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
|
|
@ -1,14 +1,21 @@
|
|||
import { createHash } from 'node:crypto';
|
||||
import type { StrategyProgram } from '../contracts/canonical-strategy';
|
||||
import type { StrategyReplayResult } from '../contracts/strategy-replay';
|
||||
import {
|
||||
createBuiltinStrategy,
|
||||
validateBuiltinStrategyBindings,
|
||||
} from '../strategies/canonical-builtins';
|
||||
import { freeze } from '../strategies/strategy-decision';
|
||||
import {
|
||||
importRetainedProgram,
|
||||
verifyRetainedProgramPackage,
|
||||
} from '../strategies/retained-program';
|
||||
import { IsolatedCandidateController } from './candidate-sandbox';
|
||||
import { verifyCanonicalWorkerBuild } from './canonical-build';
|
||||
import { buildCanonicalBacktestReport } from './canonical-result';
|
||||
import { executeReplayTrace, prepareReplayLoad } from './native-replay';
|
||||
import {
|
||||
executeReplayTrace,
|
||||
NativeReplaySession,
|
||||
prepareReplayLoad,
|
||||
runReplaySessionAsync,
|
||||
} from './native-replay';
|
||||
import {
|
||||
nativeWorkerRequestSchema,
|
||||
type NativeWorkerMessage,
|
||||
|
|
@ -34,17 +41,7 @@ process.once('message', async (input: unknown) => {
|
|||
const admitted = request.request;
|
||||
// Identity checks precede bundle import, including any top-level module side effects.
|
||||
const build = await verifyCanonicalWorkerBuild(admitted.build, import.meta.url);
|
||||
const pkg = admitted.strategy.package;
|
||||
const hash = (content: string) => createHash('sha256').update(content, 'utf8').digest('hex');
|
||||
if (
|
||||
hash(pkg.artifact.content) !== pkg.artifact.sha256 ||
|
||||
hash(pkg.dependencyLock.content) !== pkg.dependencyLock.sha256
|
||||
) {
|
||||
throw new Error('Retained strategy artifact or dependency lock hash mismatch');
|
||||
}
|
||||
if (pkg.program.apiVersion !== 'canonical-ts/v1') {
|
||||
throw new Error('Unsupported retained strategy API version');
|
||||
}
|
||||
const pkg = verifyRetainedProgramPackage(admitted.strategy.package);
|
||||
const load = prepareReplayLoad(request.dataset, admitted.policy);
|
||||
if (
|
||||
load.identity.datasetHash !== admitted.datasetHash ||
|
||||
|
|
@ -53,50 +50,48 @@ process.once('message', async (input: unknown) => {
|
|||
) {
|
||||
throw new Error('Retained strategy requires its exact eligible single-series dataset');
|
||||
}
|
||||
const selection = admitted.strategy.selection;
|
||||
validateBuiltinStrategyBindings([selection], load);
|
||||
// Only server-admitted built-ins reach this trusted worker path. Candidate
|
||||
// execution requires separate isolation; import is never performed by an API.
|
||||
// Bun's data-URL loader rejects real bundle-sized specifiers. A Blob keeps
|
||||
// these exact UTF-8 bytes in memory without temporary files surviving SIGKILL.
|
||||
const moduleUrl = URL.createObjectURL(
|
||||
new Blob([pkg.artifact.content], { type: 'text/javascript' })
|
||||
);
|
||||
let module: Record<string, unknown>;
|
||||
try {
|
||||
module = await import(moduleUrl);
|
||||
} finally {
|
||||
URL.revokeObjectURL(moduleUrl);
|
||||
}
|
||||
const factory = module[pkg.artifact.entryExport];
|
||||
if (typeof factory !== 'function') {
|
||||
throw new Error('Retained strategy factory export is missing');
|
||||
}
|
||||
const program = factory(freeze(structuredClone(selection))) as StrategyProgram;
|
||||
if (
|
||||
!program ||
|
||||
typeof program !== 'object' ||
|
||||
program.ownerId !== selection.ownerId ||
|
||||
program.strategyId !== selection.strategyId ||
|
||||
program.strategyVersion !== selection.strategyVersion ||
|
||||
program.seed !== selection.seed ||
|
||||
program.mode !== 'target' ||
|
||||
typeof program.decide !== 'function'
|
||||
) {
|
||||
throw new Error('Retained strategy factory identity or API mismatch');
|
||||
}
|
||||
await send({
|
||||
type: 'progress',
|
||||
progress: 0,
|
||||
message: 'Retained canonical strategy replay started',
|
||||
});
|
||||
const { result } = runStrategyReplay(load, [program], { resultFormat: 'report' });
|
||||
let result;
|
||||
if (admitted.strategy.origin === 'candidate') {
|
||||
if (!request.candidateContainerName) {
|
||||
throw new Error('Isolated candidate lacks its owned container identity');
|
||||
}
|
||||
const controller = await IsolatedCandidateController.start(
|
||||
admitted,
|
||||
request.candidateContainerName,
|
||||
request.executionDeadlineAt
|
||||
);
|
||||
try {
|
||||
result = (
|
||||
await runReplaySessionAsync(new NativeReplaySession(load), controller, {
|
||||
resultFormat: 'report',
|
||||
})
|
||||
).result;
|
||||
await controller.finish();
|
||||
} finally {
|
||||
await controller.dispose();
|
||||
}
|
||||
} else {
|
||||
validateBuiltinStrategyBindings([admitted.strategy.selection], load);
|
||||
const program = await importRetainedProgram(pkg, admitted.strategy.selection);
|
||||
result = runStrategyReplay(load, [program], { resultFormat: 'report' }).result;
|
||||
}
|
||||
if (!('format' in result) || result.format !== 'report') {
|
||||
throw new Error('Retained replay requires the canonical report format');
|
||||
}
|
||||
const report = buildCanonicalBacktestReport(result, admitted.analyticsPolicy);
|
||||
const envelope: RetainedStrategyResult = {
|
||||
type: 'retainedStrategyResult',
|
||||
version: 1,
|
||||
build,
|
||||
artifactHash: pkg.artifact.sha256,
|
||||
...(admitted.strategy.origin === 'candidate'
|
||||
? { execution: admitted.strategy.execution }
|
||||
: {}),
|
||||
financial: report.financial,
|
||||
analytics: report.analytics,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue