Dev.to Security 🔐 Cybersecurity 👁 2 📖 7 min read

An AI Agent Wrote Rules in My Name, and Every Session Obeyed Them

If you run AI coding agents with standing instruction files, an agent can write a rule, put your name on it, and every later session will obey it as yours. Another note in those files will not stop that, because a note e

If you run AI coding agents with standing instruction files, an agent can write a rule, put your name on it, and every later session will obey it as yours. Another note in those files will not stop that, because a note enforces nothing. Settings that the tool itself enforces will. This is how it happened to me between November 24 and November 28, 2026, and the two controls I added in Claude Code in response.

The setup

I run many Claude Code sessions on my Mac for my own work as a CTO. Each session reads standing instruction files: a global ~/.claude/CLAUDE.md, an AGENTS.md in the repository, and memory notes for each project. Those files tell each new session who I am and how I want it to work.

That is the point of instruction files, and it is also the exposure. Anything written into them instructs every later session, no matter who wrote it.

Six rules I never wrote

On November 24, 2026, a Claude session created a file of six "working limits" in my repository and committed it with Claude as co-author. The header attributed the file to me, with my name and that day's date. It told every AI tool to follow the file before every other rule.

The session did not stop at one file. It linked the new file first from the repository's AGENTS.md, added a line to my global CLAUDE.md saying the file came first, and saved a memory note that labeled it a "HARD RULE." The six limits were:

  • Stay within a narrow list of my areas of work.
  • Write drafts only when I ask.
  • Run one session per day, with no parallel agents.
  • Make no judgments about people.
  • Run an automated check on my own email drafts through a GitHub Actions job every 30 minutes on weekdays, plus a Claude Code Stop hook.
  • Keep a Friday scorecard, with a December 11 trigger that would cut AI work back to dashboards and reports only.

From November 24 to November 28, every session treated those six limits as mine. On November 28, a session quoted the file back to me as my rule, and I told it I had never asked for it. The git history shows that a Claude session wrote the file. Nothing in it shows that I asked. The agent invented the rules, signed my name to them and spread them into all three layers of my instructions.

The second failure: sessions starting sessions

That same day, one session started another session to rebuild an internal test project. Sessions kept starting more sessions until a chain of 17 worked the same project, and several of them worked the same email thread at the same time.

I sent one session's email on the project. Afterward, another session in the chain drafted a second email on it. A third, unrelated session then saved over that rewrite, and a merge of the drafts followed three minutes later. Nothing extra went out. The invented file in every session's instructions said one session per day with no parallel agents, and it did not stop any of this.

The sessions made two other mistakes that day:

  • They switched a session's model by citing a rule that nobody had verified was mine.
  • They overstated a test result in a project plan, writing that a condition was met when the log showed only part of it.

Notes do not enforce anything

Every one of those failures happened while instruction files told the sessions how to behave. A line in CLAUDE.md is text the model reads, and nothing in Claude Code holds a session to it.

I asked the AI to admit its errors plainly, stop inventing rules, and propose real fixes instead of more notes. It listed its own errors, corrected two of them in its own files, and proposed fixes that Claude Code enforces itself. I approved two of them with two words: "do it."

Fix one: block sessions from starting sessions

Two tools let a session start a new session or hand off to one. I added both to the deny list in ~/.claude/settings.json. In testing, Claude Code removed both tools from the running session at once and reported them as "denied by a permission rule." With that rule in place, sessions could not start other sessions, so only I could open them.

Fix two: rules files change only with my approval

This fix needed two parts, because testing showed that one part alone left a gap.

The first part is a set of permissions.ask rules on edits to rules files: every CLAUDE.md and AGENTS.md, the settings file, and the hooks, skills and memory folders. We tested it four times, including in auto mode. Each edit stopped and waited for my one-time approval, and the transcript recorded the decision source as user_temporary.

The second part closes the shell route, since an agent can change a file without the Edit tool. A PreToolUse hook on Bash now denies any shell command that names a rules file and would write to it. The hook checks for redirects, tee, sed -i, cp, mv, rm, Python or Node scripts, and git commands that rewrite files. Its denial tells the agent to use the Edit or Write tool, which brings the change to me for approval.

Trimmed to the parts that matter, the settings looked like this:

{
  "permissions": {
    "deny": [
      "mcp__ccd_session__start_session",
      "mcp__ccd_session__hand_off_to_session"
    ],
    "ask": [
      "Edit(//**/CLAUDE.md)",
      "Edit(//**/AGENTS.md)",
      "Edit(~/.claude/settings.json)",
      "Edit(~/.claude/hooks/**)",
      "Edit(~/.claude/skills/**)",
      "Edit(~/.claude/projects/**/memory/**)"
    ]
  },
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "python3 ~/.claude/hooks/protect-rules.py"
          }
        ]
      }
    ]
  }
}

Key finding: in auto mode, a hook's "ask" is not your approval

The first version of the hook returned "ask" instead of "deny." In auto mode, that request never reached me. Claude Code's auto-mode classifier settled the hook's "ask" on its own, the transcript recorded the decision source as classifier, and I never saw a prompt. The permissions.ask rules, tested in auto mode, did prompt me.

A hook's "ask" in auto mode guarantees no human approval. If a route must stay closed, the hook has to return "deny," so we changed the shell hook to a hard deny.

The limits

These controls close specific routes, and they have known gaps:

  • The hook also blocks a shell command that only reads a rules file with Python. Read those files with the Read tool, grep or cat instead.
  • The hook matches file names and common write commands, so it closes the usual routes rather than every possible one.
  • Subagents inside a session are not sessions, so the deny rule does not affect them.

We backed up the previous settings before the change and documented the change in a Markdown file in the repository.

Cleaning up a rule that spread to eight places

After the controls went in, I directed removal of the invented limits, and every change to a rules file went through the new prompts.

One invented rule set had spread to eight places: a line in my global CLAUDE.md, a heading and a bullet in AGENTS.md, a numbered copy of three limits in a portable rules digest that I paste into web AI tools, the file itself, the memory note, the Stop hook and the 30-minute GitHub Actions check. Two of those pieces, the hook and the scheduled job, were enforcement the agent built for rules I never asked for. A fresh session took the limits out of every rules file, the settings, memory and the scheduled job, with my approval at each prompt, and pushed the change. The new guard blocks shell deletes, so deleting the original file is left to me.

An audit session checked every rule attributed to me against the messages I typed myself. It found 11 rules that AI sessions had credited to me with no message from me behind them.

What I would tell anyone running agents with instruction files

  1. Record who wrote each rule, and accept only the rules you typed yourself, because later sessions will obey a rule an agent wrote in your name.
  2. Put your controls in settings. Lines in an instruction file do not enforce anything, while deny and ask permission rules do.
  3. In auto mode, do not count a hook's "ask" as a person's approval. Use "deny" for any route that must stay closed.
  4. Keep one writer per thread, because parallel sessions overwrite each other.
  5. Verify each claim at its source instead of repeating another session's report.

I am CTO of Inzo Technologies, and we help companies set up AI tools with real guardrails.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.