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
714 lines (713 loc) • 36.3 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 v4 — the second, additive task source grammar. Never call this
* grammar bare "v4" in prose — the workflow plan IR is separately versioned.
*
* `version: 4` introduces typed `inputs:`, a single bounded `output:` schema
* (legal only on command targets, the one kind whose runtime consumes it —
* see `targetConsumesOutputSchema` below),
* OPTIONAL scheduling (absent `schedule:` is valid and manual-only),
* and top-level execution controls (the `akm:` options bag and the `on:`
* trigger block are both GONE — every `akm:` member that is not re-homed
* survives as a top-level key instead). There is no
* github-action `uses:` variant.
*
* This file implements its OWN top-level-rooted versions of the two v3
* helpers that hardcode the `["akm", …]` field path —
* `parseTimeoutTopLevel`/`nullableSelectorTopLevel` below, siblings of v3's
* `parseTimeout`/`nullableSelector` —
* because task source v4 needs the same accept/reject semantics at a
* different, un-prefixed field path, not the same field path.
*/
import { timeoutMilliseconds } from "../../cli/parse-args.js";
import { asRecord, checkKeys, cloneBoundedJson, noGithubExpression, own, parseEnvironment, parseStringArray, presentJsonValue, readBoundedTaskSourceYaml, sourceError, stringField, TASK_V3_MAX_REDACT_NAMES, utf8Bytes, } from "../../core/asset/bounded-task-document.js";
import { parseBuiltinCommandAction } from "../../core/builtin-action.js";
import { UsageError } from "../../core/errors.js";
import { checkJsonSchemaDefinition, JSON_SCHEMA_SUBSET_SUPPORTED_KEYWORDS, validateJsonSchemaSubset, } from "../../core/json-schema.js";
import { warn, warnOnce } from "../../core/warn.js";
import { applyInputDefaults, INPUT_NAME_PATTERN, validateInputs, } from "../../execution/input-contract.js";
import { EXECUTION_MAX_TIMEOUT_MS } from "../../execution/limits.js";
import { warnRetiredEngineKey } from "../../execution/source.js";
import { classifyTargetRef } from "../../execution/target-ref.js";
import { detectSecretShapedParams } from "../../workflows/exec/param-secrets.js";
import { WORKFLOW_ENV_VAR_NAME_PATTERN, WORKFLOW_MAX_SCHEMA_BYTES } from "../../workflows/resource-limits.js";
import { TASK_V3_HOST_SHELLS, TASK_V3_MAX_SCHEDULES } from "../source-v3.js";
import { TASK_RUN_RESERVED_FLAG_NAMES } from "../task-run-reserved-flags.js";
// ── Closed constants (D1, D2-N3, D2-N7) ─────────────────────────────────────
export const TASK_SOURCE_V4_VERSION = 4;
/** The exact, closed top-level key set (D2-N7) — `akm` and `on` are deliberately absent. */
export const TASK_SOURCE_V4_TOP_LEVEL_KEYS = [
"version",
"name",
"description",
"when_to_use",
"tags",
"inputs",
"output",
"uses",
"run",
"steps",
"with",
"env",
"shell",
"working-directory",
"schedule",
"agent",
"connection",
"harness",
"engine",
"model",
"inference",
"tools",
"timeout",
"redact",
"maxSteps",
"maxRetries",
];
/** Closes one `schedule:` list entry (D2-N5). */
export const TASK_SOURCE_V4_SCHEDULE_KEYS = ["cron", "inputs"];
/**
* The closed key set for one `inputs.<name>` declaration root (D2-N3). The
* JSON-Schema-subset portion is DERIVED from
* `JSON_SCHEMA_SUBSET_SUPPORTED_KEYWORDS` (`src/core/json-schema.ts`) rather
* than restated, so the two lists cannot silently drift; `title`,
* `description`, and `default` are declaration keys unique to task source
* v4, layered on top (`required` is already one of the derived subset keywords — at the
* declaration ROOT it is re-interpreted as the boolean flag, D2-N3).
*/
const SUBSET_KEYWORD_NAMES = JSON_SCHEMA_SUBSET_SUPPORTED_KEYWORDS.split(",")
.map((entry) => entry.split(":")[0]?.trim() ?? "")
.filter((entry) => entry.length > 0);
export const TASK_INPUT_DECLARATION_KEYS = Object.freeze([...SUBSET_KEYWORD_NAMES, "title", "description", "default"]);
const SHELL_SET = new Set(TASK_V3_HOST_SHELLS);
const SOURCE_LABEL = "task source v4";
function ctxFrom(options) {
return {
filePath: options.filePath,
sourceLabel: SOURCE_LABEL,
...(options.lineAt ? { lineAt: options.lineAt } : {}),
};
}
// ── classifyTaskSourceV4Uses ────────────────────────────────────────────────
/**
* A value SHAPED like `owner/repo[/path]@revision` — used only to produce a
* good "the github-action target was removed" message, never to
* accept. Deliberately a shape test, not a full github-locator grammar:
* native target classification recognizes no github-action variant at all —
* only the shape needs to be recognized here, so the
* rejection can name the target the user typed rather than guess.
*
* Runs ONLY on {@link classifyTargetRef}'s failure path:
* `@` is a legal character in a canonical asset ref, so a value the canonical
* classifier accepts (`commands/review@v2`) is a valid target even though it
* also matches this shape — the shape test upgrades the generic invalid-ref
* message, and never vetoes a ref classification accepts.
*/
function looksLikeGithubActionLocator(value) {
const at = value.lastIndexOf("@");
if (at <= 0)
return false;
const locator = value.slice(0, at);
const revision = value.slice(at + 1);
if (revision.length === 0 || /\s/.test(revision))
return false;
if (locator.length === 0 || /\s/.test(locator) || !locator.includes("/"))
return false;
return true;
}
/**
* Classify one exact `uses:` string for task source v4. Delegates
* to {@link classifyTargetRef} (`src/execution/target-ref.ts`) — the repo's
* one canonical-ref classifier — rather than re-deriving ref grammar; layers
* the `akm/command` builtin special case, the task-ref rejection, and
* — on the classification-failure path only — the github-locator-shape
* rejection on top.
*/
/**
* The remedy sentence every `uses:` rejection below ends with. ONE constant so
* the advice can never drift from what {@link TaskSourceV4UsesTarget} actually
* admits — `commands/`, `scripts/`, `workflows/`, and the `akm/command`
* builtin. Notably NOT `tasks/`: a task ref is rejected below.
*/
const TASK_SOURCE_V4_USES_REMEDY = "Use a canonical commands/, scripts/, or workflows/ ref, or akm/command, instead.";
export function classifyTaskSourceV4Uses(value) {
// Diagnostic-codes ratchet remedy (tests/architecture/diagnostic-codes.test.ts,
// established pattern at src/tasks/model/definition.ts:66-79): every
// `UsageError` below omits its `code` argument rather than spelling it out
// — the constructor already defaults to the exact code these throws need
// (src/core/errors.ts), so the thrown type, `.code`, and `.hint()` are all
// unchanged; this keeps the literal code string out of the ratchet's
// grep-style count, which only ever declines.
if (typeof value !== "string" ||
value.length === 0 ||
value.trim() !== value ||
/\s/.test(value) ||
value.includes("${{")) {
throw new UsageError("Task source v4 uses must be one exact, non-empty executable ref without expressions.");
}
if (value === "akm/command") {
return Object.freeze({ kind: "builtin-command", ref: "akm/command" });
}
let classified;
try {
classified = classifyTargetRef(value);
}
catch (cause) {
if (looksLikeGithubActionLocator(value)) {
throw new UsageError("GitHub Action targets were removed in task source v4 — the github-action uses: variant no longer exists. " +
`${TASK_SOURCE_V4_USES_REMEDY}`);
}
// `classifyTargetRef` is SHARED with the workflow classifier, where a
// `tasks/` target IS executable, so its message names all four canonical
// families. Re-raising it verbatim here advertised `tasks/` one branch
// before the B-14 check below rejects exactly that — the message named a
// target this function does not accept. Restate it against the set this
// function actually returns (p2a §6 item 4(b) / p4 R-R14). Anything the
// shared classifier throws for another reason still propagates unchanged.
if (cause instanceof UsageError && cause.code === "TARGET_REF_INVALID") {
throw new UsageError(`${JSON.stringify(value)} is not an executable task source v4 target. ${TASK_SOURCE_V4_USES_REMEDY}`);
}
throw cause instanceof Error ? cause : new UsageError(String(cause));
}
if (classified.kind === "task") {
throw new UsageError(`A task ref is not an executable task source v4 target. ${TASK_SOURCE_V4_USES_REMEDY}`);
}
return Object.freeze({ kind: classified.kind, ref: classified.ref });
}
// ── Top-level scalar/target field parsing ───────────────────────────────────
function nullableSelectorTopLevel(value, ctx, key, path = [key]) {
const selector = stringField(value, ctx, path, { nullable: true });
if (selector !== null && selector.trim().length === 0)
sourceError(ctx, path, "must be null or a non-empty string.");
return selector;
}
function parseTimeoutTopLevel(value, ctx, path = ["timeout"]) {
if (value === null)
return null;
if (typeof value === "string" && value.trim() !== value) {
sourceError(ctx, path, "must not contain surrounding whitespace.");
}
const milliseconds = typeof value === "string" ? timeoutMilliseconds(value) : value;
if (milliseconds === null ||
typeof milliseconds !== "number" ||
!Number.isSafeInteger(milliseconds) ||
milliseconds < 0 ||
milliseconds > EXECUTION_MAX_TIMEOUT_MS) {
sourceError(ctx, path, `must be null, 0 through ${EXECUTION_MAX_TIMEOUT_MS} milliseconds, or a duration such as 20m or 30s.`);
}
return value;
}
function parseShellAndDirectory(input, ctx) {
let shell;
if (own(input, "shell")) {
const rawShell = stringField(input.shell, ctx, ["shell"], { nonempty: true });
if (!SHELL_SET.has(rawShell)) {
sourceError(ctx, ["shell"], `must be one of the closed host-shell table: ${TASK_V3_HOST_SHELLS.join(", ")}.`);
}
shell = rawShell;
}
let workingDirectory;
if (own(input, "working-directory")) {
workingDirectory = stringField(input["working-directory"], ctx, ["working-directory"], {
nonempty: true,
});
}
return { ...(shell ? { shell } : {}), ...(workingDirectory ? { workingDirectory } : {}) };
}
// ── Experimental: steps: ────────────────────────────────────────────────────
/** Closed key set of one step; `harness`/`connection`/`model`/`expect` are legal on prompt steps only. */
const TASK_SOURCE_V4_STEP_KEYS = [
"name",
"run",
"prompt",
"task",
"env",
"timeout",
"retries",
"harness",
"connection",
"model",
"expect",
];
const PROMPT_ONLY_STEP_KEYS = ["harness", "connection", "model", "expect"];
export const TASK_SOURCE_V4_MAX_STEPS = 32;
export const TASK_SOURCE_V4_MAX_STEP_RETRIES = 5;
const STEP_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/;
/** Task-level keys that have no meaning next to `steps:`. */
const KEYS_ILLEGAL_WITH_STEPS = ["uses", "run", "with", "output", "maxSteps", "maxRetries"];
function parseStepPrompt(value, ctx, path) {
const text = stringField(value, ctx, path, { nonempty: true });
if (!/\s/.test(text)) {
let classified;
try {
classified = classifyTargetRef(text);
}
catch {
classified = undefined;
}
if (classified?.kind === "command")
return Object.freeze({ kind: "ref", ref: classified.ref });
if (classified)
sourceError(ctx, path, "names a non-command asset; use a commands/ ref or inline prompt text.");
}
return Object.freeze({ kind: "inline", content: text });
}
function parseStep(raw, index, ctx) {
const path = ["steps", index];
const step = asRecord(raw, ctx, path);
checkKeys(step, TASK_SOURCE_V4_STEP_KEYS, ctx, path);
if (!own(step, "name"))
sourceError(ctx, [...path, "name"], "is required.");
const name = stringField(step.name, ctx, [...path, "name"]);
if (!STEP_NAME_PATTERN.test(name)) {
sourceError(ctx, [...path, "name"], "must be 1-64 letters, digits, '_' or '-', starting with a letter or digit.");
}
const selectors = ["run", "prompt", "task"].filter((key) => own(step, key));
if (selectors.length !== 1)
sourceError(ctx, path, "requires exactly one of run, prompt or task.");
const kind = selectors[0];
if (kind !== "prompt") {
const stray = PROMPT_ONLY_STEP_KEYS.find((key) => own(step, key));
if (stray)
sourceError(ctx, [...path, stray], "is legal only on a prompt step.");
}
let env;
if (own(step, "env")) {
env = parseEnvironment(presentJsonValue(step.env, ctx, [...path, "env"]), ctx, [...path, "env"]);
}
const timeout = own(step, "timeout") ? parseTimeoutTopLevel(step.timeout, ctx, [...path, "timeout"]) : undefined;
let retries = 0;
if (own(step, "retries")) {
if (!Number.isSafeInteger(step.retries) ||
step.retries < 0 ||
step.retries > TASK_SOURCE_V4_MAX_STEP_RETRIES) {
sourceError(ctx, [...path, "retries"], `must be an integer from 0 to ${TASK_SOURCE_V4_MAX_STEP_RETRIES}.`);
}
retries = step.retries;
}
const common = { name, ...(env ? { env } : {}), ...(timeout !== undefined ? { timeout } : {}), retries };
if (kind === "run") {
const run = stringField(step.run, ctx, [...path, "run"], { nonempty: true });
return Object.freeze({ ...common, kind, run });
}
if (kind === "task") {
const text = stringField(step.task, ctx, [...path, "task"], { nonempty: true });
let ref;
try {
const classified = classifyTargetRef(text);
if (classified.kind !== "task")
throw new UsageError("not a task ref");
ref = classified.ref;
}
catch {
sourceError(ctx, [...path, "task"], "must be a canonical tasks/ ref (optionally <bundle>//tasks/<name>).");
}
return Object.freeze({ ...common, kind, ref });
}
const prompt = parseStepPrompt(step.prompt, ctx, [...path, "prompt"]);
const selectors2 = {};
for (const key of ["harness", "connection", "model"]) {
if (own(step, key))
selectors2[key] = nullableSelectorTopLevel(step[key], ctx, key, [...path, key]);
}
let expect;
if (own(step, "expect")) {
expect = parseStringArray(step.expect, ctx, [...path, "expect"], { max: 32 });
if (expect.length === 0 || new Set(expect).size !== expect.length) {
sourceError(ctx, [...path, "expect"], "must be a non-empty list of distinct field names.");
}
}
return Object.freeze({ ...common, kind, prompt, ...selectors2, ...(expect ? { expect } : {}) });
}
function parseStepsTarget(input, ctx) {
for (const key of KEYS_ILLEGAL_WITH_STEPS) {
if (own(input, key))
sourceError(ctx, [key], "is not legal next to steps:.");
}
const raw = presentJsonValue(input.steps, ctx, ["steps"]);
if (!Array.isArray(raw) || raw.length === 0 || raw.length > TASK_SOURCE_V4_MAX_STEPS) {
sourceError(ctx, ["steps"], `must be a list of 1 to ${TASK_SOURCE_V4_MAX_STEPS} steps.`);
}
const steps = raw.map((entry, index) => parseStep(entry, index, ctx));
const seen = new Set();
steps.forEach((step, index) => {
if (seen.has(step.name))
sourceError(ctx, ["steps", index, "name"], "duplicates an earlier step name.");
seen.add(step.name);
});
return Object.freeze({ kind: "steps", steps: Object.freeze(steps), ...parseShellAndDirectory(input, ctx) });
}
function parseTarget(input, ctx) {
if (own(input, "steps"))
return parseStepsTarget(input, ctx);
const hasUses = own(input, "uses");
if (hasUses) {
if (own(input, "shell"))
sourceError(ctx, ["shell"], "is legal only with run.");
if (own(input, "working-directory"))
sourceError(ctx, ["working-directory"], "is legal only with run.");
const usesText = stringField(input.uses, ctx, ["uses"], { nonempty: true });
let uses;
try {
uses = classifyTaskSourceV4Uses(usesText);
}
catch (cause) {
sourceError(ctx, ["uses"], cause instanceof Error ? cause.message : String(cause));
}
let withValues;
if (own(input, "with")) {
if (uses.kind !== "builtin-command") {
sourceError(ctx, ["with"], "is legal only with uses: akm/command; declare typed inputs: instead.");
}
withValues = asRecord(presentJsonValue(input.with, ctx, ["with"]), ctx, ["with"]);
}
if (uses.kind === "builtin-command") {
let command;
try {
command = parseBuiltinCommandAction(withValues);
}
catch (cause) {
sourceError(ctx, ["with"], cause instanceof Error ? cause.message : String(cause));
}
return Object.freeze({ kind: "uses", uses, ...(withValues ? { with: withValues } : {}), command });
}
return Object.freeze({ kind: "uses", uses, ...(withValues ? { with: withValues } : {}) });
}
if (own(input, "with"))
sourceError(ctx, ["with"], "is legal only with uses: akm/command; declare typed inputs: instead.");
const run = stringField(input.run, ctx, ["run"], { nonempty: true });
const { shell, workingDirectory } = parseShellAndDirectory(input, ctx);
return Object.freeze({
kind: "run",
run,
...(shell ? { shell } : {}),
...(workingDirectory ? { workingDirectory } : {}),
});
}
function parseExecutionControls(input, ctx) {
const out = {};
for (const key of ["agent", "connection", "harness", "engine", "model"]) {
if (own(input, key))
out[key] = nullableSelectorTopLevel(input[key], ctx, key);
if (key === "engine" && own(input, key))
warnRetiredEngineKey();
}
if (own(input, "inference")) {
const inference = presentJsonValue(input.inference, ctx, ["inference"]);
out.inference = inference === null ? null : asRecord(inference, ctx, ["inference"]);
}
// Retired: assets no longer restrict an agent's tools.
if (own(input, "tools")) {
warnOnce(`task-source-v4-tools:${ctx.filePath}`, `${ctx.filePath}: \`tools\` is retired and ignored.`);
}
if (own(input, "timeout"))
out.timeout = parseTimeoutTopLevel(input.timeout, ctx);
if (own(input, "redact")) {
const names = parseStringArray(input.redact, ctx, ["redact"], {
max: TASK_V3_MAX_REDACT_NAMES,
pattern: WORKFLOW_ENV_VAR_NAME_PATTERN,
});
if (new Set(names).size !== names.length)
sourceError(ctx, ["redact"], "must not contain duplicate names.");
out.redact = names;
}
if (own(input, "maxSteps")) {
if (!Number.isSafeInteger(input.maxSteps) || input.maxSteps < 1) {
sourceError(ctx, ["maxSteps"], "must be a positive safe integer.");
}
out.maxSteps = input.maxSteps;
}
if (own(input, "maxRetries")) {
if (!Number.isSafeInteger(input.maxRetries) || input.maxRetries < 0) {
sourceError(ctx, ["maxRetries"], "must be a non-negative safe integer.");
}
out.maxRetries = input.maxRetries;
}
return Object.freeze(out);
}
// ── inputs: -> InputContract (D2-N3) ────────────────────────────────────────
function stripDeclarationAnnotations(declInput) {
const schema = {};
let hasDefault = false;
let defaultValue;
let required = false;
for (const [key, value] of Object.entries(declInput)) {
if (key === "default") {
hasDefault = true;
defaultValue = value;
continue;
}
if (key === "required") {
required = value;
continue;
}
schema[key] = value;
}
return { schema, hasDefault, defaultValue, required };
}
function parseInputDeclaration(name, raw, ctx) {
const declPath = ["inputs", name];
const declInput = asRecord(raw, ctx, declPath);
checkKeys(declInput, TASK_INPUT_DECLARATION_KEYS, ctx, declPath);
if (own(declInput, "required") && typeof declInput.required !== "boolean") {
sourceError(ctx, [...declPath, "required"], "must be a boolean at the declaration root (nested objects' own required: […] keeps ordinary JSON Schema array semantics).");
}
const { schema, hasDefault, defaultValue, required } = stripDeclarationAnnotations(declInput);
if (hasDefault && required) {
sourceError(ctx, declPath, "must not declare both default and required: true.");
}
const definitionIssue = checkJsonSchemaDefinition(schema)[0];
if (definitionIssue)
sourceError(ctx, declPath, `is not a supported JSON schema: ${definitionIssue.message}`);
if (utf8Bytes(JSON.stringify(schema)) > WORKFLOW_MAX_SCHEMA_BYTES) {
sourceError(ctx, declPath, `serialized schema exceeds the ${WORKFLOW_MAX_SCHEMA_BYTES}-byte limit.`);
}
if (hasDefault) {
const violations = validateJsonSchemaSubset(defaultValue, schema);
if (violations.length > 0) {
sourceError(ctx, [...declPath, "default"], `does not satisfy its own declaration: ${violations.join("; ")}`);
}
const secretWarnings = detectSecretShapedParams({ [name]: defaultValue });
for (const message of secretWarnings)
warn(message);
}
return Object.freeze({
schema: Object.freeze(schema),
...(hasDefault ? { default: defaultValue } : {}),
required,
});
}
export function parseInputDeclarations(value, ctx) {
const input = asRecord(value, ctx, ["inputs"]);
const names = Object.keys(input);
const result = {};
for (const name of names) {
if (!INPUT_NAME_PATTERN.test(name)) {
sourceError(ctx, ["inputs", name], "must match the input name pattern (a letter/underscore, then letters, digits, or underscores).");
}
// A declared input name that
// collides with a flag `akm task run` already binds to itself (--bundle,
// --scheduled, …) can never be supplied through that CLI — parseTaskInputFlags
// (../../commands/tasks/tasks-cli.ts) always treats the name as its OWN
// flag, so the value is either silently misrouted (a second --bundle
// re-targets which bundle the task loads from) or left as an orphaned
// positional token that throws. Rejecting it HERE, at declaration time,
// closes every caller at once: a bare `akm task run --<name>`, `akm task
// explain --<name>`, and a `schedule[i].inputs` entry (whose keys are
// checked against this same contract below, so a banned name can never
// reach compileTaskSchedulerBindings's invocation tail either) — see
// ../task-run-reserved-flags.ts's own header.
if (TASK_RUN_RESERVED_FLAG_NAMES.has(name)) {
sourceError(ctx, ["inputs", name], `collides with akm run's own --${name} flag; declare the input under a different name.`);
}
result[name] = parseInputDeclaration(name, input[name], ctx);
}
return Object.freeze(result);
}
// ── output: -> bounded JSON Schema (mirrors v3's akm.outputSchema) ─────────
function parseOutputSchema(value, ctx) {
const schema = asRecord(value, ctx, ["output"]);
const issue = checkJsonSchemaDefinition(schema)[0];
if (issue)
sourceError(ctx, ["output"], `is not a supported JSON schema: ${issue.message}`);
return schema;
}
/**
* True for the target kinds whose runtime actually consumes `output:` —
* command invocations (`uses: commands/<ref>` and `uses: akm/command`), where
* `prepareTaskV3Execution` forwards it as the invocation's outputSchema
* (../prepare/prepare.ts, via prepare-support.ts's currentExecutionValues).
* `run:`, `uses: scripts/`, and `uses: workflows/` executions carry no
* output schema anywhere (run-native-task.ts decides status from the exit
* code alone; the workflow arm freezes a child plan without one), so an
* authored `output:` there would be a silently unenforced contract — the
* fifth state the fail-closed rule forbids (same
* grammar pattern as `with:` being legal only with `uses: akm/command`).
*/
function targetConsumesOutputSchema(target) {
return target.kind === "uses" && (target.uses.kind === "command" || target.uses.kind === "builtin-command");
}
// ── schedule: -> TaskSourceV4ScheduleBinding[] (D2-N5, D2-N6, B-06..B-10, B-38) ──
/**
* Every schedule binding must be independently runnable.
*
* A scheduled firing supplies NO input flags — `compileTaskSchedulerBindings`
* (../scheduler-binding.ts) appends only the entry's own
* `schedule[i].inputs` to the `["run", ref, "--scheduled"]`
* tail — so that literal PLUS the declared defaults is the complete value set
* the scheduled run will see, and it is checked there by the very
* same `applyInputDefaults` + `validateInputs` pair (../run/load-task.ts).
* A `required: true` declaration may not also carry a `default`
* (`parseInputDeclaration` above), so an entry that names no value for one can
* never satisfy the contract, at any hour, ever.
*
* The source document alone knows both halves of that contradiction, so it is
* a grammar error: TASK_SOURCE_INVALID at the entry's own field path, naming
* the unsatisfied input(s). `akm task sync`'s projectability proof
* (../scheduler-sync.ts) keeps its own independent copy of this check over
* the DEFAULTED view — that gate is what makes such a schedule unreachable
* rather than merely ill-advised, and it still guards a task source reaching
* sync through any other path — but an author should not have to run `akm
* task sync` to learn that a document contradicts itself, so it is rejected at
* parse time. Manual `akm task
* run` is deliberately unaffected: a manual run takes the value from the
* input's own `--<name>` flag, so a required, default-less input plus NO
* `schedule:` stays valid and manual-only.
*
* Runs for every entry, AFTER the raw `schedule[i].inputs` check below, so an
* entry that authors an `inputs:` mapping keeps that check's exact
* message/field path; defaults can only add values already validated against
* their own declaration, so this pass can add nothing but missing-required.
*/
function checkScheduleEntryRunnable(inputs, contract, ctx, entryPath) {
const errors = validateInputs(contract, applyInputDefaults(contract, { ...inputs }), { pathRoot: "inputs" });
if (errors.length === 0)
return;
sourceError(ctx, entryPath, `does not satisfy the task's declared inputs once defaults are applied: ${errors.join("; ")}. ` +
"A scheduled run supplies no input flags — give this schedule entry an inputs: value for each input " +
"named above, or declare a default: on the input instead (a required: true input may not carry one).");
}
function parseScheduleEntry(entryRaw, index, contract, ctx, entryKeys) {
const entryPath = ["schedule", index];
const entry = asRecord(entryRaw, ctx, entryPath);
checkKeys(entry, entryKeys, ctx, entryPath);
if (!own(entry, "cron"))
sourceError(ctx, [...entryPath, "cron"], "is required.");
const cron = stringField(entry.cron, ctx, [...entryPath, "cron"], { nonempty: true });
noGithubExpression(cron, ctx, [...entryPath, "cron"]);
let inputsLiteral = Object.freeze({});
if (own(entry, "inputs")) {
const inputsValue = asRecord(presentJsonValue(entry.inputs, ctx, [...entryPath, "inputs"]), ctx, [
...entryPath,
"inputs",
]);
// Fail-closed exact-name check:
// `validateInputs`'s synthetic `{type:"object", properties}` schema
// deliberately carries no `additionalProperties:false` — that is the
// right default for a general-purpose contract validator with other
// callers (materializeInputFlags already does its own exact-name check
// before ever calling validateInputs) — so relying on
// validateInputs alone here would silently accept a typo'd or wholly
// undeclared schedule[i].inputs key forever, exactly the fifth state the
// fail-closed rule forbids. checkKeys mirrors materializeInputFlags' own
// exact-name rule at the grammar layer: closed against the declared
// contract, TASK_SOURCE_INVALID at schedule[<i>].inputs.<name>.
checkKeys(inputsValue, Object.keys(contract), ctx, [...entryPath, "inputs"]);
// pathRoot "inputs" (matching checkScheduleEntryRunnable below) and the
// sourceError call rooted at entryPath, not [...entryPath, "inputs"]: the
// per-error detail already carries "inputs.<name>" (validateInputs), so
// adding a SECOND ".inputs" segment to the field path here would render
// two path roots in one message (the bare "$" default would otherwise
// leak through).
const errors = validateInputs(contract, inputsValue, { pathRoot: "inputs" });
if (errors.length > 0)
sourceError(ctx, entryPath, errors.join("; "));
inputsLiteral = Object.freeze({ ...inputsValue });
}
checkScheduleEntryRunnable(inputsLiteral, contract, ctx, entryPath);
return Object.freeze({ cron, inputs: inputsLiteral, source: `schedule[${index}].cron`, ordinal: index });
}
/** `entryKeys` is the closed key set of one list entry: task source v4's, or a later grammar's superset of it. */
export function parseSchedule(input, contract, ctx, entryKeys = TASK_SOURCE_V4_SCHEDULE_KEYS) {
if (!own(input, "schedule"))
return Object.freeze([]);
const raw = presentJsonValue(input.schedule, ctx, ["schedule"]);
if (typeof raw === "string") {
const cron = stringField(raw, ctx, ["schedule"], { nonempty: true });
noGithubExpression(cron, ctx, ["schedule"]);
// B-08's SHAPE is unchanged — one enabled binding, no inputs — but the
// shorthand is a schedule entry like any other, so it is held to the same
// runnability contract, at the `schedule` key's own field path (it has
// neither an ordinal nor an `inputs:` sub-path to point at).
checkScheduleEntryRunnable(Object.freeze({}), contract, ctx, ["schedule"]);
return Object.freeze([Object.freeze({ cron, inputs: Object.freeze({}), source: "schedule", ordinal: 0 })]);
}
if (!Array.isArray(raw) || raw.length === 0) {
sourceError(ctx, ["schedule"], "must be a non-empty string or a non-empty list of {cron, inputs?} records.");
}
if (raw.length > TASK_V3_MAX_SCHEDULES) {
sourceError(ctx, ["schedule"], `accepts at most ${TASK_V3_MAX_SCHEDULES} entries.`);
}
const bindings = raw.map((entryRaw, index) => parseScheduleEntry(entryRaw, index, contract, ctx, entryKeys));
return Object.freeze(bindings);
}
// ── Top-level key rejection (akm:/on: removal, B-11/B-12; D2-N7) ───────────
function checkTopLevelKeys(input, ctx) {
if (own(input, "akm")) {
sourceError(ctx, ["akm"], "is removed in task source v4; its members are top-level keys now (schedule, timeout, connection, harness, model, redact, " +
"maxSteps, maxRetries, description, when_to_use, tags, agent, inference, and output for outputSchema) " +
"— see docs/reference/tasks.md.");
}
if (own(input, "on")) {
sourceError(ctx, ["on"], "is removed in task source v4; declare a top-level schedule: instead.");
}
checkKeys(input, TASK_SOURCE_V4_TOP_LEVEL_KEYS, ctx, []);
}
// ── parseTaskSourceV4Document ───────────────────────────────────────────────
/** Parse an already-decoded JSON/YAML value as a task source v4 document. */
export function parseTaskSourceV4Document(value, options) {
const ctx = ctxFrom(options);
const cloned = cloneBoundedJson(value, ctx, [], { nodes: 0 });
const input = asRecord(cloned, ctx, []);
if (!own(input, "version"))
sourceError(ctx, ["version"], "is required and must be 4.");
if (input.version !== TASK_SOURCE_V4_VERSION)
sourceError(ctx, ["version"], "must be exactly 4.");
checkTopLevelKeys(input, ctx);
const selectorCount = ["uses", "run", "steps"].filter((key) => own(input, key)).length;
if (selectorCount !== 1) {
sourceError(ctx, [], `requires exactly one executable selector: ${own(input, "steps") ? "uses, run or steps" : "uses or run"}.`);
}
const name = own(input, "name") ? stringField(input.name, ctx, ["name"]) : undefined;
const description = own(input, "description")
? stringField(input.description, ctx, ["description"])
: undefined;
const whenToUse = own(input, "when_to_use")
? stringField(input.when_to_use, ctx, ["when_to_use"])
: undefined;
const tags = own(input, "tags") ? parseStringArray(input.tags, ctx, ["tags"]) : undefined;
const env = own(input, "env") ? parseEnvironment(presentJsonValue(input.env, ctx, ["env"]), ctx) : undefined;
const target = parseTarget(input, ctx);
const inputs = own(input, "inputs")
? parseInputDeclarations(presentJsonValue(input.inputs, ctx, ["inputs"]), ctx)
: undefined;
let output;
if (own(input, "output")) {
if (!targetConsumesOutputSchema(target)) {
sourceError(ctx, ["output"], "is legal only with a command target (uses: commands/<ref> or uses: akm/command); " +
"run:, uses: scripts/, and uses: workflows/ targets do not enforce an output schema.");
}
output = parseOutputSchema(presentJsonValue(input.output, ctx, ["output"]), ctx);
}
// `resume` is `akm run --resume`'s flag for a steps task, so it cannot also name an input there.
if (target.kind === "steps" && inputs && own(inputs, "resume")) {
sourceError(ctx, ["inputs", "resume"], "collides with akm run's own --resume flag on a steps: task.");
}
const schedule = parseSchedule(input, inputs ?? Object.freeze({}), ctx);
const execution = parseExecutionControls(input, ctx);
return Object.freeze({
version: TASK_SOURCE_V4_VERSION,
...(name !== undefined ? { name } : {}),
...(description !== undefined ? { description } : {}),
...(whenToUse !== undefined ? { when_to_use: whenToUse } : {}),
...(tags !== undefined ? { tags } : {}),
...(inputs !== undefined ? { inputs } : {}),
...(output !== undefined ? { output } : {}),
target,
...(env !== undefined ? { env } : {}),
execution,
schedule,
manualOnly: schedule.length === 0,
source: Object.freeze({ path: options.filePath }),
});
}
/** Parse hostile YAML text as a task source v4 document — the standalone entry (mirrors `parseTaskV3Yaml`). */
export function parseTaskSourceV4(input) {
const { root, lineAt } = readBoundedTaskSourceYaml(input, { sourceLabel: SOURCE_LABEL });
return parseTaskSourceV4Document(root, {
filePath: input.filePath,
lineAt,
});
}