| 1 | // Package housekeeping is Equipo's tidying loop, as declarations: a |
| 2 | // registry of small detectors, each a pure function from a read-only view |
| 3 | // of the store to findings, and the limits that keep the loop bounded |
| 4 | // (spec 2026-11-08-equipo-housekeeping.md, §§2 to 4). Nothing here |
| 5 | // writes: the chat store claims each finding in its ledger and acts under |
| 6 | // the housekeeping principal, which is where every rule is enforced. |
| 7 | // |
| 8 | // The loop is team-scoped from the start. Every finding names a subject |
| 9 | // and the subject's owner; a team subject has no owner. Adding a detector |
| 10 | // is a registry entry and a fixture, which is how detectors over other |
| 11 | // facts (a chore overdue, a decision to revisit) arrive later. |
| 12 | package housekeeping |
| 13 | |
| 14 | import ( |
| 15 | "errors" |
| 16 | "fmt" |
| 17 | "slices" |
| 18 | "time" |
| 19 | ) |
| 20 | |
| 21 | // Classes say what a detector's finding may become (spec §3). |
| 22 | const ( |
| 23 | // ClassAct: deterministic, within the owner's own subjects, |
| 24 | // reversible; a receipt with Undo. |
| 25 | ClassAct = "act" |
| 26 | // ClassLink: a quiet reference on the subject, nothing else. |
| 27 | ClassLink = "link" |
| 28 | // ClassPropose: a suggestion, never an action (slice 2). |
| 29 | ClassPropose = "propose" |
| 30 | // ClassAsk: one Needs you card, answered by the owner (slice 4). |
| 31 | ClassAsk = "ask" |
| 32 | ) |
| 33 | |
| 34 | // Judgement is whether a detector needs a model's judgement. |
| 35 | const ( |
| 36 | JudgementNone = "none" |
| 37 | JudgementModel = "model" |
| 38 | ) |
| 39 | |
| 40 | // Subject kinds (spec §1). |
| 41 | const ( |
| 42 | SubjectTask = "task" |
| 43 | SubjectProject = "project" |
| 44 | SubjectConnection = "connection" |
| 45 | SubjectNote = "notebook_entry" |
| 46 | SubjectPerson = "person" |
| 47 | SubjectTeam = "team" |
| 48 | ) |
| 49 | |
| 50 | // Triggers: event types a detector runs on, plus the daily sweep. |
| 51 | const ( |
| 52 | TriggerRecordTask = "record_task" |
| 53 | TriggerSweep = "sweep" |
| 54 | ) |
| 55 | |
| 56 | // Proposal actions the store knows how to carry out. |
| 57 | const ( |
| 58 | ActionCombine = "combine" |
| 59 | ActionLink = "link" |
| 60 | ) |
| 61 | |
| 62 | // Subject is what a finding is about. |
| 63 | type Subject struct { |
| 64 | Kind string `json:"kind"` |
| 65 | ID string `json:"id"` |
| 66 | } |
| 67 | |
| 68 | // Cost is what an action reserves from the day's budget, and the shape |
| 69 | // of the day's limits. |
| 70 | type Cost struct { |
| 71 | Turns int64 `json:"turns"` |
| 72 | Tokens int64 `json:"tokens"` |
| 73 | Retries int64 `json:"retries"` |
| 74 | // Rounds are automatic Engineering rounds (needs-you triage). |
| 75 | Rounds int64 `json:"rounds,omitempty"` |
| 76 | } |
| 77 | |
| 78 | // Zero reports whether the cost spends nothing. |
| 79 | func (c Cost) Zero() bool { return c.Turns == 0 && c.Tokens == 0 && c.Retries == 0 && c.Rounds == 0 } |
| 80 | |
| 81 | // Add is the sum of two costs. |
| 82 | func (c Cost) Add(o Cost) Cost { |
| 83 | return Cost{Turns: c.Turns + o.Turns, Tokens: c.Tokens + o.Tokens, Retries: c.Retries + o.Retries, Rounds: c.Rounds + o.Rounds} |
| 84 | } |
| 85 | |
| 86 | // Rate bounds a detector's findings: at most PerSubject per subject and |
| 87 | // PerTeam across the team within Period. Zero is unbounded. |
| 88 | type Rate struct { |
| 89 | PerSubject int |
| 90 | PerTeam int |
| 91 | Period time.Duration |
| 92 | } |
| 93 | |
| 94 | // Detector is one declared unit of the registry. |
| 95 | type Detector struct { |
| 96 | ID string |
| 97 | Subject string |
| 98 | Class string |
| 99 | Triggers []string |
| 100 | Judgement string |
| 101 | Rate Rate |
| 102 | // Cost is what one action reserves; zero for deterministic actions. |
| 103 | Cost Cost |
| 104 | Detect func(View) []Finding |
| 105 | } |
| 106 | |
| 107 | // Finding is something a detector noticed. Key is its identity in the |
| 108 | // ledger (duplicate:tkA:tkB); Owner the subject's owner, "" for a team |
| 109 | // subject. Evidence is structured fields, never model prose. |
| 110 | type Finding struct { |
| 111 | Detector string `json:"detector"` |
| 112 | Subject Subject `json:"subject"` |
| 113 | Owner string `json:"owner"` |
| 114 | SubjectRevision int64 `json:"subject_revision"` |
| 115 | Key string `json:"key"` |
| 116 | Evidence Evidence `json:"evidence"` |
| 117 | Proposal Proposal `json:"proposal"` |
| 118 | } |
| 119 | |
| 120 | // Evidence is what the receipt and the view show of a finding. |
| 121 | type Evidence struct { |
| 122 | Tasks []TaskRef `json:"tasks"` |
| 123 | Similarity float64 `json:"similarity"` |
| 124 | Shared int `json:"shared"` |
| 125 | } |
| 126 | |
| 127 | // TaskRef names a task in evidence. |
| 128 | type TaskRef struct { |
| 129 | ID string `json:"id"` |
| 130 | Title string `json:"title"` |
| 131 | Creator string `json:"creator"` |
| 132 | } |
| 133 | |
| 134 | // Proposal is the action a finding asks for: combine Task into Into, or |
| 135 | // link Task to Into. |
| 136 | type Proposal struct { |
| 137 | Action string `json:"action"` |
| 138 | Task string `json:"task"` |
| 139 | Into string `json:"into"` |
| 140 | } |
| 141 | |
| 142 | // Registry is the declared set of detectors, in the order they run. |
| 143 | type Registry struct { |
| 144 | list []Detector |
| 145 | } |
| 146 | |
| 147 | var ( |
| 148 | // ErrModelMeansPropose: a detector that needs a model's judgement may |
| 149 | // only propose (spec §2). |
| 150 | ErrModelMeansPropose = errors.New("housekeeping: a judgement detector may only propose") |
| 151 | errDeclaration = errors.New("housekeeping: bad detector declaration") |
| 152 | ) |
| 153 | |
| 154 | var classes = []string{ClassAct, ClassLink, ClassPropose, ClassAsk} |
| 155 | |
| 156 | // New checks every declaration and refuses the registry if any is wrong. |
| 157 | func New(ds ...Detector) (*Registry, error) { |
| 158 | seen := map[string]bool{} |
| 159 | for _, d := range ds { |
| 160 | switch { |
| 161 | case d.ID == "" || seen[d.ID]: |
| 162 | return nil, fmt.Errorf("%w: id %q", errDeclaration, d.ID) |
| 163 | case !slices.Contains(classes, d.Class): |
| 164 | return nil, fmt.Errorf("%w: %s class %q", errDeclaration, d.ID, d.Class) |
| 165 | case d.Judgement != JudgementNone && d.Judgement != JudgementModel: |
| 166 | return nil, fmt.Errorf("%w: %s judgement %q", errDeclaration, d.ID, d.Judgement) |
| 167 | case d.Judgement == JudgementModel && d.Class != ClassPropose: |
| 168 | return nil, fmt.Errorf("%w: %s", ErrModelMeansPropose, d.ID) |
| 169 | case d.Class == ClassAct && d.Judgement != JudgementNone: |
| 170 | return nil, fmt.Errorf("%w: %s", ErrModelMeansPropose, d.ID) |
| 171 | case d.Detect == nil || len(d.Triggers) == 0 || d.Subject == "": |
| 172 | return nil, fmt.Errorf("%w: %s", errDeclaration, d.ID) |
| 173 | } |
| 174 | seen[d.ID] = true |
| 175 | } |
| 176 | return &Registry{list: slices.Clone(ds)}, nil |
| 177 | } |
| 178 | |
| 179 | // MustNew is New for declarations known at build time. |
| 180 | func MustNew(ds ...Detector) *Registry { |
| 181 | r, err := New(ds...) |
| 182 | if err != nil { |
| 183 | panic(err) |
| 184 | } |
| 185 | return r |
| 186 | } |
| 187 | |
| 188 | // Default is slice 1's registry: duplicates combined or linked. |
| 189 | func Default() *Registry { return MustNew(DuplicateSameCreator(), DuplicatePossible()) } |
| 190 | |
| 191 | // Detectors lists the declarations in order. |
| 192 | func (r *Registry) Detectors() []Detector { return slices.Clone(r.list) } |
| 193 | |
| 194 | // Get finds a detector by id. |
| 195 | func (r *Registry) Get(id string) (Detector, bool) { |
| 196 | for _, d := range r.list { |
| 197 | if d.ID == id { |
| 198 | return d, true |
| 199 | } |
| 200 | } |
| 201 | return Detector{}, false |
| 202 | } |
| 203 | |
| 204 | // Run is every finding of the detectors that run on trigger, in registry |
| 205 | // order, each stamped with its detector's id. Pause and a detector |
| 206 | // disabled for a person are the store's to apply, per finding's owner. |
| 207 | func (r *Registry) Run(trigger string, v View) []Finding { |
| 208 | var out []Finding |
| 209 | for _, d := range r.list { |
| 210 | if !slices.Contains(d.Triggers, trigger) { |
| 211 | continue |
| 212 | } |
| 213 | for _, f := range d.Detect(v) { |
| 214 | f.Detector = d.ID |
| 215 | out = append(out, f) |
| 216 | } |
| 217 | } |
| 218 | return out |
| 219 | } |
| 220 | |