Skip to content

docs(agents): move guidance beside its code, and fix the hooks in worktrees - #1172

Merged
Ardnived merged 4 commits into
mainfrom
docs/agent-context-split
Nov 16, 2026
Merged

Ardnived merged 4 commits into
mainfrom
docs/agent-context-split

Conversation

@Ardnived

Copy link
Copy Markdown
Contributor

Summary

  • Agent guidance now loads where its work is. code-style.md really loads. architecture.md and admin-ui.md are split beside their code.
  • The two project hooks now run in a worktree reached from a session started at the workspace root. Before this, 69 of 72 runs exited 127.

Change outline

Files — where each guide now loads

architecture.md (52k) never loaded at all, because its import sat in a code span. admin-ui.md (59k) loaded whole on any collection, field or global edit. Each section now lives where it loads for the work it describes. The text moves verbatim.

 AGENTS.md
-  `@docs/code-style.md`, `@docs/environment.md`, `@docs/architecture.md`   # inside backticks: none loaded
+  @docs/code-style.md                                    # real import, always loaded (~0.9k tokens)
+  docs/environment.md, docs/architecture.md              # read on demand
 docs/architecture.md                                     # 52k → 9k: storage, collections, Payload config, a guide index
+src/plugins/sentry/AGENTS.md                             # logging, Sentry, #670 / #734 / #1039
+src/jobs/AGENTS.md                                       # scheduled jobs
+src/endpoints/atlas/AGENTS.md                            # Atlas SEO, sitemap, Wix contracts
 src/collections/Events/AGENTS.md
+  ## The Atlas event feed and registration contract
 docs/rules/endpoints.md
+  ## The custom endpoints this app serves               # the endpoint and route tables
 docs/rules/admin-ui.md                                   # 59k → 28k, paths: src/components/admin, branding only
+docs/rules/admin-config.md                               # 11k, paths: collections, globals, fields, blocks
+  conditions · live preview · previewTargetField · hidden-via-join · project visibility · import map
+docs/rules/admin-navigation.md                           # 10k, paths: ProjectSelector, AtlasNav, Dashboard, AdminNavLinks, atlasOverview
+src/components/admin/WorkflowButtons/AGENTS.md
+src/components/admin/JobStageView/AGENTS.md
+src/components/admin/FrameEditor/AGENTS.md

Behaviour — which hook script a session runs

$CLAUDE_PROJECT_DIR names the workspace in a session started at the workspace root, so the hook path did not exist there. The fallback only applies when the project directory holds no hook. The opposite order would run the hook script of any checkout the session moved into.

 PostToolUse Edit|Write                                   .claude/settings.json
-  exec $CLAUDE_PROJECT_DIR/.claude/hooks/<hook>.mjs
+  $CLAUDE_PROJECT_DIR holds the hook → run it            # unchanged for a session started in the repo
+  otherwise → $(git rev-parse --show-toplevel)/.claude/hooks/<hook>.mjs
 respond(readInput, runHook)                              scripts/hook-run.mjs
-  runHook({ filePath, projectDir: $CLAUDE_PROJECT_DIR })
+  runHook({ filePath })                                  # the script's own REPO_ROOT

Preview

No runtime change. The preview behaves exactly like main.

Test results

  • Lean gate: lint ✓, format ✓, typecheck ✓, typecheck:tests ✓, unit 3878 / 3879. The one failure is tests/unit/ics-builder.spec.ts, which depends on timezone. It fails on a machine set to MST and passes with TZ=UTC. This branch does not touch it, and claude/fix-1158-ics-utc-timestamps covers it.
  • Targeted specs: hook-run, doc-citations, payload-types-hook, unit-test-hook, sanitized-config-rules: 52 passed.
  • Hook commands exercised by hand with CLAUDE_PROJECT_DIR set to the workspace root, to the worktree, and outside any checkout. All three exit 0.
  • Build: N/A

Manual verification

  1. Start a session at the workspace root and edit a file inside a SahajCloud worktree that a unit spec names. The unit hook should run that spec instead of failing with exit 127.
  2. Start a session in SahajCloud and run /memory. docs/code-style.md should be listed as loaded.
  3. Edit a collection file. admin-config.md should load, and admin-ui.md should not.

Notes for reviewer

  • Collection edits now load 11k characters of admin guidance instead of 59k. Every session now loads code-style.md, about 0.9k extra tokens, which the guide always said it did.
  • A script moved the sections, matching on headings. It refused to finish unless every heading was placed exactly once. Where "above" or "below" pointed across a new file boundary, the sentence now names the file. Every code comment citing a moved section points at its new home, and doc-citations passes.
  • /simplify and /code-review were not run. The diff is moved text plus pointer comments. The security review ran because admin components and a UserSubmissions hook comment are in the trigger paths, and it found nothing.
  • This touches .claude/ (the hook commands and two .claude/rules symlinks), so it can't be changed by an unattended run.
