10000 feat: persist AI task state in template imports & workspace builds by dannykopping · Pull Request #18449 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

feat: persist AI task state in template imports & workspace builds #18449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9fa7a93
chore: go.mod
dannykopping Jun 18, 2025
d01a050
feat: proto & db changes to persist coder_ai_task state
dannykopping Jun 18, 2025
4383350
feat: store coder_ai_task resource state
dannykopping Jun 18, 2025
957fb08
chore: rename ai_tasks_sidebar_app_id to ai_task_sidebar_app_id
dannykopping Jun 19, 2025
91c8f0c
chore: update to d9b0f892f9c2b3768b9647b089900b0628cb7828, prompt nam…
dannykopping Jun 19, 2025
efe5da9
chore: update provisioner API version comments
dannykopping Jun 19, 2025
5e0d28a
chore: move prompt param validation to ConvertState with other valida…
dannykopping Jun 19, 2025
57da39f
chore: unify tf testdata versions & fix tests
dannykopping Jun 20, 2025
c6c7b5c
chore: dbauthz
dannykopping Jun 20, 2025
4816ed5
chore: fix TestWorkspaceAgentAppHealth test after agent ID field now …
dannykopping Jun 20, 2025
e48df2e
chore: add tests
dannykopping Jun 20, 2025
0b2b732
chore: correct bad rebase
dannykopping Jun 23, 2025
5edee05
chore: undo changes to terraform testdata, so as to not complicate th…
dannykopping Jun 23, 2025
3614a5b
chore: fixing tests
dannykopping Jun 23, 2025
d7e47f9
chore: ensure sidebar app ID must be set when has_ai_task is set
dannykopping Jun 23, 2025
f4111df
chore: test multiple coder_ai_tasks cannot be configured
dannykopping Jun 23, 2025
cc0e290
chore: fix CI
dannykopping Jun 23, 2025
138d865
chore: minor refactor
dannykopping Jun 23, 2025
1aba76b
chore: add clarifying/defensive parentheses
dannykopping Jun 23, 2025
6454ffe
chore: fix conflict
dannykopping Jun 23, 2025
bce4a88
chore: review comments
dannykopping Jun 24, 2025
9a453e8
Merge branch 'main' of github.com:/coder/coder into dk/coder-ai-task-res
dannykopping Jun 24, 2025
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
Prev Previous commit
Next Next commit
chore: ensure sidebar app ID must be set when has_ai_task is set
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
  • Loading branch information
dannykopping committed Jun 23, 2025
commit d7e47f9fd290725834c9dacc2d900ea7818b64f4
2 changes: 1 addition & 1 deletion coderd/database/dbauthz/dbauthz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ func (s *MethodTestSuite) TestTemplate() {
TemplateID: uuid.NullUUID{UUID: t.ID, Valid: true},
})
check.Args(database.UpdateTemplateVersionAITaskByJobIDParams{
JobID: job.ID,
JobID: job.ID,
HasAITask: sql.NullBool{Bool: true, Valid: true},
}).Asserts(t, policy.ActionUpdate)
}))
Expand Down
7 changes: 7 additions & 0 deletions coderd/database/dbmem/dbmem.go
Original file line number Diff line number Diff line change
Expand Up @@ -12062,6 +12062,13 @@ func (q *FakeQuerier) UpdateWorkspaceAutostart(_ context.Context, arg database.U
}

func (q *FakeQuerier) UpdateWorkspaceBuildAITaskByID(_ context.Context, arg database.UpdateWorkspaceBuildAITaskByIDParams) error {
if arg.HasAITask.Bool && !arg.SidebarAppID.Valid {
return xerrors.Errorf("ai_task_sidebar_app_id is required when has_ai_task is true")
}
if !arg.HasAITask.Valid && arg.SidebarAppID.Valid {
return xerrors.Errorf("ai_task_sidebar_app_id is can only be set when has_ai_task is true")
}

err := validateDatabaseType(arg)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- Drop the check constraint first
ALTER TABLE workspace_builds DROP CONSTRAINT workspace_builds_ai_task_sidebar_app_id_required;

-- Revert ai_task_sidebar_app_id back to ai_tasks_sidebar_app_id in workspace_builds table
ALTER TABLE workspace_builds DROP CONSTRAINT workspace_builds_ai_task_sidebar_app_id_fkey;

Expand Down Expand Up @@ -46,4 +49,4 @@ FROM
)
);

COMMENT ON VIEW workspace_build_with_user IS 'Joins in the username + avatar url of the initiated by user.';
COMMENT ON VIEW workspace_build_with_user IS 'Joins in the username + avatar url of the initiated by user.';
10000
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ ALTER TABLE workspace_builds RENAME COLUMN ai_tasks_sidebar_app_id TO ai_task_si

ALTER TABLE workspace_builds ADD CONSTRAINT workspace_builds_ai_task_sidebar_app_id_fkey FOREIGN KEY (ai_task_sidebar_app_id) REFERENCES workspace_apps(id);

