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
76 lines (75 loc) • 3.74 kB
JavaScript
/**
* The one child-workflow resolver (`unit: { workflow: <ref> }`, Experimental).
* It resolves the child, refuses a composition cycle, freezes the child
* completely through the injected `ResolutionContext.freezeChild`, binds the
* step's `with:` against the child's `params:`, and embeds the frozen child
* plan in the target.
*/
import { createHash } from "node:crypto";
import { UsageError } from "../../../core/errors.js";
import { workflowParamContract } from "../../ir/params.js";
import { canonicalJson, canonicalPlanJson } from "../../ir/plan-hash.js";
import { loadWorkflowAsset } from "../../runtime/workflow-asset-loader.js";
import { declaredParamNames, earlierStepIds, } from "../step-values.js";
import { freezeTaskInputBindings } from "../task-bindings.js";
/** The child target's own content identity: its ref, embedded plan hash, and bindings. */
function childWorkflowContentHash(fields) {
return createHash("sha256")
.update("akm.workflow.child-workflow\0v2\0")
.update(canonicalJson({
ref: fields.ref,
planHash: fields.planHash,
inputBindings: fields.inputBindings.length > 0 ? fields.inputBindings : null,
}))
.digest("hex");
}
function compositionPath(refPath, childRef) {
return [...refPath, childRef].join(" -> ");
}
function assertNoCompositionCycle(stepId, childRef, refPath) {
if (!refPath.includes(childRef))
return;
throw new UsageError(`Workflow step ${stepId} cannot compose ${childRef}: that would create a composition cycle. ` +
`Path: ${compositionPath(refPath, childRef)}.`, "COMPOSITION_INVALID", "Break the cycle: remove or redirect one of the compositions in the path above so no workflow ends up " +
"composing itself, directly or through intermediates.");
}
export async function childWorkflowDispatch(input) {
const { source, baseUnit, childRefInput, context } = input;
// The loader resolves the ref through the one walk, so a missing child fails
// like any other missing workflow.
const childAsset = await loadWorkflowAsset(childRefInput);
const childRef = childAsset.ref;
// The composition cycle check, before any child compilation.
assertNoCompositionCycle(source.id, childRef, context.refPath);
// Freeze the child completely; its plan is a pure function of its own source.
const child = await context.freezeChild(childAsset, [...context.refPath, childRef]);
// Embed the child plan as plain canonical JSON, exactly as `plan_json` stores it.
const embeddedPlanJson = canonicalPlanJson(child.plan);
const frozenPlan = JSON.parse(embeddedPlanJson);
// Bind the step's `with:` against the child's declared params.
const inputBindings = freezeTaskInputBindings({
stepId: source.id,
targetRef: childRef,
with: source.with,
contract: workflowParamContract(frozenPlan),
earlierStepIds: earlierStepIds(context.plan, source.id),
declaredParamNames: declaredParamNames(context.plan),
});
const planHash = createHash("sha256").update(embeddedPlanJson).digest("hex");
const target = Object.freeze({
kind: "child-workflow",
ref: childRef,
planHash,
frozenPlan,
contentHash: childWorkflowContentHash({ ref: childRef, planHash, inputBindings }),
...(inputBindings.length > 0 ? { inputBindings } : {}),
});
return {
target,
// A child run carries its own frozen environment inside its own plan; the
// parser rejects an `env` beside `workflow`, so there is nothing to deliver.
environment: [],
unit: baseUnit,
instructions: source.instructions ?? `Run workflow ${childRef}.`,
};
}