﻿---
title: "让 Codex 给自己写钩子"
summary: "「写一个每次补丁后跑 lint 的 PostToolUse 钩子」。先本地验证，再提交到团队目录。"
category: hooks
level: advanced
surfaces: [cli]
tags: ["hooks", "lint", "worktree"]
canonical: /tips/ask-codex-to-write-hooks/
---

# 让 Codex 给自己写钩子

「写一个每次补丁后跑 lint 的 PostToolUse 钩子」。先本地验证，再提交到团队目录。

Agent 很适合写这些样板。你要盯的是：

- 钩子是否幂等
- 失败时是否 fail-closed
- 会不会把密钥打进日志
- 项目钩子在 git worktree 里是否可靠（有报告说项目钩子在 worktree 中失效，用户级钩子可作变通）

自动化跑钩子需要 `--dangerously-bypass-hook-trust`——只在你已经审查过来源时用。

## 来源

- [OpenAI · Hooks](https://developers.openai.com/codex/hooks)
