Extends #30882's `model_invocable` semantics to workspace skills and to stored skills created through the API. Claude Code and Xum let skill authors set `disable-model-invocation: true` in SKILL.md frontmatter, so agents never pick the skill on their own and it runs only when a person invokes it. Coder Agents honored the key only when someone imported a SKILL.md file while adding a project skill (#30884). ## What changes - **Workspace skills:** when the pinned SKILL.md sets the key to the YAML boolean `true`, chatd leaves the skill out of `<available-skills>` before merging sources, so a listed skill with the same name keeps its bare name, and `read_skill` reports it as not found. The skill stays in the `/` menu, and invoking it still loads its body. `read_skill_file` serves its supporting files under the bare name or `workspace/<name>` and refuses its `SKILL.md` (case-insensitive, after `path.Clean`). Skills without the key behave as on the base. - **Invoked workspace skills:** invoking a workspace skill now stores the pinned skill directory on the invocation part, and the prompt renders it as `dir` on `<invoked-skill>`, escaped as an attribute value. Before, only `read_skill` gave the model a skill's directory, and it refuses a hidden skill, so a hidden skill could not run the scripts it bundles. The part also stores the chat's workspace, and the prompt shows `dir` only while the chat stays bound to it, as it does for workspace file references. This changes #30861's invocation code. Hook payloads drop the directory and its workspace like the body, and API responses strip both. - **Stored skills:** a create request whose content sets the key and that omits `model_invocable` stores `model_invocable = false`. An explicit value wins, and PATCH is unchanged. This covers personal, organization, and project skills, which share `createSkill`. - `workspacesdk.SkillDisablesModelInvocation` reads the key. The `CreateSkillRequest.ModelInvocable` doc comment, the generated API docs, and `extending-agents.md` (the SKILL.md format, a new "Turn off automatic use" section, and the notes on the skill index, the skill tools, and aliases) describe the new behavior. ## Follow-ups - The personal and organization skill editors have no "Agents can use it automatically" switch yet. Only the project editor from #30884 has one. ## Testing - Go tests cover the parser, the turn's skill index and aliases, `read_skill`, `read_skill_file`, invocation of a hidden skill and the directory it carries (attribute escaping, no directory after the chat changes workspace, no directory in hook payloads), and the create default for personal and organization skills. Each new test fails when its code path is removed. - Remote dogfood UAT with a real model failed rounds 1 to 3. Rounds 1 and 2 found the hidden SKILL.md still readable through `read_skill_file` by symlinks and stale pins, plus a bare-name lookup bug and a docs gap that round 2 fixed. Round 3 found that the content check added against those bypasses refused a listed skill's file whose own frontmatter sets the key. After round 3 we narrowed the rule to the hidden skill's `SKILL.md` path. The key decides what agents are offered (the index and `read_skill`), not which files they can read: `read_file` and `execute` already open any workspace file, so a content check in `read_skill_file` protected nothing and kept producing edge cases. - Round 4 passed on `c9ff25dc29f`. Later commits added a behavior-neutral cleanup (`cd617533c2e`), one test case from the test audit (`044c1e6e23a`), and a comment fix (`9ec3768b18b`), which passed validation without another UAT round. Round 5 passed on `2b518c6c2b1`, which adds the skill directory. A real model ran hidden skills' scripts from the `dir` on `<invoked-skill>`, including directories whose paths contain `&`, `'`, `"`, `<` and `>`, and API responses carried no directory. Merging main after #30884 landed (`2d7d3cb9674`) did not change this PR's diff, so UAT was not rerun. `8f043b2a342`, which shows the directory only while the chat stays bound to the skill's workspace, has Go tests but no UAT round. - The doc-check skill ran in author self-check mode on `2d7d3cb9674`, and `27de7f5c6d7` fixes its findings: the skill index sentence said every skill is listed, the stored-skill sentence claimed any project editor import honors the key and presented the API default as fixed, the API path came before the UI path, and some wording broke the docs style rules. The fix is docs-only, so UAT was not rerun. - Polish and the test audit stayed inside the implementation diff. No new dependencies. - Codex review found no major issues on `9ec3768b18b`, then raised one P2 on the same head when the PR was marked ready: an invoked hidden skill gets no skill directory. A probe confirmed it, and `2b518c6c2b1` fixes it. Its earlier finding on that head (honor `CODER_AGENT_EXP_SKILL_META_FILE`) was refuted, because the pinned skill snapshot always reads `SKILL.md`. Both threads are resolved, and Codex found no major issues on `2b518c6c2b1` or on the merge `2d7d3cb9674`. - On `27de7f5c6d7`, Codex raised two P2s. One was confirmed: an invoked skill's directory stayed in the prompt after the chat moved to another workspace, which a prompt test showed on that head, and `8f043b2a342` fixes it. The other was declined: on Windows, `scripts\..\SKILL.md` gets past the `SKILL.md` check in `read_skill_file`, but that check is not an access boundary (`read_file` opens any path), and Windows has more aliases (`SKILL.md.`, `SKILL.md::$DATA`, `SKILL~1.MD`) that normalizing backslashes would not stop. On `8f043b2a342`, Codex asked to bind the directory to the workspace build instead. That was declined: every workspace start is a new build with new agent IDs, so the directory would drop after each stop and start, and the pinned body and the live files can diverge without a rebuild anyway, as they do for `read_skill`. A later request on that head asked to limit a hidden skill's `read_skill_file` access to invocations in the current workspace. That was declined too: `read_skill_file` resolves names against the current pin for listed skills on main as well, and the key decides which skills agents are offered, not which files they can read. All four threads are resolved. `a697dcd927f` adds the `format:"uuid"` tag that `TestSDKFieldsFormatted` requires on the new field, and Codex found no major issues on `a697dcd927f`. `ae70b2c533f` is docs-only: it applies the doc-check suggestion to say that an invoked skill's directory is included only while the chat stays in that workspace. The author doc-check self-check found no blocking findings on it, and Codex found no major issues on `ae70b2c533f`. > Xum, an AI agent, opened this PR for @ibetitsmike and wrote this description. --------- Co-authored-by: Jaayden Halko <jaayden.halko@gmail.com> Co-authored-by: Cursor <cursoragent@cursor.com>
230 lines
6.3 KiB
Go
230 lines
6.3 KiB
Go
// Package chathooks integrates chat lifecycle hooks into chatd: it
|
|
// builds event envelopes, dispatches them, and converts consumer
|
|
// responses into transcript effects and permission decisions.
|
|
package chathooks
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
"golang.org/x/xerrors"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/x/agenthooks/dispatch"
|
|
"github.com/coder/coder/v2/coderd/x/chatd/chatprompt"
|
|
"github.com/coder/coder/v2/codersdk"
|
|
"github.com/coder/coder/v2/codersdk/x/agenthooks"
|
|
)
|
|
|
|
const (
|
|
SessionStartSourceStartup = "startup"
|
|
SessionStartSourceResume = "resume"
|
|
SessionStartSourceClear = "clear"
|
|
)
|
|
|
|
func SessionStartSource(messages []database.ChatMessage) string {
|
|
for _, message := range messages {
|
|
if message.Role == database.ChatMessageRoleAssistant {
|
|
return SessionStartSourceResume
|
|
}
|
|
}
|
|
return SessionStartSourceStartup
|
|
}
|
|
|
|
// Trigger is the only component that talks to the hook dispatcher.
|
|
// Every lifecycle event flows through trigger, which builds the wire
|
|
// envelope, dispatches, and normalizes the outcome.
|
|
type Trigger struct {
|
|
dispatcher *dispatch.Dispatcher
|
|
}
|
|
|
|
func NewTrigger(dispatcher *dispatch.Dispatcher) *Trigger {
|
|
return &Trigger{dispatcher: dispatcher}
|
|
}
|
|
|
|
func (t *Trigger) Enabled() bool {
|
|
return t != nil && t.dispatcher.Enabled()
|
|
}
|
|
|
|
// Chat identifies the chat and turn an event belongs to. Admission
|
|
// events for chats that do not exist yet (create, subagent spawn) fill
|
|
// the fields directly instead of loading a row.
|
|
type Chat struct {
|
|
ID uuid.UUID
|
|
OwnerID uuid.UUID
|
|
WorkspaceID uuid.NullUUID
|
|
ParentChatID uuid.NullUUID
|
|
RootChatID uuid.NullUUID
|
|
TurnID *uuid.UUID
|
|
}
|
|
|
|
func ChatFor(chat database.Chat, turnID *uuid.UUID) Chat {
|
|
return Chat{
|
|
ID: chat.ID,
|
|
OwnerID: chat.OwnerID,
|
|
WorkspaceID: chat.WorkspaceID,
|
|
ParentChatID: chat.ParentChatID,
|
|
RootChatID: chat.RootChatID,
|
|
TurnID: turnID,
|
|
}
|
|
}
|
|
|
|
func (c Chat) ref() agenthooks.ChatRef {
|
|
ref := agenthooks.ChatRef{
|
|
ChatID: c.ID,
|
|
OwnerID: c.OwnerID,
|
|
TurnID: c.TurnID,
|
|
}
|
|
if c.WorkspaceID.Valid {
|
|
ref.WorkspaceID = &c.WorkspaceID.UUID
|
|
}
|
|
if c.ParentChatID.Valid {
|
|
ref.ParentChatID = &c.ParentChatID.UUID
|
|
}
|
|
if c.RootChatID.Valid {
|
|
ref.RootChatID = &c.RootChatID.UUID
|
|
}
|
|
return ref
|
|
}
|
|
|
|
type Message struct {
|
|
Source string
|
|
Prompt string
|
|
Parts json.RawMessage
|
|
ToolUseID string
|
|
ToolName string
|
|
ToolInput json.RawMessage
|
|
ToolResponse json.RawMessage
|
|
ToolError string
|
|
}
|
|
|
|
// UserPromptMessage builds the user_prompt_submit payload for parts.
|
|
// Invoked skill bodies and directories stay out of the payload sent to the
|
|
// hook consumer; the caller's parts keep them for the stored message.
|
|
func UserPromptMessage(parts []codersdk.ChatMessagePart) (Message, error) {
|
|
hookParts := make([]codersdk.ChatMessagePart, len(parts))
|
|
for i, part := range parts {
|
|
part.SkillBody = ""
|
|
part.SkillDir = ""
|
|
part.SkillWorkspaceID = uuid.Nil
|
|
hookParts[i] = part
|
|
}
|
|
encoded, err := chatprompt.MarshalParts(hookParts)
|
|
if err != nil {
|
|
return Message{}, xerrors.Errorf("marshal prompt parts for hook: %w", err)
|
|
}
|
|
return Message{
|
|
Prompt: textFromParts(parts),
|
|
Parts: encoded.RawMessage,
|
|
}, nil
|
|
}
|
|
|
|
// Result is a consumer response normalized for callers: a non-empty
|
|
// InputOverride means the permission decision was allow with a
|
|
// replacement input (the wire contract rejects allow without one).
|
|
// Denials surface as *deniedError instead.
|
|
type Result struct {
|
|
InputOverride json.RawMessage
|
|
ModelContext string
|
|
UserMessage string
|
|
}
|
|
|
|
var emptyResult = &Result{}
|
|
|
|
func (r *Result) GetModelContext() string {
|
|
if r == nil {
|
|
return ""
|
|
}
|
|
return r.ModelContext
|
|
}
|
|
|
|
func (r *Result) GetUserMessage() string {
|
|
if r == nil {
|
|
return ""
|
|
}
|
|
return r.UserMessage
|
|
}
|
|
|
|
func (t *Trigger) Trigger(
|
|
ctx context.Context,
|
|
chat Chat,
|
|
msg Message,
|
|
event agenthooks.EventType,
|
|
capacity dispatch.CapacityClass,
|
|
) (*Result, error) {
|
|
if !t.Enabled() {
|
|
return emptyResult, nil
|
|
}
|
|
var data any
|
|
switch event {
|
|
case agenthooks.EventSessionStart:
|
|
data = agenthooks.SessionStartData{Source: msg.Source}
|
|
case agenthooks.EventUserPromptSubmit:
|
|
data = agenthooks.UserPromptSubmitData{Prompt: msg.Prompt, Parts: msg.Parts}
|
|
case agenthooks.EventPreToolUse:
|
|
data = agenthooks.PreToolUseData{ToolUseID: msg.ToolUseID, ToolName: msg.ToolName, ToolInput: msg.ToolInput}
|
|
case agenthooks.EventPostToolUse:
|
|
data = agenthooks.PostToolUseData{ToolUseID: msg.ToolUseID, ToolName: msg.ToolName, ToolResponse: msg.ToolResponse, ToolError: msg.ToolError}
|
|
case agenthooks.EventPreCompact:
|
|
data = agenthooks.PreCompactData{}
|
|
case agenthooks.EventPostCompact:
|
|
data = agenthooks.PostCompactData{}
|
|
case agenthooks.EventStop:
|
|
data = agenthooks.StopData{}
|
|
default:
|
|
return nil, xerrors.Errorf("unsupported hook event %q", event)
|
|
}
|
|
response, _, err := t.dispatcher.Dispatch(ctx, dispatch.Event{
|
|
Type: event,
|
|
ChatRef: chat.ref(),
|
|
Data: data,
|
|
Capacity: capacity,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if response.Permission != nil && response.Permission.Decision == agenthooks.PermissionDeny {
|
|
return nil, &deniedError{
|
|
Event: event,
|
|
Reason: response.Permission.Reason,
|
|
ModelContext: response.ModelContext,
|
|
UserMessage: response.UserMessage,
|
|
}
|
|
}
|
|
result := &Result{
|
|
ModelContext: response.ModelContext,
|
|
UserMessage: response.UserMessage,
|
|
}
|
|
if response.Permission != nil && response.Permission.Decision == agenthooks.PermissionAllow {
|
|
result.InputOverride = response.Permission.InputOverride
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// textFromParts joins the text parts of a prompt. A skill invocation
|
|
// appears as its slash alias, such as "/project/triage", separated from
|
|
// the surrounding text by spaces.
|
|
func textFromParts(parts []codersdk.ChatMessagePart) string {
|
|
var builder strings.Builder
|
|
afterSkill := false
|
|
for _, part := range parts {
|
|
switch part.Type {
|
|
case codersdk.ChatMessagePartTypeText:
|
|
if afterSkill && part.Text != "" {
|
|
_, _ = builder.WriteString(" ")
|
|
afterSkill = false
|
|
}
|
|
_, _ = builder.WriteString(part.Text)
|
|
case codersdk.ChatMessagePartTypeSkillInvocation:
|
|
if builder.Len() > 0 {
|
|
_, _ = builder.WriteString(" ")
|
|
}
|
|
_, _ = builder.WriteString("/" + chatprompt.SkillInvocationAlias(part))
|
|
afterSkill = true
|
|
}
|
|
}
|
|
return builder.String()
|
|
}
|