Skip to content

docs(agents): split AGENTS.md core rules from on-demand pi-rules instructions - #339

Merged
tradem merged 5 commits into
mainfrom
docs/agents-md-instructions-split
Sep 3, 2026
Merged

tradem merged 5 commits into
mainfrom
docs/agents-md-instructions-split

Conversation

@tradem

@tradem tradem commented Sep 3, 2026

Copy link
Copy Markdown
Owner

Summary

Splits the always-loaded backend/AGENTS.md (48.7 KB, ~12k tokens injected into every session) into a ~12 KB normative core file plus nine glob-scoped on-demand rule files, loaded by pi-rules / GitHub-Copilot-style instructions when matching files are read.

Problem

  • backend/AGENTS.md carried both the normative core rules and large reference material (local dev runtime, AI import tables, integration-test troubleshooting, guardrail job details, domain-model digest). Everything was paid for in every session's context — even for trivial questions — and the descriptive content was also the primary drift hazard (it paraphrases state that lives in code, ADRs and specs).
  • The section comments say AGENTS.md section numbers (§1–§5, §7) are referenced by source-code comments and CI workflows, so a naive restructure would orphan those references.

Solution

Split along the two content types: normative (invariants, hard rules, workflow contract) stays in AGENTS.md as one-liners with enforcement pointers and exception syntax; descriptive long-form material moves into instruction files whose globs scope them to matching file reads.

  • backend/AGENTS.md (48.7 KB → ~12 KB): core rules only (CQRS boundary, no-panics, no string-interpolated SQL, AUTHZ-GATE, ADR-031 error surface, reliability lessons, handoff/task-spec checklist, kameo_es example, licensing). Section numbering and titles unchanged (§1–§5, §7 — referenced by CI workflows and source comments). New §6 reference register documents the instruction files, their glob scopes and the discovery semantics.
  • backend/.github/instructions/ (backend-relative globs, fire for backend/** reads from any session cwd): architecture-hard-rules (long-form rationale of the hard rules), domain-model (hierarchy/aggregate invariants), photo-context, ai-import, integration-tests, local-dev-runtime, ci-hardening (ast-grep rule/job detail).
  • Monorepo-root .github/instructions/ (repo-level globs): workflow-hardening (SHA-pinning, Dependabot, script-injection hygiene for .github/workflows/**) and local-dev-root (pointer rule for root scripts/** / .env*).
  • New root AGENTS.md (2.7 KB area dispatcher): monorepo-level dispatch table to backend/AGENTS.md (slimmed) and frontend-flutter/AGENTS.md (explicitly marked not-yet-reworked), cross-cutting rules (license/SPDX tooling, backend/openapi.yaml wire-contract artifact, gitleaks, workflow hardening), and the pi context-loading semantics explanation.

Content integrity

Every moved passage was verified line-exact against the previous file (only intentionally compressed §1/§3/§4 bullet headers and renamed headings differ), so no rule or reference material is lost. Hard rules in particular remain fully intact in AGENTS.md so they stay visible even without any rule-loading extension.

Enforcement note (pi-rules monorepo support)

pi-rules resolves the project root per read file (innermost project marker), which in this Cargo workspace stops the walk at the member crate — workspace-level instructions never fired for crates/** reads. That upstream limitation is fixed by code-yeongyu/pi-rules#31; until it is merged and installed, the backend-level instructions fire only for files directly under backend/ (scripts, rules, compose, env) and the root-level rules for repo-level files.

Testing

  • Docs-only change: no Rust code touched (git diff main...HEAD --stat confirms only .md files).
  • All glob patterns validated against the loader's actual match semantics (picomatch, project-relative and scope-relative path bases), including negative cases (e.g. crates/**/*.rs rules do not match Cargo.toml, workflow rules do not match crate files).
  • End-to-end verified in headless pi sessions with the patched loader: reading backend/crates/core/src/lib.rs injects architecture-hard-rules + domain-model + backend/AGENTS.md (once per session); reading backend/crates/infra/src/photo/sagas/mod.rs adds photo-context; reading backend/rules/cqrs-boundary.yml injects only ci-hardening; repo-root AGENTS.md is natively deduplicated.
  • Pre-commit hooks (gitleaks, commit format) passed on all four commits.

Follow-ups


Co-authored-by: glm-5.3 (neuralwatt)

pi agent added 4 commits September 3, 2026 06:54
Slim the always-loaded AGENTS.md from 48.7 KB (~12k tokens per session)
to ~12 KB by moving long-form rationale and reference material into
glob-scoped instruction files under backend/.github/instructions/
(loaded on demand by pi-rules / GitHub Copilot instructions).

What stays in AGENTS.md (normative core, always loaded):
- hard rules as one-liners with enforcement pointers and exception
  syntax (CQRS boundary, no-panics, no string-interpolated SQL,
  AUTHZ-GATE, ADR-031 error surface, reliability lessons)
- handoff/task-spec review checklist (unchanged)
- kameo_es code example, licensing, UUIDv7 rule

What moved (verbatim, verified line-by-line):
- hard-rule long forms        -> architecture-hard-rules.instructions.md
- domain model digest         -> domain-model.instructions.md
- photo bounded context + env -> photo-context.instructions.md
- AI import runtime/GC/recovery -> ai-import.instructions.md
- integration-test docs/gotchas -> integration-tests.instructions.md
- local dev runtime/OIDC      -> local-dev-runtime.instructions.md
- CI guardrail job detail    -> ci-hardening.instructions.md

