# rishvaiyer/canarynorth architecture

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

- Interactive diagram: https://gitdiagram.com/rishvaiyer/canarynorth
- Repository: https://github.com/rishvaiyer/canarynorth

## Overview

#### Purpose and Entry Points
CanaryNorth is a Node.js trust/policy proxy for AI-generated actions. It evaluates scoped capability requests, blocks unsafe or unauthorized work, and emits hash-chained signed receipts plus optional artifact/evidence packages. The HTTP service starts at `server.mjs`; the synthetic browser demo is served from `public/index.html` and its client logic in `public/app.js`. The PenTel teaching console is a separate static UI rooted at `public/pen-console/index.html`.

#### HTTP Proxy and Request Boundary
`server.mjs` owns the API surface: bootstrap data, authorization, receipt/evidence reads, artifact export/verification, signing-key publication, health, and read-only MCP audit. It is the principal enforcement boundary between browser/model-supplied request metadata and protected capability credentials: callers reference opaque `cap_*` identifiers rather than provider keys. Production requests require bearer authentication plus tenant/workspace, principal, audience, policy-version, and one-time-nonce context; demo mode deliberately relaxes this for synthetic use.

#### Authorization and Policy Evaluation
Policy enforcement is concentrated in `src/policy.mjs`, with approval-aware flows in `src/approvals.mjs` and agentic/control evaluators in `src/agentic-defense.mjs`. Authorization evaluates capability scope, action/resource allowlists, expiry, request binding, and deny-by-default rules before an action can be allowed. Deterministic content/metadata signals quarantine suspicious prompt-injection, credential, DLP, or unsafe-export-shaped inputs, but structural capability and scope checks remain the actual boundary. Approval-required synthetic actions create short-lived approval state and re-evaluate policy/nonce conditions when resolved.

#### Receipts, Signing, and Artifact Provenance
Each authorization decision becomes a tamper-evident receipt through `src/signing.mjs`, linked to its predecessor and persisted through the storage abstraction. HMAC compatibility remains available; optional Ed25519 enables independently verifiable receipt and manifest signatures, with the public key exposed by the server. `src/artifacts.mjs` binds an allowed receipt to a synthetic artifact, hashes the artifact, produces a signed sidecar manifest, and verifies returned packages. Offline verification tooling is in `scripts/verify-receipt.mjs`.

#### Evidence and Release Assurance
`src/evidence.mjs` manages the redaction-aware synthetic evidence ledger and encrypted evidence-package creation. Packages use local/operator-managed key material rather than exposing wrapping keys to clients. `src/release-evidence.mjs` evaluates a fixed replay suite at `fixtures/release-replays.v1.json`, compares reviewed and candidate policy outcomes, and records release evidence; `scripts/verify-release.mjs` supports the release gate. This distinguishes policy-regression assurance from claims of scanner, malware, or trained-ML coverage.

#### Durable Ledger Infrastructure
`src/storage.mjs` provides interchangeable receipt persistence modes: in-memory for demos, append-only JSONL for durable mounted storage, and PostgreSQL for production-style deployments. PostgreSQL initializes receipt and nonce tables and serializes receipt-chain writes; nonce persistence supports replay prevention across requests. Deployment is designed for a Node service plus PostgreSQL, including Railway-oriented boundaries in `.github/workflows/deployment-boundaries.yml`. Production fails closed without sufficiently strong signing and auth secrets.

#### Client Visualization and Learning Surfaces
The primary demo consumes bootstrap, authorization, receipt, evidence, and artifact APIs from `public/app.js`; `public/graph.mjs` renders the guided teaching graph. Supporting educational routes include `public/learn.html`, `public/threat-lab.html`, and `public/pen-entry.html`. The richer PenTel interface has distinct dashboard, attack-console, coverage, safety-lab, and documentation modules under `public/pen-console/`, but remains a static synthetic interaction layer rather than an external-tool integration.

