policy

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package policy holds Guard Mode's remote budget policies: the caps a Goei team owner set that this device enforces locally. Policies are fetched from the Goei server (via GET /api/policy or the sync-response piggyback), cached on disk so enforcement survives a network blip and a restart, and never invent a stricter rule than the server sent.

Remote policies are kept OUT of the user's config.toml on purpose: that file is hand-editable and its TOML round-trip drops comments, so mixing server-owned rules into it would clobber user intent. The cache is a separate, machine-managed JSON file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapPeriod

func MapPeriod(s string) budget.Period

MapPeriod maps the server's period string to budgetclaw's period enum.

func Save

func Save(b *Bundle) error

Save writes the policy bundle atomically (temp-file-plus-rename), mode 0600, creating the state dir if needed.

Types

type Bundle

type Bundle struct {
	PolicyVersion int      `json:"policyVersion"`
	ETag          string   `json:"etag"`
	FetchedAt     string   `json:"fetchedAt"`
	Policies      []Policy `json:"policies"`
}

Bundle is the cached policy set plus the ETag needed for a cheap conditional refresh and the time it was fetched (shown in `guard status`).

func BundleFromResponse

func BundleFromResponse(pr *goei.PolicyResponse, etag, fetchedAt string) *Bundle

BundleFromResponse builds a cacheable Bundle from a server response.

func Load

func Load() (*Bundle, error)

Load reads the cached policy bundle. A missing cache is not an error: it returns an empty bundle so a first run behaves like "no policies".

type Policy

type Policy struct {
	ID               string `json:"id"`
	ScopeType        string `json:"scopeType"`  // team | project | dev
	ScopeValue       string `json:"scopeValue"` // "" | project name
	Period           string `json:"period"`     // day | week | month
	CapCents         int    `json:"capCents"`
	Enforcement      string `json:"enforcement"` // local_exact | server_aggregate
	Action           string `json:"action"`      // warn | kill
	ServerSpentCents int    `json:"serverSpentCents"`
	AsOf             string `json:"asOf"`
	SetBy            string `json:"setBy"`
}

Policy is one remote budget cap, resolved for this device.

func Aggregate

func Aggregate(policies []Policy) []Policy

Aggregate returns only the server-aggregate (warn-only) policies.

func FromWire

func FromWire(w goei.WirePolicy) Policy

FromWire converts one server WirePolicy into a Policy.

func LocalExact

func LocalExact(policies []Policy) []Policy

LocalExact returns only the locally-enforceable policies.

func (Policy) CapUSD

func (p Policy) CapUSD() float64

CapUSD is the cap in dollars.

func (Policy) IsLocalExact

func (p Policy) IsLocalExact() bool

IsLocalExact reports whether this policy is enforceable against the local rollup (and thus kill-eligible). Server-aggregate policies are warn-only.

Jump to

Keyboard shortcuts

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