/directive:writing-directive-constraintsfrom directive
Guides writing directive constraints, resolvers, and error boundaries for @directive-run/core: when/require patterns, async constraints with deps, priority ordering, retry policies, deduplication, and system error boundaries.
Ships in directive. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
This skill applies when the project uses @directive-run/core. If not found in package.json, suggest installing it: npm install @directive-run/core.
Auto-invoke when the user:
Do NOT invoke when:
writing-directive-modules.md)building-directive-systems.md)testing-directive-code.md)Constraint declares WHEN something is needed → emits a requirement
Resolver fulfills WHAT to do about it → mutates facts
Never put condition checks in a resolver. Never do async work in a constraint's when().
Is this "when X is true, the system needs Y"?
├── Yes, and Y is async/side-effectful → Constraint + Resolver
├── Yes, but Y is just a derived value → Use derive instead
├── No, it reacts to a change that happened → Use effect
└── No, it's user-initiated → Use event handler
constraints: {
// Static – same object every time
loadConfig: {
when: (facts) => facts.config === null,
require: { type: "LOAD_CONFIG" },
},
// Dynamic – function reads facts
fetchUser: {
when: (facts) => facts.isAuthenticated && !facts.profile,
require: (facts) => ({ type: "FETCH_USER", userId: facts.userId }),
},
// Multiple requirements at once
loadAll: {
when: (facts) => facts.phase === "init",
require: [
{ type: "LOAD_CONFIG" },
{ type: "LOAD_USER" },
],
},
// Suppress conditionally – return null from require
conditionalFetch: {
when: (facts) => facts.needsUpdate,
require: (facts) => {
if (!facts.userId) {
return null;
}
return { type: "FETCH_USER", userId: facts.userId };
},
},
},
constraints: {
normalTransition: {
priority: 50, // Default is 0; higher runs first
when: (facts) => facts.phase === "red" && facts.elapsed > 30,
require: { type: "TRANSITION", to: "green" },
},
emergencyOverride: {
priority: 100, // Evaluated before normalTransition
when: (facts) => facts.emergencyActive,
require: { type: "TRANSITION", to: "red" },
},
},
after – sequencing constraintsconstraints: {
authenticate: {
when: (facts) => !facts.token,
require: { type: "AUTHENTICATE" },
},
// Only evaluates after authenticate's resolver completes
loadProfile: {
after: ["authenticate"],
when: (facts) => facts.token && !facts.profile,
require: { type: "LOAD_PROFILE" },
},
},
after blocks evaluation entirely until the named constraint's resolver finishes. If the dependency's when() returns false, the blocked constraint proceeds normally.
depsSynchronous constraints auto-track deps via proxy. Async constraints cannot (suspended across await), so you must declare deps explicitly.
constraints: {
validateToken: {
async: true,
deps: ["token"], // Re-evaluate when token changes
when: async (facts) => {
const valid = await validateTokenRemotely(facts.token);
return valid;
},
require: { type: "REFRESH_TOKEN" },
timeout: 5000, // Optional: abort if check takes too long
},
},
system.constraints.disable("fetchWhenReady");
system.constraints.isDisabled("fetchWhenReady"); // true
system.constraints.enable("fetchWhenReady");
resolvers: {
fetchUser: {
requirement: "FETCH_USER",
resolve: async (req, context) => {
// req – the requirement object emitted by the constraint
// context.facts – mutable proxy to module facts
// context.signal – AbortSignal (cancelled on stop or timeout)
// context.snapshot() – read-only snapshot for before/after
const user = await fetch(`/api/users/${req.userId}`, {
signal: context.signal,
}).then((r) => r.json());
context.facts.user = user;
context.facts.phase = "loaded";
},
},
},
Without key, requirements are deduped by structural equality. With key, you control dedup:
resolvers: {
fetchUser: {
requirement: "FETCH_USER",
key: (req) => `fetch-user-${req.userId}`, // One inflight per userId
resolve: async (req, context) => {
context.facts.user = await fetchUser(req.userId);
},
},
},
resolvers: {
fetchData: {
requirement: "FETCH_DATA",
retry: {
attempts: 3,
backoff: "exponential", // "none" | "linear" | "exponential"
initialDelay: 200,
maxDelay: 5000,
shouldRetry: (error, attempt) => {
if (error.message.includes("404")) {
return false; // Don't retry permanent failures
}
return true;
},
},
resolve: async (req, context) => {
const res = await fetch("/api/data");
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
context.facts.data = await res.json();
},
},
},
resolvers: {
fetchUsers: {
requirement: "FETCH_USER",
batch: {
enabled: true,
windowMs: 50, // Collect requirements for 50ms
maxSize: 20, // Flush immediately at 20 items
},
resolveBatch: async (reqs, context) => {
const ids = reqs.map((req) => req.userId);
context.facts.users = await fetchUsersBatch(ids);
},
},
},
For per-item success/failure, use resolveBatchWithResults and return a results array matching input order.
resolvers: {
fetchData: {
requirement: "FETCH_DATA",
timeout: 10000, // context.signal aborted after 10s
resolve: async (req, context) => {
const res = await fetch("/api/data", { signal: context.signal });
context.facts.data = await res.json();
},
},
},
const system = createSystem({
module: myModule,
errorBoundary: {
onConstraintError: "skip",
onResolverError: "retry-later",
onEffectError: "skip",
onDerivationError: "throw", // Derivation errors are bugs – throw always
onError: (error) => {
console.error(`[${error.source}] ${error.sourceId}: ${error.message}`);
},
retryLater: {
delayMs: 1000,
maxRetries: 3,
backoffMultiplier: 2,
maxDelayMs: 30000,
},
},
});
Recovery strategies:
| Strategy | Behavior |
|---|---|
"skip" | Swallow the error, continue |
"retry" | Retry immediately |
"retry-later" | Retry after exponential backoff delay |
"disable" | Disable the failing constraint/effect permanently |
"throw" | Re-throw, halting the system |
errorBoundary: {
onResolverError: (error, resolverId) => {
if (error.message.includes("NetworkError")) {
return "retry-later";
}
if (error.message.includes("401")) {
return "skip";
}
return "throw";
},
},
import { DirectiveError } from "@directive-run/core";
try {
await system.settle();
} catch (err) {
if (err instanceof DirectiveError) {
err.source; // "constraint" | "resolver" | "effect" | "derivation" | "system"
err.sourceId; // e.g., "fetchUser"
err.recoverable; // boolean
err.context; // debug data (e.g., the requirement object)
}
}
hooks: {
onInit: (system) => { ... },
onStart: (system) => { ... },
onStop: (system) => { ... },
onError: (error, hookContext) => { ... },
},
import { createCircuitBreaker } from "@directive-run/core/plugins";
const apiBreaker = createCircuitBreaker({
name: "external-api",
failureThreshold: 5,
recoveryTimeMs: 30000,
halfOpenMaxRequests: 3,
isFailure: (error) => !error.message.includes("404"),
onStateChange: (from, to) => console.log(`Circuit: ${from} -> ${to}`),
});
// Use in resolvers
resolvers: {
fetchData: {
requirement: "FETCH_DATA",
resolve: async (req, context) => {
const data = await apiBreaker.execute(async () => {
return fetch("/api/data").then((r) => r.json());
});
context.facts.data = data;
},
},
},
// Wire into constraints for automatic fallback
constraints: {
apiDown: {
when: () => apiBreaker.getState() === "OPEN",
require: { type: "USE_FALLBACK" },
},
fetchNormally: {
when: (facts) => apiBreaker.getState() !== "OPEN" && !facts.data,
require: { type: "FETCH_DATA" },
},
},
// WRONG – when() must be synchronous (unless async: true + deps)
when: async (facts) => {
return await validate(facts.token);
}
// CORRECT – either use async: true with deps, or put validation in resolver
when: (facts) => Boolean(facts.token),
// WRONG – resolver should not check conditions (that's the constraint's job)
resolve: async (req, context) => {
if (!context.facts.isAuthenticated) {
return;
}
// ...
}
// CORRECT – let the constraint's when() gate this
// WRONG – engine cannot track dependencies
{ async: true, when: async (facts) => await validate(facts.token) }
// CORRECT
{ async: true, deps: ["token"], when: async (facts) => await validate(facts.token) }
// WRONG
require: "FETCH_DATA"
// CORRECT
require: { type: "FETCH_DATA" }
// WRONG – return value ignored
resolve: async (req, context) => { return await fetchUser(req.userId); }
// CORRECT
resolve: async (req, context) => { context.facts.user = await fetchUser(req.userId); }
Always use (req, context) – never (req, ctx) or (request, context).
// WRONG
resolve: async (req, context) => {
context.facts.data = await fetch("/api").then((r) => r.json());
}
// CORRECT – add retry and check response status
resolve: async (req, context) => {
const res = await fetch("/api");
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
context.facts.data = await res.json();
},
| Feature | Purpose | When to use |
|---|---|---|
| Constraint | Declare a need (emit requirement) | "When X, the system needs Y" |
| Resolver | Fulfill a need (async work) | "Do Y" |
| Effect | React to changes (fire-and-forget) | "Whenever X changes, log it" |
| Derivation | Compute a value (sync, cached) | "X is always facts.a + facts.b" |
constraints.md – full constraint API, async constraints, disabling, common mistakesresolvers.md – full resolver API, context object, batch resolution, inspecting resolver statuserror-boundaries.md – all recovery strategies, DirectiveError API, circuit breaker patternsWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub directive-run/directive --plugin directive12 skills in this plugin
/building-ai-agentsConfigure AI provider runners (Anthropic, OpenAI, Ollama), stream tokens and structured output, and wire cross-agent communication. Use when setting up LLM provider connections, implementing token streaming, choosing between providers, or building agents that communicate results to each other./building-ai-orchestratorsCreate AI orchestration systems using createAgentOrchestrator (single agent) and createMultiAgentOrchestrator (multi-agent). Define factsSchema with t.*() builders, init state, constraints for trigger conditions, and resolvers for LLM execution. Use when building LLM-powered workflows, agent pipelines, task runners, or multi-agent coordination systems./building-directive-systemsCompose Directive modules into systems: single-module and multi-module createSystem() calls, namespaced fact/derive/event access, cross-module dependencies, initialFacts and hydration, plugins (logging, devtools, persistence, circuit breaker), React adapter hooks (useSelector, useEvent, useSystem, DirectiveProvider), and system lifecycle. Use when creating a system, connecting modules together, adding plugins, or integrating Directive with React./getting-started-with-directiveUnderstand Directive fundamentals: what modules, facts, derivations, constraints, resolvers, and systems are, and how they fit together. Use when someone is new to Directive, asks 'what is Directive', wants to understand the mental model, or needs help choosing between Directive concepts./hardening-ai-systemsAdd guardrails (input/output validation), memory strategies, token budgets, PII detection, prompt injection prevention, and circuit breakers to AI orchestrators. Use when enforcing content policies, managing costs, preventing security vulnerabilities, adding resilience, or implementing agent memory across turns./migrating-to-directiveMigrate state management code from Redux, Zustand, XState, MobX, Recoil, or Jotai to Directive. Provides concept mapping, step-by-step migration patterns, and before/after code examples. Use when asked to migrate, convert, or port existing state management to Directive.