daemon

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 32 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package daemon is the one process per data directory that runs kairo (ADR 0008), and the client every other kairocode process uses to reach it.

The daemon holds daemon.lock for as long as it runs, so a second one does not start. It serves the HTTP API (internal/server) on a Unix socket and writes where it is to server.json. Everything that happens between live processes is then an event inside the daemon: an approval is a Signal there, and the turn that waits for it wakes at once.

Index

Constants

View Source
const (

	// Owner is the daemon's name in kairo's leases. Only one daemon runs per
	// data directory, so a daemon started after one was killed takes over
	// what it left at once, without waiting for its leases to run out.
	//
	// Safe only while nothing else opens the database under this name:
	// kairo expires every lease of the name at Open, and a second process
	// with it would take steps that still run (and run them twice). The
	// lock keeps it to one process per SQLite file; a shared database
	// (PostgreSQL, several hosts) needs a name per host.
	Owner = "kairocode-daemon"

	// DefaultIdle is how long a daemon with nothing to run and no client
	// stays up.
	DefaultIdle = 10 * time.Minute
	// DefaultDrain is how long a daemon that stops waits for the steps under
	// way to finish.
	DefaultDrain = 30 * time.Second
)

Variables

View Source
var ErrNoTurn = errors.New("the session has no turn")

ErrNoTurn: the session has no turn (or is not there).

View Source
var ErrRunning = errors.New("a daemon already runs for this data directory")

ErrRunning: another daemon runs for the data directory.

Functions

func FileVersion

func FileVersion(path string) (string, error)

FileVersion names the build in the executable at path (a link is followed): the hash of its content.

func InstalledAt

func InstalledAt(path string) func() string

InstalledAt is Options.Installed for the executable at path: its version, measured again only when the file changed (its size, time or identity), so that looking each time no step runs costs a stat.

func Serve

func Serve(ctx context.Context, o Options) error

Serve runs the daemon until ctx ends, it has been idle for Idle, or a client stops it. It returns ErrRunning if another daemon holds the data directory.

