# ravaniroshan/policyctl architecture

> Architecture diagram of the GitHub repository ravaniroshan/policyctl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ravaniroshan/policyctl
- Repository: https://github.com/ravaniroshan/policyctl

## Overview

#### Purpose and entry points
policyctl is a local-first, provider-agnostic policy runtime for coding-agent tool calls and CI diffs, with an optional hosted policy/audit control plane. Main local entry point is the npm CLI at `packages/cli/src/cli.ts`; its reusable evaluation engine is exported from `packages/core/src/index.ts`. The hosted Worker starts at `packages/server/src/index.ts`, while the dashboard SPA starts at `web/src/main.tsx` and routes from `web/src/App.tsx`.

#### Policy engine
`@policyctl/core` defines the policy schema, loads YAML policy files, and deterministically evaluates rules against hook or CI inputs. Rules combine path, tool/command, and diff matchers; outcomes distinguish blocking, failing, warning, and allow behavior. This package is the shared enforcement boundary: CLI/provider adapters should translate external inputs into core types rather than reimplement matching.
Paths: `packages/core/src/types.ts`, `packages/core/src/load.ts`, `packages/core/src/evaluate.ts`, `packages/core/src/matchers.ts`.

#### CLI orchestration and local enforcement
The TypeScript CLI owns repository interaction, stdin/tool-hook handling, user-facing output, and command dispatch. `eval` is the pre-tool execution path used by generated hooks; `check` is the CI path that evaluates git changes, including untracked files. `init`, `list`, `test`, `trace`, and `doctor` support policy lifecycle and diagnostics.
Paths: `packages/cli/src/cli.ts`, `packages/cli/src/commands/eval.ts`, `packages/cli/src/commands/check.ts`, `packages/cli/src/git.ts`.

#### Provider adapter generation
`gen` converts one repository policy into provider-specific integration artifacts rather than embedding provider logic in the engine. Adapters target Claude settings/hooks, Codex execution policy, Cursor hooks, and a pre-commit/CI-oriented gate; templates and adapter definitions form the provider boundary.
Paths: `packages/cli/src/commands/gen.ts`, `packages/cli/src/adapters.ts`, `packages/cli/src/templates.ts`.
The sample repository demonstrates generated Claude/Codex configuration and a CI workflow: `examples/demo/.claude/settings.json`, `examples/demo/.codex/hooks/hooks.json`, `examples/demo/.github/workflows/policy.yml`.

#### Hosted policy and reporting flow
Authenticated CLI clients can publish immutable policy versions, pull team policy, and report check results to the hosted plane. The CLI’s hosted client and policy/report commands communicate with the Worker API; local evaluation remains usable offline. The server separates request routing from storage, authentication/session management, cache access, dashboard aggregation, and AI features.
Paths: `packages/cli/src/hosted.ts`, `packages/cli/src/commands/push.ts`, `packages/cli/src/commands/pull.ts`, `packages/cli/src/commands/report.ts`; `packages/server/src/index.ts`, `packages/server/src/store.ts`, `packages/server/src/auth.ts`, `packages/server/src/session.ts`.

#### Cloudflare control-plane infrastructure
The hosted runtime is Cloudflare Workers, configured through `packages/server/wrangler.toml`. Cloudflare D1/SQLite persists organizations, actors, authentication data, policy versions, and audit/feed records, initialized by migrations. KV caches policy/session lookups; R2 supports CSV exports; Workers AI is exposed through a dedicated server module and is intended for paid semantic-policy functionality. Auth uses session cookies and Turnstile according to the project design.
Paths: `packages/server/wrangler.toml`, `packages/server/migrations/0001_init.sql`, `packages/server/migrations/0002_actor.sql`, `packages/server/migrations/0003_auth.sql`, `packages/server/src/cache.ts`, `packages/server/src/ai.ts`.

#### Dashboard client
The dashboard is a React/Tailwind SPA consuming the Worker API through an API client and auth context. It presents policy versions, reports, sessions, settings, and AI-related views; dashboard layout is shared separately from page content.
Paths: `web/src/lib/api.ts`, `web/src/lib/auth.tsx`, `web/src/components/layout/DashboardShell.tsx`, `web/src/pages/dashboard/Policies.tsx`, `web/src/pages/dashboard/Reports.tsx`, `web/src/pages/dashboard/Sessions.tsx`.

