Skip to content

Commit f04ad4c

Browse files
author
wangzhuo
committed
fix(agents): remove zh garbling and normalize mixed-language docs
1 parent 05fa0a6 commit f04ad4c

21 files changed

Lines changed: 250 additions & 288 deletions

agents/zh/s01_agent_loop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class LoopState:
6868
def run_bash(command: str) -> str:
6969
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
7070
if any(item in command for item in dangerous):
71-
return "Error: ???????"
71+
return "Error: 危险命令已拦截"
7272
try:
7373
result = subprocess.run(
7474
command,

agents/zh/s02_tool_use.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def safe_path(p: str) -> Path:
4343
def run_bash(command: str) -> str:
4444
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
4545
if any(d in command for d in dangerous):
46-
return "Error: ???????"
46+
return "Error: 危险命令已拦截"
4747
try:
4848
r = subprocess.run(command, shell=True, cwd=WORKDIR,
4949
capture_output=True, text=True, timeout=120)

agents/zh/s03_todo_write.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def safe_path(path_str: str) -> Path:
124124
def run_bash(command: str) -> str:
125125
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
126126
if any(item in command for item in dangerous):
127-
return "Error: ???????"
127+
return "Error: 危险命令已拦截"
128128
try:
129129
result = subprocess.run(
130130
command,

agents/zh/s04_subagent.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@
66
通过 fresh `messages=[]` 启动 child agent(子智能体)。
77
子智能体在独立上下文中工作、共享同一文件系统,最后只把摘要返回给父智能体。
88
9-
Parent agent Subagent
9+
父智能体(Parent agent 子智能体(Subagent
1010
+------------------+ +------------------+
11-
| messages=[...] | | messages=[] | <-- fresh
12-
| | dispatch | |
13-
| tool: task | ---------->| while tool_use: |
14-
| prompt="..." | | call tools |
15-
| description="" | | append results |
16-
| | summary | |
17-
| result = "..." | <--------- | return last text |
11+
| messages=[...] | | messages=[] | <-- fresh(新上下文)
12+
| | dispatch(派发)| |
13+
| 工具: task | ---------->| while tool_use(工具调用): |
14+
| prompt="..." | | call tools(调用工具) |
15+
| description="" | | append results(追加结果) |
16+
| | summary(汇总) | |
17+
| result = "..." | <--------- | return last text(返回摘要) |
1818
+------------------+ +------------------+
1919
|
20-
父上下文保持干净。
21-
子上下文在任务完成后丢弃。
20+
父上下文保持干净,子上下文任务结束后丢弃。
2221
2322
关键洞察:
2423
"fresh messages=[] 就是上下文隔离,父上下文不会被污染。"
@@ -28,20 +27,17 @@
2827
但拥有新的消息数组和隔离的工具上下文,这与本教学实现一致。
2928
3029
与真实 Claude Code 的对比:
31-
+-------------------+------------------+----------------------------------+
32-
| Aspect | This demo | Real Claude Code |
33-
+-------------------+------------------+----------------------------------+
34-
| Backend | in-process only | 5 backends: in-process, tmux, |
35-
| | | iTerm2, fork, remote |
36-
| Context isolation | fresh messages=[]| createSubagentContext() isolates |
37-
| | | ~20 fields (tools, permissions, |
38-
| | | cwd, env, hooks, etc.) |
39-
| Tool filtering | manually curated | resolveAgentTools() filters from |
40-
| | | parent pool; allowedTools |
41-
| | | replaces all allow rules |
42-
| Agent definition | hardcoded system | .claude/agents/*.md with YAML |
43-
| | prompt | frontmatter (AgentTemplate) |
44-
+-------------------+------------------+----------------------------------+
30+
+-------------------+----------------------+---------------------------------------------+
31+
| 维度(Aspect) | 教学实现(This demo) | 真实 Claude Code(Real Claude Code) |
32+
+-------------------+----------------------+---------------------------------------------+
33+
| 后端(Backend) | 仅 in-process | 5 种后端:in-process、tmux、iTerm2、fork、remote |
34+
| 上下文隔离 | fresh messages=[] | createSubagentContext() 隔离约 20 个字段(tools、 |
35+
| (Context isolation)| | permissions、cwd、env、hooks 等) |
36+
| 工具过滤 | 手工挑选 | resolveAgentTools() 从父工具池过滤;allowedTools |
37+
| (Tool filtering) | | 可替代所有 allow 规则 |
38+
| 智能体定义 | 代码内硬编码 prompt | `.claude/agents/*.md` + YAML frontmatter |
39+
| (Agent definition)| | (模板 AgentTemplate) |
40+
+-------------------+----------------------+---------------------------------------------+
4541
"""
4642

4743
import os
@@ -78,8 +74,9 @@ class AgentTemplate:
7874
7975
真实 Claude Code 会从 `.claude/agents/*.md` 读取 agent 定义。
8076
frontmatter 字段包括:name、tools、disallowedTools、skills、hooks、
81-
model、effort、permissionMode、maxTurns、memory、isolation、color、
82-
background、initialPrompt、mcpServers。
77+
model(模型)、effort(推理力度)、permissionMode(权限模式)、maxTurns(轮次上限)、
78+
memory(记忆)、isolation(隔离)、color(颜色)、background(后台)、
79+
initialPrompt(初始提示)、mcpServers(MCP 服务配置)。
8380
来源通常有三类:built-in、custom(`.claude/agents/`)、plugin-provided。
8481
"""
8582
def __init__(self, path):
@@ -113,7 +110,7 @@ def safe_path(p: str) -> Path:
113110
def run_bash(command: str) -> str:
114111
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
115112
if any(d in command for d in dangerous):
116-
return "Error: ???????"
113+
return "Error: 危险命令已拦截"
117114
try:
118115
r = subprocess.run(command, shell=True, cwd=WORKDIR,
119116
capture_output=True, text=True, timeout=120)

agents/zh/s05_skill_loading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def safe_path(path_str: str) -> Path:
117117
def run_bash(command: str) -> str:
118118
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
119119
if any(item in command for item in dangerous):
120-
return "Error: ???????"
120+
return "Error: 危险命令已拦截"
121121
try:
122122
result = subprocess.run(
123123
command,

agents/zh/s06_context_compact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def compact_history(messages: list, state: CompactState, focus: str | None = Non
173173
def run_bash(command: str, tool_use_id: str) -> str:
174174
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
175175
if any(item in command for item in dangerous):
176-
return "Error: ???????"
176+
return "Error: 危险命令已拦截"
177177
try:
178178
result = subprocess.run(
179179
command,

agents/zh/s07_permission_system.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
每一次工具调用在执行前都必须经过权限管线(permission pipeline)。
77
88
教学版管线:
9-
1. deny rules
10-
2. mode check
11-
3. allow rules
12-
4. ask user
9+
1. deny rules(拒绝规则)
10+
2. mode check(模式检查)
11+
3. allow rules(放行规则)
12+
4. ask user(询问用户)
1313
1414
本版本先聚焦三种模式:
15-
- default
16-
- plan
17-
- auto
15+
- default(默认)
16+
- plan(规划)
17+
- auto(自动)
1818
1919
这已足够搭建可用且可理解的权限系统,不会在起步阶段被复杂策略分支淹没。
2020

agents/zh/s08_hook_system.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
它允许在不重写循环的前提下增量添加行为。
88
99
教学版包含:
10-
- SessionStart
11-
- PreToolUse
12-
- PostToolUse
10+
- SessionStart(会话开始)
11+
- PreToolUse(工具调用前)
12+
- PostToolUse(工具调用后)
1313
1414
教学版退出码约定:
15-
- 0 -> continue
16-
- 1 -> block
17-
- 2 -> inject a message
15+
- 0 -> continue(继续)
16+
- 1 -> block(阻断)
17+
- 2 -> inject a message(注入消息)
1818
1919
这里刻意简化于生产系统,先把扩展模式讲清楚,再进入事件级边界细节。
2020
@@ -183,7 +183,7 @@ def safe_path(p: str) -> Path:
183183
def run_bash(command: str) -> str:
184184
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
185185
if any(d in command for d in dangerous):
186-
return "Error: ???????"
186+
return "Error: 危险命令已拦截"
187187
try:
188188
r = subprocess.run(command, shell=True, cwd=WORKDIR,
189189
capture_output=True, text=True, timeout=120)

agents/zh/s09_memory_system.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
有些信息应该跨会话保留,但并非所有内容都适合进入 memory(记忆)。
88
99
建议写入 memory 的内容:
10-
- user preferences
11-
- repeated user feedback
12-
- project facts that are NOT obvious from the current code
13-
- pointers to external resources
10+
- user preferences(用户偏好)
11+
- repeated user feedback(反复出现的用户反馈)
12+
- project facts that are NOT obvious from the current code(无法直接从当前代码显见的项目事实)
13+
- pointers to external resources(外部资源指针)
1414
1515
不应写入 memory 的内容:
16-
- code structure that can be re-read from the repo
17-
- temporary task state
18-
- secrets
16+
- code structure that can be re-read from the repo(可从仓库重新读取的代码结构)
17+
- temporary task state(临时任务状态)
18+
- secrets(敏感密钥与口令)
1919
2020
存储结构:
2121
.memory/
@@ -351,7 +351,7 @@ def safe_path(p: str) -> Path:
351351
def run_bash(command: str) -> str:
352352
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
353353
if any(d in command for d in dangerous):
354-
return "Error: ???????"
354+
return "Error: 危险命令已拦截"
355355
try:
356356
r = subprocess.run(command, shell=True, cwd=WORKDIR,
357357
capture_output=True, text=True, timeout=120)

agents/zh/s10_system_prompt.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
system prompt 应由清晰分段组装,而不是写成一整块硬编码大文本。
88
99
教学版构建管线:
10-
1. core instructions
11-
2. tool listing
12-
3. skill metadata
13-
4. memory section
14-
5. CLAUDE.md chain
15-
6. dynamic context
10+
1. core instructions(核心指令)
11+
2. tool listing(工具清单)
12+
3. skill metadata(技能元数据)
13+
4. memory section(记忆区段)
14+
5. CLAUDE.md chain(CLAUDE 指令链)
15+
6. dynamic context(动态上下文)
1616
1717
构建器会把稳定信息与高频变化信息分离,
1818
并用 `DYNAMIC_BOUNDARY` 标记显式展示边界。
@@ -153,15 +153,15 @@ def _build_claude_md(self) -> str:
153153
# 项目根目录
154154
project_claude = self.workdir / "CLAUDE.md"
155155
if project_claude.exists():
156-
sources.append(("project root (CLAUDE.md)", project_claude.read_text()))
156+
sources.append(("项目根目录(project root / CLAUDE.md", project_claude.read_text()))
157157

158158
# 子目录:真实 CC 会从 cwd 向上遍历到项目根;
159159
# 教学版简化为仅在 cwd != workdir 时检查一次。
160160
cwd = Path.cwd()
161161
if cwd != self.workdir:
162162
subdir_claude = cwd / "CLAUDE.md"
163163
if subdir_claude.exists():
164-
sources.append((f"subdir ({cwd.name}/CLAUDE.md)", subdir_claude.read_text()))
164+
sources.append((f"子目录(subdir / {cwd.name}/CLAUDE.md", subdir_claude.read_text()))
165165

166166
if not sources:
167167
return ""
@@ -176,8 +176,8 @@ def _build_dynamic_context(self) -> str:
176176
lines = [
177177
f"当前日期: {datetime.date.today().isoformat()}",
178178
f"工作目录: {self.workdir}",
179-
f"Model: {MODEL}",
180-
f"Platform: {platform.system()}",
179+
f"模型(Model: {MODEL}",
180+
f"平台(Platform: {platform.system()}",
181181
]
182182
return "# 动态上下文\n" + "\n".join(lines)
183183

@@ -248,7 +248,7 @@ def safe_path(p: str) -> Path:
248248
def run_bash(command: str) -> str:
249249
dangerous = ["rm -rf /", "sudo", "shutdown", "reboot", "> /dev/"]
250250
if any(d in command for d in dangerous):
251-
return "Error: ???????"
251+
return "Error: 危险命令已拦截"
252252
try:
253253
r = subprocess.run(command, shell=True, cwd=WORKDIR,
254254
capture_output=True, text=True, timeout=120)

0 commit comments

Comments
 (0)