Repository hooks
hooks · 版本 0.1.2
智能体与自动化3 条规范1 项检查
Connects Git and Claude Code to the repository dispatcher and runs package hooks on their events.
关于该包
这段内容尚未翻译,下面是英文原文。
A package can enforce a norm at a Git or Claude Code event. The norm lists the hook under enforcers and declares enforcerKind: hook.
How it works
- On install and update,
syncfinds the repository from the project's nearest ancestor with a.gitfile or directory. It installspre-commitandcommit-msgin Git's configured hooks folder. Each callsshtuk hookwith the event and any commit message path supplied by Git. syncalso adds four entries to the repository's.claude/settings.json. They callshtuk hook agent:<Event>onSessionStart,PreToolUse(Bash, Write, Edit, MultiEdit),PostToolUse(Write, Edit, MultiEdit), andStop.- In each project selected by the dispatcher, the runner reads the
hookslists of all installed packages. A declaration has{name, event, tools, run, norm, timeout}. Claude Code hooks receive the original JSON on stdin. Git hooks receive event arguments on the command line. - When the dispatcher supplies
SHTUK_FILES, the runner usesSHTUK_OWNERSto remove files whose owner appears in the norm'sexceptlist. It skips a hook when that list excludes every supplied file. - Hook exit code 0 lets the event pass. Code 2 blocks it and reports the finding on stderr. Other exit codes report a broken hook and let the event pass.
Other people's hooks, repeats and updates
- For an existing shell hook,
syncappends one marked block. Git runs the block when the shell reaches it. Keep the dispatcher block reachable when editing the preceding script. - In the repository's Claude Code settings,
syncreplaces its own entries across events, including entries for the older direct runner. It keeps other entries and settings. Invalid JSON stopssyncand leaves the settings file untouched. shtuk run hooks removeremoves the repository dispatcher entries.shtuk run hooks installedchecks for missing, duplicate, or stale entries.
Norms
- HK-R01: the hook runner is installed once per event. The enforcer is the check
installed. - HK-R02: a database record that breaks a check does not reach the disk silently. The enforcers are the hooks
record-pre(does not let through a record with no header, no title, or an empty required field) andrecord-post(after a write runs the schema checks s01 to s03 on that record and tells the agent at once what is broken). - HK-R03: one commit touches one project. The
one-projecthook reports staged files owned by multiple projects. Setmode: enforcein the project's copy of the rule to block such commits.
Git and Claude Code need shtuk and bun on their PATH.
规范 3
| ID | 规范 | 级别 | 模式 | Enforcer |
|---|---|---|---|---|
| HK-R01 | 项目的 .claude/settings.json 中,包的钩子运行器须在 SessionStart、PreToolUse、PostToolUse 和 Stop 四个事件中各出现一次 |
MUST | 拦截 | 检查 |
| HK-R02 | 智能体写入集合记录时若破坏了结构检查,钩子要么拦下这次写入,要么在写入之后立即告诉智能体哪里有问题 |
MUST | 拦截 | 钩子 |
| HK-R03 | The staged files with an owner belong to at most one project |
MUST | 观察 | 钩子 |
“拦截”:enforcer 会阻止违规;“观察”:只记录违规。MUST 级别表示必须遵守;SHOULD 表示默认应当遵守。
动作 3
| 名称 | 作用 | 文件 | 时机 |
|---|---|---|---|
| sync | Install the repository dispatcher entries for Git and Claude Code | sync.ts | 安装后 更新后 |
| remove | Remove the repository dispatcher entries for Git and Claude Code | remove.ts | shtuk run |
| installed | The repository dispatcher is installed once per event 规范 HK-R01 | installed.ts | 检查 |
shtuk check 运行检查;shtuk run hooks <名称> 运行任意动作。“安装后”动作只有在包来自可信来源时才会自动运行。
智能体钩子 3
页面 3
这些页面会放到项目中的以下路径。
- /norms/rules/hooks/
- Claude Code 的项目设置中有钩子运行器 HK-R01
- 破坏检查的数据库记录不会悄悄写入磁盘 HK-R02
- One commit touches one project HK-R03
包文件 13
它们会放到 .shtukbase/packages/hooks/,动作从那里运行。
hooks/one-project.ts 631 B
hooks/record-post.ts 1.5 KB
hooks/record-pre.ts 2.6 KB
icon.svg 377 B
install.ts 4.3 KB
installed.ts 141 B
lib/git-hooks.ts 3.0 KB
lib/hooklib.ts 7.0 KB
lib/write.ts 927 B
remove.ts 99 B
run.ts 2.8 KB
sync.ts 95 B
test/hooks.test.ts 10.1 KB