Remove legacy strategy frameworks and clarify supported execution paths
This commit is contained in:
parent
4207bf2c1c
commit
783b619846
28 changed files with 996 additions and 4689 deletions
|
|
@ -9,24 +9,49 @@ const files = [...new Bun.Glob('**/*.ts').scanSync({ cwd: root })].map(file => j
|
|||
|
||||
/** Include static imports/re-exports and lazy imports; type-only edges do not load code. */
|
||||
function runtimeImports(file: string): string[] {
|
||||
const source = ts.createSourceFile(file, readFileSync(file, 'utf8'), ts.ScriptTarget.Latest, true);
|
||||
const source = ts.createSourceFile(
|
||||
file,
|
||||
readFileSync(file, 'utf8'),
|
||||
ts.ScriptTarget.Latest,
|
||||
true
|
||||
);
|
||||
const imports = new Set<string>();
|
||||
function visit(node: ts.Node) {
|
||||
if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) {
|
||||
const clause = node.importClause;
|
||||
const bindings = clause?.namedBindings;
|
||||
const typesOnly = clause?.isTypeOnly ||
|
||||
(clause && !clause.name && bindings && ts.isNamedImports(bindings) &&
|
||||
bindings.elements.length > 0 && bindings.elements.every(item => item.isTypeOnly));
|
||||
const typesOnly =
|
||||
clause?.isTypeOnly ||
|
||||
(clause &&
|
||||
!clause.name &&
|
||||
bindings &&
|
||||
ts.isNamedImports(bindings) &&
|
||||
bindings.elements.length > 0 &&
|
||||
bindings.elements.every(item => item.isTypeOnly));
|
||||
if (!typesOnly) imports.add(node.moduleSpecifier.text);
|
||||
}
|
||||
if (ts.isExportDeclaration(node) && !node.isTypeOnly && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) {
|
||||
if (
|
||||
ts.isExportDeclaration(node) &&
|
||||
!node.isTypeOnly &&
|
||||
node.moduleSpecifier &&
|
||||
ts.isStringLiteral(node.moduleSpecifier)
|
||||
) {
|
||||
const clause = node.exportClause;
|
||||
if (!clause || !ts.isNamedExports(clause) || !clause.elements.length || clause.elements.some(item => !item.isTypeOnly)) {
|
||||
if (
|
||||
!clause ||
|
||||
!ts.isNamedExports(clause) ||
|
||||
!clause.elements.length ||
|
||||
clause.elements.some(item => !item.isTypeOnly)
|
||||
) {
|
||||
imports.add(node.moduleSpecifier.text);
|
||||
}
|
||||
}
|
||||
if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword && node.arguments[0] && ts.isStringLiteral(node.arguments[0])) {
|
||||
if (
|
||||
ts.isCallExpression(node) &&
|
||||
node.expression.kind === ts.SyntaxKind.ImportKeyword &&
|
||||
node.arguments[0] &&
|
||||
ts.isStringLiteral(node.arguments[0])
|
||||
) {
|
||||
imports.add(node.arguments[0].text);
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
|
|
@ -39,13 +64,21 @@ const imports = new Map(files.map(file => [file, runtimeImports(file)]));
|
|||
function localFile(from: string, specifier: string): string | 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) throw new Error(`Unresolved local runtime import: ${relative(root, from)} -> ${specifier}`);
|
||||
const target = [path, `${path}.ts`, join(path, 'index.ts')].find(
|
||||
file => existsSync(file) && imports.has(file)
|
||||
);
|
||||
if (!target)
|
||||
throw new Error(`Unresolved local runtime import: ${relative(root, from)} -> ${specifier}`);
|
||||
return target;
|
||||
}
|
||||
const graph = new Map([...imports].map(([file, dependencies]) => [file,
|
||||
dependencies.map(specifier => localFile(file, specifier)).filter((file): file is string => !!file),
|
||||
]));
|
||||
const graph = new Map(
|
||||
[...imports].map(([file, dependencies]) => [
|
||||
file,
|
||||
dependencies
|
||||
.map(specifier => localFile(file, specifier))
|
||||
.filter((file): file is string => !!file),
|
||||
])
|
||||
);
|
||||
|
||||
function reachable(file: string, seen = new Set<string>()): Set<string> {
|
||||
if (seen.has(file)) return seen;
|
||||
|
|
@ -58,7 +91,10 @@ 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)) throw new Error(`Runtime dependency cycle: ${[...stack, file].map(file => relative(root, file)).join(' -> ')}`);
|
||||
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]);
|
||||
done.add(file);
|
||||
|
|
@ -68,24 +104,44 @@ describe('orchestrator architecture boundaries', () => {
|
|||
|
||||
it('contracts and native worker protocol cannot load services, storage or server adapters', () => {
|
||||
const contracts = files.filter(file => file.startsWith(join(root, 'contracts/')));
|
||||
for (const entry of [...contracts, join(root, 'types.ts'), join(root, 'backtest/native-worker-protocol.ts')]) {
|
||||
for (const entry of [
|
||||
...contracts,
|
||||
join(root, 'types.ts'),
|
||||
join(root, 'backtest/native-worker-protocol.ts'),
|
||||
]) {
|
||||
for (const file of reachable(entry)) {
|
||||
const path = relative(root, file);
|
||||
expect(path === 'types.ts' || path.startsWith('contracts/') || path === 'backtest/native-worker-protocol.ts').toBe(true);
|
||||
expect(
|
||||
path === 'types.ts' ||
|
||||
path.startsWith('contracts/') ||
|
||||
path === 'backtest/native-worker-protocol.ts'
|
||||
).toBe(true);
|
||||
for (const specifier of imports.get(file) ?? []) {
|
||||
expect(specifier.startsWith('.') || ['zod', '@stock-bot/types'].includes(specifier)).toBe(true);
|
||||
expect(specifier.startsWith('.') || ['zod', '@stock-bot/types'].includes(specifier)).toBe(
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('recovery and the ledger port do not depend on concrete adapters, strategies or the service container', () => {
|
||||
for (const entry of ['execution/SimulationExecution.ts', 'execution/StrategyRecovery.ts', 'execution/repository.ts', 'execution/strategy-recovery.ts', 'execution/model.ts']) {
|
||||
for (const entry of [
|
||||
'execution/SimulationExecution.ts',
|
||||
'execution/StrategyRecovery.ts',
|
||||
'execution/repository.ts',
|
||||
'execution/strategy-recovery.ts',
|
||||
'execution/model.ts',
|
||||
]) {
|
||||
for (const file of reachable(join(root, entry))) {
|
||||
expect(relative(root, file)).not.toMatch(/^(services|strategies|core|data)\//);
|
||||
expect(relative(root, file)).not.toMatch(/(Postgres|Backtest)ExecutionRepository/);
|
||||
for (const specifier of imports.get(file) ?? []) {
|
||||
expect(['@stock-bot/services', '@stock-bot/di', '@stock-bot/engine', 'awilix'].includes(specifier)).toBe(false);
|
||||
expect(
|
||||
['@stock-bot/services', '@stock-bot/di', '@stock-bot/engine', 'awilix'].includes(
|
||||
specifier
|
||||
)
|
||||
).toBe(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -94,14 +150,109 @@ describe('orchestrator architecture boundaries', () => {
|
|||
|
||||
it('the strategy manager cannot select or directly import concrete strategies', () => {
|
||||
const file = join(root, 'strategies/StrategyManager.ts');
|
||||
expect(imports.get(file)?.some(specifier => specifier.includes('/examples/'))).toBe(false);
|
||||
expect(
|
||||
graph
|
||||
.get(file)
|
||||
?.some(target => /^strategies\/(builtin|experimental)\//.test(relative(root, target)))
|
||||
).toBe(false);
|
||||
expect(readFileSync(file, 'utf8')).not.toMatch(/switch\s*\(\s*config\.name/);
|
||||
expect([...reachable(join(root, 'strategies/StrategyRegistry.ts'))].map(file => relative(root, file)).sort())
|
||||
.toEqual(['contracts/strategy-names.ts', 'strategies/StrategyRegistry.ts']);
|
||||
expect(
|
||||
[...reachable(join(root, 'strategies/StrategyRegistry.ts'))]
|
||||
.map(file => relative(root, file))
|
||||
.sort()
|
||||
).toEqual(['contracts/strategy-names.ts', 'strategies/StrategyRegistry.ts']);
|
||||
});
|
||||
|
||||
it('concrete strategies can only be loaded by the registry or other strategy implementations', () => {
|
||||
for (const [file, dependencies] of graph) {
|
||||
const owner = relative(root, file);
|
||||
for (const dependency of dependencies) {
|
||||
const target = relative(root, dependency);
|
||||
if (!/^strategies\/(builtin|experimental)\//.test(target)) continue;
|
||||
expect(
|
||||
owner === 'strategies/builtin-strategies.ts' ||
|
||||
/^strategies\/(builtin|experimental)\//.test(owner)
|
||||
).toBe(true);
|
||||
if (owner.startsWith('strategies/builtin/')) {
|
||||
expect(target.startsWith('strategies/experimental/')).toBe(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('retired execution paths and strategy-local financial frameworks cannot return', () => {
|
||||
const retired = [
|
||||
'backtest/RustBacktestEngine.ts',
|
||||
'strategies/RustStrategy.ts',
|
||||
'strategies/StrategyExecutor.ts',
|
||||
];
|
||||
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)\//
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('native backtest execution stays in the worker and strategies cannot submit orders directly', () => {
|
||||
const nativeEntrypoints = files.filter(
|
||||
file =>
|
||||
relative(root, file).startsWith('backtest/') &&
|
||||
imports.get(file)?.includes('@stock-bot/engine')
|
||||
);
|
||||
expect(nativeEntrypoints.map(file => relative(root, file))).toEqual([
|
||||
'backtest/native-worker.ts',
|
||||
]);
|
||||
for (const file of files) {
|
||||
const path = relative(root, file);
|
||||
const source = ts.createSourceFile(
|
||||
file,
|
||||
readFileSync(file, 'utf8'),
|
||||
ts.ScriptTarget.Latest,
|
||||
true
|
||||
);
|
||||
function visit(node: ts.Node) {
|
||||
// Check named imports anywhere, including aliases; a new wrapper outside backtest/ is still forbidden.
|
||||
if (
|
||||
ts.isImportDeclaration(node) &&
|
||||
ts.isStringLiteral(node.moduleSpecifier) &&
|
||||
node.moduleSpecifier.text === '@stock-bot/engine' &&
|
||||
!node.importClause?.isTypeOnly
|
||||
) {
|
||||
const bindings = node.importClause?.namedBindings;
|
||||
if (bindings && ts.isNamedImports(bindings)) {
|
||||
for (const binding of bindings.elements) {
|
||||
if (
|
||||
!binding.isTypeOnly &&
|
||||
(binding.propertyName ?? binding.name).text === 'BacktestEngine'
|
||||
) {
|
||||
expect(path).toBe('backtest/native-worker.ts');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ts.isCallExpression(node)) {
|
||||
const callee = node.expression;
|
||||
const method = ts.isPropertyAccessExpression(callee)
|
||||
? callee.name.text
|
||||
: ts.isElementAccessExpression(callee) &&
|
||||
callee.argumentExpression &&
|
||||
ts.isStringLiteral(callee.argumentExpression)
|
||||
? callee.argumentExpression.text
|
||||
: undefined;
|
||||
expect(method).not.toBe('addTypescriptStrategy');
|
||||
if (path.startsWith('strategies/')) expect(method).not.toBe('submitOrder');
|
||||
}
|
||||
ts.forEachChild(node, visit);
|
||||
}
|
||||
visit(source);
|
||||
}
|
||||
});
|
||||
|
||||
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)).sort();
|
||||
const dependencies = [...reachable(join(root, 'backtest/BacktestPlayback.ts'))]
|
||||
.map(file => relative(root, file))
|
||||
.sort();
|
||||
expect(dependencies).toEqual(['backtest/BacktestPlayback.ts', 'backtest/execution-engine.ts']);
|
||||
});
|
||||
|
||||
|
|
@ -109,9 +260,16 @@ describe('orchestrator architecture boundaries', () => {
|
|||
for (const entry of ['backtest/BacktestReport.ts', 'backtest/report-format.ts']) {
|
||||
for (const file of reachable(join(root, entry))) {
|
||||
expect(relative(root, file)).not.toMatch(/^(services|strategies|core|data|execution)\//);
|
||||
expect(relative(root, file)).not.toMatch(/backtest\/(BacktestEngine|BacktestMarket|BacktestRunner|BacktestPlayback)/);
|
||||
expect(relative(root, file)).not.toMatch(
|
||||
/backtest\/(BacktestEngine|BacktestMarket|BacktestRunner|BacktestPlayback)/
|
||||
);
|
||||
for (const specifier of imports.get(file) ?? []) {
|
||||
expect(specifier.startsWith('.') || ['simple-statistics', 'zod', '@stock-bot/invariants', '@stock-bot/logger'].includes(specifier)).toBe(true);
|
||||
expect(
|
||||
specifier.startsWith('.') ||
|
||||
['simple-statistics', 'zod', '@stock-bot/invariants', '@stock-bot/logger'].includes(
|
||||
specifier
|
||||
)
|
||||
).toBe(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue