mirror of
https://github.com/coder/coder.git
synced 2026-10-31 19:36:41 +08:00
## Summary Scope chat model configurations and runtime model resolution to organizations. Each model configuration now belongs to exactly one organization, each organization can have its own default model, and Chatd only uses models that belong to the chat's organization. Depends on #27968. ## Problem Chat model configurations are currently deployment-wide even though chats belong to organizations. A default model, explicitly selected model, or configured model override can therefore reference the same model configuration from every organization. This prevents model catalogs and defaults from being isolated by organization and allows a chat to retain or select a model that its organization does not own. Existing model configurations also have no organization ownership or organization-scoped ACL data, and the database enforces a single live default across the entire deployment. ## Fix Add required organization ownership and ACL storage to chat model configurations. The migration assigns every existing configuration to the default organization, seeds organization-wide read access, and replaces the deployment-wide default constraint with a per-organization constraint. Scope Chatd lookup, admission, fallback, and queue-promotion behavior to the chat's organization: - New chats use the usable default model from their organization. - Explicit model selections from another organization are rejected. - Deployment or personal overrides that point to a foreign model are not used for the chat. - Historical cross-organization model references remain unchanged in the database, but runtime generation falls back to a usable local default. - Queue promotion resolves unavailable, disabled, missing, or foreign stored models to the usable local default and fails when no usable local default exists. The existing model configuration management and availability routes remain compatibility routes for the default organization in this layer. A later layer in the stack adds organization-scoped management routes and updates clients to use them. ## Breaking change This changes chat model behavior from deployment-wide to organization-scoped. Existing configurations are migrated only to the default organization; they are not copied into every organization. As a result: - A non-default organization no longer inherits the deployment's existing default model. - Creating a chat in an organization without a usable local default fails with `400: No default chat model config is configured.` - Explicitly selecting a configuration owned by another organization fails with `400: Model config does not belong to the specified organization.` - Existing chats whose stored model belongs to another organization can generate only if a usable local default is available; otherwise generation fails. Deployments using Agents in non-default organizations must configure a usable default chat model separately for each organization after the organization-scoped management layers in this stack are available. _This pull request description was generated by Coder Agents._
41 lines
1.9 KiB
SQL
41 lines
1.9 KiB
SQL
-- Assign every chat model config to an organization. Existing rows belong to
|
|
-- the default organization so current deployments keep their model catalog.
|
|
|
|
-- Step 1: Add nullable column with FK.
|
|
ALTER TABLE chat_model_configs
|
|
ADD COLUMN organization_id UUID REFERENCES organizations(id) ON DELETE CASCADE;
|
|
|
|
-- Step 2: Backfill every existing row to the default org.
|
|
UPDATE chat_model_configs
|
|
SET organization_id = (SELECT id FROM organizations WHERE is_default = true LIMIT 1);
|
|
|
|
-- Step 3: Enforce NOT NULL going forward.
|
|
ALTER TABLE chat_model_configs ALTER COLUMN organization_id SET NOT NULL;
|
|
|
|
-- Step 4: Index for efficient lookups by organization.
|
|
CREATE INDEX idx_chat_model_configs_organization_id ON chat_model_configs (organization_id);
|
|
|
|
-- Step 5: Replace the deployment-wide single-default index with a
|
|
-- per-org one. The predicate is unchanged.
|
|
DROP INDEX idx_chat_model_configs_single_default;
|
|
CREATE UNIQUE INDEX idx_chat_model_configs_single_default
|
|
ON chat_model_configs (organization_id)
|
|
WHERE is_default = true AND deleted = false;
|
|
|
|
-- Step 6: ACL columns (nested chat/workspace shape). They are inert
|
|
-- until the RBAC stage evaluates them.
|
|
ALTER TABLE chat_model_configs
|
|
ADD COLUMN group_acl jsonb DEFAULT '{}'::jsonb NOT NULL,
|
|
ADD COLUMN user_acl jsonb DEFAULT '{}'::jsonb NOT NULL,
|
|
ADD CONSTRAINT chat_model_configs_group_acl_is_object CHECK (jsonb_typeof(group_acl) = 'object'::text),
|
|
ADD CONSTRAINT chat_model_configs_user_acl_is_object CHECK (jsonb_typeof(user_acl) = 'object'::text);
|
|
|
|
-- Step 7: Seed every existing row with the everyone-in-org read entry.
|
|
-- The Everyone group of an organization always has the organization's
|
|
-- own ID (see 000058), so the group_acl key is the org ID itself.
|
|
UPDATE chat_model_configs
|
|
SET group_acl = jsonb_build_object(
|
|
organization_id::text,
|
|
jsonb_build_object('permissions', jsonb_build_array('read'))
|
|
);
|