mirror of
https://github.com/Studio-Saelix/sencho.git
synced 2026-08-29 11:47:01 +00:00
feat: health-gated updates and rollback readiness (#1354)
* feat: classify stack deploy and update failures with suggested next actions Failed deploy and update responses now carry a failure classification (cause category, headline, and suggested next step) derived from the compose error output. The recovery panel and chip render the classification and include it in copied diagnostics, and gateway-style failures surface as a node-unreachable cause. * feat: add update and rollback readiness reports for stacks Before a manual update, Sencho now shows an advisory readiness verdict computed from the stored preflight result, open drift findings, live container health, the pending image change, the rollback backup slot, and node disk headroom. The Stack Dossier gains a rollback readiness section that states what a rollback can restore and explicitly discloses that volume and bind-mounted data are not covered. Toolbar and sidebar updates now share one update path, and admins can create a fleet snapshot from the readiness dialog before updating. Nodes that do not advertise the capability keep the direct update flow. * feat: observe stack health after updates with a post-deploy health gate After a deploy or update succeeds, Sencho now watches the stack for a configurable observation window and records a passed, failed, or unknown verdict: containers must stay running, healthchecks must report healthy, and restart loops or disappearing containers fail the gate. The deploy panel shows the observation live and holds off auto-closing until the verdict lands, a failed gate surfaces the existing recovery actions including rollback, and the stack timeline records update started and gate verdict events. Scheduled, webhook, bulk, and git-source updates are gated the same way; rollbacks and installs are deliberately not. The gate is observational only and can be tuned or disabled per node under host alert settings. * docs: document health-gated updates and rollback readiness New operator page covering the update readiness dialog, the post-update health gate and its settings, the rollback readiness disclosure, and classified failures, with cross-links from the atomic deployments and deploy progress pages. The API reference gains the readiness and health-gate endpoints, the healthGateId success field, and the failure classification schema on deploy and update error responses. * feat: withhold the success verdict while the health gate observes An update used to show a green Succeeded that a failed health gate then contradicted moments later. The deploy modal now reports Verifying health while the gate observes, shows success only when the gate passes, and makes a failed or unknown gate the headline result; success toasts soften to a verifying message while a gate runs. The mobile recovery card groups its actions behind one bottom-right Take action menu so it stays compact on a phone, with the classified cause still visible on the card. A successful image update now also counts as the last known-good marker in rollback readiness, and the docs gain screenshots of the readiness dialog, gate states, dossier section, and settings. * fix: harden log format strings and the env existence path check Log calls that interpolated the stack name into the console format string now use constant format strings with placeholder arguments, and envExists validates path containment inline at its filesystem access, matching the established patterns used elsewhere in the same files. * test: adapt deploy modal success specs to the post-deploy health gate The deploy feedback modal now withholds its success verdict while the health gate observes the new containers, showing "Verifying health" until the gate passes. The two success-path E2E tests waited for "Succeeded" within the gate's 90s default window and timed out. Shorten the observation window to the 15s minimum for these tests via the settings API, assert the verify-then-succeed sequence the modal actually renders, and restore the default window afterward so the test value does not leak into later runs. * fix: serialize health gate polling and harden gate observation Address race conditions in the post-update health gate found in review. Backend: the gate poller used setInterval, so a Docker observe slower than the 5s tick could overlap the next poll and corrupt the restart and missing-container accounting, and a wedged socket could leave a poll pending forever. Polling is now single-flight: each cycle self-schedules the next only after it settles, and the observe is bounded by an 8s timeout so a hung probe counts as a poll error and resolves the gate unknown after three in a row. Frontend: the gate poller could overlap requests, letting a slow earlier "observing" response overwrite an already-applied terminal verdict. It is now single-flight with a terminal latch, so a late response can never roll the UI back from passed or failed. Also reject a non-digit nodeId on the snapshot coverage route instead of letting parseInt coerce it, document that turning off the deploy progress panel opts out of the live gate UI while the gate still runs server-side, and add gate-coverage tests for the webhook, git source, and auto-update apply paths plus the new single-flight, observe-timeout, and recovery cases.
This commit is contained in:
@@ -106,6 +106,23 @@ export interface PreflightRunRow {
|
||||
created_by: string | null;
|
||||
}
|
||||
|
||||
/** One post-update health gate observation run. */
|
||||
export interface HealthGateRunRow {
|
||||
id: string;
|
||||
node_id: number;
|
||||
stack_name: string;
|
||||
/** Named trigger_action because TRIGGER is reserved in SQLite. */
|
||||
trigger_action: 'update' | 'deploy';
|
||||
status: 'observing' | 'passed' | 'failed' | 'unknown';
|
||||
reason: string | null;
|
||||
window_seconds: number;
|
||||
/** JSON array of per-container end states for display. */
|
||||
containers_json: string;
|
||||
started_at: number;
|
||||
ended_at: number | null;
|
||||
created_by: string | null;
|
||||
}
|
||||
|
||||
/** One finding within a stored preflight run. Never carries an environment value. */
|
||||
export interface PreflightFindingRow {
|
||||
id: string;
|
||||
@@ -914,6 +931,7 @@ export class DatabaseService {
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_snapshot_files_snapshot ON fleet_snapshot_files(snapshot_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_snapshot_files_node_stack ON fleet_snapshot_files(node_id, stack_name);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS audit_log (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -1272,6 +1290,22 @@ export class DatabaseService {
|
||||
CREATE INDEX IF NOT EXISTS idx_preflight_findings_run
|
||||
ON preflight_findings(run_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS health_gate_runs (
|
||||
id TEXT PRIMARY KEY,
|
||||
node_id INTEGER NOT NULL,
|
||||
stack_name TEXT NOT NULL,
|
||||
trigger_action TEXT NOT NULL CHECK (trigger_action IN ('update','deploy')),
|
||||
status TEXT NOT NULL CHECK (status IN ('observing','passed','failed','unknown')),
|
||||
reason TEXT,
|
||||
window_seconds INTEGER NOT NULL,
|
||||
containers_json TEXT NOT NULL DEFAULT '[]',
|
||||
started_at INTEGER NOT NULL,
|
||||
ended_at INTEGER,
|
||||
created_by TEXT
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_health_gate_runs_node_stack
|
||||
ON health_gate_runs(node_id, stack_name, started_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS secrets (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
@@ -1400,6 +1434,8 @@ export class DatabaseService {
|
||||
stmt.run('mesh_auto_recreate', '0');
|
||||
stmt.run('prune_on_update', '1');
|
||||
stmt.run('reclaim_hero', '1');
|
||||
stmt.run('health_gate_enabled', '1');
|
||||
stmt.run('health_gate_window_seconds', '90');
|
||||
|
||||
// Seed the default local node if none exists
|
||||
const nodeCount = (this.db.prepare('SELECT COUNT(*) as count FROM nodes').get() as any)?.count || 0;
|
||||
@@ -2301,6 +2337,61 @@ export class DatabaseService {
|
||||
).all(runId) as PreflightFindingRow[];
|
||||
}
|
||||
|
||||
// --- Health Gate Runs ---
|
||||
|
||||
public insertHealthGateRun(run: HealthGateRunRow): void {
|
||||
this.db.prepare(
|
||||
`INSERT INTO health_gate_runs
|
||||
(id, node_id, stack_name, trigger_action, status, reason, window_seconds, containers_json, started_at, ended_at, created_by)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
|
||||
).run(
|
||||
run.id, run.node_id, run.stack_name, run.trigger_action, run.status, run.reason,
|
||||
run.window_seconds, run.containers_json, run.started_at, run.ended_at, run.created_by,
|
||||
);
|
||||
// Bounded history: keep only the 10 most recent runs per stack.
|
||||
this.db.prepare(
|
||||
`DELETE FROM health_gate_runs
|
||||
WHERE node_id = ? AND stack_name = ?
|
||||
AND id NOT IN (
|
||||
SELECT id FROM health_gate_runs
|
||||
WHERE node_id = ? AND stack_name = ?
|
||||
ORDER BY started_at DESC, id DESC LIMIT 10
|
||||
)`
|
||||
).run(run.node_id, run.stack_name, run.node_id, run.stack_name);
|
||||
}
|
||||
|
||||
public finalizeHealthGateRun(
|
||||
id: string,
|
||||
status: 'passed' | 'failed' | 'unknown',
|
||||
reason: string | null,
|
||||
endedAt: number,
|
||||
containersJson: string,
|
||||
): void {
|
||||
this.db.prepare(
|
||||
'UPDATE health_gate_runs SET status = ?, reason = ?, ended_at = ?, containers_json = ? WHERE id = ?'
|
||||
).run(status, reason, endedAt, containersJson, id);
|
||||
}
|
||||
|
||||
public getHealthGateRun(nodeId: number, stackName: string, id: string): HealthGateRunRow | undefined {
|
||||
return this.db.prepare(
|
||||
'SELECT * FROM health_gate_runs WHERE node_id = ? AND stack_name = ? AND id = ?'
|
||||
).get(nodeId, stackName, id) as HealthGateRunRow | undefined;
|
||||
}
|
||||
|
||||
public getLatestHealthGateRun(nodeId: number, stackName: string): HealthGateRunRow | undefined {
|
||||
return this.db.prepare(
|
||||
'SELECT * FROM health_gate_runs WHERE node_id = ? AND stack_name = ? ORDER BY started_at DESC, id DESC LIMIT 1'
|
||||
).get(nodeId, stackName) as HealthGateRunRow | undefined;
|
||||
}
|
||||
|
||||
/** Finalize runs left observing by a previous process (startup sweep). */
|
||||
public markInterruptedHealthGateRuns(reason: string, endedAt: number): number {
|
||||
const result = this.db.prepare(
|
||||
"UPDATE health_gate_runs SET status = 'unknown', reason = ?, ended_at = ? WHERE status = 'observing'"
|
||||
).run(reason, endedAt);
|
||||
return result.changes;
|
||||
}
|
||||
|
||||
// --- Notification History ---
|
||||
|
||||
private mapNotificationRow(row: any): NotificationHistory {
|
||||
@@ -2641,6 +2732,7 @@ export class DatabaseService {
|
||||
this.db.prepare('DELETE FROM stack_drift_findings WHERE node_id = ?').run(id);
|
||||
this.db.prepare('DELETE FROM preflight_findings WHERE run_id IN (SELECT id FROM preflight_runs WHERE node_id = ?)').run(id);
|
||||
this.db.prepare('DELETE FROM preflight_runs WHERE node_id = ?').run(id);
|
||||
this.db.prepare('DELETE FROM health_gate_runs WHERE node_id = ?').run(id);
|
||||
this.db.prepare('UPDATE blueprints SET pinned_node_id = NULL WHERE pinned_node_id = ?').run(id);
|
||||
this.deleteRoleAssignmentsByResource('node', String(id));
|
||||
this.db.prepare('DELETE FROM fleet_sync_status WHERE node_id = ?').run(id);
|
||||
@@ -3267,6 +3359,17 @@ export class DatabaseService {
|
||||
return rows.map(row => ({ ...row, content: crypto.decrypt(row.content) }));
|
||||
}
|
||||
|
||||
/** Created-at of the most recent fleet snapshot covering a stack, or null. */
|
||||
public getLatestSnapshotTimestampFor(nodeId: number, stackName: string): number | null {
|
||||
const row = this.db.prepare(
|
||||
`SELECT MAX(s.created_at) AS latest
|
||||
FROM fleet_snapshots s
|
||||
JOIN fleet_snapshot_files f ON f.snapshot_id = s.id
|
||||
WHERE f.node_id = ? AND f.stack_name = ?`
|
||||
).get(nodeId, stackName) as { latest: number | null } | undefined;
|
||||
return row?.latest ?? null;
|
||||
}
|
||||
|
||||
public deleteSnapshot(id: number): void {
|
||||
this.db.prepare('DELETE FROM fleet_snapshots WHERE id = ?').run(id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user