Section numbering and titles (§1-§5, §7) are kept stable because
source-code comments and CI workflows reference them. A new §6
reference register documents the instruction files and their glob
scopes. Rule bodies stay under the pi-rules 12k-char per-rule cap;
glob matching was validated against the extension's picomatch
semantics (project root = backend/).

Co-authored-by: glm-5.3 (neuralwatt)
…ions

pi-rules discovers project rule directories from the directory it walks
up from the pi session cwd to the project root. Sessions launched from
the monorepo root therefore only see `.github/instructions/` at the
repo root, not `backend/.github/instructions/` — move the seven rule
files accordingly (also the correct location for GitHub Copilot
instruction files).

Glob patterns are now repo-root-relative (`backend/crates/**/...`,
`.github/workflows/**`, ...), and the local-dev rule additionally
matches the root-level `scripts/**` (seed-logto-dev.sh) and root
`.env*`. ci-hardening now anchors workflow/dependabot globs at the
repo root and covers `backend/rules/**`/`backend/rules-tests/**`.
backend/AGENTS.md pointers now reference `../.github/instructions/`
and the §6 register documents the repo-root location plus the
dynamic-only nature of glob rules (/rules lists always-on rules only).
Glob matching re-validated against the extension's picomatch
semantics.

Caveat documented in AGENTS.md §6: sessions launched from `backend/`
(project root = backend/ due to the Cargo.toml marker) do not discover
the root instructions — launch pi from the repo root for full rule
coverage.

Co-authored-by: glm-5.3 (neuralwatt)
Root-launched pi sessions did not load any AGENTS.md (pi loads context
files only from the session cwd and its parents, never from
subdirectories), so backend core rules were invisible when launching
from the repo root. Add a minimal root AGENTS.md that:

- dispatches to the area files before code is generated:
  backend/AGENTS.md (slimmed core + on-demand instructions via
  .github/instructions/) and frontend-flutter/AGENTS.md (explicitly
  marked as not yet reworked / comprehensive long-form, candidate for
  the same core/instructions split), including the pi-context
  boundary (a backend session does not auto-load the Flutter file
  and vice versa);
- carries the cross-cutting rules (AGPL-3.0 + SPDX/Co-authored-by
  tooling, openapi.yaml as wire-contract artifact, gitleaks, workflow
  hardening applied to every workflow);
- points to the pi-rules instruction register and documents the
  repo-root launch requirement plus the static/dynamic /rules
  distinction.

At 2.7 KB it stays small on purpose: it is the only file loaded in
every session of the monorepo (root, backend and frontend cwd alike).

Co-authored-by: glm-5.3 (neuralwatt)
pi-rules resolves the project root per read file via the first project
marker walking up (per-crate Cargo.toml in a workspace), so:

- backend-scoped rules live in backend/.github/instructions/ with
  backend-relative globs (crates/**/*.rs, rules/**, docker-compose*,
  scripts/**, .env*, dev-certs/**) — they fire for backend reads
  from any session launch directory;
- repo-level rules stay in the monorepo root .github/instructions/
  (workflow-hardening for .github/workflows/** + dependabot, and
  local-dev-root as pointer rule for root scripts/ and .env*);
- ci-hardening was split accordingly: workflow hardening
  (SHA-pinning, script-injection hygiene) at the root, ast-grep
  guardrail rule detail at the backend level (rules/**,
  rules-tests/**, scripts/**).

backend/AGENTS.md pointers and the §6 register document the
per-target project-root semantics; the root AGENTS.md §3 explains
the two-level layout and recommends backend/ launches for
backend-heavy sessions.

Verified empirically (headless pi from the repo root): reading
backend/crates/core/src/lib.rs now injects architecture-hard-rules,
domain-model and backend/AGENTS.md (once per session, not per file);
reading backend/crates/infra/src/photo/sagas/mod.rs additionally
injects photo-context; reading backend/rules/cqrs-boundary.yml
injects only ci-hardening.

Co-authored-by: glm-5.3 (neuralwatt)
@coderabbitai

coderabbitai Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Team

Run ID: 2c839ecb-33f8-43af-a35f-6155963393a3


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@tradem tradem self-assigned this Sep 3, 2026
The add-spdx-headers.sh tooling covers .rs/.typ/.dart/.sh/.feature/.ya?ml
but not .md, so the nine new instruction files shipped without license
headers in the previous commits. Add the standard AGPL-3.0 header block
(HTML comments, per the AGENTS.md convention) directly after the YAML
frontmatter so the pi-rules parser still recognizes the frontmatter at
file start — a header before it would degrade the rule to plain body
text and stop it from ever matching. Verified live: the rules still
inject on matching reads and the license header travels with the
injected body.

Co-authored-by: glm-5.3 (neuralwatt)
@tradem

tradem commented Sep 3, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai approve

@coderabbitai

coderabbitai Bot commented Sep 3, 2026

Copy link
Copy Markdown
✅ Action performed

Comments resolved and changes approved.

@tradem
tradem merged commit 30d7186 into main Sep 3, 2026
10 checks passed
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