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
94
apps/stock/web-api/src/services/research-agent-budget.ts
Normal file
94
apps/stock/web-api/src/services/research-agent-budget.ts
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import { createHash } from 'node:crypto';
|
||||
import type { QueryResultRow } from 'pg';
|
||||
import type { CanonicalRunRequest } from '@stock-bot/stock-config/canonical-research';
|
||||
import { canonicalStrategyJson } from '@stock-bot/stock-config/strategy';
|
||||
import { isResearchAgent } from '@stock-bot/types';
|
||||
|
||||
export type AgentQuery = {
|
||||
query(text: string, values?: unknown[]): Promise<{ rows: QueryResultRow[] }>;
|
||||
};
|
||||
export class ResearchAgentError extends Error {
|
||||
constructor(
|
||||
readonly status: 400 | 401 | 403 | 404 | 409 | 429,
|
||||
message: string
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
/** Economic inputs are pinned. Only transport identity and strategy parameter variation are excluded. */
|
||||
export function researchGrantPolicy(request: CanonicalRunRequest) {
|
||||
const policy = structuredClone(request.policy);
|
||||
policy.matching.risk.ledger.sessionId = 'agent-grant-run';
|
||||
policy.matching.risk.ledger.accountId = 'agent-grant-account';
|
||||
return {
|
||||
datasetHash: request.datasetHash,
|
||||
versionId: request.strategy.versionId,
|
||||
ownerBudget: request.strategy.selection.ownerBudget,
|
||||
seed: request.strategy.selection.seed,
|
||||
policy,
|
||||
analyticsPolicy: request.analyticsPolicy,
|
||||
build: request.build,
|
||||
modelArtifacts: request.modelArtifacts,
|
||||
};
|
||||
}
|
||||
export const researchGrantHash = (policy: ReturnType<typeof researchGrantPolicy>) =>
|
||||
createHash('sha256').update(canonicalStrategyJson(policy)).digest('hex');
|
||||
/** First lock for every agent admission. Existing receipt replay follows this authentication fence. */
|
||||
export async function lockResearchAgent(database: AgentQuery, principal: string): Promise<void> {
|
||||
if (!isResearchAgent(principal)) {
|
||||
return;
|
||||
}
|
||||
const row = (
|
||||
await database.query('SELECT id,enabled FROM research_agents WHERE id=$1 FOR UPDATE', [
|
||||
principal,
|
||||
])
|
||||
).rows[0];
|
||||
if (!row?.enabled) {
|
||||
throw new ResearchAgentError(403, 'Research principal is disabled or unavailable');
|
||||
}
|
||||
}
|
||||
/** SQL guards are authoritative; map only the deliberately typed guard diagnostics. */
|
||||
export function researchAgentDatabaseError(error: unknown): ResearchAgentError | null {
|
||||
if (
|
||||
error &&
|
||||
typeof error === 'object' &&
|
||||
'code' in error &&
|
||||
'message' in error &&
|
||||
error.code === 'P0001' &&
|
||||
typeof error.message === 'string'
|
||||
) {
|
||||
if (error.message.startsWith('research_budget:')) {
|
||||
return new ResearchAgentError(429, error.message.slice('research_budget:'.length));
|
||||
}
|
||||
if (error.message.startsWith('research_access:')) {
|
||||
return new ResearchAgentError(403, error.message.slice('research_access:'.length));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Operator interventions must reserve the owning agent before the existing parent/resource locks. */
|
||||
export async function lockExperimentResearchAgent(
|
||||
database: AgentQuery,
|
||||
experimentId: string,
|
||||
principal: string,
|
||||
ownerOnly = false
|
||||
): Promise<void> {
|
||||
const owner = (
|
||||
await database.query('SELECT submission_principal FROM research_experiments WHERE id=$1', [
|
||||
experimentId,
|
||||
])
|
||||
).rows[0]?.submission_principal;
|
||||
if (isResearchAgent(principal) && owner !== undefined && owner !== principal) {
|
||||
throw new ResearchAgentError(403, 'Research action requires its owning principal');
|
||||
}
|
||||
if (ownerOnly && isResearchAgent(owner) && owner !== principal) {
|
||||
throw new ResearchAgentError(
|
||||
403,
|
||||
'New holdouts must be selected by the owning research principal'
|
||||
);
|
||||
}
|
||||
if (isResearchAgent(owner) && owner !== principal) {
|
||||
await lockResearchAgent(database, owner);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue