Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2ac1248
feat: add project skill queries and authorization
jaaydenh Oct 15, 2026
836c956
refactor(coderd/database): share chat project config authorization he…
jaaydenh Oct 15, 2026
7bc17ba
test(coderd/database): assert project skill reads stay scoped to thei…
jaaydenh Oct 15, 2026
1c9e8f1
feat: add project skill API routes
jaaydenh Oct 15, 2026
d623418
chore(coderd): clarify chat project skill limit comment
jaaydenh Oct 15, 2026
dda45bc
feat(coderd/x): load project skills in chats
jaaydenh Oct 15, 2026
27c808d
feat(site): list project skills in the chat slash menu
jaaydenh Oct 15, 2026
009432c
refactor(site/src/pages/AgentsPage): share stored skill source loadin…
jaaydenh Oct 15, 2026
a3a2f5d
refactor(site/src/pages/AgentsPage/components/ChatMessageInput): simp…
jaaydenh Oct 15, 2026
23a5168
refactor(site/src/pages/AgentsPage): name stored skill source flags a…
jaaydenh Oct 15, 2026
5533023
refactor(site/src/pages/AgentsPage/components): clarify skill source …
jaaydenh Oct 15, 2026
681b374
test: cover project skill loader and gating gaps
jaaydenh Oct 15, 2026
2a72a92
test(site/src/pages/AgentsPage/components): assert the project skills…
jaaydenh Oct 15, 2026
e6af784
Merge remote-tracking branch 'origin/main' into HEAD
jaaydenh Oct 16, 2026
ebb0776
fix(coderd/database/migrations): renumber chat project skill audit mi…
jaaydenh Oct 16, 2026
2ea1fca
Merge branch 'chat-project-skills/queries' into chat-project-skills/api
jaaydenh Oct 16, 2026
f317b07
Merge branch 'chat-project-skills/api' into chat-project-skills/chatd
jaaydenh Oct 16, 2026
f010477
chore(coderd): remove non-essential project skill comments
jaaydenh Oct 18, 2026
cbb4bf7
Merge remote-tracking branch 'origin/main' into rs-queries
jaaydenh Oct 18, 2026
1799827
fix(coderd/database/migrations): renumber chat project skill audit mi…
jaaydenh Oct 18, 2026
d1c3934
Merge branch 'rs-queries' into rs-api
jaaydenh Oct 18, 2026
a63de89
Merge branch 'rs-api' into rs-chatd
jaaydenh Oct 18, 2026
8e1aa52
feat: add project skill API routes
jaaydenh Oct 15, 2026
34c10cd
chore(coderd): clarify chat project skill limit comment
jaaydenh Oct 15, 2026
c20a345
Merge branch 'chat-project-skills/api' into chat-project-skills/chatd
jaaydenh Oct 18, 2026
ec1b625
Merge remote-tracking branch 'origin/main' into chat-project-skills/c…
jaaydenh Oct 18, 2026
52ba82d
Merge remote-tracking branch 'origin/main' into skills-chatd-fix
jaaydenh Oct 20, 2026
1c06855
chore: update architecture.md
jaaydenh Oct 20, 2026
0c938f1
fix: gate project skills on project usability and fail open in the co…
jaaydenh Oct 20, 2026
379ae2e
fix(site/src/pages/AgentsPage): keep loaded project skills after a fa…
jaaydenh Oct 20, 2026
ee7773e
Merge remote-tracking branch 'origin/chat-project-skills/chatd' into …
jaaydenh Oct 20, 2026
8288725
docs(site/src/pages/AgentsPage/components/ChatMessageInput): clarify …
jaaydenh Oct 20, 2026
894beaf
fix(site/src): count only lost project access as no project skills
jaaydenh Oct 20, 2026
019f52f
fix(site/src): let lost project access override cached project skills
jaaydenh Oct 20, 2026
2964653
test(site/src/pages/AgentsPage/components/ChatMessageInput): cover lo…
jaaydenh Oct 20, 2026
89a2e9a
fix(site/src): hide project skills the caller's chats cannot use
jaaydenh Oct 20, 2026
9231e4e
fix(site/src/api/queries): treat a cached project list as unknown unt…
jaaydenh Oct 20, 2026
17ba94d
Merge remote-tracking branch 'origin/main' into wyav/sk-chatd
jaaydenh Oct 20, 2026
90de6c8
refactor(coderd/x/chatd): remove the unused resolveProjectInstructions
jaaydenh Oct 21, 2026
e9117ba
chore: update architecture.md
jaaydenh Oct 21, 2026
f83fb91
Merge branch 'chat-project-skills/chatd' of https://github.com/coder/…
jaaydenh Oct 21, 2026
e9a4adf
fix(site/src/pages/AgentsPage/components/ChatMessageInput): drop the …
jaaydenh Oct 21, 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
6 changes: 4 additions & 2 deletions coderd/x/chatd/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ Files uploaded into the chat's workspace through `POST /api/v2/chats/{chat}/work