Main entry points: [`packages/server/src/index.ts`](https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/index.ts) (Worker API), [`packages/core/src/index.ts`](https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/index.ts) (Policy engine), [`packages/cli/src/cli.ts`](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/cli.ts) (policyctl CLI), [`packages/cli/src/commands/check.ts`](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/commands/check.ts) (CI change check)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_local["Local runtime"]
  node_cli["policyctl CLI<br/>TypeScript CLI<br/>[cli.ts]"]
  node_eval["Hook evaluation<br/>CLI command<br/>[eval.ts]"]
  node_check["CI change check<br/>CLI command<br/>[check.ts]"]
  node_git["Git change reader<br/>CLI integration<br/>[git.ts]"]
  node_core{{"Policy engine<br/>shared library<br/>[index.ts]"}}
  node_schema["Policy schema<br/>core types<br/>[types.ts]"]
  node_loader["YAML policy loader<br/>core loader<br/>[load.ts]"]
  node_evaluator["Rule evaluator<br/>core evaluation<br/>[evaluate.ts]"]
  node_matchers["Rule matchers<br/>core matching<br/>[matchers.ts]"]
  node_hosted_client["Hosted client<br/>CLI API client<br/>[hosted.ts]"]
end

subgraph group_providers["Provider integrations"]
  node_generator["Integration generator<br/>CLI command<br/>[gen.ts]"]
  node_adapters["Provider adapters<br/>adapter definitions<br/>[adapters.ts]"]
  node_templates["Provider templates<br/>artifact templates<br/>[templates.ts]"]
end

subgraph group_hosted["Hosted control plane"]
  node_worker["Worker API<br/>Cloudflare Worker<br/>[index.ts]"]
  node_store["Control-plane store<br/>data access<br/>[store.ts]"]
  node_auth["Auth and sessions<br/>server auth<br/>[auth.ts]"]
  node_cache[("Policy/session cache<br/>KV access<br/>[cache.ts]")]
  node_database[("D1 policy and audit data<br/>Cloudflare D1<br/>[0001_init.sql]")]
  node_ai["Semantic policy AI<br/>Workers AI module<br/>[ai.ts]"]
end

subgraph group_dashboard["Dashboard"]
  node_dashboard["Dashboard SPA<br/>React SPA<br/>[App.tsx]"]
  node_dashboard_api["Dashboard API client<br/>web API client<br/>[api.ts]"]
end

node_cli -->|"dispatches"| node_eval
node_cli -->|"dispatches"| node_check
node_eval -->|"evaluates inputs"| node_core
node_check -->|"reads changes"| node_git
node_check -->|"evaluates diff"| node_core
node_core -->|"exports"| node_schema
node_core -->|"loads policy"| node_loader
node_core -->|"evaluates rules"| node_evaluator
node_evaluator -->|"uses"| node_matchers
node_cli -->|"dispatches gen"| node_generator
node_generator -->|"uses policy schema"| node_core
node_generator -->|"selects"| node_adapters
node_adapters -->|"renders"| node_templates
node_cli -.->|"pushes, pulls, reports"| node_hosted_client
node_hosted_client -->|"authenticated API calls"| node_worker
node_worker -->|"authenticates"| node_auth
node_worker -->|"reads and writes"| node_store
node_worker -->|"caches lookups"| node_cache
node_store -->|"persists"| node_database
node_worker -.->|"invokes"| node_ai
node_dashboard -->|"uses"| node_dashboard_api
node_dashboard_api -->|"consumes API"| node_worker

