feat(research): run retained strategies through durable canonical engine
This commit is contained in:
parent
d929db707a
commit
618fb9e33c
58 changed files with 6596 additions and 779 deletions
56
scripts/import-research-dataset.ts
Normal file
56
scripts/import-research-dataset.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/** Explicit offline custody of an existing v2 dataset. No provider/calendar inference. */
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { Pool } from 'pg';
|
||||
import { validateDatasetV2 } from '../apps/stock/orchestrator/src/data/historical-dataset-v2';
|
||||
|
||||
export async function retainResearchDataset(database: Pick<Pool, 'query'>, input: unknown) {
|
||||
const dataset = validateDatasetV2(input);
|
||||
await database.query(
|
||||
'INSERT INTO backtest_datasets(hash,payload) VALUES($1,$2::jsonb) ON CONFLICT(hash) DO NOTHING',
|
||||
[dataset.manifest.hash, JSON.stringify(dataset)]
|
||||
);
|
||||
const stored = (
|
||||
await database.query('SELECT payload FROM backtest_datasets WHERE hash=$1', [
|
||||
dataset.manifest.hash,
|
||||
])
|
||||
).rows[0];
|
||||
if (!stored || validateDatasetV2(stored.payload).manifest.hash !== dataset.manifest.hash) {
|
||||
throw new Error('Retained dataset content does not match the imported identity');
|
||||
}
|
||||
return dataset.manifest;
|
||||
}
|
||||
|
||||
if (import.meta.main) {
|
||||
let database: Pool | undefined;
|
||||
try {
|
||||
const args = process.argv.slice(2);
|
||||
if (args.length !== 2 || args[0] !== '--file' || !args[1]) {
|
||||
throw new Error(
|
||||
'Usage: RESEARCH_DATASET_DATABASE_URL=<database> bun run scripts/import-research-dataset.ts --file <v2-dataset.json>'
|
||||
);
|
||||
}
|
||||
const connectionString = process.env.RESEARCH_DATASET_DATABASE_URL;
|
||||
if (
|
||||
!connectionString ||
|
||||
!['postgres:', 'postgresql:'].includes(new URL(connectionString).protocol)
|
||||
) {
|
||||
throw new Error(
|
||||
'Set RESEARCH_DATASET_DATABASE_URL explicitly; this command retains research data'
|
||||
);
|
||||
}
|
||||
const input: unknown = JSON.parse(await readFile(args[1], 'utf8'));
|
||||
// Validate before opening a database connection. Existing startup migrations
|
||||
// own the schema; import never updates/deletes retained data or migrates tables.
|
||||
validateDatasetV2(input);
|
||||
database = new Pool({ connectionString, connectionTimeoutMillis: 5000 });
|
||||
const manifest = await retainResearchDataset(database, input);
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({ hash: manifest.hash, barCount: manifest.barCount, eligibility: manifest.eligibility, limitations: manifest.limitations }, null, 2)}\n`
|
||||
);
|
||||
} catch (error) {
|
||||
process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
await database?.end();
|
||||
}
|
||||
}
|
||||
|
|
@ -26,6 +26,7 @@ const workspaceSources: Record<string, string> = {
|
|||
'@stock-bot/calculations': 'libs/calculations/src/index.ts',
|
||||
'@stock-bot/calculations/indicators': 'libs/calculations/src/indicators/index.ts',
|
||||
'@stock-bot/calculations/returns': 'libs/calculations/src/returns.ts',
|
||||
'@stock-bot/stock-config/canonical-replay': 'apps/stock/config/src/contracts/canonical-replay.ts',
|
||||
'@stock-bot/stock-config/strategy': 'apps/stock/config/src/contracts/strategy.ts',
|
||||
'@stock-bot/stock-config/backtest': 'apps/stock/config/src/contracts/backtest.ts',
|
||||
};
|
||||
|
|
|
|||
|
|
@ -62,10 +62,11 @@ describe('orchestrator architecture boundaries', () => {
|
|||
expect(
|
||||
path === 'types.ts' ||
|
||||
path.startsWith('contracts/') ||
|
||||
path === 'backtest/native-worker-protocol.ts'
|
||||
path === 'backtest/native-worker-protocol.ts' ||
|
||||
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'].includes(specifier)).toBe(
|
||||
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
|
||||
);
|
||||
}
|
||||
|
|
@ -75,7 +76,7 @@ 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(['@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();
|
||||
|
|
@ -87,9 +88,9 @@ 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']) {
|
||||
for (const name of ['strategy.ts', 'builtin-strategy.ts', 'strategy-names.ts', 'canonical-research.ts', 'canonical-replay.ts']) {
|
||||
for (const specifier of runtimeImports(join(directory, name))) {
|
||||
expect(['zod', '@stock-bot/types', './builtin-strategy', './strategy-names'].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({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue