package main // The highest number held per kind (hq issue 488): what the issue tracker's reserve starts and raises its decision // and research counters above (ADR 0275 rule 8, ADR 0290 rule 5), now that the records live here and hq's folders // empty. Every number held counts — accepted, proposed, reserved or withdrawn — since each is spent. import ( "context" "errors" "fmt" stdio "git.novox.be/novox/mesh-sdk/go" ) // HighestTool is the module's tool the issue tracker asks. const HighestTool = "knowledge_highest" // Highest is the highest number held of each kind, and how many records are held. type Highest struct { Decision int `json:"decision"` Research int `json:"research"` Records int `json:"records"` Says string `json:"says"` } // HighestHeld is the highest number held per kind. A knowledge base that holds no record answers none: a counter // started from it would start at 1. func (k *KB) HighestHeld() (*Highest, error) { all, err := k.Store.All(context.Background()) if err != nil { return nil, fmt.Errorf("the knowledge base cannot be read (%v); it answers no highest number", err) } h := &Highest{Records: len(all)} for _, e := range all { switch r := e.Current; r.Kind { case KindDecision: h.Decision = max(h.Decision, r.Number) case KindResearch: h.Research = max(h.Research, r.Number) } } if h.Records == 0 { return nil, errors.New("the knowledge base holds no record yet (it is imported from hq once): it answers no highest number") } h.Says = fmt.Sprintf("The highest numbers held, of %d records, every number spent counted: decision %04d, research %03d.", h.Records, h.Decision, h.Research) return h, nil } func highestTool(k *KB) stdio.Tool { return stdio.Tool{Name: HighestTool, Description: "The highest decision number and the highest research number the knowledge base holds, every number spent " + "counted: accepted, proposed, reserved or withdrawn. The issue tracker starts and raises its counters above them. " + "Refused while the knowledge base holds no record, or cannot be read: a number is never guessed.", Input: schema(nil, map[string]any{}), Run: func(m map[string]any) (any, error) { if _, err := readArgs(HighestTool, m); err != nil { return nil, err } return k.HighestHeld() }} }