detect

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 14 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 detect works out which agent runs in a pane and what it is doing, from the terminal's foreground process, the visible screen, and lifecycle hooks the agent reports.

Index

Constants

View Source
const (
	StateIdle    = "idle"
	StateWorking = "working"
	StateBlocked = "blocked"
	StateDone    = "done" // idle, and nobody has looked since it finished
	StateUnknown = "unknown"
)

Agent states.

View Source
const FailedMessage = "the last request failed"

FailedMessage describes a request that ended with an error.

Variables

This section is empty.

Functions

func Builtin added in v0.1.7

func Builtin() fs.FS

Builtin is the manifests conch ships, for a reader that wants more out of them than detection: a manifest is one file an agent, and the ones that also say how to *run* their agent are read by internal/adapter from here. Two readers, one file, because somebody adding an agent is answering one question.

func IsShell

func IsShell(p Process) bool

IsShell reports whether the process is an interactive shell, i.e. no program is running in the foreground.

func LoadManifests

func LoadManifests(dir string) (map[string]*Manifest, []error)

LoadManifests returns the built-in manifests with overrides from dir applied. A broken override is reported and the built-in is kept.

func ManifestDir

func ManifestDir(configDir string) string

ManifestDir is where user overrides live: <dir>/<agent>.toml replaces the built-in manifest of the same agent.

func ParentPID added in v0.1.6

func ParentPID(pid int) (int, error)

ParentPID returns the parent of process pid.

Types

type Explanation

type Explanation struct {
	Status      Status   `json:"status"`
	Hint        string   `json:"hint,omitempty"`
	Process     Process  `json:"process"`
	ProcessErr  string   `json:"process_error,omitempty"`
	Manifest    string   `json:"manifest,omitempty"`
	ScreenRule  string   `json:"screen_rule,omitempty"`
	HookState   string   `json:"hook_state,omitempty"`
	HookReason  string   `json:"hook_reason,omitempty"`
	HookAge     string   `json:"hook_age,omitempty"`
	Seen        bool     `json:"seen"`
	ScreenLines []string `json:"screen_tail,omitempty"`
	// ProgramState is what the program said about itself (OSC 7501), and
	// ProgramApp what it called itself. Empty for a program that does not
	// speak the protocol.
	ProgramState string `json:"program_state,omitempty"`
	ProgramKind  string `json:"program_kind,omitempty"`
	ProgramApp   string `json:"program_app,omitempty"`
	ProgramAge   string `json:"program_age,omitempty"`
}

Explanation shows how the current status was reached.

type HookEvent

type HookEvent struct {
	Agent            string `json:"agent"`
	Event            string `json:"event"`
	NotificationType string `json:"notification_type,omitempty"`
	Message          string `json:"message,omitempty"`
	SessionID        string `json:"session_id,omitempty"`
}

HookEvent is a lifecycle event reported by an agent integration.

type Manifest

type Manifest struct {
	Agent        string   `toml:"agent"`
	ProcessNames []string `toml:"process_names"`
	// ScreenLines is how many rows from the bottom of the screen rules see.
	ScreenLines int `toml:"screen_lines"`
	// HookWorkingStale demotes a hook-reported "working" state to idle once
	// no working rule has matched the screen for this long. Hooks miss some
	// transitions (an Esc interrupt fires no hook); 0 disables.
	HookWorkingStale duration `toml:"hook_working_stale"`
	// Interrupt is the keys that stop the agent's work, in pane.send_keys
	// notation, each sent on its own: escape for most, escape twice for an
	// agent whose screen says "esc twice to interrupt". Empty is escape.
	Interrupt []string `toml:"interrupt"`
	// Helpers are programs the agent keeps running below it for its own
	// sake — a language server, a keep-awake — matched by name like
	// process_names. They are not work it left running in the background.
	Helpers []string `toml:"helpers"`
	Rules   []Rule   `toml:"rules"`
}

Manifest describes how to recognise one agent and read its screen.

func MatchAny

func MatchAny(manifests map[string]*Manifest, p Process) *Manifest

MatchAny finds the manifest whose agent owns the process.

func (*Manifest) InterruptKeys added in v0.2.1

func (m *Manifest) InterruptKeys() []string

InterruptKeys is the keys that stop the agent: its manifest's, or escape.

func (*Manifest) Match

func (m *Manifest) Match(lines []string, title string) *Rule

Match returns the first rule matching the screen or, for title rules, the terminal title.

func (*Manifest) MatchProcess

func (m *Manifest) MatchProcess(p Process) bool

MatchProcess reports whether a foreground process is this agent.

func (*Manifest) MatchScreen

func (m *Manifest) MatchScreen(lines []string) *Rule

MatchScreen returns the first rule matching the bottom of the screen. Each rule is tried against the rows as they are and flattened to one line (whitespace collapsed, box borders removed), because agents re-wrap their text when the pane is resized.

type Observation

type Observation struct {
	Now        time.Time
	Process    Process
	ProcessErr error
	Screen     []string // visible screen as plain text
	Title      string   // terminal title
	Watched    bool     // a client is looking at the pane
	// Program is what the program in the pane last said about itself
	// through the Program Status Protocol (OSC 7501), if it speaks it.
	// Nil for the agents that do not, which is most of them today.
	Program *ProgramReport
}