Project instructions are optional context injected into all chats in a project. Stored in `chat_project_instructions`. Subagents have no `project_id`, so they use the root chat's project. A root chat can move into a project, between projects, or out of its project after it's created. The move is written inside the `SetProject` transition so that it happens only while no step of the chat runs.

Project skills are the project's enabled skills, which every chat in the project gets as a project skill source alongside personal, organization, and workspace skills. Each turn checks ChatProjectUsableBy once for the chat owner, and that one result gates both project instructions and skills, so a chat gets both or neither. Skills are then read as chatd, so role grants do not count. Subagents use the root chat's project. If loading fails, the turn continues without them.

If the distinction isn't completely clear to you at this point, don't worry. It should become clearer as you learn more about the core state machine.

## Execution states
Expand Down Expand Up @@ -1040,9 +1042,9 @@ Owners share a project with users, groups, or the Everyone group (ID = organizat

Only the owner and administrators with `chat_project` delete can delete a project. Project responses include the caller's `permissions` (update, delete, share), role grants included.

Memory tools and instruction loading run as chatd, so `ChatProjectUsableBy` gates them. It allows the owner and users with an ACL read grant (direct, group, or Everyone). Role grants do not count, so an administrator cannot attach a chat to an unshared project. An administrator with `share` can still grant themselves access; this is accepted to keep access control simple.
Memory tools, instruction loading and skill loading run as chatd, so `ChatProjectUsableBy` gates them. It allows the owner and users with an ACL read grant (direct, group, or Everyone). Role grants do not count, so an administrator cannot attach a chat to an unshared project. An administrator with `share` can still grant themselves access; this is accepted to keep access control simple.

Chat creation checks `ChatProjectUsableBy` for the chat owner, and `resolveProjectMemory` and `resolveProjectInstructions` check it on every turn. If the check fails, for example after the owner's grant is removed or for non-owner chats under `--disable-chat-sharing`, the chat stays in the project but its next turn gets no memory or instructions. Content already in the chat history stays.
Chat creation checks `ChatProjectUsableBy` for the chat owner, and `resolveProjectMemory` check it on every turn. If the check fails, for example after the owner's grant is removed or for non-owner chats under `--disable-chat-sharing`, the chat stays in the project but its next turn gets no memory or instructions. Content already in the chat history stays.

##### Reasoning effort

Expand Down
56 changes: 56 additions & 0 deletions coderd/x/chatd/chatd_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2085,6 +2085,7 @@ func TestPersonalSkillsInSystemPrompt(t *testing.T) {
}},
nil,
nil,
nil,
),
"",
"",
Expand All @@ -2108,6 +2109,7 @@ func TestPersonalAndWorkspaceSkillCollisionInSystemPrompt(t *testing.T) {
Source: skillspkg.SourcePersonal,
}},
nil,
nil,
[]chattool.SkillMeta{{
Name: "deploy",
Description: "Workspace deployment process",
Expand Down Expand Up @@ -2440,6 +2442,60 @@ func TestLoadOrganizationSkillBody(t *testing.T) {
}
}

// TestLoadProjectSkillBody covers the loader directly because a turn only
// reaches it for skills it already listed, so the not-found mapping for
// disabled and missing skills is otherwise unreachable.
func TestLoadProjectSkillBody(t *testing.T) {
t.Parallel()

ctx := testutil.Context(t, testutil.WaitLong)
db, _ := dbtestutil.NewDB(t)
authzDB := dbauthz.New(db, rbac.NewStrictCachingAuthorizer(prometheus.NewRegistry()), slogtest.Make(t, nil), nil)
server := &Server{
db: authzDB,
logger: slogtest.Make(t, nil),
}

org := dbgen.Organization(t, db, database.Organization{})
owner := dbgen.User(t, db, database.User{})
dbgen.OrganizationMember(t, db, database.OrganizationMember{UserID: owner.ID, OrganizationID: org.ID})
project := dbgen.ChatProject(t, db, database.ChatProject{OrganizationID: org.ID, OwnerID: owner.ID})
for _, name := range []string{"project-review", "disabled-review"} {
_, err := db.InsertProjectSkill(ctx, database.InsertProjectSkillParams{
ID: uuid.New(),
ProjectID: project.ID,
Name: name,
Content: "---\nname: " + name + "\ndescription: Review\n---\n\nProject instructions.\n",
})
require.NoError(t, err)
}
_, err := db.UpdateProjectSkillByProjectIDAndName(ctx, database.UpdateProjectSkillByProjectIDAndNameParams{
Enabled: sql.NullBool{Bool: false, Valid: true},
ProjectID: project.ID,
Name: "disabled-review",
})
require.NoError(t, err)

chatdCtx := dbauthz.AsChatd(ctx)
got, err := server.loadProjectSkillBody(chatdCtx, project.ID, "project-review")
require.NoError(t, err)
require.Equal(t, skillspkg.SourceProject, got.Source)
require.Equal(t, "Project instructions.", got.Body)

for _, tc := range []struct {
name string
projectID uuid.UUID
skill string
}{
{name: "Disabled", projectID: project.ID, skill: "disabled-review"},
{name: "Missing", projectID: project.ID, skill: "missing-review"},
{name: "MissingProject", projectID: uuid.New(), skill: "project-review"},
} {
_, err := server.loadProjectSkillBody(chatdCtx, tc.projectID, tc.skill)
require.ErrorIs(t, err, skillspkg.ErrSkillNotFound, tc.name)
}
}

func systemPromptText(t *testing.T, prompt []fantasy.Message) string {
t.Helper()

Expand Down
9 changes: 5 additions & 4 deletions coderd/x/chatd/chattool/skill.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,10 @@ type ReadSkillOptions struct {
GetWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
GetSkills func() []SkillMeta
ResolveAlias func(string) (skillspkg.ResolvedSkill, error)
// LoadStoredSkillBody loads the current body of a personal or
// organization skill. It returns skillspkg.ErrSkillNotFound when the
// skill is gone, disabled, or no longer readable by the chat owner.
// LoadStoredSkillBody loads the current body of a personal,
// organization, or project skill. It returns skillspkg.ErrSkillNotFound
// when the skill is gone, disabled, or no longer readable by the chat
// owner.
LoadStoredSkillBody func(context.Context, skillspkg.Skill) (skillspkg.ParsedSkill, error)
}

Expand Down Expand Up @@ -346,7 +347,7 @@ func ReadSkill(options ReadSkillOptions) fantasy.AgentTool {
}

switch resolved.Source {
case skillspkg.SourcePersonal, skillspkg.SourceOrganization:
case skillspkg.SourcePersonal, skillspkg.SourceOrganization, skillspkg.SourceProject:
if options.LoadStoredSkillBody == nil {
return fantasy.NewTextErrorResponse(
"stored skill loader is not configured",
Expand Down
12 changes: 5 additions & 7 deletions coderd/x/chatd/chattool/skill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ func TestFormatResolvedSkillIndex(t *testing.T) {
t.Parallel()

resolved := skillspkg.MergeSkills(
[]skillspkg.Skill{{Name: "review", Description: "Personal"}},
[]skillspkg.Skill{{Name: "review", Description: "Organization"}},
[]skillspkg.Skill{{Name: "review", Description: "Workspace"}},
skillspkg.SourceSkills{Source: skillspkg.SourcePersonal, Skills: []skillspkg.Skill{{Name: "review", Description: "Personal"}}},
skillspkg.SourceSkills{Source: skillspkg.SourceOrganization, Skills: []skillspkg.Skill{{Name: "review", Description: "Organization"}}},
skillspkg.SourceSkills{Source: skillspkg.SourceWorkspace, Skills: []skillspkg.Skill{{Name: "review", Description: "Workspace"}}},
)
idx := chattool.FormatResolvedSkillIndex(resolved)
assert.Contains(t, idx, "- personal/review: Personal\n- org/review: Organization\n- workspace/review: Workspace\n")
Expand All @@ -141,13 +141,11 @@ func TestFormatResolvedSkillIndex(t *testing.T) {
// A 3-byte rune makes a byte-based cut at 1,024 split a rune.
atLimit := strings.Repeat("界", 1024)
idx := chattool.FormatResolvedSkillIndex(skillspkg.MergeSkills(
[]skillspkg.Skill{
skillspkg.SourceSkills{Source: skillspkg.SourcePersonal, Skills: []skillspkg.Skill{
{Name: "at-limit", Description: atLimit},
{Name: "over-limit", Description: atLimit + "界tail"},
{Name: "short", Description: "Short description"},
},
nil,
nil,
}},
))
assert.True(t, utf8.ValidString(idx))
assert.Contains(t, idx, "- at-limit: "+atLimit+"\n")
Expand Down
67 changes: 39 additions & 28 deletions coderd/x/chatd/project_instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,63 @@ import (
"regexp"
"strings"

"github.com/google/uuid"

"cdr.dev/slog/v3"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/codersdk"
)

// resolveProjectInstructions returns the system prompt block holding the
// instructions of the chat's project, or "" when there are none. Subagent
// chats do not carry a project, so they inherit the root chat's project at
// the cost of one extra lookup. Instructions are read on every turn so edits
// reach existing chats on their next turn.
//
// A chat runs without project instructions when the project or its
// instructions cannot be loaded, when the access check fails, or when the
// chat owner can no longer use the project.
func (p *Server) resolveProjectInstructions(ctx context.Context, logger slog.Logger, chat database.Chat) string {
// resolveChatProjectID returns the project the chat belongs to, or an
// invalid ID when it has none or chat projects are disabled. Subagent chats
// do not carry a project, so they inherit the root chat's project at the cost
// of one extra lookup. A failed lookup is logged and treated as no project.
func (p *Server) resolveChatProjectID(ctx context.Context, logger slog.Logger, chat database.Chat) uuid.NullUUID {
if !p.experiments.Enabled(codersdk.ExperimentChatProjects) {
return ""
return uuid.NullUUID{}
}
projectID := chat.ProjectID
if !projectID.Valid && chat.RootChatID.Valid && chat.RootChatID.UUID != chat.ID {
root, err := p.db.GetChatByID(ctx, chat.RootChatID.UUID)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
logger.Warn(ctx, "failed to load root chat for project instructions",
slog.F("root_chat_id", chat.RootChatID.UUID),
slog.Error(err),
)
}
return ""
if chat.ProjectID.Valid || !chat.RootChatID.Valid || chat.RootChatID.UUID == chat.ID {
return chat.ProjectID
}
root, err := p.db.GetChatByID(ctx, chat.RootChatID.UUID)
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
logger.Warn(ctx, "failed to load root chat for chat project",
slog.F("root_chat_id", chat.RootChatID.UUID),
slog.Error(err),
)
}
projectID = root.ProjectID
return uuid.NullUUID{}
}
return root.ProjectID
}

// resolveUsableChatProjectID returns the chat's project, as resolved by
// resolveChatProjectID, when the chat owner can use it. Otherwise it returns
// an invalid ID, and the chat runs without the project's instructions,
// skills, and memory.
func (p *Server) resolveUsableChatProjectID(ctx context.Context, logger slog.Logger, chat database.Chat) uuid.NullUUID {
projectID := p.resolveChatProjectID(ctx, logger, chat)
if !projectID.Valid {
return ""
return uuid.NullUUID{}
}

if _, ok := p.usableChatProject(ctx, logger, projectID.UUID, chat); !ok {
return ""
return uuid.NullUUID{}
}
return projectID
}

row, err := p.db.GetChatProjectInstructionsByProjectID(ctx, projectID.UUID)
// loadProjectInstructions returns the instructions block of a project the
// chat owner can use, as checked by resolveUsableChatProjectID, or "" when it
// has none or they cannot be loaded. Instructions are read on every turn so
// edits reach existing chats on their next turn.
func (p *Server) loadProjectInstructions(ctx context.Context, logger slog.Logger, projectID uuid.UUID) string {
row, err := p.db.GetChatProjectInstructionsByProjectID(ctx, projectID)
if err != nil {
// A project without a row has no instructions.
if !errors.Is(err, sql.ErrNoRows) {
logger.Warn(ctx, "failed to load chat project instructions",
slog.F("project_id", projectID.UUID),
slog.F("project_id", projectID),
slog.Error(err),
)
}
Expand Down
Loading
Loading