click node_cli "https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/cli.ts"
click node_eval "https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/commands/eval.ts"
click node_check "https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/commands/check.ts"
click node_git "https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/git.ts"
click node_core "https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/index.ts"
click node_schema "https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/types.ts"
click node_loader "https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/load.ts"
click node_evaluator "https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/evaluate.ts"
click node_matchers "https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/matchers.ts"
click node_generator "https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/commands/gen.ts"
click node_adapters "https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/adapters.ts"
click node_templates "https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/templates.ts"
click node_hosted_client "https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/hosted.ts"
click node_worker "https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/index.ts"
click node_store "https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/store.ts"
click node_auth "https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/auth.ts"
click node_cache "https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/cache.ts"
click node_database "https://github.com/ravaniroshan/policyctl/blob/main/packages/server/migrations/0001_init.sql"
click node_ai "https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/ai.ts"
click node_dashboard "https://github.com/ravaniroshan/policyctl/blob/main/web/src/App.tsx"
click node_dashboard_api "https://github.com/ravaniroshan/policyctl/blob/main/web/src/lib/api.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_eval,node_check,node_git,node_core,node_schema,node_loader,node_evaluator,node_matchers,node_hosted_client toneBlue
class node_generator,node_adapters,node_templates toneAmber
class node_worker,node_store,node_auth,node_cache,node_database,node_ai toneMint
class node_dashboard,node_dashboard_api toneRose
```

## Components

### Local runtime

Offline policy authoring and enforcement.

- [policyctl CLI](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/cli.ts): TypeScript CLI · Command dispatch, hooks, repository interaction, and output. · `packages/cli/src/cli.ts`
- [Hook evaluation](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/commands/eval.ts): CLI command · Pre-tool execution evaluation path. · `packages/cli/src/commands/eval.ts`
- [CI change check](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/commands/check.ts): CLI command · Evaluates tracked and untracked git changes. · `packages/cli/src/commands/check.ts`
- [Git change reader](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/git.ts): CLI integration · `packages/cli/src/git.ts`
- [Policy engine](https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/index.ts): shared library · Shared deterministic enforcement boundary. · `packages/core/src/index.ts`
- [Policy schema](https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/types.ts): core types · `packages/core/src/types.ts`
- [YAML policy loader](https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/load.ts): core loader · `packages/core/src/load.ts`
- [Rule evaluator](https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/evaluate.ts): core evaluation · Produces block, fail, warn, or allow outcomes. · `packages/core/src/evaluate.ts`
- [Rule matchers](https://github.com/ravaniroshan/policyctl/blob/main/packages/core/src/matchers.ts): core matching · Matches paths, tools, commands, and diffs. · `packages/core/src/matchers.ts`
- [Hosted client](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/hosted.ts): CLI API client · Used by policy publication, pull, and result reporting commands. · `packages/cli/src/hosted.ts`

### Provider integrations

Generated agent and CI artifacts.

- [Integration generator](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/commands/gen.ts): CLI command · Turns repository policy into provider artifacts. · `packages/cli/src/commands/gen.ts`
- [Provider adapters](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/adapters.ts): adapter definitions · `packages/cli/src/adapters.ts`
- [Provider templates](https://github.com/ravaniroshan/policyctl/blob/main/packages/cli/src/templates.ts): artifact templates · `packages/cli/src/templates.ts`

### Hosted control plane

Cloudflare-hosted policy, audit, and auth services.

- [Worker API](https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/index.ts): Cloudflare Worker · Routes authenticated control-plane requests. · `packages/server/src/index.ts`
- [Control-plane store](https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/store.ts): data access · Persists policy versions and audit records. · `packages/server/src/store.ts`
- [Auth and sessions](https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/auth.ts): server auth · Manages authentication and session cookies. · `packages/server/src/auth.ts`
- [Policy/session cache](https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/cache.ts): KV access · `packages/server/src/cache.ts`
- [D1 policy and audit data](https://github.com/ravaniroshan/policyctl/blob/main/packages/server/migrations/0001_init.sql): Cloudflare D1 · Organizations, actors, auth, policy versions, and audit/feed records. · `packages/server/migrations/0001_init.sql`
- [Semantic policy AI](https://github.com/ravaniroshan/policyctl/blob/main/packages/server/src/ai.ts): Workers AI module · Supports paid semantic-policy functionality. · `packages/server/src/ai.ts`

### Dashboard

React client for hosted control-plane data.

- [Dashboard SPA](https://github.com/ravaniroshan/policyctl/blob/main/web/src/App.tsx): React SPA · Policy, report, session, settings, and AI views. · `web/src/App.tsx`
- [Dashboard API client](https://github.com/ravaniroshan/policyctl/blob/main/web/src/lib/api.ts): web API client · `web/src/lib/api.ts`

## Connections

- policyctl CLI → Hook evaluation: dispatches
- policyctl CLI → CI change check: dispatches
- Hook evaluation → Policy engine: evaluates inputs
- CI change check → Git change reader: reads changes
- CI change check → Policy engine: evaluates diff
- Policy engine → Policy schema: exports
- Policy engine → YAML policy loader: loads policy
- Policy engine → Rule evaluator: evaluates rules
- Rule evaluator → Rule matchers: uses
- policyctl CLI → Integration generator: dispatches gen
- Integration generator → Policy engine: uses policy schema
- Integration generator → Provider adapters: selects
- Provider adapters → Provider templates: renders
- policyctl CLI → Hosted client: pushes, pulls, reports
- Hosted client → Worker API: authenticated API calls
- Worker API → Auth and sessions: authenticates
- Worker API → Control-plane store: reads and writes
- Worker API → Policy/session cache: caches lookups
- Control-plane store → D1 policy and audit data: persists
- Worker API → Semantic policy AI: invokes
- Dashboard SPA → Dashboard API client: uses
- Dashboard API client → Worker API: consumes API
