Files
coder/codersdk/workspacesdk/frontmatter_test.go
T
Kyle Carberry 919dc299fc feat: agent reads context files and discovers skills locally (#23935)
Piggybacks on #23878. Moves instruction file reading and skill discovery
from `chatd` (server-side, via multiple `LS`/`ReadFile` round-trips
through the agent connection) to the agent itself (local filesystem
access).

This intentionally drops backward compatibility with older agents that
don't support the context-config endpoint. Agents and server are
deployed together; there is no rolling-update contract to maintain here.

## What changed

The agent's `GET /api/v0/context-config` response now returns
`[]ChatMessagePart` directly — the same types chatd persists. This
eliminates intermediate type conversions and makes the protocol
extensible.

| Field | Type | Description |
|---|---|---|
| `parts` | `[]ChatMessagePart` | Context-file and skill parts, ready to
persist |
| `working_dir` | `string` | Agent's resolved working directory |

Removed from the response: `instructions_dirs`, `instructions_file`,
`skills_dirs`, `skill_meta_file`, `mcp_config_files` — the agent reads
files locally and returns their content as parts.

Removed from chatd: all legacy `LS`/`ReadFile` fallback code
(`readHomeInstructionFile`, `readInstructionDirFile`, `DiscoverSkills`
via LS, etc).

## Why

The previous architecture had the agent resolve paths, serve them over
HTTP, then `chatd` make N+1 round-trips back through the agent
connection to read files. The agent has direct filesystem access and
should just read the files.

## Key design decisions

- **Agent returns `ChatMessagePart` directly** — same types chatd
persists. No intermediate `InstructionFileEntry`/`SkillEntry` types
needed.
- **`SkillMeta.MetaFile`** — persisted via `ContextFileSkillMetaFile` on
the skill part, so custom meta file names
(`CODER_AGENT_EXP_SKILL_META_FILE`) survive across chat turns.
- **No pre-read body** — `read_skill` always dials the workspace to
fetch the skill body on demand. Simpler than caching the body in the
response.
- **MCP config paths kept agent-internal** — `MCPConfigFiles()` getter,
not sent over the wire.
- **No backward compat fallback** — old agents that don't support
context-config get no instruction files. This is acceptable since agent
and server deploy together.
2026-04-04 12:45:46 -04:00

132 lines
3.6 KiB
Go

package workspacesdk_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/codersdk/workspacesdk"
)
func TestParseSkillFrontmatter(t *testing.T) {
t.Parallel()
t.Run("Basic", func(t *testing.T) {
t.Parallel()
name, desc, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: my-skill\ndescription: Does a thing\n---\nBody text here.\n",
)
require.NoError(t, err)
require.Equal(t, "my-skill", name)
require.Equal(t, "Does a thing", desc)
require.Equal(t, "Body text here.", body)
})
t.Run("QuotedValues", func(t *testing.T) {
t.Parallel()
name, desc, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: \"quoted-name\"\ndescription: 'single-quoted'\n---\n",
)
require.NoError(t, err)
require.Equal(t, "quoted-name", name)
require.Equal(t, "single-quoted", desc)
})
t.Run("NoDescription", func(t *testing.T) {
t.Parallel()
name, desc, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: minimal\n---\nSome body.\n",
)
require.NoError(t, err)
require.Equal(t, "minimal", name)
require.Empty(t, desc)
require.Equal(t, "Some body.", body)
})
t.Run("HTMLCommentsStripped", func(t *testing.T) {
t.Parallel()
_, _, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: strip-test\n---\nBefore <!-- hidden --> after.\n",
)
require.NoError(t, err)
require.Equal(t, "Before after.", body)
})
t.Run("MultilineHTMLComment", func(t *testing.T) {
t.Parallel()
_, _, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: multi\n---\nKeep this.\n<!--\nRemove\nall of this.\n-->\nAnd this.\n",
)
require.NoError(t, err)
require.Contains(t, body, "Keep this.")
require.Contains(t, body, "And this.")
require.NotContains(t, body, "Remove")
})
t.Run("BOMPrefix", func(t *testing.T) {
t.Parallel()
name, _, _, err := workspacesdk.ParseSkillFrontmatter(
"\xef\xbb\xbf---\nname: bom-skill\n---\n",
)
require.NoError(t, err)
require.Equal(t, "bom-skill", name)
})
t.Run("EmptyBody", func(t *testing.T) {
t.Parallel()
_, _, body, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: nobody\ndescription: has no body\n---\n",
)
require.NoError(t, err)
require.Empty(t, body)
})
t.Run("CaseInsensitiveKeys", func(t *testing.T) {
t.Parallel()
name, desc, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nName: upper\nDescription: Also upper\n---\n",
)
require.NoError(t, err)
require.Equal(t, "upper", name)
require.Equal(t, "Also upper", desc)
})
t.Run("UnknownKeysIgnored", func(t *testing.T) {
t.Parallel()
name, _, _, err := workspacesdk.ParseSkillFrontmatter(
"---\nname: test\nauthor: someone\nversion: 1.0\n---\n",
)
require.NoError(t, err)
require.Equal(t, "test", name)
})
t.Run("ErrorMissingOpenDelimiter", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter("no frontmatter here")
require.ErrorContains(t, err, "missing opening frontmatter delimiter")
})
t.Run("ErrorMissingCloseDelimiter", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter("---\nname: oops\n")
require.ErrorContains(t, err, "missing closing frontmatter delimiter")
})
t.Run("ErrorMissingName", func(t *testing.T) {
t.Parallel()
_, _, _, err := workspacesdk.ParseSkillFrontmatter(
"---\ndescription: no name\n---\n",
)
require.ErrorContains(t, err, "frontmatter missing required 'name' field")
})
t.Run("WhitespaceAroundDelimiters", func(t *testing.T) {
t.Parallel()
name, _, _, err := workspacesdk.ParseSkillFrontmatter(
" --- \nname: spaced\n --- \n",
)
require.NoError(t, err)
require.Equal(t, "spaced", name)
})
}