my-harness-hook-ci
用途
本技能为 moai-adk-go 项目提供钩子(Hook)与 CI/CD 的领域知识,作为 expert-devops 技能的补充,给出项目专属的实现模式。
主要能力
钩子系统架构
钩子事件的调用链路如下:
Claude Code hook event
-> .claude/hooks/moai/handle-{event}.sh (shell wrapper)
-> moai hook {event} (Go binary handler)
-> internal/hook/{handler}.go (Go handler logic)
钩子事件(共 27 种)
- 会话生命周期:SessionStart、SessionEnd、Compact、PostCompact、InstructionsLoaded
- 工具生命周期:PreTool、PostTool、PostToolFailure
- 智能体生命周期:SubagentStart、SubagentStop、Stop、StopFailure
- 状态变更:ConfigChange、CwdChanged、FileChanged
- 权限:PermissionRequest、PermissionDenied
- 交互:Notification、Elicitation、ElicitationResult、UserPromptSubmit
- 团队:TeammateIdle、TaskCreated、TaskCompleted、AgentHook
- Worktree:WorktreeCreate、WorktreeRemove
CI 流水线概览
PR/Push
-> ci.yml (Lint + Test ubuntu/macos/windows + Build 5 platforms + CodeQL)
-> release-drafter.yml (Auto-draft changelog)
-> spec-lint.yml (SPEC document validation)
-> docs-i18n-check.yml (4-locale doc sync)
Tag push
-> release.yml (GoReleaser: 5-platform binaries + GitHub Release)
使用方式
常用命令
# 手动运行钩子处理程序
echo '{}' | moai hook session-start
# 测试特定钩子
echo '{"toolName":"Write"}' | moai hook post-tool
# 验证所有钩子脚本存在
ls .claude/hooks/moai/handle-*.sh | wc -l # Should be 27
# 本地 CI 检查(近似)
go vet ./... && golangci-lint run && go test ./...
钩子包装脚本模式
每个钩子遵循薄包装(thin-wrapper)模式:
#!/bin/bash
# .claude/hooks/moai/handle-{event}.sh
# Read stdin JSON from Claude Code
INPUT=$(cat)
# Call moai binary with hook subcommand
moai hook {event} <<< "$INPUT"
规则:
- 在 settings.json 的钩子命令中始终为 $CLAUDE_PROJECT_DIR 加引号
- 使用完整路径:"$CLAUDE_PROJECT_DIR/.claude/hooks/moai/handle-{event}.sh"
- 设置合适的超时(默认 5 秒,后处理最大 10 秒)
智能体钩子配置
智能体作用域钩子在智能体 YAML frontmatter 中定义,示例配置包括 PreToolUse、PostToolUse、SubagentStop 三类事件,使用 matcher 匹配 Write|Edit|MultiEdit,并设定 timeout(PreToolUse 为 5,PostToolUse 与 SubagentStop 为 10)。
GitHub Actions 工作流模式
位于 .github/workflows/ 的关键工作流:
| 工作流 | 触发条件 | 关键特性 |
|---|---|---|
| ci.yml | push/PR 到 main | 矩阵:ubuntu/macos/windows,Go 1.24,lint + test + build |
| release.yml | 推送 tag v* | GoReleaser:linux/darwin/windows,amd64/arm64 |
| release-drafter.yml | PR 合并到 main | 自动标签 + 草拟变更日志 |
| auto-merge.yml | Dependabot CI 通过 | 自动 squash 合并 |
| codeql.yml | push/PR | Go 安全分析 |
| spec-lint.yml | PR | SPEC 文档 frontmatter 校验 |
| spec-status-auto-sync.yml | 定时 | SPEC 状态漂移检测 |
| docs-i18n-check.yml | 涉及 docs-site 的 PR | 4 语言同步校验 |
发布流程
# 仅使用发布脚本(切勿手动推送 tag)
./scripts/release.sh vX.Y.Z "Release description"
# 热修复
./scripts/release.sh vX.Y.Z --hotfix
发布自动化链路:
- scripts/release.sh 创建并推送 tag
- release.yml 触发 GoReleaser
- GoReleaser 构建 5 个平台(linux/darwin/windows,amd64/arm64)
- 创建带二进制产物的 GitHub Release
- Release Drafter 更新草拟变更日志
钩子处理程序测试
钩子处理程序通过 internal/hook/ 中的 Go 单元测试进行验证,示例测试读取来自 stdin 的 JSON 输入并直接测试处理逻辑:
func TestHookHandler(t *testing.T) {
// Hook handlers read JSON from stdin
input := `{"eventType":"SessionStart",...}`
// Test handler logic directly
result, err := handler.Process(input)
if err != nil {
t.Errorf("handler failed: %v", err)
}
}
交叉引用
- .claude/rules/moai/core/agent-hooks.md:智能体钩子生命周期与 action 映射
- moai-foundation-cc 技能:Claude Code 钩子配置
- CLAUDE.local.md 第 7 节:钩子开发指南
- .github/workflows/ci.yml:CI 流水线配置
- .github/workflows/release.yml:发布自动化
- internal/hook/:Go 钩子处理程序实现
适用场景与限制
本技能适用于 moai-adk-go 项目中钩子与 CI/CD 的开发、测试和维护。其内容为项目专属模式,未必适用于其他项目。关于收费、价格、平台支持范围或具体效果,资料中暂未提供。