Files
coder/coderd/experiments/experimentstest/store.go
T
Thomas Kosiewski 84fe2dda92 feat: add per-user experiment rule evaluator (#29869)
Slice 1 of 4 for runtime experiment rules: adds an internal evaluator
that decides whether an experiment is enabled for a given user, based on
a stored rule and that user's attributes. The package has no storage,
API, or callers yet, so behavior does not change.

## What's in it

- `coderd/experiments`: `Evaluator` with `New(logger, store, static)`,
`Enabled(ctx, userID, ex)` and `EnabledExperiments(ctx, userID)`. It
also adds the `Store` interface, the `Rule`/`StoredRule`/`User` models
and `ValidateRule`.
- Rule modes: `inherit` (use the startup `--experiments` default), `on`,
`off` (a kill switch that wins over a static on), and `condition` (a CEL
expression over `user.id`, `user.username`, `user.email`, `user.roles`,
`user.organizations`, `user.groups`).
- **Fail closed:** the decision is off for a rules-read error, a
malformed rule (including a stored revision below 1) or unknown mode, a
failed attribute load, a compile or eval error, a cost overrun, a
cancelled context, or a `uuid.Nil` subject. This holds even when the
experiment is enabled statically. `New` rejects a nil store, so there is
no fallback.
- **Diagnostics:** only errors returned to the caller carry full CEL
diagnostics. Logs record the experiment, revision, error category and
line:column, never condition text or CEL message text.
- Limits: 4096 code points, cost limit 10,000, and evaluation stops when
the context is cancelled. Compiled programs go in a bounded,
concurrency-safe cache keyed by exact source text. Decisions are never
cached.
- `codersdk.ExperimentsUserScoped = {example}`. Only experiments listed
there accept rules.
- `coderd/experiments/experimentstest`: a fake `Store` for tests.

## Dependency

Adds `cel.dev/cel-go v0.32.0` (plus indirect `antlr4-go/antlr/v4
v4.13.1`). `cel.dev/cel-go` is the module path that
`github.com/google/cel-go` now declares. GHSA-gcjh-h69q-9w9g
(GO-2026-6094) is fixed in 0.29.0. `govulncheck
./coderd/experiments/...` reports nothing against either new module. Its
remaining findings are go1.26.5 standard-library issues, fixed in
go1.26.6, plus existing dependencies, and they show up on unrelated
packages too.

## Stack

1. **This PR:** evaluator library
2. Storage and audit types (`site_configs` rows, revisions, `WriteRule`)
3. Enforcement and discovery (`/api/v2/experiments`, meta tag, chatd
`mcp-tool-search`, ruleguard)
4. Control plane (experimental API, `coder exp experiment-rules` CLI,
docs)

Remote dogfood UAT through Coder Agents passed. `/api/v2/experiments` is
unchanged for admin and member with and without `--experiments=example`,
and the package tests pass with and without `-race`.

> [!NOTE]
> This PR was written by an AI agent (Xum).

---
_Generated with [`xum`](https://github.com/coder/xum) • Model:
`anthropic:claude-opus-5-5` • Thinking: `high`_

---------

Signed-off-by: Thomas Kosiewski <tk@coder.com>
2026-09-29 12:22:59 +00:00

64 lines
1.7 KiB
Go

// Package experimentstest provides test doubles for the experiments
// package.
package experimentstest
import (
"context"
"encoding/json"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/experiments"
"github.com/coder/coder/v2/codersdk"
)
// Store is an in-memory experiments.Store. The zero value is a store with
// no rules and no users.
type Store struct {
// StoredRules is returned by Rules.
StoredRules map[codersdk.Experiment]experiments.StoredRule
// RulesErr, when set, is returned by Rules.
RulesErr error
// Users is read by UserAttributes. Missing users return an error.
Users map[uuid.UUID]experiments.User
// UserErr, when set, is returned by UserAttributes.
UserErr error
}
var _ experiments.Store = Store{}
// Rules implements experiments.Store.
func (s Store) Rules(context.Context) (map[codersdk.Experiment]experiments.StoredRule, error) {
if s.RulesErr != nil {
return nil, s.RulesErr
}
return s.StoredRules, nil
}
// UserAttributes implements experiments.Store.
func (s Store) UserAttributes(_ context.Context, userID uuid.UUID) (experiments.User, error) {
if s.UserErr != nil {
return experiments.User{}, s.UserErr
}
user, ok := s.Users[userID]
if !ok {
return experiments.User{}, xerrors.Errorf("user %s not found", userID)
}
return user, nil
}
// StoredRule encodes rule as it would be stored. A zero Revision is stored
// as 1 because stored rules always have revision 1 or higher.
func StoredRule(t testing.TB, rule experiments.Rule) experiments.StoredRule {
t.Helper()
if rule.Revision == 0 {
rule.Revision = 1
}
value, err := json.Marshal(rule)
require.NoError(t, err)
return experiments.StoredRule{Value: value}
}