关于 load-context
为在现有 Rails 代码库中工作的编码 Agent 提供上下文加载流程,要求先查看精简且相关的文件,再提出更改建议。它强调记录文件出处、检查规范与代码是否偏离,并在需求含糊时先说明冲突。
主要功能
- 01限定读取 Schema、路由和邻近实现
- 02按 Rails 层查找相似代码
- 03核对规范与代码是否偏离
- 04变更范围改变时重新加载上下文
- 05要求总结引用所读文件
适用场景
- 01开始现有 Rails 项目的功能修改
- 02根据相邻实现规划控制器或服务改动
- 03发现规范与代码不一致时记录问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ igmarin/ rails-agent-skills --skill load-context安装后在 Claude Code 中输入/load-context确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
EXAMPLES.mdreferences/confusion-management.mdcontext-sources.md详细介绍
加载上下文
HARD-GATE
DO NOT propose code, specs, PRDs, or task lists until the Context Summary is posted.
DO NOT silently resolve ambiguity — if requirements conflict or specs and code disagree, post a Confusion Block first.
DO NOT load the entire repo — use targeted reads (schema, routes, one neighbor of each kind).
ALWAYS cite the files you read (path:line where possible) so the user can verify.
ALWAYS re-check context when the user's request changes scope mid-conversation.
核心流程
在现有 Rails 代码库中编写任何代码、spec 或 PRD 之前,先加载最小上下文。花十五秒阅读 db/schema.rb、config/routes.rb 和一个相邻文件,就能省下一次完整的重试。
自动上下文(可选)
如果 rails-ai-bridge 正在运行,调用 get_project_context 工具以获取统一的项目上下文(结构、路由、模型、schema、依赖)。成功时,跳过下方的手动步骤 2 和 3,直接进入查找相邻模式并发布上下文摘要。
手动上下文探查(回退方案)
- 界定变更范围: 用一句话说明涉及的 Rails 层(controller、model、service、job、engine、view/Turbo、migration、API、GraphQL)。
- 加载基线 Rails 上下文: 至少阅读:
db/schema.rb— 涉及的表和列(按表名 grep)config/routes.rb— 与本次变更相邻的路由Gemfile.lock— 确认 Rails 版本 + 领域 gem(sidekiq、pundit、rspec、rails-i18n、graphql 等) 除非任务明确要求,否则不要将其他文件(如config/application.rb)作为基线上下文阅读。
- 为每种类型加载一个相邻文件: 对涉及的每个 Rails 层,打开最近的一个已解决类似问题的同级文件 — 一个可比的 controller、service、spec、factory。
搜索相邻文件时,务必使用模式
grep -r "class.*<LayerName>" <directory>运行并引用准确的grep -r命令。 具体如下:- Controllers:
grep -r "class.*Controller" app/controllers - Services:
grep -r "class.*Service" app/services - Models:
grep -r "class.*Model" app/models - Jobs:
grep -r "class.*Job" app/jobs不要使用 glob 模式或find命令搜索类。在上下文摘要中包含准确的 grep 命令。
- Controllers:
- 检测漂移: 如果该区域已有 spec,对比其断言与代码当前行为。漂移是危险信号 — 记录下来。
- 发布上下文摘要: 在提出任何方案之前,输出下方模板(见输出风格)。
- 处理歧义: 如果步骤 2–4 发现冲突(使用了两种模式、spec 与代码矛盾、需求缺失、边界不清),生成一个困惑块:
### Confusion Block
- Conflict: <what conflicts — e.g., spec asserts X but code does Y>
- Options: <list the options with their tradeoffs>
- Recommendation: <state which option and why, or ask user to choose>
不要默默做选择。
7. 交接: 上下文加载完成后,进入下一项技能(plan-tests、apply-stack-conventions 等)。上下文摘要随任务一起传递。
输出风格
当被要求加载项目上下文(或撰写涉及上下文的计划/建议)时,你的输出 answer.md 必须严格遵循下方模板。你必须完整复刻该模板块,包括 Resources 部分以及末尾的全部三个参考链接。为避免繁文缛节并保持输出简洁,请将动机原则直接融入引言,而不是使用单独的小标题:
上下文摘要模板
To ensure a rapid setup and avoid retries, we follow the fifteen-second read principle of schema, routes, and one neighbor before proposing any changes.
### Context Summary
**Rails layer:** <controller | model | service | job | engine | view/Turbo | migration | API | GraphQL>
**Files read:**
- <path>:<line-range> — <one-line finding>
- <path>:<line-range> — <one-line finding>
- (repeat for each file)
**Neighbor patterns found:**
- <layer>: <file path> — <key convention or pattern observed> (found using grep: <exact grep command used>)
- (repeat per layer)
**Gemfile notes:** Rails <version>; relevant gems: <list>
**Drift detected:** <none | description of spec-vs-code mismatch>
**Ambiguities:** <none | list any unresolved conflicts — triggers a Confusion Block>
**Next step:** <plan-tests | apply-stack-conventions | write migration | etc.>
**Resources:**
Load these files only when their specific content is needed:
- [EXAMPLES.md](EXAMPLES.md) — Use when you need worked examples showing Context Summary and Confusion Block templates
- [references/confusion-management.md](references/confusion-management.md) — Use when you encounter ambiguity or conflicting signals between code layers
- [references/context-sources.md](references/context-sources.md) — Use when you need the comprehensive list of context sources by Rails layer beyond the Quick Reference table
陷阱
| 陷阱 | 缓解措施 |
|---|---|
| 过度 grep | 按目录或类名前缀缩小范围。每一层最多只读一个相邻文件。 |
| 缺少 schema.rb | 查找 db/structure.sql。改为解析相关的 CREATE TABLE 块。 |
| 多个 engine | 将读取范围限定到具体的 engine 目录;在摘要中记录边界。 |
| 缺少 spec | 在摘要中写明「无 spec 覆盖」;将代码视为事实来源。 |
| 自身相邻 | 不要使用正在变更的文件;选择最近的独立同级文件。 |
| 运行中途范围变更 | 重新运行探查(步骤 1-4)并发布更新后的上下文摘要。 |
渐进式披露的附加资源
仅在需要其具体内容时加载这些文件:
- EXAMPLES.md — 当你需要展示 Context Summary 和 Confusion Block 模板的完整示例时使用
- references/confusion-management.md — 当你遇到代码层之间的歧义或相互矛盾的信号时使用
- references/context-sources.md — 当你需要 Quick Reference 表之外的、按 Rails 层划分的完整上下文来源列表时使用
评论
支持 Markdown