# margo/sandbox architecture

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

- Interactive diagram: https://gitdiagram.com/margo/sandbox
- Repository: https://github.com/margo/sandbox

## Overview

1. **Repository purpose**
   - This is a Margo “code-first sandbox” for experimenting with fleet-management workflows, package formats, and device-side behavior.
   - It is not a single deployable product; it is a set of cooperating components, libraries, deployment assets, and proof-of-concept implementations.

2. **Main architectural boundary: three code zones**
   - `shared-lib` contains reusable building blocks with no Margo-specific deployment role.
   - `standard` contains code mapped to the Margo specification snapshot.
   - `non-standard` contains enabling functionality that the sandbox needs, but that is not part of the Margo spec itself.

3. **Standard/spec-aligned layer**
   - `standard/generatedCode/wfm/sbi` and `non-standard/generatedCode/wfm/nbi` are generated Go clients/models for the spec-defined service interfaces.
   - `standard/pkg/parser.go` suggests spec-to-code translation and parsing of the snapshot spec in `standard/snapshot.spec.yaml`.
   - This layer is the “contract boundary” for Margo API compatibility and traceability.

4. **Non-standard package management layer**
   - `non-standard/pkg/packageManager` is a dedicated subsystem for handling application packages as either directory-based packages or OCI-backed packages.
   - It includes validation, error handling, and package type abstractions, so package ingestion and retrieval are isolated from workload deployment logic.
   - `non-standard/pkg/models` defines package/application state objects used by that management flow.

5. **Shared library layer**
   - `shared-lib` centralizes cross-cutting capabilities used by sandbox components:
     - archive extraction and packaging
     - caching of bundles and deployments
     - crypto/digest/signing/TLS/verifier support
     - HTTP helpers and auth token handling
     - git cloning/branch/pull operations
     - OCI client/image/repository utilities
     - workload execution helpers for Compose and Helm
   - Architecturally, this is the foundation layer beneath both control-plane and device-side code.

6. **Device-agent proof of concept**
   - `poc/device/agent` is a standalone device-side agent implementation with its own `main.go`, configuration, and local persistence.
   - Subcomponents include:
     - `onboarding.go` for initial registration/joining
     - `stateSync.go` for syncing device/workload state
     - `monitor.go` and `status.go` for health/telemetry/status handling
     - `deployment.go` plus `device/capabilities.go` for interpreting what the device can run
     - `database/database.go` backed by a local JSON database under `data/`
   - This agent is the sandbox’s “managed device” endpoint for workload lifecycle interactions.

7. **Lifecycle/data flow on the device side**
   - The agent receives configuration, establishes secure connectivity, and persists local state.
   - It consumes package metadata/capability information, determines whether a workload is deployable, and then coordinates deployment/update/monitoring.
   - State changes are stored locally and synchronized outward, rather than keeping all truth only in memory.

8. **Workload execution backends**
   - The sandbox supports two practical workload formats:
     - **Helm** via `shared-lib/workloads/helm.go`
     - **Docker Compose** via `shared-lib/workloads/compose.go` and the Docker client wrapper
   - This means the runtime boundary is not “one runtime only”; the repo abstracts multiple workload substrates behind shared helpers.

9. **Registry and artifact flow**
   - Harbor is the primary registry/repository backend described in the README and supported by `scripts/harbor`.
   - Application packages, container images, and Helm artifacts are expected to be stored and retrieved from OCI-compatible repositories.
   - The package manager and OCI helpers form the bridge between package metadata and pullable deployable artifacts.

10. **Sandbox deployment targets**
   - `helmchart/` provides Kubernetes/K3s deployment packaging for sandbox components.
   - `docker-compose/` provides a compose-based deployment path for local or simpler environments.
   - These are environment-specific delivery layers, not core business logic.

11. **Operational and bootstrap tooling**
   - `scripts/` contains environment setup and orchestration helpers for installing/configuring core dependencies such as Docker, Go, Helm, k3s, Redis, Harbor, ORAS, and observability tools.
   - `scripts/easy-cli` and `scripts/wfm-cli.sh` indicate command-line workflows for managing packages, instances, and environment configuration.
   - `justfile` likely provides a unified developer entrypoint for common tasks.

