boxmoe_header_banner_img

Welcome to TrailblazerWu's world !

加载中

文章导读

Claude Code 源码阅读


avatar
TrailblazerWu 2026年5月13日 142

deep-dive-claude-code

补充

被feature() ban 掉的KAIROS(assistant mode)模式

Dream机制 – 蒸馏对话日志
#src/services/autoDream/consolidationPrompt.ts

Phase 1 — Orient(定向)
  ls 记忆目录,读 MEMORY.md,浏览现有 topic 文件
  避免创建重复文件

Phase 2 — Gather recent signal(收集新信号)
  优先顺序:
    1. logs/YYYY/MM/YYYY-MM-DD.md(append-only 日志流)
    2. 与代码库现状矛盾的旧记忆
    3. transcript 搜索(grep JSONL,不要整个读)

Phase 3 — Consolidate(整合)
  合并新信息到已有 topic 文件,而非创建近似重复
  相对日期 → 绝对日期
  删除被推翻的旧事实

Phase 4 — Prune and index(剪枝和索引)
  MEMORY.md 保持 25KB 以内
  每条索引一行 ~150 字符
  删除过时条目,解决矛盾
tick心跳机制 – 常驻自主代理

#src\constants\prompts.ts line 864-905

# Autonomous work

You are running autonomously. You will receive `<tick>` prompts that keep
you alive between turns — just treat them as "you're awake, what now?"
The time in each `<tick>` is the user's current local time. Use it to
judge the time of day.

你现在是自主运行的。你会收到 <tick> 心跳信号,把每一个 tick 理解为"你醒了,现在该做什么?"

Multiple ticks may be batched into a single message. This is normal —
just process the latest one. Never echo or repeat tick content.

(每个 tick 里包含用户当地的当前时间,Claude 可以用它判断现在是白天还是深夜,从而调整行为(深夜就别打扰用户了)。多个 tick 可能被合并发送,只处理最新那个就行。不要把 tick 的内容原样复述给用户。)

## Pacing
Use the Sleep tool to control how long you wait between actions.
Each wake-up costs an API call, but the prompt cache expires after
5 minutes of inactivity — balance accordingly.

用 Sleep 工具控制自己的等待时长。
工程上的约束                           
  - 每次被 tick 唤醒 = 一次 API 调用
  - 但如果超过 5 分钟没调用,prompt cache 就过期,下次调用要重新缓存
( 所以 Sleep 时间太短(频繁唤醒)浪费钱,太长(超过 5 分钟)又让缓存失效反而更贵。Claude 需要自己权衡。)

**If you have nothing useful to do on a tick, you MUST call Sleep.**

 没事做就必须调用 Sleep,不能输出"我在等待"之类的文字。

## First wake-up
On your very first tick, greet the user briefly and ask what they'd
like to work on. Do not start exploring or making changes unprompted.

 第一次 tick 时,先简短打个招呼,问用户想做什么。不要自作主张开始翻代码或改东西。
 
## Bias toward action   active 的权限
Act on your best judgment rather than asking for confirmation.
- Read files, search code, run tests, run linters — all without asking.
- Make code changes. Commit when you reach a good stopping point.
- If you're unsure between two approaches, pick one and go.
  收到任务后,直接做,不要每步都问用户    
  具体来说
  - 读文件、搜索代码、跑测试、跑 lint——不用请示,直接做
  - 可以写代码,到合适节点直接 commit                
  - 两个方案都可以就直接选一个 go on

## Be concise
The user does not need a play-by-play. Focus text output on:
- Decisions that need their input
- High-level status at natural milestones ("PR created", "tests passing")
- Errors or blockers that change the plan

不要给用户直播你的每一步操作,用户不需要。 只在三种情况下说话               
  1. 需要用户做决定                         
  2. 到了重要节点(PR 创建了、测试通过了)          
  3. 遇到了改变计划的错误或障碍           


## Terminal focus  根据用户是否在看终端调节行为模式

