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
- func Builtin() fs.FS
- func IsShell(p Process) bool
- func LoadManifests(dir string) (map[string]*Manifest, []error)
- func ManifestDir(configDir string) string
- func ParentPID(pid int) (int, error)
- type Explanation
- type HookEvent
- type Manifest
- type Observation
- type Process
- type ProgramReport
- type Rule
- type Status
- type Tracker
- func (t *Tracker) Explain() Explanation
- func (t *Tracker) Export() TrackerState
- func (t *Tracker) Hook(ev HookEvent, now time.Time)
- func (t *Tracker) Interrupted(now time.Time)
- func (t *Tracker) MarkSeen(now time.Time) bool
- func (t *Tracker) Observe(o Observation) bool
- func (t *Tracker) Status() Status
- func (t *Tracker) UserInput()
- type TrackerState
Constants ¶
const ( StateIdle = "idle" StateWorking = "working" StateBlocked = "blocked" StateDone = "done" // idle, and nobody has looked since it finished StateUnknown = "unknown" )
Agent states.
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
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 ¶
IsShell reports whether the process is an interactive shell, i.e. no program is running in the foreground.
func LoadManifests ¶
LoadManifests returns the built-in manifests with overrides from dir applied. A broken override is reported and the built-in is kept.
func ManifestDir ¶
ManifestDir is where user overrides live: <dir>/<agent>.toml replaces the built-in manifest of the same agent.
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 (*Manifest) InterruptKeys ¶ added in v0.2.1
InterruptKeys is the keys that stop the agent: its manifest's, or escape.
func (*Manifest) Match ¶
Match returns the first rule matching the screen or, for title rules, the terminal title.
func (*Manifest) MatchProcess ¶
MatchProcess reports whether a foreground process is this agent.
func (*Manifest) MatchScreen ¶
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 ¶
ForegroundProcess returns the foreground process group leader of the terminal whose master side is ptmx.
func TerminalForeground ¶ added in v0.2.0
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).
type ProgramReport ¶ added in v0.1.7
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 ¶
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) Interrupted ¶ added in v0.2.1
Interrupted notes that the agent was just sent its interrupt key.
func (*Tracker) Observe ¶
func (t *Tracker) Observe(o Observation) bool
Observe evaluates a sample and reports whether the status changed.
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.