T-4: Close research-agent holdout leak

Merge a8c5fe2d using host Build #221 and PostgreSQL Test #222 as sign-off. Keep the dev branch bun.lock.
This commit is contained in:
Boki 2026-11-27 02:29:24 -05:00
commit 0c0ebb6f44
17 changed files with 1512 additions and 86 deletions

View file

@ -1,12 +1,17 @@
import { Hono } from 'hono';
import { z } from 'zod';
import { getCredentialHttpPrincipal, getVerifiedHttpPrincipal } from '@stock-bot/di';
import {
getCredentialHttpPrincipal,
getResearchHttpPrincipal,
getVerifiedHttpPrincipal,
} from '@stock-bot/di';
import type { IServiceContainer } from '@stock-bot/handlers';
import { getLogger } from '@stock-bot/logger';
import {
isBacktestRequestError,
rejectCatalogVersionReferences,
} from '@stock-bot/stock-config/backtest';
import { isResearchAgent } from '@stock-bot/types';
import {
BacktestRetentionError,
BacktestServiceV2,
@ -21,6 +26,10 @@ const logger = getLogger('backtests-v2-routes');
export function createBacktestV2Routes(container: IServiceContainer) {
const app = new Hono();
const backtestService = new BacktestServiceV2(container);
const agentScope = (request: Request) => {
const principal = getResearchHttpPrincipal(request);
return principal && isResearchAgent(principal) ? principal : undefined;
};
// Backtest endpoints
app.get('/api/v2/backtests', async c => {
@ -31,10 +40,12 @@ export function createBacktestV2Routes(container: IServiceContainer) {
if (archiveQuery !== undefined && archiveQuery !== 'true' && archiveQuery !== 'false') {
return c.json({ error: 'includeArchived must be true or false' }, 400);
}
const agentId = agentScope(c.req.raw);
const backtests = await backtestService.listBacktests({
limit,
offset,
includeArchived: archiveQuery === 'true',
...(agentId ? { agentId } : {}),
});
return c.json(backtests);
} catch (error) {
@ -138,7 +149,10 @@ export function createBacktestV2Routes(container: IServiceContainer) {
// Run endpoints
app.get('/api/v2/backtests/:backtestId/runs', async c => {
try {
const runs = await backtestService.listRuns(c.req.param('backtestId'));
const runs = await backtestService.listRuns(
c.req.param('backtestId'),
agentScope(c.req.raw)
);
return c.json(runs);
} catch (error) {
logger.error('Failed to list runs', error);

View file

@ -3,6 +3,7 @@ import { z } from 'zod';
import { getResearchHttpPrincipal } from '@stock-bot/di';
import { getLogger } from '@stock-bot/logger';
import type { IServiceContainer } from '@stock-bot/services';
import { isResearchAgent } from '@stock-bot/types';
import {
parseCostSensitivityInput,
parseCostSensitivitySubmission,
@ -68,6 +69,10 @@ export function createExperimentRoutes(
logger.error('Experiment operation failed', error);
return c.json({ error: 'Experiment operation unavailable' }, 500);
});
const agentScope = (request: Request) => {
const principal = getResearchHttpPrincipal(request);
return principal && isResearchAgent(principal) ? principal : undefined;
};
const body = async (request: Request): Promise<unknown> => {
try {
return await request.json();
@ -116,7 +121,9 @@ export function createExperimentRoutes(
return c.json({ walkForward }, 202);
});
app.get('/api/v2/experiments/walk-forward', async c =>
c.json({ walkForwards: await walkForwards.list(c.req.query('backtestId')) })
c.json({
walkForwards: await walkForwards.list(c.req.query('backtestId'), agentScope(c.req.raw)),
})
);
app.get('/api/v2/experiments/walk-forward/:id', async c =>
c.json({ walkForward: await walkForwards.get(experimentId(c.req.param('id'))) })
@ -167,7 +174,9 @@ export function createExperimentRoutes(
return c.json({ validation }, 202);
});
app.get('/api/v2/experiments/validation', async c =>
c.json({ validations: await validations.list(c.req.query('backtestId')) })
c.json({
validations: await validations.list(c.req.query('backtestId'), agentScope(c.req.raw)),
})
);
app.get('/api/v2/experiments/validation/:id', async c =>
c.json({ validation: await validations.get(experimentId(c.req.param('id'))) })
@ -235,7 +244,7 @@ export function createExperimentRoutes(
if (filter !== undefined) {
experimentId(filter);
}
return c.json({ experiments: await service.list(filter) });
return c.json({ experiments: await service.list(filter, agentScope(c.req.raw)) });
});
app.get('/api/v2/experiments/compare', async c => {
const params = new URL(c.req.url).searchParams;

View file

@ -3,6 +3,7 @@ import { z } from 'zod';
import { getResearchHttpPrincipal } from '@stock-bot/di';
import type { IServiceContainer } from '@stock-bot/services';
import { archiveResearchSchema, parseResearchInput } from '@stock-bot/stock-config/research';
import { isResearchAgent } from '@stock-bot/types';
import { researchAgentDatabaseError, ResearchAgentError } from '../services/research-agent-budget';
import { ResearchError } from '../services/research-errors';
import { createResearchServices } from '../services/research-services';
@ -75,9 +76,17 @@ export function createResearchRoutes(
return principal;
};
const agentScope = (request: Request) => {
const principal = getResearchHttpPrincipal(request);
return principal && isResearchAgent(principal) ? principal : undefined;
};
app.get('/api/v2/research-projects', async c =>
c.json({
projects: await service.listProjects(includeArchived(c.req.query('includeArchived'))),
projects: await service.listProjects(
includeArchived(c.req.query('includeArchived')),
agentScope(c.req.raw)
),
})
);
app.post('/api/v2/research-projects', async c =>
@ -157,7 +166,8 @@ export function createResearchRoutes(
c.json({
artifacts: await service.listArtifacts(
c.req.param('id'),
includeArchived(c.req.query('includeArchived'))
includeArchived(c.req.query('includeArchived')),
agentScope(c.req.raw)
),
})
);
@ -174,10 +184,12 @@ export function createResearchRoutes(
)
);
app.get('/api/v2/analysis-artifacts/:id', async c =>
c.json({ artifact: await service.getArtifact(c.req.param('id')) })
c.json({ artifact: await service.getArtifact(c.req.param('id'), agentScope(c.req.raw)) })
);
app.get('/api/v2/analysis-artifacts/:id/revisions', async c =>
c.json({ revisions: await service.listArtifactRevisions(c.req.param('id')) })
c.json({
revisions: await service.listArtifactRevisions(c.req.param('id'), agentScope(c.req.raw)),
})
);
app.post('/api/v2/analysis-artifacts/:id/revisions', async c =>
c.json(

View file

@ -2,6 +2,7 @@ import { Hono, type MiddlewareHandler } from 'hono';
import { z } from 'zod';
import { getResearchHttpPrincipal } from '@stock-bot/di';
import type { IServiceContainer } from '@stock-bot/services';
import { isResearchAgent } from '@stock-bot/types';
import { researchAgentDatabaseError, ResearchAgentError } from '../services/research-agent-budget';
import { StrategyCatalogError, StrategyCatalogService } from '../services/strategy-catalog.service';
@ -61,7 +62,13 @@ export function createStrategyRoutes(
}
return value;
};
app.get('/api/v2/strategies', async c => c.json({ strategies: await service.listStrategies() }));
const agentScope = (request: Request) => {
const principal = getResearchHttpPrincipal(request);
return principal && isResearchAgent(principal) ? principal : undefined;
};
app.get('/api/v2/strategies', async c =>
c.json({ strategies: await service.listStrategies(agentScope(c.req.raw)) })
);
app.post('/api/v2/strategies', async c =>
c.json({ strategy: await service.createStrategy(await c.req.json(), actor(c.req.raw)) }, 201)
);