# anentrypoint/casey architecture

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

- Interactive diagram: https://gitdiagram.com/anentrypoint/casey
- Repository: https://github.com/anentrypoint/casey

## Overview

#### Purpose and entry points
Casey is a Node.js orchestration service for animal-disease reports received over messaging channels. It creates and manages cases, runs an agent through Freddie, persists workflow/audit data in Thatcher, and exposes an operator dashboard.

Primary commands are implemented in `bin/casey.js`; supervised production startup is coordinated by `bin/worker.js`, `src/supervisor.js`, and `src/supervisor-machine.js`.

#### Runtime assembly and channel ingress
`src/casey.js` is the top-level composition root: it assembles the store, agent host, channel gateway/adapters, hooks, and logging. `src/gateway-hooks.js` supplies the case-aware inbound handler, while `src/hooks/handler.js` and `src/hooks/heuristics.js` cover message handling paths and deterministic interaction behavior.

Ingress normalizes a channel message into case context, deduplicates it using the channel message ID, appends an inbound event, then either handles suitable fixed interactions or invokes an agent turn. Channel credentials and WhatsApp webhook verification/HMAC boundary are environment-driven.

#### Case persistence, workflow, and audit boundary
Thatcher is the system of record, configured in `thatcher.config.yml`. `src/case-store.js` is the principal adapter around it: it owns find-or-create behavior, locking, event persistence, transitions, pagination, and compatibility handling. `src/case-runtime.js` exposes the live store to the plugin/runtime.

The durable model is case/contact/event oriented. The key architectural invariant is append-only observability: inbound and outbound messages, agent actions, observations, transitions, and operator actions become event records. Core record shaping and write semantics are separated in `src/core/event-log.js`, `src/core/write-path.js`, and `src/core/raw-log.js`.

#### Agent execution and controlled autonomy
Freddie provides the agent harness, sessions, channel adapters, and model-provider resolution; Casey contributes domain context and tools. `plugins/case-tools/plugin.js` registers Casey capabilities, and `src/case-tools.js` implements case read/list/update/observe/transition tools.

`src/attn.js`, `src/conversation-state.js`, and `src/conversation-spec.js` define the conversation/case-context layer supplied to turns. Tool operations are constrained by each case’s autonomy mode (`auto`, `assisted`, `observe`), making the tool boundary—not free-form model text—the control point for mutations. Model errors, timeouts, or empty responses are recorded rather than converted into scripted contact replies.

#### Domain interpretation and rule packs
The repository has a modular domain-analysis layer. `src/engine/rule-engine.js` evaluates configured rules; reusable case interpretation primitives live in `src/core/interpretation.js` and `src/core/observation.js`. Domain packs, including animal-health and water-point definitions, are in `src/packs/animal-health.js` and `src/packs/water-point.js`.

This layer can enrich observations, quality flags, aggregation, and reporting without making it the authority for escalation: operators retain workflow and priority control. Related analytical outputs are produced through `src/report.js`, `src/report-analytics.js`, and `src/workload.js`.

#### Operator dashboard and outbound control
`src/dashboard/server.js` serves an Express-backed, no-build SPA. API routes split operational concerns across `src/dashboard/routes/cases.js`, `src/dashboard/routes/operations.js`, and `src/dashboard/routes/reports.js`; session authentication is handled by `src/dashboard/auth.js`.

The browser client in `src/dashboard/public/app.js` reads cases/timelines and lets operators edit records, override transitions, claim work, and send human replies. Operator mutations pass back through the store/workflow boundary and are audited. Replies cross the channel boundary only through controlled outbound handling; opt-out and autonomy state must be respected.

#### Infrastructure and external dependencies
Material runtimes/services are Node.js, Freddie (agent/channel runtime), Thatcher (CRUD/workflow/RBAC/audit), Express, Discord, and Meta WhatsApp Graph/webhooks. Optional media processing in `src/hooks/media.js` and `src/llm.js` can use OpenAI, Anthropic, or ElevenLabs and is explicitly opt-in because it sends user media to external providers.

`src/supervisor.js` isolates the parent process from application imports, forks a worker, restarts on crashes, and can watch Casey/Freddie source paths for reload. CI runs the repository’s dependency-light lint gate through `.github/workflows/ci.yml`.

