本文是 ZCode v2 接入 skill 的实现级规划。它遵循当前 v2 架构原则:
模型入口与上下文:
Skill tool。启动时只给模型可用 skill 列表,不把完整正文塞进 system prompt;模型必须先调用 tool 才能获得完整指令。Skill tool 输入是 skill name 和可选 args;输出完整内容和 base directory,帮助模型按需读取 references/scripts。能力域与权限:
SkillMetadata、SkillLoadOutcome、SkillRoot、SkillPolicy、SkillDependencies 等明确模型。allowed-tools、model、effort、disable-model-invocation、when_to_use 等运行时影响项。第一版不做:
SKILL.md 指令包。context: fork)依赖成熟 subagent runtime,第一版只预留 schema。本轮核对确认 skill P0 主线已经落地,本文后续条目仍作为契约基准,但不应再把以下能力当作未实现缺口:
packages/contracts/src/skills/index.ts 已定义 SkillRoot、SkillScope、SkillMetadata、SkillDiagnostic、SkillContent、SkillLoadOutcome、SkillPort 和 SkillConfig;packages/contracts/src/tools/skill.ts 已提供 Skill tool 的运行时 schema 与 provider JSON schema。packages/adapters/src/skills/index.ts 已通过 Node adapter 发现从 cwd 向上到 worktree root 的 .zcode/skills、.agents/skills、.claude/skills 及对应 user roots,支持 config extra roots、flat YAML frontmatter、未知字段 diagnostics、同名优先级去重、正文按需读取和 100KB 截断。packages/core/src/context/sections/skills.ts 已把 skill metadata 渲染为 meta_user section,不把正文放进 system prompt;packages/core/src/tool/handlers/skill.ts 已通过 SkillPort 加载正文,并声明只读、session side effect、runtime input/output schema、result budget、timeout、取消和 trace 传播。packages/bootstrap/src/index.ts 已按 feature gate 和 skills.* config 注入 SkillPort;listZCodeSkills() 复用同一 adapter;zcode skills / zcode skills list 支持 human、--json 和 --verbose diagnostics;TUI/command-center 的裸 /skill 只读列举 skills,/skill <name> [task] 会改写为必须先调用 Skill tool 的 prompt;ZCode app-server 暴露 /skill <skill-name> [task],裸 /skill 在 ZCode app-server 中返回 usage,不启动模型 turn。packages/adapters/tests/skills.test.ts 覆盖 discovery、正文加载和重复名优先级;packages/core/tests/skill-tool.test.ts 与 packages/core/tests/context-builder.test.ts 覆盖 metadata 注入和 Skill tool;packages/cli/tests/cli.unit.test.ts 覆盖 CLI/TUI /skill;packages/bootstrap/tests/zcode-protocol.test.ts 覆盖 ZCode app-server command 暴露、禁用和 prompt rewrite。当前仍未落地,继续作为后续范围:
$skill-name / skill://... mention。Skill tool 的权限声明仍是 tool-level low-risk 自动允许,没有根据 unsafe frontmatter、remote/plugin source 或 runtime modifier 字段动态升级为 ask。SkillRootsResolved / SkillLoaded 等)、loaded-skill session snapshot、compact 后恢复完整 skill body,以及 diagnostics 的结构化观测面。context: fork、model / effort runtime modifier、allowed-tools scoped suggestion 等 P1/P2 能力。ZCode skill 采用“两层加载”:
docs/skills-live-catalog.md。Skill tool 后,按会话快照解析名称,才读取对应 SKILL.md 正文并注入当前会话。这能同时满足 token 控制、权限可见、trace 可追踪和跨平台 I/O 收口。
contracts 增加 skill contract:
SkillRootSkillScopeSkillMetadataSkillPolicySkillLoadOutcomeSkillDiagnosticSkillContentSkillPortSkillConfigadapters 实现 Node skill loader:
core 增加 skill registry / manager:
skills_listing 收集器渲染可用 skill 列表(ContextBuilder 不再渲染,见根目录 docs/skills-live-catalog.md)。Skill tool contract。Skill tool 只通过 SkillPort 加载正文,不直接读文件。bootstrap 负责组装:
SkillPort 到 runtime/tool executor。P0 只要求每个 skill 目录包含 SKILL.md:
my-skill/
SKILL.md
references/
scripts/
assets/
SKILL.md 必须有 YAML frontmatter:
---
name: my-skill
description: When this skill should be used
---
# My Skill
Workflow instructions.
P0 frontmatter:
name:必填。建议 ^[a-z0-9]+(-[a-z0-9]+)*$,长度 1-64。description:必填,长度 1-1024。when_to_use:可选,追加到描述预算中。license:可选,仅 metadata。metadata:可选,string map。P1/P2 预留字段:
allowed-toolsmodeleffortcontext: inline | forkdisable-model-invocationpolicy.allow_implicit_invocationdependencies.toolsinterfaceP0 对未知字段不报错,但写入 diagnostics,避免未来字段被静默误用。
P0 默认 roots:
skills.roots[],按配置顺序优先。<dir>/.zcode/skills/*/SKILL.md。<dir>/.agents/skills/*/SKILL.md。<dir>/.claude/skills/*/SKILL.md。~/.zcode/skills/*/SKILL.md~/.agents/skills/*/SKILL.md~/.claude/skills/*/SKILL.mdProject discovery 使用 nearest-first 语义:先检查当前 working directory 的 native / agent-compatible / Claude-compatible roots,再检查父目录,直到包含 .git marker 的 worktree root;如果找不到 worktree root,则只使用传入 working directory 的 project roots,避免意外扫描到用户目录或文件系统根。
已实现的额外 roots:
bootstrap/src/app/bundled-skills.ts):随 CLI 内置的技能包
apps/zcode-cli/packages/bundled-skills/skills/,以 source: "bundled"、scope: "system"
进入发现,与 plugin roots 并列注入 runtime、zcode skills list 与协议 skill catalog。它不是
插件:不进市场目录、没有启停开关、不能卸载、不出现在 Settings 与 $ 引用面板
(skills/referenceCatalog 过滤 bundled)。开发态/桌面态/ZCode Server 发行包沿官方插件同款
候选目录在入口旁原地读取(Server 发行包 stage 在 runtime/packages/bundled-skills);SEA
按内容 hash 一次性解压到 ~/.zcode/cli/bundled-skills/<hash>/。包内每个
文件都是必需资产,缺一拒绝整包并告警。首个成员是 dynamic-workflows
(docs/dynamic-workflow/authoring.md)。Plugin-provided skill roots are defined by
plugin-compat.md. They use the same SKILL.md file
format and enter discovery as source: "plugin" roots. Plugin loading does not
inline skill bodies; the existing Skill tool remains the only P0 path for
loading full plugin skill content into the model context.
P2 增加:
优先级:
同名 skill 默认取高优先级项,重复项进入 diagnostics。后续如果引入 plugin namespace,可支持 plugin:skill-name。
当前实现:列表作为 skills_listing 附件进入会话(不在 system prompt),首发为完整列表,之后只追加新增或内容变化的行,删除不通知;格式、指纹与持久化见根目录 docs/skills-live-catalog.md。下文为最初的规划格式。
ContextBuilder 增加 Skills section,位于 user/project context 之后。工具说明不再作为
ContextBuilder section 注入 system prompt,而是随 model request 的 tools 数组提供。
内容只包含 metadata,不包含正文。初始格式:
## Skills
A skill is a set of local instructions stored in SKILL.md. Use the Skill tool to load full instructions only when relevant.
### Available Skills
- my-skill: When this skill should be used (file: /abs/path/to/SKILL.md)
### How To Use Skills
- If the task matches a skill description, call Skill with the skill name before doing the task.
- If the user types `/<skill-name>`, call Skill only when that exact skill appears in the available skills list. Do not guess missing skill names.
- After loading a skill, read only the referenced files you need.
- Resolve relative references from the skill base directory.
预算策略:
Skill Tool工具名采用 Skill,保持与现有 Read / Write / Edit / Bash 命名风格一致。
输入:
{
"name": "my-skill",
"args": "optional user/model supplied arguments"
}
P0 输出:
<skill_content name="my-skill">
# Skill: my-skill
...SKILL.md body...
Base directory for this skill: /abs/path/to/my-skill
Relative paths in this skill are relative to this base directory.
</skill_content>
例外:动态工作流 mode 为 onDemand 且工具面尚未激活时,加载 dynamic-workflows 的结果在 <skill_content>
之前多一段固定提示,让模型请用户发送 /workflow;子 runtime(Agent 子代理、/expert 子会话)拿到的那段改为让它把限制报告给父代理(见 Tool Change Chain
「Skill 结果的工作流工具未激活提示」)。
工具 metadata:
readOnly: truedestructive: falseconcurrentSafe: truesideEffectScope: "session",因为它会改变当前会话上下文。riskLevel: "low" for safe inline skills。maxOutputBytes 默认 100k。timeoutMs 默认 30s。权限策略:
allowed-tools、model、effort、context: fork、hooks、dependencies、remote/plugin source 时,默认 ask。Skill(my-skill) 和 wildcard,例如 Skill(internal-*)。P1 行为:
allowed-tools 不直接绕过权限,只作为 permission suggestion 或 scoped allow rule。model / effort 通过 runtime context modifier 生效,并记录 event。context: fork 等 subagent runtime 稳定后再实现。P0 增加 slash command:
/skill [<skill-name> [task...]]
语义:
/skill 是只读 discovery,列举当前工作目录可发现的 skills;它不创建 app/session、不加载模型、不提交 prompt,也不调用 Skill tool。<skill-name> 对应 SKILL.md frontmatter 的 name。[task...] 可选。存在时,表示“使用该 skill 执行这个任务”;不存在时,表示“先加载该 skill 并基于 skill 指令响应当前 turn”。<skill-name> 的 command 不直接读取 skill 文件,不绕过 Skill tool、permission、trace 和 session persistence。Skill tool 加载 <skill-name>,再继续原任务。--prompt "/skill" 和 TUI 输入 /skill 都返回 skill 列表;--prompt "/skill <name> ..." 和 TUI 输入 /skill <name> ... 都改写成显式 Skill tool 调用要求。P1 可增加 $skill-name explicit mention,但 P0 先用 slash command,避免和普通自然语言混淆。
当前已实现只读 CLI 命令:
zcode skills list
zcode skills inspect <name>
语义:
skills list 只做本地 skill discovery,不创建 session,不加载模型,不提交 prompt,也不调用 Skill tool。SkillPort/Node skill adapter 契约;CLI 只负责解析子命令、格式化输出和退出码,不直接遍历 skill 目录。zcode skills 等价于 zcode skills list。<skill-name> 给 /skill <name> 使用。--json 输出 { skills, diagnostics, totalDiscovered, cwd },其中 skills 保留 name、description、whenToUse、scope、source、path、directory、rootPath。--verbose 在普通输出后追加 discovery diagnostics;默认普通输出不打印 warning,避免正常使用时被可恢复诊断刷屏。skills inspect <name> 只做本地 discovery 加单个 skill body 读取,不创建 session、不加载模型、不提交 prompt,也不调用 Skill tool。skills inspect <name> 必须复用 bootstrap 注入的 SkillPort/Node skill adapter 契约;CLI 只负责解析子命令、格式化输出和退出码,不直接读取 SKILL.md。skills inspect <name> 普通输出展示 name、scope/source、path、directory、description、whenToUse、safe-to-auto-load、size/truncation 和去掉 frontmatter 后的正文。skills inspect <name> --json 输出 { cwd, skill, diagnostics },其中 skill 保留 metadata、baseDirectory、content、bytesRead、sizeBytes 和 truncated。skills inspect <name> --verbose 在普通输出后追加 discovery diagnostics;默认普通输出不打印 warning。skills 子命令或参数数量错误返回非 0,并打印 Usage: zcode skills [list|inspect <name>]。错误行为:
Error: <message>;--verbose 可追加 stack。skills inspect <name> 找不到 skill 时返回非 0,并输出稳定的 Skill not found: <name> 错误;如果 skill 功能被配置禁用,返回非 0,并输出 Skills are disabled.。No skills found.;JSON 中 skills 为空数组。RuntimeConfig 增加候选字段:
features.skill
skills.enabled
skills.includeInstructions
skills.metadataBudget
skills.roots
skills.compatibility.claude
skills.compatibility.agents
skills.rules
Skill 配置不声明环境变量入口。需要调整 skill 开关、兼容目录或 metadata budget 时,走 config file、CLI/session override 或更高层的运行时配置契约。
新增 ZCODE_ 环境变量必须先补齐 spec,明确用途、优先级、错误行为和测试覆盖,避免把临时实现细节扩散成长期配置面。
Config 合并仍走 system/user/project/session/env/cli 优先级,core 不读取环境变量。
新增或复用事件语义:
SkillRootsResolvedSkillDiscoveredSkillDiscoveryWarningSkillLoadRequestedSkillLoadedSkillLoadFailed每个事件必须携带 traceId/sessionId/turnId。日志默认不记录完整 skill 正文,只记录 name、scope、source、path hash、size、truncated、durationMs。
需要在 context diagnostics 中暴露:
path / url / fs adapter,不手写分隔符。SkillPort 只接受 adapter 已规范化的 absolute path 或 stable skill id。Skill tool 读取 skill directory 之外的文件。Bash/ExecutionPort 和权限。maxOutputBytes 时返回截断摘要和 diagnostic,后续进入 artifact/storage。Status: completed. 本文档、contract 边界和 CLI/core/adapter 测试骨架已经存在。
验收:
Status: completed for P0. packages/contracts/src/skills/index.ts 和 packages/contracts/src/tools/skill.ts 已提供共享类型、运行时 schema、provider JSON schema 和 diagnostics/error code;更细粒度 policy / dependency 字段仍保留为后续扩展。
packages/contracts/src/skills 增加 skill schema/type。RuntimeConfig 增加最小 skills 配置。验收:
contracts 类型可被 core/adapters/bootstrap 单独引用。Status: completed for P0. Node loader 已支持 native/compatible project 与 user roots、extra roots、从 cwd 向上到 worktree root 的 project discovery、frontmatter diagnostics、重复名优先级、正文截断和取消前检查;symlink canonical 去重、remote/cache 和 plugin roots 尚未实现。
packages/adapters/src/skills 实现 Node loader。验收:
Status: completed for P0. 内置 registry 已注册 Skill tool,并允许在没有 SkillPort 时省略该 tool;session 级 loaded-skill snapshot 和 compact 恢复仍未实现。ContextBuilder 曾把 available skills 渲染为独立 meta_user section;现在列表只由 skills_listing 收集器渲染,ContextBuilder 只按会话开始时的目录决定 skill 引导语。
SkillRegistry / SkillManager。skills_listing 收集器渲染 Skills 列表。验收:
Skill ToolStatus: completed for P0 inline load. Tool handler 已通过 SkillPort 读取正文并返回 <skill_content>,声明 output schema、result budget、timeout、取消和 trace 传播;按 skill name/policy 动态审批、unsafe frontmatter ask、runtime modifier 和 fork context 尚未实现。
Skill tool。SkillPort 加载正文。验收:
Skill 后,下一轮 model 能看到 skill body。Status: partially completed. zcode skills / zcode skills list、zcode skills inspect <name>、--json、--verbose diagnostics、TUI/command-center 裸 /skill list、/skill <name> [task] prompt rewrite、ZCode app-server /skill <name> [task] 已实现。TUI/debug diagnostics projection 尚未实现。
zcode skills list:已实现,只读 discovery,不启动模型/session。zcode skills inspect <name>:已实现,只读 discovery 加正文读取,不启动模型/session,CLI 不直接读取 SKILL.md。验收:
--json。$skill-name 和 skill://.../SKILL.md。docs: add skill integration planrefactor(contracts): add skill contractsfeat(adapters): add node skill loaderfeat(core): render available skills in contextfeat(tool): add Skill toolfeat(permission): support skill permission rulesfeat(cli): add skills commandstest: add skill boundary coverage