Skip to content

HTN Decomposition Guide

The HTN system decomposes tasks through four levels:

LevelDescriptionExample
portfolioStrategic workspace tasks”Improve portfolio health”
projectProject-scoped tasks”Stabilize CI pipeline”
domainDomain-specific tasks”Restart failing service”
actionPrimitive executable actions”Set service.status = healthy”
import { action } from "../src/planner/action.ts";
const restart = action("restart", "restart-service")
.level("action")
.requireEquals("service.status", "down")
.set({ "service.status": "healthy" })
.cost(2)
.build();
import type { CompositeTask } from "../src/planner/types.ts";
const fixService: CompositeTask = {
id: "fix-service",
name: "Fix Service",
level: "domain",
precondition: (state) => state["service.status"] === "down",
decompose: (state) => ["restart", "verify-health"],
};
import { TaskNetwork } from "../src/planner/task-network.ts";
const network = new TaskNetwork();
network.addPrimitiveAction(restart);
network.addPrimitiveAction(verifyHealth);
network.addCompositeTask(fixService);
import { HTNDecomposer } from "../src/planner/htn-decomposer.ts";
const decomposer = new HTNDecomposer(network);
const result = decomposer.decompose("fix-service", currentState);
if (result.success) {
// result.actions contains ordered primitive actions
}