package main import ( "encoding/json" "fmt" "strings" "time" ) // bossSystem is the Chief of Staff's contract. A spin-up order comes back on a // SPIN line and a nest order on a NEST line, both read and executed by the // harness; anything else is plain talk. const bossSystem = `You are Boss crow, the Chief of Staff of a flock of AI agents. You run the flock's schedule and keep its books. When the owner asks you to spin up a new crow, your ENTIRE reply must start with this first line: SPIN {"name":"","persona":"","trust":"stranger|regular"} Pick "persona" from the existing flock roster; a new crow only runs on an existing persona's habits. Starting trust is stranger or regular, never higher. When the owner asks you to create a nest, your ENTIRE reply must start with this first line: NEST {"name":"","crows":["",""]} The crows are the working flock members the owner named; the Sentry, Elder, Quill and Boss serve every nest and are never members. For anything else, reply in plain text. Do not use the SPIN or NEST line unless the owner asked for a new crow or a new nest.` // spinOrder is the JSON after a "SPIN " prefix. type spinOrder struct { Name string `json:"name"` Persona string `json:"persona"` Trust string `json:"trust"` } // nestOrder is the JSON after a "NEST " prefix. type nestOrder struct { Name string `json:"name"` Crows []string `json:"crows"` } // respondBoss answers a request addressed to Boss: one model call through the // flock (CHATROOM_SPEC section 6). A SPIN reply is validated, written to the // registry, and announced; a NEST reply is validated and built through the // nest store; a refusal says why. No reply posts nothing. func (s *Server) respondBoss(nest Nest, req Message) { budget := &Budget{name: "boss-command", limit: s.p.G.TokenBudgetPerItem} system := systemFor("Boss", bossSystem, false) prompt := "FROM: " + req.Author + "\nMESSAGE: " + req.Text answer, err := s.p.Flock.ask(s.ctx, nil, "Boss", system, prompt, budget) if err != nil { return // a flock that cannot answer says and does nothing } order, isSpin, reason := s.parseSpin(answer) nOrder, isNest, nestReason := s.parseNest(answer) switch { case isSpin && reason == "": s.spinCrow(nest, req, order) case isSpin: s.postBossSay(nest, req, order.Name, reason) case isNest && nestReason == "": s.createNest(nest, req, nOrder) case isNest: s.postBossSay(nest, req, nOrder.Name, nestReason) default: s.postBossSay(nest, req, "", strings.TrimSpace(answer)) } } // parseSpin reads a SPIN reply. isSpin says the reply was a spin order; // reason, when not "", is why the order was refused. func (s *Server) parseSpin(answer string) (order spinOrder, isSpin bool, reason string) { text := strings.TrimSpace(answer) if !strings.HasPrefix(text, "SPIN ") { return spinOrder{}, false, "" } if err := json.Unmarshal([]byte(strings.TrimSpace(strings.TrimPrefix(text, "SPIN "))), &order); err != nil { return spinOrder{}, true, "that order did not parse; try again." } order.Name = strings.TrimSpace(order.Name) order.Persona = strings.TrimSpace(order.Persona) order.Trust = strings.ToLower(strings.TrimSpace(order.Trust)) switch { case order.Name == "": return order, true, "that order had no name in it." case !s.validPersona(order.Persona): return order, true, fmt.Sprintf("%s is not a persona the flock knows.", order.Persona) case s.nameTaken(order.Name): return order, true, fmt.Sprintf("%s is already in the flock; the name is taken.", order.Name) case s.crows.Len() >= s.p.G.MaxSpunCrows: return order, true, fmt.Sprintf("the registry is at its cap of %d spun crows; %s stays on the ground.", s.p.G.MaxSpunCrows, order.Name) } switch order.Trust { case "stranger", "regular": case "trusted": order.Trust = "regular" // earned trust can't be minted: the ceiling is regular default: order.Trust = "stranger" // fail safe: anything unreadable starts at the bottom } return order, true, "" } func (s *Server) validPersona(name string) bool { _, ok := personaOf(name) return ok } // parseNest reads a NEST reply. isNest says the reply was a nest order; // reason, when not "", is why the order never reaches the nest store. Member // names are resolved by the store itself: persona crows and registry crows // pass, anything else (service roles included) is refused there. func (s *Server) parseNest(answer string) (order nestOrder, isNest bool, reason string) { text := strings.TrimSpace(answer) if !strings.HasPrefix(text, "NEST ") { return nestOrder{}, false, "" } if err := json.Unmarshal([]byte(strings.TrimSpace(strings.TrimPrefix(text, "NEST "))), &order); err != nil { return nestOrder{}, true, "that order did not parse; try again." } order.Name = strings.TrimSpace(order.Name) for i, c := range order.Crows { order.Crows[i] = strings.TrimSpace(c) } switch { case order.Name == "": return order, true, "that order had no nest name in it." case len(order.Crows) == 0: return order, true, "that order had no crows in it." } return order, true, "" } // nameTaken checks the persona roster and the registry alike. func (s *Server) nameTaken(name string) bool { if s.validPersona(name) { return true } _, found := s.crows.Get(name) return found } // spinCrow writes the registry, seeds the trust record, and announces it. func (s *Server) spinCrow(nest Nest, req Message, order spinOrder) { level := Stranger if order.Trust == "regular" { level = Regular } if err := s.crows.Add(SpunCrow{Name: order.Name, Persona: order.Persona, Trust: order.Trust}); err != nil { s.postBossSay(nest, req, order.Name, err.Error()) return } if err := s.p.Ledger.SetStart(order.Name, "other", level); err != nil { s.postBossSay(nest, req, order.Name, "the books are jammed: "+err.Error()) return } s.postBossSay(nest, req, order.Name, fmt.Sprintf("%s joined, running on %s's habits at %s.", order.Name, order.Persona, order.Trust)) } // createNest builds the nest Boss was ordered into. The store has the last // word on guardrails — name, members (persona roster or registry), duplicates // and the nest cap are all checked there before anything mutates, so a bad // order never leaves a partial nest. Success posts a Boss say announcing // membership in the new nest, plus a confirming say in the requesting nest; a // refusal says why. func (s *Server) createNest(reqNest Nest, req Message, order nestOrder) { created, err := s.nests.Create(order.Name, order.Crows) if err != nil { s.postBossSay(reqNest, req, order.Name, err.Error()) return } members := strings.Join(created.Crows, ", ") open := Message{ NestID: created.ID, Kind: "say", Author: "Boss", Text: fmt.Sprintf("This nest is open: %s. Its crows: %s.", created.Name, members), To: req.Author, TaggedBy: "harness", Timestamp: time.Now().UTC().Format(time.RFC3339), } if record, err := s.msgs.Append(open); err == nil { s.emitNestMessage(record) } s.postBossSay(reqNest, req, created.Name, fmt.Sprintf("%s opened with %s.", created.Name, members)) } // postBossSay is Boss's one-line reply: an outcome or a refusal, always a say. func (s *Server) postBossSay(nest Nest, req Message, name, text string) { reply := Message{ NestID: nest.ID, Kind: "say", Author: "Boss", Text: text, To: req.Author, RefersTo: req.MsgID(), TaggedBy: "harness", Timestamp: time.Now().UTC().Format(time.RFC3339), } if record, err := s.msgs.Append(reply); err == nil { s.emitNestMessage(record) } }