Pattern: How to prevent agents from committing to wrong sessions when...
Documentation for Sessions
🎨 Pattern: How to prevent agents from committing to wrong sessions when...
Date: 2026-03-27
Time: 14:28:51 UTC
Session: gspace2-0
Agent: architecture
Confidence: 94%
Status: ✅ Documented
📋 Decision Context
Question:
How to prevent agents from committing to wrong sessions when they bypass or forget MCP tools?
Task: Smart Session System v13.0 — enforcement strategy
🎯 Decision Made
Chosen:
Defense in depth: 3 layers. (1) MCP resolution + commitPrefix in every response (agents see the right tag constantly). (2) Claude Code hook v2.0 parses commit tags and outputs session context on every prompt. (3) Git pre-commit hook validates [S{N}.{sub}] pattern on session branches — catches ALL agents at the git layer regardless of platform.
Options Considered:
- A) MCP-only enforcement (no git hooks)
- B) Git pre-commit hook only
- C) Defense in depth — MCP + Claude hook + git hook (CHOSEN)
- D) Blocking hook that prevents all work without preflight
💭 Reasoning
No single enforcement point is sufficient. MCP tools require the agent to call them. The Claude Code hook only affects Claude Code. The pre-commit hook is the only universal enforcement — every agent that commits goes through it. But it only validates the tag format, not the correctness of the session number. The MCP resolution + commitPrefix injection handles correctness (by computing the right tag and showing it in every response). The Claude Code hook bridges the gap by showing the most recent commit tag before the agent even calls preflight. Together: MCP knows the right session, hook reminds the agent, git enforces the format. The macOS grep -oP incompatibility was caught and fixed during implementation (replaced with grep -oE + sed).
📁 Files Involved
tools/hooks/commit-msg.claude/hooks/session-check.shMCP/mcp-gograb-ai/src/tools/preflight.ts
🔗 Related
- Decision ID:
dev_1774621731623_x62qla8 - Session: 0
- Workspace: gspace2
Auto-generated by MCP Smart Consolidation