## Why The Automations RFC controls availability with one user-scoped experiment. Its text: "The user-scoped `chat-automations` experiment controls availability. Operators configure defaults with `--experiments` and can change per-user rules while Coder is running. Each surface checks the relevant caller or automation's owner, and experiment evaluation fails closed." The later backend PRs (management routes, the `manage_automations` tool, `Publish`) need that check first. This PR registers the experiment and proves the per-user, fail-closed check. Nothing consumes it yet. Part of CODAGT-1135 ## What changed - Registers `chat-automations` in `ExperimentsKnown` and `ExperimentsUserScoped`. An earlier experiment with this name was removed in #30412, and this PR restores none of its consumers. - Keeps it out of `ExperimentsSafe`, so `--experiments=*` does not turn it on. Automations act with their owner's authority, and a test pins the choice. - Adds the name to the `userScopedExperimentStaticCheck` ruleguard rule, so server code cannot decide it from the static list. `TestExperimentsUserScopedRuleguard` keeps the rule in step with `ExperimentsUserScoped`. - Regenerates the swagger files, `docs/reference/api/schemas.md` and `typesGenerated.ts`. - Adds tests for the real experiment name. The evaluator tests cover the startup default, stored `on`, `off` and condition rules for two users, and an off decision on a rules read error, a malformed rule, an unknown mode, an attribute read error, a condition error and a nil subject. A coderd test with a real database checks the same per-user behavior through `api.ExperimentEvaluator` and `GET /api/v2/experiments`. ## Scope No routes, tool, UI or chatd change. I added no helper: `Evaluator.Enabled(ctx, userID, ex)` already decides per user and fails closed, and API handlers reach it through `api.ExperimentEvaluator`. chatd has no evaluator handle on main, because #31327 removed the unused field. The first chatd consumer adds it together with its read. ## Blast radius With the experiment off, which is the default, behavior on main does not change. Two things become visible. `GET /api/experimental/experiments/rules` and `coder exp experiment-rules list` show a `chat-automations` row, and an administrator can store a rule for it. A deployment that already passes `--experiments=chat-automations` sees three small differences: the startup log reports an opted-in hidden experiment where it reported an unknown one, the deployment Overview page stops flagging the name as invalid, and the rules row shows the static default as on. Nothing reads the experiment yet, so no feature turns on. ## Live check A local `./scripts/develop.sh` deployment built from this head ran with no flag, with `--experiments=*` and with `--experiments=chat-automations`. `GET /api/v2/experiments` lists `chat-automations` for the owner and for a member only when the startup list or a rule turns it on, and `--experiments=*` does not turn it on. A condition rule `user.username == "member"` turned it on for the member and off for the owner without a restart, and `reset` restored the startup default. --- _Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-sonnet-5-5` • Thinking: `xhigh`_
135 lines
4.9 KiB
Go
135 lines
4.9 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/cli/clitest"
|
|
"github.com/coder/coder/v2/coderd/coderdtest"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
func TestExperimentRules(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
setup := func(t *testing.T, experiments ...string) (*codersdk.Client, *codersdk.Client, func(args ...string) (string, string, error)) {
|
|
t.Helper()
|
|
ownerClient := coderdtest.New(t, &coderdtest.Options{
|
|
DeploymentValues: coderdtest.DeploymentValues(t, func(v *codersdk.DeploymentValues) {
|
|
v.Experiments = experiments
|
|
}),
|
|
})
|
|
owner := coderdtest.CreateFirstUser(t, ownerClient)
|
|
memberClient, _ := coderdtest.CreateAnotherUser(t, ownerClient, owner.OrganizationID)
|
|
run := func(args ...string) (string, string, error) {
|
|
t.Helper()
|
|
inv, root := clitest.New(t, append([]string{"exp", "experiment-rules"}, args...)...)
|
|
clitest.SetupConfig(t, ownerClient, root)
|
|
var stdout, stderr bytes.Buffer
|
|
inv.Stdout = &stdout
|
|
inv.Stderr = &stderr
|
|
err := inv.WithContext(testutil.Context(t, testutil.WaitLong)).Run()
|
|
return stdout.String(), stderr.String(), err
|
|
}
|
|
return ownerClient, memberClient, run
|
|
}
|
|
storedRule := func(ctx context.Context, t *testing.T, client *codersdk.Client, ex codersdk.Experiment) *codersdk.ExperimentRule {
|
|
t.Helper()
|
|
entries, err := codersdk.NewExperimentalClient(client).ExperimentRules(ctx)
|
|
require.NoError(t, err)
|
|
for _, entry := range entries {
|
|
if entry.Experiment == string(ex) {
|
|
return entry.Rule
|
|
}
|
|
}
|
|
t.Fatalf("no rules entry for %q", ex)
|
|
return nil
|
|
}
|
|
requireMemberEnabled := func(ctx context.Context, t *testing.T, member *codersdk.Client, ex codersdk.Experiment, want bool) {
|
|
t.Helper()
|
|
got, err := member.Experiments(ctx)
|
|
require.NoError(t, err)
|
|
require.Equal(t, want, got.Enabled(ex))
|
|
}
|
|
|
|
// Without --expected-revision, each command writes against the
|
|
// current revision, and the change applies to the next request.
|
|
t.Run("WriteAndList", func(t *testing.T) {
|
|
t.Parallel()
|
|
ownerClient, memberClient, run := setup(t)
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
stdout, _, err := run("on", "example")
|
|
require.NoError(t, err)
|
|
require.Contains(t, stdout, `Experiment "example" rule is now on (revision 1).`)
|
|
requireMemberEnabled(ctx, t, memberClient, codersdk.ExperimentExample, true)
|
|
|
|
_, _, err = run("set", "example", `user.username == "owner-only"`)
|
|
require.NoError(t, err)
|
|
requireMemberEnabled(ctx, t, memberClient, codersdk.ExperimentExample, false)
|
|
|
|
_, _, err = run("off", "example")
|
|
require.NoError(t, err)
|
|
_, stderr, err := run("reset", "example")
|
|
require.NoError(t, err)
|
|
require.NotContains(t, stderr, "Reset is not a kill switch")
|
|
rule := storedRule(ctx, t, ownerClient, codersdk.ExperimentExample)
|
|
require.Equal(t, string(codersdk.ExperimentRuleModeInherit), rule.Mode)
|
|
require.Equal(t, int64(4), rule.Revision)
|
|
|
|
stdout, _, err = run("list", "-o", "json")
|
|
require.NoError(t, err)
|
|
var entries []codersdk.ExperimentRuleEntry
|
|
require.NoError(t, json.Unmarshal([]byte(stdout), &entries))
|
|
require.Len(t, entries, len(codersdk.ExperimentsUserScoped))
|
|
require.Equal(t, rule, entries[0].Rule)
|
|
|
|
stdout, _, err = run("list")
|
|
require.NoError(t, err)
|
|
require.Regexp(t, `example\s+false\s+inherit\s+4`, stdout)
|
|
})
|
|
|
|
// A stale --expected-revision fails with the current state and is not
|
|
// retried: the stored rule keeps its revision.
|
|
t.Run("ConflictNotRetried", func(t *testing.T) {
|
|
t.Parallel()
|
|
ownerClient, _, run := setup(t)
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
_, _, err := run("on", "example", "--expected-revision", "0")
|
|
require.NoError(t, err)
|
|
_, stderr, err := run("set", "example", `user.username == "x"`, "--expected-revision", "0")
|
|
require.Error(t, err)
|
|
require.Contains(t, err.Error(), "409")
|
|
require.Contains(t, stderr, `Current rule for experiment "example": on (revision 1)`)
|
|
rule := storedRule(ctx, t, ownerClient, codersdk.ExperimentExample)
|
|
require.Equal(t, string(codersdk.ExperimentRuleModeOn), rule.Mode)
|
|
require.Equal(t, int64(1), rule.Revision)
|
|
})
|
|
|
|
// Reset restores the startup default, which may be on, so it warns
|
|
// that it is not a kill switch.
|
|
t.Run("ResetWarnsWhenStaticDefaultOn", func(t *testing.T) {
|
|
t.Parallel()
|
|
_, memberClient, run := setup(t, string(codersdk.ExperimentExample))
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
stdout, _, err := run("list")
|
|
require.NoError(t, err)
|
|
require.Regexp(t, `example\s+true\s+\(none\)\s+0`, stdout)
|
|
|
|
_, _, err = run("off", "example")
|
|
require.NoError(t, err)
|
|
requireMemberEnabled(ctx, t, memberClient, codersdk.ExperimentExample, false)
|
|
_, stderr, err := run("reset", "example")
|
|
require.NoError(t, err)
|
|
require.Contains(t, stderr, "Reset is not a kill switch")
|
|
requireMemberEnabled(ctx, t, memberClient, codersdk.ExperimentExample, true)
|
|
})
|
|
}
|