| 1 | package housekeeping |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "time" |
| 6 | "unicode" |
| 7 | ) |
| 8 | |
| 9 | // Detector ids. |
| 10 | const ( |
| 11 | DuplicateSameCreatorID = "duplicate_same_creator" |
| 12 | DuplicatePossibleID = "duplicate_possible" |
| 13 | ) |
| 14 | |
| 15 | // Thresholds for duplicates, beside the detectors that use them (spec §2: |
| 16 | // the similarity function is record_task's). A pair matches when the |
| 17 | // titles share MatchTitle of the smaller one's significant words and at |
| 18 | // least MatchTitleWords, or title and brief share MatchBrief and |
| 19 | // MatchBriefWords. It reads so much alike that Equipo combines it when |
| 20 | // the titles share CombineTitle and CombineTitleWords, or title and brief |
| 21 | // CombineBrief and CombineBriefWords. These are the values UX1 shipped on |
| 22 | // staging; the fixture pins every edge. |
| 23 | const ( |
| 24 | MatchTitle = 0.6 |
| 25 | MatchTitleWords = 2 |
| 26 | MatchBrief = 0.5 |
| 27 | MatchBriefWords = 3 |
| 28 | CombineTitle = 0.75 |
| 29 | CombineTitleWords = 3 |
| 30 | CombineBrief = 0.7 |
| 31 | CombineBriefWords = 5 |
| 32 | ) |
| 33 | |
| 34 | // View is the read-only facts a detector reads. For duplicates: one |
| 35 | // project's open tasks (not cancelled, not done, not folded, no cancel |
| 36 | // asked) oldest first, and the pairs someone kept apart. |
| 37 | type View struct { |
| 38 | Project string `json:"project"` |
| 39 | Tasks []TaskFact `json:"tasks"` |
| 40 | Distinct [][2]string `json:"distinct"` |
| 41 | } |
| 42 | |
| 43 | // TaskFact is one open task as a detector sees it. Stopped: its last step |
| 44 | // failed. Busy: a step is queued or running. Carded: an open card other |
| 45 | // than its task card, a decision still to make (it blocks closing this |
| 46 | // task, never keeping it). Decided: the task's newest merge card, the one |
| 47 | // for its current candidate, was answered by a person (only the task's |
| 48 | // creator answers one). Hidden: its creator is a test identity. |
| 49 | type TaskFact struct { |
| 50 | ID string `json:"id"` |
| 51 | Title string `json:"title"` |
| 52 | Brief string `json:"brief"` |
| 53 | Creator string `json:"creator"` |
| 54 | Stage string `json:"stage"` |
| 55 | Revision int64 `json:"revision"` |
| 56 | CreatedAt time.Time `json:"created_at"` |
| 57 | DuplicateOf string `json:"duplicate_of"` |
| 58 | Stopped bool `json:"stopped"` |
| 59 | Busy bool `json:"busy"` |
| 60 | Carded bool `json:"carded"` |
| 61 | Decided bool `json:"decided"` |
| 62 | Hidden bool `json:"hidden"` |
| 63 | // Kind is the task's kind (wire.NormalKind): an exploration and a |
| 64 | // change are never duplicates. |
| 65 | Kind string `json:"kind,omitempty"` |
| 66 | } |
| 67 | |
| 68 | // DuplicateSameCreator combines two open tasks one person filed in one |
| 69 | // project that read very much alike: the weaker into the one furthest |
| 70 | // along. Act and tell: a receipt with Undo. |
| 71 | func DuplicateSameCreator() Detector { |
| 72 | return Detector{ID: DuplicateSameCreatorID, Subject: SubjectTask, Class: ClassAct, Triggers: []string{TriggerRecordTask, TriggerSweep}, Judgement: JudgementNone, |
| 73 | Detect: func(v View) []Finding { return duplicates(v, true) }} |
| 74 | } |
| 75 | |
| 76 | // DuplicatePossible names, on the weaker task, an open task it reads like |
| 77 | // whenever the pair is not combined: weaker matches, another person's |
| 78 | // task, or a pair that cannot be combined now. Link only. |
| 79 | func DuplicatePossible() Detector { |
| 80 | return Detector{ID: DuplicatePossibleID, Subject: SubjectTask, Class: ClassLink, Triggers: []string{TriggerRecordTask, TriggerSweep}, Judgement: JudgementNone, |
| 81 | Detect: func(v View) []Finding { return duplicates(v, false) }} |
| 82 | } |
| 83 | |
| 84 | // stageRank orders how far along a task is. |
| 85 | var stageRank = map[string]int{"noted": 0, "investigated": 1, "in_progress": 2, "in_review": 3, "ready_to_merge": 4, "merging": 5} |
| 86 | |
| 87 | // stronger reports whether a is further along than b: a later stage, then |
| 88 | // the one a person decided on, then not stopped, then the older one, then |
| 89 | // the smaller id. A person's decision outranks age (staging's Woodstar |
| 90 | // pair, 2026-11-11: the older, undecided task kept the work and the one |
| 91 | // Paul had answered Wait for a Mac on was closed). |
| 92 | func stronger(a, b TaskFact) bool { |
| 93 | if ra, rb := stageRank[a.Stage], stageRank[b.Stage]; ra != rb { |
| 94 | return ra > rb |
| 95 | } |
| 96 | if a.Decided != b.Decided { |
| 97 | return a.Decided |
| 98 | } |
| 99 | if a.Stopped != b.Stopped { |
| 100 | return !a.Stopped |
| 101 | } |
| 102 | if !a.CreatedAt.Equal(b.CreatedAt) { |
| 103 | return a.CreatedAt.Before(b.CreatedAt) |
| 104 | } |
| 105 | return a.ID < b.ID |
| 106 | } |
| 107 | |
| 108 | // Similarity is how alike two tasks read: whether they match, whether |
| 109 | // they read so much alike that they may be combined, and the overlap and |
| 110 | // shared words behind it. |
| 111 | func Similarity(aTitle, aBrief, bTitle, bBrief string) (match, high bool, score float64, shared int) { |
| 112 | ts, tn := Overlap(Significant(aTitle), Significant(bTitle)) |
| 113 | bs, bn := Overlap(Significant(aTitle+" "+aBrief), Significant(bTitle+" "+bBrief)) |
| 114 | match = ts >= MatchTitle && tn >= MatchTitleWords || bs >= MatchBrief && bn >= MatchBriefWords |
| 115 | high = match && (ts >= CombineTitle && tn >= CombineTitleWords || bs >= CombineBrief && bn >= CombineBriefWords) |
| 116 | score, shared = ts, tn |
| 117 | if bs > ts { |
| 118 | score, shared = bs, bn |
| 119 | } |
| 120 | return match, high, score, shared |
| 121 | } |
| 122 | |
| 123 | func duplicates(v View, combine bool) []Finding { |
| 124 | apart := map[[2]string]bool{} |
| 125 | for _, p := range v.Distinct { |
| 126 | apart[[2]string{min(p[0], p[1]), max(p[0], p[1])}] = true |
| 127 | } |
| 128 | var out []Finding |
| 129 | for i := 0; i < len(v.Tasks); i++ { |
| 130 | for j := i + 1; j < len(v.Tasks); j++ { |
| 131 | a, b := v.Tasks[i], v.Tasks[j] |
| 132 | pair := [2]string{min(a.ID, b.ID), max(a.ID, b.ID)} |
| 133 | if apart[pair] { |
| 134 | continue |
| 135 | } |
| 136 | // A test identity's work never meets anyone else's. |
| 137 | if a.Creator != b.Creator && (a.Hidden || b.Hidden) || a.Kind != b.Kind { |
| 138 | continue |
| 139 | } |
| 140 | match, high, score, shared := Similarity(a.Title, a.Brief, b.Title, b.Brief) |
| 141 | if !match { |
| 142 | continue |
| 143 | } |
| 144 | keep, fold := a, b |
| 145 | if stronger(b, a) { |
| 146 | keep, fold = b, a |
| 147 | } |
| 148 | // Only a pending decision on the task that would close blocks |
| 149 | // a combine; an open card on the one kept (staging's Woodstar |
| 150 | // pair: a merge card) stays where it is (coordinator, |
| 151 | // 2026-11-11, on Paul's "combine automatically with Undo"). |
| 152 | combinable := high && keep.Creator == fold.Creator && keep.Stage != "merging" && fold.Stage != "merging" && !fold.Busy && !fold.Carded |
| 153 | ev := Evidence{Tasks: []TaskRef{{ID: fold.ID, Title: fold.Title, Creator: fold.Creator}, {ID: keep.ID, Title: keep.Title, Creator: keep.Creator}}, Similarity: float64(int(score*100+0.5)) / 100, Shared: shared} |
| 154 | f := Finding{Subject: Subject{Kind: SubjectTask, ID: fold.ID}, Owner: fold.Creator, SubjectRevision: fold.Revision, Evidence: ev} |
| 155 | switch { |
| 156 | case combine && combinable: |
| 157 | f.Key = "duplicate:" + pair[0] + ":" + pair[1] |
| 158 | f.Proposal = Proposal{Action: ActionCombine, Task: fold.ID, Into: keep.ID} |
| 159 | case !combine && !combinable && fold.DuplicateOf == "": |
| 160 | f.Key = "possible:" + fold.ID + ":" + keep.ID |
| 161 | f.Proposal = Proposal{Action: ActionLink, Task: fold.ID, Into: keep.ID} |
| 162 | default: |
| 163 | continue |
| 164 | } |
| 165 | out = append(out, f) |
| 166 | } |
| 167 | } |
| 168 | return out |
| 169 | } |
| 170 | |
| 171 | var stopWords = map[string]bool{"the": true, "and": true, "for": true, "with": true, "that": true, "this": true, "from": true, "when": true, "into": true, "not": true, "but": true, "are": true, "was": true, "has": true, "have": true, "its": true, "it's": true, "can": true, "you": true, "your": true, "our": true, "they": true, "them": true, "there": true, "then": true, "than": true, "what": true, "which": true, "will": true, "would": true, "should": true, "could": true, "does": true, "doesn": true, "don": true, "isn": true, "all": true, "any": true, "out": true, "app": true, "please": true, "fix": true, "bug": true, "issue": true, "make": true, "work": true, "working": true, "works": true} |
| 172 | |
| 173 | // Significant is a text's significant words, lower case, singular-ish: |
| 174 | // record_task's similarity reads these. |
| 175 | func Significant(s string) map[string]bool { |
| 176 | out := map[string]bool{} |
| 177 | for _, w := range strings.FieldsFunc(strings.ToLower(s), func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) }) { |
| 178 | w = strings.TrimSuffix(w, "s") |
| 179 | if len(w) < 3 || stopWords[w] || stopWords[w+"s"] { |
| 180 | continue |
| 181 | } |
| 182 | out[w] = true |
| 183 | } |
| 184 | return out |
| 185 | } |
| 186 | |
| 187 | // Overlap is how much of the smaller set the larger one shares, and how |
| 188 | // many words they share. |
| 189 | func Overlap(a, b map[string]bool) (float64, int) { |
| 190 | if len(a) == 0 || len(b) == 0 { |
| 191 | return 0, 0 |
| 192 | } |
| 193 | n := 0 |
| 194 | for w := range a { |
| 195 | if b[w] { |
| 196 | n++ |
| 197 | } |
| 198 | } |
| 199 | return float64(n) / float64(min(len(a), len(b))), n |
| 200 | } |
| 201 | |