environment

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: Apache-2.0 Imports: 17 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

func Initialize

func Initialize(client *dagger.Client) error

func InitializeLocalRemote

func InitializeLocalRemote(ctx context.Context, localRepoPath string) (string, error)

func List

func List(ctx context.Context, source string) ([]string, error)

Types

type EndpointMapping

type EndpointMapping struct {
	Internal string `json:"internal"`
	External string `json:"external"`
}

type EndpointMappings

type EndpointMappings map[int]*EndpointMapping

type Environment

type Environment struct {
	ID       string `json:"-"`
	Name     string `json:"-"`
	Source   string `json:"-"`
	Worktree string `json:"-"`

	Instructions  string   `json:"-"`
	Workdir       string   `json:"workdir"`
	BaseImage     string   `json:"base_image"`
	SetupCommands []string `json:"setup_commands,omitempty"`
	Secrets       []string `json:"secrets,omitempty"`

	History History `json:"-"`
	// contains filtered or unexported fields
}

func Create

func Create(ctx context.Context, explanation, source, name string) (*Environment, error)

func Get

func Get(idOrName string) *Environment

func Open

func Open(ctx context.Context, explanation, source, id string) (*Environment, error)

func (*Environment) Checkpoint

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

func (*Environment) Delete

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

func (*Environment) DeleteLocalRemoteBranch

func (env *Environment) DeleteLocalRemoteBranch() error

func (*Environment) DeleteWorktree

func (env *Environment) DeleteWorktree() error

func (*Environment) Download

func (s *Environment) Download(ctx context.Context, source string, target string) 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) Fork

func (env *Environment) Fork(ctx context.Context, explanation, name string, version *Version) (*Environment, error)

func (*Environment) GetWorktreePath

func (env *Environment) GetWorktreePath() (string, error)

func (*Environment) InitializeWorktree

func (env *Environment) InitializeWorktree(ctx context.Context, localRepoPath string) (string, error)

func (*Environment) RemoteDiff

func (s *Environment) RemoteDiff(ctx context.Context, source string, target string) (string, error)

func (*Environment) Revert

func (env *Environment) Revert(ctx context.Context, explanation string, version Version) error

func (*Environment) RevisionDiff

func (s *Environment) RevisionDiff(ctx context.Context, path string, fromVersion, toVersion Version) (string, error)

func (*Environment) Run

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

func (*Environment) RunBackground

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

func (*Environment) SetEnv

func (env *Environment) SetEnv(ctx context.Context, explanation string, envs []string) error

func (*Environment) Terminal

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

func (*Environment) Update

func (env *Environment) Update(ctx context.Context, explanation, instructions, baseImage string, setupCommands, secrets []string) error

func (*Environment) Upload

func (s *Environment) Upload(ctx context.Context, explanation, source string, target string) error

type History

type History []*Revision

func StateFromCommit

func StateFromCommit(ctx context.Context, repoDir, commit string) (History, error)

func (History) Get

func (h History) Get(version Version) *Revision

func (History) Latest

func (h History) Latest() *Revision

func (History) LatestVersion

func (h History) LatestVersion() Version

type Revision

type Revision struct {
	Version     Version   `json:"version"`
	Name        string    `json:"name"`
	Explanation string    `json:"explanation"`
	Output      string    `json:"output,omitempty"`
	CreatedAt   time.Time `json:"created_at"`
	State       string    `json:"state"`
	// contains filtered or unexported fields
}

type Version

type Version int

Jump to

Keyboard shortcuts

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