Skip to content

Architecture

This page is for contributors and for authors of plugins. It tells how the code of localspace is organized and how its parts talk to each other. The terms are in the glossary.

lo is one binary. It has a small kernel and a set of modules. The core is the module that does the work. The CLI, the dashboard and the MCP server are three front ends for the same core.

ProvidersFront endsgRPCCLI: cobra commandsui module: dashboard andAPImcp module: tools foragentskernel: command tree andinjectorcore module: contract.Coreengine.docker,engine.nativeresource.postgres, redis,mongo, s3, volumeproxy.caddysecret.filepluginhost modulelo-plugin-name processes

The kernel is in internal/kernel. It has no business logic. It does three jobs:

  • It builds the command tree of the CLI. Each module can add commands.
  • It holds the injector, a dependency injection container from github.com/samber/do. Modules give their services to the injector and read the services of other modules from it, by name.
  • It runs the lifecycle of the services. A service is built the first time a command reads it. After each command, the kernel shuts down the services that the command built.

kernel.New refuses a module that is registered two times, a service that is given two times and a command that two modules claim. It does this before any command runs.

The kernel prints errors for the CLI. A command returns an error, and the kernel writes it on one line. With --json, the kernel also writes {"error": {"code", "message"}}. The exit code is 1 when a command fails and 2 when the command line is incorrect.

A module is a Go package that implements kernel.Module. It has a name and a Register function. Register runs at each start of lo, so it does no input or output. It only declares services, commands and checks for lo doctor. The module pluginhost is the only exception.

Modules do not import each other. A module reads a different module through the injector, by a name such as engine.docker. The name follows the form <point>.<provider>. The linter enforces the rule: the core cannot import internal/modules.

cmd/localspace/main.go lists the modules of the binary.

Module Package Task
core internal/core Plans, starts and stops workspaces. It keeps the state. It adds the commands of workspaces.
engine.docker internal/modules/engine/docker Runs containers with the docker CLI of the user.
engine.native internal/modules/engine/native Runs host processes in a process group.
resource.* internal/modules/resource The resource kinds postgres, redis, mongo, s3 and volume.
proxy.caddy internal/modules/proxy/caddy The proxy for the public URLs. It runs Caddy in a container.
secret.file internal/modules/secret/file The secret store. It keeps one file for each secret.
ui internal/modules/ui The dashboard and its HTTP API.
mcp internal/modules/mcp The MCP server and its tools.
pluginhost internal/modules/pluginhost Finds the plugins and runs them.

The module pluginhost is last in the list. A plugin cannot claim the name of a built-in module.

The package internal/contract has the interfaces, the data types and the errors that all parts share. The data is plain, with JSON tags. The --json output of the CLI and the answers of the API are these values. The CLI and the API use the same shapes.

The interface contract.Core is the most important one. It has New, Up, Down, Remove, Logs and the other operations. The CLI, the dashboard and the MCP server call it. Each front end shows the result in a different form.

Errors have one of four codes. The core makes them with contract.Errorf.

Code Meaning
invalid The request or the template is incorrect.
not_found A workspace, a template, a service or a resource is not there.
conflict The state refuses the request.
unavailable A thing that localspace uses does not answer.

The core is in internal/core. It returns contract values and does not print. These parts do its work:

  • The template loader and checker, in internal/core/template, read localspace.yaml.
  • The planner selects the sources, the engines and the isolation of a new workspace.
  • The orchestrator runs up, down and rm. See how it works.
  • The reconciler compares the state with the machine before each read.
  • The state is a SQLite database with a lock for each workspace, in internal/core/state.
  • The safety rules decide which things localspace can remove. See safety.

The core reaches the providers only through the injector. For example, it reads engine.<name> for the engine that the workspace keeps.

An extension point is a contract interface with built-in implementations, which plugins can implement too. The injector name of a provider is <point>.<name>.

Extension point Interface Injector name Built-in providers
Engine contract.Engine engine.<name> docker, native
Resource contract.Resource resource.<kind> postgres, redis, mongo, s3, volume
Proxy contract.Proxy proxy.<name> caddy
Secret contract.Secret secret.<name> file
Tool contract.Tool tool.<name> The tools of lo mcp

The machine settings select the engine for each run kind, the proxy and the secret store. A template selects a resource kind with the kind field.

A plugin is an executable lo-plugin-<name> in ~/.localspace/plugins/. It runs as a separate process, so a fault in a plugin does not stop lo. lo and the plugin talk over gRPC with HashiCorp go-plugin. The protocol is in api/localspace/plugin/v1/plugin.proto, and the Go module sdk implements it.

At the start of lo, the module pluginhost reads the description of each plugin. The description lists the providers, the commands and the tools of the plugin. The module caches the description by the size and the modification time of the file. A start of lo does not run a plugin that did not change. The plugin process starts when a command first uses it, and it stops when the command ends.

The plugin gives its providers to the injector with the same names as the built-in providers. The core does not know if a provider is a built-in or a plugin.

To write a plugin, read Write a plugin.

  • Keep business logic in the core. The kernel and the modules have none.
  • Do not print in the core. Return a value or an error, and let the front end show it.
  • Return contract.Errorf errors with the code that matches the failure.
  • Put a new interface that more than one module uses in internal/contract.
  • Do not import a module from the core or from a different module. Use the injector.

For the build and test commands, read Develop localspace.