diff --git a/docs/deployments.md b/docs/deployments.md index d44b564..04ab458 100644 --- a/docs/deployments.md +++ b/docs/deployments.md @@ -1,45 +1,53 @@ # Deployments and releases -`createDeployments()` reads deployment runs and resource releases for the host panes and -model tools. Every action is a read (`GET`) bound to the shared session's account. Billing, -cost and credential fields never reach the result. A run or release the reader may not see -answers with `hidden: true` and a sentence that does not say whether it exists. +`createDeployments()` reads deployments for the host panes and model tools. Every action is +a read (`GET`) bound to the shared session's account. Billing, cost and credential fields +never reach the result. A deployment the reader may not see answers with `hidden: true` and +a sentence that does not say whether it exists. | Action | Reads | Use | | --- | --- | --- | -| `deployment-history` | One page of the environment's workflow runs and the deployment runs no workflow owns, optionally for one branch | The environment-wide list | -| `deployment-run` | One deployment or workflow run with its stages | The run detail | -| `deployment-logs` | One page of a run's logs, by cursor | Logs | -| `deployment-releases` | One page of the environment's releases (APIs, agents, static sites), then their runs of the last `days` (default 30) in one more request. Releases without a successful run in that window get their newest successful run from one request with no window (up to ten it leaves out are read one by one), and releases with no runs at all get their repository and branch names from one branches request | The Releases list: the last ten run states, the last run, the active run and stats per release, plus `deployed` (the release has an active revision), `lastDeploy` (its newest successful run, whatever the window) and its repository and branch | -| `deployment-release` | One release, its revisions and its runs of the last `days` | A release's history: the serving and wanted revisions, the runs and their stats | -| `deployment-active` | Only runs still `queued` or `running` (at most 20), optionally for one branch | The cheap check while something is deploying | -| `deployment-wait` | One run, every ten seconds, until it ends or `timeoutSeconds` (at most 120) runs out | Waiting for a deployment the person or the model started | - -## Stats - -The backend has no statistics endpoint, so `releaseStats()` computes them from the runs -read: - -- **Success rate:** succeeded runs out of those that succeeded, failed or were blocked. - Superseded, cancelled and skipped runs don't count. -- **Median and 90th-percentile time:** taken over runs that succeeded. -- **Slowest stage:** the stage with the longest median time across runs that succeeded. - The time a run waits before its first stage, usually for its workflow's image build, is - its own stage, "Waiting to start". -- **Most common failure:** the error code of failed and blocked runs. A blocked run - without one is named by its outcome, such as `workflow_prerequisite_failed`. +| `deployment-release-history` | One page of the environment's resource release deployments of the last `days` (default 1), newest first, optionally for one branch, release, kind or search text | Releases: the deployment history | +| `deployment-active` | Only resource release deployments still `queued` or `running` (at most 100), optionally for one branch | The cheap check while something is deploying | +| `deployment-run` | One deployment or workflow run with its stages | A deployment's detail | +| `deployment-logs` | One page of a deployment's logs, by cursor | Logs | +| `deployment-wait` | One deployment, every ten seconds, until it ends or `timeoutSeconds` (at most 120) runs out | Waiting for a deployment the person or the model started | +| `deployment-history` | One page of every entry in the environment: workflow runs and the deployments no workflow owns | The Code section's deployments | + +## Resource release deployment history + +This is Command Center's Deployment History narrowed to resource releases: one row per +deployment, newest first, in one request. The backend does the narrowing with +`target_type__in` (`RELEASE_TARGETS`: `resource_release`, `harness_agent`, `static_site`), +so workflow runs, Jobs and image builds are never counted, paged or returned. + +It is a history and not a list of releases: a release that was not deployed in the window +has no row. Each row carries what Command Center's columns show: + +- **Revision:** `commit` and `revisionUid`. +- **Branch:** `repositoryName` and `branchName`. +- **Date:** `startedAt` (when it was created, until it starts) and `finishedAt`. +- **Type:** `kindLabel`, the backend's label. +- **State:** `state`, with `outcome`, `errorCode` and `error`. +- **Progress:** `steps` in order. A stage left running in a deployment that ended reads + `stopped`. +- **Source:** `source`. +- **Duration:** `durationMs`, absent when the deployment ended without a finish time. + +Names repeat across repositories (several releases are named `app`), so show the repository +with `releaseName`. An agent's release also carries `agentName`. ## Polling The backend offers no push channel, so hosts poll. These rules keep that small: -- **Nothing re-reads while no run is active.** Opening a list or a release reads it once. -- **Finished runs never change.** `ACTIVE_RUN_STATES` (`queued`, `running`) are the only - states that do, and the backend allows one active run per target. -- **While runs are active, re-read with `deployment-active`.** It returns only the active - runs. When its count reaches zero, read the list once more and stop. -- **Watching one run re-reads only that run,** at `WAIT_INTERVAL_MS`. Stop when it ends, - and pause after an hour. +- **Nothing re-reads while no deployment is active.** Opening the history reads it once. +- **Finished deployments never change.** `ACTIVE_RUN_STATES` (`queued`, `running`) are the + only states that do, and the backend allows one active deployment per target. +- **While a shown deployment is active, re-read with `deployment-active`.** It returns only + the active ones. When a shown one leaves it, read the history once more. +- **Watching one deployment re-reads only that deployment,** at `WAIT_INTERVAL_MS`. Stop + when it ends, and pause after an hour. - **Poll once per account and environment, not once per chat.** The [shared desktop transport](shared-desktop-transport.md) merges identical reads that are in flight at the same moment. A host with several chats still lets one of them run the diff --git a/package-lock.json b/package-lock.json index 2b97cb5..39d4702 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@dev-mainsequence/devtools", - "version": "0.3.21", + "version": "0.3.22", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@dev-mainsequence/devtools", - "version": "0.3.21", + "version": "0.3.22", "license": "MIT", "bin": { "ms-devtools": "dist/runner.js" diff --git a/package.json b/package.json index 63c7ca7..a21428e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@dev-mainsequence/devtools", - "version": "0.3.21", + "version": "0.3.22", "description": "Shared MainSequence developer actions, project operations, MCP client and command runner", "license": "MIT", "engines": { diff --git a/src/index.ts b/src/index.ts index a09b322..85bfa4b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -57,6 +57,6 @@ export { parseArguments, tokenizeCommand, HELP } from './shared/arguments'; export { createAgentAssets, stillAgentPng } from './shared/agentAssets'; export type { AgentAsset } from './shared/agentAssets'; -export { createDeployments, DEPLOYMENT_ACTIONS, HIDDEN_DEPLOYMENT, HIDDEN_RELEASE, RELEASE_KINDS, ACTIVE_RUN_STATES, WAIT_INTERVAL_MS, WAIT_LIMIT_SECONDS, WAITING_STAGE, releaseStats } from './shared/deployments'; +export { createDeployments, DEPLOYMENT_ACTIONS, HIDDEN_DEPLOYMENT, RELEASE_KINDS, RELEASE_TARGETS, RELEASE_HISTORY_DAYS, ACTIVE_RUN_STATES, WAIT_INTERVAL_MS, WAIT_LIMIT_SECONDS } from './shared/deployments'; export type { DeploymentRequest } from './shared/deployments'; export { writeWorkspacePreferences, readDesktopPreferences } from './shared/workspaceSettings'; diff --git a/src/shared/deployments.ts b/src/shared/deployments.ts index 138d49b..203ebb1 100644 --- a/src/shared/deployments.ts +++ b/src/shared/deployments.ts @@ -8,28 +8,27 @@ export type DeploymentRequest = { action: 'deployment-history'; backend: string; expectedAccount: string; environmentUid: string; codeRepositoryBranchUid?: string; limit?: number; offset?: number } | { action: 'deployment-run'; backend: string; expectedAccount: string; runUid: string; kind?: 'deployment_run' | 'workflow_run' } | { action: 'deployment-logs'; backend: string; expectedAccount: string; runUid: string; stepUid?: string; cursor?: string; limit?: number } - | { action: 'deployment-releases'; backend: string; expectedAccount: string; environmentUid: string; codeRepositoryBranchUid?: string; releaseKind?: string; search?: string; days?: number; limit?: number; offset?: number } - | { action: 'deployment-release'; backend: string; expectedAccount: string; environmentUid: string; releaseUid: string; days?: number; limit?: number } + | { action: 'deployment-release-history'; backend: string; expectedAccount: string; environmentUid: string; codeRepositoryBranchUid?: string; releaseUid?: string; releaseKind?: string; search?: string; days?: number; limit?: number; offset?: number } | { action: 'deployment-active'; backend: string; expectedAccount: string; environmentUid: string; codeRepositoryBranchUid?: string } | { action: 'deployment-wait'; backend: string; expectedAccount: string; runUid: string; timeoutSeconds?: number }; -export const DEPLOYMENT_ACTIONS = new Set(['deployment-history', 'deployment-run', 'deployment-logs', 'deployment-releases', 'deployment-release', 'deployment-active', 'deployment-wait']); -/** Release kinds as the backend names them, with the words the pane shows. */ -export const RELEASE_KINDS: Record = { fastapi: 'API', harness_agent: 'Agent', agent: 'Agent', static_site: 'Static site' }; +export const DEPLOYMENT_ACTIONS = new Set(['deployment-history', 'deployment-run', 'deployment-logs', 'deployment-release-history', 'deployment-active', 'deployment-wait']); +/** Release kinds as the backend names them, with the words to show when it sends no label. */ +export const RELEASE_KINDS: Record = { fastapi: 'API', harness_agent: 'Agent', static_site: 'Static site' }; /** A run in any other state has ended and never changes again. */ export const ACTIVE_RUN_STATES = new Set(['queued', 'running']); /** How often a wait re-reads its run, and the longest one call waits. */ export const WAIT_INTERVAL_MS = 10_000; export const WAIT_LIMIT_SECONDS = 120; -/** The stage name release stats give the time a run waits before its first stage starts. */ -export const WAITING_STAGE = 'Waiting to start'; /** Command Center's wording for a run that answers 404: it never says whether the record exists. */ export const HIDDEN_DEPLOYMENT = "You can't see this deployment. A deployment is shown to the people who can see its release or Job."; -export const HIDDEN_RELEASE = "You can't see this release. A release is shown to the people it is shared with."; const UUID = /^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/i; const ACTIVE = new Set(['pending', 'queued', 'running']); -const RUN_PATH = /^\/api\/v1\/(?:deployment-runs\/[a-f0-9-]{36}\/(?:logs\/)?|deployment-history\/[a-f0-9-]{36}\/|resource-releases\/[a-f0-9-]{36}\/(?:revisions\/)?)$/; -const RELEASE_TARGETS = new Set(['resource_release', 'harness_agent', 'static_site']); +const RUN_PATH = /^\/api\/v1\/(?:deployment-runs\/[a-f0-9-]{36}\/(?:logs\/)?|deployment-history\/[a-f0-9-]{36}\/)$/; +/** The target types whose deployments are a resource release's. A Job, its image and an image build are not. */ +export const RELEASE_TARGETS = new Set(['resource_release', 'harness_agent', 'static_site']); +/** How far back release deployment history reads when no window is chosen. */ +export const RELEASE_HISTORY_DAYS = 1; function uuid(value: unknown, message: string): string { if (typeof value !== 'string' || !UUID.test(value)) throw new ToolingError('INVALID_REQUEST', message); @@ -104,7 +103,7 @@ export function deploymentRun(row: unknown, now: number) { const run = rec(row); const uid = optionalUuid(run.uid); if (!uid) return null; - const target = rec(run.target), repository = rec(target.code_repository), branch = rec(target.code_repository_branch); + const target = rec(run.target), release = rec(target.resource_release), repository = rec(target.code_repository), branch = rec(target.code_repository_branch); const steps = list(rec(run.pipeline).steps).map(rec).filter(step => Number.isFinite(Number(step.sequence))) .sort((a, b) => Number(a.sequence) - Number(b.sequence)).slice(0, 60).map(step => { const startedAt = when(step.started_at), finishedAt = when(step.finished_at); @@ -124,7 +123,9 @@ export function deploymentRun(row: unknown, now: number) { currentStepKey: text(rec(run.pipeline).current_step_key, 200), steps, workflowStepKeys: list(run.workflow_step_keys).slice(0, 40).map(key => text(key, 200)), workflowRun: workflow, logsAvailable: rec(run.logs).state === 'available' || typeof rec(run.logs).url === 'string', error: errorText(run.error), errorCode: errorCode(run.error), - releaseUid: optionalUuid(rec(target.resource_release).uid) ?? (RELEASE_TARGETS.has(text(run.target_type, 60)) ? optionalUuid(target.uid) ?? optionalUuid(run.target_uid) : null) }; + releaseName: text(release.name, 200) || (RELEASE_TARGETS.has(text(run.target_type, 60)) ? text(target.name, 200) : ''), agentName: text(rec(target.agent).name, 200), + targetKind: text(target.kind, 60), targetKindLabel: text(target.kind_label, 100), revisionUid: optionalUuid(rec(target.revision).uid), + releaseUid: optionalUuid(release.uid) ?? (RELEASE_TARGETS.has(text(run.target_type, 60)) ? optionalUuid(target.uid) ?? optionalUuid(run.target_uid) : null) }; } type Run = NonNullable>; @@ -132,72 +133,31 @@ function errorCode(value: unknown): string { const code = rec(value).code; return typeof code === 'string' ? text(code, 100) : ''; } -function percentile(sorted: number[], share: number): number | null { - if (!sorted.length) return null; - return sorted[Math.min(sorted.length - 1, Math.max(0, Math.ceil(share * sorted.length) - 1))]; +/** One deployment of a resource release, as a history row shows it. */ +function releaseDeployment(run: Run) { + const active = ACTIVE_RUN_STATES.has(run.state); + return { uid: run.uid, releaseUid: run.releaseUid, releaseName: run.releaseName, agentName: run.agentName, kind: run.targetKind, + kindLabel: run.targetKindLabel || RELEASE_KINDS[run.targetKind] || run.targetKind.replace(/_/g, ' '), revisionUid: run.revisionUid, commit: run.commit, + repositoryName: run.repositoryName, branchUid: run.branchUid, branchName: run.branchName, state: run.state, outcome: run.outcome, source: run.source, + createdAt: run.createdAt, startedAt: run.startedAt, finishedAt: run.finishedAt, durationMs: active || run.finishedAt ? run.durationMs : null, + error: run.error, errorCode: run.errorCode, + // A stage left running in a deployment that has ended was stopped, as Command Center shows it. + steps: run.steps.map(step => ({ name: step.name || step.key, state: !active && step.state === 'running' ? 'stopped' : step.state })) }; } -const numbers = (values: (number | null)[]) => values.filter((value): value is number => typeof value === 'number' && Number.isFinite(value)).sort((a, b) => a - b); - -/** What a set of runs says about a release: counts by state, success rate over runs that - * either deployed or failed, durations of the runs that succeeded, the stage that takes - * longest and the failure seen most. The backend has no statistics endpoint. */ -export function releaseStats(runs: Run[]) { - const counts: Record = { succeeded: 0, failed: 0, blocked: 0, cancelled: 0, superseded: 0, skipped: 0, active: 0 }; - const stages = new Map(), failures = new Map(); - for (const run of runs) { - if (ACTIVE_RUN_STATES.has(run.state)) { counts.active++; continue; } - if (run.state in counts) counts[run.state]++; - if (run.state === 'succeeded') { - // A run can sit for minutes before its first stage, waiting on its workflow (an image - // build in another run): that wait is counted as a stage of its own. - const first = earliest(run.steps.map(step => step.startedAt)), waited = duration(run.startedAt, first, Number.NaN); - if (waited !== null) stages.set(WAITING_STAGE, [...(stages.get(WAITING_STAGE) || []), waited]); - for (const step of run.steps) if (step.state === 'succeeded') { - const name = step.name || step.key || step.kind; - if (name) stages.set(name, [...(stages.get(name) || []), step.durationMs]); - } - } - if (run.state === 'failed' || run.state === 'blocked') { - // Blocked runs often say why only in their outcome (workflow_prerequisite_failed). - const code = run.errorCode || run.steps.find(step => step.errorCode)?.errorCode || run.outcome || 'unknown'; - failures.set(code, (failures.get(code) || 0) + 1); - } - } - const decided = counts.succeeded + counts.failed + counts.blocked; - const durations = numbers(runs.filter(run => run.state === 'succeeded' && run.finishedAt).map(run => run.durationMs)); - let slowestStage: { name: string; medianMs: number } | null = null; - for (const [name, values] of stages) { - const median = percentile(numbers(values), 0.5); - if (median !== null && (!slowestStage || median > slowestStage.medianMs)) slowestStage = { name, medianMs: median }; - } - const [code, count] = [...failures].sort((a, b) => b[1] - a[1])[0] || []; - return { runs: runs.length, ...counts, successRate: decided ? counts.succeeded / decided : null, - medianMs: percentile(durations, 0.5), p90Ms: percentile(durations, 0.9), slowestStage, - commonFailure: code ? { code, count: count as number, of: counts.failed + counts.blocked } : null }; -} -function runSummary(run: Run) { - return { uid: run.uid, state: run.state, outcome: run.outcome, operation: run.operation, source: run.source, commit: run.commit, - createdAt: run.createdAt, startedAt: run.startedAt, finishedAt: run.finishedAt, durationMs: run.durationMs, error: run.error, errorCode: run.errorCode, - currentStepKey: run.currentStepKey, stepsDone: run.steps.filter(step => step.state === 'succeeded').length, stepsTotal: run.steps.length, - releaseUid: run.releaseUid, targetLabel: run.targetLabel, repositoryName: run.repositoryName, branchName: run.branchName }; -} -function releaseRow(row: unknown) { - const release = rec(row), kind = text(release.release_kind, 40); - return { uid: optionalUuid(release.uid), name: text(release.name, 200), kind, kindLabel: RELEASE_KINDS[kind] || kind.replace(/_/g, ' '), - branchUid: optionalUuid(release.code_repository_branch_uid), activeRevisionUid: optionalUuid(release.active_revision), desiredRevisionUid: optionalUuid(release.desired_revision) }; -} -function revisionRow(row: unknown) { - const revision = rec(row); - return { uid: optionalUuid(revision.uid), state: text(revision.state, 40), commit: text(revision.commit_sha, 40).slice(0, 7), - configurationRevision: Number.isInteger(revision.configuration_revision) ? revision.configuration_revision as number : null, - createdAt: when(revision.created_at), activatedAt: when(revision.activated_at) }; -} -const since = (days: number, now: number) => new Date(now - days * 86_400_000).toISOString(); function page(value: unknown, what: string): { count: number; results: unknown[] } { const body = rec(value); if (!Array.isArray(body.results) || !Number.isInteger(body.count) || (body.count as number) < 0) throw new ToolingError('API_ERROR', `${what} ${what.endsWith('s') ? 'are' : 'is'} unavailable.`); return { count: body.count as number, results: body.results }; } +function releaseQuery(environmentUid: string, branchUid: string, values: Record): URLSearchParams { + const query = new URLSearchParams({ organization_environment_uid: environmentUid, target_type__in: [...RELEASE_TARGETS].join(','), ordering: '-creation_date', ...values }); + if (branchUid) query.set('code_repository_branch_uid', branchUid); + return query; +} +function releaseDeployments(rows: unknown[], now: number) { + return rows.filter(row => RELEASE_TARGETS.has(text(rec(row).target_type, 60))) + .map(row => deploymentRun(row, now)).filter((run): run is Run => !!run).map(releaseDeployment); +} function pause(ms: number, signal?: AbortSignal): Promise { return new Promise((resolve, reject) => { if (signal?.aborted) { reject(new ToolingError('CANCELLED', 'The read was cancelled.')); return; } @@ -279,95 +239,32 @@ export function createDeployments(options: { host: HostServices; signal?: AbortS complete: page.complete === true, entries, now: at }; summary = `${entries.length} log ${entries.length === 1 ? 'entry' : 'entries'}.`; } - } else if (input.action === 'deployment-releases') { - // Two reads: one page of releases, then their runs of the window in one request. + } else if (input.action === 'deployment-release-history') { + // Command Center's Deployment History narrowed to resource releases: one read, newest first. const environmentUid = uuid(input.environmentUid, 'Choose an organization environment first.'); - const days = integer(input.days, 1, 90, 30, 'Choose between 1 and 90 days of release history.'); - const limit = integer(input.limit, 1, 100, 50, 'Request between 1 and 100 releases.'); - const offset = integer(input.offset, 0, 1_000_000, 0, 'The release list offset is invalid.'); + const days = integer(input.days, 1, 90, RELEASE_HISTORY_DAYS, 'Choose between 1 and 90 days of deployment history.'); + const limit = integer(input.limit, 1, 100, 25, 'Request between 1 and 100 deployments.'); + const offset = integer(input.offset, 0, 1_000_000, 0, 'The deployment history offset is invalid.'); const branchUid = input.codeRepositoryBranchUid === undefined ? '' : uuid(input.codeRepositoryBranchUid, 'Choose a repository branch from the current environment.'); - if (input.releaseKind !== undefined && !(typeof input.releaseKind === 'string' && input.releaseKind in RELEASE_KINDS)) throw new ToolingError('INVALID_REQUEST', `Choose a release kind: ${Object.keys(RELEASE_KINDS).join(', ')}.`); + const releaseUid = input.releaseUid === undefined ? '' : uuid(input.releaseUid, 'Choose a release from the current environment.'); + if (input.releaseKind !== undefined && !(typeof input.releaseKind === 'string' && Object.hasOwn(RELEASE_KINDS, input.releaseKind))) throw new ToolingError('INVALID_REQUEST', `Choose a release kind: ${Object.keys(RELEASE_KINDS).join(', ')}.`); if (input.search !== undefined && typeof input.search !== 'string') throw new ToolingError('INVALID_REQUEST', 'Search with text.'); const kind = input.releaseKind || '', search = text(input.search ?? '', 300).trim(); - const query = new URLSearchParams({ organization_environment_uid: environmentUid, ordering: 'name', limit: String(limit), offset: String(offset) }); - if (branchUid) query.set('code_repository_branch_uid', branchUid); - if (kind) query.set('release_kind', kind); + const query = releaseQuery(environmentUid, branchUid, { created_after: new Date(now - days * 86_400_000).toISOString(), limit: String(limit), offset: String(offset) }); + if (releaseUid) query.set('target_uid', releaseUid); + if (kind) query.set('target_kind', kind); if (search) query.set('search', search); - const releases = page(await scoped.request(backend, 'GET', `/api/v1/resource-releases/?${query}`), 'The release list'); - const rows = releases.results.map(releaseRow).filter(row => row.uid); - let runs: Run[] = [], inWindow = 0; - if (rows.length) { - const runQuery = new URLSearchParams({ organization_environment_uid: environmentUid, target_uid__in: rows.map(row => row.uid).join(','), - created_after: since(days, now), ordering: '-creation_date', limit: '500' }); - const found = page(await scoped.request(backend, 'GET', `/api/v1/deployment-runs/?${runQuery}`), 'The deployment runs'); - inWindow = found.count; runs = found.results.map(row => deploymentRun(row, now)).filter((run): run is Run => !!run); - } - const byRelease = new Map(); - for (const run of runs) if (run.releaseUid) byRelease.set(run.releaseUid, [...(byRelease.get(run.releaseUid) || []), run]); - // A release is deployed whatever the window: its last deploy, repository and branch come from its own - // history, not only from the window's runs. One more request covers every release without a - // successful run in the window; a few that request leaves out are read one by one. - const lastDeploy = new Map(); - for (const [releaseUid, own] of byRelease) { const deployed = own.find(run => run.state === 'succeeded'); if (deployed) lastDeploy.set(releaseUid, deployed); } - const quiet = rows.filter(row => !lastDeploy.has(row.uid as string)); - const deploys = async (targets: string[], limit: number) => page(await scoped.request(backend, 'GET', `/api/v1/deployment-runs/?${new URLSearchParams({ organization_environment_uid: environmentUid, - target_uid__in: targets.join(','), state: 'succeeded', ordering: '-creation_date', limit: String(limit) })}`), 'The deployment runs').results - .map(row => deploymentRun(row, now)).filter((run): run is Run => !!run); - if (quiet.length) { - for (const run of await deploys(quiet.map(row => row.uid as string), 200)) if (run.releaseUid && !lastDeploy.has(run.releaseUid)) lastDeploy.set(run.releaseUid, run); - const missing = quiet.filter(row => row.activeRevisionUid && !lastDeploy.has(row.uid as string)).slice(0, 10); - for (const run of (await Promise.all(missing.map(row => deploys([row.uid as string], 1)))).flat()) if (run.releaseUid) lastDeploy.set(run.releaseUid, run); - } - const names = new Map(); - const unnamed = [...new Set(rows.filter(row => row.branchUid && !(byRelease.get(row.uid as string)?.[0]?.repositoryName || lastDeploy.get(row.uid as string)?.repositoryName)).map(row => row.branchUid as string))]; - if (unnamed.length) { - // Names only: a reader who may see a release but not its branch still gets the list. - const branches = await scoped.request(backend, 'GET', `/api/v1/code-repository-branches/?${new URLSearchParams({ uid__in: unnamed.join(','), limit: String(unnamed.length) })}`) - .then(body => page(body, 'The repository branches').results, error => { if (error instanceof ToolingError && ['API_ERROR', 'FORBIDDEN'].includes(error.code)) return []; throw error; }); - for (const branch of branches.map(rec)) { const uid = optionalUuid(branch.uid); if (uid) names.set(uid, { repositoryName: text(branch.code_repository_name, 200), branchName: text(branch.repository_branch, 200) }); } - } - const results = rows.map(row => { - const own = byRelease.get(row.uid as string) || [], last = own[0], active = own.find(run => ACTIVE_RUN_STATES.has(run.state)); - const deployed = lastDeploy.get(row.uid as string), named = names.get(row.branchUid as string); - return { ...row, repositoryName: last?.repositoryName || deployed?.repositoryName || named?.repositoryName || '', branchName: last?.branchName || deployed?.branchName || named?.branchName || '', - recent: own.slice(0, 10).map(run => ({ uid: run.uid, state: run.state })), deployed: !!row.activeRevisionUid, - lastRun: last ? runSummary(last) : null, lastDeploy: deployed ? runSummary(deployed) : null, activeRun: active ? runSummary(active) : null, stats: releaseStats(own) }; - }); - const deploying = results.filter(row => row.activeRun).length; - data = { environmentUid, branchUid: branchUid || null, releaseKind: kind || null, search: search || null, days, count: releases.count, limit, offset, now: at, - runsCounted: runs.length, runsInWindow: inWindow, runsTruncated: inWindow > runs.length, results }; - summary = `${releases.count} ${releases.count === 1 ? 'release' : 'releases'}${deploying ? `; ${deploying} deploying` : ''}.`; - } else if (input.action === 'deployment-release') { - const environmentUid = uuid(input.environmentUid, 'Choose an organization environment first.'); - const releaseUid = uuid(input.releaseUid, 'Choose a release from the current environment.'); - const days = integer(input.days, 1, 90, 30, 'Choose between 1 and 90 days of release history.'); - const limit = integer(input.limit, 1, 200, 100, 'Request between 1 and 200 runs.'); - const body = await scoped.request(backend, 'GET', `/api/v1/resource-releases/${releaseUid}/`); - if (rec(body).hidden === true) { data = { hidden: true, uid: releaseUid, now: at }; summary = HIDDEN_RELEASE; } - else { - const release = releaseRow(body); - if (release.uid !== releaseUid) throw new ToolingError('API_ERROR', 'The release record is unavailable.'); - const revisionBody = rec(await scoped.request(backend, 'GET', `/api/v1/resource-releases/${releaseUid}/revisions/?limit=20`)); - const revisions = list(revisionBody.results).map(revisionRow); - const runQuery = new URLSearchParams({ target_uid: releaseUid, organization_environment_uid: environmentUid, created_after: since(days, now), ordering: '-creation_date', limit: String(limit) }); - const found = page(await scoped.request(backend, 'GET', `/api/v1/deployment-runs/?${runQuery}`), 'The deployment runs'); - const runs = found.results.map(row => deploymentRun(row, now)).filter((run): run is Run => !!run); - const revision = (uid: string | null) => uid ? revisions.find(item => item.uid === uid) || { uid, state: '', commit: '', configurationRevision: null, createdAt: null, activatedAt: null } : null; - data = { hidden: false, uid: releaseUid, environmentUid, days, now: at, - release: { ...release, repositoryName: runs[0]?.repositoryName || '', branchName: runs[0]?.branchName || '', - activeRevision: revision(release.activeRevisionUid), desiredRevision: revision(release.desiredRevisionUid) }, - count: found.count, counted: runs.length, truncated: found.count > runs.length, runs: runs.map(runSummary), stats: releaseStats(runs) }; - summary = `${release.name || 'Release'}: ${found.count} ${found.count === 1 ? 'run' : 'runs'} in ${days} ${days === 1 ? 'day' : 'days'}.`; - } + const found = page(await scoped.request(backend, 'GET', `/api/v1/deployment-runs/?${query}`), 'The deployment history'); + data = { environmentUid, branchUid: branchUid || null, releaseUid: releaseUid || null, releaseKind: kind || null, search: search || null, + days, count: found.count, limit, offset, now: at, results: releaseDeployments(found.results, now) }; + summary = `${found.count} ${found.count === 1 ? 'deployment' : 'deployments'} in the last ${days === 1 ? 'day' : `${days} days`}.`; } else if (input.action === 'deployment-active') { - // The cheap check: only runs still queued or running, which the backend allows one of per target. + // The cheap check: only release deployments still queued or running, one at most per release. const environmentUid = uuid(input.environmentUid, 'Choose an organization environment first.'); const branchUid = input.codeRepositoryBranchUid === undefined ? '' : uuid(input.codeRepositoryBranchUid, 'Choose a repository branch from the current environment.'); - const query = new URLSearchParams({ organization_environment_uid: environmentUid, state__in: [...ACTIVE_RUN_STATES].join(','), ordering: '-creation_date', limit: '20' }); - if (branchUid) query.set('code_repository_branch_uid', branchUid); - const found = page(await scoped.request(backend, 'GET', `/api/v1/deployment-runs/?${query}`), 'The deployment runs'); - const runs = found.results.map(row => deploymentRun(row, now)).filter((run): run is Run => !!run).map(runSummary); - data = { environmentUid, branchUid: branchUid || null, count: found.count, now: at, runs }; + const query = releaseQuery(environmentUid, branchUid, { state__in: [...ACTIVE_RUN_STATES].join(','), limit: '100' }); + const found = page(await scoped.request(backend, 'GET', `/api/v1/deployment-runs/?${query}`), 'The deployment history'); + data = { environmentUid, branchUid: branchUid || null, count: found.count, now: at, results: releaseDeployments(found.results, now) }; summary = found.count ? `${found.count} ${found.count === 1 ? 'deployment' : 'deployments'} in progress.` : 'No deployments in progress.'; } else if (input.action === 'deployment-wait') { // Re-reads one run every ten seconds until it ends or the wait runs out; never longer than two minutes. diff --git a/tests/deployment-release-history.test.cjs b/tests/deployment-release-history.test.cjs new file mode 100644 index 0000000..07428fa --- /dev/null +++ b/tests/deployment-release-history.test.cjs @@ -0,0 +1,128 @@ +const test = require('node:test'), assert = require('node:assert/strict'); +const { createDeployments, createNodeHost, WAIT_LIMIT_SECONDS } = require('../dist'); +const id = n => '00000000-0000-4000-8000-' + String(n).padStart(12, '0'), backend = 'https://fixture.example'; +const API = id(10), AGENT = id(11), SITE = id(12), JOB = id(13), IMAGE = id(14), ENV = id(1), BRANCH = id(5); +const iso = minutes => new Date(Date.parse('2026-11-18T12:00:00Z') + minutes * 60_000).toISOString(); +const step = (sequence, name, state, minutes, base = 0) => ({ uid: id(500 + sequence), sequence, name, kind: name.toLowerCase(), state, + started_at: iso(base), finished_at: state === 'running' || state === 'pending' ? null : iso(base + minutes) }); +function run(n, release, state, { minutes = 4, error, type = 'resource_release', steps } = {}) { + return { uid: id(100 + n), target_type: type, target_uid: release, state, operation: 'build_and_deploy', source: 'repository_event', commit_sha: `abc${n}def0123456`, + code_repository_branch_uid: BRANCH, created_at: iso(-n * 60), started_at: iso(-n * 60), finished_at: ['queued', 'running'].includes(state) ? null : iso(-n * 60 + minutes), + target: { uid: release, name: 'release-name', display_name: 'pricing-api', kind: type === 'resource_release' ? 'fastapi' : type, kind_label: { resource_release: 'FastAPI', harness_agent: 'Harness Agent', static_site: 'Static site', job: 'Job' }[type], + code_repository: { uid: id(4), name: 'pricing-engine' }, code_repository_branch: { uid: BRANCH, repository_branch: 'main' }, revision: { uid: id(20 + n) }, + agent: type === 'harness_agent' ? { uid: id(6), name: 'Risk Analyst' } : null, + resource_release: ['resource_release', 'harness_agent'].includes(type) ? { uid: release, name: type === 'harness_agent' ? 'tau' : 'app' } : null }, + pipeline: { current_step_key: state === 'running' ? 'deploy' : '', steps: steps || [step(1, 'Build', 'succeeded', 3, -n * 60), step(2, 'Deploy', state === 'succeeded' ? 'succeeded' : state === 'running' ? 'running' : 'failed', minutes, -n * 60)] }, + ...(error ? { error } : {}), billing: { secret: 'never-return' }, cost_summary: { total: 99 } }; +} +function fixture({ runs, waitStates, ignoreFilters = false } = {}) { + const calls = [], state = { username: 'fixture' }; + const allRuns = runs || [run(0, API, 'running'), run(1, JOB, 'running', { type: 'job' }), run(2, API, 'succeeded'), run(3, IMAGE, 'succeeded', { type: 'code_repository_image' }), + run(4, AGENT, 'failed', { type: 'harness_agent', error: { code: 'image_build_failed', detail: 'no image' } }), run(5, SITE, 'succeeded', { type: 'static_site' })]; + const reads = { wait: 0 }; + const api = createDeployments({ host: createNodeHost(), env: { MAINSEQUENCE_ENDPOINT: backend, MAINSEQUENCE_ACCESS_TOKEN: 'synthetic-secret' }, fetch: async (url, init) => { + const u = new URL(url); calls.push({ path: u.pathname, query: Object.fromEntries(u.searchParams), method: init.method }); + if (u.pathname === '/api/v1/users/me/') return Response.json({ username: state.username }); + if (u.pathname === '/api/v1/deployment-runs/') { + let rows = allRuns; + const q = u.searchParams; + if (!ignoreFilters && q.get('target_type__in')) rows = rows.filter(row => q.get('target_type__in').split(',').includes(row.target_type)); + if (q.get('target_uid')) rows = rows.filter(row => row.target_uid === q.get('target_uid')); + if (q.get('state__in')) rows = rows.filter(row => q.get('state__in').split(',').includes(row.state)); + return Response.json({ count: rows.length, results: rows }); + } + if (u.pathname === `/api/v1/deployment-runs/${id(100)}/`) { + const states = waitStates || ['running']; const now = states[Math.min(reads.wait++, states.length - 1)]; + return Response.json(run(0, API, now)); + } + return Response.json({ detail: 'not found' }, { status: 404 }); + } }); + return { calls, state, reads, run: (action, args = {}) => api.dispatch({ action, backend, expectedAccount: 'fixture', ...args }) }; +} + +const NOT_RELEASES = new RegExp([id(101), id(103), JOB, IMAGE].join('|')); +const historyCalls = f => f.calls.filter(call => call.path !== '/api/v1/users/me/'); + +test('release deployment history is one read of resource release deployments only, newest first', async () => { + const f = fixture(), result = await f.run('deployment-release-history', { environmentUid: ENV }); + assert.equal(result.status, 'success', result.summary); + const calls = historyCalls(f); + assert.deepEqual(calls.map(call => call.path), ['/api/v1/deployment-runs/'], 'no release inventory, revisions, branches or per-release reads'); + const { query } = calls[0]; + assert.equal(query.target_type__in, 'resource_release,harness_agent,static_site'); + assert.deepEqual([query.organization_environment_uid, query.ordering, query.limit, query.offset], [ENV, '-creation_date', '25', '0']); + assert.ok(Math.abs(Date.parse(query.created_after) - (Date.now() - 86_400_000)) < 60_000, 'the history opens on the last day'); + assert.deepEqual(result.data.results.map(row => row.uid), [id(100), id(102), id(104), id(105)]); + assert.equal(result.data.count, 4); assert.equal(result.summary, '4 deployments in the last day.'); + assert.doesNotMatch(JSON.stringify(result), NOT_RELEASES, 'no Job or image'); + assert.doesNotMatch(JSON.stringify(result), /never-return|cost_summary|synthetic-secret/); +}); + +test('each row carries what Command Center\'s columns show', async () => { + const [api, done, agent, site] = (await fixture().run('deployment-release-history', { environmentUid: ENV })).data.results; + assert.deepEqual([api.releaseName, api.releaseUid, api.kindLabel, api.repositoryName, api.branchName, api.branchUid], ['app', API, 'FastAPI', 'pricing-engine', 'main', BRANCH]); + assert.deepEqual([api.commit, api.revisionUid, api.state, api.source, api.startedAt], ['abc0def', id(20), 'running', 'repository_event', iso(0)]); + assert.deepEqual(api.steps, [{ name: 'Build', state: 'succeeded' }, { name: 'Deploy', state: 'running' }]); + assert.equal(done.durationMs, 4 * 60_000); assert.equal(done.finishedAt, iso(-116)); + assert.deepEqual([agent.releaseName, agent.agentName, agent.kindLabel, agent.errorCode], ['tau', 'Risk Analyst', 'Harness Agent', 'image_build_failed']); assert.match(agent.error, /no image/); + assert.deepEqual([site.releaseUid, site.releaseName], [SITE, 'release-name'], 'a target without a release reference is still that release'); +}); + +test('a deployment that ended does not keep a running stage or a growing duration', async () => { + const ended = run(1, API, 'cancelled', { steps: [step(1, 'Deploy', 'running', 0)] }); + ended.finished_at = null; + const [row] = (await fixture({ runs: [ended] }).run('deployment-release-history', { environmentUid: ENV })).data.results; + assert.deepEqual(row.steps, [{ name: 'Deploy', state: 'stopped' }]); assert.equal(row.durationMs, null); +}); + +test('release history sends its filters to the backend and refuses unknown ones', async () => { + const f = fixture(); + const result = await f.run('deployment-release-history', { environmentUid: ENV, codeRepositoryBranchUid: BRANCH, releaseUid: API, releaseKind: 'fastapi', search: 'app', days: 30, limit: 50, offset: 50 }); + assert.equal(result.status, 'success', result.summary); + const { query } = historyCalls(f)[0]; + assert.deepEqual([query.code_repository_branch_uid, query.target_uid, query.target_kind, query.search, query.limit, query.offset], [BRANCH, API, 'fastapi', 'app', '50', '50']); + assert.equal(query.target_type__in, 'resource_release,harness_agent,static_site', 'a filter never widens the history'); + assert.equal(result.summary, '2 deployments in the last 30 days.'); + for (const bad of [{ releaseKind: 'job' }, { releaseKind: 'toString' }, { releaseUid: 'app' }, { days: 0 }, { days: 91 }, { limit: 101 }, { search: 7 }, { environmentUid: 'nope' }]) { + const refused = await f.run('deployment-release-history', { environmentUid: ENV, ...bad }); + assert.equal(refused.status, 'failure', JSON.stringify(bad)); assert.equal(refused.error.code, 'INVALID_REQUEST'); + } +}); + +test('anything that is not a resource release is dropped even if the backend returns it', async () => { + for (const action of ['deployment-release-history', 'deployment-active']) { + const result = await fixture({ ignoreFilters: true }).run(action, { environmentUid: ENV }); + assert.ok(result.data.results.length); assert.doesNotMatch(JSON.stringify(result.data.results), NOT_RELEASES, action); + } +}); + +test('the active check asks only for queued and running release deployments', async () => { + const f = fixture(), result = await f.run('deployment-active', { environmentUid: ENV, codeRepositoryBranchUid: BRANCH }); + assert.equal(result.status, 'success'); + const { query } = historyCalls(f)[0]; + assert.deepEqual([query.state__in, query.target_type__in, query.code_repository_branch_uid], ['queued,running', 'resource_release,harness_agent,static_site', BRANCH]); + assert.deepEqual(result.data.results.map(row => row.uid), [id(100)], 'the running Job is not a release deployment'); + assert.equal(result.summary, '1 deployment in progress.'); + assert.equal((await fixture({ runs: [run(1, API, 'succeeded')] }).run('deployment-active', { environmentUid: ENV })).summary, 'No deployments in progress.'); +}); + +test('a wait returns as soon as the run ends, and says so when it is still running at the limit', async () => { + const done = fixture({ waitStates: ['running', 'succeeded'] }); + const started = Date.now(), finished = await done.run('deployment-wait', { runUid: id(100), timeoutSeconds: 2 }); + assert.equal(finished.status, 'success', finished.summary); assert.equal(finished.data.finished, true); assert.equal(finished.data.run.state, 'succeeded'); + assert.equal(finished.summary, 'Deployment succeeded.'); assert.ok(Date.now() - started < 4000); + const still = fixture({ waitStates: ['running'] }), waited = await still.run('deployment-wait', { runUid: id(100), timeoutSeconds: 1 }); + assert.equal(waited.data.finished, false); assert.match(waited.summary, /still running after \d+ s/); + assert.ok(still.reads.wait >= 2 && still.reads.wait <= 3, `reads ${still.reads.wait}`); + assert.equal((await still.run('deployment-wait', { runUid: id(100), timeoutSeconds: WAIT_LIMIT_SECONDS + 1 })).error.code, 'INVALID_REQUEST'); + assert.equal((await still.run('deployment-wait', { runUid: id(77), timeoutSeconds: 1 })).data.hidden, true); +}); + +test('a wait stops when its read is cancelled', async () => { + const controller = new AbortController(); + const api = createDeployments({ host: createNodeHost(), signal: controller.signal, env: { MAINSEQUENCE_ENDPOINT: backend, MAINSEQUENCE_ACCESS_TOKEN: 'synthetic-secret' }, + fetch: async url => new URL(url).pathname === '/api/v1/users/me/' ? Response.json({ username: 'fixture' }) : Response.json(run(0, API, 'running')) }); + setTimeout(() => controller.abort(), 300); + const started = Date.now(), result = await api.dispatch({ action: 'deployment-wait', backend, expectedAccount: 'fixture', runUid: id(100), timeoutSeconds: 60 }); + assert.equal(result.status, 'cancelled'); assert.ok(Date.now() - started < 3000); +}); diff --git a/tests/deployment-releases.test.cjs b/tests/deployment-releases.test.cjs deleted file mode 100644 index bdcfb9b..0000000 --- a/tests/deployment-releases.test.cjs +++ /dev/null @@ -1,179 +0,0 @@ -const test = require('node:test'), assert = require('node:assert/strict'); -const { createDeployments, createNodeHost, releaseStats, WAIT_LIMIT_SECONDS } = require('../dist'); -const id = n => '00000000-0000-4000-8000-' + String(n).padStart(12, '0'), backend = 'https://fixture.example'; -const API = id(10), AGENT = id(11), SITE = id(12), ENV = id(1), BRANCH = id(5); -const iso = minutes => new Date(Date.parse('2026-11-18T12:00:00Z') + minutes * 60_000).toISOString(); -const step = (sequence, name, state, minutes, base = 0) => ({ uid: id(500 + sequence), sequence, name, kind: name.toLowerCase(), state, - started_at: iso(base), finished_at: state === 'running' || state === 'pending' ? null : iso(base + minutes) }); -function run(n, release, state, { minutes = 4, error, type = 'resource_release', steps } = {}) { - return { uid: id(100 + n), target_type: type, target_uid: release, state, operation: 'build_and_deploy', source: 'repository_event', commit_sha: `abc${n}def0123456`, - code_repository_branch_uid: BRANCH, created_at: iso(-n * 60), started_at: iso(-n * 60), finished_at: ['queued', 'running'].includes(state) ? null : iso(-n * 60 + minutes), - target: { uid: release, display_name: 'pricing-api', code_repository: { uid: id(4), name: 'pricing-engine' }, code_repository_branch: { uid: BRANCH, repository_branch: 'main' }, - resource_release: type === 'static_site' ? null : { uid: release, name: 'x' } }, - pipeline: { current_step_key: state === 'running' ? 'deploy' : '', steps: steps || [step(1, 'Build', 'succeeded', 3, -n * 60), step(2, 'Deploy', state === 'succeeded' ? 'succeeded' : state === 'running' ? 'running' : 'failed', minutes, -n * 60)] }, - ...(error ? { error } : {}), billing: { secret: 'never-return' }, cost_summary: { total: 99 } }; -} -function fixture({ runs, waitStates, batchLimit, siteDeployed = false, branchStatus = 200 } = {}) { - const calls = [], state = { username: 'fixture' }; - const allRuns = runs || [run(0, API, 'running'), run(1, API, 'succeeded'), run(2, API, 'failed', { error: { code: 'runtime_readiness_timeout', detail: 'not ready' } }), - run(3, AGENT, 'failed', { type: 'harness_agent', error: { code: 'image_build_failed' } }), run(4, AGENT, 'superseded', { type: 'harness_agent' }), run(5, SITE, 'succeeded', { type: 'static_site' })]; - const reads = { wait: 0 }; - const api = createDeployments({ host: createNodeHost(), env: { MAINSEQUENCE_ENDPOINT: backend, MAINSEQUENCE_ACCESS_TOKEN: 'synthetic-secret' }, fetch: async (url, init) => { - const u = new URL(url); calls.push({ path: u.pathname, query: Object.fromEntries(u.searchParams), method: init.method }); - if (u.pathname === '/api/v1/users/me/') return Response.json({ username: state.username }); - if (u.pathname === '/api/v1/resource-releases/') return Response.json({ count: 3, results: [ - { uid: API, name: 'pricing-api', release_kind: 'fastapi', code_repository_branch_uid: BRANCH, active_revision: id(20), desired_revision: id(21), workload_user_uid: 'w' }, - { uid: AGENT, name: 'risk-agent', release_kind: 'harness_agent', code_repository_branch_uid: BRANCH }, - { uid: SITE, name: 'docs-site', release_kind: 'static_site', code_repository_branch_uid: BRANCH, ...(siteDeployed ? { active_revision: id(22) } : {}) }] }); - if (u.pathname === '/api/v1/code-repository-branches/') return branchStatus === 200 - ? Response.json({ count: 1, results: [{ uid: BRANCH, code_repository_name: 'pricing-engine', repository_branch: 'main' }].filter(row => u.searchParams.get('uid__in').split(',').includes(row.uid)) }) - : Response.json({ detail: 'forbidden' }, { status: branchStatus }); - if (u.pathname === `/api/v1/resource-releases/${API}/`) return Response.json({ uid: API, name: 'pricing-api', release_kind: 'fastapi', code_repository_branch_uid: BRANCH, active_revision: id(20), desired_revision: id(21) }); - if (u.pathname === `/api/v1/resource-releases/${API}/revisions/`) return Response.json({ count: 2, results: [ - { uid: id(21), state: 'pending', commit_sha: 'abc0def0123456', configuration_revision: 15, created_at: iso(0) }, - { uid: id(20), state: 'ready', commit_sha: 'abc1def0123456', configuration_revision: 14, created_at: iso(-60), activated_at: iso(-56) }] }); - if (u.pathname === '/api/v1/deployment-runs/') { - let rows = allRuns; - if (u.searchParams.get('target_uid__in')) rows = rows.filter(row => u.searchParams.get('target_uid__in').split(',').includes(row.target_uid)); - if (u.searchParams.get('target_uid')) rows = rows.filter(row => row.target_uid === u.searchParams.get('target_uid')); - if (u.searchParams.get('state__in')) rows = rows.filter(row => u.searchParams.get('state__in').split(',').includes(row.state)); - if (u.searchParams.get('state')) rows = rows.filter(row => row.state === u.searchParams.get('state')); - if (u.searchParams.get('created_after')) rows = rows.filter(row => row.created_at >= u.searchParams.get('created_after')); - const count = rows.length, multiple = (u.searchParams.get('target_uid__in') || '').includes(','); - if (batchLimit && multiple && u.searchParams.get('state')) rows = rows.slice(0, batchLimit); - return Response.json({ count, results: rows }); - } - if (u.pathname === `/api/v1/deployment-runs/${id(100)}/`) { - const states = waitStates || ['running']; const now = states[Math.min(reads.wait++, states.length - 1)]; - return Response.json(run(0, API, now)); - } - return Response.json({ detail: 'not found' }, { status: 404 }); - } }); - return { calls, state, reads, run: (action, args = {}) => api.dispatch({ action, backend, expectedAccount: 'fixture', ...args }) }; -} - -test('the release list reads one page of releases and their runs in one more request, grouped by release', async () => { - const f = fixture(), result = await f.run('deployment-releases', { environmentUid: ENV, days: 30 }); - assert.equal(result.status, 'success', result.summary); - const reads = f.calls.filter(call => call.path !== '/api/v1/users/me/'); - assert.deepEqual(reads.map(call => call.path), ['/api/v1/resource-releases/', '/api/v1/deployment-runs/', '/api/v1/deployment-runs/']); - assert.deepEqual([reads[2].query.target_uid__in, reads[2].query.state, reads[2].query.created_after], [AGENT, 'succeeded', undefined], 'only the release without a successful run in the window is looked up further back'); - assert.ok(f.calls.every(call => call.method === 'GET')); - assert.equal(reads[0].query.organization_environment_uid, ENV); - assert.deepEqual(reads[1].query.target_uid__in.split(','), [API, AGENT, SITE]); - assert.ok(Date.parse(reads[1].query.created_after) <= Date.now() - 29 * 86_400_000, 'the window starts 30 days back'); - const [api, agent, site] = result.data.results; - assert.deepEqual([api.kindLabel, agent.kindLabel, site.kindLabel], ['API', 'Agent', 'Static site']); - assert.deepEqual(api.recent.map(item => item.state), ['running', 'succeeded', 'failed']); - assert.equal(api.activeRun.uid, id(100)); assert.equal(api.activeRun.stepsTotal, 2); - assert.equal(api.lastRun.uid, id(100)); assert.equal(api.repositoryName, 'pricing-engine'); assert.equal(api.branchName, 'main'); - assert.equal(api.stats.succeeded, 1); assert.equal(api.stats.failed, 1); assert.equal(api.stats.active, 1); assert.equal(api.stats.successRate, 0.5); - assert.equal(agent.activeRun, null); assert.equal(agent.stats.superseded, 1); - assert.equal(site.recent.length, 1, 'a static site run without a release reference still counts by its target'); - assert.match(result.summary, /3 releases; 1 deploying/); - assert.doesNotMatch(JSON.stringify(result), /never-return|cost_summary|synthetic-secret/); -}); - -test('the release list passes its filters to the backend and refuses unknown ones', async () => { - const f = fixture(); - assert.equal((await f.run('deployment-releases', { environmentUid: ENV, codeRepositoryBranchUid: BRANCH, releaseKind: 'fastapi', search: 'pricing' })).status, 'success'); - const query = f.calls.find(call => call.path === '/api/v1/resource-releases/').query; - assert.equal(query.code_repository_branch_uid, BRANCH); assert.equal(query.release_kind, 'fastapi'); assert.equal(query.search, 'pricing'); - for (const bad of [{ releaseKind: 'streamlit' }, { days: 0 }, { days: 91 }, { limit: 101 }, { search: 7 }, { environmentUid: 'nope' }]) { - const result = await f.run('deployment-releases', { environmentUid: ENV, ...bad }); - assert.equal(result.status, 'failure', JSON.stringify(bad)); assert.equal(result.error.code, 'INVALID_REQUEST'); - } -}); - -test('a release history shows what is serving, what is wanted, its runs and their stats', async () => { - const f = fixture(), result = await f.run('deployment-release', { environmentUid: ENV, releaseUid: API, days: 7 }); - assert.equal(result.status, 'success', result.summary); - const runs = f.calls.find(call => call.path === '/api/v1/deployment-runs/').query; - assert.equal(runs.target_uid, API); assert.equal(runs.organization_environment_uid, ENV); assert.equal(runs.ordering, '-creation_date'); - const { release, stats } = result.data; - assert.equal(release.activeRevision.commit, 'abc1def'); assert.equal(release.activeRevision.configurationRevision, 14); - assert.equal(release.desiredRevision.commit, 'abc0def'); assert.equal(release.repositoryName, 'pricing-engine'); - assert.equal(result.data.runs.length, 3); assert.equal(result.data.runs[2].errorCode, 'runtime_readiness_timeout'); - assert.deepEqual(stats.commonFailure, { code: 'runtime_readiness_timeout', count: 1, of: 1 }); - assert.equal(stats.medianMs, 4 * 60_000); - assert.deepEqual(stats.slowestStage, { name: 'Deploy', medianMs: 4 * 60_000 }); - const hidden = await f.run('deployment-release', { environmentUid: ENV, releaseUid: id(99) }); - assert.equal(hidden.status, 'success'); assert.equal(hidden.data.hidden, true); assert.match(hidden.summary, /can't see this release/); -}); - -test('release stats count only decided runs toward the success rate and ignore unfinished durations', () => { - const t = s => new Date(Date.parse('2026-11-18T12:00:00Z') + s * 1000).toISOString(); - const runs = [ - { state: 'succeeded', finishedAt: 'x', durationMs: 1000, startedAt: t(0), steps: [{ state: 'succeeded', name: 'Build', durationMs: 600, startedAt: t(0) }, { state: 'succeeded', name: 'Deploy', durationMs: 400, startedAt: t(1) }] }, - { state: 'succeeded', finishedAt: 'x', durationMs: 3000, startedAt: t(0), steps: [{ state: 'succeeded', name: 'Build', durationMs: 2600, startedAt: t(0) }, { state: 'succeeded', name: 'Deploy', durationMs: 400, startedAt: t(3) }] }, - { state: 'succeeded', finishedAt: 'x', durationMs: 2000, steps: [] }, - { state: 'blocked', errorCode: 'quota', steps: [] }, { state: 'failed', errorCode: '', steps: [{ state: 'failed', errorCode: 'image_build_failed' }] }, - { state: 'blocked', errorCode: '', outcome: 'workflow_prerequisite_failed', steps: [] }, { state: 'blocked', errorCode: '', outcome: 'workflow_prerequisite_failed', steps: [] }, - { state: 'superseded', steps: [] }, { state: 'cancelled', steps: [] }, { state: 'running', durationMs: 99_000, steps: [] }]; - const stats = releaseStats(runs); - assert.equal(stats.runs, 10); assert.equal(stats.active, 1); assert.equal(stats.successRate, 3 / 7); - assert.equal(stats.medianMs, 2000); assert.equal(stats.p90Ms, 3000); - assert.deepEqual(stats.slowestStage, { name: 'Build', medianMs: 600 }); - assert.deepEqual(stats.commonFailure, { code: 'workflow_prerequisite_failed', count: 2, of: 4 }, 'a blocked run without an error code is named by its outcome'); - const waiting = releaseStats([{ state: 'succeeded', finishedAt: 'x', durationMs: 300_000, startedAt: t(0), steps: [{ state: 'succeeded', name: 'Deploy', durationMs: 60_000, startedAt: t(240) }] }]); - assert.deepEqual(waiting.slowestStage, { name: 'Waiting to start', medianMs: 240_000 }, 'the wait before the first stage counts as a stage'); - assert.equal(releaseStats([]).successRate, null); assert.equal(releaseStats([]).medianMs, null); assert.equal(releaseStats([]).commonFailure, null); -}); - -test('the active check asks only for queued and running runs', async () => { - const f = fixture(), result = await f.run('deployment-active', { environmentUid: ENV, codeRepositoryBranchUid: BRANCH }); - assert.equal(result.status, 'success'); - const query = f.calls.find(call => call.path === '/api/v1/deployment-runs/').query; - assert.equal(query.state__in, 'queued,running'); assert.equal(query.code_repository_branch_uid, BRANCH); - assert.equal(result.data.count, 1); assert.equal(result.data.runs[0].uid, id(100)); assert.equal(result.summary, '1 deployment in progress.'); - const quiet = fixture({ runs: [run(1, API, 'succeeded')] }); - assert.equal((await quiet.run('deployment-active', { environmentUid: ENV })).summary, 'No deployments in progress.'); -}); - -test('a wait returns as soon as the run ends, and says so when it is still running at the limit', async () => { - const done = fixture({ waitStates: ['running', 'succeeded'] }); - const started = Date.now(), finished = await done.run('deployment-wait', { runUid: id(100), timeoutSeconds: 2 }); - assert.equal(finished.status, 'success', finished.summary); assert.equal(finished.data.finished, true); assert.equal(finished.data.run.state, 'succeeded'); - assert.equal(finished.summary, 'Deployment succeeded.'); assert.ok(Date.now() - started < 4000); - const still = fixture({ waitStates: ['running'] }), waited = await still.run('deployment-wait', { runUid: id(100), timeoutSeconds: 1 }); - assert.equal(waited.data.finished, false); assert.match(waited.summary, /still running after \d+ s/); - assert.ok(still.reads.wait >= 2 && still.reads.wait <= 3, `reads ${still.reads.wait}`); - assert.equal((await still.run('deployment-wait', { runUid: id(100), timeoutSeconds: WAIT_LIMIT_SECONDS + 1 })).error.code, 'INVALID_REQUEST'); - assert.equal((await still.run('deployment-wait', { runUid: id(77), timeoutSeconds: 1 })).data.hidden, true); -}); - -test('a wait stops when its read is cancelled', async () => { - const controller = new AbortController(); - const api = createDeployments({ host: createNodeHost(), signal: controller.signal, env: { MAINSEQUENCE_ENDPOINT: backend, MAINSEQUENCE_ACCESS_TOKEN: 'synthetic-secret' }, - fetch: async url => new URL(url).pathname === '/api/v1/users/me/' ? Response.json({ username: 'fixture' }) : Response.json(run(0, API, 'running')) }); - setTimeout(() => controller.abort(), 300); - const started = Date.now(), result = await api.dispatch({ action: 'deployment-wait', backend, expectedAccount: 'fixture', runUid: id(100), timeoutSeconds: 60 }); - assert.equal(result.status, 'cancelled'); assert.ok(Date.now() - started < 3000); -}); - -test('a release with no run in the window still says it is deployed, when, and from which repository and branch', async () => { - const f = fixture(), result = await f.run('deployment-releases', { environmentUid: ENV, days: 1 }); - assert.equal(result.status, 'success', result.summary); - const reads = f.calls.filter(call => call.path !== '/api/v1/users/me/'); - assert.deepEqual(reads.map(call => call.path), ['/api/v1/resource-releases/', '/api/v1/deployment-runs/', '/api/v1/deployment-runs/', '/api/v1/code-repository-branches/'], 'at most two more requests'); - assert.ok(reads[1].query.created_after, 'the window read is bounded'); assert.equal(reads[2].query.state, 'succeeded'); assert.equal(reads[2].query.created_after, undefined); - assert.deepEqual(reads[2].query.target_uid__in.split(','), [API, AGENT, SITE]); assert.equal(reads[3].query.uid__in, BRANCH); - const [api, agent, site] = result.data.results; - assert.deepEqual(api.recent, [], 'nothing ran in the last day'); assert.equal(api.lastRun, null); - assert.equal(api.deployed, true); assert.equal(api.lastDeploy.uid, id(101)); assert.equal(api.lastDeploy.finishedAt, iso(-60 + 4)); - assert.deepEqual([api.repositoryName, api.branchName], ['pricing-engine', 'main']); - assert.equal(agent.deployed, false, 'no active revision'); assert.equal(agent.lastDeploy, null); - assert.deepEqual([agent.repositoryName, agent.branchName], ['pricing-engine', 'main'], 'named from its branch when it never deployed'); - assert.equal(site.lastDeploy.uid, id(105)); -}); - -test('a deployed release the batched lookup leaves out is read on its own; a branch it may not read leaves the name blank', async () => { - const f = fixture({ batchLimit: 1, siteDeployed: true, branchStatus: 403 }), result = await f.run('deployment-releases', { environmentUid: ENV, days: 1 }); - assert.equal(result.status, 'success', result.summary); - const lookups = f.calls.filter(call => call.path === '/api/v1/deployment-runs/' && call.query.state === 'succeeded'); - assert.equal(lookups.length, 2); assert.equal(lookups[1].query.target_uid__in, SITE); assert.equal(lookups[1].query.limit, '1'); - const [api, agent, site] = result.data.results; - assert.equal(api.lastDeploy.uid, id(101)); assert.equal(site.lastDeploy.uid, id(105), 'found by its own read'); - assert.equal(agent.lastDeploy, null, 'a release never deployed is not read on its own'); - assert.deepEqual([agent.repositoryName, agent.branchName], ['', ''], 'a refused branch read does not fail the list'); -});