Observation is one sample of a pane.

type Process

type Process struct {
	PID  int      `json:"pid"`
	Name string   `json:"name"` // kernel command name
	Args []string `json:"args,omitempty"`
}

Process describes the foreground process of a terminal.

func ForegroundProcess

func ForegroundProcess(ptmx *os.File) (Process, error)

ForegroundProcess returns the foreground process group leader of the terminal whose master side is ptmx.

func TerminalForeground added in v0.2.0

func TerminalForeground(pid int) (Process, error)

TerminalForeground is the foreground process of the terminal pid runs on: what a person sees running there, which is pid itself while its shell waits at a prompt. It is read without the terminal's descriptor, for a process that does not hold it (conch cloud agent, beside the server that does).

func (Process) Names

func (p Process) Names() []string

Names returns the identifiers an agent can be matched by: the kernel command name, the base name of argv[0], and for interpreters the script (so `node /usr/local/bin/gemini` matches gemini).

type ProgramReport added in v0.1.7

type ProgramReport struct {
	State string
	Kind  string
	App   string
	Msg   string
	At    time.Time
}

ProgramReport is a program's own account of its state, taken off the terminal rather than guessed from it. The states are the protocol's: idle, working, done, blocked, error; Kind says why a blocked program is blocked (permission, question, auth).

type Rule

type Rule struct {
	Name    string `toml:"name"`
	State   string `toml:"state"`
	Pattern string `toml:"pattern"`
	// On is what the pattern is matched against: "screen" (default) or
	// "title", the terminal title agents use to show their activity.
	On string `toml:"on"`
	// contains filtered or unexported fields
}

Rule maps a pattern to a state. Rules are tried in order.

type Status

type Status struct {
	Agent     string    `json:"agent"`            // "" when no agent runs in the pane
	State     string    `json:"state,omitempty"`  // idle, working, blocked, done
	Source    string    `json:"source,omitempty"` // hook, screen
	Reason    string    `json:"reason,omitempty"` // rule or hook event that decided
	Message   string    `json:"message,omitempty"`
	SessionID string    `json:"session_id,omitempty"`
	Since     time.Time `json:"since"`
	// Failed means the agent's last request ended with an error (Claude's
	// StopFailure, OpenCode's session.error) rather than an answer.
	Failed bool `json:"failed,omitempty"`
}

Status is what conch believes an agent pane is doing.

type Tracker

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

Tracker keeps the agent status of one pane. It is not safe for concurrent use; the owner serialises calls.

func NewTracker

func NewTracker(manifests map[string]*Manifest, hint string) *Tracker

NewTracker creates a tracker. hint names the agent the pane was launched as, which identifies it when a wrapper process hides the agent binary.

func RestoreTracker

func RestoreTracker(manifests map[string]*Manifest, st TrackerState) *Tracker

RestoreTracker rebuilds a tracker from exported state.

func (*Tracker) Explain

func (t *Tracker) Explain() Explanation

Explain returns details of the last evaluation.

func (*Tracker) Export

func (t *Tracker) Export() TrackerState

Export returns the tracker's state.

func (*Tracker) Hook

func (t *Tracker) Hook(ev HookEvent, now time.Time)

Hook records a lifecycle event.

func (*Tracker) Interrupted added in v0.2.1

func (t *Tracker) Interrupted(now time.Time)

Interrupted notes that the agent was just sent its interrupt key.

func (*Tracker) MarkSeen

func (t *Tracker) MarkSeen(now time.Time) bool

MarkSeen clears the done overlay. It reports whether the status changed.

func (*Tracker) Observe

func (t *Tracker) Observe(o Observation) bool

Observe evaluates a sample and reports whether the status changed.

func (*Tracker) Status

func (t *Tracker) Status() Status

Status returns the current status.

func (*Tracker) UserInput

func (t *Tracker) UserInput()

UserInput notes that someone typed into the pane, which also means they have seen it. A hook-reported blocked state is dropped: whatever was asked has likely been answered, and a refusal fires no hook to say so.

type TrackerState

type TrackerState struct {
	Hint          string    `json:"hint,omitempty"`
	HookState     string    `json:"hook_state,omitempty"`
	HookReason    string    `json:"hook_reason,omitempty"`
	HookMessage   string    `json:"hook_message,omitempty"`
	HookAt        time.Time `json:"hook_at,omitempty"`
	HookFailed    bool      `json:"hook_failed,omitempty"`
	SessionID     string    `json:"session_id,omitempty"`
	ScreenWorking time.Time `json:"screen_working,omitempty"`
	Base          string    `json:"base,omitempty"`
	Seen          bool      `json:"seen"`
	Status        Status    `json:"status"`
	// ProgramAt is the last program report (OSC 7501) conch acted on, so
	// a program that said "done" once and has not spoken since does not
	// have that answered for it again after a reload.
	ProgramAt time.Time `json:"program_at,omitempty"`
	// Interrupted is when conch last sent the agent its interrupt key.
	Interrupted time.Time `json:"interrupted,omitempty"`
}

TrackerState is what a tracker carries across a server reload: what hooks last said, so an agent doesn't briefly look idle until its next event.

Jump to

Keyboard shortcuts

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