feat(research): cut over canonical runtime and enforce agent budgets

This commit is contained in:
Boki 2026-10-24 16:47:19 -04:00
parent ab08db82fd
commit bbf0dd4453
103 changed files with 6138 additions and 3112 deletions

View file

@ -1,6 +1,10 @@
/** Prepared inputs only: this contract does not admit runs or certify out-of-sample evidence. */
import { z } from 'zod';
import type { CanonicalBacktestAnalytics, HistoricalDatasetManifestV2 } from '@stock-bot/types';
import type {
CanonicalBacktestAnalytics,
HistoricalDatasetManifestV2,
ResearchActor,
} from '@stock-bot/types';
import type { CanonicalRunRequest } from './canonical-research';
import {
experimentInputSchema,
@ -123,7 +127,7 @@ export interface ChronologicalValidationSelection {
previewHash: string;
selectionHash: string;
createdAt: string;
principal: 'operator' | 'service';
principal: ResearchActor;
/** Current original-version approval for the new holdout admissions. */
reviewId: string;
winner: ExperimentCandidate;
@ -139,7 +143,7 @@ export interface ChronologicalValidationReport {
selectionHash: string;
reportHash: string;
createdAt: string;
principal: 'operator' | 'service';
principal: ResearchActor;
/** Independent reset accounts. This is not a continuous portfolio equity curve. */
reset: 'initial-capital-empty-positions';
/** All training candidates in ascending original candidate index. */
@ -155,7 +159,7 @@ export interface ChronologicalValidationActionReceipt {
previewHash: string;
selectionHash: string;
createdAt: string;
principal: 'operator' | 'service';
principal: ResearchActor;
}
export interface ValidationActionCapability {
available: boolean;

View file

@ -3,6 +3,7 @@ import { z } from 'zod';
import type {
CanonicalBacktestAnalytics,
HistoricalDatasetManifestV2,
ResearchActor,
StrategyParameterValue,
} from '@stock-bot/types';
import type { CanonicalRunRequest } from './canonical-research';
@ -67,7 +68,7 @@ export interface ExperimentTrialRetry {
pausedAt: string | null;
failureUpdatedAt: string;
recordedAt: string;
principal: 'operator' | 'service';
principal: ResearchActor;
/** Current approval at retry admission; the original run snapshot stays immutable. */
reviewId: string;
}

View file

@ -0,0 +1,76 @@
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,
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}$/) })
.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;
}

View file

@ -1,8 +1,12 @@
import { z } from 'zod';
import { isResearchActor, type ResearchActor } from '@stock-bot/types';
import { assertStrategyJsonText } from './strategy';
export const researchIdSchema = z.string().uuid();
export const researchActorSchema = z.enum(['operator', 'service']);
export const researchActorSchema = z.custom<ResearchActor>(
isResearchActor,
'Invalid research actor'
);
const origin = z.enum(['human', 'ai', 'unknown']).optional();
const title = z.string().trim().min(1);
const ordinal = z.number().int().positive().max(2147483647);

View file

@ -1,5 +1,6 @@
/** A bounded collection of existing chronological validations, not a new execution engine. */
import { z } from 'zod';
import type { ResearchActor } from '@stock-bot/types';
import {
chronologicalValidationInputSchema,
type ChronologicalValidationDetail,
@ -70,7 +71,7 @@ export interface WalkForwardReport {
previewHash: string;
reportHash: string;
createdAt: string;
principal: 'operator' | 'service';
principal: ResearchActor;
reset: 'initial-capital-empty-positions';
/** Every frozen window in ascending index; absent evidence cannot be omitted. */
segments: WalkForwardSegment[];
@ -107,7 +108,7 @@ export interface WalkForwardReceipt {
previewHash: string;
reportHash: string;
createdAt: string;
principal: 'operator' | 'service';
principal: ResearchActor;
}
export interface WalkForwardActionResult {
receipt: WalkForwardReceipt;

View file

@ -85,7 +85,7 @@ export const stockAppSchema = baseAppSchema.extend({
orchestrator: z
.object({
port: z.number().default(3002),
defaultMode: z.enum(['backtest', 'paper', 'live']).default('paper'),
defaultMode: z.enum(['backtest', 'paper', 'live']).default('backtest'),
paperTradingCapital: z.number().default(100000),
// Validated by the orchestrator's canonical contract before opening resources.
canonicalPaper: z.record(z.unknown()).optional(),