hermes

package
v0.140.2 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 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 hermes reads sessions from Hermes Agent's SQLite state database.

Hermes Agent (Nous Research's hermes-agent) keeps every conversation it has — in its own CLI and TUI, in cron jobs, in a Telegram chat through its gateway — in one database, `<hermes home>/state.db`. The `sessions` table carries per-session token counters and Hermes's own cost figure, and `messages` keeps the prose, the prompts and the tool calls. Reading it is a translation job, as OpenCode's is: the numbers arrive finished and are mapped onto Caprock's event model, never recomputed.

The database belongs to another program that is writing to it while we read. It is opened with mode=ro, which never takes a write lock, and a busy timeout, so a moment where Hermes holds the database is a wait rather than an error. It is never opened with immutable=1: Hermes runs in WAL mode, and an immutable open both misses whatever sits in the WAL and may read a page mid-change. Nothing here writes, creates or migrates anything.

Everything below was read from Hermes Agent v0.21.6's own source (hermes_state_common.py, hermes_state_usage.py, agent/usage_pricing.py, agent/session_source.py, hermes_state_ids.py) on 2026-10-15; see .ai/24-hermes.md.

Index

Constants

View Source
const (
	BasisActual    = "actual"    // the provider reported what it billed
	BasisEstimated = "estimated" // Hermes priced the tokens from its own table
	BasisIncluded  = "included"  // a subscription route: no per-call invoice
	BasisUnknown   = "unknown"   // Hermes could not price it
)

Cost bases, as the UI labels them.

View Source
const Agent = "hermes"

Agent is the value sessions read from Hermes carry in sessions.agent.

View Source
const EnvHome = "HERMES_HOME"

EnvHome is Hermes's own override for its data directory.

Variables

This section is empty.

Functions

func Background

func Background(source string) bool

Background reports whether nobody was reading the session as it ran. Hermes defines its attended sources itself (agent/session_source.py, ATTENDED_SOURCES): its CLI, TUI, desktop app and ACP editors. Everything else — cron, a Telegram or Slack chat through the gateway, a one-shot run, a kanban worker — is work that happened without a person watching it.

func DBPath

func DBPath() string

DBPath is Hermes's state database when it exists, "" otherwise.

func Home

func Home() string

Home is the directory Hermes keeps its state in, resolved the way Hermes resolves it (hermes_constants.get_hermes_home): HERMES_HOME when set, else %LOCALAPPDATA%\hermes on Windows and ~/.hermes everywhere else. "" when no home directory can be found at all.

func IDTime

func IDTime(id string) (time.Time, bool)

IDTime is the moment Hermes's own session id was minted, when the id has the CLI's `YYYYMMDD_HHMMSS_<hex>` shape (hermes_state_ids.new_session_id), in local time as Hermes writes it. The CLI mints the id when it starts and creates the row only on the first message, so this — not started_at — is when a process Caprock spawned began.

func Open

func Open(path string) (*sql.DB, error)

Open opens Hermes's database read-only. SQLite opens lazily, so a file that is not a database (or one that is locked) is only found out on the first query; callers read Sessions once before announcing anything.

func ToolInput

func ToolInput(hermes string, in map[string]any, cwd string) map[string]any

ToolInput reshapes a call's arguments into a Claude Code hook payload's tool_input: Hermes's `path` becomes `file_path` for the file tools (a relative one resolved against the session's folder, so per-directory attribution places it), and stays `path` for a search. Every other key is kept as Hermes wrote it.

func ToolName

func ToolName(hermes string) string

ToolName is the name Caprock stores for a Hermes tool.

Types

type Ingester

type Ingester struct {

	// Link files a session under the Caprock session that started it, when
	// Caprock did (internal/sessionlink). Nil keeps Hermes's own ids.
	Link *sessionlink.Linker
	// contains filtered or unexported fields
}

Ingester copies Hermes's sessions into Caprock's store.

It polls, as the OpenCode reader does: Hermes has outbound webhooks and shell hooks, but cost lives only in state.db, and the database is the one source that also carries history from before Caprock was installed.

Hermes keeps usage per session, not per message: every API call adds its tokens and its cost to the session's counters (update_token_counts, the delta path — the only one 0.21.6 uses). So the importer reconciles. Each pass it compares the session's counters with what Caprock has stored for it and records the difference on the newest assistant message of the pass, or, when no new message arrived with it, on a usage record of its own. Caprock's totals therefore equal Hermes's, whatever order messages and counters were flushed in, and a re-read or a restart adds nothing: the difference is zero.

