@@ -5,7 +5,8 @@ import { artifactKey } from "../artifacts";
55import { ArtifactRepository } from "./artifact-repository" ;
66import { safeDiagnosticText } from "../harness-diagnostics" ;
77import { decrypt } from "../crypto" ;
8- import { ExeVmBackend , vmNameFor } from "../exe-vm-backend" ;
8+ import { ExeVmBackend } from "../exe-vm-backend" ;
9+ import type { ExecutionBackend } from "../execution" ;
910import type { ExeRunConnection } from "../exe" ;
1011import type { Env } from "../types" ;
1112import { ConnectionRepository } from "./connection-repository" ;
@@ -18,7 +19,7 @@ import { rolloutTraceSources } from "../trace-source";
1819import { TraceProjectionRepository , TRACE_PARSER_VERSION } from "./trace-projection-repository" ;
1920import { LiveTraceRepository } from "./live-trace-repository" ;
2021import { JOB_RESULT_INSTRUCTIONS , parseJobResult } from "../job-result" ;
21- import { issueRunCredential , runProxyInstructions , runProxyName } from "../run-credentials" ;
22+ import { issueRunCredential , runProxyInstructions } from "../run-credentials" ;
2223
2324export class RunScheduler {
2425 private runs : RunRepository ;
@@ -32,9 +33,11 @@ export class RunScheduler {
3233 }
3334
3435 private connections ( run : PersistedRun ) { return new ConnectionRepository ( this . database , run . tenantId , this . env . CREDENTIAL_ENCRYPTION_KEY ) ; }
35- private async backend ( run : PersistedRun ) {
36+ private async backend ( run : PersistedRun ) : Promise < ExecutionBackend > {
3637 const id = String ( run . executionConfig . connectionId ?? "" ) ;
37- if ( run . executionConfig . agentKind === "Amp" ) { const connection = await this . connections ( run ) . get < AmpConnection > ( `amp:${ id } ` ) ; if ( ! connection ) throw new Error ( "Amp connection is unavailable" ) ; return new AmpBackend ( connection ) ; }
38+ const kind = run . executionConfig . backendKind ?? ( run . executionConfig . agentKind === "Amp" ? "amp" : "exe-vm" ) ;
39+ if ( kind === "amp" ) { const connection = await this . connections ( run ) . get < AmpConnection > ( `amp:${ id } ` ) ; if ( ! connection ) throw new Error ( "Amp connection is unavailable" ) ; return new AmpBackend ( connection ) ; }
40+ if ( kind !== "exe-vm" ) throw new Error ( `Unsupported execution backend: ${ String ( kind ) } ` ) ;
3841 const connection = await this . connections ( run ) . get < ExeRunConnection > ( `exe:${ id } ` ) ; if ( ! connection ) throw new Error ( "exe.dev connection is unavailable" ) ;
3942 return new ExeVmBackend ( { ...connection , tags : Array . isArray ( run . executionConfig . tags ) ? run . executionConfig . tags : [ ] , image : typeof run . executionConfig . image === "string" ? run . executionConfig . image : undefined , model : String ( run . executionConfig . model ?? "" ) , effort : String ( run . executionConfig . effort ?? "" ) } ) ;
4043 }
@@ -45,20 +48,22 @@ export class RunScheduler {
4548 const backend = await this . backend ( run ) , context = run . executionConfig . inputContext ;
4649 if ( ! context || typeof context !== "object" || Array . isArray ( context ) ) throw new Error ( "The code job input context snapshot is missing" ) ;
4750 const runToken = await issueRunCredential ( this . env , run . tenantId , run . id , run . executionConfig . runScopes ) ;
48- await this . runs . recordLaunchHandle ( run , { backendKind : backend . kind , id : vmNameFor ( run . id ) , runProxyName : runProxyName ( run . id ) } ) ;
49- const launched = await backend . launch ( { runId : run . id , prompt : "" , runProxy : { targetOrigin : this . env . APP_ORIGIN , credential : runToken } , code : { script : run . executionConfig . script , context } } ) ;
51+ const reservation = backend . reserve ?.( run . id ) ?? null ;
52+ if ( reservation ) await this . runs . recordLaunchHandle ( run , reservation ) ;
53+ const launched = await backend . launch ( { runId : run . id , reservation : reservation ?? undefined , prompt : "" , runProxy : { targetOrigin : this . env . APP_ORIGIN , credential : runToken } , code : { script : run . executionConfig . script , context } } ) ;
5054 await this . runs . markLaunched ( run , launched . handle , launched . destinationUrl , launched . capabilities ) ;
5155 if ( launched . observation . state === "failed" ) await this . runs . recordObservation ( run , launched . observation ) ;
5256 return ;
5357 }
5458 if ( run . executionConfig . agentKind === "Amp" ) throw new Error ( "Amp execution is unavailable for new runs because it cannot deliver the required structured result file. Choose an exe.dev agent integration." ) ;
55- const backend = await this . backend ( run ) , driver = agentDriver ( run . executionConfig . agentKind as AgentKind ) , harness = driver . launch ( run . id , { model : run . executionConfig . model , effort : run . executionConfig . effort } ) ;
59+ const backend = await this . backend ( run ) , driver = agentDriver ( run . executionConfig . agentKind as AgentKind ) , harness = driver . launch ( run . id , { model : run . executionConfig . model , effort : run . executionConfig . effort , ... ( driver . kind === "codex" ? { codexModel : { baseUrl : this . env . CODEX_MODEL_BASE_URL ?? "https://llm.int.exe.xyz/v1" } } : { } ) } ) ;
5660 const sources = rolloutTraceSources ( driverTraceSources ( driver . kind , harness ) , run . tenantId , this . env ) ;
5761 validateTraceSources ( sources ) ;
5862 const runToken = await issueRunCredential ( this . env , run . tenantId , run . id , run . executionConfig . runScopes ) ;
5963 const prompt = await decrypt ( run . encryptedPrompt , this . env . CREDENTIAL_ENCRYPTION_KEY ) ;
60- await this . runs . recordLaunchHandle ( run , { backendKind : backend . kind , id : vmNameFor ( run . id ) , runProxyName : runProxyName ( run . id ) } ) ;
61- const launched = await backend . launch ( { runId : run . id , prompt : `${ prompt } ${ JOB_RESULT_INSTRUCTIONS } ${ runProxyInstructions ( run . id ) } ` , runProxy : { targetOrigin : this . env . APP_ORIGIN , credential : runToken } , traceSources : sources , harness : { executable : harness . executable , args : harness . args , env : harness . env } } ) ;
64+ const reservation = backend . reserve ?.( run . id ) ?? null ;
65+ if ( reservation ) await this . runs . recordLaunchHandle ( run , reservation ) ;
66+ const launched = await backend . launch ( { runId : run . id , reservation : reservation ?? undefined , prompt : `${ prompt } ${ JOB_RESULT_INSTRUCTIONS } ${ runProxyInstructions ( run . id ) } ` , runProxy : { targetOrigin : this . env . APP_ORIGIN , credential : runToken } , traceSources : sources , harness : { executable : harness . executable , args : harness . args , env : harness . env } } ) ;
6267 await this . runs . markLaunched ( run , launched . handle , launched . destinationUrl , launched . capabilities , sources ) ;
6368 if ( launched . observation . state === "failed" ) await this . runs . recordObservation ( run , launched . observation ) ;
6469 }
0 commit comments