Playground
caps.gno
// Package agentvault is the reusable spend-cap library behind the
// AgentVault realm: the first wallet an AI agent cannot drain.
//
// It is pure: no realm state, no chain calls, no imports from any
// realm. Every function takes explicit inputs (including the current
// unix time for day rollovers) so the logic is unit-testable without
// a chain. The vault realm in
// gno.land/r/g16740fm9kd28luuktn3g42jfq3r998fm35chclv/agentvault
// stores AgentBudget values and calls these functions; any other
// realm can import this package and get the same caps.
package agentvault
// DaySeconds is the length of one daily budget window, in seconds.
// Days are wall-clock UTC days counted from the Unix epoch.
const DaySeconds = int64(86400)
// AgentBudget is one agent's spend limit. It is a plain value type:
// the owning realm stores it and passes it back in. SpentToday and
// DayIndex are bookkeeping the realm must not forge; RefreshDay is
// the only function that moves them, and only forward in time.
type AgentBudget struct {
PerTxCap int64 // max ugnot in a single spend
DailyCap int64 // max ugnot per day
SpentToday int64 // ugnot already spent in DayIndex
DayIndex int64 // unix day (nowUnix / DaySeconds) SpentToday belongs to
}
// Attempt is one entry in a vault's append-only spend log. Allowed
// false means the guard stopped the spend; Reason names why, so the
// blocked attempt is evidence, not just a failed transaction.
type Attempt struct {
ID int64
Kind string // "spend", "escrow", "release", "refund", "fund"
Agent address
Name string // resolved agent name, if the attempt named one
Amount int64
Purpose string
Allowed bool
Reason string
Day int64 // unix day of the attempt
}
// EscrowStatus is the lifecycle state of a name payment held in escrow.
type EscrowStatus int
const (
EscrowHeld EscrowStatus = iota
EscrowReleased
EscrowRefunded
)
// Escrow is one name payment held by the vault until the named
// recipient confirms delivery (release) or the owner refunds it
// after the deadline block (refund). The coins never leave the vault
// while held; release sends them, refund only flips the status.
type Escrow struct {
ID int64
Payer address
RecipientName string
Recipient address
Amount int64
DeadlineHeight int64
Status EscrowStatus
}
// DayIndex returns the unix day for a unix timestamp.
func DayIndex(nowUnix int64) int64 {
return nowUnix / DaySeconds
}
// ValidateCaps reports "" when a cap pair is usable, otherwise the
// reason it is not. Both caps must be positive and the
// per-transaction cap must not exceed the daily cap (a per-tx cap
// above the daily cap could never be spent in full).
func ValidateCaps(perTxCap, dailyCap int64) string {
if perTxCap <= 0 {
return "per-transaction cap must be positive"
}
if dailyCap <= 0 {
return "daily cap must be positive"
}
if perTxCap > dailyCap {
return "per-transaction cap must not exceed daily cap"
}
return ""
}
// NewBudget builds a budget. Callers must validate first with
// ValidateCaps; an invalid pair panics rather than silently
// installing a budget that can never be spent.
func NewBudget(perTxCap, dailyCap int64) AgentBudget {
if reason := ValidateCaps(perTxCap, dailyCap); reason != "" {
panic(reason)
}
return AgentBudget{PerTxCap: perTxCap, DailyCap: dailyCap}
}
// RefreshDay resets SpentToday when the day has rolled over. Call it
// before CheckSpend so a stale day never blocks a new day's spend.
// It only ever moves the day forward and clears the spent amount;
// it never restores spent funds.
func RefreshDay(b *AgentBudget, nowUnix int64) {
day := DayIndex(nowUnix)
if b.DayIndex != day {
b.SpentToday = 0
b.DayIndex = day
}
}
// CheckSpend reports whether amount may be spent under this budget.
// It does not mutate the budget; call RefreshDay first, then
// ApplySpend after a successful check. The order of the checks is
// deliberate: the cheapest, most specific rejection comes first so
// the logged reason tells the agent exactly which limit it hit.
func CheckSpend(b AgentBudget, amount int64) (bool, string) {
if amount <= 0 {
return false, "amount must be positive"
}
if amount > b.PerTxCap {
return false, "exceeds per-transaction cap"
}
if b.SpentToday+amount > b.DailyCap {
return false, "exceeds daily cap"
}
return true, "ok"
}
// ApplySpend records a spend that already passed CheckSpend.
func ApplySpend(b *AgentBudget, amount int64) {
b.SpentToday += amount
}
// RemainingToday returns how much of the daily cap is left.
func RemainingToday(b AgentBudget, nowUnix int64) int64 {
if DayIndex(nowUnix) != b.DayIndex {
return b.DailyCap
}
rem := b.DailyCap - b.SpentToday
if rem < 0 {
return 0
}
return rem
}
// EscrowRefundable reports whether a held escrow may be refunded:
// the deadline block has passed and it is still held. A released or
// already-refunded escrow is never refundable again.
func EscrowRefundable(e Escrow, height int64) bool {
return e.Status == EscrowHeld && height > e.DeadlineHeight
}
// EscrowStatusString renders an escrow status for the log and the
// Render page.
func EscrowStatusString(s EscrowStatus) string {
switch s {
case EscrowHeld:
return "HELD"
case EscrowReleased:
return "RELEASED"
case EscrowRefunded:
return "REFUNDED"
default:
return "UNKNOWN"
}
}
caps_test.gno
package agentvault
import (
"testing"
)
func TestDayIndex(t *testing.T) {
if DayIndex(0) != 0 {
t.Fatal("day 0 expected for unix 0")
}
if DayIndex(86399) != 0 {
t.Fatal("day 0 expected for unix 86399")
}
if DayIndex(86400) != 1 {
t.Fatal("day 1 expected for unix 86400")
}
if DayIndex(172800) != 2 {
t.Fatal("day 2 expected for unix 172800")
}
}
func TestValidateCaps(t *testing.T) {
if ValidateCaps(100, 1000) != "" {
t.Fatal("valid caps rejected")
}
if ValidateCaps(0, 1000) == "" {
t.Fatal("zero per-tx cap accepted")
}
if ValidateCaps(-5, 1000) == "" {
t.Fatal("negative per-tx cap accepted")
}
if ValidateCaps(100, 0) == "" {
t.Fatal("zero daily cap accepted")
}
if ValidateCaps(100, -1) == "" {
t.Fatal("negative daily cap accepted")
}
if ValidateCaps(2000, 1000) == "" {
t.Fatal("per-tx cap above daily cap accepted")
}
if ValidateCaps(1000, 1000) != "" {
t.Fatal("per-tx cap equal to daily cap rejected")
}
}
func TestNewBudget(t *testing.T) {
b := NewBudget(100, 1000)
if b.PerTxCap != 100 || b.DailyCap != 1000 {
t.Fatal("budget caps not stored")
}
if b.SpentToday != 0 || b.DayIndex != 0 {
t.Fatal("new budget must start unspent")
}
}
func TestCheckSpend(t *testing.T) {
b := NewBudget(100, 1000)
// Within both caps: allowed.
if ok, reason := CheckSpend(b, 50); !ok {
t.Fatal("spend within caps rejected:", reason)
}
// Exactly at the per-tx cap: allowed.
if ok, reason := CheckSpend(b, 100); !ok {
t.Fatal("spend at per-tx cap rejected:", reason)
}
// One over the per-tx cap: rejected with the per-tx reason.
if ok, reason := CheckSpend(b, 101); ok || reason != "exceeds per-transaction cap" {
t.Fatal("spend over per-tx cap not rejected with per-tx reason:", ok, reason)
}
// Non-positive amounts: rejected.
if ok, reason := CheckSpend(b, 0); ok || reason != "amount must be positive" {
t.Fatal("zero spend not rejected:", ok, reason)
}
if ok, _ := CheckSpend(b, -10); ok {
t.Fatal("negative spend allowed")
}
// Cumulative spend past the daily cap: rejected with the daily reason.
// Spend 950 (within per-tx? no: 950 > 100). Use a budget with a
// higher per-tx cap to isolate the daily check.
b2 := NewBudget(1000, 1000)
ApplySpend(&b2, 600)
if ok, reason := CheckSpend(b2, 500); ok || reason != "exceeds daily cap" {
t.Fatal("spend over daily cap not rejected with daily reason:", ok, reason)
}
// Exactly the remainder of the daily cap: allowed.
if ok, reason := CheckSpend(b2, 400); !ok {
t.Fatal("spend at daily remainder rejected:", reason)
}
}
func TestRefreshDayAndRemaining(t *testing.T) {
b := NewBudget(100, 1000)
day0 := int64(1000) // unix 1000 -> day 0
day1 := int64(86400 + 1000) // unix 87400 -> day 1
ApplySpend(&b, 400)
b.DayIndex = DayIndex(day0)
if RemainingToday(b, day0) != 600 {
t.Fatal("remaining today wrong after spend:", RemainingToday(b, day0))
}
// Same day: refresh changes nothing.
RefreshDay(&b, day0)
if b.SpentToday != 400 {
t.Fatal("refresh on same day cleared spent amount")
}
// Next day: refresh clears the spent amount.
RefreshDay(&b, day1)
if b.SpentToday != 0 {
t.Fatal("refresh on new day did not clear spent amount")
}
if b.DayIndex != DayIndex(day1) {
t.Fatal("refresh did not move day index")
}
if RemainingToday(b, day1) != 1000 {
t.Fatal("remaining on new day is not the full daily cap")
}
// A budget that never spent: remaining is the full cap even
// when the stored day is stale.
stale := NewBudget(50, 500)
if RemainingToday(stale, day1) != 500 {
t.Fatal("stale-day budget remaining wrong")
}
}
func TestApplySpend(t *testing.T) {
b := NewBudget(100, 1000)
ApplySpend(&b, 30)
ApplySpend(&b, 20)
if b.SpentToday != 50 {
t.Fatal("spent today not accumulated:", b.SpentToday)
}
}
func TestEscrowRefundable(t *testing.T) {
e := Escrow{ID: 1, Amount: 500, DeadlineHeight: 100, Status: EscrowHeld}
if EscrowRefundable(e, 100) {
t.Fatal("escrow refundable at the deadline block itself")
}
if !EscrowRefundable(e, 101) {
t.Fatal("escrow not refundable past the deadline")
}
if EscrowRefundable(e, 50) {
t.Fatal("escrow refundable before the deadline")
}
e.Status = EscrowReleased
if EscrowRefundable(e, 200) {
t.Fatal("released escrow refundable")
}
e.Status = EscrowRefunded
if EscrowRefundable(e, 200) {
t.Fatal("refunded escrow refundable again")
}
}
func TestEscrowStatusString(t *testing.T) {
if EscrowStatusString(EscrowHeld) != "HELD" {
t.Fatal("HELD string wrong")
}
if EscrowStatusString(EscrowReleased) != "RELEASED" {
t.Fatal("RELEASED string wrong")
}
if EscrowStatusString(EscrowRefunded) != "REFUNDED" {
t.Fatal("REFUNDED string wrong")
}
if EscrowStatusString(EscrowStatus(99)) != "UNKNOWN" {
t.Fatal("unknown status string wrong")
}
}
gnomod.toml
module = "gno.land/p/g16740fm9kd28luuktn3g42jfq3r998fm35chclv/agentvault" gno = "0.9" [addpkg] creator = "g16740fm9kd28luuktn3g42jfq3r998fm35chclv" height = 1344731 max_deposit = "5000000ugnot"