claude

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CwdToProjectDir added in v0.13.0

func CwdToProjectDir(cwd string) string

CwdToProjectDir converts an absolute cwd to the Claude project subdir name. e.g. "/home/user/project" -> "-home-user-project"

func FormatMemory added in v0.13.0

func FormatMemory(mem *MemoryInfo) string

func FormatModelRows added in v0.13.0

func FormatModelRows(w io.Writer, summary *UsageSummary, grandTotal int) error

func FormatMultiUsage added in v0.12.0

func FormatMultiUsage(w io.Writer, instances []InstanceUsage, now time.Time) error

FormatMultiUsage writes per-instance and aggregated total usage.

func FormatTmuxPanes added in v0.12.0

func FormatTmuxPanes(w io.Writer, panes []TmuxPane) error

FormatTmuxPanes writes the tmux pane listing to w.

func FormatUsage added in v0.11.0

func FormatUsage(w io.Writer, summary *UsageSummary, now time.Time) error

FormatUsage writes the usage summary to w.

func Install

func Install(w io.Writer, fw FileWriter, personal bool) error

Install writes the Claude Code skill and agent files to disk. When personal is true, files are written under ~/.claude/ instead of .claude/.

func InstallHooks added in v0.13.0

func InstallHooks(w io.Writer, fw FileWriter) error

InstallHooks writes the hook script and registers hooks in ~/.claude/settings.json.

func MergeUsage added in v0.12.0

func MergeUsage(dst, src *UsageSummary)

MergeUsage adds all model usage from src into dst.

func ShortProjectName added in v0.13.0

func ShortProjectName(cwd string) string

ShortProjectName returns the last two path components for a readable label. e.g. "/home/user/dev/myproject" -> "dev/myproject"

func WindowEnd added in v0.11.0

func WindowEnd(start time.Time) time.Time

WindowEnd returns the end of the current 5-hour usage window in UTC.

func WindowStart added in v0.11.0

func WindowStart(now time.Time) time.Time

WindowStart returns the start of the current 5-hour usage window in UTC.

Types

type ByteWalker added in v0.12.0

type ByteWalker struct {
	Data []byte
}

ByteWalker implements DirWalker over in-memory bytes (one JSONL line per text line).

func (*ByteWalker) WalkJSONL added in v0.12.0

func (b *ByteWalker) WalkJSONL(_ string, fn func(line []byte) error) error

type CombinedFinder added in v0.12.0

type CombinedFinder struct {
	Finders []InstanceFinder
}

CombinedFinder aggregates multiple InstanceFinders, logging and skipping failures.

func (*CombinedFinder) FindInstances added in v0.12.0

func (c *CombinedFinder) FindInstances(ctx context.Context) ([]Instance, error)

type CommChecker added in v0.13.0

type CommChecker func(pid int) bool

CommChecker verifies a process command name.

type CommandRunner added in v0.12.0

type CommandRunner interface {
	Run(ctx context.Context, name string, args ...string) ([]byte, error)
}

CommandRunner abstracts running external commands for testability.

type ContainerChecker added in v0.13.0

type ContainerChecker interface {
	IsContainerized(pid int) bool
}

ContainerChecker determines whether a process is running inside a container.

type ContainerInfo added in v0.12.0

type ContainerInfo struct {
	ID   string
	Name string
}

ContainerInfo holds minimal container metadata.

type CwdResolver added in v0.13.0

type CwdResolver interface {
	ResolveCwd(pid int) (string, error)
}

CwdResolver resolves the current working directory for a process.

type DirWalker added in v0.11.0

type DirWalker interface {
	WalkJSONL(root string, fn func(line []byte) error) error
}

DirWalker abstracts walking JSONL files for testability.

type DockerClient added in v0.12.0

type DockerClient interface {
	ListContainers(ctx context.Context) ([]ContainerInfo, error)
	Exec(ctx context.Context, containerID string, cmd []string) (int, io.Reader, error)
	ContainerStats(ctx context.Context, containerID string) (*MemoryInfo, error)
	Close() error
}

DockerClient abstracts Docker operations for testability.

func NewEngineDockerClient added in v0.12.0

func NewEngineDockerClient() (DockerClient, error)

NewEngineDockerClient creates a DockerClient backed by the Docker Engine API.