Main entry points: [`src/case-store.js`](https://github.com/anentrypoint/casey/blob/main/src/case-store.js) (Case store), [`src/gateway-hooks.js`](https://github.com/anentrypoint/casey/blob/main/src/gateway-hooks.js) (Gateway hooks), [`src/dashboard/server.js`](https://github.com/anentrypoint/casey/blob/main/src/dashboard/server.js) (Dashboard server), [`src/engine/rule-engine.js`](https://github.com/anentrypoint/casey/blob/main/src/engine/rule-engine.js) (Rule engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime &amp; ingress"]
  node_supervisor{{"Supervisor<br/>[supervisor.js]"}}
  node_worker["Worker startup<br/>worker process<br/>[worker.js]"]
  node_casey["Casey composition<br/>Node.js app<br/>[casey.js]"]
  node_channels{{"Discord &amp; WhatsApp<br/>channel boundary"}}
  node_gateway_hooks["Gateway hooks<br/>inbound handler<br/>[gateway-hooks.js]"]
  node_media_llm{{"Optional media LLM<br/>external AI providers<br/>[llm.js]"}}
end

subgraph group_cases["Case system"]
  node_case_store["Case store<br/>Thatcher adapter<br/>[case-store.js]"]
  node_event_write_path["Event write path<br/>audit writer<br/>[write-path.js]"]
  node_thatcher[("Thatcher<br/>system of record")]
end

subgraph group_agent["Controlled agent"]
  node_case_runtime["Case runtime<br/>plugin runtime<br/>[case-runtime.js]"]
  node_freddie{{"Freddie<br/>agent harness"}}
  node_case_tools["Case tools plugin<br/>agent plugin<br/>[plugin.js]"]
  node_conversation_context["Conversation context<br/>turn context"]
end

subgraph group_analysis["Domain analysis"]
  node_rule_engine["Rule engine<br/>domain evaluator<br/>[rule-engine.js]"]
  node_domain_packs["Domain packs<br/>rule definitions<br/>[animal-health.js]"]
  node_reporting["Reporting analytics<br/>reporting service"]
end

subgraph group_dashboard["Operator dashboard"]
  node_dashboard_server["Dashboard server<br/>Express API<br/>[server.js]"]
  node_operator_spa["Operator SPA<br/>browser client<br/>[app.js]"]
end

node_supervisor -->|"forks and restarts"| node_worker
node_worker -->|"starts"| node_casey
node_casey -->|"installs"| node_gateway_hooks
node_channels -->|"inbound messages"| node_gateway_hooks
node_gateway_hooks -->|"deduplicate and append inbound event"| node_case_store
node_gateway_hooks -->|"agent turn when needed"| node_freddie
node_gateway_hooks -.->|"optional media processing"| node_media_llm
node_case_store -->|"writes audit events"| node_event_write_path
node_event_write_path -->|"append-only persistence"| node_thatcher
node_case_store -->|"cases, contacts, workflow"| node_thatcher
node_case_runtime -->|"live store access"| node_case_store
node_freddie -->|"loads plugin"| node_case_tools
node_case_tools -->|"reads and mutates by autonomy mode"| node_case_runtime
node_conversation_context -->|"case context for turns"| node_freddie
node_case_store -->|"case observations"| node_rule_engine
node_domain_packs -->|"configured rules"| node_rule_engine
node_rule_engine -->|"enriched observations"| node_reporting
node_dashboard_server -->|"audited operator mutations"| node_case_store
node_dashboard_server -->|"report APIs"| node_reporting
node_operator_spa -->|"authenticated API requests"| node_dashboard_server
node_case_store -->|"controlled outbound replies"| node_channels

click node_supervisor "https://github.com/anentrypoint/casey/blob/main/src/supervisor.js"
click node_worker "https://github.com/anentrypoint/casey/blob/main/bin/worker.js"
click node_casey "https://github.com/anentrypoint/casey/blob/main/src/casey.js"
click node_gateway_hooks "https://github.com/anentrypoint/casey/blob/main/src/gateway-hooks.js"
click node_case_store "https://github.com/anentrypoint/casey/blob/main/src/case-store.js"
click node_event_write_path "https://github.com/anentrypoint/casey/blob/main/src/core/write-path.js"
click node_thatcher "https://github.com/anentrypoint/casey/blob/main/thatcher.config.yml"
click node_case_runtime "https://github.com/anentrypoint/casey/blob/main/src/case-runtime.js"
click node_case_tools "https://github.com/anentrypoint/casey/blob/main/plugins/case-tools/plugin.js"
click node_conversation_context "https://github.com/anentrypoint/casey/blob/main/src/conversation-state.js"
click node_rule_engine "https://github.com/anentrypoint/casey/blob/main/src/engine/rule-engine.js"
click node_domain_packs "https://github.com/anentrypoint/casey/blob/main/src/packs/animal-health.js"
click node_reporting "https://github.com/anentrypoint/casey/blob/main/src/report-analytics.js"
click node_dashboard_server "https://github.com/anentrypoint/casey/blob/main/src/dashboard/server.js"
click node_operator_spa "https://github.com/anentrypoint/casey/blob/main/src/dashboard/public/app.js"
click node_media_llm "https://github.com/anentrypoint/casey/blob/main/src/llm.js"

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_supervisor,node_worker,node_casey,node_channels,node_gateway_hooks,node_media_llm toneBlue
class node_case_store,node_event_write_path,node_thatcher toneAmber
class node_case_runtime,node_freddie,node_case_tools,node_conversation_context toneMint
class node_rule_engine,node_domain_packs,node_reporting toneRose
class node_dashboard_server,node_operator_spa toneIndigo
```

## Components

### Runtime & ingress

- [Supervisor](https://github.com/anentrypoint/casey/blob/main/src/supervisor.js): process supervisor · Forks and restarts the isolated worker. · `src/supervisor.js`
- [Worker startup](https://github.com/anentrypoint/casey/blob/main/bin/worker.js): worker process · `bin/worker.js`
- [Casey composition](https://github.com/anentrypoint/casey/blob/main/src/casey.js): Node.js app · Assembles stores, channels, hooks, and agent host. · `src/casey.js`
- **Discord & WhatsApp**: channel boundary · WhatsApp webhook verification and HMAC are enforced at this boundary.
- [Gateway hooks](https://github.com/anentrypoint/casey/blob/main/src/gateway-hooks.js): inbound handler · Normalizes inbound messages into case-aware handling. · `src/gateway-hooks.js`
- [Optional media LLM](https://github.com/anentrypoint/casey/blob/main/src/llm.js): external AI providers · Opt-in media processing may send user media externally. · `src/llm.js`

### Case system

- [Case store](https://github.com/anentrypoint/casey/blob/main/src/case-store.js): Thatcher adapter · Owns find-or-create, locking, events, transitions, and pagination. · `src/case-store.js`
- [Event write path](https://github.com/anentrypoint/casey/blob/main/src/core/write-path.js): audit writer · Shapes append-only events and raw audit records. · `src/core/write-path.js`
- [Thatcher](https://github.com/anentrypoint/casey/blob/main/thatcher.config.yml): system of record · Provides CRUD, workflow, RBAC, and audit persistence. · `thatcher.config.yml`

### Controlled agent

- [Case runtime](https://github.com/anentrypoint/casey/blob/main/src/case-runtime.js): plugin runtime · Exposes the live store to the agent plugin. · `src/case-runtime.js`
- **Freddie**: agent harness · Provides sessions, channel adapters, and model resolution.
- [Case tools plugin](https://github.com/anentrypoint/casey/blob/main/plugins/case-tools/plugin.js): agent plugin · Registers constrained case read and mutation capabilities. · `plugins/case-tools/plugin.js`
- [Conversation context](https://github.com/anentrypoint/casey/blob/main/src/conversation-state.js): turn context · Supplies attention and case state to agent turns. · `src/conversation-state.js`

### Domain analysis

- [Rule engine](https://github.com/anentrypoint/casey/blob/main/src/engine/rule-engine.js): domain evaluator · Evaluates configured interpretation rules. · `src/engine/rule-engine.js`
- [Domain packs](https://github.com/anentrypoint/casey/blob/main/src/packs/animal-health.js): rule definitions · Animal-health and water-point interpretation definitions. · `src/packs/animal-health.js`
- [Reporting analytics](https://github.com/anentrypoint/casey/blob/main/src/report-analytics.js): reporting service · Produces analytical and workload outputs. · `src/report-analytics.js`

### Operator dashboard

- [Dashboard server](https://github.com/anentrypoint/casey/blob/main/src/dashboard/server.js): Express API · Serves the SPA and operational case, operation, and report routes. · `src/dashboard/server.js`
- [Operator SPA](https://github.com/anentrypoint/casey/blob/main/src/dashboard/public/app.js): browser client · Lets operators inspect timelines, control workflow, claim work, and reply. · `src/dashboard/public/app.js`

## Connections

- Supervisor → Worker startup: forks and restarts
- Worker startup → Casey composition: starts
- Casey composition → Gateway hooks: installs
- Discord & WhatsApp → Gateway hooks: inbound messages
- Gateway hooks → Case store: deduplicate and append inbound event
- Gateway hooks → Freddie: agent turn when needed
- Gateway hooks → Optional media LLM: optional media processing
- Case store → Event write path: writes audit events
- Event write path → Thatcher: append-only persistence
- Case store → Thatcher: cases, contacts, workflow
- Case runtime → Case store: live store access
- Freddie → Case tools plugin: loads plugin
- Case tools plugin → Case runtime: reads and mutates by autonomy mode
- Conversation context → Freddie: case context for turns
- Case store → Rule engine: case observations
- Domain packs → Rule engine: configured rules
- Rule engine → Reporting analytics: enriched observations
- Dashboard server → Case store: audited operator mutations
- Dashboard server → Reporting analytics: report APIs
- Operator SPA → Dashboard server: authenticated API requests
- Case store → Discord & WhatsApp: controlled outbound replies
