Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
fefeee9
feat(coderd): run chat automation schedules
ThomasK33 Sep 29, 2026
dd1d9e9
fix(coderd): authorize chatd schedule cursor writes on the automation
ThomasK33 Sep 30, 2026
0787d08
Merge remote-tracking branch 'origin/tk/codagt-1210b-automations-new-…
ThomasK33 Sep 30, 2026
d4b00aa
Merge remote-tracking branch 'origin/tk/codagt-1210b-automations-new-…
ThomasK33 Sep 30, 2026
84dd44b
feat(coderd/x/chatd): audit chats that new_chat schedules create
ThomasK33 Sep 30, 2026
d355630
fix(coderd/x/chatd): page through due schedules and retry experiment …
ThomasK33 Sep 30, 2026
0514751
fix(coderd/x/chatd): publish schedule occurrences concurrently
ThomasK33 Sep 30, 2026
d483cd5
chore(coderd/x/chatd): retrigger CI for the stacked PR
ThomasK33 Sep 30, 2026
f6d5075
Merge tk/codagt-1210b-automations-new-chat (main d9c13dc06d) into tk/…
ThomasK33 Oct 1, 2026
e057356
Merge tk/codagt-1210b-automations-new-chat (main 35abc22fa9) into tk/…
ThomasK33 Oct 1, 2026
7971eb5
Merge tk/codagt-1210b-automations-new-chat (main d11809f095) into tk/…
ThomasK33 Oct 1, 2026
67db54f
refactor(coderd/x/chatd): make the automation schedule batch size an …
ThomasK33 Oct 1, 2026
8b43e65
Merge tk/codagt-1210b-automations-new-chat (main a328bee007) into tk/…
ThomasK33 Oct 1, 2026
e31492f
test(coderd/x/chatd): drop the testpackage suppression from the sched…
ThomasK33 Oct 1, 2026
4a18d06
Merge tk/codagt-1210b-automations-new-chat (main 15382ebd5d) into tk/…
ThomasK33 Oct 2, 2026
a7a510c
Merge tk/codagt-1210b-automations-new-chat (main 15382ebd5d) into tk/…
ThomasK33 Oct 2, 2026
d72e1b9
Merge tk/codagt-1210b-automations-new-chat (main dc07908504) into tk/…
ThomasK33 Oct 2, 2026
21bc13e
Merge tk/codagt-1210b-automations-new-chat into tk/codagt-1211-automa…
ThomasK33 Oct 2, 2026
549f506
Merge tk/codagt-1210b-automations-new-chat into tk/codagt-1211-automa…
ThomasK33 Oct 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions coderd/database/dbauthz/dbauthz.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,12 @@ var (
Identifier: rbac.RoleIdentifier{Name: "chatd"},
DisplayName: "Chat Daemon",
Site: rbac.Permissions(map[string][]policy.Action{
rbac.ResourceAIProvider.Type: {policy.ActionRead},
rbac.ResourceChat.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete},
rbac.ResourceChatAutomation.Type: {policy.ActionRead},
rbac.ResourceAIProvider.Type: {policy.ActionRead},
rbac.ResourceChat.Type: {policy.ActionCreate, policy.ActionRead, policy.ActionUpdate, policy.ActionDelete},
// Update lets the schedule loop move automation cursors
// (AdvanceChatAutomationScheduleCursor). Owner-only
// changes are enforced by the chatd automation service.
rbac.ResourceChatAutomation.Type: {policy.ActionRead, policy.ActionUpdate},
rbac.ResourceChatModelConfig.Type: {policy.ActionRead},
rbac.ResourceWorkspace.Type: {policy.ActionRead, policy.ActionUpdate},
rbac.ResourceDeploymentConfig.Type: {policy.ActionRead},
Expand Down Expand Up @@ -1817,6 +1820,17 @@ func (q *querier) ActivityBumpWorkspace(ctx context.Context, arg database.Activi
return update(q.log, q.auth, fetch, q.db.ActivityBumpWorkspace)(ctx, arg)
}

func (q *querier) AdvanceChatAutomationScheduleCursor(ctx context.Context, arg database.AdvanceChatAutomationScheduleCursorParams) (int64, error) {
automation, err := q.db.GetChatAutomationByID(ctx, arg.ID)
if err != nil {
return 0, err
}
if err := q.authorizeContext(ctx, policy.ActionUpdate, automation); err != nil {
return 0, err
}
return q.db.AdvanceChatAutomationScheduleCursor(ctx, arg)
}

func (q *querier) AllUserIDs(ctx context.Context, includeSystem bool) ([]uuid.UUID, error) {
// Although this technically only reads users, only system-related functions
// should be allowed to call this.
Expand Down Expand Up @@ -3978,6 +3992,10 @@ func (q *querier) GetDeploymentWorkspaceStats(ctx context.Context) (database.Get
return q.db.GetDeploymentWorkspaceStats(ctx)
}

func (q *querier) GetDueChatAutomationSchedules(ctx context.Context, arg database.GetDueChatAutomationSchedulesParams) ([]database.ChatAutomation, error) {
return fetchWithPostFilter(q.auth, policy.ActionRead, q.db.GetDueChatAutomationSchedules)(ctx, arg)
}

func (q *querier) GetEligibleProvisionerDaemonsByProvisionerJobIDs(ctx context.Context, provisionerJobIDs []uuid.UUID) ([]database.GetEligibleProvisionerDaemonsByProvisionerJobIDsRow, error) {
return fetchWithPostFilter(q.auth, policy.ActionRead, q.db.GetEligibleProvisionerDaemonsByProvisionerJobIDs)(ctx, provisionerJobIDs)
}
Expand Down
33 changes: 33 additions & 0 deletions coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,26 @@ func (s *MethodTestSuite) TestChats() {
object := rbac.ResourceChatAutomation.WithID(automation.ID).InOrg(automation.OrganizationID).WithOwner(automation.OwnerID.String())
check.Args(automation.OrganizationID).Asserts(object, policy.ActionRead).Returns([]database.ChatAutomation{automation})
}))
s.Run("GetDueChatAutomationSchedules", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
automation := testutil.Fake(s.T(), faker, database.ChatAutomation{})
arg := database.GetDueChatAutomationSchedulesParams{Now: dbtime.Now(), LimitCount: 10}
dbm.EXPECT().GetDueChatAutomationSchedules(gomock.Any(), arg).Return([]database.ChatAutomation{automation}, nil).AnyTimes()
object := rbac.ResourceChatAutomation.WithID(automation.ID).InOrg(automation.OrganizationID).WithOwner(automation.OwnerID.String())
check.Args(arg).Asserts(object, policy.ActionRead).Returns([]database.ChatAutomation{automation})
}))
s.Run("AdvanceChatAutomationScheduleCursor", s.Mocked(func(dbm *dbmock.MockStore, faker *gofakeit.Faker, check *expects) {
automation := testutil.Fake(s.T(), faker, database.ChatAutomation{})
dbm.EXPECT().GetChatAutomationByID(gomock.Any(), automation.ID).Return(automation, nil).AnyTimes()
arg := database.AdvanceChatAutomationScheduleCursorParams{
ID: automation.ID,
ScheduleRevision: 2,
ObservedNextRunAt: dbtime.Now(),
NextRunAt: sql.NullTime{Time: dbtime.Now().Add(time.Hour), Valid: true},
UpdatedAt: dbtime.Now(),
}
dbm.EXPECT().AdvanceChatAutomationScheduleCursor(gomock.Any(), arg).Return(int64(1), nil).AnyTimes()
check.Args(arg).Asserts(automation, policy.ActionUpdate).Returns(int64(1))
}))
s.Run("CountChatAutomationsByOwnerID", s.Mocked(func(dbm *dbmock.MockStore, _ *gofakeit.Faker, check *expects) {
ownerID := uuid.New()
dbm.EXPECT().CountChatAutomationsByOwnerID(gomock.Any(), ownerID).Return(int64(3), nil).AnyTimes()
Expand Down Expand Up @@ -8070,6 +8090,19 @@ func TestAsChatd(t *testing.T) {
err = auth.Authorize(ctx, actor, policy.ActionRead, rbac.ResourceChatModelConfig.InOrg(uuid.New()))
require.NoError(t, err, "chat model config read should be allowed")

// Pin the complete ResourceChatAutomation action set: the schedule
// loop reads automations and moves their cursors, but chatd never
// creates or deletes them.
automation := rbac.ResourceChatAutomation.WithOwner(uuid.NewString()).InOrg(uuid.New())
for _, action := range rbac.ResourceChatAutomation.AvailableActions() {
err := auth.Authorize(ctx, actor, action, automation)
if action == policy.ActionRead || action == policy.ActionUpdate {
require.NoError(t, err, "chat automation %s should be allowed", action)
} else {
require.Error(t, err, "chat automation %s should be denied", action)
}
}

// Pin the complete ResourceUser action set: read_personal only.
// Token refresh persistence uses the per-user AsChatdTokenOwner subject.
for _, action := range rbac.ResourceUser.AvailableActions() {
Expand Down
16 changes: 16 additions & 0 deletions coderd/database/dbmetrics/querymetrics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions coderd/database/dbmock/dbmock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

132 changes: 132 additions & 0 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions coderd/database/queries/chatautomations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,51 @@ WHERE
AND kind = 'webhook'
AND webhook_use = 'single'
AND webhook_consumed_at IS NULL;

-- name: GetDueChatAutomationSchedules :many
-- Returns enabled schedule automations whose cursor is at or before now,
-- oldest cursor first, starting after the (after_next_run_at, after_id)
-- keyset so callers can page through every due row. Automations of
-- inactive owners and existing_chat automations whose target chat is gone
-- or archived are left out. It takes no locks: publishing rechecks each
-- row under the chat and automation locks.
SELECT
chat_automations.*
FROM
chat_automations
JOIN users ON users.id = chat_automations.owner_id
LEFT JOIN chats ON chats.id = chat_automations.target_chat_id
WHERE
chat_automations.kind = 'schedule'
AND chat_automations.enabled
AND chat_automations.schedule_next_run_at <= @now::timestamptz
AND (chat_automations.schedule_next_run_at, chat_automations.id) > (@after_next_run_at::timestamptz, @after_id::uuid)
AND users.status = 'active'
AND NOT users.deleted
AND (
chat_automations.target_mode = 'new_chat'
OR (chats.id IS NOT NULL AND NOT chats.archived)
)
ORDER BY
chat_automations.schedule_next_run_at,
chat_automations.id
LIMIT
@limit_count::int;

-- name: AdvanceChatAutomationScheduleCursor :execrows
-- Moves the schedule cursor of an enabled schedule automation from the
-- observed occurrence to next_run_at. It affects no row when the schedule
-- revision or the cursor changed since they were observed, so exactly one
-- caller moves the cursor past each occurrence. A NULL next_run_at means
-- no occurrence is pending.
UPDATE
chat_automations
SET
schedule_next_run_at = sqlc.narg('next_run_at')::timestamptz,
updated_at = @updated_at::timestamptz
WHERE
id = @id::uuid
AND kind = 'schedule'
AND enabled
AND schedule_revision = @schedule_revision::bigint
AND schedule_next_run_at = @observed_next_run_at::timestamptz;
Loading
Loading