agents

package
v0.58.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: Apache-2.0 Imports: 20 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 agents spawns and controls Claude Code sessions Caprock owns: a real `claude` process in a PTY, its output buffered for the terminal view and its exit recorded. Externally started sessions are never touched here — we only spawn, type into, pause and kill processes we started (.ai/05-orchestration.md).

Index

Constants

View Source
const (
	AgentClaude = "claude"
	AgentGemini = "gemini"
)

AgentClaude and AgentGemini name the coding agents Caprock can launch.

View Source
const ShutdownGrace = 5 * time.Second

ShutdownGrace is how long a session gets to finish on its own before it is killed. Claude Code flushes its transcript and releases its session id on the way out; none of that survives a SIGKILL.

Variables

View Source
var ErrNotOwned = errors.New("caprock did not start this session, so only its own terminal can control it")

ErrNotOwned is returned for control operations on a session Caprock did not spawn.

Functions

func RevokeTrustGrants added in v0.18.0

func RevokeTrustGrants() (int, error)

RevokeTrustGrants removes hasTrustDialogAccepted from every folder Caprock granted it to, and clears the ledger. Grants Caprock did not create are left alone — the user may have accepted those folders themselves, and undoing a human's decision is not uninstall's job. Returns how many were revoked.

It exists because the grant was previously permanent and one-way: nothing, including `caprock hooks uninstall`, ever removed it, so a machine accumulated a trusted-project entry per worktree forever — including for worktrees long since deleted.

func TrustGrantCount added in v0.18.0

func TrustGrantCount() int

TrustGrantCount reports how many folders Caprock has granted trust to, for the uninstall command's summary line.

Types

type Agent

type Agent struct {
	SessionID string
	Cwd       string
	Worktree  string
	Command   string
	StartedAt time.Time
	// contains filtered or unexported fields
}

Agent is a live owned session.

func (*Agent) Exited

func (a *Agent) Exited() (int, bool)

Exited reports the exit state.

func (*Agent) Paused

func (a *Agent) Paused() bool

Paused reports the pause state.

func (*Agent) Resize

func (a *Agent) Resize(cols, rows int) error

Resize the terminal.

func (*Agent) Signal

func (a *Agent) Signal(sig ptyman.Signal) error

Signal the process.

func (*Agent) Snapshot

func (a *Agent) Snapshot() []byte

Snapshot returns the current terminal scrollback (for a fresh terminal connect).

func (*Agent) Subscribe

func (a *Agent) Subscribe() (<-chan []byte, func())

Subscribe returns a channel of new output chunks and a cancel func. The channel is closed when the process exits.

func (*Agent) Write

func (a *Agent) Write(b []byte) error

Write types into the session.

type Manager

type Manager struct {
	OnExit   func(sessionID string, code int)
	OnOutput func(sessionID string) // called (throttled by caller) when new bytes arrive
	// NewSessionID generates a session id; overridable in tests.
	NewSessionID func() string
	// Now is the clock chat directory names are stamped from; overridable in
	// tests so a name can be asserted rather than merely observed to exist.
	Now func() time.Time
	// contains filtered or unexported fields
}

Manager owns the set of running agents.

func NewManager

func NewManager(st *store.Store, dataDir, claudePath string, log *slog.Logger) *Manager

NewManager builds a manager. claudePath "" resolves "claude" on PATH.

func (*Manager) ClaudeAvailable

func (m *Manager) ClaudeAvailable() bool

ClaudeAvailable reports whether the resolved claude binary can be launched.

func (*Manager) GeminiAvailable added in v0.44.0

func (m *Manager) GeminiAvailable() bool

GeminiAvailable reports whether the gemini binary can be launched, so the dialog offers an agent the machine actually has rather than a choice that fails on click.

func (*Manager) Get

func (m *Manager) Get(sessionID string) (*Agent, bool)

Get returns a running agent.

func (*Manager) Input

func (m *Manager) Input(sessionID string, data []byte) error

Input writes typed bytes to an owned session.

func (*Manager) List

func (m *Manager) List() []*Agent

List returns running agents.

func (*Manager) OwnedRunning added in v0.39.0