Detail
  • Found by a Claude Code /doctor run. In the 50 most recent sessions, both project hooks failed 69 of 72 times, each with /bin/sh: …/WeMeditate/.claude/hooks/<hook>.mjs: No such file or directory.
  • admin-ui.md and access.md were each over Claude Code's large-memory-file threshold of 50k characters, which applies at a 1M context. access.md is still over it. Its sections are rationale and warnings rather than anything a session can derive from the code, so it is left for a separate split.
  • Four file trees were cut because ls answers them. Two had drifted: seeds/ was missing nine entries, and the OpenAPI module list was missing basicAuth.ts and endpointAuthPlugin.ts.
  • code-style.md lost its Import Order section. ESLint's import/order rule enforces it, and the workflow plugin's eslint-fix hook applies it on every edit.

🤖 Generated with Claude Code

Ardnived and others added 4 commits November 16, 2026 10:35
…lready shows

AGENTS.md said code-style.md, environment.md and architecture.md were
"loaded every session", but each import sat inside backticks, and an `@`
import in a code span is plain text. None of the three ever loaded.
code-style.md now imports for real, minus its Import Order section, which
ESLint's import/order rule enforces and the eslint-fix hook applies on every
edit. environment.md and architecture.md stay on demand: architecture.md
alone is past Claude Code's large-memory-file threshold.

Four file trees go too. Each repeated what ls shows, and two had drifted:
seeds/ was missing nine entries, and the OpenAPI module list two files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…space root

Both PostToolUse hooks were addressed as $CLAUDE_PROJECT_DIR/.claude/hooks/…,
and respond() handed the scripts the same variable as their project. A session
started at the WeMeditate workspace root and working in a SahajCloud worktree
has CLAUDE_PROJECT_DIR naming the workspace, so the command was not found:
69 of 72 runs exited 127 over the last two weeks, and neither types nor unit
specs ran after those edits.

The command still prefers CLAUDE_PROJECT_DIR, so a session started in the
repo runs exactly what it did. Only when the project holds no hook does it
fall back to the checkout the session is in (git rev-parse --show-toplevel).
The other order would run the hook script of any checkout the session moved
into. respond() now leaves each script on its own REPO_ROOT.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
architecture.md was 52k characters that no session loaded on its own, so
its warnings reached nobody unless they went looking. Each section now
lives where it loads for the work it describes:

- Logging, Sentry and the three capture rules (#670, #734, #1039) ->
  src/plugins/sentry/AGENTS.md
- Scheduled jobs -> src/jobs/AGENTS.md
- The Atlas SEO, sitemap and Wix contracts -> src/endpoints/atlas/AGENTS.md
- The Atlas event feed and registration gate -> src/collections/Events/AGENTS.md
- The custom endpoint list -> docs/rules/endpoints.md

The text moves verbatim. What stays is the whole-app view, read on demand:
storage, collections, the Payload config settings (#684's error body,
slow-query logging, the pool and depth caps), and a table of where each
subsystem's guide now is. The route layout and the config file list went,
since ls answers both. Every code comment citing a moved section points at
its new home.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
admin-ui.md loaded its whole 59k characters on any edit under
src/collections/, src/fields/ or src/globals/, past Claude Code's
large-memory-file threshold, though most of it is about writing a
component. It is now four rules plus three component guides, each loading
where its work is:

- admin-ui.md (28k) — writing a component; src/components/admin/
- admin-config.md (11k) — admin settings a collection, global or field
  config owes: conditions, live preview, previewTargetField, project
  visibility, the import map; collections, fields, globals, blocks
- admin-navigation.md (10k) — the project selector, the Atlas sidebar and
  the dashboards; only those folders, src/lib/atlasOverview and
  currentProject.ts
- WorkflowButtons, JobStageView and FrameEditor each get an AGENTS.md
  beside the component

A collection edit now loads 11k of admin guidance instead of 59k. The text
moves verbatim; "above" and "below" that crossed a new file boundary name
the file instead, and every citation of a moved section points at its new
home.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@railway-app

railway-app Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

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

Service Status Web Updated
SahajCloud ✅ Success (View Logs) Web Nov 16, 2026 at 5:40 pm UTC
1 service not affected by this PR
  • mailpit

@railway-app
railway-app Bot temporarily deployed to sahajcloud / SahajCloud-pr-1172 November 16, 2026 17:37 Destroyed
@Ardnived
Ardnived marked this pull request as ready for review November 16, 2026 17:45
@Ardnived
Ardnived merged commit c9f9c9b into main Nov 16, 2026
2 of 3 checks passed
@Ardnived
Ardnived deleted the docs/agent-context-split branch November 16, 2026 17:46

This branch was successfully deployed

No deployments
sahajcloud / SahajCloud-pr-1172 — 98bf4641 Deployed Nov 16, 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

Development

Successfully merging this pull request may close these issues.

1 participant