wishforge-ide/apps/ide/provisioner/run-release.mjs
eust-w 0ef99b1e6d style: add Prettier and EditorConfig, format the codebase
Adds prettier 3.9.8 (exact) with a shared config (single quotes,
120 columns, trailing commas), .prettierignore, .editorconfig and the
`format` / `format:check` scripts, then formats all TypeScript,
JavaScript, CSS, JSON and YAML sources.

Purely mechanical: no behaviour change. Typecheck passes, the vitest
suite and the 74 IDE node tests produce the same results as before
(socket-binding tests still cannot run inside this sandbox).
Previously the longest source line was ~3000 characters; reviewing the
following semantic changes now produces readable diffs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 13:32:43 +08:00

57 lines
2.8 KiB
JavaScript

import { createHash } from 'node:crypto';
import { verifyResourceGrant } from './resource-grants.mjs';
import { lifecycleCommand } from './provisioner.mjs';
/** Stops only matching compute Jobs; PVCs are retained and never called free storage. */
export async function releaseRun(config, request, execute = lifecycleCommand) {
if (
!request ||
Object.keys(request).some(key => !['tenantId', 'runId', 'generation', 'resourceGrant'].includes(key)) ||
!/^[A-Za-z0-9_-]{1,128}$/.test(request.runId ?? '') ||
!/^[A-Za-z0-9_-]{1,128}$/.test(request.generation ?? '')
)
throw new Error('RESOURCE_GRANT_INVALID');
verifyResourceGrant(config, request, { allowExpired: true, allowRetiredImage: true, cleanup: true });
const run = createHash('sha256').update(`${request.tenantId}\0${request.runId}`).digest('hex').slice(0, 24),
generation = createHash('sha256').update(request.generation).digest('hex').slice(0, 24);
const prefix = `wf-${createHash('sha256').update(`${request.tenantId}\0${request.runId}\0${request.generation}`).digest('hex').slice(0, 24)}`;
const selector = `app.kubernetes.io/managed-by=wishforge-provisioner,wishforge.io/run=${run},wishforge.io/generation=${generation},wishforge.io/component=runner`;
const assertScope = value => {
if (
value.metadata?.namespace !== config.namespace ||
value.metadata?.labels?.['app.kubernetes.io/managed-by'] !== 'wishforge-provisioner' ||
value.metadata?.labels?.['wishforge.io/component'] !== 'runner' ||
value.metadata?.labels?.['wishforge.io/run'] !== run ||
value.metadata?.labels?.['wishforge.io/generation'] !== generation
)
throw new Error('KUBERNETES_RELEASE_SCOPE_MISMATCH');
};
const jobs = JSON.parse(await execute(config, ['get', 'jobs', '-l', selector, '-o', 'json']));
if (!Array.isArray(jobs.items)) throw new Error('KUBERNETES_RELEASE_INVALID');
for (const job of jobs.items) {
assertScope(job);
if (![`${prefix}-executor`, `${prefix}-verifier`].includes(job.metadata.name))
throw new Error('KUBERNETES_RELEASE_SCOPE_MISMATCH');
await execute(config, [
'delete',
'job',
job.metadata.name,
'--ignore-not-found=true',
'--cascade=foreground',
'--wait=true',
'--timeout=30s',
]);
}
const pods = JSON.parse(await execute(config, ['get', 'pods', '-l', selector, '-o', 'json']));
if (!Array.isArray(pods.items)) throw new Error('KUBERNETES_RELEASE_INVALID');
for (const pod of pods.items) assertScope(pod);
const active = pods.items.some(pod => !['Succeeded', 'Failed'].includes(pod.status?.phase));
return {
tenantId: request.tenantId,
runId: request.runId,
generation: request.generation,
status: active ? 'releasing' : 'released',
computeReleased: !active,
persistentStorageRetained: true,
};
}