func (m *Manager) OwnedRunning() []string

OwnedRunning lists the sessions Caprock started that are still running.

The name says "owned" rather than "all" because that distinction is the whole safety story: `m.agents` holds only sessions this manager spawned, and a caller must not have to remember that. See PauseOwned.

func (*Manager) PauseOwned added in v0.39.0

func (m *Manager) PauseOwned(sessionID string) (bool, error)

PauseOwned pauses a session Caprock started, reporting whether it did.

An id this manager does not own is refused here, quietly and without error: [Rule 7] is enforced at the thing that holds the process handles rather than by whoever happens to be calling. A session started in someone's own terminal is watched and never signalled, however much it is costing — and the daily spend cap, the one caller today, therefore cannot violate that rule even by mistake.

[Rule 7]: ../../CLAUDE.md

func (*Manager) Resize

func (m *Manager) Resize(sessionID string, cols, rows int) error

Resize an owned session's terminal.

func (*Manager) Shutdown

func (m *Manager) Shutdown()

Shutdown ends every owned session, giving each a chance to stop cleanly.

It used to SIGKILL them outright, so upgrading Caprock — or any restart of the daemon — silently destroyed whatever the user had running under it, mid-turn, with no warning and nothing written out. A tool that watches your work must not be the thing that eats it.

Every session is signalled first and waited on together, so the grace period is spent once rather than once per session. Anything still alive at the end is killed: shutdown has to terminate, and a process that ignores SIGTERM has had its chance.

func (*Manager) Signal

func (m *Manager) Signal(sessionID string, sig ptyman.Signal) error

Signal pauses/resumes/kills an owned session.

func (*Manager) Spawn

func (m *Manager) Spawn(ctx context.Context, req SpawnRequest) (*Agent, error)

Spawn launches a new session and registers it. The PTY session id is forced with `claude --session-id <uuid>` so hooks and transcript arrive under the id Caprock already knows.

type SpawnRequest

type SpawnRequest struct {
	Cwd            string `json:"cwd"`
	Chat           bool   `json:"chat,omitempty"`            // a quick chat: Caprock picks the directory, no repository needed
	Create         bool   `json:"create,omitempty"`          // make Cwd if it does not exist (one level, under an existing parent)
	Worktree       string `json:"worktree,omitempty"`        // git worktree name to create under the repo
	Model          string `json:"model,omitempty"`           // --model
	PermissionMode string `json:"permission_mode,omitempty"` // --permission-mode
	Command        string `json:"command,omitempty"`         // default "claude"
	// Agent picks which coding agent to launch: "claude" (default) or
	// "gemini". They take different flags — gemini spells the model -m and
	// needs --skip-trust — so the argv is built per agent rather than
	// pretending one shape fits both.
	Agent string `json:"agent,omitempty"`
	// Resume continues an existing conversation instead of starting a new one.
	//
	// This is how a session that lives in somebody's terminal can be picked up
	// inside Caprock. It cannot type into a process it did not start — two
	// writers on one PTY interleave characters and ruin both — so it starts a
	// second process on the same conversation, which is what `--resume` is for.
	// The history is on disk, so nothing is lost and nothing is fought over.
	Resume string `json:"resume,omitempty"`
	// Fork branches the resumed conversation into a new session id rather than
	// reusing the original.
	//
	// Set when the session being picked up is still running somewhere: two live
	// processes claiming one id would write the same transcript and each end up
	// with half the other's turns. A fork keeps the history and leaves the
	// original alone. Claude Code refuses --session-id alongside --resume
	// unless --fork-session is present, which is the same distinction from the
	// other side.
	Fork bool `json:"fork,omitempty"`
	// GeminiKey is the key the daemon holds, passed into the child's
	// environment. Never accepted from the browser — the API fills it in from
	// settings, so a page cannot hand a spawned process someone else's
	// credential.
	GeminiKey string   `json:"-"`
	Args      []string `json:"args,omitempty"` // extra args
	Cols      int      `json:"cols,omitempty"`
	Rows      int      `json:"rows,omitempty"`
}

SpawnRequest describes a session to launch.

Jump to

Keyboard shortcuts

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