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

@ -48,14 +48,14 @@ export class BacktestDispatcher {
// Recovery is independent of Redis availability. Every terminal write fences the old owner.
await this.postgres.transaction(async client => {
await client.query(
`UPDATE runs SET
`UPDATE runs AS r SET
status = CASE WHEN cancel_requested THEN 'cancelled'::run_status
WHEN attempt_number >= $1 THEN 'failed'::run_status ELSE 'pending'::run_status END,
WHEN (attempt_number >= LEAST($1,COALESCE((to_jsonb(r)#>>'{research_limits,maxAttempts}')::integer,$1)) OR ((to_jsonb(r)->>'research_deadline_at')::timestamptz <= clock_timestamp())) THEN 'failed'::run_status ELSE 'pending'::run_status END,
error = CASE WHEN cancel_requested THEN NULL
WHEN attempt_number >= $1 THEN 'Execution lease expired; retry limit reached' ELSE NULL END,
completed_at = CASE WHEN cancel_requested OR attempt_number >= $1 THEN NOW() ELSE NULL END,
WHEN (attempt_number >= LEAST($1,COALESCE((to_jsonb(r)#>>'{research_limits,maxAttempts}')::integer,$1)) OR ((to_jsonb(r)->>'research_deadline_at')::timestamptz <= clock_timestamp())) THEN CASE WHEN ((to_jsonb(r)->>'research_deadline_at')::timestamptz <= clock_timestamp()) THEN 'Research attempt wall time exceeded' ELSE 'Execution lease expired; retry limit reached' END ELSE NULL END,
completed_at = CASE WHEN cancel_requested OR (attempt_number >= LEAST($1,COALESCE((to_jsonb(r)#>>'{research_limits,maxAttempts}')::integer,$1)) OR ((to_jsonb(r)->>'research_deadline_at')::timestamptz <= clock_timestamp())) THEN NOW() ELSE NULL END,
attempt_id = NULL, lease_expires_at = NULL, updated_at = NOW()
WHERE status IN ('running', 'paused') AND lease_expires_at <= clock_timestamp()`,
WHERE status IN ('running', 'paused') AND (lease_expires_at <= clock_timestamp() OR ((to_jsonb(r)->>'research_deadline_at')::timestamptz <= clock_timestamp()))`,
[this.options.maxAttempts]
);
});