Documentation
¶
Overview ¶
Package messenger owns the stream-json process bridge used to deliver a message to an agent session. It is intentionally independent of HTTP and worker entry points so both server-local and worker dispatch paths share the same process lifecycle and environment rules.
Index ¶
- Constants
- func ListAgentsPromptIn(lang i18n.Lang) string
- type Agent
- type AgentList
- type Delivery
- type Manager
- func (m *Manager) Close()
- func (m *Manager) Command() string
- func (m *Manager) ListAgents(ctx context.Context, runner, cwd string, command []string) (AgentList, error)
- func (m *Manager) ListAgentsPrompt() string
- func (m *Manager) ResidentMessengerStatus(runner string) string
- func (m *Manager) Send(ctx context.Context, runner, cwd, target string, command []string) (string, error)
- func (m *Manager) SetClaudeConfigDir(dir string)
- func (m *Manager) SetIdle(idle time.Duration)
- func (m *Manager) SetLanguage(fn func() i18n.Lang)
- func (m *Manager) SetReplyHandler(h ReplyHandler)
- func (m *Manager) SetRotation(maxMessages int, maxAge time.Duration)
- func (m *Manager) Snapshot(runner string) Snapshot
- func (m *Manager) Status(runner string) string
- type PeerReply
- type ReplyHandler
- type Rotation
- type Snapshot
- type Usage
Constants ¶
const ( StatusStopped = "stopped" // no resident process StatusIdle = "idle" // process alive, no request in flight StatusBusy = "busy" // a request is queued or in flight )
Status values reported by Manager.Status / Snapshot.Status.
const ( // MaxDeliveries is how many recent deliveries a runner remembers. MaxDeliveries = 20 // MaxStderrTail caps the stderr ring buffer (bytes). MaxStderrTail = 4096 // MaxRotations is how many recent resident-process rebuilds a runner remembers. MaxRotations = 10 )
const ( ReasonIdle = "idle" // the idle timer stopped it ReasonFailed = "failed" // the process died or the request failed ReasonMaxMessages = "max_messages" // it reached the message-count rotation limit ReasonMaxAge = "max_age" // it reached the lifetime rotation limit )
Rebuild reasons recorded on every resident-process rebuild (Snapshot.Rotations).
const OpReply = "reply"
OpReply marks a delivery-history entry that is a session's reply received by the messenger (not a request sent through it).
Variables ¶
This section is empty.
Functions ¶
func ListAgentsPromptIn ¶ added in v0.206.0
ListAgentsPromptIn is what the resident process is asked to run, in lang. The listing is read from the ListAgents tool_result, so the wording only has to make the model call the tool once.
Types ¶
type Agent ¶
type Agent struct {
Name string `json:"name"`
ShortID string `json:"short_id,omitempty"`
Kind string `json:"kind,omitempty"` // e.g. interactive
Status string `json:"status,omitempty"` // idle | busy | ...
Started string `json:"started,omitempty"`
Cwd string `json:"cwd,omitempty"`
LastActivity string `json:"last_activity,omitempty"`
}
Agent is one session ListAgents reports. Claude's listing carries a name, a short id, a kind, a status and a relative start time; it has NO directory or absolute timestamp, so Cwd/LastActivity stay empty unless a caller fills them from another source (the gofer session registry).
type AgentList ¶
type AgentList struct {
Self string `json:"self,omitempty"` // the messenger's own session name
Agents []Agent `json:"agents"`
RawOutput string `json:"raw_output"`
}
AgentList is the parsed ListAgents result. RawOutput is always the text the parser worked from, so nothing is lost when the format changes.
func ParseAgents ¶
ParseAgents turns the ListAgents listing into rows. It is deliberately tolerant: code fences are stripped, unknown "·" segments are ignored, and lines that do not look like a session row are skipped. RawOutput keeps the original text.
type Delivery ¶
type Delivery struct {
At int64 `json:"at"` // unix seconds the request started
Op string `json:"op,omitempty"` // "send" | "list_agents"
Target string `json:"target,omitempty"` // target session name (send)
Message string `json:"message,omitempty"` // excerpt, <= 200 runes
OK bool `json:"ok"`
Error string `json:"error,omitempty"`
DurationMS int64 `json:"duration_ms,omitempty"`
}
Delivery is the summary of one request sent through the resident process. Message holds a short excerpt (never the full text) so the history is useful without becoming a second message log.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager keeps one stream-json Claude process per local runner. A process is never shared with a remote worker; remote paths use the worker's own Manager.
func New ¶
New creates a resident messenger manager. Non-positive idle values use the shipped ten minute default, matching the pre-package implementation. The rotation limits start at the shipped defaults (config.DefaultMessengerMaxMessages / DefaultMessengerMaxAgeSec); SetRotation overrides them from the config.
func (*Manager) Close ¶ added in v0.135.0
func (m *Manager) Close()
Close stops every resident process and waits until its stdout reader ended. The manager stays usable: the next request starts a new process.
func (*Manager) Command ¶
Command is the configured messenger binary ("" on a worker, where each dispatch carries its own command).
func (*Manager) ListAgents ¶
func (m *Manager) ListAgents(ctx context.Context, runner, cwd string, command []string) (AgentList, error)
ListAgents asks the resident process for the sessions Claude Code can message. command only supplies the binary (command[0]); the prompt is fixed.
func (*Manager) ListAgentsPrompt ¶ added in v0.206.0
ListAgentsPrompt is ListAgentsPromptIn in the language SetLanguage supplied (else the default language).
func (*Manager) ResidentMessengerStatus ¶
ResidentMessengerStatus preserves the session-relay assembly seam while the implementation lives in this package.
func (*Manager) Send ¶
func (m *Manager) Send(ctx context.Context, runner, cwd, target string, command []string) (string, error)
Send writes one stream-json request and waits for its result. target is the addressed session's name; it only labels the delivery history.
func (*Manager) SetClaudeConfigDir ¶ added in v0.135.0
SetClaudeConfigDir overrides where the resident process's Claude transcripts live ("" = ~/.claude: the child runs with every CLAUDE* variable scrubbed, so CLAUDE_CONFIG_DIR never applies to it).
func (*Manager) SetIdle ¶
SetIdle updates the idle lifetime used by newly created resident processes. Existing processes keep their current timer until the next request refreshes it.
func (*Manager) SetLanguage ¶ added in v0.206.0
SetLanguage sets where the language of the prompts sent to the resident process comes from (the server's text language). Unset, the default language is used.
func (*Manager) SetReplyHandler ¶ added in v0.135.0
func (m *Manager) SetReplyHandler(h ReplyHandler)
SetReplyHandler installs the callback for peer replies (nil = only record them in the delivery history).
func (*Manager) SetRotation ¶ added in v0.184.0
SetRotation sets the rotation limits: a resident process that has handled maxMessages messages or lived maxAge is replaced BEFORE the next message, never mid-delivery. Non-positive values switch that limit off (no rotation).
type PeerReply ¶ added in v0.135.0
type PeerReply struct {
From string // peer address, e.g. uds:/tmp/cc-socks/<pid>.sock
Name string // peer session name (Claude Code's from-name)
Body string
Retold string
MsgID string
At int64
}
PeerReply is a message another Claude session sent TO a resident messenger — in practice a target session answering a web message with SendMessage (its natural reply path: the message came from the messenger's socket). Body is the verbatim text from the messenger's own transcript; it is empty when the transcript could not be read, and Retold then holds the messenger model's own account of the turn (never posted as the target's words).
type ReplyHandler ¶ added in v0.135.0
ReplyHandler receives every PeerReply with a verbatim Body, on its own goroutine (the stdout reader never waits for it).
type Rotation ¶ added in v0.184.0
type Rotation struct {
At int64 `json:"at"` // unix seconds of the rebuild
Reason string `json:"reason"` // idle | failed | max_messages | max_age
Messages int64 `json:"messages,omitempty"` // messages the retired process handled
AgeSec int64 `json:"age_sec,omitempty"` // how long it lived
Usage *Usage `json:"usage,omitempty"` // token / cost totals it reported
}
Rotation is one resident-process rebuild: why it happened and what the retired process had handled before it (gofer-li3t).
type Snapshot ¶
type Snapshot struct {
Status string `json:"status"`
StartedAt int64 `json:"started_at,omitempty"`
LastUsedAt int64 `json:"last_used_at,omitempty"`
IdleDeadline int64 `json:"idle_deadline,omitempty"`
// Messages and AgeSec describe the CURRENT resident process (0 when none is
// running): how many messages it handled and how long it has lived. Usage is
// what its result frames reported for tokens / cost. MaxMessages and MaxAgeSec
// are the rotation limits in force (0 = unlimited), so a reader can tell how
// close the process is to being rebuilt.
Messages int64 `json:"messages,omitempty"`
AgeSec int64 `json:"age_sec,omitempty"`
Usage *Usage `json:"usage,omitempty"`
MaxMessages int `json:"max_messages,omitempty"`
MaxAgeSec int `json:"max_age_sec,omitempty"`
Rotations []Rotation `json:"rotations,omitempty"` // recent rebuilds, newest first
Deliveries []Delivery `json:"deliveries,omitempty"`
StderrTail string `json:"stderr_tail,omitempty"`
}
Snapshot is a point-in-time, JSON-friendly view of one runner's messenger. Times are unix seconds (0 = never). Deliveries are newest first.
type Usage ¶ added in v0.184.0
type Usage struct {
InputTokens int64 `json:"input_tokens,omitempty"`
OutputTokens int64 `json:"output_tokens,omitempty"`
CacheReadTokens int64 `json:"cache_read_input_tokens,omitempty"`
CacheCreationTokens int64 `json:"cache_creation_input_tokens,omitempty"`
CostUSD float64 `json:"cost_usd,omitempty"`
}
Usage is the token / cost accounting the stream-json result frames report. A process whose result frames carry none keeps all zeros (never a guess).