研究技能Skill skill-researcher

此技能是一个薄包装,用于委托一般研究任务给AI代理,通过代码库探索、网络搜索和文档分析,支持软件开发中的研究需求。关键词:研究、代码库、文档搜索、AI代理、自动化、实现规划。

AI智能体 0 次安装 0 次浏览 更新于 3/22/2026

name: 技能-研究员 description: 使用网络搜索、文档和代码库探索进行一般研究。为一般研究任务调用。 allowed-tools: Task, Bash, Edit, Read, Write

原始上下文(现在由子代理加载):

- .claude/context/core/formats/report-format.md

原始工具(现在由子代理使用):

- Read, Write, Edit, Glob, Grep, WebSearch, WebFetch


研究员技能

薄包装,将一般研究委托给 general-research-agent 子代理。

重要:此技能实现技能内部后处理模式。在子代理返回后, 此技能处理所有后处理操作(状态更新、工件链接、git提交)然后返回。 这消除了技能返回和协调器之间的“继续”提示问题。

上下文引用

引用(不要急切加载):

  • 路径:.claude/context/core/formats/return-metadata-file.md - 元数据文件模式
  • 路径:.claude/context/core/patterns/postflight-control.md - 标记文件协议
  • 路径:.claude/context/core/patterns/file-metadata-exchange.md - 文件 I/O 助手
  • 路径:.claude/context/core/patterns/jq-escaping-workarounds.md - jq 转义模式(问题 #1132)

注意:此技能是一个带有内部后处理的薄包装。上下文由委托的代理加载。

触发条件

此技能在以下情况激活:

  • 任务语言是“general”、“meta”、“markdown”、“latex”或“typst”
  • 需要研究用于实现规划
  • 需要收集文档或外部资源

执行流程

阶段 1:输入验证

验证必需输入:

  • task_number - 必须提供并在 state.json 中存在
  • focus_prompt - 可选,用于研究方向
# 查找任务
task_data=$(jq -r --argjson num "$task_number" \
  '.active_projects[] | select(.project_number == $num)' \
  specs/state.json)

# 验证存在
if [ -z "$task_data" ]; then
  return error "Task $task_number not found"
fi

# 提取字段
language=$(echo "$task_data" | jq -r '.language // "general"')
status=$(echo "$task_data" | jq -r '.status')
project_name=$(echo "$task_data" | jq -r '.project_name')
description=$(echo "$task_data" | jq -r '.description // ""')

阶段 2:预飞行状态更新

在调用子代理之前,将任务状态更新为“researching”。

更新 state.json

jq --arg ts "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
   --arg status "researching" \
   --arg sid "$session_id" \
  '(.active_projects[] | select(.project_number == '$task_number')) |= . + {
    status: $status,
    last_updated: $ts,
    session_id: $sid
  }' specs/state.json > /tmp/state.json && mv /tmp/state.json specs/state.json

更新 TODO.md:使用 Edit 工具将状态标记从 [NOT STARTED][RESEARCHED] 更改为 [RESEARCHING]


阶段 3:创建后处理标记

创建标记文件以防止过早终止:

# 确保任务目录存在
padded_num=$(printf "%03d" "$task_number")
mkdir -p "specs/${padded_num}_${project_name}"

cat > "specs/${padded_num}_${project_name}/.postflight-pending" << EOF
{
  "session_id": "${session_id}",
  "skill": "skill-researcher",
  "task_number": ${task_number},
  "operation": "research",
  "reason": "Postflight pending: status update, artifact linking, git commit",
  "created": "$(date -u +%Y-%m-%dT%H:%M:%SZ)",
  "stop_hook_active": false
}
EOF

阶段 4:准备委托上下文

为子代理准备委托上下文:

{
  "session_id": "sess_{timestamp}_{random}",
  "delegation_depth": 1,
  "delegation_path": ["orchestrator", "research", "skill-researcher"],
  "timeout": 3600,
  "task_context": {
    "task_number": N,
    "task_name": "{project_name}",
    "description": "{description}",
    "language": "{language}"
  },
  "focus_prompt": "{optional focus}",
  "metadata_file_path": "specs/{NNN}_{SLUG}/.return-meta.json"
}

阶段 5:调用子代理

关键:您必须使用 Task 工具来生成子代理。

必需工具调用

Tool: Task (NOT Skill)
Parameters:
  - subagent_type: "general-research-agent"
  - prompt: [包括 task_context, delegation_context, focus_prompt, metadata_file_path]
  - description: "为任务 {N} 执行研究"

不要 使用 Skill(general-research-agent) - 这将失败。

