Repository navigation
feat(guardrails): Phase 1 deterministic safety enforcement (GUARD-001) - #315
Merged
Merged
Conversation
#140) Ship Claude Code hooks, per-agent CLI budget controls, and a backend override endpoint so dangerous bash commands and credential-file writes can be blocked at the infrastructure layer — not at the model layer. Hooks fire deterministically even under --dangerously-skip-permissions. Base image: - /opt/trinity/hooks/{lib,bash-guardrail,file-guardrail,output-scanner}.py shipped root-owned 0555; guardrails-baseline.json 0444 - ~/.claude/settings.json pre-registers PreToolUse + PostToolUse hooks - startup.sh calls write-runtime-config.py via sudo to render /opt/trinity/guardrails-runtime.json (root-owned) merging baseline with the AGENT_GUARDRAILS env var — agent user cannot rewrite - Fail-closed on hook errors; logs to /logs/guardrails.jsonl Agent server (docker/base-image/agent_server/services/claude_code.py): - _load_guardrails() reads runtime config on every invocation - Chat mode now always passes --max-turns (default 50) - Task mode falls back to guardrails' max_turns_task (default 20) when no caller value is provided - Both paths pick up disallowed_tools from the config Backend: - New agent_ownership.guardrails_config TEXT column (schema + idempotent migration), SecurityMixin get/set helpers, db wrapper - GET/PUT /api/agents/{name}/guardrails with strict whitelist validation (bounded numbers; literal substring lists, ≤50 × ≤256 chars; no regex) - Agent creation serialises overrides into AGENT_GUARDRAILS env var - check_guardrails_env_matches() in helpers.py, wired into lifecycle.py start_agent so POST /stop + /start recreates the container when overrides change — matches the existing pattern used for API keys, resource limits, and full-capabilities Tests (tests/unit/test_guardrails.py — 50 cases, all passing): - Baseline bash deny-list: 17 deny cases + 14 false-positive guards - Baseline credential pattern presence - Router payload validator: accept + 9 reject paths - write-runtime-config.py sanitisation: baseline fallback, overrides, number clamping, malformed JSON, junk list items - Migration idempotency on in-memory SQLite End-to-end verified on a live Trinity instance with a real agent: baseline + per-agent override both trigger real Claude Code tool-call denies with structured JSON log events. Deferred to follow-ups: - Chat-mode wall-clock subprocess timeout (#313) - Credential env masking (GUARD-004) - Guardrails tab + fleet dashboard (GUARD-005) - Network egress controls (GUARD-006) Closes #140 (Phase 1) Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
dolho
force-pushed
the
feature/140-agent-guardrails
branch
from
April 13, 2026 13:10
7c01293 to
d28121d
Compare
Document GET/PUT /api/agents/{name}/guardrails endpoints added in
GUARD-001 Phase 1 implementation.
Co-Authored-By: Claude Opus 4.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Phase 1 of Agent Guardrails (#140): layered, infrastructure-level safety enforcement for autonomous agents. Claude Code hooks fire even under
--dangerously-skip-permissions, so the base image ships deterministic deny-lists that agents cannot opt out of./opt/trinity/hooks/, root-owned 0555) —PreToolUseonBashandEdit|Write|NotebookEdit|MultiEdit,PostToolUseonBashfor credential-pattern logging.--max-turnson every Claude Code invocation — chat mode now passes it (default 50); task mode picks upmax_turns_taskfrom config (default 20) when callers don't specify.GET/PUT /api/agents/{name}/guardrailswith strict whitelist validation. Per-agent overrides are bounded numbers + literal substring lists (no regex from users).POST /stop+POST /starttriggers a container recreate viacheck_guardrails_env_matches(), matching the existing pattern used for API keys / resource limits / full-capabilities./logs/guardrails.jsonl(pattern names only — never matched values).Test plan
Unit (
tests/unit/test_guardrails.py, 50 cases, all green):rm -rf /,curl | sh,git push -f,mkfs.ext4, fork bombs, etc.)rm -rf ./build,chmod +x,git push origin main, etc.)write-runtime-config.pysanitises overrides (clamps numbers, ignores malformed JSON, drops junk list items)End-to-end on a live Trinity instance:
max_turns_chat=50,max_turns_task=20,extra_bash_deny=[])PUT /guardrails+POST /stop+POST /startflows overrides into/opt/trinity/guardrails-runtime.jsonguardrails.jsonlrecords the structured event[Chat] Limiting to 10 agentic turnslog line confirms--max-turnsinjection on every chatrm -rf /,chmod 777,curl | sh,git push -f,mkfs.ext4and allowsls -la, normalcurl/opt/trinity/— filesystem-levelPermission deniedDeferred to follow-ups
env/printenv)Rollout notes
./scripts/deploy/build-base-image.shdocker restartreuses the old image. Delete + recreate or change any setting that triggersrecreate_container_with_updated_config.Closes #140 (Phase 1)
🤖 Generated with Claude Code