Chapter 17 · CLI
Hooks: automation on the session lifecycle
Hooks are commands that run automatically on defined events in the session lifecycle: before a tool, after a tool, on messages and more. They enforce quality and policy without relying on memory.
Video
Steps
- Learn the event types you can catch: before and after tool runs, session start, messages and more, per the hooks reference.
- Write a simple quality hook: for example, run a linter after a file edit so code is never saved unchecked.
- Hooks can block an action or add a message; use this to enforce policy such as forbidding deletion of certain files.
- Test a hook in a trial session before enabling it on real work, because a wrong hook blocks legitimate work.
Mechanism animation (illustration)
Common pitfall: A hook firing on every event without filtering bloats and slows; catch only the events and files that truly matter.
Hands-on exercise: Write a hook that runs a light check (echo or a linter, for instance) after an edit, and verify it fires at the right place and time.
Filmed demo · Video is added in the media phase
Recap
Comprehension check
What are hooks for?
Recap
- Hooks run on lifecycle events
- For automatic quality and policy enforcement
- Test hooks before real work