# coderifts/agent-guard architecture

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

- Interactive diagram: https://gitdiagram.com/coderifts/agent-guard
- Repository: https://github.com/coderifts/agent-guard

## Overview

@coderifts/agent-guard is a fail-closed runtime boundary for AI-agent tool calls: it detects and preflights contract-changing mutations, verifies a decision, then conditionally permits deferred execution. The graph follows registry construction through guard evaluation and execution, with separate branches for adapters, freshness/CAS safeguards, monitoring, and proof. README documents execution-state rechecks and post-commit observation; source excerpts establish key control and adapter links, while unsampled subsystem wiring is not assumed.

Main entry points: [`src/guard.ts`](https://github.com/coderifts/agent-guard/blob/main/src/guard.ts) (Tool-call guard), [`src/execution-proof.ts`](https://github.com/coderifts/agent-guard/blob/main/src/execution-proof.ts) (Execution proof), [`src/final-answer-proof.ts`](https://github.com/coderifts/agent-guard/blob/main/src/final-answer-proof.ts) (Final-answer proof), [`src/with-coderifts.ts`](https://github.com/coderifts/agent-guard/blob/main/src/with-coderifts.ts) (CodeRifts composition)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Agent integration"]
  node_adapters["Framework adapters"]
  node_composition["CodeRifts composition<br/>[with-coderifts.ts]"]
  node_registry["Guarded tool registry<br/>[tool-registry.ts]"]
end

subgraph group_decision["Preflight and decision"]
  node_guard["Tool-call guard<br/>[guard.ts]"]
  node_detection["Change detection<br/>[detector.ts]"]
  node_artifacts["Artifact resolution"]
  node_decision_read["Decision and policy<br/>[read-decision.ts]"]
end

subgraph group_execution["Execution safeguards"]
  node_freshness["Freshness checks<br/>[freshness.ts]"]
  node_state_recheck["Execution-state recheck<br/>[auto-recheck.ts]"]
  node_conditional["Conditional write contract"]
  node_cas_adapters["CAS adapters"]
  node_commit_observation["Commit observation"]
  node_execute["Deferred tool execution"]
end

subgraph group_assurance["Evidence and monitoring"]
  node_coverage["Coverage observation"]
  node_monitoring["Monitoring delivery"]
  node_monitor_attest["Monitoring attestation"]
  node_proof["Execution proof<br/>[execution-proof.ts]"]
  node_final_proof["Final-answer proof"]
  node_cas_attest["CAS attestation<br/>[cas-attestation.ts]"]
end

node_agent(("Agent host"))
node_sdk["CodeRifts SDK"]
node_host_store[("Host resource store")]

node_agent -->|"uses"| node_adapters
node_adapters -->|"wraps"| node_composition
node_composition -->|"constructs"| node_registry
node_registry -->|"guards calls"| node_guard
node_registry -->|"dispatches"| node_execute
node_guard -->|"detects changes"| node_detection
node_guard -->|"reads verdict"| node_decision_read
node_guard -->|"checks freshness"| node_freshness
node_guard -.->|"rechecks state"| node_state_recheck
node_guard -->|"requires contract"| node_conditional
node_detection -.->|"resolves artifacts"| node_artifacts
node_guard -->|"preflights"| node_sdk
node_execute -.->|"host writes"| node_host_store
node_cas_adapters -->|"executes conditionally"| node_conditional
node_host_store -.->|"reads and writes"| node_cas_adapters
node_commit_observation -->|"observes after write"| node_cas_adapters
node_composition -.->|"observes tool traffic"| node_coverage
node_monitoring -->|"attests delivery"| node_monitor_attest
node_guard -.->|"delivers events"| node_monitoring
node_guard -.->|"builds proof"| node_proof
node_proof -->|"includes observation"| node_coverage
node_final_proof -->|"renders proof"| node_proof
node_final_proof -->|"formats delivery"| node_monitoring
node_final_proof -->|"formats attestation"| node_monitor_attest
node_cas_attest -->|"verifies token"| node_sdk
node_cas_attest -->|"binds proof"| node_proof

click node_adapters "https://github.com/coderifts/agent-guard/tree/main/src/adapters"
click node_composition "https://github.com/coderifts/agent-guard/blob/main/src/with-coderifts.ts"
click node_registry "https://github.com/coderifts/agent-guard/blob/main/src/tool-registry.ts"
click node_guard "https://github.com/coderifts/agent-guard/blob/main/src/guard.ts"
click node_detection "https://github.com/coderifts/agent-guard/blob/main/src/detector.ts"
click node_artifacts "https://github.com/coderifts/agent-guard/blob/main/src/artifact-resolver.ts"
click node_decision_read "https://github.com/coderifts/agent-guard/blob/main/src/read-decision.ts"
click node_freshness "https://github.com/coderifts/agent-guard/blob/main/src/freshness.ts"
click node_state_recheck "https://github.com/coderifts/agent-guard/blob/main/src/auto-recheck.ts"
click node_conditional "https://github.com/coderifts/agent-guard/blob/main/src/conditional-write.ts"
click node_cas_adapters "https://github.com/coderifts/agent-guard/tree/main/src/cas-adapters"
click node_commit_observation "https://github.com/coderifts/agent-guard/blob/main/src/commit-observation.ts"
click node_execute "https://github.com/coderifts/agent-guard/blob/main/src/execute-tool-call.ts"
click node_coverage "https://github.com/coderifts/agent-guard/blob/main/src/coverage-observed.ts"
click node_monitoring "https://github.com/coderifts/agent-guard/blob/main/src/monitoring-delivery.ts"
click node_monitor_attest "https://github.com/coderifts/agent-guard/blob/main/src/monitoring-attestation.ts"
click node_proof "https://github.com/coderifts/agent-guard/blob/main/src/execution-proof.ts"
click node_final_proof "https://github.com/coderifts/agent-guard/blob/main/src/final-answer-proof.ts"
click node_cas_attest "https://github.com/coderifts/agent-guard/blob/main/src/cas-attestation.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_adapters,node_composition,node_registry toneBlue
class node_guard,node_detection,node_artifacts,node_decision_read,node_host_store toneAmber
class node_freshness,node_state_recheck,node_conditional,node_cas_adapters,node_commit_observation,node_execute toneMint
class node_coverage,node_monitoring,node_monitor_attest,node_proof,node_final_proof,node_cas_attest toneRose
class node_agent,node_sdk toneIndigo
```

## Components

### Agent integration

- [Framework adapters](https://github.com/coderifts/agent-guard/tree/main/src/adapters): `src/adapters`
- [CodeRifts composition](https://github.com/coderifts/agent-guard/blob/main/src/with-coderifts.ts): `src/with-coderifts.ts`
- [Guarded tool registry](https://github.com/coderifts/agent-guard/blob/main/src/tool-registry.ts): `src/tool-registry.ts`

### Preflight and decision

- [Tool-call guard](https://github.com/coderifts/agent-guard/blob/main/src/guard.ts): `src/guard.ts`
- [Change detection](https://github.com/coderifts/agent-guard/blob/main/src/detector.ts): `src/detector.ts`
- [Artifact resolution](https://github.com/coderifts/agent-guard/blob/main/src/artifact-resolver.ts): `src/artifact-resolver.ts`
- [Decision and policy](https://github.com/coderifts/agent-guard/blob/main/src/read-decision.ts): `src/read-decision.ts`

### Execution safeguards

- [Freshness checks](https://github.com/coderifts/agent-guard/blob/main/src/freshness.ts): `src/freshness.ts`
- [Execution-state recheck](https://github.com/coderifts/agent-guard/blob/main/src/auto-recheck.ts): `src/auto-recheck.ts`
- [Conditional write contract](https://github.com/coderifts/agent-guard/blob/main/src/conditional-write.ts): `src/conditional-write.ts`
- [CAS adapters](https://github.com/coderifts/agent-guard/tree/main/src/cas-adapters): `src/cas-adapters`
- [Commit observation](https://github.com/coderifts/agent-guard/blob/main/src/commit-observation.ts): `src/commit-observation.ts`
- [Deferred tool execution](https://github.com/coderifts/agent-guard/blob/main/src/execute-tool-call.ts): `src/execute-tool-call.ts`

### Evidence and monitoring

- [Coverage observation](https://github.com/coderifts/agent-guard/blob/main/src/coverage-observed.ts): `src/coverage-observed.ts`
- [Monitoring delivery](https://github.com/coderifts/agent-guard/blob/main/src/monitoring-delivery.ts): `src/monitoring-delivery.ts`
- [Monitoring attestation](https://github.com/coderifts/agent-guard/blob/main/src/monitoring-attestation.ts): `src/monitoring-attestation.ts`
- [Execution proof](https://github.com/coderifts/agent-guard/blob/main/src/execution-proof.ts): `src/execution-proof.ts`
- [Final-answer proof](https://github.com/coderifts/agent-guard/blob/main/src/final-answer-proof.ts): `src/final-answer-proof.ts`
- [CAS attestation](https://github.com/coderifts/agent-guard/blob/main/src/cas-attestation.ts): `src/cas-attestation.ts`

### Other components

- **Agent host**
- **CodeRifts SDK**
- **Host resource store**

## Connections

- Agent host → Framework adapters: uses (evidence: `README.md`)
- Framework adapters → CodeRifts composition: wraps (evidence: `src/adapters/openai.ts`)
- CodeRifts composition → Guarded tool registry: constructs (evidence: `src/with-coderifts.ts`)
- Guarded tool registry → Tool-call guard: guards calls (evidence: `src/tool-registry.ts`)
- Guarded tool registry → Deferred tool execution: dispatches (evidence: `src/tool-registry.ts`)
- Tool-call guard → Change detection: detects changes (evidence: `src/guard.ts`)
- Tool-call guard → Decision and policy: reads verdict (evidence: `src/guard.ts`)
- Tool-call guard → Freshness checks: checks freshness (evidence: `src/guard.ts`)
- Tool-call guard → Execution-state recheck: rechecks state
- Tool-call guard → Conditional write contract: requires contract (evidence: `src/guard.ts`)
- Change detection → Artifact resolution: resolves artifacts
- Tool-call guard → CodeRifts SDK: preflights (evidence: `README.md`)
- Deferred tool execution → Host resource store: host writes (evidence: `README.md`)
- CAS adapters → Conditional write contract: executes conditionally (evidence: `src/cas-adapters/fs.ts`)
- Host resource store → CAS adapters: reads and writes
- Commit observation → CAS adapters: observes after write (evidence: `src/commit-observation.ts`)
- CodeRifts composition → Coverage observation: observes tool traffic (evidence: `src/with-coderifts.ts`)
- Monitoring delivery → Monitoring attestation: attests delivery (evidence: `src/monitoring-attestation.ts`)
- Tool-call guard → Monitoring delivery: delivers events (evidence: `src/guard.ts`)
- Tool-call guard → Execution proof: builds proof (evidence: `src/guard.ts`)
- Execution proof → Coverage observation: includes observation (evidence: `src/execution-proof.ts`)
- Final-answer proof → Execution proof: renders proof (evidence: `src/final-answer-proof.ts`)
- Final-answer proof → Monitoring delivery: formats delivery (evidence: `src/final-answer-proof.ts`)
- Final-answer proof → Monitoring attestation: formats attestation (evidence: `src/final-answer-proof.ts`)
- CAS attestation → CodeRifts SDK: verifies token (evidence: `src/cas-attestation.ts`)
- CAS attestation → Execution proof: binds proof (evidence: `src/cas-attestation.ts`)
