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
|
|
@ -1,24 +1,22 @@
|
|||
import { EventEmitter } from 'node:events';
|
||||
import type { Logger } from '@stock-bot/services';
|
||||
import { parseBacktestRequest } from '@stock-bot/stock-config/backtest';
|
||||
import {
|
||||
canonicalRunRequestSchema,
|
||||
type CanonicalRunRequest,
|
||||
type CanonicalRunResult,
|
||||
} from '@stock-bot/stock-config/canonical-research';
|
||||
import type { HistoricalDatasetV2 } from '@stock-bot/types';
|
||||
import { resolveDataset, type DatasetResolver } from '../data/historical-dataset';
|
||||
import { PREPARATION_BATCH_SIZE, preparationCheckpoint } from '../data/preparation';
|
||||
import type { DatasetResolver } from '../data/historical-dataset';
|
||||
import type { StorageService } from '../services/StorageService';
|
||||
import type { BacktestConfig } from '../types';
|
||||
import {
|
||||
BacktestCancelledError,
|
||||
BacktestTimeoutError,
|
||||
LEGACY_EXECUTION_RETIRED,
|
||||
type BacktestExecutionEngine,
|
||||
} from './execution-engine';
|
||||
import { nativeResultSchema, type NativeBacktestResult } from './native-result';
|
||||
import { resolveNativeStrategies } from './native-strategies';
|
||||
import { parseRetainedStrategyResult, type NativeWorkerRequest } from './native-worker-protocol';
|
||||
import type { NativeBacktestResult } from './native-result';
|
||||
import { parseRetainedStrategyResult } from './native-worker-protocol';
|
||||
import { NativeBacktestWorker } from './NativeBacktestWorker';
|
||||
|
||||
export type BacktestResult = NativeBacktestResult;
|
||||
|
|
@ -52,22 +50,10 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
|
|||
}
|
||||
|
||||
async runBacktest(
|
||||
input: BacktestConfig,
|
||||
dataset?: DatasetResolver
|
||||
_input: BacktestConfig,
|
||||
_dataset?: DatasetResolver
|
||||
): Promise<NativeBacktestResult> {
|
||||
const config = parseBacktestRequest(input);
|
||||
return this.runOwned(Date.parse(config.startDate), async (signal, aborted) => {
|
||||
resolveNativeStrategies(config);
|
||||
const data = await Promise.race([this.loadData(config, signal, dataset), aborted]);
|
||||
signal.throwIfAborted();
|
||||
const raw = await this.worker.execute({ type: 'run', config, data }, signal, progress => {
|
||||
this.progress = progress.progress;
|
||||
this.publish('progress', progress);
|
||||
});
|
||||
const result = nativeResultSchema.parse(JSON.parse(raw));
|
||||
this.currentTime = Date.parse(config.endDate);
|
||||
return result;
|
||||
});
|
||||
throw new Error(LEGACY_EXECUTION_RETIRED);
|
||||
}
|
||||
|
||||
async runCanonical(
|
||||
|
|
@ -95,7 +81,7 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
|
|||
);
|
||||
}
|
||||
|
||||
/** One ownership/deadline/cancellation lifecycle for legacy and canonical work. */
|
||||
/** One ownership/deadline/cancellation lifecycle for canonical work. */
|
||||
private async runOwned<Result>(
|
||||
initialTime: number,
|
||||
work: (signal: AbortSignal, aborted: Promise<never>) => Promise<Result>
|
||||
|
|
@ -147,25 +133,6 @@ export class RustBacktestAdapter extends EventEmitter implements BacktestExecuti
|
|||
}
|
||||
}
|
||||
|
||||
private async loadData(
|
||||
config: BacktestConfig,
|
||||
signal: AbortSignal,
|
||||
resolver?: DatasetResolver
|
||||
): Promise<Extract<NativeWorkerRequest, { type: 'run' }>['data']> {
|
||||
signal.throwIfAborted();
|
||||
const dataset = await resolveDataset(this.dependencies.storage, config, resolver, signal);
|
||||
signal.throwIfAborted();
|
||||
const data: Extract<NativeWorkerRequest, { type: 'run' }>['data'] = [];
|
||||
for (const bar of dataset.bars) {
|
||||
data.push({ ...bar, type: 'bar' });
|
||||
if (data.length % PREPARATION_BATCH_SIZE === 0) {
|
||||
await preparationCheckpoint(signal);
|
||||
}
|
||||
}
|
||||
signal.throwIfAborted();
|
||||
return data;
|
||||
}
|
||||
|
||||
private publish(event: string, data: unknown): void {
|
||||
try {
|
||||
this.emit(event, data);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue