environment

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

Environments

An environment is an isolated, containerized development workspace that combines Docker containers with Git branches to provide agents with safe, persistent workspaces.

What is an Environment?

Each environment consists of:

  • Git Branch: Dedicated branch tracking all changes and history
  • Container: Dagger container with your code and dependencies
  • History: Versioned snapshots of container state changes appended to the branch as notes
  • Configuration: Base image, setup commands, secrets, and instructions that can be checked into the source repo.

Key Features

  • Branch-Based: Each environment is a Git branch that syncs into the container-use/ remote
  • Isolation: Each environment runs in its own container and branch
  • Persistence: All changes automatically committed with full history
  • Standard Git:
    • Use git log to view source code history
    • Use git log --notes=container-use to view container state history
    • Use git checkout env-branch to inspect any environment's work - each env branch tracks the upstream container-use/
  • State Recovery: Container states stored in Git notes for reconstruction

How It Works

When you create an environment, container-use:

  1. Creates a new Git branch in your source repo (e.g., env-name/adverb-animal)
  2. Sets up a container-use remote branch inside ~/.config/container-use/repos/project/
  3. Sets up a worktree copy of the branch in ~/.config/container-use/worktrees/project/
  4. Spins up a Dagger container with that worktree copied into /workdir

When an agent runs commands:

  1. Commands execute inside the isolated container
  2. File changes get written back to the container filesystem
  3. Everything gets committed to the environment's Git branch automatically
  4. Container state snapshots are stored as Git notes for later recovery

Each environment is just a Git branch that your source repo tracks on the container-use/ remote. You can inspect any environment's work using standard Git commands, and the container state can always be reconstructed from an environment branch's Git history.

Architecture

projectName/ Source Repo            container-use/ Remote
├── main                   ←──→ ├── main
├── feature-branch         ←──→ ├── feature-branch
└── env-name/adverb-animal ←──→ └── env-name/adverb-animal
                                       │
                                       │ (host filesystem implementation)
                                       ▼
                    ~/.config/container-use/
                    ├── repos/projectName/ (bare)
                    └── worktrees/env-name/adverb-animal (only env branches become worktrees)
                        ├── .git -> ../../repos/projectName/worktrees/env-name/adverb-animal
                        └── (your code)
                            │
                            ▼
                        Container
                        └── /workdir

The diagram shows how branches sync between your source repo and the container-use remote. Each environment branch (like env-name/adverb-animal) exists in both places and stays synchronized.

Below the branch level, the system creates a bare Git repository and worktree in ~/.config/container-use/ - this is plumbing to make the Git operations work with minimal modifications to your source repository. The worktree contains a copy of your code that gets mounted into the Docker container at /workdir.

So the flow is: Branch (the logical environment) → Worktree (filesystem implementation) → Container (where code actually runs).

Files

  • environment.go - Core environment management
  • git.go - Worktree and Git integration
  • filesystem.go - File operations within containers

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EndpointMapping

type EndpointMapping struct {
	EnvironmentInternal string `json:"environment_internal"`
	HostExternal        string `json:"host_external"`
}

type EndpointMappings

type EndpointMappings map[int]*EndpointMapping

type Environment

type Environment struct {
	*EnvironmentInfo

	Services []*Service
	Notes    Notes
	// contains filtered or unexported fields
}

func Load added in v0.1.0

func Load(ctx context.Context, dag *dagger.Client, id string, state []byte, worktree string) (*Environment, error)

func New added in v0.1.0

func New(ctx context.Context, dag *dagger.Client, id, title, worktree string, initialSourceDir *dagger.Directory) (*Environment, error)

func (*Environment) AddService added in v0.0.3

func (env *Environment) AddService(ctx context.Context, explanation string, cfg *ServiceConfig) (*Service, error)

func (*Environment) Checkpoint

func (env *Environment) Checkpoint(ctx context.Context, target string) (string, error)

func (*Environment) Export added in v0.1.0

func (env *Environment) Export(ctx context.Context) (rerr error)

func (*Environment) FileDelete

func (s *Environment) FileDelete(ctx context.Context, explanation, targetFile string) error

func (*Environment) FileList

func (s *Environment) FileList(ctx context.Context, path string) (string, error)

func (*Environment) FileRead

