From 84f3cf9074513dd732aeb2066eeab78bd5c522b8 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 22 Nov 2026 22:43:47 -0600 Subject: [PATCH] fleet(pools): refuse pool-drawn seatings at >=97% monthly, >1pp/h pace, or steward hard-stop (#2732) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Slice 1 of #2732: the placement evaluator — the shared seating helper the triage suggested for the dispatcher and lane-shift paths — now enforces the monthly pace guard at seating time. A pool-drawn placement on a fresh quota read is refused (exit non-zero, pool + % + guard source on stderr) when pool_monthly_pct >= 97, pool_pace_pph > 1, or pool_hard_stop is set. Unknown state never refuses (B4 reliance, absent monthly data, non-pool options proceed); E1 exceptions bypass with their own record as the log. Prerequisite repair in the same package: policy version now decodes the v1.1 string form ("1.1") as well as the v1.0 int — all existing placement tests were failing at HEAD after the #2179 amendment. --- pkgs/fleet/placement/README.md | 15 ++- pkgs/fleet/placement/evaluate.go | 73 +++++++++++- pkgs/fleet/placement/monthly_guard_test.go | 129 +++++++++++++++++++++ pkgs/fleet/placement/policy.go | 41 ++++++- 4 files changed, 253 insertions(+), 5 deletions(-) create mode 100644 pkgs/fleet/placement/monthly_guard_test.go diff --git a/pkgs/fleet/placement/README.md b/pkgs/fleet/placement/README.md index 2f79577cf6..f84ac2bfe3 100644 --- a/pkgs/fleet/placement/README.md +++ b/pkgs/fleet/placement/README.md @@ -38,7 +38,20 @@ that cannot produce a record does not proceed unrecorded. checked-in `placement-policy.yaml`; they skip only if it is absent): every rule path, first-match-wins, the guard's exclusion of R8 and unreachability of R3, the B5 gate (missing read, stale read, B4 reliance), the `"n/a"` -non-pool case, and the exception path. +non-pool case, the exception path, and the monthly pace guard (refuse at +≥97% / >1pp/h / hard-stop; B4, missing data, and non-pool seatings +proceed). + +## Monthly pace guard (issue #2732, slice 1) + +A pool-drawn seating is refused — exit non-zero with the pool, the % read, +and the guard source on stderr — when the fresh quota read behind it shows +`pool_monthly_pct` at/above 97%, `pool_pace_pph` above 1pp/h, or +`pool_hard_stop` set (the steward hard-stop in machine-readable form). +Unknown state never refuses: a B4 reliance proceeds, absent monthly data +seats on the fresh read, and non-pool options ignore the guard inputs. +An E1 exception bypasses the guard — the exception record itself is the +required log line. ## Demonstration diff --git a/pkgs/fleet/placement/evaluate.go b/pkgs/fleet/placement/evaluate.go index 70adb1ad1f..6132bacf34 100644 --- a/pkgs/fleet/placement/evaluate.go +++ b/pkgs/fleet/placement/evaluate.go @@ -45,7 +45,23 @@ type Answers struct { // PoolReadUnobtainable records a B4 reliance: no fresh read could be // obtained, so dispatch proceeds on the last known state and // PoolReadAt carries that last read's timestamp. + // PoolReadUnobtainable records a B4 reliance: no fresh read could be + // obtained, so dispatch proceeds on the last known state and + // PoolReadAt carries that last read's timestamp. PoolReadUnobtainable bool `json:"pool_read_unobtainable,omitempty"` + // PoolID names the cost pool the chosen option draws on (e.g. + // "chatgpt-plan"), for guard refusals and dispatcher-log lines. + PoolID string `json:"pool_id,omitempty"` + // PoolMonthlyPct is the pool's current monthly utilization percent from + // the quota hub (quota-current.json, ≤60 min old per B5). Nil means the + // read carried no monthly figure, and the monthly guard cannot apply. + PoolMonthlyPct *float64 `json:"pool_monthly_pct,omitempty"` + // PoolPacePph is the pool's trailing burn pace in percentage points per + // hour toward a wall. Nil means unknown. + PoolPacePph *float64 `json:"pool_pace_pph,omitempty"` + // PoolHardStop records a steward hard-stop note for the pool in a + // machine-readable field the seating path checks (issue #2732 step 2). + PoolHardStop bool `json:"pool_hard_stop,omitempty"` } // Record is the placement record declared by placement-policy.yaml's @@ -60,10 +76,18 @@ type Record struct { Task string `json:"task,omitempty"` GuardR2 []string `json:"guard_r2_excluded_options,omitempty"` B4Relied bool `json:"b4_unknown_read_relied_on,omitempty"` - PolicyVersion int `json:"policy_version"` + PolicyVersion string `json:"policy_version"` EvaluatedAt time.Time `json:"evaluated_at"` } +// Monthly pace-guard thresholds (issue #2732): a seating attempt against +// a pool at or above MonthlyGuardPct, burning faster than PaceGuardPph +// toward a wall, or under a steward hard-stop is refused and rerouted. +const ( + MonthlyGuardPct = 97.0 + PaceGuardPph = 1.0 +) + // Evaluate runs the policy's decision procedure over the answers and // returns the placement record. Evaluation order is the policy's own: // Q1 first; the R2 guard at Q2 (it excludes options rather than selecting @@ -149,7 +173,7 @@ func buildRecord(p *Policy, a Answers, ruleID, optionID string, excluded []strin Rule: ruleID, Supervision: opt.Supervision, Task: a.Task, - PolicyVersion: p.Version, + PolicyVersion: p.Version.String(), EvaluatedAt: now.UTC(), } if ruleID == "exception" { @@ -180,5 +204,50 @@ func buildRecord(p *Policy, a Answers, ruleID, optionID string, excluded []strin } else if age < -5*time.Minute { return Record{}, fmt.Errorf("pool_read_at is %s in the future", (-age).Round(time.Second)) } + // Monthly pace guard (#2732 step 1): on a fresh read for a pool-drawn + // seating, refuse at/above the monthly threshold, above the pace + // threshold, or under a steward hard-stop. The refusal names the pool, + // the % read, and the guard source so the dispatcher log can score + // compliance from data. Unknown state never refuses: a B4 reliance + // returns above, and absent monthly data seats on the fresh read. + if reason := monthlyGuardRefusal(a); reason != "" { + return Record{}, fmt.Errorf("monthly-guard refusal: option %q on pool %q refused (%s)", optionID, guardPoolID(p, a, optionID), reason) + } return rec, nil } + +// monthlyGuardRefusal reports why a seating must be refused under issue +// #2732, or "" when it may proceed. Guard sources: "monthly-guard", +// "pace-guard", "hard-stop". +func monthlyGuardRefusal(a Answers) string { + monthly := -1.0 + if a.PoolMonthlyPct != nil { + monthly = *a.PoolMonthlyPct + } + pace := 0.0 + if a.PoolPacePph != nil { + pace = *a.PoolPacePph + } + if a.PoolHardStop { + return fmt.Sprintf("steward hard-stop in effect (monthly %.1f%%) [source: hard-stop]", monthly) + } + if a.PoolMonthlyPct != nil && monthly >= MonthlyGuardPct { + return fmt.Sprintf("monthly %.1f%% at/above %.0f%% threshold [source: monthly-guard]", monthly, MonthlyGuardPct) + } + if a.PoolPacePph != nil && pace > PaceGuardPph { + return fmt.Sprintf("trailing pace %.1fpp/h above %.0fpp/h toward a wall (monthly %.1f%%) [source: pace-guard]", pace, PaceGuardPph, monthly) + } + return "" +} + +// guardPoolID names the pool for a refusal line: the explicit PoolID when +// supplied, else the option's cost-pool label from the policy. +func guardPoolID(p *Policy, a Answers, optionID string) string { + if a.PoolID != "" { + return a.PoolID + } + if opt, ok := p.Option(optionID); ok { + return opt.CostPool + } + return "unknown-pool" +} diff --git a/pkgs/fleet/placement/monthly_guard_test.go b/pkgs/fleet/placement/monthly_guard_test.go new file mode 100644 index 0000000000..c48b0b27ef --- /dev/null +++ b/pkgs/fleet/placement/monthly_guard_test.go @@ -0,0 +1,129 @@ +package placement + +import ( + "strings" + "testing" + "time" +) + +// TestMonthlyPaceGuard is the first slice of issue #2732: a seating attempt +// against a pool at/above the 97% monthly guard, with a trailing pace above +// 1pp/h toward a wall, or under a steward hard-stop, is refused (with the +// pool, the % read, and the guard source in the refusal) instead of seating +// silently. Unknown state never refuses: a B4 reliance and a non-pool +// option proceed, per burn rules B4/B5. +func TestMonthlyPaceGuard(t *testing.T) { + p := testPolicy(t) + now := time.Date(2026, 11, 22, 3, 0, 0, 0, time.UTC) + fresh := now.Add(-10 * time.Minute) + + pct := func(f float64) *float64 { return &f } + pace := func(f float64) *float64 { return &f } + + judgement := func(pool string, monthly *float64, pph *float64, hardStop bool) Answers { + return Answers{ + Task: "queue task test / issue #2732", + Q4Judgement: true, + PoolReadAt: &fresh, + PoolID: pool, + PoolMonthlyPct: monthly, + PoolPacePph: pph, + PoolHardStop: hardStop, + } + } + + t.Run("monthly at 97 refuses", func(t *testing.T) { + _, err := Evaluate(p, judgement("chatgpt-plan", pct(97.0), pace(0.1), false), now) + if err == nil { + t.Fatal("expected a monthly-guard refusal at 97%") + } + msg := err.Error() + for _, want := range []string{"chatgpt-plan", "97", "monthly"} { + if !strings.Contains(msg, want) { + t.Fatalf("refusal %q does not cite %q (pool, %%, guard source)", msg, want) + } + } + }) + + t.Run("monthly below 97 seats", func(t *testing.T) { + rec, err := Evaluate(p, judgement("chatgpt-plan", pct(96.9), pace(0.1), false), now) + if err != nil { + t.Fatalf("Evaluate below the guard: %v", err) + } + if rec.Option != "Muse" || rec.Rule != "R6" { + t.Fatalf("got %s/%s, want Muse/R6", rec.Option, rec.Rule) + } + }) + + t.Run("pace above 1pp/h refuses", func(t *testing.T) { + _, err := Evaluate(p, judgement("meta-pool", pct(90.0), pace(1.2), false), now) + if err == nil { + t.Fatal("expected a pace-guard refusal above 1pp/h") + } + msg := err.Error() + for _, want := range []string{"meta-pool", "pace"} { + if !strings.Contains(msg, want) { + t.Fatalf("refusal %q does not cite %q", msg, want) + } + } + }) + + t.Run("pace at 1pp/h seats", func(t *testing.T) { + if _, err := Evaluate(p, judgement("meta-pool", pct(90.0), pace(1.0), false), now); err != nil { + t.Fatalf("Evaluate at exactly 1pp/h: %v", err) + } + }) + + t.Run("steward hard-stop refuses at any pct", func(t *testing.T) { + _, err := Evaluate(p, judgement("plan-routes", pct(42.0), pace(0.0), true), now) + if err == nil { + t.Fatal("expected a hard-stop refusal") + } + if msg := err.Error(); !strings.Contains(msg, "hard-stop") { + t.Fatalf("refusal %q does not cite its guard source", msg) + } + }) + + t.Run("B4 reliance proceeds despite the guard", func(t *testing.T) { + stale := now.Add(-2 * time.Hour) + rec, err := Evaluate(p, Answers{ + Task: "queue task test / issue #2732", + Q4Judgement: true, + PoolReadAt: &stale, + PoolReadUnobtainable: true, + PoolID: "chatgpt-plan", + PoolMonthlyPct: pct(99.5), + PoolHardStop: true, + }, now) + if err != nil { + t.Fatalf("B4 reliance must never refuse (B4 governs, not the guard): %v", err) + } + if !rec.B4Relied { + t.Fatal("record does not carry the B4 reliance") + } + }) + + t.Run("non-pool option ignores guard inputs", func(t *testing.T) { + rec, err := Evaluate(p, Answers{ + Q1Mechanical: true, + PoolID: "chatgpt-plan", + PoolMonthlyPct: pct(99.5), + PoolHardStop: true, + }, now) + if err != nil { + t.Fatalf("non-pool placement must ignore the monthly guard: %v", err) + } + if rec.PoolReadAt != "n/a" { + t.Fatalf("pool_read_at = %q, want n/a", rec.PoolReadAt) + } + }) + + t.Run("no monthly data seats on a fresh read", func(t *testing.T) { + if _, err := Evaluate(p, Answers{ + Q4Judgement: true, + PoolReadAt: &fresh, + }, now); err != nil { + t.Fatalf("a fresh read with no monthly data must seat: %v", err) + } + }) +} diff --git a/pkgs/fleet/placement/policy.go b/pkgs/fleet/placement/policy.go index 0de56b201e..4b1e110306 100644 --- a/pkgs/fleet/placement/policy.go +++ b/pkgs/fleet/placement/policy.go @@ -18,6 +18,43 @@ import ( "gopkg.in/yaml.v3" ) +// Version is a dotted policy version. The v1.0 policy file stated it as +// a YAML int (e.g. 1); the v1.1 amendment (#2179) states it as a string +// (e.g. "1.1"). Both decode; String renders the canonical dotted form. +type Version struct { + Major int + Minor int +} + +// UnmarshalYAML accepts a YAML int or a "MAJOR[.MINOR]" string. +func (v *Version) UnmarshalYAML(node *yaml.Node) error { + var n int + if err := node.Decode(&n); err == nil { + v.Major, v.Minor = n, 0 + return nil + } + var s string + if err := node.Decode(&s); err != nil { + return fmt.Errorf("policy version must be an int or dotted string: %w", err) + } + if _, err := fmt.Sscanf(s, "%d.%d", &v.Major, &v.Minor); err != nil { + var major int + if _, err2 := fmt.Sscanf(s, "%d", &major); err2 != nil { + return fmt.Errorf("policy version %q is not an int or dotted version", s) + } + v.Major, v.Minor = major, 0 + } + return nil +} + +// String renders the canonical dotted form ("1" or "1.1"). +func (v Version) String() string { + if v.Minor == 0 { + return fmt.Sprintf("%d", v.Major) + } + return fmt.Sprintf("%d.%d", v.Major, v.Minor) +} + // Option is one placement option from the policy's options list. type Option struct { ID string `yaml:"id"` @@ -54,7 +91,7 @@ type Pool struct { // Policy is the subset of placement-policy.yaml the evaluator consumes. type Policy struct { - Version int `yaml:"version"` + Version Version `yaml:"version"` SourceIssue int `yaml:"source_issue"` ImplementingIssue int `yaml:"implementing_issue"` Options []Option `yaml:"options"` @@ -89,7 +126,7 @@ func Load(path string) (*Policy, error) { } func (p *Policy) index() error { - if p.Version == 0 { + if p.Version.Major == 0 { return fmt.Errorf("policy: missing version") } p.optionByID = make(map[string]Option, len(p.Options))