|
| 1 | +import { AppError, normalizeError, type NormalizedError } from '@agent-device/kernel/errors'; |
| 2 | +import type { RuntimeOwnerRef } from './platform-runtime.ts'; |
| 3 | + |
| 4 | +export type PlatformRuntimeShutdownFailure = Readonly<{ |
| 5 | + owner: RuntimeOwnerRef; |
| 6 | + error: NormalizedError; |
| 7 | +}>; |
| 8 | + |
| 9 | +export type PlatformRuntimeShutdownErrorDetails = Readonly<{ |
| 10 | + reason: 'platform-owner-shutdown-failed'; |
| 11 | + failures: readonly PlatformRuntimeShutdownFailure[]; |
| 12 | +}>; |
| 13 | + |
| 14 | +type OwnerShutdownFailure = Readonly<{ owner: RuntimeOwnerRef; error: unknown }>; |
| 15 | + |
| 16 | +/** Preserves every owner failure without granting authority to retry cleanup or release a claim. */ |
| 17 | +export function createPlatformRuntimeShutdownError( |
| 18 | + failures: readonly [OwnerShutdownFailure, ...OwnerShutdownFailure[]], |
| 19 | +): AppError { |
| 20 | + const details: PlatformRuntimeShutdownErrorDetails = { |
| 21 | + reason: 'platform-owner-shutdown-failed', |
| 22 | + failures: failures.map(({ owner, error }) => ({ owner, error: normalizeError(error) })), |
| 23 | + }; |
| 24 | + const message = `Platform runtime owner shutdown failed: ${details.failures |
| 25 | + .map( |
| 26 | + ({ owner, error }) => |
| 27 | + `${owner.kind === 'local-family' ? owner.family : `${owner.provider} (${owner.instance})`}: ${error.message}`, |
| 28 | + ) |
| 29 | + .join('; ')}`; |
| 30 | + return new AppError( |
| 31 | + 'COMMAND_FAILED', |
| 32 | + message, |
| 33 | + { |
| 34 | + ...details, |
| 35 | + hint: 'Cleanup is unconfirmed. Inspect the owner failures and verify claim ownership before attempting recovery.', |
| 36 | + retriable: false, |
| 37 | + }, |
| 38 | + failures.length === 1 |
| 39 | + ? failures[0].error |
| 40 | + : new AggregateError( |
| 41 | + failures.map(({ error }) => error), |
| 42 | + 'Original platform runtime owner shutdown failures', |
| 43 | + ), |
| 44 | + ); |
| 45 | +} |
0 commit comments