| 1 | package housekeeping |
| 2 | |
| 3 | import "time" |
| 4 | |
| 5 | // Limits are what housekeeping may spend per person per day (Paul, |
| 6 | // 2026-11-08): four judgement turns, 150,000 tokens and three automatic |
| 7 | // retries; and three automatic Engineering rounds (needs-you triage, |
| 8 | // 2026-11-11). Person turns are never budgeted. |
| 9 | var Limits = Cost{Turns: 4, Tokens: 150_000, Retries: 3, Rounds: 3} |
| 10 | |
| 11 | // What a spending action reserves (spec §4): a judgement turn one turn |
| 12 | // and an equal share of the day's tokens (150,000 / 4 = 37,500, Paul's |
| 13 | // ruling: four turns must fit; the token budget stays the binding |
| 14 | // total); an automatic retry one retry and 100,000 tokens for the |
| 15 | // retried step and the review and checks after it. |
| 16 | var ( |
| 17 | JudgementTurn = Cost{Turns: 1, Tokens: Limits.Tokens / Limits.Turns} |
| 18 | AutoRetry = Cost{Retries: 1, Tokens: 100_000} |
| 19 | ) |
| 20 | |
| 21 | // Control constants (spec §4). |
| 22 | const ( |
| 23 | // StandDownAt: nothing that spends is dispatched on a connection at or |
| 24 | // above this utilization; unknown headroom stands down too. |
| 25 | StandDownAt = 0.8 |
| 26 | // HeadroomFresh: a headroom figure older than this is unknown. |
| 27 | HeadroomFresh = 15 * time.Minute |
| 28 | // ActionsPerDay and SuggestionsPerDay: the ceilings per person, after |
| 29 | // which findings are held. |
| 30 | ActionsPerDay = 10 |
| 31 | SuggestionsPerDay = 5 |
| 32 | // HeldPerPerson: beyond this the oldest held finding expires. |
| 33 | HeldPerPerson = 20 |
| 34 | // UndoWindow: an action can be undone for seven days. |
| 35 | UndoWindow = 7 * 24 * time.Hour |
| 36 | // ClaimStale: a claim older than this is revalidated by the sweep. |
| 37 | ClaimStale = 10 * time.Minute |
| 38 | // FailureWindow and FailuresToDisable: two failures within seven days |
| 39 | // disable a detector for the person until they switch it back on. |
| 40 | FailureWindow = 7 * 24 * time.Hour |
| 41 | FailuresToDisable = 2 |
| 42 | // VetoWindow: a dismissed key stays quiet this long, then may recur once. |
| 43 | VetoWindow = 30 * 24 * time.Hour |
| 44 | // QuietFrom and QuietUntil: the default quiet hours; a person may |
| 45 | // choose others or none. |
| 46 | QuietFrom = 22 |
| 47 | QuietUntil = 8 |
| 48 | // SweepHour: the daily sweep's hour in the person's zone. |
| 49 | SweepHour = 3 |
| 50 | ) |
| 51 | |
| 52 | // Stand-down reasons. |
| 53 | const ( |
| 54 | StandDownBudget = "budget" |
| 55 | StandDownHeadroom = "headroom" |
| 56 | StandDownUnknown = "headroom_unknown" |
| 57 | StandDownLimited = "limited" |
| 58 | ) |
| 59 | |
| 60 | // Fits reports whether reserving c on top of what today already used |
| 61 | // stays within every limit. |
| 62 | func Fits(used, c Cost, limits Cost) bool { |
| 63 | n := used.Add(c) |
| 64 | return n.Turns <= limits.Turns && n.Tokens <= limits.Tokens && n.Retries <= limits.Retries && n.Rounds <= limits.Rounds |
| 65 | } |
| 66 | |
| 67 | // Headroom is the selected connection's last report: Reported is false |
| 68 | // when the provider gave no utilization. |
| 69 | type Headroom struct { |
| 70 | Healthy bool |
| 71 | Reported bool |
| 72 | Utilization float64 |
| 73 | CheckedAt time.Time |
| 74 | } |
| 75 | |
| 76 | // StandDown says why nothing that spends may run on the connection the |
| 77 | // choice selected, or "" when it may. Unknown means stand down. |
| 78 | func StandDown(h *Headroom, now time.Time) string { |
| 79 | switch { |
| 80 | case h == nil: |
| 81 | return StandDownUnknown |
| 82 | case !h.Healthy: |
| 83 | return StandDownLimited |
| 84 | case !h.Reported || h.CheckedAt.IsZero() || now.Sub(h.CheckedAt) > HeadroomFresh: |
| 85 | return StandDownUnknown |
| 86 | case h.Utilization >= StandDownAt: |
| 87 | return StandDownHeadroom |
| 88 | } |
| 89 | return "" |
| 90 | } |
| 91 | |
| 92 | // QuietHours is a person's window, in whole hours of their zone: receipts |
| 93 | // made inside it wait until it ends. From equal to Until means none. |
| 94 | type QuietHours struct{ From, Until int } |
| 95 | |
| 96 | // DefaultQuiet is 22:00 to 08:00. |
| 97 | var DefaultQuiet = QuietHours{From: QuietFrom, Until: QuietUntil} |
| 98 | |
| 99 | // Off reports whether the window is empty. |
| 100 | func (q QuietHours) Off() bool { return q.From == q.Until } |
| 101 | |
| 102 | // Valid reports whether both ends are hours of a day. |
| 103 | func (q QuietHours) Valid() bool { return q.From >= 0 && q.From < 24 && q.Until >= 0 && q.Until < 24 } |
| 104 | |
| 105 | // Quiet reports whether at falls in the window, in loc. |
| 106 | func (q QuietHours) Quiet(at time.Time, loc *time.Location) bool { |
| 107 | if q.Off() { |
| 108 | return false |
| 109 | } |
| 110 | h := at.In(loc).Hour() |
| 111 | if q.From < q.Until { |
| 112 | return h >= q.From && h < q.Until |
| 113 | } |
| 114 | return h >= q.From || h < q.Until |
| 115 | } |
| 116 | |
| 117 | // VisibleAt is when a receipt made at at may show in the conversation: |
| 118 | // nil outside the window, else the window's next end in loc. |
| 119 | func (q QuietHours) VisibleAt(at time.Time, loc *time.Location) *time.Time { |
| 120 | if !q.Quiet(at, loc) { |
| 121 | return nil |
| 122 | } |
| 123 | l := at.In(loc) |
| 124 | v := time.Date(l.Year(), l.Month(), l.Day(), q.Until, 0, 0, 0, loc) |
| 125 | if !v.After(l) { |
| 126 | v = v.AddDate(0, 0, 1) |
| 127 | } |
| 128 | v = v.UTC() |
| 129 | return &v |
| 130 | } |
| 131 | |
| 132 | // Morning is the hour the day's digest may be written from: the end of |
| 133 | // the window, or 08:00 without one. |
| 134 | func (q QuietHours) Morning() int { |
| 135 | if q.Off() { |
| 136 | return QuietUntil |
| 137 | } |
| 138 | return q.Until |
| 139 | } |
| 140 | |
| 141 | // DayStart is the start of at's day in loc, in UTC. |
| 142 | func DayStart(at time.Time, loc *time.Location) time.Time { |
| 143 | l := at.In(loc) |
| 144 | return time.Date(l.Year(), l.Month(), l.Day(), 0, 0, 0, 0, loc).UTC() |
| 145 | } |
| 146 | |
| 147 | // AtHour is hour o'clock on at's day in loc, in UTC: the clock's own hour |
| 148 | // on a day that springs forward or falls back, never midnight plus hours |
| 149 | // (paused work review N1). |
| 150 | func AtHour(at time.Time, loc *time.Location, hour int) time.Time { |
| 151 | l := at.In(loc) |
| 152 | return time.Date(l.Year(), l.Month(), l.Day(), hour, 0, 0, 0, loc).UTC() |
| 153 | } |
| 154 | |