Skip to content

Commit 5b2d34f

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. 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: it returns the tree with Score recomputed and the controller persists, staying the single writer of tree state. 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. Interface only; concrete impls and controller wiring are deferred.
1 parent 3f567d1 commit 5b2d34f

9 files changed

Lines changed: 356 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. 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: it returns the scored tree and the controller persists the scores, so the controller stays the single writer of tree state. Path structure and status pass through unchanged; only `Score` is recomputed.
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 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/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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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. By
16+
// default Score echoes the tree it is given; Returns overrides that with a
17+
// canned scored tree, and FailWith injects an error on every call. It is
18+
// intended for examples and tests only, never production.
19+
package fake
20+
21+
import (
22+
"context"
23+
24+
"github.com/uber/submitqueue/submitqueue/entity"
25+
"github.com/uber/submitqueue/submitqueue/extension/speculation/scorer"
26+
)
27+
28+
// Scorer is a programmable scorer.Scorer.
29+
type Scorer struct {
30+
tree entity.SpeculationTree
31+
hasTree bool
32+
err error
33+
}
34+
35+
// New returns a fake Scorer that echoes the tree it is given. Override the
36+
// returned tree with Returns.
37+
func New() *Scorer {
38+
return &Scorer{}
39+
}
40+
41+
// Returns makes every Score call return tree instead of echoing its input.
42+
func (s *Scorer) Returns(tree entity.SpeculationTree) *Scorer {
43+
s.tree = tree
44+
s.hasTree = true
45+
return s
46+
}
47+
48+
// FailWith makes every Score call return err.
49+
func (s *Scorer) FailWith(err error) *Scorer {
50+
s.err = err
51+
return s
52+
}
53+
54+
// Score returns the canned tree if one was set with Returns, otherwise the tree
55+
// it was given, unchanged.
56+
func (s *Scorer) Score(_ context.Context, tree entity.SpeculationTree) (entity.SpeculationTree, error) {
57+
if s.err != nil {
58+
return entity.SpeculationTree{}, s.err
59+
}
60+
if s.hasTree {
61+
return s.tree, nil
62+
}
63+
return tree, nil
64+
}
65+
66+
// ensure the fake satisfies the interface.
67+
var _ scorer.Scorer = (*Scorer)(nil)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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_EchoesInputByDefault(t *testing.T) {
28+
tree := entity.SpeculationTree{
29+
BatchID: "q/batch/2",
30+
Paths: []entity.SpeculationPathInfo{
31+
{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, tree, got)
37+
}
38+
39+
func TestScore_ReturnsCanned(t *testing.T) {
40+
in := entity.SpeculationTree{BatchID: "q/batch/2"}
41+
scored := entity.SpeculationTree{
42+
BatchID: "q/batch/2",
43+
Paths: []entity.SpeculationPathInfo{
44+
{Path: entity.SpeculationPath{Head: "q/batch/2"}, Score: 0.75},
45+
},
46+
}
47+
got, err := New().Returns(scored).Score(context.Background(), in)
48+
require.NoError(t, err)
49+
assert.Equal(t, scored, got)
50+
}
51+
52+
func TestScore_FailWith(t *testing.T) {
53+
sentinel := errors.New("boom")
54+
_, err := New().FailWith(sentinel).Score(context.Background(), entity.SpeculationTree{BatchID: "q/batch/1"})
55+
require.ErrorIs(t, err, sentinel)
56+
}
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
// The controller hands the scorer the batch's speculation tree directly — the
47+
// subject it scores. Any richer signal an implementation needs (the dependency
48+
// batches' scores, historical pass rates) is injected at its Factory, not passed
49+
// here. It never writes: it returns the scored tree and the controller persists
50+
// the scores, keeping the controller the single writer of tree state.
51+
type Scorer interface {
52+
// Score returns the given tree with each path's Score set to its freshly
53+
// computed predicted-success value. Path structure and controller-stamped
54+
// Status are carried through unchanged; only Score is (re)computed. The
55+
// combination formula is the implementation's concern.
56+
Score(ctx context.Context, tree entity.SpeculationTree) (entity.SpeculationTree, error)
57+
}
58+
59+
// Config carries the per-queue identity handed to a Factory. The system knows
60+
// only the queue name; everything an implementation needs (scoring knobs, and
61+
// read access to any extra signals such as the dependency batches' scores) is
62+
// injected at construction by the integrator.
63+
type Config struct {
64+
// QueueName identifies the queue this Scorer serves.
65+
QueueName string
66+
}
67+
68+
// Factory builds the Scorer for a queue. Implementations are provided by
69+
// integrators (and tests) and inject whatever they need at construction.
70+
type Factory interface {
71+
// For returns the Scorer for the given queue.
72+
For(cfg Config) (Scorer, error)
73+
}

0 commit comments

Comments
 (0)