Files
sencho/backend/src/services/composeProjectContext.ts
T
Anso 3f1f15a6f4 fix: keep running containers until stack pull/build succeeds (#1657)
* fix: keep running containers until stack pull/build succeeds

Acquire images before reconcile, capture a recovery generation for
compensation, and only remove classified orphans after handoff.

* fix: address recovery audit blockers for safe stack updates

Retire abandoned and expired recovery artifacts, probe compensated
runtimes before reporting rollback success, preserve local Docker when
deleting a node, validate the exact Compose invocation before capture,
and repair updateStack return-contract fixtures.

* fix: resolve ESLint errors blocking CI on this branch

Unused-import and unused-variable errors left over from the stack
deletion refactor: MeshService in stacks.ts (its opt-out cascade moved
into DeployedStackDeletionService), a redundant pruneVolumes
destructure in deleteDeployedStack (the real one is re-derived from
the same input object inside runDeletionBody), and an unused beforeAll
import in a Docker-integration test stub. Also scopes the webhook
pull-action case body in a block to satisfy no-case-declarations;
purely syntactic, no behavior change.

* fix: harden recovery probe, cleanup retry, and failed-pull Docker test

Reject absent or unhealthy expected replicas before reporting rollback
success, keep cleanup records until artifacts are actually removed, fail
closed when a mesh override cannot be generated, and assert a real
failed pull leaves the original container running.

* fix: verify recovery probe image identity and stack-scoped override paths

Reject recovered runtimes that use the wrong image or leave scale-zero
services running, and confine tombstone override deletion to the intent
stack directory so forged cross-stack paths cannot be swept.

* test: batch notification cap fixtures in a SQLite transaction

Unbatched 1200-row inserts were timing out at the default 30s under
CI load even though the same assertions pass in under 2s when green.
2026-07-21 12:18:01 -04:00

95 lines
3.3 KiB
TypeScript

/**
* Thin Compose project context for safe full-stack updates.
*
* Wraps the current authored compose argument path and atomic file backup/restore.
* When a richer shared Compose project context lands, migrate callers to that type;
* this module must not become a competing full-manifest resolver.
*/
import path from 'path';
import { randomUUID } from 'crypto';
import { ComposeService } from './ComposeService';
import { FileSystemService } from './FileSystemService';
import { buildEffectiveServiceModel } from './effectiveServiceModel';
import { getErrorMessage } from '../utils/errors';
export type ImageReferenceKind = 'moving_tag' | 'digest_pinned' | 'none';
const DIGEST_PIN_PATTERN = /@sha256:[a-f0-9]{64}$/i;
export function classifyReferenceKind(declaredImageRef: string | null): ImageReferenceKind {
if (!declaredImageRef) return 'none';
if (DIGEST_PIN_PATTERN.test(declaredImageRef)) return 'digest_pinned';
return 'moving_tag';
}
async function requireRenderableModel(nodeId: number, stackName: string) {
const model = await buildEffectiveServiceModel(nodeId, stackName);
if (!model.renderable) {
throw new Error(model.error || 'Effective Compose model failed to render');
}
return model;
}
export interface ComposeProjectContext {
readonly nodeId: number;
readonly stackName: string;
readonly stackDir: string;
backupSlotId: string | null;
toComposeArgs(action: string[]): Promise<string[]>;
validateForMutation(): Promise<void>;
backupFromContext(operation: 'update' | 'deployment'): Promise<string>;
restoreFromContext(): Promise<void>;
resolveServiceImageMap(): Promise<Map<string, string | null>>;
}
class AuthoredComposeProjectContext implements ComposeProjectContext {
backupSlotId: string | null = null;
constructor(
readonly nodeId: number,
readonly stackName: string,
readonly stackDir: string,
) {}
async toComposeArgs(action: string[]): Promise<string[]> {
return ComposeService.getInstance(this.nodeId).buildAuthoredComposeArgs(this.stackName, action);
}
async validateForMutation(): Promise<void> {
await ComposeService.getInstance(this.nodeId).validateStackForMutation(this.stackName);
await requireRenderableModel(this.nodeId, this.stackName);
}
async backupFromContext(_operation: 'update' | 'deployment'): Promise<string> {
await FileSystemService.getInstance(this.nodeId).backupStackFiles(this.stackName);
const slotId = randomUUID();
this.backupSlotId = slotId;
return slotId;
}
async restoreFromContext(): Promise<void> {
await FileSystemService.getInstance(this.nodeId).restoreStackFiles(this.stackName);
}
async resolveServiceImageMap(): Promise<Map<string, string | null>> {
const model = await requireRenderableModel(this.nodeId, this.stackName);
const map = new Map<string, string | null>();
for (const svc of model.services) {
map.set(svc.name, svc.declaredImage);
}
return map;
}
}
export async function resolveComposeProjectContext(
nodeId: number,
stackName: string,
): Promise<ComposeProjectContext> {
const stackDir = path.join(FileSystemService.getInstance(nodeId).getBaseDir(), stackName);
return new AuthoredComposeProjectContext(nodeId, stackName, stackDir);
}
export function describeContextError(error: unknown): string {
return getErrorMessage(error, 'Compose project context failed');
}