type DockerFinder added in v0.12.0

type DockerFinder struct {
	Client   DockerClient
	CacheTTL time.Duration // TTL for container data cache; defaults to 2s
	// contains filtered or unexported fields
}

DockerFinder discovers Claude Code instances inside Docker containers.

func (*DockerFinder) FindInstances added in v0.12.0

func (d *DockerFinder) FindInstances(ctx context.Context) ([]Instance, error)

type FileSessionResolver added in v0.13.0

type FileSessionResolver struct {
	HomeDir string
}

FileSessionResolver reads session info from ~/.claude/sessions/<PID>.json.

func (FileSessionResolver) ResolveSessionID added in v0.13.0

func (r FileSessionResolver) ResolveSessionID(pid int) (string, error)

type FileWriter

type FileWriter interface {
	MkdirAll(path string, perm os.FileMode) error
	WriteFile(name string, data []byte, perm os.FileMode) error
	ReadFile(name string) ([]byte, error)
}

FileWriter abstracts filesystem operations for testability.

type HostFinder added in v0.12.0

type HostFinder struct {
	Runner           CommandRunner
	HomeDir          string           // override for testing; empty uses os.UserHomeDir result passed externally
	CwdResolver      CwdResolver      // nil defaults to ProcCwdResolver
	ContainerChecker ContainerChecker // nil defaults to ProcContainerChecker
	SessionResolver  SessionResolver  // nil defaults to FileSessionResolver{HomeDir: h.HomeDir}
	CommChecker      CommChecker      // nil defaults to verifyProcComm
}

HostFinder discovers Claude Code instances on the local host via pgrep.

func (*HostFinder) FindInstances added in v0.12.0

func (h *HostFinder) FindInstances(ctx context.Context) ([]Instance, error)

type Instance added in v0.12.0

type Instance struct {
	Label       string      // e.g. "Host (PID 7046)" or `Container "dev-myapp" (abc123)`
	Source      string      // "host" or "container"
	Walker      DirWalker   // how to read its JSONL data
	Root        string      // JSONL root path (or virtual path for containers)
	Memory      *MemoryInfo // memory usage (containers only)
	ContainerID string      // full Docker container ID (containers only)
	PID         int         // host PID of the claude process (0 for containers)
	FilePath    string      // resolved JSONL path for fsnotify (host instances only)
}

Instance represents a discovered Claude Code instance.

type InstanceFinder added in v0.12.0

type InstanceFinder interface {
	FindInstances(ctx context.Context) ([]Instance, error)
}

InstanceFinder discovers running Claude Code instances.

type InstanceState added in v0.12.0

type InstanceState int

InstanceState represents whether a Claude Code instance is busy or ready.

const (
	StateUnknown InstanceState = iota
	StateBusy
	StateReady
)

func (InstanceState) String added in v0.12.0

func (s InstanceState) String() string

type InstanceUsage added in v0.12.0

type InstanceUsage struct {
	Instance Instance
	Summary  *UsageSummary
	State    InstanceState
}

InstanceUsage pairs an Instance with its calculated usage.

func CollectInstanceUsage added in v0.13.0

func CollectInstanceUsage(instances []Instance, now time.Time) []InstanceUsage

CollectInstanceUsage calculates usage for each instance and returns results.

type MemoryInfo added in v0.12.0

type MemoryInfo struct {
	Usage uint64 // current memory usage in bytes
	Limit uint64 // memory limit in bytes (0 = unlimited)
}

MemoryInfo holds memory usage and limit for a container.

type ModelUsage added in v0.11.0

type ModelUsage struct {
	InputTokens  int
	OutputTokens int
	CacheCreate  int
	CacheRead    int
}

ModelUsage holds aggregated token counts for one model class.

func (*ModelUsage) Total added in v0.13.0

func (mu *ModelUsage) Total() int

Total returns the sum of all token fields.

type OSCommandRunner added in v0.12.0

type OSCommandRunner struct{}

OSCommandRunner implements CommandRunner using os/exec.

func (OSCommandRunner) Run added in v0.12.0

func (OSCommandRunner) Run(ctx context.Context, name string, args ...string) ([]byte, error)

type OSDirWalker added in v0.11.0

type OSDirWalker struct{}