func (s *Environment) FileRead(ctx context.Context, targetFile string, shouldReadEntireFile bool, startLineOneIndexed int, endLineOneIndexedInclusive int) (string, error)

func (*Environment) FileWrite

func (s *Environment) FileWrite(ctx context.Context, explanation, targetFile, contents string) error

func (*Environment) Run

func (env *Environment) Run(ctx context.Context, command, shell string, useEntrypoint bool) (string, error)

func (*Environment) RunBackground

func (env *Environment) RunBackground(ctx context.Context, command, shell string, ports []int, useEntrypoint bool) (EndpointMappings, error)

func (*Environment) Terminal

func (env *Environment) Terminal(ctx context.Context) error

func (*Environment) UpdateConfig added in v0.0.3

func (env *Environment) UpdateConfig(ctx context.Context, explanation string, newConfig *EnvironmentConfig) error

type EnvironmentConfig added in v0.0.3

type EnvironmentConfig struct {
	Instructions  string         `json:"-"`
	Workdir       string         `json:"workdir,omitempty"`
	BaseImage     string         `json:"base_image,omitempty"`
	SetupCommands []string       `json:"setup_commands,omitempty"`
	Env           []string       `json:"env,omitempty"`
	Secrets       []string       `json:"secrets,omitempty"`
	Services      ServiceConfigs `json:"services,omitempty"`
}

func DefaultConfig added in v0.0.3

func DefaultConfig() *EnvironmentConfig

func (*EnvironmentConfig) Copy added in v0.0.3

func (config *EnvironmentConfig) Copy() *EnvironmentConfig

func (*EnvironmentConfig) Load added in v0.0.3

func (config *EnvironmentConfig) Load(baseDir string) error

func (*EnvironmentConfig) Locked added in v0.0.3

func (config *EnvironmentConfig) Locked(baseDir string) bool

func (*EnvironmentConfig) Save added in v0.0.3

func (config *EnvironmentConfig) Save(baseDir string) error

type EnvironmentInfo added in v0.1.0

type EnvironmentInfo struct {
	Config *EnvironmentConfig
	State  *State

	ID string
	//TODO(braa): I think we only need this for Export, now. remove and pass explicitly.
	Worktree string
}

EnvironmentInfo contains basic metadata about an environment without requiring dagger operations

func LoadInfo added in v0.1.0

func LoadInfo(ctx context.Context, id string, state []byte, worktree string) (*EnvironmentInfo, error)

LoadInfo loads basic environment metadata without requiring dagger operations. This is useful for operations that only need access to configuration and state information without the overhead of initializing container operations.

type Notes added in v0.1.0

type Notes struct {
	// contains filtered or unexported fields
}

func (*Notes) Add added in v0.1.0

func (n *Notes) Add(format string, a ...any)

func (*Notes) Clear added in v0.1.0

func (n *Notes) Clear()

func (*Notes) Pop added in v0.1.0

func (n *Notes) Pop() string

func (*Notes) String added in v0.1.0

func (n *Notes) String() string

type Service added in v0.0.3

type Service struct {
	Config    *ServiceConfig   `json:"config"`
	Endpoints EndpointMappings `json:"endpoints"`
	// contains filtered or unexported fields
}

type ServiceConfig added in v0.0.3

type ServiceConfig struct {
	Name         string   `json:"name,omitempty"`
	Image        string   `json:"image,omitempty"`
	Command      string   `json:"command,omitempty"`
	ExposedPorts []int    `json:"exposed_ports,omitempty"`
	Env          []string `json:"env,omitempty"`
	Secrets      []string `json:"secrets,omitempty"`
}

type ServiceConfigs added in v0.0.3

type ServiceConfigs []*ServiceConfig

func (ServiceConfigs) Get added in v0.0.3

func (sc ServiceConfigs) Get(name string) *ServiceConfig

type State added in v0.1.0

type State struct {
	Container string    `json:"container,omitempty"`
	Title     string    `json:"title,omitempty"`
	CreatedAt time.Time `json:"created_at,omitempty"`
	UpdatedAt time.Time `json:"updated_at,omitempty"`
}

func (*State) Marshal added in v0.1.0

func (s *State) Marshal() ([]byte, error)

func (*State) Unmarshal added in v0.1.0

func (s *State) Unmarshal(data []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL