feat(research): cut over canonical runtime and enforce agent budgets
This commit is contained in:
parent
ab08db82fd
commit
bbf0dd4453
103 changed files with 6138 additions and 3112 deletions
|
|
@ -10,13 +10,16 @@ const files = [...new Bun.Glob('**/*.ts').scanSync({ cwd: root })].map(file => j
|
|||
|
||||
const imports = new Map(files.map(file => [file, runtimeImports(file)]));
|
||||
function localFile(from: string, specifier: string): string | undefined {
|
||||
if (!specifier.startsWith('.')) return undefined;
|
||||
if (!specifier.startsWith('.')) {
|
||||
return undefined;
|
||||
}
|
||||
const path = resolve(dirname(from), specifier);
|
||||
const target = [path, `${path}.ts`, join(path, 'index.ts')].find(
|
||||
file => existsSync(file) && imports.has(file)
|
||||
);
|
||||
if (!target)
|
||||
if (!target) {
|
||||
throw new Error(`Unresolved local runtime import: ${relative(root, from)} -> ${specifier}`);
|
||||
}
|
||||
return target;
|
||||
}
|
||||
const graph = new Map(
|
||||
|
|
@ -29,9 +32,13 @@ const graph = new Map(
|
|||
);
|
||||
|
||||
function reachable(file: string, seen = new Set<string>()): Set<string> {
|
||||
if (seen.has(file)) return seen;
|
||||
if (seen.has(file)) {
|
||||
return seen;
|
||||
}
|
||||
seen.add(file);
|
||||
for (const next of graph.get(file) ?? []) reachable(next, seen);
|
||||
for (const next of graph.get(file) ?? []) {
|
||||
reachable(next, seen);
|
||||
}
|
||||
return seen;
|
||||
}
|
||||
|
||||
|
|
@ -39,15 +46,22 @@ describe('orchestrator architecture boundaries', () => {
|
|||
it('local runtime dependencies are acyclic', () => {
|
||||
const done = new Set<string>();
|
||||
function visit(file: string, stack: string[]) {
|
||||
if (stack.includes(file))
|
||||
if (stack.includes(file)) {
|
||||
throw new Error(
|
||||
`Runtime dependency cycle: ${[...stack, file].map(file => relative(root, file)).join(' -> ')}`
|
||||
);
|
||||
if (done.has(file)) return;
|
||||
for (const next of graph.get(file) ?? []) visit(next, [...stack, file]);
|
||||
}
|
||||
if (done.has(file)) {
|
||||
return;
|
||||
}
|
||||
for (const next of graph.get(file) ?? []) {
|
||||
visit(next, [...stack, file]);
|
||||
}
|
||||
done.add(file);
|
||||
}
|
||||
for (const file of files) visit(file, []);
|
||||
for (const file of files) {
|
||||
visit(file, []);
|
||||
}
|
||||
});
|
||||
|
||||
it('contracts and native worker protocol cannot load services, storage or server adapters', () => {
|
||||
|
|
@ -66,9 +80,19 @@ describe('orchestrator architecture boundaries', () => {
|
|||
path === 'backtest/canonical-result.ts'
|
||||
).toBe(true);
|
||||
for (const specifier of imports.get(file) ?? []) {
|
||||
expect(specifier.startsWith('.') || ['zod', '@stock-bot/types', '@stock-bot/stock-config/backtest', '@stock-bot/stock-config/strategy', '@stock-bot/stock-config/canonical-replay', '@stock-bot/stock-config/canonical-research', '@stock-bot/calculations/analytics', '@stock-bot/calculations/returns'].includes(specifier)).toBe(
|
||||
true
|
||||
);
|
||||
expect(
|
||||
specifier.startsWith('.') ||
|
||||
[
|
||||
'zod',
|
||||
'@stock-bot/types',
|
||||
'@stock-bot/stock-config/backtest',
|
||||
'@stock-bot/stock-config/strategy',
|
||||
'@stock-bot/stock-config/canonical-replay',
|
||||
'@stock-bot/stock-config/canonical-research',
|
||||
'@stock-bot/calculations/analytics',
|
||||
'@stock-bot/calculations/returns',
|
||||
].includes(specifier)
|
||||
).toBe(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -76,7 +100,11 @@ describe('orchestrator architecture boundaries', () => {
|
|||
|
||||
it('the shared backtest contract loads no configuration manager, services or application runtime', async () => {
|
||||
const entry = resolve(root, '../../config/src/contracts/backtest.ts');
|
||||
expect(runtimeImports(entry).sort()).toEqual(['./canonical-research', '@stock-bot/types', 'zod']);
|
||||
expect(runtimeImports(entry).sort()).toEqual([
|
||||
'./canonical-research',
|
||||
'@stock-bot/types',
|
||||
'zod',
|
||||
]);
|
||||
const bundle = await Bun.build({ entrypoints: [entry], target: 'browser' });
|
||||
expect(bundle.success).toBe(true);
|
||||
const code = await bundle.outputs[0]?.text();
|
||||
|
|
@ -88,26 +116,50 @@ describe('orchestrator architecture boundaries', () => {
|
|||
|
||||
it('shared strategy catalog contracts cannot import strategy execution or application services', async () => {
|
||||
const directory = resolve(root, '../../config/src/contracts');
|
||||
for (const name of ['strategy.ts', 'builtin-strategy.ts', 'strategy-names.ts', 'canonical-research.ts', 'canonical-replay.ts', 'research.ts']) {
|
||||
for (const name of [
|
||||
'strategy.ts',
|
||||
'builtin-strategy.ts',
|
||||
'strategy-names.ts',
|
||||
'canonical-research.ts',
|
||||
'canonical-replay.ts',
|
||||
'research.ts',
|
||||
]) {
|
||||
for (const specifier of runtimeImports(join(directory, name))) {
|
||||
expect(['zod', '@stock-bot/types', './builtin-strategy', './strategy-names', './strategy', './canonical-replay'].includes(specifier)).toBe(true);
|
||||
expect(
|
||||
[
|
||||
'zod',
|
||||
'@stock-bot/types',
|
||||
'./builtin-strategy',
|
||||
'./strategy-names',
|
||||
'./strategy',
|
||||
'./canonical-replay',
|
||||
].includes(specifier)
|
||||
).toBe(true);
|
||||
}
|
||||
}
|
||||
const bundle = await Bun.build({
|
||||
entrypoints: [join(directory, 'strategy.ts')], target: 'browser',
|
||||
plugins: [{
|
||||
name: 'current-contract-types',
|
||||
setup(builder) {
|
||||
builder.onResolve({ filter: /^@stock-bot\/types$/ }, () => ({
|
||||
path: fileURLToPath(new URL('../../libs/core/types/src/index.ts', import.meta.url)),
|
||||
}));
|
||||
entrypoints: [join(directory, 'strategy.ts')],
|
||||
target: 'browser',
|
||||
plugins: [
|
||||
{
|
||||
name: 'current-contract-types',
|
||||
setup(builder) {
|
||||
builder.onResolve({ filter: /^@stock-bot\/types$/ }, () => ({
|
||||
path: fileURLToPath(new URL('../../libs/core/types/src/index.ts', import.meta.url)),
|
||||
}));
|
||||
},
|
||||
},
|
||||
}],
|
||||
],
|
||||
});
|
||||
expect(bundle.success).toBe(true);
|
||||
const code = await bundle.outputs[0]?.text();
|
||||
expect(code).toBeDefined();
|
||||
for (const forbidden of ['config-instance', 'PostgresClient', 'engine.node', 'createBuiltinStrategy']) {
|
||||
for (const forbidden of [
|
||||
'config-instance',
|
||||
'PostgresClient',
|
||||
'engine.node',
|
||||
'createBuiltinStrategy',
|
||||
]) {
|
||||
expect(code).not.toContain(forbidden);
|
||||
}
|
||||
});
|
||||
|
|
@ -162,11 +214,15 @@ describe('orchestrator architecture boundaries', () => {
|
|||
expect(source).not.toMatch(/\b(OrchestratorServices|IServiceContainer)\b/);
|
||||
const tree = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, true);
|
||||
function visit(node: ts.Node) {
|
||||
if (ts.isPropertyAccessExpression(node)) expect(node.name.text).not.toBe('custom');
|
||||
if (ts.isPropertyAccessExpression(node)) {
|
||||
expect(node.name.text).not.toBe('custom');
|
||||
}
|
||||
if (ts.isElementAccessExpression(node) && ts.isStringLiteral(node.argumentExpression)) {
|
||||
expect(node.argumentExpression.text).not.toBe('custom');
|
||||
}
|
||||
if (ts.isNewExpression(node)) expect(node.expression.getText(tree)).not.toBe('StorageService');
|
||||
if (ts.isNewExpression(node)) {
|
||||
expect(node.expression.getText(tree)).not.toBe('StorageService');
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
visit(tree);
|
||||
|
|
@ -178,7 +234,9 @@ describe('orchestrator architecture boundaries', () => {
|
|||
const owner = relative(root, file);
|
||||
for (const dependency of dependencies) {
|
||||
const target = relative(root, dependency);
|
||||
if (!/^strategies\/(builtin|experimental)\//.test(target)) continue;
|
||||
if (!/^strategies\/(builtin|experimental)\//.test(target)) {
|
||||
continue;
|
||||
}
|
||||
expect(
|
||||
owner === 'strategies/builtin-strategies.ts' ||
|
||||
/^strategies\/(builtin|experimental)\//.test(owner)
|
||||
|
|
@ -196,7 +254,9 @@ describe('orchestrator architecture boundaries', () => {
|
|||
'strategies/RustStrategy.ts',
|
||||
'strategies/StrategyExecutor.ts',
|
||||
];
|
||||
for (const file of retired) expect(existsSync(join(root, file))).toBe(false);
|
||||
for (const file of retired) {
|
||||
expect(existsSync(join(root, file))).toBe(false);
|
||||
}
|
||||
for (const file of files) {
|
||||
expect(relative(root, file)).not.toMatch(
|
||||
/^strategies\/(examples|rust|position|risk|signals|indicators)\//
|
||||
|
|
@ -212,7 +272,6 @@ describe('orchestrator architecture boundaries', () => {
|
|||
);
|
||||
expect(nativeEntrypoints.map(file => relative(root, file)).sort()).toEqual([
|
||||
'backtest/native-replay.ts',
|
||||
'backtest/native-worker.ts',
|
||||
]);
|
||||
for (const file of files) {
|
||||
const path = relative(root, file);
|
||||
|
|
@ -244,7 +303,7 @@ describe('orchestrator architecture boundaries', () => {
|
|||
!binding.isTypeOnly &&
|
||||
(binding.propertyName ?? binding.name).text === 'BacktestEngine'
|
||||
) {
|
||||
expect(path).toBe('backtest/native-worker.ts');
|
||||
throw new Error(`Retired native simulator imported by ${path}`);
|
||||
}
|
||||
if (
|
||||
!binding.isTypeOnly &&
|
||||
|
|
@ -271,7 +330,9 @@ describe('orchestrator architecture boundaries', () => {
|
|||
? callee.argumentExpression.text
|
||||
: undefined;
|
||||
expect(method).not.toBe('addTypescriptStrategy');
|
||||
if (path.startsWith('strategies/')) expect(method).not.toBe('submitOrder');
|
||||
if (path.startsWith('strategies/')) {
|
||||
expect(method).not.toBe('submitOrder');
|
||||
}
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
|
|
@ -279,6 +340,38 @@ describe('orchestrator architecture boundaries', () => {
|
|||
}
|
||||
});
|
||||
|
||||
it('production research and paper entrypoints cannot reach prototype financial execution', () => {
|
||||
const forbidden = [
|
||||
'backtest/BacktestEngine.ts',
|
||||
'backtest/BacktestMarket.ts',
|
||||
'core/ModeManager.ts',
|
||||
'execution/SimulationExecution.ts',
|
||||
'execution/model.ts',
|
||||
'services/ExecutionService.ts',
|
||||
'paper/PaperTradingManager.ts',
|
||||
'api/rest/orders.ts',
|
||||
'api/rest/positions.ts',
|
||||
'api/websocket/index.ts',
|
||||
];
|
||||
for (const entry of [
|
||||
'index.ts',
|
||||
'container.ts',
|
||||
'simple-container.ts',
|
||||
'backtest/native-worker.ts',
|
||||
]) {
|
||||
const production = [...reachable(join(root, entry))].map(file => relative(root, file));
|
||||
for (const prototype of forbidden) {
|
||||
expect(production).not.toContain(prototype);
|
||||
}
|
||||
}
|
||||
expect(readFileSync(join(root, 'backtest/native-worker-protocol.ts'), 'utf8')).not.toContain(
|
||||
"z.literal('run')"
|
||||
);
|
||||
expect(readFileSync(join(root, 'backtest/BacktestRunner.ts'), 'utf8')).not.toContain(
|
||||
'engine.runBacktest('
|
||||
);
|
||||
});
|
||||
|
||||
it('backtest playback cannot depend on trading, data, reporting or service adapters', () => {
|
||||
const dependencies = [...reachable(join(root, 'backtest/BacktestPlayback.ts'))]
|
||||
.map(file => relative(root, file))
|
||||
|
|
|
|||
|
|
@ -113,9 +113,8 @@ describe('generated native bindings', () => {
|
|||
slippage: 0,
|
||||
dataFrequency: '1d',
|
||||
});
|
||||
expect(() =>
|
||||
engine.addTypescriptStrategy('unsupported', 'unsupported', {}, () => '{}')
|
||||
).toThrow('TypeScript strategy callbacks are not supported');
|
||||
expect(engine).not.toHaveProperty('addTypescriptStrategy');
|
||||
expect(engine).not.toHaveProperty('addTypescriptStrategy');
|
||||
expect(() =>
|
||||
engine.addNativeStrategy('sma_crossover', 'invalid', 'invalid', {
|
||||
fastPeriod: 0,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue