How to Manage AI Agent Safety in Monorepos
SafeClaw by Authensor supports hierarchical policy configuration for monorepos, letting you define a shared base policy at the root and per-package overrides for each workspace. Each agent in your monorepo gets the right permissions for its scope without blanket access to the entire repository. Install with npx @authensor/safeclaw at the monorepo root.
The Monorepo Agent Safety Problem
Monorepos contain multiple packages, services, and agents in a single repository. A coding agent working on the frontend should not have write access to the backend database migration files. A testing agent should not be able to modify production configuration. Without scoped policies, every agent has access to everything.
SafeClaw's hierarchical policy system solves this with 446 validated tests and hash-chained audit logging across all agents.
Installation
# At monorepo root
npx @authensor/safeclaw
Monorepo Structure
my-monorepo/
├── safeclaw.policy.yaml # root base policy
├── packages/
│ ├── frontend/
│ │ ├── safeclaw.policy.yaml # frontend overrides
│ │ └── src/
│ ├── backend/
│ │ ├── safeclaw.policy.yaml # backend overrides
│ │ └── src/
│ ├── ai-agent/
│ │ ├── safeclaw.policy.yaml # agent-specific policy
│ │ └── src/
│ └── shared/
│ └── src/
├── turbo.json
└── package.json
Root Base Policy
# safeclaw.policy.yaml (root)
version: 1
defaultAction: deny
rules:
# All packages can read shared code
- action: file.read
path:
glob: "./packages/shared/**"
decision: allow
# All packages can use LLM APIs
- action: network.request
host:
in: ["api.openai.com", "api.anthropic.com"]
decision: allow
# No package can read env files
- action: file.read
path:
glob: "*/.env"
decision: deny
# No shell execution by default
- action: process.exec
decision: deny
Package-Level Overrides
Frontend agent policy:
# packages/frontend/safeclaw.policy.yaml
version: 1
extends: "../../safeclaw.policy.yaml"
rules:
- action: file.read
path:
glob: "./packages/frontend/src/**"
decision: allow
- action: file.write
path:
glob: "./packages/frontend/src/**"
decision: allow
- action: process.exec
command:
in: ["npm run build", "npm run test", "npx next build"]
decision: allow
Backend agent policy:
# packages/backend/safeclaw.policy.yaml
version: 1
extends: "../../safeclaw.policy.yaml"
rules:
- action: file.read
path:
glob: "./packages/backend/src/**"
decision: allow
- action: file.write
path:
glob: "./packages/backend/src/**"
decision: allow
- action: process.exec
command:
in: ["npm run build", "npm run test"]
decision: allow
# Backend can access database (frontend cannot)
- action: network.request
host:
equals: "db.internal"
decision: allow
Turborepo Integration
Add SafeClaw validation to your Turborepo pipeline:
{
"$schema": "https://turbo.build/schema.json",
"pipeline": {
"safeclaw:validate": {
"inputs": ["safeclaw.policy.yaml"],
"cache": true
},
"build": {
"dependsOn": ["safeclaw:validate"],
"outputs": ["dist/**"]
},
"test": {
"dependsOn": ["safeclaw:validate"]
}
}
}
Package Script
{
"scripts": {
"safeclaw:validate": "npx @authensor/safeclaw validate",
"safeclaw:start": "npx @authensor/safeclaw --policy ./safeclaw.policy.yaml",
"dev": "concurrently \"npm run safeclaw:start\" \"npm run dev:app\""
}
}
CI Validation
Validate all policies in CI before merge:
# .github/workflows/safeclaw.yml
name: SafeClaw Policy Validation
on: [pull_request]
jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Validate root policy
run: npx @authensor/safeclaw validate
- name: Validate package policies
run: |
for policy in packages/*/safeclaw.policy.yaml; do
echo "Validating $policy"
npx @authensor/safeclaw validate --policy "$policy"
done
Per-Agent Scoping
When running multiple agents in the same monorepo, start SafeClaw with the package-specific policy:
import { Gate } from '@authensor/safeclaw';
// Frontend agent gets frontend policy
const frontendGate = new Gate({
policy: './packages/frontend/safeclaw.policy.yaml'
});
// Backend agent gets backend policy
const backendGate = new Gate({
policy: './packages/backend/safeclaw.policy.yaml'
});
Every decision is hash-chained in the audit trail, tagged by which policy file was used. MIT licensed, works with Claude and OpenAI.
Cross-References
- TypeScript Integration
- Policy Design Principles
- Per-Agent Isolation Pattern
- CI/CD Pipeline Recipe
- Policy Engine Architecture
Try SafeClaw
Action-level gating for AI agents. Set it up in your browser in 60 seconds.
$ npx @authensor/safeclaw