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
|
|
@ -28,6 +28,7 @@ import type {
|
|||
ResearchProject,
|
||||
StrategyRecord,
|
||||
} from '@stock-bot/types';
|
||||
import { lockResearchAgent } from './research-agent-budget';
|
||||
import { ResearchError } from './research-errors';
|
||||
import { ResearchEvidenceResolver } from './research-evidence';
|
||||
import { StrategyCatalogService } from './strategy-catalog.service';
|
||||
|
|
@ -47,12 +48,16 @@ function required(row: QueryResultRow | undefined, label: string): QueryResultRo
|
|||
}
|
||||
function authorship(actor: ResearchActor, origin?: ResearchContentOrigin): ResearchAuthorship {
|
||||
researchActorSchema.parse(actor);
|
||||
if (actor === 'service' && origin === 'human') {
|
||||
throw new ResearchError(403, 'Service credentials cannot declare human authorship');
|
||||
if (actor.startsWith('agent:') && origin !== undefined && origin !== 'ai') {
|
||||
throw new ResearchError(403, 'Research principals must declare AI authorship');
|
||||
}
|
||||
if (actor !== 'operator' && origin === 'human') {
|
||||
throw new ResearchError(403, 'Only operator credentials can declare human authorship');
|
||||
}
|
||||
return {
|
||||
submittedBy: actor,
|
||||
contentOrigin: origin ?? (actor === 'operator' ? 'human' : 'unknown'),
|
||||
contentOrigin:
|
||||
origin ?? (actor === 'operator' ? 'human' : actor.startsWith('agent:') ? 'ai' : 'unknown'),
|
||||
};
|
||||
}
|
||||
function project(row: QueryResultRow): ResearchProject {
|
||||
|
|
@ -169,6 +174,7 @@ export class ResearchService {
|
|||
throw new ResearchError(400, 'Supply a project change');
|
||||
}
|
||||
return this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
const old = await this.projectRow(client, id, true);
|
||||
expectRevision(old.revision, data.expectedRevision);
|
||||
if (
|
||||
|
|
@ -217,6 +223,7 @@ export class ResearchService {
|
|||
researchIdSchema.parse(strategyId);
|
||||
researchActorSchema.parse(actor);
|
||||
await this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
active(await this.projectRow(client, projectId, true), 'Project');
|
||||
const selected = required(
|
||||
(await client.query('SELECT * FROM strategies WHERE id=$1 FOR UPDATE', [strategyId]))
|
||||
|
|
@ -241,6 +248,7 @@ export class ResearchService {
|
|||
researchIdSchema.parse(strategyId);
|
||||
researchActorSchema.parse(actor);
|
||||
await this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
active(await this.projectRow(client, projectId, true), 'Project');
|
||||
const removed = await client.query(
|
||||
'DELETE FROM research_project_strategies WHERE project_id=$1 AND strategy_id=$2 RETURNING project_id',
|
||||
|
|
@ -274,6 +282,7 @@ export class ResearchService {
|
|||
const data = parseResearchInput(createResearchHypothesisSchema, input);
|
||||
const author = authorship(actor, data.contentOrigin);
|
||||
return this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
active(await this.projectRow(client, projectId, true), 'Project');
|
||||
const row = required(
|
||||
(
|
||||
|
|
@ -313,6 +322,7 @@ export class ResearchService {
|
|||
const data = parseResearchInput(reviseResearchHypothesisSchema, input);
|
||||
const author = authorship(actor, data.contentOrigin);
|
||||
return this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
const row = await this.lockHypothesis(client, id);
|
||||
active(row, 'Hypothesis');
|
||||
const current = await this.hypothesis(client, row);
|
||||
|
|
@ -339,6 +349,7 @@ export class ResearchService {
|
|||
const data = parseResearchInput(archiveResearchSchema, input);
|
||||
researchActorSchema.parse(actor);
|
||||
return this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
await this.lockHypothesis(client, id);
|
||||
const row = required(
|
||||
(
|
||||
|
|
@ -377,6 +388,7 @@ export class ResearchService {
|
|||
const author = authorship(actor, data.contentOrigin);
|
||||
this.requireFindingEvidence(data.kind, data.evidence);
|
||||
return this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
active(await this.projectRow(client, projectId, true), 'Project');
|
||||
if (data.hypothesisRevisionId) {
|
||||
const linked = (
|
||||
|
|
@ -440,6 +452,7 @@ export class ResearchService {
|
|||
const data = parseResearchInput(reviseResearchArtifactSchema, input);
|
||||
const author = authorship(actor, data.contentOrigin);
|
||||
return this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
const row = await this.lockArtifact(client, id);
|
||||
active(row, 'Artifact');
|
||||
this.requireFindingEvidence(row.kind, data.evidence);
|
||||
|
|
@ -482,6 +495,7 @@ export class ResearchService {
|
|||
const data = parseResearchInput(archiveResearchSchema, input);
|
||||
researchActorSchema.parse(actor);
|
||||
return this.database.transaction(async client => {
|
||||
await lockResearchAgent(client, actor);
|
||||
await this.lockArtifact(client, id);
|
||||
const row = required(
|
||||
(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue