Skip to content

feat(div-anthropic): skill body-defer 懒加载 (closes #77, CC 2.1.234, item 9) - #78

Merged
dahai80 merged 1 commit into
mainfrom
feat/skill-body-defer-lazyload
Aug 20, 2026
Merged

dahai80 merged 1 commit into
mainfrom
feat/skill-body-defer-lazyload

Conversation

@dahai80

@dahai80 dahai80 commented Aug 20, 2026

Copy link
Copy Markdown
Owner

概述

落地 enhance-0819.md Tier-1 item 9 — 技能正文 body-defer 懒加载, 对齐 Claude Code 2.1.234 "skill 按需加载正文" 模式。Closes #77。

背景 (审计结论)

  • token 目标本已达成: system prompt / skill-listing 已是 frontmatter-only + 1% context budget cap (attachments.ts, SkillTool/prompt.ts); bundled + MCP skill 已惰性 (dynamic import / remote fetch)。
  • 剩余 gap: directory/plugin skill 的 SKILL.md 正文在 registry build 时 eager fs.readFile 全文, 作为 markdownContent: string 常驻 Command 闭包。loadSkillsDir.ts:97-99 注释 claim "only loaded on invocation" 但代码矛盾 — 过时注释。
  • 决策: 做 body-defer 重构 (对齐 CC 2.1.234 模式 + 修过时注释)。

改动

src/skills/loadSkillsDir.ts (+29/-7 surgical, 保留原 2-space/单引号风格)

  • createSkillCommand 签名改双可选: markdownContent?: string + skillFilePath?: string (向后兼容, export 类型给 MCP dead-path 不破坏)。
  • 闭包 getPromptForCommand: file-backed skill (skillFilePath 有值) 首次调用 ??= lazy fs.readFile 全文 + Promise memo (镜像 bundledSkills.ts:65-73 模式, 并发 invoke 共享 in-flight read); MCP/inline 走 markdownContent eager。
  • 2 调用点 (loadSkillsFromSkillsDir / loadSkillsFromCommandsDir) 传 skillFilePath, build 时 markdownContent 局部变量函数返回 GC 回收, body 不常驻。
  • contentLength: markdownContent?.length ?? 0 (全 repo 零 reader, 无害)。
  • 修 estimateSkillFrontmatterTokens 过时注释 — frontmatter-only 估算对全部 skill 来源成立。

src/__tests__/skills/skillLazyLoad.test.ts (新增, 6 单测)

  • contentLength=0 (body 不常驻闭包)
  • 首次 invoke lazy read, 二次命中 Promise memo (改文件后仍读旧内容 — 证 cache 命中不重读)
  • baseDir 前缀 + \$ARGUMENTS 替换正确注入 prompt
  • 无 frontmatter.description 时 fallback 取正文首行
  • estimateSkillFrontmatterTokens 不含 body (frontmatter-only)
  • markdownContent (inline/MCP) 路径仍 eager, 不走文件 read

不动: src/utils/plugins/loadPluginCommands.ts

PluginMarkdownFile.content 必填 + 5 站点 retain, 半转换闭包 lazy re-read 而不删 retain = 零 RAM 收益 + 多一次废读。真省需改类型 + 5 站点 + inline 虚拟路径 guard, 成本高收益低 (见 plan 执行期调整)。本 PR 内存胜利集中在 loadSkillsDir.ts (真实大文档场景)。

验证

  • bun run typecheck — 0 errors
  • bun test src/__tests__/skills/skillLazyLoad.test.ts — 6/6 pass
  • bun test src/__tests__/ — 122 pass / 0 fail (不回归)
  • bun run build + bun run build:dev:full — pass
  • bunx biome check src/__tests__/skills/skillLazyLoad.test.ts — clean
  • CI lint (ci.yml) 不扫 src/skills/ + src/__tests__/ (explicit allowlist), loadSkillsDir.ts 既存 lint findings 未变

取舍

  • 2 次 fs.readFile (build + 首次 invoke) — skill invoke 低频, fs.readFile <1ms, RAM 不常驻 N 个 body 串更值。
  • contentLength=0 — 零 reader, 若未来加 reader 需改 lazy 算 (Rule 2 不预判)。

Co-Authored-By: Claude Fable 5 [email protected]

directory/plugin skill 的 SKILL.md 正文不再在 registry build 时 eager
read 全文常驻 Command 闭包。createSkillCommand 签名改双可选
(markdownContent?: string + skillFilePath?: string), file-backed skill
传 skillFilePath, 首次 getPromptForCommand 时 ??= lazy read + Promise
memo (镜像 bundledSkills.ts 模式, 并发 invoke 共享 in-flight read)。
MCP/inline (无文件) 仍走 markdownContent eager, 向后兼容。

- src/skills/loadSkillsDir.ts: 2 调用点 (skills-dir/legacy) 传
  skillFilePath, contentLength=0 (零 reader, 无害), 修过时注释
- src/__tests__/skills/skillLazyLoad.test.ts: 6 单测 (contentLength=0,
  首次 lazy/二次 memo 命中, baseDir+$ARGUMENTS 注入, description fallback,
  frontmatter-only token 估算, markdownContent eager 路径)
- plugin loader (loadPluginCommands.ts) 不动: PluginMarkdownFile.content
  必填 + 5 站点 retain, 半转换零 RAM 收益 (见 plan 执行期调整)

验证: typecheck 0, 6/6 新测 + 122/0 src/__tests__ 全测, build +
build:dev:full pass, diff +29/-7 surgical (保留原 2-space/单引号风格)

Co-Authored-By: Claude Fable 5 <[email protected]>
@dahai80
dahai80 merged commit 429f0e4 into main Aug 20, 2026
1 check passed
@dahai80
dahai80 deleted the feat/skill-body-defer-lazyload branch August 20, 2026 12:00
dahai80 added a commit that referenced this pull request Aug 20, 2026
追 CC 2.1.196→2.1.234 Tier-1 特性批量落地 (13 PR):

变更自 v0.4.22:
- WS1 DirectoryAdded hook + Notification agent_* 类型 (#73)
- WS2 堆叠 slash-skill 调用, 最多 5 前导 (#74)
- LSP+filecache 内存泄漏 cap, 对齐 CC 2.1.208 (#76)
- skill body-defer 懒加载, item 9 (#78)
- SessionStart source "fork" 区分 fork 与 resume (#80)
- P2.1 子代理护栏 — 并发/会话/深度三限 (#82)
- P1.1 大输出 tool_result tail 预览 (#84)
- P4.1 --ax-screen-reader 入口点对齐 — env 别名 + settings key (#86)
- item 13 WebSearch per-session 搜索次数上限 (#88)
- P4.2 §6 /doctor FUSION_* vs ANTHROPIC_* 环境变量冲突检测 (#90)
- P4.2 #1+#3 /doctor MLX 网关连通 + 模型加载探测 (#92)
- item 4 MCP 工具自动后台化 FUSION_MCP_AUTO_BACKGROUND_MS (#94)
- item 22 审计日志凭证脱敏 (#96)

- package.json: version 0.4.22 → 0.4.23

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(div-anthropic): 技能正文 body-defer 懒加载 (CC 2.1.234 对齐, item 9)

1 participant