Runtime enforcement library for AgentSpec - execute and validate agent specifications.
The enforcer applies AgentSpec specifications at runtime. It checks permissions before actions, tracks resource usage, logs audit trails, and prevents spec violations.
npm install @agentspec/enforcer
import { Enforcer } from "@agentspec/enforcer"; import { AgentSpec } from "@goalos/agentspec-core";
const spec = await AgentSpec.fromFile("claude.agent"); const enforcer = new Enforcer(spec);
// Check permission before action if (enforcer.canExecute("write", "code")) { // Safe to execute await writeCode(changes); enforcer.recordAction("write", "code", { files: 3 }); } else { throw new Error("Permission denied by spec"); }
// Get audit log const log = enforcer.getAuditLog(); console.log("Actions taken:", log);
class Enforcer: spec: AgentSpec context: ExecutionContext
Methods:
- canExecute(action, resource, context?) - Check if allowed
- checkBoundaries(context) - Verify within limits
- recordAction(action, resource, metadata) - Log action
- recordViolation(action, reason) - Log violation attempt
- getAuditLog() - Get full action log
- getMetrics() - Resource usage stats
- reset() - Clear tracking data
- enforce(fn) - Wrap function with enforcement
Pre-check (before action):
enforcer.preCheck({ action: "write", resource: "code", metadata: { files: 5, lines: 1000 } });
Post-check (after action):
enforcer.postCheck({ action: "write", resource: "code", result: { success: true, filesWritten: 5 }, });
const enforcer = new Enforcer(spec);
const currentUsage = enforcer.getMetrics(); const tokenSpent = currentUsage.tokens; const maxTokens = spec.boundaries.maxTokenSpend;
if (tokenSpent >= maxTokens) { throw new Error("Token limit exceeded"); }
const retries = enforcer.getMetrics().retries; const maxRetries = spec.boundaries.maxRetries;
if (retries >= maxRetries) { throw new Error("Retry limit exceeded"); }
const writeCodeEnforced = enforcer.enforce(async (changes) => { return await writeCode(changes); }, { action: "write", resource: "code" });
// Automatic checking and logging await writeCodeEnforced(myChanges);
const log = enforcer.getAuditLog();
log.forEach(entry => {
console.log(${entry.timestamp}: ${entry.action} ${entry.resource});
console.log( Allowed: ${entry.allowed});
console.log( Metadata: ${JSON.stringify(entry.metadata)});
});
Violations are logged but not thrown by default:
enforcer.on("violation", (violation) => { console.error("Spec violation:", violation); // Send to monitoring, trigger alert, etc. });
Or throw:
enforcer.setThrowOnViolation(true); // Now enforcer throws on any violation
enforcer.getMetrics() returns:
{ actions: 42, violations: 0, tokens: 45000, maxTokens: 100000, retries: 3, maxRetries: 5, requestSize: 4096, maxRequestSize: 8192, startTime: "2024-03-01T12:00:00Z", lastAction: "2024-03-01T12:30:45Z" }
Enforcement can use execution context:
enforcer.setContext({ userId: "user123", sessionId: "sess_abc", ipAddress: "192.168.1.1", timestamp: new Date().toISOString(), });
Context is included in all audit logs.
npm test
Includes:
- Permission checks
- Boundary enforcement
- Metric tracking
- Audit logging
- Violation detection
npm run dev - Development mode npm run build - Build npm test - Test npm run lint - Lint
- Main README: ../../README.md
- AgentSpec Spec: ../../spec/agentspec-lang-v0.1.md
- Core Library: ../agentspec-core/README.md
- Quickstart: ../../spec/agentspec-quickstart.md
MIT - See LICENSE