engine

package
v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package engine provides the core business logic for monodev operations.

The engine package acts as the orchestration layer between CLI commands and lower-level operations. It coordinates workspace discovery, state management, store operations, and overlay application/removal.

Key components:

  • Engine: Main orchestrator that coordinates all operations
  • Apply/Unapply: Manages overlay application and removal
  • Track/Commit: Handles tracking and persisting changes
  • State management: Workspace and store state operations

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConflict indicates a conflict was detected during apply.
	ErrConflict = errors.New("conflict detected")

	// ErrValidation indicates a validation failure.
	ErrValidation = errors.New("validation failed")

	// ErrNotFound indicates a resource was not found.
	ErrNotFound = errors.New("not found")

	// ErrDrift indicates drift was detected in copy mode.
	ErrDrift = errors.New("drift detected")

	// ErrStateMissing indicates workspace state is missing.
	ErrStateMissing = errors.New("state missing")

	// ErrNotInRepo indicates the current directory is not in a git repository.
	ErrNotInRepo = errors.New("not in a git repository")

	// ErrNoActiveStore indicates no active store is set.
	ErrNoActiveStore = errors.New("no active store set")
)

Functions

This section is empty.

Types

type AppliedStoreInfo added in v0.2.0

type AppliedStoreInfo struct {
	// StoreID is the store identifier
	StoreID string

	// Mode is the overlay mode for this store
	Mode string

	// AppliedCount is the number of paths applied from this store
	AppliedCount int
}

AppliedStoreInfo contains information about an applied store.

type ApplyRequest

type ApplyRequest struct {
	// CWD is the current working directory (workspace path)
	CWD string

	// Mode is the overlay mode ("symlink" or "copy")
	Mode string

	// Force allows overwriting conflicts
	Force bool

	// DryRun performs planning only without making changes
	DryRun bool

	// StoreID is an optional store ID to apply instead of the active store
	StoreID string
}

ApplyRequest represents a request to apply store overlays.

type ApplyResult

type ApplyResult struct {
	// Plan is the generated plan
	Plan *planner.ApplyPlan

	// Applied is the list of operations that were executed (empty if DryRun)
	Applied []planner.Operation

	// WorkspaceID is the computed workspace ID
	WorkspaceID string

	// RepoFingerprint is the repository fingerprint
	RepoFingerprint string

	// WorkspacePath is the relative path from repo root
	WorkspacePath string
}

ApplyResult represents the result of applying store overlays.

type CommitRequest added in v0.2.0

type CommitRequest struct {
	// CWD is the current working directory
	CWD string

	// Paths is the list of paths to commit (relative to CWD)
	// If empty and All is false, commits nothing
	Paths []string

	// All commits all tracked paths
	All bool

	// DryRun shows what would be committed without actually committing
	DryRun bool
}

CommitRequest represents a request to commit workspace files to the store.

type CommitResult added in v0.2.0

type CommitResult struct {
	// Committed is the list of paths that were committed
	Committed []string

	// Skipped is the list of paths that were skipped (e.g., symlinks in symlink mode)
	Skipped []string

	// Missing is the list of paths that could not be committed because they don't exist in workspace
	Missing []string

	// Removed is the list of paths that were removed from the store (no longer tracked)
	Removed []string
}

CommitResult represents the result of a commit operation.

type CreateStoreRequest

type CreateStoreRequest struct {
	// CWD is the current working directory (needed to set as active store)
	CWD string

	// StoreID is the ID of the new store
	StoreID string

	// Name is the human-readable name
	Name string

	// Scope is the store scope ("global", "profile", "component")
	Scope string

	// Description is an optional description
	Description string

	// Source indicates how the store was created (human, agent, other)
	Source string

	// Type categorizes the store (issue, plan, feature, task, other)
	Type string

	// Owner identifies who owns the store
	Owner string

	// TaskID links the store to an external task
	TaskID string

	// ParentTaskID links the store to a parent task
	ParentTaskID string

	// Priority indicates the store's priority (low, medium, high, none)
	Priority string

	// Status indicates the store's workflow status
	Status string
}

CreateStoreRequest represents a request to create a new store.

type DeleteStoreRequest added in v0.2.1

