# xagi-lab/melra architecture

> Architecture diagram of the GitHub repository xagi-lab/melra, 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/xagi-lab/melra
- Repository: https://github.com/xagi-lab/melra

## Overview

#### Purpose and Entry Points
MELRA is an agent-independent, durable execution kernel for bounded external effects. It owns policy, approval, persistence, execution, verification, and auditable receipts; LLMs and harnesses remain responsible for reasoning and planning.

Primary entry points:
- CLI and stdio server: `apps/cli/src/bin.ts`, `apps/cli/src/index.ts`
- Runtime composition, MCP, and HTTP transports: `packages/server/src/runtime.ts`, `packages/server/src/mcp-server.ts`, `packages/server/src/http-server.ts`
- TypeScript and Python clients: `packages/sdk-ts/src/index.ts`, `sdk-py/src/melra/client.py`

#### Public Contract and Workflow Model
Strict schemas define bounded operations, tasks, workflows, events, and node kinds. Workflows compose effect nodes with approval, conditions, parallelism, loops, checkpoints, compensation, human input, and delegation.
- `packages/protocol/src/index.ts`
- `packages/protocol/src/workflow.ts`
- `packages/receipt-schema/src/index.ts`

Clients reach one common runtime through MCP, CLI, SDK, or token-protected loopback HTTP. Transport is a boundary only; it must not create alternate execution semantics.

#### Task Lifecycle and Durable Control Plane
`runtime-core` implements the effect lifecycle: plan/persist, policy recheck, approval gate, idempotent execution, cancellation/budgets, verification, recovery, event emission, and task/workflow state transitions. Mutations interrupted after dispatch are not silently replayed; they require reconciliation or recovery handling.
- `packages/runtime-core/src/task-controller.ts`
- `packages/runtime-core/src/workflow-controller.ts`
- `packages/runtime-core/src/workflow-graph.ts`
- `packages/runtime-core/src/verification-gate.ts`

Key control flow: request → schema validation/classification → plan-time policy → durable record → execution-time policy/approval → adapter → evidence verification → event/projection → receipt.

#### Governance, Identity, and Secrets
Policy distinguishes reads, mutations, and destructive actions; it constrains paths, commands, network destinations, and capabilities. Approval is exact, scoped, and expiring. Server-side OAuth/client identity and the credential broker ensure agents receive scoped capabilities rather than raw credentials.
- `packages/policy-core/src/index.ts`
- `packages/policy-core/src/network-policy.ts`
- `packages/policy-core/src/credentials.ts`
- `packages/server/src/oauth.ts`

Deployment mode is material: developer mode governs requested effects; enforced mode rejects unsafe-local operation, non-loopback binding, and self-registration assumptions.

#### Persistence, Encryption, and Audit Evidence
SQLite is the local transactional system of record for tasks, workflows, events, idempotency state, evidence, and projections. Exact executable payloads are protected with an on-host key and encrypted envelopes; receipts are redacted and certificate-oriented.
- `packages/storage-sqlite/src/index.ts`
- `packages/storage-sqlite/src/workflow-store.test.ts`
- `packages/runtime-core/src/payload-cipher.ts`
- `packages/server/src/payload-key.ts`

Operational memory is separate from agent semantic memory: it stores scoped, redacted, provenance-aware knowledge about prior effects.
- `packages/memory/src/index.ts`

#### Verification and Reference Effect Adapters
Verifier predicates determine whether an action’s declared outcome is proven and annotate evidence strength based on source (adapter execution, state reread, independent channel, or semantic observation).
- `packages/verifier-core/src/index.ts`
- `packages/verifier-core/src/probe.ts`

Adapters are downstream capability boundaries, not bypasses of the core pipeline:
- Filesystem confinement and atomic operations: `packages/file-runtime/src/index.ts`
- Shell-free supervised processes: `packages/terminal-runtime/src/index.ts`
- Isolated Playwright browser sessions, stable DOM, and destination pinning: `packages/browser-runtime/src/browser-connection.ts`, `packages/browser-runtime/src/stable-dom.ts`, `packages/browser-runtime/src/pinning-proxy.ts`
- Local desktop controls: `packages/computer-runtime/src/index.ts`
- Governed HTTP/API effects: `packages/http-runtime/src/index.ts`

#### Runtime and Infrastructure
The primary runtime is strict TypeScript on Node.js, organized as a pnpm workspace. SQLite provides local durable state; Playwright powers browser automation. Supported execution environments include macOS, Linux, and Windows, with Docker packaging via `Dockerfile` and `compose.yaml`. Python is used for the SDK and browser-agent benchmark harness.
- `Dockerfile`
- `compose.yaml`
- `benchmarks/browser-agent/src/melra_browser_bench/runner.py`

