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
|
|
@ -7,6 +7,7 @@ import { Hono } from 'hono';
|
|||
import { cors } from 'hono/cors';
|
||||
import { createHttpSecurity, httpSecurityOptions, type HttpSecurity } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/handlers';
|
||||
import { ResearchAgentsService } from '../services/research-agents.service';
|
||||
import { createBacktestRoutes } from './backtest.routes';
|
||||
import { createBacktestV2Routes } from './backtests-v2.routes';
|
||||
import { createDatasetRoutes } from './datasets.routes';
|
||||
|
|
@ -15,6 +16,7 @@ import { createExperimentRoutes } from './experiments.routes';
|
|||
import { createHealthRoutes } from './health.routes';
|
||||
import { createMonitoringRoutes } from './monitoring.routes';
|
||||
import { createPipelineRoutes } from './pipeline.routes';
|
||||
import { createResearchAgentRoutes } from './research-agents.routes';
|
||||
import { createResearchRoutes } from './research.routes';
|
||||
import { createStrategyRoutes } from './strategies.routes';
|
||||
|
||||
|
|
@ -22,10 +24,15 @@ import { createStrategyRoutes } from './strategies.routes';
|
|||
// the old 1 MiB restriction; normalizers still validate every submitted record.
|
||||
export const WEB_API_MAX_REQUEST_BODY_SIZE = Number.MAX_SAFE_INTEGER;
|
||||
|
||||
export function createRoutes(
|
||||
container: IServiceContainer,
|
||||
security: HttpSecurity = createHttpSecurity({ ...httpSecurityOptions(), browserSessions: true })
|
||||
): Hono {
|
||||
export function createRoutes(container: IServiceContainer, suppliedSecurity?: HttpSecurity): Hono {
|
||||
const agents = new ResearchAgentsService(container);
|
||||
const security =
|
||||
suppliedSecurity ??
|
||||
createHttpSecurity({
|
||||
...httpSecurityOptions(),
|
||||
browserSessions: true,
|
||||
resolveResearchCredential: request => agents.authenticate(request),
|
||||
});
|
||||
const app = new Hono();
|
||||
|
||||
// Add CORS middleware
|
||||
|
|
@ -58,6 +65,7 @@ export function createRoutes(
|
|||
app.route('/', createDatasetRoutes(container));
|
||||
app.route('/', createStrategyRoutes(container));
|
||||
app.route('/', createResearchRoutes(container));
|
||||
app.route('/', createResearchAgentRoutes(container, security.researchEnabled));
|
||||
app.route('/', createExperimentRoutes(container));
|
||||
|
||||
return app;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import { Hono, type MiddlewareHandler } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { getCredentialHttpPrincipal } from '@stock-bot/di';
|
||||
import { getResearchHttpPrincipal } from '@stock-bot/di';
|
||||
import { getLogger } from '@stock-bot/logger';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { ChronologicalValidationService } from '../services/chronological-validation.service';
|
||||
|
|
@ -17,6 +17,7 @@ import {
|
|||
} from '../services/experiment-input';
|
||||
import { parseExperimentRetry } from '../services/experiment-retry';
|
||||
import { ExperimentsService } from '../services/experiments.service';
|
||||
import { researchAgentDatabaseError, ResearchAgentError } from '../services/research-agent-budget';
|
||||
import {
|
||||
parseValidationFinalization,
|
||||
parseValidationSelection,
|
||||
|
|
@ -39,7 +40,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
const logger = getLogger('experiments-api');
|
||||
const credentials: MiddlewareHandler = async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
if (!['GET', 'HEAD'].includes(c.req.method) && !getCredentialHttpPrincipal(c.req.raw)) {
|
||||
if (!['GET', 'HEAD'].includes(c.req.method) && !getResearchHttpPrincipal(c.req.raw)) {
|
||||
return c.json({ error: 'Verified credential principal required' }, 401);
|
||||
}
|
||||
await next();
|
||||
|
|
@ -48,6 +49,17 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
app.use('/api/v2/experiments', credentials);
|
||||
app.use('/api/v2/experiments/*', credentials);
|
||||
app.onError((error, c) => {
|
||||
const agentError =
|
||||
error instanceof ResearchAgentError ? error : researchAgentDatabaseError(error);
|
||||
if (agentError) {
|
||||
return c.json(
|
||||
{
|
||||
error: agentError.message,
|
||||
code: agentError.status === 429 ? 'research_budget_exceeded' : 'research_access_denied',
|
||||
},
|
||||
agentError.status
|
||||
);
|
||||
}
|
||||
if (error instanceof ExperimentError) {
|
||||
return c.json({ error: error.message }, error.status);
|
||||
}
|
||||
|
|
@ -71,7 +83,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
const experiment = await service.submitCosts(
|
||||
input,
|
||||
experimentReceipt({
|
||||
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
|
||||
principal: getResearchHttpPrincipal(c.req.raw) ?? '',
|
||||
key: c.req.header('Idempotency-Key') ?? '',
|
||||
})
|
||||
);
|
||||
|
|
@ -86,7 +98,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
const walkForward = await walkForwards.submit(
|
||||
input,
|
||||
experimentReceipt({
|
||||
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
|
||||
principal: getResearchHttpPrincipal(c.req.raw) ?? '',
|
||||
key: c.req.header('Idempotency-Key') ?? '',
|
||||
})
|
||||
);
|
||||
|
|
@ -107,7 +119,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
id,
|
||||
input,
|
||||
experimentReceipt({
|
||||
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
|
||||
principal: getResearchHttpPrincipal(c.req.raw) ?? '',
|
||||
key: c.req.header('Idempotency-Key') ?? '',
|
||||
})
|
||||
),
|
||||
|
|
@ -135,7 +147,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
const validation = await validations.submit(
|
||||
input,
|
||||
experimentReceipt({
|
||||
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
|
||||
principal: getResearchHttpPrincipal(c.req.raw) ?? '',
|
||||
key: c.req.header('Idempotency-Key') ?? '',
|
||||
})
|
||||
);
|
||||
|
|
@ -156,7 +168,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
id,
|
||||
input,
|
||||
experimentReceipt({
|
||||
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
|
||||
principal: getResearchHttpPrincipal(c.req.raw) ?? '',
|
||||
key: c.req.header('Idempotency-Key') ?? '',
|
||||
})
|
||||
),
|
||||
|
|
@ -171,7 +183,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
id,
|
||||
input,
|
||||
experimentReceipt({
|
||||
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
|
||||
principal: getResearchHttpPrincipal(c.req.raw) ?? '',
|
||||
key: c.req.header('Idempotency-Key') ?? '',
|
||||
})
|
||||
),
|
||||
|
|
@ -197,7 +209,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
app.post('/api/v2/experiments', async c => {
|
||||
const input = parseExperimentSubmission(await body(c.req.raw));
|
||||
const receipt = experimentReceipt({
|
||||
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
|
||||
principal: getResearchHttpPrincipal(c.req.raw) ?? '',
|
||||
key: c.req.header('Idempotency-Key') ?? '',
|
||||
});
|
||||
const experiment = await service.submit(input, receipt);
|
||||
|
|
@ -250,7 +262,7 @@ export function createExperimentRoutes(container: IServiceContainer) {
|
|||
runId = experimentId(c.req.param('runId'));
|
||||
const input = parseExperimentRetry(await body(c.req.raw));
|
||||
const receipt = experimentReceipt({
|
||||
principal: getCredentialHttpPrincipal(c.req.raw) ?? '',
|
||||
principal: getResearchHttpPrincipal(c.req.raw) ?? '',
|
||||
key: c.req.header('Idempotency-Key') ?? '',
|
||||
});
|
||||
const result = await service.retryTrial(id, runId, input, receipt);
|
||||
|
|
|
|||
63
apps/stock/web-api/src/routes/research-agents.routes.ts
Normal file
63
apps/stock/web-api/src/routes/research-agents.routes.ts
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import { Hono, type MiddlewareHandler } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { getCredentialHttpPrincipal, getResearchHttpPrincipal } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { isResearchAgent } from '@stock-bot/types';
|
||||
import { ResearchAgentError } from '../services/research-agent-budget';
|
||||
import { ResearchAgentsService } from '../services/research-agents.service';
|
||||
|
||||
export function createResearchAgentRoutes(container: IServiceContainer, researchEnabled = false) {
|
||||
const app = new Hono(),
|
||||
service = new ResearchAgentsService(container);
|
||||
const operatorOnly: MiddlewareHandler = async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
if (!researchEnabled) {
|
||||
return c.json(
|
||||
{
|
||||
error:
|
||||
'Research credentials require enabled authentication and the web research resolver',
|
||||
},
|
||||
403
|
||||
);
|
||||
}
|
||||
if (getCredentialHttpPrincipal(c.req.raw) !== 'operator') {
|
||||
return c.json({ error: 'Human operator credentials required' }, 403);
|
||||
}
|
||||
await next();
|
||||
return undefined;
|
||||
};
|
||||
app.use('/api/v2/research-agents', operatorOnly);
|
||||
app.use('/api/v2/research-agents/*', operatorOnly);
|
||||
app.use('/api/v2/research-agent/*', async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
await next();
|
||||
});
|
||||
app.onError((error, c) => {
|
||||
if (error instanceof ResearchAgentError) {
|
||||
return c.json({ error: error.message }, error.status);
|
||||
}
|
||||
if (error instanceof z.ZodError || error instanceof SyntaxError) {
|
||||
return c.json({ error: 'Invalid research principal policy' }, 400);
|
||||
}
|
||||
container.logger.error('Research principal operation failed', { error });
|
||||
return c.json({ error: 'Research principal operation unavailable' }, 500);
|
||||
});
|
||||
app.get('/api/v2/research-agents', async c => c.json({ agents: await service.list() }));
|
||||
app.post('/api/v2/research-agents', async c =>
|
||||
c.json(await service.create(await c.req.json()), 201)
|
||||
);
|
||||
app.patch('/api/v2/research-agents/:id', async c =>
|
||||
c.json({ agent: await service.update(c.req.param('id'), await c.req.json()) })
|
||||
);
|
||||
app.get('/api/v2/research-agents/:id/budget', async c =>
|
||||
c.json(await service.budget(c.req.param('id'), true))
|
||||
);
|
||||
app.get('/api/v2/research-agent/budget', async c => {
|
||||
const principal = getResearchHttpPrincipal(c.req.raw);
|
||||
if (!isResearchAgent(principal)) {
|
||||
throw new ResearchAgentError(403, 'Research credentials required');
|
||||
}
|
||||
return c.json(await service.budget(principal));
|
||||
});
|
||||
return app;
|
||||
}
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
import { Hono, type MiddlewareHandler } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { getCredentialHttpPrincipal } from '@stock-bot/di';
|
||||
import { getResearchHttpPrincipal } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { archiveResearchSchema, parseResearchInput } from '@stock-bot/stock-config/research';
|
||||
import { researchAgentDatabaseError, ResearchAgentError } from '../services/research-agent-budget';
|
||||
import { ResearchError } from '../services/research-errors';
|
||||
import { ResearchService } from '../services/research.service';
|
||||
|
||||
|
|
@ -29,7 +30,7 @@ export function createResearchRoutes(container: IServiceContainer) {
|
|||
const service = new ResearchService(container);
|
||||
const credentials: MiddlewareHandler = async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
if (!['GET', 'HEAD'].includes(c.req.method) && !getCredentialHttpPrincipal(c.req.raw)) {
|
||||
if (!['GET', 'HEAD'].includes(c.req.method) && !getResearchHttpPrincipal(c.req.raw)) {
|
||||
return c.json({ error: 'Verified credential principal required' }, 401);
|
||||
}
|
||||
await next();
|
||||
|
|
@ -44,6 +45,17 @@ export function createResearchRoutes(container: IServiceContainer) {
|
|||
app.use(path, credentials);
|
||||
}
|
||||
app.onError((error, c) => {
|
||||
const agentError =
|
||||
error instanceof ResearchAgentError ? error : researchAgentDatabaseError(error);
|
||||
if (agentError) {
|
||||
return c.json(
|
||||
{
|
||||
error: agentError.message,
|
||||
code: agentError.status === 429 ? 'research_budget_exceeded' : 'research_access_denied',
|
||||
},
|
||||
agentError.status
|
||||
);
|
||||
}
|
||||
if (error instanceof ResearchError) {
|
||||
return c.json({ error: error.message }, error.status);
|
||||
}
|
||||
|
|
@ -54,7 +66,7 @@ export function createResearchRoutes(container: IServiceContainer) {
|
|||
return c.json({ error: 'Research workspace unavailable' }, 500);
|
||||
});
|
||||
const actor = (request: Request) => {
|
||||
const principal = getCredentialHttpPrincipal(request);
|
||||
const principal = getResearchHttpPrincipal(request);
|
||||
if (!principal) {
|
||||
throw new ResearchError(401, 'Verified credential principal required');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
import { Hono, type MiddlewareHandler } from 'hono';
|
||||
import { z } from 'zod';
|
||||
import { getCredentialHttpPrincipal } from '@stock-bot/di';
|
||||
import { getResearchHttpPrincipal } from '@stock-bot/di';
|
||||
import type { IServiceContainer } from '@stock-bot/services';
|
||||
import { researchAgentDatabaseError, ResearchAgentError } from '../services/research-agent-budget';
|
||||
import { StrategyCatalogError, StrategyCatalogService } from '../services/strategy-catalog.service';
|
||||
|
||||
const publication = z
|
||||
|
|
@ -15,7 +16,7 @@ export function createStrategyRoutes(container: IServiceContainer) {
|
|||
const service = new StrategyCatalogService(container);
|
||||
const credentials: MiddlewareHandler = async (c, next) => {
|
||||
c.header('Cache-Control', 'no-store');
|
||||
if (!['GET', 'HEAD'].includes(c.req.method) && !getCredentialHttpPrincipal(c.req.raw)) {
|
||||
if (!['GET', 'HEAD'].includes(c.req.method) && !getResearchHttpPrincipal(c.req.raw)) {
|
||||
return c.json({ error: 'Verified credential principal required' }, 401);
|
||||
}
|
||||
await next();
|
||||
|
|
@ -29,6 +30,17 @@ export function createStrategyRoutes(container: IServiceContainer) {
|
|||
app.use(path, credentials);
|
||||
}
|
||||
app.onError((error, c) => {
|
||||
const agentError =
|
||||
error instanceof ResearchAgentError ? error : researchAgentDatabaseError(error);
|
||||
if (agentError) {
|
||||
return c.json(
|
||||
{
|
||||
error: agentError.message,
|
||||
code: agentError.status === 429 ? 'research_budget_exceeded' : 'research_access_denied',
|
||||
},
|
||||
agentError.status
|
||||
);
|
||||
}
|
||||
if (error instanceof StrategyCatalogError) {
|
||||
return c.json({ error: error.message }, error.status);
|
||||
}
|
||||
|
|
@ -41,7 +53,7 @@ export function createStrategyRoutes(container: IServiceContainer) {
|
|||
// The mounted security layer establishes read access. Writes additionally require
|
||||
// actual credentials, including on development listeners with auth bypass enabled.
|
||||
const actor = (request: Request) => {
|
||||
const value = getCredentialHttpPrincipal(request);
|
||||
const value = getResearchHttpPrincipal(request);
|
||||
if (!value) {
|
||||
throw new StrategyCatalogError(401, 'Verified credential principal required');
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue