Skip to content

Commit e954c66

Browse files
committed
feat(speculation): add path scorer extension
Add the scorer seam from the speculation RFC, as a vendor-agnostic extension interface under submitqueue/extension/speculation/scorer/. 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. 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. It is pure: it reads the tree and dependency batches, returns the tree with Score recomputed, and never reads storage/builds or writes status. Follows the repo extension contract (conflict.Analyzer reference shape): Factory.For(Config) (Scorer, error) with Config carrying only QueueName. Includes README, gomock package, and Makefile mock-gen wiring. Interface only; concrete impls and controller wiring are deferred.
1 parent e547a50 commit e954c66

9 files changed

Lines changed: 358 additions & 1 deletion

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/... ./platform/consumer/... ./submitqueue/core/changeset/... ./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/scorer/... ./platform/consumer/... ./submitqueue/core/changeset/... ./stovepipe/extension/storage/... ./stovepipe/extension/sourcecontrol/...
368368
@echo "Mocks generated successfully!"
369369

370370
proto: ## Generate protobuf files from .proto definitions
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 = ["scorer.go"],
6+
importpath = "github.com/uber/submitqueue/submitqueue/extension/speculation/scorer",
7+
visibility = ["//visibility:public"],
8+
deps = ["//submitqueue/entity:go_default_library"],
9+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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. It is a pure function of its inputs: it reads the tree and the dependency batches and returns scores only. It never reads storage or builds and never writes status — status is the controller's, fed back in on the tree; only `Score` is recomputed while path structure and status pass through unchanged.
14+
15+
## Factory
16+
17+
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 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/scorer/fake",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//submitqueue/entity:go_default_library",
10+
"//submitqueue/extension/speculation/scorer: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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 scorer.Scorer for tests and examples. It
16+
// returns the tree unchanged except that every path's Score is set to a single
17+
// configured value (0 unless SetScore is called), leaving path structure and
18+
// status intact. FailWith injects an error on every call. It is intended for
19+
// examples and 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/scorer"
27+
)
28+
29+
// Scorer is a programmable scorer.Scorer that stamps a constant score on every
30+
// path.
31+
type Scorer struct {
32+
score float32
33+
err error
34+
}
35+
36+
// New returns a fake Scorer that scores every path 0. Adjust with SetScore.
37+
func New() *Scorer {
38+
return &Scorer{}
39+
}
40+
41+
// SetScore sets the score stamped on every path by Score.
42+
func (s *Scorer) SetScore(score float32) *Scorer {
43+
s.score = score
44+
return s
45+
}
46+
47+
// FailWith makes every Score call return err.
48+
func (s *Scorer) FailWith(err error) *Scorer {
49+
s.err = err
50+
return s
51+
}
52+
53+
// Score returns a copy of the tree with every path's Score set to the configured
54+
// value; path structure and status are carried through unchanged. The input tree
55+
// is not mutated. The deps argument is ignored.
56+
func (s *Scorer) Score(_ context.Context, tree entity.SpeculationTree, _ []entity.Batch) (entity.SpeculationTree, error) {
57+
if s.err != nil {
58+
return entity.SpeculationTree{}, s.err
59+
}
60+
scored := tree
61+
scored.Paths = make([]entity.SpeculationPathInfo, len(tree.Paths))
62+
copy(scored.Paths, tree.Paths)
63+
for i := range scored.Paths {
64+
scored.Paths[i].Score = s.score
65+
}
66+
return scored, nil
67+
}
68+
69+
// ensure the fake satisfies the interface.
70+
var _ scorer.Scorer = (*Scorer)(nil)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 tree() entity.SpeculationTree {
28+
return entity.SpeculationTree{
29+
BatchID: "q/batch/2",
30+
Paths: []entity.SpeculationPathInfo{
31+
{Path: entity.SpeculationPath{Head: "q/batch/2"}, Status: entity.SpeculationPathStatusCandidate},
32+
{Path: entity.SpeculationPath{Base: []string{"q/batch/1"}, Head: "q/batch/2"}, Status: entity.SpeculationPathStatusBuilding},
33+
},
34+
}
35+
}
36+
37+
func TestScore_StampsConstantAndPreservesStructure(t *testing.T) {
38+
in := tree()
39+
got, err := New().SetScore(0.5).Score(context.Background(), in, nil)
40+
require.NoError(t, err)
41+
42+
for _, p := range got.Paths {
43+
assert.Equal(t, float32(0.5), p.Score)
44+
}
45+
// structure and status carried through
46+
assert.Equal(t, in.BatchID, got.BatchID)
47+
assert.Equal(t, entity.SpeculationPathStatusBuilding, got.Paths[1].Status)
48+
assert.Equal(t, []string{"q/batch/1"}, got.Paths[1].Path.Base)
49+
// input not mutated
50+
assert.Equal(t, float32(0), in.Paths[0].Score)
51+
}
52+
53+
func TestScore_FailWith(t *testing.T) {
54+
sentinel := errors.New("boom")
55+
_, err := New().FailWith(sentinel).Score(context.Background(), tree(), nil)
56+
require.ErrorIs(t, err, sentinel)
57+
}
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 = ["scorer_mock.go"],
6+
importpath = "github.com/uber/submitqueue/submitqueue/extension/speculation/scorer/mock",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//submitqueue/entity:go_default_library",
10+
"//submitqueue/extension/speculation/scorer:go_default_library",
11+
"@org_uber_go_mock//gomock:go_default_library",
12+
],
13+
)

submitqueue/extension/speculation/scorer/mock/scorer_mock.go

Lines changed: 97 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 scorer
16+
17+
//go:generate mockgen -source=scorer.go -destination=mock/scorer_mock.go -package=mock
18+
19+
import (
20+
"context"
21+
22+
"github.com/uber/submitqueue/submitqueue/entity"
23+
)
24+
25+
// Scorer computes the predicted-success score of every path in a batch's
26+
// speculation tree.
27+
//
28+
// A path's score is a prediction — "how likely is this bet to pay off?" — and
29+
// predictions must move as evidence arrives. The scorer answers "how good is
30+
// each path right now" from the current state: the per-batch success
31+
// probabilities of a path's base batches (entity.Batch.Score, set by the score
32+
// stage), which of those dependencies have already landed or had their build
33+
// pass (resolved assumptions raise confidence), and optionally other signals
34+
// (how long the batch has waited, historical pass rates).
35+
//
36+
// The controller re-runs the scorer on every respeculate, right after it
37+
// reconciles path status — so when a dependency lands, its build passes, or a
38+
// sibling path fails, the surviving paths' scores are recomputed against the new
39+
// reality before anything is selected or prioritized. The controller drives
40+
// *when* to rescore and persists the result; the scorer owns the *formula*.
41+
//
42+
// This is the per-*path* scorer, distinct from the per-*batch* score stage
43+
// (extension/scorer), which sets entity.Batch.Score. The path scorer consumes
44+
// those batch scores to score whole paths.
45+
//
46+
// It is a pure function of its inputs: it reads the tree and the dependency
47+
// batches and returns scores only. It never reads storage or builds, and never
48+
// writes status — status is the controller's, fed back in on the tree.
49+
type Scorer interface {
50+
// Score returns the speculation tree with each path's Score set to its
51+
// freshly computed predicted-success value, given the current dependency
52+
// batches (carrying their Batch.Score and current state). Path structure and
53+
// controller-stamped Status are carried through unchanged; only Score is
54+
// (re)computed. The combination formula is the implementation's concern.
55+
Score(ctx context.Context, tree entity.SpeculationTree, deps []entity.Batch) (entity.SpeculationTree, error)
56+
}
57+
58+
// Config carries the per-queue identity handed to a Factory. The system knows
59+
// only the queue name; everything an implementation needs (including scoring
60+
// knobs and extra signals) is injected at construction by the integrator.
61+
type Config struct {
62+
// QueueName identifies the queue this Scorer serves.
63+
QueueName string
64+
}
65+
66+
// Factory builds the Scorer for a queue. Implementations are provided by
67+
// integrators (and tests) and inject whatever they need at construction.
68+
type Factory interface {
69+
// For returns the Scorer for the given queue.
70+
For(cfg Config) (Scorer, error)
71+
}

0 commit comments

Comments
 (0)