type DeleteStoreRequest struct {
	StoreID string
	Force   bool   // Skip safety checks
	DryRun  bool   // Preview only
	Scope   string // Optional scope to disambiguate (empty = auto-resolve)
}

DeleteStoreRequest represents a request to delete a store.

type DeleteStoreResult added in v0.2.1

type DeleteStoreResult struct {
	StoreID            string
	AffectedWorkspaces []WorkspaceUsage
	DryRun             bool
	Deleted            bool
}

DeleteStoreResult represents the result of deleting a store.

type DeleteWorkspaceRequest added in v0.2.1

type DeleteWorkspaceRequest struct {
	WorkspaceID string
	Force       bool
	DryRun      bool
}

DeleteWorkspaceRequest represents a request to delete a workspace.

type DeleteWorkspaceResult added in v0.2.1

type DeleteWorkspaceResult struct {
	WorkspaceID   string
	WorkspacePath string
	Deleted       bool
	DryRun        bool
	PathsRemoved  int
}

DeleteWorkspaceResult represents the result of deleting a workspace.

type DescribeWorkspaceResult added in v0.2.1

type DescribeWorkspaceResult struct {
	WorkspaceID   string
	WorkspacePath string
	Repo          string
	Applied       bool
	Mode          string
	ActiveStore   string
	Stack         []string
	AppliedStores []state.AppliedStore
	Paths         map[string]state.PathOwnership
}

DescribeWorkspaceResult represents the result of describing a workspace.

type DiffFileInfo added in v0.2.2

type DiffFileInfo struct {
	// Path is the relative path from workspace root
	Path string

	// Status is the diff status: "modified", "added", "removed", "unchanged"
	Status string

	// WorkspaceHash is the hash of the file in the workspace (empty if doesn't exist)
	WorkspaceHash string

	// StoreHash is the hash of the file in store overlay (empty if doesn't exist)
	StoreHash string

	// UnifiedDiff contains the unified diff content (if ShowContent is true)
	UnifiedDiff string

	// Additions is the number of added lines in the diff
	Additions int

	// Deletions is the number of removed lines in the diff
	Deletions int

	// IsDir indicates if the path is a directory
	IsDir bool
}

DiffFileInfo contains information about a single diffed file.

type DiffRequest added in v0.2.2

type DiffRequest struct {
	// CWD is the current working directory
	CWD string

	// StoreID is an optional store ID to diff against (default: active store)
	StoreID string

	// ShowContent indicates whether to show actual diff content (unified diff)
	ShowContent bool

	// NameOnly shows only filenames without status indicators
	NameOnly bool

	// NameStatus shows filenames with status indicators (M, A, D)
	NameStatus bool
}

DiffRequest represents a request to diff workspace files against store overlay.

type DiffResult added in v0.2.2

type DiffResult struct {
	// WorkspaceID is the workspace identifier
	WorkspaceID string

	// StoreID is the store that was diffed against
	StoreID string

	// Files contains all diffed files with their status
	Files []DiffFileInfo
}

DiffResult represents the result of a diff operation.

type Engine

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

Engine orchestrates all monodev operations. It is the main API surface called by the CLI.

func New

func New(
	gitRepo gitx.GitRepo,
	storeRepo stores.StoreRepo,
	stateStore state.StateStore,
	fs fsops.FS,
	hasher hash.Hasher,
	clk clock.Clock,
	paths config.Paths,
) *Engine

New creates a new Engine with the given dependencies.

func NewScoped added in v0.2.5

func NewScoped(
	gitRepo gitx.GitRepo,
	scopedPaths *config.ScopedPaths,
	fs fsops.FS,
	hasher hash.Hasher,
	clk clock.Clock,
) *Engine

NewScoped creates a new Engine with dual-scope StoreRepo instances. Global stores live at ~/.monodev/stores/, component stores at repo_root/.monodev/stores/.

func (*Engine) Apply

func (e *Engine) Apply(ctx context.Context, req *ApplyRequest) (*ApplyResult, error)

Algorithm steps: 1. Resolve stores (stack + active store) 2. Discover repo and compute workspace ID 3. Load workspace state (if exists) 4. Preflight checks (generate plan, check for conflicts) 5. Apply overlays (if not DryRun) 6. Persist workspace state 7. Return result

