hooks-guide.md

July 26, 2026 · View on GitHub

Maestro Hook 系统为 Claude Code、Codex 和 Agy (Antigravity) 提供自动化的上下文管理、规范注入和工作流感知能力。Hook 以子进程方式运行,通过 stdin/stdout JSON 协议与宿主环境交互。

目录


概览

架构

注册方式运行方式
Claude Code Hookssettings.json子进程 maestro hooks run <name>
Codex Hookshooks.json子进程 maestro hooks run <name>
Agy (Antigravity) Hooks~/.gemini/config/hooks.json子进程 maestro hooks run <name>
Coordinator HooksWorkflowHookRegistry进程内插件

协议

退出码含义
0允许操作继续
2阻止操作
事件类型可返回
PreToolUseupdatedInput(重写工具参数)/ additionalContext
PostToolUseadditionalContext
Stopdecision: "block"(无 additionalContext

工作空间感知

标记 requiresWorkspace 的 Hook 仅在检测到有效 Maestro 工作空间时激活(向上遍历查找含 version + phases_summary 指纹的 .workflow/state.json),否则 exit(0) 静默退出,零开销。


Hook 清单

Hook事件类型Matcher级别Workspace用途
spec-injectorPreToolUseAgentminimal必需按 agent 类型自动注入项目规范
delegate-monitorPostToolUseBash|Agentstandard监控异步委托任务完成状态
team-monitorStopstandard团队协作心跳记录
telemetryStopstandard执行遥测数据采集(每轮一次)
session-contextNotificationstandard会话启动时注入工作流状态
skill-contextUserPromptSubmitstandard必需Skill 调用时注入工作流状态和产物树
coordinator-trackerStopstandard必需协调器链执行进度追踪
preflight-guardPreToolUseBash|Write|Edit|Agentstandard命令执行前预检守卫
spec-validatorPreToolUseWritestandard完整规范写入验证
keyword-spec-injectorUserPromptSubmitstandard单次注入 keyword/spec/wiki/domain/KG 上下文
kg-syncUserPromptSubmitstandard必需知识图谱增量同步(CooldownGuard 30s 去抖)
kg-auto-initUserPromptSubmitstandard必需知识图谱自动初始化(CooldownGuard 5min 去抖)
workflow-guardPreToolUseBash|Write|Editfull必需保护关键文件和操作
prompt-guardUserPromptSubmitfull用户 prompt 安全检查

性能优化:Stop 事件 Hook 每轮仅触发 1 次;delegate-monitor 通过 Bash|Agent matcher 过滤。相比无 matcher 的 PostToolUse,每轮子进程 spawn 减少约 72%。

Codex Hook 清单

Hook事件类型Matcher级别Workspace用途
session-contextSessionStartstartup|resumeminimal必需会话启动注入工作流状态
spec-injectorSessionStartstartupstandard必需会话启动注入规范
skill-contextUserPromptSubmitstandard必需Skill 调用注入上下文
keyword-spec-injectorUserPromptSubmitstandard必需单次注入 keyword/spec/wiki/domain/KG 上下文
kg-syncUserPromptSubmitstandard必需知识图谱增量同步
kg-auto-initSessionStartstartupstandard必需知识图谱自动初始化
delegate-monitorPostToolUseBashstandard监控异步委托
coordinator-trackerStopstandard必需协调器进度追踪
team-monitorStopstandard团队心跳记录
telemetryStopstandard遥测采集
workflow-guardPreToolUseBashfull必需保护文件(仅 Bash)
prompt-guardUserPromptSubmitfull用户 prompt 安全检查

与 Claude Code 差异:Codex spec-injector 用 SessionStart(无法拦截 Agent);workflow-guard 仅防护 Bash;并发执行;正则 matcher。

Agy (Antigravity) Hook 清单(v0.4.19+)

Agy 通过 hooks.json 注册 Hook(前缀 maestro-),使用 PreInvocation / PreToolUse / PostToolUse / Stop 事件类型:

Hook事件类型Matcher级别Workspace用途
spec-injectorPreToolUseinvoke_subagentminimal必需按 agent 类型自动注入项目规范
session-contextPreInvocationstandard必需会话启动注入工作流状态
skill-contextPreInvocationstandard必需Skill 调用注入上下文
keyword-spec-injectorPreInvocationstandard必需单次注入 keyword/spec/wiki/domain/KG 上下文
kg-syncPreInvocationstandard必需知识图谱增量同步
kg-auto-initPreInvocationstandard必需知识图谱自动初始化
delegate-monitorPostToolUserun_command|invoke_subagentstandard监控异步委托
team-monitorStopstandard团队心跳记录
telemetryStopstandard遥测采集
coordinator-trackerStopstandard必需协调器进度追踪
preflight-guardPreToolUserun_command|write_to_file|replace_file_content|multi_replace_file_content|invoke_subagentstandard必需命令执行前预检守卫
spec-validatorPreToolUsewrite_to_filestandard必需完整规范写入验证
workflow-guardPreToolUserun_command|write_to_file|replace_file_content|multi_replace_file_contentfull必需保护文件和操作
prompt-guardPreInvocationfull用户 prompt 安全检查

安装路径

  • 全局 → ~/.gemini/config/hooks.json
  • 项目级 → <project>/.agents/hooks.json

与 Claude/Codex 差异:Agy 使用 PreInvocation(对应 Claude 的 UserPromptSubmit)做上下文注入;matcher 使用 Agy 工具名(如 invoke_subagentrun_commandwrite_to_file);所有 Hook 以 maestro- 前缀注册到 hooks.json 顶层。


安装级别

Hook 按累积级别安装,高级别包含所有低级别:

级别包含内容适用场景
none无 Hook完全手动控制
minimalStatusline + spec-injector日常开发
standard+ delegate-monitor + kg-sync + kg-auto-init + keyword/spec/wiki/domain/KG prompt context + team/telemetry/coordinator(Stop) + session-context + skill-context + preflight/spec guards团队协作
full+ workflow-guard严格工作流

安装命令

# Claude Code
maestro hooks install --level <minimal|standard|full>
maestro hooks install --level standard --project       # 项目级

# Codex(需 ~/.codex/config.toml 启用 codex_hooks)
maestro hooks install --target codex --level <level>
maestro hooks install --target codex --level standard --project

# Agy (Antigravity)
maestro hooks install --target agy --level <level>

# 查看
maestro hooks status    # 安装状态
maestro hooks list      # 可用 Hook 列表

核心 Hook 详解

spec-injector — 规范自动注入

事件: PreToolUse (Agent) | 级别: minimal

根据 subagent_type 自动注入对应规范,使用 updatedInput 重写 prompt。

Agent 类型注入分类
code-developer / workflow-executor / universal-executorcoding
tdd-developer / test-fix-agentcoding, test
cli-lite-planning-agent / action-planning-agent / workflow-plannerarch
workflow-reviewerreview
debug-explore-agent / workflow-debuggerdebug

context-budget — 上下文预算

spec-injector 内部模块,非独立 Hook。

剩余上下文动作策略
> 50%full注入全部内容
35-50%reduced保留标题 + 每节首段(max 4096 字符)
25-35%minimal仅标题列表 + learnings
< 25%skip不注入

session-context — 会话上下文

事件: Notification | 级别: standard

会话启动时注入轻量概览:工作流状态 + 规范文件列表 + Git 分支/最近提交。不注入完整规范(由 spec-injector 按需注入)。

delegate-monitor — 委托监控

事件: PostToolUse (Bash|Agent) | 级别: standard

读取 /tmp/maestro-notify-{session_id}.jsonl 注入异步委托完成/失败状态。Bash|Agent matcher 避免只读操作触发。

team-monitor — 团队监控

事件: Stop | 级别: standard

每轮向 .workflow/collab/activity.jsonl 写入心跳,Stop 事件每轮仅 1 次。

skill-context — Skill 感知上下文

事件: UserPromptSubmit | 级别: standard

匹配 Skill 调用时注入工作流状态 + 阶段产物树 + 前序成果(additionalContext,不重写 prompt)。支持模式:首要步骤 skill 调用(analyzeplanexecutereviewtestauto-test、milestone 等,由编排器派发)以及编排入口 /maestro/maestro-next/maestro-ralph

协调器 Skill 额外注入 coordinator-tracker bridge 的 next-step 提示:Chain: full-lifecycle [3/6] | Status: paused | Next: review 2 | Resume: /maestro -c

coordinator-tracker — 协调器进度追踪

事件: Stop | 级别: standard | Workspace: 必需

每轮结束时更新 bridge 文件供 Statusline 和 skill-context 消费。纯 I/O 操作,不产生 additionalContext

Bridge 文件示例
{
  "session_id": "cc-session-abc123",
  "maestro_session_id": "maestro-20260412-103500",
  "chain_name": "full-lifecycle",
  "intent": "implement OAuth2 authentication",
  "phase": 2,
  "steps_total": 6,
  "steps_completed": 3,
  "current_step": { "index": 3, "skill": "review", "args": "2" },
  "next_step": { "index": 4, "skill": "test", "args": "2" },
  "status": "paused",
  "updated_at": 1744668285953
}

Statuslineclaude-sonnet-4-6 | P2 | [3/6]review(暂停态 [P]review

workflow-guard — 工作流守卫

事件: PreToolUse (Bash|Write|Edit) | 级别: full

检查受保护文件和工作流阶段约束。退出码 2 阻止操作。

CooldownGuard — 跨进程抖动抑制

src/utils/cooldown-guard.ts — 非独立 Hook,供 kg-sync / kg-auto-init 内部使用。

通过 tmpdir bridge 文件实现跨子进程调用的时序节流。每次执行后写入 JSON bridge 文件,冷却期内的后续调用被跳过(shouldRun() 返回 false)。

预设实例冷却时间用途
kgSyncGuard30 秒kg-sync 增量同步去抖
kgInitGuard5 分钟kg-auto-init 初始化去抖

API

import { CooldownGuard, kgSyncGuard, kgInitGuard } from '../utils/cooldown-guard.js';

// 自定义 guard
const guard = new CooldownGuard({ prefix: 'my-guard-', cooldownMs: 60_000 });

// 使用
if (guard.shouldRun(sessionId)) {
  // 执行操作
  guard.markDone(sessionId, { extra: 'data' });
}

// 查询距上次执行的时间
const elapsed = guard.timeSinceLastMs(sessionId); // number | null

Bridge 文件{tmpdir}/{prefix}{sessionId}.json,格式为 { last_trigger: number, session_id?: string, extra?: Record<string, unknown> }

Coordinator 插件

SpecInjectionPlugin(进程内)通过关键词推断规范分类:

关键词推断分类
review, audit, check qualityreview
test, spec, coverage, asserttest
debug, diagnose, fix, error, bugdebug
plan, design, architect, decompose, explore, analyzearch
其他(默认)coding

配置

Hook 开关

maestro hooks toggle <name> <on|off> — 单独开关 Hook。

自定义 Agent-Spec 映射

配置示例
{
  "specInjection": {
    "mapping": {
      "my-custom-agent": {
        "categories": ["coding", "test"],
        "extras": []
      }
    },
    "maxContentLength": 8192
  }
}
字段说明
mapping覆盖/扩展 agent -> category 映射
always始终注入的额外文件路径列表
maxContentLength截断前最大字符数

自定义映射与默认映射合并,不替换。

项目规范文件

规范文件示例
---
title: Coding Conventions
category: coding
---

# Coding Conventions

- Use camelCase for variables
- Use PascalCase for classes

可用分类: coding, arch, quality, review, test, debug, learning

初始化:maestro spec init

状态转换记录

转换自动写入 state.jsontransition_history[]。API:

import { buildTransitionEntry, appendTransition } from '../tools/transition-recorder.js';
appendTransition('.workflow/state.json', buildTransitionEntry({ type: 'phase', fromPhase: 1, toPhase: 2, milestone: 'MVP' }));

命令参考

# 安装 / 卸载
maestro hooks install --level <level>                          # 安装
maestro hooks install --level standard --project               # 项目级
maestro hooks uninstall --global                               # 卸载全局
maestro hooks uninstall --project                              # 卸载项目级

# Codex
maestro hooks install --target codex --level <level>
maestro hooks uninstall --target codex

# 查看
maestro hooks status          # 安装状态
maestro hooks list            # 可用 Hook
maestro hooks config          # 当前配置

# 开关
maestro hooks toggle <name> <on|off>

# 手动运行(调试)
echo '{"tool_name":"Agent","tool_input":{"subagent_type":"code-developer","prompt":"test"}}' \
  | maestro hooks run spec-injector