claude code的阅读感想

异步生产者模式

使用的是生产者-消费者模式,这种模式并不是基于回调的event模式,主要采用该模式的原因是前端渲染、工具结果产出可能比较大,生产-消费模式不会丢信息,还可以把状态保留完整。(其实也没太看明白...)

5层上下文压缩机制:

  1. Tool Result Budget:这个有点像pre-hook,如果单条工具调用的结果超过阈值,是需要做对应处理的。(想象一下,可以保留最开始和最后的信息,中间用...替代,然后用[warning]字段提示超过限制,推荐使用grep、head等操作);
  2. Snip Compact: 最轻量级操作,移除x条旧消息;
  3. Micro Compact: 移除不再需要的tool result(这个不再需要就比较抽象,需要研究下);
  4. Context Collapse: 中间的一些消息进行压缩总结,而不是压缩所有上下文(怎么确定哪些中间消息,这个也不明确?)
  5. Auto Compact: 最重的操作,对所有对话压缩,有失败限制;

stop hook

Loop如果没有返回工具,在传统的观念里就是loop结束的标志,但是结束并不意味着任务完成,agent很可能是不知道该怎么做了,这个时候,需要把上下文传给stop hook,由它来判断任务是否完成,没完成的话,需要给新的提示、工具环境重新来一轮Loop

工具执行

无论是内置工具还是外部MCP,都是用的同一套工具执行流程。其中比较需要注意的是:

  1. 检查工具线程安全,是否可以并发
  2. preToolUse hook:make permission decisions, modify inputs, inject context, or stop execution entirely,加规则的好时机
  3. postToolUse hook:modify MCP output or block continuation;
  4. tool results budget:跟压缩那里一致;

sub-agent

是否要启动一个新的sub-agent,是由模型来判断的。sub-agent通常可以使得主agent更聚焦,避免上下文干扰、工具太多的干扰。

sub-agent可以根据自己的需要,派生新的sub-agent,本质上,这个就是一种状结构。

启发:每个sub-agent的完整轨迹都可以记录下来,编号之后持久化为文件,sub-agent的结果传给母agent,得到编号和结果。

Swarm System的Team Context这种形式,和auto research中的多个sub-agent共享一个白板概念挺像的,主agent会把白板上合适的merge交给新的sub-agent进行消融实验。

memory

md文件,yaml格式,4类:user, project, feedback, reference

---
name: {{memory name}}
description: {{one-line description -- used to decide relevance}}
type: {{user, feedback, project, reference}}
---

两步添加记忆:

  1. 写一个md文件
---
name: Testing Policy
description: Integration tests must hit real DB, not mocks
type: feedback
---

Don't mock the database in integration tests.

**Why:** We got burned last quarter when mocked tests passed but production
queries hit edge cases the mocks didn't cover.

**How to apply:** Any test file under `__tests__/` that touches database
operations should use the real PGlite instance from test-utils.
  1. 更新、添加一个索引文件
- [Testing Policy](feedback_testing.md) -- integration tests must hit real DB

When the model learns new information that modifies an existing memory, it uses FileEditTool to update the existing file rather than creating a duplicate.

索引技巧,跟skill渐进式披露比较像,会单独发给一个模型,做保守判断,已经在上下文中的记忆不会被加载。

记忆文件还会设置更新时间,超过设定阈值的记忆会添加warning,提醒模型做好re-check(Staleness)

Memory.md本质是索引文件,有两个强约束:The index has two hard caps: 200 lines and 25,000 bytes. 200行的限制,使得模型对一些相似的记忆需要做merge并重新整理,不然肯定会超。

KAIROS模式:即可生成新的memory和索引会带来明显的中断,新模式可以直接将信息结构化存储在日志系统里,然后流式更新,无感更新;

Skill & Hook

Skill扩展能力、Hook控制流(约束)

Skill的几个字段:

YAML Field  Purpose
name    User-facing display name
description Shown in autocomplete and system prompt
when_to_use Detailed usage scenarios for model discovery
allowed-tools   Which tools the skill can use
disable-model-invocation    Block autonomous model use
context 'fork' to run as sub-agent
hooks   Lifecycle hooks registered on invocation
paths   Glob patterns for conditional activation
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容