func (*Engine) Commit added in v0.2.0

func (e *Engine) Commit(ctx context.Context, req *CommitRequest) (*CommitResult, error)

Commit copies workspace files to the active store and records them in workspace state.

Behavior: - Copies files from workspace → store overlay - Updates workspace state to mark paths as managed (adds to workspace.Paths) - Does NOT set applied=true (that's only done by Apply) - Does NOT create overlays (symlinks/copies) - that's Apply's job

This allows tracking which files are managed by monodev even before overlays are created. The workspace state is the "intent" layer, while Apply creates the actual overlays.

func (*Engine) CreateStore

func (e *Engine) CreateStore(ctx context.Context, req *CreateStoreRequest) error

CreateStore creates a new store and sets it as the active store for the current repository. If there's existing workspace state for a different store, it will be cleared.

func (*Engine) DeleteStore added in v0.2.1

func (e *Engine) DeleteStore(ctx context.Context, req *DeleteStoreRequest) (*DeleteStoreResult, error)

DeleteStore deletes a store after checking for usage by workspaces. Algorithm steps: 1. Validate store exists (resolve scope) 2. Find all workspaces using the store 3. Return early if dry-run 4. If store is in use and not forced, return error with affected workspaces 5. Clean workspace references 6. Delete store 7. Return result

func (*Engine) DeleteWorkspace added in v0.2.1

func (e *Engine) DeleteWorkspace(ctx context.Context, req *DeleteWorkspaceRequest) (*DeleteWorkspaceResult, error)

DeleteWorkspace deletes a workspace state file. Algorithm steps: 1. Load workspace state (error if not found) 2. If DryRun: return preview of what would be deleted 3. If Applied==true && len(Paths)>0 && !Force: error with message to unapply first 4. Otherwise: call stateStore.DeleteWorkspace(workspaceID) 5. Return result with deletion status

func (*Engine) DescribeStore

func (e *Engine) DescribeStore(ctx context.Context, storeID string) ([]ScopedStoreDetails, error)

DescribeStore returns detailed information about a store. If the store exists in both scopes, returns details for both.

func (*Engine) DescribeWorkspace added in v0.2.1

func (e *Engine) DescribeWorkspace(ctx context.Context, workspaceID string) (*DescribeWorkspaceResult, error)

DescribeWorkspace loads and returns detailed information about a specific workspace. Algorithm steps: 1. Load workspace state by ID 2. Return detailed information 3. Error if workspace not found

func (*Engine) Diff added in v0.2.2

func (e *Engine) Diff(ctx context.Context, req *DiffRequest) (*DiffResult, error)

Diff compares workspace files against store overlay files.

func (*Engine) DiscoverWorkspace added in v0.2.0

func (e *Engine) DiscoverWorkspace(cwd string) (root, fingerprint, workspacePath string, err error)

discoverWorkspace returns repo root, fingerprint, and workspace path

func (*Engine) GetActiveStoreID added in v0.2.5

func (e *Engine) GetActiveStoreID(ctx context.Context, cwd string) (storeID, scope string, err error)

GetActiveStoreID returns the active store ID and scope for the given working directory. Returns ErrNoActiveStore if no store is currently active.

func (*Engine) ListStores

func (e *Engine) ListStores(ctx context.Context) ([]stores.ScopedStore, error)

ListStores returns all available stores from both scopes. Global stores are listed first, then component stores.

func (*Engine) ListWorkspaces added in v0.2.1

func (e *Engine) ListWorkspaces(ctx context.Context) (*ListWorkspacesResult, error)

ListWorkspaces enumerates all workspace state files and returns summary information. Scans both global and component workspace directories, deduplicating by workspace ID.

func (*Engine) LoadOrCreateWorkspaceState added in v0.2.0

func (e *Engine) LoadOrCreateWorkspaceState(root, repoFingerprint, workspacePath, mode string) (*state.WorkspaceState, string, error)

func (*Engine) Prune

func (e *Engine) Prune(ctx context.Context, req *PruneRequest) (*PruneResult, error)

Prune deletes overlay store content for paths that are no longer tracked.

func (*Engine) StackAdd

func (e *Engine) StackAdd(ctx context.Context, req *StackAddRequest) error

StackAdd adds a store to the stack.

func (*Engine) StackApply added in v0.2.0

func (e *Engine) StackApply(ctx context.Context, req *StackApplyRequest) (*StackApplyResult, error)

StackApply applies all stores in the configured stack to the workspace. This does not include the active store - only stores added via 'stack add'.

func (*Engine) StackClear

func (e *Engine) StackClear(ctx context.Context, req *StackClearRequest) error

StackClear removes all stores from the stack.

func (*Engine) StackList

func (e *Engine) StackList(ctx context.Context, req *StackListRequest) (*StackListResult, error)

StackList returns the current store stack for the workspace.

func (*Engine) StackPop

func (e *Engine) StackPop(ctx context.Context, req *StackPopRequest) (*StackPopResult, error)

StackPop removes a store from the stack. If StoreID is empty, removes the last store (LIFO). If StoreID is specified, removes that specific store.

func (*Engine) StackUnapply added in v0.2.0

func (e *Engine) StackUnapply(ctx context.Context, req *StackUnapplyRequest) (*StackUnapplyResult, error)

StackUnapply removes only paths applied by the stack stores. Paths applied by the active store are not affected, unless they overlap

func (*Engine) Status

func (e *Engine) Status(ctx context.Context, req *StatusRequest) (*StatusResult, error)

Status returns the current status of the workspace.

func (*Engine) Track

func (e *Engine) Track(ctx context.Context, req *TrackRequest) (*TrackResult, error)

Track adds paths to the active store's track file.

func (*Engine) Unapply

func (e *Engine) Unapply(ctx context.Context, req *UnapplyRequest) (*UnapplyResult, error)

Unapply removes paths owned by the active store from the workspace.

Only removes paths that were applied via 'monodev apply' (the active store). Paths applied by the stack (via 'stack apply') are not affected. Use 'stack unapply' to remove stack-applied paths.

Algorithm: 1. Discover repo and load workspace state (must exist) 2. Collect paths owned by the active store 3. Remove paths in deepest-first order 4. Update workspace state

func (*Engine) Untrack

func (e *Engine) Untrack(ctx context.Context, req *UntrackRequest) (*UntrackResult, error)

Untrack removes paths from the active store's track file.

func (*Engine) UpdateStore added in v0.2.5

func (e *Engine) UpdateStore(ctx context.Context, req *UpdateStoreRequest) error

UpdateStore updates metadata fields on an existing store.

func (*Engine) UseStore

func (e *Engine) UseStore(ctx context.Context, req *UseStoreRequest) error

UseStore selects a store as the active store for the current repository. If there's existing workspace state for a different store, it will be cleared to avoid inconsistent state where applied=true but for the wrong store.

type ListWorkspacesResult added in v0.2.1

type ListWorkspacesResult struct {
	Workspaces []WorkspaceInfo
}

ListWorkspacesResult represents the result of listing workspaces.

type PathInfo

type PathInfo struct {
	// Store is the store that owns this path
	Store string

	// Type is the path type ("symlink" or "copy")
	Type string
}

PathInfo contains information about an applied path.

type PruneRequest

type PruneRequest struct {
	// CWD is the current working directory
	CWD string

	// DryRun indicates whether to only show what would be deleted
	DryRun bool

	// Force indicates whether to skip confirmation prompt
	Force bool
}

PruneRequest represents a request to prune untracked files from a store.

type PruneResult

type PruneResult struct {
	// StoreID is the ID of the pruned store
	StoreID string

	// DeletedPaths is the list of paths that were (or would be) deleted
	DeletedPaths []string

	// DryRun indicates whether this was a dry run
	DryRun bool
}

PruneResult contains the result of a prune operation.

type ScopedStoreDetails added in v0.2.5

type ScopedStoreDetails struct {
	// Scope is where the store is located ("global" or "component")
	Scope string

	// Meta is the store metadata
	Meta *stores.StoreMeta

	// TrackedPaths is the list of tracked paths
	TrackedPaths []stores.TrackedPath
}

ScopedStoreDetails contains detailed information about a store in a specific scope.

type StackAddRequest

type StackAddRequest struct {
	// CWD is the current working directory
	CWD string

	// StoreID is the store to add to the stack
	StoreID string
}

StackAddRequest represents a request to add a store to the stack.

type StackApplyRequest added in v0.2.0

type StackApplyRequest struct {
	// CWD is the current working directory (workspace path)
	CWD string

	// Mode is the overlay mode ("symlink" or "copy")
	Mode string

	// Force allows overwriting conflicts
	Force bool

	// DryRun performs planning only without making changes
	DryRun bool
}

StackApplyRequest represents a request to apply the configured stack.

type StackApplyResult added in v0.2.0

type StackApplyResult struct {
	// Plan is the generated plan
	Plan *planner.ApplyPlan

	// Applied is the list of operations that were executed (empty if DryRun)
	Applied []planner.Operation

	// WorkspaceID is the computed workspace ID
	WorkspaceID string

	// RepoFingerprint is the repository fingerprint
	RepoFingerprint string

	// WorkspacePath is the relative path from repo root
	WorkspacePath string
}

StackApplyResult represents the result of applying the stack.

type StackClearRequest

type StackClearRequest struct {
	// CWD is the current working directory
	CWD string
}

StackClearRequest represents a request to clear the stack.

type StackListRequest

type StackListRequest struct {
	// CWD is the current working directory
	CWD string
}

StackListRequest represents a request to list the store stack.

type StackListResult

type StackListResult struct {
	// Stack is the ordered list of stores
	Stack []string

	// ActiveStore is the currently active store
	ActiveStore string
}

StackListResult represents the result of listing the store stack.

type StackPopRequest

type StackPopRequest struct {
	// CWD is the current working directory
	CWD string

	// StoreID is the store to remove (if empty, removes last store - LIFO)
	StoreID string
}

StackPopRequest represents a request to remove a store from the stack.

type StackPopResult

type StackPopResult struct {
	// Removed is the store that was removed
	Removed string
}

StackPopResult represents the result of removing a store from the stack.

type StackUnapplyRequest added in v0.2.0

type StackUnapplyRequest struct {
	// CWD is the current working directory (workspace path)
	CWD string

	// Force allows removing paths even if validation fails
	Force bool

	// DryRun shows what would be removed without actually removing
	DryRun bool
}

StackUnapplyRequest represents a request to unapply the stack portion only.

type StackUnapplyResult added in v0.2.0

type StackUnapplyResult struct {
	// Removed is the list of paths that were removed
	Removed []string

	// WorkspaceID is the workspace ID
	WorkspaceID string
}

StackUnapplyResult represents the result of unapplying the stack.

type StatusRequest

type StatusRequest struct {
	// CWD is the current working directory
	CWD string
}

StatusRequest represents a request for workspace status.

type StatusResult

type StatusResult struct {

	// WorkspaceID is the workspace ID
	WorkspaceID string

	// RepoFingerprint is the repository fingerprint
	RepoFingerprint string

	// WorkspacePath is the relative path from repo root
	WorkspacePath string

	// AbsolutePath is the absolute path to the repository root
	AbsolutePath string

	// GitURL is the git remote origin URL (empty if not a git repo)
	GitURL string

	// Applied indicates if overlays are currently applied
	Applied bool

	// Mode is the current overlay mode
	Mode string

	// Stack is the store stack
	Stack []string

	// ActiveStore is the active store
	ActiveStore string

	// Paths is the map of applied paths
	Paths map[string]PathInfo

	// TrackedPaths is the list of paths tracked in the active store
	TrackedPaths []string

	// AppliedStores is the list of stores that have been applied
	AppliedStores []string

	// All paths in the workspace
	AllPaths []string

	// AppliedStoreDetails contains per-store applied path counts
	AppliedStoreDetails []AppliedStoreInfo

	// TrackedPathDetails contains detailed info for tracked paths in active store
	TrackedPathDetails []TrackedPathInfo

	// ActiveStoreStatus is the application status of the active store
	ActiveStoreStatus string // "Applied", "Not Applied", or "Partial"
}

StatusResult represents the current workspace status.

type StoreDetails

type StoreDetails struct {
	// Meta is the store metadata
	Meta *stores.StoreMeta

	// TrackedPaths is the list of tracked paths
	TrackedPaths []stores.TrackedPath
}

StoreDetails contains detailed information about a store.

type TrackRequest

type TrackRequest struct {
	// CWD is the current working directory
	CWD string

	// Paths is the list of paths to track (relative to CWD, absolute, or containing "..")
	Paths []string

	// Role categorizes the tracked paths (script, docs, style, config, other)
	Role string

	// Description provides additional context about the tracked paths
	Description string

	// Origin indicates how the paths were tracked (user, agent, other)
	Origin string
}

TrackRequest represents a request to track paths.

type TrackResult added in v0.2.5

type TrackResult struct {
	// ResolvedPaths maps each user-provided path to its workspace-relative resolved path.
	ResolvedPaths map[string]string

	// MissingPaths contains user-provided paths that were not found in the workspace.
	MissingPaths []string
}

TrackResult represents the result of a track operation.

type TrackedPathInfo added in v0.2.0

type TrackedPathInfo struct {
	// Path is the relative path
	Path string

	// IsApplied indicates if the path exists in workspace.Paths
	IsApplied bool

	// IsSaved indicates if the path exists in the store overlay
	IsSaved bool

	// IsModified indicates if the workspace version differs from the store overlay
	IsModified bool
}

TrackedPathInfo contains detailed information about a tracked path.

type UnUseStoreRequest added in v0.2.0

type UnUseStoreRequest struct {
	// CWD is the current working directory
	CWD string
}

type UnapplyRequest

type UnapplyRequest struct {
	// CWD is the current working directory (workspace path)
	CWD string

	// Force allows removing paths even if validation fails
	Force bool

	// DryRun shows what would be removed without actually removing
	DryRun bool
}

UnapplyRequest represents a request to unapply overlays.

type UnapplyResult

type UnapplyResult struct {
	// Removed is the list of paths that were removed
	Removed []string

	// WorkspaceID is the workspace ID
	WorkspaceID string
	// contains filtered or unexported fields
}

UnapplyResult represents the result of unapplying overlays.

type UntrackRequest

type UntrackRequest struct {
	// CWD is the current working directory
	CWD string

	// Paths is the list of paths to untrack (relative to CWD, absolute, or containing "..")
	Paths []string
}

UntrackRequest represents a request to untrack paths.

type UntrackResult added in v0.2.5

type UntrackResult struct {
	// RemovedPaths contains user-provided paths that were found and removed from tracking.
	RemovedPaths []string

	// NotFoundPaths contains user-provided paths that were not found in the track file.
	NotFoundPaths []string
}

UntrackResult represents the result of an untrack operation.

type UpdateStoreRequest added in v0.2.5

type UpdateStoreRequest struct {
	// CWD is the current working directory
	CWD string

	// StoreID is the store to update
	StoreID string

	// Scope optionally specifies which scope to use (empty = auto-resolve)
	Scope string

	// Optional fields — nil means "do not change"
	Description  *string
	Source       *string
	Type         *string
	Owner        *string
	TaskID       *string
	ParentTaskID *string
	Priority     *string
	Status       *string
}

UpdateStoreRequest represents a request to update store metadata. Nil pointer fields mean "do not change".

type UseStoreRequest

type UseStoreRequest struct {
	// CWD is the current working directory
	CWD string

	// StoreID is the store to select
	StoreID string

	// Scope optionally specifies which scope to use (empty = auto-resolve)
	Scope string
}

UseStoreRequest represents a request to select a store as active.

type WorkspaceInfo added in v0.2.1

type WorkspaceInfo struct {
	WorkspaceID      string
	WorkspacePath    string
	AbsolutePath     string
	Repo             string
	Applied          bool
	Mode             string
	ActiveStore      string
	StackCount       int
	AppliedPathCount int
}

WorkspaceInfo contains summary information about a workspace.

type WorkspaceUsage added in v0.2.1

type WorkspaceUsage struct {
	WorkspaceID      string
	WorkspacePath    string
	IsActive         bool
	InStack          bool
	AppliedPathCount int
}

WorkspaceUsage describes how a workspace uses a store.

Jump to

Keyboard shortcuts

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