From bf3e05c716d4965d79154f86053f8813215b5569 Mon Sep 17 00:00:00 2001 From: Harshi-Shah-CS Date: Wed, 18 Nov 2026 15:24:57 +0530 Subject: [PATCH 1/3] fix(deployments): word only the cancel race 400 as cannot-be-cancelled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to #290 review. Treating every 400 from cancel as "no longer in progress" could misreport a gateway 400 with no error body, or a validation 400 with its own code, while the deployment is still running. The cancel-not-allowed wording now applies to: - the CANCEL_NOT_ALLOWED code, on any status - a 400 carrying the server's raw refusal message - a 400 whose error entries are all empty, which is what the server sends when the deployment finishes before the cancel lands Any other 400 keeps its own wording. Adds an integration case for the {"errors":[{}]} body, so the parse-to-exit path is covered. #claude_code# 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .talismanrc | 4 ++-- src/deployments/deployments.api.test.ts | 25 ++++++++++++++++++++- src/deployments/deployments.api.ts | 21 +++++++++++++++-- test/integration/deployments-cancel.test.ts | 1 + 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/.talismanrc b/.talismanrc index 99c7e54a..bc0b32f9 100644 --- a/.talismanrc +++ b/.talismanrc @@ -447,13 +447,13 @@ fileignoreconfig: - filename: test/integration/deployments-rollback.test.ts checksum: 08400c5435c84cd162fefc585f6c32cddfcaa8909d2369341748b421e7f12073 - filename: test/integration/deployments-cancel.test.ts - checksum: 02c012da974f1f9b8b851d7e00878fef50c1ba7e32b6acf72a309582c66aa5c2 + checksum: 95bf08ce05089d01ede8f4b41faa7ff01fff3b4e7961742a288ad2e3f5959d9d - filename: src/commands/launch/deployments/cancel.test.ts checksum: da8d09de7f03cfe67df75ebc1636b36feb6688cf85984c357fd5304852d126e5 - filename: src/commands/launch/deployments/rollback.test.ts checksum: 5331e774d07387b1691c47168afa8d454bdee60d26ac9c6ed3f4640fc85ff8b8 - filename: src/deployments/deployments.api.test.ts - checksum: b27d91317c17f1754d5853c68f4968e63dc2b3579eab26b4c772cf3cd3efbdd0 + checksum: e581dc4de15f9e669f5dff81b9e5079835e9e3e73f5476399ccd1aeb6d974091 - filename: src/deployments/deployment.prompt.test.ts checksum: 3367e53ce2f0284c14524da893e7f82ef613fcdac1e9689ac408709e7f7c153d - filename: test/integration/deployments-request-bodies.test.ts diff --git a/src/deployments/deployments.api.test.ts b/src/deployments/deployments.api.test.ts index a8d89fad..10e871dc 100644 --- a/src/deployments/deployments.api.test.ts +++ b/src/deployments/deployments.api.test.ts @@ -6,6 +6,7 @@ import { CANCEL_NOT_ALLOWED_RAW_MESSAGE, DEPLOYMENT_ERROR_MESSAGES, DeploymentUnsuccessfulError, + INVALID_DEPLOYMENT_UID_CODE, INVALID_DEPLOYMENT_UID_RAW_MESSAGE, } from './deployment.errors'; import { DeploymentsApi } from './deployments.api'; @@ -340,7 +341,7 @@ describe('DeploymentsApi', () => { ['the raw message as the code', [{ code: CANCEL_NOT_ALLOWED_RAW_MESSAGE, message: CANCEL_NOT_ALLOWED_RAW_MESSAGE }]], ['the raw message alone', [{ message: CANCEL_NOT_ALLOWED_RAW_MESSAGE }]], ['an empty error entry', [{}]], - ['another message', [{ message: 'something else' }]], + ['empty error entries only', [{}, {}]], ])('words a cancel refused with %s as the cancel-not-allowed message', async (_name, errors) => { const rejection = new LaunchApiError(400, errors, DEPLOYMENT_ERROR_MESSAGES); const client = { request: async () => Promise.reject(rejection) } as unknown as RestApiClient; @@ -369,6 +370,28 @@ describe('DeploymentsApi', () => { expect(failure.message).toBe(`Deployment "${DEPLOYMENT}" cannot be cancelled because it is no longer in progress.`); }); + it.each([ + ['another message', [{ message: 'something else' }]], + ['no error entries, as a proxy answers', []], + ['an empty entry beside another message', [{}, { message: 'something else' }]], + ['another known code', [{ code: INVALID_DEPLOYMENT_UID_CODE }]], + ['a field-only entry', [{ field: 'deployment_uid' }]], + ])('leaves a 400 cancel refusal carrying %s untouched', async (_name, errors) => { + const rejection = new LaunchApiError(400, errors, DEPLOYMENT_ERROR_MESSAGES); + const client = { request: async () => Promise.reject(rejection) } as unknown as RestApiClient; + + await expect(new DeploymentsApi(client).cancel({ ...SCOPE, deployment: DEPLOYMENT })).rejects.toBe(rejection); + }); + + it('keeps the wording of another known code on a 400 cancel refusal', async () => { + const rejection = new LaunchApiError(400, [{ code: INVALID_DEPLOYMENT_UID_CODE }], DEPLOYMENT_ERROR_MESSAGES); + const client = { request: async () => Promise.reject(rejection) } as unknown as RestApiClient; + + await expect(new DeploymentsApi(client).cancel({ ...SCOPE, deployment: DEPLOYMENT })).rejects.toThrow( + 'The deployment UID is invalid. Provide a 24-character hexadecimal deployment UID.', + ); + }); + it('leaves a non-400 refusal untouched on cancel', async () => { const wrongStatus = new LaunchApiError(409, [{ message: CANCEL_NOT_ALLOWED_RAW_MESSAGE }]); const notFound = new LaunchApiError(404, [{ code: 'launch.DEPLOYMENT.NOT_FOUND', message: 'x' }], DEPLOYMENT_ERROR_MESSAGES); diff --git a/src/deployments/deployments.api.ts b/src/deployments/deployments.api.ts index e172413c..aeef1dee 100644 --- a/src/deployments/deployments.api.ts +++ b/src/deployments/deployments.api.ts @@ -1,9 +1,10 @@ import { assertPage, hasUid, malformed, unwrap } from '../transport/envelope'; import type { RestApiClient, RestRequest } from '../transport/rest-client'; import { isRecord } from '../core/values'; -import { LaunchApiError } from '../transport/errors'; +import { LaunchApiError, type ApiErrorEntry } from '../transport/errors'; import { CANCEL_NOT_ALLOWED_CODE, + CANCEL_NOT_ALLOWED_RAW_MESSAGE, DEPLOYMENT_ERROR_MESSAGES, DEPLOYMENT_NOT_FOUND_CODE, cancelNotAllowedError, @@ -38,10 +39,26 @@ export interface RollbackDeploymentParams extends DeploymentScope { reason?: string; } +function isRawRefusal(entry: ApiErrorEntry): boolean { + return entry.code === CANCEL_NOT_ALLOWED_RAW_MESSAGE || entry.message === CANCEL_NOT_ALLOWED_RAW_MESSAGE; +} + +function isEmpty(entry: ApiErrorEntry): boolean { + return entry.field === undefined && entry.code === undefined && entry.message === undefined; +} + +// The server answers a cancel that lost the race with a deployment finishing as {"errors":[{}]}. +function isCancelRaceRefusal(error: LaunchApiError): boolean { + return ( + error.status === 400 && + (error.errors.some(isRawRefusal) || (error.errors.length > 0 && error.errors.every(isEmpty))) + ); +} + function isCancelNotAllowed(error: unknown): error is LaunchApiError { return ( error instanceof LaunchApiError && - (error.status === 400 || error.errors.some((entry) => entry.code === CANCEL_NOT_ALLOWED_CODE)) + (error.errors.some((entry) => entry.code === CANCEL_NOT_ALLOWED_CODE) || isCancelRaceRefusal(error)) ); } diff --git a/test/integration/deployments-cancel.test.ts b/test/integration/deployments-cancel.test.ts index 7dc79a96..2d4c0254 100644 --- a/test/integration/deployments-cancel.test.ts +++ b/test/integration/deployments-cancel.test.ts @@ -264,6 +264,7 @@ describe('integration: launch:deployments:cancel on the wire', () => { ['the CANCEL_NOT_ALLOWED code', { code: 'launch.DEPLOYMENT.CANCEL_NOT_ALLOWED', message: 'x' }], ['the raw server message as the code', { code: RAW_REFUSAL, message: RAW_REFUSAL }], ['the raw server message alone', { message: RAW_REFUSAL }], + ['an empty error entry', {}], ])('exits 1 with readable wording, posting once, when the cancel is refused with %s', async (_name, entry) => { lookup(); const cancel = cancelScope() From 5287886ba264436771f591134d94973de47fbc67 Mon Sep 17 00:00:00 2001 From: Harshi-Shah-CS Date: Wed, 18 Nov 2026 16:21:57 +0530 Subject: [PATCH 2/3] fix(deployments): judge the cancel race on the errors the server sent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Address #292 review. The race check matched any 400 whose parsed entries were all empty, but the parser reduces every entry it can't read (a string, null, a non-string code, unknown keys, a field with a plain string value) to {}. Those 400s still said the deployment was no longer in progress. LaunchApiError now keeps the errors array as the server sent it (rawErrors; hand-built errors default to their own entries), and the cancel check matches only literal {} entries there. Also adds wire-level cases for 400s that must keep their own wording, drops a duplicate invalid-UID row, renames isEmpty to isEmptyEntry and documents both race shapes. #claude_code# 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .talismanrc | 6 +++--- src/deployments/deployments.api.test.ts | 1 - src/deployments/deployments.api.ts | 9 +++++---- src/transport/errors.test.ts | 14 ++++++++++++++ src/transport/errors.ts | 16 +++++++++++++--- test/integration/deployments-cancel.test.ts | 21 +++++++++++++++++++++ 6 files changed, 56 insertions(+), 11 deletions(-) diff --git a/.talismanrc b/.talismanrc index bc0b32f9..17e6401d 100644 --- a/.talismanrc +++ b/.talismanrc @@ -297,7 +297,7 @@ fileignoreconfig: - filename: src/functions/function.errors.test.ts checksum: ac0a55e5bee974fefa9fb95541adba63e9a2c2c6e6c47b87c8ca1022542e75f0 - filename: src/transport/errors.test.ts - checksum: f38547e2a4639ae150b53f955963ce00c1922a4223c89e489ba5b7ca75a691d0 + checksum: 72bd1e9af33dbb9dd4e86803534ebadbda37af5f0aeeefb4f99734cf027c7f39 - filename: src/transport/auth-strategy.ts checksum: 6fe87def1b508cb67203d4445d9bd889570e512ed4194dfc4fdd3182bc8b87b2 - filename: src/functions/function.logger.test.ts @@ -447,13 +447,13 @@ fileignoreconfig: - filename: test/integration/deployments-rollback.test.ts checksum: 08400c5435c84cd162fefc585f6c32cddfcaa8909d2369341748b421e7f12073 - filename: test/integration/deployments-cancel.test.ts - checksum: 95bf08ce05089d01ede8f4b41faa7ff01fff3b4e7961742a288ad2e3f5959d9d + checksum: 8972ed5f23e7dee8acf677ae9a26d2d28ef8144d03bc7623c5fd29f096a3aed0 - filename: src/commands/launch/deployments/cancel.test.ts checksum: da8d09de7f03cfe67df75ebc1636b36feb6688cf85984c357fd5304852d126e5 - filename: src/commands/launch/deployments/rollback.test.ts checksum: 5331e774d07387b1691c47168afa8d454bdee60d26ac9c6ed3f4640fc85ff8b8 - filename: src/deployments/deployments.api.test.ts - checksum: e581dc4de15f9e669f5dff81b9e5079835e9e3e73f5476399ccd1aeb6d974091 + checksum: 7a9b375ee156a6c61de30570814ce8f8109c90ca3863e760167ba018dd60b596 - filename: src/deployments/deployment.prompt.test.ts checksum: 3367e53ce2f0284c14524da893e7f82ef613fcdac1e9689ac408709e7f7c153d - filename: test/integration/deployments-request-bodies.test.ts diff --git a/src/deployments/deployments.api.test.ts b/src/deployments/deployments.api.test.ts index 10e871dc..6af754b2 100644 --- a/src/deployments/deployments.api.test.ts +++ b/src/deployments/deployments.api.test.ts @@ -374,7 +374,6 @@ describe('DeploymentsApi', () => { ['another message', [{ message: 'something else' }]], ['no error entries, as a proxy answers', []], ['an empty entry beside another message', [{}, { message: 'something else' }]], - ['another known code', [{ code: INVALID_DEPLOYMENT_UID_CODE }]], ['a field-only entry', [{ field: 'deployment_uid' }]], ])('leaves a 400 cancel refusal carrying %s untouched', async (_name, errors) => { const rejection = new LaunchApiError(400, errors, DEPLOYMENT_ERROR_MESSAGES); diff --git a/src/deployments/deployments.api.ts b/src/deployments/deployments.api.ts index aeef1dee..dd41fa30 100644 --- a/src/deployments/deployments.api.ts +++ b/src/deployments/deployments.api.ts @@ -43,15 +43,16 @@ function isRawRefusal(entry: ApiErrorEntry): boolean { return entry.code === CANCEL_NOT_ALLOWED_RAW_MESSAGE || entry.message === CANCEL_NOT_ALLOWED_RAW_MESSAGE; } -function isEmpty(entry: ApiErrorEntry): boolean { - return entry.field === undefined && entry.code === undefined && entry.message === undefined; +function isEmptyEntry(sent: unknown): boolean { + return isRecord(sent) && Object.keys(sent).length === 0; } -// The server answers a cancel that lost the race with a deployment finishing as {"errors":[{}]}. +// The race answers {"errors":[{}]} (or the raw refusal message). Judge {} on what was sent: +// entries the parser can't read also parse to {}. function isCancelRaceRefusal(error: LaunchApiError): boolean { return ( error.status === 400 && - (error.errors.some(isRawRefusal) || (error.errors.length > 0 && error.errors.every(isEmpty))) + (error.errors.some(isRawRefusal) || (error.rawErrors.length > 0 && error.rawErrors.every(isEmptyEntry))) ); } diff --git a/src/transport/errors.test.ts b/src/transport/errors.test.ts index c5d1daa4..5ab9d21a 100644 --- a/src/transport/errors.test.ts +++ b/src/transport/errors.test.ts @@ -223,6 +223,20 @@ describe('parseErrorEnvelope', () => { }, ); + it('keeps the errors array exactly as the server sent it, beside the readable entries', () => { + const sent = [{}, 'deployment_uid is required', { error_message: 'x' }]; + + const error = parseErrorEnvelope(400, { errors: sent }, MESSAGES); + + expect(error.errors).toEqual([{}, {}, {}]); + expect(error.rawErrors).toEqual(sent); + expect(parseErrorEnvelope(400, { status: 400 }, MESSAGES).rawErrors).toEqual([]); + }); + + it('treats the entries of a hand-built error as what was sent', () => { + expect(new LaunchApiError(400, [{ code: 'x' }]).rawErrors).toEqual([{ code: 'x' }]); + }); + it('names the error LaunchApiError so it is recognisable once serialised', () => { const error = parseErrorEnvelope(404, { errors: [{ code: 'launch.RESOURCE.NOT_FOUND', message: 'x' }] }, MESSAGES); diff --git a/src/transport/errors.ts b/src/transport/errors.ts index 982e83d0..c2c0ad14 100644 --- a/src/transport/errors.ts +++ b/src/transport/errors.ts @@ -44,8 +44,15 @@ export class LaunchApiError extends LaunchError { readonly status: number; readonly code: string; readonly errors: ApiErrorEntry[]; - - constructor(status: number, errors: ApiErrorEntry[], messages: ErrorMessages = {}) { + /** The errors array as the server sent it, before entries it can't read are reduced to {}. */ + readonly rawErrors: readonly unknown[]; + + constructor( + status: number, + errors: ApiErrorEntry[], + messages: ErrorMessages = {}, + rawErrors: readonly unknown[] = errors, + ) { const [primary] = errors; super( messageForCode(messages, primary?.code) ?? @@ -57,6 +64,7 @@ export class LaunchApiError extends LaunchError { this.status = status; this.code = primary?.code ?? 'launch.UNKNOWN'; this.errors = errors; + this.rawErrors = rawErrors; } } @@ -100,7 +108,9 @@ function entryFrom(value: unknown): ApiErrorEntry { export function parseErrorEnvelope(status: number, body: unknown, messages: ErrorMessages = {}): LaunchApiError { const errors = typeof body === 'object' && body !== null && 'errors' in body ? body.errors : undefined; - return new LaunchApiError(status, Array.isArray(errors) ? errors.map(entryFrom) : [], messages); + const sent: unknown[] = Array.isArray(errors) ? errors : []; + + return new LaunchApiError(status, sent.map(entryFrom), messages, sent); } export const PROXY_ERROR_CODES: readonly string[] = ['ECONNREFUSED', 'ETIMEDOUT', 'ENOTFOUND', 'ERR_BAD_RESPONSE']; diff --git a/test/integration/deployments-cancel.test.ts b/test/integration/deployments-cancel.test.ts index 2d4c0254..482c2ba3 100644 --- a/test/integration/deployments-cancel.test.ts +++ b/test/integration/deployments-cancel.test.ts @@ -38,6 +38,7 @@ const POST_PENDING = `POST ${LAUNCH_HUB_URL}:443${CANCEL_PATH}`; const BODYLESS = { badheaders: ['content-type'] }; const RAW_REFUSAL = 'This deployment has progressed beyond the point where it can be cancelled.'; const CANCEL_WORDING = `Deployment "${DEPLOYMENT_UID}" cannot be cancelled because it is no longer in progress.`; +const STATUS_400_WORDING = 'Launch API request failed with status 400.'; let config: Interfaces.Config; let onWire: string[]; @@ -280,6 +281,26 @@ describe('integration: launch:deployments:cancel on the wire', () => { expect(onWire).toEqual([]); }); + it.each([ + ['no errors array', { status: 400 }, STATUS_400_WORDING], + ['another message', { errors: [{ message: 'something else' }] }, 'something else'], + ['a string entry', { errors: ['deployment_uid is required'] }, STATUS_400_WORDING], + ['a null entry', { errors: [null] }, STATUS_400_WORDING], + ['an array entry', { errors: [[]] }, STATUS_400_WORDING], + ['a numeric code', { errors: [{ code: 400 }] }, STATUS_400_WORDING], + ['an entry under other keys', { errors: [{ error_message: 'x' }] }, STATUS_400_WORDING], + ['a field with a string value', { errors: [{ deployment_uid: 'must be a string' }] }, STATUS_400_WORDING], + ])('keeps its own wording when the cancel is refused with a 400 carrying %s', async (_name, body, wording) => { + lookup(); + const cancel = cancelScope().post(CANCEL_PATH).once().reply(400, body); + + const { error } = await runCommand(args('--deployment', DEPLOYMENT_UID, '--yes'), config); + + expect(error?.oclif?.exit).toBe(1); + expect(error?.message).toBe(wording); + expect(cancel.isDone()).toBe(true); + }); + it.each([ ['a finished deployment', getFixture], ['a deployment the service marks as not cancellable', { deployment: { ...IN_FLIGHT_FIXTURE.deployment, isCancellable: false } }], From cf68dcb3b047029fc36414d0736ad5e10aba76f0 Mon Sep 17 00:00:00 2001 From: Harshi-Shah-CS Date: Wed, 18 Nov 2026 16:29:23 +0530 Subject: [PATCH 3/3] refactor(deployments): drop the comments added for the cancel race MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit AGENTS.md: "Do not create code comments for any changes." The test names already document the race body. The rawErrors JSDoc also overstated it: for hand-built errors it holds the hand-built entries, not what the server sent. #claude_code# 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/deployments/deployments.api.ts | 2 -- src/transport/errors.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/src/deployments/deployments.api.ts b/src/deployments/deployments.api.ts index dd41fa30..6592d907 100644 --- a/src/deployments/deployments.api.ts +++ b/src/deployments/deployments.api.ts @@ -47,8 +47,6 @@ function isEmptyEntry(sent: unknown): boolean { return isRecord(sent) && Object.keys(sent).length === 0; } -// The race answers {"errors":[{}]} (or the raw refusal message). Judge {} on what was sent: -// entries the parser can't read also parse to {}. function isCancelRaceRefusal(error: LaunchApiError): boolean { return ( error.status === 400 && diff --git a/src/transport/errors.ts b/src/transport/errors.ts index c2c0ad14..3fe48834 100644 --- a/src/transport/errors.ts +++ b/src/transport/errors.ts @@ -44,7 +44,6 @@ export class LaunchApiError extends LaunchError { readonly status: number; readonly code: string; readonly errors: ApiErrorEntry[]; - /** The errors array as the server sent it, before entries it can't read are reduced to {}. */ readonly rawErrors: readonly unknown[]; constructor(