codeexecutor

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: Apache-2.0 Imports: 15 Imported by: 6

Documentation

Overview

Package codeexecutor provides an interface and utilities for executing code blocks and running programs in workspaces.

Package codeexecutor holds workspace metadata helpers and constants.

Package codeexecutor defines workspace types and helpers.

Index

Constants

View Source
const (
	// ProgramStatusRunning indicates the interactive program is still
	// running and may accept more input.
	ProgramStatusRunning = "running"
	// ProgramStatusExited indicates the interactive program has exited.
	ProgramStatusExited = "exited"
)
View Source
const (
	// DirSkills contains read-only staged skill trees.
	DirSkills = "skills"
	// DirWork contains writable shared intermediates.
	DirWork = "work"
	// DirRuns contains per-run working directories.
	DirRuns = "runs"
	// DirOut contains collected outputs for artifacting.
	DirOut = "out"
	// MetaFileName is the metadata file name at workspace root.
	MetaFileName = "metadata.json"
)

Well-known subdirectories in a workspace.

View Source
const (
	EnvSkillsDir = "SKILLS_DIR"
	EnvWorkDir   = "WORK_DIR"
	EnvOutputDir = "OUTPUT_DIR"
	EnvRunDir    = "RUN_DIR"
	EnvSkillName = "SKILL_NAME"
)

Additional environment variable keys injected at runtime.

View Source
const (
	// WorkspaceEnvDirKey is set in program env to point to the workspace
	// directory for outputs and scratch files.
	WorkspaceEnvDirKey = "WORKSPACE_DIR"

	// Span names for workspace lifecycle.
	SpanWorkspaceCreate     = "workspace.create"
	SpanWorkspaceCleanup    = "workspace.cleanup"
	SpanWorkspaceStageFiles = "workspace.stage.files"
	SpanWorkspaceStageDir   = "workspace.stage.dir"
	SpanWorkspaceRun        = "workspace.run"
	SpanWorkspaceCollect    = "workspace.collect"
	SpanWorkspaceInline     = "workspace.inline"

	// Common attribute keys used in tracing spans.
	AttrExecID    = "exec_id"
	AttrPath      = "path"
	AttrCount     = "count"
	AttrPatterns  = "patterns"
	AttrCmd       = "cmd"
	AttrCwd       = "cwd"
	AttrExitCode  = "exit_code"
	AttrTimedOut  = "timed_out"
	AttrHostPath  = "host_path"
	AttrTo        = "to"
	AttrMountUsed = "mount_used"
)

Well-known environment and telemetry keys to avoid magic strings.

View Source
const (
	// DefaultScriptFileMode is the default POSIX mode for text scripts.
	DefaultScriptFileMode = 0o644
	// DefaultExecFileMode is the default POSIX mode for executables.
	DefaultExecFileMode = 0o755
	// InlineSourceDir is the subdirectory where inline code blocks
	// are written and executed as the current working directory.
	InlineSourceDir = "src"
)

Default file modes and common subdirectories.

Variables

This section is empty.

Functions

func ArtifactServiceFromContext added in v0.5.0

func ArtifactServiceFromContext(
	ctx context.Context,
) (artifact.Service, bool)

ArtifactServiceFromContext fetches the artifact.Service previously stored by WithArtifactService. It returns the service and a boolean indicating presence.

func BuildBlockSpec added in v0.5.0

func BuildBlockSpec(
	idx int,
	b CodeBlock,
) (file string, mode uint32, cmd string, args []string, err error)

BuildBlockSpec maps a code block into a file name, mode, command, and arguments suitable for execution via RunProgram. It supports a minimal set of languages to keep behavior predictable.

func DirDigest added in v0.5.0

func DirDigest(root string) (string, error)

DirDigest computes a stable digest of a directory tree. It walks the tree, sorts entries, and hashes relative path and contents.

func EnsureLayout added in v0.5.0

func EnsureLayout(root string) (map[string]string, error)

