How to Stop AI Agents From Force-Pushing to Main

2 min read · Works with Claude Code, Cursor, Codex, and any MCP agent

The problem

You write never force-push to main in your CLAUDE.md. Your agent reads it. Then it force-pushes to main anyway. Prompt rules are suggestions. Agents can and do ignore them when the context window fills up or when a chain of reasoning overrides the instruction.

Real example: A developer lost 14 commits when their Claude Code agent ran git push --force origin main during a rebase. The CLAUDE.md said "never force-push." The agent did it anyway.

Why prompt rules fail

The fix: a pre-action gate

A pre-action gate intercepts the tool call before it executes. It pattern-matches the command against known-bad actions and blocks them. The agent cannot bypass it because the gate runs outside the agent's control, at the MCP hook layer.

Step 1: Install ThumbGate

npx mcp-memory-gateway init

This auto-detects your agent (Claude Code, Cursor, etc.) and configures the PreToolUse hook.

Step 2: Give feedback

The next time your agent tries a force-push (or anything dangerous), give it a thumbs-down with context:

👎 "Never force-push to main. This destroyed 14 commits last time."

Step 3: Gate auto-generates

ThumbGate captures the feedback, matches it against the tool call pattern, and auto-generates a prevention rule. After repeated failures (configurable), it promotes to a hard gate:

# Auto-generated prevention rule
pattern: "git push --force"
target_branch: "main"
action: BLOCK
reason: "Force-push to main blocked — destroyed 14 commits (2026-03-15)"

Step 4: Gate fires on every future attempt

The PreToolUse hook checks every Bash tool call. If it matches git push --force targeting main, the action is blocked before execution. The agent receives a rejection with the reason and adapts.

Key difference: Prompt rules ask nicely. Pre-action gates physically block. The gate runs at the hook layer, outside the agent's reasoning chain, so it cannot be overridden by context or chain-of-thought.

What about other dangerous actions?

The same pattern works for any tool call you want to prevent:

Every thumbs-down teaches the system. Thompson Sampling adapts gate sensitivity: high-risk patterns get strict enforcement, low-risk ones stay relaxed.

Try it now

One command. Your agent stops repeating mistakes today.

$ npx mcp-memory-gateway init