@@ -10,7 +10,7 @@ import { readNativeResult } from "./native-result-file.mjs"
1010import { assertNoRuntimeSourcePaths , sanitizeRuntimeSourceJson , sanitizeRuntimeSourceText , sanitizeRuntimeSourceValue } from "./runtime-source-sanitizer.mjs"
1111import { publishRunnerWorkspace } from "./runner-workspace-publisher.mjs"
1212import { createRunnerWorkspaceSeedSnapshot , RUNNER_WORKSPACE_SEED_EXCLUDES } from "./runner-workspace-seed-snapshot.mjs"
13- import { createTrustedArtifactSnapshot } from "./trusted-artifact-snapshot.mjs"
13+ import { createTrustedArtifactApplyChannel , trustedArtifactApplyRefs } from "./trusted-artifact-snapshot.mjs"
1414
1515const requestPath = process . env . AGENT_TASK_REQUEST_PATH || ".codebox/agent-task-request.json"
1616const workspace = resolve ( process . env . AGENT_TASK_WORKSPACE || process . cwd ( ) )
@@ -24,6 +24,7 @@ let privateRuntimeSourceRoot = ""
2424let privateRuntimeSourceRootForSanitization = ""
2525let runnerWorkspaceSeedSnapshot
2626let reviewerEvidence
27+ let trustedApplyArtifactRoot
2728
2829const SIGNAL_EXIT_CODES = { SIGINT : 130 , SIGTERM : 143 , SIGHUP : 129 }
2930let materializedSourceCleanup
@@ -37,6 +38,10 @@ function claimMaterializedSourcePaths() {
3738 paths . push ( privateRuntimeSourceRoot )
3839 privateRuntimeSourceRoot = ""
3940 }
41+ if ( trustedApplyArtifactRoot ) {
42+ paths . push ( trustedApplyArtifactRoot )
43+ trustedApplyArtifactRoot = ""
44+ }
4045 return paths
4146}
4247// Single idempotent cleanup coordinator for the private runtime source
@@ -152,8 +157,8 @@ function safeEnvironment(extra = {}) {
152157 return { PATH : process . env . PATH || "" , HOME : process . env . HOME || "" , CI : process . env . CI || "true" , ...extra }
153158}
154159
155- function agentEnvironment ( ) {
156- return safeEnvironment ( Object . fromEntries ( [ "OPENAI_API_KEY" , "MODEL_PROVIDER_SECRET_1" , "MODEL_PROVIDER_SECRET_2" , "MODEL_PROVIDER_SECRET_3" , "MODEL_PROVIDER_SECRET_4" , "MODEL_PROVIDER_SECRET_5" , "GITHUB_TOKEN" ] . map ( ( name ) => [ name , process . env [ name ] ] ) . filter ( ( [ , value ] ) => value ) ) )
160+ function agentEnvironment ( extra = { } ) {
161+ return safeEnvironment ( { ... Object . fromEntries ( [ "OPENAI_API_KEY" , "MODEL_PROVIDER_SECRET_1" , "MODEL_PROVIDER_SECRET_2" , "MODEL_PROVIDER_SECRET_3" , "MODEL_PROVIDER_SECRET_4" , "MODEL_PROVIDER_SECRET_5" , "GITHUB_TOKEN" ] . map ( ( name ) => [ name , process . env [ name ] ] ) . filter ( ( [ , value ] ) => value ) ) , ... extra } )
157162}
158163
159164function command ( command , args , cwd , env = safeEnvironment ( ) ) {
@@ -605,8 +610,9 @@ const taskInput = {
605610await writeFile ( executionInputPath , `${ JSON . stringify ( taskInput , null , 2 ) } \n` )
606611
607612let execution = { code : 0 , stdout : "" , stderr : "" , stdout_truncated : false , stderr_truncated : false }
608- if ( request . run_agent && ! request . dry_run ) {
609- execution = await command ( "node" , [ codeboxCliPath , "agent-task-run" , "--input-file" , executionInputPath , "--result-file" , nativeResultPath ] , workspace , agentEnvironment ( ) )
613+ if ( request . run_agent && ! request . dry_run ) {
614+ if ( request . runner_workspace ?. enabled ) trustedApplyArtifactRoot = await createTrustedArtifactApplyChannel ( )
615+ execution = await command ( "node" , [ codeboxCliPath , "agent-task-run" , "--input-file" , executionInputPath , "--result-file" , nativeResultPath ] , workspace , agentEnvironment ( trustedApplyArtifactRoot ? { WP_CODEBOX_TRUSTED_APPLY_ARTIFACT_ROOT : trustedApplyArtifactRoot } : { } ) )
610616 }
611617
612618// Public package bytes are embedded in the runtime recipe and consumed only by
@@ -638,16 +644,16 @@ if (execution.code === 0 && runtimeResult.success === true && request.runner_wor
638644 const publicCore = await import ( pathToFileURL ( join ( codeboxRoot , "packages/runtime-core/dist/public.js" ) ) . href )
639645 const refs = publicCore . normalizePublicArtifactRefDTOs ( runtimeResult )
640646 . filter ( ( ref ) => ref . kind === "codebox-patch" || ref . kind === "codebox-changed-files" )
641- const trustedArtifacts = await createTrustedArtifactSnapshot ( artifactsPath , refs )
642- try {
643- workspaceApply = await runnerWorkspaceCore . applyRunnerWorkspacePatch ( { artifactRoot : trustedArtifacts . root , artifactRefs : trustedArtifacts . refs , workspaceRoot : workspace , writablePaths, seedIdentity : runnerWorkspaceSeedSnapshot ?. provenance . identity } )
644- } finally {
645- await rm ( trustedArtifacts . root , { recursive : true , force : true } )
646- }
647+ const trustedArtifacts = await trustedArtifactApplyRefs ( trustedApplyArtifactRoot , refs )
648+ workspaceApply = await runnerWorkspaceCore . applyRunnerWorkspacePatch ( { artifactRoot : trustedArtifacts . root , artifactRefs : trustedArtifacts . refs , workspaceRoot : workspace , writablePaths, seedIdentity : runnerWorkspaceSeedSnapshot ?. provenance . identity } )
647649 } catch ( error ) {
648650 downstreamFailure = { stage : "apply" , message : bounded ( error instanceof Error ? error . message : String ( error ) , MAX_WORKFLOW_OUTPUT_BYTES ) , ...( error ?. evidence ? { evidence : error . evidence } : { } ) }
649651 }
650652}
653+ if ( trustedApplyArtifactRoot ) {
654+ await rm ( trustedApplyArtifactRoot , { recursive : true , force : true } )
655+ trustedApplyArtifactRoot = ""
656+ }
651657runtimeResult = sanitizeRuntimeSourceValue ( runtimeResult , runtimeSourceOutputRoots )
652658privateRuntimeSourceRootForSanitization = runtimeSourceOutputRoots
653659assertNoRuntimeSourcePaths ( runtimeResult , privateRuntimeSourceRootForSanitization )
0 commit comments