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 ¶
- Variables
- func BodyOf(raw string) string
- type Repo
- type Rule
- type Store
- func (s *Store) Body(name string) (string, error)
- func (s *Store) Get(name string) (Rule, bool, error)
- func (s *Store) List() ([]Rule, error)
- func (s *Store) Path(name string) string
- func (s *Store) Read(name string) (string, error)
- func (s *Store) Remove(name string) error
- func (s *Store) Root() string
- func (s *Store) Set(name string, content []byte, caller string) (Rule, error)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
Get returns the indexed rule. The bool is false (with a nil error) when the library has no such rule.
func (*Store) Path ¶
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 ¶
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 ¶
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) Set ¶
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.