Removes the experimental Chat Automations feature (`chat-automations` experiment) from main. This reverts the 16-PR stack that added it and adds two forward migrations that undo its schema and credential changes.
> [!WARNING]
> **Upgrade consequences**
>
> - **Automation data is deleted.** Migration 000615 drops the `chat_automations` table and deletes every queued message an automation delivered. Chats, their messages and ordinary queued messages stay. Messages lose their automation provenance (`automation_id`, `input_id`).
> - **Automation-only credentials are revoked.** Migration 000616 deletes API keys, OAuth2 codes and OAuth2 tokens whose only scopes (or allow-list entries) were `chat_automation:*`. Mixed credentials keep their other scopes. Custom roles lose their `chat_automation` permissions. OAuth2 apps that allowed only `chat_automation` scopes get the whitespace allowlist that grants nothing, as in 000595.
> - **A coordinated restart is required.** Stop all old replicas before the new version runs the migrations. Old replicas read the dropped table and columns, so a rolling deploy is not supported.
> - **Remove `--chat-max-automations-per-owner` before upgrading.** The flag, the YAML key `chat.maxAutomationsPerOwner` and the env var `CODER_CHAT_MAX_AUTOMATIONS_PER_OWNER` are removed. A deployment that still sets the flag or the YAML key will not start (`unknown flag` / `unknown option`). The env var is ignored. This includes dogfood. No release or tag contains the flag.
> - **Down migrations restore the schema only.** 000616 down and 000615 down recreate the 000611 schema with empty tables. They do not restore deleted automations, webhook secrets, provenance, queued deliveries, revoked credentials, stripped permissions or the deleted experiment rule.
## What is removed
- The `chat-automations` experiment and its stored runtime rule (`experiment_rule:chat-automations`).
- The `--chat-max-automations-per-owner` deployment option.
- The `chat_automation` RBAC resource, its member and chatd grants, and the `chat_automation:*` API key scopes.
- The automation sqlc queries, dbauthz wrappers and audit table entries.
- The management routes under `/api/experimental/organizations/{organization}/chat-automations`, and the webhook route `POST /api/experimental/chat-automations/{automation}/events` with its rate limiter.
- The chatd management service, webhook publish, schedule loop, the `manage_automations` tool, and the chatstate admission seam and queue promotion guard that only automations used.
- The Agents Automations page, editor, nav entry, chat labels, the Manage automations toggle and the API client.
- The `archived:any` chat search value that the automations "View chats" dialog added.
- `docs/ai-coder/agents/automations.md`, its manifest entry, the glossary entry and the related rows.
Reverted PRs: #30085, #30096, #30112, #30117, #30127, #30134, #30160, #30166, #30167, #30171, #30187, #30207, #30214, #30217, #30219, #30220.
## What stays, and why
- Migrations 000609, 000610 and 000611, their fixture, and the #30297 lock fix with `migration000609_test.go` stay unchanged. Deployments such as dogfood already ran them.
- New migration **000615** reverses the schema changes of 000609 and 000611 against the current schema. It uses the same short-wait lock-and-retry block as 000609, and it rebuilds `chats_expanded` from main's current definition minus the two automation columns. New migration **000616** follows the 000595 Tasks removal for credentials and recreates `api_key_scope` without the five `chat_automation:*` values. It keeps the `chat_project_memory:*` values from 000612.
- The `chat_automation` value stays in the `resource_type` enum, and `codersdk.ResourceTypeChatAutomation` stays. Old audit log rows stay readable, as with `task` after the Tasks removal.
- The 000615 delete fires the existing queue-version trigger, so `queue_version` moves to `snapshot_version` for each affected chat. `snapshot_version` is not bumped: the coordinated restart restarts every stream loop from an empty state, and the site does not read the version fields.
- `coderd/x/chatd/ARCHITECTURE.md` drops the sections the stack added and gets back its pre-stack wording, merged with the later main edits to the same file.
## Notes for reviewers
- #30379 (chat project page) merged while this PR was in progress. It passed `manageAutomationsEnabled` in `AgentCreatePage.test.tsx` and listed an `automations` route in `AgentsPageLayout.test.tsx`. Later main commits also added automation props in `ConversationTimeline.test.tsx` and `ConversationTimelineWorkingBlocks.stories.tsx`. This PR removes all of them.
- Remaining `automation` matches under `coderd`, `codersdk`, `enterprise`, `site/src` and `docs` are intended:
- migrations 000609 to 000611, their fixture and `migration000609_test.go`;
- the new 000615 and 000616 migrations and `migration000615_test.go`;
- the `chat_automation` resource type in `dump.sql`, `models.go`, `codersdk/audit.go`, the generated API docs, `swagger.json` and `typesGenerated.ts`;
- unrelated uses of the word "automation" (docs, `chatlabels_test.go`, `ratelimit.go`, `autostop.go`, `subagent_catalog.go`, `WorkspaceSharingSection.tsx`, `AgentChatPage.stories.tsx`).
Remote UAT on an isolated dev deployment passed. It upgraded a populated pre-removal database, then checked chats, queued messages, tokens, the 404 routes, the removed page and nav entry, and the experiments list.
---
_Generated with [`xum`](https://github.com/coder/xum) • Model: `anthropic:claude-opus-5-5` • Thinking: `high`_

331 lines
12 KiB
Go
331 lines
12 KiB
Go
package codersdk
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/netip"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type ResourceType string
|
|
|
|
const (
|
|
ResourceTypeTemplate ResourceType = "template"
|
|
ResourceTypeTemplateVersion ResourceType = "template_version"
|
|
ResourceTypeUser ResourceType = "user"
|
|
ResourceTypeWorkspace ResourceType = "workspace"
|
|
ResourceTypeWorkspaceBuild ResourceType = "workspace_build"
|
|
ResourceTypeGitSSHKey ResourceType = "git_ssh_key"
|
|
ResourceTypeAPIKey ResourceType = "api_key"
|
|
ResourceTypeGroup ResourceType = "group"
|
|
ResourceTypeLicense ResourceType = "license"
|
|
ResourceTypeConvertLogin ResourceType = "convert_login"
|
|
ResourceTypeHealthSettings ResourceType = "health_settings"
|
|
ResourceTypeNotificationsSettings ResourceType = "notifications_settings"
|
|
ResourceTypePrebuildsSettings ResourceType = "prebuilds_settings"
|
|
ResourceTypeOAuth2ProviderSettings ResourceType = "oauth2_provider_settings"
|
|
ResourceTypeWorkspaceProxy ResourceType = "workspace_proxy"
|
|
ResourceTypeOrganization ResourceType = "organization"
|
|
ResourceTypeOAuth2ProviderApp ResourceType = "oauth2_provider_app"
|
|
// nolint:gosec // This is not a secret.
|
|
ResourceTypeOAuth2ProviderAppSecret ResourceType = "oauth2_provider_app_secret"
|
|
ResourceTypeCustomRole ResourceType = "custom_role"
|
|
ResourceTypeOrganizationMember ResourceType = "organization_member"
|
|
ResourceTypeNotificationTemplate ResourceType = "notification_template"
|
|
ResourceTypeIdpSyncSettingsOrganization ResourceType = "idp_sync_settings_organization"
|
|
ResourceTypeIdpSyncSettingsGroup ResourceType = "idp_sync_settings_group"
|
|
ResourceTypeIdpSyncSettingsRole ResourceType = "idp_sync_settings_role"
|
|
// Deprecated: Workspace Agent connections are now included in the
|
|
// connection log.
|
|
ResourceTypeWorkspaceAgent ResourceType = "workspace_agent"
|
|
// Deprecated: Workspace App connections are now included in the
|
|
// connection log.
|
|
ResourceTypeWorkspaceApp ResourceType = "workspace_app"
|
|
// ResourceTypeTask is retained for audit logs recorded before the Tasks
|
|
// feature was removed.
|
|
ResourceTypeTask ResourceType = "task"
|
|
ResourceTypeAISeat ResourceType = "ai_seat"
|
|
ResourceTypeAIProvider ResourceType = "ai_provider"
|
|
ResourceTypeAIProviderKey ResourceType = "ai_provider_key"
|
|
ResourceTypeAIGatewayKey ResourceType = "ai_gateway_key"
|
|
ResourceTypeGroupAIBudget ResourceType = "group_ai_budget"
|
|
ResourceTypeUserAIBudgetOverride ResourceType = "user_ai_budget_override"
|
|
ResourceTypeChat ResourceType = "chat"
|
|
ResourceTypeChatProject ResourceType = "chat_project"
|
|
ResourceTypeChatProjectMemory ResourceType = "chat_project_memory"
|
|
ResourceTypeMCPServerConfig ResourceType = "mcp_server_config"
|
|
ResourceTypeChatModelConfig ResourceType = "chat_model_config"
|
|
// ResourceTypeChatAutomation is retained for audit logs recorded before
|
|
// the chat automations experiment was removed.
|
|
ResourceTypeChatAutomation ResourceType = "chat_automation"
|
|
ResourceTypeUserSecret ResourceType = "user_secret"
|
|
ResourceTypeUserSkill ResourceType = "user_skill"
|
|
ResourceTypeChatInstructionSettings ResourceType = "chat_instruction_settings"
|
|
ResourceTypeChatOperationalSettings ResourceType = "chat_operational_settings"
|
|
ResourceTypeChatOrganizationSystemPrompt ResourceType = "chat_organization_system_prompt"
|
|
ResourceTypeExperimentRule ResourceType = "experiment_rule"
|
|
)
|
|
|
|
func (r ResourceType) FriendlyString() string {
|
|
switch r {
|
|
case ResourceTypeTemplate:
|
|
return "template"
|
|
case ResourceTypeTemplateVersion:
|
|
return "template version"
|
|
case ResourceTypeUser:
|
|
return "user"
|
|
case ResourceTypeWorkspace:
|
|
return "workspace"
|
|
case ResourceTypeWorkspaceBuild:
|
|
// workspace builds have a unique friendly string
|
|
// see coderd/audit.go:298 for explanation
|
|
return "workspace"
|
|
case ResourceTypeGitSSHKey:
|
|
return "git ssh key"
|
|
case ResourceTypeAPIKey:
|
|
return "token"
|
|
case ResourceTypeGroup:
|
|
return "group"
|
|
case ResourceTypeLicense:
|
|
return "license"
|
|
case ResourceTypeConvertLogin:
|
|
return "login type conversion"
|
|
case ResourceTypeWorkspaceProxy:
|
|
return "workspace proxy"
|
|
case ResourceTypeOrganization:
|
|
return "organization"
|
|
case ResourceTypeHealthSettings:
|
|
return "health_settings"
|
|
case ResourceTypeNotificationsSettings:
|
|
return "notifications_settings"
|
|
case ResourceTypePrebuildsSettings:
|
|
return "prebuilds_settings"
|
|
case ResourceTypeOAuth2ProviderSettings:
|
|
return "oauth2 provider settings"
|
|
case ResourceTypeOAuth2ProviderApp:
|
|
return "oauth2 app"
|
|
case ResourceTypeOAuth2ProviderAppSecret:
|
|
return "oauth2 app secret"
|
|
case ResourceTypeCustomRole:
|
|
return "custom role"
|
|
case ResourceTypeOrganizationMember:
|
|
return "organization member"
|
|
case ResourceTypeNotificationTemplate:
|
|
return "notification template"
|
|
case ResourceTypeIdpSyncSettingsOrganization:
|
|
return "settings"
|
|
case ResourceTypeIdpSyncSettingsGroup:
|
|
return "settings"
|
|
case ResourceTypeIdpSyncSettingsRole:
|
|
return "settings"
|
|
case ResourceTypeWorkspaceAgent:
|
|
return "workspace agent"
|
|
case ResourceTypeWorkspaceApp:
|
|
return "workspace app"
|
|
case ResourceTypeTask:
|
|
return "task"
|
|
case ResourceTypeAISeat:
|
|
return "ai seat"
|
|
case ResourceTypeAIProvider:
|
|
return "ai provider"
|
|
case ResourceTypeAIProviderKey:
|
|
return "ai provider key"
|
|
case ResourceTypeAIGatewayKey:
|
|
return "ai gateway key"
|
|
case ResourceTypeGroupAIBudget:
|
|
return "group ai budget"
|
|
case ResourceTypeUserAIBudgetOverride:
|
|
return "user ai budget override"
|
|
case ResourceTypeChat:
|
|
return "chat"
|
|
case ResourceTypeChatProject:
|
|
return "chat project"
|
|
case ResourceTypeChatProjectMemory:
|
|
return "chat project memory"
|
|
case ResourceTypeMCPServerConfig:
|
|
return "mcp server config"
|
|
case ResourceTypeChatModelConfig:
|
|
return "chat model config"
|
|
case ResourceTypeChatAutomation:
|
|
return "chat automation"
|
|
case ResourceTypeUserSecret:
|
|
return "user secret"
|
|
case ResourceTypeUserSkill:
|
|
return "user skill"
|
|
case ResourceTypeChatInstructionSettings:
|
|
return "chat instruction settings"
|
|
case ResourceTypeChatOperationalSettings:
|
|
return "chat operational settings"
|
|
case ResourceTypeChatOrganizationSystemPrompt:
|
|
return "chat organization system prompt"
|
|
case ResourceTypeExperimentRule:
|
|
return "experiment rule"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
type AuditAction string
|
|
|
|
const (
|
|
AuditActionCreate AuditAction = "create"
|
|
AuditActionWrite AuditAction = "write"
|
|
AuditActionDelete AuditAction = "delete"
|
|
AuditActionStart AuditAction = "start"
|
|
AuditActionStop AuditAction = "stop"
|
|
AuditActionLogin AuditAction = "login"
|
|
AuditActionLogout AuditAction = "logout"
|
|
AuditActionRegister AuditAction = "register"
|
|
AuditActionRequestPasswordReset AuditAction = "request_password_reset"
|
|
// Deprecated: Workspace connections are now included in the
|
|
// connection log.
|
|
AuditActionConnect AuditAction = "connect"
|
|
// Deprecated: Workspace disconnections are now included in the
|
|
// connection log.
|
|
AuditActionDisconnect AuditAction = "disconnect"
|
|
// Deprecated: Workspace App connections are now included in the
|
|
// connection log.
|
|
AuditActionOpen AuditAction = "open"
|
|
// Deprecated: This action is unused.
|
|
AuditActionClose AuditAction = "close"
|
|
)
|
|
|
|
func (a AuditAction) Friendly() string {
|
|
switch a {
|
|
case AuditActionCreate:
|
|
return "created"
|
|
case AuditActionWrite:
|
|
return "updated"
|
|
case AuditActionDelete:
|
|
return "deleted"
|
|
case AuditActionStart:
|
|
return "started"
|
|
case AuditActionStop:
|
|
return "stopped"
|
|
case AuditActionLogin:
|
|
return "logged in"
|
|
case AuditActionLogout:
|
|
return "logged out"
|
|
case AuditActionRegister:
|
|
return "registered"
|
|
case AuditActionRequestPasswordReset:
|
|
return "password reset requested"
|
|
case AuditActionConnect:
|
|
return "connected"
|
|
case AuditActionDisconnect:
|
|
return "disconnected"
|
|
case AuditActionOpen:
|
|
return "opened"
|
|
case AuditActionClose:
|
|
return "closed"
|
|
default:
|
|
return "unknown"
|
|
}
|
|
}
|
|
|
|
type AuditDiff map[string]AuditDiffField
|
|
|
|
type AuditDiffField struct {
|
|
Old any `json:"old,omitempty"`
|
|
New any `json:"new,omitempty"`
|
|
Secret bool `json:"secret"`
|
|
}
|
|
|
|
type AuditLog struct {
|
|
ID uuid.UUID `json:"id" format:"uuid"`
|
|
RequestID uuid.UUID `json:"request_id" format:"uuid"`
|
|
Time time.Time `json:"time" format:"date-time"`
|
|
IP netip.Addr `json:"ip"`
|
|
UserAgent string `json:"user_agent"`
|
|
ResourceType ResourceType `json:"resource_type"`
|
|
ResourceID uuid.UUID `json:"resource_id" format:"uuid"`
|
|
// ResourceTarget is the name of the resource.
|
|
ResourceTarget string `json:"resource_target"`
|
|
ResourceIcon string `json:"resource_icon"`
|
|
Action AuditAction `json:"action"`
|
|
Diff AuditDiff `json:"diff"`
|
|
StatusCode int32 `json:"status_code"`
|
|
AdditionalFields json.RawMessage `json:"additional_fields" swaggertype:"object"`
|
|
Description string `json:"description"`
|
|
ResourceLink string `json:"resource_link"`
|
|
IsDeleted bool `json:"is_deleted"`
|
|
|
|
// Deprecated: Use 'organization.id' instead.
|
|
OrganizationID uuid.UUID `json:"organization_id" format:"uuid"`
|
|
|
|
Organization *MinimalOrganization `json:"organization,omitempty"`
|
|
|
|
User *User `json:"user"`
|
|
}
|
|
|
|
type AuditLogsRequest struct {
|
|
SearchQuery string `json:"q,omitempty"`
|
|
Pagination
|
|
}
|
|
|
|
type AuditLogResponse struct {
|
|
AuditLogs []AuditLog `json:"audit_logs"`
|
|
Count int64 `json:"count"`
|
|
CountCap int64 `json:"count_cap"`
|
|
}
|
|
|
|
type CreateTestAuditLogRequest struct {
|
|
Action AuditAction `json:"action,omitempty" enums:"create,write,delete,start,stop"`
|
|
ResourceType ResourceType `json:"resource_type,omitempty" enums:"template,template_version,user,workspace,workspace_build,git_ssh_key,auditable_group"`
|
|
ResourceID uuid.UUID `json:"resource_id,omitempty" format:"uuid"`
|
|
AdditionalFields json.RawMessage `json:"additional_fields,omitempty"`
|
|
Time time.Time `json:"time,omitempty" format:"date-time"`
|
|
BuildReason BuildReason `json:"build_reason,omitempty" enums:"autostart,autostop,initiator"`
|
|
OrganizationID uuid.UUID `json:"organization_id,omitempty" format:"uuid"`
|
|
RequestID uuid.UUID `json:"request_id,omitempty" format:"uuid"`
|
|
}
|
|
|
|
// AuditLogs retrieves audit logs from the given page.
|
|
func (c *Client) AuditLogs(ctx context.Context, req AuditLogsRequest) (AuditLogResponse, error) {
|
|
res, err := c.Request(ctx, http.MethodGet, "/api/v2/audit", nil, req.asRequestOption(), func(r *http.Request) {
|
|
q := r.URL.Query()
|
|
var params []string
|
|
if req.SearchQuery != "" {
|
|
params = append(params, req.SearchQuery)
|
|
}
|
|
q.Set("q", strings.Join(params, " "))
|
|
r.URL.RawQuery = q.Encode()
|
|
})
|
|
if err != nil {
|
|
return AuditLogResponse{}, err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
return AuditLogResponse{}, ReadBodyAsError(res)
|
|
}
|
|
|
|
var logRes AuditLogResponse
|
|
err = ReadBodyAsJSON(res, &logRes)
|
|
if err != nil {
|
|
return AuditLogResponse{}, err
|
|
}
|
|
|
|
return logRes, nil
|
|
}
|
|
|
|
// CreateTestAuditLog creates a fake audit log. Only owners of the organization
|
|
// can perform this action. It's used for testing purposes.
|
|
func (c *Client) CreateTestAuditLog(ctx context.Context, req CreateTestAuditLogRequest) error {
|
|
res, err := c.Request(ctx, http.MethodPost, "/api/v2/audit/testgenerate", req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusNoContent {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|