子代理将:

  • 搜索代码库以寻找相关模式
  • 搜索网络以获取文档和示例
  • 分析发现并合成推荐
  • specs/{NNN}_{SLUG}/reports/ 中创建研究报告
  • 将元数据写入 specs/{NNN}_{SLUG}/.return-meta.json
  • 返回一个简短的文本摘要(非 JSON)

阶段 6:解析子代理返回(读取元数据文件)

子代理返回后,读取元数据文件:

metadata_file="specs/${padded_num}_${project_name}/.return-meta.json"

if [ -f "$metadata_file" ] && jq empty "$metadata_file" 2>/dev/null; then
    status=$(jq -r '.status' "$metadata_file")
    artifact_path=$(jq -r '.artifacts[0].path // ""' "$metadata_file")
    artifact_type=$(jq -r '.artifacts[0].type // ""' "$metadata_file")
    artifact_summary=$(jq -r '.artifacts[0].summary // ""' "$metadata_file")
else
    echo "错误:无效或缺少元数据文件"
    status="failed"
fi

阶段 7:更新任务状态(后处理)

如果状态是“researched”,更新 state.json 和 TODO.md

更新 state.json

jq --arg ts "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
   --arg status "researched" \
  '(.active_projects[] | select(.project_number == '$task_number')) |= . + {
    status: $status,
    last_updated: $ts,
    researched: $ts
  }' specs/state.json > /tmp/state.json && mv /tmp/state.json specs/state.json

更新 TODO.md:使用 Edit 工具将状态标记从 [RESEARCHING] 更改为 [RESEARCHED]

在部分/失败时:保持状态为“researching”以便恢复。


阶段 8:链接工件

添加工件到 state.json 并附上摘要。

重要:使用两步 jq 模式以避免问题 #1132 转义错误。参见 jq-escaping-workarounds.md

if [ -n "$artifact_path" ]; then
    # 步骤 1:过滤出现有的研究工件(使用“| not”模式以避免 != 转义 - 问题 #1132)
    jq '(.active_projects[] | select(.project_number == '$task_number')).artifacts =
        [(.active_projects[] | select(.project_number == '$task_number')).artifacts // [] | .[] | select(.type == "research" | not)]' \
      specs/state.json > /tmp/state.json && mv /tmp/state.json specs/state.json

    # 步骤 2:添加新的研究工件
    jq --arg path "$artifact_path" \
       --arg type "$artifact_type" \
       --arg summary "$artifact_summary" \
      '(.active_projects[] | select(.project_number == '$task_number')).artifacts += [{"path": $path, "type": $type, "summary": $summary}]' \
      specs/state.json > /tmp/state.json && mv /tmp/state.json specs/state.json
fi

更新 TODO.md:添加研究工件链接:

- **研究**: [research-{NNN}.md]({artifact_path})

阶段 9:Git 提交

提交更改并附上会话 ID:

git add -A
git commit -m "任务 ${task_number}: 完成研究

会话: ${session_id}

共同作者: Claude Opus 4.5 <noreply@anthropic.com>"

阶段 10:清理

删除标记和元数据文件:

rm -f "specs/${padded_num}_${project_name}/.postflight-pending"
rm -f "specs/${padded_num}_${project_name}/.postflight-loop-guard"
rm -f "specs/${padded_num}_${project_name}/.return-meta.json"

阶段 11:返回简短摘要

返回一个简短的文本摘要(非 JSON)。示例:

为任务 {N} 完成研究:
- 找到 {count} 个相关模式和资源
- 确定实现方法: {approach}
- 创建报告在 specs/{NNN}_{SLUG}/reports/research-{NNN}.md
- 状态更新为 [RESEARCHED]
- 更改已提交

错误处理

输入验证错误

如果任务未找到,立即返回错误消息。

元数据文件缺失

如果子代理没有写入元数据文件:

  1. 保持状态为“researching”
  2. 不要清理后处理标记
  3. 向用户报告错误

Git 提交失败

非阻塞:记录失败但继续成功响应。

子代理超时

如果子代理超时(默认 3600 秒),返回部分状态。 保持状态为“researching”以便恢复。


返回格式

此技能返回一个简短的文本摘要(非 JSON)。JSON 元数据被写入文件并在内部处理。

示例成功返回:

为任务 412 完成研究:
- 找到 8 个相关模式用于实现
- 确定延迟上下文加载和技能到代理映射模式
- 创建报告在 specs/412_general_research/reports/research-001.md
- 状态更新为 [RESEARCHED]
- 更改已提交,会话 sess_1736700000_abc123

示例部分返回:

为任务 412 部分完成研究:
- 找到 4 个代码库模式
- 由于网络错误,网络搜索失败
- 部分报告创建在 specs/412_general_research/reports/research-001.md
- 状态保持 [RESEARCHING] - 运行 /research 412 继续