From 9aecaa054d0072cb73cc78fb35fbc61b70599406 Mon Sep 17 00:00:00 2001 From: Kentaro Wakayama Date: Sun, 27 Sep 2026 02:28:08 +0200 Subject: [PATCH] refactor(observability): make project run dispatch and run id parsing easier to follow Move the project run dispatch out of the project_run.execute span callback into executeProjectRun, read the run id from a regex capture group instead of a path segment index, and drop an error branch identical to its fallthrough. --- src/channels/control-plane-routes.ts | 13 +++--- .../request/project-run-execute.handler.ts | 43 +++++++++++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/channels/control-plane-routes.ts b/src/channels/control-plane-routes.ts index bee4b9f2e5..d675c67f81 100644 --- a/src/channels/control-plane-routes.ts +++ b/src/channels/control-plane-routes.ts @@ -9,12 +9,15 @@ * @internal */ -/** Matches the signed run operation routes (`execute`, `stream`, `resume`) of a control-plane run. */ +/** + * Matches the signed run operation routes (`execute`, `stream`, `resume`) of a + * control-plane run. Group 1 is the run id. + */ export const CONTROL_PLANE_RUN_OPERATION_PATH = - /^\/api\/control-plane\/runs\/[^/]+\/(?:execute|stream|resume)$/u; + /^\/api\/control-plane\/runs\/([^/]+)\/(?:execute|stream|resume)$/u; -/** Matches the bare run route, which only DELETE addresses. */ -export const CONTROL_PLANE_RUN_PATH = /^\/api\/control-plane\/runs\/[^/]+$/u; +/** Matches the bare run route, which only DELETE addresses. Group 1 is the run id. */ +export const CONTROL_PLANE_RUN_PATH = /^\/api\/control-plane\/runs\/([^/]+)$/u; /** * The run id a control-plane run route addresses, or `undefined` when the @@ -27,5 +30,5 @@ export function controlPlaneRunIdFromPath(method: string, pathname: string): str : normalizedMethod === "DELETE" ? CONTROL_PLANE_RUN_PATH : undefined; - return route?.test(pathname) ? pathname.split("/")[4] : undefined; + return route?.exec(pathname)?.[1]; } diff --git a/src/server/handlers/request/project-run-execute.handler.ts b/src/server/handlers/request/project-run-execute.handler.ts index 76aea22e02..51d8f4c34e 100644 --- a/src/server/handlers/request/project-run-execute.handler.ts +++ b/src/server/handlers/request/project-run-execute.handler.ts @@ -2001,6 +2001,31 @@ const defaultDeps: ProjectRunExecuteHandlerDeps = { now: () => Date.now(), }; +/** Runs the task, eval or workflow a control-plane execute request names. */ +function executeProjectRun( + request: ProjectRunExecuteRequest, + ctx: HandlerContext, + req: Request, + deps: ProjectRunExecuteHandlerDeps, +): Promise { + if (request.kind === "task") { + switch (request.target) { + case "task:knowledge-ingest": + return deps.executeKnowledgeIngest({ request, ctx, req }); + case "task:release-asset-build": + return deps.executeReleaseAssetBuild({ request, ctx, req }); + case "task:dependency-artifact-build": + return deps.executeDependencyArtifactBuild({ request, ctx, req }); + case "task:style-artifact-build": + return deps.executeStyleArtifactBuild({ request, ctx, req }); + default: + return executeTaskRun(request, ctx, deps); + } + } + if (request.kind === "eval") return executeEvalRun(request, ctx, req, deps); + return executeWorkflowRun(request, ctx, deps); +} + export class ProjectRunExecuteHandler extends BaseHandler { metadata: HandlerMetadata = { name: "ProjectRunExecuteHandler", @@ -2052,19 +2077,7 @@ export class ProjectRunExecuteHandler extends BaseHandler { async () => { const startedAt = this.deps.now(); try { - const response = request.kind === "task" && request.target === "task:knowledge-ingest" - ? await this.deps.executeKnowledgeIngest({ request, ctx, req }) - : request.kind === "task" && request.target === "task:release-asset-build" - ? await this.deps.executeReleaseAssetBuild({ request, ctx, req }) - : request.kind === "task" && request.target === "task:dependency-artifact-build" - ? await this.deps.executeDependencyArtifactBuild({ request, ctx, req }) - : request.kind === "task" && request.target === "task:style-artifact-build" - ? await this.deps.executeStyleArtifactBuild({ request, ctx, req }) - : request.kind === "task" - ? await executeTaskRun(request, ctx, this.deps) - : request.kind === "eval" - ? await executeEvalRun(request, ctx, req, this.deps) - : await executeWorkflowRun(request, ctx, this.deps); + const response = await executeProjectRun(request, ctx, req, this.deps); if (!response.success) setActiveSpanErrorStatus(new Error("Project run failed")); return this.respond(builder.json(response, 200)); } catch (error) { @@ -2095,10 +2108,6 @@ export class ProjectRunExecuteHandler extends BaseHandler { return this.respond(builder.json({ error: error.message }, error.status)); } - if (error instanceof SyntaxError || error instanceof Error) { - return this.respond(builder.json({ error: "Invalid project run execute request" }, 400)); - } - return this.respond(builder.json({ error: "Invalid project run execute request" }, 400)); } });