How nezhahq/agent works
Nezha Agent is a host-monitoring agent that connects to the Nezha service, reports host state, and handles server-directed tasks. Its principal workflow is startup/configuration, remote connection, periodic state reporting, and task execution; task capabilities include file operations, terminal access, NAT forwarding, and monitoring. The excerpts show parts of this flow, but not the task dispatcher or several session implementations, so their detailed links are omitted or marked unverified. The source index and README provide only limited product documentation.
Main entry points: main.go Agent entry · task_dispatch.go Task dispatch · state.go State collection · connection_session.go Connection session
Components 21
Agent runtime
- Agent entry
cmd/agent/main.go - Configuration
model/config.go - Connection session
cmd/agent/connection_session.go - Task dispatch
cmd/agent/task_dispatch.go - Host and task models
model/host.go - Service protocol
proto/nezha.proto
Host monitoring
- State reporting
cmd/agent/report_state_session.go - State collection
pkg/monitor/state.go - Host inventory
pkg/monitor/host.go - GPU metrics
pkg/monitor/gpu/gpu_darwin.go - IP discovery
pkg/monitor/myip.go
Remote tasks
- MCP handlers
cmd/agent/mcp_handlers.go - Terminal session
cmd/agent/terminal_session.go - NAT session
cmd/agent/nat_session.go - Process tasks
cmd/agent/mcp_exec.go - PTY support
pkg/pty/pty.go
File operations
- File transfer tasks
pkg/fm/tasks.go - Transfer operations
pkg/fm/operations.go - Anchored filesystem
pkg/hostfs/anchor.go
Other components
- Operator
- Nezha service
Connections 20
- Operator → Agent entry: configures
cmd/agent/main.go - Agent entry → Configuration: loads
cmd/agent/main.go - Agent entry → Connection session: opens
- Connection session → Nezha service: connects
- Agent entry → State reporting: starts
cmd/agent/main.go - State reporting → State collection: reports
- Agent entry → Task dispatch: starts
cmd/agent/main.go - Task dispatch → MCP handlers: routes
- Task dispatch → Terminal session: routes
- Task dispatch → NAT session: routes
- Task dispatch → Process tasks: routes
- MCP handlers → Anchored filesystem: uses
- File transfer tasks → Transfer operations: dispatches
pkg/fm/tasks.go - Terminal session → PTY support: starts
cmd/agent/terminal_session.go - State collection → Host and task models: produces state
- State collection → GPU metrics: collects
- State collection → IP discovery: discovers IP
- State collection → Host inventory: collects host
- Host and task models → Service protocol: encodes
model/host.go - Connection session → Service protocol: streams