UNPKG

akm-cli

Version:

akm (Agent Knowledge Manager) — a portable, local-first capability library for AI agents. Discover, load, share, and improve reusable skills, scripts, workflows, and knowledge across any shell-capable coding agent, including Claude Code, OpenCode, and Cur

194 lines (193 loc) • 7.47 kB
// This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. /** * `akm task run <id>` — what cron / launchd / schtasks invoke at the * scheduled moment. * * The durable boundary is intentional: id/bundle resolution, source read, * strict task source parsing, target resolution, command lowering, workflow * projectability, and frozen script-byte capture (load-task.ts) finish before * dispatch. A failed scheduled activation records its preparation failure; * interactive preparation stays non-mutating. Once prepared, the runner * dispatches the immutable command, workflow, shell, or script projection to its own arm * module (run-command-task.ts, run-workflow-task.ts, run-native-task.ts) and * records that actual attempt. Task source v4 has no document-level disabled * state to skip at fire time — a schedule binding that should not * fire is never installed with the OS scheduler in the first place * (scheduler-sync.ts). * * Orchestration only. * * Resolves `options.provenance` against the default context (run/provenance.ts) * ONCE per call, and threads the result into every dispatch arm. */ import { rethrowIfTestIsolationError } from "../../core/errors.js"; import { releaseLock } from "../../core/file-lock.js"; import { runWorkflowSteps } from "../../workflows/exec/run-workflow.js"; import { nameStepError } from "../prepare/prepare-steps.js"; import { prepareRunV5, runPreparedV5 } from "../v5/run.js"; import { acquireTaskRunLock, recordTaskAttemptFailure, reserveTaskAttempt } from "./attempt-lifecycle.js"; import { loadPreparedTask } from "./load-task.js"; import { resolveProvenanceContext } from "./provenance.js"; import { reconcileAbandonedExecutionState } from "./reconcile-abandoned.js"; import { runPreparedCommandTask } from "./run-command-task.js"; import { runNativeTask } from "./run-native-task.js"; import { resolveResume, runStepsTask } from "./run-steps-task.js"; import { runWorkflowTask } from "./run-workflow-task.js"; import { resolveTaskLogPath } from "./task-log.js"; export async function runTask(id, options) { const runWorkflowStepsImpl = options.runWorkflowStepsImpl ?? runWorkflowSteps; const now = options.now ?? (() => new Date()); const requestedStartedAt = now(); const provenance = resolveProvenanceContext(options.provenance, options.scheduled === true); const recordPreparationFailure = (failure) => { if (provenance.scheduled) { recordTaskAttemptFailure({ taskId: id, reason: "task_load_failed", failure, startedAt: requestedStartedAt, finishedAt: now(), logDir: options.logDir, scheduled: true, ...(options.bundleName ? { bundle: options.bundleName } : {}), }); } }; let task; try { task = await loadPreparedTask(id, options); } catch (failure) { recordPreparationFailure(failure); throw failure; } if (task.kind === "v5") { try { return await runPreparedV5(await prepareRunV5(task, options), options); } catch (failure) { recordPreparationFailure(failure); throw failure; } } try { reconcileAbandonedExecutionState(); } catch (error) { rethrowIfTestIsolationError(error); // Settling abandoned state is housekeeping; it never prevents a run. } // Experimental steps task: a run does not start while the previous one is alive, and `task:` // steps and `--resume` are settled before any attempt is reserved. let resume; let lock; if (task.kind === "steps") { const acquired = acquireTaskRunLock(id, task.taskRef, { skip: options.scheduled === true, at: requestedStartedAt }); if ("skipped" in acquired) return acquired.skipped; lock = acquired; try { for (const step of task.steps) { if (step.kind !== "task") continue; try { await options.taskStepRunner?.check(step.ref); } catch (error) { throw nameStepError(step.name, error); } } if (task.resume) resume = resolveResume(task); } catch (failure) { releaseLock(lock.ownership); recordPreparationFailure(failure); throw failure; } } // A prepared task reserves its execution identity here. Scheduled preparation // failures above have their own failed activation record and never dispatch. let attempt; try { attempt = reserveTaskAttempt(id, requestedStartedAt); } catch (error) { if (lock) releaseLock(lock.ownership); throw error; } const startedAt = attempt.startedAt; const startedIso = startedAt.toISOString(); const logPath = resolveTaskLogPath(options.logDir, id, startedIso); try { if (task.kind === "steps") { return await runStepsTask({ task, logPath, startedAt, now, historyReserved: attempt.historyReserved, provenance, options, ...(resume ? { resume } : {}), }); } if (task.kind === "workflow") { return await runWorkflowTask({ task, logPath, startedAt, now, runWorkflowStepsImpl, historyReserved: attempt.historyReserved, provenance, ...(options.setTimeoutFn ? { setTimeoutFn: options.setTimeoutFn } : {}), ...(options.clearTimeoutFn ? { clearTimeoutFn: options.clearTimeoutFn } : {}), }); } if (task.kind === "command") { return await runPreparedCommandTask({ task, logPath, startedAt, now, historyReserved: attempt.historyReserved, provenance, runAgentImpl: options.runAgentImpl, }); } options.beforeNativeDispatch?.(task); return await runNativeTask({ task, logPath, startedAt, now, historyReserved: attempt.historyReserved, provenance, ...(options.spawnFn ? { spawnFn: options.spawnFn } : {}), ...(options.setTimeoutFn ? { setTimeoutFn: options.setTimeoutFn } : {}), ...(options.clearTimeoutFn ? { clearTimeoutFn: options.clearTimeoutFn } : {}), }); } catch (failure) { recordTaskAttemptFailure({ taskId: id, reason: "task_dispatch_failed", failure, startedAt, finishedAt: now(), logDir: options.logDir, historyReserved: attempt.historyReserved, scheduled: provenance.scheduled, ...(options.bundleName ? { bundle: options.bundleName } : {}), }); throw failure; } finally { if (lock) releaseLock(lock.ownership); } }