OSDirWalker implements DirWalker using the real filesystem.

func (OSDirWalker) WalkJSONL added in v0.11.0

func (OSDirWalker) WalkJSONL(root string, fn func(line []byte) error) error

type OSFileWriter

type OSFileWriter struct{}

OSFileWriter implements FileWriter using the os package.

func (OSFileWriter) MkdirAll

func (OSFileWriter) MkdirAll(path string, perm os.FileMode) error

func (OSFileWriter) ReadFile

func (OSFileWriter) ReadFile(name string) ([]byte, error)

func (OSFileWriter) WriteFile

func (OSFileWriter) WriteFile(name string, data []byte, perm os.FileMode) error

type OSProcessLister added in v0.12.0

type OSProcessLister struct {
	Runner CommandRunner
}

OSProcessLister implements ProcessLister using ps.

func (*OSProcessLister) ListProcesses added in v0.12.0

func (l *OSProcessLister) ListProcesses(ctx context.Context) ([]ProcessInfo, error)

ListProcesses returns all processes with their PID, PPID, command name, and args.

type OSTmuxClient added in v0.12.0

type OSTmuxClient struct {
	Runner CommandRunner
}

OSTmuxClient implements TmuxClient using the real tmux command.

func (*OSTmuxClient) ListPanes added in v0.12.0

func (c *OSTmuxClient) ListPanes(ctx context.Context) ([]TmuxPane, error)

ListPanes runs tmux list-panes and parses the output.

type ProcContainerChecker added in v0.13.0

type ProcContainerChecker struct{}

ProcContainerChecker reads /proc/<pid>/cgroup to detect containerized processes.

func (ProcContainerChecker) IsContainerized added in v0.13.0

func (ProcContainerChecker) IsContainerized(pid int) bool

type ProcCwdResolver added in v0.13.0

type ProcCwdResolver struct{}

ProcCwdResolver reads /proc/<pid>/cwd (Linux).

func (ProcCwdResolver) ResolveCwd added in v0.13.0

func (ProcCwdResolver) ResolveCwd(pid int) (string, error)

type ProcessInfo added in v0.12.0

type ProcessInfo struct {
	PID  int
	PPID int
	Comm string
	Args string // full command line (may be empty)
}

ProcessInfo holds PID, parent PID, command name, and full argument line.

type ProcessLister added in v0.12.0

type ProcessLister interface {
	ListProcesses(ctx context.Context) ([]ProcessInfo, error)
}

ProcessLister abstracts listing all processes with PID, PPID, and command info.

type SessionResolver added in v0.13.0

type SessionResolver interface {
	ResolveSessionID(pid int) (string, error)
}

SessionResolver resolves the session ID for a Claude process.

type TmuxClient added in v0.12.0

type TmuxClient interface {
	ListPanes(ctx context.Context) ([]TmuxPane, error)
}

TmuxClient abstracts listing tmux panes for testability.

type TmuxPane added in v0.12.0

type TmuxPane struct {
	PID          int
	SessionName  string
	WindowIndex  int
	PaneIndex    int
	Cwd          string        // pane working directory
	Devcontainer bool          // true when claude runs inside a devcontainer in this pane
	ContainerID  string        // matched container ID (only set when Devcontainer is true)
	ClaudePID    int           // PID of the actual claude process (0 if unknown)
	State        InstanceState // busy/ready/unknown
}

TmuxPane represents a tmux pane with an active Claude process.

func FindClaudePanes added in v0.12.0

func FindClaudePanes(ctx context.Context, client TmuxClient, lister ProcessLister, claudeContainerIDs []string) ([]TmuxPane, error)

FindClaudePanes discovers tmux panes that have a Claude process running in them. It lists all tmux panes, builds a process tree from ps output, and checks each pane's descendant processes for a "claude" command or a "docker exec" into a container known to run Claude (identified by claudeContainerIDs).

type UsageSummary added in v0.11.0

type UsageSummary struct {
	Models map[string]*ModelUsage
}

UsageSummary holds the full usage breakdown for the current window.

func CalculateUsage added in v0.11.0

func CalculateUsage(walker DirWalker, root string, now time.Time) (*UsageSummary, error)

CalculateUsage scans JSONL files under root and returns usage broken down by model.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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