12. **Observability and telemetry**
   - The sandbox is designed to emit telemetry from the device/client side to external backends.
   - The README identifies OpenTelemetry Collector, Promtail, Prometheus, Grafana, Loki, and Jaeger as the monitoring stack.
   - In architecture terms, telemetry is a side-channel from the device agent/workload client into centralized observability services, not part of the package deployment path.

13. **Security and transport**
   - The repo emphasizes HTTP/1.1 over TLS with server-side TLS rather than mTLS as the default secure transport model.
   - `shared-lib/crypto` and `shared-lib/http/auth` provide the TLS, digest, signer, verifier, and token-related building blocks used across components.
   - Client and server identity are treated as certificate-backed concerns, especially around onboarding and package/API interactions.

14. **Test and sample artifacts**
   - `poc/tests/artefacts` contains sample application packages for Helm and Compose scenarios.
   - These artifacts serve as integration inputs for validating package ingestion, repository flows, and deployment behavior across the sandbox.

15. **Overall system picture**
   - The repository is centered on a control/data path where application packages are stored in a registry, discovered through Margo-aligned APIs, and deployed by a device-side agent using Helm or Compose.
   - Supporting layers provide spec alignment, OCI/package management, secure transport, local state persistence, observability, and environment bootstrap tooling.

Main entry points: [`non-standard/pkg/packageManager/packageManager.go`](https://github.com/margo/sandbox/blob/main/non-standard/pkg/packageManager/packageManager.go) (Package manager), [`poc/device/agent/main.go`](https://github.com/margo/sandbox/blob/main/poc/device/agent/main.go) (Agent main), [`scripts/wfm-cli.sh`](https://github.com/margo/sandbox/blob/main/scripts/wfm-cli.sh) (Bootstrap scripts), [`poc/device/agent/deployment.go`](https://github.com/margo/sandbox/blob/main/poc/device/agent/deployment.go) (Deployment)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_shared["shared-lib"]
  node_node_archiver["Archive<br/>bundle utils<br/>[archiver.go]"]
  node_node_cache["Cache<br/>[cache.go]"]
  node_node_crypto["Crypto<br/>security utils<br/>[tls.go]"]
  node_node_httpauth["HTTP auth<br/>token helpers<br/>[token.go]"]
  node_node_oci["OCI utils<br/>registry client<br/>[client.go]"]
  node_node_git["Git ops<br/>source fetch<br/>[client.go]"]
  node_node_workloads["Workloads<br/>runtime adapters<br/>[helm.go]"]
end

subgraph group_group_standard["standard"]
  node_node_snapshot["Spec snapshot<br/>spec yaml<br/>[snapshot.spec.yaml]"]
  node_node_parser["Parser<br/>[parser.go]"]
  node_node_sbi["SBI client<br/>generated api<br/>[client.go]"]
end

subgraph group_group_nonstandard["non-standard"]
  node_node_nbi["NBI client<br/>generated api<br/>[client.go]"]
  node_node_pkgmgr["Package manager<br/>package subsystem<br/>[packageManager.go]"]
  node_node_pkgtypes["Package types<br/>package model<br/>[types.go]"]
  node_node_pkgmodels["Pkg models<br/>state model"]
end

subgraph group_group_agent["device agent"]
  node_node_agent_main["Agent main<br/>device runtime<br/>[main.go]"]
  node_node_onboarding["Onboarding<br/>join flow<br/>[onboarding.go]"]
  node_node_statesync["State sync<br/>sync flow<br/>[stateSync.go]"]
  node_node_deployment["Deployment<br/>lifecycle flow<br/>[deployment.go]"]
  node_node_db[("Local DB<br/>json store<br/>[database.go]")]
  node_node_capabilities["Capabilities<br/>device model<br/>[capabilities.go]"]
end

subgraph group_group_delivery["delivery"]
  node_node_helmchart["Helm chart<br/>deployment package<br/>[Chart.yaml]"]
  node_node_compose["Compose stack<br/>deployment package"]
  node_node_artefacts["Sample artefacts<br/>test inputs<br/>[margo.yaml]"]
end

subgraph group_group_scripts["scripts"]
  node_node_scripts["Bootstrap scripts<br/>ops tooling<br/>[wfm-cli.sh]"]
end

node_node_snapshot -->|"parsed by"| node_node_parser
node_node_parser -->|"generates"| node_node_sbi
node_node_parser -->|"generates"| node_node_nbi
node_node_pkgmgr -->|"uses"| node_node_pkgtypes
node_node_pkgmgr -->|"updates"| node_node_pkgmodels
node_node_pkgmgr -->|"pulls via"| node_node_oci
node_node_pkgmgr -->|"extracts via"| node_node_archiver
node_node_pkgmgr -->|"verifies via"| node_node_crypto
node_node_pkgmgr -->|"auth via"| node_node_httpauth
node_node_pkgmgr -->|"caches"| node_node_cache
node_node_git -.->|"feeds"| node_node_pkgmgr
node_node_workloads -->|"deploys"| node_node_helmchart
node_node_workloads -->|"deploys"| node_node_compose
node_node_agent_main -->|"starts"| node_node_onboarding
node_node_agent_main -->|"runs"| node_node_statesync
node_node_agent_main -->|"coordinates"| node_node_deployment
node_node_agent_main -->|"persists"| node_node_db
node_node_deployment -->|"evaluates"| node_node_capabilities
node_node_deployment -->|"invokes"| node_node_workloads
node_node_deployment -->|"consumes"| node_node_pkgmgr
node_node_statesync -->|"reads/writes"| node_node_db
node_node_onboarding -->|"secures"| node_node_crypto
node_node_artefacts -.->|"validates"| node_node_pkgmgr
node_node_scripts -->|"boots"| node_node_agent_main
node_node_scripts -->|"installs"| node_node_helmchart
node_node_scripts -->|"orchestrates"| node_node_compose
node_node_scripts -->|"prepares"| node_node_oci

click node_node_snapshot "https://github.com/margo/sandbox/blob/main/standard/snapshot.spec.yaml"
click node_node_parser "https://github.com/margo/sandbox/blob/main/standard/pkg/parser.go"
click node_node_sbi "https://github.com/margo/sandbox/blob/main/standard/generatedCode/wfm/sbi/client.go"
click node_node_nbi "https://github.com/margo/sandbox/blob/main/non-standard/generatedCode/wfm/nbi/client.go"
click node_node_pkgmgr "https://github.com/margo/sandbox/blob/main/non-standard/pkg/packageManager/packageManager.go"
click node_node_pkgtypes "https://github.com/margo/sandbox/blob/main/non-standard/pkg/packageManager/types.go"
click node_node_pkgmodels "https://github.com/margo/sandbox/blob/main/non-standard/pkg/models/applicationPackage.go"
click node_node_archiver "https://github.com/margo/sandbox/blob/main/shared-lib/archive/archiver.go"
click node_node_cache "https://github.com/margo/sandbox/blob/main/shared-lib/cache/cache.go"
click node_node_crypto "https://github.com/margo/sandbox/blob/main/shared-lib/crypto/tls.go"
click node_node_httpauth "https://github.com/margo/sandbox/blob/main/shared-lib/http/auth/token.go"
click node_node_oci "https://github.com/margo/sandbox/blob/main/shared-lib/oci/client.go"
click node_node_git "https://github.com/margo/sandbox/blob/main/shared-lib/git/client.go"
click node_node_workloads "https://github.com/margo/sandbox/blob/main/shared-lib/workloads/helm.go"
click node_node_agent_main "https://github.com/margo/sandbox/blob/main/poc/device/agent/main.go"
click node_node_onboarding "https://github.com/margo/sandbox/blob/main/poc/device/agent/onboarding.go"
click node_node_statesync "https://github.com/margo/sandbox/blob/main/poc/device/agent/stateSync.go"
click node_node_deployment "https://github.com/margo/sandbox/blob/main/poc/device/agent/deployment.go"
click node_node_db "https://github.com/margo/sandbox/blob/main/poc/device/agent/database/database.go"
click node_node_capabilities "https://github.com/margo/sandbox/blob/main/poc/device/agent/device/capabilities.go"
click node_node_helmchart "https://github.com/margo/sandbox/blob/main/helmchart/Chart.yaml"
click node_node_compose "https://github.com/margo/sandbox/blob/main/docker-compose/docker-compose.yaml"
click node_node_artefacts "https://github.com/margo/sandbox/blob/main/poc/tests/artefacts/custom-otel-helm-app/margo-package/margo.yaml"
click node_node_scripts "https://github.com/margo/sandbox/blob/main/scripts/wfm-cli.sh"

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_node_archiver,node_node_cache,node_node_crypto,node_node_httpauth,node_node_oci,node_node_git,node_node_workloads toneBlue
class node_node_snapshot,node_node_parser,node_node_sbi toneAmber
class node_node_nbi,node_node_pkgmgr,node_node_pkgtypes,node_node_pkgmodels toneMint
class node_node_agent_main,node_node_onboarding,node_node_statesync,node_node_deployment,node_node_db,node_node_capabilities toneRose
class node_node_helmchart,node_node_compose,node_node_artefacts toneIndigo
class node_node_scripts toneTeal
```

## Components

### shared-lib

Reusable foundation utilities shared across sandbox components.

- [Archive](https://github.com/margo/sandbox/blob/main/shared-lib/archive/archiver.go): bundle utils · Archive extraction and packaging helpers. · `shared-lib/archive/archiver.go`
- [Cache](https://github.com/margo/sandbox/blob/main/shared-lib/cache/cache.go): bundle cache · Bundle and deployment cache primitives. · `shared-lib/cache/cache.go`
- [Crypto](https://github.com/margo/sandbox/blob/main/shared-lib/crypto/tls.go): security utils · Digest, signer, TLS, and verifier support. · `shared-lib/crypto/tls.go`
- [HTTP auth](https://github.com/margo/sandbox/blob/main/shared-lib/http/auth/token.go): token helpers · HTTP request and token/auth helpers. · `shared-lib/http/auth/token.go`
- [OCI utils](https://github.com/margo/sandbox/blob/main/shared-lib/oci/client.go): registry client · OCI client, image, and repository utilities for artifact flow. · `shared-lib/oci/client.go`
- [Git ops](https://github.com/margo/sandbox/blob/main/shared-lib/git/client.go): source fetch · Clone, branch, pull, and auth helpers for repository access. · `shared-lib/git/client.go`
- [Workloads](https://github.com/margo/sandbox/blob/main/shared-lib/workloads/helm.go): runtime adapters · Helm and Docker Compose execution helpers plus Docker client wrapper. · `shared-lib/workloads/helm.go`

### standard

Margo spec-aligned contract and generated API layer.

- [Spec snapshot](https://github.com/margo/sandbox/blob/main/standard/snapshot.spec.yaml): spec yaml · Canonical Margo workflow-management snapshot used for code generation and traceability. · `standard/snapshot.spec.yaml`
- [Parser](https://github.com/margo/sandbox/blob/main/standard/pkg/parser.go): spec parser · Translates or interprets the spec snapshot into code-facing structure. · `standard/pkg/parser.go`
- [SBI client](https://github.com/margo/sandbox/blob/main/standard/generatedCode/wfm/sbi/client.go): generated api · Generated client/models for the spec-defined southbound interface. · `standard/generatedCode/wfm/sbi/client.go`

### non-standard

Sandbox-enabling functionality outside the core Margo specification.

- [NBI client](https://github.com/margo/sandbox/blob/main/non-standard/generatedCode/wfm/nbi/client.go): generated api · Generated client/models for the non-standard northbound interface. · `non-standard/generatedCode/wfm/nbi/client.go`
- [Package manager](https://github.com/margo/sandbox/blob/main/non-standard/pkg/packageManager/packageManager.go): package subsystem · Coordinates directory-based and OCI-backed package handling. · `non-standard/pkg/packageManager/packageManager.go`
- [Package types](https://github.com/margo/sandbox/blob/main/non-standard/pkg/packageManager/types.go): package model · Package abstractions, validation, and error/domain types for ingestion and retrieval. · `non-standard/pkg/packageManager/types.go`
- [Pkg models](https://github.com/margo/sandbox/blob/main/non-standard/pkg/models/applicationPackage.go): state model · Application and package state objects used by the management flow. · `non-standard/pkg/models/applicationPackage.go`

### device agent

Standalone proof-of-concept managed-device runtime.

- [Agent main](https://github.com/margo/sandbox/blob/main/poc/device/agent/main.go): device runtime · Standalone device-side agent entrypoint and local runtime configuration. · `poc/device/agent/main.go`
- [Onboarding](https://github.com/margo/sandbox/blob/main/poc/device/agent/onboarding.go): join flow · Initial registration and secure joining behavior. · `poc/device/agent/onboarding.go`
- [State sync](https://github.com/margo/sandbox/blob/main/poc/device/agent/stateSync.go): sync flow · Synchronizes device and workload state outward and into local persistence. · `poc/device/agent/stateSync.go`
- [Deployment](https://github.com/margo/sandbox/blob/main/poc/device/agent/deployment.go): lifecycle flow · Interprets capabilities and coordinates workload deployment/update decisions. · `poc/device/agent/deployment.go`
- [Local DB](https://github.com/margo/sandbox/blob/main/poc/device/agent/database/database.go): json store · Local JSON-backed persistence for agent state. · `poc/device/agent/database/database.go`
- [Capabilities](https://github.com/margo/sandbox/blob/main/poc/device/agent/device/capabilities.go): device model · Device capability interpretation for deployability decisions. · `poc/device/agent/device/capabilities.go`

### delivery

Environment-specific deployment packaging and sample artifacts.

- [Helm chart](https://github.com/margo/sandbox/blob/main/helmchart/Chart.yaml): deployment package · Kubernetes/K3s packaging for sandbox components. · `helmchart/Chart.yaml`
- [Compose stack](https://github.com/margo/sandbox/blob/main/docker-compose/docker-compose.yaml): deployment package · Compose-based delivery path for local or simpler environments. · `docker-compose/docker-compose.yaml`
- [Sample artefacts](https://github.com/margo/sandbox/blob/main/poc/tests/artefacts/custom-otel-helm-app/margo-package/margo.yaml): test inputs · Helm and Compose package samples used to validate ingestion and deployment behavior. · `poc/tests/artefacts/custom-otel-helm-app/margo-package/margo.yaml`

### scripts

Bootstrap, environment setup, and operational tooling.

- [Bootstrap scripts](https://github.com/margo/sandbox/blob/main/scripts/wfm-cli.sh): ops tooling · Environment setup and orchestration helpers for Docker, Go, Helm, k3s, Harbor, ORAS, Redis, and observability. · `scripts/wfm-cli.sh`

## Connections

- Spec snapshot → Parser: parsed by · The spec snapshot is the input to code translation and generation.
- Parser → SBI client: generates · Spec-aligned southbound client/models are produced from the snapshot.
- Parser → NBI client: generates · Non-standard API client/models are generated alongside the spec-aligned layer.
- Package manager → Package types: uses · Package manager relies on package abstractions, validation, and error types.
- Package manager → Pkg models: updates · Package handling produces and consumes application/package state objects.
- Package manager → OCI utils: pulls via · OCI-backed package handling bridges metadata to registry artifacts.
- Package manager → Archive: extracts via · Directory-based packages rely on archive extraction and packaging helpers.
- Package manager → Crypto: verifies via · Package and transport flows use digest/signing/TLS primitives.
- Package manager → HTTP auth: auth via · Package/API interaction uses HTTP token helpers.
- Package manager → Cache: caches · Package and deployment artifacts can be cached across operations.
- Git ops → Package manager: feeds · Git-backed sources can supply package material into the package flow.
- Workloads → Helm chart: deploys · Helm helpers execute chart-based workloads.
- Workloads → Compose stack: deploys · Compose helpers execute compose-based workloads.
- Agent main → Onboarding: starts · Agent bootstraps with onboarding and initial registration.
- Agent main → State sync: runs · Agent continuously syncs state with its control environment.
- Agent main → Deployment: coordinates · Agent delegates lifecycle decisions to deployment logic.
- Agent main → Local DB: persists · Agent stores local state in JSON-backed persistence.
- Deployment → Capabilities: evaluates · Deployability decisions depend on device capability interpretation.
- Deployment → Workloads: invokes · Deployment flows use shared workload adapters.
- Deployment → Package manager: consumes · Deployment uses package metadata and retrieval logic.
- State sync → Local DB: reads/writes · State synchronization is backed by local persistence.
- Onboarding → Crypto: secures · Onboarding depends on TLS and identity primitives.
- Sample artefacts → Package manager: validates · Sample artefacts exercise package ingestion and retrieval behavior.
- Bootstrap scripts → Agent main: boots · Scripts provide startup and environment wiring for the agent.
- Bootstrap scripts → Helm chart: installs · Bootstrap tooling supports Helm/K3s deployment paths.
- Bootstrap scripts → Compose stack: orchestrates · Bootstrap tooling supports compose-based local environments.
- Bootstrap scripts → OCI utils: prepares · Operational scripts install and configure OCI/registry dependencies.
