Files
everything-claude-code/docs/zh-CN/skills/continuous-learning-v2/agents/observer.md
zdoc.app abcf38b085 docs(zh-CN): sync Chinese docs with latest upstream changes (#341)
* docs(zh-CN): sync Chinese docs with latest upstream changes

* docs(zh-CN): update link

---------

Co-authored-by: neo <neo.dowithless@gmail.com>
2026-03-07 14:48:02 -08:00

215 lines
7.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
name: observer
description: 分析会话观察以检测模式并创建本能的背景代理。使用Haiku以实现成本效益。v2.1版本增加了项目范围的本能。
model: haiku
---
# Observer Agent
一个后台代理,用于分析 Claude Code 会话中的观察结果,以检测模式并创建本能。
## 何时运行
* 在积累足够多的观察后(可配置,默认 20 条)
* 在计划的时间间隔(可配置,默认 5 分钟)
* 当通过向观察者进程发送 SIGUSR1 信号手动触发时
## 输入
从**项目作用域**的观察文件中读取观察记录:
* 项目:`~/.claude/homunculus/projects/<project-hash>/observations.jsonl`
* 全局后备:`~/.claude/homunculus/observations.jsonl`
```jsonl
{"timestamp":"2025-01-22T10:30:00Z","event":"tool_start","session":"abc123","tool":"Edit","input":"...","project_id":"a1b2c3d4e5f6","project_name":"my-react-app"}
{"timestamp":"2025-01-22T10:30:01Z","event":"tool_complete","session":"abc123","tool":"Edit","output":"...","project_id":"a1b2c3d4e5f6","project_name":"my-react-app"}
{"timestamp":"2025-01-22T10:30:05Z","event":"tool_start","session":"abc123","tool":"Bash","input":"npm test","project_id":"a1b2c3d4e5f6","project_name":"my-react-app"}
{"timestamp":"2025-01-22T10:30:10Z","event":"tool_complete","session":"abc123","tool":"Bash","output":"All tests pass","project_id":"a1b2c3d4e5f6","project_name":"my-react-app"}
```
## 模式检测
在观察结果中寻找以下模式:
### 1. 用户更正
当用户的后续消息纠正了 Claude 之前的操作时:
* "不,使用 X 而不是 Y"
* "实际上,我的意思是……"
* 立即的撤销/重做模式
→ 创建本能:"当执行 X 时,优先使用 Y"
### 2. 错误解决
当错误发生后紧接着修复时:
* 工具输出包含错误
* 接下来的几个工具调用修复了它
* 相同类型的错误以类似方式多次解决
→ 创建本能:"当遇到错误 X 时,尝试 Y"
### 3. 重复的工作流
当多次使用相同的工具序列时:
* 具有相似输入的相同工具序列
* 一起变化的文件模式
* 时间上聚集的操作
→ 创建工作流本能:"当执行 X 时,遵循步骤 Y, Z, W"
### 4. 工具偏好
当始终偏好使用某些工具时:
* 总是在编辑前使用 Grep
* 优先使用 Read 而不是 Bash cat
* 对特定任务使用特定的 Bash 命令
→ 创建本能:"当需要 X 时,使用工具 Y"
## 输出
在**项目作用域**的本能目录中创建/更新本能:
* 项目:`~/.claude/homunculus/projects/<project-hash>/instincts/personal/`
* 全局:`~/.claude/homunculus/instincts/personal/`(用于通用模式)
### 项目作用域本能(默认)
```yaml
---
id: use-react-hooks-pattern
trigger: "when creating React components"
confidence: 0.65
domain: "code-style"
source: "session-observation"
scope: project
project_id: "a1b2c3d4e5f6"
project_name: "my-react-app"
---
# Use React Hooks Pattern
## Action
Always use functional components with hooks instead of class components.
## Evidence
- Observed 8 times in session abc123
- Pattern: All new components use useState/useEffect
- Last observed: 2025-01-22
```
### 全局本能(通用模式)
```yaml
---
id: always-validate-user-input
trigger: "when handling user input"
confidence: 0.75
domain: "security"
source: "session-observation"
scope: global
---
# Always Validate User Input
## Action
Validate and sanitize all user input before processing.
## Evidence
- Observed across 3 different projects
- Pattern: User consistently adds input validation
- Last observed: 2025-01-22
```
## 作用域决策指南
创建本能时,请根据以下经验法则确定其作用域:
| 模式类型 | 作用域 | 示例 |
|-------------|-------|---------|
| 语言/框架约定 | **项目** | "使用 React hooks"、"遵循 Django REST 模式" |
| 文件结构偏好 | **项目** | "测试在 `__tests__`/"、"组件在 src/components/" |
| 代码风格 | **项目** | "使用函数式风格"、"首选数据类" |
| 错误处理策略 | **项目**(通常) | "使用 Result 类型处理错误" |
| 安全实践 | **全局** | "验证用户输入"、"清理 SQL" |
| 通用最佳实践 | **全局** | "先写测试"、"始终处理错误" |
| 工具工作流偏好 | **全局** | "编辑前先 Grep"、"写之前先读" |
| Git 实践 | **全局** | "约定式提交"、"小而专注的提交" |
**如果不确定,默认选择 `scope: project`** — 先设为项目作用域,之后再提升,这比污染全局空间更安全。
## 置信度计算
基于观察频率的初始置信度:
* 1-2 次观察0.3(初步)
* 3-5 次观察0.5(中等)
* 6-10 次观察0.7(强)
* 11+ 次观察0.85(非常强)
置信度随时间调整:
* 每次确认性观察 +0.05
* 每次矛盾性观察 -0.1
* 每周无观察 -0.02(衰减)
## 本能提升(项目 → 全局)
当一个本能满足以下条件时,应从项目作用域提升到全局:
1. **相同模式**(通过 id 或类似触发器)存在于 **2 个以上不同的项目**中
2. 每个实例的置信度 **>= 0.8**
3. 其领域属于全局友好列表(安全、通用最佳实践、工作流)
提升操作由 `instinct-cli.py promote` 命令或 `/evolve` 分析处理。
## 重要准则
1. **保持保守**只为明确的模式3 次以上观察)创建本能
2. **保持具体**:狭窄的触发器优于宽泛的触发器
3. **追踪证据**:始终包含导致该本能的观察记录
4. **尊重隐私**:切勿包含实际的代码片段,只包含模式
5. **合并相似项**:如果新本能与现有本能相似,则更新而非重复创建
6. **默认项目作用域**:除非模式明显是通用的,否则设为项目作用域
7. **包含项目上下文**:对于项目作用域的本能,始终设置 `project_id``project_name`
## 示例分析会话
给定观察结果:
```jsonl
{"event":"tool_start","tool":"Grep","input":"pattern: useState","project_id":"a1b2c3","project_name":"my-app"}
{"event":"tool_complete","tool":"Grep","output":"Found in 3 files","project_id":"a1b2c3","project_name":"my-app"}
{"event":"tool_start","tool":"Read","input":"src/hooks/useAuth.ts","project_id":"a1b2c3","project_name":"my-app"}
{"event":"tool_complete","tool":"Read","output":"[file content]","project_id":"a1b2c3","project_name":"my-app"}
{"event":"tool_start","tool":"Edit","input":"src/hooks/useAuth.ts...","project_id":"a1b2c3","project_name":"my-app"}
```
分析:
* 检测到的工作流Grep → Read → Edit
* 频率:本次会话中观察到 5 次
* **作用域决策**:这是一种通用工作流模式(非项目特定)→ **全局**
* 创建本能:
* 触发器:"当修改代码时"
* 操作:"用 Grep 搜索,用 Read 确认,然后 Edit"
* 置信度0.6
* 领域:"workflow"
* 作用域:"global"
## 与 Skill Creator 集成
当本能从 Skill Creator仓库分析导入时它们具有
* `source: "repo-analysis"`
* `source_repo: "https://github.com/..."`
* `scope: "project"`(因为它们来自特定的仓库)
这些应被视为具有更高初始置信度0.7+)的团队/项目约定。