-- if has_ai_task is true, ai_task_sidebar_app_id MUST be set
-- ai_task_sidebar_app_id can ONLY be set if has_ai_task is true
--
-- has_ai_task | ai_task_sidebar_app_id | Result
-- ------------|------------------------|---------------
-- NULL | NULL | TRUE (passes)
-- NULL | NOT NULL | FALSE (fails)
-- FALSE | NULL | TRUE (passes)
-- FALSE | NOT NULL | FALSE (fails)
-- TRUE | NULL | FALSE (fails)
-- TRUE | NOT NULL | TRUE (passes)
ALTER TABLE workspace_builds ADD CONSTRAINT workspace_builds_ai_task_sidebar_app_id_required CHECK (
(has_ai_task IS NULL OR has_ai_task = false) AND ai_task_sidebar_app_id IS NULL OR
(has_ai_task = true AND ai_task_sidebar_app_id IS NOT NULL)
);

-- Update the workspace_build_with_user view to use the new column name
DROP VIEW workspace_build_with_user;

Expand Down Expand Up @@ -46,4 +62,4 @@ FROM
)
);

COMMENT ON VIEW workspace_build_with_user IS 'Joins in the username + avatar url of the initiated by user.';
COMMENT ON VIEW workspace_build_with_user IS 'Joins in the username + avatar url of the initiated by user.';
62 changes: 24 additions & 38 deletions coderd/workspaceagents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package coderd_test

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"maps"
Expand Down Expand Up @@ -1507,52 +1506,39 @@ func TestWorkspaceAgentAppHealth(t *testing.T) {
t.Parallel()
client, db := coderdtest.NewWithDatabase(t, nil)
user := coderdtest.CreateFirstUser(t, client)

// When using WithAgent() here and setting some *proto.App instances on the agent, Do() ends up trying to insert duplicate records.
apps := []*proto.App{
{
Slug: "code-server",
Command: "some-command",
Url: "http://localhost:3000",
Icon: "/code.svg",
},
{
Slug: "code-server-2",
DisplayName: "code-server-2",
Command: "some-command",
Url: "http://localhost:3000",
Icon: "/code.svg",
Healthcheck: &proto.Healthcheck{
Url: "http://localhost:3000",
Interval: 5,
Threshold: 6,
},
},
}
r := dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
OrganizationID: user.OrganizationID,
OwnerID: user.UserID,
}).WithAgent(func(agents []*proto.Agent) []*proto.Agent {
agents[0].Apps = apps
return agents
}).Do()

res := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{JobID: r.Build.JobID})
agent := dbgen.WorkspaceAgent(t, db, database.WorkspaceAgent{ResourceID: res.ID})

// It's simpler to call db.InsertWorkspaceApp directly than dbgen.WorkspaceApp because it's more terse and direct;
// the latter sets a bunch of defaults which make this test hard.
_, err := db.InsertWorkspaceApp(dbauthz.AsSystemRestricted(t.Context()), database.InsertWorkspaceAppParams{
ID: uuid.New(),
Slug: "code-server",
AgentID: agent.ID,
Icon: "/code.svg",
Command: sql.NullString{String: "some-command", Valid: true},
Url: sql.NullString{String: "http://localhost:3000", Valid: true},
SharingLevel: database.AppSharingLevelOwner,
Health: database.WorkspaceAppHealthDisabled,
OpenIn: database.WorkspaceAppOpenInWindow,
})
require.NoError(t, err)
_, err = db.InsertWorkspaceApp(dbauthz.AsSystemRestricted(t.Context()), database.InsertWorkspaceAppParams{
ID: uuid.New(),
Slug: "code-server-2",
DisplayName: "code-server-2",
AgentID: agent.ID,
Icon: "/code.svg",
Command: sql.NullString{String: "some-command", Valid: true},
Url: sql.NullString{String: "http://localhost:3000", Valid: true},
HealthcheckInterval: 5,
HealthcheckUrl: "http://localhost:3000",
HealthcheckThreshold: 6,
Health: database.WorkspaceAppHealthInitializing,
SharingLevel: database.AppSharingLevelOwner,
OpenIn: database.WorkspaceAppOpenInWindow,
})
require.NoError(t, err)

ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
defer cancel()

agentClient := agentsdk.New(client.URL)
agentClient.SetSessionToken(agent.AuthToken.String())
agentClient.SetSessionToken(r.AgentToken)
conn, err := agentClient.ConnectRPC(ctx)
require.NoError(t, err)
defer func() {
Expand Down
17 changes: 11 additions & 6 deletions coderd/workspaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4531,13 +4531,18 @@ func TestWorkspaceFilterHasAITask(t *testing.T) {
}
job := dbgen.ProvisionerJob(t, db, pubsub, jobConfig)

res := dbgen.WorkspaceResource(t, db, database.WorkspaceResource{JobID: job.ID})
agnt := dbgen.WorkspaceAgent(t, db, database.WorkspaceAgent{ResourceID: res.ID})
sidebarApp := dbgen.WorkspaceApp(t, db, database.WorkspaceApp{AgentID: agnt.ID})

build := dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{
WorkspaceID: ws.ID,
TemplateVersionID: version.ID,
InitiatorID: user.UserID,
JobID: job.ID,
BuildNumber: 1,
HasAITask: hasAITask,
WorkspaceID: ws.ID,
TemplateVersionID: version.ID,
InitiatorID: user.UserID,
JobID: job.ID,
BuildNumber: 1,
HasAITask: hasAITask,
AITaskSidebarAppID: uuid.NullUUID{UUID: sidebarApp.ID, Valid: true},
})

if aiTaskPrompt != nil {
Expand Down
Loading
0