Main entry points: [`server.mjs`](https://github.com/rishvaiyer/canarynorth/blob/main/server.mjs) (Proxy HTTP service), [`src/policy.mjs`](https://github.com/rishvaiyer/canarynorth/blob/main/src/policy.mjs) (Policy evaluator), [`src/signing.mjs`](https://github.com/rishvaiyer/canarynorth/blob/main/src/signing.mjs) (Receipt chain signer), [`src/release-evidence.mjs`](https://github.com/rishvaiyer/canarynorth/blob/main/src/release-evidence.mjs) (Release replay evaluator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Client &amp; Learning Surfaces"]
  node_demo["Browser demo<br/>static client<br/>[app.js]"]
  node_teaching_graph["Guided teaching graph<br/>client visualization<br/>[graph.mjs]"]
  node_learning_pages["Learning routes<br/>static pages<br/>[learn.html]"]
  node_pentel["PenTel console<br/>static teaching UI<br/>[index.html]"]
end

subgraph group_api["HTTP Enforcement"]
  node_server["Proxy HTTP service<br/>Node entry point<br/>[server.mjs]"]
  node_request_context["Production request context<br/>auth boundary<br/>[server.mjs]"]
end

subgraph group_policy["Policy &amp; Decisions"]
  node_policy_engine["Policy evaluator<br/>authorization engine<br/>[policy.mjs]"]
  node_agentic_defense["Suspicion signals<br/>content evaluator"]
  node_approvals["Approval workflow<br/>decision state<br/>[approvals.mjs]"]
end

subgraph group_provenance["Provenance &amp; Evidence"]
  node_signing["Receipt chain signer<br/>provenance signer<br/>[signing.mjs]"]
  node_artifacts["Artifact provenance<br/>package service<br/>[artifacts.mjs]"]
  node_evidence["Evidence ledger &amp; packages<br/>evidence service<br/>[evidence.mjs]"]
end

subgraph group_durability["Durability &amp; Release Assurance"]
  node_storage[("Ledger storage abstraction<br/>persistence adapter<br/>[storage.mjs]")]
  node_postgres[("PostgreSQL ledger<br/>production datastore<br/>[storage.mjs]")]
  node_release_evidence["Release replay evaluator<br/>release assurance"]
  node_replay_fixture["Reviewed replay suite<br/>fixture"]
  node_receipt_verifier["Offline receipt verifier<br/>CLI verifier<br/>[verify-receipt.mjs]"]
  node_release_verifier["Release gate verifier<br/>CLI verifier<br/>[verify-release.mjs]"]
end

node_demo -->|"calls APIs"| node_server
node_teaching_graph -->|"renders guidance for"| node_demo
node_learning_pages -.->|"links to"| node_pentel
node_pentel -.->|"synthetic interactions"| node_server
node_server -->|"validates"| node_request_context
node_request_context -->|"scoped request"| node_policy_engine
node_server -->|"authorizes capability"| node_policy_engine
node_agentic_defense -->|"quarantine signals"| node_policy_engine
node_policy_engine -->|"approval-required action"| node_approvals
node_approvals -->|"re-evaluates resolution"| node_policy_engine
node_policy_engine -->|"decision"| node_signing
node_signing -->|"persists chained receipt"| node_storage
node_storage -->|"production mode"| node_postgres
node_signing -->|"allowed receipt"| node_artifacts
node_signing -->|"decision record"| node_evidence
node_server -->|"export and verify API"| node_artifacts
node_server -->|"evidence API"| node_evidence
node_receipt_verifier -->|"verifies signatures and chain"| node_signing
node_replay_fixture -->|"replay cases"| node_release_evidence
node_release_evidence -->|"records release evidence"| node_evidence
node_release_verifier -->|"release gate"| node_release_evidence

click node_demo "https://github.com/rishvaiyer/canarynorth/blob/main/public/app.js"
click node_teaching_graph "https://github.com/rishvaiyer/canarynorth/blob/main/public/graph.mjs"
click node_learning_pages "https://github.com/rishvaiyer/canarynorth/blob/main/public/learn.html"
click node_pentel "https://github.com/rishvaiyer/canarynorth/blob/main/public/pen-console/index.html"
click node_server "https://github.com/rishvaiyer/canarynorth/blob/main/server.mjs"
click node_request_context "https://github.com/rishvaiyer/canarynorth/blob/main/server.mjs"
click node_policy_engine "https://github.com/rishvaiyer/canarynorth/blob/main/src/policy.mjs"
click node_agentic_defense "https://github.com/rishvaiyer/canarynorth/blob/main/src/agentic-defense.mjs"
click node_approvals "https://github.com/rishvaiyer/canarynorth/blob/main/src/approvals.mjs"
click node_signing "https://github.com/rishvaiyer/canarynorth/blob/main/src/signing.mjs"
click node_artifacts "https://github.com/rishvaiyer/canarynorth/blob/main/src/artifacts.mjs"
click node_evidence "https://github.com/rishvaiyer/canarynorth/blob/main/src/evidence.mjs"
click node_storage "https://github.com/rishvaiyer/canarynorth/blob/main/src/storage.mjs"
click node_postgres "https://github.com/rishvaiyer/canarynorth/blob/main/src/storage.mjs"
click node_release_evidence "https://github.com/rishvaiyer/canarynorth/blob/main/src/release-evidence.mjs"
click node_replay_fixture "https://github.com/rishvaiyer/canarynorth/blob/main/fixtures/release-replays.v1.json"
click node_receipt_verifier "https://github.com/rishvaiyer/canarynorth/blob/main/scripts/verify-receipt.mjs"
click node_release_verifier "https://github.com/rishvaiyer/canarynorth/blob/main/scripts/verify-release.mjs"

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_demo,node_teaching_graph,node_learning_pages,node_pentel toneBlue
class node_server,node_request_context toneAmber
class node_policy_engine,node_agentic_defense,node_approvals toneMint
class node_signing,node_artifacts,node_evidence toneRose
class node_storage,node_postgres,node_release_evidence,node_replay_fixture,node_receipt_verifier,node_release_verifier toneIndigo
```

## Components

### Client & Learning Surfaces

- [Browser demo](https://github.com/rishvaiyer/canarynorth/blob/main/public/app.js): static client · Synthetic capability-request interface. · `public/app.js`
- [Guided teaching graph](https://github.com/rishvaiyer/canarynorth/blob/main/public/graph.mjs): client visualization · `public/graph.mjs`
- [Learning routes](https://github.com/rishvaiyer/canarynorth/blob/main/public/learn.html): static pages · `public/learn.html`
- [PenTel console](https://github.com/rishvaiyer/canarynorth/blob/main/public/pen-console/index.html): static teaching UI · Synthetic security interaction layer. · `public/pen-console/index.html`

### HTTP Enforcement

- [Proxy HTTP service](https://github.com/rishvaiyer/canarynorth/blob/main/server.mjs): Node entry point · API and protected capability boundary. · `server.mjs`
- [Production request context](https://github.com/rishvaiyer/canarynorth/blob/main/server.mjs): auth boundary · Bearer identity, tenant scope, policy version, and nonce. · `server.mjs`

### Policy & Decisions

- [Policy evaluator](https://github.com/rishvaiyer/canarynorth/blob/main/src/policy.mjs): authorization engine · Deny-by-default capability, scope, binding, and expiry checks. · `src/policy.mjs`
- [Suspicion signals](https://github.com/rishvaiyer/canarynorth/blob/main/src/agentic-defense.mjs): content evaluator · Quarantines injection, credential, DLP, and unsafe-export-shaped inputs. · `src/agentic-defense.mjs`
- [Approval workflow](https://github.com/rishvaiyer/canarynorth/blob/main/src/approvals.mjs): decision state · Short-lived approvals re-check policy and nonce conditions. · `src/approvals.mjs`

### Provenance & Evidence

- [Receipt chain signer](https://github.com/rishvaiyer/canarynorth/blob/main/src/signing.mjs): provenance signer · Creates predecessor-linked signed decisions. · `src/signing.mjs`
- [Artifact provenance](https://github.com/rishvaiyer/canarynorth/blob/main/src/artifacts.mjs): package service · Binds allowed receipts to hashed, signed artifact manifests. · `src/artifacts.mjs`
- [Evidence ledger & packages](https://github.com/rishvaiyer/canarynorth/blob/main/src/evidence.mjs): evidence service · Redaction-aware ledger with encrypted packages. · `src/evidence.mjs`

### Durability & Release Assurance

- [Ledger storage abstraction](https://github.com/rishvaiyer/canarynorth/blob/main/src/storage.mjs): persistence adapter · Memory, append-only JSONL, or PostgreSQL persistence. · `src/storage.mjs`
- [PostgreSQL ledger](https://github.com/rishvaiyer/canarynorth/blob/main/src/storage.mjs): production datastore · Receipt and nonce tables serialize chain writes and prevent replay. · `src/storage.mjs`
- [Release replay evaluator](https://github.com/rishvaiyer/canarynorth/blob/main/src/release-evidence.mjs): release assurance · Compares reviewed and candidate policy outcomes. · `src/release-evidence.mjs`
- [Reviewed replay suite](https://github.com/rishvaiyer/canarynorth/blob/main/fixtures/release-replays.v1.json): fixture · `fixtures/release-replays.v1.json`
- [Offline receipt verifier](https://github.com/rishvaiyer/canarynorth/blob/main/scripts/verify-receipt.mjs): CLI verifier · `scripts/verify-receipt.mjs`
- [Release gate verifier](https://github.com/rishvaiyer/canarynorth/blob/main/scripts/verify-release.mjs): CLI verifier · `scripts/verify-release.mjs`

## Connections

- Browser demo → Proxy HTTP service: calls APIs
- Guided teaching graph → Browser demo: renders guidance for
- Learning routes → PenTel console: links to
- PenTel console → Proxy HTTP service: synthetic interactions
- Proxy HTTP service → Production request context: validates
- Production request context → Policy evaluator: scoped request
- Proxy HTTP service → Policy evaluator: authorizes capability
- Suspicion signals → Policy evaluator: quarantine signals
- Policy evaluator → Approval workflow: approval-required action
- Approval workflow → Policy evaluator: re-evaluates resolution
- Policy evaluator → Receipt chain signer: decision
- Receipt chain signer → Ledger storage abstraction: persists chained receipt
- Ledger storage abstraction → PostgreSQL ledger: production mode
- Receipt chain signer → Artifact provenance: allowed receipt
- Receipt chain signer → Evidence ledger & packages: decision record
- Proxy HTTP service → Artifact provenance: export and verify API
- Proxy HTTP service → Evidence ledger & packages: evidence API
- Offline receipt verifier → Receipt chain signer: verifies signatures and chain
- Reviewed replay suite → Release replay evaluator: replay cases
- Release replay evaluator → Evidence ledger & packages: records release evidence
- Release gate verifier → Release replay evaluator: release gate
