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
387 lines (386 loc) • 18.4 kB
JavaScript
// 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/.
/**
* Task source version 5: the reader for one task file. Every rule of the design's
* "Checked before anything runs" that needs nothing but the file itself is here,
* so `akm lint`, an import, the index and `checkTaskV5` (`./check.ts`, which adds
* what needs other files) all read a file through this one function.
*
* A build reads only its own `5-beta.<n>` (`TASK_SOURCE_V5_BETA`); `n` goes up
* only when the syntax changes.
*/
import { parseTimeoutValue } from "../../cli/parse-args.js";
import { bundleRefToString, parseBundleRef } from "../../core/asset/asset-ref.js";
import { asRecord, cloneBoundedJson, own, parseStringArray, sourceError, stringField, } from "../../core/asset/bounded-task-document.js";
import { isSecretRefToken, parseSecretSpec, REF_TOKEN_RE } from "../../core/ref-token.js";
import { warnOnce } from "../../core/warn.js";
import { WORKFLOW_ENV_VAR_NAME_PATTERN } from "../../workflows/resource-limits.js";
import { parseInputDeclarations, parseSchedule } from "../source/task-source-v4.js";
/** The one beta this build reads. */
export const TASK_SOURCE_V5_BETA = 1;
export const TASK_SOURCE_V5_VERSION = `5-beta.${TASK_SOURCE_V5_BETA}`;
/** Any `5-beta.<n>`: the router sends these here, and `parseTaskSourceV5Document` refuses all but this build's own. */
export const TASK_SOURCE_V5_VERSION_PATTERN = /^5-beta\.\d+$/;
const SOURCE_LABEL = "task source v5";
/** The name of the one step of a task that is one command: it adds no entry to the state. */
export const TASK_V5_SINGLE_STEP = "";
/** A step's name as a person reads it: a task that is one command has the one step "(only)". */
export const taskV5StepLabel = (name) => (name === TASK_V5_SINGLE_STEP ? "(only)" : name);
/** The flags a start of a version 5 task takes, beside those in `TASK_RUN_RESERVED_FLAG_NAMES`, that can be an input's name: an input may not be named like one. */
const START_FLAG_NAMES = ["env", "secret", "cwd", "timeout", "resume", "inherit"];
const STEP_NAME = /^[A-Za-z0-9_-]+$/;
const FLAG = /^--([A-Za-z_][A-Za-z0-9_-]*)$/;
const WHOLE_TOKEN = /^\$\{([^}\s]+)\}$/;
const COMMAND_KEYS = ["run", "exec", "prompt", "args", "arguments", "expect"];
/** Options of a step; at the top of a task each is the default for every step. */
const OPTION_KEYS = [
"env",
"secret",
"inherit",
"cwd",
"timeout",
"max-retries",
"harness",
"connection",
"model",
];
const TASK_KEYS = [
"version",
"name",
"description",
"when_to_use",
"tags",
"schedule",
"inputs",
"steps",
"lock",
...OPTION_KEYS,
];
const STEP_KEYS = ["name", ...COMMAND_KEYS, ...OPTION_KEYS];
const SCHEDULE_ENTRY_KEYS = ["cron", "inputs", "env", "secret", "timeout"];
const FAMILY_KIND = { tasks: "tasks", commands: "commands", scripts: "scripts", agents: "agents" };
/** The key a step is written with, which the run's history and `doctor explain` show: `run:`, `exec:` or `prompt:`. */
export function taskV5StepKey(step) {
if (step.run !== undefined)
return "run";
return step.kind === "exec" ? "exec" : "prompt";
}
const isMap = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
/** The kind of asset a `run:` value names, or undefined when it is no ref to a task, prompt asset, script or agent. */
export function taskV5RefKind(ref) {
let parsed;
try {
parsed = parseBundleRef(ref);
}
catch {
return undefined;
}
if (parsed.fragment !== undefined || bundleRefToString(parsed) !== ref)
return undefined;
const slash = parsed.conceptId.indexOf("/");
if (slash < 0 || slash === parsed.conceptId.length - 1)
return undefined;
const family = parsed.conceptId.slice(0, slash);
return Object.hasOwn(FAMILY_KIND, family) ? FAMILY_KIND[family] : undefined;
}
const isSecretRef = (body) => isSecretRefToken(`\${${body}}`);
/**
* The shape of `env:`, `secret:`, `inherit:`, `cwd:`, `timeout:`, `max-retries:` and the agent selectors, on a task, a
* step, a schedule entry or the flags of a start. `keys` are the ones this place takes. A task file names a secret only
* by its ref. What the refs name is `checkTaskV5`'s.
*/
export function readTaskV5Options(source, keys, base, stateNames, fail) {
const out = {};
for (const key of keys) {
if (!own(source, key))
continue;
const value = source[key];
const at = [...base, key];
if (key === "env" || key === "secret" || key === "inherit") {
if (!Array.isArray(value))
fail(at, "is a list.");
}
else if (key === "cwd" || key === "harness" || key === "connection" || key === "model") {
if (typeof value !== "string")
fail(at, "is text.");
}
switch (key) {
case "env":
for (const [index, entry] of value.entries())
readEnvEntry(entry, [...at, index], stateNames, fail);
break;
case "secret":
for (const [index, entry] of value.entries()) {
if (typeof entry !== "string" || parseSecretSpec(entry) === undefined)
fail([...at, index], "is not <secret ref>:<VAR>.");
}
break;
case "inherit":
for (const [index, name] of value.entries()) {
if (typeof name !== "string" || !WORKFLOW_ENV_VAR_NAME_PATTERN.test(name))
fail([...at, index], "is not a variable name.");
}
break;
case "cwd":
for (const [, body] of value.matchAll(REF_TOKEN_RE)) {
if (stateNames.has(body))
fail(at, "does not take a state entry; a state entry does not go into cwd:.");
}
break;
case "timeout":
try {
parseTimeoutValue(String(value), `${key}:`);
}
catch (cause) {
fail(at, `is not a duration. ${cause instanceof Error ? cause.message : String(cause)}`);
}
break;
case "max-retries":
if (typeof value !== "number" || !Number.isInteger(value) || value < 0)
fail(at, "takes a whole number, 0 or more.");
break;
}
out[key] = value;
}
return out;
}
function readEnvEntry(entry, at, stateNames, fail) {
if (typeof entry === "string") {
if (isSecretRef(entry))
fail(at, `names a secret; a secret goes under secret:, not env:.`);
return;
}
if (!isMap(entry))
fail(at, "is an env asset's ref or NAME: value.");
for (const [name, value] of Object.entries(entry)) {
if (!["string", "number", "boolean"].includes(typeof value))
fail([...at, name], "is text.");
if (!WORKFLOW_ENV_VAR_NAME_PATTERN.test(name))
fail([...at, name], `${name} is not a variable name.`);
for (const [, body] of String(value).matchAll(REF_TOKEN_RE)) {
if (isSecretRef(body))
fail([...at, name], "holds a secret ref; a secret goes under secret:, not here.");
if (stateNames.has(body))
fail([...at, name], "does not take a state entry; a state entry does not go into env:.");
}
}
}
/** Parse an already-decoded YAML value as a task source version 5 document. */
export function parseTaskSourceV5Document(value, options) {
const ctx = {
filePath: options.filePath,
sourceLabel: SOURCE_LABEL,
...(options.lineAt ? { lineAt: options.lineAt } : {}),
};
const fail = (path, detail) => sourceError(ctx, path, detail);
const root = asRecord(cloneBoundedJson(value, ctx, [], { nodes: 0 }), ctx, []);
const label = options.filePath;
const ignoreKeys = (source, allowed, where) => {
for (const key of Object.keys(source)) {
if (!allowed.includes(key))
warnOnce(`${where}: ${key}`, `${where}: ${key}: is not a key taken here; ignored`);
}
};
if (root.version !== TASK_SOURCE_V5_VERSION)
fail(["version"], `must be exactly "${TASK_SOURCE_V5_VERSION}".`);
let inputs;
if (own(root, "inputs")) {
inputs = parseInputDeclarations(root.inputs, ctx);
for (const name of Object.keys(inputs)) {
if (START_FLAG_NAMES.includes(name)) {
fail(["inputs", name], `collides with akm run's own --${name} flag; declare the input under a different name.`);
}
}
}
if (own(root, "lock") && typeof root.lock !== "boolean")
fail(["lock"], "is true or false.");
if (own(root, "steps") && !(Array.isArray(root.steps) && root.steps.length > 0 && root.steps.every(isMap))) {
fail(["steps"], "is a list of steps, and has one.");
}
const single = !own(root, "steps");
if (!single && (own(root, "run") || own(root, "exec") || own(root, "prompt"))) {
fail([], "is one command or steps:, not both.");
}
const rawSteps = single ? [root] : root.steps;
const declaredNames = Object.keys(inputs ?? {});
const stateNames = new Set([
...declaredNames,
...(single ? [] : rawSteps.map((step) => step.name).filter((name) => typeof name === "string")),
]);
const taskOptions = readTaskV5Options(root, OPTION_KEYS, [], stateNames, fail);
ignoreKeys(root, single ? [...TASK_KEYS, ...COMMAND_KEYS] : TASK_KEYS, label);
const steps = [];
const seen = [];
for (const [index, raw] of rawSteps.entries()) {
const base = single ? [] : ["steps", index];
if (!single && (typeof raw.name !== "string" || !STEP_NAME.test(raw.name))) {
fail([...base, "name"], "is needed: letters, digits, _ and - only.");
}
const name = single ? TASK_V5_SINGLE_STEP : raw.name;
const where = single ? label : `${label} step ${name}`;
if (!single)
ignoreKeys(raw, STEP_KEYS, where);
if (seen.includes(name))
fail([...base, "name"], `has two steps named ${name}.`);
if (declaredNames.includes(name))
fail([...base, "name"], `${name} has the name of an input.`);
const known = (state) => declaredNames.includes(state) || seen.includes(state);
for (const key of ["run", "exec", "prompt", "arguments"]) {
if (own(raw, key) && typeof raw[key] !== "string")
fail([...base, key], "is text.");
}
if (own(raw, "expect") && !(Array.isArray(raw.expect) && raw.expect.every((key) => typeof key === "string"))) {
fail([...base, "expect"], "is a list of keys.");
}
const kind = commandKind(raw, base, fail);
const options = single ? {} : readTaskV5Options(raw, OPTION_KEYS, base, stateNames, fail);
for (const key of ["harness", "connection", "model"]) {
const given = single ? taskOptions[key] : options[key];
if (given !== undefined && kind !== "agents" && kind !== "commands" && kind !== "prompt") {
warnOnce(`${where}: ${key}`, `${where}: ${key}: is for a step an agent runs; ignored`);
}
}
const args = own(raw, "args") ? raw.args : undefined;
if (args !== undefined && kind !== "tasks" && kind !== "scripts")
fail([...base, "args"], "are the arguments of a task or a script.");
if (args !== undefined && !Array.isArray(args))
fail([...base, "args"], "is a list.");
for (const [position, item] of (args ?? []).entries())
checkArg(item, known, [...base, "args", position], fail);
if (own(raw, "arguments") && kind !== "commands")
fail([...base, "arguments"], "is for a prompt asset.");
if (own(raw, "expect") && (kind === "exec" || kind === "scripts" || kind === "tasks")) {
fail([...base, "expect"], "checks an agent's reply.");
}
if (own(raw, "exec"))
checkExec(raw.exec, stateNames, [...base, "exec"], fail);
if (own(raw, "prompt"))
checkText(raw.prompt, known, [...base, "prompt"], fail);
if (own(raw, "arguments"))
checkText(raw.arguments, known, [...base, "arguments"], fail);
const taskInputs = kind === "tasks" ? inputFlags((args ?? []), [...base, "args"], fail) : undefined;
steps.push({
name,
kind,
...(own(raw, "run") ? { run: raw.run } : {}),
...(own(raw, "exec") ? { exec: raw.exec } : {}),
...(own(raw, "prompt") ? { prompt: raw.prompt } : {}),
...(args !== undefined ? { args: args } : {}),
...(own(raw, "arguments") ? { arguments: raw.arguments } : {}),
...(own(raw, "expect") ? { expect: raw.expect } : {}),
...(taskInputs ? { taskInputs } : {}),
options,
});
seen.push(name);
}
const bindings = parseSchedule(root, inputs ?? Object.freeze({}), ctx, SCHEDULE_ENTRY_KEYS);
const rawSchedule = Array.isArray(root.schedule) ? root.schedule : [];
const schedule = bindings.map((binding, index) => {
const entry = rawSchedule[index];
// An entry's env: is what --env is to a typed start, and --env takes an env asset's ref.
if (entry !== undefined && Array.isArray(entry.env)) {
for (const [at, item] of entry.env.entries()) {
if (typeof item !== "string")
fail(["schedule", index, "env", at], "is an env asset's ref, as --env takes.");
}
}
const extras = entry === undefined
? {}
: readTaskV5Options(entry, ["env", "secret", "timeout"], ["schedule", index], new Set(), fail);
return Object.freeze({ ...binding, ...extras });
});
const text = (key) => own(root, key) ? stringField(root[key], ctx, [key]) : undefined;
const name = text("name");
const description = text("description");
const whenToUse = text("when_to_use");
const tags = own(root, "tags") ? parseStringArray(root.tags, ctx, ["tags"]) : undefined;
return Object.freeze({
version: TASK_SOURCE_V5_VERSION,
...(name !== undefined ? { name } : {}),
...(description !== undefined ? { description } : {}),
...(whenToUse !== undefined ? { when_to_use: whenToUse } : {}),
...(tags !== undefined ? { tags } : {}),
...(inputs !== undefined ? { inputs } : {}),
...(own(root, "lock") ? { lock: root.lock } : {}),
options: taskOptions,
single,
steps: Object.freeze(steps),
schedule: Object.freeze(schedule),
source: Object.freeze({ path: options.filePath }),
});
}
/** A step is exactly one command. Returns what it runs. */
function commandKind(step, base, fail) {
if (own(step, "run")) {
const kind = taskV5RefKind(step.run);
if (!kind)
fail([...base, "run"], "takes a ref; use exec: for anything else.");
if (own(step, "exec"))
fail(base, "is one command.");
if (own(step, "prompt") !== (kind === "agents")) {
fail([...base, "prompt"], "goes with run: only when run: names an agent, and an agent needs one.");
}
return kind;
}
if (own(step, "exec")) {
if (own(step, "prompt"))
fail(base, "is one command.");
return "exec";
}
if (own(step, "prompt"))
return "prompt";
return fail(base, "is one command: run:, exec: or prompt:.");
}
/** The `${...}` tokens in `text` that are neither an input nor an earlier step, in order; a secret ref is refused on its own. */
export function unknownReferences(text, known) {
return [...text.matchAll(REF_TOKEN_RE)]
.map((match) => match[1])
.filter((body) => !isSecretRef(body) && !known(body));
}
/** `prompt:` and `arguments:`: `${name}` is an input or an earlier step. A secret is never named here. */
function checkText(text, known, at, fail) {
for (const [, body] of text.matchAll(REF_TOKEN_RE)) {
if (isSecretRef(body))
fail(at, "holds a secret ref; a secret goes under secret:, never into a prompt.");
}
const [unknown] = unknownReferences(text, known);
if (unknown !== undefined)
fail(at, `\${${unknown}} is not an input or an earlier step.`);
}
/** An `args:` item is one argument: text or a number as written, or one state entry as the whole item. */
function checkArg(value, known, at, fail) {
if (value === null || typeof value === "object")
fail(at, "is one argument.");
if (typeof value !== "string" || !value.includes("${"))
return;
const whole = WHOLE_TOKEN.exec(value)?.[1];
for (const [, body] of value.matchAll(REF_TOKEN_RE)) {
if (isSecretRef(body))
fail(at, "holds a secret ref; a secret goes under secret:, and is not an input.");
}
if (whole === undefined)
fail(at, "must be a whole item: ${name} is one argument.");
if (!known(whole))
fail(at, `\${${whole}} is not an input or an earlier step, and no shell expands it here.`);
}
/** An `exec:` command is run as written. A state entry is never put into it. */
function checkExec(command, stateNames, at, fail) {
for (const [, body] of command.matchAll(REF_TOKEN_RE)) {
if (isSecretRef(body))
fail(at, "holds a secret ref; a secret goes under secret:, not into a command.");
if (stateNames.has(body))
fail(at, `does not put \${${body}} into a command; the command reads the state file.`);
}
}
/** A task's `args:` are its input flags as on the command line: `--<name> <value>`, in pairs. */
function inputFlags(args, at, fail) {
const flags = [];
for (let i = 0; i < args.length; i += 2) {
const name = typeof args[i] === "string" ? FLAG.exec(args[i])?.[1] : undefined;
if (name === undefined || i + 1 === args.length)
fail(at, "of a task are --<name> <value> pairs.");
flags.push({ name, value: args[i + 1] });
}
return flags;
}