Skip to content

Commit cd0d7e5

Browse files
authored
feat(speculation): add path scorer extension (#316)
Add the scorer seam from the speculation RFC, as a vendor-agnostic extension interface under submitqueue/extension/speculation/pathscorer/. The scorer computes each speculation path's predicted-success score from the current state: the per-batch scores of the path's base batches (entity.Batch.Score) and which of those dependencies have resolved (landed or build-passed), plus optionally other signals. It is a prediction over live state, so the controller re-runs it on every respeculate right after reconciling status, and persists the result; the scorer owns only the formula. The controller hands it the batch's speculation tree directly — the subject it scores. Any richer signal an implementation needs (dependency batch scores, historical pass rates) is injected at its Factory, not put in the signature. It never writes: its only output is per-path scores ([]entity.PathScore — path ID plus fresh score, an entity-level seam-output type alongside the path-decision type), which the controller merges into the tree and persists, staying the single writer of tree state; structure and status never pass through the scorer. Scores are probabilities in [0, 1] — the contract every implementation must satisfy, enforced by the controller on consume. This is the per-path scorer, distinct from the existing per-batch score stage (`extension/scorer`) that sets entity.Batch.Score — the path scorer consumes those to score whole paths. Follows the repo extension contract: Factory.For(Config) (Scorer, error) with Config carrying only QueueName. Includes README, gomock package, and a programmable fake. The speculation RFC's seam descriptions are updated to match the identity-keyed minimal-output contracts (and gain a design-decision entry for assigned path identity). Interface only; concrete impls and controller wiring are deferred. ## Stack 1. #337 1. #315 1. @ #316 1. #317 1. #320 1. #331 1. #332 1. #333
1 parent d760fe2 commit cd0d7e5

12 files changed

Lines changed: 381 additions & 6 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ local-stovepipe-stop: ## Stop the Stovepipe service
364364

365365
mocks: ## Generate mock files using mockgen
366366
@echo "Generating mocks..."
367-
@$(BAZEL) run @rules_go//go -- generate ./submitqueue/extension/storage/... ./submitqueue/extension/buildrunner/... ./submitqueue/extension/changeprovider/... ./platform/extension/counter/... ./platform/extension/messagequeue/... ./submitqueue/extension/queueconfig/... ./submitqueue/extension/mergechecker/... ./submitqueue/extension/pusher/... ./submitqueue/extension/scorer/... ./submitqueue/extension/conflict/... ./submitqueue/extension/speculation/enumerator/... ./submitqueue/extension/speculation/dependencylimit/... ./submitqueue/extension/validator/... ./platform/consumer/... ./stovepipe/extension/storage/... ./stovepipe/extension/sourcecontrol/...
367+
@$(BAZEL) run @rules_go//go -- generate ./submitqueue/extension/storage/... ./submitqueue/extension/buildrunner/... ./submitqueue/extension/changeprovider/... ./platform/extension/counter/... ./platform/extension/messagequeue/... ./submitqueue/extension/queueconfig/... ./submitqueue/extension/mergechecker/... ./submitqueue/extension/pusher/... ./submitqueue/extension/scorer/... ./submitqueue/extension/conflict/... ./submitqueue/extension/speculation/enumerator/... ./submitqueue/extension/speculation/dependencylimit/... ./submitqueue/extension/speculation/pathscorer/... ./submitqueue/extension/validator/... ./platform/consumer/... ./stovepipe/extension/storage/... ./stovepipe/extension/sourcecontrol/...
368368
@echo "Mocks generated successfully!"
369369

370370
proto: ## Generate protobuf files from .proto definitions

doc/rfc/submitqueue/speculation.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ Re-speculation needs no special undo path: the controller refreshes statuses, an
187187

188188
## Interfaces
189189

190-
The seams are vendor-agnostic extensions, each in its own package; the exact Go signatures live in the source. All are per-queue: the system hands a `Factory` the queue identity, and the factory builds the seam for that queue — so the queue is bound at construction and never re-passed to a method.
190+
The seams are vendor-agnostic extensions, each in its own package; the exact Go signatures live in the source. All are per-queue: the system hands a `Factory` the queue identity, and the factory builds the seam for that queue — so the queue is bound at construction and never re-passed to a method. Persisted paths carry a controller-assigned identity, and every seam output names a path by that ID rather than restating its Base/Head split — each seam returns only its verdict, and everything else about a path stays controller-owned.
191191

192192
**Decision seams:**
193193

194-
- **Enumerator** (`extension/speculation/enumerator`) — given a batch ID and its ordered active dependencies, returns the batch's speculation tree *structure*: the candidate paths, each a Base/Head split. Pure and deterministic; sets no score and no status.
195-
- **Scorer** (`extension/speculation/scorer`) — given the speculation tree and the current dependency batches, returns each path's predicted-success score. Called by the controller on every respeculate (during reconciliation) so scores track the live state — dependencies landing, dependency builds passing, siblings failing. Owns the score formula; combines the base batches' `Batch.Score` and their resolved/unresolved state (and optionally other signals).
196-
- **Selector** (`extension/speculation/selector`) — given a speculation tree (with each path's controller-stamped status and freshly recomputed score), returns a per-path action (`Build` or `Cancel`) for the paths it chooses to act on. It reads status and score and emits actions only; paths it leaves alone are omitted. It is constructed with its **selection limit** and calls it to cap how many paths it builds in parallel.
194+
- **Enumerator** (`extension/speculation/enumerator`) — given a batch and its ordered active dependencies, returns the batch's candidate paths, each a Base/Head split — structure only. Pure and deterministic; the controller assembles the persisted tree, assigning each path's identity and stamping its status.
195+
- **Scorer** (`extension/speculation/pathscorer`) — given the speculation tree and the current dependency batches, returns each path's predicted-success score, keyed by path identity. Scores are probabilities in [0, 1]; the controller enforces the range when it merges them into the tree. Called by the controller on every respeculate (during reconciliation) so scores track the live state — dependencies landing, dependency builds passing, siblings failing. Owns the score formula; combines the base batches' `Batch.Score` and their resolved/unresolved state (and optionally other signals).
196+
- **Selector** (`extension/speculation/selector`) — given a speculation tree (with each path's controller-stamped status and freshly recomputed score), returns a per-path action (`Build` or `Cancel`) for the paths it chooses to act on, each decision naming its path by identity, at most one decision per path. It reads status and score and emits actions only; paths it leaves alone are omitted. It is constructed with its **selection limit** and calls it to cap how many paths it builds in parallel.
197197
- **Prioritizer** (`extension/speculation/prioritizer`) — given the queue's pending build candidates, returns the subset admitted to run, ranked by score plus any fairness policy. It is constructed with its **prioritization limit** and applies it itself. Operates queue-wide, across all of the queue's in-flight batches.
198198

199199
**Limit policies** — each a signal-driven "how much" seam returning a bound from build-resource and other signals:
@@ -206,6 +206,8 @@ The scorer, prioritizer, and the three limit policies are design-level here —
206206

207207
## Design decisions
208208

209+
**Persisted paths are referenced by assigned identity.** Each path entry in a persisted tree gets a controller-assigned, immutable, opaque ID when it is first written; scores, decisions, and durable links (such as the path→build mapping) all name paths by it. *Why:* seam outputs stay minimal — an ID plus a verdict — instead of restating structure, and cross-entity links stay valid however the tree is re-derived. *Rejected:* structural reference (restating Base/Head in every output) — couples every consumer to path structure and forces ordered-slice comparison everywhere; an ID derived from the structure — an ID that encodes structure invites parsing, and identity should be free to survive re-enumeration on the controller's terms.
210+
209211
**Two layers: decisions and limits.** Decision seams (enumerator, scorer, selector, prioritizer) — enumeration and scoring *describe* the tree, selection and prioritization *act* on it; limit policies (dependency, selection, prioritization) decide *how much*. *Why:* the "which" is qualitative policy that is stable, while the "how much" must scale with volatile build resources; separating them lets the resource-aware knobs move independently of the decision logic, and lets each be tested in isolation. *Rejected:* baking counts into each decision seam as constants — it hard-codes a policy that needs to breathe with CI capacity.
210212

211213
**Limits are signal-driven, and resources are the primary but not the only signal.** A limit is whatever its policy computes — from available capacity, and optionally historical pass rates, cost, time, or experiment flags. *Why:* speculation aggression should rise and fall with the build system, and the design should not foreclose other inputs. *Rejected:* a single fixed constant, or a static per-queue config value — neither can react to load.

submitqueue/entity/speculation_tree.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ type SpeculationPathInfo struct {
130130
BuildID string
131131
}
132132

133+
// PathScore is the path scorer's verdict for a single path: the
134+
// path's identity and its freshly computed predicted-success score. It is the
135+
// scorer seam's only output — the controller merges scores into the tree by
136+
// path ID and persists them; tree structure and status never pass through the
137+
// scorer. Like SpeculationPathDecision, it is ephemeral and never persisted.
138+
type PathScore struct {
139+
// PathID identifies the scored path (SpeculationPathInfo.ID) within the
140+
// tree the scorer was handed.
141+
PathID string
142+
// Score is the path's predicted-success probability, in [0, 1].
143+
Score float32
144+
}
145+
133146
// SpeculationPathDecision is a seam's decision for a single path: the action the
134147
// controller should take for it. It is the output of both the selector (per
135148
// batch) and the prioritizer (queue-wide), and is not persisted. A seam returns

submitqueue/extension/speculation/enumerator/enumerator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// deliberately dumb and purely structural: it mechanically lists candidate
3030
// Base/Head paths from the dependency batches it is handed and nothing else. It
3131
// does not score paths — that is the scorer's job (see
32-
// extension/speculation/scorer), which the controller re-runs on every
32+
// extension/speculation/pathscorer), which the controller re-runs on every
3333
// respeculate — it does not decide which paths to build — that is the selector's
3434
// job (see extension/speculation/selector) — and it does not decide how far
3535
// back to speculate: the controller gates on the dependency limit and hands
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
load("@rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["pathscorer.go"],
6+
importpath = "github.com/uber/submitqueue/submitqueue/extension/speculation/pathscorer",
7+
visibility = ["//visibility:public"],
8+
deps = ["//submitqueue/entity:go_default_library"],
9+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Speculation Path Scorer
2+
3+
Vendor-agnostic interface for scoring the paths in a batch's **speculation tree** — the predicted-success probability of each candidate bet, recomputed as the batch's world changes.
4+
5+
See the [Speculation RFC](/doc/rfc/submitqueue/speculation.md) for the end-to-end design and how scoring fits into the orchestrator pipeline.
6+
7+
## Scorer
8+
9+
A path's score is a **prediction**: *how likely is this bet to pay off, right now?* The scorer answers it from the current state — the per-batch success probabilities of a path's base batches (`entity.Batch.Score`, set by the score stage), which of those dependencies have already landed or had their build pass (resolved assumptions raise confidence), and optionally other signals such as how long the batch has waited or historical pass rates. The score is the common currency the [selector](../selector) and prioritizer both rank on, so keeping it current is what makes both act on the latest reality.
10+
11+
Because it is a prediction over live state, the scorer is **re-run on every respeculate**, right after the controller reconciles path status — so when a dependency lands, its build passes, or a sibling path fails, the surviving paths' scores are recomputed before anything is selected or prioritized. The controller drives *when* to rescore (it is part of reconciliation) and persists the result; the scorer owns the *formula*.
12+
13+
This is the per-**path** scorer, distinct from the per-**batch** [score stage](../../scorer), which sets `entity.Batch.Score`. The path scorer consumes those batch scores to score whole paths. The controller hands it the batch's **speculation tree** directly — the subject it scores — and any richer signal an implementation needs (the dependency batches' scores, historical pass rates) is injected at its factory, not passed in. It never writes: its only output is per-path scores, each naming a path by its ID, and the controller merges them into the tree and persists — the controller stays the single writer of tree state, and everything else about a path (structure, status) never passes through the scorer at all. Paths omitted from the result keep their last persisted score.
14+
15+
Scores are **probabilities in [0, 1]** — 0 is a bet certain to lose, 1 a bet certain to pay off. That is the contract every implementation must satisfy, and the controller enforces the range when it consumes the result. The selector and prioritizer rank on these values, so implementations sharing a queue must agree on this scale.
16+
17+
## Factory
18+
19+
A per-queue factory returns the scorer for a queue, following the repo's extension contract. It is handed only the queue identity; scoring knobs and read access to any extra signals are injected at construction by the integrator in the wiring layer, which resolves per-queue settings through `queueconfig`. Scoring itself stays config-free.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["fake.go"],
6+
importpath = "github.com/uber/submitqueue/submitqueue/extension/speculation/pathscorer/fake",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//submitqueue/entity:go_default_library",
10+
"//submitqueue/extension/speculation/pathscorer:go_default_library",
11+
],
12+
)
13+
14+
go_test(
15+
name = "go_default_test",
16+
srcs = ["fake_test.go"],
17+
embed = [":go_default_library"],
18+
deps = [
19+
"//submitqueue/entity:go_default_library",
20+
"@com_github_stretchr_testify//assert:go_default_library",
21+
"@com_github_stretchr_testify//require:go_default_library",
22+
],
23+
)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2025 Uber Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package fake provides a programmable pathscorer.Scorer for tests and
16+
// examples. By default Score echoes each input path's current score back,
17+
// keyed by its ID; Returns overrides that with canned path scores, and
18+
// FailWith injects an error on every call. It is intended for examples and
19+
// tests only, never production.
20+
package fake
21+
22+
import (
23+
"context"
24+
25+
"github.com/uber/submitqueue/submitqueue/entity"
26+
"github.com/uber/submitqueue/submitqueue/extension/speculation/pathscorer"
27+
)
28+
29+
// Scorer is a programmable pathscorer.Scorer.
30+
type Scorer struct {
31+
scores []entity.PathScore
32+
hasScores bool
33+
err error
34+
}
35+
36+
// New returns a fake Scorer that echoes each input path's current score back.
37+
// Override the returned scores with Returns.
38+
func New() *Scorer {
39+
return &Scorer{}
40+
}
41+
42+
// Returns makes every Score call return scores instead of echoing its input.
43+
func (s *Scorer) Returns(scores []entity.PathScore) *Scorer {
44+
s.scores = scores
45+
s.hasScores = true
46+
return s
47+
}
48+
49+
// FailWith makes every Score call return err.
50+
func (s *Scorer) FailWith(err error) *Scorer {
51+
s.err = err
52+
return s
53+
}
54+
55+
// Score returns the canned scores if set with Returns, otherwise one PathScore
56+
// per input path echoing the path's current score, unchanged.
57+
func (s *Scorer) Score(_ context.Context, tree entity.SpeculationTree) ([]entity.PathScore, error) {
58+
if s.err != nil {
59+
return nil, s.err
60+
}
61+
if s.hasScores {
62+
return s.scores, nil
63+
}
64+
scores := make([]entity.PathScore, 0, len(tree.Paths))
65+
for _, p := range tree.Paths {
66+
scores = append(scores, entity.PathScore{PathID: p.ID, Score: p.Score})
67+
}
68+
return scores, nil
69+
}
70+
71+
// ensure the fake satisfies the interface.
72+
var _ pathscorer.Scorer = (*Scorer)(nil)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) 2025 Uber Technologies, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package fake
16+
17+
import (
18+
"context"
19+
"errors"
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
"github.com/uber/submitqueue/submitqueue/entity"
25+
)
26+
27+
func TestScore_EchoesInputScoresByDefault(t *testing.T) {
28+
tree := entity.SpeculationTree{
29+
BatchID: "q/batch/2",
30+
Paths: []entity.SpeculationPathInfo{
31+
{ID: "q/batch/2/path/0", Path: entity.SpeculationPath{Head: "q/batch/2"}, Score: 0.9},
32+
},
33+
}
34+
got, err := New().Score(context.Background(), tree)
35+
require.NoError(t, err)
36+
assert.Equal(t, []entity.PathScore{{PathID: "q/batch/2/path/0", Score: 0.9}}, got)
37+
}
38+
39+
func TestScore_ReturnsCanned(t *testing.T) {
40+
in := entity.SpeculationTree{BatchID: "q/batch/2"}
41+
scores := []entity.PathScore{{PathID: "q/batch/2/path/0", Score: 0.75}}
42+
got, err := New().Returns(scores).Score(context.Background(), in)
43+
require.NoError(t, err)
44+
assert.Equal(t, scores, got)
45+
}
46+
47+
func TestScore_FailWith(t *testing.T) {
48+
sentinel := errors.New("boom")
49+
_, err := New().FailWith(sentinel).Score(context.Background(), entity.SpeculationTree{BatchID: "q/batch/1"})
50+
require.ErrorIs(t, err, sentinel)
51+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@rules_go//go:def.bzl", "go_library")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["pathscorer_mock.go"],
6+
importpath = "github.com/uber/submitqueue/submitqueue/extension/speculation/pathscorer/mock",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//submitqueue/entity:go_default_library",
10+
"//submitqueue/extension/speculation/pathscorer:go_default_library",
11+
"@org_uber_go_mock//gomock:go_default_library",
12+
],
13+
)

0 commit comments

Comments
 (0)