storage

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package storage provides run storage infrastructure for Moat. It handles persisting and loading run metadata, logs, and traces.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultBaseDir

func DefaultBaseDir() string

DefaultBaseDir returns the default base directory for run storage. This is ~/.moat/runs.

func ListRunDirs

func ListRunDirs(baseDir string) ([]string, error)

ListRunDirs returns all run IDs that have stored metadata. It scans baseDir for directories containing metadata.json.

Types

type ExecEvent

type ExecEvent struct {
	Timestamp  time.Time      `json:"timestamp"`
	PID        int            `json:"pid"`
	PPID       int            `json:"ppid"`
	Command    string         `json:"command"`
	Args       []string       `json:"args"`
	WorkingDir string         `json:"working_dir,omitempty"`
	ExitCode   *int           `json:"exit_code,omitempty"`
	Duration   *time.Duration `json:"duration,omitempty"`
}

ExecEvent represents a command execution captured by the tracer. This is a duplicate of trace.ExecEvent to avoid circular imports.

type LogEntry

type LogEntry struct {
	Timestamp time.Time `json:"ts"`
	Line      string    `json:"line"`
}

LogEntry represents a single log line with timestamp.

type LogWriter

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

LogWriter wraps writes to add timestamps.

func (*LogWriter) Close

func (w *LogWriter) Close() error

Close closes the underlying file.

func (*LogWriter) Write

func (w *LogWriter) Write(p []byte) (n int, err error)

Write implements io.Writer, adding timestamps to each line.

type Metadata

type Metadata struct {
	Name        string         `json:"name"`
	Workspace   string         `json:"workspace"`
	Grants      []string       `json:"grants,omitempty"`
	Agent       string         `json:"agent,omitempty"` // Agent type from config (e.g., "claude-code")
	Image       string         `json:"image,omitempty"` // Container image used
	Ports       map[string]int `json:"ports,omitempty"`
	ContainerID string         `json:"container_id,omitempty"`
	State       string         `json:"state,omitempty"`
	Interactive bool           `json:"interactive,omitempty"`
	CreatedAt   time.Time      `json:"created_at,omitempty"`
	StartedAt   time.Time      `json:"started_at,omitempty"`
	StoppedAt   time.Time      `json:"stopped_at,omitempty"`
	Error       string         `json:"error,omitempty"`

	// Worktree fields (set when run was created via moat wt or --wt)
	WorktreeBranch string `json:"worktree_branch,omitempty"`
	WorktreePath   string `json:"worktree_path,omitempty"`
	WorktreeRepoID string `json:"worktree_repo_id,omitempty"`

	// Service dependency fields
	ServiceContainers map[string]string `json:"service_containers,omitempty"` // service name -> container ID

	// BuildKit sidecar fields (docker:dind only)
	BuildkitContainerID string `json:"buildkit_container_id,omitempty"`
	NetworkID           string `json:"network_id,omitempty"`
}

Metadata holds information about an agent run.

type NetworkRequest

type NetworkRequest struct {
	Timestamp       time.Time         `json:"ts"`
	Method          string            `json:"method"`
	URL             string            `json:"url"`
	StatusCode      int               `json:"status_code"`
	Duration        int64             `json:"duration_ms"`
	Error           string            `json:"error,omitempty"`
	RequestHeaders  map[string]string `json:"req_headers,omitempty"`
	ResponseHeaders map[string]string `json:"resp_headers,omitempty"`
	RequestBody     string            `json:"req_body,omitempty"`
	ResponseBody    string            `json:"resp_body,omitempty"`
	BodyTruncated   bool              `json:"truncated,omitempty"`
}

NetworkRequest represents a logged HTTP request.

type RunStore

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

RunStore manages storage for a single agent run.

func NewRunStore

func NewRunStore(baseDir, runID string) (*RunStore, error)

NewRunStore creates a new RunStore for the given run ID. It creates the run directory under baseDir if it doesn't exist.

func (*RunStore) Dir

func (s *RunStore) Dir() string

Dir returns the directory path for this run's storage.

func (*RunStore) LoadMetadata

func (s *RunStore) LoadMetadata() (Metadata, error)

LoadMetadata reads the metadata from metadata.json in the run directory.

func (*RunStore) LogWriter

func (s *RunStore) LogWriter() (*LogWriter, error)

LogWriter returns a writer that timestamps log entries.

func (*RunStore) ReadDockerfile

func (s *RunStore) ReadDockerfile() (string, error)

ReadDockerfile reads the Dockerfile from the run directory.

func (*RunStore) ReadExecEvents

func (s *RunStore) ReadExecEvents() ([]ExecEvent, error)

ReadExecEvents reads all execution events from exec.jsonl.

func (*RunStore) ReadLogs

func (s *RunStore) ReadLogs(offset, limit int) ([]LogEntry, error)

ReadLogs reads log entries with offset and limit.

func (*RunStore) ReadNetworkRequests

func (s *RunStore) ReadNetworkRequests() ([]NetworkRequest, error)

ReadNetworkRequests reads all network requests.

func (*RunStore) ReadSecretResolutions

func (s *RunStore) ReadSecretResolutions() ([]SecretResolution, error)

ReadSecretResolutions reads all secret resolutions.

func (*RunStore) ReadSpans

func (s *RunStore) ReadSpans() ([]Span, error)

ReadSpans reads all spans from the trace file.

func (*RunStore) Remove

func (s *RunStore) Remove() error

Remove deletes the run's storage directory and all its contents. Returns an error if the run ID is empty to prevent accidental deletion of the base directory.

func (*RunStore) RunID

func (s *RunStore) RunID() string

RunID returns the run identifier.

func (*RunStore) SaveDockerfile

func (s *RunStore) SaveDockerfile(dockerfile string) error

SaveDockerfile saves the Dockerfile used to build the container image.

func (*RunStore) SaveMetadata

func (s *RunStore) SaveMetadata(m Metadata) error

SaveMetadata writes the metadata to metadata.json in the run directory.

func (*RunStore) WriteExecEvent

func (s *RunStore) WriteExecEvent(event ExecEvent) error

WriteExecEvent writes an execution event to exec.jsonl.

func (*RunStore) WriteNetworkRequest

func (s *RunStore) WriteNetworkRequest(req NetworkRequest) error

WriteNetworkRequest appends a network request to the log.

func (*RunStore) WriteSecretResolution

func (s *RunStore) WriteSecretResolution(res SecretResolution) error

WriteSecretResolution records that a secret was resolved.

func (*RunStore) WriteSpan

func (s *RunStore) WriteSpan(span Span) error

WriteSpan appends a span to the trace file.

type SecretResolution

type SecretResolution struct {
	Timestamp time.Time `json:"ts"`
	Name      string    `json:"name"`    // env var name
	Backend   string    `json:"backend"` // e.g., "1password"
}

SecretResolution records a resolved secret (without the value).

type Span

type Span struct {
	TraceID    string                 `json:"trace_id"`
	SpanID     string                 `json:"span_id"`
	ParentID   string                 `json:"parent_id,omitempty"`
	Name       string                 `json:"name"`
	Kind       string                 `json:"kind,omitempty"` // client, server, internal
	StartTime  time.Time              `json:"start_time"`
	EndTime    time.Time              `json:"end_time"`
	Attributes map[string]interface{} `json:"attributes,omitempty"`
	Status     string                 `json:"status,omitempty"` // ok, error
	StatusMsg  string                 `json:"status_msg,omitempty"`
}

Span represents a trace span (OpenTelemetry-compatible).

Jump to

Keyboard shortcuts

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