feat(boss): Chief of Staff Boss + spun-crow registry with cap (#42)
This commit is contained in:
1 parent
ccb0e503c5
commit
663c8a19d8
10 files changed
+329
-20
No files matched your search
+124
@@ -0,0 +1,124 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// A SpunCrow is a crow the Boss created: its own name, the persona whose
|
||||
// habits it runs on, and the trust it started at ("stranger" or "regular").
|
||||
// Once created it is an ordinary crow: the same ledger and guardrails apply.
|
||||
type SpunCrow struct {
|
||||
Name string `json:"name"`
|
||||
Persona string `json:"persona"`
|
||||
Trust string `json:"trust"`
|
||||
}
|
||||
|
||||
// CrowStore is the spun-crow registry. It follows the atomic-save style of
|
||||
// NestStore: one JSON file, written to a temp path and renamed.
|
||||
type CrowStore struct {
|
||||
path string
|
||||
mu sync.Mutex
|
||||
spun []SpunCrow
|
||||
}
|
||||
|
||||
func LoadCrowStore(path string) (*CrowStore, error) {
|
||||
s := &CrowStore{path: path}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return nil, err
|
||||
}
|
||||
if err == nil {
|
||||
if err := json.Unmarshal(data, &s.spun); err != nil {
|
||||
return nil, fmt.Errorf("crows file is unreadable: %w", err)
|
||||
}
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// All returns every spun crow, sorted by name.
|
||||
func (s *CrowStore) All() []SpunCrow {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
out := make([]SpunCrow, len(s.spun))
|
||||
copy(out, s.spun)
|
||||
slices.SortFunc(out, func(a, b SpunCrow) int { return strings.Compare(a.Name, b.Name) })
|
||||
return out
|
||||
}
|
||||
|
||||
// Get finds a spun crow by name.
|
||||
func (s *CrowStore) Get(name string) (SpunCrow, bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, c := range s.spun {
|
||||
if c.Name == name {
|
||||
return c, true
|
||||
}
|
||||
}
|
||||
return SpunCrow{}, false
|
||||
}
|
||||
|
||||
// Add registers a new spun crow and saves. A name already in the registry is
|
||||
// refused; any other error means the save failed.
|
||||
func (s *CrowStore) Add(c SpunCrow) error {
|
||||
c.Name = strings.TrimSpace(c.Name)
|
||||
if c.Name == "" {
|
||||
return fmt.Errorf("a spun crow needs a name")
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for _, e := range s.spun {
|
||||
if strings.EqualFold(e.Name, c.Name) {
|
||||
return fmt.Errorf("%s is already in the registry", c.Name)
|
||||
}
|
||||
}
|
||||
s.spun = append(s.spun, c)
|
||||
if err := s.save(); err != nil {
|
||||
s.spun = s.spun[:len(s.spun)-1]
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Len is how many crows the registry holds.
|
||||
func (s *CrowStore) Len() int {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return len(s.spun)
|
||||
}
|
||||
|
||||
// save writes atomically. The caller holds s.mu.
|
||||
func (s *CrowStore) save() error {
|
||||
data, err := json.MarshalIndent(s.spun, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(s.path), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
tmp := s.path + ".tmp"
|
||||
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Rename(tmp, s.path)
|
||||
}
|
||||
|
||||
// getCrowRegistry lists the spun crows (EVENTS.md: the registry lives here,
|
||||
// never the persona crows). A server with no registry reads as empty.
|
||||
func (s *Server) getCrowRegistry(w http.ResponseWriter, r *http.Request) {
|
||||
var spun []SpunCrow
|
||||
if s.crows != nil {
|
||||
spun = s.crows.All()
|
||||
}
|
||||
out := make([]map[string]string, 0, len(spun))
|
||||
for _, c := range spun {
|
||||
out = append(out, map[string]string{"name": c.Name, "persona": c.Persona, "trust": c.Trust})
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"crows": out})
|
||||
}
|
||||
Reference in new issue
Block a user