Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

3.7 KB
Raw Blame
package runtime

import (
	"context"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"syscall"
)

type localSandbox struct {
	id, workspace string
	environment   map[string]string
	driver        string
}

func (s *localSandbox) ID() string        { return s.id }
func (s *localSandbox) Workspace() string { return s.workspace }
func (s *localSandbox) Driver() string {
	if s.driver != "" {
		return s.driver
	}
	return "local"
}
func (s *localSandbox) Close(context.Context) error {
	stopHostBrowsers(s.workspace)
	return nil
}
func (s *localSandbox) Exec(ctx context.Context, command CommandSpec, sink EventSink) (string, error) {
	cmd, err := s.command(ctx, command, controlledBaseEnvironment(os.Environ()))
	if err != nil {
		return "", fmt.Errorf("%w: %w", errProcessNotStarted, err)
	}
	return runCommand(cmd, sink, command)
}

func (s *localSandbox) command(ctx context.Context, command CommandSpec, base []string) (*exec.Cmd, error) {
	if len(command.Argv) == 0 {
		return nil, fmt.Errorf("empty command")
	}
	workspace, err := filepath.Abs(s.workspace)
	if err != nil {
		return nil, fmt.Errorf("sandbox workspace: %w", err)
	}
	info, err := os.Stat(workspace)
	if err != nil || !info.IsDir() {
		return nil, fmt.Errorf("sandbox workspace missing %s: %w", workspace, err)
	}
	cmd := exec.CommandContext(ctx, command.Argv[0], command.Argv[1:]...)
	cmd.Dir = workspace
	cmd.Env = appendEnvironment(base, s.environment, command.environment)
	cmd.Env = upsertEnv(cmd.Env, "PWD", workspace)
	cmd.Env = upsertEnv(cmd.Env, "GIT_CEILING_DIRECTORIES", filepath.Dir(workspace))
	cmd.Stdin = command.Stdin
	if command.WorkingDir != "" {
		cmd.Dir = command.WorkingDir
		cmd.Env = upsertEnv(cmd.Env, "PWD", command.WorkingDir)
	}
	if command.OwnProcessGroup {
		cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
		cmd.Cancel = func() error {
			if cmd.Process == nil {
				return nil
			}
			return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
		}
	}
	return cmd, nil
}

// ExecuteLocalCommand runs a prepared provider command with the same closed
// credential binding used by managed sandboxes. A temporary private HOME keeps
// control-plane utilities (for example requirement clarification) from reading
// or modifying the operator's CLI state or the inspected repository.
func ExecuteLocalCommand(ctx context.Context, workspace string, command CommandSpec, credentials RunCredentials, sink EventSink) (string, error) {
	sandbox := &localSandbox{id: "control-plane", workspace: workspace}
	command, closeGit, err := prepareRunGitBinding(ctx, sandbox, command, credentials)
	if err != nil {
		return "", err
	}
	defer closeGit()
	if credentials.GitHTTPCredentials != nil {
		credentials.GitHTTPHosts = runGitHosts(credentials)
	}
	var cleanup func()
	if command.PrivateHome {
		root, err := os.MkdirTemp("", "agentflow-private-home-")
		if err != nil {
			return "", err
		}
		cleanup = func() { _ = os.RemoveAll(root) }
		command.PrivateHome = false
		env := make(map[string]string, len(command.environment)+2)
		for key, value := range command.environment {
			env[key] = value
		}
		env["HOME"] = root
		env["TMPDIR"] = filepath.Join(root, "tmp")
		command.environment = env
		if err := os.MkdirAll(command.environment["TMPDIR"], 0o700); err != nil {
			cleanup()
			return "", err
		}
	}
	if cleanup != nil {
		defer cleanup()
	}
	bound, err := bindCommand(sandbox, workspace, command, credentials)
	if err != nil {
		return "", err
	}
	stopSSH, err := bindCommandGitSSH(sandbox, workspace, bound, credentials)
	if err != nil {
		return "", err
	}
	defer stopSSH()
	defer scrubRunGitCredentials(ctx, sandbox, workspace, bound, credentials)
	out, err := sandbox.Exec(ctx, bound, runGitEventSink(sink, credentials))
	return redactRunGitSecrets(out, credentials), err
}