ACAgentic Craft

Advanced~30 minHazelJS

Auth, CASL, and Agent Policy

Align Jwt/Tenant guards and CASL with agent capabilities and PolicyEngine—with policy snippets.

Authors
editorial-team
Published
Last reviewed
Progress is stored locally in this browser.

Direct answer

HTTP identity (JwtAuthGuard, TenantGuard, @hazeljs/casl PoliciesGuard) answers who may call your API. Agent capabilities + PolicyEngine answer what the runtime may do with tools. Keep them consistent.

Pattern: /patterns/bounded-autonomy.

Worked example — capabilities + PolicyEngine

typescript
import { Agent, AgentRuntime, PolicyEngine, defaultPiiMaskPolicies } from '@hazeljs/agent';

@Agent({
  name: 'desk',
  capabilities: ['orders.read'], // empty = unrestricted — avoid in prod
  maxSteps: 8,
  version: '1.0.0',
})
export class DeskAgent { /* @Tool methods */ }

const runtime = new AgentRuntime({
  policyEngine: new PolicyEngine([
    ...defaultPiiMaskPolicies(),
    { id: 'no-shell', tool: 'shell', effect: 'deny', priority: 100 },
    {
      id: 'refund-hitl',
      tool: 'processRefund',
      effect: 'require_approval',
      priority: 20,
    },
  ]),
});

await runtime.execute('desk', goal, {
  maxSteps: 8,
  budget: { maxTokens: 50_000, maxCostUsd: 0.5 },
  // pass user/tenant from JwtAuthGuard into metadata / identity hooks per your app
});

A tenant user without payments.write must not reach an agent whose ToolRegistry can refund.

Artifact: Capability ↔ CASL permission matrix for one agent

Sources

Related