Files
pulse/server.js
T
jaredandClaude Opus 5.5 235d9240c0
CI / UI screenshots (before/after) (push) Skipped
CI / Lint (eslint) (push) Successful in 23s
CI / Test (jest) (push) Successful in 22s
CI / Security (npm audit) (push) Successful in 14s
CI / Scan (secrets, workflows, shell) (push) Failing after 18s
CI / Integration (api) (push) Successful in 2m43s
CI / Lint (eslint) (pull_request) Successful in 17s
CI / Test (jest) (pull_request) Successful in 18s
CI / Security (npm audit) (pull_request) Successful in 13s
CI / UI (playwright) (push) Successful in 3m0s
CI / Deploy (push) Skipped
CI / Deploy worker (push) Skipped
CI / Notify (push) Skipped
CI / Scan (secrets, workflows, shell) (pull_request) Failing after 14s
CI / Integration (api) (pull_request) Successful in 2m24s
CI / UI (playwright) (pull_request) Successful in 2m16s
CI / Deploy (pull_request) Skipped
CI / Deploy worker (pull_request) Skipped
CI / Notify (pull_request) Skipped
CI / UI screenshots (before/after) (pull_request) Successful in 2m18s
CI: eslint-plugin-security and zizmor; harden the workflow
- eslint-plugin-security 4.2.1 (recommended-legacy) with
  detect-object-injection off (it flags every obj[key]) and file/regex
  rules off for CI scripts and tests. The remaining hits were false
  positives with a one-line reason each (fixed page names, the worker's
  own directory, a null check), plus one real tidy-up: the condition
  tokenizer's number regex is now two flat patterns with no backtracking,
  and malformed numbers like 1.2.3 are refused.
- zizmor 1.32.0 in the Scan job. Its findings are fixed:
  persist-credentials: false on every checkout (no job pushes), container
  images pinned by digest (tag kept in the reference), and the deploy tag
  step takes github.* values through env instead of template expansion.
  Left as is: secrets-outside-env (auditor persona; Gitea has no
  deployment environments).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 23:45:35 -05:00

2100 lines
76 KiB
JavaScript

