refactor(api): compose research services with narrow collaborators (CL28)
This commit is contained in:
parent
3a63b2039e
commit
24487d35da
28 changed files with 281 additions and 94 deletions
|
|
@ -18,7 +18,7 @@ import { createResearchAgentRoutes } from '../../apps/stock/web-api/src/routes/r
|
|||
import { createResearchRoutes } from '../../apps/stock/web-api/src/routes/research.routes';
|
||||
import { createStrategyRoutes } from '../../apps/stock/web-api/src/routes/strategies.routes';
|
||||
import { ResearchAgentsService } from '../../apps/stock/web-api/src/services/research-agents.service';
|
||||
import { ResearchService } from '../../apps/stock/web-api/src/services/research.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import {
|
||||
runResearchCycle,
|
||||
type ResearchCycleEvent,
|
||||
|
|
@ -91,7 +91,7 @@ async function fixture() {
|
|||
);
|
||||
const container = { postgres, logger };
|
||||
const agents = new ResearchAgentsService(container),
|
||||
research = new ResearchService(container);
|
||||
research = createResearchServices(container).research;
|
||||
const project = await research.createProject({ title: 'AI research acceptance' }, 'operator');
|
||||
const provisioned = await agents.create({
|
||||
name: 'Workflow researcher',
|
||||
|
|
|
|||
|
|
@ -16,9 +16,9 @@ import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src
|
|||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import { experimentHash } from '../../apps/stock/web-api/src/services/experiment-input';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
|
||||
import {
|
||||
selectValidationWinner,
|
||||
|
|
@ -168,7 +168,7 @@ async function fixture(poolMax = 1, withValidation = true) {
|
|||
}
|
||||
return rows;
|
||||
};
|
||||
const validations = new ChronologicalValidationService(container);
|
||||
const validations = createResearchServices(container).validations;
|
||||
const repository = new PostgresBacktestRunRepository(postgres);
|
||||
const execute = async (id: string) => {
|
||||
const engine = new RustBacktestAdapter({
|
||||
|
|
|
|||
|
|
@ -13,10 +13,10 @@ import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src
|
|||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import { ExperimentError } from '../../apps/stock/web-api/src/services/experiment-input';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { canonicalReproductionHash } from '../../apps/stock/web-api/src/services/reproduction-comparison';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { packageBuiltinStrategies } from '../../scripts/package-builtin-strategies';
|
||||
|
|
@ -162,7 +162,7 @@ async function fixture(poolMax = 1, withCosts = true, capital = 10000) {
|
|||
}
|
||||
return rows;
|
||||
};
|
||||
const validations = new ChronologicalValidationService(container);
|
||||
const validations = createResearchServices(container).validations;
|
||||
const repository = new PostgresBacktestRunRepository(postgres);
|
||||
const execute = async (id: string) => {
|
||||
const engine = new RustBacktestAdapter({
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src
|
|||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { packageBuiltinStrategies } from '../../scripts/package-builtin-strategies';
|
||||
|
|
@ -163,7 +163,7 @@ async function fixture(poolMax = 1, withCosts = true, capital = 10000) {
|
|||
}
|
||||
return rows;
|
||||
};
|
||||
const validations = new ChronologicalValidationService(container);
|
||||
const validations = createResearchServices(container).validations;
|
||||
const repository = new PostgresBacktestRunRepository(postgres);
|
||||
const execute = async (id: string) => {
|
||||
const engine = new RustBacktestAdapter({
|
||||
|
|
|
|||
|
|
@ -13,11 +13,10 @@ import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src
|
|||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { runStrategyReplay } from '../../apps/stock/orchestrator/src/backtest/strategy-runtime';
|
||||
import { createBuiltinStrategy } from '../../apps/stock/orchestrator/src/strategies/canonical-builtins';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { researchGrantPolicy } from '../../apps/stock/web-api/src/services/research-agent-budget';
|
||||
import { ResearchAgentsService } from '../../apps/stock/web-api/src/services/research-agents.service';
|
||||
import { WalkForwardService } from '../../apps/stock/web-api/src/services/walk-forward.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { canonicalAdmissionFixture } from '../fixtures/canonical-admission';
|
||||
import { walkForwardDataset } from '../fixtures/walk-forward';
|
||||
|
|
@ -359,8 +358,7 @@ suite('isolated candidate durable research', () => {
|
|||
endDate: new Date(data.dataset.manifest.bounds.tradeEnd).toISOString(),
|
||||
config: { ...f.definition.config, datasetHash: data.dataset.manifest.hash },
|
||||
});
|
||||
const walks = new WalkForwardService(f.container),
|
||||
validations = new ChronologicalValidationService(f.container);
|
||||
const { walkForwards: walks, validations } = createResearchServices(f.container);
|
||||
const input = {
|
||||
name: 'Candidate rolling windows',
|
||||
backtestId: f.draft.id,
|
||||
|
|
|
|||
|
|
@ -17,9 +17,8 @@ import {
|
|||
import { readPaperHostIdentity } from '../../apps/stock/orchestrator/src/paper/paper-host-identity';
|
||||
import { PaperDeploymentHost } from '../../apps/stock/orchestrator/src/paper/PaperDeploymentHost';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { PaperDeploymentsService } from '../../apps/stock/web-api/src/services/paper-deployments.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { packageBuiltinStrategies } from '../../scripts/package-builtin-strategies';
|
||||
|
|
@ -72,11 +71,12 @@ async function fixture(max = 1, withPromotion = true, candidate = false) {
|
|||
Object.defineProperty(postgres, 'connectionPool', { get: () => pool });
|
||||
Object.defineProperty(postgres, 'query', { value: pool.query.bind(pool) });
|
||||
const container = { postgres, logger },
|
||||
researchServices = createResearchServices(container),
|
||||
api = new BacktestServiceV2(container),
|
||||
experiments = new ExperimentsService(container),
|
||||
validations = new ChronologicalValidationService(container),
|
||||
validations = researchServices.validations,
|
||||
catalog = new StrategyCatalogService(container),
|
||||
service = new PaperDeploymentsService(container);
|
||||
service = researchServices.paperDeployments;
|
||||
const pkg = (await packageBuiltinStrategies())[0];
|
||||
if (!pkg) {
|
||||
throw new Error('Missing builtin');
|
||||
|
|
@ -495,14 +495,18 @@ describe.skipIf(!url)('human-reviewed paper deployment', () => {
|
|||
}, 60000);
|
||||
test('changed installed host and retained artifact cannot initialize or overwrite an account', async () => {
|
||||
const f = await fixture(),
|
||||
drifted = new PaperDeploymentsService(f.container, async () => ({
|
||||
drifted = createResearchServices(f.container, {
|
||||
paperIdentity: async () => ({
|
||||
...(await readPaperHostIdentity()),
|
||||
hostSha256: 'f'.repeat(64),
|
||||
}),
|
||||
}).paperDeployments;
|
||||
const wrongNative = createResearchServices(f.container, {
|
||||
paperIdentity: async () => ({
|
||||
...(await readPaperHostIdentity()),
|
||||
hostSha256: 'f'.repeat(64),
|
||||
}));
|
||||
const wrongNative = new PaperDeploymentsService(f.container, async () => ({
|
||||
...(await readPaperHostIdentity()),
|
||||
engineBinarySha256: 'f'.repeat(64),
|
||||
}));
|
||||
engineBinarySha256: 'f'.repeat(64),
|
||||
}),
|
||||
}).paperDeployments;
|
||||
await expect(wrongNative.preview(f.promotion)).rejects.toThrow('same native');
|
||||
const preview = await drifted.preview(f.promotion),
|
||||
saved = await drifted.create(
|
||||
|
|
|
|||
|
|
@ -17,13 +17,11 @@ import { createRoutes } from '../../apps/stock/web-api/src/routes/create-routes'
|
|||
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { BacktestDispatcher } from '../../apps/stock/web-api/src/services/BacktestDispatcher';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { researchGrantPolicy } from '../../apps/stock/web-api/src/services/research-agent-budget';
|
||||
import { ResearchAgentsService } from '../../apps/stock/web-api/src/services/research-agents.service';
|
||||
import { ResearchService } from '../../apps/stock/web-api/src/services/research.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
|
||||
import { WalkForwardService } from '../../apps/stock/web-api/src/services/walk-forward.service';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { packageBuiltinStrategies } from '../../scripts/package-builtin-strategies';
|
||||
import { canonicalSettings } from '../fixtures/canonical-research';
|
||||
|
|
@ -142,7 +140,7 @@ async function fixture(poolMax = 4, withAgents = true, dataset = canonicalSmaFix
|
|||
postgres,
|
||||
container,
|
||||
agents: new ResearchAgentsService(container),
|
||||
research: new ResearchService(container),
|
||||
research: createResearchServices(container).research,
|
||||
schema,
|
||||
api,
|
||||
experiments,
|
||||
|
|
@ -687,8 +685,7 @@ describe.skipIf(!url)('authoritative research principals and budgets', () => {
|
|||
);
|
||||
expect(cost.trials).toHaveLength(2);
|
||||
await f.experiments.cancel(cost.id);
|
||||
const validation = new ChronologicalValidationService(f.container),
|
||||
walk = new WalkForwardService(f.container);
|
||||
const { validations: validation, walkForwards: walk } = createResearchServices(f.container);
|
||||
const input = {
|
||||
backtestId: f.draft.id,
|
||||
name: 'Validation',
|
||||
|
|
@ -909,7 +906,7 @@ describe.skipIf(!url)('authoritative research principals and budgets', () => {
|
|||
[randomUUID(), runId, b.agent.id, randomUUID(), 'a'.repeat(64)]
|
||||
)
|
||||
).rejects.toThrow(/owning.*principal/);
|
||||
const validation = new ChronologicalValidationService(f.container),
|
||||
const validation = createResearchServices(f.container).validations,
|
||||
input = {
|
||||
backtestId: f.draft.id,
|
||||
name: 'Owned phases',
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src
|
|||
import { BacktestLeaseLostError } from '../../apps/stock/orchestrator/src/backtest/run-repository';
|
||||
import { canonicalReproductionHash } from '../../apps/stock/web-api/src/services/reproduction-comparison';
|
||||
import { ResearchAgentsService } from '../../apps/stock/web-api/src/services/research-agents.service';
|
||||
import { ResearchService } from '../../apps/stock/web-api/src/services/research.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { deferred } from '../fixtures/canonical-runner';
|
||||
import { logger } from '../fixtures/execution';
|
||||
import { nativeReplayFixture } from '../fixtures/native-replay';
|
||||
|
|
@ -201,7 +201,7 @@ describe.skipIf(!process.env.PIPELINE_TEST_DATABASE_URL || !process.env.BACKTEST
|
|||
}, 60000);
|
||||
test('four slots cannot bypass agent admission capacity or per-attempt absolute deadlines', async () => {
|
||||
const f = await fixture();
|
||||
const research = new ResearchService({ postgres: f.postgres, logger });
|
||||
const research = createResearchServices({ postgres: f.postgres, logger }).research;
|
||||
const project = await research.createProject({ title: 'Pool budget gate' }, 'operator');
|
||||
const agents = new ResearchAgentsService({ postgres: f.postgres, logger });
|
||||
const agent = await agents.create({
|
||||
|
|
|
|||
|
|
@ -13,12 +13,10 @@ import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src
|
|||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { createResearchRoutes } from '../../apps/stock/web-api/src/routes/research.routes';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import { experimentHash } from '../../apps/stock/web-api/src/services/experiment-input';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { ResearchService } from '../../apps/stock/web-api/src/services/research.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
|
||||
import { WalkForwardService } from '../../apps/stock/web-api/src/services/walk-forward.service';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { packageBuiltinStrategies } from '../../scripts/package-builtin-strategies';
|
||||
import { canonicalSettings } from '../fixtures/canonical-research';
|
||||
|
|
@ -145,7 +143,8 @@ async function fixture(poolMax = 1, withSources = true) {
|
|||
}
|
||||
return rows;
|
||||
};
|
||||
const validations = new ChronologicalValidationService(container);
|
||||
const services = createResearchServices(container);
|
||||
const validations = services.validations;
|
||||
const repository = new PostgresBacktestRunRepository(postgres);
|
||||
const execute = async (id: string) => {
|
||||
const engine = new RustBacktestAdapter({
|
||||
|
|
@ -179,7 +178,7 @@ async function fixture(poolMax = 1, withSources = true) {
|
|||
{ autoStart: false }
|
||||
),
|
||||
validations,
|
||||
walks: new WalkForwardService(container),
|
||||
walks: services.walkForwards,
|
||||
repository,
|
||||
execute,
|
||||
api,
|
||||
|
|
@ -191,7 +190,7 @@ async function fixture(poolMax = 1, withSources = true) {
|
|||
draft,
|
||||
dataset,
|
||||
input,
|
||||
research: new ResearchService(container),
|
||||
research: services.research,
|
||||
container,
|
||||
schema,
|
||||
review,
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ import { BacktestRunner } from '../../apps/stock/orchestrator/src/backtest/Backt
|
|||
import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src/backtest/PostgresBacktestRunRepository';
|
||||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { ResearchService } from '../../apps/stock/web-api/src/services/research.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { packageBuiltinStrategies } from '../../scripts/package-builtin-strategies';
|
||||
|
|
@ -81,7 +81,7 @@ async function fixture(applyWorkspace = true, maxConnections = 10) {
|
|||
Object.defineProperty(postgres, 'connectionPool', { get: () => pool });
|
||||
Object.defineProperty(postgres, 'query', { value: pool.query.bind(pool) });
|
||||
const container = { postgres, logger };
|
||||
const service = new ResearchService(container);
|
||||
const service = createResearchServices(container).research;
|
||||
const backtests = new BacktestServiceV2(container);
|
||||
const catalog = new StrategyCatalogService(container);
|
||||
const legacy = async (status = 'completed') => {
|
||||
|
|
|
|||
|
|
@ -12,11 +12,10 @@ import { PostgresBacktestRunRepository } from '../../apps/stock/orchestrator/src
|
|||
import { RustBacktestAdapter } from '../../apps/stock/orchestrator/src/backtest/RustBacktestAdapter';
|
||||
import { createExperimentRoutes } from '../../apps/stock/web-api/src/routes/experiments.routes';
|
||||
import { BacktestServiceV2 } from '../../apps/stock/web-api/src/services/backtest-v2.service';
|
||||
import { ChronologicalValidationService } from '../../apps/stock/web-api/src/services/chronological-validation.service';
|
||||
import { ExperimentError } from '../../apps/stock/web-api/src/services/experiment-input';
|
||||
import { ExperimentsService } from '../../apps/stock/web-api/src/services/experiments.service';
|
||||
import { createResearchServices } from '../../apps/stock/web-api/src/services/research-services';
|
||||
import { StrategyCatalogService } from '../../apps/stock/web-api/src/services/strategy-catalog.service';
|
||||
import { WalkForwardService } from '../../apps/stock/web-api/src/services/walk-forward.service';
|
||||
import { retainResearchDataset } from '../../scripts/import-research-dataset';
|
||||
import { packageBuiltinStrategies } from '../../scripts/package-builtin-strategies';
|
||||
import { canonicalSettings } from '../fixtures/canonical-research';
|
||||
|
|
@ -158,7 +157,8 @@ async function fixture(poolMax = 1, withWalkForward = true) {
|
|||
}
|
||||
return rows;
|
||||
};
|
||||
const validations = new ChronologicalValidationService(container);
|
||||
const services = createResearchServices(container);
|
||||
const validations = services.validations;
|
||||
const repository = new PostgresBacktestRunRepository(postgres);
|
||||
const execute = async (id: string) => {
|
||||
const engine = new RustBacktestAdapter({
|
||||
|
|
@ -192,7 +192,7 @@ async function fixture(poolMax = 1, withWalkForward = true) {
|
|||
{ autoStart: false }
|
||||
),
|
||||
validations,
|
||||
walks: new WalkForwardService(container),
|
||||
walks: services.walkForwards,
|
||||
repository,
|
||||
execute,
|
||||
api,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue