Commit Hooks are Critical with AI Agents in Cursor

AI agents are notorious for getting tasks done but also taking some shortcuts in TypeScript such as falling back to using things like 'any', which you probably do not want in your codebase.

This creates a scenario where the AI has done all of the work, you've tested and verified everything, but it's left you with data and functions untyped. So in this situation, when you run git commit, you can set up a commit hook to expose all the TypeScript errors then you just let the AI agent run that commit hook and it can find all of the mistakes that it made and go through and fix everything without you ever having to touch the code.

This lesson covers a real world scenario of a commit hook running based on a refactor that was made when extracting some types. You will learn how Cursor AI agents handle and respond to commit hooks and by giving exhaustive information on what went wrong in a commit, the agent can continue on and make all the changes for you.

Share with a coworker

Transcript

[00:00] When you ask an agent to complete a task and it does all of the work for you and you check your features and run your tests, you'll often get stuck in these little annoying loops with various linting errors and TypeScript errors. Now luckily you can add commit hooks to lint and check the types before you commit. And you can actually run that inside of the agent. So I'm going to use one of my dictation aliases called hard save. Hard save, which adds in this prompt to add, commit, and push.

[00:25] So when I turn this over to the agent and I allow it to run those commands, It'll add everything. Sometimes it does newline characters and it'll fix it. And now it's running the commit command and it's catching that there are TypeScript errors. That there are some extra anys, extra unknowns, and this will happen a lot no matter how much you put in guards against it typing any and whatnot. And so now it's automatically responding to fixing those types and going through.

[00:55] I didn't have to click anything and now it's trying to commit again and it's doing like a simple cleanup and it did all this work of cleaning up the ennies and cleaning up the types without me doing anything. The key here was having a commit hook that once the agent ran it, it gets all of the output for linting and typescript and those tiny little things that you would trust an AI to fix before checking in your code.