guard

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 15 Imported by: 1 module, 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 guard is the Guardrails tab's data: which gates refused what, and whether the lessons that became gates stopped recurring.

A gate refusal is a tool result the hook runner turned into an error: its text starts with "<Event>:<Tool> hook error: " (or is the hook's own deny JSON). The attribution table below was built from the real refusals of a week of transcripts; a refusal that matches no row is "other gate", counted and shown, never dropped.

Index

Constants

View Source
const (
	GuardBash  = "guard-bash"
	Replystamp = "replystamp"
	Agentsync  = "agentsync gate"
	Recur      = "recur"
	Mutsweep   = "mutsweep"
	Modtidy    = "modtidy"
	Modelgate  = "modelgate"
	Ctxgate    = "ctxgate"
	Ghbyhand   = "ghbyhand"
	Paritychk  = "paritycheck"
	Prbody     = "prbody"
	Quotaclaim = "quotaclaim"
	Other      = "other gate"
)

The gates a refusal is attributed to. Other holds what no rule recognises.

View Source
const (
	// Weeks is how many weeks the trend charts show.
	Weeks = 12
	// SilentWeeks is how long a landed gate must have made no refusal, nor seen a
	// recurrence, to be flagged silent.
	SilentWeeks = 4
)
View Source
const (
	// BurstN refusals of one gate to one session within BurstWithin is a burst: the
	// session is retrying what the gate will keep refusing.
	BurstN      = 3
	BurstWithin = 15 * time.Minute
	// KeepExcerpts is how many of a session's latest refusals are kept to show.
	KeepExcerpts = 5
)

The thresholds of the refusals view.

View Source
const MaxExcerpt = 200

MaxExcerpt bounds an excerpt, in runes.

View Source
const MinInterval = 10 * time.Minute

MinInterval is how seldom recur is run: its answer is a reading of memory files.

Variables

Gates lists every gate in the stack order of the chart; Other is last.

Functions

func Attribute

func Attribute(body string) string

Attribute names the gate that wrote a refusal body.

func Build

func Build(in Input) *model.Guardrails

Build assembles the tab's data.

func BuildRefusals added in v0.23.0

func BuildRefusals(in RefusalInput) *model.Refusals

BuildRefusals builds the view: session by gate counts over the window, the latest refusals of each session, and the cross-check with replystamp's log.

func DenyLogPath added in v0.23.0

func DenyLogPath() (string, error)

DenyLogPath is where replystamp keeps its denial log: its state directory under the user's config directory.

func Excerpt added in v0.23.0

func Excerpt(body, home string) string

Excerpt is what is kept of a refusal to show beside its count: the gate's own words only. It is cut before the command the gate quotes ("it matched ..."), which is the tool input and never kept, passed through gitsafe's redaction so a token-shaped string does not reach the screen, has its home directory written "~", has its white space collapsed and is capped at MaxExcerpt runes.

func Exec

func Exec(ctx context.Context, memDir string) ([]byte, error)

Exec is the Runner that runs the recur binary on PATH, with fixed arguments and no shell, under a timeout.

func MemoryDir

func MemoryDir(given, claudeDir string) string

MemoryDir finds the memory directory to read: the one given, else the one under claudeDir/projects whose recur ledger was written last. "" when there is none.

func Named

func Named(text string) []string

Named lists the gates a ledger text names (a lesson's gate is written in words: "replystamp --pretool", "mutsweep -hook"), so a lesson's refusals can be found. "recur" and "agentsync" alone name too many other things to count: they need their flag, "recur --hook" or "agentsync gate".

func Refusal

func Refusal(text string, isError bool) (body string, ok bool)

Refusal reports whether a tool result's text is a gate refusal, and its body: the text after the hook prefix. Only a result the runner marked as an error, whose text STARTS like a refusal, is one: a file or a log that merely mentions a hook is not.

func StopGate added in v0.23.0

func StopGate(command string) string

StopGate names the gate behind a Stop hook's blocking error from the hook's command line: the base name of its program ("~/.local/bin/quotaclaim --x" is quotaclaim). A program that is no known gate is Other.

Types

type Count

type Count struct {
	Hour int64
	Gate string
	N    int64
}

Count is the refusals of one gate in one hour: Hour is the Unix second the hour starts at.

type Event added in v0.23.0

type Event struct {
	T       int64  `json:"t"` // Unix seconds
	Session string `json:"s"`
	Gate    string `json:"g"`
	Text    string `json:"x,omitempty"` // the gate's words, redacted and capped (see Excerpt)
}

Event is one refusal read from a transcript: a gate's refusal of a tool call, or a Stop hook's blocking error.

type Input

type Input struct {
	Now time.Time
	// Rec is what recur answered; RecErr is why it could not be read, "" when it
	// could (and then Rec is its answer, however empty). A value and not a
	// pointer: there is no nil to forget.
	Rec    Recurrence
	RecErr string
	// Refusals are the refusals the transcripts hold; Scanned is whether they were
	// scanned at all (false: not measured, which is not zero).
	Refusals []Count
	Scanned  bool
}

Input is what Build needs.

type Lesson

type Lesson struct {
	Name        string `json:"name"`
	Recurrences []struct {
		Date         *string `json:"date"`
		YearInferred bool    `json:"yearInferred"`
		Line         string  `json:"line"`
	} `json:"recurrences"`
	Ledger struct {
		Decision  string  `json:"decision"`
		Text      string  `json:"text"`
		WrittenAt *string `json:"writtenAt"`
	} `json:"ledger"`
}

Lesson is one memory with its recurrences and ledger entry.

type Recurrence

type Recurrence struct {
	Lessons []Lesson `json:"lessons"`
	Totals  struct {
		Lessons    int `json:"lessons"`
		Unreadable int `json:"unreadable"`
	} `json:"totals"`
}

Recurrence is `recur --memory <dir> --json`: the lessons that recorded a recurrence, what the ledger decided about each, and the totals. Only the fields the tab shows are decoded.

func Parse

func Parse(b []byte) (Recurrence, error)

Parse decodes recur's JSON; it refuses what has no "lessons" key, so an answer that is not recur's (an error page, a usage text) is a failure and not an empty list.

type RefusalInput added in v0.23.0

type RefusalInput struct {
	Now        time.Time
	Events     []Event
	Streaks    []Streak
	Scanned    bool // a pass over the transcripts ended
	Unreadable int  // transcripts that could not be opened
	// LogPath is replystamp's denial log; LogErr says why there is none to name.
	LogPath, LogErr string
}

RefusalInput is what the refusals view is built from.

type Runner

type Runner func(ctx context.Context, memDir string) ([]byte, error)

Runner runs recur on a memory directory and returns its standard output. A non-zero exit status is not an error here when the output parses: recur exits 1 to report an unaccounted recurrence, and that is an answer.

type Source

type Source struct {
	Dir string // "" means no memory directory could be found
	Run Runner
	// contains filtered or unexported fields
}

Source reads recur at most every MinInterval and keeps the last answer. A failure is kept as the reason, never as an empty answer, so the tab says "not measured" and not "0".

func (*Source) Get

func (s *Source) Get(ctx context.Context, now time.Time) (rec Recurrence, err string)

Get is the last answer, running recur first when it has not been read for MinInterval. err is "" when rec is the answer; otherwise rec is empty and err says why, so nothing shows an old answer as current.

type Streak added in v0.23.0

type Streak struct {
	Session string
	N       int
	Last    int64
}

Streak is how many tool results at the end of a session's transcript were refusals in a row, and when the latest was.

Jump to

Keyboard shortcuts

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