事件层
- 事件源Agent 循环、工具、dispatcher、任务运行器、auth、context、channels、memory
emit- EventBus每进程一个;不导入 WebUI 的任何东西
subscribe- 消费者WebUI(ws.frame)、插件、配置钩子、events.jsonl
分发
| 方式 | 调用 | 效果 | 订阅者抛错 |
|---|---|---|---|
| notify | emit / subscribe(handler, types=…) | 只观察;发送方从不等结果 | 记录;其余订阅者照常运行 |
| gate | emit_gate(event, timeout_s=None) / subscribe_gate | 按注册顺序在发送方线程全部运行。任一理由即 allowed=False,理由累积。 | 记录,按放行处理 |
| transform | emit_transform(event, value) / subscribe_transform | 每个接收上一个的值并返回下一个;None 保持原值 | 跳过,输入继续传下去 |
- gate 是
tool.before和turn.stop;唯一的 transform 是tool.result,shell 钩子不能订阅。其余都是 notify:事件参考。 - gate 要快:不调 LLM,不做慢 I/O。
- 同一线程内对同一类型嵌套
emit_gate会立即放行并警告。 timeout_s是软总预算;超过后其余 gate 被跳过并警告。tool.before在permission_mode审批包装之外,bypass和子 Agent 都逃不开。- notify 处理函数也在发送方线程运行;异步函数在有运行中事件循环时被调度到其上。
事件
@dataclass(frozen=True)
class Event:
id: str
ts: float
type: str
origin: str # user / agent / tool / system / proactive
payload: dict
metadata: dict # {"session": …, "turn": …, "lane": …}
make_event 填充 id、ts,并从 store 的 ContextVar 取 session 和 turn;显式传入的 metadata 键优先。session 和 turn 放在 metadata,因为它们是外部关联,对 auth、channels 等一半事件没有意义。框架没有 Turn 对象。
准入
只有真实消费者订阅时,一个类型才进入 EVENTS。新增类型只改发送点;新增载荷字段是安全的;改动已有字段可能破坏订阅者。
发送未注册类型时每种类型警告一次,绝不抛错。test_every_emitted_type_is_registered 覆盖所有 emit_safe 类型。
| 事件源类别 | 接入方式 |
|---|---|
| A:Agent 活动 | turn、工具、任务路径上的发送点;metadata 通常带 session 和 turn |
| B:系统状态,可能没有 Agent 在运行 | auth 保留自己的 subscribe/_emit,经 events/bridges.py 单向桥接。context、channels、memory 和 WebUI watcher 用 emit_safe 直接接入。 |
否决
| 订阅者 | 协议 |
|---|---|
| Python gate | None 放行;字符串拒绝。tool.before:理由成为模型收到的错误工具结果。turn.stop:理由成为续跑提示,前缀 [hook]。 |
| Shell,事件以 JSON 从 stdin 传入 | 退出码 0 放行。2 拒绝,stderr 是理由。其他退出码或超时(默认 60 秒)放行并警告。 |
{"hooks": {"turn.stop": [{"command": "python check_done.py", "timeout": 30}]}}
config.json 顶层的 hooks 键。install_config_hooks() 把 gate 类注册为同步 shell gate,notify 类注册为后台运行器(守护线程,忽略退出码)。修改在 worker 下次重启时生效。
命令在 macOS 和 Linux 上由 /bin/sh 运行,在 Windows 上由 bash 运行,没有 bash 时改用 PowerShell 并提示一次。条目可以写 "shell": "powershell",或给出 args,不经 shell 直接运行 command(hook 用哪个 shell)。事件以 UTF-8 JSON 从标准输入送入。
turn.stop 续跑循环
- 每个完成的顶层对话轮次后,
continue_stop_hook_turns询问 gate。失败或取消的轮次不询问。 - 被拒绝时再启动一轮普通轮次(
dataclasses.replace、source="hook_continue"、INHERIT_PARENT),然后再问 gate。 - 没有次数上限:第一次之后的询问里
stop_hook_active都为真,钩子据此放行结束,与 Claude Code、Codex 相同。 - Goal 轮次不走这条路径:
goal()自带完成判定,/goal clear可以停止。 - Head 移动仍由正常的
TurnWriter路径负责。
事件日志
| 项目 | 取值 |
|---|---|
| 会话目录存在 | ~/.openprogram/projects/<project-id>/chats/<sid>/events.jsonl |
| 否则 | ~/.openprogram/logs/events.jsonl |
| 轮转 | 超过 5 MB 改名为 .1,覆盖旧的 |
| gate 裁决 | 写在事件同一行:gate: {allowed, reasons, duration_ms, subscribers} |
| 写入者 | 只有单例;create_event_bus() 创建的总线不写 |
gate 事件与日志行
gate 事件经 emit 给类型观察者时推迟落盘;同一 Event 随后必须进入 emit_gate,写下带裁决的唯一一行。只调 emit 不写日志。只调 emit_gate 是完整分发,但不通知观察者,turn.stop 就是这样。
接线
| 接口 | 实现 |
|---|---|
register_tool_gate、decide_tool_gate、ToolGateDenied | tool.before gate 的薄封装(tool_gate.py) |
插件 hooks | extensions/plugins/hooks.py 读取 manifest,通过插件的 ProgramContext 挂载每条命令,禁用即退订。接受 config.json 形式或 Claude Code 的 hooks/hooks.json。 |
PreToolUse、PostToolUse、UserPromptSubmit、Stop、SessionStart | tool.before、tool.after、chat.before_send、turn.stop、session.start |
/goal 变化 | _emit_goal_update 发送 goal.update |
| worker 启动 | install_event_bridges()、install_config_hooks() |
各部分的位置
| 部分 | 位置,在 openprogram/runtime/events/ 下 |
|---|---|
| 注册表 | registry.py |
| 事件、分发、订阅、进程单例 | bus.py(get_event_bus(),与 get_store() 一样的双重检查锁) |
| shell 协议 | shell_hooks.py |
| 日志 | event_log.py |
| 桥接 | bridges.py |
相关组件都是同一 worker 进程内的守护线程,因此没有跨进程桥。从 openprogram.runtime.events 导入。
实现状态
以上全部已实现。已注册但没有发送点:sessions.listed、branches.listed。要消费总线的主动引擎未在 worker 启动时安装:主动层。