How kushagrasaxena28/crator-assignment works
Purpose and topology
This repository implements a local permissions, approval, and audit gateway for AI-agent calls to external services. It has two independently run Python processes: a Django/DRF backend and a Claude Agent SDK CLI. The CLI intentionally routes external operations through backend HTTP endpoints, making the backend the enforcement boundary.
• Backend entry points: backend/manage.py, backend/config/urls.py, backend/config/settings.py
• Agent entry points: agent-cli/agent_cli_script/__main__.py, agent-cli/agent_cli_script/session.py
Backend API and trust boundary
Django exposes token issuance plus /api/external-services/ APIs for toolkit discovery, schemas, action calls, approval polling/resolution, permission administration, and audit queries. JWT authentication identifies agents for normal calls; Basic auth protects human-only approval and permission-management operations.
• Routing/configuration: backend/config/urls.py, backend/external_services/urls.py
• Auth/token enforcement: backend/external_services/authentication.py, backend/external_services/tokens.py
• Endpoint handlers: backend/external_services/views/toolkits.py, backend/external_services/views/approvals.py, backend/external_services/views/permissions.py
Permission and validation pipeline
An action call resolves the caller’s effective policy from a sparse (agent, action) override, otherwise using the cataloged action default. Policies branch into immediate execution, approval-ticket creation, or denial. Request parameters are checked against the action’s persisted JSON schema before execution, preventing separate runtime validators from drifting.
• Core policy: backend/external_services/permissions.py
• Persistent domain/schema: backend/external_services/models.py
• Validation: backend/external_services/validation.py
Approval and immutable audit flow
Approval-required calls return a ticket and create a pending_approval audit event. A human resolves the ticket; resolution executes or rejects the action and emits a separate terminal audit event. Expiration is handled lazily during ticket reads/resolution. Audit identity is snapshot-based rather than an agent foreign key, preserving history after agent deletion.
• Approval API: backend/external_services/views/approvals.py
• Audit write/mirroring path: backend/external_services/audit.py
• Models/admin controls: backend/external_services/models.py, backend/external_services/admin.py
Toolkit abstraction and external boundaries
Toolkit-aware code is isolated from the generic auth, permissions, approvals, and audit pipeline. Catalogs define actions and defaults; a registry dispatches to toolkit executors. items and github mutate process-local in-memory stores, while Notion uses its live REST API and environment-provided credentials.
• Registry/dispatch: backend/external_services/toolkits/__init__.py
• Mock toolkit example: backend/external_services/toolkits/items/catalog.py, backend/external_services/toolkits/items/executors.py, backend/external_services/toolkits/items/store.py
• Live integration: backend/external_services/toolkits/notion/client.py, backend/external_services/toolkits/notion/executors.py
Provisioning and persistence
SQLite persists only gateway metadata: agents, toolkit/action catalog, sparse overrides, tickets, and audit history. The idempotent seed command provisions the demo agent and action catalog. Django ORM is the persistence boundary, so database replacement is configuration-led.
• Schema/migrations: backend/external_services/models.py, backend/external_services/migrations/0001_initial.py
• Catalog provisioning: backend/external_services/management/commands/seed.py
• Runtime settings: backend/config/settings.py
Agent CLI control flow
The CLI hosts an interactive Claude session. Its sole custom tool obtains JWTs; session code caches and refreshes them. The model uses Bash/curl to invoke backend APIs, while a background watcher independently polls pending ticket statuses and emits approval notifications. It requires Anthropic’s API; the backend itself does not.
• CLI/session orchestration: agent-cli/agent_cli_script/__main__.py, agent-cli/agent_cli_script/session.py
• Model instructions and JWT tool: agent-cli/agent_cli_script/system_prompt.py, agent-cli/agent_cli_script/tools/generate_jwt.py
• Async approval notifications: agent-cli/agent_cli_script/ticket_watcher.py
Main entry points: session.py Claude session · toolkits.py API handlers · permissions.py Policy & schema validation · urls.py Gateway API routes
Components 18
Django Gateway
Enforcement, approval, and audit boundary.
- Django app
backend/manage.py - Gateway API routes · Exposes token and external-services endpoints.
backend/external_services/urls.py - API handlers · Handles discovery, action calls, approvals, permissions, and audit queries.
backend/external_services/views/toolkits.py - JWT & Basic auth · JWT identifies agents; Basic auth gates human administration.
backend/external_services/authentication.py - Policy & schema validation · Resolves override/default policy and validates persisted action schemas.
backend/external_services/permissions.py - Approvals & audit · Creates tickets, lazily expires them, and records pending and terminal events.
backend/external_services/audit.py - Toolkit registry · Dispatches cataloged actions to isolated toolkit executors.
backend/external_services/toolkits/__init__.py - Gateway domain models · Stores agents, catalog, overrides, tickets, and snapshot-based audit history.
backend/external_services/models.py - SQLite metadata · Persists gateway metadata only.
- Catalog seed command · Idempotently provisions the demo agent and action catalog.
backend/external_services/management/commands/seed.py - Items/GitHub mock toolkits · Mutate process-local stores.
backend/external_services/toolkits/items/executors.py - Notion toolkit · Uses environment-provided credentials for live Notion calls.
backend/external_services/toolkits/notion/executors.py
Agent CLI
Interactive Claude Agent SDK process.
- CLI entrypoint
agent-cli/agent_cli_script/__main__.py - Claude session · Hosts the interactive model session and token cache.
agent-cli/agent_cli_script/session.py - Instructions & JWT tool
agent-cli/agent_cli_script/tools/generate_jwt.py - Ticket watcher · Polls ticket status and emits approval notifications.
agent-cli/agent_cli_script/ticket_watcher.py
External Systems
- Anthropic API
- Notion REST API
Connections 20
- CLI entrypoint → Claude session: starts
- Claude session → Instructions & JWT tool: provides tool
- Claude session → Ticket watcher: runs watcher
- Claude session → Anthropic API: interactive session
- Claude session → Gateway API routes: Bash/curl API calls
- Ticket watcher → Gateway API routes: polls tickets
- Django app → Gateway API routes: serves
- Gateway API routes → API handlers: routes requests
- API handlers → JWT & Basic auth: authenticates
- API handlers → Policy & schema validation: authorizes action calls
- Policy & schema validation → Gateway domain models: reads policy and schema
- Policy & schema validation → Approvals & audit: creates approval or audit outcome
- Policy & schema validation → Toolkit registry: dispatches allowed actions
- API handlers → Approvals & audit: resolves and queries tickets
- Approvals & audit → Gateway domain models: persists tickets and events
- Gateway domain models → SQLite metadata: ORM persistence
- Catalog seed command → Gateway domain models: provisions catalog
- Toolkit registry → Items/GitHub mock toolkits: dispatches mock actions
- Toolkit registry → Notion toolkit: dispatches live actions
- Notion toolkit → Notion REST API: REST calls