Zum Inhalt springen

Claude Code hooks and session red flags

How codecharter mcp install --hooks adds a SessionStart, Stop and SessionEnd hook pack to a project's Claude Code settings, how the shared host turns each Claude Code conversation into one session with its CodeCharter runs, and which red flags it raises when a session ends.

The shared CodeCharter host can record which Claude Code conversation ran which CodeCharter command. The hook pack is what tells it when a conversation starts, pauses and ends. It is optional, it is written into one repository only, and it never makes Claude Code fail.

Installing the hooks

codecharter mcp install --client claude-code --scope project --hooks

The command writes the CodeCharter MCP server entry as usual and then adds three hooks to the project's .claude/settings.json: one for SessionStart, one for Stop and one for SessionEnd. Each calls codecharter host session-hook, a hidden command that reads the hook's JSON from standard input and reports the event to the host.

  • Hooks only go into the project settings, never into your user settings. The command refuses --scope user together with --hooks, and any client other than claude-code.
  • Hooks of other tools in the same file stay as they are, and so does the rest of the file.
  • Running the command again changes nothing.
  • --dry-run prints the resulting file without writing it.
  • .claude/settings.json is normally committed, so your collaborators and CI run the hooks too. They need codecharter on their PATH; without it the hook prints a "command not found" and Claude Code carries on. Where the host is switched off (CODECHARTER_NO_HOST, or a CI environment) the hook starts nothing and posts nothing.

The Stop hook is marked async. A synchronous Stop hook would add about 0.9 seconds after every answer of the assistant, because it starts the codecharter process each time; an async one runs in the background and does not delay the turn. If you installed the pack with an earlier version, run the install command again to replace the old Stop entry.

To remove the pack, run:

codecharter mcp install --client claude-code --scope project --remove-hooks

Only the CodeCharter hooks are removed. A group, an event or the hooks object that this leaves empty is removed with them; anything else in the file stays.

What the host records

On session start the hook exports CODECHARTER_SESSION through the CLAUDE_ENV_FILE that Claude Code provides, so every CodeCharter command the agent runs in that conversation joins the session. It then tells the host the session id and the working directory. The host starts the session with the git head at that moment. On Stop it refreshes the git state, and on SessionEnd it reads the git state once more and ends the session.

The MCP server of the same conversation is matched to the session by time: a transport session and a hook session in the same working directory that start within two minutes of each other are one conversation. If you start two Claude Code windows in the same directory within two minutes, the closest start wins. /clear ends the hook session and starts another one while the MCP server keeps running; the new session adopts the MCP server again if it starts within two minutes of the end of the old one.

The hook posts over the host's local pipe. The route is not reachable over the TCP listener that serves the browser UI. If no host runs, a start hook starts one, while stop and end hooks never start one. A hook that cannot reach the host writes the reason to standard error and exits with 0, so Claude Code carries on.

Red flags

When a session ends, the host compares the working tree at the start of the session with the end, including new source files git does not track yet, and raises a flag for each of these:

Flag Raised when
Added suppression A line was added that contains codecharter-disable, codeguard-disable, SuppressMessage or #pragma warning disable.
Changed public API A public or protected declaration line in a C# file was changed or removed.
Diff too large The change is over the size the host reads (about 20,000 changed lines, or 1 MiB of new files). It is then not scanned for the flags above.
Baseline write A CodeCharter run of the session wrote a baseline.
Not scanned The host stopped first, or git did not answer in time (10 s for the state at the session's start, 30 s for the diff) or failed, so the change was not scanned for the flags above. The flag names the reason and keeps the session's gate open.

The comparison runs when the session ends. For a window that was closed or sat idle that can be long after the work, so it may include edits another Claude Code window made in the same working tree: a flag is a hint to look, not proof of who wrote the line. The check for a changed public API is a heuristic on the declaration line, not a parse of the code.

A flag stores the file path and line number, for example src/A.cs:12, and never the source text. A session keeps at most 25 flags per kind, plus one flag that says how many more there were.