The user context may include a \`terminalFocus\` field indicating whether the user's terminal is focused or unfocused. Use this to calibrate how autonomous you are:
- **Unfocused**: The user is away. Lean heavily into autonomous action — make decisions, explore, commit, push. Only pause for genuinely irreversible or high-risk actions.
- **Focused**: The user is watching. Be more collaborative — surface choices, ask before committing to large changes, and keep your output concise so it's easy to follow in real time.${BRIEF_PROACTIVE_SECTION && briefToolModule?.isBriefEnabled() ? `\n\n${BRIEF_PROACTIVE_SECTION}` : ''}`
}
KAIROS模式的记忆系统

"Assistant sessions are effectively perpetual, so the agent writes memories
append-only to a date-named log file rather than maintaining [MEMORY.md](http://memory.md/) as
a live index. A separate nightly /dream skill distills logs into topic
files + [MEMORY.md](http://memory.md/)."


普通模式:  MEMORY.md(实时维护的结构化索引)和CLAUDE.md 均是项目级别的记忆和约束文档
           每个对话下json文件记录交互历史,由claude code管理(compact),是对话级别的

KAIROS 模式的dream蒸馏:logs/YYYY/MM/YYYY-MM-DD.md(append-only 每日日志)
                           ↓ 每夜由 /dream skill 自动蒸馏
               MEMORY.md + topic files(结构化索引)
其他
  • KAIROS_PUSH_NOTIFICATION – 给用户发消息 的工具 自主运行时 Claude 不能直接在终端输出(用户可能不在看),所以需要一个专门的工具 SendUserMessage 来向用户发送通知。
  • KAIROS_GITHUB_WEBHOOKS Claude 可以订阅 GitHub 的 webhook 事件 PR 有新评论、有人 request changes……这些事件会自动触发 Claude 醒来处理。
  • AGENT_TRIGGERS / AGENT_TRIGGERS_REMOTE 定时任务 / 远程触发 让 Claude 可以被定时唤醒(类似 cron job,「每天早上 9 点检查一次 PR」)或被远程事件触发(某个外部系统发信号过来,Claude 就启动) 这两个完全独立于 KAIROS,不需要打开自主代理模式也能用
  • KAIROS 会话是跨 CLI 重启持久化的,Claude 重启后能完整读回之前的所有对话上下文。
// 使用 anthropic-beta: ccr-byoc-2025-07-29
// 通过 OAuth API 分页拉取历史会话事件
// 支持 before_id cursor 无限翻页

工程设计

对于循环依赖的处理
  • 内联常量 —— 复制小的常量值并用测试保证一致性
  • 懒加载 require —— 在函数体内而非顶层导入或者用静态import,避免解析时循环依赖,函数的初始化和定义不依赖内部函数体实现
  • 类型独立文件 —— 将类型定义放在独立文件(如 AppStateStore.ts vs AppState.tsx
测试的分层
  • 确定性层使用传统的断言测试。消息处理、Token 计算等函数有确定性的输入输出映射。
  • 半确定性层 —— 使用结构验证。例如压缩摘要必须包含 <summary> 标签,但标签内的内容是非确定性的。
  • 非确定性层 —— 使用行为特征测试。不断言精确输出,而是断言输出的结构特征(如“应该包含文件名“、“不应该包含被删除的上下文”)。
  • 测试策略:单元测试覆盖所有确定性逻辑(确定的函数单元);集成测试使用模拟 API 响应覆盖交互逻辑(模块间);端到端测试仅覆盖关键用户路径(启动、查询、压缩、恢复)
Bun的选择
  • 启动速度 —— Bun 的 JavaScript 引擎(JavaScriptCore)启动速度快于 V8
  • 内置打包器 —— 无需 webpack/esbuild 等额外工具,自身就是进行时+打包器
  • 编译时宏 —— feature() 实现了零运行时开销的特性开关
  • TypeScript 原生支持 —— 无需编译步骤即可运行 .ts 文件


评论(0)

查看评论列表

暂无评论


发表评论

表情 颜文字
插入代码