Every write is keyed — messages by Hermes's message id, usage records by the counters they bring the session up to — and `(session_id, key)` is unique in the store.

func NewIngester

func NewIngester(db *sql.DB, rec *rollup.Recorder, log *slog.Logger, every time.Duration) *Ingester

NewIngester builds an ingester over an already-open Hermes database.

func (*Ingester) Once

func (in *Ingester) Once(ctx context.Context) error

Once reads everything that changed since the last pass.

func (*Ingester) Run

func (in *Ingester) Run(ctx context.Context) error

Run polls until the context is cancelled, importing once immediately.

func (*Ingester) Stats

func (in *Ingester) Stats() Stats

Stats returns a snapshot of what has been imported.

type Message

type Message struct {
	ID        int64
	SessionID string
	Role      string // user, assistant, tool, system
	Content   string
	ToolName  string
	ToolCalls string // JSON, OpenAI's shape
	At        time.Time
	// Summary marks the synthetic summary Hermes writes when it compresses a
	// conversation: its words, not the model's reply.
	Summary bool
}

Message is one row of Hermes's `messages` table.

func Messages

func Messages(ctx context.Context, db *sql.DB, sessionID string, after int64) ([]Message, error)

Messages reads one session's messages with an id above after, in order.

type Session

type Session struct {
	ID       string
	Source   string // cli, tui, desktop, acp, cron, telegram, oneshot, …
	ParentID string
	Model    string
	Title    string
	Cwd      string
	Branch   string

	Started      time.Time
	Ended        time.Time
	EndReason    string
	LastActivity time.Time

	// The cumulative counters Hermes keeps per session. InputTokens is fresh
	// input: Hermes's prompt is input + cache read + cache write
	// (CanonicalUsage.prompt_tokens), the same split as Caprock's.
	InputTokens, OutputTokens         int64
	CacheReadTokens, CacheWriteTokens int64
	ReasoningTokens                   int64
	APICalls, ToolCalls, Messages     int64

	BillingProvider, BillingMode string
	CostStatus, CostSource       string
	Estimated, Actual            sql.NullFloat64

	// The model_config markers Hermes writes on a child row, which say what
	// kind of child it is (hermes_state_common.py, delegate_tool.py).
	DelegateFrom, BranchedFrom, ResetFrom string
}

Session is one row of Hermes's `sessions` table, with the figures Caprock uses. Times are Hermes's own unix seconds converted to time.Time; a zero time means the column was NULL.

func Sessions

func Sessions(ctx context.Context, db *sql.DB) ([]Session, error)

Sessions reads every session, oldest first, so a parent is always read before the children it spawned.

func (Session) Cost

func (s Session) Cost() (usd float64, basis string, ok bool)

Cost is the session's spend and what kind of figure it is. Hermes's actual figure wins when Hermes says it is real — status "actual", or a source that is the provider's own billing — and is recorded; otherwise its estimate. A subscription route is priced at zero and called that. ok is false when Hermes has no figure at all, which Caprock shows as unpriced rather than as a guess (rule 6).

A provider profile may hand Hermes the provider's own figure as the call's cost (providers/base.py get_usage_cost, "distinguish estimates from invoices"); Hermes then writes it to estimated_cost_usd with status "actual" and leaves actual_cost_usd empty, so that pair reads as actual too. Hermes's own readers take COALESCE(actual_cost_usd, estimated_cost_usd) (hermes_state_maintenance.py), which is the same figure.

func (Session) Tokens

func (s Session) Tokens() int64

Tokens is every token the session moved.

type Stats

type Stats struct {
	Sessions   int   `json:"sessions"`
	Events     int   `json:"events"`
	Unpriced   int   `json:"unpriced,omitempty"`
	Background int   `json:"background,omitempty"`
	LastPoll   int64 `json:"last_poll_ms,omitempty"`
}

Stats is what the daemon reports about the Hermes reader.

type ToolCall

type ToolCall struct {
	ID    string
	Name  string // Hermes's own name: terminal, read_file, patch, …
	Input map[string]any
}

ToolCall is one call an assistant message asked for.

func ToolCalls

func ToolCalls(raw string) []ToolCall

ToolCalls parses a message's tool_calls column, OpenAI's shape ({id, type, function: {name, arguments}}), whose arguments are a JSON string. A call that names no tool is skipped; arguments that are not JSON are kept under `command`, the shape a shell call takes.

Jump to

Keyboard shortcuts

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