lock

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package lock provides agent identity locking to prevent multiple agents from claiming the same worker identity.

Lock files are stored at <worker>/.runtime/agent.lock and contain: - PID of the owning process - Timestamp when lock was acquired - Session ID (tmux session name)

Stale locks (where the PID is dead) are automatically cleaned up.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrLocked      = errors.New("worker is locked by another agent")
	ErrNotLocked   = errors.New("worker is not locked")
	ErrInvalidLock = errors.New("invalid lock file")
)

Common errors

Functions

func CleanStaleLocks

func CleanStaleLocks(root string) (int, error)

CleanStaleLocks removes all stale locks in a directory tree. Returns the number of stale locks cleaned. A lock is only truly stale if BOTH the PID is dead AND the tmux session doesn't exist. This prevents killing active workers whose spawning process has exited (which is normal - the agent runs as a child in tmux).

func DetectCollisions

func DetectCollisions(root string, activeSessions []string) []string

DetectCollisions finds workers with multiple agents claiming the same identity. This detects the case where multiple processes think they own the same worker by comparing tmux sessions with lock files. Returns a list of collision descriptions.

func FindAllLocks

func FindAllLocks(root string) (map[string]*LockInfo, error)

FindAllLocks scans a directory tree for agent.lock files. Returns a map of worker directory -> LockInfo.

Types

type Lock

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

Lock represents an agent identity lock for a worker directory.

func New

func New(workerDir string) *Lock

New creates a Lock for the given worker directory.

func (*Lock) Acquire

func (l *Lock) Acquire(sessionID string) error

Acquire attempts to acquire the lock for this worker. Returns ErrLocked if another live process holds the lock. Automatically cleans up stale locks.

func (*Lock) Check

func (l *Lock) Check() error

Check checks if the worker is locked by another agent. Returns nil if unlocked or locked by us. Returns ErrLocked if locked by another live process. Automatically cleans up stale locks.

func (*Lock) ForceRelease

func (l *Lock) ForceRelease() error

ForceRelease removes the lock regardless of who holds it. Use with caution - only for doctor --fix scenarios.

func (*Lock) Read

func (l *Lock) Read() (*LockInfo, error)

Read reads the current lock info without modifying it.

func (*Lock) Release

func (l *Lock) Release() error

Release releases the lock if we hold it.

func (*Lock) Status

func (l *Lock) Status() string

Status returns a human-readable status of the lock.

type LockInfo

type LockInfo struct {
	PID        int       `json:"pid"`
	AcquiredAt time.Time `json:"acquired_at"`
	SessionID  string    `json:"session_id,omitempty"`
	Hostname   string    `json:"hostname,omitempty"`
}

LockInfo contains information about who holds a lock.

func (*LockInfo) IsStale

func (l *LockInfo) IsStale() bool

IsStale checks if the lock is stale (owning process is dead).

Jump to

Keyboard shortcuts

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