# sharma-open-source/opensentry architecture

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

- Interactive diagram: https://gitdiagram.com/sharma-open-source/opensentry
- Repository: https://github.com/sharma-open-source/opensentry

## Overview

#### Purpose and public surface
`opensentry` is a TypeScript prompt-injection validation library with a synchronous, zero-dependency Tier 0 and optional asynchronous semantic tiers. The principal API is `createGuard`, exposing single-text, message, stream, wrapper, and tool-call checks. Package entry/export wiring begins at `src/index.ts`; public contracts live in `src/types.ts`; orchestration is centered in `src/guard.ts`.

#### Guard orchestration and policy
A guard instance applies configured modes (`enforce`, `soft`, `shadow`), per-source rules, thresholds, hard blocks, cache behavior, metrics, and fail-open/closed decisions. It selects escalation based on source, flag-band status, and high-risk actions, then re-decides verdicts as evidence arrives. Relevant paths: `src/guard.ts`, `src/config.ts`, `src/cache.ts`.

#### Tier 0 synchronous detection pipeline
The edge-safe hot path first bounds input and detects flooding, then maintains separate model-facing and matching representations. It normalizes Unicode/invisible/confusable content, performs entropy-gated decoding and rescanning, and applies structural injection patterns. Stage implementations are organized in `src/tiers/l0.ts`, `src/normalize/normalize.ts`, `src/tiers/l2.ts`, and `src/tiers/l3.ts`. This tier avoids Node built-ins and is intended for Node, Deno, Bun, and Worker runtimes.

#### Evidence aggregation and decisions
Detectors emit typed reasons with weights and spans. Scores are combined through noisy-OR, mapped through source-specific thresholds and operating mode, and returned with sanitized/normalized copies, tier, latency, and degradation state. This evidence boundary is represented by `src/scoring.ts`, `src/reason.ts`, and `src/types.ts`. Hard-block rules can override normal threshold behavior.

#### Optional semantic escalation
Tier 1 invokes a local classifier only under escalation policy; it chunks long content, loads a singleton runner, applies timeout/circuit-breaker behavior, and folds classifier output into existing evidence. Core ML control code is in `src/ml/chunker.ts`, `src/ml/circuit-breaker.ts`, and `src/ml/singleton.ts`. Runtime-specific model integrations are split between Node ONNX (`src/onnx/index.ts`) and WASM/web ONNX (`src/wasm/index.ts`), using optional Transformers/ONNX dependencies.

Tier 2 can run an embedding-corpus signal and/or a caller-provided remote guard. Remote content is spotlight-delimited before transmission, and failures surface as degraded results rather than silently changing the decision. Paths: `src/embedding/index.ts`, `src/remote/index.ts`, `src/spotlight/index.ts`.

#### Integration and privileged-action boundaries
Framework adapters structurally integrate guard checks into Express, Hono, and Next request flows: `src/middleware/express.ts`, `src/middleware/hono.ts`, `src/middleware/next.ts`. `checkToolCall` is a pre-execution boundary: it enforces an allowlist, scans serialized arguments as high risk, and can reject tainted untrusted data entering privileged actions. Provenance tracking is in `src/taint/index.ts`.

#### Defense-in-depth companions and state
Independent subpath modules address boundaries outside stateless input scanning: prompt assembly and spotlighting (`src/prompt/index.ts`, `src/spotlight/index.ts`), outbound URL/secret/PII filtering (`src/egress/index.ts`), system-prompt leak canaries (`src/canary/index.ts`), and multi-turn risk accumulation with pluggable session storage (`src/session/index.ts`). These are opt-in layers around LLM input, output, and agent execution paths rather than prerequisites for the core guard.

#### Quality and distribution
The package is bundled from TypeScript via `tsup.config.ts`, with runtime compatibility and zero-Node-core guarantees architecturally important to Tier 0. Seed corpora and evaluation gates reside in `corpus/` and `eval/corpora.test.ts`; larger real-corpus benchmarking and optional external model comparisons live in `bench/`. CI, release, and package distribution are defined by `.github/workflows/ci.yml`, `.github/workflows/release.yml`, and `package.json`.