It stops between steps: no new step starts, and the steps under way finish with their outcomes recorded (kairo's Drain), for up to DrainTimeout. A step still running then is cut, as in a crash, and a real one waits in the next daemon for a person to say whether it took effect (invariant 4).

func Stop

func Stop(ctx context.Context, dir string) (bool, error)

Stop stops the daemon of dir, if one runs, and returns once it has let go of the data directory. A step running then is cut, as in a crash: a real one waits in the next daemon for a person (invariant 4). It reports whether a daemon was running.

Types

type Client

type Client struct {

	// Stale is what to tell the user when the daemon cannot be of the
	// client's build: the client is older than the build installed, or
	// (an older daemon) others use it.
	Stale string
	// contains filtered or unexported fields
}

Client talks to a data directory's daemon. When it cannot reach it (the daemon handed over to a new build, or stopped), it connects again, as Connect does (ADR 0019).

func Connect

func Connect(ctx context.Context, dir string, start Start) (*Client, error)

Connect reaches the daemon of data directory dir, starting it with start if none runs. Clients that find none at once start one: the first takes start.lock, and the others wait on the lock and find the daemon it started.

func (*Client) Answer

func (c *Client) Answer(ctx context.Context, key, option string) error

Answer answers a flow's question with one of its options. Only the first answer goes through: a later one returns api.ErrAnswered.

func (*Client) Approve

func (c *Client) Approve(ctx context.Context, key string, ok bool, reason string) error

Approve answers one of them. Only the first answer goes through: a later one returns api.ErrAnswered.

func (*Client) Cancel

func (c *Client) Cancel(ctx context.Context, session string) (int, error)

Undo starts a turn that puts the working files back as they were before the session's last turn in effect; Redo puts back what an undo took away. They return the turn's number. Cancel stops the session's last turn (ADR 0011). It returns the turn's number, or api.ErrNoTurnToCancel when it has ended.

func (*Client) Close

func (c *Client) Close()

func (*Client) CloseSession

func (c *Client) CloseSession(ctx context.Context, session string) error

CloseSession ends the session after the turns it has been sent.

func (*Client) Commands

func (c *Client) Commands(ctx context.Context, session string) ([]opencode.Command, error)

Commands are the commands the session can run.

func (*Client) Compact

func (c *Client) Compact(ctx context.Context, session string) (int, error)

Compact starts a turn that only compacts the session's conversation (ADR 0020), and returns its number.

func (*Client) CreateSession

func (c *Client) CreateSession(ctx context.Context, in workflows.SessionInput) (string, error)

CreateSession starts a session and returns its id. in.Dir must be absolute: the daemon runs elsewhere.

func (*Client) Efforts

func (c *Client) Efforts(ctx context.Context, id string) ([]string, error)

Efforts are provider id's efforts to choose from.

func (*Client) Events

func (c *Client) Events(ctx context.Context, session string) (<-chan api.Event, error)

Events follows the session's events (GET /sessions/{id}/events): first what waits for a person, then what happens. The channel closes when ctx ends or the stream does (the daemon stopped, or this client fell behind); call it again to go on.

func (*Client) FlowRun

func (c *Client) FlowRun(ctx context.Context, id string) (api.FlowRun, error)

FlowRun is a flow's run, with its chart and steps.

func (*Client) FlowRuns

func (c *Client) FlowRuns(ctx context.Context, dir, flow, only string, n int) ([]api.FlowRun, error)

FlowRuns are the flows' runs of dir and flow ("" any), the latest first, at most n (0: all), only "tries" or "runs" or both ("") (ADR 0029).

func (*Client) FlowStats

func (c *Client) FlowStats(ctx context.Context, dir string) ([]api.FlowStat, error)

FlowStats are the flows the sessions in dir ran, and the project's flows that none ran.

func (*Client) Flows

func (c *Client) Flows(ctx context.Context, session string) ([]api.FlowInfo, error)

Flows lists the flows of the session's project (ADR 0003).

func (*Client) Fork

func (c *Client) Fork(ctx context.Context, session string, n int, files bool) (string, int, error)

Fork starts a session from turn n of this one (n < 0: its last finished turn), with files put back as they were after it if asked (ADR 0021). It returns the new session and the turn it was forked from; ErrNoForkPoint when there is no such turn.

func (*Client) History

func (c *Client) History(ctx context.Context, session string) ([]msg.Message, error)

History is the session's conversation as of its last finished turn, then what the turns under way were asked: as a client shows it again.

func (*Client) ImageData

func (c *Client) ImageData(ctx context.Context, ref string) ([]byte, error)

ImageData is the bytes of the image of a message stored as ref.

func (*Client) InstallProvider

func (c *Client) InstallProvider(ctx context.Context, id, version string) (api.ProviderInfo, error)

InstallProvider installs agent id of ACP's registry (ADR 0015).

func (*Client) JobDone

func (c *Client) JobDone(ctx context.Context, id string) error

JobDone tells the daemon that a command in the background ended (its runner, ADR 0028).

func (*Client) LastFlow

func (c *Client) LastFlow(ctx context.Context, session string) (*flowchart.View, error)

LastFlow is the session's latest flow, with its calls' states.

func (*Client) LastTurn

func (c *Client) LastTurn(ctx context.Context, session string) (int, error)

LastTurn is the number of the session's latest turn.

func (*Client) Log

func (c *Client) Log(ctx context.Context, session string) (api.SessionLog, error)

Log is the session's record (ADR 0023).

func (*Client) Login

func (c *Client) Login(ctx context.Context, id, method string, done bool) error

Login signs in to provider id with its agent's method; done instead tells the daemon the user ran a terminal method.

func (*Client) LoginMethods

func (c *Client) LoginMethods(ctx context.Context, id string) ([]api.AuthMethod, error)

LoginMethods are the ways to sign in to provider id.

func (*Client) MCP

func (c *Client) MCP(ctx context.Context, session string) ([]api.MCPInfo, error)

MCP lists the session's MCP servers and custom tools (ADR 0022).

func (*Client) Models

func (c *Client) Models(ctx context.Context, id string) ([]string, error)

Models are provider id's models to choose from.

func (*Client) NewFlow

func (c *Client) NewFlow(ctx context.Context, session, request string) (int, error)

NewFlow has the model write a flow from request (/flow new) and returns the turn's number.

func (*Client) PID

func (c *Client) PID() int

PID is the daemon's process.

func (*Client) Pending

func (c *Client) Pending(ctx context.Context, session string) ([]api.Pending, error)

Pending lists the session's calls that wait for a person.

func (*Client) PluginConnected

func (c *Client) PluginConnected(ctx context.Context, name string) error

PluginConnected tells the daemon a plugin's credentials changed.

func (*Client) Plugins

func (c *Client) Plugins(ctx context.Context, session string) ([]api.PluginInfo, error)

Plugins lists the session's plugins and their state (ADR 0025).

func (*Client) PluginsIn

func (c *Client) PluginsIn(ctx context.Context, dir string) ([]api.PluginInfo, error)

PluginsIn lists dir's plugins and their state.

func (*Client) Providers

func (c *Client) Providers(ctx context.Context) ([]api.ProviderInfo, error)

Providers are the providers a session can switch to (ADR 0014).

func (*Client) Redo

func (c *Client) Redo(ctx context.Context, session string) (int, error)

func (*Client) RunCommand

func (c *Client) RunCommand(ctx context.Context, session, name, args string) (int, error)

RunCommand runs one with args and returns the turn's number.

func (*Client) RunFlow

func (c *Client) RunFlow(ctx context.Context, session, name, args string) (int, error)

RunFlow runs the flow name with args (/flow) and returns the turn's number.

func (*Client) Send

func (c *Client) Send(ctx context.Context, session, text string) (int, error)

Send gives the session a message and returns the turn that answers it.

func (*Client) SendImages

func (c *Client) SendImages(ctx context.Context, session, text string, images []api.Image) (int, error)

SendImages is Send with images (ADR 0026).

func (*Client) Sessions

func (c *Client) Sessions(ctx context.Context, dir string, limit int) ([]api.SessionInfo, error)

Sessions lists the sessions of dir ("" all), the latest used first, at most limit (0: all).

func (*Client) SetMode

func (c *Client) SetMode(ctx context.Context, session, mode string) (api.Settings, error)

SetMode sets the session's mode from its next turn on (ADR 0016).

func (*Client) Settings

func (c *Client) Settings(ctx context.Context, session string) (api.Settings, error)

Settings are the provider and model the session's next turn runs with.

func (*Client) Skills

func (c *Client) Skills(ctx context.Context, session string) (api.SkillsInfo, error)

Skills are the session's skills, and the SKILL.md files not taken.

func (*Client) Status

func (c *Client) Status(ctx context.Context) (Status, error)

Status is the daemon's build, process and number of clients.

func (*Client) Switch

func (c *Client) Switch(ctx context.Context, session, provider, model, effort string) (api.Settings, error)

Switch switches the session's provider or model ("": keep it) from its next turn on, and returns the settings it then has.

func (*Client) TryFlow

func (c *Client) TryFlow(ctx context.Context, key, args string) error

TryFlow tries the flow an approval would write first (ADR 0017).

func (*Client) Undo

func (c *Client) Undo(ctx context.Context, session string) (int, error)

func (*Client) Usage

func (c *Client) Usage(ctx context.Context, session string) (api.SessionUsage, error)

Usage is what the session's model calls recorded they used.

func (*Client) WaitTurn

func (c *Client) WaitTurn(ctx context.Context, session string, n int) (Turn, error)

WaitTurn waits for turn n of the session to finish.

type Info

type Info struct {
	Socket  string `json:"socket"`
	Token   string `json:"token"`
	PID     int    `json:"pid"`
	Version string `json:"version,omitempty"`
}

Info is server.json: how a client reaches the daemon.

func ReadInfo

func ReadInfo(dir string) (Info, error)

ReadInfo reads the data directory's server.json.

type Options

type Options struct {
	// Dir is the data directory: the database, the lock, server.json and the
	// socket live there.
	Dir string
	// Open opens the runtime with cfg (DB, Owner and Observe set by the
	// daemon); the caller adds the engine and the model.
	Open func(ctx context.Context, cfg runtime.Config) (*runtime.Runtime, error)
	// Idle is how long the daemon stays up with no step running and no
	// client connected (DefaultIdle when zero; negative: forever).
	Idle time.Duration
	// Ready, if set, is told "ready" once clients can connect.
	Ready io.Writer
	// WaitLock is how long to wait for a daemon that holds the data
	// directory to end (zero: not at all). A client starts a daemon only
	// when it found none answering: one that holds the lock then is one on
	// its way out (idle, or killed and not yet gone).
	WaitLock time.Duration
	// Version names the build.
	Version string
	// Installed is the build installed now: the daemon's executable as it
	// is on disk (InstalledAt). One other than Version retires the daemon
	// once no step runs, and Successor starts it for the clients connected
	// (ADR 0019). Nil, or "": the daemon does not retire.
	Installed func() string
	Successor func() Start
	// DrainTimeout is how long a daemon that stops lets the steps under way
	// finish (DefaultDrain when zero). A step still running then is cut, as
	// in a crash: a real one waits in the next daemon for review.
	DrainTimeout time.Duration
	// Cut, when closed, ends the drain at once: the steps still running are
	// cut (a second Ctrl-C).
	Cut <-chan struct{}
}

type Start

type Start struct {
	Argv []string
	Env  []string
	// Version is the client's build. A daemon of another build is stopped
	// and started again from Argv, if no other client uses it.
	Version string
}

Start says how a client starts the daemon when none runs: the command (argv) and its environment. The command must run Serve with Ready set to its standard output.

type Status

type Status struct {
	Version string `json:"version"`
	PID     int    `json:"pid"`
	// Steps is the number of steps running; Clients the number of
	// connections, the asking one included.
	Steps   int `json:"steps"`
	Clients int `json:"clients"`
}

Status is what GET /daemon/status says.

type Turn

type Turn struct {
	Reply   string    `json:"reply"`
	Steps   int       `json:"steps"`
	Stopped string    `json:"stopped"`
	Usage   msg.Usage `json:"usage"`
}

Turn is a finished turn.

type Update

type Update struct {
	Installed string `json:"installed,omitempty"`
	Replaced  bool   `json:"replaced,omitempty"`
	Later     bool   `json:"later,omitempty"`
}

Update is what POST /daemon/update says (ADR 0019): the build installed now; Replaced, the daemon handed over to it (connect again); Later, it will once no step runs.

Jump to

Keyboard shortcuts

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