# t-crypt/berth architecture

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

- Interactive diagram: https://gitdiagram.com/t-crypt/berth
- Repository: https://github.com/t-crypt/berth

## Overview

Berth is a local GPU lease broker: clients request a card, the broker queues by priority, drains the current holder, waits for VRAM to become available, and grants the next lease. The CLI and gateway are principal entry points; visibility, model management, claims, alerts, and agent support are major adjacent capabilities. Several subsystem links below come from the README because their implementation files were not sampled; those paths are present in the tree, but their internals were not inspected.

Main entry points: [`internal/cli/cli.go`](https://github.com/t-crypt/berth/blob/main/internal/cli/cli.go) (CLI commands), [`internal/gateway/gateway.go`](https://github.com/t-crypt/berth/blob/main/internal/gateway/gateway.go) (API gateway), [`internal/broker/broker.go`](https://github.com/t-crypt/berth/blob/main/internal/broker/broker.go) (Lease broker), [`internal/harbor/harbor.go`](https://github.com/t-crypt/berth/blob/main/internal/harbor/harbor.go) (Harbor web UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_lease["Lease coordination"]
  node_daemon["Daemon entry<br/>[main.go]"]
  node_socket["Lease protocol<br/>[proto.go]"]
  node_server["Lease server<br/>[server.go]"]
  node_broker["Lease broker<br/>[broker.go]"]
  node_gpu["GPU sampling<br/>[gpu.go]"]
  node_claims["Process claims<br/>[claims.go]"]
end

subgraph group_clients["Clients and gateway"]
  node_cli["CLI commands<br/>[cli.go]"]
  node_runner["Leased process runner<br/>[runner.go]"]
  node_gateway["API gateway<br/>[gateway.go]"]
  node_apikey["Gateway key checks<br/>[apikey.go]"]
end

subgraph group_visibility["GPU visibility"]
  node_enginewatch["Engine discovery and metrics<br/>[engines.go]"]
  node_harbor["Harbor web UI<br/>[harbor.go]"]
  node_harborview["Harbor engine views<br/>[engines.js]"]
  node_store[("Telemetry store<br/>[store.go]")]
  node_alerts["Alerts<br/>[alerts.go]"]
  node_export["Telemetry export<br/>[export.go]"]
end

subgraph group_models["Model management"]
  node_models["Model catalog and scan<br/>[scan.go]"]
  node_pullhf["Hugging Face pulls<br/>[hf.go]"]
  node_pullcivitai["CivitAI pulls<br/>[civitai.go]"]
  node_storage["Model storage roots<br/>[root.go]"]
end

subgraph group_integrations["Integrations"]
  node_suite["Engine suite management<br/>[manage.go]"]
  node_wire["Agent skill and wiring<br/>[wire.go]"]
  node_skill["Agent skill install<br/>[skill.go]"]
end

node_user(("User or app"))
node_enginesvc["Inference engine"]
node_gpu_driver["GPU driver"]
node_hf_service["Hugging Face"]
node_civitai_service["CivitAI"]

node_user -->|"runs commands"| node_cli
node_cli -->|"runs engine"| node_runner
node_runner -->|"requests lease"| node_socket
node_daemon -.->|"starts daemon"| node_server
node_server -->|"handles messages"| node_socket
node_server -->|"requests lease"| node_broker
node_broker -->|"checks GPU state"| node_gpu
node_gpu -.->|"samples memory"| node_gpu_driver
node_broker -->|"sends drain"| node_runner
node_user -->|"sends API request"| node_gateway
node_gateway -->|"checks bearer key"| node_apikey
node_gateway -->|"starts engine"| node_runner
node_gateway -->|"proxies request"| node_enginesvc
node_gateway -->|"queues lease"| node_broker
node_user -->|"defines claim rules"| node_claims
node_broker -.->|"accounts GPU processes"| node_claims
node_cli -->|"checks engines"| node_enginewatch
node_enginewatch -->|"reads status"| node_enginesvc
node_user -->|"opens dashboard"| node_harbor
node_harbor -->|"reads history"| node_store
node_harbor -.->|"serves UI"| node_harborview
node_harborview -.->|"renders telemetry"| node_harbor
node_cli -.->|"configures alerts"| node_alerts
node_cli -->|"exports telemetry"| node_export
node_cli -->|"lists and verifies"| node_models
node_cli -->|"downloads model"| node_pullhf
node_pullhf -->|"fetches files"| node_hf_service
node_cli -->|"downloads model"| node_pullcivitai
node_pullcivitai -->|"fetches files"| node_civitai_service
node_cli -->|"checks roots"| node_storage
node_cli -->|"manages installs"| node_suite
node_cli -->|"wires clients"| node_wire
node_cli -->|"installs skill"| node_skill

click node_cli "https://github.com/t-crypt/berth/blob/main/internal/cli/cli.go"
click node_daemon "https://github.com/t-crypt/berth/blob/main/cmd/berth/main.go"
click node_socket "https://github.com/t-crypt/berth/blob/main/internal/proto/proto.go"
click node_server "https://github.com/t-crypt/berth/blob/main/internal/server/server.go"
click node_broker "https://github.com/t-crypt/berth/blob/main/internal/broker/broker.go"
click node_gpu "https://github.com/t-crypt/berth/blob/main/internal/gpu/gpu.go"
click node_runner "https://github.com/t-crypt/berth/blob/main/internal/runner/runner.go"
click node_gateway "https://github.com/t-crypt/berth/blob/main/internal/gateway/gateway.go"
click node_apikey "https://github.com/t-crypt/berth/blob/main/internal/apikey/apikey.go"
click node_claims "https://github.com/t-crypt/berth/blob/main/internal/claims/claims.go"
click node_enginewatch "https://github.com/t-crypt/berth/blob/main/internal/engines/engines.go"
click node_harbor "https://github.com/t-crypt/berth/blob/main/internal/harbor/harbor.go"
click node_harborview "https://github.com/t-crypt/berth/blob/main/internal/harbor/web/engines.js"
click node_store "https://github.com/t-crypt/berth/blob/main/internal/store/store.go"
click node_alerts "https://github.com/t-crypt/berth/blob/main/internal/alerts/alerts.go"
click node_export "https://github.com/t-crypt/berth/blob/main/internal/export/export.go"
click node_models "https://github.com/t-crypt/berth/blob/main/internal/models/scan.go"
click node_pullhf "https://github.com/t-crypt/berth/blob/main/internal/hf/hf.go"
click node_pullcivitai "https://github.com/t-crypt/berth/blob/main/internal/civitai/civitai.go"
click node_storage "https://github.com/t-crypt/berth/blob/main/internal/storage/root.go"
click node_suite "https://github.com/t-crypt/berth/blob/main/internal/suite/manage.go"
click node_wire "https://github.com/t-crypt/berth/blob/main/internal/wire/wire.go"
click node_skill "https://github.com/t-crypt/berth/blob/main/internal/skill/skill.go"

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_daemon,node_socket,node_server,node_broker,node_gpu,node_claims,node_user toneBlue
class node_cli,node_runner,node_gateway,node_apikey toneAmber
class node_enginewatch,node_harbor,node_harborview,node_store,node_alerts,node_export toneMint
class node_models,node_pullhf,node_pullcivitai,node_storage toneRose
class node_suite,node_wire,node_skill,node_enginesvc,node_gpu_driver,node_hf_service,node_civitai_service toneIndigo
```

## Components

### Lease coordination

- [Daemon entry](https://github.com/t-crypt/berth/blob/main/cmd/berth/main.go): `cmd/berth/main.go`
- [Lease protocol](https://github.com/t-crypt/berth/blob/main/internal/proto/proto.go): `internal/proto/proto.go`
- [Lease server](https://github.com/t-crypt/berth/blob/main/internal/server/server.go): `internal/server/server.go`
- [Lease broker](https://github.com/t-crypt/berth/blob/main/internal/broker/broker.go): `internal/broker/broker.go`
- [GPU sampling](https://github.com/t-crypt/berth/blob/main/internal/gpu/gpu.go): `internal/gpu/gpu.go`
- [Process claims](https://github.com/t-crypt/berth/blob/main/internal/claims/claims.go): `internal/claims/claims.go`

### Clients and gateway

- [CLI commands](https://github.com/t-crypt/berth/blob/main/internal/cli/cli.go): `internal/cli/cli.go`
- [Leased process runner](https://github.com/t-crypt/berth/blob/main/internal/runner/runner.go): `internal/runner/runner.go`
- [API gateway](https://github.com/t-crypt/berth/blob/main/internal/gateway/gateway.go): `internal/gateway/gateway.go`
- [Gateway key checks](https://github.com/t-crypt/berth/blob/main/internal/apikey/apikey.go): `internal/apikey/apikey.go`

### GPU visibility

- [Engine discovery and metrics](https://github.com/t-crypt/berth/blob/main/internal/engines/engines.go): `internal/engines/engines.go`
- [Harbor web UI](https://github.com/t-crypt/berth/blob/main/internal/harbor/harbor.go): `internal/harbor/harbor.go`
- [Harbor engine views](https://github.com/t-crypt/berth/blob/main/internal/harbor/web/engines.js): `internal/harbor/web/engines.js`
- [Telemetry store](https://github.com/t-crypt/berth/blob/main/internal/store/store.go): `internal/store/store.go`
- [Alerts](https://github.com/t-crypt/berth/blob/main/internal/alerts/alerts.go): `internal/alerts/alerts.go`
- [Telemetry export](https://github.com/t-crypt/berth/blob/main/internal/export/export.go): `internal/export/export.go`

### Model management

- [Model catalog and scan](https://github.com/t-crypt/berth/blob/main/internal/models/scan.go): `internal/models/scan.go`
- [Hugging Face pulls](https://github.com/t-crypt/berth/blob/main/internal/hf/hf.go): `internal/hf/hf.go`
- [CivitAI pulls](https://github.com/t-crypt/berth/blob/main/internal/civitai/civitai.go): `internal/civitai/civitai.go`
- [Model storage roots](https://github.com/t-crypt/berth/blob/main/internal/storage/root.go): `internal/storage/root.go`

### Integrations

- [Engine suite management](https://github.com/t-crypt/berth/blob/main/internal/suite/manage.go): `internal/suite/manage.go`
- [Agent skill and wiring](https://github.com/t-crypt/berth/blob/main/internal/wire/wire.go): `internal/wire/wire.go`
- [Agent skill install](https://github.com/t-crypt/berth/blob/main/internal/skill/skill.go): `internal/skill/skill.go`

### Other components

- **User or app**
- **Inference engine**
- **GPU driver**
- **Hugging Face**
- **CivitAI**

## Connections

- User or app → CLI commands: runs commands (evidence: `README.md`)
- CLI commands → Leased process runner: runs engine (evidence: `README.md`)
- Leased process runner → Lease protocol: requests lease (evidence: `internal/runner/runner.go`)
- Daemon entry → Lease server: starts daemon
- Lease server → Lease protocol: handles messages (evidence: `internal/server/server.go`)
- Lease server → Lease broker: requests lease (evidence: `internal/server/server.go`)
- Lease broker → GPU sampling: checks GPU state (evidence: `internal/broker/broker.go`)
- GPU sampling → GPU driver: samples memory
- Lease broker → Leased process runner: sends drain (evidence: `README.md`)
- User or app → API gateway: sends API request (evidence: `README.md`)
- API gateway → Gateway key checks: checks bearer key (evidence: `internal/gateway/gateway.go`)
- API gateway → Leased process runner: starts engine (evidence: `internal/gateway/gateway.go`)
- API gateway → Inference engine: proxies request (evidence: `README.md`)
- API gateway → Lease broker: queues lease (evidence: `README.md`)
- User or app → Process claims: defines claim rules (evidence: `README.md`)
- Lease broker → Process claims: accounts GPU processes
- CLI commands → Engine discovery and metrics: checks engines
- Engine discovery and metrics → Inference engine: reads status (evidence: `internal/engines/engines.go`)
- User or app → Harbor web UI: opens dashboard (evidence: `README.md`)
- Harbor web UI → Telemetry store: reads history (evidence: `internal/harbor/harbor.go`)
- Harbor web UI → Harbor engine views: serves UI
- Harbor engine views → Harbor web UI: renders telemetry
- CLI commands → Alerts: configures alerts
- CLI commands → Telemetry export: exports telemetry (evidence: `README.md`)
- CLI commands → Model catalog and scan: lists and verifies (evidence: `README.md`)
- CLI commands → Hugging Face pulls: downloads model (evidence: `README.md`)
- Hugging Face pulls → Hugging Face: fetches files (evidence: `README.md`)
- CLI commands → CivitAI pulls: downloads model (evidence: `README.md`)
- CivitAI pulls → CivitAI: fetches files (evidence: `README.md`)
- CLI commands → Model storage roots: checks roots
- CLI commands → Engine suite management: manages installs
- CLI commands → Agent skill and wiring: wires clients (evidence: `README.md`)
- CLI commands → Agent skill install: installs skill (evidence: `README.md`)
