rule

package
v0.146.2 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2026 License: MIT Imports: 12 Imported by: 0 modules, 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 rule is the gofer RULE library (JOB-06①, design §一.1): a rule is a short, MANDATORY piece of discipline the server owns and injects at the TOP of every job prompt that binds it ("改文件只用 apply_patch", "测试先写先提交"). It is deliberately the opposite of a skill (internal/skill): a skill is optional reading material the agent may open, a rule is text that is always placed in front of the agent.

Storage mirrors the skill library on purpose: <config-dir>/rules/<name>.md plus a metadata index (this package's Repo seam, implemented by internal/jobstore). The text lives in a file so an operator can edit it with any editor and back it up with the rest of the config; the index is only what `agent rule ls` and the binding resolver need.

It imports the standard library plus internal/template's frontmatter splitter — no gofer package depends on it in return, so the job service (which injects) and the HTTP/CLI surfaces (which manage) can both use it without an import cycle (G022).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("rule not found")
	ErrInvalid  = errors.New("invalid rule")
)

The failures a caller can act on. Everything else is an I/O error wrapped with context. They are separate sentinels because the HTTP/CLI surfaces map them to different statuses/messages.

Functions

func BodyOf

func BodyOf(raw string) string

BodyOf strips the optional `---`-delimited YAML frontmatter from rule text and trims the result. A file without a head (or with a malformed one) is all body.

Types

type Repo

type Repo interface {
	UpsertRule(Rule) error
	GetRule(name string) (Rule, bool, error)
	ListRules() ([]Rule, error)
	DeleteRule(name string) error
}

Repo is the rule index: one row per rule. It is an interface so the library can be tested (and stored) without SQLite; internal/jobstore implements the same column set.

type Rule

type Rule struct {
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`
	Size        int64  `json:"size,omitempty"`
	// SHA256 is the digest of the stored file VERBATIM (frontmatter included), which
	// is what a job row records as "the version that was injected" — two rules whose
	// bytes differ never share a digest, so `job show` can answer "which text did this
	// run actually carry?" against the library as it is today.
	SHA256    string `json:"sha256,omitempty"`
	UpdatedAt int64  `json:"updated_at,omitempty"`
	UpdatedBy string `json:"updated_by,omitempty"`
}

Rule is one library entry's INDEX row: what `agent rule ls` lists, what the binding resolver validates a name against, and what a job row records as the version it injected. The text itself stays on disk (see Store.Read / Store.Body), exactly like a skill's files.

The optional `agents:` frontmatter key a rule may carry is a human-facing hint ("this one is for the codex agent") and is deliberately NOT part of the index: it changes no behaviour, and an index column that nothing reads is a column that drifts.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the rule library: a root directory of <name>.md files plus an index.

func NewStore

func NewStore(root string, repo Repo) (*Store, error)

NewStore opens (creating if needed) the library at root. Callers pass <config-dir>/rules so the library is backed up with the config (design §一.1).

func (*Store) Body

func (s *Store) Body(name string) (string, error)

Body returns the text that is INJECTED into a prompt: the file with its optional YAML frontmatter stripped, so the metadata head never reaches the agent twice.

func (*Store) Get

func (s *Store) Get(name string) (Rule, bool, error)

Get returns the indexed rule. The bool is false (with a nil error) when the library has no such rule.

func (*Store) List

func (s *Store) List() ([]Rule, error)

List returns every indexed rule, in the index's order.

func (*Store) Path

func (s *Store) Path(name string) string

Path is the on-disk file of one rule. It returns "" for a name that fails validation, so a caller that ignores the index (or a typo) can never be handed a path outside the library root.

func (*Store) Read

func (s *Store) Read(name string) (string, error)

Read returns the rule's file verbatim, frontmatter included (what `rule show` prints). A valid name with no file is ErrNotFound: the index and the tree disagree, and the caller must not silently get an empty rule.

func (*Store) Remove

func (s *Store) Remove(name string) error

Remove deletes the index row first and the file second — the same order (and the same reason) as the skill store: a crash between the two steps leaves an orphan file that is invisible to `rule ls` and reused by the next Set of the same name, instead of an index row pointing at a file that is gone.

func (*Store) Root

func (s *Store) Root() string

Root is the library root, absolute when the caller passed one.

func (*Store) Set

func (s *Store) Set(name string, content []byte, caller string) (Rule, error)

Set writes <name>.md (atomically: temp file + rename, so a crash never leaves a half-written rule the index points at) and then updates the index row. A RE-SET of an existing name replaces it — that is what `agent rule set` is for.

The file is written first and the index second because the index is what the binding resolver validates against: a crash between the two leaves a file that no binding can name yet (harmless, repaired by the next Set), never an index row pointing at a missing file.

Jump to

Keyboard shortcuts

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