Main entry points: [`src/guard.ts`](https://github.com/sharma-open-source/opensentry/blob/main/src/guard.ts) (Guard orchestrator), [`src/tiers/l0.ts`](https://github.com/sharma-open-source/opensentry/blob/main/src/tiers/l0.ts) (Tier 0 detector), [`src/scoring.ts`](https://github.com/sharma-open-source/opensentry/blob/main/src/scoring.ts) (Evidence scoring), [`src/ml/singleton.ts`](https://github.com/sharma-open-source/opensentry/blob/main/src/ml/singleton.ts) (Tier 1 classifier control)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["Core validation"]
  node_api["Public API<br/>entrypoint<br/>[index.ts]"]
  node_contracts["Public contracts<br/>types<br/>[types.ts]"]
  node_guard["Guard orchestrator<br/>[guard.ts]"]
  node_policy["Policy, cache and modes<br/>policy-state<br/>[config.ts]"]
  node_tier0["Tier 0 detector<br/>sync-pipeline<br/>[l0.ts]"]
  node_normalization["Normalization<br/>text-processing<br/>[normalize.ts]"]
  node_structural["Structural detectors<br/>[l2.ts]"]
  node_evidence["Evidence scoring<br/>decision-engine<br/>[scoring.ts]"]
  node_reasons["Typed reasons<br/>evidence<br/>[reason.ts]"]
end

subgraph group_semantic["Semantic escalation"]
  node_tier1["Tier 1 classifier control<br/>ml-control<br/>[singleton.ts]"]
  node_onnx["Node ONNX runtime<br/>optional-runtime<br/>[index.ts]"]
  node_wasm["WASM ONNX runtime<br/>optional-runtime<br/>[index.ts]"]
  node_tier2["Tier 2 signals<br/>semantic-signals<br/>[index.ts]"]
  node_spotlight["Spotlight delimiter<br/>content-boundary<br/>[index.ts]"]
end

subgraph group_boundaries["Integration boundaries"]
  node_middleware["Framework middleware<br/>adapters<br/>[express.ts]"]
  node_toolcall["Tool-call boundary<br/>privileged-action<br/>[index.ts]"]
  node_companions["Opt-in companion layers<br/>defense-in-depth<br/>[index.ts]"]
end

subgraph group_quality["Distribution and quality"]
  node_build["Package build<br/>bundling<br/>[tsup.config.ts]"]
  node_evaluation["Evaluation corpora<br/>quality-gate<br/>[corpora.test.ts]"]
  node_release["CI and release<br/>distribution<br/>[ci.yml]"]
end

node_api -->|"exports"| node_contracts
node_api -->|"creates"| node_guard
node_guard -->|"applies"| node_policy
node_guard -->|"runs first"| node_tier0
node_tier0 -->|"normalizes"| node_normalization
node_tier0 -->|"rescans"| node_structural
node_tier0 -->|"emits"| node_reasons
node_reasons -->|"weighted evidence"| node_evidence
node_policy -->|"thresholds and hard blocks"| node_evidence
node_evidence -->|"verdict updates"| node_guard
node_guard -.->|"policy escalation"| node_tier1
node_tier1 -.->|"Node runtime"| node_onnx
node_tier1 -.->|"web runtime"| node_wasm
node_tier1 -->|"classifier evidence"| node_evidence
node_guard -.->|"optional escalation"| node_tier2
node_spotlight -->|"delimited content"| node_tier2
node_tier2 -->|"signal or degradation"| node_evidence
node_middleware -->|"checks requests"| node_guard
node_toolcall -->|"checks arguments"| node_guard
node_companions -.->|"protects surrounding flows"| node_guard
node_build -->|"bundles"| node_api
node_evaluation -.->|"tests detection"| node_tier0
node_release -->|"runs"| node_build
node_release -->|"gates"| node_evaluation

click node_api "https://github.com/sharma-open-source/opensentry/blob/main/src/index.ts"
click node_contracts "https://github.com/sharma-open-source/opensentry/blob/main/src/types.ts"
click node_guard "https://github.com/sharma-open-source/opensentry/blob/main/src/guard.ts"
click node_policy "https://github.com/sharma-open-source/opensentry/blob/main/src/config.ts"
click node_tier0 "https://github.com/sharma-open-source/opensentry/blob/main/src/tiers/l0.ts"
click node_normalization "https://github.com/sharma-open-source/opensentry/blob/main/src/normalize/normalize.ts"
click node_structural "https://github.com/sharma-open-source/opensentry/blob/main/src/tiers/l2.ts"
click node_evidence "https://github.com/sharma-open-source/opensentry/blob/main/src/scoring.ts"
click node_reasons "https://github.com/sharma-open-source/opensentry/blob/main/src/reason.ts"
click node_tier1 "https://github.com/sharma-open-source/opensentry/blob/main/src/ml/singleton.ts"
click node_onnx "https://github.com/sharma-open-source/opensentry/blob/main/src/onnx/index.ts"
click node_wasm "https://github.com/sharma-open-source/opensentry/blob/main/src/wasm/index.ts"
click node_tier2 "https://github.com/sharma-open-source/opensentry/blob/main/src/embedding/index.ts"
click node_spotlight "https://github.com/sharma-open-source/opensentry/blob/main/src/spotlight/index.ts"
click node_middleware "https://github.com/sharma-open-source/opensentry/blob/main/src/middleware/express.ts"
click node_toolcall "https://github.com/sharma-open-source/opensentry/blob/main/src/taint/index.ts"
click node_companions "https://github.com/sharma-open-source/opensentry/blob/main/src/prompt/index.ts"
click node_build "https://github.com/sharma-open-source/opensentry/blob/main/tsup.config.ts"
click node_evaluation "https://github.com/sharma-open-source/opensentry/blob/main/eval/corpora.test.ts"
click node_release "https://github.com/sharma-open-source/opensentry/blob/main/.github/workflows/ci.yml"

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_api,node_contracts,node_guard,node_policy,node_tier0,node_normalization,node_structural,node_evidence,node_reasons toneBlue
class node_tier1,node_onnx,node_wasm,node_tier2,node_spotlight toneAmber
class node_middleware,node_toolcall,node_companions toneMint
class node_build,node_evaluation,node_release toneRose
```

## Components

### Core validation

Synchronous guard orchestration and evidence decisions.

- [Public API](https://github.com/sharma-open-source/opensentry/blob/main/src/index.ts): entrypoint · Exports createGuard and public checks. · `src/index.ts`
- [Public contracts](https://github.com/sharma-open-source/opensentry/blob/main/src/types.ts): types · Typed inputs, verdicts, reasons, and results. · `src/types.ts`
- [Guard orchestrator](https://github.com/sharma-open-source/opensentry/blob/main/src/guard.ts): orchestrator · Selects tiers and re-decides as evidence arrives. · `src/guard.ts`
- [Policy, cache and modes](https://github.com/sharma-open-source/opensentry/blob/main/src/config.ts): policy-state · Controls source rules, thresholds, cache, metrics, and failure behavior. · `src/config.ts`
- [Tier 0 detector](https://github.com/sharma-open-source/opensentry/blob/main/src/tiers/l0.ts): sync-pipeline · Bounds input, detects flooding, decodes, rescans, and matches injection structure. · `src/tiers/l0.ts`
- [Normalization](https://github.com/sharma-open-source/opensentry/blob/main/src/normalize/normalize.ts): text-processing · Maintains safe matching forms across Unicode and invisible content. · `src/normalize/normalize.ts`
- [Structural detectors](https://github.com/sharma-open-source/opensentry/blob/main/src/tiers/l2.ts): detectors · Additional pattern stages used by the synchronous pipeline. · `src/tiers/l2.ts`
- [Evidence scoring](https://github.com/sharma-open-source/opensentry/blob/main/src/scoring.ts): decision-engine · Aggregates weighted reasons, hard blocks, thresholds, and verdict metadata. · `src/scoring.ts`
- [Typed reasons](https://github.com/sharma-open-source/opensentry/blob/main/src/reason.ts): evidence · Detector reason weights and spans. · `src/reason.ts`

### Semantic escalation

Optional asynchronous detection tiers.

- [Tier 1 classifier control](https://github.com/sharma-open-source/opensentry/blob/main/src/ml/singleton.ts): ml-control · Chunks content and manages singleton, timeout, and circuit-breaker execution. · `src/ml/singleton.ts`
- [Node ONNX runtime](https://github.com/sharma-open-source/opensentry/blob/main/src/onnx/index.ts): optional-runtime · Optional Node model integration. · `src/onnx/index.ts`
- [WASM ONNX runtime](https://github.com/sharma-open-source/opensentry/blob/main/src/wasm/index.ts): optional-runtime · Optional web and Worker model integration. · `src/wasm/index.ts`
- [Tier 2 signals](https://github.com/sharma-open-source/opensentry/blob/main/src/embedding/index.ts): semantic-signals · Optional embedding corpus and caller-provided remote guard. · `src/embedding/index.ts`
- [Spotlight delimiter](https://github.com/sharma-open-source/opensentry/blob/main/src/spotlight/index.ts): content-boundary · Delimits content sent to remote semantic checks. · `src/spotlight/index.ts`

### Integration boundaries

Request, agent, and LLM companion protections.

- [Framework middleware](https://github.com/sharma-open-source/opensentry/blob/main/src/middleware/express.ts): adapters · Express, Hono, and Next request-flow integration. · `src/middleware/express.ts`
- [Tool-call boundary](https://github.com/sharma-open-source/opensentry/blob/main/src/taint/index.ts): privileged-action · Allowlists tools and scans serialized high-risk arguments before execution. · `src/taint/index.ts`
- [Opt-in companion layers](https://github.com/sharma-open-source/opensentry/blob/main/src/prompt/index.ts): defense-in-depth · Prompt assembly, egress filtering, canaries, and session risk accumulation. · `src/prompt/index.ts`

### Distribution and quality

Build, evaluation, and release controls.

- [Package build](https://github.com/sharma-open-source/opensentry/blob/main/tsup.config.ts): bundling · Bundles TypeScript while preserving Tier 0 runtime compatibility. · `tsup.config.ts`
- [Evaluation corpora](https://github.com/sharma-open-source/opensentry/blob/main/eval/corpora.test.ts): quality-gate · Seed corpora and evaluation gates validate detection quality. · `eval/corpora.test.ts`
- [CI and release](https://github.com/sharma-open-source/opensentry/blob/main/.github/workflows/ci.yml): distribution · Build, test, publish, and package distribution controls. · `.github/workflows/ci.yml`

## Connections

- Public API → Public contracts: exports
- Public API → Guard orchestrator: creates
- Guard orchestrator → Policy, cache and modes: applies
- Guard orchestrator → Tier 0 detector: runs first
- Tier 0 detector → Normalization: normalizes
- Tier 0 detector → Structural detectors: rescans
- Tier 0 detector → Typed reasons: emits
- Typed reasons → Evidence scoring: weighted evidence
- Policy, cache and modes → Evidence scoring: thresholds and hard blocks
- Evidence scoring → Guard orchestrator: verdict updates
- Guard orchestrator → Tier 1 classifier control: policy escalation
- Tier 1 classifier control → Node ONNX runtime: Node runtime
- Tier 1 classifier control → WASM ONNX runtime: web runtime
- Tier 1 classifier control → Evidence scoring: classifier evidence
- Guard orchestrator → Tier 2 signals: optional escalation
- Spotlight delimiter → Tier 2 signals: delimited content
- Tier 2 signals → Evidence scoring: signal or degradation
- Framework middleware → Guard orchestrator: checks requests
- Tool-call boundary → Guard orchestrator: checks arguments
- Opt-in companion layers → Guard orchestrator: protects surrounding flows
- Package build → Public API: bundles
- Evaluation corpora → Tier 0 detector: tests detection
- CI and release → Package build: runs
- CI and release → Evaluation corpora: gates
