stock-bot/apps/stock/config/src/contracts/research-agents.ts

82 lines
2.4 KiB
TypeScript

import { z } from 'zod';
export const researchAgentIdSchema = z
.string()
.regex(/^agent:[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
const count = z.number().int().positive().max(Number.MAX_SAFE_INTEGER);
export const researchAgentLimitsSchema = z
.object({
maxActiveTrials: count,
maxTrialsPerExperiment: count,
maxBarEvaluationsPerExperiment: count,
maxAttemptWallMs: count.max(2147483647),
maxAttempts: count.max(3),
})
.strict();
export type ResearchAgentLimits = z.infer<typeof researchAgentLimitsSchema>;
const ids = z
.array(
z
.string()
.uuid()
.transform(value => value.toLowerCase())
)
.refine(values => new Set(values).size === values.length, 'Duplicate identifiers');
export const createResearchAgentSchema = z
.object({
name: z.string().trim().min(1),
allowedProjectIds: ids,
allowedBacktestIds: ids,
/** Explicit operator opt-in, a subset of allowed backtests. Omitted means none. */
allowOwnCandidateBacktestIds: ids.optional(),
limits: researchAgentLimitsSchema,
})
.strict();
export const updateResearchAgentSchema = createResearchAgentSchema
.partial()
.extend({
expectedRevision: count,
enabled: z.boolean().optional(),
})
.strict()
.refine(
value => Object.keys(value).some(key => key !== 'expectedRevision'),
'Supply a policy change'
);
export const researchAgentRecordSchema = z
.object({
id: researchAgentIdSchema,
name: z.string().min(1),
enabled: z.boolean(),
revision: count,
allowedProjectIds: ids,
grants: z.array(
z
.object({
backtestId: z.string().uuid(),
policyHash: z.string().regex(/^[a-f0-9]{64}$/),
allowOwnCandidates: z.boolean().optional(),
})
.strict()
),
limits: researchAgentLimitsSchema,
createdAt: z.string().datetime(),
updatedAt: z.string().datetime(),
})
.strict();
export type ResearchAgentRecord = z.infer<typeof researchAgentRecordSchema>;
export const researchAgentBudgetSchema = z
.object({
agent: researchAgentRecordSchema,
activeTrials: z.number().int().nonnegative(),
remainingActiveTrials: z.number().int().nonnegative(),
})
.strict();
export type ResearchAgentBudget = z.infer<typeof researchAgentBudgetSchema>;
export interface ResearchAgentAuditEvent {
id: string;
agentId: string;
operation: string;
entityId: string;
createdAt: string;
}