const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const mysql = require('mysql2/promise');
const crypto = require('crypto');
const rateLimit = require('express-rate-limit');
const path = require('path');
const fs = require('fs');
const helmet = require('helmet');
require('dotenv').config();
const { validateWebhookUrl, applyParams, evalCondition, conditionErrors, calculateNextRun } = require('./lib/utils');
const { PAGES } = require('./lib/nav');
const { renderPage, renderError } = require('./lib/render');
const { upsertUser, makeAuthenticatePage, ssoIdentity, trustedProxies, ALLOWED_GROUPS } = require('./lib/pageauth');
const { safeEqual, checkWorkerKey, workerAuthEnforced, classifyUpgrade, allowWorkerConnect } = require('./lib/wsauth');
const app = express();
const server = http.createServer(app);
// Authenticate every WebSocket upgrade (see lib/wsauth.js). Rejected handshakes get a 401
// before the upgrade completes; the classification is kept on the request for 'connection'.
const wss = new WebSocket.Server({
server,
verifyClient: ({ req }) => {
const kind = classifyUpgrade(req);
req.pulseKind = kind;
if (kind === 'reject') {
const addr = (req.socket && req.socket.remoteAddress) || '';
console.warn(`[Security] WebSocket upgrade rejected from ${JSON.stringify(addr)}`);
}
return kind !== 'reject';
}
});
// Dev/read-only guard: skip every background job that writes to the database.
const DEV_READONLY = process.env.PULSE_DEV_READONLY === '1' || process.env.DISABLE_BACKGROUND_JOBS === '1';
// Middleware
// CSP nonce: must run before helmet so the directive can read it.
app.use((req, res, next) => {
res.locals.nonce = crypto.randomBytes(16).toString('base64');
next();
});
const CSP_REPORT_ONLY = process.env.PULSE_CSP_REPORT_ONLY === '1';
const cspDirectives = {
defaultSrc: ["'self'"],
scriptSrc: ["'self'", (req, res) => `'nonce-${res.locals.nonce}'`],
scriptSrcAttr: ["'none'"],
styleSrcElem: ["'self'", 'https://fonts.googleapis.com'],
styleSrcAttr: ["'unsafe-inline'"],
styleSrc: ["'self'", 'https://fonts.googleapis.com', "'unsafe-inline'"],
fontSrc: ["'self'", 'https://fonts.gstatic.com', 'data:'],
imgSrc: ["'self'", 'data:'],
connectSrc: ["'self'", 'ws:', 'wss:'],
objectSrc: ["'none'"],
baseUri: ["'self'"],
frameAncestors: ["'none'"],
formAction: ["'self'"],
upgradeInsecureRequests: null
};
if (CSP_REPORT_ONLY) {
cspDirectives.reportUri = ['/csp-report'];
}
app.use(helmet({
contentSecurityPolicy: {
useDefaults: false,
directives: cspDirectives,
reportOnly: CSP_REPORT_ONLY
},
crossOriginEmbedderPolicy: false,
hsts: process.env.NODE_ENV === 'production' ? undefined : false
}));
app.use(express.json());
// Rate limiting. The limits can be raised for test stacks (the UI tests make a
// few hundred requests from one address); production uses the defaults.
const limitFromEnv = (name, fallback) => {
const n = parseInt(process.env[name], 10);
return Number.isInteger(n) && n > 0 ? n : fallback;
};
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: limitFromEnv('PULSE_API_RATE_MAX', 300),
standardHeaders: true,
legacyHeaders: false,
message: { error: 'Too many requests, please try again later' }
});
const executionLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: limitFromEnv('PULSE_EXEC_RATE_MAX', 20),
standardHeaders: true,
legacyHeaders: false,
message: { error: 'Too many execution requests, please slow down' }
});
// Named constants for timeouts and limits
const PROMPT_TIMEOUT_MS = 60 * 60 * 1000; // 60 min: how long a prompt waits for user input
const COMMAND_TIMEOUT_MS = 120_000; // 2 min: workflow step command timeout
const QUICK_CMD_TIMEOUT_MS = 60_000; // 1 min: quick/direct/gandalf command timeout
const WEBHOOK_TIMEOUT_MS = 10_000; // 10 s: outbound webhook HTTP timeout
const WAIT_STEP_MAX_MS = 24 * 60 * 60_000; // 24 h: cap on workflow wait step
const GOTO_MAX_VISITS = 100; // max times a step may be revisited via goto
const WORKER_STALE_MINUTES = 5; // minutes before a worker is marked offline
const SERVER_VERSION = '1.0.0';
// Request logging middleware
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
console.log(`[HTTP] ${req.method} ${req.path} ${res.statusCode} ${Date.now() - start}ms`);
});
next();
});
// CSP violation reports (only useful with PULSE_CSP_REPORT_ONLY=1, but always mounted)
app.post('/csp-report',
express.json({ type: ['application/csp-report', 'application/reports+json', 'application/json'] }),
(req, res) => {
try {
console.log('[CSP]', JSON.stringify(req.body));
} catch (_) {
console.log('[CSP] (unparseable report)');
}
res.status(204).end();
});
// Static assets: the old root mount of public/ is gone on purpose.
app.use('/web_template', express.static(path.join(__dirname, 'public/web_template'), { maxAge: '1h' }));
app.use('/assets', express.static(path.join(__dirname, 'public/assets'), { maxAge: '1h' }));
app.use('/api/', apiLimiter);
// Content-Type guard for JSON endpoints
function requireJSON(req, res, next) {
const ct = req.headers['content-type'] || '';
if (!ct.includes('application/json')) {
return res.status(415).json({ error: 'Content-Type must be application/json' });
}
next();
}
// Validate and parse a webhook URL; returns { ok, url, reason }
// validateWebhookUrl, applyParams, evalCondition, calculateNextRun imported from ./lib/utils
// Database pool
const pool = mysql.createPool({
host: process.env.DB_HOST,
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
waitForConnections: true,
connectionLimit: 50,
queueLimit: 0
});
// Initialize database tables
async function initDatabase() {
const connection = await pool.getConnection();
try {
await connection.query(`
CREATE TABLE IF NOT EXISTS users (
id VARCHAR(36) PRIMARY KEY,
username VARCHAR(255) UNIQUE NOT NULL,
display_name VARCHAR(255),
email VARCHAR(255),
groups TEXT,
last_login TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
// Database schema is managed manually - migrations removed after direct database fixes
await connection.query(`
CREATE TABLE IF NOT EXISTS workers (
id VARCHAR(36) PRIMARY KEY,
name VARCHAR(255) UNIQUE NOT NULL,
status VARCHAR(50) NOT NULL,
last_heartbeat TIMESTAMP NULL,
api_key VARCHAR(255),
metadata JSON,
INDEX idx_status (status),
INDEX idx_heartbeat (last_heartbeat)
)
`);
await connection.query(`
CREATE TABLE IF NOT EXISTS workflows (
id VARCHAR(36) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
definition JSON NOT NULL,
webhook_url VARCHAR(500) NULL,
created_by VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_name (name)
)
`);
// Add webhook_url to existing workflows table if missing
await connection.query(`
ALTER TABLE workflows ADD COLUMN IF NOT EXISTS webhook_url VARCHAR(500) NULL
`).catch(() => {});
await connection.query(`
CREATE TABLE IF NOT EXISTS executions (
id VARCHAR(36) PRIMARY KEY,
workflow_id VARCHAR(36) NULL,
status VARCHAR(50) NOT NULL,
started_by VARCHAR(255),
started_at TIMESTAMP NULL,
completed_at TIMESTAMP NULL,
logs JSON,
INDEX idx_workflow (workflow_id),
INDEX idx_status (status),
INDEX idx_started (started_at)
)
`);
await connection.query(`
CREATE TABLE IF NOT EXISTS scheduled_commands (
id VARCHAR(36) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
command TEXT NOT NULL,
worker_ids JSON NOT NULL,
schedule_type VARCHAR(50) NOT NULL,
schedule_value VARCHAR(255) NOT NULL,
enabled BOOLEAN DEFAULT TRUE,
created_by VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_run TIMESTAMP NULL,
next_run TIMESTAMP NULL,
INDEX idx_enabled (enabled),
INDEX idx_next_run (next_run)
)
`);
// Recover stale executions from a previous server crash
if (!DEV_READONLY) {
const [staleExecs] = await connection.query("SELECT id FROM executions WHERE status = 'running'");
if (staleExecs.length > 0) {
for (const exec of staleExecs) {
await connection.query(
"UPDATE executions SET status = 'failed', completed_at = NOW() WHERE id = ?",
[exec.id]
);
await connection.query(
"UPDATE executions SET logs = JSON_ARRAY_APPEND(COALESCE(logs, '[]'), '$', JSON_EXTRACT(?, '$')) WHERE id = ?",
[JSON.stringify({ action: 'server_restart_recovery', message: 'Execution marked failed due to server restart', timestamp: new Date().toISOString() }), exec.id]
);
}
console.log(`[Recovery] Marked ${staleExecs.length} stale execution(s) as failed`);
}
} else {
console.log('[DEV] skipped: stale-execution recovery');
}
console.log('Database tables initialized successfully');
} catch (error) {
console.error('Database initialization error:', error);
throw error;
} finally {
connection.release();
}
}
// Auto-cleanup old executions (runs hourly)
async function cleanupOldExecutions() {
try {
const retentionDays = parseInt(process.env.EXECUTION_RETENTION_DAYS) || 30;
const [result] = await pool.query(
`DELETE FROM executions
WHERE status IN ('completed', 'failed')
AND started_at < DATE_SUB(NOW(), INTERVAL ? DAY)`,
[retentionDays]
);
if (result.affectedRows > 0) {
console.log(`[Cleanup] Removed ${result.affectedRows} executions older than ${retentionDays} day(s)`);
}
} catch (error) {
console.error('[Cleanup] Error removing old executions:', error);
}
}
if (!DEV_READONLY) {
// Run cleanup hourly
setInterval(cleanupOldExecutions, 60 * 60 * 1000);
// Run cleanup on startup
cleanupOldExecutions();
} else {
console.log('[DEV] skipped: cleanupOldExecutions interval + startup run');
}
// Scheduled Commands Processor
async function processScheduledCommands() {
try {
const [schedules] = await pool.query(
`SELECT * FROM scheduled_commands
WHERE enabled = TRUE
AND (next_run IS NULL OR next_run <= NOW())`
);
for (const schedule of schedules) {
// Atomically claim this run by advancing next_run before doing any work.
// If two scheduler instances race, only the one that updates a row proceeds.
const claimNextRun = calculateNextRun(schedule.schedule_type, schedule.schedule_value);
const [claimed] = await pool.query(
`UPDATE scheduled_commands SET next_run = ?, last_run = NOW()
WHERE id = ? AND (next_run IS NULL OR next_run <= NOW())`,
[claimNextRun, schedule.id]
);
if (claimed.affectedRows === 0) {
console.log(`[Scheduler] Skipping "${schedule.name}" - already claimed by another run`);
continue;
}
// Also skip if a previous run is still active
const [runningExecs] = await pool.query(
"SELECT id FROM executions WHERE started_by = ? AND status = 'running'",
[`scheduler:${schedule.name}`]
);
if (runningExecs.length > 0) {
console.log(`[Scheduler] Skipping "${schedule.name}" - previous execution still running`);
continue;
}
console.log(`[Scheduler] Running scheduled command: ${schedule.name}`);
// Handle both string (raw SQL) and object (auto-parsed by MySQL2 JSON column)
let workerIds;
try {
workerIds = typeof schedule.worker_ids === 'string'
? JSON.parse(schedule.worker_ids)
: schedule.worker_ids;
} catch {
console.error(`[Scheduler] Invalid worker_ids JSON for "${schedule.name}": skipping`);
continue;
}
// Execute command on each worker
for (const workerId of workerIds) {
const workerWs = workers.get(workerId);
if (workerWs && workerWs.readyState === WebSocket.OPEN) {
const executionId = crypto.randomUUID();
// Create execution record
await pool.query(
'INSERT INTO executions (id, workflow_id, status, started_by, started_at, logs) VALUES (?, ?, ?, ?, NOW(), ?)',
[executionId, null, 'running', `scheduler:${schedule.name}`, JSON.stringify([{
step: 'scheduled_command',
action: 'command_sent',
worker_id: workerId,
command: schedule.command,
timestamp: new Date().toISOString()
}])]
);
// Send command to worker
sendCommandToWorker(workerWs, {
type: 'execute_command',
execution_id: executionId,
command: schedule.command,
worker_id: workerId,
timeout: 5 * 60_000 // SCHEDULED_CMD_TIMEOUT
});
broadcast({ type: 'execution_started', execution_id: executionId, workflow_id: null });
}
}
// next_run and last_run already updated atomically above when we claimed the slot
}
} catch (error) {
console.error('[Scheduler] Error processing scheduled commands:', error);
}
}
if (!DEV_READONLY) {
// Run scheduler every minute (tests shorten it with PULSE_SCHEDULER_TICK_MS, minimum 1 s)
const tick = parseInt(process.env.PULSE_SCHEDULER_TICK_MS, 10);
setInterval(processScheduledCommands, Number.isInteger(tick) && tick >= 1000 ? tick : 60 * 1000);
// Initial run on startup
setTimeout(processScheduledCommands, 5000);
} else {
console.log('[DEV] skipped: processScheduledCommands interval + startup run');
}
// Mark workers offline when their heartbeat goes stale
async function markStaleWorkersOffline() {
try {
const [stale] = await pool.query(
`SELECT id FROM workers WHERE status = 'online'
AND last_heartbeat < DATE_SUB(NOW(), INTERVAL ? MINUTE)`,
[WORKER_STALE_MINUTES]
);
for (const w of stale) {
await pool.query(`UPDATE workers SET status='offline' WHERE id=?`, [w.id]);
broadcast({ type: 'worker_update', worker_id: w.id, status: 'offline' });
console.log(`[Worker] Marked stale worker ${w.id} offline`);
}
} catch (error) {
console.error('[Worker] Stale check error:', error);
}
}
// Backstop for #7: fail any execution still 'running' long past the global limit
// that nothing in this process is tracking any more (no workflow engine loop, no
// command awaiting a result). Restart recovery covers executions orphaned by a restart.
async function reapOrphanedExecutions() {
try {
const maxMinutes = parseInt(process.env.EXECUTION_MAX_MINUTES) || 60;
const [rows] = await pool.query(
`SELECT id FROM executions WHERE status = 'running'
AND started_at < DATE_SUB(NOW(), INTERVAL ? MINUTE)`,
[maxMinutes + 10]
);
const tracked = new Set([..._commandTargets.values()].map(t => t.executionId));
for (const { id } of rows) {
if (_executionState.has(id) || tracked.has(id)) continue;
await addExecutionLog(id, {
action: 'execution_timeout',
message: `Execution was still running after ${maxMinutes + 10} minutes with nothing tracking it; marked failed`,
timestamp: new Date().toISOString()
});
if (await updateExecutionStatus(id, 'failed')) console.warn(`[Reaper] Failed orphaned execution ${id}`);
}
} catch (error) {
console.error('[Reaper] Error reaping orphaned executions:', error);
}
}
if (!DEV_READONLY) {
setInterval(markStaleWorkersOffline, 60_000);
setInterval(reapOrphanedExecutions, 5 * 60_000);
} else {
console.log('[DEV] skipped: markStaleWorkersOffline interval');
}
// WebSocket connections
const browserClients = new Set(); // Browser UI connections
const workerClients = new Set(); // Worker agent connections
const workers = new Map(); // Map worker_id -> WebSocket connection
// Which worker each in-flight command was sent to, keyed by command_id (workflow steps)
// or execution_id (quick/scheduled/internal commands). Results from any other
// connection are dropped, so a connection cannot forge another worker's output.
// The DB worker id is used rather than the socket so a worker that reconnects
// mid-command can still deliver its result.
const _commandTargets = new Map(); // key → { dbWorkerId, ws, executionId, direct, timer, sentAt }
const COMMAND_TARGET_TTL_MS = 25 * 60 * 60_000; // longer than any command or wait step
const COMMAND_RESULT_GRACE_MS = 30_000; // slack on top of the command's own timeout
function sendCommandToWorker(workerWs, message) {
const key = message.command_id || message.execution_id;
const target = {
dbWorkerId: workerWs.dbWorkerId,
ws: workerWs,
executionId: message.execution_id,
// Workflow steps carry a command_id and time out in waitForCommandResult.
// Direct commands (quick, scheduled, Gandalf) had no server-side timeout and
// could stay 'running' forever when the result never came back (#7).
direct: !message.command_id,
timer: null,
sentAt: Date.now()
};
if (target.direct) {
const ms = (message.timeout || QUICK_CMD_TIMEOUT_MS) + COMMAND_RESULT_GRACE_MS;
target.timer = setTimeout(() => {
failInflightCommand(key, `No result from the worker within ${Math.round(ms / 1000)}s`)
.catch(err => console.error('[Command] Timeout handling failed:', err));
}, ms);
}
_commandTargets.set(key, target);
workerWs.send(JSON.stringify(message));
}
// Give up on an in-flight command: the worker timed out, disconnected, or never answered.
// Direct commands get a failed command_result log entry (rendered like any other failed
// result, and seen by Gandalf as status 'failed'); workflow steps get their pending
// waitForCommandResult resolved as a failure so the engine moves on immediately.
async function failInflightCommand(key, error) {
const t = _commandTargets.get(key);
if (!t) return;
_commandTargets.delete(key);
clearTimeout(t.timer);
console.warn(`[Command] ${t.direct ? 'Execution' : 'Workflow command'} ${key} failed: ${error}`);
if (!t.direct) {
const resolver = _commandResolvers.get(key);
if (resolver) resolver({ success: false, error, stdout: '', stderr: '' });
return;
}
const timestamp = new Date().toISOString();
await addExecutionLog(t.executionId, {
step: 'command_execution', action: 'command_result', worker_id: t.dbWorkerId,
success: false, error, stdout: '', stderr: '', timestamp
});
if (await updateExecutionStatus(t.executionId, 'failed')) {
broadcast({
type: 'command_result', execution_id: t.executionId, worker_id: t.dbWorkerId,
success: false, error, stdout: '', stderr: ''
});
}
}
// Entries for commands whose result never arrives would otherwise accumulate.
setInterval(() => {
const cutoff = Date.now() - COMMAND_TARGET_TTL_MS;
for (const [key, t] of _commandTargets) {
if (t.sentAt < cutoff) _commandTargets.delete(key);
}
}, 60 * 60_000).unref();
const PENDING_IDENTIFY_MS = 30_000; // unauthenticated connections must identify as a worker within this
wss.on('connection', (ws, req) => {
ws.kind = req.pulseKind || 'pending';
ws.isWorker = false;
// Only authenticated browser sessions receive the live event feed.
if (ws.kind === 'browser') browserClients.add(ws);
if (ws.kind === 'pending') {
ws.identifyTimer = setTimeout(() => {
if (!ws.isWorker) ws.close(4001, 'Unauthorized');
}, PENDING_IDENTIFY_MS);
}
// Handle incoming messages from workers
ws.on('message', async (data) => {
try {
const message = JSON.parse(data.toString());
// Before identifying, a connection may only send worker_connect.
if (!ws.isWorker && message.type !== 'worker_connect') return;
if (message.type === 'command_result') {
// Handle command result from worker
const { execution_id, command_id, worker_id, success, stdout, stderr, duration, timestamp } = message;
// Failure details from the worker (#14). Older workers send only `error`.
const failure = success ? {} : {
error: typeof message.error === 'string' ? message.error.slice(0, 2000) : undefined,
exit_code: message.exit_code,
signal: message.signal || undefined,
timed_out: message.timed_out || undefined
};
const targetKey = command_id || execution_id;
const target = _commandTargets.get(targetKey);
if (!target || target.dbWorkerId !== ws.dbWorkerId) {
console.warn(`[Security] Dropped command_result for ${JSON.stringify(targetKey)} from worker ` +
`${JSON.stringify(ws.dbWorkerId)}: ${target ? 'sent to a different worker' : 'no such command in flight'}`);
return;
}
_commandTargets.delete(targetKey);
clearTimeout(target.timer);
// Add result to execution logs
await addExecutionLog(execution_id, {
step: 'command_execution',
action: 'command_result',
command_id: command_id, // Include command_id for workflow tracking
worker_id: worker_id,
success: success,
stdout: stdout,
stderr: stderr,
...failure,
duration: duration,
timestamp: timestamp || new Date().toISOString()
});
// For non-workflow executions, update status immediately
// For workflow executions, the workflow engine will update status
const [execution] = await pool.query('SELECT workflow_id, started_by FROM executions WHERE id = ?', [execution_id]);
const startedBy = execution.length > 0 ? (execution[0].started_by || '') : '';
const isAutomated = startedBy.startsWith('gandalf:') || startedBy.startsWith('scheduler:');
if (execution.length > 0 && !execution[0].workflow_id) {
// Only update status for quick commands (no workflow_id)
const finalStatus = success ? 'completed' : 'failed';
await updateExecutionStatus(execution_id, finalStatus);
}
// Resolve any pending event-driven command promise (eliminates DB polling)
if (command_id) {
const resolver = _commandResolvers.get(command_id);
if (resolver) {
resolver({ success, stdout, stderr, ...failure });
}
}
// Store last command output in execution state so prompt steps can surface it
const execStateForOutput = _executionState.get(execution_id);
if (execStateForOutput) {
execStateForOutput.state._lastCommandOutput = stdout || '';
}
// Broadcast result to browser clients only
broadcast({
type: 'command_result',
execution_id: execution_id,
worker_id: worker_id,
success: success,
stdout: stdout,
stderr: stderr,
...failure,
is_automated: isAutomated,
});
if (!success) console.warn(`[Worker] Command failed for execution ${execution_id} on worker ${worker_id}`);
}
if (message.type === 'workflow_result') {
// Handle workflow result from worker
const { execution_id, worker_id, success, message: resultMessage, timestamp } = message;
// Add final result to logs
await addExecutionLog(execution_id, {
step: 'workflow_completion',
action: 'workflow_result',
worker_id: worker_id,
success: success,
message: resultMessage,
timestamp: timestamp || new Date().toISOString()
});
// Update execution status
const finalStatus = success ? 'completed' : 'failed';
await updateExecutionStatus(execution_id, finalStatus);
// Broadcast completion to all clients
broadcast({
type: 'workflow_result',
execution_id: execution_id,
status: finalStatus,
success: success,
message: resultMessage
});
console.log(`Workflow result received for execution ${execution_id}: ${finalStatus}`);
}
if (message.type === 'worker_connect') {
const { worker_id, worker_name, api_key } = message;
if (ws.isWorker) return; // already identified; ignore repeats
const auth = allowWorkerConnect(ws.kind, api_key);
if (!auth.ok) {
console.warn(`[Security] Worker connection rejected: missing or invalid API key from ${JSON.stringify(worker_name)}`);
ws.close(4001, 'Unauthorized');
return;
}
if (auth.legacy) {
console.warn(`[Security] Worker ${JSON.stringify(worker_name)} connected without an API key. ` +
'Update the worker; keyless workers are refused once WORKER_AUTH_ENFORCE=1.');
}
clearTimeout(ws.identifyTimer);
ws.isWorker = true;
// Reported by the worker (#40); only a commit sha is accepted, anything else is 'unknown'.
ws.workerVersion = typeof message.version === 'string' && /^[0-9a-f]{40}$/.test(message.version)
? message.version : 'unknown';
// Move from browser set to worker set
browserClients.delete(ws);
workerClients.add(ws);
console.log(`Worker connected: ${worker_name} (${worker_id}), version ${ws.workerVersion}`);
// Find the database worker ID by name
const [dbWorkers] = await pool.query(
'SELECT id FROM workers WHERE name = ?',
[worker_name]
);
if (dbWorkers.length > 0) {
const dbWorkerId = dbWorkers[0].id;
// Clean up any stale entry for this db worker before storing the new one
// (handles reconnect: old runtime-ID entry would otherwise linger).
for (const [key, val] of workers) {
if (val.dbWorkerId === dbWorkerId && val !== ws) workers.delete(key);
}
// Store worker WebSocket connection using BOTH IDs
workers.set(worker_id, ws); // Runtime ID
workers.set(dbWorkerId, ws); // Database ID
// Store mapping for cleanup
ws.workerId = worker_id;
ws.dbWorkerId = dbWorkerId;
console.log(`Mapped worker: runtime_id=${worker_id}, db_id=${dbWorkerId}, name=${worker_name}`);
// Update worker status to online
await pool.query(
`UPDATE workers SET status='online', last_heartbeat=NOW() WHERE id=?`,
[dbWorkerId]
);
// Broadcast worker status update with database ID
broadcast({
type: 'worker_update',
worker_id: dbWorkerId,
status: 'online'
});
} else {
console.log(`Worker ${worker_name} not found in database, will be created on heartbeat`);
}
}
if (message.type === 'pong') {
// Only the DB worker ID bound at worker_connect; never a client-supplied id.
const dbId = ws.dbWorkerId;
if (dbId) {
await pool.query(
`UPDATE workers SET last_heartbeat=NOW() WHERE id=?`,
[dbId]
);
}
}
} catch (error) {
console.error('WebSocket message error:', error);
}
});
ws.on('close', () => {
clearTimeout(ws.identifyTimer);
// Commands sent over this socket can no longer report back (the worker drops
// results while disconnected), so fail them now rather than leave them 'running'.
if (ws.isWorker) {
for (const [key, t] of _commandTargets) {
if (t.ws === ws) {
failInflightCommand(key, 'Worker disconnected before returning a result')
.catch(err => console.error('[Command] Disconnect handling failed:', err));
}
}
}
browserClients.delete(ws);
workerClients.delete(ws);
// Only if this socket is still the live connection for the worker: after a
// reconnect, the old socket closing must not unmap the new one or mark it offline.
// Decide before unmapping anything: the runtime and database ids are usually the
// same key, so deleting one first made the database check always fail and left
// disconnected workers 'online'.
const liveForDb = !!ws.dbWorkerId && workers.get(ws.dbWorkerId) === ws;
if (ws.workerId && workers.get(ws.workerId) === ws) {
workers.delete(ws.workerId);
console.log(`Worker ${ws.workerId} (runtime ID) disconnected`);
}
if (liveForDb) {
workers.delete(ws.dbWorkerId);
console.log(`Worker ${ws.dbWorkerId} (database ID) disconnected`);
// Mark worker offline in DB
pool.query(`UPDATE workers SET status='offline' WHERE id=?`, [ws.dbWorkerId])
.then(() => broadcast({ type: 'worker_update', worker_id: ws.dbWorkerId, status: 'offline' }))
.catch(err => console.error('[Worker] Failed to mark worker offline:', err));
}
});
});
// Broadcast to browser clients only (NOT worker agents)
function broadcast(data) {
// Snapshot the Set before iterating: a close event during iteration would
// otherwise modify the Set in-place, causing skipped or double-visited entries.
const snapshot = Array.from(browserClients);
snapshot.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(data));
}
});
}
// Helper function to add log entry to execution (atomic: no read-modify-write race condition)
async function addExecutionLog(executionId, logEntry) {
try {
await pool.query(
"UPDATE executions SET logs = JSON_ARRAY_APPEND(COALESCE(logs, '[]'), '$', JSON_EXTRACT(?, '$')) WHERE id = ?",
[JSON.stringify(logEntry), executionId]
);
} catch (error) {
console.error(`[Workflow] Error adding execution log:`, error);
}
}
// Helper function to update execution status
// Only a running execution changes status: once failed (aborted, timed out, worker
// gone) or completed, a late result or the workflow engine finishing can't flip it
// back or fire a second webhook. Returns true if the status changed.
async function updateExecutionStatus(executionId, status) {
try {
const [result] = await pool.query(
"UPDATE executions SET status = ?, completed_at = NOW() WHERE id = ? AND status = 'running'",
[status, executionId]
);
if (!result || result.affectedRows === 0) {
console.log(`[Workflow] Execution ${executionId} is no longer running; not changing status to ${status}`);
return false;
}
broadcast({
type: 'execution_status',
execution_id: executionId,
status: status
});
console.log(`[Workflow] Execution ${executionId} status updated to: ${status}`);
// Fire webhook if configured on the workflow and execution reached a terminal state
if (status === 'completed' || status === 'failed') {
fireWebhook(executionId, status).catch(err => {
console.error(`[Webhook] Delivery error for execution ${executionId}:`, err.message);
});
}
return true;
} catch (error) {
console.error(`[Workflow] Error updating execution status:`, error);
return false;
}
}
// Fire optional webhook on workflow completion/failure
async function fireWebhook(executionId, status) {
const [rows] = await pool.query(
`SELECT e.id, e.workflow_id, e.started_by, e.started_at, e.completed_at,
w.webhook_url, w.name as workflow_name
FROM executions e
LEFT JOIN workflows w ON e.workflow_id = w.id
WHERE e.id = ?`,
[executionId]
);
if (!rows.length || !rows[0].webhook_url) return;
const exec = rows[0];
const { ok, url, reason } = validateWebhookUrl(exec.webhook_url);
if (!ok) {
console.warn(`[Webhook] Skipping invalid stored URL for execution ${executionId}: ${reason}`);
return;
}
const payload = {
execution_id: executionId,
workflow_id: exec.workflow_id,
workflow_name: exec.workflow_name,
status,
started_by: exec.started_by,
started_at: exec.started_at,
completed_at: exec.completed_at,
timestamp: new Date().toISOString()
};
const response = await fetch(exec.webhook_url, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'User-Agent': 'PULSE-Webhook/1.0' },
body: JSON.stringify(payload),
signal: AbortSignal.timeout(WEBHOOK_TIMEOUT_MS)
});
console.log(`[Webhook] ${url.hostname} responded ${response.status} for execution ${executionId}`);
}
// Authelia SSO Middleware
async function authenticateSSO(req, res, next) {
// Authelia headers, only honoured from TRUSTED_PROXIES (see lib/pageauth.js)
const identity = ssoIdentity(req);
if (!identity) {
return res.status(401).json({
error: 'Not authenticated',
message: 'Please access this service through auth.lotusguild.org'
});
}
const { username: remoteUser, name: remoteName, email: remoteEmail, groups } = identity;
// Only the groups in ALLOWED_GROUPS (lib/pageauth.js): admins.
const hasAccess = groups.some(g => ALLOWED_GROUPS.includes(g));
if (!hasAccess) {
return res.status(403).json({
error: 'Access denied',
message: 'Pulse is limited to the admin group'
});
}
// Store/update user in database
try {
await upsertUser(pool, req.headers);
} catch (error) {
console.error('Error updating user:', error);
}
// Attach user info to request
req.user = {
username: remoteUser,
name: remoteName || remoteUser,
email: remoteEmail || '',
groups: groups,
isAdmin: groups.includes('admin')
};
next();
}
// Gandalf machine-to-machine API key auth
function authenticateGandalf(req, res, next) {
const apiKey = req.headers['x-gandalf-api-key'];
if (!safeEqual(apiKey, process.env.GANDALF_API_KEY)) {
return res.status(401).json({ error: 'Unauthorized' });
}
req.user = { username: 'gandalf:link_stats', isAdmin: false };
next();
}
// Workflow Execution Engine
// Substitute {{param_name}} placeholders in a command string.
// Only alphanumeric + safe punctuation allowed in substituted values.
// Per-execution mutable state (params + user-keyed state dict).
// Survives across step boundaries; cleaned up when execution ends.
const _executionState = new Map(); // executionId → { params, state }
// Pending prompt resolvers: set when a prompt step is waiting for user input.
const _executionPrompts = new Map(); // executionId → resolve fn
const _commandResolvers = new Map(); // commandId → resolve fn (event-driven result delivery)
async function executeWorkflowSteps(executionId, workflowId, definition, username, params = {}, dryRun = false) {
_executionState.set(executionId, { params, state: {} });
// Global execution timeout
const maxMinutes = parseInt(process.env.EXECUTION_MAX_MINUTES) || 60;
const executionDeadline = Date.now() + maxMinutes * 60000;
try {
console.log(`[Workflow] Starting execution ${executionId} for workflow ${workflowId}${dryRun ? ' [DRY RUN]' : ''}`);
const steps = definition.steps || [];
// Build step-id → index map for goto support
const stepIdMap = new Map();
steps.forEach((step, i) => { if (step.id) stepIdMap.set(step.id, i); });
let currentIndex = 0;
const stepVisits = new Array(steps.length).fill(0);
while (currentIndex < steps.length) {
// Detect goto infinite loops
stepVisits[currentIndex] = (stepVisits[currentIndex] || 0) + 1;
if (stepVisits[currentIndex] > GOTO_MAX_VISITS) {
await addExecutionLog(executionId, {
action: 'workflow_error',
error: `Infinite loop detected at step ${currentIndex + 1} (visited ${stepVisits[currentIndex]} times)`,
timestamp: new Date().toISOString()
});
await updateExecutionStatus(executionId, 'failed');
return;
}
// Enforce global execution timeout
if (Date.now() > executionDeadline) {
await addExecutionLog(executionId, {
action: 'execution_timeout',
message: `Execution exceeded maximum runtime of ${maxMinutes} minutes`,
timestamp: new Date().toISOString()
});
await updateExecutionStatus(executionId, 'failed');
return;
}
const step = steps[currentIndex];
const execState = _executionState.get(executionId);
if (!execState) {
// State was lost (server restart or bug): fail cleanly rather than throwing TypeError
await addExecutionLog(executionId, {
action: 'workflow_error',
error: 'Execution state lost unexpectedly; aborting.',
timestamp: new Date().toISOString()
});
await updateExecutionStatus(executionId, 'failed');
return;
}
const stepLabel = step.name || step.id || `Step ${currentIndex + 1}`;
console.log(`[Workflow] ${executionId} step ${currentIndex + 1}: ${stepLabel}`);
await addExecutionLog(executionId, {
step: currentIndex + 1, step_name: stepLabel, action: 'step_started',
timestamp: new Date().toISOString()
});
broadcast({ type: 'workflow_step_started', execution_id: executionId,
step: currentIndex + 1, step_name: stepLabel });
let gotoId = step.goto || null; // may be overridden by prompt routes
if (step.type === 'execute') {
const condOk = !step.condition || evalCondition(step.condition, execState.state, execState.params);
if (condOk) {
if (dryRun) {
await addExecutionLog(executionId, {
step: currentIndex + 1, step_name: stepLabel, action: 'dry_run_skipped',
command: step.command, targets: step.targets || ['all'],
message: '[DRY RUN] Command not executed',
timestamp: new Date().toISOString()
});
} else {
const success = await executeCommandStep(executionId, step, currentIndex + 1, params);
if (!success) {
await updateExecutionStatus(executionId, 'failed');
return;
}
}
} else {
await addExecutionLog(executionId, {
step: currentIndex + 1, action: 'step_skipped',
reason: 'condition false', timestamp: new Date().toISOString()
});
}
} else if (step.type === 'prompt') {
const response = await executePromptStep(executionId, step, currentIndex + 1);
if (response !== null) {
execState.state[step.key || 'lastPrompt'] = response;
execState.state.promptResponse = response; // backwards compat
// Prompt routes override goto
if (step.routes && step.routes[response]) {
gotoId = step.routes[response];
}
}
} else if (step.type === 'wait') {
let ms = parseFloat(step.duration || 5) * 1000;
if (isNaN(ms) || ms < 0) ms = 5000;
if (ms > WAIT_STEP_MAX_MS) ms = WAIT_STEP_MAX_MS;
await addExecutionLog(executionId, {
step: currentIndex + 1, action: 'waiting', duration_ms: ms,
timestamp: new Date().toISOString()
});
await new Promise(r => setTimeout(r, ms));
} else if (step.type === 'parse') {
// Parse KEY=VALUE lines from last command output into execution state.
// Matches lines of the form: UPPER_KEY=value (key must start with uppercase letter)
const condOkParse = !step.condition || evalCondition(step.condition, execState?.state || {}, execState?.params || {});
const parseState = condOkParse ? _executionState.get(executionId) : null;
if (parseState) {
const output = parseState.state._lastCommandOutput || '';
const parsed = {};
for (const line of output.split('\n')) {
const m = line.match(/^([A-Z][A-Z0-9_]*)=(.*)$/);
if (m) {
const k = m[1].toLowerCase();
const v = m[2].trim();
parsed[k] = v;
parseState.state[k] = v;
}
}
await addExecutionLog(executionId, {
step: currentIndex + 1, step_name: stepLabel, action: 'parse_complete',
parsed_count: Object.keys(parsed).length, parsed,
timestamp: new Date().toISOString()
});
}
} else if (step.type === 'route') {
// Evaluate conditions in order; jump to the first match (no user input needed).
const routeState = execState?.state || {};
const routeParams = execState?.params || {};
let routeTaken = null;
for (const cond of (step.conditions || [])) {
const matches = cond.default || evalCondition(cond.if || 'false', routeState, routeParams);
if (matches) { routeTaken = cond; gotoId = cond.goto || null; break; }
}
await addExecutionLog(executionId, {
step: currentIndex + 1, step_name: stepLabel, action: 'route_taken',
condition: routeTaken?.if || 'default',
label: routeTaken?.label || null,
goto: gotoId,
timestamp: new Date().toISOString()
});
}
await addExecutionLog(executionId, {
step: currentIndex + 1, step_name: stepLabel, action: 'step_completed',
timestamp: new Date().toISOString()
});
broadcast({ type: 'workflow_step_completed', execution_id: executionId,
step: currentIndex + 1, step_name: stepLabel });
// Determine next step
if (gotoId === 'end' || gotoId === '__end__') {
break;
} else if (gotoId) {
if (stepIdMap.has(gotoId)) {
currentIndex = stepIdMap.get(gotoId);
} else {
await addExecutionLog(executionId, {
action: 'goto_error', target: gotoId,
message: `No step with id "${gotoId}" found`,
timestamp: new Date().toISOString()
});
break;
}
} else {
currentIndex++;
}
}
await updateExecutionStatus(executionId, 'completed');
console.log(`[Workflow] Execution ${executionId} completed`);
} catch (error) {
console.error(`[Workflow] Execution ${executionId} error:`, error);
await addExecutionLog(executionId, {
action: 'workflow_error', error: error.message,
timestamp: new Date().toISOString()
});
await updateExecutionStatus(executionId, 'failed');
} finally {
_executionState.delete(executionId);
_executionPrompts.delete(executionId);
}
}
// Pause execution and wait for user to respond via POST /api/executions/:id/respond.
// Resolves with the chosen option string, or null on 60-minute timeout.
async function executePromptStep(executionId, step, stepNumber) {
const execState = _executionState.get(executionId);
// Apply param substitution to the message so {{server_name}}, {{iface}} etc. work
let message = step.message || 'Please choose an option:';
if (execState && Object.keys(execState.params).length > 0) {
message = applyParams(message, execState.params);
}
const options = step.options || ['Continue'];
// Include the last command output so the user can review results alongside the question
const lastOutput = (execState?.state?._lastCommandOutput) || null;
const logEntry = {
step: stepNumber, step_name: step.name, action: 'prompt',
message, options, timestamp: new Date().toISOString()
};
if (lastOutput) logEntry.output = lastOutput;
await addExecutionLog(executionId, logEntry);
broadcast({
type: 'execution_prompt',
execution_id: executionId,
prompt: { message, options, step: stepNumber, step_name: step.name, output: lastOutput || undefined }
});
return new Promise(resolve => {
const timer = setTimeout(() => {
_executionPrompts.delete(executionId);
console.warn(`[Workflow] Prompt timed out for execution ${executionId}`);
resolve(null);
}, PROMPT_TIMEOUT_MS);
_executionPrompts.set(executionId, {
resolve: (response) => {
clearTimeout(timer);
_executionPrompts.delete(executionId);
resolve(response);
},
options
});
});
}
async function executeCommandStep(executionId, step, stepNumber, params = {}) {
try {
let command = step.command;
if (Object.keys(params).length > 0) {
command = applyParams(command, params);
}
const targets = step.targets || ['all'];
// Determine which workers to target
let targetWorkerIds = [];
if (targets.includes('all')) {
// Get all online workers
const [onlineWorkers] = await pool.query('SELECT id FROM workers WHERE status = ?', ['online']);
targetWorkerIds = onlineWorkers.map(w => w.id);
} else {
// Specific worker IDs or names
for (const target of targets) {
// Try to find by ID first, then by name
const [workerById] = await pool.query('SELECT id FROM workers WHERE id = ?', [target]);
if (workerById.length > 0) {
targetWorkerIds.push(workerById[0].id);
} else {
const [workerByName] = await pool.query('SELECT id FROM workers WHERE name = ?', [target]);
if (workerByName.length > 0) {
targetWorkerIds.push(workerByName[0].id);
}
}
}
}
if (targetWorkerIds.length === 0) {
await addExecutionLog(executionId, {
step: stepNumber,
action: 'no_workers',
message: 'No workers available for this step',
timestamp: new Date().toISOString()
});
return false;
}
// Execute command on each target worker and wait for results
const results = [];
let sentCount = 0;
for (const workerId of targetWorkerIds) {
const workerWs = workers.get(workerId);
if (!workerWs || workerWs.readyState !== WebSocket.OPEN) {
await addExecutionLog(executionId, {
step: stepNumber,
action: 'worker_offline',
worker_id: workerId,
timestamp: new Date().toISOString()
});
continue;
}
sentCount++;
// Send command to worker
let commandId = crypto.randomUUID();
await addExecutionLog(executionId, {
step: stepNumber,
action: 'command_sent',
worker_id: workerId,
command: command,
command_id: commandId,
timestamp: new Date().toISOString()
});
// Per-step timeout override: step.timeout (seconds) overrides global default
const stepTimeoutMs = step.timeout
? Math.min(Math.max(parseInt(step.timeout, 10) * 1000, 5000), 600000)
: COMMAND_TIMEOUT_MS;
sendCommandToWorker(workerWs, {
type: 'execute_command',
execution_id: executionId,
command_id: commandId,
command: command,
worker_id: workerId,
timeout: stepTimeoutMs
});
// Per-step retry: step.retries (default 0) with step.retryDelayMs (default 2000)
const maxRetries = Math.min(parseInt(step.retries || 0, 10), 5);
const retryDelayMs = Math.min(parseInt(step.retryDelayMs || 2000, 10), 30000);
let result;
let attempt = 0;
while (true) {
result = await waitForCommandResult(executionId, commandId, stepTimeoutMs);
if (result.success || attempt >= maxRetries) break;
attempt++;
await addExecutionLog(executionId, {
step: stepNumber,
action: 'command_retry',
worker_id: workerId,
attempt,
max_retries: maxRetries,
message: `Command failed, retrying (attempt ${attempt}/${maxRetries})...`,
timestamp: new Date().toISOString()
});
await new Promise(r => setTimeout(r, retryDelayMs));
// Re-send command for retry
const retryCommandId = crypto.randomUUID();
await addExecutionLog(executionId, {
step: stepNumber, action: 'command_sent', worker_id: workerId,
command: command, command_id: retryCommandId, timestamp: new Date().toISOString()
});
sendCommandToWorker(workerWs, {
type: 'execute_command', execution_id: executionId, command_id: retryCommandId,
command: command, worker_id: workerId, timeout: stepTimeoutMs
});
commandId = retryCommandId; // update for next waitForCommandResult call
}
results.push(result);
if (!result.success) {
// Command failed after all retries, workflow should stop
return false;
}
}
// If every target worker was offline, treat as step failure
if (sentCount === 0) {
await addExecutionLog(executionId, {
step: stepNumber,
action: 'no_workers',
message: 'All target workers were offline when step executed',
timestamp: new Date().toISOString()
});
return false;
}
return true; // all sent commands succeeded (failures return false above)
} catch (error) {
console.error(`[Workflow] Error executing command step:`, error);
await addExecutionLog(executionId, {
step: stepNumber,
action: 'step_error',
error: error.message,
timestamp: new Date().toISOString()
});
return false;
}
}
// Wait for a command result using event-driven promise resolution.
// The resolver is stored in _commandResolvers and called immediately when
// command_result arrives via WebSocket: no DB polling required.
async function waitForCommandResult(executionId, commandId, timeout) {
return new Promise((resolve) => {
const timer = setTimeout(() => {
_commandResolvers.delete(commandId);
resolve({ success: false, error: 'Command timeout' });
}, timeout);
_commandResolvers.set(commandId, (result) => {
clearTimeout(timer);
_commandResolvers.delete(commandId);
resolve(result);
});
});
}
// Health check for deploys and monitoring (#45 phase 2). Unauthenticated on purpose,
// so CI and the deploy script can reach it; it reveals only the running commit,
// whether the database answers and how many workers are connected.
const GIT_SHA = (() => {
try {
return require('child_process')
.execFileSync('git', ['rev-parse', 'HEAD'], { cwd: __dirname, timeout: 5000, stdio: ['ignore', 'pipe', 'ignore'] })
.toString().trim();
} catch {
return 'unknown';
}
})();
app.get('/healthz', async (req, res) => {
let db = false;
try {
await Promise.race([
pool.query('SELECT 1'),
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 3000))
]);
db = true;
} catch { /* reported as db: false */ }
const live = [...new Set([...workers.values()].filter(w => w.dbWorkerId))];
const connectedWorkers = new Set(live.map(w => w.dbWorkerId)).size;
// Commits of the connected workers (public repo, so not sensitive): lets the
// worker deploy confirm the new code is the one that reconnected (#40).
const workerVersions = [...new Set(live.map(w => w.workerVersion || 'unknown'))].sort();
res.set('Cache-Control', 'no-store');
res.status(db ? 200 : 503).json({
status: db ? 'ok' : 'degraded',
version: SERVER_VERSION,
sha: GIT_SHA,
db,
workers: connectedWorkers,
worker_versions: workerVersions,
uptime_s: Math.round(process.uptime())
});
});
// HTML page routes (registered before the API routes)
const authenticatePage = makeAuthenticatePage(pool);
PAGES.forEach((p) => {
app.get(p.path, authenticatePage, async (req, res, next) => {
try {
await renderPage(req, res, p.view, {
pageTitle: p.title,
activeNav: p.key,
pageScripts: [`/assets/pages/${p.view}.js`],
// eslint-disable-next-line security/detect-non-literal-fs-filename -- p.view comes from the fixed PAGES list
pageStyles: fs.existsSync(path.join(__dirname, `public/assets/pages/${p.view}.css`))
? [`/assets/pages/${p.view}.css`]
: [],
pageConfig: { isAdmin: req.user.isAdmin }
});
} catch (e) {
next(e);
}
});
});
// Routes - All protected by SSO
app.get('/api/user', authenticateSSO, (req, res) => {
res.json(req.user);
});
app.get('/api/workflows', authenticateSSO, async (req, res) => {
try {
const [rows] = await pool.query('SELECT * FROM workflows ORDER BY created_at DESC');
res.json(rows);
} catch (error) {
console.error('[API] GET /api/workflows error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/workflows/:id', authenticateSSO, async (req, res) => {
try {
const [rows] = await pool.query('SELECT * FROM workflows WHERE id = ?', [req.params.id]);
if (rows.length === 0) return res.status(404).json({ error: 'Not found' });
const wf = rows[0];
let definition = {};
try {
definition = typeof wf.definition === 'string'
? JSON.parse(wf.definition || '{}')
: (wf.definition || {});
} catch { /* corrupt definition: return empty */ }
res.json({ ...wf, definition });
} catch (error) {
console.error('[API] GET /api/workflows/:id error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/api/workflows', authenticateSSO, requireJSON, async (req, res) => {
try {
if (!req.user.isAdmin) return res.status(403).json({ error: 'Admin only' });
const { name, description, definition, webhook_url } = req.body;
if (!name || !definition) return res.status(400).json({ error: 'name and definition are required' });
const badConditions = conditionErrors(definition);
if (badConditions.length) return res.status(400).json({ error: 'Invalid condition', problems: badConditions });
const webhookCheck = validateWebhookUrl(webhook_url);
if (!webhookCheck.ok) return res.status(400).json({ error: webhookCheck.reason });
const id = crypto.randomUUID();
await pool.query(
'INSERT INTO workflows (id, name, description, definition, webhook_url, created_by) VALUES (?, ?, ?, ?, ?, ?)',
[id, name, description, JSON.stringify(definition), webhook_url || null, req.user.username]
);
res.json({ id, name, description, definition, webhook_url: webhook_url || null });
broadcast({ type: 'workflow_created', workflow_id: id });
} catch (error) {
console.error('[Workflow] Error creating workflow:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.delete('/api/workflows/:id', authenticateSSO, async (req, res) => {
try {
// Only admins can delete workflows
if (!req.user.isAdmin) {
return res.status(403).json({ error: 'Admin access required' });
}
await pool.query('DELETE FROM workflows WHERE id = ?', [req.params.id]);
res.json({ success: true });
broadcast({ type: 'workflow_deleted', workflow_id: req.params.id });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/workers', authenticateSSO, async (req, res) => {
try {
const [rows] = await pool.query('SELECT id, name, status, last_heartbeat, metadata FROM workers ORDER BY name');
res.json(rows);
} catch (error) {
console.error('[API] GET /api/workers error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/api/workers/heartbeat', async (req, res) => {
try {
const { worker_id, name, metadata } = req.body;
const apiKey = req.headers['x-api-key'];
// Verify API key: reject missing or wrong keys
if (!checkWorkerKey(apiKey)) {
return res.status(401).json({ error: 'Invalid API key' });
}
await pool.query(
`INSERT INTO workers (id, name, status, last_heartbeat, api_key, metadata)
VALUES (?, ?, 'online', NOW(), ?, ?)
ON DUPLICATE KEY UPDATE
status='online',
last_heartbeat=NOW(),
metadata=VALUES(metadata)`,
[worker_id, name, apiKey, JSON.stringify(metadata)]
);
broadcast({ type: 'worker_update', worker_id, status: 'online' });
res.json({ success: true });
} catch (error) {
console.error('[Worker] Heartbeat error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/api/executions', executionLimiter, authenticateSSO, requireJSON, async (req, res) => {
try {
const { workflow_id, params = {}, dry_run = false } = req.body;
if (!workflow_id) return res.status(400).json({ error: 'workflow_id is required' });
const id = crypto.randomUUID();
// Get workflow definition
const [workflows] = await pool.query('SELECT * FROM workflows WHERE id = ?', [workflow_id]);
if (workflows.length === 0) {
return res.status(404).json({ error: 'Workflow not found' });
}
const workflow = workflows[0];
let definition;
try {
definition = typeof workflow.definition === 'string' ? JSON.parse(workflow.definition) : workflow.definition;
} catch {
return res.status(500).json({ error: 'Workflow definition is corrupt' });
}
// Validate required params
const paramDefs = definition.params || [];
for (const pd of paramDefs) {
if (pd.required && !params[pd.name]) {
return res.status(400).json({ error: `Missing required parameter: ${pd.label || pd.name}` });
}
}
// Create execution record
const initLogs = Object.keys(params).length > 0
? [{ action: 'params', params, timestamp: new Date().toISOString() }]
: [];
await pool.query(
'INSERT INTO executions (id, workflow_id, status, started_by, started_at, logs) VALUES (?, ?, ?, ?, NOW(), ?)',
[id, workflow_id, 'running', req.user.username, JSON.stringify(initLogs)]
);
if (dry_run) {
broadcast({ type: 'execution_started', execution_id: id, workflow_id, dry_run: true });
} else {
broadcast({ type: 'execution_started', execution_id: id, workflow_id });
}
// Start workflow execution asynchronously
executeWorkflowSteps(id, workflow_id, definition, req.user.username, params, dry_run).catch(err => {
console.error(`[Workflow] Execution ${id} failed:`, err);
});
res.json({ id, workflow_id, status: 'running', dry_run: !!dry_run });
} catch (error) {
console.error('[Execution] Error starting execution:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/executions', authenticateSSO, async (req, res) => {
try {
const limit = Math.min(parseInt(req.query.limit) || 50, 1000);
const offset = parseInt(req.query.offset) || 0;
const conditions = [];
const queryParams = [];
// Existing filter
if (req.query.hide_internal === 'true') {
conditions.push("e.started_by NOT LIKE 'gandalf:%' AND e.started_by NOT LIKE 'scheduler:%'");
}
// New filters
if (req.query.status) {
conditions.push('e.status = ?');
queryParams.push(req.query.status);
}
if (req.query.workflow_id) {
conditions.push('e.workflow_id = ?');
queryParams.push(req.query.workflow_id);
}
if (req.query.started_by) {
conditions.push('e.started_by = ?');
queryParams.push(req.query.started_by);
}
if (req.query.after) {
conditions.push('e.started_at >= ?');
queryParams.push(new Date(req.query.after));
}
if (req.query.before) {
conditions.push('e.started_at <= ?');
queryParams.push(new Date(req.query.before));
}
if (req.query.search) {
conditions.push('(w.name LIKE ? OR e.started_by LIKE ?)');
const term = `%${req.query.search}%`;
queryParams.push(term, term);
}
const whereClause = conditions.length ? `WHERE ${conditions.join(' AND ')}` : '';
const [rows] = await pool.query(
`SELECT e.id, e.workflow_id, e.status, e.started_by, e.started_at, e.completed_at,
w.name as workflow_name
FROM executions e
LEFT JOIN workflows w ON e.workflow_id = w.id
${whereClause}
ORDER BY e.started_at DESC LIMIT ? OFFSET ?`,
[...queryParams, limit, offset]
);
const [countRows] = await pool.query(
`SELECT COUNT(*) as total FROM executions e LEFT JOIN workflows w ON e.workflow_id = w.id ${whereClause}`,
queryParams
);
const total = countRows[0].total;
res.json({
executions: rows,
total,
limit,
offset,
hasMore: offset + rows.length < total
});
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.delete('/api/executions/completed', authenticateSSO, async (req, res) => {
try {
if (!req.user.isAdmin) return res.status(403).json({ error: 'Admin access required' });
const [result] = await pool.query(
"DELETE FROM executions WHERE status IN ('completed', 'failed')"
);
broadcast({ type: 'executions_bulk_deleted' });
res.json({ success: true, deleted: result.affectedRows });
} catch (error) {
console.error('[Execution] Bulk delete error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.delete('/api/executions/:id', authenticateSSO, async (req, res) => {
try {
if (!req.user.isAdmin) return res.status(403).json({ error: 'Admin access required' });
await pool.query('DELETE FROM executions WHERE id = ?', [req.params.id]);
broadcast({ type: 'execution_deleted', execution_id: req.params.id });
res.json({ success: true });
} catch (error) {
console.error('[Execution] Error deleting execution:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/api/executions/:id/abort', authenticateSSO, async (req, res) => {
try {
const executionId = req.params.id;
// Check if execution exists and is running
const [execution] = await pool.query('SELECT status FROM executions WHERE id = ?', [executionId]);
if (execution.length === 0) {
return res.status(404).json({ error: 'Execution not found' });
}
if (execution[0].status !== 'running') {
return res.status(400).json({ error: 'Execution is not running' });
}
// Add abort log entry
await addExecutionLog(executionId, {
action: 'execution_aborted',
aborted_by: req.user.username,
timestamp: new Date().toISOString()
});
// Update execution status to failed
await updateExecutionStatus(executionId, 'failed');
// Stop tracking its direct command so the result timeout doesn't fire later
for (const [key, t] of _commandTargets) {
if (t.direct && t.executionId === executionId) {
clearTimeout(t.timer);
_commandTargets.delete(key);
}
}
// Unblock any pending prompt so the thread can exit
const pending = _executionPrompts.get(executionId);
if (pending) pending.resolve(null);
console.log(`[Execution] Execution ${executionId} aborted by ${req.user.username}`);
res.json({ success: true });
} catch (error) {
console.error('[Execution] Error aborting execution:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Respond to a pending prompt in a running execution
app.post('/api/executions/:id/respond', authenticateSSO, async (req, res) => {
try {
const { id } = req.params;
const { response } = req.body;
if (!response || typeof response !== 'string') {
return res.status(400).json({ error: 'response is required' });
}
const pending = _executionPrompts.get(id);
if (!pending) return res.status(404).json({ error: 'No pending prompt for this execution' });
// Validate response is one of the allowed options
if (pending.options && !pending.options.includes(response)) {
return res.status(400).json({
error: `Invalid response. Allowed values: ${pending.options.join(', ')}`
});
}
await addExecutionLog(id, {
action: 'prompt_response', response,
responded_by: req.user.username,
timestamp: new Date().toISOString()
});
broadcast({ type: 'prompt_response', execution_id: id, response });
pending.resolve(response);
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
// Edit a workflow definition (admin only)
app.put('/api/workflows/:id', authenticateSSO, requireJSON, async (req, res) => {
try {
if (!req.user.isAdmin) return res.status(403).json({ error: 'Admin only' });
const { id } = req.params;
const { name, description, definition, webhook_url } = req.body;
if (!name || !definition) return res.status(400).json({ error: 'name and definition required' });
const webhookCheck = validateWebhookUrl(webhook_url);
if (!webhookCheck.ok) return res.status(400).json({ error: webhookCheck.reason });
let defObj;
try {
defObj = typeof definition === 'string' ? JSON.parse(definition) : definition;
} catch {
return res.status(400).json({ error: 'Invalid JSON in definition' });
}
const badConditions = conditionErrors(defObj);
if (badConditions.length) return res.status(400).json({ error: 'Invalid condition', problems: badConditions });
const [result] = await pool.query(
'UPDATE workflows SET name=?, description=?, definition=?, webhook_url=?, updated_at=NOW() WHERE id=?',
[name, description || '', JSON.stringify(defObj), webhook_url || null, id]
);
if (result.affectedRows === 0) return res.status(404).json({ error: 'Workflow not found' });
broadcast({ type: 'workflow_updated', workflow_id: id });
res.json({ success: true });
} catch (error) {
console.error('[Workflow] Error updating workflow:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Scheduled Commands API
app.get('/api/scheduled-commands', authenticateSSO, async (req, res) => {
try {
const [schedules] = await pool.query(
'SELECT * FROM scheduled_commands ORDER BY created_at DESC'
);
res.json(schedules);
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.post('/api/scheduled-commands', authenticateSSO, requireJSON, async (req, res) => {
try {
if (!req.user.isAdmin) return res.status(403).json({ error: 'Admin access required' });
const { name, command, worker_ids, schedule_type, schedule_value } = req.body;
if (!name || !command || !worker_ids || !schedule_type || !schedule_value) {
return res.status(400).json({ error: 'Missing required fields' });
}
// Validate schedule_value for numeric types
if (schedule_type === 'interval' || schedule_type === 'hourly') {
const n = parseInt(schedule_value, 10);
if (!Number.isInteger(n) || n <= 0) {
return res.status(400).json({ error: `schedule_value for type "${schedule_type}" must be a positive integer` });
}
}
const id = crypto.randomUUID();
const nextRun = calculateNextRun(schedule_type, schedule_value);
await pool.query(
`INSERT INTO scheduled_commands
(id, name, command, worker_ids, schedule_type, schedule_value, created_by, next_run)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
[id, name, command, JSON.stringify(worker_ids), schedule_type, schedule_value, req.user.username, nextRun]
);
res.json({ success: true, id });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.put('/api/scheduled-commands/:id/toggle', authenticateSSO, async (req, res) => {
try {
if (!req.user.isAdmin) return res.status(403).json({ error: 'Admin access required' });
const { id } = req.params;
const [current] = await pool.query('SELECT enabled FROM scheduled_commands WHERE id = ?', [id]);
if (current.length === 0) {
return res.status(404).json({ error: 'Schedule not found' });
}
const newEnabled = !current[0].enabled;
await pool.query('UPDATE scheduled_commands SET enabled = ? WHERE id = ?', [newEnabled, id]);
res.json({ success: true, enabled: newEnabled });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.delete('/api/scheduled-commands/:id', authenticateSSO, async (req, res) => {
try {
if (!req.user.isAdmin) return res.status(403).json({ error: 'Admin access required' });
const { id } = req.params;
await pool.query('DELETE FROM scheduled_commands WHERE id = ?', [id]);
res.json({ success: true });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
// Internal M2M API for Gandalf
app.post('/api/internal/command', authenticateGandalf, async (req, res) => {
try {
const { worker_id, command } = req.body;
if (!worker_id || !command) {
return res.status(400).json({ error: 'worker_id and command are required' });
}
const workerWs = workers.get(worker_id);
if (!workerWs || workerWs.readyState !== WebSocket.OPEN) {
return res.status(400).json({ error: 'Worker not connected' });
}
const executionId = crypto.randomUUID();
await pool.query(
'INSERT INTO executions (id, workflow_id, status, started_by, started_at, logs) VALUES (?, ?, ?, ?, NOW(), ?)',
[executionId, null, 'running', req.user.username, JSON.stringify([{
step: 'internal_command',
action: 'command_sent',
worker_id: worker_id,
command: command,
timestamp: new Date().toISOString()
}])]
);
sendCommandToWorker(workerWs, {
type: 'execute_command',
execution_id: executionId,
command: command,
worker_id: worker_id,
timeout: QUICK_CMD_TIMEOUT_MS
});
res.json({ execution_id: executionId });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
app.get('/api/internal/executions/:id', authenticateGandalf, async (req, res) => {
try {
const [rows] = await pool.query('SELECT * FROM executions WHERE id = ?', [req.params.id]);
if (rows.length === 0) {
return res.status(404).json({ error: 'Not found' });
}
const execution = rows[0];
let logs = [];
try {
logs = typeof execution.logs === 'string'
? JSON.parse(execution.logs || '[]')
: (execution.logs || []);
} catch { logs = []; }
res.json({ ...execution, logs });
} catch (error) {
console.error('[API] GET /api/internal/executions/:id error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Detailed health + stats (auth required)
app.get('/api/health', authenticateSSO, async (req, res) => {
try {
await pool.query('SELECT 1');
const [workerRows] = await pool.query('SELECT status, COUNT(*) as count FROM workers GROUP BY status');
const workerCounts = Object.fromEntries(workerRows.map(r => [r.status, Number(r.count)]));
res.json({
status: 'ok',
version: SERVER_VERSION,
uptime_seconds: Math.floor(process.uptime()),
timestamp: new Date().toISOString(),
workers: { online: workerCounts.online || 0, offline: workerCounts.offline || 0 },
database: 'connected'
});
} catch (error) {
console.error('[Health] /api/health error:', error);
res.status(500).json({ status: 'error', timestamp: new Date().toISOString() });
}
});
// Health check (no auth required)
app.get('/health', async (req, res) => {
try {
await pool.query('SELECT 1');
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
database: 'connected',
auth: 'authelia-sso'
});
} catch (error) {
res.status(500).json({
status: 'error',
timestamp: new Date().toISOString(),
database: 'disconnected',
error: 'Internal server error'
});
}
});
// Get execution details with logs
app.get('/api/executions/:id', authenticateSSO, async (req, res) => {
try {
const [rows] = await pool.query('SELECT * FROM executions WHERE id = ?', [req.params.id]);
if (rows.length === 0) {
return res.status(404).json({ error: 'Not found' });
}
const execution = rows[0];
let parsedLogs = [];
try {
parsedLogs = typeof execution.logs === 'string' ? JSON.parse(execution.logs || '[]') : (execution.logs || []);
} catch { parsedLogs = []; }
const waitingForInput = _executionPrompts.has(req.params.id);
let pendingPrompt = null;
if (waitingForInput) {
for (let i = parsedLogs.length - 1; i >= 0; i--) {
if (parsedLogs[i].action === 'prompt') {
pendingPrompt = {
message: parsedLogs[i].message,
options: parsedLogs[i].options,
output: parsedLogs[i].output || null
};
break;
}
}
}
res.json({
...execution,
logs: parsedLogs,
waiting_for_input: waitingForInput,
prompt: pendingPrompt,
});
} catch (error) {
console.error('[API] GET /api/executions/:id error:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
// Delete worker (admin only)
app.delete('/api/workers/:id', authenticateSSO, async (req, res) => {
try {
if (!req.user.isAdmin) {
return res.status(403).json({ error: 'Admin access required' });
}
await pool.query('DELETE FROM workers WHERE id = ?', [req.params.id]);
res.json({ success: true });
broadcast({ type: 'worker_deleted', worker_id: req.params.id });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
// Send direct command to specific worker
app.post('/api/workers/:id/command', authenticateSSO, requireJSON, async (req, res) => {
try {
const { command } = req.body;
if (!command || typeof command !== 'string' || !command.trim()) {
return res.status(400).json({ error: 'command is required' });
}
const executionId = crypto.randomUUID();
const workerId = req.params.id;
// Create execution record in database
await pool.query(
'INSERT INTO executions (id, workflow_id, status, started_by, started_at, logs) VALUES (?, ?, ?, ?, NOW(), ?)',
[executionId, null, 'running', req.user.username, JSON.stringify([{
step: 'quick_command',
action: 'command_sent',
worker_id: workerId,
command: command,
timestamp: new Date().toISOString()
}])]
);
// Send command via WebSocket to specific worker
const commandMessage = {
type: 'execute_command',
execution_id: executionId,
command: command,
worker_id: workerId,
timeout: QUICK_CMD_TIMEOUT_MS
};
const workerWs = workers.get(workerId);
if (!workerWs || workerWs.readyState !== WebSocket.OPEN) {
return res.status(400).json({ error: 'Worker not connected' });
}
sendCommandToWorker(workerWs, commandMessage);
console.log(`Command sent to worker ${workerId}: ${command}`);
broadcast({ type: 'execution_started', execution_id: executionId, workflow_id: null });
res.json({ success: true, execution_id: executionId });
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
// 404: JSON under /api/, themed HTML elsewhere
app.use((req, res) => {
if (req.path.startsWith('/api/')) {
return res.status(404).json({ error: 'Not found' });
}
renderError(req, res, 404, 'Not found', `No page exists at ${req.path}`);
});
// Error handler
app.use((err, req, res, next) => { // eslint-disable-line no-unused-vars
console.error('[Error]', err && err.stack ? err.stack : err);
if (res.headersSent) return;
if (req.path.startsWith('/api/')) {
return res.status(500).json({ error: 'Internal server error' });
}
renderError(req, res, 500, 'Internal server error', 'Something went wrong rendering this page.');
});
// Start server
const PORT = process.env.PORT || 8080;
const HOST = process.env.HOST || '0.0.0.0';
initDatabase().then(() => {
server.listen(PORT, HOST, () => {
console.log(`PULSE Server running on http://${HOST}:${PORT}`);
console.log(`Connected to MariaDB at ${process.env.DB_HOST}`);
console.log(`Authentication: Authelia SSO`);
const trustedCount = trustedProxies().count;
if (trustedCount) {
console.log(`Trusted proxies for SSO headers: ${trustedCount}`);
} else {
console.warn('WARNING: TRUSTED_PROXIES is not set, so no SSO login can succeed. ' +
'Set it to the reverse proxy address(es) that run Authelia forward auth.');
}
console.log(`Worker API Key configured: ${process.env.WORKER_API_KEY ? 'Yes' : 'No'}`);
if (!process.env.WORKER_API_KEY) {
console.error('ERROR: WORKER_API_KEY is not set, so no worker can authenticate.');
}
console.log(`Worker auth enforcement: ${workerAuthEnforced() ? 'ON (keyless workers refused)' : 'OFF (keyless workers allowed with a warning; set WORKER_AUTH_ENFORCE=1 once all workers send their key)'}`);
if (DEV_READONLY) {
console.warn('************************************************************');
console.warn('* PULSE_DEV_READONLY IS ON - background jobs are disabled *');
console.warn('* No cleanup, scheduler, stale-worker or recovery writes. *');
console.warn('************************************************************');
}
});
}).catch(err => {
console.error('Failed to start server:', err);
process.exit(1);
});