Files
coder/coderd/database/queries/skills.sql
T
Michael Suchacz d1f308d9f3 feat: let organization members turn off organization skills in chats (#31134)
## 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.
2026-10-29 12:53:50 +00:00

160 lines
6.0 KiB
SQL

-- name: InsertUserSkill :one
INSERT INTO skills (id, user_id, name, description, content, model_invocable)
VALUES (@id::uuid, @user_id::uuid, @name::text, @description::text, @content::text, COALESCE(sqlc.narg('model_invocable')::boolean, true))
RETURNING *;
-- name: GetUserSkillByUserIDAndName :one
SELECT *
FROM skills
WHERE user_id = @user_id::uuid AND name = @name;
-- name: ListUserSkillMetadataByUserID :many
SELECT
id, user_id, name, description, enabled, action, model_invocable, created_at, updated_at
FROM skills
WHERE user_id = @user_id::uuid
ORDER BY name ASC;
-- name: UpdateUserSkillByUserIDAndName :one
UPDATE skills
SET
description = COALESCE(sqlc.narg('description')::text, description),
content = COALESCE(sqlc.narg('content')::text, content),
enabled = COALESCE(sqlc.narg('enabled')::boolean, enabled),
model_invocable = COALESCE(sqlc.narg('model_invocable')::boolean, model_invocable),
updated_at = now()
WHERE user_id = @user_id::uuid AND name = @name
RETURNING *;
-- name: DeleteUserSkillByUserIDAndName :one
DELETE FROM skills
WHERE user_id = @user_id::uuid AND name = @name
RETURNING *;
-- name: InsertOrganizationSkill :one
INSERT INTO skills (id, organization_id, name, description, content, model_invocable, group_acl, user_acl)
VALUES (@id::uuid, @organization_id::uuid, @name::text, @description::text, @content::text, COALESCE(sqlc.narg('model_invocable')::boolean, true), @group_acl, @user_acl)
RETURNING *;
-- name: GetOrganizationSkillByOrganizationIDAndName :one
SELECT *
FROM skills
WHERE organization_id = @organization_id::uuid AND name = @name;
-- name: ListOrganizationSkillMetadataByOrganizationID :many
SELECT
id, organization_id, name, description, enabled, action, model_invocable, created_at, updated_at
FROM skills
WHERE organization_id = @organization_id::uuid
-- Authorize Filter clause will be injected below in GetAuthorizedOrganizationSkillMetadata
-- @authorize_filter
ORDER BY name ASC;
-- name: UpdateOrganizationSkillByOrganizationIDAndName :one
UPDATE skills
SET
description = COALESCE(sqlc.narg('description')::text, description),
content = COALESCE(sqlc.narg('content')::text, content),
enabled = COALESCE(sqlc.narg('enabled')::boolean, enabled),
model_invocable = COALESCE(sqlc.narg('model_invocable')::boolean, model_invocable),
updated_at = now()
WHERE organization_id = @organization_id::uuid AND name = @name
RETURNING *;
-- name: DeleteOrganizationSkillByOrganizationIDAndName :one
DELETE FROM skills
WHERE organization_id = @organization_id::uuid AND name = @name
RETURNING *;
-- name: GetOrganizationSkillByIDForUpdate :one
SELECT *
FROM skills
WHERE id = @id::uuid AND organization_id IS NOT NULL
FOR UPDATE;
-- name: UpdateOrganizationSkillACLByID :one
UPDATE skills
SET
group_acl = @group_acl,
user_acl = @user_acl,
updated_at = now()
WHERE id = @id::uuid AND organization_id IS NOT NULL
RETURNING *;
-- name: InsertProjectSkill :one
INSERT INTO skills (id, project_id, name, description, content, action, model_invocable)
VALUES (@id::uuid, @project_id::uuid, @name::text, @description::text, @content::text, COALESCE(sqlc.narg('action')::boolean, false), COALESCE(sqlc.narg('model_invocable')::boolean, true))
RETURNING *;
-- name: GetProjectSkillByProjectIDAndName :one
SELECT *
FROM skills
WHERE project_id = @project_id::uuid AND name = @name;
-- name: ListProjectSkillMetadataByProjectID :many
SELECT
id, project_id, name, description, enabled, action, model_invocable, created_at, updated_at
FROM skills
WHERE project_id = @project_id::uuid
ORDER BY name ASC;
-- name: UpdateProjectSkillByProjectIDAndName :one
UPDATE skills
SET
description = COALESCE(sqlc.narg('description')::text, description),
content = COALESCE(sqlc.narg('content')::text, content),
enabled = COALESCE(sqlc.narg('enabled')::boolean, enabled),
model_invocable = COALESCE(sqlc.narg('model_invocable')::boolean, model_invocable),
action = COALESCE(sqlc.narg('action')::boolean, action),
updated_at = now()
WHERE project_id = @project_id::uuid AND name = @name
RETURNING *;
-- name: DeleteProjectSkillByProjectIDAndName :one
DELETE FROM skills
WHERE project_id = @project_id::uuid AND name = @name
RETURNING *;
-- name: GetProjectSkillByIDForUpdate :one
SELECT *
FROM skills
WHERE id = @id::uuid AND project_id IS NOT NULL
FOR UPDATE;
-- name: GetOrganizationSkillMemberPreferences :one
-- Returns the organization's member opt-out setting and the member's stored
-- choices. Members without stored choices get the defaults.
SELECT
organizations.members_can_disable_skills,
COALESCE(s.all_skills_disabled, false)::boolean AS all_skills_disabled,
ARRAY(
SELECT skill_id
FROM organization_member_disabled_skills
WHERE organization_id = @organization_id::uuid AND user_id = @user_id::uuid
ORDER BY skill_id
)::uuid[] AS disabled_skill_ids
FROM organizations
LEFT JOIN organization_member_skill_settings s
ON s.organization_id = organizations.id AND s.user_id = @user_id::uuid
WHERE organizations.id = @organization_id::uuid;
-- name: UpsertOrganizationMemberSkillSettings :exec
INSERT INTO organization_member_skill_settings (organization_id, user_id, all_skills_disabled)
VALUES (@organization_id::uuid, @user_id::uuid, @all_skills_disabled::boolean)
ON CONFLICT (organization_id, user_id) DO UPDATE
SET all_skills_disabled = EXCLUDED.all_skills_disabled;
-- name: InsertOrganizationMemberDisabledSkill :exec
-- Stores nothing unless the skill belongs to the organization.
INSERT INTO organization_member_disabled_skills (organization_id, user_id, skill_id)
SELECT @organization_id::uuid, @user_id::uuid, skills.id
FROM skills
WHERE skills.id = @skill_id::uuid AND skills.organization_id = @organization_id::uuid
ON CONFLICT DO NOTHING;
-- name: DeleteOrganizationMemberDisabledSkill :exec
DELETE FROM organization_member_disabled_skills
WHERE organization_id = @organization_id::uuid
AND user_id = @user_id::uuid
AND skill_id = @skill_id::uuid;