Main entry points: [`packages/runtime-core/src/task-controller.ts`](https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/task-controller.ts) (Task controller), [`packages/server/src/runtime.ts`](https://github.com/xagi-lab/melra/blob/main/packages/server/src/runtime.ts) (Runtime composition), [`packages/runtime-core/src/workflow-controller.ts`](https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/workflow-controller.ts) (Workflow controller), [`packages/storage-sqlite/src/index.ts`](https://github.com/xagi-lab/melra/blob/main/packages/storage-sqlite/src/index.ts) (SQLite system of record)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_clients["Clients &amp; Transports"]
  node_cli["CLI / stdio<br/>entry point<br/>[bin.ts]"]
  node_sdk["TypeScript SDK<br/>client<br/>[index.ts]"]
  node_python_sdk["Python SDK<br/>client<br/>[client.py]"]
  node_server_runtime["Runtime composition<br/>server runtime<br/>[runtime.ts]"]
end

subgraph group_control["Durable Control Plane"]
  node_task_controller["Task controller<br/>lifecycle orchestrator<br/>[task-controller.ts]"]
  node_workflow_controller["Workflow controller<br/>workflow orchestrator"]
  node_workflow_graph["Workflow graph<br/>execution graph<br/>[workflow-graph.ts]"]
end

subgraph group_governance["Governance &amp; Security"]
  node_oauth["OAuth &amp; client identity<br/>identity boundary<br/>[oauth.ts]"]
  node_policy["Policy engine<br/>governance<br/>[index.ts]"]
  node_credentials["Credential broker<br/>capability broker<br/>[credentials.ts]"]
  node_payload_cipher["Payload encryption<br/>payload protection<br/>[payload-cipher.ts]"]
end

subgraph group_state["State &amp; Evidence"]
  node_receipt_contract["Receipt schema<br/>audit contract<br/>[index.ts]"]
  node_sqlite[("SQLite system of record<br/>durable store<br/>[index.ts]")]
  node_verifier["Verification gate<br/>evidence gate"]
  node_memory[("Operational memory<br/>scoped knowledge store<br/>[index.ts]")]
end

subgraph group_effects["Effect Boundaries"]
  node_adapters{{"Governed effect adapters<br/>capability boundary<br/>[index.ts]"}}
end

node_protocol["Protocol schemas<br/>contract<br/>[index.ts]"]
node_workflow_contract["Workflow model<br/>workflow schema"]

node_cli -->|"invokes"| node_server_runtime
node_sdk -->|"requests"| node_server_runtime
node_python_sdk -->|"requests"| node_server_runtime
node_server_runtime -->|"authenticates clients"| node_oauth
node_server_runtime -->|"dispatches"| node_task_controller
node_task_controller -->|"validates request"| node_protocol
node_task_controller -->|"plan and execution recheck"| node_policy
node_task_controller -->|"persists lifecycle and idempotency"| node_sqlite
node_task_controller -->|"protects executable payload"| node_payload_cipher
node_task_controller -->|"obtains scoped capability"| node_credentials
node_task_controller -->|"executes approved effect"| node_adapters
node_adapters -->|"supplies execution evidence"| node_verifier
node_task_controller -->|"requires outcome proof"| node_verifier
node_verifier -->|"records evidence"| node_sqlite
node_sqlite -->|"projects auditable receipt"| node_receipt_contract
node_task_controller -->|"records redacted operational knowledge"| node_memory
node_workflow_controller -->|"interprets"| node_workflow_graph
node_workflow_controller -->|"validates workflow"| node_workflow_contract
node_workflow_controller -->|"orchestrates effect nodes"| node_task_controller
node_workflow_controller -->|"persists workflow state"| node_sqlite

click node_protocol "https://github.com/xagi-lab/melra/blob/main/packages/protocol/src/index.ts"
click node_receipt_contract "https://github.com/xagi-lab/melra/blob/main/packages/receipt-schema/src/index.ts"
click node_cli "https://github.com/xagi-lab/melra/blob/main/apps/cli/src/bin.ts"
click node_sdk "https://github.com/xagi-lab/melra/blob/main/packages/sdk-ts/src/index.ts"
click node_python_sdk "https://github.com/xagi-lab/melra/blob/main/sdk-py/src/melra/client.py"
click node_server_runtime "https://github.com/xagi-lab/melra/blob/main/packages/server/src/runtime.ts"
click node_oauth "https://github.com/xagi-lab/melra/blob/main/packages/server/src/oauth.ts"
click node_task_controller "https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/task-controller.ts"
click node_workflow_controller "https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/workflow-controller.ts"
click node_workflow_graph "https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/workflow-graph.ts"
click node_policy "https://github.com/xagi-lab/melra/blob/main/packages/policy-core/src/index.ts"
click node_credentials "https://github.com/xagi-lab/melra/blob/main/packages/policy-core/src/credentials.ts"
click node_payload_cipher "https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/payload-cipher.ts"
click node_sqlite "https://github.com/xagi-lab/melra/blob/main/packages/storage-sqlite/src/index.ts"
click node_verifier "https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/verification-gate.ts"
click node_memory "https://github.com/xagi-lab/melra/blob/main/packages/memory/src/index.ts"
click node_adapters "https://github.com/xagi-lab/melra/blob/main/packages/file-runtime/src/index.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_sdk,node_python_sdk,node_server_runtime toneBlue
class node_task_controller,node_workflow_controller,node_workflow_graph toneAmber
class node_oauth,node_policy,node_credentials,node_payload_cipher toneMint
class node_receipt_contract,node_sqlite,node_verifier,node_memory toneRose
class node_adapters toneIndigo
class node_protocol,node_workflow_contract toneNeutral
```

## Components

### Clients & Transports

Multiple interfaces share one runtime.

- [CLI / stdio](https://github.com/xagi-lab/melra/blob/main/apps/cli/src/bin.ts): entry point · `apps/cli/src/bin.ts`
- [TypeScript SDK](https://github.com/xagi-lab/melra/blob/main/packages/sdk-ts/src/index.ts): client · `packages/sdk-ts/src/index.ts`
- [Python SDK](https://github.com/xagi-lab/melra/blob/main/sdk-py/src/melra/client.py): client · `sdk-py/src/melra/client.py`
- [Runtime composition](https://github.com/xagi-lab/melra/blob/main/packages/server/src/runtime.ts): server runtime · Composes MCP and loopback HTTP over the common control plane. · `packages/server/src/runtime.ts`

### Durable Control Plane

Lifecycle orchestration and workflow execution.

- [Task controller](https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/task-controller.ts): lifecycle orchestrator · Plans, persists, gates, executes, recovers, and emits task transitions. · `packages/runtime-core/src/task-controller.ts`
- [Workflow controller](https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/workflow-controller.ts): workflow orchestrator · Runs durable workflow state transitions and control nodes. · `packages/runtime-core/src/workflow-controller.ts`
- [Workflow graph](https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/workflow-graph.ts): execution graph · `packages/runtime-core/src/workflow-graph.ts`

### Governance & Security

Policy, approval, identity, and payload protection.

- [OAuth & client identity](https://github.com/xagi-lab/melra/blob/main/packages/server/src/oauth.ts): identity boundary · Issues scoped capabilities instead of exposing raw credentials. · `packages/server/src/oauth.ts`
- [Policy engine](https://github.com/xagi-lab/melra/blob/main/packages/policy-core/src/index.ts): governance · Classifies effects and constrains capabilities, paths, commands, and destinations. · `packages/policy-core/src/index.ts`
- [Credential broker](https://github.com/xagi-lab/melra/blob/main/packages/policy-core/src/credentials.ts): capability broker · `packages/policy-core/src/credentials.ts`
- [Payload encryption](https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/payload-cipher.ts): payload protection · Encrypts exact executable payloads with an on-host key. · `packages/runtime-core/src/payload-cipher.ts`

### State & Evidence

Durable records, verification, and receipts.

- [Receipt schema](https://github.com/xagi-lab/melra/blob/main/packages/receipt-schema/src/index.ts): audit contract · `packages/receipt-schema/src/index.ts`
- [SQLite system of record](https://github.com/xagi-lab/melra/blob/main/packages/storage-sqlite/src/index.ts): durable store · Stores tasks, workflows, events, idempotency, evidence, and projections. · `packages/storage-sqlite/src/index.ts`
- [Verification gate](https://github.com/xagi-lab/melra/blob/main/packages/runtime-core/src/verification-gate.ts): evidence gate · Proves declared outcomes and grades evidence strength. · `packages/runtime-core/src/verification-gate.ts`
- [Operational memory](https://github.com/xagi-lab/melra/blob/main/packages/memory/src/index.ts): scoped knowledge store · Stores redacted, provenance-aware knowledge about prior effects. · `packages/memory/src/index.ts`

### Effect Boundaries

Governed downstream capability adapters.

- [Governed effect adapters](https://github.com/xagi-lab/melra/blob/main/packages/file-runtime/src/index.ts): capability boundary · Filesystem, process, browser, desktop, and HTTP effects remain downstream of the core pipeline. · `packages/file-runtime/src/index.ts`

### Other components

- [Protocol schemas](https://github.com/xagi-lab/melra/blob/main/packages/protocol/src/index.ts): contract · Defines operations, tasks, workflows, events, and node kinds. · `packages/protocol/src/index.ts`
- **Workflow model**: workflow schema

## Connections

- CLI / stdio → Runtime composition: invokes
- TypeScript SDK → Runtime composition: requests
- Python SDK → Runtime composition: requests
- Runtime composition → OAuth & client identity: authenticates clients
- Runtime composition → Task controller: dispatches
- Task controller → Protocol schemas: validates request
- Task controller → Policy engine: plan and execution recheck
- Task controller → SQLite system of record: persists lifecycle and idempotency
- Task controller → Payload encryption: protects executable payload
- Task controller → Credential broker: obtains scoped capability
- Task controller → Governed effect adapters: executes approved effect
- Governed effect adapters → Verification gate: supplies execution evidence
- Task controller → Verification gate: requires outcome proof
- Verification gate → SQLite system of record: records evidence
- SQLite system of record → Receipt schema: projects auditable receipt
- Task controller → Operational memory: records redacted operational knowledge
- Workflow controller → Workflow graph: interprets
- Workflow controller → Workflow model: validates workflow
- Workflow controller → Task controller: orchestrates effect nodes
- Workflow controller → SQLite system of record: persists workflow state
