Skip to content

refactor(claude): move rules and docs out of the protected .claude/ directory - #669

Merged
Ardnived merged 2 commits into
mainfrom
claude/migrate-rules-to-nested-claude-md
Sep 1, 2026
Merged

Ardnived merged 2 commits into
mainfrom
claude/migrate-rules-to-nested-claude-md

Conversation

@Ardnived

@Ardnived Ardnived commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Why

Claude Code's Protected Paths guard makes any write under .claude/ require interactive approval, and it runs before permissions.allow. The allowlist added in #667 (Edit(.claude/rules/**), Edit(.claude/docs/**)) therefore never took effect. The unattended cloud loop stalls indefinitely on the prompt — roughly 75 minutes of one run this week — and cannot perceive that it is blocked. The guard cannot be configured away, so the content has to leave .claude/.

The mechanism that shapes this PR

.claude/rules/ follows symlinks, and the injected rule cites the target path. Verified empirically, not assumed: reading a file matching a rule's paths: glob injected the rule into context, and the injection named docs/rules/…, not the symlink. The target is outside .claude/, so the guard never sees the edit.

ln -s ../../docs/rules/<name>.md .claude/rules/<name>.md

Creating the symlink may prompt once. Editing the rule afterwards never does. That is a one-time human setup cost, not a per-edit one — which is exactly the property the loop needs.

So guidance loads two ways, by scope shape

A nested AGENTS.md scopes by directory. A paths: rule scopes by glob. Neither is better in general; the right one depends on whether the rule's scope is a directory.

Nested AGENTS.md + CLAUDE.md symlink — the scope is one clean subtree, so the guide sits beside the code it governs and its location is its scope:

Rule Now
collections.md src/collections/AGENTS.md
components.md src/components/AGENTS.md
globals.md src/globals/AGENTS.md
migrations.md src/migrations/AGENTS.md
types.md src/types/AGENTS.md
scripts.md scripts/AGENTS.md
blocks.md src/lib/richEditor/blocks/AGENTS.md
project-structure.md src/AGENTS.md
tests.md tests/AGENTS.md

docs/rules/*.md, symlinked into .claude/rules/ — the scope is not a directory, so the paths: frontmatter is kept intact and the rule loads by glob:

Rule Why a directory cannot express it
routes.md src/app/**/route.ts — a filename pattern across many directories; src/app/AGENTS.md fires for all of src/app
endpoints.md src/collections/*/endpoints/** — wildcard middle segment
testing-reqs.md **/*.test.ts / **/*.spec.ts reach outside tests/
access.md 3 unrelated paths
admin-ui.md 3 unrelated paths
storage.md 2 unrelated paths
openapi.md 2 unrelated paths
email.md 2 unrelated directories
api-clients.md a directory plus a single file elsewhere

Root import: code-style.md had no frontmatter, so it loaded unconditionally every session. No nested guide reproduces that, so it stays docs/code-style.md, @-imported from AGENTS.md beside the two docs already imported that way. Same behaviour, different mechanism.

.claude/docs/{architecture,environment,mcp-setup}.md → docs/.

Each pointer file was evidence of a mis-fit

The first pass had no glob mechanism available, so a rule spanning unrelated directories had to be split into a body plus one-line pointer files. Those pointers are now redundant — the glob reaches those paths again — and fourteen files are deleted:

src/app/(payload)/api/openapi.json/, src/app/(payload)/api/webhooks/, src/collections/Clients/, src/collections/Managers/, src/components/branding/, src/emails/ (each an AGENTS.md + CLAUDE.md pair). Two inline pointer paragraphs go too, from src/collections/AGENTS.md and src/globals/AGENTS.md.

src/fields/AGENTS.md stays. collections.md covered src/collections/** and src/fields/**. With the body nested under src/collections/, that pointer is the only thing that reaches src/fields/ — it is load-bearing, not redundant. It is the one rule in the keep-nested list whose scope is not strictly a single subtree.

tests.md and testing-reqs.md, split back apart

The first pass merged them because both scoped to tests/ and one directory holds one guide. They separate cleanly under the split:

  • tests.md — how to write tests, tests/**/*.spec.ts, directory-clean → stays tests/AGENTS.md
  • testing-reqs.md — how to run them (the CPU guard, the three-tier speed contract), reaching **/*.spec.ts outside tests/ → symlinked rule

Both were recovered from the merge-base rather than unpicked from the merged file, so no prose was rewritten in either direction.

Documentation

AGENTS.md's Documentation section now describes both mechanisms and which one a subsystem uses, and states why the content lives in docs/ — so nobody "tidies" it back into .claude/ and reintroduces the stall. docs/architecture.md's opening claim about where subsystem detail lives is corrected the same way.

.claude/settings.json already grants Edit/Write(docs/**). It deliberately does not grant .claude/rules/**: nothing should ever need to write there, and a grant would not help if it did.

Verification

  • Lean gate green — pnpm lint, pnpm typecheck, pnpm typecheck:tests, pnpm test:unit (1497/1497, 47s)
  • Every .claude/rules/*.md symlink resolves; git ls-files -s shows mode 120000 for all nine, and for every CLAUDE.md (seeds/atlas/ was missing one and now has it)
  • Every relocated rule diffs against the merge-base by path strings only — one deliberate exception, a parenthetical in testing-reqs.md that described the old loading mechanism
  • No dangling doc reference: every docs/rules/*.md, docs/*.md and nested-guide path cited anywhere in the tree exists

This remains a pure relocation. No rule prose was rewritten beyond repointing paths the move broke.

…irectory

Claude Code's Protected Paths guard makes any write under `.claude/` require
interactive approval, and it runs BEFORE `permissions.allow` — so the allowlist
added in #667 never applied. The unattended cloud loop stalls indefinitely on
it (~75 minutes of one run this week) and cannot even perceive that it is
blocked. Moving the content out of `.claude/` is the only fix.

Path-conditional loading is preserved, not abandoned. Each `.claude/rules/*.md`
becomes an `AGENTS.md` in the directory its `paths:` frontmatter targeted, with
a `CLAUDE.md` symlink beside it — the pairing this repo already uses at the root
and throughout `seeds/`. Subdirectory guides are included when an agent reads
files in that directory, so the file's location now IS its scope and the globs
are gone.

  .claude/rules/access.md            -> src/plugins/access/AGENTS.md
  .claude/rules/admin-ui.md          -> src/components/admin/AGENTS.md
  .claude/rules/api-clients.md       -> src/plugins/usage/AGENTS.md
  .claude/rules/blocks.md            -> src/lib/richEditor/blocks/AGENTS.md
  .claude/rules/collections.md       -> src/collections/AGENTS.md
  .claude/rules/components.md        -> src/components/AGENTS.md
  .claude/rules/email.md             -> src/plugins/email/AGENTS.md
  .claude/rules/endpoints.md         -> src/endpoints/AGENTS.md
  .claude/rules/globals.md           -> src/globals/AGENTS.md
  .claude/rules/migrations.md        -> src/migrations/AGENTS.md
  .claude/rules/openapi.md           -> src/plugins/openapi/AGENTS.md
  .claude/rules/project-structure.md -> src/AGENTS.md
  .claude/rules/routes.md            -> src/app/AGENTS.md
  .claude/rules/scripts.md           -> scripts/AGENTS.md
  .claude/rules/storage.md           -> src/plugins/storage/AGENTS.md
  .claude/rules/types.md             -> src/types/AGENTS.md
  .claude/rules/{tests,testing-reqs}.md -> tests/AGENTS.md   (merged)
  .claude/rules/code-style.md        -> docs/code-style.md   (@-imported)
  .claude/docs/*.md                  -> docs/*.md

Three cases needed deciding rather than moving:

- `code-style.md` had NO frontmatter, so it loaded every session. A nested
  guide cannot reproduce that, so it moves to `docs/` and is `@`-imported from
  AGENTS.md alongside the two docs already imported that way. Behaviour is
  unchanged; only the mechanism is.
- `tests.md` (how to write tests) and `testing-reqs.md` (how to run them — the
  CPU guard and three-tier contract) both scoped to `tests/`. One directory can
  hold one guide, so they are merged into `tests/AGENTS.md` with all content
  kept; testing-reqs' sections are appended under "Test execution requirements"
  and its two cross-file pointers become in-file ones.
- Six rules spanned unrelated directories. The body lives in the primary one and
  the others get a one-line pointer, so no prose is duplicated: Clients and
  Managers point at plugins/access, Clients also at plugins/usage, branding at
  components/admin, fields at collections, emails at plugins/email, and the two
  api/ route folders at plugins/openapi and plugins/storage.

AGENTS.md's Documentation section claimed `.claude/rules/` were "path-scoped
rules that auto-load when reading matching files ... each file's frontmatter
declares its globs". That would have been false the moment the globs were
dropped, so it now describes the nested arrangement it actually has.

118 path references across 68 files were rewritten — docs, skills, hooks,
settings, and the ~35 source comments that cite a rule by path. `.claude/`
keeps what is genuinely Claude-specific: hooks, skills, commands, agents,
output styles and settings. The one surviving `.claude/docs` mention is a
`$comment` in workflow.json describing a file deleted before this change.

Verified: `pnpm lint` and `pnpm typecheck` pass; no reference to the old
paths remains outside that historical note.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@railway-app

railway-app Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

🚅 Deployed to the SahajCloud-pr-669 environment in sahajcloud

Service Status Web Updated (UTC)
SahajCloud ✅ Success (View Logs) Sep 1, 2026 at 3:28 pm
1 service not affected by this PR
  • mailpit

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-669 September 1, 2026 02:59 Destroyed
…of nesting them

`.claude/rules/` supports symlinks, and this was verified rather than assumed:
reading a file matching a rule's `paths:` glob injected the rule, and the
injection cited the *target* path (`docs/rules/…`), not the symlink. That target
is outside `.claude/`, so the Protected Paths guard never sees the edit and an
unattended run does not stall. Creating the symlink is a one-time human cost;
editing the rule is not.

That changes the design of the previous commit. A nested `AGENTS.md` scopes by
DIRECTORY; a `paths:` rule scopes by GLOB. Where a rule's scope was one clean
subtree, the nested guide is the better home — it sits beside the code it
governs. Where it was not, the previous commit had to invent pointer files, and
each pointer was evidence of the mis-fit. Nine rules go back to `docs/rules/`
with their original `paths:` frontmatter, symlinked into `.claude/rules/`:

  docs/rules/routes.md        src/app/**/route.ts — a filename across many dirs
  docs/rules/endpoints.md     src/collections/*/endpoints/** — wildcard middle
  docs/rules/testing-reqs.md  tests/** + **/*.test.ts + **/*.spec.ts
  docs/rules/access.md        3 unrelated paths
  docs/rules/admin-ui.md      3 unrelated paths
  docs/rules/storage.md       2 unrelated paths
  docs/rules/openapi.md       2 unrelated paths
  docs/rules/email.md         2 unrelated dirs
  docs/rules/api-clients.md   a directory plus one file elsewhere

Eight stay nested, because their scope really is one subtree: collections,
components, globals, migrations, types, scripts, blocks, project-structure.
`code-style.md` is unchanged — unscoped, `@`-imported from the root guide.

`tests.md` and `testing-reqs.md` are split back apart. `tests.md` (how to write
tests, `tests/**/*.spec.ts`) is directory-clean and stays `tests/AGENTS.md`;
`testing-reqs.md` (how to RUN them — the CPU guard and the three-tier contract)
reaches `**/*.spec.ts` outside `tests/`, so it becomes a symlinked rule. Both
were recovered from the merge-base rather than unpicked from the merged file, so
no prose was rewritten.

Fourteen pointer files are deleted — every one that existed only because a rule
spanned directories, now redundant because the glob reaches those paths again:
the two `api/` route folders, Clients, Managers, branding, and emails.
`src/fields/AGENTS.md` STAYS: `collections.md` covered `src/collections/**` and
`src/fields/**`, and with the body nested under `src/collections/` the pointer
is the only thing that reaches `src/fields/`.

Every nested guide that remains keeps its `CLAUDE.md` symlink (mode 120000);
`seeds/atlas/` was missing one and now has it. Path references are rewritten
across docs, hooks, skills and source comments; `.claude/settings.json` already
allowed `Edit/Write(docs/**)`, and deliberately does NOT allow `.claude/rules/**`
— nothing should ever need to write there.

The root guide now describes both mechanisms and says why the content lives in
`docs/`, so nobody tidies it back into `.claude/` and reintroduces the stall.

Verified: lean gate green (lint, typecheck, typecheck:tests, 1497 unit tests);
every symlink resolves; each relocated rule diffs against the merge-base by path
strings only.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Ardnived
Ardnived merged commit 228c3a1 into main Sep 1, 2026
2 checks passed
@Ardnived
Ardnived deleted the claude/migrate-rules-to-nested-claude-md branch September 1, 2026 17:31

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-669 — 6557f931 Deployed Sep 1, 2026 by railway-app[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant