@nmakarov/cli-toolkit 0.27.0 → 0.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/tasks.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/tasks/index.js","../src/utils/date-utils.js","../src/utils/fs-utils.js","../src/utils/os-utils.js","../src/utils/format-utils.js","../src/utils/core-utils.js","../src/tasks/servicesRegistry.js","../src/tasks/taskUtils.js","../src/tasks/time-matcher.js","../src/tasks/taskLogs.js","../src/filedatabase/index.js","../src/filedatabase/serializers.js","../src/errors.js","../src/tasks/AbstractTask.js","../src/tasks/coreTasks/TaskPing.js","../src/tasks/coreTasks/TaskSampleProcess.js","../src/tasks/coreTasks/TaskShellCommand.js","../src/tasks/coreTasks/TaskSystemInfo.js","../src/tasks/coreTasks/TaskSumAB.js","../src/tasks/coreTasks/TaskStopRunner.js","../src/tasks/coreTasks/TaskGetLogs.js","../src/tasks/TasksRegistry.js","../src/tasks/serviceTaskAllowlist.js","../src/tasks/taskScriptRunner.js"],"sourcesContent":["import os from \"node:os\";\nimport { sleepMs, toJsonColumn } from \"../utils/index.js\";\nimport {\n registerInServicesRegistry,\n touchServicesRegistry,\n unregisterServicesRegistry,\n} from \"./servicesRegistry.js\";\nimport {\n enqueueTask,\n ensureTaskTables,\n queueToTableNames,\n taskHistoryInsertFromQueueRow,\n updateTaskProgress,\n} from \"./taskUtils.js\";\nimport { appendTaskIpcLog } from \"./taskLogs.js\";\nimport { nextTimeMatch, timeMatcher } from \"./time-matcher.js\";\nexport {\n timeMatcher,\n nextTimeMatch,\n matchesParsedPattern,\n convertPattern,\n resolveAsterisks,\n resolveRanges,\n resolveSteps,\n} from \"./time-matcher.js\";\nimport { TasksRegistry } from \"./TasksRegistry.js\";\nimport { normalizeAllowedTasks } from \"./serviceTaskAllowlist.js\";\n\n/** Sentinel value stored in the `progress` column when a task is paused due to error. */\nconst LOCKED_BY_ERROR_MESSAGE = \"locked by error\";\n\nexport {\n enqueueTask,\n ensureTaskTables,\n queueToTableNames,\n taskHistoryInsertFromQueueRow,\n updateTaskProgress,\n} from \"./taskUtils.js\";\nexport {\n listServicesRegistry,\n registerInServicesRegistry,\n touchServicesRegistry,\n unregisterServicesRegistry,\n updateServicesRegistryMetadata,\n} from \"./servicesRegistry.js\";\n/** @deprecated Use registerInServicesRegistry */\nexport { registerInServicesRegistry as registerRunnerHeartbeat } from \"./servicesRegistry.js\";\n/** @deprecated Use touchServicesRegistry */\nexport { touchServicesRegistry as touchRunnerHeartbeat } from \"./servicesRegistry.js\";\n/** @deprecated Use unregisterServicesRegistry */\nexport { unregisterServicesRegistry as unregisterRunnerHeartbeat } from \"./servicesRegistry.js\";\n/** @deprecated Use listServicesRegistry */\nexport { listServicesRegistry as listAliveRunnerHeartbeats } from \"./servicesRegistry.js\";\nexport {\n appendTaskIpcLog,\n flushTaskIpcLogs,\n ipcFileLogsTableNameForSourceResource,\n readTaskIpcLogsSnapshot,\n resolveIpcFileLogsDir,\n} from \"./taskLogs.js\";\nexport { runNodeTaskScript } from \"./taskScriptRunner.js\";\nexport { AbstractTask } from \"./AbstractTask.js\";\nexport { TasksRegistry } from \"./TasksRegistry.js\";\nexport { TaskPing } from \"./coreTasks/TaskPing.js\";\nexport { TaskSampleProcess } from \"./coreTasks/TaskSampleProcess.js\";\nexport { TaskShellCommand } from \"./coreTasks/TaskShellCommand.js\";\nexport { TaskSystemInfo } from \"./coreTasks/TaskSystemInfo.js\";\nexport { TaskSumAB } from \"./coreTasks/TaskSumAB.js\";\nexport { TaskStopRunner } from \"./coreTasks/TaskStopRunner.js\";\nexport { TaskGetLogs } from \"./coreTasks/TaskGetLogs.js\";\nexport {\n normalizeAllowedTasks,\n mergeAllowedTasksWithServiceTasks,\n SERVICE_TASK_NAMES,\n} from \"./serviceTaskAllowlist.js\";\n\n/** Shared default registry preloaded with every core task (including legacy aliases). */\nexport const defaultTasksRegistry = TasksRegistry.withCoreTasks();\n\n/**\n * Fail-fast accessor for `context.db` with a tasks-specific error message.\n *\n * @param {object} context\n * @returns {Function}\n */\nfunction getDb(context) {\n const db = context.db;\n if (!db) {\n throw new Error(\"Tasks component requires context.db. Initialize DB first and attach to context.\");\n }\n return db;\n}\n\n/**\n * Coerce whatever the caller passed as `registry` into a `TasksRegistry` instance:\n *\n * - `undefined` / missing → {@link defaultTasksRegistry} (every core task)\n * - already a `TasksRegistry` → returned as-is\n * - plain `{ name: Class }` map → wrapped in a fresh registry\n *\n * @param {TasksRegistry | Record<string, Function> | undefined} registry\n * @returns {TasksRegistry}\n */\nfunction normalizeRegistry(registry) {\n if (!registry) return defaultTasksRegistry;\n if (registry instanceof TasksRegistry) return registry;\n return new TasksRegistry().addMany(registry);\n}\n\n/**\n * Convenience for enqueuing a targeted `stopRunner` task so a specific service\n * group exits cleanly on its next tick. Intended for operator tooling — the\n * runner itself also accepts in-process stop signals via `context.isStop()`.\n *\n * @param {object} context\n * @param {string} serviceGroup\n * @param {string} [queueName]\n * @param {number} [allowanceMs]\n * @returns {Promise<string>} UUID of the enqueued stop task.\n */\nexport async function enqueueStopTask(context, serviceGroup, queueName = \"tasks\", allowanceMs = 5000) {\n return enqueueTask(context, {\n queueName,\n name: \"stopRunner\",\n params: { allowanceMs },\n priority: 0,\n serviceGroup,\n });\n}\n\n/**\n * Broadcast a cooperative stop signal to every currently-running task instance.\n * Tasks decide per-call how much of `allowanceMs` to honor; we also emit on\n * `context.emitter` so other subscribers (e.g. fetchers) can wind down.\n *\n * @param {object} context\n * @param {Map<string, { requestStop?: Function }>} runningTaskInstances\n * @param {number} allowanceMs\n * @returns {Promise<void>}\n */\nasync function signalRunningTasksStop(context, runningTaskInstances, allowanceMs) {\n context.logger.warn?.(`[tasks] signaling ${runningTaskInstances.size} running task(s) to stop`);\n for (const [, taskInstance] of runningTaskInstances) {\n if (typeof taskInstance.requestStop === \"function\") {\n try {\n await taskInstance.requestStop(allowanceMs);\n } catch (error) {\n context.logger.warn?.(\"[tasks] task requestStop failed:\", error);\n }\n }\n }\n context.emitter.emit(\"stop\", allowanceMs);\n}\n\n/**\n * Run a single claimed task row end-to-end:\n *\n * 1. Resolve its class from the registry (unknown name → record failure + remove/pause).\n * 2. Construct an instance, stash it in `runningTaskInstances` so stop signals can reach it.\n * 3. `await instance.run(reportProgress)`; capture thrown errors into a structured `results` payload.\n * 4. Append a row to the history table, update/delete/pause the queue row depending on schedule/success.\n * 5. Return a summary telling the loop whether a `stopRunner` was requested.\n *\n * @param {object} context\n * @param {string} tasksTable\n * @param {string} historyTable\n * @param {object} row The claimed queue row.\n * @param {TasksRegistry} registry\n * @param {Map<string, object>} runningTaskInstances\n * @returns {Promise<{ stopRunnerRequested: boolean, stopAllowanceMs: number }>}\n */\nasync function executeClaimedTask(context, tasksTable, historyTable, row, registry, runningTaskInstances) {\n const db = getDb(context);\n const taskName = row.name;\n const TaskClass = registry.get(taskName);\n if (!TaskClass) {\n const err = { message: `Unknown task \"${taskName}\"` };\n await db(historyTable).insert(\n taskHistoryInsertFromQueueRow(row, {\n completed_at: new Date(),\n success: false,\n status: \"failed\",\n status_changed_at: db.fn.now(),\n params: toJsonColumn(row.params),\n results: toJsonColumn(err),\n })\n );\n if (row.schedule) {\n await db(tasksTable).where({ id: row.id }).update({\n started_at: null,\n completed_at: new Date(),\n success: false,\n results: toJsonColumn(err),\n past_due: null,\n status: \"paused\",\n status_changed_at: db.fn.now(),\n progress: LOCKED_BY_ERROR_MESSAGE,\n });\n } else {\n await db(tasksTable).where({ id: row.id }).delete();\n }\n return { stopRunnerRequested: false, stopAllowanceMs: 0 };\n }\n\n let success = false;\n let results = null;\n let taskInstance = null;\n try {\n taskInstance = new TaskClass(context, row);\n runningTaskInstances.set(row.id, taskInstance);\n const runResult = await taskInstance.run((progress) => updateTaskProgress(context, tasksTable, row.id, progress));\n success = !!runResult?.success;\n results = runResult?.results ?? null;\n } catch (error) {\n success = false;\n results = {\n message: error?.message ?? String(error),\n name: error?.name ?? \"Error\",\n stack: error?.stack ?? null,\n };\n } finally {\n runningTaskInstances.delete(row.id);\n }\n\n await db(historyTable).insert(\n taskHistoryInsertFromQueueRow(row, {\n completed_at: new Date(),\n success,\n status: success ? \"completed\" : \"failed\",\n status_changed_at: db.fn.now(),\n params: toJsonColumn(row.params),\n results: toJsonColumn(results),\n })\n );\n if (!success) {\n const dbName = String(context?.params?.get?.(\"dbName\") || \"local\");\n const tableName = String(context?.params?.get?.(\"table\") || \"tasks\");\n const fallbackRecoverCommand = [\n \"npx\",\n \"tsx\",\n \"examples/tasks/recover-task.ts\",\n `--dbName='${dbName.replace(/'/g, `'\\\\''`)}'`,\n `--table='${tableName.replace(/'/g, `'\\\\''`)}'`,\n `--id='${String(row.id).replace(/'/g, `'\\\\''`)}'`,\n ].join(\" \");\n const rerunCommand = results && typeof results === \"object\" && results.rerunCommand\n ? results.rerunCommand\n : fallbackRecoverCommand;\n appendTaskIpcLog(context, row, {\n level: \"error\",\n message: `[tasks] task failed: ${row.name} id=${row.id}. Once problem is fixed, re-run: ${rerunCommand}`,\n details: results,\n });\n }\n\n if (row.schedule) {\n if (success) {\n let nextRunAt = null;\n try {\n nextRunAt = nextTimeMatch(row.schedule, new Date());\n } catch (e) {\n context.logger?.warn?.(`[tasks] nextTimeMatch after success for task ${row.id}: ${e?.message ?? String(e)}`);\n }\n await db(tasksTable).where({ id: row.id }).update({\n started_at: null,\n completed_at: new Date(),\n success,\n results: toJsonColumn(results),\n progress: null,\n past_due: null,\n status: \"idle\",\n status_changed_at: db.fn.now(),\n next_run_at: nextRunAt,\n // Claim overwrites these; clear so idle rows stay “any worker” (see claimNextRunnableTask).\n service_name: null,\n server_name: null,\n instance_number: null,\n });\n } else {\n await db(tasksTable).where({ id: row.id }).update({\n started_at: null,\n completed_at: new Date(),\n success,\n results: toJsonColumn(results),\n status: \"paused\",\n status_changed_at: db.fn.now(),\n progress: LOCKED_BY_ERROR_MESSAGE,\n past_due: null,\n });\n }\n } else {\n await db(tasksTable).where({ id: row.id }).delete();\n }\n\n const stopRunnerRequested = !!(results && typeof results === \"object\" && results.stopRunner === true);\n const stopAllowanceMs = stopRunnerRequested ? Number(results.allowanceMs ?? 5000) : 0;\n return { stopRunnerRequested, stopAllowanceMs };\n}\n\n/** Fisher–Yates shuffle so concurrent workers don't all try the same candidate row first. */\nfunction shuffleTaskRowsInPlace(rows) {\n for (let i = rows.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n const t = rows[i];\n rows[i] = rows[j];\n rows[j] = t;\n }\n}\n\n/**\n * Atomically claim one runnable task row matching the caller's service group /\n * identity / allowlist, and return the (now-`running`) row — or `null` when none\n * are ready.\n *\n * Targeting rules for task columns (`service_group`, `service_name`, `server_name`,\n * `instance_number`): NULL on the task row means \"any\" for that field; a value\n * means \"this runner must match exactly\". This lets operators enqueue a task for\n * a specific host/instance while letting other rows fan out to whoever is free.\n *\n * @param {object} context\n * @param {string} tasksTable\n * @param {string} serviceGroup\n * @param {TasksRegistry} registry\n * @param {number} scanLimit How many candidate rows to pull before attempting claim.\n * @param {string[]|undefined} taskNames When set, only claim rows with `name IN taskNames`.\n * @param {{ service_name: string, server_name: string, instance_number: number }|null} runnerIdentity\n * @returns {Promise<object|null>} The claimed row, or `null` when nothing is ready.\n */\nasync function claimNextRunnableTask(\n context,\n tasksTable,\n serviceGroup,\n registry,\n scanLimit,\n taskNames,\n runnerIdentity\n) {\n const db = getDb(context);\n\n // Targeting: NULL on the task row means \"any\" for that field. Rows must match the runner's\n // service_group and identity (when provided) for each non-null task column.\n let query = db(tasksTable)\n .where({ status: \"idle\" })\n .where(function () {\n this.whereNull(\"service_group\").orWhere({ service_group: serviceGroup });\n })\n .orderByRaw(\"CASE WHEN past_due IS NULL THEN 1 ELSE 0 END ASC\")\n .orderBy([{ column: \"priority\", order: \"asc\" }])\n // Fair rotation for recurring tasks:\n // 1) never-run rows first\n // 2) then least recently completed rows\n // 3) then stable created_at order\n .orderByRaw(\"CASE WHEN completed_at IS NULL THEN 0 ELSE 1 END ASC\")\n .orderBy([{ column: \"completed_at\", order: \"asc\" }, { column: \"created_at\", order: \"asc\" }])\n .limit(scanLimit);\n if (taskNames && taskNames.length > 0) {\n query = query.whereIn(\"name\", taskNames);\n }\n if (runnerIdentity) {\n query = query\n .where(function () {\n this.whereNull(\"service_name\").orWhere({ service_name: runnerIdentity.service_name });\n })\n .where(function () {\n this.whereNull(\"instance_number\").orWhere({ instance_number: runnerIdentity.instance_number });\n })\n .where(function () {\n this.whereNull(\"server_name\").orWhere({ server_name: runnerIdentity.server_name });\n });\n } else {\n // Without registry identity we cannot match a specific instance; only rows with no per-instance targeting.\n query = query\n .whereNull(\"service_name\")\n .whereNull(\"instance_number\")\n .whereNull(\"server_name\");\n }\n const candidates = await query;\n shuffleTaskRowsInPlace(candidates);\n\n for (const row of candidates) {\n if (!row.past_due && row.schedule && !timeMatcher(row.schedule)) {\n continue;\n }\n\n const TaskClass = registry.get(row.name);\n if (!TaskClass) {\n // Not registered on this runner — skip without claiming so another worker can run it.\n continue;\n }\n\n const taskInstance = new TaskClass(context, row);\n const reason = taskInstance.cantRunReason ? await taskInstance.cantRunReason() : null;\n if (reason) {\n if (!row.past_due) {\n await db(tasksTable).where({ id: row.id }).update({\n past_due: db.fn.now(),\n progress: String(reason),\n });\n }\n continue;\n }\n\n const claimPatch = {\n started_at: db.fn.now(),\n status: \"running\",\n status_changed_at: db.fn.now(),\n };\n if (runnerIdentity) {\n claimPatch.service_name = runnerIdentity.service_name;\n claimPatch.server_name = runnerIdentity.server_name;\n claimPatch.instance_number = runnerIdentity.instance_number;\n }\n\n const updated = await db(tasksTable)\n .where({ id: row.id, status: \"idle\" })\n .update(claimPatch)\n .returning(\"*\");\n\n const claimed = Array.isArray(updated) ? updated[0] : null;\n if (claimed) return claimed;\n }\n\n return null;\n}\n\n/**\n * Main runner loop. Registers in the services registry (optional), then polls\n * the queue, claiming up to `maxParallel` tasks at a time plus one extra\n * \"control lane\" for `stop`/`stopRunner` so a graceful stop can always be picked\n * even when workers are saturated.\n *\n * Exits when any of the following become true:\n * - `context.isStop()` flips to `true` (external shutdown signal).\n * - A `stopRunner` task completes successfully.\n * - `context.tasksRunnerStop === true` (manual flag, mostly for tests).\n *\n * @param {object} context\n * @param {{\n * queueName?: string,\n * target: string,\n * pollMs?: number,\n * claimJitterMs?: number,\n * maxParallel?: number,\n * scanLimit?: number,\n * allowedTasks?: string | string[],\n * registry?: TasksRegistry | Record<string, Function>,\n * runnerServiceGroup?: string,\n * runnerServiceName?: string,\n * runnerInstanceNumber?: number,\n * runnerHeartbeatIntervalMs?: number,\n * runnerHeartbeatStaleMs?: number,\n * runnerGroupMaxInstances?: number,\n * runnerEnforceMaxInstances?: boolean,\n * runnerMetadata?: Record<string, unknown>,\n * }} options\n * @returns {Promise<void>}\n */\nexport async function runTasksLoop(context, options) {\n const queueName = options.queueName ?? \"tasks\";\n const target = options.target;\n const pollMs = options.pollMs ?? 1000;\n const claimJitterMs = options.claimJitterMs ?? 0;\n const maxParallel = options.maxParallel ?? 32;\n const scanLimit = options.scanLimit ?? 100;\n const allowedTasks = normalizeAllowedTasks(options.allowedTasks);\n const registry = normalizeRegistry(options.registry);\n const { tasksTable, historyTable } = queueToTableNames(queueName);\n\n if (!target) throw new Error(\"runTasksLoop: target is required\");\n\n context.tasksQueueName = queueName;\n\n const runningPromises = new Set();\n const runningTaskInstances = new Map();\n let runningStopControlPromise = null;\n let stopRequested = false;\n let stopAllowanceMs = 5000;\n context.tasksRunnerStop = false;\n\n let registryReg = null;\n let registryInterval = null;\n let runnerIdentity = null;\n const hbGroup = options.runnerServiceGroup?.trim();\n if (hbGroup) {\n const hbIntervalMs = options.runnerHeartbeatIntervalMs ?? 10_000;\n const staleMs = options.runnerHeartbeatStaleMs ?? 45_000;\n const defaultMeta = {\n component: \"tasks-runner\",\n allowedTasks: allowedTasks?.length ? allowedTasks.join(\",\") : \"all\",\n };\n registryReg = await registerInServicesRegistry(context, {\n queueName,\n target,\n serviceGroup: hbGroup,\n serviceName: options.runnerServiceName,\n instanceNumber: options.runnerInstanceNumber,\n staleMs,\n groupMaxInstances: options.runnerGroupMaxInstances,\n enforceMaxInstances: options.runnerEnforceMaxInstances ?? true,\n metadata: options.runnerMetadata ?? defaultMeta,\n });\n runnerIdentity = {\n service_name: registryReg.serviceName,\n server_name: os.hostname(),\n instance_number: registryReg.instanceNumber,\n };\n registryInterval = setInterval(() => {\n void touchServicesRegistry(context, registryReg).catch((err) => {\n context.logger.warn?.(`[services-registry] touch failed: ${err?.message ?? String(err)}`);\n });\n }, hbIntervalMs);\n }\n\n try {\n while (!context.isStop() && !stopRequested && context.tasksRunnerStop !== true) {\n // Control lane: always allow stop task to be picked even when workers are busy.\n if (!runningStopControlPromise) {\n const claimedStopTask = await claimNextRunnableTask(\n context,\n tasksTable,\n target,\n registry,\n 10,\n [\"stopRunner\", \"stop\"],\n runnerIdentity\n );\n if (claimedStopTask) {\n runningStopControlPromise = executeClaimedTask(\n context,\n tasksTable,\n historyTable,\n claimedStopTask,\n registry,\n runningTaskInstances\n )\n .then(async (outcome) => {\n if (outcome.stopRunnerRequested && !stopRequested) {\n stopRequested = true;\n stopAllowanceMs = outcome.stopAllowanceMs || 5000;\n context.tasksRunnerStop = true;\n await signalRunningTasksStop(context, runningTaskInstances, stopAllowanceMs);\n }\n })\n .finally(() => {\n runningStopControlPromise = null;\n });\n }\n }\n\n if (claimJitterMs > 0) {\n await sleepMs(Math.floor(Math.random() * (claimJitterMs + 1)));\n }\n\n while (runningPromises.size < maxParallel) {\n const claimed = await claimNextRunnableTask(\n context,\n tasksTable,\n target,\n registry,\n scanLimit,\n allowedTasks,\n runnerIdentity\n );\n if (!claimed) break;\n\n const p = executeClaimedTask(context, tasksTable, historyTable, claimed, registry, runningTaskInstances)\n .then(async (outcome) => {\n if (outcome.stopRunnerRequested && !stopRequested) {\n stopRequested = true;\n stopAllowanceMs = outcome.stopAllowanceMs || 5000;\n context.tasksRunnerStop = true;\n await signalRunningTasksStop(context, runningTaskInstances, stopAllowanceMs);\n }\n })\n .finally(() => {\n runningPromises.delete(p);\n });\n runningPromises.add(p);\n }\n\n const wakePromises = [...runningPromises];\n if (runningStopControlPromise) {\n wakePromises.push(runningStopControlPromise);\n }\n if (wakePromises.length === 0) {\n await sleepMs(pollMs);\n } else {\n const safe = wakePromises.map((p) => p.catch(() => undefined));\n await Promise.race([sleepMs(pollMs), Promise.race(safe)]);\n }\n }\n\n if (context.isStop() && !stopRequested) {\n await signalRunningTasksStop(context, runningTaskInstances, 5000);\n }\n\n if (runningPromises.size > 0) {\n if (stopRequested) {\n await Promise.race([\n Promise.allSettled(Array.from(runningPromises)),\n sleepMs(stopAllowanceMs).then(() => {\n context.logger.warn?.(\n `[tasks] stop allowance (${stopAllowanceMs}ms) reached; ${runningPromises.size} task(s) still running`\n );\n }),\n ]);\n } else {\n await Promise.allSettled(Array.from(runningPromises));\n }\n }\n } finally {\n if (registryInterval) {\n clearInterval(registryInterval);\n registryInterval = null;\n }\n if (registryReg) {\n await unregisterServicesRegistry(context, registryReg).catch((err) => {\n context.logger.warn?.(`[services-registry] unregister failed: ${err?.message ?? String(err)}`);\n });\n registryReg = null;\n delete context.servicesRegistry;\n delete context.runnerHeartbeat;\n }\n }\n}\n\n/**\n * Poll for the outcome of a task by id. Returns the matching `_history` row when\n * the task completes, or `null` when the wait times out / the queue row vanishes\n * without a history entry (unusual; usually a manual delete).\n *\n * Resolves either of:\n * - Legacy path: a history row whose `id` matches the original task id.\n * - Modern path: a history row with the same `name`+`opid` whose\n * `completed_at` is ≥ when we started waiting (so we don't pick up an older run).\n *\n * @param {object} context\n * @param {string} taskId\n * @param {{ queueName?: string, timeoutMs?: number, pollMs?: number }} [options]\n * @returns {Promise<object|null>}\n */\nexport async function waitForTaskResult(context, taskId, options = {}) {\n const db = getDb(context);\n const queueName = options.queueName ?? \"tasks\";\n const timeoutMs = options.timeoutMs ?? 60000;\n const pollMs = options.pollMs ?? 500;\n const { tasksTable, historyTable } = queueToTableNames(queueName);\n const deadline = Date.now() + timeoutMs;\n /** Only match history rows completed after we began waiting (avoids picking an older run with the same name/opid). */\n const waitStartedAt = new Date();\n let cachedNameOpid = null;\n\n /**\n * Look for a matching history entry completed since we started waiting.\n *\n * @param {string} name\n * @param {string|null|undefined} opid\n * @returns {Promise<object|undefined>}\n */\n async function historySinceWait(name, opid) {\n let q = db(historyTable).where({ name }).where(\"completed_at\", \">=\", waitStartedAt);\n if (opid == null || opid === \"\") {\n q = q.whereNull(\"opid\");\n } else {\n q = q.where({ opid });\n }\n return await q.orderBy(\"completed_at\", \"desc\").first();\n }\n\n while (Date.now() <= deadline) {\n const legacy = await db(historyTable).where({ id: taskId }).orderBy(\"created_at\", \"desc\").first();\n if (legacy) {\n return legacy;\n }\n\n const pending = await db(tasksTable).where({ id: taskId }).first();\n if (pending) {\n cachedNameOpid = { name: pending.name, opid: pending.opid };\n const done = await historySinceWait(pending.name, pending.opid);\n if (done) {\n return done;\n }\n } else if (cachedNameOpid) {\n const done = await historySinceWait(cachedNameOpid.name, cachedNameOpid.opid);\n if (done) {\n return done;\n }\n return null;\n } else {\n return null;\n }\n await sleepMs(pollMs);\n }\n return null;\n}\n\n/**\n * Higher-level wrapper over {@link runTasksLoop}: captures defaults / params-driven\n * config at construction, then exposes them as methods (`ensureTaskTables`,\n * `runTasksLoop`) so callers don't have to plumb the same options through twice.\n *\n * Prefer `TasksManager.init(context)` over the bare constructor — `init` pulls\n * sensible defaults from `context.params` using the `tasks` module namespace.\n */\nexport class TasksManager {\n /**\n * @param {object} context\n * @param {{\n * queueName?: string,\n * target?: string,\n * recreateTaskTables?: boolean,\n * pollMs?: number,\n * claimJitterMs?: number,\n * maxParallel?: number,\n * scanLimit?: number,\n * allowedTasks?: string | string[],\n * registry?: TasksRegistry | Record<string, Function>,\n * runnerServiceGroup?: string,\n * runnerServiceName?: string,\n * runnerInstanceNumber?: number,\n * runnerHeartbeatIntervalMs?: number,\n * runnerHeartbeatStaleMs?: number,\n * runnerGroupMaxInstances?: number,\n * runnerEnforceMaxInstances?: boolean,\n * runnerMetadata?: Record<string, unknown>,\n * }} [options]\n */\n constructor(context, options = {}) {\n this.context = context;\n this.queueName = options.queueName ?? \"tasks\";\n this.target = options.target ?? \"localRunner\";\n this.recreateTaskTables = options.recreateTaskTables ?? false;\n this.pollMs = options.pollMs ?? 1000;\n this.claimJitterMs = options.claimJitterMs ?? 0;\n this.maxParallel = options.maxParallel ?? 1;\n this.scanLimit = options.scanLimit ?? 100;\n this.allowedTasks = normalizeAllowedTasks(options.allowedTasks);\n this.registry = normalizeRegistry(options.registry);\n this.runnerServiceGroup = options.runnerServiceGroup;\n this.runnerServiceName = options.runnerServiceName;\n this.runnerInstanceNumber = options.runnerInstanceNumber;\n this.runnerHeartbeatIntervalMs = options.runnerHeartbeatIntervalMs;\n this.runnerHeartbeatStaleMs = options.runnerHeartbeatStaleMs;\n this.runnerGroupMaxInstances = options.runnerGroupMaxInstances;\n this.runnerEnforceMaxInstances = options.runnerEnforceMaxInstances;\n this.runnerMetadata = options.runnerMetadata;\n }\n\n /**\n * Preferred factory: reads defaults from `context.params` (module namespace\n * `\"tasks\"`), then overlays explicit `options`. Keeps CLI flags, env vars,\n * and inline options in one consistent resolver.\n *\n * @param {object} context\n * @param {ConstructorParameters<typeof TasksManager>[1]} [options]\n * @returns {TasksManager}\n */\n static init(context, options = {}) {\n const defs = {\n table: \"string default tasks\",\n target: \"string default localRunner\",\n recreateTaskTables: \"boolean default false\",\n pollMs: \"number default 1000\",\n claimJitterMs: \"number default 0\",\n maxParallel: \"number default 1\",\n scanLimit: \"number default 100\",\n allowedTasks: \"string\",\n runnerServiceGroup: \"string\",\n runnerServiceName: \"string\",\n runnerInstanceNumber: \"number\",\n runnerHeartbeatIntervalMs: \"number default 10000\",\n runnerHeartbeatStaleMs: \"number default 45000\",\n runnerGroupMaxInstances: \"number\",\n runnerEnforceMaxInstances: \"boolean default true\",\n };\n\n const discovered = context.params.getAllForModule(\"tasks\", defs);\n const resolved = {\n queueName: discovered.table,\n target: discovered.target,\n recreateTaskTables: discovered.recreateTaskTables,\n pollMs: discovered.pollMs,\n claimJitterMs: discovered.claimJitterMs,\n maxParallel: discovered.maxParallel,\n scanLimit: discovered.scanLimit,\n allowedTasks: discovered.allowedTasks,\n runnerServiceGroup: discovered.runnerServiceGroup,\n runnerServiceName: discovered.runnerServiceName,\n runnerInstanceNumber: discovered.runnerInstanceNumber,\n runnerHeartbeatIntervalMs: discovered.runnerHeartbeatIntervalMs,\n runnerHeartbeatStaleMs: discovered.runnerHeartbeatStaleMs,\n runnerGroupMaxInstances: discovered.runnerGroupMaxInstances,\n runnerEnforceMaxInstances: discovered.runnerEnforceMaxInstances,\n ...options,\n };\n return new TasksManager(context, resolved);\n }\n\n /**\n * Idempotently ensure the three backing tables exist for this queue.\n *\n * @param {{ recreate?: boolean }} [options]\n * @returns {Promise<void>}\n */\n async ensureTaskTables(options = {}) {\n await ensureTaskTables(this.context, {\n queueName: this.queueName,\n recreate: options.recreate ?? this.recreateTaskTables,\n });\n }\n\n /**\n * Start the runner loop using this manager's resolved config. Per-call\n * options override the stored defaults, but `runnerMetadata` still falls\n * through when omitted.\n *\n * @param {Partial<ConstructorParameters<typeof TasksManager>[1]>} [options]\n * @returns {Promise<void>}\n */\n async runTasksLoop(options = {}) {\n await runTasksLoop(this.context, {\n queueName: options.queueName ?? this.queueName,\n target: options.target ?? this.target,\n pollMs: options.pollMs ?? this.pollMs,\n claimJitterMs: options.claimJitterMs ?? this.claimJitterMs,\n maxParallel: options.maxParallel ?? this.maxParallel,\n scanLimit: options.scanLimit ?? this.scanLimit,\n allowedTasks: options.allowedTasks ?? this.allowedTasks,\n registry: options.registry ?? this.registry,\n runnerServiceGroup: options.runnerServiceGroup ?? this.runnerServiceGroup,\n runnerServiceName: options.runnerServiceName ?? this.runnerServiceName,\n runnerInstanceNumber: options.runnerInstanceNumber ?? this.runnerInstanceNumber,\n runnerHeartbeatIntervalMs: options.runnerHeartbeatIntervalMs ?? this.runnerHeartbeatIntervalMs,\n runnerHeartbeatStaleMs: options.runnerHeartbeatStaleMs ?? this.runnerHeartbeatStaleMs,\n runnerGroupMaxInstances: options.runnerGroupMaxInstances ?? this.runnerGroupMaxInstances,\n runnerEnforceMaxInstances: options.runnerEnforceMaxInstances ?? this.runnerEnforceMaxInstances,\n runnerMetadata: options.runnerMetadata ?? this.runnerMetadata,\n });\n }\n}\n","/**\n * Date/time utility functions for timestamp formatting and timezone conversions\n */\n\n\n\n\n\n\n/**\n * Format a date string or Date object to ISO8601 format in specified timezone\n * Always returns UTC-based ISO strings (YYYY-MM-DDTHH:mm:ssZ) as internal representation\n * \n * @param value - Date object, ISO string, or timestamp\n * @param options - Formatting options\n * @returns ISO8601 formatted string\n */\nexport function formatDate(value, options = {}) {\n if (value === undefined || value === null) {\n return \"\";\n }\n\n const { timezone = \"UTC\", format = \"iso\" } = options;\n \n let date;\n if (value instanceof Date) {\n date = value;\n } else if (typeof value === \"string\") {\n date = new Date(value);\n } else if (typeof value === \"number\") {\n date = new Date(value);\n } else {\n return \"\";\n }\n\n if (isNaN(date.getTime())) {\n return \"\";\n }\n\n // Format based on requested format\n switch (format) {\n case \"iso\":\n // Full ISO8601: 2025-01-01T01:01:01Z\n return date.toISOString();\n \n case \"iso-date\":\n // Date only: 2025-01-01\n return date.toISOString().split(\"T\")[0];\n \n case \"iso-time\":\n // Time only: 01:01:01Z\n return date.toISOString().split(\"T\")[1];\n \n case \"human\":\n // Human-readable: Jan 1, 2025 01:01:01 UTC\n if (timezone === \"user\") {\n return date.toLocaleString();\n }\n return date.toUTCString();\n \n default:\n return date.toISOString();\n }\n}\n\n/**\n * Parse a date value and return ISO8601 string in UTC\n * This is the canonical format for internal storage\n * \n * @param value - Date object or string\n * @returns ISO8601 string in UTC timezone\n */\nexport function toISOString(value) {\n return formatDate(value, { timezone: \"UTC\", format: \"iso\" });\n}\n\n/**\n * Get current timestamp as ISO8601 string in UTC\n * @returns Current time as ISO8601 string\n */\nexport function nowISO() {\n return new Date().toISOString();\n}\n\n/**\n * Parse ISO8601 string to Date object\n * @param isoString - ISO8601 formatted string\n * @returns Date object\n */\nexport function fromISOString(isoString) {\n return new Date(isoString);\n}\n\n/**\n * Calculate difference between two dates in various units\n * @param start - Start date (ISO string or Date)\n * @param end - End date (ISO string or Date)\n * @returns Object with duration in multiple units\n */\nexport function dateDiff(\n start,\n end\n)\n\n\n\n\n\n{\n const startDate = start instanceof Date ? start : new Date(start);\n const endDate = end instanceof Date ? end : new Date(end);\n \n const diffMs = endDate.getTime() - startDate.getTime();\n \n return {\n milliseconds: diffMs,\n seconds: Math.floor(diffMs / 1000),\n minutes: Math.floor(diffMs / (1000 * 60)),\n hours: Math.floor(diffMs / (1000 * 60 * 60)),\n days: Math.floor(diffMs / (1000 * 60 * 60 * 24))\n };\n}\n\n/**\n * Check if a folder name looks like an ISO8601 timestamp\n * Used to identify version folders vs regular folders\n * @param folderName - Folder name to check\n * @returns true if the folder name is a valid ISO8601 timestamp\n */\nexport function isTimestampFolder(folderName) {\n // Match ISO8601 format: YYYY-MM-DDTHH:mm:ssZ\n const isoRegex = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(Z|\\.\\d{3}Z)$/;\n \n if (!isoRegex.test(folderName)) {\n return false;\n }\n \n const date = new Date(folderName);\n return !isNaN(date.getTime()) && date.getTime() > 0;\n}\n\n/**\n * Generate a new version name (ISO8601 timestamp)\n * If existingVersions provided, ensures the new version is later than all existing ones\n * @param existingVersions - Array of existing version timestamps\n * @returns New version timestamp that is unique and later than existing ones\n */\nexport function generateVersionName(existingVersions = []) {\n if (existingVersions.length === 0) {\n // No existing versions, use current timestamp\n const now = new Date();\n return now.toISOString().split(\".\")[0] + \"Z\";\n }\n \n // Find the maximum timestamp among all existing versions\n const maxTimestamp = existingVersions.reduce((max, version) => {\n const versionDate = new Date(version);\n const maxDate = new Date(max);\n return versionDate > maxDate ? version : max;\n });\n \n // Increment by 1 second to ensure uniqueness\n const maxDate = new Date(maxTimestamp);\n const nextDate = new Date(maxDate.getTime() + 1000);\n \n return nextDate.toISOString().split(\".\")[0] + \"Z\";\n}\n\n","/**\n * File System Utilities\n * \n * Helper functions for file system operations like path management,\n * directory creation, and file type detection\n */\n\nimport fs from \"fs\";\nimport path from \"path\";\nimport { fileURLToPath } from \"url\";\n\n/**\n * Ensure a directory path exists, creating it if necessary\n * Returns the absolute path\n */\nexport async function ensurePath(...pathParts) {\n const fullPath = path.resolve(...pathParts);\n \n if (!fs.existsSync(fullPath)) {\n await fs.promises.mkdir(fullPath, { recursive: true });\n }\n \n return fullPath;\n}\n\n/**\n * Synchronous version of ensurePath\n */\nexport function ensurePathSync(...pathParts) {\n const fullPath = path.resolve(...pathParts);\n \n if (!fs.existsSync(fullPath)) {\n fs.mkdirSync(fullPath, { recursive: true });\n }\n \n return fullPath;\n}\n\n/**\n * Get file extension for a given data type\n */\nexport function getFileExtension(dataType) {\n switch (dataType) {\n case \"json-array\":\n case \"json-object\":\n return \"json\";\n case \"text\":\n return \"txt\";\n case \"xml\":\n return \"xml\";\n default:\n return \"json\";\n }\n}\n\n/**\n * Get __dirname equivalent for ES modules\n * \n * Returns the directory path of the module file\n * Useful for resolving relative paths in ES modules where __dirname is not available\n * \n * @param metaUrl - The import.meta.url from the calling module (must be passed from calling module)\n * @returns The directory path of the module\n * \n * @example\n * ```typescript\n * import { getDirname } from \"@nmakarov/cli-toolkit/utils\";\n * const __dirname = getDirname(import.meta.url);\n * ```\n */\nexport function getDirname(metaUrl) {\n return path.dirname(fileURLToPath(metaUrl));\n}\n\n","/**\n * Operating System Utilities\n * \n * Helper functions for interacting with the operating system\n * to get system statistics and information\n */\n\nimport fs from \"fs\";\nimport path from \"path\";\nimport { execSync } from \"child_process\";\n\n/**\n * Get free disk space for a given path (in bytes)\n * Returns null if unable to determine\n * Uses `df` command on Unix-like systems\n */\nexport function getFreeDiskSpace(targetPath) {\n try {\n // If the target path doesn't exist, use its parent directory\n let pathToCheck = targetPath;\n if (!fs.existsSync(targetPath)) {\n const parentDir = path.dirname(targetPath);\n if (fs.existsSync(parentDir)) {\n pathToCheck = parentDir;\n } else {\n // If parent doesn't exist either, use the root directory\n pathToCheck = process.platform === \"win32\" ? \"C:\\\\\" : \"/\";\n }\n }\n\n if (process.platform === \"win32\") {\n // Windows: use wmic command\n // Note: This is a simplified implementation, may need adjustments\n return null; // TODO: Implement robust Windows support\n } else {\n // Unix-like systems: use df command\n const stdout = execSync(`df -k \"${pathToCheck}\"`, { encoding: \"utf8\" });\n const lines = stdout.trim().split(\"\\n\");\n const parts = lines[1].split(/\\s+/); // second line, split on whitespace\n const freeKb = parseInt(parts[3], 10); // 4th column is \"Available\"\n return freeKb * 1024; // Convert to bytes\n }\n } catch (error) {\n // Silently fail and return null\n return null;\n }\n}\n\n","/**\n * Formatting Utilities\n * \n * Helper functions for converting between different data formats\n * and human-readable representations\n */\n\n/**\n * Convert bytes to human-readable format (e.g., \"1.5 MB\")\n */\nexport function bytesToHumanReadable(bytes) {\n if (bytes === 0) return \"0 B\";\n \n const k = 1024;\n const sizes = [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\"];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n \n return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + \" \" + sizes[i];\n}\n\n/**\n * Convert human-readable format to bytes (e.g., \"1.5 MB\" -> 1572864)\n */\nexport function humanReadableToBytes(humanString) {\n const units = {\n \"B\": 1,\n \"KB\": 1024,\n \"MB\": 1024 * 1024,\n \"GB\": 1024 * 1024 * 1024,\n \"TB\": 1024 * 1024 * 1024 * 1024,\n \"PB\": 1024 * 1024 * 1024 * 1024 * 1024,\n };\n \n const match = humanString.trim().match(/^([\\d.]+)\\s*([A-Z]+)$/i);\n if (!match) {\n throw new Error(`Invalid format: ${humanString}. Expected format like \"2MB\" or \"1.5 GB\"`);\n }\n \n const value = parseFloat(match[1]);\n const unit = match[2].toUpperCase();\n \n if (!units[unit]) {\n throw new Error(`Unknown unit: ${unit}. Supported units: ${Object.keys(units).join(\", \")}`);\n }\n \n return Math.round(value * units[unit]);\n}\n\n","/**\n * Core lightweight utilities\n */\n\nexport function sleepMs(ms) {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport function toJsonColumn(value) {\n if (value === undefined || value === null) return null;\n return JSON.stringify(value);\n}\n","import os from \"node:os\";\nimport { toJsonColumn } from \"../utils/index.js\";\nimport { queueToTableNames } from \"./taskUtils.js\";\n\n/**\n * Fail-fast accessor for `context.db`. The services-registry never lazy-inits the DB;\n * if it's missing, that's a caller wiring mistake.\n *\n * @param {object} context\n * @returns {Function}\n */\nfunction getDb(context) {\n const db = context.db;\n if (!db) {\n throw new Error(\"Services registry requires context.db\");\n }\n return db;\n}\n\n/**\n * Decode a JSON column value into a plain object. Returns `{}` for nulls,\n * arrays, parse errors — callers never get `null`/`undefined` back so they can\n * safely spread the result.\n *\n * @param {unknown} value\n * @returns {Record<string, unknown>}\n */\nfunction parseMetadataColumn(value) {\n if (!value) return {};\n if (typeof value === \"object\" && !Array.isArray(value)) return value;\n if (typeof value === \"string\") {\n try {\n const p = JSON.parse(value);\n return p && typeof p === \"object\" && !Array.isArray(value) ? p : {};\n } catch {\n return {};\n }\n }\n return {};\n}\n\n/** Default max concurrent *alive* services per group (0 = unlimited). Override with runnerGroupMaxInstances. */\nconst DEFAULT_GROUP_MAX_INSTANCES = {\n intake: 1,\n harvest: 1,\n harvester: 0,\n loader: 0,\n photos: 0,\n photosprocessor: 0,\n ingest: 0,\n};\n\n/**\n * Produce a DB-safe, human-readable name part: letters / digits / `._-` only,\n * trimmed and capped at 80 chars. Empty inputs fall back to `\"runner\"`.\n *\n * @param {unknown} raw\n * @returns {string}\n */\nfunction sanitizeNamePart(raw) {\n const s = String(raw || \"\")\n .trim()\n .replace(/[^a-zA-Z0-9._-]+/g, \"-\")\n .replace(/^-+|-+$/g, \"\");\n return s.slice(0, 80) || \"runner\";\n}\n\n/**\n * How many alive instances are allowed in this group? Explicit `override` wins;\n * otherwise use the per-group default (lowercased group name). `0` = unlimited.\n *\n * @param {string} serviceGroup\n * @param {number|undefined} override\n * @returns {number}\n */\nfunction resolveMaxInstances(serviceGroup, override) {\n if (override !== undefined && Number.isFinite(override)) {\n return Math.max(0, Math.floor(Number(override)));\n }\n const g = serviceGroup.trim().toLowerCase();\n return DEFAULT_GROUP_MAX_INSTANCES[g] ?? 0;\n}\n\n/**\n * Count rows in a group that have heartbeat'd within `staleMs`. Used to gate\n * `groupMaxInstances` at registration time.\n *\n * @param {Function} db knex instance.\n * @param {string} registryTable\n * @param {string} queueName\n * @param {string} serviceGroup\n * @param {number} staleMs\n * @param {string|undefined} excludeRowId When retrying, skip the row we're about to reuse.\n * @returns {Promise<number>}\n */\nasync function countAliveInGroup(\n db,\n registryTable,\n queueName,\n serviceGroup,\n staleMs,\n excludeRowId\n) {\n const cutoff = new Date(Date.now() - staleMs);\n let q = db(registryTable)\n .where({ queue_name: queueName, service_group: serviceGroup })\n .where(\"last_seen_at\", \">\", cutoff);\n if (excludeRowId) {\n q = q.whereNot(\"id\", excludeRowId);\n }\n const row = await q.count(\"id as count\").first();\n return Number(row?.count ?? 0);\n}\n\n/**\n * Instance numbers currently held by *alive* rows (fresh last_seen).\n *\n * @param {Function} db\n * @param {string} registryTable\n * @param {string} queueName\n * @param {string} serviceGroup\n * @param {number} staleMs\n * @returns {Promise<Set<number>>}\n */\nasync function getOccupiedInstanceSlots(\n db,\n registryTable,\n queueName,\n serviceGroup,\n staleMs\n) {\n const cutoff = new Date(Date.now() - staleMs);\n const rows = await db(registryTable)\n .where({ queue_name: queueName, service_group: serviceGroup })\n .where(\"last_seen_at\", \">\", cutoff)\n .select(\"instance_number\");\n const set = new Set();\n for (const r of rows) {\n const n = Number(r.instance_number);\n if (Number.isFinite(n) && n >= 1) set.add(Math.floor(n));\n }\n return set;\n}\n\n/**\n * Best-effort detection of Postgres unique-constraint violations. Covers both\n * pg's `23505` SQLSTATE and drivers that only surface the error message.\n *\n * @param {unknown} error\n * @returns {boolean}\n */\nfunction isUniqueViolation(error) {\n const code = error?.code ?? error?.errno;\n return code === \"23505\" || String(error?.message || \"\").includes(\"duplicate key\");\n}\n\n/**\n * Package the `metadata` JSON column payload for a new/updated registry row.\n * Always carries `runnerTarget` when provided so read-side filters can see it\n * without decoding the whole blob.\n *\n * @param {{ metadata?: Record<string, unknown>, target?: string }} options\n * @returns {string|null}\n */\nfunction buildMetadata(options) {\n const base = options.metadata && typeof options.metadata === \"object\" ? { ...options.metadata } : {};\n if (options.target) {\n base.runnerTarget = options.target;\n }\n return toJsonColumn(Object.keys(base).length ? base : null);\n}\n\n/**\n * Pick first free instance number: smallest n >= 1 with n ∉ occupied.\n * If explicit is set, use it only if not in occupied and within maxSlots (when > 0).\n *\n * @param {Set<number>} occupied\n * @param {number|undefined|null} explicit\n * @param {number|undefined} maxSlots\n * @returns {number}\n */\nfunction allocateInstanceNumber(occupied, explicit, maxSlots) {\n if (explicit !== undefined && explicit !== null && Number.isFinite(Number(explicit))) {\n const e = Math.max(1, Math.floor(Number(explicit)));\n if (occupied.has(e)) {\n throw new Error(`[services-registry] instance slot ${e} is already occupied by an alive peer`);\n }\n if (maxSlots !== undefined && maxSlots > 0 && e > maxSlots) {\n throw new Error(`[services-registry] instance ${e} exceeds configured max slots ${maxSlots} for this group`);\n }\n return e;\n }\n const cap = maxSlots !== undefined && maxSlots > 0 ? maxSlots : 10_000;\n for (let n = 1; n <= cap; n++) {\n if (!occupied.has(n)) return n;\n }\n throw new Error(`[services-registry] no free instance slot (searched 1..${cap})`);\n}\n\n/**\n * Build a conventional `service_name` when the caller didn't pick one:\n * `<group>-<host>-<instance>`.\n *\n * @param {string} groupBase\n * @param {string} hostBase\n * @param {number} instanceNumber\n * @returns {string}\n */\nfunction defaultServiceName(groupBase, hostBase, instanceNumber) {\n return `${groupBase}-${hostBase}-${instanceNumber}`;\n}\n\n/**\n * Register this process in `{queue}_services_registry` (no local identity files).\n * Allocates the first free instance number among *alive* peers, then inserts or takes over a stale row\n * with the same `service_name` when restarting on the same host/name pattern.\n *\n * @param {object} context\n * @param {{\n * queueName: string,\n * target?: string,\n * serviceGroup: string,\n * serviceName?: string,\n * instanceNumber?: number|null,\n * staleMs: number,\n * groupMaxInstances?: number,\n * enforceMaxInstances?: boolean,\n * metadata?: Record<string, unknown>,\n * }} options\n * @returns {Promise<{\n * serviceName: string,\n * serviceGroup: string,\n * queueName: string,\n * target?: string,\n * rowId: string,\n * registryTable: string,\n * instanceNumber: number,\n * }>}\n */\nexport async function registerInServicesRegistry(context, options) {\n const db = getDb(context);\n const registryTable = queueToTableNames(options.queueName).registryTable;\n const serviceGroup = options.serviceGroup.trim();\n if (!serviceGroup) {\n throw new Error(\"registerInServicesRegistry: serviceGroup is required\");\n }\n\n const serverName = os.hostname();\n const pid = typeof process.pid === \"number\" ? process.pid : null;\n const meta = buildMetadata(options);\n const groupBase = sanitizeNamePart(serviceGroup);\n const hostBase = sanitizeNamePart(serverName);\n\n const maxAllowed = resolveMaxInstances(serviceGroup, options.groupMaxInstances);\n const aliveCount = await countAliveInGroup(db, registryTable, options.queueName, serviceGroup, options.staleMs, undefined);\n\n if (maxAllowed > 0 && aliveCount >= maxAllowed) {\n const msg = `[services-registry] group limit reached for \"${serviceGroup}\": ${aliveCount} alive (max ${maxAllowed}, queue=${options.queueName}).`;\n if (options.enforceMaxInstances) {\n throw new Error(msg);\n }\n context.logger.warn?.(`${msg} Starting anyway (runnerEnforceMaxInstances=false).`);\n }\n\n const maxSlots = maxAllowed > 0 ? maxAllowed : undefined;\n const cutoff = new Date(Date.now() - options.staleMs);\n\n const MAX_ATTEMPTS = 8;\n for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {\n const occupied = await getOccupiedInstanceSlots(db, registryTable, options.queueName, serviceGroup, options.staleMs);\n const instanceNumber = allocateInstanceNumber(occupied, options.instanceNumber, maxSlots);\n\n const serviceNameRaw = options.serviceName?.trim()\n ? sanitizeNamePart(options.serviceName.trim())\n : defaultServiceName(groupBase, hostBase, instanceNumber);\n\n const existing = await db(registryTable)\n .where({ queue_name: options.queueName, service_name: serviceNameRaw })\n .first();\n\n if (existing) {\n const lastSeen = new Date(existing.last_seen_at);\n const isAlive = !Number.isNaN(lastSeen.getTime()) && lastSeen > cutoff;\n\n if (isAlive) {\n if (options.serviceName?.trim()) {\n throw new Error(\n `[services-registry] service_name \"${serviceNameRaw}\" is already registered by an alive peer`\n );\n }\n context.logger.warn?.(\n `[services-registry] service_name \"${serviceNameRaw}\" already alive; retrying allocation (attempt ${attempt + 1})`\n );\n if (options.instanceNumber !== undefined && options.instanceNumber !== null) {\n throw new Error(\n `[services-registry] instance slot ${instanceNumber} / name \"${serviceNameRaw}\" is already held by an alive peer`\n );\n }\n await new Promise((r) => setTimeout(r, 50 + attempt * 30));\n continue;\n }\n\n await db(registryTable)\n .where({ id: existing.id })\n .update({\n server_name: serverName,\n pid,\n metadata: meta,\n service_group: serviceGroup,\n instance_number: instanceNumber,\n last_seen_at: db.fn.now(),\n });\n\n const reg = {\n serviceName: serviceNameRaw,\n serviceGroup,\n queueName: options.queueName,\n target: options.target,\n rowId: String(existing.id),\n registryTable,\n instanceNumber,\n };\n context.servicesRegistry = reg;\n context.runnerHeartbeat = reg;\n\n context.logger.info?.(\n `[services-registry] took over stale row name=${serviceNameRaw} instance=${instanceNumber} group=${serviceGroup} queue=${options.queueName}`\n );\n return reg;\n }\n\n try {\n const rows = await db(registryTable)\n .insert({\n queue_name: options.queueName,\n service_group: serviceGroup,\n instance_number: instanceNumber,\n service_name: serviceNameRaw,\n server_name: serverName,\n pid,\n metadata: meta,\n last_seen_at: db.fn.now(),\n created_at: db.fn.now(),\n })\n .returning([\"id\", \"service_name\"]);\n\n const row = Array.isArray(rows) ? rows[0] : rows;\n let rowId = row && typeof row === \"object\" ? String(row.id ?? \"\") : \"\";\n if (!rowId) {\n const again = await db(registryTable)\n .where({ queue_name: options.queueName, service_name: serviceNameRaw })\n .first();\n rowId = again?.id != null ? String(again.id) : \"\";\n }\n if (!rowId) continue;\n\n const regNew = {\n serviceName: String(row?.service_name ?? serviceNameRaw),\n serviceGroup,\n queueName: options.queueName,\n target: options.target,\n rowId,\n registryTable,\n instanceNumber,\n };\n context.servicesRegistry = regNew;\n context.runnerHeartbeat = regNew;\n\n context.logger.info?.(\n `[services-registry] registered name=${regNew.serviceName} instance=${instanceNumber} group=${serviceGroup} queue=${options.queueName}`\n );\n return regNew;\n } catch (error) {\n if (!isUniqueViolation(error)) {\n throw error;\n }\n context.logger.warn?.(`[services-registry] insert race on \"${serviceNameRaw}\", retrying (attempt ${attempt + 1})`);\n }\n }\n\n throw new Error(\n `[services-registry] could not allocate a registry row for group=${serviceGroup} queue=${options.queueName} after ${MAX_ATTEMPTS} attempts`\n );\n}\n\n/**\n * Heartbeat: bump `last_seen_at`, refresh `server_name` / `pid` in case the\n * hostname rotates or the process PID changes (container restart in place).\n *\n * @param {object} context\n * @param {{ registryTable: string, rowId: string }} registration\n * @returns {Promise<void>}\n */\nexport async function touchServicesRegistry(context, registration) {\n const db = getDb(context);\n const serverName = os.hostname();\n const pid = typeof process.pid === \"number\" ? process.pid : null;\n await db(registration.registryTable)\n .where({ id: registration.rowId })\n .update({\n last_seen_at: db.fn.now(),\n server_name: serverName,\n pid,\n });\n}\n\n/**\n * Merge metadata (e.g. new allowedTasks / role) for this service row. Bumps last_seen_at.\n * Use when a service changes what it handles without restarting the process.\n *\n * @param {object} context\n * @param {{ registryTable: string, rowId: string, serviceName: string }} registration\n * @param {Record<string, unknown>} patch\n * @returns {Promise<void>}\n */\nexport async function updateServicesRegistryMetadata(context, registration, patch) {\n const db = getDb(context);\n const row = await db(registration.registryTable).where({ id: registration.rowId }).first();\n const prev = parseMetadataColumn(row?.metadata);\n const merged = { ...prev, ...patch };\n await db(registration.registryTable)\n .where({ id: registration.rowId })\n .update({\n metadata: toJsonColumn(merged),\n last_seen_at: db.fn.now(),\n });\n context.logger.info?.(`[services-registry] metadata updated for ${registration.serviceName}`);\n}\n\n/**\n * Drop this process's registry row. Call on graceful shutdown so peers don't\n * have to wait for `staleMs` to reclaim the slot.\n *\n * @param {object} context\n * @param {{ registryTable: string, rowId: string, serviceName: string }} registration\n * @returns {Promise<void>}\n */\nexport async function unregisterServicesRegistry(context, registration) {\n const db = getDb(context);\n await db(registration.registryTable).where({ id: registration.rowId }).delete();\n context.logger.info?.(`[services-registry] unregistered name=${registration.serviceName} id=${registration.rowId}`);\n}\n\n/**\n * List registry rows (optionally filter by queue / group). Rows with last_seen older than staleMs are excluded.\n *\n * @param {object} context\n * @param {{ queueName: string, staleMs?: number, serviceGroup?: string }} [options]\n * @returns {Promise<object[]>}\n */\nexport async function listServicesRegistry(context, options = { queueName: \"tasks\" }) {\n const db = getDb(context);\n const staleMs = options.staleMs ?? 60_000;\n const cutoff = new Date(Date.now() - staleMs);\n const table = queueToTableNames(options.queueName).registryTable;\n let q = db(table).where(\"last_seen_at\", \">\", cutoff).orderBy([{ column: \"service_group\", order: \"asc\" }, { column: \"service_name\", order: \"asc\" }]);\n if (options.serviceGroup?.trim()) {\n q = q.where({ service_group: options.serviceGroup.trim() });\n }\n return await q;\n}\n","import { randomUUID } from \"node:crypto\";\nimport { toJsonColumn } from \"../utils/index.js\";\nimport { nextTimeMatch } from \"./time-matcher.js\";\n\n//KEEP THIS FOR REFERENCE !!!\n// this is a sample of how to add a column to a table if it does not exist\n// const tasksHasOpid = await db.schema.hasColumn(tasksTable, \"opid\");\n// if (!tasksHasOpid) {\n// await db.schema.alterTable(tasksTable, (t) => {\n// t.text(\"opid\");\n// });\n// }\n\n\n/**\n * Fail-fast accessor for the knex instance on `context.db`. The tasks component\n * assumes the DB is already initialized by the caller — we don't lazy-init here,\n * so a missing `db` is a programming error, not a runtime condition.\n *\n * @param {object} context\n * @returns {Function} knex instance\n */\nfunction getDb(context) {\n const db = context.db;\n if (!db) {\n throw new Error(\"Tasks component requires context.db. Initialize DB first and attach to context.\");\n }\n return db;\n}\n\n/**\n * Given a queue name, derive the three related table names the runtime uses:\n *\n * - `tasksTable` — active queue (rows that may still run)\n * - `historyTable` — append-only audit of completed attempts\n * - `registryTable` — live service/runner heartbeats for the queue\n *\n * @param {string} queueName\n * @returns {{ tasksTable: string, historyTable: string, registryTable: string }}\n */\nexport function queueToTableNames(queueName) {\n return {\n tasksTable: queueName,\n historyTable: `${queueName}_history`,\n registryTable: `${queueName}_services_registry`,\n };\n}\n\n/**\n * Column definition shared by the active queue and its history mirror. Kept in\n * one place so the two tables stay structurally compatible (history receives a\n * full row snapshot).\n *\n * @param {import(\"knex\").Knex.CreateTableBuilder} t\n * @param {import(\"knex\").Knex} db\n * @param {string} tableNameForIndex Used to name indexes uniquely per table.\n */\nfunction defineTasksTable(t, db, tableNameForIndex) {\n t.uuid(\"id\").primary().defaultTo(db.raw(\"uuid_generate_v4()\"));\n t.timestamp(\"created_at\").notNullable().defaultTo(db.fn.now());\n t.timestamp(\"started_at\");\n t.timestamp(\"completed_at\");\n /*\n * Priority: lower number = claimed first (see claimNextRunnableTask: ORDER BY priority ASC).\n * Suggested range 0–100: 0 = most urgent, 100 = least; default 50 for normal work.\n */\n t.integer(\"priority\").notNullable().defaultTo(50);\n\n t.text(\"schedule\");\n t.timestamp(\"next_run_at\").defaultTo(null);\n t.timestamp(\"past_due\").defaultTo(null);\n\n t.text(\"name\").notNullable();\n t.text(\"opid\");\n t.json(\"params\");\n\n // those are tagret identifiers, kind of who is going to run a task.\n t.text(\"service_group\"); // harvester, loader, photos, ...\n t.integer(\"instance_number\");\n t.text(\"service_name\"); // that's a \"<server_name>_<service_group>_<instance_number>\"\n t.text(\"server_name\"); // filled by runner when registering, auto.\n\n t.text(\"status\").notNullable().defaultTo(\"idle\"); // idle, running, completed, failed, paused\n t.timestamp(\"status_changed_at\").defaultTo(null);\n\n t.text(\"progress\");\n t.boolean(\"success\");\n t.json(\"results\");\n\n t.index([\"service_group\", \"status\", \"priority\", \"created_at\"], `${tableNameForIndex}_claim_idx`);\n t.index([\"service_group\", \"name\"], `${tableNameForIndex}_group_name_idx`);\n}\n\n/**\n * Build an insert payload for `*_history`: never copies the queue row `id` as the history PK — PostgreSQL default\n * generates a new `id`. The snapshot still carries `name`, `opid`, `params`, etc. for auditing and ad hoc queries.\n *\n * @param {object} row Original queue row.\n * @param {object} overrides Fields to override on the snapshot (e.g. `completed_at`, `success`).\n * @returns {object}\n */\nexport function taskHistoryInsertFromQueueRow(row, overrides) {\n const { id, ...snapshot } = row;\n void id;\n return {\n ...snapshot,\n ...overrides,\n };\n}\n\n/**\n * Idempotently create the three tables backing a queue (tasks / history / registry).\n * Pass `recreate: true` to drop-and-recreate, useful in dev/test.\n *\n * Requires the `uuid-ossp` extension; creates it on first run if missing.\n *\n * @param {object} context\n * @param {{ queueName?: string, recreate?: boolean }} [options]\n * @returns {Promise<void>}\n */\nexport async function ensureTaskTables(context, options = {}) {\n const queueName = options.queueName ?? \"tasks\";\n const recreate = options.recreate ?? false;\n const db = getDb(context);\n const { tasksTable, historyTable, registryTable } = queueToTableNames(queueName);\n\n const needsTasks = recreate ? true : !(await db.tableExists(tasksTable));\n const needsHistory = recreate ? true : !(await db.tableExists(historyTable));\n const needsRegistry = recreate ? true : !(await db.tableExists(registryTable));\n\n if (recreate) {\n await db.schema.dropTableIfExists(historyTable);\n await db.schema.dropTableIfExists(tasksTable);\n await db.schema.dropTableIfExists(registryTable);\n }\n\n if (needsTasks) {\n await db.raw(`CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"`);\n await db.schema.createTable(tasksTable, (t) => {\n defineTasksTable(t, db, tasksTable);\n });\n }\n\n if (needsHistory) {\n await db.schema.createTable(historyTable, (t) => {\n defineTasksTable(t, db, historyTable);\n });\n }\n\n if (needsRegistry) {\n await db.schema.createTable(registryTable, (t) => {\n t.uuid(\"id\").primary().defaultTo(db.raw(\"uuid_generate_v4()\"));\n\n t.text(\"queue_name\").notNullable();\n t.text(\"service_group\").notNullable(); // harvester, loader, photos, ...\n t.integer(\"instance_number\").notNullable().defaultTo(1);\n t.text(\"service_name\").notNullable(); // that's a \"<server_name>_<service_group>_<instance_number>\"\n t.text(\"server_name\").notNullable(); // filled by runner when registering, auto.\n t.integer(\"pid\");\n\n t.json(\"metadata\");\n\n t.timestamp(\"created_at\").notNullable().defaultTo(db.fn.now());\n t.timestamp(\"last_seen_at\").notNullable().defaultTo(db.fn.now());\n t.unique([\"queue_name\", \"service_name\"], `${registryTable}_queue_name_service_name_uniq`);\n t.index([\"queue_name\", \"service_group\", \"last_seen_at\"], `${registryTable}_queue_group_seen_idx`);\n t.index([\"queue_name\", \"last_seen_at\"], `${registryTable}_queue_seen_idx`);\n });\n\n // TODO: a reference is needed - task_history entry should reference the registry entry, so that we can easily find all executed tasks for a given service and calculate the average workload or identify if there's a bottleneck. Also may be used for the load balancing.\n }\n}\n\n/**\n * Insert one task row into the queue. Supports targeting (service_group + optional\n * instance/server), recurring schedules (cron-like 6-field string, see `time-matcher.js`),\n * and explicit `nextRunAt` overrides.\n *\n * @param {object} context\n * @param {{\n * queueName?: string,\n * name?: string,\n * task?: string,\n * params?: unknown,\n * opid?: string|null,\n * priority?: number,\n * schedule?: string|null,\n * nextRunAt?: Date|string|number|null,\n * serviceGroup?: string|null,\n * instanceNumber?: number|null,\n * serviceName?: string|null,\n * serverName?: string|null,\n * }} options\n * @returns {Promise<string>} The new task's UUID.\n */\nexport async function enqueueTask(context, options) {\n const db = getDb(context);\n const queueName = options.queueName ?? \"tasks\";\n const { tasksTable } = queueToTableNames(queueName);\n const id = randomUUID();\n\n const name = options.name ?? options.task;\n if (!name) {\n throw new Error(\"enqueueTask: name (or task) is required\");\n }\n\n const schedule = options.schedule?.trim() ? options.schedule : null;\n let nextRunAt = null;\n if (options.nextRunAt !== undefined) {\n nextRunAt = options.nextRunAt == null ? null : new Date(options.nextRunAt);\n } else if (schedule) {\n nextRunAt = nextTimeMatch(schedule, new Date());\n }\n\n await db(tasksTable).insert({\n id,\n name,\n params: toJsonColumn(options.params ?? null),\n opid: options.opid ?? null,\n priority: options.priority ?? 50,\n schedule,\n next_run_at: nextRunAt,\n service_group: options.serviceGroup ?? null,\n instance_number: options.instanceNumber ?? null,\n service_name: options.serviceName ?? null,\n server_name: options.serverName ?? null,\n status: \"idle\",\n status_changed_at: db.fn.now(),\n });\n return id;\n}\n\n/**\n * Update the `progress` column for one task. Strings go in verbatim; anything\n * else gets JSON-stringified so the column stays text-friendly.\n *\n * @param {object} context\n * @param {string} tasksTable\n * @param {string} taskId\n * @param {unknown} progress\n * @returns {Promise<void>}\n */\nexport async function updateTaskProgress(context, tasksTable, taskId, progress) {\n const db = getDb(context);\n await db(tasksTable).where({ id: taskId }).update({\n progress: typeof progress === \"string\" ? progress : JSON.stringify(progress),\n });\n}\n","/**\n * 6-field cron-like matcher: `sec min hour day month weekday`.\n * Fields support `*`, numeric literals, ranges (`1-5`), comma lists (`0,15,30,45`),\n * and step expressions (`*\\/10`, `0-30/5`). No named months/weekdays, no\n * `L`/`#`/`?` magic — keep patterns explicit.\n */\n\n/** Field index → `\"lo-hi\"` range that `*` expands into. Order: sec, min, hour, day, month, weekday. */\nconst RANGES = [\"0-59\", \"0-59\", \"0-23\", \"1-31\", \"1-12\", \"0-6\"];\n\n/**\n * Replace `*` with a concrete `lo-hi` range so the rest of the pipeline only\n * has to deal with numeric forms.\n *\n * @param {string} field\n * @param {string} range e.g. `\"0-59\"`\n * @returns {string}\n */\nexport function resolveAsterisks(field, range) {\n return field.includes(\"*\") ? field.replace(\"*\", range) : field;\n}\n\n/**\n * Expand every `lo-hi` run in `field` to the comma-separated list of integers it\n * covers. Handles reversed bounds (`5-2` → `2,3,4,5`). Does not handle steps;\n * run {@link resolveSteps} after this.\n *\n * @param {string} field\n * @returns {string}\n */\nexport function resolveRanges(field) {\n const regex = /(\\d+)-(\\d+)/;\n let current = field;\n while (true) {\n const match = regex.exec(current);\n if (!match) break;\n const raw = match[0];\n let first = Number(match[1]);\n let last = Number(match[2]);\n if (last < first) {\n [first, last] = [last, first];\n }\n const values = [];\n for (let i = first; i <= last; i += 1) {\n values.push(i);\n }\n current = current.replace(raw, values.join(\",\"));\n }\n return current;\n}\n\n/**\n * Apply a `.../step` suffix, keeping only values divisible by `step`.\n * Expects ranges to already be expanded to comma-lists. No-op when the suffix is missing.\n *\n * @param {string} field e.g. `\"0,1,2,...,59/10\"` → `\"0,10,20,30,40,50\"`\n * @returns {string}\n */\nexport function resolveSteps(field) {\n const match = /^(.+)\\/(\\d+)$/.exec(field);\n if (!match) return field;\n const base = match[1];\n const step = Number(match[2]);\n if (!Number.isFinite(step) || step <= 0) return field;\n return base\n .split(\",\")\n .map((v) => Number(v))\n .filter((v) => Number.isFinite(v) && v % step === 0)\n .join(\",\");\n}\n\n/**\n * Normalize a raw 6-field schedule string into an array of comma-separated integer\n * lists — one per field, ready for {@link matchesParsedPattern}.\n *\n * @param {string} pattern\n * @returns {string[]}\n * @throws If `pattern` does not contain exactly six whitespace-separated fields.\n */\nexport function convertPattern(pattern) {\n const parts = pattern.trim().split(/\\s+/);\n if (parts.length !== 6) {\n throw new Error(`Invalid schedule \"${pattern}\". Expected 6 fields: sec min hour day month weekday`);\n }\n return parts\n .map((field, idx) => resolveAsterisks(field, RANGES[idx]))\n .map((field) => resolveRanges(field))\n .map((field) => resolveSteps(field));\n}\n\n/**\n * Does `value` appear in the comma-list `field`?\n *\n * @param {string} field\n * @param {number} value\n * @returns {boolean}\n */\nfunction fieldMatches(field, value) {\n const allowed = field.split(\",\").map((v) => Number(v));\n return allowed.includes(value);\n}\n\n/**\n * Check an already-parsed pattern against a `Date`. All six fields must match\n * (month is 1-indexed here; weekday follows `Date#getDay`, 0 = Sunday).\n *\n * @param {string[]} parsed Output of {@link convertPattern}.\n * @param {Date} date\n * @returns {boolean}\n */\nexport function matchesParsedPattern(parsed, date) {\n return (\n fieldMatches(parsed[0], date.getSeconds()) &&\n fieldMatches(parsed[1], date.getMinutes()) &&\n fieldMatches(parsed[2], date.getHours()) &&\n fieldMatches(parsed[3], date.getDate()) &&\n fieldMatches(parsed[4], date.getMonth() + 1) &&\n fieldMatches(parsed[5], date.getDay())\n );\n}\n\n/**\n * One-shot check: does `pattern` match `date`? Parses the pattern each call —\n * fine for the runner loop where we only test one date per tick; use\n * {@link convertPattern} + {@link matchesParsedPattern} if you need to test\n * many dates against the same schedule.\n *\n * @param {string} pattern\n * @param {Date} [date]\n * @returns {boolean}\n */\nexport function timeMatcher(pattern, date = new Date()) {\n const parsed = convertPattern(pattern);\n return matchesParsedPattern(parsed, date);\n}\n\nconst MS_PER_SECOND = 1000;\nconst DEFAULT_SEARCH_HORIZON_MS = 10 * 365 * 24 * 60 * 60 * MS_PER_SECOND;\n\n/**\n * Earliest calendar second strictly after `from` where the schedule matches.\n * Use this to sleep until the next run instead of polling `timeMatcher` and risking missed seconds.\n *\n * @param {string} pattern Same 6-field format as `timeMatcher`: `sec min hour day month weekday`\n * @param {Date} [from=new Date()] Reference instant. Matching is second-granularity; search starts at the next second after `from`.\n * @param {number} [maxSearchMs] Abort if no match within this window (default ~10 years).\n * @returns {Date}\n */\nexport function nextTimeMatch(\n pattern,\n from = new Date(),\n maxSearchMs = DEFAULT_SEARCH_HORIZON_MS\n) {\n const parsed = convertPattern(pattern);\n let t = Math.ceil((from.getTime() + 1) / MS_PER_SECOND) * MS_PER_SECOND;\n const end = t + maxSearchMs;\n while (t <= end) {\n const date = new Date(t);\n if (matchesParsedPattern(parsed, date)) {\n return date;\n }\n t += MS_PER_SECOND;\n }\n throw new Error(\n `nextTimeMatch: no match for \"${pattern}\" within ${maxSearchMs}ms after ${from.toISOString()}`\n );\n}\n","import path from \"node:path\";\nimport { FileDatabase } from \"../filedatabase/index.js\";\n\n/**\n * Gets or lazily builds the default IPC-logs FileDatabase state, cached on\n * `context.__tasksLogsState`. Reads `tasksLogs*` params (basePath, namespace,\n * table, errorTable, enabled, maxVersions, pageSize). When `tasksLogsEnabled=false`\n * the main `db` is null but `errorDb` still captures error payloads.\n *\n * @param {object} context\n * @returns {object} `{ db, errorDb, queue, initialized, errorInitialized }`\n */\nfunction getLogsState(context) {\n const holder = context;\n if (holder.__tasksLogsState) return holder.__tasksLogsState;\n\n const basePath = holder.params?.get?.(\"tasksLogsBasePath\") || \"./data\";\n const namespace = holder.params?.get?.(\"tasksLogsNamespace\") || \"tasks-logs\";\n const tableName = holder.params?.get?.(\"tasksLogsTable\") || \"runner\";\n const errorTableName = holder.params?.get?.(\"tasksErrorLogsTable\") || `${tableName}-errors`;\n const maxVersionsRaw = Number(holder.params?.get?.(\"tasksLogsMaxVersions\"));\n const pageSizeRaw = Number(holder.params?.get?.(\"tasksLogsPageSize\"));\n\n const errorDb = new FileDatabase({\n basePath,\n namespace,\n tableName: errorTableName,\n versioned: true,\n useMetadata: true,\n maxVersions: Number.isFinite(maxVersionsRaw) && maxVersionsRaw > 0 ? maxVersionsRaw : 20,\n pageSize: Number.isFinite(pageSizeRaw) && pageSizeRaw > 0 ? pageSizeRaw : 2000,\n logger: holder.logger,\n });\n\n const enabledRaw = holder.params?.get?.(\"tasksLogsEnabled\");\n const enabled = enabledRaw === undefined ? true : !!enabledRaw;\n if (!enabled) {\n const disabledState = {\n db: null,\n errorDb,\n queue: Promise.resolve(),\n initialized: true,\n errorInitialized: false,\n };\n holder.__tasksLogsState = disabledState;\n return disabledState;\n }\n\n const db = new FileDatabase({\n basePath,\n namespace,\n tableName,\n versioned: true,\n useMetadata: true,\n maxVersions: Number.isFinite(maxVersionsRaw) && maxVersionsRaw > 0 ? maxVersionsRaw : 20,\n pageSize: Number.isFinite(pageSizeRaw) && pageSizeRaw > 0 ? pageSizeRaw : 2000,\n logger: holder.logger,\n });\n const state = {\n db,\n errorDb,\n queue: Promise.resolve(),\n initialized: false,\n errorInitialized: false,\n };\n holder.__tasksLogsState = state;\n return state;\n}\n\n/**\n * Cache key for a target so multiple writers sharing the same\n * `basePath`/`namespace`/`tableName` reuse one FileDatabase state\n * (see `getLogsStateForTarget`).\n *\n * @param {object} target `{ basePath?, namespace?, tableName }`\n * @returns {string}\n */\nfunction ipcLogTargetKey(target) {\n const bp = target.basePath ?? \"\";\n const ns = target.namespace ?? \"\";\n return `${bp}::${ns}::${target.tableName}`;\n}\n\n/**\n * Produce a FileDatabase `tableName` of the form `source/resource`, sanitizing each\n * segment so only `[a-zA-Z0-9._-]` survive. Empty segments fall back to `\"x\"`.\n *\n * @param {string} source\n * @param {string} resource\n * @returns {string} e.g. `\"actris/properties\"`\n */\nexport function ipcFileLogsTableNameForSourceResource(source, resource) {\n const seg = (s) => {\n const t = String(s).trim().replace(/[^a-zA-Z0-9._-]+/g, \"_\").replace(/^_+|_+$/g, \"\");\n return t.length ? t : \"x\";\n };\n return `${seg(source)}/${seg(resource)}`;\n}\n\n/**\n * Read IPC log records from the latest FileDatabase version for `source/resource`.\n * Intended for tailing / incremental polling — use the returned `latestTs` as the\n * next call's `afterTs`.\n *\n * @param {object} context\n * @param {object} options\n * @param {string} options.source\n * @param {string} options.resource\n * @param {number} [options.tail=100] Max records returned after filtering (clamped 1..10000).\n * @param {string|null} [options.afterTs] ISO timestamp watermark; keeps rows with `ts > afterTs`.\n * @returns {Promise<{ records: object[], latestTs: string|null }>}\n */\nexport async function readTaskIpcLogsSnapshot(context, options) {\n const holder = context;\n const basePath = holder.params?.get?.(\"tasksLogsBasePath\") ?? \"./data\";\n const namespace = holder.params?.get?.(\"tasksLogsNamespace\") ?? \"tasks-logs\";\n const tableName = ipcFileLogsTableNameForSourceResource(options.source, options.resource);\n const tail = Math.max(1, Math.min(10_000, Number(options.tail) > 0 ? Number(options.tail) : 100));\n\n const fd = new FileDatabase({\n basePath,\n namespace,\n tableName,\n versioned: true,\n useMetadata: true,\n maxVersions: 30,\n pageSize: 2000,\n logger: holder.logger,\n });\n\n const versions = await fd.getVersions();\n if (versions.length === 0) {\n return { records: [], latestTs: null };\n }\n const latest = versions[versions.length - 1];\n const raw = await fd.read({ version: latest });\n const arr = Array.isArray(raw) ? raw : [];\n\n let filtered = arr;\n if (options.afterTs && String(options.afterTs).trim()) {\n const cut = String(options.afterTs).trim();\n filtered = arr.filter((r) => r && typeof r.ts === \"string\" && String(r.ts) > cut);\n }\n\n /** Watermark for incremental fetches: max `ts` among all matching rows, not only the returned tail. */\n let latestTs = null;\n for (const r of filtered) {\n const ts = typeof r?.ts === \"string\" ? String(r.ts) : null;\n if (ts && (!latestTs || ts > latestTs)) latestTs = ts;\n }\n\n const incremental = !!(options.afterTs && String(options.afterTs).trim());\n /** Incremental polls may return many lines between ticks — cap at 10k so we do not drop rows then advance `latestTs` past them. */\n const maxReturn = incremental ? 10_000 : tail;\n const sliced = filtered.length > maxReturn ? filtered.slice(-maxReturn) : filtered;\n\n return { records: sliced, latestTs };\n}\n\n/**\n * Absolute path to the FileDatabase table directory for a given target (matches the\n * FileDatabase on-disk layout). Versioned writes create timestamp subfolders inside.\n *\n * @param {object} context\n * @param {object} target `{ basePath?, namespace?, tableName }`\n * @returns {string}\n */\nexport function resolveIpcFileLogsDir(context, target) {\n const holder = context;\n const basePath = target.basePath ?? (holder.params?.get?.(\"tasksLogsBasePath\") || \"./data\");\n const namespace = target.namespace ?? (holder.params?.get?.(\"tasksLogsNamespace\") || \"tasks-logs\");\n const segments = target.tableName.split(\"/\").filter(Boolean);\n return path.resolve(basePath, namespace, ...segments);\n}\n\n/**\n * Lazily build a FileDatabase state for the given target, memoized on\n * `context.__tasksLogsTargetStates` (Map keyed by `ipcLogTargetKey`).\n * Returns `null` when `tasksLogsEnabled=false`.\n *\n * @param {object} context\n * @param {object} target `{ basePath?, namespace?, tableName }`\n * @returns {object|null} same shape as `getLogsState`, but `errorDb` is always null.\n */\nfunction getLogsStateForTarget(context, target) {\n const holder = context;\n const enabledRaw = holder.params?.get?.(\"tasksLogsEnabled\");\n const enabled = enabledRaw === undefined ? true : !!enabledRaw;\n if (!enabled) return null;\n\n if (!holder.__tasksLogsTargetStates) holder.__tasksLogsTargetStates = new Map();\n const map = holder.__tasksLogsTargetStates;\n const key = ipcLogTargetKey(target);\n if (map.has(key)) return map.get(key);\n\n const basePath = target.basePath ?? (holder.params?.get?.(\"tasksLogsBasePath\") || \"./data\");\n const namespace = target.namespace ?? (holder.params?.get?.(\"tasksLogsNamespace\") || \"tasks-logs\");\n const maxVersionsRaw = Number(holder.params?.get?.(\"tasksLogsMaxVersions\"));\n const pageSizeRaw = Number(holder.params?.get?.(\"tasksLogsPageSize\"));\n\n const db = new FileDatabase({\n basePath,\n namespace,\n tableName: target.tableName,\n versioned: true,\n useMetadata: true,\n maxVersions: Number.isFinite(maxVersionsRaw) && maxVersionsRaw > 0 ? maxVersionsRaw : 20,\n pageSize: Number.isFinite(pageSizeRaw) && pageSizeRaw > 0 ? pageSizeRaw : 2000,\n logger: holder.logger,\n });\n const state = {\n db,\n errorDb: null,\n queue: Promise.resolve(),\n initialized: false,\n errorInitialized: false,\n };\n map.set(key, state);\n return state;\n}\n\n/**\n * Heuristic: does this IPC payload represent an error?\n * - object with `level` of `\"error\"`/`\"fatal\"`, OR\n * - object with a `message` string containing the word \"error\", OR\n * - plain string containing \"error\" (case-insensitive).\n *\n * @param {unknown} payload\n * @returns {boolean}\n */\nfunction isErrorPayload(payload) {\n if (!payload) return false;\n if (typeof payload === \"object\") {\n const level = typeof payload.level === \"string\" ? payload.level.toLowerCase() : \"\";\n if (level === \"error\" || level === \"fatal\") return true;\n if (typeof payload.message === \"string\" && /\\berror\\b/i.test(payload.message)) return true;\n return false;\n }\n if (typeof payload === \"string\") {\n return /\\berror\\b/i.test(payload);\n }\n return false;\n}\n\n/**\n * Build the persisted log record for an IPC payload. Copies `source`/`resource` from\n * `task.params` when present so logs stay queryable by them later.\n *\n * @param {object} task\n * @param {unknown} payload\n * @returns {object} `{ ts, opid, taskId, taskName, target, source, resource, payload }`\n */\nfunction buildLogRecord(task, payload) {\n const params = task.params && typeof task.params === \"object\" ? task.params : {};\n return {\n ts: new Date().toISOString(),\n opid: task.opid ?? null,\n taskId: task.id,\n taskName: task.name,\n target: task.service_group,\n source: typeof params.source === \"string\" ? params.source : null,\n resource: typeof params.resource === \"string\" ? params.resource : null,\n payload,\n };\n}\n\n/**\n * Append one IPC log line from a child worker. Never throws; writes are serialized\n * per-state on a promise queue (drain with `flushTaskIpcLogs`).\n *\n * - Without `target`: writes to the default store (`tasksLogsTable`, usually `runner`)\n * and, when the payload looks like an error, also to `tasksErrorLogsTable`.\n * - With `target`: writes only to that target's store (e.g. per source/resource,\n * later read by `readTaskIpcLogsSnapshot`).\n *\n * @param {object} context\n * @param {object} task\n * @param {unknown} payload\n * @param {object} [target] `{ basePath?, namespace?, tableName }`\n * @returns {void}\n */\nexport function appendTaskIpcLog(context, task, payload, target) {\n if (target) {\n const state = getLogsStateForTarget(context, target);\n if (!state?.db) return;\n const record = buildLogRecord(task, payload);\n state.queue = state.queue\n .then(async () => {\n await state.db.write([record], { forceNewVersion: !state.initialized });\n state.initialized = true;\n })\n .catch((error) => {\n context.logger.warn?.(\"[tasks] failed to persist IPC log entry (targeted):\", error);\n });\n return;\n }\n\n const state = getLogsState(context);\n if (!state.db && !state.errorDb) return;\n\n const record = buildLogRecord(task, payload);\n state.queue = state.queue\n .then(async () => {\n if (state.db) {\n await state.db.write([record], { forceNewVersion: !state.initialized });\n state.initialized = true;\n }\n if (state.errorDb && isErrorPayload(payload)) {\n await state.errorDb.write([record], { forceNewVersion: !state.errorInitialized });\n state.errorInitialized = true;\n }\n })\n .catch((error) => {\n context.logger.warn?.(\"[tasks] failed to persist IPC log entry:\", error);\n });\n}\n\n/**\n * Await pending FileDatabase writes from task IPC logging (default store + every\n * per-target store). Does not close anything; subsequent appends continue to work.\n *\n * @param {object} context\n * @returns {Promise<void>}\n */\nexport async function flushTaskIpcLogs(context) {\n const holder = context;\n const promises = [];\n if (holder.__tasksLogsState?.queue) promises.push(holder.__tasksLogsState.queue);\n const map = holder.__tasksLogsTargetStates;\n if (map) {\n for (const s of map.values()) {\n if (s.queue) promises.push(s.queue);\n }\n }\n await Promise.all(promises);\n}\n","/**\n * FileDatabase - Versioned, file-based data storage system\n * \n * Provides organized file storage with:\n * - Timestamp-based versioning\n * - Chunked/paginated file writes for large datasets\n * - Metadata tracking\n * - Backward compatibility with legacy structures\n * - Multiple storage modes (versioned, catalog, logs)\n */\n\nimport fs from \"fs\";\nimport path from \"path\";\n\n\n\n\n\n\n\n\n\n\n\n\n\nimport { ensurePath, getFileExtension, getFreeDiskSpace, bytesToHumanReadable, isTimestampFolder } from \"./utils.js\";\nimport { detectDataType, serializeData, deserializeData } from \"./serializers.js\";\nimport { ParamError, FileDatabaseError } from \"../errors.js\";\n\nexport { FileDatabaseError };\n\nexport class FileDatabase {\n basePath;\n namespace;\n tableName = null;\n versioned;\n maxVersions;\n pageSize;\n useMetadata;\n freeSpaceThreshold;\n logger;\n\n // Current operation state\n currentVersion = null;\n currentVersionFolder = null;\n currentFileNumber = 0;\n currentRecord = 0;\n hasReadFirstPage = false;\n lastFileData = null;\n metadata;\n\n // Synopsis calculation functions\n fileSynopsisFunction = null;\n versionSynopsisFunction = null;\n\n /**\n * Constructor - accepts context as first parameter (new pattern)\n * or config object (legacy pattern for backward compatibility)\n */\n constructor(contextOrConfig, options) {\n let config;\n \n // Check if first parameter is Context (has params property)\n if (contextOrConfig && typeof contextOrConfig === \"object\" && \"params\" in contextOrConfig) {\n // New pattern: context is first parameter\n const context = contextOrConfig ;\n const opts = options || {};\n \n // Get configuration from context.params (module \"filedatabase\" for --showUsedParams grouping)\n const defs = {\n basePath: \"string default ./data\",\n namespace: \"string default default\",\n tableName: \"string\",\n maxVersions: \"number default 5\",\n pageSize: \"number default 5000\",\n };\n const discovered = context.params.getAllForModule(defs);\n config = { ...discovered, ...opts, logger: context.logger } ;\n } else {\n // Legacy pattern: config object\n config = contextOrConfig ;\n }\n \n // Validate required configuration\n if (!config.basePath) {\n throw new ParamError(\"[FileDatabase] basePath is required\");\n }\n\n this.basePath = config.basePath;\n this.namespace = config.namespace || \"default\";\n this.tableName = config.tableName || null;\n this.versioned = config.versioned ?? true; // Default true for backward compatibility\n this.maxVersions = config.maxVersions || 5;\n this.pageSize = config.pageSize || 5000;\n this.useMetadata = config.useMetadata !== false; // Default true\n this.freeSpaceThreshold = config.freeSpaceThreshold || 100 * 1024 * 1024; // 100MB\n this.logger = config.logger || console;\n\n // Initialize metadata\n this.metadata = this.getDefaultMetadata();\n }\n\n /**\n * Initialize FileDatabase from context and options.\n * Params are read via getAllForModule(\"filedatabase\", defs) for --showUsedParams grouping.\n */\n static init(context, options) {\n return new FileDatabase(context, options ?? {});\n }\n\n /**\n * Get default metadata structure\n */\n getDefaultMetadata() {\n return {\n version: this.currentVersion || null,\n files: [],\n createdAt: new Date().toISOString(),\n modifiedAt: new Date().toISOString(),\n totalRecords: 0,\n synopsis: null,\n dataType: null,\n };\n }\n\n /**\n * Get the destination path (basePath/namespace/tableName[/version])\n */\n getDestinationPath(version) {\n const errors = [\"basePath\", \"namespace\", \"tableName\"]\n .filter(prop => !this[prop ])\n .map(prop => `${prop} is not set`);\n if (errors.length) {\n throw new FileDatabaseError(`[FileDatabase] ${errors.join(\"; \")}`);\n }\n\n const parts = [this.basePath, this.namespace];\n if (this.tableName) {\n parts.push(...this.tableName.split(\"/\"));\n }\n\n // Only add version folder if in versioned mode and version is specified\n if (this.versioned && version) {\n parts.push(version);\n }\n\n return path.resolve(...parts);\n }\n\n /**\n * Set current version and version folder\n */\n async setCurrentVersion(version) {\n this.currentVersion = version;\n this.currentVersionFolder = await ensurePath(this.getDestinationPath(), version);\n }\n\n /**\n * Create a new version folder with comprehensive timestamp logic\n * Only works in versioned mode\n */\n async makeNewVersion() {\n if (!this.versioned) {\n throw new FileDatabaseError(\"makeNewVersion() only works in versioned mode\");\n }\n\n // Reset in-memory metadata when creating a new version\n this.metadata = this.getDefaultMetadata();\n\n const existingVersions = await this.getVersions();\n let versionName;\n\n if (existingVersions.length > 0) {\n // Find the maximum timestamp among all existing versions\n const maxTimestamp = existingVersions.reduce((max, version) => {\n const versionDate = new Date(version.replace(\"Z\", \"\"));\n const maxDate = new Date(max.replace(\"Z\", \"\"));\n return versionDate > maxDate ? version : max;\n });\n\n // Increment the maximum timestamp by 1 second\n const maxDate = new Date(maxTimestamp.replace(\"Z\", \"\"));\n const nextDate = new Date(maxDate.getTime() + 1000);\n versionName = nextDate.toISOString().split(\".\")[0] + \"Z\";\n } else {\n // No existing versions, use current timestamp\n const now = new Date();\n versionName = now.toISOString().split(\".\")[0] + \"Z\";\n }\n\n await this.setCurrentVersion(versionName);\n\n // Reset file numbering for new version\n this.currentFileNumber = 0;\n\n // Delete old versions if we exceed maxVersions\n const versions = await this.getVersions();\n while (versions.length > this.maxVersions) {\n const versionToDelete = path.resolve(this.getDestinationPath(), versions.shift());\n this.logger.silly?.(`[FileDatabase] Deleting old version: ${versionToDelete}`);\n await fs.promises.rm(versionToDelete, { recursive: true, force: true });\n }\n\n return versionName;\n }\n\n /**\n * Get list of all versions (sorted chronologically)\n * Only works in versioned mode\n */\n async getVersions() {\n if (!this.versioned) {\n return []; // No versions in non-versioned mode\n }\n\n const destPath = this.getDestinationPath();\n\n try {\n await ensurePath(destPath);\n const items = await fs.promises.readdir(destPath);\n const versions = items.filter(item => {\n const itemPath = path.join(destPath, item);\n const stat = fs.statSync(itemPath);\n return stat.isDirectory() && isTimestampFolder(item);\n });\n\n return versions.sort();\n } catch (error) {\n return [];\n }\n }\n\n /**\n * Get the latest version (most recent timestamp)\n * Only works in versioned mode\n * @returns Latest version string or null if no versions\n */\n async getLatestVersion() {\n if (!this.versioned) {\n throw new FileDatabaseError(\"getLatestVersion() only works in versioned mode\");\n }\n\n const versions = await this.getVersions();\n if (versions.length === 0) {\n return null;\n }\n\n return versions[versions.length - 1];\n }\n\n /**\n * Check if any data exists in this table\n * Works for both versioned and non-versioned modes\n * @returns true if data exists\n */\n async hasData() {\n const tablePath = this.getDestinationPath();\n\n if (!fs.existsSync(tablePath)) {\n return false;\n }\n\n if (this.versioned) {\n // Check for version folders\n const versions = await this.getVersions();\n return versions.length > 0;\n } else {\n // Check for any data files or metadata\n const items = await fs.promises.readdir(tablePath);\n return items.some(item =>\n item === \"metadata.json\" ||\n item.match(/^\\d{6}\\.(json|txt|xml)$/) ||\n item.endsWith(\".json\")\n );\n }\n }\n\n /**\n * Auto-detect the data format in this table\n * Used when reading existing data\n * @returns Format detection result\n */\n async detectDataFormat()\n\n\n\n {\n const tablePath = this.getDestinationPath();\n\n if (!fs.existsSync(tablePath)) {\n return { versioned: false, hasMetadata: false, dataType: null };\n }\n\n const items = await fs.promises.readdir(tablePath);\n\n // Check for metadata.json in root (non-versioned with metadata)\n if (items.includes(\"metadata.json\")) {\n const metadata = JSON.parse(\n await fs.promises.readFile(path.join(tablePath, \"metadata.json\"), \"utf8\")\n );\n return {\n versioned: false,\n hasMetadata: true,\n dataType: metadata.dataType || null\n };\n }\n\n // Check for version folders\n const versionFolders = items.filter(item => {\n const itemPath = path.join(tablePath, item);\n const stat = fs.statSync(itemPath);\n return stat.isDirectory() && isTimestampFolder(item);\n });\n\n if (versionFolders.length > 0) {\n // Check if latest version has metadata\n const latestVersion = versionFolders.sort().pop();\n const versionMetadataPath = path.join(tablePath, latestVersion, \"metadata.json\");\n\n return {\n versioned: true,\n hasMetadata: fs.existsSync(versionMetadataPath),\n dataType: null\n };\n }\n\n // Check for sequential files (legacy non-versioned)\n const dataFiles = items.filter(f => f.match(/^\\d{6}\\.(json|txt|xml)$/));\n if (dataFiles.length > 0) {\n return {\n versioned: false,\n hasMetadata: false,\n dataType: null\n };\n }\n\n return { versioned: false, hasMetadata: false, dataType: null };\n }\n\n /**\n * Load metadata from JSON file\n */\n async loadMetadataJson(version) {\n const metadataFile = path.join(this.getDestinationPath(), version, \"metadata.json\");\n if (fs.existsSync(metadataFile)) {\n try {\n const rawData = await fs.promises.readFile(metadataFile, \"utf8\");\n return JSON.parse(rawData);\n } catch (e) {\n throw new FileDatabaseError(`Failed to read metadata for version \"${version}\": ${(e ).message}`);\n }\n }\n return null;\n }\n\n /**\n * Build metadata by scanning files in a version folder (backward compatibility)\n * Reads all files to get accurate counts - used when synopsis calculation is needed\n */\n async figureMetadataFromVersionFiles(version) {\n const versionPath = path.join(this.getDestinationPath(), version);\n\n if (!fs.existsSync(versionPath)) {\n return this.getDefaultMetadata();\n }\n\n const files = (await fs.promises.readdir(versionPath))\n .filter(file => file !== \"metadata.json\" && !file.startsWith(\".\"))\n .sort();\n\n const metadata = this.getDefaultMetadata();\n metadata.version = version;\n metadata.files = [];\n\n let totalRecords = 0;\n let detectedDataType = null;\n\n for (let i = 0; i < files.length; i++) {\n const fileName = files[i];\n const filePath = path.join(versionPath, fileName);\n\n try {\n const rawData = await fs.promises.readFile(filePath, \"utf8\");\n const extension = path.extname(fileName).toLowerCase();\n let dataType = \"text\";\n\n if (extension === \".json\") {\n dataType = \"json-array\";\n } else if (extension === \".xml\") {\n dataType = \"xml\";\n }\n\n const fileData = deserializeData(rawData, dataType);\n const recordsCount = Array.isArray(fileData) ? fileData.length : 1;\n\n if (detectedDataType === null) {\n detectedDataType = detectDataType(fileData);\n }\n\n const fileInfo = {\n number: i + 1,\n recordsCount,\n fileName,\n };\n\n metadata.files.push(fileInfo);\n totalRecords += recordsCount;\n } catch (error) {\n this.logger.error?.(`[FileDatabase] Failed to read file ${fileName}: ${(error ).message}`);\n }\n }\n\n metadata.totalRecords = totalRecords;\n metadata.dataType = detectedDataType;\n\n return metadata;\n }\n\n /**\n * Build metadata optimized - only reads first and last files\n * Assumes all middle files have the same record count as the first file\n * Much faster for large datasets with many files\n */\n async buildMetadataOptimized(version) {\n const versionPath = path.join(this.getDestinationPath(), version);\n\n if (!fs.existsSync(versionPath)) {\n return this.getDefaultMetadata();\n }\n\n const files = (await fs.promises.readdir(versionPath))\n .filter(file => file !== \"metadata.json\" && !file.startsWith(\".\"))\n .sort();\n\n if (files.length === 0) {\n return this.getDefaultMetadata();\n }\n\n const metadata = this.getDefaultMetadata();\n metadata.version = version;\n metadata.files = files.map((fileName, index) => ({\n number: index + 1,\n recordsCount: 0,\n fileName,\n }));\n\n // Read first file to determine data type and standard record count\n const firstFile = metadata.files[0];\n const firstFilePath = path.join(versionPath, firstFile.fileName);\n const firstFileRaw = await fs.promises.readFile(firstFilePath, \"utf8\");\n\n let firstFileData;\n try {\n firstFileData = JSON.parse(firstFileRaw);\n } catch (e) {\n firstFileData = firstFileRaw;\n }\n\n metadata.dataType = detectDataType(firstFileData);\n\n // Only proceed with optimization for json-array data\n if (metadata.dataType === \"json-array\") {\n const firstFileCount = Array.isArray(firstFileData) ? firstFileData.length : 1;\n firstFile.recordsCount = firstFileCount;\n\n // Assume all middle files have the same count as the first\n for (let i = 1; i < metadata.files.length - 1; i++) {\n metadata.files[i].recordsCount = firstFileCount;\n }\n\n // Read last file to get its actual count (might be partial)\n if (files.length > 1) {\n const lastFile = metadata.files[metadata.files.length - 1];\n const lastFilePath = path.join(versionPath, lastFile.fileName);\n const lastFileRaw = await fs.promises.readFile(lastFilePath, \"utf8\");\n const lastFileData = deserializeData(lastFileRaw, metadata.dataType);\n lastFile.recordsCount = Array.isArray(lastFileData) ? lastFileData.length : 1;\n }\n\n // Calculate total records\n metadata.totalRecords = metadata.files.reduce((sum, file) => sum + file.recordsCount, 0);\n } else {\n // For non-array data, count each file as 1 record\n metadata.files.forEach(file => {\n file.recordsCount = 1;\n });\n metadata.totalRecords = files.length;\n }\n\n return metadata;\n }\n\n /**\n * Figure out metadata - tries JSON first, then builds from files\n * Uses optimized building when no synopsis calculation is needed\n */\n async figureMetadata(version, useOptimized = true) {\n if (this.useMetadata) {\n const metadata = await this.loadMetadataJson(version);\n if (metadata) {\n return metadata;\n }\n }\n \n // Fallback: build from files\n // Use optimized version (only reads first+last) when no synopsis needed\n if (useOptimized && !this.fileSynopsisFunction && !this.versionSynopsisFunction) {\n return await this.buildMetadataOptimized(version);\n }\n \n // Use full version (reads all files) when synopsis calculation needed\n return await this.figureMetadataFromVersionFiles(version);\n }\n\n /**\n * Load version metadata (main entry point for loading)\n */\n async loadVersionMetadata(version) {\n const metadata = await this.figureMetadata(version);\n this.metadata = metadata;\n return metadata;\n }\n\n /**\n * Save version metadata to file\n */\n async saveVersionMetadata(metadata) {\n if (!this.useMetadata) {\n return;\n }\n\n const metadataToSave = metadata || this.metadata;\n let metadataFile;\n\n if (this.versioned) {\n // Versioned mode: metadata in version folder\n if (!this.currentVersion) {\n return;\n }\n metadataFile = path.join(this.getDestinationPath(), this.currentVersion, \"metadata.json\");\n } else {\n // Non-versioned mode: metadata in root table folder\n metadataFile = path.join(this.getDestinationPath(), \"metadata.json\");\n }\n\n await fs.promises.writeFile(metadataFile, JSON.stringify(metadataToSave, null, 4), \"utf8\");\n }\n\n /**\n * Create a new file entry in metadata\n */\n makeNewFile() {\n this.currentFileNumber = (this.currentFileNumber || 0) + 1;\n\n const dataType = this.metadata.dataType || \"json-array\";\n const fileEntry = {\n number: this.currentFileNumber,\n recordsCount: 0,\n fileName: `${this.currentFileNumber.toString().padStart(6, \"0\")}.${getFileExtension(dataType)}`,\n };\n\n this.metadata.files.push(fileEntry);\n this.lastFileData = null;\n\n this.logger.silly?.(`[FileDatabase] Created new file: ${fileEntry.fileName}, fileNumber: ${this.currentFileNumber}`);\n }\n\n /**\n * Figure out what data to write and which file to use (for pagination)\n * @param data - Data to write\n * @param targetFileIndex - Optional index of existing file to overwrite (when customMetadata matches)\n * @param forceNewFile - If true, always create a new file (when customMetadata provided but no match)\n */\n figureOutDataAndFileToWrite(data, targetFileIndex = null, forceNewFile = false) {\n let dataToWrite;\n let dataLeftOver;\n\n // Detect data type from incoming data\n // Always use the incoming data's type to ensure correct file extension\n const incomingDataType = detectDataType(data);\n if (this.metadata.dataType !== incomingDataType) {\n this.metadata.dataType = incomingDataType;\n }\n\n // If targetFileIndex is provided, use that file (overwrite existing file with matching customMetadata)\n if (targetFileIndex !== null && targetFileIndex < this.metadata.files.length) {\n const targetFile = this.metadata.files[targetFileIndex];\n // For non-array data, overwrite the file\n if (!Array.isArray(data)) {\n dataToWrite = data;\n dataLeftOver = null;\n return { dataToWrite, dataLeftOver, fileName: targetFile.fileName };\n } else {\n // For arrays, start fresh in the target file (don't append)\n dataToWrite = data.slice(0, this.pageSize);\n dataLeftOver = data.slice(this.pageSize);\n this.lastFileData = dataToWrite;\n return { dataToWrite, dataLeftOver, fileName: targetFile.fileName };\n }\n }\n\n // If forceNewFile is true (customMetadata provided but no match), create a new file\n // Skip the initial file creation if forceNewFile is true to avoid creating an extra empty file\n let newlyCreatedFileIndex = null;\n if (forceNewFile) {\n const filesBeforeCreate = this.metadata.files.length;\n this.makeNewFile();\n newlyCreatedFileIndex = filesBeforeCreate; // Index of the newly created file\n this.logger.silly?.(`[FileDatabase] Creating new file for unique custom metadata combination, fileNumber: ${this.currentFileNumber}`);\n } else if (this.metadata.files.length === 0) {\n // If no files exist yet and we're not forcing a new file, create the first file\n this.makeNewFile();\n }\n\n // Get the last file (which might be the one we just created)\n const lastFile = this.metadata.files[this.metadata.files.length - 1];\n const lastFileRecordsCount = lastFile.recordsCount;\n \n // Verify that if we created a new file, we're using it\n if (forceNewFile && newlyCreatedFileIndex !== null) {\n const newlyCreatedFile = this.metadata.files[newlyCreatedFileIndex];\n if (newlyCreatedFile && newlyCreatedFile.fileName !== lastFile.fileName) {\n this.logger.warn?.(`[FileDatabase] Warning: Newly created file ${newlyCreatedFile.fileName} doesn't match last file ${lastFile.fileName}`);\n }\n }\n \n // For non-array data (text/xml/object), check if we need a new file with correct extension\n // But skip this check if forceNewFile is true - we already created the file we need\n if (!Array.isArray(data) && !forceNewFile) {\n const lastFileExtension = path.extname(lastFile.fileName);\n const expectedExtension = `.${getFileExtension(incomingDataType)}`;\n // If the last file has wrong extension, create a new file with correct extension\n // Check even if recordsCount is 0 (empty file) - we want correct extension for new writes\n if (lastFileExtension !== expectedExtension) {\n // Only create new file if the existing one has content, otherwise we'll use it\n if (lastFileRecordsCount > 0) {\n this.makeNewFile();\n } else {\n // File is empty, update its name to have correct extension\n lastFile.fileName = `${this.currentFileNumber.toString().padStart(6, \"0\")}.${getFileExtension(incomingDataType)}`;\n }\n }\n } else if (!Array.isArray(data) && forceNewFile) {\n // If forceNewFile is true, ensure the newly created file has the correct extension\n const lastFileExtension = path.extname(lastFile.fileName);\n const expectedExtension = `.${getFileExtension(incomingDataType)}`;\n if (lastFileExtension !== expectedExtension) {\n lastFile.fileName = `${this.currentFileNumber.toString().padStart(6, \"0\")}.${getFileExtension(incomingDataType)}`;\n }\n }\n\n if (Array.isArray(data)) {\n // For arrays, handle pagination\n // If forceNewFile is true, write fresh data to the new file (don't append)\n if (forceNewFile) {\n // Write fresh data to the newly created file\n dataToWrite = data.slice(0, this.pageSize);\n dataLeftOver = data.slice(this.pageSize);\n this.lastFileData = dataToWrite;\n } else if (lastFileRecordsCount < this.pageSize) {\n // Try to append to existing file if there's space\n dataToWrite = [...(this.lastFileData || []), ...data.slice(0, this.pageSize - lastFileRecordsCount)];\n dataLeftOver = data.slice(this.pageSize - lastFileRecordsCount);\n this.lastFileData = dataToWrite;\n } else {\n // Last file is full, create a new file\n this.makeNewFile();\n dataToWrite = data.slice(0, this.pageSize);\n dataLeftOver = data.slice(this.pageSize);\n this.lastFileData = dataToWrite;\n }\n } else {\n // For non-arrays, write as-is\n dataToWrite = data;\n dataLeftOver = null;\n }\n\n // Get the file name - if forceNewFile is true, we just created a new file, so use that one\n // Otherwise, use the last file (which might have been created earlier or is being reused)\n const fileName = this.metadata.files[this.metadata.files.length - 1].fileName;\n\n this.logger.silly?.(\n `[FileDatabase] figureOutDataAndFileToWrite: filename=${fileName}, forceNewFile=${forceNewFile}, targetFileIndex=${targetFileIndex}, dataToWrite.length=${Array.isArray(dataToWrite) ? dataToWrite.length : \"N/A\"}, lastFileRecordsCount=${lastFileRecordsCount}`\n );\n\n return { dataToWrite, dataLeftOver, fileName };\n }\n\n /**\n * Calculate file-level synopsis if function is set\n */\n calculateFileSynopsis(data, fileIndex = this.metadata.files.length - 1) {\n if (!this.fileSynopsisFunction) {\n return;\n }\n const fileInfo = this.metadata.files[fileIndex];\n const enhancedFileInfo = this.fileSynopsisFunction(fileInfo, data);\n this.metadata.files[fileIndex] = enhancedFileInfo;\n }\n\n /**\n * Calculate version-level synopsis if function is set\n */\n calculateVersionSynopsis() {\n if (!this.versionSynopsisFunction) {\n return;\n }\n const enhancedMetadata = this.versionSynopsisFunction(this.metadata);\n this.metadata = enhancedMetadata;\n }\n\n /**\n * Update metadata after writing data\n */\n updateMetadata(dataToWrite, fileName, customMetadata) {\n let currentFile;\n\n if (fileName) {\n // Find the specific file by filename\n const foundFile = this.metadata.files.find(file => file.fileName === fileName);\n if (!foundFile) {\n this.logger.warn?.(`[FileDatabase] File ${fileName} not found in metadata, using last file`);\n currentFile = this.metadata.files[this.metadata.files.length - 1];\n } else {\n currentFile = foundFile;\n }\n } else {\n // Get the current file info from metadata (always the last file)\n currentFile = this.metadata.files[this.metadata.files.length - 1];\n }\n\n // Calculate the actual records count for the data being written\n const recordsCount = Array.isArray(dataToWrite) ? dataToWrite.length : 1;\n\n // Update existing file entry with the correct records count\n currentFile.recordsCount = recordsCount;\n\n // Add custom metadata fields if provided\n if (customMetadata) {\n Object.assign(currentFile, customMetadata);\n }\n\n // Find the file index for synopsis calculation\n const fileIndex = this.metadata.files.indexOf(currentFile);\n if (fileIndex !== -1) {\n this.calculateFileSynopsis(dataToWrite, fileIndex);\n }\n\n // Update version metadata\n this.metadata.version = this.currentVersion;\n this.metadata.modifiedAt = new Date().toISOString();\n this.metadata.dataType = detectDataType(dataToWrite);\n\n // Recalculate total records by summing all file records counts\n this.metadata.totalRecords = this.metadata.files.reduce((sum, file) => sum + (file.recordsCount || 0), 0);\n\n this.logger.silly?.(\n `[FileDatabase] Updated metadata for file ${currentFile.fileName}: recordsCount=${recordsCount}, totalRecords=${this.metadata.totalRecords}`\n );\n }\n\n /**\n * Safe write with disk space check\n */\n async safeWrite(filePath, data) {\n const serializedData = serializeData(data);\n const dir = path.dirname(filePath);\n const requiredBytes = Buffer.byteLength(serializedData, \"utf8\");\n\n // Check disk space\n const freeBytes = getFreeDiskSpace(dir);\n if (freeBytes !== null) {\n if (freeBytes < requiredBytes) {\n throw new FileDatabaseError(\n `Not enough disk space. Required: ${bytesToHumanReadable(requiredBytes)}, Free: ${bytesToHumanReadable(freeBytes)}`\n );\n }\n\n if (freeBytes < this.freeSpaceThreshold) {\n this.logger.warn?.(`Low disk space warning: only ${bytesToHumanReadable(freeBytes)} left`);\n }\n }\n\n try {\n await fs.promises.writeFile(filePath, serializedData, \"utf8\");\n this.logger.silly?.(`[FileDatabase] Wrote ${bytesToHumanReadable(requiredBytes)} to ${filePath}`);\n } catch (error) {\n throw new FileDatabaseError(`Failed to write file ${filePath}: ${(error ).message}`);\n }\n }\n\n /**\n * Prepare the instance for read or write operations\n * This discovers state and sets up internal members based on mode and current data\n */\n async prepare(options\n\n\n\n\n\n\n\n\n\n ) {\n const { write, read, version, deferInitialVersion } = options;\n if (write) {\n if (this.versioned) {\n // Versioned mode\n if (this.currentVersion === null) {\n if (!deferInitialVersion) {\n await this.makeNewVersion();\n this.metadata = this.getDefaultMetadata();\n this.metadata.version = this.currentVersion;\n this.makeNewFile();\n }\n } else {\n // For existing versions, load the metadata if not already loaded\n if (!this.metadata.files.length) {\n this.metadata = await this.figureMetadata(this.currentVersion);\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n }\n }\n } else {\n // Non-versioned mode - ensure table directory exists\n await ensurePath(this.getDestinationPath());\n\n // Non-versioned mode - auto-detect useMetadata if not set\n if (this.useMetadata === true) {\n // Try to load existing metadata, create new if doesn't exist\n const metadataPath = path.join(this.getDestinationPath(), \"metadata.json\");\n if (fs.existsSync(metadataPath)) {\n try {\n const rawData = await fs.promises.readFile(metadataPath, \"utf8\");\n this.metadata = JSON.parse(rawData);\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n } catch (e) {\n this.metadata = this.getDefaultMetadata();\n this.currentFileNumber = 0;\n }\n } else {\n // Don't create a file here - let the write logic handle it\n // This prevents creating an empty file when customMetadata is provided\n this.metadata = this.getDefaultMetadata();\n this.currentFileNumber = 0;\n }\n } else {\n // No metadata mode - just create default metadata\n // Don't create a file here either - let the write logic handle it\n this.metadata = this.getDefaultMetadata();\n this.currentFileNumber = 0;\n }\n }\n } else if (read) {\n if (this.versioned) {\n // Versioned mode\n const versions = await this.getVersions();\n if (versions.length === 0) {\n throw new FileDatabaseError(\"[FileDatabase] No versions found, cannot read\");\n }\n\n if (version) {\n if (!versions.includes(version)) {\n throw new FileDatabaseError(`[FileDatabase] Version \"${version}\" not found`);\n }\n await this.setCurrentVersion(version);\n } else {\n await this.setCurrentVersion(versions[versions.length - 1]);\n }\n\n if (!this.metadata.files.length) {\n this.metadata = await this.figureMetadata(this.currentVersion);\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n }\n } else {\n // Non-versioned mode\n this.currentVersion = null; // No version concept\n\n // Auto-detect useMetadata if not explicitly set\n if (this.useMetadata === undefined) {\n const format = await this.detectDataFormat();\n this.useMetadata = format.hasMetadata;\n }\n\n if (this.useMetadata) {\n // Load metadata from root\n const destPath = this.getDestinationPath();\n const metadataPath = path.join(destPath, \"metadata.json\");\n if (fs.existsSync(metadataPath)) {\n try {\n const rawData = await fs.promises.readFile(metadataPath, \"utf8\");\n this.metadata = JSON.parse(rawData);\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n } catch (e) {\n throw new FileDatabaseError(`Failed to read metadata: ${(e ).message}`);\n }\n } else {\n throw new FileDatabaseError(\n `[FileDatabase] No metadata found in non-versioned mode. Looked for: ${metadataPath} (table path: ${destPath})`\n );\n }\n } else {\n // Figure metadata from files\n this.metadata = await this.figureMetadataFromVersionFiles(\"\");\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n }\n }\n }\n }\n\n /**\n * Write data to the file database\n */\n async write(data, options = {}) {\n // Catalog mode: write to specific filename in destination path\n if (options.filename) {\n const destPath = this.getDestinationPath();\n await ensurePath(destPath);\n const filePath = path.join(destPath, options.filename);\n await this.safeWrite(filePath, data);\n return;\n }\n\n // Check for forceNewVersion in non-versioned mode\n if (options.forceNewVersion && !this.versioned) {\n throw new FileDatabaseError(\"Cannot use forceNewVersion in non-versioned mode\");\n }\n\n // Prepare for writing (this may load existing metadata).\n // If this write will force a new version on an empty store, defer the initial version in\n // prepare so we only call makeNewVersion() once (in the forceNewVersion block below).\n await this.prepare({ write: true, deferInitialVersion: !!(options.forceNewVersion && this.versioned) });\n\n // Always detect data type from incoming data AFTER prepare()\n // This ensures we use the correct type even if existing metadata has a different type\n const incomingDataType = detectDataType(data);\n this.metadata.dataType = incomingDataType;\n\n // Force new version if requested (versioned mode only)\n if (options.forceNewVersion) {\n await this.makeNewVersion();\n this.metadata = this.getDefaultMetadata();\n this.metadata.version = this.currentVersion;\n // Set data type from incoming data\n this.metadata.dataType = incomingDataType;\n this.makeNewFile();\n }\n\n // Check if customMetadata is provided and find existing file with matching metadata\n let targetFileIndex = null;\n const hasCustomMetadata = options.customMetadata && Object.keys(options.customMetadata).length > 0;\n \n if (hasCustomMetadata) {\n // Search through existing files for matching custom metadata\n for (let i = 0; i < this.metadata.files.length; i++) {\n const fileEntry = this.metadata.files[i];\n // Check if all customMetadata fields match\n // A file matches if it has all the customMetadata keys and their values match\n const matches = Object.keys(options.customMetadata).every(key => {\n // File must have the key and the value must match\n return key in fileEntry && fileEntry[key] === options.customMetadata[key];\n });\n if (matches) {\n targetFileIndex = i;\n this.logger.silly?.(`[FileDatabase] Found existing file with matching custom metadata: ${fileEntry.fileName}, metadata: ${JSON.stringify(options.customMetadata)}`);\n break;\n } else {\n this.logger.silly?.(`[FileDatabase] File ${fileEntry.fileName} does not match custom metadata: ${JSON.stringify(options.customMetadata)}`);\n }\n }\n if (targetFileIndex === null) {\n this.logger.silly?.(`[FileDatabase] No existing file found with custom metadata: ${JSON.stringify(options.customMetadata)}, will create new file`);\n }\n } else {\n this.logger.silly?.(`[FileDatabase] No custom metadata provided, will create new file`);\n }\n\n // If we found a matching file, prepare to overwrite it\n if (targetFileIndex !== null) {\n const targetFile = this.metadata.files[targetFileIndex];\n // Set current file number to match the target file\n this.currentFileNumber = targetFile.number;\n // Reset pagination state since we're overwriting\n this.lastFileData = null;\n this.currentRecord = 0;\n this.hasReadFirstPage = false;\n }\n\n // Pass flag indicating if we should force a new file (when customMetadata provided but no match)\n const forceNewFile = hasCustomMetadata && targetFileIndex === null;\n // eslint-disable-next-line prefer-const\n let { dataToWrite, dataLeftOver, fileName } = this.figureOutDataAndFileToWrite(data, targetFileIndex, forceNewFile);\n\n // Write first batch\n const destPath = this.getDestinationPath(this.currentVersion || undefined);\n await this.safeWrite(path.join(destPath, fileName), dataToWrite);\n this.updateMetadata(dataToWrite, fileName, options.customMetadata);\n\n // Handle pagination for remaining data (arrays only)\n // Note: For customMetadata matches, we only write to the target file, so no pagination needed\n while (dataLeftOver && dataLeftOver.length > 0 && targetFileIndex === null) {\n const writeContext = this.figureOutDataAndFileToWrite(dataLeftOver);\n await this.safeWrite(path.join(destPath, writeContext.fileName), writeContext.dataToWrite);\n this.updateMetadata(writeContext.dataToWrite, writeContext.fileName, options.customMetadata);\n dataLeftOver = writeContext.dataLeftOver;\n }\n\n // Calculate version synopsis\n this.calculateVersionSynopsis();\n\n // Save metadata to file\n if (this.useMetadata) {\n await this.saveVersionMetadata(this.metadata);\n }\n }\n\n /**\n * Read data from the file database\n */\n async read(options = {}) {\n const { version, nextPage = false, pageSize, filename } = options;\n\n // Catalog mode: read specific file by name\n if (filename) {\n const destPath = this.getDestinationPath(version);\n const filePath = path.join(destPath, filename);\n try {\n const rawData = await fs.promises.readFile(filePath, \"utf8\");\n return JSON.parse(rawData);\n } catch (error) {\n throw new FileDatabaseError(`Failed to read file ${filename}: ${(error ).message}`);\n }\n }\n\n // Prepare for reading\n await this.prepare({ read: true, version });\n\n // Check for non-paginated data types\n const isNonPaginatedData =\n this.metadata.dataType === \"text\" || this.metadata.dataType === \"xml\" || this.metadata.dataType === \"json-object\";\n\n if (isNonPaginatedData) {\n // For text/xml/object data, return all content\n const file = this.metadata.files[0];\n const filePath = path.join(this.getDestinationPath(this.currentVersion || undefined), file.fileName);\n try {\n const rawData = await fs.promises.readFile(filePath, \"utf8\");\n return deserializeData(rawData, this.metadata.dataType);\n } catch (error) {\n throw new FileDatabaseError(`Failed to read file ${file.fileName}: ${(error ).message}`);\n }\n }\n\n // Handle paginated data (JSON arrays)\n let effectivePageSize;\n\n if (nextPage && this.hasReadFirstPage) {\n // Move to next page\n effectivePageSize = pageSize || this.pageSize;\n this.currentRecord += effectivePageSize;\n } else if (!nextPage) {\n // If not paginating, read all records (unless pageSize is explicitly provided)\n effectivePageSize = pageSize !== undefined ? pageSize : this.metadata.totalRecords;\n this.currentRecord = 0;\n } else {\n // First call with nextPage=true (no previous page read)\n effectivePageSize = pageSize || this.pageSize;\n }\n\n // If beyond total records, return empty array\n if (this.currentRecord >= this.metadata.totalRecords) {\n return [];\n }\n\n const result = [];\n let recordsRead = 0;\n let currentFileIndex = 0;\n let currentFileOffset = 0;\n\n // Calculate which file and offset to start from\n let totalRecords = 0;\n for (let i = 0; i < this.metadata.files.length; i++) {\n const file = this.metadata.files[i];\n if (this.currentRecord < totalRecords + file.recordsCount) {\n currentFileIndex = i;\n currentFileOffset = totalRecords;\n break;\n }\n totalRecords += file.recordsCount;\n }\n\n // Read from files\n let cumulativeRecords = currentFileOffset;\n for (let i = currentFileIndex; i < this.metadata.files.length && recordsRead < effectivePageSize; i++) {\n const file = this.metadata.files[i];\n const filePath = path.join(this.getDestinationPath(this.currentVersion || undefined), file.fileName);\n\n try {\n const rawData = await fs.promises.readFile(filePath, \"utf8\");\n const fileData = deserializeData(rawData, this.metadata.dataType);\n\n let startIndex = 0;\n if (i === currentFileIndex) {\n startIndex = this.currentRecord - cumulativeRecords;\n }\n\n const endIndex = Math.min(startIndex + (effectivePageSize - recordsRead), fileData.length);\n const recordsFromThisFile = fileData.slice(startIndex, endIndex);\n\n result.push(...recordsFromThisFile);\n recordsRead += recordsFromThisFile.length;\n\n cumulativeRecords += file.recordsCount;\n } catch (error) {\n throw new FileDatabaseError(`Failed to read file ${file.fileName}: ${(error ).message}`);\n }\n }\n\n // Mark page as read for pagination tracking\n // When nextPage=true, we're explicitly paginating\n // When nextPage=false with explicit pageSize, we're also paginating (starting from beginning)\n if (result.length > 0) {\n if (nextPage || (pageSize !== undefined && pageSize < this.metadata.totalRecords)) {\n this.hasReadFirstPage = true;\n }\n }\n\n return result;\n }\n\n /**\n * Set the starting record for pagination (1-based index)\n */\n setStartRecord(startRecord) {\n this.currentRecord = startRecord - 1;\n this.hasReadFirstPage = false;\n }\n\n /**\n * Reset read pagination state\n */\n resetPagination() {\n this.currentRecord = 0;\n this.hasReadFirstPage = false;\n }\n\n /**\n * List filenames in the table directory.\n * For catalog/key-value usage (files written with { filename }).\n * Returns data file names (.json, .txt, .xml) excluding metadata.json.\n */\n async listFilenames() {\n const destPath = this.versioned && this.currentVersion\n ? path.join(this.getDestinationPath(), this.currentVersion)\n : this.getDestinationPath();\n try {\n const entries = await fs.promises.readdir(destPath, { withFileTypes: true });\n return entries\n .filter((e) => e.isFile() && e.name !== \"metadata.json\" && /\\.(json|txt|xml)$/i.test(e.name))\n .map((e) => e.name);\n } catch (err) {\n if (err?.code === \"ENOENT\") return [];\n throw new FileDatabaseError(`Failed to list files: ${(err ).message}`);\n }\n }\n\n /**\n * Remove a file from the table directory (catalog mode).\n * Use with listFilenames() to manage individual files.\n */\n async removeFile(filename) {\n const destPath = this.versioned && this.currentVersion\n ? path.join(this.getDestinationPath(), this.currentVersion)\n : this.getDestinationPath();\n const filePath = path.join(destPath, filename);\n try {\n await fs.promises.unlink(filePath);\n } catch (err) {\n if (err?.code === \"ENOENT\") return;\n throw new FileDatabaseError(`Failed to remove file ${filename}: ${(err ).message}`);\n }\n }\n\n /**\n * Remove a file and its metadata entry (non-versioned mode with useMetadata).\n * Use with findData() to get fileName, then call removeFileEntry to delete.\n */\n async removeFileEntry(filename) {\n if (this.versioned) {\n throw new FileDatabaseError(\"removeFileEntry is only supported in non-versioned mode\");\n }\n await this.prepare({ read: true });\n const idx = this.metadata.files.findIndex((f) => f.fileName === filename);\n if (idx === -1) {\n throw new FileDatabaseError(`File entry ${filename} not found in metadata`);\n }\n const entry = this.metadata.files[idx];\n const recordsCount = entry.recordsCount || 0;\n this.metadata.files.splice(idx, 1);\n this.metadata.totalRecords = Math.max(0, (this.metadata.totalRecords || 0) - recordsCount);\n const destPath = this.getDestinationPath();\n const filePath = path.join(destPath, filename);\n try {\n await fs.promises.unlink(filePath);\n } catch (err) {\n if (err?.code === \"ENOENT\") {\n this.logger.warn?.(`[FileDatabase] File ${filename} already missing on disk`);\n } else {\n throw new FileDatabaseError(`Failed to remove file ${filename}: ${(err ).message}`);\n }\n }\n if (this.useMetadata) {\n await this.saveVersionMetadata(this.metadata);\n }\n }\n\n /**\n * Set file-level synopsis calculation function\n */\n setFileSynopsisFunction(fn) {\n this.fileSynopsisFunction = fn;\n }\n\n /**\n * Set version-level synopsis calculation function\n */\n setVersionSynopsisFunction(fn) {\n this.versionSynopsisFunction = fn;\n }\n\n /**\n * Get current version name\n */\n getCurrentVersion() {\n return this.currentVersion;\n }\n\n /**\n * Get current metadata\n */\n getMetadata() {\n return { ...this.metadata };\n }\n\n /**\n * Find data by custom metadata fields\n * Searches through all versions and files to find entries matching the search criteria\n * \n * @param searchCriteria - Object with field names and values to search for (e.g., { ListingKey: \"123\", id: \"456\" })\n * @returns Array of found entries with their file paths and metadata\n */\n async findData(searchCriteria)\n\n\n\n\n\n {\n const results\n\n\n\n\n\n = [];\n\n // Handle non-versioned mode separately\n if (!this.versioned) {\n // Load metadata for non-versioned mode\n await this.prepare({ read: true });\n const metadata = this.getMetadata();\n \n // Search through files\n for (const fileEntry of metadata.files) {\n // Check if file entry matches search criteria\n const matches = Object.keys(searchCriteria).every(key => {\n return fileEntry[key] === searchCriteria[key];\n });\n\n if (matches) {\n // Read the file data\n const destPath = this.getDestinationPath();\n const filePath = path.join(destPath, fileEntry.fileName);\n const fileData = await fs.promises.readFile(filePath, \"utf8\");\n const data = deserializeData(fileData, metadata.dataType || \"json-object\");\n\n results.push({\n filePath,\n fileName: fileEntry.fileName,\n version: null,\n metadata: fileEntry,\n data,\n });\n }\n }\n } else {\n // Versioned mode: search through all versions\n const versions = await this.getVersions();\n \n for (const version of versions) {\n // Load metadata for this version\n await this.prepare({ read: true, version });\n const metadata = this.getMetadata();\n\n // Search through files in this version\n for (const fileEntry of metadata.files) {\n // Check if file entry matches search criteria\n const matches = Object.keys(searchCriteria).every(key => {\n return fileEntry[key] === searchCriteria[key];\n });\n\n if (matches) {\n // Read the file data\n const destPath = this.getDestinationPath(version);\n const filePath = path.join(destPath, fileEntry.fileName);\n const fileData = await fs.promises.readFile(filePath, \"utf8\");\n const data = deserializeData(fileData, metadata.dataType || \"json-object\");\n\n results.push({\n filePath,\n fileName: fileEntry.fileName,\n version,\n metadata: fileEntry,\n data,\n });\n }\n }\n }\n }\n\n return results;\n }\n}\n\n// Export types\n\n\n\n\n\n\n\n\n\n\n\n;\n\n// Export synopsis functions\nexport { defaultFileSynopsisFunction, defaultVersionSynopsisFunction } from \"./synopsis-functions.js\";\n\n/**\n * Initialize FileDatabase from context\n * Similar to MlsClient.init pattern\n * \n * @param context - Context from init()\n * @param options - Optional configuration (takes precedence over context.params)\n * @returns Initialized FileDatabase instance\n */\n/**\n * List all table names in a given namespace\n * Scans the filesystem to find all table directories\n *\n * @param basePath - Base path for file storage\n * @param namespace - Namespace to scan (e.g., \"harvested\", \"fromMLS\")\n * @returns Array of table names (directory names)\n */\nexport function listTables(basePath, namespace) {\n const namespacePath = path.join(basePath, namespace);\n\n if (!fs.existsSync(namespacePath)) {\n return [];\n }\n\n try {\n return fs.readdirSync(namespacePath, { withFileTypes: true })\n .filter(dirent => dirent.isDirectory())\n .map(dirent => dirent.name);\n } catch (error) {\n // Return empty array if can't read directory\n return [];\n }\n}\n\n/**\n * List all sources (namespaces) in a given base path\n * Scans the filesystem to find all namespace directories\n *\n * @param basePath - Base path for file storage\n * @returns Array of source names (directory names)\n */\nexport function listSources(basePath) {\n if (!fs.existsSync(basePath)) {\n return [];\n }\n\n try {\n return fs.readdirSync(basePath, { withFileTypes: true })\n .filter(dirent => dirent.isDirectory())\n .map(dirent => dirent.name);\n } catch (error) {\n // Return empty array if can't read directory\n return [];\n }\n}\n","\n\n\n\n\n\n\n\n/**\n * Detect the data type of a given value\n */\nexport function detectDataType(data) {\n if (Array.isArray(data)) {\n return \"json-array\";\n } else if (typeof data === \"object\" && data !== null) {\n return \"json-object\";\n } else if (typeof data === \"string\") {\n // Try to detect if it's XML\n const trimmed = data.trim();\n if (trimmed.startsWith(\"<?xml\") || trimmed.startsWith(\"<\")) {\n return \"xml\";\n }\n return \"text\";\n } else {\n return \"text\";\n }\n}\n\n/**\n * Serialize data to a string for file storage\n */\nexport function serializeData(data) {\n const dataType = detectDataType(data);\n \n if (dataType === \"json-array\" || dataType === \"json-object\") {\n return JSON.stringify(data, null, 4);\n } else {\n // For text and XML, return as-is\n return String(data);\n }\n}\n\n/**\n * Deserialize data from a string based on data type\n */\nexport function deserializeData(rawData, dataType) {\n if (dataType === \"json-array\" || dataType === \"json-object\") {\n return JSON.parse(rawData);\n } else {\n // For text and XML, return as-is\n return rawData;\n }\n}\n\n","/**\n * Error classes for the CLI toolkit\n */\n\nexport class FrameworkError extends Error {\n constructor(message) {\n super(message);\n this.name = \"FrameworkError\";\n }\n}\n\nexport class ParamError extends FrameworkError {\n constructor(message) {\n super(message);\n this.name = \"ParamError\";\n }\n}\n\nexport class InitError extends FrameworkError {\n constructor(message) {\n super(message);\n this.name = \"InitError\";\n }\n}\n\nexport class CriticalRequestError extends FrameworkError {\n constructor(message) {\n super(message);\n this.name = \"CriticalRequestError\";\n }\n}\n\nexport class ControlFlowError extends Error {\n constructor(message) {\n super(message);\n this.name = \"ControlFlowError\";\n }\n}\n\nexport class HttpClientError extends FrameworkError {\n constructor(message, cause) {\n super(message);this.cause = cause;;\n this.name = \"HttpClientError\";\n }\n}\n\nexport class FileDatabaseError extends FrameworkError {\n constructor(message) {\n super(message);\n this.name = \"FileDatabaseError\";\n }\n}\n\n","import { ParamError } from \"../errors.js\";\n\n/**\n * Base class every task handler extends. Holds the runner `context` and the\n * claimed task row, and defines the contract the runner calls into:\n *\n * - {@link AbstractTask#cantRunReason}: synchronous / async precondition check.\n * Returning a truthy string tells the runner \"skip for now, stash the reason\n * into `progress`\" without burning a retry; returning `false` means \"go\".\n * - {@link AbstractTask#run}: the actual work, handed a `reportProgress`\n * callback that updates the DB `progress` column.\n * - {@link AbstractTask#requestStop}: cooperative shutdown signal from the runner\n * (noop by default; long-running tasks override to honor it).\n *\n * Static enqueue-time hooks (write-time validation):\n *\n * - {@link AbstractTask.resolveParams}: build a full row payload (envelope +\n * inner `params` blob) for {@link enqueueTask}. Subclasses normally do not\n * override this — they override {@link AbstractTask.resolveCustomParams}\n * instead. Override here only for envelope-level cross-field rules\n * (e.g. \"stop tasks must target a specific service_name\").\n * - {@link AbstractTask.resolveCustomParams}: validate / default the typed\n * fields that go into the `params` JSON column. Default returns a\n * `--paramsJson` passthrough; subclasses override.\n *\n * Both static methods are async and accept `(context, overrides)`. `overrides`\n * is a partial that wins over CLI/env values — the typical caller is the\n * {@link TasksRegistry#resolveTaskParams} dispatcher, which seeds it with\n * `{ name }`. Programmatic enqueuers can pass any envelope field plus\n * `overrides.params` to inject inner-blob values.\n */\nexport class AbstractTask {\n /**\n * Whether `send-task` should wait for completion (and print a result\n * report) when no explicit `--wait` / `--noWait` flag is given. Defaults\n * to false; short-lived probe tasks (e.g. `ping`) override to true.\n *\n * @type {boolean}\n */\n static defaultWaitForResult = false;\n\n /**\n * @param {object} context Runner context (db, logger, params, emitter...).\n * @param {object} task Task row as claimed from the queue.\n */\n constructor(context, task) {\n this.context = context;\n this.task = task;\n }\n\n /**\n * Return a short reason string when the task should be deferred (e.g. \"locked\n * by source\"), or `false`/falsy when it is free to run. Default: always `false`.\n *\n * @returns {string | false | Promise<string | false>}\n */\n cantRunReason() {\n return false;\n }\n\n /**\n * Called by the runner when a stop has been requested. Subclasses running\n * long loops should flip a flag here and check it between iterations.\n *\n * @param {number} [_allowanceMs] Grace period the runner promises before hard exit.\n */\n requestStop(_allowanceMs) {\n // Default no-op; long-running tasks can override.\n }\n\n /**\n * Perform the task. Must be implemented by subclasses.\n *\n * @param {(progress: unknown) => Promise<void>} _reportProgress\n * Updates the DB `progress` column. Accepts any serializable value;\n * strings are stored verbatim, objects are JSON-stringified.\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run(_reportProgress) {\n throw new Error(\"AbstractTask.run must be implemented by subclass\");\n }\n\n /**\n * Resolve a complete row payload for this task — envelope fields (queue,\n * priority, targeting, schedule…) plus the inner `params` blob produced by\n * {@link AbstractTask.resolveCustomParams}. Output shape matches\n * {@link enqueueTask}'s `options` argument, so the typical call is:\n *\n * const payload = await TaskClass.resolveParams(context, { name });\n * await enqueueTask(context, payload);\n *\n * Validation failures throw {@link ParamError} so the script aborts before\n * a malformed row hits the DB.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides] Partial overrides; takes precedence over CLI/env.\n * Recognised keys: `name`, `queueName`, `priority`, `serviceGroup`,\n * `serviceName`, `instanceNumber`, `serverName`, `opid`, `schedule`,\n * `nextRunAt`, plus `params` (object — overlay onto inner blob).\n * @returns {Promise<object>}\n */\n static async resolveParams(context, overrides = {}) {\n const main = AbstractTask._resolveMainFields(context, overrides);\n const params = await this.resolveCustomParams(context, overrides);\n return { ...main, params };\n }\n\n /**\n * Resolve the inner JSON blob stored in the `params` column. Default\n * implementation passes through `--paramsJson` (parsed as a JSON object)\n * overlaid with `overrides.params` when supplied; returns `null` when\n * neither is provided.\n *\n * Subclasses with typed fields should override and call\n * {@link AbstractTask._mergeTypedParams} for layered CLI/env/JSON/override\n * resolution, then validate and throw {@link ParamError} on bad input.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<object|null>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n return AbstractTask._defaultParamsBlob(context, overrides);\n }\n\n /**\n * Read main task envelope fields from `context.params` (CLI/env), with\n * any matching key on `overrides` taking precedence. Internal; called by\n * {@link AbstractTask.resolveParams}.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {object}\n */\n static _resolveMainFields(context, overrides = {}) {\n const defs = {\n queueName: \"string default tasks\",\n priority: \"number default 50\",\n serviceGroup: \"string\",\n serviceName: \"string\",\n instanceNumber: \"number\",\n serverName: \"string\",\n opid: \"string\",\n schedule: \"string\",\n };\n const cli = context.params.getAllForModule(\"task-envelope\", defs);\n\n const name = emptyToUndef(overrides.name) ?? emptyToUndef(context.params.get(\"name\", \"string\"));\n if (!name) {\n throw new ParamError(\"Task --name is required (e.g. ping, stop, dummyHarvest)\");\n }\n\n let instanceNumber;\n const rawInstance = overrides.instanceNumber ?? cli.instanceNumber;\n if (rawInstance !== undefined && rawInstance !== null && String(rawInstance).trim() !== \"\") {\n const n = Number(rawInstance);\n if (!Number.isFinite(n) || !Number.isInteger(n) || n < 1) {\n throw new ParamError(\"--instanceNumber must be a positive integer when set\");\n }\n instanceNumber = n;\n } else {\n instanceNumber = null;\n }\n\n const priorityRaw = overrides.priority ?? cli.priority ?? 50;\n const priority = Number(priorityRaw);\n if (!Number.isFinite(priority)) {\n throw new ParamError(`--priority must be a number (got ${JSON.stringify(priorityRaw)})`);\n }\n\n return {\n name,\n queueName: overrides.queueName ?? emptyToUndef(cli.queueName) ?? \"tasks\",\n priority,\n serviceGroup: emptyToUndef(overrides.serviceGroup) ?? emptyToUndef(cli.serviceGroup) ?? null,\n serviceName: emptyToUndef(overrides.serviceName) ?? emptyToUndef(cli.serviceName) ?? null,\n instanceNumber,\n serverName: emptyToUndef(overrides.serverName) ?? emptyToUndef(cli.serverName) ?? null,\n opid: emptyToUndef(overrides.opid) ?? emptyToUndef(cli.opid) ?? null,\n schedule: emptyToUndef(overrides.schedule) ?? emptyToUndef(cli.schedule) ?? null,\n nextRunAt: overrides.nextRunAt ?? null,\n };\n }\n\n /**\n * Default inner-params resolver: parses `--paramsJson` (must be a JSON\n * object), then overlays `overrides.params` on top. Returns `null` when\n * neither is provided.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {object|null}\n */\n static _defaultParamsBlob(context, overrides = {}) {\n const cli = context.params.getAllForModule(\"task-params\", { paramsJson: \"string\" });\n const fromJson = parseParamsJson(cli.paramsJson);\n const fromOverride = pickParamsObject(overrides);\n if (!fromJson && !fromOverride) return null;\n return { ...(fromJson ?? {}), ...(fromOverride ?? {}) };\n }\n\n /**\n * Helper for subclass `resolveCustomParams` overrides. Reads typed CLI\n * params (per `defs`) plus `--paramsJson` under a module namespace, then\n * merges them with explicit `overrides.params` in increasing priority:\n *\n * typed CLI flags → --paramsJson → overrides.params\n *\n * Undefined values are dropped so defaults declared in `defs` aren't\n * overwritten by missing-flag noise. Returns the merged object; the\n * caller is responsible for validation and throwing `ParamError`.\n *\n * @param {object} context\n * @param {string} moduleName Namespace for `--showUsedParams` grouping.\n * @param {Record<string, string>} defs Param defs in `getAllForModule` syntax.\n * @param {Record<string, unknown>} [overrides] As passed to `resolveCustomParams`.\n * @returns {Record<string, unknown>}\n */\n static _mergeTypedParams(context, moduleName, defs, overrides = {}) {\n const fullDefs = { ...defs, paramsJson: \"string\" };\n const cliRaw = context.params.getAllForModule(moduleName, fullDefs);\n const fromJson = parseParamsJson(cliRaw.paramsJson) ?? {};\n const fromCli = {};\n for (const [k, v] of Object.entries(cliRaw)) {\n if (k === \"paramsJson\") continue;\n if (v !== undefined && v !== null) fromCli[k] = v;\n }\n const fromOverride = pickParamsObject(overrides) ?? {};\n return { ...fromCli, ...fromJson, ...fromOverride };\n }\n}\n\n/** Trim a string-ish to a non-empty string, or return undefined. Non-strings pass through. */\nfunction emptyToUndef(s) {\n if (s === undefined || s === null) return undefined;\n if (typeof s !== \"string\") return s;\n const t = s.trim();\n return t.length ? t : undefined;\n}\n\n/**\n * Parse a `--paramsJson` value. Returns `null` for empty/missing input.\n * Throws `ParamError` for non-JSON or non-object payloads.\n *\n * @param {unknown} raw\n * @returns {object|null}\n */\nfunction parseParamsJson(raw) {\n if (raw == null) return null;\n const t = String(raw).trim();\n if (!t) return null;\n let parsed;\n try {\n parsed = JSON.parse(t);\n } catch (e) {\n throw new ParamError(`--paramsJson: not valid JSON: ${e?.message ?? String(e)}`);\n }\n if (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n throw new ParamError(\"--paramsJson must be a JSON object\");\n }\n return parsed;\n}\n\n/** Extract `overrides.params` when it is a plain object, else undefined. */\nfunction pickParamsObject(overrides) {\n const p = overrides?.params;\n if (p && typeof p === \"object\" && !Array.isArray(p)) return p;\n return undefined;\n}\n","import { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Trivial health-check task: used by `registryMaintenance` to probe runners and\n * by operators to confirm a runner is picking up work. Emits a log line and\n * returns `{ success: true, results: \"pong\" }`.\n */\nexport class TaskPing extends AbstractTask {\n /** Default-wait so `send-task --name=ping` prints a result without `--wait`. */\n static defaultWaitForResult = true;\n\n /** Ping takes no params. */\n static async resolveCustomParams() {\n return null;\n }\n\n /**\n * @returns {Promise<{ success: true, results: \"pong\" }>}\n */\n async run() {\n this.context.logger.info?.(`[TaskPing] pong (${this.task.id})`);\n return { success: true, results: \"pong\" };\n }\n}\n","import { sleepMs } from \"../../utils/index.js\";\nimport { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Long-running demo task used for exercising the runner's progress/stop paths.\n *\n * Ticks `total` times with `delay` ms between iterations, calling `reportProgress`\n * each tick. Honors cooperative stop (`requestStop`) by completing the current\n * iteration and deciding whether to finish the run or abort based on the\n * remaining work vs. the allowance window.\n */\nexport class TaskSampleProcess extends AbstractTask {\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ total: number, delay: number, name?: string }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-sample-process\", {\n total: \"number default 10\",\n delay: \"number default 1000\",\n name: \"string\",\n }, overrides);\n const total = Number(merged.total);\n const delay = Number(merged.delay);\n if (!Number.isInteger(total) || total <= 0) {\n throw new ParamError(`sampleProcess: param \"total\" must be a positive integer (got ${JSON.stringify(merged.total)})`);\n }\n if (!Number.isInteger(delay) || delay < 0) {\n throw new ParamError(`sampleProcess: param \"delay\" must be an integer >= 0 (got ${JSON.stringify(merged.delay)})`);\n }\n const out = { total, delay };\n if (typeof merged.name === \"string\" && merged.name.trim()) {\n out.name = merged.name.trim();\n }\n return out;\n }\n\n /**\n * @param {object} context\n * @param {object} task\n */\n constructor(context, task) {\n super(context, task);\n this.stopRequested = false;\n this.stopAllowanceMs = 0;\n this.stopDecisionLogged = false;\n }\n\n /**\n * Runner-facing stop signal. Records the allowance window so the main loop\n * can decide per-iteration whether to finish or abort early.\n *\n * @param {number} allowanceMs\n */\n requestStop(allowanceMs) {\n this.stopRequested = true;\n this.stopAllowanceMs = Number.isFinite(allowanceMs) && allowanceMs > 0 ? allowanceMs : 0;\n this.context.logger.warn?.(\n `[TaskSampleProcess] stop signal received (${this.task.id}), allowanceMs=${this.stopAllowanceMs}`\n );\n }\n\n /**\n * Iterate `total` times, sleeping `delay` ms between ticks and reporting\n * progress every iteration. Validates params up front; invalid values short-\n * circuit to a structured failure without starting the loop.\n *\n * @param {(progress: object) => Promise<void>} reportProgress\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run(reportProgress) {\n const totalRaw = this.task?.params?.total ?? 10;\n const delayRaw = this.task?.params?.delay ?? 1000;\n const nameRaw = this.task?.params?.name;\n\n const total = Number(totalRaw);\n const delay = Number(delayRaw);\n const name = typeof nameRaw === \"string\" && nameRaw.trim() ? nameRaw.trim() : \"sampleProcess\";\n\n const errors = [];\n if (!Number.isInteger(total) || total <= 0) {\n errors.push('param \"total\" must be a positive integer');\n }\n if (!Number.isInteger(delay) || delay < 0) {\n errors.push('param \"delay\" must be an integer >= 0');\n }\n\n if (errors.length > 0) {\n return {\n success: false,\n results: {\n error: `Validation failed: ${errors.join(\", \")}`,\n received: { total: totalRaw, delay: delayRaw, name: nameRaw },\n },\n };\n }\n\n const startedAt = Date.now();\n for (let i = 1; i <= total; i += 1) {\n if (this.stopRequested) {\n const remainingMs = Math.max(0, (total - i + 1) * delay);\n if (remainingMs <= this.stopAllowanceMs) {\n if (!this.stopDecisionLogged) {\n this.stopDecisionLogged = true;\n this.context.logger.warn?.(\n `[TaskSampleProcess] continue to finish (${this.task.id}): remainingMs=${remainingMs} <= allowanceMs=${this.stopAllowanceMs}`\n );\n }\n } else {\n this.context.logger.warn?.(\n `[TaskSampleProcess] stopping gracefully at iteration ${i}/${total} (${this.task.id}), remainingMs=${remainingMs} > allowanceMs=${this.stopAllowanceMs}`\n );\n return {\n success: false,\n results: {\n message: `Stopped before completion at iteration ${i}/${total}`,\n completed: i - 1,\n total,\n name,\n remainingMs,\n allowanceMs: this.stopAllowanceMs,\n },\n };\n }\n }\n\n const elapsed = Date.now() - startedAt;\n const remaining = Math.max(0, (total - i) * delay);\n const progress = {\n name,\n count: i,\n total,\n elapsedMs: elapsed,\n remainingMs: remaining,\n status: `running ${name}: ${i}/${total}`,\n };\n\n this.context.logger.progress(\"running\", {\n prefix: name,\n count: i,\n total,\n });\n await reportProgress(progress);\n await sleepMs(delay);\n }\n\n return {\n success: true,\n results: {\n message: `Completed ${total} iterations`,\n total,\n delay,\n name,\n },\n };\n }\n}\n","import { spawn } from \"node:child_process\";\nimport { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Run a shell string, buffering stdout/stderr until exit. Use for short commands\n * only — everything accumulates in memory; long-running / high-throughput work\n * should spawn its own worker instead (see `taskScriptRunner.js`).\n *\n * @param {string} command\n * @param {string} [cwd]\n * @returns {Promise<{ exitCode: number|null, output: string, stderr: string, signal: NodeJS.Signals|null }>}\n */\nfunction runShellCommand(command, cwd) {\n return new Promise((resolve, reject) => {\n const child = spawn(command, {\n shell: true,\n cwd: cwd || process.cwd(),\n stdio: [\"ignore\", \"pipe\", \"pipe\"],\n });\n\n let output = \"\";\n let stderr = \"\";\n\n child.stdout.on(\"data\", (chunk) => {\n output += String(chunk);\n });\n child.stderr.on(\"data\", (chunk) => {\n stderr += String(chunk);\n });\n\n child.on(\"error\", (error) => {\n reject(error);\n });\n\n child.on(\"close\", (exitCode, signal) => {\n resolve({\n exitCode,\n output: output.trim(),\n stderr: stderr.trim(),\n signal,\n });\n });\n });\n}\n\n/**\n * Task wrapper for {@link runShellCommand}. Accepts either:\n *\n * - `params: \"echo hi\"` (string shortcut), or\n * - `params: { command: string, cwd?: string }`.\n *\n * Success is defined as `exitCode === 0`. Non-zero / spawn errors come back as\n * `{ success: false, results: { ... } }` — never a thrown exception.\n */\nexport class TaskShellCommand extends AbstractTask {\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ command: string, cwd?: string }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-shell\", {\n command: \"string\",\n cwd: \"string\",\n }, overrides);\n const command = typeof merged.command === \"string\" ? merged.command.trim() : \"\";\n if (!command) {\n throw new ParamError('shellCommand: param \"command\" must be a non-empty string');\n }\n const cwd = typeof merged.cwd === \"string\" && merged.cwd.trim() ? merged.cwd.trim() : null;\n return cwd ? { command, cwd } : { command };\n }\n\n /**\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run() {\n const params = this.task?.params;\n const commandRaw = typeof params === \"string\" ? params : params?.command;\n const cwdRaw = typeof params === \"string\" ? undefined : params?.cwd;\n const command = typeof commandRaw === \"string\" ? commandRaw.trim() : \"\";\n const cwd = typeof cwdRaw === \"string\" && cwdRaw.trim() ? cwdRaw.trim() : undefined;\n\n if (!command) {\n return {\n success: false,\n results: {\n error: 'Validation failed: param \"command\" must be a non-empty string',\n received: this.task?.params ?? null,\n },\n };\n }\n\n try {\n const result = await runShellCommand(command, cwd);\n const success = result.exitCode === 0;\n\n this.context.logger.info?.(\n `[TaskShellCommand] command=\"${command}\" exitCode=${String(result.exitCode)} (${this.task.id})`\n );\n\n return {\n success,\n results: {\n command,\n cwd: cwd ?? process.cwd(),\n output: result.output,\n stderr: result.stderr,\n exitCode: result.exitCode,\n signal: result.signal,\n },\n };\n } catch (error) {\n return {\n success: false,\n results: {\n command,\n cwd: cwd ?? process.cwd(),\n output: \"\",\n stderr: \"\",\n exitCode: null,\n error: error?.message ?? String(error),\n },\n };\n }\n }\n}\n","import os from \"node:os\";\nimport fs from \"node:fs/promises\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Format bytes as a human-readable gigabyte string, e.g. `\"3.25 GB\"`.\n *\n * @param {number} valueBytes\n * @returns {string}\n */\nfunction toGb(valueBytes) {\n return `${(valueBytes / (1024 ** 3)).toFixed(2)} GB`;\n}\n\n/**\n * Format bytes as a human-readable megabyte string, e.g. `\"128.00 MB\"`.\n *\n * @param {number} valueBytes\n * @returns {string}\n */\nfunction toMb(valueBytes) {\n return `${(valueBytes / (1024 ** 2)).toFixed(2)} MB`;\n}\n\n/**\n * Disk usage for `/` as `{ total, used, free }` strings. Uses `fs.statfs`\n * (Node 20+); for host-level signal we probe the root mount rather than the cwd.\n *\n * @returns {Promise<{ total: string, used: string, free: string }>}\n */\nasync function getDiskStats() {\n // Node 20+ statfs; use root path for host-level signal.\n const stats = await fs.statfs(\"/\");\n const total = Number(stats.bsize) * Number(stats.blocks);\n const free = Number(stats.bsize) * Number(stats.bavail);\n const used = total - free;\n return {\n total: toGb(total),\n used: toGb(used),\n free: toGb(free),\n };\n}\n\n/**\n * Snapshot of host + process stats (memory, CPU utilization, disk, runtime info).\n * Useful for ops dashboards and cluster-wide \"ping with telemetry\".\n */\nexport class TaskSystemInfo extends AbstractTask {\n /** Same UX expectation as `ping` — short probe, print the result. */\n static defaultWaitForResult = true;\n\n /** systemInfo takes no params. */\n static async resolveCustomParams() {\n return null;\n }\n\n /**\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run() {\n try {\n const totalMemory = os.totalmem();\n const freeMemory = os.freemem();\n const usedMemory = totalMemory - freeMemory;\n\n const cpus = os.cpus();\n const cpuUtilization = cpus.map((cpu) => {\n const total = Object.values(cpu.times).reduce((acc, time) => acc + time, 0);\n const usage = ((total - cpu.times.idle) / total) * 100;\n return Number(usage.toFixed(2));\n });\n\n const processMemory = process.memoryUsage();\n const disk = await getDiskStats();\n\n const results = {\n memory: {\n total: toGb(totalMemory),\n used: toGb(usedMemory),\n free: toGb(freeMemory),\n },\n processMemory: {\n rss: toMb(processMemory.rss),\n heapTotal: toMb(processMemory.heapTotal),\n heapUsed: toMb(processMemory.heapUsed),\n external: toMb(processMemory.external),\n },\n disk,\n cpu: {\n cores: cpuUtilization.length,\n utilization: cpuUtilization,\n },\n runtime: {\n platform: os.platform(),\n arch: os.arch(),\n uptimeSec: os.uptime(),\n hostname: os.hostname(),\n },\n };\n\n this.context.logger.info?.(`[TaskSystemInfo] collected system metrics (${this.task.id})`);\n return { success: true, results };\n } catch (error) {\n return {\n success: false,\n results: {\n error: \"Can't collect system stats\",\n message: error?.message ?? String(error),\n },\n };\n }\n }\n}\n","import { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Sanity-check task for wiring: reads `params.a` and `params.b` (both numbers),\n * returns `{ a, b, sum }`. Any non-numeric input short-circuits to a structured\n * validation failure, not an exception.\n */\nexport class TaskSumAB extends AbstractTask {\n /** Short, deterministic — wait by default so callers see the sum. */\n static defaultWaitForResult = true;\n\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ a: number, b: number }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-sumab\", {\n a: \"number\",\n b: \"number\",\n }, overrides);\n if (typeof merged.a !== \"number\" || Number.isNaN(merged.a)) {\n throw new ParamError(`taskSumAB: param \"a\" must be a valid number (got ${JSON.stringify(merged.a)})`);\n }\n if (typeof merged.b !== \"number\" || Number.isNaN(merged.b)) {\n throw new ParamError(`taskSumAB: param \"b\" must be a valid number (got ${JSON.stringify(merged.b)})`);\n }\n return { a: merged.a, b: merged.b };\n }\n\n /**\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run() {\n const a = this.task?.params?.a;\n const b = this.task?.params?.b;\n\n if (typeof a !== \"number\" || Number.isNaN(a)) {\n return {\n success: false,\n results: {\n error: 'Validation failed: param \"a\" must be a valid number',\n received: { a, b },\n },\n };\n }\n\n if (typeof b !== \"number\" || Number.isNaN(b)) {\n return {\n success: false,\n results: {\n error: 'Validation failed: param \"b\" must be a valid number',\n received: { a, b },\n },\n };\n }\n\n const sum = a + b;\n this.context.logger.info?.(`[TaskSumAB] ${a} + ${b} = ${sum} (${this.task.id})`);\n return {\n success: true,\n results: { a, b, sum },\n };\n }\n}\n","import { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Cooperative stop signal for the runner that claims it. Returns a result with\n * `stopRunner: true`; `runTasksLoop` sees that flag in `executeClaimedTask`'s\n * outcome and flips its own stop state, propagating `requestStop(allowanceMs)`\n * to every currently-running task.\n *\n * `params.allowanceMs` controls the grace window (default 5000 ms).\n */\nexport class TaskStopRunner extends AbstractTask {\n /**\n * Stop tasks must target a concrete instance — without `serviceName` the\n * row would race against any worker on the queue. Layered on top of the\n * envelope built by {@link AbstractTask.resolveParams}.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<object>}\n */\n static async resolveParams(context, overrides = {}) {\n const main = await super.resolveParams(context, overrides);\n if (!main.serviceName) {\n throw new ParamError(\n \"stop/stopRunner requires --serviceName (registry instance name; optional --serviceGroup, --instanceNumber, --serverName to narrow targeting)\"\n );\n }\n return main;\n }\n\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ allowanceMs: number }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-stop\", {\n allowanceMs: \"number default 5000\",\n }, overrides);\n const allowanceMs = Number(merged.allowanceMs);\n if (!Number.isFinite(allowanceMs) || allowanceMs < 0) {\n throw new ParamError(\n `stopRunner: allowanceMs must be a non-negative number (got ${JSON.stringify(merged.allowanceMs)})`\n );\n }\n return { allowanceMs };\n }\n\n /**\n * @returns {Promise<{ success: true, results: { stopRunner: true, allowanceMs: number, message: string } }>}\n */\n async run() {\n const allowanceMs = Number(this.task?.params?.allowanceMs ?? 5000);\n this.context.logger.warn?.(`[TaskStopRunner] stop requested (allowanceMs=${allowanceMs})`);\n return {\n success: true,\n results: {\n stopRunner: true,\n allowanceMs,\n message: \"Runner stop requested\",\n },\n };\n }\n}\n","import { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\nimport { readTaskIpcLogsSnapshot } from \"../taskLogs.js\";\n\n/**\n * Fetches IPC FileDatabase rows for `dummyHarvest` (and similar) logs keyed by params.source / params.resource.\n * Intended for machine-targeted enqueue (`server_name` set, `service_group` null) so any runner on that host can execute.\n *\n * Params:\n * - `source` (required) — logical source name, e.g. `\"actris\"`\n * - `resource` (required) — resource slug, e.g. `\"properties\"`\n * - `tail` — max records to return (clamped 1..10000; default 100)\n * - `afterTs` — ISO timestamp watermark; keeps only rows with `ts > afterTs`\n */\nexport class TaskGetLogs extends AbstractTask {\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ source: string, resource: string, tail: number, afterTs?: string }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-get-logs\", {\n source: \"string\",\n resource: \"string\",\n tail: \"number default 100\",\n afterTs: \"string\",\n }, overrides);\n const source = typeof merged.source === \"string\" ? merged.source.trim() : \"\";\n const resource = typeof merged.resource === \"string\" ? merged.resource.trim() : \"\";\n if (!source) throw new ParamError('getLogs: param \"source\" is required');\n if (!resource) throw new ParamError('getLogs: param \"resource\" is required');\n let tail = Number(merged.tail);\n if (!Number.isFinite(tail) || tail < 1) tail = 100;\n tail = Math.min(10_000, Math.max(1, Math.floor(tail)));\n const out = { source, resource, tail };\n if (typeof merged.afterTs === \"string\" && merged.afterTs.trim()) {\n out.afterTs = merged.afterTs.trim();\n }\n return out;\n }\n\n /**\n * @param {unknown} _reportProgress Unused (single-shot read, no progress events).\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run(_reportProgress) {\n const p = this.task.params ?? {};\n const source = String(p.source ?? \"\").trim();\n const resource = String(p.resource ?? \"\").trim();\n const tail = Math.max(1, Math.min(10_000, Number(p.tail) > 0 ? Number(p.tail) : 100));\n const afterTs = p.afterTs != null && String(p.afterTs).trim() ? String(p.afterTs).trim() : null;\n\n if (!source || !resource) {\n return {\n success: false,\n results: { error: 'getLogs requires params \"source\" and \"resource\"' },\n };\n }\n\n try {\n const { records, latestTs } = await readTaskIpcLogsSnapshot(this.context, {\n source,\n resource,\n tail,\n afterTs,\n });\n return {\n success: true,\n results: { records, latestTs, source, resource },\n };\n } catch (e) {\n return {\n success: false,\n results: { error: e?.message ?? String(e) },\n };\n }\n }\n}\n","import { ParamError } from \"../errors.js\";\nimport { TaskPing } from \"./coreTasks/TaskPing.js\";\nimport { TaskSampleProcess } from \"./coreTasks/TaskSampleProcess.js\";\nimport { TaskShellCommand } from \"./coreTasks/TaskShellCommand.js\";\nimport { TaskSystemInfo } from \"./coreTasks/TaskSystemInfo.js\";\nimport { TaskSumAB } from \"./coreTasks/TaskSumAB.js\";\nimport { TaskStopRunner } from \"./coreTasks/TaskStopRunner.js\";\nimport { TaskGetLogs } from \"./coreTasks/TaskGetLogs.js\";\n\n/**\n * Name → task class map. Runners look up the class by `task.name` when claiming\n * a row. Keep names stable across versions: the DB queue references them as strings.\n *\n * Backward-compat aliases (e.g. `stop` → `TaskStopRunner`, `info` → `TaskSystemInfo`)\n * are seeded by {@link TasksRegistry.withCoreTasks}.\n */\nexport class TasksRegistry {\n /**\n * @param {Record<string, Function>} [initial] Optional seed entries to copy in.\n */\n constructor(initial) {\n this.map = {};\n if (initial) {\n this.addMany(initial);\n }\n }\n\n /**\n * Build a registry pre-populated with every core task plus legacy aliases.\n * Prefer this over `new TasksRegistry()` for anything that wants `ping`/`stop`/etc.\n *\n * @returns {TasksRegistry}\n */\n static withCoreTasks() {\n return new TasksRegistry()\n .add(\"ping\", TaskPing)\n .add(\"sampleProcess\", TaskSampleProcess)\n .add(\"shellCommand\", TaskShellCommand)\n .add(\"systemInfo\", TaskSystemInfo)\n .add(\"info\", TaskSystemInfo)\n .add(\"taskSumAB\", TaskSumAB)\n .add(\"stopRunner\", TaskStopRunner)\n // Backward-compat alias\n .add(\"stop\", TaskStopRunner)\n .add(\"getLogs\", TaskGetLogs);\n }\n\n /**\n * Register a single task class under a name. Overwrites any previous entry.\n *\n * @param {string} taskName\n * @param {Function} taskClass Subclass of `AbstractTask`.\n * @returns {this}\n */\n add(taskName, taskClass) {\n this.map[taskName] = taskClass;\n return this;\n }\n\n /**\n * Bulk-register a name → class map. Later calls override earlier ones.\n *\n * @param {Record<string, Function>} entries\n * @returns {this}\n */\n addMany(entries) {\n for (const [name, klass] of Object.entries(entries)) {\n this.add(name, klass);\n }\n return this;\n }\n\n /**\n * Look up a task class by name. Returns `undefined` when the name is unknown;\n * the runner treats that as \"some other worker may handle this\" and skips.\n *\n * @param {string} taskName\n * @returns {Function | undefined}\n */\n get(taskName) {\n return this.map[taskName];\n }\n\n /**\n * Strict variant of {@link get}: throws {@link ParamError} (with the list\n * of supported names) when `taskName` is unknown. Use from enqueuer code\n * paths where an unknown name is a hard CLI/programmer error.\n *\n * @param {string} taskName\n * @returns {Function}\n */\n requireClass(taskName) {\n const TaskClass = taskName ? this.map[taskName] : undefined;\n if (!TaskClass) {\n const supported = this.listSupportedTasks().join(\", \") || \"(none)\";\n throw new ParamError(\n `Unknown task \"${taskName ?? \"\"}\". Supported on this registry: ${supported}`\n );\n }\n return TaskClass;\n }\n\n /**\n * Dispatcher used by `send-task` / programmatic enqueuers: pick `name`\n * from `overrides` or `context.params`, look up the class, and delegate\n * to its static {@link AbstractTask.resolveParams} with `name` seeded into\n * the overrides. The returned object is shaped for {@link enqueueTask}.\n *\n * Validation failures (unknown task, missing required custom params, etc.)\n * surface as {@link ParamError} so the caller aborts cleanly before any\n * row is inserted.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<object>}\n */\n async resolveTaskParams(context, overrides = {}) {\n const overrideName = typeof overrides.name === \"string\" ? overrides.name.trim() : overrides.name;\n const fromCli = context.params.get(\"name\", \"string\");\n const cliName = typeof fromCli === \"string\" ? fromCli.trim() : fromCli;\n const name = overrideName || cliName;\n if (!name) {\n throw new ParamError(\"Task --name is required (e.g. ping, stop, dummyHarvest)\");\n }\n const TaskClass = this.requireClass(name);\n return TaskClass.resolveParams(context, { ...overrides, name });\n }\n\n /**\n * Names of every registered task, sorted alphabetically (useful for CLI output\n * and allowlist sanity checks).\n *\n * @returns {string[]}\n */\n listSupportedTasks() {\n return Object.keys(this.map).sort();\n }\n\n /**\n * Shallow copy of the internal map, for handing to `addMany` on another registry\n * or for serialization.\n *\n * @returns {Record<string, Function>}\n */\n toObject() {\n return { ...this.map };\n }\n}\n","/**\n * Shared allowlist helpers for task runners: normalize CLI strings, merge with service (control) tasks.\n */\n\n/**\n * Task names every runner should be willing to claim (in addition to `--allowedTasks` or\n * service-group config). These map to core `TasksRegistry.withCoreTasks` handlers.\n */\nexport const SERVICE_TASK_NAMES = [\n \"ping\",\n \"stop\",\n \"stopRunner\",\n \"shellCommand\",\n \"systemInfo\",\n \"info\",\n \"getLogs\",\n];\n\n/**\n * Coerce a caller-supplied allowlist value (CLI string, array, or undefined) into\n * a clean string array, or `undefined` when nothing was provided / only blanks.\n *\n * - `\"a,b\"` → `[\"a\", \"b\"]`\n * - `[\"a \", \"\", \" b\"]` → `[\"a\", \"b\"]`\n * - `\" \"` / `undefined` / `null` → `undefined`\n *\n * @param {unknown} value\n * @returns {string[] | undefined}\n */\nexport function normalizeAllowedTasks(value) {\n if (!value) return undefined;\n if (Array.isArray(value)) {\n const out = value.map((v) => String(v).trim()).filter(Boolean);\n return out.length ? out : undefined;\n }\n const out = String(value)\n .split(\",\")\n .map((v) => v.trim())\n .filter(Boolean);\n return out.length ? out : undefined;\n}\n\n/**\n * Union of `SERVICE_TASK_NAMES` and caller-provided names (deduped, sorted).\n * Use when building the allowlist for `runTasksLoop` / `claimNextRunnableTask`.\n *\n * @param {string[]} [names]\n * @returns {string[]}\n */\nexport function mergeAllowedTasksWithServiceTasks(names) {\n const set = new Set([...SERVICE_TASK_NAMES, ...(names ?? [])]);\n return Array.from(set).sort();\n}\n","import { spawn } from \"node:child_process\";\nimport {\n appendTaskIpcLog,\n flushTaskIpcLogs,\n resolveIpcFileLogsDir,\n} from \"./taskLogs.js\";\n\n/** Upper bound on the `progress` column write; keeps a runaway child from bloating the row. */\nconst MAX_PROGRESS_TEXT_LEN = 4000;\n\n/**\n * Remove empty / non-string entries from a CLI args array.\n *\n * @param {unknown[]} [args]\n * @returns {string[]}\n */\nfunction toCliArgs(args = []) {\n return args.filter((a) => typeof a === \"string\" && a.length > 0);\n}\n\n/**\n * Short prefix for child-side log lines: `<taskName>:<id8>[:<opid>]`.\n *\n * @param {{ name: string, id: string, opid?: string|null }} task\n * @returns {string}\n */\nfunction formatChildLogPrefix(task) {\n return `${task.name}:${task.id.slice(0, 8)}${task.opid ? `:${task.opid}` : \"\"}`;\n}\n\n/**\n * True progress event: must carry `level === \"progress\"` and numeric `count`/`total`.\n * Anything else (debug, info, error, worker-result sentinel...) is treated as a regular log.\n *\n * @param {unknown} payload\n * @returns {boolean}\n */\nfunction isProgressPayload(payload) {\n if (!payload || typeof payload !== \"object\") return false;\n if (payload.level !== \"progress\") return false;\n const count = Number(payload.count);\n const total = Number(payload.total);\n return Number.isFinite(count) && Number.isFinite(total) && total > 0;\n}\n\n/**\n * Render a progress payload as `\"[prefix ][message ]count/total\"` for the DB `progress` column.\n *\n * @param {{ prefix?: string, message?: string, count: number, total: number }} payload\n * @param {string} fallbackPrefix Used when `payload.prefix` is missing.\n * @returns {string}\n */\nfunction formatProgressText(payload, fallbackPrefix) {\n const pfx = payload.prefix ? `${payload.prefix} ` : (fallbackPrefix ? `${fallbackPrefix} ` : \"\");\n const label = typeof payload.message === \"string\" && payload.message ? `${payload.message} ` : \"\";\n return `${pfx}${label}${payload.count}/${payload.total}`;\n}\n\n/**\n * Forward a structured IPC log line from the child to the parent's logger at the\n * matching level. `stdout`/`stderr` go through a different path (raw forwarding);\n * this only handles `{ level, message, ... }` payloads.\n *\n * @param {object} context\n * @param {string} prefix\n * @param {unknown} message\n * @returns {void}\n */\nfunction forwardChildLogToParent(context, prefix, message) {\n if (!message || typeof message !== \"object\") return;\n const text = typeof message.message === \"string\" ? message.message : null;\n if (!text) return;\n const level = typeof message.level === \"string\" ? message.level.toLowerCase() : \"\";\n const line = `[child:${prefix}] ${text}`;\n const logger = context.logger;\n switch (level) {\n case \"error\":\n case \"fatal\":\n logger.error?.(line);\n return;\n case \"warn\":\n case \"warning\":\n logger.warn?.(line);\n return;\n case \"debug\":\n logger.debug?.(line);\n return;\n case \"info\":\n default:\n logger.info?.(line);\n }\n}\n\n/**\n * Build the `node` argv for `spawn`:\n *\n * - If the parent was started with `tsx`/`ts-node` (visible in `process.execArgv`),\n * inherit those flags so the child can load `.ts` files.\n * - Otherwise fall back to `--import tsx` so plain `node` can still run TypeScript\n * workers without each caller wiring it up.\n *\n * @param {string} scriptPath\n * @param {string[]} cliArgs\n * @returns {string[]}\n */\nfunction buildNodeArgs(scriptPath, cliArgs) {\n const inheritedExecArgs = Array.isArray(process.execArgv) ? [...process.execArgv] : [];\n const hasTsRuntimeInParent = inheritedExecArgs.some((arg) => /tsx|ts-node/i.test(arg));\n return hasTsRuntimeInParent\n ? [...inheritedExecArgs, scriptPath, ...cliArgs]\n : [\"--import\", \"tsx\", scriptPath, ...cliArgs];\n}\n\n/**\n * Where progress writes land: `context.tasksQueueName` > `params.get(\"table\")` > `\"tasks\"`.\n *\n * @param {object} context\n * @returns {string}\n */\nfunction resolveTasksTableName(context) {\n return context.tasksQueueName || context.params?.get?.(\"table\") || \"tasks\";\n}\n\n/**\n * Announce the IPC-file-logs target once at spawn time. Emits a single info line\n * with the resolved directory and notes when `tasksLogsEnabled=false` keeps logs in-memory only.\n *\n * @param {object} context\n * @param {{ ipcFileLogs?: { basePath?: string, namespace?: string, tableName: string } }} options\n * @returns {void}\n */\nfunction announceIpcFileLogsTarget(context, options) {\n if (!options.ipcFileLogs) return;\n const logsDir = resolveIpcFileLogsDir(context, options.ipcFileLogs);\n const enabledRaw = context.params?.get?.(\"tasksLogsEnabled\");\n const logsEnabled = enabledRaw === undefined ? true : !!enabledRaw;\n context.logger.info?.(\n `[tasks] IPC file logs: ${logsDir}` +\n (logsEnabled ? \"\" : \" (tasksLogsEnabled=false; not persisted)\")\n );\n}\n\n/**\n * Single-consumer FIFO for async work: each `push` runs after the previous one\n * finishes (success or failure). Thrown errors are swallowed by the chain so one\n * bad task never wedges the rest; callers handle errors inside their own `fn`.\n *\n * @returns {{ push: (fn: () => Promise<void>) => Promise<void>, drain: () => Promise<void> }}\n */\nfunction createSerializedQueue() {\n let chain = Promise.resolve();\n return {\n push(fn) {\n chain = chain.then(fn, () => {}).catch(() => {});\n return chain;\n },\n drain() {\n return chain.catch(() => {});\n },\n };\n}\n\n/**\n * Fork `scriptPath` as a Node child with IPC, forward its stdio + IPC logs to the\n * parent logger, update the task row's `progress` column on `{ level: \"progress\" }`\n * payloads, and resolve with a summary once the child closes.\n *\n * Contract:\n * - **Only** IPC payloads matching {@link isProgressPayload} touch the `progress` column.\n * stdout/stderr are diagnostic — accumulated and forwarded to logger but never\n * persisted to `progress`.\n * - The worker returns its final value via `process.send({ __taskWorkerResult: ... })`;\n * that sentinel is captured and exposed as `workerResult`.\n * - DB write and `onProgress` callback for a given payload run sequentially on a shared\n * queue so ordering is preserved across rapid updates.\n *\n * @param {object} context\n * @param {{\n * scriptPath: string,\n * task: { id: string, name: string, opid?: string|null },\n * args?: string[],\n * cwd?: string,\n * onProgress?: (progressText: string) => unknown | Promise<unknown>,\n * onChildIpcMessage?: (message: unknown) => void,\n * ipcFileLogs?: { basePath?: string, namespace?: string, tableName: string },\n * }} options\n * @returns {Promise<{\n * exitCode: number | null,\n * signal: NodeJS.Signals | null,\n * stdout: string,\n * stderr: string,\n * workerResult: unknown,\n * hadErrorMessage: boolean,\n * }>}\n */\nexport async function runNodeTaskScript(context, options) {\n const cliArgs = toCliArgs([\"--route=ipc\", \"--mode=json\", ...(options.args || [])]);\n const nodeArgs = buildNodeArgs(options.scriptPath, cliArgs);\n const child = spawn(process.execPath, nodeArgs, {\n cwd: options.cwd || process.cwd(),\n stdio: [\"ignore\", \"pipe\", \"pipe\", \"ipc\"],\n env: {\n ...process.env,\n TASK_ID: options.task.id,\n TASK_NAME: options.task.name,\n TASK_OPID: options.task.opid || \"\",\n },\n });\n\n announceIpcFileLogsTarget(context, options);\n\n const prefix = formatChildLogPrefix(options.task);\n const tasksTable = resolveTasksTableName(context);\n const progressQueue = createSerializedQueue();\n\n const state = {\n stdout: \"\",\n stderr: \"\",\n workerResult: null,\n hadErrorMessage: false,\n };\n\n /**\n * Serialize a progress update: write to DB, then invoke the optional callback.\n * Each half is independently try/caught so one failure doesn't skip the other.\n *\n * @param {string} text\n */\n const writeProgress = (text) => {\n const trimmed = typeof text === \"string\" ? text.slice(0, MAX_PROGRESS_TEXT_LEN) : \"\";\n if (!trimmed) return;\n progressQueue.push(async () => {\n const db = context.db;\n if (db) {\n try {\n await db(tasksTable).where({ id: options.task.id }).update({ progress: trimmed });\n } catch (error) {\n context.logger.warn?.(\n `[tasks] failed to update progress for task=${options.task.id}: ${error?.message ?? String(error)}`\n );\n }\n }\n if (options.onProgress) {\n try {\n await options.onProgress(trimmed);\n } catch (error) {\n context.logger.warn?.(\n `[tasks] reportProgress callback failed for task=${options.task.id}: ${error?.message ?? String(error)}`\n );\n }\n }\n });\n };\n\n child.stdout?.on(\"data\", (chunk) => {\n const text = String(chunk);\n state.stdout += text;\n if (text.trim()) {\n context.logger.info?.(`[child:${prefix}] ${text.trimEnd()}`);\n }\n });\n child.stderr?.on(\"data\", (chunk) => {\n const text = String(chunk);\n state.stderr += text;\n if (text.trim()) {\n context.logger.warn?.(`[child:${prefix}] ${text.trimEnd()}`);\n }\n });\n\n child.on(\"message\", (message) => {\n if (message && typeof message === \"object\" && \"__taskWorkerResult\" in message) {\n state.workerResult = message.__taskWorkerResult;\n return;\n }\n\n if (message && typeof message === \"object\") {\n const level = typeof message.level === \"string\" ? message.level.toLowerCase() : \"\";\n if (level === \"error\" || level === \"fatal\") {\n state.hadErrorMessage = true;\n }\n }\n\n appendTaskIpcLog(context, options.task, message, options.ipcFileLogs);\n\n try {\n options.onChildIpcMessage?.(message);\n } catch (e) {\n context.logger.warn?.(`[tasks] onChildIpcMessage failed: ${e?.message ?? String(e)}`);\n }\n\n if (isProgressPayload(message)) {\n context.logger.progress(message.message || \"progress\", {\n prefix: message.prefix || prefix,\n count: Number(message.count),\n total: Number(message.total),\n });\n writeProgress(formatProgressText(message, prefix));\n return;\n }\n\n forwardChildLogToParent(context, prefix, message);\n });\n\n return await new Promise((resolve, reject) => {\n child.on(\"error\", (error) => reject(error));\n child.on(\"close\", (exitCode, signal) => {\n void (async () => {\n await flushTaskIpcLogs(context);\n await progressQueue.drain();\n resolve({\n exitCode,\n signal,\n stdout: state.stdout.trim(),\n stderr: state.stderr.trim(),\n workerResult: state.workerResult,\n hadErrorMessage: state.hadErrorMessage,\n });\n })();\n });\n });\n}\n"],"mappings":";AAAA,OAAOA,SAAQ;;;ACiIR,SAAS,kBAAkB,YAAY;AAE1C,QAAM,WAAW;AAEjB,MAAI,CAAC,SAAS,KAAK,UAAU,GAAG;AAC5B,WAAO;AAAA,EACX;AAEA,QAAM,OAAO,IAAI,KAAK,UAAU;AAChC,SAAO,CAAC,MAAM,KAAK,QAAQ,CAAC,KAAK,KAAK,QAAQ,IAAI;AACtD;;;ACpIA,OAAO,QAAQ;AACf,OAAO,UAAU;AAOjB,eAAsB,cAAc,WAAW;AAC3C,QAAM,WAAW,KAAK,QAAQ,GAAG,SAAS;AAE1C,MAAI,CAAC,GAAG,WAAW,QAAQ,GAAG;AAC1B,UAAM,GAAG,SAAS,MAAM,UAAU,EAAE,WAAW,KAAK,CAAC;AAAA,EACzD;AAEA,SAAO;AACX;AAkBO,SAAS,iBAAiB,UAAU;AACvC,UAAQ,UAAU;AAAA,IACd,KAAK;AAAA,IACL,KAAK;AACD,aAAO;AAAA,IACX,KAAK;AACD,aAAO;AAAA,IACX,KAAK;AACD,aAAO;AAAA,IACX;AACI,aAAO;AAAA,EACf;AACJ;;;AC9CA,OAAOC,SAAQ;AACf,OAAOC,WAAU;AACjB,SAAS,gBAAgB;AAOlB,SAAS,iBAAiB,YAAY;AACzC,MAAI;AAEA,QAAI,cAAc;AAClB,QAAI,CAACD,IAAG,WAAW,UAAU,GAAG;AAC5B,YAAM,YAAYC,MAAK,QAAQ,UAAU;AACzC,UAAID,IAAG,WAAW,SAAS,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Q,2BAA2B,KAAK;AAAA,MACjE,2BAA2B,QAAQ,6BAA6B,KAAK;AAAA,MACrE,gBAAgB,QAAQ,kBAAkB,KAAK;AAAA,IACnD,CAAC;AAAA,EACL;AACJ;","names":["os","fs","path","getDb","path","fs","path","path","maxDate","fs","destPath","path","state","record","os","fs","fs","os","out","spawn","spawn","getDb","os"]}
1
+ {"version":3,"sources":["../src/tasks/index.js","../src/utils/date-utils.js","../src/utils/fs-utils.js","../src/utils/os-utils.js","../src/utils/format-utils.js","../src/utils/core-utils.js","../src/tasks/servicesRegistry.js","../src/tasks/taskUtils.js","../src/tasks/time-matcher.js","../src/tasks/taskLogs.js","../src/filedatabase/index.js","../src/filedatabase/serializers.js","../src/errors.js","../src/tasks/AbstractTask.js","../src/tasks/coreTasks/TaskPing.js","../src/tasks/coreTasks/TaskSampleProcess.js","../src/tasks/coreTasks/TaskShellCommand.js","../src/tasks/coreTasks/TaskSystemInfo.js","../src/tasks/coreTasks/TaskSumAB.js","../src/tasks/coreTasks/TaskStopRunner.js","../src/tasks/coreTasks/TaskGetLogs.js","../src/tasks/TasksRegistry.js","../src/tasks/serviceTaskAllowlist.js","../src/tasks/taskScriptRunner.js"],"sourcesContent":["import os from \"node:os\";\nimport { sleepMs, toJsonColumn } from \"../utils/index.js\";\nimport {\n registerInServicesRegistry,\n touchServicesRegistry,\n unregisterServicesRegistry,\n} from \"./servicesRegistry.js\";\nimport {\n enqueueTask,\n ensureTaskTables,\n queueToTableNames,\n taskHistoryInsertFromQueueRow,\n updateTaskProgress,\n} from \"./taskUtils.js\";\nimport { appendTaskIpcLog } from \"./taskLogs.js\";\nimport { nextTimeMatch, timeMatcher } from \"./time-matcher.js\";\nexport {\n timeMatcher,\n nextTimeMatch,\n matchesParsedPattern,\n convertPattern,\n resolveAsterisks,\n resolveRanges,\n resolveSteps,\n} from \"./time-matcher.js\";\nimport { TasksRegistry } from \"./TasksRegistry.js\";\nimport { normalizeAllowedTasks } from \"./serviceTaskAllowlist.js\";\n\n/** Sentinel value stored in the `progress` column when a task is paused due to error. */\nconst LOCKED_BY_ERROR_MESSAGE = \"locked by error\";\n\nexport {\n enqueueTask,\n ensureTaskTables,\n queueToTableNames,\n taskHistoryInsertFromQueueRow,\n updateTaskProgress,\n} from \"./taskUtils.js\";\nexport {\n listServicesRegistry,\n registerInServicesRegistry,\n touchServicesRegistry,\n unregisterServicesRegistry,\n updateServicesRegistryMetadata,\n} from \"./servicesRegistry.js\";\n/** @deprecated Use registerInServicesRegistry */\nexport { registerInServicesRegistry as registerRunnerHeartbeat } from \"./servicesRegistry.js\";\n/** @deprecated Use touchServicesRegistry */\nexport { touchServicesRegistry as touchRunnerHeartbeat } from \"./servicesRegistry.js\";\n/** @deprecated Use unregisterServicesRegistry */\nexport { unregisterServicesRegistry as unregisterRunnerHeartbeat } from \"./servicesRegistry.js\";\n/** @deprecated Use listServicesRegistry */\nexport { listServicesRegistry as listAliveRunnerHeartbeats } from \"./servicesRegistry.js\";\nexport {\n appendTaskIpcLog,\n flushTaskIpcLogs,\n ipcFileLogsTableNameForSourceResource,\n readTaskIpcLogsSnapshot,\n resolveIpcFileLogsDir,\n} from \"./taskLogs.js\";\nexport { runNodeTaskScript } from \"./taskScriptRunner.js\";\nexport { AbstractTask } from \"./AbstractTask.js\";\nexport { TasksRegistry } from \"./TasksRegistry.js\";\nexport { TaskPing } from \"./coreTasks/TaskPing.js\";\nexport { TaskSampleProcess } from \"./coreTasks/TaskSampleProcess.js\";\nexport { TaskShellCommand } from \"./coreTasks/TaskShellCommand.js\";\nexport { TaskSystemInfo } from \"./coreTasks/TaskSystemInfo.js\";\nexport { TaskSumAB } from \"./coreTasks/TaskSumAB.js\";\nexport { TaskStopRunner } from \"./coreTasks/TaskStopRunner.js\";\nexport { TaskGetLogs } from \"./coreTasks/TaskGetLogs.js\";\nexport {\n normalizeAllowedTasks,\n mergeAllowedTasksWithServiceTasks,\n SERVICE_TASK_NAMES,\n} from \"./serviceTaskAllowlist.js\";\n\n/** Shared default registry preloaded with every core task (including legacy aliases). */\nexport const defaultTasksRegistry = TasksRegistry.withCoreTasks();\n\n/**\n * Fail-fast accessor for `context.db` with a tasks-specific error message.\n *\n * @param {object} context\n * @returns {Function}\n */\nfunction getDb(context) {\n const db = context.db;\n if (!db) {\n throw new Error(\"Tasks component requires context.db. Initialize DB first and attach to context.\");\n }\n return db;\n}\n\n/**\n * Coerce whatever the caller passed as `registry` into a `TasksRegistry` instance:\n *\n * - `undefined` / missing → {@link defaultTasksRegistry} (every core task)\n * - already a `TasksRegistry` → returned as-is\n * - plain `{ name: Class }` map → wrapped in a fresh registry\n *\n * @param {TasksRegistry | Record<string, Function> | undefined} registry\n * @returns {TasksRegistry}\n */\nfunction normalizeRegistry(registry) {\n if (!registry) return defaultTasksRegistry;\n if (registry instanceof TasksRegistry) return registry;\n return new TasksRegistry().addMany(registry);\n}\n\n/**\n * Convenience for enqueuing a targeted `stopRunner` task so a specific service\n * group exits cleanly on its next tick. Intended for operator tooling — the\n * runner itself also accepts in-process stop signals via `context.isStop()`.\n *\n * @param {object} context\n * @param {string} serviceGroup\n * @param {string} [queueName]\n * @param {number} [allowanceMs]\n * @returns {Promise<string>} UUID of the enqueued stop task.\n */\nexport async function enqueueStopTask(context, serviceGroup, queueName = \"tasks\", allowanceMs = 5000) {\n return enqueueTask(context, {\n queueName,\n name: \"stopRunner\",\n params: { allowanceMs },\n priority: 0,\n serviceGroup,\n });\n}\n\n/**\n * Broadcast a cooperative stop signal to every currently-running task instance.\n * Tasks decide per-call how much of `allowanceMs` to honor; we also emit on\n * `context.emitter` so other subscribers (e.g. fetchers) can wind down.\n *\n * @param {object} context\n * @param {Map<string, { requestStop?: Function }>} runningTaskInstances\n * @param {number} allowanceMs\n * @returns {Promise<void>}\n */\nasync function signalRunningTasksStop(context, runningTaskInstances, allowanceMs) {\n context.logger.warn?.(`[tasks] signaling ${runningTaskInstances.size} running task(s) to stop`);\n for (const [, taskInstance] of runningTaskInstances) {\n if (typeof taskInstance.requestStop === \"function\") {\n try {\n await taskInstance.requestStop(allowanceMs);\n } catch (error) {\n context.logger.warn?.(\"[tasks] task requestStop failed:\", error);\n }\n }\n }\n context.emitter.emit(\"stop\", allowanceMs);\n}\n\n/**\n * Run a single claimed task row end-to-end:\n *\n * 1. Resolve its class from the registry (unknown name → record failure + remove/pause).\n * 2. Construct an instance, stash it in `runningTaskInstances` so stop signals can reach it.\n * 3. `await instance.run(reportProgress)`; capture thrown errors into a structured `results` payload.\n * 4. Append a row to the history table, update/delete/pause the queue row depending on schedule/success.\n * 5. Return a summary telling the loop whether a `stopRunner` was requested.\n *\n * @param {object} context\n * @param {string} tasksTable\n * @param {string} historyTable\n * @param {object} row The claimed queue row.\n * @param {TasksRegistry} registry\n * @param {Map<string, object>} runningTaskInstances\n * @returns {Promise<{ stopRunnerRequested: boolean, stopAllowanceMs: number }>}\n */\nasync function executeClaimedTask(context, tasksTable, historyTable, row, registry, runningTaskInstances) {\n const db = getDb(context);\n const taskName = row.name;\n const TaskClass = registry.get(taskName);\n if (!TaskClass) {\n const err = { message: `Unknown task \"${taskName}\"` };\n await db(historyTable).insert(\n taskHistoryInsertFromQueueRow(row, {\n completed_at: new Date(),\n success: false,\n status: \"failed\",\n status_changed_at: db.fn.now(),\n params: toJsonColumn(row.params),\n results: toJsonColumn(err),\n })\n );\n if (row.schedule) {\n await db(tasksTable).where({ id: row.id }).update({\n started_at: null,\n completed_at: new Date(),\n success: false,\n results: toJsonColumn(err),\n past_due: null,\n status: \"paused\",\n status_changed_at: db.fn.now(),\n progress: LOCKED_BY_ERROR_MESSAGE,\n });\n } else {\n await db(tasksTable).where({ id: row.id }).delete();\n }\n return { stopRunnerRequested: false, stopAllowanceMs: 0 };\n }\n\n let success = false;\n let results = null;\n let taskInstance = null;\n try {\n taskInstance = new TaskClass(context, row);\n runningTaskInstances.set(row.id, taskInstance);\n const runResult = await taskInstance.run((progress) => updateTaskProgress(context, tasksTable, row.id, progress));\n success = !!runResult?.success;\n results = runResult?.results ?? null;\n } catch (error) {\n success = false;\n results = {\n message: error?.message ?? String(error),\n name: error?.name ?? \"Error\",\n stack: error?.stack ?? null,\n };\n } finally {\n runningTaskInstances.delete(row.id);\n }\n\n await db(historyTable).insert(\n taskHistoryInsertFromQueueRow(row, {\n completed_at: new Date(),\n success,\n status: success ? \"completed\" : \"failed\",\n status_changed_at: db.fn.now(),\n params: toJsonColumn(row.params),\n results: toJsonColumn(results),\n })\n );\n if (!success) {\n const dbName = String(context?.params?.get?.(\"dbName\") || \"local\");\n const tableName = String(context?.params?.get?.(\"table\") || \"tasks\");\n const fallbackRecoverCommand = [\n \"node\",\n \"examples/tasks/recover-task.js\",\n `--dbName='${dbName.replace(/'/g, `'\\\\''`)}'`,\n `--table='${tableName.replace(/'/g, `'\\\\''`)}'`,\n `--id='${String(row.id).replace(/'/g, `'\\\\''`)}'`,\n ].join(\" \");\n const rerunCommand = results && typeof results === \"object\" && results.rerunCommand\n ? results.rerunCommand\n : fallbackRecoverCommand;\n appendTaskIpcLog(context, row, {\n level: \"error\",\n message: `[tasks] task failed: ${row.name} id=${row.id}. Once problem is fixed, re-run: ${rerunCommand}`,\n details: results,\n });\n }\n\n if (row.schedule) {\n if (success) {\n let nextRunAt = null;\n try {\n nextRunAt = nextTimeMatch(row.schedule, new Date());\n } catch (e) {\n context.logger?.warn?.(`[tasks] nextTimeMatch after success for task ${row.id}: ${e?.message ?? String(e)}`);\n }\n await db(tasksTable).where({ id: row.id }).update({\n started_at: null,\n completed_at: new Date(),\n success,\n results: toJsonColumn(results),\n progress: null,\n past_due: null,\n status: \"idle\",\n status_changed_at: db.fn.now(),\n next_run_at: nextRunAt,\n // Claim overwrites these; clear so idle rows stay “any worker” (see claimNextRunnableTask).\n service_name: null,\n server_name: null,\n instance_number: null,\n });\n } else {\n await db(tasksTable).where({ id: row.id }).update({\n started_at: null,\n completed_at: new Date(),\n success,\n results: toJsonColumn(results),\n status: \"paused\",\n status_changed_at: db.fn.now(),\n progress: LOCKED_BY_ERROR_MESSAGE,\n past_due: null,\n });\n }\n } else {\n await db(tasksTable).where({ id: row.id }).delete();\n }\n\n const stopRunnerRequested = !!(results && typeof results === \"object\" && results.stopRunner === true);\n const stopAllowanceMs = stopRunnerRequested ? Number(results.allowanceMs ?? 5000) : 0;\n return { stopRunnerRequested, stopAllowanceMs };\n}\n\n/** Fisher–Yates shuffle so concurrent workers don't all try the same candidate row first. */\nfunction shuffleTaskRowsInPlace(rows) {\n for (let i = rows.length - 1; i > 0; i--) {\n const j = Math.floor(Math.random() * (i + 1));\n const t = rows[i];\n rows[i] = rows[j];\n rows[j] = t;\n }\n}\n\n/**\n * Atomically claim one runnable task row matching the caller's service group /\n * identity / allowlist, and return the (now-`running`) row — or `null` when none\n * are ready.\n *\n * Targeting rules for task columns (`service_group`, `service_name`, `server_name`,\n * `instance_number`): NULL on the task row means \"any\" for that field; a value\n * means \"this runner must match exactly\". This lets operators enqueue a task for\n * a specific host/instance while letting other rows fan out to whoever is free.\n *\n * @param {object} context\n * @param {string} tasksTable\n * @param {string} serviceGroup\n * @param {TasksRegistry} registry\n * @param {number} scanLimit How many candidate rows to pull before attempting claim.\n * @param {string[]|undefined} taskNames When set, only claim rows with `name IN taskNames`.\n * @param {{ service_name: string, server_name: string, instance_number: number }|null} runnerIdentity\n * @returns {Promise<object|null>} The claimed row, or `null` when nothing is ready.\n */\nasync function claimNextRunnableTask(\n context,\n tasksTable,\n serviceGroup,\n registry,\n scanLimit,\n taskNames,\n runnerIdentity\n) {\n const db = getDb(context);\n\n // Targeting: NULL on the task row means \"any\" for that field. Rows must match the runner's\n // service_group and identity (when provided) for each non-null task column.\n let query = db(tasksTable)\n .where({ status: \"idle\" })\n .where(function () {\n this.whereNull(\"service_group\").orWhere({ service_group: serviceGroup });\n })\n // Honor next_run_at as a \"not before\" gate. NULL = no delay (claim now).\n // This makes delayed one-off retries (next_run_at set, no schedule) wait\n // their turn; scheduled rows already set next_run_at to their next fire\n // time on enqueue/completion, so this stays consistent with timeMatcher.\n .where(function () {\n this.whereNull(\"next_run_at\").orWhere(\"next_run_at\", \"<=\", db.fn.now());\n })\n .orderByRaw(\"CASE WHEN past_due IS NULL THEN 1 ELSE 0 END ASC\")\n .orderBy([{ column: \"priority\", order: \"asc\" }])\n // Fair rotation for recurring tasks:\n // 1) never-run rows first\n // 2) then least recently completed rows\n // 3) then stable created_at order\n .orderByRaw(\"CASE WHEN completed_at IS NULL THEN 0 ELSE 1 END ASC\")\n .orderBy([{ column: \"completed_at\", order: \"asc\" }, { column: \"created_at\", order: \"asc\" }])\n .limit(scanLimit);\n if (taskNames && taskNames.length > 0) {\n query = query.whereIn(\"name\", taskNames);\n }\n if (runnerIdentity) {\n query = query\n .where(function () {\n this.whereNull(\"service_name\").orWhere({ service_name: runnerIdentity.service_name });\n })\n .where(function () {\n this.whereNull(\"instance_number\").orWhere({ instance_number: runnerIdentity.instance_number });\n })\n .where(function () {\n this.whereNull(\"server_name\").orWhere({ server_name: runnerIdentity.server_name });\n });\n } else {\n // Without registry identity we cannot match a specific instance; only rows with no per-instance targeting.\n query = query\n .whereNull(\"service_name\")\n .whereNull(\"instance_number\")\n .whereNull(\"server_name\");\n }\n const candidates = await query;\n shuffleTaskRowsInPlace(candidates);\n\n for (const row of candidates) {\n if (!row.past_due && row.schedule && !timeMatcher(row.schedule)) {\n continue;\n }\n\n const TaskClass = registry.get(row.name);\n if (!TaskClass) {\n // Not registered on this runner — skip without claiming so another worker can run it.\n continue;\n }\n\n const taskInstance = new TaskClass(context, row);\n const reason = taskInstance.cantRunReason ? await taskInstance.cantRunReason() : null;\n if (reason) {\n if (!row.past_due) {\n await db(tasksTable).where({ id: row.id }).update({\n past_due: db.fn.now(),\n progress: String(reason),\n });\n }\n continue;\n }\n\n const claimPatch = {\n started_at: db.fn.now(),\n status: \"running\",\n status_changed_at: db.fn.now(),\n };\n if (runnerIdentity) {\n claimPatch.service_name = runnerIdentity.service_name;\n claimPatch.server_name = runnerIdentity.server_name;\n claimPatch.instance_number = runnerIdentity.instance_number;\n }\n\n const updated = await db(tasksTable)\n .where({ id: row.id, status: \"idle\" })\n .update(claimPatch)\n .returning(\"*\");\n\n const claimed = Array.isArray(updated) ? updated[0] : null;\n if (claimed) return claimed;\n }\n\n return null;\n}\n\n/**\n * Main runner loop. Registers in the services registry (optional), then polls\n * the queue, claiming up to `maxParallel` tasks at a time plus one extra\n * \"control lane\" for `stop`/`stopRunner` so a graceful stop can always be picked\n * even when workers are saturated.\n *\n * Exits when any of the following become true:\n * - `context.isStop()` flips to `true` (external shutdown signal).\n * - A `stopRunner` task completes successfully.\n * - `context.tasksRunnerStop === true` (manual flag, mostly for tests).\n *\n * @param {object} context\n * @param {{\n * queueName?: string,\n * target: string,\n * pollMs?: number,\n * claimJitterMs?: number,\n * maxParallel?: number,\n * scanLimit?: number,\n * allowedTasks?: string | string[],\n * registry?: TasksRegistry | Record<string, Function>,\n * runnerServiceGroup?: string,\n * runnerServiceName?: string,\n * runnerInstanceNumber?: number,\n * runnerHeartbeatIntervalMs?: number,\n * runnerHeartbeatStaleMs?: number,\n * runnerGroupMaxInstances?: number,\n * runnerEnforceMaxInstances?: boolean,\n * runnerMetadata?: Record<string, unknown>,\n * }} options\n * @returns {Promise<void>}\n */\nexport async function runTasksLoop(context, options) {\n const queueName = options.queueName ?? \"tasks\";\n const target = options.target;\n const pollMs = options.pollMs ?? 1000;\n const claimJitterMs = options.claimJitterMs ?? 0;\n const maxParallel = options.maxParallel ?? 32;\n const scanLimit = options.scanLimit ?? 100;\n const allowedTasks = normalizeAllowedTasks(options.allowedTasks);\n const registry = normalizeRegistry(options.registry);\n const { tasksTable, historyTable } = queueToTableNames(queueName);\n\n if (!target) throw new Error(\"runTasksLoop: target is required\");\n\n context.tasksQueueName = queueName;\n\n const runningPromises = new Set();\n const runningTaskInstances = new Map();\n let runningStopControlPromise = null;\n let stopRequested = false;\n let stopAllowanceMs = 5000;\n context.tasksRunnerStop = false;\n\n let registryReg = null;\n let registryInterval = null;\n let runnerIdentity = null;\n const hbGroup = options.runnerServiceGroup?.trim();\n if (hbGroup) {\n const hbIntervalMs = options.runnerHeartbeatIntervalMs ?? 10_000;\n const staleMs = options.runnerHeartbeatStaleMs ?? 45_000;\n const defaultMeta = {\n component: \"tasks-runner\",\n allowedTasks: allowedTasks?.length ? allowedTasks.join(\",\") : \"all\",\n };\n registryReg = await registerInServicesRegistry(context, {\n queueName,\n target,\n serviceGroup: hbGroup,\n serviceName: options.runnerServiceName,\n instanceNumber: options.runnerInstanceNumber,\n staleMs,\n groupMaxInstances: options.runnerGroupMaxInstances,\n enforceMaxInstances: options.runnerEnforceMaxInstances ?? true,\n metadata: options.runnerMetadata ?? defaultMeta,\n });\n runnerIdentity = {\n service_name: registryReg.serviceName,\n server_name: os.hostname(),\n instance_number: registryReg.instanceNumber,\n };\n registryInterval = setInterval(() => {\n void touchServicesRegistry(context, registryReg).catch((err) => {\n context.logger.warn?.(`[services-registry] touch failed: ${err?.message ?? String(err)}`);\n });\n }, hbIntervalMs);\n }\n\n try {\n while (!context.isStop() && !stopRequested && context.tasksRunnerStop !== true) {\n // Control lane: always allow stop task to be picked even when workers are busy.\n if (!runningStopControlPromise) {\n const claimedStopTask = await claimNextRunnableTask(\n context,\n tasksTable,\n target,\n registry,\n 10,\n [\"stopRunner\", \"stop\"],\n runnerIdentity\n );\n if (claimedStopTask) {\n runningStopControlPromise = executeClaimedTask(\n context,\n tasksTable,\n historyTable,\n claimedStopTask,\n registry,\n runningTaskInstances\n )\n .then(async (outcome) => {\n if (outcome.stopRunnerRequested && !stopRequested) {\n stopRequested = true;\n stopAllowanceMs = outcome.stopAllowanceMs || 5000;\n context.tasksRunnerStop = true;\n await signalRunningTasksStop(context, runningTaskInstances, stopAllowanceMs);\n }\n })\n .finally(() => {\n runningStopControlPromise = null;\n });\n }\n }\n\n if (claimJitterMs > 0) {\n await sleepMs(Math.floor(Math.random() * (claimJitterMs + 1)));\n }\n\n while (runningPromises.size < maxParallel) {\n const claimed = await claimNextRunnableTask(\n context,\n tasksTable,\n target,\n registry,\n scanLimit,\n allowedTasks,\n runnerIdentity\n );\n if (!claimed) break;\n\n const p = executeClaimedTask(context, tasksTable, historyTable, claimed, registry, runningTaskInstances)\n .then(async (outcome) => {\n if (outcome.stopRunnerRequested && !stopRequested) {\n stopRequested = true;\n stopAllowanceMs = outcome.stopAllowanceMs || 5000;\n context.tasksRunnerStop = true;\n await signalRunningTasksStop(context, runningTaskInstances, stopAllowanceMs);\n }\n })\n .finally(() => {\n runningPromises.delete(p);\n });\n runningPromises.add(p);\n }\n\n const wakePromises = [...runningPromises];\n if (runningStopControlPromise) {\n wakePromises.push(runningStopControlPromise);\n }\n if (wakePromises.length === 0) {\n await sleepMs(pollMs);\n } else {\n const safe = wakePromises.map((p) => p.catch(() => undefined));\n await Promise.race([sleepMs(pollMs), Promise.race(safe)]);\n }\n }\n\n if (context.isStop() && !stopRequested) {\n await signalRunningTasksStop(context, runningTaskInstances, 5000);\n }\n\n if (runningPromises.size > 0) {\n if (stopRequested) {\n await Promise.race([\n Promise.allSettled(Array.from(runningPromises)),\n sleepMs(stopAllowanceMs).then(() => {\n context.logger.warn?.(\n `[tasks] stop allowance (${stopAllowanceMs}ms) reached; ${runningPromises.size} task(s) still running`\n );\n }),\n ]);\n } else {\n await Promise.allSettled(Array.from(runningPromises));\n }\n }\n } finally {\n if (registryInterval) {\n clearInterval(registryInterval);\n registryInterval = null;\n }\n if (registryReg) {\n await unregisterServicesRegistry(context, registryReg).catch((err) => {\n context.logger.warn?.(`[services-registry] unregister failed: ${err?.message ?? String(err)}`);\n });\n registryReg = null;\n delete context.servicesRegistry;\n delete context.runnerHeartbeat;\n }\n }\n}\n\n/**\n * Poll for the outcome of a task by id. Returns the matching `_history` row when\n * the task completes, or `null` when the wait times out / the queue row vanishes\n * without a history entry (unusual; usually a manual delete).\n *\n * Resolves either of:\n * - Legacy path: a history row whose `id` matches the original task id.\n * - Modern path: a history row with the same `name`+`opid` whose\n * `completed_at` is ≥ when we started waiting (so we don't pick up an older run).\n *\n * @param {object} context\n * @param {string} taskId\n * @param {{ queueName?: string, timeoutMs?: number, pollMs?: number }} [options]\n * @returns {Promise<object|null>}\n */\nexport async function waitForTaskResult(context, taskId, options = {}) {\n const db = getDb(context);\n const queueName = options.queueName ?? \"tasks\";\n const timeoutMs = options.timeoutMs ?? 60000;\n const pollMs = options.pollMs ?? 500;\n const { tasksTable, historyTable } = queueToTableNames(queueName);\n const deadline = Date.now() + timeoutMs;\n /** Only match history rows completed after we began waiting (avoids picking an older run with the same name/opid). */\n const waitStartedAt = new Date();\n let cachedNameOpid = null;\n\n /**\n * Look for a matching history entry completed since we started waiting.\n *\n * @param {string} name\n * @param {string|null|undefined} opid\n * @returns {Promise<object|undefined>}\n */\n async function historySinceWait(name, opid) {\n let q = db(historyTable).where({ name }).where(\"completed_at\", \">=\", waitStartedAt);\n if (opid == null || opid === \"\") {\n q = q.whereNull(\"opid\");\n } else {\n q = q.where({ opid });\n }\n return await q.orderBy(\"completed_at\", \"desc\").first();\n }\n\n while (Date.now() <= deadline) {\n const legacy = await db(historyTable).where({ id: taskId }).orderBy(\"created_at\", \"desc\").first();\n if (legacy) {\n return legacy;\n }\n\n const pending = await db(tasksTable).where({ id: taskId }).first();\n if (pending) {\n cachedNameOpid = { name: pending.name, opid: pending.opid };\n const done = await historySinceWait(pending.name, pending.opid);\n if (done) {\n return done;\n }\n } else if (cachedNameOpid) {\n const done = await historySinceWait(cachedNameOpid.name, cachedNameOpid.opid);\n if (done) {\n return done;\n }\n return null;\n } else {\n return null;\n }\n await sleepMs(pollMs);\n }\n return null;\n}\n\n/**\n * Higher-level wrapper over {@link runTasksLoop}: captures defaults / params-driven\n * config at construction, then exposes them as methods (`ensureTaskTables`,\n * `runTasksLoop`) so callers don't have to plumb the same options through twice.\n *\n * Prefer `TasksManager.init(context)` over the bare constructor — `init` pulls\n * sensible defaults from `context.params` using the `tasks` module namespace.\n */\nexport class TasksManager {\n /**\n * @param {object} context\n * @param {{\n * queueName?: string,\n * target?: string,\n * recreateTaskTables?: boolean,\n * pollMs?: number,\n * claimJitterMs?: number,\n * maxParallel?: number,\n * scanLimit?: number,\n * allowedTasks?: string | string[],\n * registry?: TasksRegistry | Record<string, Function>,\n * runnerServiceGroup?: string,\n * runnerServiceName?: string,\n * runnerInstanceNumber?: number,\n * runnerHeartbeatIntervalMs?: number,\n * runnerHeartbeatStaleMs?: number,\n * runnerGroupMaxInstances?: number,\n * runnerEnforceMaxInstances?: boolean,\n * runnerMetadata?: Record<string, unknown>,\n * }} [options]\n */\n constructor(context, options = {}) {\n this.context = context;\n this.queueName = options.queueName ?? \"tasks\";\n this.target = options.target ?? \"localRunner\";\n this.recreateTaskTables = options.recreateTaskTables ?? false;\n this.pollMs = options.pollMs ?? 1000;\n this.claimJitterMs = options.claimJitterMs ?? 0;\n this.maxParallel = options.maxParallel ?? 1;\n this.scanLimit = options.scanLimit ?? 100;\n this.allowedTasks = normalizeAllowedTasks(options.allowedTasks);\n this.registry = normalizeRegistry(options.registry);\n this.runnerServiceGroup = options.runnerServiceGroup;\n this.runnerServiceName = options.runnerServiceName;\n this.runnerInstanceNumber = options.runnerInstanceNumber;\n this.runnerHeartbeatIntervalMs = options.runnerHeartbeatIntervalMs;\n this.runnerHeartbeatStaleMs = options.runnerHeartbeatStaleMs;\n this.runnerGroupMaxInstances = options.runnerGroupMaxInstances;\n this.runnerEnforceMaxInstances = options.runnerEnforceMaxInstances;\n this.runnerMetadata = options.runnerMetadata;\n }\n\n /**\n * Preferred factory: reads defaults from `context.params` (module namespace\n * `\"tasks\"`), then overlays explicit `options`. Keeps CLI flags, env vars,\n * and inline options in one consistent resolver.\n *\n * @param {object} context\n * @param {ConstructorParameters<typeof TasksManager>[1]} [options]\n * @returns {TasksManager}\n */\n static init(context, options = {}) {\n const defs = {\n table: \"string default tasks\",\n target: \"string default localRunner\",\n recreateTaskTables: \"boolean default false\",\n pollMs: \"number default 1000\",\n claimJitterMs: \"number default 0\",\n maxParallel: \"number default 1\",\n scanLimit: \"number default 100\",\n allowedTasks: \"string\",\n runnerServiceGroup: \"string\",\n runnerServiceName: \"string\",\n runnerInstanceNumber: \"number\",\n runnerHeartbeatIntervalMs: \"number default 10000\",\n runnerHeartbeatStaleMs: \"number default 45000\",\n runnerGroupMaxInstances: \"number\",\n runnerEnforceMaxInstances: \"boolean default true\",\n };\n\n const discovered = context.params.getAllForModule(\"tasks\", defs);\n const resolved = {\n queueName: discovered.table,\n target: discovered.target,\n recreateTaskTables: discovered.recreateTaskTables,\n pollMs: discovered.pollMs,\n claimJitterMs: discovered.claimJitterMs,\n maxParallel: discovered.maxParallel,\n scanLimit: discovered.scanLimit,\n allowedTasks: discovered.allowedTasks,\n runnerServiceGroup: discovered.runnerServiceGroup,\n runnerServiceName: discovered.runnerServiceName,\n runnerInstanceNumber: discovered.runnerInstanceNumber,\n runnerHeartbeatIntervalMs: discovered.runnerHeartbeatIntervalMs,\n runnerHeartbeatStaleMs: discovered.runnerHeartbeatStaleMs,\n runnerGroupMaxInstances: discovered.runnerGroupMaxInstances,\n runnerEnforceMaxInstances: discovered.runnerEnforceMaxInstances,\n ...options,\n };\n return new TasksManager(context, resolved);\n }\n\n /**\n * Idempotently ensure the three backing tables exist for this queue.\n *\n * @param {{ recreate?: boolean }} [options]\n * @returns {Promise<void>}\n */\n async ensureTaskTables(options = {}) {\n await ensureTaskTables(this.context, {\n queueName: this.queueName,\n recreate: options.recreate ?? this.recreateTaskTables,\n });\n }\n\n /**\n * Start the runner loop using this manager's resolved config. Per-call\n * options override the stored defaults, but `runnerMetadata` still falls\n * through when omitted.\n *\n * @param {Partial<ConstructorParameters<typeof TasksManager>[1]>} [options]\n * @returns {Promise<void>}\n */\n async runTasksLoop(options = {}) {\n await runTasksLoop(this.context, {\n queueName: options.queueName ?? this.queueName,\n target: options.target ?? this.target,\n pollMs: options.pollMs ?? this.pollMs,\n claimJitterMs: options.claimJitterMs ?? this.claimJitterMs,\n maxParallel: options.maxParallel ?? this.maxParallel,\n scanLimit: options.scanLimit ?? this.scanLimit,\n allowedTasks: options.allowedTasks ?? this.allowedTasks,\n registry: options.registry ?? this.registry,\n runnerServiceGroup: options.runnerServiceGroup ?? this.runnerServiceGroup,\n runnerServiceName: options.runnerServiceName ?? this.runnerServiceName,\n runnerInstanceNumber: options.runnerInstanceNumber ?? this.runnerInstanceNumber,\n runnerHeartbeatIntervalMs: options.runnerHeartbeatIntervalMs ?? this.runnerHeartbeatIntervalMs,\n runnerHeartbeatStaleMs: options.runnerHeartbeatStaleMs ?? this.runnerHeartbeatStaleMs,\n runnerGroupMaxInstances: options.runnerGroupMaxInstances ?? this.runnerGroupMaxInstances,\n runnerEnforceMaxInstances: options.runnerEnforceMaxInstances ?? this.runnerEnforceMaxInstances,\n runnerMetadata: options.runnerMetadata ?? this.runnerMetadata,\n });\n }\n}\n","/**\n * Date/time utility functions for timestamp formatting and timezone conversions\n */\n\n\n\n\n\n\n/**\n * Format a date string or Date object to ISO8601 format in specified timezone\n * Always returns UTC-based ISO strings (YYYY-MM-DDTHH:mm:ssZ) as internal representation\n * \n * @param value - Date object, ISO string, or timestamp\n * @param options - Formatting options\n * @returns ISO8601 formatted string\n */\nexport function formatDate(value, options = {}) {\n if (value === undefined || value === null) {\n return \"\";\n }\n\n const { timezone = \"UTC\", format = \"iso\" } = options;\n \n let date;\n if (value instanceof Date) {\n date = value;\n } else if (typeof value === \"string\") {\n date = new Date(value);\n } else if (typeof value === \"number\") {\n date = new Date(value);\n } else {\n return \"\";\n }\n\n if (isNaN(date.getTime())) {\n return \"\";\n }\n\n // Format based on requested format\n switch (format) {\n case \"iso\":\n // Full ISO8601: 2025-01-01T01:01:01Z\n return date.toISOString();\n \n case \"iso-date\":\n // Date only: 2025-01-01\n return date.toISOString().split(\"T\")[0];\n \n case \"iso-time\":\n // Time only: 01:01:01Z\n return date.toISOString().split(\"T\")[1];\n \n case \"human\":\n // Human-readable: Jan 1, 2025 01:01:01 UTC\n if (timezone === \"user\") {\n return date.toLocaleString();\n }\n return date.toUTCString();\n \n default:\n return date.toISOString();\n }\n}\n\n/**\n * Parse a date value and return ISO8601 string in UTC\n * This is the canonical format for internal storage\n * \n * @param value - Date object or string\n * @returns ISO8601 string in UTC timezone\n */\nexport function toISOString(value) {\n return formatDate(value, { timezone: \"UTC\", format: \"iso\" });\n}\n\n/**\n * Get current timestamp as ISO8601 string in UTC\n * @returns Current time as ISO8601 string\n */\nexport function nowISO() {\n return new Date().toISOString();\n}\n\n/**\n * Parse ISO8601 string to Date object\n * @param isoString - ISO8601 formatted string\n * @returns Date object\n */\nexport function fromISOString(isoString) {\n return new Date(isoString);\n}\n\n/**\n * Calculate difference between two dates in various units\n * @param start - Start date (ISO string or Date)\n * @param end - End date (ISO string or Date)\n * @returns Object with duration in multiple units\n */\nexport function dateDiff(\n start,\n end\n)\n\n\n\n\n\n{\n const startDate = start instanceof Date ? start : new Date(start);\n const endDate = end instanceof Date ? end : new Date(end);\n \n const diffMs = endDate.getTime() - startDate.getTime();\n \n return {\n milliseconds: diffMs,\n seconds: Math.floor(diffMs / 1000),\n minutes: Math.floor(diffMs / (1000 * 60)),\n hours: Math.floor(diffMs / (1000 * 60 * 60)),\n days: Math.floor(diffMs / (1000 * 60 * 60 * 24))\n };\n}\n\n/**\n * Check if a folder name looks like an ISO8601 timestamp\n * Used to identify version folders vs regular folders\n * @param folderName - Folder name to check\n * @returns true if the folder name is a valid ISO8601 timestamp\n */\nexport function isTimestampFolder(folderName) {\n // Match ISO8601 format: YYYY-MM-DDTHH:mm:ssZ\n const isoRegex = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(Z|\\.\\d{3}Z)$/;\n \n if (!isoRegex.test(folderName)) {\n return false;\n }\n \n const date = new Date(folderName);\n return !isNaN(date.getTime()) && date.getTime() > 0;\n}\n\n/**\n * Generate a new version name (ISO8601 timestamp)\n * If existingVersions provided, ensures the new version is later than all existing ones\n * @param existingVersions - Array of existing version timestamps\n * @returns New version timestamp that is unique and later than existing ones\n */\nexport function generateVersionName(existingVersions = []) {\n if (existingVersions.length === 0) {\n // No existing versions, use current timestamp\n const now = new Date();\n return now.toISOString().split(\".\")[0] + \"Z\";\n }\n \n // Find the maximum timestamp among all existing versions\n const maxTimestamp = existingVersions.reduce((max, version) => {\n const versionDate = new Date(version);\n const maxDate = new Date(max);\n return versionDate > maxDate ? version : max;\n });\n \n // Increment by 1 second to ensure uniqueness\n const maxDate = new Date(maxTimestamp);\n const nextDate = new Date(maxDate.getTime() + 1000);\n \n return nextDate.toISOString().split(\".\")[0] + \"Z\";\n}\n\n","/**\n * File System Utilities\n * \n * Helper functions for file system operations like path management,\n * directory creation, and file type detection\n */\n\nimport fs from \"fs\";\nimport path from \"path\";\nimport { fileURLToPath } from \"url\";\n\n/**\n * Ensure a directory path exists, creating it if necessary\n * Returns the absolute path\n */\nexport async function ensurePath(...pathParts) {\n const fullPath = path.resolve(...pathParts);\n \n if (!fs.existsSync(fullPath)) {\n await fs.promises.mkdir(fullPath, { recursive: true });\n }\n \n return fullPath;\n}\n\n/**\n * Synchronous version of ensurePath\n */\nexport function ensurePathSync(...pathParts) {\n const fullPath = path.resolve(...pathParts);\n \n if (!fs.existsSync(fullPath)) {\n fs.mkdirSync(fullPath, { recursive: true });\n }\n \n return fullPath;\n}\n\n/**\n * Get file extension for a given data type\n */\nexport function getFileExtension(dataType) {\n switch (dataType) {\n case \"json-array\":\n case \"json-object\":\n return \"json\";\n case \"text\":\n return \"txt\";\n case \"xml\":\n return \"xml\";\n default:\n return \"json\";\n }\n}\n\n/**\n * Get __dirname equivalent for ES modules\n * \n * Returns the directory path of the module file\n * Useful for resolving relative paths in ES modules where __dirname is not available\n * \n * @param metaUrl - The import.meta.url from the calling module (must be passed from calling module)\n * @returns The directory path of the module\n * \n * @example\n * ```typescript\n * import { getDirname } from \"@nmakarov/cli-toolkit/utils\";\n * const __dirname = getDirname(import.meta.url);\n * ```\n */\nexport function getDirname(metaUrl) {\n return path.dirname(fileURLToPath(metaUrl));\n}\n\n","/**\n * Operating System Utilities\n * \n * Helper functions for interacting with the operating system\n * to get system statistics and information\n */\n\nimport fs from \"fs\";\nimport path from \"path\";\nimport { execSync } from \"child_process\";\n\n/**\n * Get free disk space for a given path (in bytes)\n * Returns null if unable to determine\n * Uses `df` command on Unix-like systems\n */\nexport function getFreeDiskSpace(targetPath) {\n try {\n // If the target path doesn't exist, use its parent directory\n let pathToCheck = targetPath;\n if (!fs.existsSync(targetPath)) {\n const parentDir = path.dirname(targetPath);\n if (fs.existsSync(parentDir)) {\n pathToCheck = parentDir;\n } else {\n // If parent doesn't exist either, use the root directory\n pathToCheck = process.platform === \"win32\" ? \"C:\\\\\" : \"/\";\n }\n }\n\n if (process.platform === \"win32\") {\n // Windows: use wmic command\n // Note: This is a simplified implementation, may need adjustments\n return null; // TODO: Implement robust Windows support\n } else {\n // Unix-like systems: use df command\n const stdout = execSync(`df -k \"${pathToCheck}\"`, { encoding: \"utf8\" });\n const lines = stdout.trim().split(\"\\n\");\n const parts = lines[1].split(/\\s+/); // second line, split on whitespace\n const freeKb = parseInt(parts[3], 10); // 4th column is \"Available\"\n return freeKb * 1024; // Convert to bytes\n }\n } catch (error) {\n // Silently fail and return null\n return null;\n }\n}\n\n","/**\n * Formatting Utilities\n * \n * Helper functions for converting between different data formats\n * and human-readable representations\n */\n\n/**\n * Convert bytes to human-readable format (e.g., \"1.5 MB\")\n */\nexport function bytesToHumanReadable(bytes) {\n if (bytes === 0) return \"0 B\";\n \n const k = 1024;\n const sizes = [\"B\", \"KB\", \"MB\", \"GB\", \"TB\", \"PB\"];\n const i = Math.floor(Math.log(bytes) / Math.log(k));\n \n return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + \" \" + sizes[i];\n}\n\n/**\n * Convert human-readable format to bytes (e.g., \"1.5 MB\" -> 1572864)\n */\nexport function humanReadableToBytes(humanString) {\n const units = {\n \"B\": 1,\n \"KB\": 1024,\n \"MB\": 1024 * 1024,\n \"GB\": 1024 * 1024 * 1024,\n \"TB\": 1024 * 1024 * 1024 * 1024,\n \"PB\": 1024 * 1024 * 1024 * 1024 * 1024,\n };\n \n const match = humanString.trim().match(/^([\\d.]+)\\s*([A-Z]+)$/i);\n if (!match) {\n throw new Error(`Invalid format: ${humanString}. Expected format like \"2MB\" or \"1.5 GB\"`);\n }\n \n const value = parseFloat(match[1]);\n const unit = match[2].toUpperCase();\n \n if (!units[unit]) {\n throw new Error(`Unknown unit: ${unit}. Supported units: ${Object.keys(units).join(\", \")}`);\n }\n \n return Math.round(value * units[unit]);\n}\n\n","/**\n * Core lightweight utilities\n */\n\nexport function sleepMs(ms) {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\nexport function toJsonColumn(value) {\n if (value === undefined || value === null) return null;\n return JSON.stringify(value);\n}\n","import os from \"node:os\";\nimport { toJsonColumn } from \"../utils/index.js\";\nimport { queueToTableNames } from \"./taskUtils.js\";\n\n/**\n * Fail-fast accessor for `context.db`. The services-registry never lazy-inits the DB;\n * if it's missing, that's a caller wiring mistake.\n *\n * @param {object} context\n * @returns {Function}\n */\nfunction getDb(context) {\n const db = context.db;\n if (!db) {\n throw new Error(\"Services registry requires context.db\");\n }\n return db;\n}\n\n/**\n * Decode a JSON column value into a plain object. Returns `{}` for nulls,\n * arrays, parse errors — callers never get `null`/`undefined` back so they can\n * safely spread the result.\n *\n * @param {unknown} value\n * @returns {Record<string, unknown>}\n */\nfunction parseMetadataColumn(value) {\n if (!value) return {};\n if (typeof value === \"object\" && !Array.isArray(value)) return value;\n if (typeof value === \"string\") {\n try {\n const p = JSON.parse(value);\n return p && typeof p === \"object\" && !Array.isArray(value) ? p : {};\n } catch {\n return {};\n }\n }\n return {};\n}\n\n/** Default max concurrent *alive* services per group (0 = unlimited). Override with runnerGroupMaxInstances. */\nconst DEFAULT_GROUP_MAX_INSTANCES = {\n intake: 1,\n harvest: 1,\n harvester: 0,\n loader: 0,\n photos: 0,\n photosprocessor: 0,\n ingest: 0,\n};\n\n/**\n * Produce a DB-safe, human-readable name part: letters / digits / `._-` only,\n * trimmed and capped at 80 chars. Empty inputs fall back to `\"runner\"`.\n *\n * @param {unknown} raw\n * @returns {string}\n */\nfunction sanitizeNamePart(raw) {\n const s = String(raw || \"\")\n .trim()\n .replace(/[^a-zA-Z0-9._-]+/g, \"-\")\n .replace(/^-+|-+$/g, \"\");\n return s.slice(0, 80) || \"runner\";\n}\n\n/**\n * How many alive instances are allowed in this group? Explicit `override` wins;\n * otherwise use the per-group default (lowercased group name). `0` = unlimited.\n *\n * @param {string} serviceGroup\n * @param {number|undefined} override\n * @returns {number}\n */\nfunction resolveMaxInstances(serviceGroup, override) {\n if (override !== undefined && Number.isFinite(override)) {\n return Math.max(0, Math.floor(Number(override)));\n }\n const g = serviceGroup.trim().toLowerCase();\n return DEFAULT_GROUP_MAX_INSTANCES[g] ?? 0;\n}\n\n/**\n * Count rows in a group that have heartbeat'd within `staleMs`. Used to gate\n * `groupMaxInstances` at registration time.\n *\n * @param {Function} db knex instance.\n * @param {string} registryTable\n * @param {string} queueName\n * @param {string} serviceGroup\n * @param {number} staleMs\n * @param {string|undefined} excludeRowId When retrying, skip the row we're about to reuse.\n * @returns {Promise<number>}\n */\nasync function countAliveInGroup(\n db,\n registryTable,\n queueName,\n serviceGroup,\n staleMs,\n excludeRowId\n) {\n const cutoff = new Date(Date.now() - staleMs);\n let q = db(registryTable)\n .where({ queue_name: queueName, service_group: serviceGroup })\n .where(\"last_seen_at\", \">\", cutoff);\n if (excludeRowId) {\n q = q.whereNot(\"id\", excludeRowId);\n }\n const row = await q.count(\"id as count\").first();\n return Number(row?.count ?? 0);\n}\n\n/**\n * Instance numbers currently held by *alive* rows (fresh last_seen).\n *\n * @param {Function} db\n * @param {string} registryTable\n * @param {string} queueName\n * @param {string} serviceGroup\n * @param {number} staleMs\n * @returns {Promise<Set<number>>}\n */\nasync function getOccupiedInstanceSlots(\n db,\n registryTable,\n queueName,\n serviceGroup,\n staleMs\n) {\n const cutoff = new Date(Date.now() - staleMs);\n const rows = await db(registryTable)\n .where({ queue_name: queueName, service_group: serviceGroup })\n .where(\"last_seen_at\", \">\", cutoff)\n .select(\"instance_number\");\n const set = new Set();\n for (const r of rows) {\n const n = Number(r.instance_number);\n if (Number.isFinite(n) && n >= 1) set.add(Math.floor(n));\n }\n return set;\n}\n\n/**\n * Best-effort detection of Postgres unique-constraint violations. Covers both\n * pg's `23505` SQLSTATE and drivers that only surface the error message.\n *\n * @param {unknown} error\n * @returns {boolean}\n */\nfunction isUniqueViolation(error) {\n const code = error?.code ?? error?.errno;\n return code === \"23505\" || String(error?.message || \"\").includes(\"duplicate key\");\n}\n\n/**\n * Package the `metadata` JSON column payload for a new/updated registry row.\n * Always carries `runnerTarget` when provided so read-side filters can see it\n * without decoding the whole blob.\n *\n * @param {{ metadata?: Record<string, unknown>, target?: string }} options\n * @returns {string|null}\n */\nfunction buildMetadata(options) {\n const base = options.metadata && typeof options.metadata === \"object\" ? { ...options.metadata } : {};\n if (options.target) {\n base.runnerTarget = options.target;\n }\n return toJsonColumn(Object.keys(base).length ? base : null);\n}\n\n/**\n * Pick first free instance number: smallest n >= 1 with n ∉ occupied.\n * If explicit is set, use it only if not in occupied and within maxSlots (when > 0).\n *\n * @param {Set<number>} occupied\n * @param {number|undefined|null} explicit\n * @param {number|undefined} maxSlots\n * @returns {number}\n */\nfunction allocateInstanceNumber(occupied, explicit, maxSlots) {\n if (explicit !== undefined && explicit !== null && Number.isFinite(Number(explicit))) {\n const e = Math.max(1, Math.floor(Number(explicit)));\n if (occupied.has(e)) {\n throw new Error(`[services-registry] instance slot ${e} is already occupied by an alive peer`);\n }\n if (maxSlots !== undefined && maxSlots > 0 && e > maxSlots) {\n throw new Error(`[services-registry] instance ${e} exceeds configured max slots ${maxSlots} for this group`);\n }\n return e;\n }\n const cap = maxSlots !== undefined && maxSlots > 0 ? maxSlots : 10_000;\n for (let n = 1; n <= cap; n++) {\n if (!occupied.has(n)) return n;\n }\n throw new Error(`[services-registry] no free instance slot (searched 1..${cap})`);\n}\n\n/**\n * Build a conventional `service_name` when the caller didn't pick one:\n * `<group>-<host>-<instance>`.\n *\n * @param {string} groupBase\n * @param {string} hostBase\n * @param {number} instanceNumber\n * @returns {string}\n */\nfunction defaultServiceName(groupBase, hostBase, instanceNumber) {\n return `${groupBase}-${hostBase}-${instanceNumber}`;\n}\n\n/**\n * Register this process in `{queue}_services_registry` (no local identity files).\n * Allocates the first free instance number among *alive* peers, then inserts or takes over a stale row\n * with the same `service_name` when restarting on the same host/name pattern.\n *\n * @param {object} context\n * @param {{\n * queueName: string,\n * target?: string,\n * serviceGroup: string,\n * serviceName?: string,\n * instanceNumber?: number|null,\n * staleMs: number,\n * groupMaxInstances?: number,\n * enforceMaxInstances?: boolean,\n * metadata?: Record<string, unknown>,\n * }} options\n * @returns {Promise<{\n * serviceName: string,\n * serviceGroup: string,\n * queueName: string,\n * target?: string,\n * rowId: string,\n * registryTable: string,\n * instanceNumber: number,\n * }>}\n */\nexport async function registerInServicesRegistry(context, options) {\n const db = getDb(context);\n const registryTable = queueToTableNames(options.queueName).registryTable;\n const serviceGroup = options.serviceGroup.trim();\n if (!serviceGroup) {\n throw new Error(\"registerInServicesRegistry: serviceGroup is required\");\n }\n\n const serverName = os.hostname();\n const pid = typeof process.pid === \"number\" ? process.pid : null;\n const meta = buildMetadata(options);\n const groupBase = sanitizeNamePart(serviceGroup);\n const hostBase = sanitizeNamePart(serverName);\n\n const maxAllowed = resolveMaxInstances(serviceGroup, options.groupMaxInstances);\n const aliveCount = await countAliveInGroup(db, registryTable, options.queueName, serviceGroup, options.staleMs, undefined);\n\n if (maxAllowed > 0 && aliveCount >= maxAllowed) {\n const msg = `[services-registry] group limit reached for \"${serviceGroup}\": ${aliveCount} alive (max ${maxAllowed}, queue=${options.queueName}).`;\n if (options.enforceMaxInstances) {\n throw new Error(msg);\n }\n context.logger.warn?.(`${msg} Starting anyway (runnerEnforceMaxInstances=false).`);\n }\n\n const maxSlots = maxAllowed > 0 ? maxAllowed : undefined;\n const cutoff = new Date(Date.now() - options.staleMs);\n\n const MAX_ATTEMPTS = 8;\n for (let attempt = 0; attempt < MAX_ATTEMPTS; attempt++) {\n const occupied = await getOccupiedInstanceSlots(db, registryTable, options.queueName, serviceGroup, options.staleMs);\n const instanceNumber = allocateInstanceNumber(occupied, options.instanceNumber, maxSlots);\n\n const serviceNameRaw = options.serviceName?.trim()\n ? sanitizeNamePart(options.serviceName.trim())\n : defaultServiceName(groupBase, hostBase, instanceNumber);\n\n const existing = await db(registryTable)\n .where({ queue_name: options.queueName, service_name: serviceNameRaw })\n .first();\n\n if (existing) {\n const lastSeen = new Date(existing.last_seen_at);\n const isAlive = !Number.isNaN(lastSeen.getTime()) && lastSeen > cutoff;\n\n if (isAlive) {\n if (options.serviceName?.trim()) {\n throw new Error(\n `[services-registry] service_name \"${serviceNameRaw}\" is already registered by an alive peer`\n );\n }\n context.logger.warn?.(\n `[services-registry] service_name \"${serviceNameRaw}\" already alive; retrying allocation (attempt ${attempt + 1})`\n );\n if (options.instanceNumber !== undefined && options.instanceNumber !== null) {\n throw new Error(\n `[services-registry] instance slot ${instanceNumber} / name \"${serviceNameRaw}\" is already held by an alive peer`\n );\n }\n await new Promise((r) => setTimeout(r, 50 + attempt * 30));\n continue;\n }\n\n await db(registryTable)\n .where({ id: existing.id })\n .update({\n server_name: serverName,\n pid,\n metadata: meta,\n service_group: serviceGroup,\n instance_number: instanceNumber,\n last_seen_at: db.fn.now(),\n });\n\n const reg = {\n serviceName: serviceNameRaw,\n serviceGroup,\n queueName: options.queueName,\n target: options.target,\n rowId: String(existing.id),\n registryTable,\n instanceNumber,\n };\n context.servicesRegistry = reg;\n context.runnerHeartbeat = reg;\n\n context.logger.info?.(\n `[services-registry] took over stale row name=${serviceNameRaw} instance=${instanceNumber} group=${serviceGroup} queue=${options.queueName}`\n );\n return reg;\n }\n\n try {\n const rows = await db(registryTable)\n .insert({\n queue_name: options.queueName,\n service_group: serviceGroup,\n instance_number: instanceNumber,\n service_name: serviceNameRaw,\n server_name: serverName,\n pid,\n metadata: meta,\n last_seen_at: db.fn.now(),\n created_at: db.fn.now(),\n })\n .returning([\"id\", \"service_name\"]);\n\n const row = Array.isArray(rows) ? rows[0] : rows;\n let rowId = row && typeof row === \"object\" ? String(row.id ?? \"\") : \"\";\n if (!rowId) {\n const again = await db(registryTable)\n .where({ queue_name: options.queueName, service_name: serviceNameRaw })\n .first();\n rowId = again?.id != null ? String(again.id) : \"\";\n }\n if (!rowId) continue;\n\n const regNew = {\n serviceName: String(row?.service_name ?? serviceNameRaw),\n serviceGroup,\n queueName: options.queueName,\n target: options.target,\n rowId,\n registryTable,\n instanceNumber,\n };\n context.servicesRegistry = regNew;\n context.runnerHeartbeat = regNew;\n\n context.logger.info?.(\n `[services-registry] registered name=${regNew.serviceName} instance=${instanceNumber} group=${serviceGroup} queue=${options.queueName}`\n );\n return regNew;\n } catch (error) {\n if (!isUniqueViolation(error)) {\n throw error;\n }\n context.logger.warn?.(`[services-registry] insert race on \"${serviceNameRaw}\", retrying (attempt ${attempt + 1})`);\n }\n }\n\n throw new Error(\n `[services-registry] could not allocate a registry row for group=${serviceGroup} queue=${options.queueName} after ${MAX_ATTEMPTS} attempts`\n );\n}\n\n/**\n * Heartbeat: bump `last_seen_at`, refresh `server_name` / `pid` in case the\n * hostname rotates or the process PID changes (container restart in place).\n *\n * @param {object} context\n * @param {{ registryTable: string, rowId: string }} registration\n * @returns {Promise<void>}\n */\nexport async function touchServicesRegistry(context, registration) {\n const db = getDb(context);\n const serverName = os.hostname();\n const pid = typeof process.pid === \"number\" ? process.pid : null;\n await db(registration.registryTable)\n .where({ id: registration.rowId })\n .update({\n last_seen_at: db.fn.now(),\n server_name: serverName,\n pid,\n });\n}\n\n/**\n * Merge metadata (e.g. new allowedTasks / role) for this service row. Bumps last_seen_at.\n * Use when a service changes what it handles without restarting the process.\n *\n * @param {object} context\n * @param {{ registryTable: string, rowId: string, serviceName: string }} registration\n * @param {Record<string, unknown>} patch\n * @returns {Promise<void>}\n */\nexport async function updateServicesRegistryMetadata(context, registration, patch) {\n const db = getDb(context);\n const row = await db(registration.registryTable).where({ id: registration.rowId }).first();\n const prev = parseMetadataColumn(row?.metadata);\n const merged = { ...prev, ...patch };\n await db(registration.registryTable)\n .where({ id: registration.rowId })\n .update({\n metadata: toJsonColumn(merged),\n last_seen_at: db.fn.now(),\n });\n context.logger.info?.(`[services-registry] metadata updated for ${registration.serviceName}`);\n}\n\n/**\n * Drop this process's registry row. Call on graceful shutdown so peers don't\n * have to wait for `staleMs` to reclaim the slot.\n *\n * @param {object} context\n * @param {{ registryTable: string, rowId: string, serviceName: string }} registration\n * @returns {Promise<void>}\n */\nexport async function unregisterServicesRegistry(context, registration) {\n const db = getDb(context);\n await db(registration.registryTable).where({ id: registration.rowId }).delete();\n context.logger.info?.(`[services-registry] unregistered name=${registration.serviceName} id=${registration.rowId}`);\n}\n\n/**\n * List registry rows (optionally filter by queue / group). Rows with last_seen older than staleMs are excluded.\n *\n * @param {object} context\n * @param {{ queueName: string, staleMs?: number, serviceGroup?: string }} [options]\n * @returns {Promise<object[]>}\n */\nexport async function listServicesRegistry(context, options = { queueName: \"tasks\" }) {\n const db = getDb(context);\n const staleMs = options.staleMs ?? 60_000;\n const cutoff = new Date(Date.now() - staleMs);\n const table = queueToTableNames(options.queueName).registryTable;\n let q = db(table).where(\"last_seen_at\", \">\", cutoff).orderBy([{ column: \"service_group\", order: \"asc\" }, { column: \"service_name\", order: \"asc\" }]);\n if (options.serviceGroup?.trim()) {\n q = q.where({ service_group: options.serviceGroup.trim() });\n }\n return await q;\n}\n","import { randomUUID } from \"node:crypto\";\nimport { toJsonColumn } from \"../utils/index.js\";\nimport { nextTimeMatch } from \"./time-matcher.js\";\n\n//KEEP THIS FOR REFERENCE !!!\n// this is a sample of how to add a column to a table if it does not exist\n// const tasksHasOpid = await db.schema.hasColumn(tasksTable, \"opid\");\n// if (!tasksHasOpid) {\n// await db.schema.alterTable(tasksTable, (t) => {\n// t.text(\"opid\");\n// });\n// }\n\n\n/**\n * Fail-fast accessor for the knex instance on `context.db`. The tasks component\n * assumes the DB is already initialized by the caller — we don't lazy-init here,\n * so a missing `db` is a programming error, not a runtime condition.\n *\n * @param {object} context\n * @returns {Function} knex instance\n */\nfunction getDb(context) {\n const db = context.db;\n if (!db) {\n throw new Error(\"Tasks component requires context.db. Initialize DB first and attach to context.\");\n }\n return db;\n}\n\n/**\n * Given a queue name, derive the three related table names the runtime uses:\n *\n * - `tasksTable` — active queue (rows that may still run)\n * - `historyTable` — append-only audit of completed attempts\n * - `registryTable` — live service/runner heartbeats for the queue\n *\n * @param {string} queueName\n * @returns {{ tasksTable: string, historyTable: string, registryTable: string }}\n */\nexport function queueToTableNames(queueName) {\n return {\n tasksTable: queueName,\n historyTable: `${queueName}_history`,\n registryTable: `${queueName}_services_registry`,\n };\n}\n\n/**\n * Column definition shared by the active queue and its history mirror. Kept in\n * one place so the two tables stay structurally compatible (history receives a\n * full row snapshot).\n *\n * @param {import(\"knex\").Knex.CreateTableBuilder} t\n * @param {import(\"knex\").Knex} db\n * @param {string} tableNameForIndex Used to name indexes uniquely per table.\n */\nfunction defineTasksTable(t, db, tableNameForIndex) {\n t.uuid(\"id\").primary().defaultTo(db.raw(\"uuid_generate_v4()\"));\n t.timestamp(\"created_at\").notNullable().defaultTo(db.fn.now());\n t.timestamp(\"started_at\");\n t.timestamp(\"completed_at\");\n /*\n * Priority: lower number = claimed first (see claimNextRunnableTask: ORDER BY priority ASC).\n * Suggested range 0–100: 0 = most urgent, 100 = least; default 50 for normal work.\n */\n t.integer(\"priority\").notNullable().defaultTo(50);\n\n t.text(\"schedule\");\n t.timestamp(\"next_run_at\").defaultTo(null);\n t.timestamp(\"past_due\").defaultTo(null);\n\n t.text(\"name\").notNullable();\n t.text(\"opid\");\n t.jsonb(\"params\");\n\n // those are tagret identifiers, kind of who is going to run a task.\n t.text(\"service_group\"); // harvester, loader, photos, ...\n t.integer(\"instance_number\");\n t.text(\"service_name\"); // that's a \"<server_name>_<service_group>_<instance_number>\"\n t.text(\"server_name\"); // filled by runner when registering, auto.\n\n t.text(\"status\").notNullable().defaultTo(\"idle\"); // idle, running, completed, failed, paused\n t.timestamp(\"status_changed_at\").defaultTo(null);\n\n t.text(\"progress\");\n t.boolean(\"success\");\n t.jsonb(\"results\");\n\n t.index([\"service_group\", \"status\", \"priority\", \"created_at\"], `${tableNameForIndex}_claim_idx`);\n t.index([\"service_group\", \"name\"], `${tableNameForIndex}_group_name_idx`);\n}\n\n/**\n * Build an insert payload for `*_history`: never copies the queue row `id` as the history PK — PostgreSQL default\n * generates a new `id`. The snapshot still carries `name`, `opid`, `params`, etc. for auditing and ad hoc queries.\n *\n * @param {object} row Original queue row.\n * @param {object} overrides Fields to override on the snapshot (e.g. `completed_at`, `success`).\n * @returns {object}\n */\nexport function taskHistoryInsertFromQueueRow(row, overrides) {\n const { id, ...snapshot } = row;\n void id;\n return {\n ...snapshot,\n ...overrides,\n };\n}\n\n/**\n * Idempotently create the three tables backing a queue (tasks / history / registry).\n * Pass `recreate: true` to drop-and-recreate, useful in dev/test.\n *\n * Pass `dryRun: true` to only report the DDL it *would* run (drops/creates) and\n * make no changes — so a `--dryRun` script never mutates the schema.\n *\n * Requires the `uuid-ossp` extension; creates it on first run if missing.\n *\n * @param {object} context\n * @param {{ queueName?: string, recreate?: boolean, dryRun?: boolean }} [options]\n * @returns {Promise<void>}\n */\nexport async function ensureTaskTables(context, options = {}) {\n const queueName = options.queueName ?? \"tasks\";\n const recreate = options.recreate ?? false;\n const dryRun = options.dryRun ?? false;\n const db = getDb(context);\n const log = context.logger ?? console;\n const { tasksTable, historyTable, registryTable } = queueToTableNames(queueName);\n\n const needsTasks = recreate ? true : !(await db.tableExists(tasksTable));\n const needsHistory = recreate ? true : !(await db.tableExists(historyTable));\n const needsRegistry = recreate ? true : !(await db.tableExists(registryTable));\n\n if (dryRun) {\n const plan = [];\n if (recreate) {\n plan.push(`DROP TABLE IF EXISTS ${historyTable}, ${tasksTable}, ${registryTable}`);\n }\n if (needsTasks) plan.push(`CREATE TABLE ${tasksTable} (tasks queue)`);\n if (needsHistory) plan.push(`CREATE TABLE ${historyTable} (history mirror)`);\n if (needsRegistry) plan.push(`CREATE TABLE ${registryTable} (services registry)`);\n if (plan.length === 0) {\n log.info?.(`[tasks-schema] dryRun — queue \"${queueName}\" already up to date; no DDL`);\n } else {\n log.info?.(`[tasks-schema] dryRun — would run ${plan.length} statement(s) for queue \"${queueName}\":`);\n for (const s of plan) log.info?.(` - ${s}`);\n }\n return;\n }\n\n if (recreate) {\n await db.schema.dropTableIfExists(historyTable);\n await db.schema.dropTableIfExists(tasksTable);\n await db.schema.dropTableIfExists(registryTable);\n }\n\n if (needsTasks) {\n await db.raw(`CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"`);\n await db.schema.createTable(tasksTable, (t) => {\n defineTasksTable(t, db, tasksTable);\n });\n }\n\n if (needsHistory) {\n await db.schema.createTable(historyTable, (t) => {\n defineTasksTable(t, db, historyTable);\n });\n }\n\n if (needsRegistry) {\n await db.schema.createTable(registryTable, (t) => {\n t.uuid(\"id\").primary().defaultTo(db.raw(\"uuid_generate_v4()\"));\n\n t.text(\"queue_name\").notNullable();\n t.text(\"service_group\").notNullable(); // harvester, loader, photos, ...\n t.integer(\"instance_number\").notNullable().defaultTo(1);\n t.text(\"service_name\").notNullable(); // that's a \"<server_name>_<service_group>_<instance_number>\"\n t.text(\"server_name\").notNullable(); // filled by runner when registering, auto.\n t.integer(\"pid\");\n\n t.json(\"metadata\");\n\n t.timestamp(\"created_at\").notNullable().defaultTo(db.fn.now());\n t.timestamp(\"last_seen_at\").notNullable().defaultTo(db.fn.now());\n t.unique([\"queue_name\", \"service_name\"], `${registryTable}_queue_name_service_name_uniq`);\n t.index([\"queue_name\", \"service_group\", \"last_seen_at\"], `${registryTable}_queue_group_seen_idx`);\n t.index([\"queue_name\", \"last_seen_at\"], `${registryTable}_queue_seen_idx`);\n });\n\n // TODO: a reference is needed - task_history entry should reference the registry entry, so that we can easily find all executed tasks for a given service and calculate the average workload or identify if there's a bottleneck. Also may be used for the load balancing.\n }\n}\n\n/**\n * Insert one task row into the queue. Supports targeting (service_group + optional\n * instance/server), recurring schedules (cron-like 6-field string, see `time-matcher.js`),\n * and explicit `nextRunAt` overrides.\n *\n * @param {object} context\n * @param {{\n * queueName?: string,\n * name?: string,\n * task?: string,\n * params?: unknown,\n * opid?: string|null,\n * priority?: number,\n * schedule?: string|null,\n * nextRunAt?: Date|string|number|null,\n * serviceGroup?: string|null,\n * instanceNumber?: number|null,\n * serviceName?: string|null,\n * serverName?: string|null,\n * }} options\n * @returns {Promise<string>} The new task's UUID.\n */\nexport async function enqueueTask(context, options) {\n const db = getDb(context);\n const queueName = options.queueName ?? \"tasks\";\n const { tasksTable } = queueToTableNames(queueName);\n const id = randomUUID();\n\n const name = options.name ?? options.task;\n if (!name) {\n throw new Error(\"enqueueTask: name (or task) is required\");\n }\n\n const schedule = options.schedule?.trim() ? options.schedule : null;\n let nextRunAt = null;\n if (options.nextRunAt !== undefined) {\n nextRunAt = options.nextRunAt == null ? null : new Date(options.nextRunAt);\n } else if (schedule) {\n nextRunAt = nextTimeMatch(schedule, new Date());\n }\n\n await db(tasksTable).insert({\n id,\n name,\n params: toJsonColumn(options.params ?? null),\n opid: options.opid ?? null,\n priority: options.priority ?? 50,\n schedule,\n next_run_at: nextRunAt,\n service_group: options.serviceGroup ?? null,\n instance_number: options.instanceNumber ?? null,\n service_name: options.serviceName ?? null,\n server_name: options.serverName ?? null,\n status: \"idle\",\n status_changed_at: db.fn.now(),\n });\n return id;\n}\n\n/**\n * Update the `progress` column for one task. Strings go in verbatim; anything\n * else gets JSON-stringified so the column stays text-friendly.\n *\n * @param {object} context\n * @param {string} tasksTable\n * @param {string} taskId\n * @param {unknown} progress\n * @returns {Promise<void>}\n */\nexport async function updateTaskProgress(context, tasksTable, taskId, progress) {\n const db = getDb(context);\n await db(tasksTable).where({ id: taskId }).update({\n progress: typeof progress === \"string\" ? progress : JSON.stringify(progress),\n });\n}\n","/**\n * 6-field cron-like matcher: `sec min hour day month weekday`.\n * Fields support `*`, numeric literals, ranges (`1-5`), comma lists (`0,15,30,45`),\n * and step expressions (`*\\/10`, `0-30/5`). No named months/weekdays, no\n * `L`/`#`/`?` magic — keep patterns explicit.\n */\n\n/** Field index → `\"lo-hi\"` range that `*` expands into. Order: sec, min, hour, day, month, weekday. */\nconst RANGES = [\"0-59\", \"0-59\", \"0-23\", \"1-31\", \"1-12\", \"0-6\"];\n\n/**\n * Replace `*` with a concrete `lo-hi` range so the rest of the pipeline only\n * has to deal with numeric forms.\n *\n * @param {string} field\n * @param {string} range e.g. `\"0-59\"`\n * @returns {string}\n */\nexport function resolveAsterisks(field, range) {\n return field.includes(\"*\") ? field.replace(\"*\", range) : field;\n}\n\n/**\n * Expand every `lo-hi` run in `field` to the comma-separated list of integers it\n * covers. Handles reversed bounds (`5-2` → `2,3,4,5`). Does not handle steps;\n * run {@link resolveSteps} after this.\n *\n * @param {string} field\n * @returns {string}\n */\nexport function resolveRanges(field) {\n const regex = /(\\d+)-(\\d+)/;\n let current = field;\n while (true) {\n const match = regex.exec(current);\n if (!match) break;\n const raw = match[0];\n let first = Number(match[1]);\n let last = Number(match[2]);\n if (last < first) {\n [first, last] = [last, first];\n }\n const values = [];\n for (let i = first; i <= last; i += 1) {\n values.push(i);\n }\n current = current.replace(raw, values.join(\",\"));\n }\n return current;\n}\n\n/**\n * Apply a `.../step` suffix, keeping only values divisible by `step`.\n * Expects ranges to already be expanded to comma-lists. No-op when the suffix is missing.\n *\n * @param {string} field e.g. `\"0,1,2,...,59/10\"` → `\"0,10,20,30,40,50\"`\n * @returns {string}\n */\nexport function resolveSteps(field) {\n const match = /^(.+)\\/(\\d+)$/.exec(field);\n if (!match) return field;\n const base = match[1];\n const step = Number(match[2]);\n if (!Number.isFinite(step) || step <= 0) return field;\n return base\n .split(\",\")\n .map((v) => Number(v))\n .filter((v) => Number.isFinite(v) && v % step === 0)\n .join(\",\");\n}\n\n/**\n * Normalize a raw 6-field schedule string into an array of comma-separated integer\n * lists — one per field, ready for {@link matchesParsedPattern}.\n *\n * @param {string} pattern\n * @returns {string[]}\n * @throws If `pattern` does not contain exactly six whitespace-separated fields.\n */\nexport function convertPattern(pattern) {\n const parts = pattern.trim().split(/\\s+/);\n if (parts.length !== 6) {\n throw new Error(`Invalid schedule \"${pattern}\". Expected 6 fields: sec min hour day month weekday`);\n }\n return parts\n .map((field, idx) => resolveAsterisks(field, RANGES[idx]))\n .map((field) => resolveRanges(field))\n .map((field) => resolveSteps(field));\n}\n\n/**\n * Does `value` appear in the comma-list `field`?\n *\n * @param {string} field\n * @param {number} value\n * @returns {boolean}\n */\nfunction fieldMatches(field, value) {\n const allowed = field.split(\",\").map((v) => Number(v));\n return allowed.includes(value);\n}\n\n/**\n * Check an already-parsed pattern against a `Date`. All six fields must match\n * (month is 1-indexed here; weekday follows `Date#getDay`, 0 = Sunday).\n *\n * @param {string[]} parsed Output of {@link convertPattern}.\n * @param {Date} date\n * @returns {boolean}\n */\nexport function matchesParsedPattern(parsed, date) {\n return (\n fieldMatches(parsed[0], date.getSeconds()) &&\n fieldMatches(parsed[1], date.getMinutes()) &&\n fieldMatches(parsed[2], date.getHours()) &&\n fieldMatches(parsed[3], date.getDate()) &&\n fieldMatches(parsed[4], date.getMonth() + 1) &&\n fieldMatches(parsed[5], date.getDay())\n );\n}\n\n/**\n * One-shot check: does `pattern` match `date`? Parses the pattern each call —\n * fine for the runner loop where we only test one date per tick; use\n * {@link convertPattern} + {@link matchesParsedPattern} if you need to test\n * many dates against the same schedule.\n *\n * @param {string} pattern\n * @param {Date} [date]\n * @returns {boolean}\n */\nexport function timeMatcher(pattern, date = new Date()) {\n const parsed = convertPattern(pattern);\n return matchesParsedPattern(parsed, date);\n}\n\nconst MS_PER_SECOND = 1000;\nconst DEFAULT_SEARCH_HORIZON_MS = 10 * 365 * 24 * 60 * 60 * MS_PER_SECOND;\n\n/**\n * Earliest calendar second strictly after `from` where the schedule matches.\n * Use this to sleep until the next run instead of polling `timeMatcher` and risking missed seconds.\n *\n * @param {string} pattern Same 6-field format as `timeMatcher`: `sec min hour day month weekday`\n * @param {Date} [from=new Date()] Reference instant. Matching is second-granularity; search starts at the next second after `from`.\n * @param {number} [maxSearchMs] Abort if no match within this window (default ~10 years).\n * @returns {Date}\n */\nexport function nextTimeMatch(\n pattern,\n from = new Date(),\n maxSearchMs = DEFAULT_SEARCH_HORIZON_MS\n) {\n const parsed = convertPattern(pattern);\n let t = Math.ceil((from.getTime() + 1) / MS_PER_SECOND) * MS_PER_SECOND;\n const end = t + maxSearchMs;\n while (t <= end) {\n const date = new Date(t);\n if (matchesParsedPattern(parsed, date)) {\n return date;\n }\n t += MS_PER_SECOND;\n }\n throw new Error(\n `nextTimeMatch: no match for \"${pattern}\" within ${maxSearchMs}ms after ${from.toISOString()}`\n );\n}\n","import path from \"node:path\";\nimport { FileDatabase } from \"../filedatabase/index.js\";\n\n/**\n * Gets or lazily builds the default IPC-logs FileDatabase state, cached on\n * `context.__tasksLogsState`. Reads `tasksLogs*` params (basePath, namespace,\n * table, errorTable, enabled, maxVersions, pageSize). When `tasksLogsEnabled=false`\n * the main `db` is null but `errorDb` still captures error payloads.\n *\n * @param {object} context\n * @returns {object} `{ db, errorDb, queue, initialized, errorInitialized }`\n */\nfunction getLogsState(context) {\n const holder = context;\n if (holder.__tasksLogsState) return holder.__tasksLogsState;\n\n const basePath = holder.params?.get?.(\"tasksLogsBasePath\") || \"./data\";\n const namespace = holder.params?.get?.(\"tasksLogsNamespace\") || \"tasks-logs\";\n const tableName = holder.params?.get?.(\"tasksLogsTable\") || \"runner\";\n const errorTableName = holder.params?.get?.(\"tasksErrorLogsTable\") || `${tableName}-errors`;\n const maxVersionsRaw = Number(holder.params?.get?.(\"tasksLogsMaxVersions\"));\n const pageSizeRaw = Number(holder.params?.get?.(\"tasksLogsPageSize\"));\n\n const errorDb = new FileDatabase({\n basePath,\n namespace,\n tableName: errorTableName,\n versioned: true,\n useMetadata: true,\n maxVersions: Number.isFinite(maxVersionsRaw) && maxVersionsRaw > 0 ? maxVersionsRaw : 20,\n pageSize: Number.isFinite(pageSizeRaw) && pageSizeRaw > 0 ? pageSizeRaw : 2000,\n logger: holder.logger,\n });\n\n const enabledRaw = holder.params?.get?.(\"tasksLogsEnabled\");\n const enabled = enabledRaw === undefined ? true : !!enabledRaw;\n if (!enabled) {\n const disabledState = {\n db: null,\n errorDb,\n queue: Promise.resolve(),\n initialized: true,\n errorInitialized: false,\n };\n holder.__tasksLogsState = disabledState;\n return disabledState;\n }\n\n const db = new FileDatabase({\n basePath,\n namespace,\n tableName,\n versioned: true,\n useMetadata: true,\n maxVersions: Number.isFinite(maxVersionsRaw) && maxVersionsRaw > 0 ? maxVersionsRaw : 20,\n pageSize: Number.isFinite(pageSizeRaw) && pageSizeRaw > 0 ? pageSizeRaw : 2000,\n logger: holder.logger,\n });\n const state = {\n db,\n errorDb,\n queue: Promise.resolve(),\n initialized: false,\n errorInitialized: false,\n };\n holder.__tasksLogsState = state;\n return state;\n}\n\n/**\n * Cache key for a target so multiple writers sharing the same\n * `basePath`/`namespace`/`tableName` reuse one FileDatabase state\n * (see `getLogsStateForTarget`).\n *\n * @param {object} target `{ basePath?, namespace?, tableName }`\n * @returns {string}\n */\nfunction ipcLogTargetKey(target) {\n const bp = target.basePath ?? \"\";\n const ns = target.namespace ?? \"\";\n return `${bp}::${ns}::${target.tableName}`;\n}\n\n/**\n * Produce a FileDatabase `tableName` of the form `source/resource`, sanitizing each\n * segment so only `[a-zA-Z0-9._-]` survive. Empty segments fall back to `\"x\"`.\n *\n * @param {string} source\n * @param {string} resource\n * @returns {string} e.g. `\"actris/properties\"`\n */\nexport function ipcFileLogsTableNameForSourceResource(source, resource) {\n const seg = (s) => {\n const t = String(s).trim().replace(/[^a-zA-Z0-9._-]+/g, \"_\").replace(/^_+|_+$/g, \"\");\n return t.length ? t : \"x\";\n };\n return `${seg(source)}/${seg(resource)}`;\n}\n\n/**\n * Read IPC log records from the latest FileDatabase version for `source/resource`.\n * Intended for tailing / incremental polling — use the returned `latestTs` as the\n * next call's `afterTs`.\n *\n * @param {object} context\n * @param {object} options\n * @param {string} options.source\n * @param {string} options.resource\n * @param {number} [options.tail=100] Max records returned after filtering (clamped 1..10000).\n * @param {string|null} [options.afterTs] ISO timestamp watermark; keeps rows with `ts > afterTs`.\n * @returns {Promise<{ records: object[], latestTs: string|null }>}\n */\nexport async function readTaskIpcLogsSnapshot(context, options) {\n const holder = context;\n const basePath = holder.params?.get?.(\"tasksLogsBasePath\") ?? \"./data\";\n const namespace = holder.params?.get?.(\"tasksLogsNamespace\") ?? \"tasks-logs\";\n const tableName = ipcFileLogsTableNameForSourceResource(options.source, options.resource);\n const tail = Math.max(1, Math.min(10_000, Number(options.tail) > 0 ? Number(options.tail) : 100));\n\n const fd = new FileDatabase({\n basePath,\n namespace,\n tableName,\n versioned: true,\n useMetadata: true,\n maxVersions: 30,\n pageSize: 2000,\n logger: holder.logger,\n });\n\n const versions = await fd.getVersions();\n if (versions.length === 0) {\n return { records: [], latestTs: null };\n }\n const latest = versions[versions.length - 1];\n const raw = await fd.read({ version: latest });\n const arr = Array.isArray(raw) ? raw : [];\n\n let filtered = arr;\n if (options.afterTs && String(options.afterTs).trim()) {\n const cut = String(options.afterTs).trim();\n filtered = arr.filter((r) => r && typeof r.ts === \"string\" && String(r.ts) > cut);\n }\n\n /** Watermark for incremental fetches: max `ts` among all matching rows, not only the returned tail. */\n let latestTs = null;\n for (const r of filtered) {\n const ts = typeof r?.ts === \"string\" ? String(r.ts) : null;\n if (ts && (!latestTs || ts > latestTs)) latestTs = ts;\n }\n\n const incremental = !!(options.afterTs && String(options.afterTs).trim());\n /** Incremental polls may return many lines between ticks — cap at 10k so we do not drop rows then advance `latestTs` past them. */\n const maxReturn = incremental ? 10_000 : tail;\n const sliced = filtered.length > maxReturn ? filtered.slice(-maxReturn) : filtered;\n\n return { records: sliced, latestTs };\n}\n\n/**\n * Absolute path to the FileDatabase table directory for a given target (matches the\n * FileDatabase on-disk layout). Versioned writes create timestamp subfolders inside.\n *\n * @param {object} context\n * @param {object} target `{ basePath?, namespace?, tableName }`\n * @returns {string}\n */\nexport function resolveIpcFileLogsDir(context, target) {\n const holder = context;\n const basePath = target.basePath ?? (holder.params?.get?.(\"tasksLogsBasePath\") || \"./data\");\n const namespace = target.namespace ?? (holder.params?.get?.(\"tasksLogsNamespace\") || \"tasks-logs\");\n const segments = target.tableName.split(\"/\").filter(Boolean);\n return path.resolve(basePath, namespace, ...segments);\n}\n\n/**\n * Lazily build a FileDatabase state for the given target, memoized on\n * `context.__tasksLogsTargetStates` (Map keyed by `ipcLogTargetKey`).\n * Returns `null` when `tasksLogsEnabled=false`.\n *\n * @param {object} context\n * @param {object} target `{ basePath?, namespace?, tableName }`\n * @returns {object|null} same shape as `getLogsState`, but `errorDb` is always null.\n */\nfunction getLogsStateForTarget(context, target) {\n const holder = context;\n const enabledRaw = holder.params?.get?.(\"tasksLogsEnabled\");\n const enabled = enabledRaw === undefined ? true : !!enabledRaw;\n if (!enabled) return null;\n\n if (!holder.__tasksLogsTargetStates) holder.__tasksLogsTargetStates = new Map();\n const map = holder.__tasksLogsTargetStates;\n const key = ipcLogTargetKey(target);\n if (map.has(key)) return map.get(key);\n\n const basePath = target.basePath ?? (holder.params?.get?.(\"tasksLogsBasePath\") || \"./data\");\n const namespace = target.namespace ?? (holder.params?.get?.(\"tasksLogsNamespace\") || \"tasks-logs\");\n const maxVersionsRaw = Number(holder.params?.get?.(\"tasksLogsMaxVersions\"));\n const pageSizeRaw = Number(holder.params?.get?.(\"tasksLogsPageSize\"));\n\n const db = new FileDatabase({\n basePath,\n namespace,\n tableName: target.tableName,\n versioned: true,\n useMetadata: true,\n maxVersions: Number.isFinite(maxVersionsRaw) && maxVersionsRaw > 0 ? maxVersionsRaw : 20,\n pageSize: Number.isFinite(pageSizeRaw) && pageSizeRaw > 0 ? pageSizeRaw : 2000,\n logger: holder.logger,\n });\n const state = {\n db,\n errorDb: null,\n queue: Promise.resolve(),\n initialized: false,\n errorInitialized: false,\n };\n map.set(key, state);\n return state;\n}\n\n/**\n * Heuristic: does this IPC payload represent an error?\n * - object with `level` of `\"error\"`/`\"fatal\"`, OR\n * - object with a `message` string containing the word \"error\", OR\n * - plain string containing \"error\" (case-insensitive).\n *\n * @param {unknown} payload\n * @returns {boolean}\n */\nfunction isErrorPayload(payload) {\n if (!payload) return false;\n if (typeof payload === \"object\") {\n const level = typeof payload.level === \"string\" ? payload.level.toLowerCase() : \"\";\n if (level === \"error\" || level === \"fatal\") return true;\n if (typeof payload.message === \"string\" && /\\berror\\b/i.test(payload.message)) return true;\n return false;\n }\n if (typeof payload === \"string\") {\n return /\\berror\\b/i.test(payload);\n }\n return false;\n}\n\n/**\n * Build the persisted log record for an IPC payload. Copies `source`/`resource` from\n * `task.params` when present so logs stay queryable by them later.\n *\n * @param {object} task\n * @param {unknown} payload\n * @returns {object} `{ ts, opid, taskId, taskName, target, source, resource, payload }`\n */\nfunction buildLogRecord(task, payload) {\n const params = task.params && typeof task.params === \"object\" ? task.params : {};\n return {\n ts: new Date().toISOString(),\n opid: task.opid ?? null,\n taskId: task.id,\n taskName: task.name,\n target: task.service_group,\n source: typeof params.source === \"string\" ? params.source : null,\n resource: typeof params.resource === \"string\" ? params.resource : null,\n payload,\n };\n}\n\n/**\n * Append one IPC log line from a child worker. Never throws; writes are serialized\n * per-state on a promise queue (drain with `flushTaskIpcLogs`).\n *\n * - Without `target`: writes to the default store (`tasksLogsTable`, usually `runner`)\n * and, when the payload looks like an error, also to `tasksErrorLogsTable`.\n * - With `target`: writes only to that target's store (e.g. per source/resource,\n * later read by `readTaskIpcLogsSnapshot`).\n *\n * @param {object} context\n * @param {object} task\n * @param {unknown} payload\n * @param {object} [target] `{ basePath?, namespace?, tableName }`\n * @returns {void}\n */\nexport function appendTaskIpcLog(context, task, payload, target) {\n if (target) {\n const state = getLogsStateForTarget(context, target);\n if (!state?.db) return;\n const record = buildLogRecord(task, payload);\n state.queue = state.queue\n .then(async () => {\n await state.db.write([record], { forceNewVersion: !state.initialized });\n state.initialized = true;\n })\n .catch((error) => {\n context.logger.warn?.(\"[tasks] failed to persist IPC log entry (targeted):\", error);\n });\n return;\n }\n\n const state = getLogsState(context);\n if (!state.db && !state.errorDb) return;\n\n const record = buildLogRecord(task, payload);\n state.queue = state.queue\n .then(async () => {\n if (state.db) {\n await state.db.write([record], { forceNewVersion: !state.initialized });\n state.initialized = true;\n }\n if (state.errorDb && isErrorPayload(payload)) {\n await state.errorDb.write([record], { forceNewVersion: !state.errorInitialized });\n state.errorInitialized = true;\n }\n })\n .catch((error) => {\n context.logger.warn?.(\"[tasks] failed to persist IPC log entry:\", error);\n });\n}\n\n/**\n * Await pending FileDatabase writes from task IPC logging (default store + every\n * per-target store). Does not close anything; subsequent appends continue to work.\n *\n * @param {object} context\n * @returns {Promise<void>}\n */\nexport async function flushTaskIpcLogs(context) {\n const holder = context;\n const promises = [];\n if (holder.__tasksLogsState?.queue) promises.push(holder.__tasksLogsState.queue);\n const map = holder.__tasksLogsTargetStates;\n if (map) {\n for (const s of map.values()) {\n if (s.queue) promises.push(s.queue);\n }\n }\n await Promise.all(promises);\n}\n","/**\n * FileDatabase - Versioned, file-based data storage system\n * \n * Provides organized file storage with:\n * - Timestamp-based versioning\n * - Chunked/paginated file writes for large datasets\n * - Metadata tracking\n * - Backward compatibility with legacy structures\n * - Multiple storage modes (versioned, catalog, logs)\n */\n\nimport fs from \"fs\";\nimport path from \"path\";\n\n\n\n\n\n\n\n\n\n\n\n\n\nimport { ensurePath, getFileExtension, getFreeDiskSpace, bytesToHumanReadable, isTimestampFolder } from \"./utils.js\";\nimport { detectDataType, serializeData, deserializeData } from \"./serializers.js\";\nimport { ParamError, FileDatabaseError } from \"../errors.js\";\n\nexport { FileDatabaseError };\n\nexport class FileDatabase {\n basePath;\n namespace;\n tableName = null;\n versioned;\n maxVersions;\n pageSize;\n useMetadata;\n freeSpaceThreshold;\n logger;\n\n // Current operation state\n currentVersion = null;\n currentVersionFolder = null;\n currentFileNumber = 0;\n currentRecord = 0;\n hasReadFirstPage = false;\n lastFileData = null;\n metadata;\n\n // Synopsis calculation functions\n fileSynopsisFunction = null;\n versionSynopsisFunction = null;\n\n /**\n * Constructor - accepts context as first parameter (new pattern)\n * or config object (legacy pattern for backward compatibility)\n */\n constructor(contextOrConfig, options) {\n let config;\n \n // Check if first parameter is Context (has params property)\n if (contextOrConfig && typeof contextOrConfig === \"object\" && \"params\" in contextOrConfig) {\n // New pattern: context is first parameter\n const context = contextOrConfig ;\n const opts = options || {};\n \n // Get configuration from context.params (module \"filedatabase\" for --showUsedParams grouping)\n const defs = {\n basePath: \"string default ./data\",\n namespace: \"string default default\",\n tableName: \"string\",\n maxVersions: \"number default 5\",\n pageSize: \"number default 5000\",\n };\n const discovered = context.params.getAllForModule(defs);\n config = { ...discovered, ...opts, logger: context.logger } ;\n } else {\n // Legacy pattern: config object\n config = contextOrConfig ;\n }\n \n // Validate required configuration\n if (!config.basePath) {\n throw new ParamError(\"[FileDatabase] basePath is required\");\n }\n\n this.basePath = config.basePath;\n this.namespace = config.namespace || \"default\";\n this.tableName = config.tableName || null;\n this.versioned = config.versioned ?? true; // Default true for backward compatibility\n this.maxVersions = config.maxVersions || 5;\n this.pageSize = config.pageSize || 5000;\n this.useMetadata = config.useMetadata !== false; // Default true\n this.freeSpaceThreshold = config.freeSpaceThreshold || 100 * 1024 * 1024; // 100MB\n this.logger = config.logger || console;\n\n // Initialize metadata\n this.metadata = this.getDefaultMetadata();\n }\n\n /**\n * Initialize FileDatabase from context and options.\n * Params are read via getAllForModule(\"filedatabase\", defs) for --showUsedParams grouping.\n */\n static init(context, options) {\n return new FileDatabase(context, options ?? {});\n }\n\n /**\n * Get default metadata structure\n */\n getDefaultMetadata() {\n return {\n version: this.currentVersion || null,\n files: [],\n createdAt: new Date().toISOString(),\n modifiedAt: new Date().toISOString(),\n totalRecords: 0,\n synopsis: null,\n dataType: null,\n };\n }\n\n /**\n * Get the destination path (basePath/namespace/tableName[/version])\n */\n getDestinationPath(version) {\n const errors = [\"basePath\", \"namespace\", \"tableName\"]\n .filter(prop => !this[prop ])\n .map(prop => `${prop} is not set`);\n if (errors.length) {\n throw new FileDatabaseError(`[FileDatabase] ${errors.join(\"; \")}`);\n }\n\n const parts = [this.basePath, this.namespace];\n if (this.tableName) {\n parts.push(...this.tableName.split(\"/\"));\n }\n\n // Only add version folder if in versioned mode and version is specified\n if (this.versioned && version) {\n parts.push(version);\n }\n\n return path.resolve(...parts);\n }\n\n /**\n * Set current version and version folder\n */\n async setCurrentVersion(version) {\n this.currentVersion = version;\n this.currentVersionFolder = await ensurePath(this.getDestinationPath(), version);\n }\n\n /**\n * Create a new version folder with comprehensive timestamp logic\n * Only works in versioned mode\n */\n async makeNewVersion() {\n if (!this.versioned) {\n throw new FileDatabaseError(\"makeNewVersion() only works in versioned mode\");\n }\n\n // Reset in-memory metadata when creating a new version\n this.metadata = this.getDefaultMetadata();\n\n const existingVersions = await this.getVersions();\n let versionName;\n\n if (existingVersions.length > 0) {\n // Find the maximum timestamp among all existing versions\n const maxTimestamp = existingVersions.reduce((max, version) => {\n const versionDate = new Date(version.replace(\"Z\", \"\"));\n const maxDate = new Date(max.replace(\"Z\", \"\"));\n return versionDate > maxDate ? version : max;\n });\n\n // Increment the maximum timestamp by 1 second\n const maxDate = new Date(maxTimestamp.replace(\"Z\", \"\"));\n const nextDate = new Date(maxDate.getTime() + 1000);\n versionName = nextDate.toISOString().split(\".\")[0] + \"Z\";\n } else {\n // No existing versions, use current timestamp\n const now = new Date();\n versionName = now.toISOString().split(\".\")[0] + \"Z\";\n }\n\n await this.setCurrentVersion(versionName);\n\n // Reset file numbering for new version\n this.currentFileNumber = 0;\n\n // Delete old versions if we exceed maxVersions\n const versions = await this.getVersions();\n while (versions.length > this.maxVersions) {\n const versionToDelete = path.resolve(this.getDestinationPath(), versions.shift());\n this.logger.silly?.(`[FileDatabase] Deleting old version: ${versionToDelete}`);\n await fs.promises.rm(versionToDelete, { recursive: true, force: true });\n }\n\n return versionName;\n }\n\n /**\n * Get list of all versions (sorted chronologically)\n * Only works in versioned mode\n */\n async getVersions() {\n if (!this.versioned) {\n return []; // No versions in non-versioned mode\n }\n\n const destPath = this.getDestinationPath();\n\n try {\n await ensurePath(destPath);\n const items = await fs.promises.readdir(destPath);\n const versions = items.filter(item => {\n const itemPath = path.join(destPath, item);\n const stat = fs.statSync(itemPath);\n return stat.isDirectory() && isTimestampFolder(item);\n });\n\n return versions.sort();\n } catch (error) {\n return [];\n }\n }\n\n /**\n * Get the latest version (most recent timestamp)\n * Only works in versioned mode\n * @returns Latest version string or null if no versions\n */\n async getLatestVersion() {\n if (!this.versioned) {\n throw new FileDatabaseError(\"getLatestVersion() only works in versioned mode\");\n }\n\n const versions = await this.getVersions();\n if (versions.length === 0) {\n return null;\n }\n\n return versions[versions.length - 1];\n }\n\n /**\n * Check if any data exists in this table\n * Works for both versioned and non-versioned modes\n * @returns true if data exists\n */\n async hasData() {\n const tablePath = this.getDestinationPath();\n\n if (!fs.existsSync(tablePath)) {\n return false;\n }\n\n if (this.versioned) {\n // Check for version folders\n const versions = await this.getVersions();\n return versions.length > 0;\n } else {\n // Check for any data files or metadata\n const items = await fs.promises.readdir(tablePath);\n return items.some(item =>\n item === \"metadata.json\" ||\n item.match(/^\\d{6}\\.(json|txt|xml)$/) ||\n item.endsWith(\".json\")\n );\n }\n }\n\n /**\n * Auto-detect the data format in this table\n * Used when reading existing data\n * @returns Format detection result\n */\n async detectDataFormat()\n\n\n\n {\n const tablePath = this.getDestinationPath();\n\n if (!fs.existsSync(tablePath)) {\n return { versioned: false, hasMetadata: false, dataType: null };\n }\n\n const items = await fs.promises.readdir(tablePath);\n\n // Check for metadata.json in root (non-versioned with metadata)\n if (items.includes(\"metadata.json\")) {\n const metadata = JSON.parse(\n await fs.promises.readFile(path.join(tablePath, \"metadata.json\"), \"utf8\")\n );\n return {\n versioned: false,\n hasMetadata: true,\n dataType: metadata.dataType || null\n };\n }\n\n // Check for version folders\n const versionFolders = items.filter(item => {\n const itemPath = path.join(tablePath, item);\n const stat = fs.statSync(itemPath);\n return stat.isDirectory() && isTimestampFolder(item);\n });\n\n if (versionFolders.length > 0) {\n // Check if latest version has metadata\n const latestVersion = versionFolders.sort().pop();\n const versionMetadataPath = path.join(tablePath, latestVersion, \"metadata.json\");\n\n return {\n versioned: true,\n hasMetadata: fs.existsSync(versionMetadataPath),\n dataType: null\n };\n }\n\n // Check for sequential files (legacy non-versioned)\n const dataFiles = items.filter(f => f.match(/^\\d{6}\\.(json|txt|xml)$/));\n if (dataFiles.length > 0) {\n return {\n versioned: false,\n hasMetadata: false,\n dataType: null\n };\n }\n\n return { versioned: false, hasMetadata: false, dataType: null };\n }\n\n /**\n * Load metadata from JSON file\n */\n async loadMetadataJson(version) {\n const metadataFile = path.join(this.getDestinationPath(), version, \"metadata.json\");\n if (fs.existsSync(metadataFile)) {\n try {\n const rawData = await fs.promises.readFile(metadataFile, \"utf8\");\n return JSON.parse(rawData);\n } catch (e) {\n throw new FileDatabaseError(`Failed to read metadata for version \"${version}\": ${(e ).message}`);\n }\n }\n return null;\n }\n\n /**\n * Build metadata by scanning files in a version folder (backward compatibility)\n * Reads all files to get accurate counts - used when synopsis calculation is needed\n */\n async figureMetadataFromVersionFiles(version) {\n const versionPath = path.join(this.getDestinationPath(), version);\n\n if (!fs.existsSync(versionPath)) {\n return this.getDefaultMetadata();\n }\n\n const files = (await fs.promises.readdir(versionPath))\n .filter(file => file !== \"metadata.json\" && !file.startsWith(\".\"))\n .sort();\n\n const metadata = this.getDefaultMetadata();\n metadata.version = version;\n metadata.files = [];\n\n let totalRecords = 0;\n let detectedDataType = null;\n\n for (let i = 0; i < files.length; i++) {\n const fileName = files[i];\n const filePath = path.join(versionPath, fileName);\n\n try {\n const rawData = await fs.promises.readFile(filePath, \"utf8\");\n const extension = path.extname(fileName).toLowerCase();\n let dataType = \"text\";\n\n if (extension === \".json\") {\n dataType = \"json-array\";\n } else if (extension === \".xml\") {\n dataType = \"xml\";\n }\n\n const fileData = deserializeData(rawData, dataType);\n const recordsCount = Array.isArray(fileData) ? fileData.length : 1;\n\n if (detectedDataType === null) {\n detectedDataType = detectDataType(fileData);\n }\n\n const fileInfo = {\n number: i + 1,\n recordsCount,\n fileName,\n };\n\n metadata.files.push(fileInfo);\n totalRecords += recordsCount;\n } catch (error) {\n this.logger.error?.(`[FileDatabase] Failed to read file ${fileName}: ${(error ).message}`);\n }\n }\n\n metadata.totalRecords = totalRecords;\n metadata.dataType = detectedDataType;\n\n return metadata;\n }\n\n /**\n * Build metadata optimized - only reads first and last files\n * Assumes all middle files have the same record count as the first file\n * Much faster for large datasets with many files\n */\n async buildMetadataOptimized(version) {\n const versionPath = path.join(this.getDestinationPath(), version);\n\n if (!fs.existsSync(versionPath)) {\n return this.getDefaultMetadata();\n }\n\n const files = (await fs.promises.readdir(versionPath))\n .filter(file => file !== \"metadata.json\" && !file.startsWith(\".\"))\n .sort();\n\n if (files.length === 0) {\n return this.getDefaultMetadata();\n }\n\n const metadata = this.getDefaultMetadata();\n metadata.version = version;\n metadata.files = files.map((fileName, index) => ({\n number: index + 1,\n recordsCount: 0,\n fileName,\n }));\n\n // Read first file to determine data type and standard record count\n const firstFile = metadata.files[0];\n const firstFilePath = path.join(versionPath, firstFile.fileName);\n const firstFileRaw = await fs.promises.readFile(firstFilePath, \"utf8\");\n\n let firstFileData;\n try {\n firstFileData = JSON.parse(firstFileRaw);\n } catch (e) {\n firstFileData = firstFileRaw;\n }\n\n metadata.dataType = detectDataType(firstFileData);\n\n // Only proceed with optimization for json-array data\n if (metadata.dataType === \"json-array\") {\n const firstFileCount = Array.isArray(firstFileData) ? firstFileData.length : 1;\n firstFile.recordsCount = firstFileCount;\n\n // Assume all middle files have the same count as the first\n for (let i = 1; i < metadata.files.length - 1; i++) {\n metadata.files[i].recordsCount = firstFileCount;\n }\n\n // Read last file to get its actual count (might be partial)\n if (files.length > 1) {\n const lastFile = metadata.files[metadata.files.length - 1];\n const lastFilePath = path.join(versionPath, lastFile.fileName);\n const lastFileRaw = await fs.promises.readFile(lastFilePath, \"utf8\");\n const lastFileData = deserializeData(lastFileRaw, metadata.dataType);\n lastFile.recordsCount = Array.isArray(lastFileData) ? lastFileData.length : 1;\n }\n\n // Calculate total records\n metadata.totalRecords = metadata.files.reduce((sum, file) => sum + file.recordsCount, 0);\n } else {\n // For non-array data, count each file as 1 record\n metadata.files.forEach(file => {\n file.recordsCount = 1;\n });\n metadata.totalRecords = files.length;\n }\n\n return metadata;\n }\n\n /**\n * Figure out metadata - tries JSON first, then builds from files\n * Uses optimized building when no synopsis calculation is needed\n */\n async figureMetadata(version, useOptimized = true) {\n if (this.useMetadata) {\n const metadata = await this.loadMetadataJson(version);\n if (metadata) {\n return metadata;\n }\n }\n \n // Fallback: build from files\n // Use optimized version (only reads first+last) when no synopsis needed\n if (useOptimized && !this.fileSynopsisFunction && !this.versionSynopsisFunction) {\n return await this.buildMetadataOptimized(version);\n }\n \n // Use full version (reads all files) when synopsis calculation needed\n return await this.figureMetadataFromVersionFiles(version);\n }\n\n /**\n * Load version metadata (main entry point for loading)\n */\n async loadVersionMetadata(version) {\n const metadata = await this.figureMetadata(version);\n this.metadata = metadata;\n return metadata;\n }\n\n /**\n * Save version metadata to file\n */\n async saveVersionMetadata(metadata) {\n if (!this.useMetadata) {\n return;\n }\n\n const metadataToSave = metadata || this.metadata;\n let metadataFile;\n\n if (this.versioned) {\n // Versioned mode: metadata in version folder\n if (!this.currentVersion) {\n return;\n }\n metadataFile = path.join(this.getDestinationPath(), this.currentVersion, \"metadata.json\");\n } else {\n // Non-versioned mode: metadata in root table folder\n metadataFile = path.join(this.getDestinationPath(), \"metadata.json\");\n }\n\n await fs.promises.writeFile(metadataFile, JSON.stringify(metadataToSave, null, 4), \"utf8\");\n }\n\n /**\n * Create a new file entry in metadata\n */\n makeNewFile() {\n this.currentFileNumber = (this.currentFileNumber || 0) + 1;\n\n const dataType = this.metadata.dataType || \"json-array\";\n const fileEntry = {\n number: this.currentFileNumber,\n recordsCount: 0,\n fileName: `${this.currentFileNumber.toString().padStart(6, \"0\")}.${getFileExtension(dataType)}`,\n };\n\n this.metadata.files.push(fileEntry);\n this.lastFileData = null;\n\n this.logger.silly?.(`[FileDatabase] Created new file: ${fileEntry.fileName}, fileNumber: ${this.currentFileNumber}`);\n }\n\n /**\n * Figure out what data to write and which file to use (for pagination)\n * @param data - Data to write\n * @param targetFileIndex - Optional index of existing file to overwrite (when customMetadata matches)\n * @param forceNewFile - If true, always create a new file (when customMetadata provided but no match)\n */\n figureOutDataAndFileToWrite(data, targetFileIndex = null, forceNewFile = false) {\n let dataToWrite;\n let dataLeftOver;\n\n // Detect data type from incoming data\n // Always use the incoming data's type to ensure correct file extension\n const incomingDataType = detectDataType(data);\n if (this.metadata.dataType !== incomingDataType) {\n this.metadata.dataType = incomingDataType;\n }\n\n // If targetFileIndex is provided, use that file (overwrite existing file with matching customMetadata)\n if (targetFileIndex !== null && targetFileIndex < this.metadata.files.length) {\n const targetFile = this.metadata.files[targetFileIndex];\n // For non-array data, overwrite the file\n if (!Array.isArray(data)) {\n dataToWrite = data;\n dataLeftOver = null;\n return { dataToWrite, dataLeftOver, fileName: targetFile.fileName };\n } else {\n // For arrays, start fresh in the target file (don't append)\n dataToWrite = data.slice(0, this.pageSize);\n dataLeftOver = data.slice(this.pageSize);\n this.lastFileData = dataToWrite;\n return { dataToWrite, dataLeftOver, fileName: targetFile.fileName };\n }\n }\n\n // If forceNewFile is true (customMetadata provided but no match), create a new file\n // Skip the initial file creation if forceNewFile is true to avoid creating an extra empty file\n let newlyCreatedFileIndex = null;\n if (forceNewFile) {\n const filesBeforeCreate = this.metadata.files.length;\n this.makeNewFile();\n newlyCreatedFileIndex = filesBeforeCreate; // Index of the newly created file\n this.logger.silly?.(`[FileDatabase] Creating new file for unique custom metadata combination, fileNumber: ${this.currentFileNumber}`);\n } else if (this.metadata.files.length === 0) {\n // If no files exist yet and we're not forcing a new file, create the first file\n this.makeNewFile();\n }\n\n // Get the last file (which might be the one we just created)\n const lastFile = this.metadata.files[this.metadata.files.length - 1];\n const lastFileRecordsCount = lastFile.recordsCount;\n \n // Verify that if we created a new file, we're using it\n if (forceNewFile && newlyCreatedFileIndex !== null) {\n const newlyCreatedFile = this.metadata.files[newlyCreatedFileIndex];\n if (newlyCreatedFile && newlyCreatedFile.fileName !== lastFile.fileName) {\n this.logger.warn?.(`[FileDatabase] Warning: Newly created file ${newlyCreatedFile.fileName} doesn't match last file ${lastFile.fileName}`);\n }\n }\n \n // For non-array data (text/xml/object), check if we need a new file with correct extension\n // But skip this check if forceNewFile is true - we already created the file we need\n if (!Array.isArray(data) && !forceNewFile) {\n const lastFileExtension = path.extname(lastFile.fileName);\n const expectedExtension = `.${getFileExtension(incomingDataType)}`;\n // If the last file has wrong extension, create a new file with correct extension\n // Check even if recordsCount is 0 (empty file) - we want correct extension for new writes\n if (lastFileExtension !== expectedExtension) {\n // Only create new file if the existing one has content, otherwise we'll use it\n if (lastFileRecordsCount > 0) {\n this.makeNewFile();\n } else {\n // File is empty, update its name to have correct extension\n lastFile.fileName = `${this.currentFileNumber.toString().padStart(6, \"0\")}.${getFileExtension(incomingDataType)}`;\n }\n }\n } else if (!Array.isArray(data) && forceNewFile) {\n // If forceNewFile is true, ensure the newly created file has the correct extension\n const lastFileExtension = path.extname(lastFile.fileName);\n const expectedExtension = `.${getFileExtension(incomingDataType)}`;\n if (lastFileExtension !== expectedExtension) {\n lastFile.fileName = `${this.currentFileNumber.toString().padStart(6, \"0\")}.${getFileExtension(incomingDataType)}`;\n }\n }\n\n if (Array.isArray(data)) {\n // For arrays, handle pagination\n // If forceNewFile is true, write fresh data to the new file (don't append)\n if (forceNewFile) {\n // Write fresh data to the newly created file\n dataToWrite = data.slice(0, this.pageSize);\n dataLeftOver = data.slice(this.pageSize);\n this.lastFileData = dataToWrite;\n } else if (lastFileRecordsCount < this.pageSize) {\n // Try to append to existing file if there's space\n dataToWrite = [...(this.lastFileData || []), ...data.slice(0, this.pageSize - lastFileRecordsCount)];\n dataLeftOver = data.slice(this.pageSize - lastFileRecordsCount);\n this.lastFileData = dataToWrite;\n } else {\n // Last file is full, create a new file\n this.makeNewFile();\n dataToWrite = data.slice(0, this.pageSize);\n dataLeftOver = data.slice(this.pageSize);\n this.lastFileData = dataToWrite;\n }\n } else {\n // For non-arrays, write as-is\n dataToWrite = data;\n dataLeftOver = null;\n }\n\n // Get the file name - if forceNewFile is true, we just created a new file, so use that one\n // Otherwise, use the last file (which might have been created earlier or is being reused)\n const fileName = this.metadata.files[this.metadata.files.length - 1].fileName;\n\n this.logger.silly?.(\n `[FileDatabase] figureOutDataAndFileToWrite: filename=${fileName}, forceNewFile=${forceNewFile}, targetFileIndex=${targetFileIndex}, dataToWrite.length=${Array.isArray(dataToWrite) ? dataToWrite.length : \"N/A\"}, lastFileRecordsCount=${lastFileRecordsCount}`\n );\n\n return { dataToWrite, dataLeftOver, fileName };\n }\n\n /**\n * Calculate file-level synopsis if function is set\n */\n calculateFileSynopsis(data, fileIndex = this.metadata.files.length - 1) {\n if (!this.fileSynopsisFunction) {\n return;\n }\n const fileInfo = this.metadata.files[fileIndex];\n const enhancedFileInfo = this.fileSynopsisFunction(fileInfo, data);\n this.metadata.files[fileIndex] = enhancedFileInfo;\n }\n\n /**\n * Calculate version-level synopsis if function is set\n */\n calculateVersionSynopsis() {\n if (!this.versionSynopsisFunction) {\n return;\n }\n const enhancedMetadata = this.versionSynopsisFunction(this.metadata);\n this.metadata = enhancedMetadata;\n }\n\n /**\n * Update metadata after writing data\n */\n updateMetadata(dataToWrite, fileName, customMetadata) {\n let currentFile;\n\n if (fileName) {\n // Find the specific file by filename\n const foundFile = this.metadata.files.find(file => file.fileName === fileName);\n if (!foundFile) {\n this.logger.warn?.(`[FileDatabase] File ${fileName} not found in metadata, using last file`);\n currentFile = this.metadata.files[this.metadata.files.length - 1];\n } else {\n currentFile = foundFile;\n }\n } else {\n // Get the current file info from metadata (always the last file)\n currentFile = this.metadata.files[this.metadata.files.length - 1];\n }\n\n // Calculate the actual records count for the data being written\n const recordsCount = Array.isArray(dataToWrite) ? dataToWrite.length : 1;\n\n // Update existing file entry with the correct records count\n currentFile.recordsCount = recordsCount;\n\n // Add custom metadata fields if provided\n if (customMetadata) {\n Object.assign(currentFile, customMetadata);\n }\n\n // Find the file index for synopsis calculation\n const fileIndex = this.metadata.files.indexOf(currentFile);\n if (fileIndex !== -1) {\n this.calculateFileSynopsis(dataToWrite, fileIndex);\n }\n\n // Update version metadata\n this.metadata.version = this.currentVersion;\n this.metadata.modifiedAt = new Date().toISOString();\n this.metadata.dataType = detectDataType(dataToWrite);\n\n // Recalculate total records by summing all file records counts\n this.metadata.totalRecords = this.metadata.files.reduce((sum, file) => sum + (file.recordsCount || 0), 0);\n\n this.logger.silly?.(\n `[FileDatabase] Updated metadata for file ${currentFile.fileName}: recordsCount=${recordsCount}, totalRecords=${this.metadata.totalRecords}`\n );\n }\n\n /**\n * Safe write with disk space check\n */\n async safeWrite(filePath, data) {\n const serializedData = serializeData(data);\n const dir = path.dirname(filePath);\n const requiredBytes = Buffer.byteLength(serializedData, \"utf8\");\n\n // Check disk space\n const freeBytes = getFreeDiskSpace(dir);\n if (freeBytes !== null) {\n if (freeBytes < requiredBytes) {\n throw new FileDatabaseError(\n `Not enough disk space. Required: ${bytesToHumanReadable(requiredBytes)}, Free: ${bytesToHumanReadable(freeBytes)}`\n );\n }\n\n if (freeBytes < this.freeSpaceThreshold) {\n this.logger.warn?.(`Low disk space warning: only ${bytesToHumanReadable(freeBytes)} left`);\n }\n }\n\n try {\n await fs.promises.writeFile(filePath, serializedData, \"utf8\");\n this.logger.silly?.(`[FileDatabase] Wrote ${bytesToHumanReadable(requiredBytes)} to ${filePath}`);\n } catch (error) {\n throw new FileDatabaseError(`Failed to write file ${filePath}: ${(error ).message}`);\n }\n }\n\n /**\n * Prepare the instance for read or write operations\n * This discovers state and sets up internal members based on mode and current data\n */\n async prepare(options\n\n\n\n\n\n\n\n\n\n ) {\n const { write, read, version, deferInitialVersion } = options;\n if (write) {\n if (this.versioned) {\n // Versioned mode\n if (this.currentVersion === null) {\n if (!deferInitialVersion) {\n await this.makeNewVersion();\n this.metadata = this.getDefaultMetadata();\n this.metadata.version = this.currentVersion;\n this.makeNewFile();\n }\n } else {\n // For existing versions, load the metadata if not already loaded\n if (!this.metadata.files.length) {\n this.metadata = await this.figureMetadata(this.currentVersion);\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n }\n }\n } else {\n // Non-versioned mode - ensure table directory exists\n await ensurePath(this.getDestinationPath());\n\n // Non-versioned mode - auto-detect useMetadata if not set\n if (this.useMetadata === true) {\n // Try to load existing metadata, create new if doesn't exist\n const metadataPath = path.join(this.getDestinationPath(), \"metadata.json\");\n if (fs.existsSync(metadataPath)) {\n try {\n const rawData = await fs.promises.readFile(metadataPath, \"utf8\");\n this.metadata = JSON.parse(rawData);\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n } catch (e) {\n this.metadata = this.getDefaultMetadata();\n this.currentFileNumber = 0;\n }\n } else {\n // Don't create a file here - let the write logic handle it\n // This prevents creating an empty file when customMetadata is provided\n this.metadata = this.getDefaultMetadata();\n this.currentFileNumber = 0;\n }\n } else {\n // No metadata mode - just create default metadata\n // Don't create a file here either - let the write logic handle it\n this.metadata = this.getDefaultMetadata();\n this.currentFileNumber = 0;\n }\n }\n } else if (read) {\n if (this.versioned) {\n // Versioned mode\n const versions = await this.getVersions();\n if (versions.length === 0) {\n throw new FileDatabaseError(\"[FileDatabase] No versions found, cannot read\");\n }\n\n if (version) {\n if (!versions.includes(version)) {\n throw new FileDatabaseError(`[FileDatabase] Version \"${version}\" not found`);\n }\n await this.setCurrentVersion(version);\n } else {\n await this.setCurrentVersion(versions[versions.length - 1]);\n }\n\n if (!this.metadata.files.length) {\n this.metadata = await this.figureMetadata(this.currentVersion);\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n }\n } else {\n // Non-versioned mode\n this.currentVersion = null; // No version concept\n\n // Auto-detect useMetadata if not explicitly set\n if (this.useMetadata === undefined) {\n const format = await this.detectDataFormat();\n this.useMetadata = format.hasMetadata;\n }\n\n if (this.useMetadata) {\n // Load metadata from root\n const destPath = this.getDestinationPath();\n const metadataPath = path.join(destPath, \"metadata.json\");\n if (fs.existsSync(metadataPath)) {\n try {\n const rawData = await fs.promises.readFile(metadataPath, \"utf8\");\n this.metadata = JSON.parse(rawData);\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n } catch (e) {\n throw new FileDatabaseError(`Failed to read metadata: ${(e ).message}`);\n }\n } else {\n throw new FileDatabaseError(\n `[FileDatabase] No metadata found in non-versioned mode. Looked for: ${metadataPath} (table path: ${destPath})`\n );\n }\n } else {\n // Figure metadata from files\n this.metadata = await this.figureMetadataFromVersionFiles(\"\");\n // Initialize currentFileNumber from existing files\n if (this.metadata.files && this.metadata.files.length > 0) {\n this.currentFileNumber = Math.max(...this.metadata.files.map(f => f.number || 0));\n } else {\n this.currentFileNumber = 0;\n }\n }\n }\n }\n }\n\n /**\n * Write data to the file database\n */\n async write(data, options = {}) {\n // Catalog mode: write to specific filename in destination path\n if (options.filename) {\n const destPath = this.getDestinationPath();\n await ensurePath(destPath);\n const filePath = path.join(destPath, options.filename);\n await this.safeWrite(filePath, data);\n return;\n }\n\n // Check for forceNewVersion in non-versioned mode\n if (options.forceNewVersion && !this.versioned) {\n throw new FileDatabaseError(\"Cannot use forceNewVersion in non-versioned mode\");\n }\n\n // Prepare for writing (this may load existing metadata).\n // If this write will force a new version on an empty store, defer the initial version in\n // prepare so we only call makeNewVersion() once (in the forceNewVersion block below).\n await this.prepare({ write: true, deferInitialVersion: !!(options.forceNewVersion && this.versioned) });\n\n // Always detect data type from incoming data AFTER prepare()\n // This ensures we use the correct type even if existing metadata has a different type\n const incomingDataType = detectDataType(data);\n this.metadata.dataType = incomingDataType;\n\n // Force new version if requested (versioned mode only)\n if (options.forceNewVersion) {\n await this.makeNewVersion();\n this.metadata = this.getDefaultMetadata();\n this.metadata.version = this.currentVersion;\n // Set data type from incoming data\n this.metadata.dataType = incomingDataType;\n this.makeNewFile();\n }\n\n // Check if customMetadata is provided and find existing file with matching metadata\n let targetFileIndex = null;\n const hasCustomMetadata = options.customMetadata && Object.keys(options.customMetadata).length > 0;\n \n if (hasCustomMetadata) {\n // Search through existing files for matching custom metadata\n for (let i = 0; i < this.metadata.files.length; i++) {\n const fileEntry = this.metadata.files[i];\n // Check if all customMetadata fields match\n // A file matches if it has all the customMetadata keys and their values match\n const matches = Object.keys(options.customMetadata).every(key => {\n // File must have the key and the value must match\n return key in fileEntry && fileEntry[key] === options.customMetadata[key];\n });\n if (matches) {\n targetFileIndex = i;\n this.logger.silly?.(`[FileDatabase] Found existing file with matching custom metadata: ${fileEntry.fileName}, metadata: ${JSON.stringify(options.customMetadata)}`);\n break;\n } else {\n this.logger.silly?.(`[FileDatabase] File ${fileEntry.fileName} does not match custom metadata: ${JSON.stringify(options.customMetadata)}`);\n }\n }\n if (targetFileIndex === null) {\n this.logger.silly?.(`[FileDatabase] No existing file found with custom metadata: ${JSON.stringify(options.customMetadata)}, will create new file`);\n }\n } else {\n this.logger.silly?.(`[FileDatabase] No custom metadata provided, will create new file`);\n }\n\n // If we found a matching file, prepare to overwrite it\n if (targetFileIndex !== null) {\n const targetFile = this.metadata.files[targetFileIndex];\n // Set current file number to match the target file\n this.currentFileNumber = targetFile.number;\n // Reset pagination state since we're overwriting\n this.lastFileData = null;\n this.currentRecord = 0;\n this.hasReadFirstPage = false;\n }\n\n // Pass flag indicating if we should force a new file (when customMetadata provided but no match)\n const forceNewFile = hasCustomMetadata && targetFileIndex === null;\n // eslint-disable-next-line prefer-const\n let { dataToWrite, dataLeftOver, fileName } = this.figureOutDataAndFileToWrite(data, targetFileIndex, forceNewFile);\n\n // Write first batch\n const destPath = this.getDestinationPath(this.currentVersion || undefined);\n await this.safeWrite(path.join(destPath, fileName), dataToWrite);\n this.updateMetadata(dataToWrite, fileName, options.customMetadata);\n\n // Handle pagination for remaining data (arrays only)\n // Note: For customMetadata matches, we only write to the target file, so no pagination needed\n while (dataLeftOver && dataLeftOver.length > 0 && targetFileIndex === null) {\n const writeContext = this.figureOutDataAndFileToWrite(dataLeftOver);\n await this.safeWrite(path.join(destPath, writeContext.fileName), writeContext.dataToWrite);\n this.updateMetadata(writeContext.dataToWrite, writeContext.fileName, options.customMetadata);\n dataLeftOver = writeContext.dataLeftOver;\n }\n\n // Calculate version synopsis\n this.calculateVersionSynopsis();\n\n // Save metadata to file\n if (this.useMetadata) {\n await this.saveVersionMetadata(this.metadata);\n }\n }\n\n /**\n * Read data from the file database\n */\n async read(options = {}) {\n const { version, nextPage = false, pageSize, filename } = options;\n\n // Catalog mode: read specific file by name\n if (filename) {\n const destPath = this.getDestinationPath(version);\n const filePath = path.join(destPath, filename);\n try {\n const rawData = await fs.promises.readFile(filePath, \"utf8\");\n return JSON.parse(rawData);\n } catch (error) {\n throw new FileDatabaseError(`Failed to read file ${filename}: ${(error ).message}`);\n }\n }\n\n // Prepare for reading\n await this.prepare({ read: true, version });\n\n // Check for non-paginated data types\n const isNonPaginatedData =\n this.metadata.dataType === \"text\" || this.metadata.dataType === \"xml\" || this.metadata.dataType === \"json-object\";\n\n if (isNonPaginatedData) {\n // For text/xml/object data, return all content\n const file = this.metadata.files[0];\n const filePath = path.join(this.getDestinationPath(this.currentVersion || undefined), file.fileName);\n try {\n const rawData = await fs.promises.readFile(filePath, \"utf8\");\n return deserializeData(rawData, this.metadata.dataType);\n } catch (error) {\n throw new FileDatabaseError(`Failed to read file ${file.fileName}: ${(error ).message}`);\n }\n }\n\n // Handle paginated data (JSON arrays)\n let effectivePageSize;\n\n if (nextPage && this.hasReadFirstPage) {\n // Move to next page\n effectivePageSize = pageSize || this.pageSize;\n this.currentRecord += effectivePageSize;\n } else if (!nextPage) {\n // If not paginating, read all records (unless pageSize is explicitly provided)\n effectivePageSize = pageSize !== undefined ? pageSize : this.metadata.totalRecords;\n this.currentRecord = 0;\n } else {\n // First call with nextPage=true (no previous page read)\n effectivePageSize = pageSize || this.pageSize;\n }\n\n // If beyond total records, return empty array\n if (this.currentRecord >= this.metadata.totalRecords) {\n return [];\n }\n\n const result = [];\n let recordsRead = 0;\n let currentFileIndex = 0;\n let currentFileOffset = 0;\n\n // Calculate which file and offset to start from\n let totalRecords = 0;\n for (let i = 0; i < this.metadata.files.length; i++) {\n const file = this.metadata.files[i];\n if (this.currentRecord < totalRecords + file.recordsCount) {\n currentFileIndex = i;\n currentFileOffset = totalRecords;\n break;\n }\n totalRecords += file.recordsCount;\n }\n\n // Read from files\n let cumulativeRecords = currentFileOffset;\n for (let i = currentFileIndex; i < this.metadata.files.length && recordsRead < effectivePageSize; i++) {\n const file = this.metadata.files[i];\n const filePath = path.join(this.getDestinationPath(this.currentVersion || undefined), file.fileName);\n\n try {\n const rawData = await fs.promises.readFile(filePath, \"utf8\");\n const fileData = deserializeData(rawData, this.metadata.dataType);\n\n let startIndex = 0;\n if (i === currentFileIndex) {\n startIndex = this.currentRecord - cumulativeRecords;\n }\n\n const endIndex = Math.min(startIndex + (effectivePageSize - recordsRead), fileData.length);\n const recordsFromThisFile = fileData.slice(startIndex, endIndex);\n\n result.push(...recordsFromThisFile);\n recordsRead += recordsFromThisFile.length;\n\n cumulativeRecords += file.recordsCount;\n } catch (error) {\n throw new FileDatabaseError(`Failed to read file ${file.fileName}: ${(error ).message}`);\n }\n }\n\n // Mark page as read for pagination tracking\n // When nextPage=true, we're explicitly paginating\n // When nextPage=false with explicit pageSize, we're also paginating (starting from beginning)\n if (result.length > 0) {\n if (nextPage || (pageSize !== undefined && pageSize < this.metadata.totalRecords)) {\n this.hasReadFirstPage = true;\n }\n }\n\n return result;\n }\n\n /**\n * Set the starting record for pagination (1-based index)\n */\n setStartRecord(startRecord) {\n this.currentRecord = startRecord - 1;\n this.hasReadFirstPage = false;\n }\n\n /**\n * Reset read pagination state\n */\n resetPagination() {\n this.currentRecord = 0;\n this.hasReadFirstPage = false;\n }\n\n /**\n * List filenames in the table directory.\n * For catalog/key-value usage (files written with { filename }).\n * Returns data file names (.json, .txt, .xml) excluding metadata.json.\n */\n async listFilenames() {\n const destPath = this.versioned && this.currentVersion\n ? path.join(this.getDestinationPath(), this.currentVersion)\n : this.getDestinationPath();\n try {\n const entries = await fs.promises.readdir(destPath, { withFileTypes: true });\n return entries\n .filter((e) => e.isFile() && e.name !== \"metadata.json\" && /\\.(json|txt|xml)$/i.test(e.name))\n .map((e) => e.name);\n } catch (err) {\n if (err?.code === \"ENOENT\") return [];\n throw new FileDatabaseError(`Failed to list files: ${(err ).message}`);\n }\n }\n\n /**\n * Remove a file from the table directory (catalog mode).\n * Use with listFilenames() to manage individual files.\n */\n async removeFile(filename) {\n const destPath = this.versioned && this.currentVersion\n ? path.join(this.getDestinationPath(), this.currentVersion)\n : this.getDestinationPath();\n const filePath = path.join(destPath, filename);\n try {\n await fs.promises.unlink(filePath);\n } catch (err) {\n if (err?.code === \"ENOENT\") return;\n throw new FileDatabaseError(`Failed to remove file ${filename}: ${(err ).message}`);\n }\n }\n\n /**\n * Remove a file and its metadata entry (non-versioned mode with useMetadata).\n * Use with findData() to get fileName, then call removeFileEntry to delete.\n */\n async removeFileEntry(filename) {\n if (this.versioned) {\n throw new FileDatabaseError(\"removeFileEntry is only supported in non-versioned mode\");\n }\n await this.prepare({ read: true });\n const idx = this.metadata.files.findIndex((f) => f.fileName === filename);\n if (idx === -1) {\n throw new FileDatabaseError(`File entry ${filename} not found in metadata`);\n }\n const entry = this.metadata.files[idx];\n const recordsCount = entry.recordsCount || 0;\n this.metadata.files.splice(idx, 1);\n this.metadata.totalRecords = Math.max(0, (this.metadata.totalRecords || 0) - recordsCount);\n const destPath = this.getDestinationPath();\n const filePath = path.join(destPath, filename);\n try {\n await fs.promises.unlink(filePath);\n } catch (err) {\n if (err?.code === \"ENOENT\") {\n this.logger.warn?.(`[FileDatabase] File ${filename} already missing on disk`);\n } else {\n throw new FileDatabaseError(`Failed to remove file ${filename}: ${(err ).message}`);\n }\n }\n if (this.useMetadata) {\n await this.saveVersionMetadata(this.metadata);\n }\n }\n\n /**\n * Set file-level synopsis calculation function\n */\n setFileSynopsisFunction(fn) {\n this.fileSynopsisFunction = fn;\n }\n\n /**\n * Set version-level synopsis calculation function\n */\n setVersionSynopsisFunction(fn) {\n this.versionSynopsisFunction = fn;\n }\n\n /**\n * Get current version name\n */\n getCurrentVersion() {\n return this.currentVersion;\n }\n\n /**\n * Get current metadata\n */\n getMetadata() {\n return { ...this.metadata };\n }\n\n /**\n * Find data by custom metadata fields\n * Searches through all versions and files to find entries matching the search criteria\n * \n * @param searchCriteria - Object with field names and values to search for (e.g., { ListingKey: \"123\", id: \"456\" })\n * @returns Array of found entries with their file paths and metadata\n */\n async findData(searchCriteria)\n\n\n\n\n\n {\n const results\n\n\n\n\n\n = [];\n\n // Handle non-versioned mode separately\n if (!this.versioned) {\n // Load metadata for non-versioned mode\n await this.prepare({ read: true });\n const metadata = this.getMetadata();\n \n // Search through files\n for (const fileEntry of metadata.files) {\n // Check if file entry matches search criteria\n const matches = Object.keys(searchCriteria).every(key => {\n return fileEntry[key] === searchCriteria[key];\n });\n\n if (matches) {\n // Read the file data\n const destPath = this.getDestinationPath();\n const filePath = path.join(destPath, fileEntry.fileName);\n const fileData = await fs.promises.readFile(filePath, \"utf8\");\n const data = deserializeData(fileData, metadata.dataType || \"json-object\");\n\n results.push({\n filePath,\n fileName: fileEntry.fileName,\n version: null,\n metadata: fileEntry,\n data,\n });\n }\n }\n } else {\n // Versioned mode: search through all versions\n const versions = await this.getVersions();\n \n for (const version of versions) {\n // Load metadata for this version\n await this.prepare({ read: true, version });\n const metadata = this.getMetadata();\n\n // Search through files in this version\n for (const fileEntry of metadata.files) {\n // Check if file entry matches search criteria\n const matches = Object.keys(searchCriteria).every(key => {\n return fileEntry[key] === searchCriteria[key];\n });\n\n if (matches) {\n // Read the file data\n const destPath = this.getDestinationPath(version);\n const filePath = path.join(destPath, fileEntry.fileName);\n const fileData = await fs.promises.readFile(filePath, \"utf8\");\n const data = deserializeData(fileData, metadata.dataType || \"json-object\");\n\n results.push({\n filePath,\n fileName: fileEntry.fileName,\n version,\n metadata: fileEntry,\n data,\n });\n }\n }\n }\n }\n\n return results;\n }\n}\n\n// Export types\n\n\n\n\n\n\n\n\n\n\n\n;\n\n// Export synopsis functions\nexport { defaultFileSynopsisFunction, defaultVersionSynopsisFunction } from \"./synopsis-functions.js\";\n\n/**\n * Initialize FileDatabase from context\n * Similar to MlsClient.init pattern\n * \n * @param context - Context from init()\n * @param options - Optional configuration (takes precedence over context.params)\n * @returns Initialized FileDatabase instance\n */\n/**\n * List all table names in a given namespace\n * Scans the filesystem to find all table directories\n *\n * @param basePath - Base path for file storage\n * @param namespace - Namespace to scan (e.g., \"harvested\", \"fromMLS\")\n * @returns Array of table names (directory names)\n */\nexport function listTables(basePath, namespace) {\n const namespacePath = path.join(basePath, namespace);\n\n if (!fs.existsSync(namespacePath)) {\n return [];\n }\n\n try {\n return fs.readdirSync(namespacePath, { withFileTypes: true })\n .filter(dirent => dirent.isDirectory())\n .map(dirent => dirent.name);\n } catch (error) {\n // Return empty array if can't read directory\n return [];\n }\n}\n\n/**\n * List all sources (namespaces) in a given base path\n * Scans the filesystem to find all namespace directories\n *\n * @param basePath - Base path for file storage\n * @returns Array of source names (directory names)\n */\nexport function listSources(basePath) {\n if (!fs.existsSync(basePath)) {\n return [];\n }\n\n try {\n return fs.readdirSync(basePath, { withFileTypes: true })\n .filter(dirent => dirent.isDirectory())\n .map(dirent => dirent.name);\n } catch (error) {\n // Return empty array if can't read directory\n return [];\n }\n}\n","\n\n\n\n\n\n\n\n/**\n * Detect the data type of a given value\n */\nexport function detectDataType(data) {\n if (Array.isArray(data)) {\n return \"json-array\";\n } else if (typeof data === \"object\" && data !== null) {\n return \"json-object\";\n } else if (typeof data === \"string\") {\n // Try to detect if it's XML\n const trimmed = data.trim();\n if (trimmed.startsWith(\"<?xml\") || trimmed.startsWith(\"<\")) {\n return \"xml\";\n }\n return \"text\";\n } else {\n return \"text\";\n }\n}\n\n/**\n * Serialize data to a string for file storage\n */\nexport function serializeData(data) {\n const dataType = detectDataType(data);\n \n if (dataType === \"json-array\" || dataType === \"json-object\") {\n return JSON.stringify(data, null, 4);\n } else {\n // For text and XML, return as-is\n return String(data);\n }\n}\n\n/**\n * Deserialize data from a string based on data type\n */\nexport function deserializeData(rawData, dataType) {\n if (dataType === \"json-array\" || dataType === \"json-object\") {\n return JSON.parse(rawData);\n } else {\n // For text and XML, return as-is\n return rawData;\n }\n}\n\n","/**\n * Error classes for the CLI toolkit\n */\n\nexport class FrameworkError extends Error {\n constructor(message) {\n super(message);\n this.name = \"FrameworkError\";\n }\n}\n\nexport class ParamError extends FrameworkError {\n constructor(message) {\n super(message);\n this.name = \"ParamError\";\n }\n}\n\nexport class InitError extends FrameworkError {\n constructor(message) {\n super(message);\n this.name = \"InitError\";\n }\n}\n\nexport class CriticalRequestError extends FrameworkError {\n constructor(message) {\n super(message);\n this.name = \"CriticalRequestError\";\n }\n}\n\nexport class ControlFlowError extends Error {\n constructor(message) {\n super(message);\n this.name = \"ControlFlowError\";\n }\n}\n\nexport class HttpClientError extends FrameworkError {\n constructor(message, cause) {\n super(message);this.cause = cause;;\n this.name = \"HttpClientError\";\n }\n}\n\nexport class FileDatabaseError extends FrameworkError {\n constructor(message) {\n super(message);\n this.name = \"FileDatabaseError\";\n }\n}\n\n","import { ParamError } from \"../errors.js\";\n\n/**\n * Base class every task handler extends. Holds the runner `context` and the\n * claimed task row, and defines the contract the runner calls into:\n *\n * - {@link AbstractTask#cantRunReason}: synchronous / async precondition check.\n * Returning a truthy string tells the runner \"skip for now, stash the reason\n * into `progress`\" without burning a retry; returning `false` means \"go\".\n * - {@link AbstractTask#run}: the actual work, handed a `reportProgress`\n * callback that updates the DB `progress` column.\n * - {@link AbstractTask#requestStop}: cooperative shutdown signal from the runner\n * (noop by default; long-running tasks override to honor it).\n *\n * Static enqueue-time hooks (write-time validation):\n *\n * - {@link AbstractTask.resolveParams}: build a full row payload (envelope +\n * inner `params` blob) for {@link enqueueTask}. Subclasses normally do not\n * override this — they override {@link AbstractTask.resolveCustomParams}\n * instead. Override here only for envelope-level cross-field rules\n * (e.g. \"stop tasks must target a specific service_name\").\n * - {@link AbstractTask.resolveCustomParams}: validate / default the typed\n * fields that go into the `params` JSON column. Default returns a\n * `--paramsJson` passthrough; subclasses override.\n *\n * Both static methods are async and accept `(context, overrides)`. `overrides`\n * is a partial that wins over CLI/env values — the typical caller is the\n * {@link TasksRegistry#resolveTaskParams} dispatcher, which seeds it with\n * `{ name }`. Programmatic enqueuers can pass any envelope field plus\n * `overrides.params` to inject inner-blob values.\n */\nexport class AbstractTask {\n /**\n * Whether `send-task` should wait for completion (and print a result\n * report) when no explicit `--wait` / `--noWait` flag is given. Defaults\n * to false; short-lived probe tasks (e.g. `ping`) override to true.\n *\n * @type {boolean}\n */\n static defaultWaitForResult = false;\n\n /**\n * @param {object} context Runner context (db, logger, params, emitter...).\n * @param {object} task Task row as claimed from the queue.\n */\n constructor(context, task) {\n this.context = context;\n this.task = task;\n }\n\n /**\n * Return a short reason string when the task should be deferred (e.g. \"locked\n * by source\"), or `false`/falsy when it is free to run. Default: always `false`.\n *\n * @returns {string | false | Promise<string | false>}\n */\n cantRunReason() {\n return false;\n }\n\n /**\n * Called by the runner when a stop has been requested. Subclasses running\n * long loops should flip a flag here and check it between iterations.\n *\n * @param {number} [_allowanceMs] Grace period the runner promises before hard exit.\n */\n requestStop(_allowanceMs) {\n // Default no-op; long-running tasks can override.\n }\n\n /**\n * Perform the task. Must be implemented by subclasses.\n *\n * @param {(progress: unknown) => Promise<void>} _reportProgress\n * Updates the DB `progress` column. Accepts any serializable value;\n * strings are stored verbatim, objects are JSON-stringified.\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run(_reportProgress) {\n throw new Error(\"AbstractTask.run must be implemented by subclass\");\n }\n\n /**\n * Resolve a complete row payload for this task — envelope fields (queue,\n * priority, targeting, schedule…) plus the inner `params` blob produced by\n * {@link AbstractTask.resolveCustomParams}. Output shape matches\n * {@link enqueueTask}'s `options` argument, so the typical call is:\n *\n * const payload = await TaskClass.resolveParams(context, { name });\n * await enqueueTask(context, payload);\n *\n * Validation failures throw {@link ParamError} so the script aborts before\n * a malformed row hits the DB.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides] Partial overrides; takes precedence over CLI/env.\n * Recognised keys: `name`, `queueName`, `priority`, `serviceGroup`,\n * `serviceName`, `instanceNumber`, `serverName`, `opid`, `schedule`,\n * `nextRunAt`, plus `params` (object — overlay onto inner blob).\n * @returns {Promise<object>}\n */\n static async resolveParams(context, overrides = {}) {\n const main = AbstractTask._resolveMainFields(context, overrides);\n const params = await this.resolveCustomParams(context, overrides);\n return { ...main, params };\n }\n\n /**\n * Resolve the inner JSON blob stored in the `params` column. Default\n * implementation passes through `--paramsJson` (parsed as a JSON object)\n * overlaid with `overrides.params` when supplied; returns `null` when\n * neither is provided.\n *\n * Subclasses with typed fields should override and call\n * {@link AbstractTask._mergeTypedParams} for layered CLI/env/JSON/override\n * resolution, then validate and throw {@link ParamError} on bad input.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<object|null>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n return AbstractTask._defaultParamsBlob(context, overrides);\n }\n\n /**\n * Read main task envelope fields from `context.params` (CLI/env), with\n * any matching key on `overrides` taking precedence. Internal; called by\n * {@link AbstractTask.resolveParams}.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {object}\n */\n static _resolveMainFields(context, overrides = {}) {\n const defs = {\n queueName: \"string default tasks\",\n priority: \"number default 50\",\n serviceGroup: \"string\",\n serviceName: \"string\",\n instanceNumber: \"number\",\n serverName: \"string\",\n opid: \"string\",\n schedule: \"string\",\n };\n const cli = context.params.getAllForModule(\"task-envelope\", defs);\n\n const name = emptyToUndef(overrides.name) ?? emptyToUndef(context.params.get(\"name\", \"string\"));\n if (!name) {\n throw new ParamError(\"Task --name is required (e.g. ping, stop, dummyHarvest)\");\n }\n\n let instanceNumber;\n const rawInstance = overrides.instanceNumber ?? cli.instanceNumber;\n if (rawInstance !== undefined && rawInstance !== null && String(rawInstance).trim() !== \"\") {\n const n = Number(rawInstance);\n if (!Number.isFinite(n) || !Number.isInteger(n) || n < 1) {\n throw new ParamError(\"--instanceNumber must be a positive integer when set\");\n }\n instanceNumber = n;\n } else {\n instanceNumber = null;\n }\n\n const priorityRaw = overrides.priority ?? cli.priority ?? 50;\n const priority = Number(priorityRaw);\n if (!Number.isFinite(priority)) {\n throw new ParamError(`--priority must be a number (got ${JSON.stringify(priorityRaw)})`);\n }\n\n return {\n name,\n queueName: overrides.queueName ?? emptyToUndef(cli.queueName) ?? \"tasks\",\n priority,\n serviceGroup: emptyToUndef(overrides.serviceGroup) ?? emptyToUndef(cli.serviceGroup) ?? null,\n serviceName: emptyToUndef(overrides.serviceName) ?? emptyToUndef(cli.serviceName) ?? null,\n instanceNumber,\n serverName: emptyToUndef(overrides.serverName) ?? emptyToUndef(cli.serverName) ?? null,\n opid: emptyToUndef(overrides.opid) ?? emptyToUndef(cli.opid) ?? null,\n schedule: emptyToUndef(overrides.schedule) ?? emptyToUndef(cli.schedule) ?? null,\n nextRunAt: overrides.nextRunAt ?? null,\n };\n }\n\n /**\n * Default inner-params resolver: parses `--paramsJson` (must be a JSON\n * object), then overlays `overrides.params` on top. Returns `null` when\n * neither is provided.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {object|null}\n */\n static _defaultParamsBlob(context, overrides = {}) {\n const cli = context.params.getAllForModule(\"task-params\", { paramsJson: \"string\" });\n const fromJson = parseParamsJson(cli.paramsJson);\n const fromOverride = pickParamsObject(overrides);\n if (!fromJson && !fromOverride) return null;\n return { ...(fromJson ?? {}), ...(fromOverride ?? {}) };\n }\n\n /**\n * Helper for subclass `resolveCustomParams` overrides. Reads typed CLI\n * params (per `defs`) plus `--paramsJson` under a module namespace, then\n * merges them with explicit `overrides.params` in increasing priority:\n *\n * typed CLI flags → --paramsJson → overrides.params\n *\n * Undefined values are dropped so defaults declared in `defs` aren't\n * overwritten by missing-flag noise. Returns the merged object; the\n * caller is responsible for validation and throwing `ParamError`.\n *\n * @param {object} context\n * @param {string} moduleName Namespace for `--showUsedParams` grouping.\n * @param {Record<string, string>} defs Param defs in `getAllForModule` syntax.\n * @param {Record<string, unknown>} [overrides] As passed to `resolveCustomParams`.\n * @returns {Record<string, unknown>}\n */\n static _mergeTypedParams(context, moduleName, defs, overrides = {}) {\n const fullDefs = { ...defs, paramsJson: \"string\" };\n const cliRaw = context.params.getAllForModule(moduleName, fullDefs);\n const fromJson = parseParamsJson(cliRaw.paramsJson) ?? {};\n const fromCli = {};\n for (const [k, v] of Object.entries(cliRaw)) {\n if (k === \"paramsJson\") continue;\n if (v !== undefined && v !== null) fromCli[k] = v;\n }\n const fromOverride = pickParamsObject(overrides) ?? {};\n return { ...fromCli, ...fromJson, ...fromOverride };\n }\n}\n\n/** Trim a string-ish to a non-empty string, or return undefined. Non-strings pass through. */\nfunction emptyToUndef(s) {\n if (s === undefined || s === null) return undefined;\n if (typeof s !== \"string\") return s;\n const t = s.trim();\n return t.length ? t : undefined;\n}\n\n/**\n * Parse a `--paramsJson` value. Returns `null` for empty/missing input.\n * Throws `ParamError` for non-JSON or non-object payloads.\n *\n * @param {unknown} raw\n * @returns {object|null}\n */\nfunction parseParamsJson(raw) {\n if (raw == null) return null;\n const t = String(raw).trim();\n if (!t) return null;\n let parsed;\n try {\n parsed = JSON.parse(t);\n } catch (e) {\n throw new ParamError(`--paramsJson: not valid JSON: ${e?.message ?? String(e)}`);\n }\n if (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n throw new ParamError(\"--paramsJson must be a JSON object\");\n }\n return parsed;\n}\n\n/** Extract `overrides.params` when it is a plain object, else undefined. */\nfunction pickParamsObject(overrides) {\n const p = overrides?.params;\n if (p && typeof p === \"object\" && !Array.isArray(p)) return p;\n return undefined;\n}\n","import { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Trivial health-check task: used by `registryMaintenance` to probe runners and\n * by operators to confirm a runner is picking up work. Emits a log line and\n * returns `{ success: true, results: \"pong\" }`.\n */\nexport class TaskPing extends AbstractTask {\n /** Default-wait so `send-task --name=ping` prints a result without `--wait`. */\n static defaultWaitForResult = true;\n\n /** Ping takes no params. */\n static async resolveCustomParams() {\n return null;\n }\n\n /**\n * @returns {Promise<{ success: true, results: \"pong\" }>}\n */\n async run() {\n this.context.logger.info?.(`[TaskPing] pong (${this.task.id})`);\n return { success: true, results: \"pong\" };\n }\n}\n","import { sleepMs } from \"../../utils/index.js\";\nimport { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Long-running demo task used for exercising the runner's progress/stop paths.\n *\n * Ticks `total` times with `delay` ms between iterations, calling `reportProgress`\n * each tick. Honors cooperative stop (`requestStop`) by completing the current\n * iteration and deciding whether to finish the run or abort based on the\n * remaining work vs. the allowance window.\n */\nexport class TaskSampleProcess extends AbstractTask {\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ total: number, delay: number, name?: string }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-sample-process\", {\n total: \"number default 10\",\n delay: \"number default 1000\",\n name: \"string\",\n }, overrides);\n const total = Number(merged.total);\n const delay = Number(merged.delay);\n if (!Number.isInteger(total) || total <= 0) {\n throw new ParamError(`sampleProcess: param \"total\" must be a positive integer (got ${JSON.stringify(merged.total)})`);\n }\n if (!Number.isInteger(delay) || delay < 0) {\n throw new ParamError(`sampleProcess: param \"delay\" must be an integer >= 0 (got ${JSON.stringify(merged.delay)})`);\n }\n const out = { total, delay };\n if (typeof merged.name === \"string\" && merged.name.trim()) {\n out.name = merged.name.trim();\n }\n return out;\n }\n\n /**\n * @param {object} context\n * @param {object} task\n */\n constructor(context, task) {\n super(context, task);\n this.stopRequested = false;\n this.stopAllowanceMs = 0;\n this.stopDecisionLogged = false;\n }\n\n /**\n * Runner-facing stop signal. Records the allowance window so the main loop\n * can decide per-iteration whether to finish or abort early.\n *\n * @param {number} allowanceMs\n */\n requestStop(allowanceMs) {\n this.stopRequested = true;\n this.stopAllowanceMs = Number.isFinite(allowanceMs) && allowanceMs > 0 ? allowanceMs : 0;\n this.context.logger.warn?.(\n `[TaskSampleProcess] stop signal received (${this.task.id}), allowanceMs=${this.stopAllowanceMs}`\n );\n }\n\n /**\n * Iterate `total` times, sleeping `delay` ms between ticks and reporting\n * progress every iteration. Validates params up front; invalid values short-\n * circuit to a structured failure without starting the loop.\n *\n * @param {(progress: object) => Promise<void>} reportProgress\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run(reportProgress) {\n const totalRaw = this.task?.params?.total ?? 10;\n const delayRaw = this.task?.params?.delay ?? 1000;\n const nameRaw = this.task?.params?.name;\n\n const total = Number(totalRaw);\n const delay = Number(delayRaw);\n const name = typeof nameRaw === \"string\" && nameRaw.trim() ? nameRaw.trim() : \"sampleProcess\";\n\n const errors = [];\n if (!Number.isInteger(total) || total <= 0) {\n errors.push('param \"total\" must be a positive integer');\n }\n if (!Number.isInteger(delay) || delay < 0) {\n errors.push('param \"delay\" must be an integer >= 0');\n }\n\n if (errors.length > 0) {\n return {\n success: false,\n results: {\n error: `Validation failed: ${errors.join(\", \")}`,\n received: { total: totalRaw, delay: delayRaw, name: nameRaw },\n },\n };\n }\n\n const startedAt = Date.now();\n for (let i = 1; i <= total; i += 1) {\n if (this.stopRequested) {\n const remainingMs = Math.max(0, (total - i + 1) * delay);\n if (remainingMs <= this.stopAllowanceMs) {\n if (!this.stopDecisionLogged) {\n this.stopDecisionLogged = true;\n this.context.logger.warn?.(\n `[TaskSampleProcess] continue to finish (${this.task.id}): remainingMs=${remainingMs} <= allowanceMs=${this.stopAllowanceMs}`\n );\n }\n } else {\n this.context.logger.warn?.(\n `[TaskSampleProcess] stopping gracefully at iteration ${i}/${total} (${this.task.id}), remainingMs=${remainingMs} > allowanceMs=${this.stopAllowanceMs}`\n );\n return {\n success: false,\n results: {\n message: `Stopped before completion at iteration ${i}/${total}`,\n completed: i - 1,\n total,\n name,\n remainingMs,\n allowanceMs: this.stopAllowanceMs,\n },\n };\n }\n }\n\n const elapsed = Date.now() - startedAt;\n const remaining = Math.max(0, (total - i) * delay);\n const progress = {\n name,\n count: i,\n total,\n elapsedMs: elapsed,\n remainingMs: remaining,\n status: `running ${name}: ${i}/${total}`,\n };\n\n this.context.logger.progress(\"running\", {\n prefix: name,\n count: i,\n total,\n });\n await reportProgress(progress);\n await sleepMs(delay);\n }\n\n return {\n success: true,\n results: {\n message: `Completed ${total} iterations`,\n total,\n delay,\n name,\n },\n };\n }\n}\n","import { spawn } from \"node:child_process\";\nimport { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Run a shell string, buffering stdout/stderr until exit. Use for short commands\n * only — everything accumulates in memory; long-running / high-throughput work\n * should spawn its own worker instead (see `taskScriptRunner.js`).\n *\n * @param {string} command\n * @param {string} [cwd]\n * @returns {Promise<{ exitCode: number|null, output: string, stderr: string, signal: NodeJS.Signals|null }>}\n */\nfunction runShellCommand(command, cwd) {\n return new Promise((resolve, reject) => {\n const child = spawn(command, {\n shell: true,\n cwd: cwd || process.cwd(),\n stdio: [\"ignore\", \"pipe\", \"pipe\"],\n });\n\n let output = \"\";\n let stderr = \"\";\n\n child.stdout.on(\"data\", (chunk) => {\n output += String(chunk);\n });\n child.stderr.on(\"data\", (chunk) => {\n stderr += String(chunk);\n });\n\n child.on(\"error\", (error) => {\n reject(error);\n });\n\n child.on(\"close\", (exitCode, signal) => {\n resolve({\n exitCode,\n output: output.trim(),\n stderr: stderr.trim(),\n signal,\n });\n });\n });\n}\n\n/**\n * Task wrapper for {@link runShellCommand}. Accepts either:\n *\n * - `params: \"echo hi\"` (string shortcut), or\n * - `params: { command: string, cwd?: string }`.\n *\n * Success is defined as `exitCode === 0`. Non-zero / spawn errors come back as\n * `{ success: false, results: { ... } }` — never a thrown exception.\n */\nexport class TaskShellCommand extends AbstractTask {\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ command: string, cwd?: string }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-shell\", {\n command: \"string\",\n cwd: \"string\",\n }, overrides);\n const command = typeof merged.command === \"string\" ? merged.command.trim() : \"\";\n if (!command) {\n throw new ParamError('shellCommand: param \"command\" must be a non-empty string');\n }\n const cwd = typeof merged.cwd === \"string\" && merged.cwd.trim() ? merged.cwd.trim() : null;\n return cwd ? { command, cwd } : { command };\n }\n\n /**\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run() {\n const params = this.task?.params;\n const commandRaw = typeof params === \"string\" ? params : params?.command;\n const cwdRaw = typeof params === \"string\" ? undefined : params?.cwd;\n const command = typeof commandRaw === \"string\" ? commandRaw.trim() : \"\";\n const cwd = typeof cwdRaw === \"string\" && cwdRaw.trim() ? cwdRaw.trim() : undefined;\n\n if (!command) {\n return {\n success: false,\n results: {\n error: 'Validation failed: param \"command\" must be a non-empty string',\n received: this.task?.params ?? null,\n },\n };\n }\n\n try {\n const result = await runShellCommand(command, cwd);\n const success = result.exitCode === 0;\n\n this.context.logger.info?.(\n `[TaskShellCommand] command=\"${command}\" exitCode=${String(result.exitCode)} (${this.task.id})`\n );\n\n return {\n success,\n results: {\n command,\n cwd: cwd ?? process.cwd(),\n output: result.output,\n stderr: result.stderr,\n exitCode: result.exitCode,\n signal: result.signal,\n },\n };\n } catch (error) {\n return {\n success: false,\n results: {\n command,\n cwd: cwd ?? process.cwd(),\n output: \"\",\n stderr: \"\",\n exitCode: null,\n error: error?.message ?? String(error),\n },\n };\n }\n }\n}\n","import os from \"node:os\";\nimport fs from \"node:fs/promises\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Format bytes as a human-readable gigabyte string, e.g. `\"3.25 GB\"`.\n *\n * @param {number} valueBytes\n * @returns {string}\n */\nfunction toGb(valueBytes) {\n return `${(valueBytes / (1024 ** 3)).toFixed(2)} GB`;\n}\n\n/**\n * Format bytes as a human-readable megabyte string, e.g. `\"128.00 MB\"`.\n *\n * @param {number} valueBytes\n * @returns {string}\n */\nfunction toMb(valueBytes) {\n return `${(valueBytes / (1024 ** 2)).toFixed(2)} MB`;\n}\n\n/**\n * Disk usage for `/` as `{ total, used, free }` strings. Uses `fs.statfs`\n * (Node 20+); for host-level signal we probe the root mount rather than the cwd.\n *\n * @returns {Promise<{ total: string, used: string, free: string }>}\n */\nasync function getDiskStats() {\n // Node 20+ statfs; use root path for host-level signal.\n const stats = await fs.statfs(\"/\");\n const total = Number(stats.bsize) * Number(stats.blocks);\n const free = Number(stats.bsize) * Number(stats.bavail);\n const used = total - free;\n return {\n total: toGb(total),\n used: toGb(used),\n free: toGb(free),\n };\n}\n\n/**\n * Snapshot of host + process stats (memory, CPU utilization, disk, runtime info).\n * Useful for ops dashboards and cluster-wide \"ping with telemetry\".\n */\nexport class TaskSystemInfo extends AbstractTask {\n /** Same UX expectation as `ping` — short probe, print the result. */\n static defaultWaitForResult = true;\n\n /** systemInfo takes no params. */\n static async resolveCustomParams() {\n return null;\n }\n\n /**\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run() {\n try {\n const totalMemory = os.totalmem();\n const freeMemory = os.freemem();\n const usedMemory = totalMemory - freeMemory;\n\n const cpus = os.cpus();\n const cpuUtilization = cpus.map((cpu) => {\n const total = Object.values(cpu.times).reduce((acc, time) => acc + time, 0);\n const usage = ((total - cpu.times.idle) / total) * 100;\n return Number(usage.toFixed(2));\n });\n\n const processMemory = process.memoryUsage();\n const disk = await getDiskStats();\n\n const results = {\n memory: {\n total: toGb(totalMemory),\n used: toGb(usedMemory),\n free: toGb(freeMemory),\n },\n processMemory: {\n rss: toMb(processMemory.rss),\n heapTotal: toMb(processMemory.heapTotal),\n heapUsed: toMb(processMemory.heapUsed),\n external: toMb(processMemory.external),\n },\n disk,\n cpu: {\n cores: cpuUtilization.length,\n utilization: cpuUtilization,\n },\n runtime: {\n platform: os.platform(),\n arch: os.arch(),\n uptimeSec: os.uptime(),\n hostname: os.hostname(),\n },\n };\n\n this.context.logger.info?.(`[TaskSystemInfo] collected system metrics (${this.task.id})`);\n return { success: true, results };\n } catch (error) {\n return {\n success: false,\n results: {\n error: \"Can't collect system stats\",\n message: error?.message ?? String(error),\n },\n };\n }\n }\n}\n","import { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Sanity-check task for wiring: reads `params.a` and `params.b` (both numbers),\n * returns `{ a, b, sum }`. Any non-numeric input short-circuits to a structured\n * validation failure, not an exception.\n */\nexport class TaskSumAB extends AbstractTask {\n /** Short, deterministic — wait by default so callers see the sum. */\n static defaultWaitForResult = true;\n\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ a: number, b: number }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-sumab\", {\n a: \"number\",\n b: \"number\",\n }, overrides);\n if (typeof merged.a !== \"number\" || Number.isNaN(merged.a)) {\n throw new ParamError(`taskSumAB: param \"a\" must be a valid number (got ${JSON.stringify(merged.a)})`);\n }\n if (typeof merged.b !== \"number\" || Number.isNaN(merged.b)) {\n throw new ParamError(`taskSumAB: param \"b\" must be a valid number (got ${JSON.stringify(merged.b)})`);\n }\n return { a: merged.a, b: merged.b };\n }\n\n /**\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run() {\n const a = this.task?.params?.a;\n const b = this.task?.params?.b;\n\n if (typeof a !== \"number\" || Number.isNaN(a)) {\n return {\n success: false,\n results: {\n error: 'Validation failed: param \"a\" must be a valid number',\n received: { a, b },\n },\n };\n }\n\n if (typeof b !== \"number\" || Number.isNaN(b)) {\n return {\n success: false,\n results: {\n error: 'Validation failed: param \"b\" must be a valid number',\n received: { a, b },\n },\n };\n }\n\n const sum = a + b;\n this.context.logger.info?.(`[TaskSumAB] ${a} + ${b} = ${sum} (${this.task.id})`);\n return {\n success: true,\n results: { a, b, sum },\n };\n }\n}\n","import { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\n\n/**\n * Cooperative stop signal for the runner that claims it. Returns a result with\n * `stopRunner: true`; `runTasksLoop` sees that flag in `executeClaimedTask`'s\n * outcome and flips its own stop state, propagating `requestStop(allowanceMs)`\n * to every currently-running task.\n *\n * `params.allowanceMs` controls the grace window (default 5000 ms).\n */\nexport class TaskStopRunner extends AbstractTask {\n /**\n * Stop tasks must target a concrete instance — without `serviceName` the\n * row would race against any worker on the queue. Layered on top of the\n * envelope built by {@link AbstractTask.resolveParams}.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<object>}\n */\n static async resolveParams(context, overrides = {}) {\n const main = await super.resolveParams(context, overrides);\n if (!main.serviceName) {\n throw new ParamError(\n \"stop/stopRunner requires --serviceName (registry instance name; optional --serviceGroup, --instanceNumber, --serverName to narrow targeting)\"\n );\n }\n return main;\n }\n\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ allowanceMs: number }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-stop\", {\n allowanceMs: \"number default 5000\",\n }, overrides);\n const allowanceMs = Number(merged.allowanceMs);\n if (!Number.isFinite(allowanceMs) || allowanceMs < 0) {\n throw new ParamError(\n `stopRunner: allowanceMs must be a non-negative number (got ${JSON.stringify(merged.allowanceMs)})`\n );\n }\n return { allowanceMs };\n }\n\n /**\n * @returns {Promise<{ success: true, results: { stopRunner: true, allowanceMs: number, message: string } }>}\n */\n async run() {\n const allowanceMs = Number(this.task?.params?.allowanceMs ?? 5000);\n this.context.logger.warn?.(`[TaskStopRunner] stop requested (allowanceMs=${allowanceMs})`);\n return {\n success: true,\n results: {\n stopRunner: true,\n allowanceMs,\n message: \"Runner stop requested\",\n },\n };\n }\n}\n","import { ParamError } from \"../../errors.js\";\nimport { AbstractTask } from \"../AbstractTask.js\";\nimport { readTaskIpcLogsSnapshot } from \"../taskLogs.js\";\n\n/**\n * Fetches IPC FileDatabase rows for `dummyHarvest` (and similar) logs keyed by params.source / params.resource.\n * Intended for machine-targeted enqueue (`server_name` set, `service_group` null) so any runner on that host can execute.\n *\n * Params:\n * - `source` (required) — logical source name, e.g. `\"actris\"`\n * - `resource` (required) — resource slug, e.g. `\"properties\"`\n * - `tail` — max records to return (clamped 1..10000; default 100)\n * - `afterTs` — ISO timestamp watermark; keeps only rows with `ts > afterTs`\n */\nexport class TaskGetLogs extends AbstractTask {\n /**\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<{ source: string, resource: string, tail: number, afterTs?: string }>}\n */\n static async resolveCustomParams(context, overrides = {}) {\n const merged = AbstractTask._mergeTypedParams(context, \"task-get-logs\", {\n source: \"string\",\n resource: \"string\",\n tail: \"number default 100\",\n afterTs: \"string\",\n }, overrides);\n const source = typeof merged.source === \"string\" ? merged.source.trim() : \"\";\n const resource = typeof merged.resource === \"string\" ? merged.resource.trim() : \"\";\n if (!source) throw new ParamError('getLogs: param \"source\" is required');\n if (!resource) throw new ParamError('getLogs: param \"resource\" is required');\n let tail = Number(merged.tail);\n if (!Number.isFinite(tail) || tail < 1) tail = 100;\n tail = Math.min(10_000, Math.max(1, Math.floor(tail)));\n const out = { source, resource, tail };\n if (typeof merged.afterTs === \"string\" && merged.afterTs.trim()) {\n out.afterTs = merged.afterTs.trim();\n }\n return out;\n }\n\n /**\n * @param {unknown} _reportProgress Unused (single-shot read, no progress events).\n * @returns {Promise<{ success: boolean, results: unknown }>}\n */\n async run(_reportProgress) {\n const p = this.task.params ?? {};\n const source = String(p.source ?? \"\").trim();\n const resource = String(p.resource ?? \"\").trim();\n const tail = Math.max(1, Math.min(10_000, Number(p.tail) > 0 ? Number(p.tail) : 100));\n const afterTs = p.afterTs != null && String(p.afterTs).trim() ? String(p.afterTs).trim() : null;\n\n if (!source || !resource) {\n return {\n success: false,\n results: { error: 'getLogs requires params \"source\" and \"resource\"' },\n };\n }\n\n try {\n const { records, latestTs } = await readTaskIpcLogsSnapshot(this.context, {\n source,\n resource,\n tail,\n afterTs,\n });\n return {\n success: true,\n results: { records, latestTs, source, resource },\n };\n } catch (e) {\n return {\n success: false,\n results: { error: e?.message ?? String(e) },\n };\n }\n }\n}\n","import { ParamError } from \"../errors.js\";\nimport { TaskPing } from \"./coreTasks/TaskPing.js\";\nimport { TaskSampleProcess } from \"./coreTasks/TaskSampleProcess.js\";\nimport { TaskShellCommand } from \"./coreTasks/TaskShellCommand.js\";\nimport { TaskSystemInfo } from \"./coreTasks/TaskSystemInfo.js\";\nimport { TaskSumAB } from \"./coreTasks/TaskSumAB.js\";\nimport { TaskStopRunner } from \"./coreTasks/TaskStopRunner.js\";\nimport { TaskGetLogs } from \"./coreTasks/TaskGetLogs.js\";\n\n/**\n * Name → task class map. Runners look up the class by `task.name` when claiming\n * a row. Keep names stable across versions: the DB queue references them as strings.\n *\n * Backward-compat aliases (e.g. `stop` → `TaskStopRunner`, `info` → `TaskSystemInfo`)\n * are seeded by {@link TasksRegistry.withCoreTasks}.\n */\nexport class TasksRegistry {\n /**\n * @param {Record<string, Function>} [initial] Optional seed entries to copy in.\n */\n constructor(initial) {\n this.map = {};\n if (initial) {\n this.addMany(initial);\n }\n }\n\n /**\n * Build a registry pre-populated with every core task plus legacy aliases.\n * Prefer this over `new TasksRegistry()` for anything that wants `ping`/`stop`/etc.\n *\n * @returns {TasksRegistry}\n */\n static withCoreTasks() {\n return new TasksRegistry()\n .add(\"ping\", TaskPing)\n .add(\"sampleProcess\", TaskSampleProcess)\n .add(\"shellCommand\", TaskShellCommand)\n .add(\"systemInfo\", TaskSystemInfo)\n .add(\"info\", TaskSystemInfo)\n .add(\"taskSumAB\", TaskSumAB)\n .add(\"stopRunner\", TaskStopRunner)\n // Backward-compat alias\n .add(\"stop\", TaskStopRunner)\n .add(\"getLogs\", TaskGetLogs);\n }\n\n /**\n * Register a single task class under a name. Overwrites any previous entry.\n *\n * @param {string} taskName\n * @param {Function} taskClass Subclass of `AbstractTask`.\n * @returns {this}\n */\n add(taskName, taskClass) {\n this.map[taskName] = taskClass;\n return this;\n }\n\n /**\n * Bulk-register a name → class map. Later calls override earlier ones.\n *\n * @param {Record<string, Function>} entries\n * @returns {this}\n */\n addMany(entries) {\n for (const [name, klass] of Object.entries(entries)) {\n this.add(name, klass);\n }\n return this;\n }\n\n /**\n * Look up a task class by name. Returns `undefined` when the name is unknown;\n * the runner treats that as \"some other worker may handle this\" and skips.\n *\n * @param {string} taskName\n * @returns {Function | undefined}\n */\n get(taskName) {\n return this.map[taskName];\n }\n\n /**\n * Strict variant of {@link get}: throws {@link ParamError} (with the list\n * of supported names) when `taskName` is unknown. Use from enqueuer code\n * paths where an unknown name is a hard CLI/programmer error.\n *\n * @param {string} taskName\n * @returns {Function}\n */\n requireClass(taskName) {\n const TaskClass = taskName ? this.map[taskName] : undefined;\n if (!TaskClass) {\n const supported = this.listSupportedTasks().join(\", \") || \"(none)\";\n throw new ParamError(\n `Unknown task \"${taskName ?? \"\"}\". Supported on this registry: ${supported}`\n );\n }\n return TaskClass;\n }\n\n /**\n * Dispatcher used by `send-task` / programmatic enqueuers: pick `name`\n * from `overrides` or `context.params`, look up the class, and delegate\n * to its static {@link AbstractTask.resolveParams} with `name` seeded into\n * the overrides. The returned object is shaped for {@link enqueueTask}.\n *\n * Validation failures (unknown task, missing required custom params, etc.)\n * surface as {@link ParamError} so the caller aborts cleanly before any\n * row is inserted.\n *\n * @param {object} context\n * @param {Record<string, unknown>} [overrides]\n * @returns {Promise<object>}\n */\n async resolveTaskParams(context, overrides = {}) {\n const overrideName = typeof overrides.name === \"string\" ? overrides.name.trim() : overrides.name;\n const fromCli = context.params.get(\"name\", \"string\");\n const cliName = typeof fromCli === \"string\" ? fromCli.trim() : fromCli;\n const name = overrideName || cliName;\n if (!name) {\n throw new ParamError(\"Task --name is required (e.g. ping, stop, dummyHarvest)\");\n }\n const TaskClass = this.requireClass(name);\n return TaskClass.resolveParams(context, { ...overrides, name });\n }\n\n /**\n * Names of every registered task, sorted alphabetically (useful for CLI output\n * and allowlist sanity checks).\n *\n * @returns {string[]}\n */\n listSupportedTasks() {\n return Object.keys(this.map).sort();\n }\n\n /**\n * Shallow copy of the internal map, for handing to `addMany` on another registry\n * or for serialization.\n *\n * @returns {Record<string, Function>}\n */\n toObject() {\n return { ...this.map };\n }\n}\n","/**\n * Shared allowlist helpers for task runners: normalize CLI strings, merge with service (control) tasks.\n */\n\n/**\n * Task names every runner should be willing to claim (in addition to `--allowedTasks` or\n * service-group config). These map to core `TasksRegistry.withCoreTasks` handlers.\n */\nexport const SERVICE_TASK_NAMES = [\n \"ping\",\n \"stop\",\n \"stopRunner\",\n \"shellCommand\",\n \"systemInfo\",\n \"info\",\n \"getLogs\",\n];\n\n/**\n * Coerce a caller-supplied allowlist value (CLI string, array, or undefined) into\n * a clean string array, or `undefined` when nothing was provided / only blanks.\n *\n * - `\"a,b\"` → `[\"a\", \"b\"]`\n * - `[\"a \", \"\", \" b\"]` → `[\"a\", \"b\"]`\n * - `\" \"` / `undefined` / `null` → `undefined`\n *\n * @param {unknown} value\n * @returns {string[] | undefined}\n */\nexport function normalizeAllowedTasks(value) {\n if (!value) return undefined;\n if (Array.isArray(value)) {\n const out = value.map((v) => String(v).trim()).filter(Boolean);\n return out.length ? out : undefined;\n }\n const out = String(value)\n .split(\",\")\n .map((v) => v.trim())\n .filter(Boolean);\n return out.length ? out : undefined;\n}\n\n/**\n * Union of `SERVICE_TASK_NAMES` and caller-provided names (deduped, sorted).\n * Use when building the allowlist for `runTasksLoop` / `claimNextRunnableTask`.\n *\n * @param {string[]} [names]\n * @returns {string[]}\n */\nexport function mergeAllowedTasksWithServiceTasks(names) {\n const set = new Set([...SERVICE_TASK_NAMES, ...(names ?? [])]);\n return Array.from(set).sort();\n}\n","import { spawn } from \"node:child_process\";\nimport {\n appendTaskIpcLog,\n flushTaskIpcLogs,\n resolveIpcFileLogsDir,\n} from \"./taskLogs.js\";\n\n/** Upper bound on the `progress` column write; keeps a runaway child from bloating the row. */\nconst MAX_PROGRESS_TEXT_LEN = 4000;\n\n/**\n * Remove empty / non-string entries from a CLI args array.\n *\n * @param {unknown[]} [args]\n * @returns {string[]}\n */\nfunction toCliArgs(args = []) {\n return args.filter((a) => typeof a === \"string\" && a.length > 0);\n}\n\n/**\n * Short prefix for child-side log lines: `<taskName>:<id8>[:<opid>]`.\n *\n * @param {{ name: string, id: string, opid?: string|null }} task\n * @returns {string}\n */\nfunction formatChildLogPrefix(task) {\n return `${task.name}:${task.id.slice(0, 8)}${task.opid ? `:${task.opid}` : \"\"}`;\n}\n\n/**\n * True progress event: must carry `level === \"progress\"` and numeric `count`/`total`.\n * Anything else (debug, info, error, worker-result sentinel...) is treated as a regular log.\n *\n * @param {unknown} payload\n * @returns {boolean}\n */\nfunction isProgressPayload(payload) {\n if (!payload || typeof payload !== \"object\") return false;\n if (payload.level !== \"progress\") return false;\n const count = Number(payload.count);\n const total = Number(payload.total);\n return Number.isFinite(count) && Number.isFinite(total) && total > 0;\n}\n\n/**\n * Render a progress payload as `\"[prefix ][message ]count/total\"` for the DB `progress` column.\n *\n * @param {{ prefix?: string, message?: string, count: number, total: number }} payload\n * @param {string} fallbackPrefix Used when `payload.prefix` is missing.\n * @returns {string}\n */\nfunction formatProgressText(payload, fallbackPrefix) {\n const pfx = payload.prefix ? `${payload.prefix} ` : (fallbackPrefix ? `${fallbackPrefix} ` : \"\");\n const label = typeof payload.message === \"string\" && payload.message ? `${payload.message} ` : \"\";\n return `${pfx}${label}${payload.count}/${payload.total}`;\n}\n\n/**\n * Forward a structured IPC log line from the child to the parent's logger at the\n * matching level. `stdout`/`stderr` go through a different path (raw forwarding);\n * this only handles `{ level, message, ... }` payloads.\n *\n * @param {object} context\n * @param {string} prefix\n * @param {unknown} message\n * @returns {void}\n */\nfunction forwardChildLogToParent(context, prefix, message) {\n if (!message || typeof message !== \"object\") return;\n const text = typeof message.message === \"string\" ? message.message : null;\n if (!text) return;\n const level = typeof message.level === \"string\" ? message.level.toLowerCase() : \"\";\n const line = `[child:${prefix}] ${text}`;\n const logger = context.logger;\n switch (level) {\n case \"error\":\n case \"fatal\":\n logger.error?.(line);\n return;\n case \"warn\":\n case \"warning\":\n logger.warn?.(line);\n return;\n case \"debug\":\n logger.debug?.(line);\n return;\n case \"info\":\n default:\n logger.info?.(line);\n }\n}\n\n/**\n * Build the `node` argv for `spawn`. Workers are plain `.js` (ESM), so we just\n * run them with `node`. Any exec flags the parent was started with (e.g.\n * `--inspect`) are inherited so child workers behave consistently.\n *\n * @param {string} scriptPath\n * @param {string[]} cliArgs\n * @returns {string[]}\n */\nfunction buildNodeArgs(scriptPath, cliArgs) {\n const inheritedExecArgs = Array.isArray(process.execArgv) ? [...process.execArgv] : [];\n return [...inheritedExecArgs, scriptPath, ...cliArgs];\n}\n\n/**\n * Where progress writes land: `context.tasksQueueName` > `params.get(\"table\")` > `\"tasks\"`.\n *\n * @param {object} context\n * @returns {string}\n */\nfunction resolveTasksTableName(context) {\n return context.tasksQueueName || context.params?.get?.(\"table\") || \"tasks\";\n}\n\n/**\n * Announce the IPC-file-logs target once at spawn time. Emits a single info line\n * with the resolved directory and notes when `tasksLogsEnabled=false` keeps logs in-memory only.\n *\n * @param {object} context\n * @param {{ ipcFileLogs?: { basePath?: string, namespace?: string, tableName: string } }} options\n * @returns {void}\n */\nfunction announceIpcFileLogsTarget(context, options) {\n if (!options.ipcFileLogs) return;\n const logsDir = resolveIpcFileLogsDir(context, options.ipcFileLogs);\n const enabledRaw = context.params?.get?.(\"tasksLogsEnabled\");\n const logsEnabled = enabledRaw === undefined ? true : !!enabledRaw;\n context.logger.info?.(\n `[tasks] IPC file logs: ${logsDir}` +\n (logsEnabled ? \"\" : \" (tasksLogsEnabled=false; not persisted)\")\n );\n}\n\n/**\n * Single-consumer FIFO for async work: each `push` runs after the previous one\n * finishes (success or failure). Thrown errors are swallowed by the chain so one\n * bad task never wedges the rest; callers handle errors inside their own `fn`.\n *\n * @returns {{ push: (fn: () => Promise<void>) => Promise<void>, drain: () => Promise<void> }}\n */\nfunction createSerializedQueue() {\n let chain = Promise.resolve();\n return {\n push(fn) {\n chain = chain.then(fn, () => {}).catch(() => {});\n return chain;\n },\n drain() {\n return chain.catch(() => {});\n },\n };\n}\n\n/**\n * Fork `scriptPath` as a Node child with IPC, forward its stdio + IPC logs to the\n * parent logger, update the task row's `progress` column on `{ level: \"progress\" }`\n * payloads, and resolve with a summary once the child closes.\n *\n * Contract:\n * - **Only** IPC payloads matching {@link isProgressPayload} touch the `progress` column.\n * stdout/stderr are diagnostic — accumulated and forwarded to logger but never\n * persisted to `progress`.\n * - The worker returns its final value via `process.send({ __taskWorkerResult: ... })`;\n * that sentinel is captured and exposed as `workerResult`.\n * - DB write and `onProgress` callback for a given payload run sequentially on a shared\n * queue so ordering is preserved across rapid updates.\n *\n * @param {object} context\n * @param {{\n * scriptPath: string,\n * task: { id: string, name: string, opid?: string|null },\n * args?: string[],\n * cwd?: string,\n * onProgress?: (progressText: string) => unknown | Promise<unknown>,\n * onChildIpcMessage?: (message: unknown) => void,\n * ipcFileLogs?: { basePath?: string, namespace?: string, tableName: string },\n * }} options\n * @returns {Promise<{\n * exitCode: number | null,\n * signal: NodeJS.Signals | null,\n * stdout: string,\n * stderr: string,\n * workerResult: unknown,\n * hadErrorMessage: boolean,\n * }>}\n */\nexport async function runNodeTaskScript(context, options) {\n const cliArgs = toCliArgs([\"--route=ipc\", \"--mode=json\", ...(options.args || [])]);\n const nodeArgs = buildNodeArgs(options.scriptPath, cliArgs);\n const child = spawn(process.execPath, nodeArgs, {\n cwd: options.cwd || process.cwd(),\n stdio: [\"ignore\", \"pipe\", \"pipe\", \"ipc\"],\n env: {\n ...process.env,\n TASK_ID: options.task.id,\n TASK_NAME: options.task.name,\n TASK_OPID: options.task.opid || \"\",\n },\n });\n\n announceIpcFileLogsTarget(context, options);\n\n const prefix = formatChildLogPrefix(options.task);\n const tasksTable = resolveTasksTableName(context);\n const progressQueue = createSerializedQueue();\n\n const state = {\n stdout: \"\",\n stderr: \"\",\n workerResult: null,\n hadErrorMessage: false,\n };\n\n /**\n * Serialize a progress update: write to DB, then invoke the optional callback.\n * Each half is independently try/caught so one failure doesn't skip the other.\n *\n * @param {string} text\n */\n const writeProgress = (text) => {\n const trimmed = typeof text === \"string\" ? text.slice(0, MAX_PROGRESS_TEXT_LEN) : \"\";\n if (!trimmed) return;\n progressQueue.push(async () => {\n const db = context.db;\n if (db) {\n try {\n await db(tasksTable).where({ id: options.task.id }).update({ progress: trimmed });\n } catch (error) {\n context.logger.warn?.(\n `[tasks] failed to update progress for task=${options.task.id}: ${error?.message ?? String(error)}`\n );\n }\n }\n if (options.onProgress) {\n try {\n await options.onProgress(trimmed);\n } catch (error) {\n context.logger.warn?.(\n `[tasks] reportProgress callback failed for task=${options.task.id}: ${error?.message ?? String(error)}`\n );\n }\n }\n });\n };\n\n child.stdout?.on(\"data\", (chunk) => {\n const text = String(chunk);\n state.stdout += text;\n if (text.trim()) {\n context.logger.info?.(`[child:${prefix}] ${text.trimEnd()}`);\n }\n });\n child.stderr?.on(\"data\", (chunk) => {\n const text = String(chunk);\n state.stderr += text;\n if (text.trim()) {\n context.logger.warn?.(`[child:${prefix}] ${text.trimEnd()}`);\n }\n });\n\n child.on(\"message\", (message) => {\n if (message && typeof message === \"object\" && \"__taskWorkerResult\" in message) {\n state.workerResult = message.__taskWorkerResult;\n return;\n }\n\n if (message && typeof message === \"object\") {\n const level = typeof message.level === \"string\" ? message.level.toLowerCase() : \"\";\n if (level === \"error\" || level === \"fatal\") {\n state.hadErrorMessage = true;\n }\n }\n\n appendTaskIpcLog(context, options.task, message, options.ipcFileLogs);\n\n try {\n options.onChildIpcMessage?.(message);\n } catch (e) {\n context.logger.warn?.(`[tasks] onChildIpcMessage failed: ${e?.message ?? String(e)}`);\n }\n\n if (isProgressPayload(message)) {\n context.logger.progress(message.message || \"progress\", {\n prefix: message.prefix || prefix,\n count: Number(message.count),\n total: Number(message.total),\n });\n writeProgress(formatProgressText(message, prefix));\n return;\n }\n\n forwardChildLogToParent(context, prefix, message);\n });\n\n return await new Promise((resolve, reject) => {\n child.on(\"error\", (error) => reject(error));\n child.on(\"close\", (exitCode, signal) => {\n void (async () => {\n await flushTaskIpcLogs(context);\n await progressQueue.drain();\n resolve({\n exitCode,\n signal,\n stdout: state.stdout.trim(),\n stderr: state.stderr.trim(),\n workerResult: state.workerResult,\n hadErrorMessage: state.hadErrorMessage,\n });\n })();\n });\n });\n}\n"],"mappings":";AAAA,OAAOA,SAAQ;;;ACiIR,SAAS,kBAAkB,YAAY;AAE1C,QAAM,WAAW;AAEjB,MAAI,CAAC,SAAS,KAAK,UAAU,GAAG;AAC5B,WAAO;AAAA,EACX;AAEA,QAAM,OAAO,IAAI,KAAK,UAAU;AAChC,SAAO,CAAC,MAAM,KAAK,QAAQ,CAAC,KAAK,KAAK,QAAQ,IAAI;AACtD;;;ACpIA,OAA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