EnsureLayout creates standard workspace subdirectories and a metadata file when absent. It returns full paths for convenience.

func IsTextMIME added in v1.3.0

func IsTextMIME(mimeType string) bool

IsTextMIME reports whether mimeType describes a text format that is safe to inline as UTF-8 text.

func LoadArtifactHelper added in v0.5.0

func LoadArtifactHelper(
	ctx context.Context, name string, version *int,
) ([]byte, string, int, error)

LoadArtifactHelper resolves artifact name@version via callback context. If version is nil, loads latest. Returns data, mime, actual version.

func NormalizeGlobs added in v0.6.0

func NormalizeGlobs(patterns []string) []string

NormalizeGlobs rewrites glob patterns that use well-known environment-style prefixes such as $OUTPUT_DIR into workspace- relative paths like out/. It understands the variables injected by workspace runtimes: WORKSPACE_DIR, SKILLS_DIR, WORK_DIR, OUTPUT_DIR.

Examples:

$OUTPUT_DIR/a.txt   -> out/a.txt
${WORK_DIR}/x/**    -> work/x/**
$WORKSPACE_DIR/out  -> out

Unknown variables and patterns without a prefix are returned as-is.

func ParseArtifactRef added in v0.5.0

func ParseArtifactRef(ref string) (string, *int, error)

ParseArtifactRef splits "name@version" into name and optional version.

func SaveArtifactHelper added in v0.5.0

func SaveArtifactHelper(
	ctx context.Context, filename string, data []byte, mime string,
) (int, error)

SaveArtifactHelper saves a file as artifact using callback context.

func SaveMetadata added in v0.5.0

func SaveMetadata(root string, md WorkspaceMetadata) error

SaveMetadata writes metadata.json to the workspace root.

func WithArtifactService added in v0.5.0

func WithArtifactService(
	ctx context.Context, svc artifact.Service,
) context.Context

WithArtifactService stores an artifact.Service in the context. Callers retrieve it in lower layers to load/save artifacts without importing higher-level packages.

func WithArtifactSession added in v0.5.0

func WithArtifactSession(
	ctx context.Context, info artifact.SessionInfo,
) context.Context

WithArtifactSession stores artifact session info in context.

Types

type Capabilities added in v0.5.0

type Capabilities struct {
	Isolation      string
	NetworkAllowed bool
	ReadOnlyMount  bool
	Streaming      bool
	MaxDiskBytes   int64
}

Capabilities describes engine capabilities for selection.

type CodeBlock

type CodeBlock struct {
	Code     string `json:"code"`
	Language string `json:"language"`
}

CodeBlock represents a single block of code to be executed.

func ExtractCodeBlock

func ExtractCodeBlock(
	input string,
	delimiter CodeBlockDelimiter,
) []CodeBlock

ExtractCodeBlock extracts fenced code blocks using the given delimiter.

type CodeBlockDelimiter

type CodeBlockDelimiter struct {
	Start string
	End   string
}

CodeBlockDelimiter defines the start and end delimiters for code blocks.

type CodeExecutionInput

type CodeExecutionInput struct {
	CodeBlocks  []CodeBlock `json:"code_blocks"`
	ExecutionID string      `json:"execution_id,omitempty"`
}

CodeExecutionInput is the input for code execution.

type CodeExecutionResult

type CodeExecutionResult struct {
	Output      string `json:"output"`
	OutputFiles []File `json:"output_files,omitempty"`
}

CodeExecutionResult is the result of code execution including files.

func (CodeExecutionResult) String

func (r CodeExecutionResult) String() string

String formats a human-readable result.

type CodeExecutor

type CodeExecutor interface {
	// ExecuteCode executes the code blocks provided in the input and
	// returns the result.
	ExecuteCode(context.Context, CodeExecutionInput) (CodeExecutionResult, error)
	// CodeBlockDelimiter returns the delimiters used for code blocks.
	CodeBlockDelimiter() CodeBlockDelimiter
}

CodeExecutor executes code blocks via a friendly front-door API.

func NewEnvInjectingCodeExecutor added in v1.8.0

func NewEnvInjectingCodeExecutor(
	exec CodeExecutor,
	provider RunEnvProvider,
) CodeExecutor

NewEnvInjectingCodeExecutor wraps exec so that Engine() returns an env-injecting engine. exec must implement EngineProvider; if it does not (or is nil), the original executor is returned unchanged.

This is the recommended top-level entry point: pass the wrapped executor to llmagent.WithCodeExecutor and all tool paths (skill_run, workspace_exec, interactive sessions) will automatically receive the injected environment variables.

type Engine added in v0.5.0

type Engine interface {
	Manager() WorkspaceManager
	FS() WorkspaceFS
	Runner() ProgramRunner
	// Describe returns optional capabilities.
	Describe() Capabilities
}

Engine is a backend that provides workspace and execution services.

func NewEngine added in v0.5.0

func NewEngine(
	m WorkspaceManager,
	f WorkspaceFS,
	r ProgramRunner,
) Engine

NewEngine constructs a simple Engine from its components.

func NewEnvInjectingEngine added in v1.8.0

func NewEnvInjectingEngine(eng Engine, provider RunEnvProvider) Engine

NewEnvInjectingEngine wraps eng so that every RunProgram and StartProgram call merges environment variables from provider into the spec before delegating to the underlying runner.

If eng or provider is nil the original engine is returned unchanged.

type EngineProvider added in v0.5.0

type EngineProvider interface {
	Engine() Engine
}

EngineProvider is an optional interface that a CodeExecutor may implement to expose its underlying engine for skill tools.

type File

type File struct {
	Name      string `json:"name"`
	Content   string `json:"content,omitempty"`
	MIMEType  string `json:"mime_type"`
	SizeBytes int64  `json:"size_bytes,omitempty"`
	Truncated bool   `json:"truncated,omitempty"`
}

File represents a file generated during code execution.

type FileRef added in v0.5.0

type FileRef struct {
	Name      string
	MIMEType  string
	Content   string
	SavedAs   string
	Version   int
	SizeBytes int64
	Truncated bool
}

FileRef references a file collected from workspace.

type InputRecord added in v0.5.0

type InputRecord struct {
	From      string    `json:"from"`
	To        string    `json:"to"`
	Resolved  string    `json:"resolved,omitempty"`
	Version   *int      `json:"version,omitempty"`
	Mode      string    `json:"mode,omitempty"`
	Timestamp time.Time `json:"ts"`
}

InputRecord tracks a staged input resolution.

type InputSpec added in v0.5.0

type InputSpec struct {
	From string
	To   string
	Mode string
	Pin  bool
}

InputSpec declares a single input mapping into the workspace.

From supports schemes:

  • artifact://name[@version]
  • host://abs/path
  • workspace://rel/path
  • skill://name/rel/path

To is a workspace-relative destination (default: WORK_DIR/inputs/<name>). Mode hints the strategy: "link" (symlink/hardlink where possible) or "copy" (default fallback when link is not possible).

type InteractiveProgramRunner added in v1.7.0

type InteractiveProgramRunner interface {
	StartProgram(
		ctx context.Context,
		ws Workspace,
		spec InteractiveProgramSpec,
	) (ProgramSession, error)
}

InteractiveProgramRunner is an optional executor capability for multi-turn interactive program execution.

type InteractiveProgramSpec added in v1.7.0

type InteractiveProgramSpec struct {
	RunProgramSpec
	TTY bool
}

InteractiveProgramSpec describes a session-oriented program invocation in a workspace.

type OutputManifest added in v0.5.0

type OutputManifest struct {
	Files     []FileRef
	LimitsHit bool
}

OutputManifest is the structured result of CollectOutputs.

type OutputRecord added in v0.5.0

type OutputRecord struct {
	Globs     []string  `json:"globs"`
	SavedAs   []string  `json:"saved_as,omitempty"`
	Versions  []int     `json:"versions,omitempty"`
	LimitsHit bool      `json:"limits_hit"`
	Timestamp time.Time `json:"ts"`
}

OutputRecord tracks an output collection run.

type OutputSpec added in v0.5.0

type OutputSpec struct {
	Globs         []string
	MaxFiles      int
	MaxFileBytes  int64
	MaxTotalBytes int64
	Save          bool
	NameTemplate  string
	Inline        bool
}

OutputSpec declares outputs to collect and optionally persist. Globs are workspace-relative patterns; implementations should support ** semantics.

func (*OutputSpec) UnmarshalJSON added in v1.3.0

func (s *OutputSpec) UnmarshalJSON(data []byte) error

UnmarshalJSON accepts both legacy Go-style keys (MaxFiles) and the recommended snake_case keys (max_files).

type ProgramLog added in v1.7.0

type ProgramLog struct {
	Output     string
	Offset     int
	NextOffset int
}

ProgramLog returns output from a specific offset without mutating the incremental cursor.

type ProgramPoll added in v1.7.0

type ProgramPoll struct {
	Status     string
	Output     string
	Offset     int
	NextOffset int
	ExitCode   *int
}

ProgramPoll captures the latest incremental output for a running or exited interactive program session.

type ProgramResultProvider added in v1.7.0

type ProgramResultProvider interface {
	RunResult() RunResult
}

ProgramResultProvider optionally exposes a final RunResult for interactive sessions after they exit.

type ProgramRunner added in v0.5.0

type ProgramRunner interface {
	RunProgram(ctx context.Context, ws Workspace,
		spec RunProgramSpec) (RunResult, error)
}

ProgramRunner executes programs within a workspace.

type ProgramSession added in v1.7.0

type ProgramSession interface {
	ID() string
	Poll(limit *int) ProgramPoll
	Log(offset *int, limit *int) ProgramLog
	Write(data string, newline bool) error
	Kill(grace time.Duration) error
	Close() error
}

ProgramSession exposes a running interactive program session.

Implementations are expected to be safe for concurrent use by tool-layer callers. In particular, Poll, Log, Write, Kill, Close, and any optional state/result helpers may be invoked concurrently for the same session.

type ProgramState added in v1.8.0

type ProgramState struct {
	Status   string
	ExitCode *int
}

ProgramState captures non-streaming session status without advancing any incremental output cursor.

type ProgramStateProvider added in v1.8.0

type ProgramStateProvider interface {
	State() ProgramState
}

ProgramStateProvider optionally exposes non-destructive session state for lifecycle management and cleanup.

type PutFile added in v0.5.0

type PutFile struct {
	Path    string // relative to workspace root
	Content []byte
	Mode    uint32 // POSIX mode bits (e.g., 0644, 0755)
}

PutFile describes a file to place into a workspace.

type ResourceLimits added in v0.5.0

type ResourceLimits struct {
	// CPUPercent is an approximate percentage of one CPU.
	CPUPercent int
	// MemoryMB is a soft limit in megabytes.
	MemoryMB int
	// MaxPIDs limits number of processes/threads.
	MaxPIDs int
}

ResourceLimits restrict program execution resources.

type RunEnvProvider added in v1.8.0

type RunEnvProvider func(ctx context.Context) map[string]string

RunEnvProvider returns per-run environment variables derived from the execution context. Implementations typically read caller-supplied state (e.g. user tokens) from the context and return them as key-value pairs to be merged into every RunProgramSpec executed by the wrapped engine.

Returned entries never override values already present in the spec's Env map, preserving explicit per-call overrides from tools.

type RunProgramSpec added in v0.5.0

type RunProgramSpec struct {
	Cmd     string
	Args    []string
	Env     map[string]string
	Cwd     string // relative to workspace root
	Stdin   string
	Timeout time.Duration
	Limits  ResourceLimits
}

RunProgramSpec describes a program invocation in a workspace.

type RunResult added in v0.5.0

type RunResult struct {
	Stdout   string
	Stderr   string
	ExitCode int
	Duration time.Duration
	TimedOut bool
}

RunResult captures a single program run result.

type SkillMeta added in v0.5.0

type SkillMeta struct {
	Name     string    `json:"name"`
	RelPath  string    `json:"rel_path"`
	Digest   string    `json:"digest"`
	Mounted  bool      `json:"mounted"`
	StagedAt time.Time `json:"staged_at"`
}

SkillMeta records a staged skill snapshot.

type StageOptions added in v0.5.0

type StageOptions struct {
	// ReadOnly makes the staged tree non-writable after copy/mount.
	ReadOnly bool
	// AllowMount lets implementations use read-only mounts when possible.
	AllowMount bool
}

StageOptions controls directory staging behavior.

type Workspace added in v0.5.0

type Workspace struct {
	ID   string
	Path string
}

Workspace represents an isolated execution workspace. Path is host path for local runtime or a logical mount path for containers.

type WorkspaceFS added in v0.5.0

type WorkspaceFS interface {
	PutFiles(ctx context.Context, ws Workspace,
		files []PutFile) error
	StageDirectory(ctx context.Context, ws Workspace,
		src, to string, opt StageOptions) error
	Collect(ctx context.Context, ws Workspace,
		patterns []string) ([]File, error)
	// StageInputs maps external inputs into workspace according to
	// the provided specs. Implementations should prefer link
	// strategies when Mode=="link" and environment allows it.
	StageInputs(ctx context.Context, ws Workspace,
		specs []InputSpec) error
	// CollectOutputs applies the declarative output spec to collect
	// files and optionally persist artifacts.
	CollectOutputs(ctx context.Context, ws Workspace,
		spec OutputSpec) (OutputManifest, error)
}

WorkspaceFS performs file operations within a workspace.

type WorkspaceManager added in v0.5.0

type WorkspaceManager interface {
	CreateWorkspace(ctx context.Context, execID string,
		pol WorkspacePolicy) (Workspace, error)
	Cleanup(ctx context.Context, ws Workspace) error
}

WorkspaceManager handles workspace lifecycle.

type WorkspaceMetadata added in v0.5.0

type WorkspaceMetadata struct {
	Version    int                  `json:"version"`
	CreatedAt  time.Time            `json:"created_at"`
	UpdatedAt  time.Time            `json:"updated_at"`
	LastAccess time.Time            `json:"last_access"`
	Skills     map[string]SkillMeta `json:"skills"`
	Inputs     []InputRecord        `json:"inputs,omitempty"`
	Outputs    []OutputRecord       `json:"outputs,omitempty"`
}

WorkspaceMetadata describes staged skills and recent activity.

func LoadMetadata added in v0.5.0

func LoadMetadata(root string) (WorkspaceMetadata, error)

LoadMetadata loads metadata.json from workspace root. When missing, an empty metadata with defaults is returned without error.

type WorkspacePolicy added in v0.5.0

type WorkspacePolicy struct {
	// Isolated toggles runtime isolation (e.g., container usage).
	Isolated bool
	// Persist keeps workspace after Cleanup when true.
	Persist bool
	// MaxDiskBytes is a soft limit for staged and produced files.
	MaxDiskBytes int64
}

WorkspacePolicy configures workspace behavior.

type WorkspaceRegistry added in v0.5.0

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

WorkspaceRegistry keeps a process-level mapping of logical IDs to created workspaces for reuse within a session.

func NewWorkspaceRegistry added in v0.5.0

func NewWorkspaceRegistry() *WorkspaceRegistry

NewWorkspaceRegistry creates a new in-memory registry.

func (*WorkspaceRegistry) Acquire added in v0.5.0

Acquire creates or returns an existing workspace with the given id.

Directories

Path Synopsis
container module
jupyter module
Package local provides a CodeExecutor that executes code blocks in the local environment.
Package local provides a CodeExecutor that executes code blocks in the local environment.

Jump to

Keyboard shortcuts

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