Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c713d5

Browse files
fix(coderd/agentapi): make sub agent slugs more unique (#18581)
The incorrect assumption that slugs were unique per-agent was made when the subagent API was implemented. Whilst this PR doesn't completely enforce that, we instead compute a stable hash to prefix the slug that should provide a reasonable level of probability that the slug will be unique.
1 parent aef101f commit 6c713d5

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

coderd/agentapi/subagent.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package agentapi
22

33
import (
44
"context"
5+
"crypto/sha256"
56
"database/sql"
7+
"encoding/base32"
68
"errors"
79
"fmt"
810
"strings"
@@ -165,11 +167,20 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create
165167
}
166168
}
167169

170+
// NOTE(DanielleMaywood):
171+
// Slugs must be unique PER workspace/template. As of 2025-06-25,
172+
// there is no database-layer enforcement of this constraint.
173+
// We can get around this by creating a slug that *should* be
174+
// unique (at least highly probable).
175+
slugHash := sha256.Sum256([]byte(subAgent.Name + "/" + app.Slug))
176+
slugHashEnc := base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(slugHash[:])
177+
computedSlug := strings.ToLower(slugHashEnc[:8]) + "-" + app.Slug
178+
168179
_, err := a.Database.UpsertWorkspaceApp(ctx, database.UpsertWorkspaceAppParams{
169180
ID: uuid.New(), // NOTE: we may need to maintain the app's ID here for stability, but for now we'll leave this as-is.
170181
CreatedAt: createdAt,
171182
AgentID: subAgent.ID,
172-
Slug: app.Slug,
183+
Slug: computedSlug,
173184
DisplayName: app.GetDisplayName(),
174185
Icon: app.GetIcon(),
175186
Command: sql.NullString{

coderd/agentapi/subagent_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func TestSubAgentAPI(t *testing.T) {
216216
},
217217
expectApps: []database.WorkspaceApp{
218218
{
219-
Slug: "code-server",
219+
Slug: "fdqf0lpd-code-server",
220220
DisplayName: "VS Code",
221221
Icon: "/icon/code.svg",
222222
Command: sql.NullString{},
@@ -234,7 +234,7 @@ func TestSubAgentAPI(t *testing.T) {
234234
DisplayGroup: sql.NullString{},
235235
},
236236
{
237-
Slug: "vim",
237+
Slug: "547knu0f-vim",
238238
DisplayName: "Vim",
239239
Icon: "/icon/vim.svg",
240240
Command: sql.NullString{Valid: true, String: "vim"},
@@ -377,7 +377,7 @@ func TestSubAgentAPI(t *testing.T) {
377377
},
378378
expectApps: []database.WorkspaceApp{
379379
{
380-
Slug: "valid-app",
380+
Slug: "511ctirn-valid-app",
381381
DisplayName: "Valid App",
382382
SharingLevel: database.AppSharingLevelOwner,
383383
Health: database.WorkspaceAppHealthDisabled,
@@ -410,19 +410,19 @@ func TestSubAgentAPI(t *testing.T) {
410410
},
411411
expectApps: []database.WorkspaceApp{
412412
{
413-
Slug: "authenticated-app",
413+
Slug: "atpt261l-authenticated-app",
414414
SharingLevel: database.AppSharingLevelAuthenticated,
415415
Health: database.WorkspaceAppHealthDisabled,
416416
OpenIn: database.WorkspaceAppOpenInSlimWindow,
417417
},
418418
{
419-
Slug: "owner-app",
419+
Slug: "eh5gp1he-owner-app",
420420
SharingLevel: database.AppSharingLevelOwner,
421421
Health: database.WorkspaceAppHealthDisabled,
422422
OpenIn: database.WorkspaceAppOpenInSlimWindow,
423423
},
424424
{
425-
Slug: "public-app",
425+
Slug: "oopjevf1-public-app",
426426
SharingLevel: database.AppSharingLevelPublic,
427427
Health: database.WorkspaceAppHealthDisabled,
428428
OpenIn: database.WorkspaceAppOpenInSlimWindow,
@@ -443,13 +443,13 @@ func TestSubAgentAPI(t *testing.T) {
443443
},
444444
expectApps: []database.WorkspaceApp{
445445
{
446-
Slug: "tab-app",
446+
Slug: "ci9500rm-tab-app",
447447
SharingLevel: database.AppSharingLevelOwner,
448448
Health: database.WorkspaceAppHealthDisabled,
449449
OpenIn: database.WorkspaceAppOpenInTab,
450450
},
451451
{
452-
Slug: "window-app",
452+
Slug: "p17s76re-window-app",
453453
SharingLevel: database.AppSharingLevelOwner,
454454
Health: database.WorkspaceAppHealthDisabled,
455455
OpenIn: database.WorkspaceAppOpenInSlimWindow,
@@ -479,7 +479,7 @@ func TestSubAgentAPI(t *testing.T) {
479479
},
480480
expectApps: []database.WorkspaceApp{
481481
{
482-
Slug: "full-app",
482+
Slug: "0ccdbg39-full-app",
483483
Command: sql.NullString{Valid: true, String: "echo hello"},
484484
DisplayName: "Full Featured App",
485485
External: true,
@@ -507,7 +507,7 @@ func TestSubAgentAPI(t *testing.T) {
507507
},
508508
expectApps: []database.WorkspaceApp{
509509
{
510-
Slug: "no-health-app",
510+
Slug: "nphrhbh6-no-health-app",
511511
Health: database.WorkspaceAppHealthDisabled,
512512
SharingLevel: database.AppSharingLevelOwner,
513513
OpenIn: database.WorkspaceAppOpenInSlimWindow,
@@ -531,7 +531,7 @@ func TestSubAgentAPI(t *testing.T) {
531531
},
532532
expectApps: []database.WorkspaceApp{
533533
{
534-
Slug: "duplicate-app",
534+
Slug: "uiklfckv-duplicate-app",
535535
DisplayName: "First App",
536536
SharingLevel: database.AppSharingLevelOwner,
537537
Health: database.WorkspaceAppHealthDisabled,
@@ -568,14 +568,14 @@ func TestSubAgentAPI(t *testing.T) {
568568
},
569569
expectApps: []database.WorkspaceApp{
570570
{
571-
Slug: "duplicate-app",
571+
Slug: "uiklfckv-duplicate-app",
572572
DisplayName: "First Duplicate",
573573
SharingLevel: database.AppSharingLevelOwner,
574574
Health: database.WorkspaceAppHealthDisabled,
575575
OpenIn: database.WorkspaceAppOpenInSlimWindow,
576576
},
577577
{
578-
Slug: "valid-app",
578+
Slug: "511ctirn-valid-app",
579579
DisplayName: "Valid App",
580580
SharingLevel: database.AppSharingLevelOwner,
581581
Health: database.WorkspaceAppHealthDisabled,
@@ -754,7 +754,7 @@ func TestSubAgentAPI(t *testing.T) {
754754
apps, err := db.GetWorkspaceAppsByAgentID(dbauthz.AsSystemRestricted(ctx), agentID) //nolint:gocritic // this is a test.
755755
require.NoError(t, err)
756756
require.Len(t, apps, 1)
757-
require.Equal(t, "duplicate-slug", apps[0].Slug)
757+
require.Equal(t, "k5jd7a99-duplicate-slug", apps[0].Slug)
758758
require.Equal(t, "First Duplicate", apps[0].DisplayName)
759759
})
760760
})
@@ -1128,7 +1128,7 @@ func TestSubAgentAPI(t *testing.T) {
11281128
apps, err := api.Database.GetWorkspaceAppsByAgentID(dbauthz.AsSystemRestricted(ctx), agentID) //nolint:gocritic // this is a test.
11291129
require.NoError(t, err)
11301130
require.Len(t, apps, 1)
1131-
require.Equal(t, "custom-app", apps[0].Slug)
1131+
require.Equal(t, "v4qhkq17-custom-app", apps[0].Slug)
11321132
require.Equal(t, "Custom App", apps[0].DisplayName)
11331133
})
11341134

0 commit comments

Comments
 (0)