From c87a3ad51dd67ea349e9ffbc8c601590aa3c639d Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 24 Nov 2026 06:14:06 +0800 Subject: [PATCH] fix(platform): show the real reason when a queued deploy is refused When the deploy handler refused an admitted deploy at run time (for example a Zero Trust flip that won the race, or a canceled build), the stream always said "Admitted deployment is no longer authorized or valid". Read the handler's small JSON body and pass on its message, keeping the generic text as a fallback. Co-Authored-By: Claude Opus 5.5 --- platform/packages/api/src/routes/deploy.ts | 65 ++++++++++++++++--- .../integration/deployment-execution.test.ts | 32 ++++++++- 2 files changed, 88 insertions(+), 9 deletions(-) diff --git a/platform/packages/api/src/routes/deploy.ts b/platform/packages/api/src/routes/deploy.ts index f7b4bea1c..c5b88794a 100644 --- a/platform/packages/api/src/routes/deploy.ts +++ b/platform/packages/api/src/routes/deploy.ts @@ -483,6 +483,60 @@ async function presignUnderProjectLock( // Deploy to a project app.post('/:id/deploy', handleManifestOnlyDeploy); +const ADMISSION_REJECTED_MESSAGE = 'Admitted deployment is no longer authorized or valid'; +const ADMISSION_REASON_MAX_BYTES = 8192; +const ADMISSION_REASON_MAX_CHARS = 4096; + +/** + * Read why the deploy handler refused an admitted input, for example a Zero + * Trust change that landed while the deploy waited. Its JSON body carries the + * same `message` (or bare `error`) a direct deploy would show, so the user + * learns whether a retry fixes it. Only a small JSON body is read. + */ +async function rejectedAdmissionReason(response: Response): Promise { + const type = response.headers.get('content-type') ?? ''; + const reader = response.body?.getReader(); + if (!reader) { + return null; + } + const bytes = new Uint8Array(ADMISSION_REASON_MAX_BYTES); + let size = 0; + try { + if (type.split(';')[0]!.trim().toLowerCase() !== 'application/json') { + return null; + } + for (;;) { + const chunk = await reader.read(); + if (chunk.done) { + break; + } + if (size + chunk.value.byteLength > bytes.byteLength) { + return null; + } + bytes.set(chunk.value, size); + size += chunk.value.byteLength; + } + } finally { + void reader.cancel().catch(() => {}); + } + let body: unknown; + try { + body = JSON.parse(new TextDecoder().decode(bytes.subarray(0, size))); + } catch { + return null; + } + if (!body || typeof body !== 'object') { + return null; + } + const { message, error } = body as { message?: unknown; error?: unknown }; + for (const candidate of [message, error]) { + if (typeof candidate === 'string' && candidate.trim()) { + return candidate.trim().slice(0, ADMISSION_REASON_MAX_CHARS); + } + } + return null; +} + export async function executeQueuedDeployment( env: ApiEnv['Bindings'], db: Db, @@ -583,6 +637,7 @@ export async function executeQueuedDeployment( env, ); if (response.status !== 204) { + const reason = (await rejectedAdmissionReason(response)) ?? ADMISSION_REJECTED_MESSAGE; // A rejected admitted input never entered the pipeline. No provider write // occurred, so its placeholder can be finalized without replaying it. await db.transaction(async (tx) => { @@ -593,17 +648,11 @@ export async function executeQueuedDeployment( if (!candidate || candidate.manifest !== null || candidate.status !== 'pending') { throw new Error('Admitted deployment needs reconciliation'); } - await updateDeploymentStatus( - tx, - operation.id, - 'failed', - 'Admitted deployment is no longer authorized or valid', - { force: true }, - ); + await updateDeploymentStatus(tx, operation.id, 'failed', reason, { force: true }); }); await appendDeploymentEvent(db, operation.id, operation.owner_id!, { event: 'error', - message: 'Admitted deployment is no longer authorized or valid', + message: reason, step: 'authorization', }); } diff --git a/platform/packages/api/test/integration/deployment-execution.test.ts b/platform/packages/api/test/integration/deployment-execution.test.ts index bdcc6daa4..30d65148f 100644 --- a/platform/packages/api/test/integration/deployment-execution.test.ts +++ b/platform/packages/api/test/integration/deployment-execution.test.ts @@ -42,6 +42,8 @@ import { } from '../../src/schema.ts'; import type { DeployEvent } from '../../src/types.ts'; import { WORKER_UPLOAD_OUT_OF_TIME } from '../../src/worker-upload.ts'; +import { ZeroTrustUnavailableError } from '../../src/zero-trust/errors.ts'; +import * as zeroTrustRouting from '../../src/zero-trust/routing.ts'; import { createTestBuild, clearTables, @@ -1182,9 +1184,37 @@ describe('durable execution claim and revocation', () => { expect(await env.ROUTING.get(`slug:${project.slug}`)).toBeNull(); const operation = await getDeploymentOperation(env.db, id); expect(operation).toMatchObject({ status: 'terminal' }); + const reason = + 'This build is no longer active. Start a new build to deploy the current version.'; + expect(await env.db.select().from(deployments)).toMatchObject([{ error_message: reason }]); expect(operation?.events.at(-1)?.event).toMatchObject({ event: 'error', - step: expect.any(String), + message: reason, + step: 'authorization', + }); + }); + + it('reports the Zero Trust reason when protection changes while a deploy waits', async () => { + const id = await admit(); + // A Zero Trust flip that lands after admission makes the deploy handler + // answer 409 when the queued work runs. + vi.spyOn(zeroTrustRouting, 'resolveProjectZeroTrustRouting').mockRejectedValue( + new ZeroTrustUnavailableError('The project public exception is still reconciling.'), + ); + const message = await run(id); + expect(message.ack).toHaveBeenCalledOnce(); + expect(message.retry).not.toHaveBeenCalled(); + const [deployment] = await env.db.select().from(deployments); + expect(deployment).toMatchObject({ id, status: 'failed' }); + expect(deployment!.error_message).toContain( + 'Zero Trust protection for this project is not ready: The project public exception is still reconciling.', + ); + expect(deployment!.error_message).toContain('then retry.'); + const operation = await getDeploymentOperation(env.db, id); + expect(operation?.events.at(-1)?.event).toMatchObject({ + event: 'error', + message: deployment!.error_message, + step: 'authorization', }); }); });