1- import { type ArtifactBundle , type ArtifactManifestFile , type ExecutionResult , type Runtime , type WorkspaceRecipe , type WorkspaceRecipeProbe } from "@automattic/wp-codebox-core"
1+ import { type ArtifactBundle , type ArtifactManifestFile , type ExecutionResult , type Runtime , type WorkspaceRecipe , type WorkspaceRecipeDistributionStartupProbe , type WorkspaceRecipeProbe } from "@automattic/wp-codebox-core"
22import { stripUndefined } from "@automattic/wp-codebox-core/internals"
33import { recipeExecutionSpec , sandboxWorkspaceContract } from "../agent-sandbox.js"
44import { executeAgentFanoutFromArgs } from "../agent-fanout.js"
55import { recipeWorkflowSteps , type RecipeWorkflowPhase } from "../recipe-validation.js"
66import { artifactManifestFilesByPath } from "./recipe-run-benchmark-artifacts.js"
77import { serializeRecipeRunError } from "./recipe-run-output.js"
8- import type { RecipeAdvisoryFailure , RecipeBrowserEvidence , RecipeBrowserEvidenceFileRef , RecipeExecutionResult , RecipeRunOptions , RecipeRunProbe } from "./recipe-run-types.js"
8+ import type { RecipeAdvisoryFailure , RecipeBrowserEvidence , RecipeBrowserEvidenceFileRef , RecipeExecutionResult , RecipeRunDistributionStartupProbe , RecipeRunOptions , RecipeRunProbe } from "./recipe-run-types.js"
99
1010export function withRecipeExecutionPhase ( execution : ExecutionResult , recipePhase : RecipeWorkflowPhase , recipeStepIndex : number , recipeCommand ?: string ) : RecipeExecutionResult {
1111 return {
@@ -177,6 +177,46 @@ export async function runRecipeProbes(recipe: WorkspaceRecipe, recipeDirectory:
177177 return results
178178}
179179
180+ export async function runDistributionStartupProbes ( recipe : WorkspaceRecipe , runtime : Runtime , executions : RecipeExecutionResult [ ] ) : Promise < RecipeRunDistributionStartupProbe [ ] > {
181+ const results : RecipeRunDistributionStartupProbe [ ] = [ ]
182+ for ( const [ index , probe ] of ( recipe . distribution ?. startupProbes ?? [ ] ) . entries ( ) ) {
183+ if ( probe . type === "http" || probe . type === "browser" ) {
184+ results . push ( stripUndefined ( {
185+ schema : "wp-codebox/distribution-startup-probe-result/v1" as const ,
186+ index,
187+ name : probe . name ,
188+ type : probe . type ,
189+ status : "skipped" as const ,
190+ reason : "Distribution startup probe type is planned but not executable by this runtime primitive." ,
191+ metadata : probe . metadata ,
192+ } ) )
193+ continue
194+ }
195+
196+ const execution = await executeDistributionStartupProbe ( runtime , probe , index )
197+ executions . push ( execution )
198+ results . push ( stripUndefined ( {
199+ schema : "wp-codebox/distribution-startup-probe-result/v1" as const ,
200+ index,
201+ name : probe . name ,
202+ type : probe . type ,
203+ status : execution . exitCode === 0 ? "passed" as const : "failed" as const ,
204+ command : execution . command ,
205+ args : execution . args ,
206+ exitCode : execution . exitCode ,
207+ stdout : execution . stdout ,
208+ stderr : execution . stderr ,
209+ metadata : probe . metadata ,
210+ } ) )
211+ }
212+ return results
213+ }
214+
215+ export function distributionStartupProbeFailure ( probes : RecipeRunDistributionStartupProbe [ ] ) : Error | undefined {
216+ const failed = probes . find ( ( probe ) => probe . status === "failed" )
217+ return failed ? new Error ( `Distribution startup probe "${ failed . name } " failed with exit code ${ failed . exitCode ?? "unknown" } .` ) : undefined
218+ }
219+
180220async function executeRecipeProbe ( runtime : Runtime , probe : WorkspaceRecipeProbe , recipeDirectory : string , index : number ) : Promise < RecipeExecutionResult > {
181221 try {
182222 const execution = await runtime . execute ( await recipeExecutionSpec ( probe . step , recipeDirectory ) )
@@ -187,6 +227,19 @@ async function executeRecipeProbe(runtime: Runtime, probe: WorkspaceRecipeProbe,
187227 }
188228}
189229
230+ async function executeDistributionStartupProbe ( runtime : Runtime , probe : WorkspaceRecipeDistributionStartupProbe , index : number ) : Promise < RecipeExecutionResult > {
231+ const spec = probe . type === "wp-cli"
232+ ? { command : "wordpress.wp-cli" , args : [ `command=${ probe . command ?? "" } ` ] }
233+ : { command : "wordpress.run-php" , args : [ `code=${ probe . code ?? "" } ` ] }
234+ try {
235+ const execution = await runtime . execute ( spec )
236+ return withRecipeExecutionPhase ( execution , "setup" , index , `distribution.startupProbe:${ probe . name } ` )
237+ } catch ( error ) {
238+ const message = error instanceof Error ? error . message : String ( error )
239+ throw new Error ( `Distribution startup probe "${ probe . name } " failed before producing a result: ${ message } ` , { cause : error } )
240+ }
241+ }
242+
190243function parseProbeJson ( stdout : string ) : unknown | undefined {
191244 const trimmed = stdout . trim ( )
192245 if ( ! trimmed ) {
0 commit comments