agent

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsRunning

func IsRunning(workdir string) (bool, int)

IsRunning returns true if the daemon is alive (PID exists and process is reachable).

func LogPath

func LogPath(workdir string) string

LogPath returns the absolute path to the agent log file.

func PIDPath

func PIDPath(workdir string) string

PIDPath returns the absolute path to the PID file.

func ReadPID

func ReadPID(workdir string) int

ReadPID reads the daemon PID from .cloop/agent.pid; returns 0 if not found.

func RemovePID

func RemovePID(workdir string) error

RemovePID removes the PID file (called by worker on exit). Returns the underlying os.Remove error so callers can log a true failure (perms, busy filesystem) — a stale PID file blocks the next `agent start` because IsRunning() will see it and assume the prior worker is alive. ENOENT is expected when the file was already removed (e.g. by Stop) and is returned untouched so callers can filter with os.IsNotExist.

func StatePath

func StatePath(workdir string) string

StatePath returns the absolute path to the agent state file.

func Stop

func Stop(workdir string) error

Stop sends SIGTERM to the daemon process.

func WritePID

func WritePID(workdir string, pid int) error

WritePID writes the daemon PID to .cloop/agent.pid atomically. A partial write here would let `IsRunning` see "0" or a half-printed PID and treat the daemon as dead even when it is alive.

Types

type Agent

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

Agent drives a ReAct (Reason + Act) loop to accomplish a goal using tools.

func New

func New(cfg AgentConfig) *Agent

New creates a new Agent with the built-in tool set registered.

func (*Agent) Register

func (a *Agent) Register(t Tool)

Register adds a tool to the agent's registry.

func (*Agent) Run

func (a *Agent) Run(ctx context.Context, goal string, maxSteps int) (string, error)

Run drives the ReAct loop until the goal is achieved or maxSteps is reached. Returns the final result string (from the "finish" action) or an error.

func (*Agent) Steps

func (a *Agent) Steps() []StepLog

Steps returns the recorded step log.

type AgentConfig

type AgentConfig struct {
	Provider provider.Provider
	Model    string
	Timeout  time.Duration
	DryRun   bool
	WorkDir  string
}

Config holds configuration for creating a new Agent.

type State

type State struct {
	PID                 int       `json:"pid"`
	StartedAt           time.Time `json:"started_at"`
	LastRunAt           time.Time `json:"last_run_at,omitempty"`
	NextRunAt           time.Time `json:"next_run_at,omitempty"`
	RunCount            int       `json:"run_count"`
	TotalTasksCompleted int       `json:"total_tasks_completed"`
	TotalTasksFailed    int       `json:"total_tasks_failed"`
	Status              string    `json:"status"` // starting, idle, running, stopped
	Interval            string    `json:"interval"`
	Provider            string    `json:"provider,omitempty"`
	Model               string    `json:"model,omitempty"`
	LastError           string    `json:"last_error,omitempty"`
}

State tracks the daemon's runtime status, persisted to .cloop/agent.json.

func Load

func Load(workdir string) (*State, error)

Load reads the agent state from disk; returns nil if not found.

On parse failure (zero-byte file from a torn pre-atomicfile write, schema drift, manual edit gone wrong) the corrupt file is quarantined aside as agent.json.corrupt-<unix> and (nil, nil) is returned. Losing run counters is preferable to refusing to start the daemon — and the user has the bytes preserved next to the original location for forensics.

func (*State) Save

func (s *State) Save(workdir string) error

Save writes the agent state to disk atomically.

A torn write of agent.json (crash mid-write, ENOSPC) would corrupt the JSON; `Load` would then return an error and `cloop agent status` / the daemon itself would refuse to read its own counters. The write also runs under stateMu so two daemon goroutines saving in parallel can't interleave their MarshalIndent buffers.

type StepLog

type StepLog struct {
	Step        int
	Thought     string
	Action      string
	ActionInput map[string]string
	Observation string
}

StepLog records one completed ReAct step.

type Tool

type Tool interface {
	Name() string
	Description() string
	// Run executes the tool with the given arguments and returns its output.
	Run(args map[string]string) (string, error)
}

Tool is a capability the agent can invoke during its ReAct loop.

Jump to

Keyboard shortcuts

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