## Stack Organization skills load into every member's Coder Agents chats in their organization, with no way for a member to opt out. This stack adds a per-organization setting that lets members turn them off for their own chats, bottom to top: 1. **#31134** data model, API, and chatd (this PR) 2. #31135 admin and member controls in the dashboard, the `/` menu, and docs ## What changes Organizations get `members_can_disable_skills`, off by default. Users who can update organization skills change it with `PUT /api/experimental/organizations/{organization}/skill-settings`, which writes an organization audit entry. Anyone who can read the organization can `GET` it. While the setting is on, each member saves their own choices with `PATCH /api/experimental/organizations/{organization}/members/{user}/skill-preferences`: `all_skills_disabled` (a master switch that also covers skills created later) and `skills`, a sparse map from skill ID to on or off. `GET` returns the setting, the master switch, and `disabled_skill_ids`. The routes follow the personal-settings rules: without `read_personal` or `update_personal` on that user the caller gets 404, a PATCH while the setting is off gets 403, and a key that is not an organization skill that member can read gets a 400 that names the key. The check runs as the member, not the caller, so an owner or user admin editing someone else's choices is held to that member's skills. Turning the setting off keeps the choices but ignores them. Migration 000636 adds the column and two tables keyed by organization membership. Both cascade when the membership goes away, and the per-skill table also cascades when the skill is deleted, so a recreated skill loads by default. chatd reads the preferences as the chat owner when it builds the skill index and when it loads a body for `read_skill` or for a structured skill invocation (#30861). A skill the member turned off is left out of the index, and both loads report it as not found, including later in a running chat. If the preferences cannot be read, turn preparation fails, as it already does when the skill metadata cannot be read. Older migration tests created organizations with `dbgen.Organization` and `GetDefaultOrganization`, which now select the new column before it exists, so they insert organizations with raw SQL instead. ## Review focus - Authorization in `coderd/organizationskillpreferences.go` and the five new dbauthz methods (`read_personal` and `update_personal` on the user, `organization_skill` update for the setting). - `GetOrganizationSkillMemberPreferencesRow.Excludes` is the one rule chatd applies; #31135 mirrors it in `activeOrganizationSkills`. ## Testing - Go tests cover the queries and cascades, dbauthz, both routes (including the audit diff and each 400 case), and chatd (index, `read_skill`, skill invocations, master switch, a change during a chat). Each new test fails with its code path removed. - Remote dogfood UAT (10 scenarios, real model) passed on the pre-split branch head `186c28d81ab`, which has the same code except the fix below. It found one issue: two spellings of one skill ID with opposite values stored a random result. The PATCH now rejects every aliased key with a 400. A second run on the stack's top head at the time, `718dd6d1bd1`, passed: every aliased form returns 400 and stores nothing, and chats leave out a skill the member turned off, including in `read_skill`. - Polish and the test audit stayed inside the implementation diff. No new dependencies. - Rebased on main as one commit. The migration is now 000636, after main's 000634 and 000635. Every organization skill load passes the chat owner, including main's skill invocation path, so `/skill` invocations honor the opt-out too. The model's skill index leaves out a skill that is not model-invocable or that the member turned off. Apart from that, the diff is unchanged. > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description.
151 lines
6.2 KiB
Go
151 lines
6.2 KiB
Go
package migrations_test
|
|
|
|
import (
|
|
"database/sql"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbgen"
|
|
"github.com/coder/coder/v2/testutil"
|
|
)
|
|
|
|
const (
|
|
insertConnectionLog000616 = `INSERT INTO connection_logs (id, connect_time, organization_id,
|
|
workspace_owner_id, workspace_id, workspace_name, agent_name, type, slug_or_port)
|
|
VALUES ($1, now(), $2, $3, $4, $5, 'main', $6, $7)`
|
|
insertConnectionLog000617 = `INSERT INTO connection_logs (id, connect_time, organization_id,
|
|
workspace_owner_id, workspace_id, workspace_name, agent_name, connection_method, app_name_or_port)
|
|
VALUES ($1, now(), $2, $3, $4, $5, 'main', $6, $7)`
|
|
selectConnectionLog000616 = `SELECT type::text, slug_or_port FROM connection_logs WHERE id = $1`
|
|
selectConnectionLog000617 = `SELECT connection_method::text, app_name_or_port FROM connection_logs WHERE id = $1`
|
|
)
|
|
|
|
// A pending migration batch can add tunnel to the old enum and then replace
|
|
// that enum. Comparisons must not materialize its uncommitted new value.
|
|
func TestMigration000617ConnectionLogsMethodInSingleTxn(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
|
|
sqlDB := testSQLDB(t)
|
|
ctx := testutil.Context(t, testutil.WaitSuperLong)
|
|
applyMigrationsInTxn(ctx, t, sqlDB, 1, 556)
|
|
orgID, ownerID, templateID, workspaceID := uuid.New(), uuid.New(), uuid.New(), uuid.New()
|
|
_, err := sqlDB.ExecContext(ctx, `
|
|
INSERT INTO organizations (id, name, description, display_name, created_at, updated_at, default_org_member_roles)
|
|
VALUES ($1, 'migration-org', '', '', now(), now(), '{}');
|
|
`, orgID)
|
|
require.NoError(t, err)
|
|
_, err = sqlDB.ExecContext(ctx, `
|
|
INSERT INTO users (id, email, username, hashed_password, created_at, updated_at) VALUES ($1, 'migration@example.com', 'migration-user', '', now(), now());
|
|
`, ownerID)
|
|
require.NoError(t, err)
|
|
_, err = sqlDB.ExecContext(ctx, `
|
|
INSERT INTO templates (id, created_at, updated_at, organization_id, name, provisioner, active_version_id, created_by)
|
|
VALUES ($1, now(), now(), $2, 'migration-template', 'echo', $3, $4);
|
|
`, templateID, orgID, uuid.New(), ownerID)
|
|
require.NoError(t, err)
|
|
_, err = sqlDB.ExecContext(ctx, `
|
|
INSERT INTO workspaces (id, created_at, updated_at, owner_id, organization_id, template_id, name)
|
|
VALUES ($1, now(), now(), $2, $3, $4, 'migration-workspace');
|
|
`, workspaceID, ownerID, orgID, templateID)
|
|
require.NoError(t, err)
|
|
id := uuid.New()
|
|
_, err = sqlDB.ExecContext(ctx, insertConnectionLog000616, id, orgID, ownerID, workspaceID, "migration-workspace", "vscode", nil)
|
|
require.NoError(t, err)
|
|
|
|
applyMigrationsInTxn(ctx, t, sqlDB, 557, 617)
|
|
var method string
|
|
var appName sql.NullString
|
|
require.NoError(t, sqlDB.QueryRowContext(ctx, selectConnectionLog000617, id).Scan(&method, &appName))
|
|
require.Equal(t, "ssh", method)
|
|
require.Equal(t, sql.NullString{String: "vscode", Valid: true}, appName)
|
|
}
|
|
|
|
// The up migration splits each connection type into a method and an app name,
|
|
// and the down migration folds app names back into types.
|
|
func TestMigration000617ConnectionLogsMethod(t *testing.T) {
|
|
t.Parallel()
|
|
if testing.Short() {
|
|
t.SkipNow()
|
|
}
|
|
|
|
sqlDB := testSQLDB(t)
|
|
stepTo(t, sqlDB, 616)
|
|
ctx := testutil.Context(t, testutil.WaitLong)
|
|
|
|
db := database.New(sqlDB)
|
|
orgID := insertMigrationTestOrganization(ctx, t, sqlDB, []string{})
|
|
owner := dbgen.User(t, db, database.User{})
|
|
tpl := dbgen.Template(t, db, database.Template{OrganizationID: orgID, CreatedBy: owner.ID})
|
|
ws := dbgen.Workspace(t, db, database.WorkspaceTable{OrganizationID: orgID, OwnerID: owner.ID, TemplateID: tpl.ID})
|
|
|
|
// row is (type, slug_or_port) before 000617 and
|
|
// (connection_method, app_name_or_port) after it.
|
|
type row struct {
|
|
kind string
|
|
appNameOrPort sql.NullString
|
|
}
|
|
app := func(name string) sql.NullString { return sql.NullString{String: name, Valid: true} }
|
|
ids := map[row]uuid.UUID{}
|
|
insert := func(query string, r row) {
|
|
ids[r] = uuid.New()
|
|
_, err := sqlDB.ExecContext(ctx, query, ids[r], orgID, owner.ID, ws.ID, ws.Name, r.kind, r.appNameOrPort)
|
|
require.NoError(t, err)
|
|
}
|
|
read := func(query string, r row) (got row) {
|
|
require.NoError(t, sqlDB.QueryRowContext(ctx, query, ids[r]).Scan(&got.kind, &got.appNameOrPort))
|
|
return got
|
|
}
|
|
migrate := func(file string) {
|
|
migration, err := os.ReadFile(file)
|
|
require.NoError(t, err)
|
|
_, err = sqlDB.ExecContext(ctx, string(migration))
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
// Each released type and what the up migration turns it into.
|
|
converted := map[row]row{
|
|
{"ssh", sql.NullString{}}: {"ssh", sql.NullString{}},
|
|
{"vscode", sql.NullString{}}: {"ssh", app("vscode")},
|
|
{"jetbrains", sql.NullString{}}: {"ssh", app("jetbrains")},
|
|
{"reconnecting_pty", sql.NullString{}}: {"reconnecting_pty", sql.NullString{}},
|
|
{"workspace_app", app("code-server")}: {"workspace_app", app("code-server")},
|
|
{"port_forwarding", app("8080")}: {"port_forwarding", app("8080")},
|
|
{"tunnel", sql.NullString{}}: {"tunnel", sql.NullString{}},
|
|
}
|
|
for old := range converted {
|
|
insert(insertConnectionLog000616, old)
|
|
}
|
|
migrate("000617_connection_logs_method_app_name.up.sql")
|
|
for old, want := range converted {
|
|
require.Equal(t, want, read(selectConnectionLog000617, old), old.kind)
|
|
}
|
|
|
|
// App names only newer servers write. The down migration folds them into
|
|
// types with its snapshot of the app registry.
|
|
folded := map[row]row{
|
|
{"ssh", app("cursor")}: {"vscode", sql.NullString{}},
|
|
{"ssh", app("trae_cn")}: {"vscode", sql.NullString{}},
|
|
{"ssh", app("goland")}: {"jetbrains", sql.NullString{}},
|
|
{"ssh", app("zed")}: {"ssh", sql.NullString{}},
|
|
{"ssh", app("a_new_ide")}: {"ssh", sql.NullString{}},
|
|
{"reconnecting_pty", app("cursor")}: {"reconnecting_pty", sql.NullString{}},
|
|
}
|
|
for current := range folded {
|
|
insert(insertConnectionLog000617, current)
|
|
}
|
|
migrate("000617_connection_logs_method_app_name.down.sql")
|
|
for old := range converted {
|
|
require.Equal(t, old, read(selectConnectionLog000616, old), old.kind)
|
|
}
|
|
for current, want := range folded {
|
|
require.Equal(t, want, read(selectConnectionLog000616, current), current.kind+" "+current.appNameOrPort.String)
|
|
}
|
|
}
|