Back to home

Compare

Comparing: 通 过 AGENTS.md、Hooks 与 Skills 强制执行组件复用 & Component Reuse Enforcement via AGENTS.md, Hooks, and Skills

AZH
Claude CodeAGENTS.mdmonorepo·

通 过 AGENTS.md、Hooks 与 Skills 强制执行组件复用

事件概述

得物技术的工程师近日在 掘金发布了一份详尽的落地实践报告,描述了一套已 部署至生产环境的三层 Skill 架构——专为 Claude Code 设计,用于在 monorepo 环境中 强制执行组件复用策略。该系统综合运用了 AGENTS.md 持久上下文、 UserPromptSubmit Hook 进行意图路由,以及以 find-component.js 为统一入口的结 构化 Skill,在新组件被创建之前拦截代码生成行为。

团队明确指出的核心问题在于: 无论是开发者还是 AI Agent,面对需求时 都倾向于直接创建新组件,而非优先检索已有组件,由 此导致组件库膨胀、维护成本攀升、UI 一致性下降。团队对这一 问题的定性带有鲜明的行为视角—— 问题的根源并不在于搜索能力的缺失,而在于 Agent 的决策顺序出了偏差。

为何值得关注

这篇报告是目前公开资料中,将 Claude Code Skills 落 地于生产级 monorepo 最为严谨的案例之 一。其中若干发现对正在评估 Agentic 编程工具的工程团队具有直接参 考价值:

  • 隐式 Skill 触发机制并不可 靠。团队引用了 Vercel 自身的 Agent 评测 数据:默认的 Skill 触发方式并未提 升任务通过率;而显式指令 配合 AGENTS.md 式的被动上下文注入,则 能产生更稳定的行为表现。这一结论与多 个 Agentic 框架中观察到的现象高度吻合。
  • OpenAI Codex Skills 采用渐进式信息披露机制。文章援 引 Codex Skills 文档指出:Agent 在运行时 仅首先读取 Skill 的元数据与描述,只有在激 活时才加载完整的 SKILL.md——这使得 description 字段成为触发行为的核心接触面。因 此,任何框架下构建 Skill 的团队都应将 description 视为路由契约,而非普通文档。
  • 统一入口可有 效降低 Agent 的幻觉路径。通过将 scan-componentsmatch-componentresolve- scope 合并为单一的 find-component.js 调用,团队消除了分支不 确定性。Agent 只需执行一个业务动作——"检索 可复用组件"——而无需独立编排多个子步骤。

技 术细节

三层架构设计

整套系统的结 构如下:

  • 第一层 — AGENTS.md(持久上下文): 编码了复用优先规则、组件索引入口,以及扫 描后的描述补全流程。每个会话均默 认加载。其目的在于:确保 Agent 始终知晓该 机制的存在。
  • 第二层 — Hook(UserPromptSubmit):当用户 Prompt 中出现"封装组件"或"是否有现成组件"等语义信号时,系统会 在 Prompt 被处理之前注入额外上下文。Claude Code 官方文档记录的 UserPromptSubmit Hook 支持在此阶段进 行 additionalContext 注入。
  • 第三层 — Skill(find-component.js):接受 queryrepoRootstartDir 三个参数。内部逻辑如 下:若 components.csv 不存在,则自动触发 run-scan.js;调用 resolve-scope 计算搜索边界;调用 match-component 获取排序结果;记录使用情 况以支持频率加权;最终返回固定 JSON 协议,包含成 功、无匹配、触发扫描和失败四种状态字段。

Monorepo 作用域解析

resolve-scope.js 通过解析 pnpm- workspace.yaml 枚举工作区包,从当前聚焦的文件路径反向 推算 currentAppRoot,进而构建搜索范围:当前应用 + 根级共享包(如 apps/_share/packages/)。仅当 startDir 等于仓库根目录时,才会激 活全仓库搜索。这一逻辑完全复刻了人类工 程师的搜索习惯:先在当前应用中查找,再检索共享库 ,而非一上来就遍历整个 monorepo。

多因子匹配机 制

match-component.jsfuzzy-match.js 实现了一 套复合评分模型,综合以下多维信号进行打 分:

  • 精确匹配与子串名称匹配
  • 编辑距离模糊匹配
  • Token 重叠度
  • 首字母 缩写匹配(例如 dlpDateLinkPicker
  • 当前应用加 权(本地组件优先排名)
  • 使用频率加权(每次命 中均记录)
  • 来源质量加权(基于 README 推断的描述优 先于纯粹推断的描述)
  • 文件存在性校验(缺失文件降 权或过滤)
  • 记录类型优先级(组件条目优先于依赖条目)

系统设有可配置的低分阈值(NO_MATCH_SCORE_THRESHOLD)以过滤噪声:若最高 分结果低于阈值,系统将触发一次重新扫描并重新查询,之 后再返回无匹配响应,从而防止低置信度的命中结 果直接传递给 Agent。

值得持续关注的方向

  • Claude Code Hooks API 的演进:UserPromptSubmit Hook 是当 前可用的机制——Anthropic 一直在持续迭代 Claude Code 的扩展接口。一旦 Hook 生命周期或 additionalContext 的 Payload Schema 发生变更,文中所描 述的路由层将需要相应更新。
  • Codex Skills 的采用模式:Open AI 为 Skills 设计的渐进式信息披露模型( 元数据优先加载)在架构上与 AGENTS.md 方案存在本质差异。随着更多团队发布对 比实现,各框架触发可靠性的基准数据有望 逐步浮现。
  • Monorepo 工具链集成:作用域解析对 pnpm-workspace.yaml 的依赖,意味着该方案在 当前实现下是 pnpm 专属的。使用 Nx、Turbore po 或 Yarn Workspaces 的团队需要相应的工作区 Manifest 解析器——这很可能成为该工具开源 后的主要 Fork 分叉点。
  • 组件索引的时 效性:components.csv 缺失时自动触发扫描的回退机制是一种正 确性保障,但在大型 monorepo 中,扫描延 迟可能导致 Agent 响应时间变长。随着该模式的规 模化推广,缓存方案或增 量索引方案值得重点关注。
Source: juejin.cn
BEN
Claude-CodeAGENTS.mdmonorepo·

Component Reuse Enforcement via AGENTS.md, Hooks, and Skills

What Happened

Engineers at Dewu (得物) Technology published a detailed implementation report — via Juejin — describing a production-deployed, three-layer skill architecture for Claude Code that enforces component reuse in a monorepo environment. The system uses AGENTS.md persistent context, a UserPromptSubmit hook for intent routing, and a structured Skill with a unified entry point (find-component.js) to intercept code generation before new components are created.

The stated problem: developers and AI agents default to creating new components rather than searching for existing ones, causing library bloat, rising maintenance costs, and degraded UI consistency. The team 's framing is explicitly behavioral — the problem is not search capability , it is the agent's decision sequence.

Why It Matters

This write-up is one of the more rigorous public accounts of operationalizing Claude Code skills in a production mon orepo. Several findings are directly actionable for engineering teams evalu ating agentic coding tooling:

  • Implicit skill triggering is unreliable. The team cites Vercel's own agent evalu ations: default skill triggering did not improve task pass rates; explicit instructions and AGENTS.md-style passive context produced more stable behavior. This matches observed behavior across multiple agentic frameworks.
  • OpenAI Codex Skills use progressive disclosure. The post references Codex Skills documentation noting that agents first read only a skill's metadata/ description at runtime, loading the full SKILL.md only upon activation — making the description field the primary trigger surface. Teams building skills for any framework should treat the description as a routing contract, not documentation.
  • Unified entry points reduce agent hallucination paths. By collapsing scan-components, match-component, and resolve-scope into a single find-component.js call, the team eliminated branching uncertainty. The agent executes one business action — "check for reusable component " — rather than orchestrating multiple sub-steps independently.

The Technical Detail

Three-Layer Architecture

The system is structured as follows:

  • Layer 1 — AGENTS.md (persistent context): Encodes the reuse-first rule, the component index entry point, and the post-scan description completion flow. Present every session. Purpose: ensure the agent knows the mechanism exists.
  • Layer 2 — Hook ( UserPromptSubmit): When the user's prompt contains semantic signals like "封装组件" (encapsulate component) or "是 否有现成组件" (is there an existing component), additional context is injected before the prompt is processed. Claude Code 's documented UserPromptSubmit hook supports additionalContext injection at this stage.
  • Layer 3 — Skill (find-component.js): Accepts query, repoRoot, and startDir. Internally: auto-triggers run-scan .js if components.csv is absent; calls resolve-scope to compute search boundaries; calls match-component for ranked results; logs usage for frequency weighting; returns a fixed JSON protocol with fields for success, no-match, scan-triggered, and failure states .

Monorepo Scope Resolution

resolve-scope.js parses pnpm- workspace.yaml to enumerate workspace packages, back-calculates currentAppRoot from the focused file path, then constructs a search set of: current application + root-level shared packages (e.g., apps/_share/, packages/). Full-repo search activ ates only when startDir equals the repo root. This mirrors how a human engineer searches : current app first, then shared libraries, not the entire monorepo.

Multi-Factor Matching

match-component.js and fuzzy-match. js implement a composite scoring model with the following signals, in combination:

  • Exact and substring name match
  • Edit-distance fuzzy match
  • Token overlap
  • Acron ym matching (e.g., dlpDateLinkPicker)
  • Current-app boost (local components ranked higher)
  • Usage frequency weighting (recorded on each match hit)
  • Source quality weighting (README-inferred descriptions outrank pure inferred)
  • File existence validation (missing files are downweighted or filtered)
  • Record type priority (components ranked above dependency entries)

A configurable low-score threshold (NO_MATCH_SCORE_THRESHOLD) gates noise: if the top result scores below the threshold, the system triggers one re -scan and re-queries before returning a no-match response. This prevents low -confidence hits from reaching the agent.

What To Watch

  • Claude Code hooks API evolution: The UserPromptSubmit hook is a current mechanism — Anthropic has been iter ating on Claude Code's extension surface. Any changes to hook lifecycle or additionalContext payload schema would require updates to the routing layer described here.
  • Codex Skills adoption patterns: Open AI's progressive disclosure model for Skills (metadata-first loading) is architect urally different from the AGENTS.md approach. As more teams publish comparative implementations, benchmark data on trigger reliability across frameworks should emerge.
  • Monorepo tooling integration: The pnpm-workspace.yaml dependency for scope resolution means this approach is pn pm-specific as implemented. Teams on Nx, Turborepo, or Yarn workspaces would need equivalent workspace manifest parsers — a likely fork point for any open-source release of this tooling.
  • Component index freshness: The auto-scan fall back on missing components.csv is a correctness safeguard, but scan lat ency in large monorepos could introduce agent response delays. Watch for c aching or incremental indexing approaches as this pattern scales.
Source: juejin.cn