Documentation
¶
Overview ¶
Package config resolves Caprock's data directory and the small on-disk files that live in it: config.json (user settings) and runtime.json (per-run port + token, read by the hook shim). See .ai/03-contracts.md and ADR-013.
Index ¶
- Constants
- func ChatsDir(dir string) string
- func ConfigPath(dir string) string
- func DBPath(dir string) string
- func DataDir() (string, error)
- func EnsureDataDir() (string, error)
- func HookDebugLogPath(dir string) string
- func LogPath(dir string) string
- func NewSessionID() string
- func PasteDir(dir string) string
- func PricingPath(dir string) string
- func ReadDevices(dir string) ([]pairing.Device, error)
- func RemoveDevices(dir string) error
- func RemoveRuntime(dir string) error
- func RuntimePath(dir string) string
- func Save(dir string, cfg Config) error
- func ServiceLogPath(dir string) string
- func ShimPath(dir string) string
- func WriteDevices(dir string, ds []pairing.Device) error
- func WriteFileAtomic(path string, data []byte, perm os.FileMode) error
- func WriteRuntime(dir string, rt Runtime) error
- type Config
- type Runtime
Constants ¶
const ( // DefaultPort is the single loopback port shared by the API, WS, UI and // hook receiver on a fresh install. 22776 spells CAPRO on a phone keypad, // is unassigned by IANA, sits below common ephemeral ranges, and avoids // Vite Preview's default 4173. DefaultPort = 22776 // LegacyDefaultPort preserves the origin and bookmarks of an existing // install that never wrote config.json. A port is part of a browser origin; // silently moving it would also strand LAN pairing tokens in localStorage. LegacyDefaultPort = 4173 )
const EnvDataDir = "CAPROCK_DATA_DIR"
EnvDataDir overrides the resolved data directory when set.
Variables ¶
This section is empty.
Functions ¶
func ChatsDir ¶ added in v0.28.0
ChatsDir is where quick chats live — sessions started to ask something rather than to work on a repository.
Under the data directory rather than a second `~/.caprock`: one place that holds Caprock's state is one place to back up, clean out and explain. A chat is state, not a user's project, and it has no business being somewhere the user has to discover separately.
func DataDir ¶
DataDir resolves the data directory: $CAPROCK_DATA_DIR, else os.UserConfigDir()/caprock.
func EnsureDataDir ¶
EnsureDataDir resolves and creates the data directory (0700).
func HookDebugLogPath ¶
func NewSessionID ¶
func NewSessionID() string
NewSessionID returns a random RFC-4122-ish v4 UUID for `claude --session-id`.
func PasteDir ¶ added in v0.33.0
PasteDir is where a file pasted or dropped into the terminal is written.
A browser hands over an image's *bytes*, never its path — there is no path to hand over for something copied from a screenshot tool. Claude Code reads files by path, so the bytes have to become a file somewhere before its path can be typed into the session.
Under the data directory for the same reason chats are: it is Caprock's state, it is one place to back up and clear out, and it is not somewhere the user has to go looking for.
func PricingPath ¶
func ReadDevices ¶ added in v0.49.0
ReadDevices restores the guest list. A missing file is the normal first-run case and returns nothing rather than an error.
func RemoveDevices ¶ added in v0.49.0
RemoveDevices forgets every paired device.
func RemoveRuntime ¶
RemoveRuntime deletes runtime.json; a missing file is not an error.
func RuntimePath ¶
func ServiceLogPath ¶ added in v0.59.6
ServiceLogPath is where launchd and systemd send the daemon's output.
func WriteDevices ¶ added in v0.49.0
WriteDevices saves the paired devices. Called after every pairing and every revocation, so a restart does not send someone walking back to their tablet.
What does *not* survive a restart is the decision to listen on the network at all: that is a run flag, so a laptop opened in a coworking space cannot carry a choice made at home. Devices are the guest list; the listener is the open door.
func WriteFileAtomic ¶
WriteFileAtomic writes to a temp file in the same directory and renames it into place, so readers never observe a partial file (the shim reads runtime.json while the daemon may be rewriting it).
func WriteRuntime ¶
WriteRuntime persists runtime.json with 0600 permissions, atomically.
Types ¶
type Config ¶
type Config struct {
Port int `json:"port"`
// Loop detector: >= K same-tool similar-input tool.pre events within T minutes.
LoopK int `json:"loop_k"`
LoopTMinutes int `json:"loop_t_minutes"`
// AutoPause applies to owned sessions only (Phase 1). Default off.
AutoPause bool `json:"auto_pause"`
// Memory decides whether a session opening a folder is told what the last
// one left there. On by default: 79% of sessions open somewhere work
// already happened, so it helps more often than it intrudes — and a
// feature that acts before you type is one nobody discovers if it ships
// off. Stored, unlike LAN access, because this is a working habit rather
// than a door left open.
Memory *bool `json:"memory,omitempty"`
// MemoryHoldoutPct holds the handoff back from this share of the new
// sessions that could have had one, so there is something to compare the
// ones that got it against. 0 — the default — holds nothing back: nobody's
// sessions get worse unless they asked to measure it.
MemoryHoldoutPct int `json:"memory_holdout_pct,omitempty"`
// OpenBrowser controls whether `caprock up` opens the dashboard.
OpenBrowser bool `json:"open_browser"`
// RetentionDays prunes events older than this many days (0 = keep forever).
// The database grows ~1 KB per event; set this if you run Caprock constantly.
RetentionDays int `json:"retention_days"`
// Billing describes how the user actually pays for Claude Code, so the
// dashboard can say something true about what their usage is worth.
//
// Caprock cannot detect this and never guesses: Claude Code does not report
// the plan, and inferring one from usage would be an invented number
// (engineering rule 6). The user states it; we store it locally.
//
// PlanKind is one of:
// "" not stated — no comparison is shown
// "flat" a flat subscription (Pro/Max/Team seat): usage priced at
// API list is an equivalent, and comparing it to the fee is
// meaningful
// "metered" API key, Bedrock, Vertex, or Enterprise usage billed at
// API rates: the API-list figure IS approximately the bill,
// so it must never be framed as a saving
// PlanLabel is the user's own words for the plan ("Max", "Team seat").
// PlanUSDPerMonth is what they pay per month for one seat; 0 = not stated.
// UpdateChecks enables the one outbound call Caprock makes: asking GitHub
// for the latest release tag. Off by default and asked for once — see
// internal/update for why this is opt-in and why Caprock never installs
// the update itself.
UpdateChecks bool `json:"update_checks"`
PlanKind string `json:"plan_kind"`
PlanLabel string `json:"plan_label"`
PlanUSDPerMonth float64 `json:"plan_usd_per_month"`
// LicenseKey unlocks the paid features. Checked locally against its own
// embedded expiry — no network call, no signature (ADR-022). Empty is the
// normal state: the free product is the whole product for one person.
LicenseKey string `json:"license_key,omitempty"`
// CapUSDPerDay is the daily spend ceiling. Zero is off, which is the
// default: a threshold nobody chose would eventually stop work for a
// reason its owner could not explain. See internal/cap.
CapUSDPerDay float64 `json:"cap_usd_per_day,omitempty"`
// ReportBotToken and ReportChatID configure the weekly report's delivery to
// the user's own Telegram bot.
//
// The token is stored here, unlike the Gemini key, and ADR-024 is where
// that difference is argued: a bot token drives a bot the user made for
// this, with no billing attached, while an AI Studio key spends money.
// Putting it in the environment instead would mean editing a launchd plist
// to turn on a feature sold as two minutes of setup. The file is 0600
// inside a 0700 data dir, and the token is never returned over HTTP.
ReportBotToken string `json:"report_bot_token,omitempty"`
ReportChatID string `json:"report_chat_id,omitempty"`
// GeminiAPIKey is the user's Google AI Studio key, entered in the dashboard.
// GEMINI_API_KEY in the environment takes precedence when both exist, so a
// machine already configured that way is untouched (ADR-025). Stored under
// the same 0600/0700 posture as everything else here, and never returned
// over HTTP.
GeminiAPIKey string `json:"gemini_api_key,omitempty"`
// BrowseRoot is where the folder picker may look. Empty means $HOME.
BrowseRoot string `json:"browse_root,omitempty"`
// Terminal is the terminal application "Open in my terminal" uses
// ("ghostty", "iterm2", ...; internal/nativeterm). Empty means the first
// one installed.
Terminal string `json:"terminal,omitempty"`
}
Config is the user-editable configuration stored at <data_dir>/config.json. Every field has a default; a missing file means "all defaults".
func Defaults ¶
func Defaults() Config
Defaults returns the built-in configuration for a fresh install.
func (Config) MemoryOn ¶ added in v0.52.0
MemoryOn reports whether a session should be told what the last one left in the same folder.
A pointer in the struct so that "never set" is distinguishable from "set to false": a config written before this existed must get the feature, and a config where someone turned it off must keep it off. Reading a bare bool would have silently re-enabled it for everyone who had said no.
type Runtime ¶
type Runtime struct {
Port int `json:"port"`
Token string `json:"token"`
PID int `json:"pid"`
StartedAt int64 `json:"started_at"` // unix ms
Version string `json:"version"`
}
Runtime is <data_dir>/runtime.json — written by the daemon on start, read by the shim on every invocation, removed on clean shutdown.
func NewRuntime ¶
NewRuntime creates a runtime record with a fresh random token.
func ReadRuntime ¶
ReadRuntime loads runtime.json. os.ErrNotExist means the daemon is not running.