Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 44 additions & 48 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# AGENTS.md
# AGENTS.md: the CGP library

Read this file before working in this repository. The
[knowledge base's AGENTS.md](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/AGENTS.md)
carries the canonical wording of the universal rules. Change a universal rule there first, then
update this block to match.
Follow these rules for every change to this repository. Read [README.md](README.md) for how the
crates are organized, and [sibling-projects.md](sibling-projects.md) for the related repositories.

## Universal rules

These rules hold in every CGP repository. The
[knowledge base's AGENTS.md](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/AGENTS.md)
holds their canonical wording: read it first, and change a rule there before updating this block:

- Learn CGP and its tools from the `/cgp` skill and the knowledge base; never restate them in this
file.
- Prefer the local sibling checkout when reading another project.
- Write a cross-project link as a GitHub URL on `main`.
- Name official release versions only, never a pre-release.
Expand All @@ -16,24 +20,41 @@ update this block to match.
- Ask the user when a decision is ambiguous.
- Apply point-first writing to internal prose.

## Versions
## Task guides

Each kind of task has a guide that holds its rules. Read the guide before the task:

- Document this tree as v0.8.0.
- Treat v0.7.0 as a separate, older line; do not reconcile this tree's syntax against it.
- **Changing a macro crate:** [crates/macros/README.md](crates/macros/README.md).
- **Adding, moving, or refactoring a test, or adding a failure case:**
[crates/tests/README.md](crates/tests/README.md#rules-for-the-test-suite).
- **Reviewing a macro:** the knowledge base's
[cgp/implementation/review.md](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/review.md),
as [Macro review workflow](#macro-review-workflow) says.
- **Adding a fixture for a post-codegen failure:** `cargo-cgp`'s
[tests/README.md](https://github.com/contextgeneric/cargo-cgp/blob/main/tests/README.md).
- **Writing a reference or implementation document:** the knowledge base's
[cgp/writing-guide.md](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/writing-guide.md)
and
[cgp/implementation/writing-guide.md](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/writing-guide.md).

## Before any task
## Orient before any task

- Invoke the `/cgp` skill before reading, writing, reviewing, or debugging CGP code.
- Re-invoke it when you move into an unfamiliar construct.
Read what a task touches in the skill and the knowledge base before changing the code:

- Invoke the `/cgp` skill before reading, writing, reviewing, or debugging CGP code, and re-invoke it
when you move into an unfamiliar construct.
- Read the knowledge base's [cgp README](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/README.md),
the [construct reference](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/reference/README.md),
and the [implementation reference](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/README.md)
when the task needs them.
- Find the module that exports a name in the `/cgp` skill's import table or the construct reference
rather than assuming `cgp::prelude` carries it.
- Load the `/point-first-writing` skill before editing Markdown or inline comments.
- Start from `cgp::prelude` and the prelude re-exports when a name is unclear.

## Code rules

The crates stay layered and `no_std`, and each change keeps its docs, snapshots, and checks current:

- Keep every crate `no_std`-compatible unless it already requires `std`; gate `std` and `alloc` behind
features.
- Place functionality in the lowest layer that makes sense and re-export it upward.
Expand All @@ -45,47 +66,22 @@ update this block to match.
- Treat [CHANGELOG.md](CHANGELOG.md) as the record of which macro forms are current and which are
removed.
- Add a `///` to every public item that lacks one, and keep inline docs brief.
- Run the repository's checks before committing; [`.github/workflows/tests.yml`](.github/workflows/tests.yml) lists them, and CI runs the same commands.
- Put a post-codegen compile failure in a `cargo-cgp` UI fixture, not in this repository, and update the
matching fixture when a construct change alters a diagnostic.

## Tests

- A compile-time wiring test passes when the crate compiles; still run the suite for the runtime tests.
- Update the expansion snapshots when macro codegen changes, and review the expanded code.

## Macro review workflow

Review one macro at a time when the user asks for a macro review, and continue until no further issue
is found. Follow the detailed procedure in
[cgp-knowledge-base/cgp/implementation/review.md](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/review.md).

- Orient first: read the macro's reference and implementation documents, its source, and its tests.
- Fix bugs and corner cases; close test gaps; verify each test exercises what it claims; deduplicate
the tests; improve the documentation and inline docs.
- Scrutinize the codegen across attributes, parsing, expansion, generics, hygiene, and spans.
- Capture an accepted input whose expansion fails to compile as a `cargo-cgp` UI fixture, and catalog
the class.
- Keep the reference, implementation documents, snapshots, and skill in sync in the same change.
- Run the format, clippy, and test checks for the affected crates before finishing.
- Ask when a behavior, corner case, or design choice is ambiguous.
- Run the repository's checks before committing;
[`.github/workflows/tests.yml`](.github/workflows/tests.yml) lists them, and CI runs the same
commands.

## Sibling projects

- Read [sibling-projects.md](sibling-projects.md) to find a sibling and the revision to read.
- Use the local checkout first; fetch the file from the repository when it is absent.
- Write a link to another project as a GitHub URL on `main`; keep a bare checkout path relative.
`cgp` sits below its siblings, and a construct change reaches each one that describes it:

- Keep the code dependency one-way: no `cgp` crate may depend on a `cargo-cgp` crate.
- Prefer `cargo-cgp` over raw `cargo check` when diagnosing a wiring failure.
- Propagate a construct change to the matching skill sub-skill and to `cargo-cgp` when it changes a
diagnostic.

## AGENTS.md files
- Propagate a construct change to the matching skill sub-skill, and to `cargo-cgp`'s UI fixtures when
it changes a diagnostic.

- Add a nested `AGENTS.md` only when its directory has significant rules of its own; otherwise extend
this file.

## Committing
## Macro review workflow

- Never commit as a side effect, suggest a commit unprompted, or treat a request as standing
authorization.
Follow the knowledge base's
[cgp/implementation/review.md](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/review.md)
when the user asks for a macro review; it holds the procedure and its rules.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ knowledge base when you need the exact semantics of a construct or are changing
[`/cgp` agent skill](https://contextgeneric.dev/docs/ai/skills/) teaches an assistant to read and
write CGP; it does not remove the reviewer's need to understand the code.

Start from [AGENTS.md](AGENTS.md) for the conventions this repository follows, and
Start from [AGENTS.md](AGENTS.md) for the rules agents follow in this repository, and
[sibling-projects.md](sibling-projects.md) for the related repositories.

## How the project is organized
Expand Down
27 changes: 0 additions & 27 deletions crates/macros/AGENTS.md

This file was deleted.

47 changes: 47 additions & 0 deletions crates/macros/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# CGP macro crates: the procedural macros and their rules

This directory holds the crates that implement CGP's procedural macros. Read the rules below before
changing any of them; the knowledge base's
[implementation documents](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/README.md)
describe each crate's module map, its pipeline, and what each macro generates.

## The crates

The macros are split into a shared core, the implementation of each macro, and the thin crates that
export them:

- **`cgp-macro-core`:** the AST types, parsers, visitors, and code-generation helpers the macro
crates build on. It has no `#[proc_macro]` entrypoints and does not depend on `proc-macro`.
- **`cgp-macro-lib`** and **`cgp-macro`:** the implementation of CGP's macros, one module per macro,
and the crate that exports them.
- **`cgp-macro-extra-core`**, **`cgp-macro-extra-lib`**, and **`cgp-macro-extra`:** the same three
layers for the extra macros `#[cgp_computer]`, `#[cgp_producer]`, and `#[cgp_auto_dispatch]`,
which lower through `cgp-macro-core`'s AST types.
- **`cgp-async-macro`:** `#[async_trait]`, which rewrites a trait's `async fn` to return
`impl Future`.
- **`cgp-macro-test-util-lib`** and **`cgp-macro-test-util`:** the snapshot-testing macros, which
emit the real generated code and pin it as an inline `insta` snapshot; their
[README](cgp-macro-test-util/README.md) describes them.

## Rules for every macro crate

Every macro parses, builds, and emits code the same way, so its expansion stays hygienic and its
errors stay spanned:

- Parse input by implementing `syn::parse::Parse`; emit code by implementing `quote::ToTokens`.
- Build AST nodes with `parse_internal!`, inside a `syn::Result`-returning function.
- Emit every CGP item through an `exports` marker; add a new item to `export_constructs!`.
- Keep the staged `preprocess` → `eval` → `to_items` pipeline.
- Define a custom keyword with `define_keyword!` and `IsKeyword`.
- Rewrite ASTs with `VisitMut` visitors, never with string manipulation.
- Put parser corner cases and rejection cases in `cgp-macro-tests`, and a macro's expansion snapshot
in the `cgp-tests` target that owns its feature, following the
[test suite's rules](../tests/README.md#rules-for-the-test-suite).

## Rules for the extra macros

The extra macros reuse `cgp-macro-core` rather than generating input for the core macros:

- Lower through `cgp-macro-core`'s intermediate representation, not by emitting a nested macro call.
- Reuse `cgp-macro-core`'s `exports` markers rather than declaring a duplicate.
- Build each intermediate node with `parse_internal!`.
2 changes: 1 addition & 1 deletion crates/macros/cgp-macro-test-util/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ changes only the golden assertion, never the compile-time or runtime coverage.
Two documents carry what this README does not. The
[implementation document](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/entrypoints/snapshot_macros.md)
in the knowledge base explains how the macros are built and what an invocation
expands to; [crates/tests/AGENTS.md](../../tests/AGENTS.md) sets the convention for
expands to; the [test suite's rules](../../tests/README.md#snapshots) set the convention for
*when* to snapshot: a macro's expansion is pinned only in the concept target that
owns that macro's feature, and written plainly everywhere else.

Expand Down
24 changes: 0 additions & 24 deletions crates/tests/AGENTS.md

This file was deleted.

45 changes: 40 additions & 5 deletions crates/tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ This directory holds the test suite for Context-Generic Programming, organized *
CGP concept**, such as basic delegation, abstract types, implicit arguments, or namespaces,
rather than by the macro that implements each concept, because a single
macro such as `delegate_components!` serves many concepts at once. This README is
the map of what is here and how to run it; [AGENTS.md](AGENTS.md) is the
authoritative guide to the conventions, and you should read it before adding,
the map of what is here and how to run it, and its
[rules](#rules-for-the-test-suite) are the conventions to follow before adding,
moving, or refactoring a test.

**[Website](https://contextgeneric.dev/) · [Reference](https://contextgeneric.dev/docs/reference/) · [Implementation record](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/implementation/README.md)**
Expand All @@ -31,8 +31,9 @@ provider behind its `alloc` feature.
A fourth category lives in another repository: the cases where a macro *accepts*
input whose *expansion* then fails to compile are UI fixtures in
[`cargo-cgp`](https://github.com/contextgeneric/cargo-cgp/blob/main/tests/README.md),
so each is pinned as the readable error the tool renders for it. AGENTS.md's
"Adding a failure case" says which of the three a new case belongs in.
so each is pinned as the readable error the tool renders for it.
[Adding a failure case](#adding-a-failure-case) says which of the three a new case
belongs in.

## How the tests are laid out

Expand Down Expand Up @@ -89,4 +90,38 @@ macros from `cgp-macro-test-util`: each emits the real generated code into the
module *and* generates a `#[test]` asserting a pretty-printed inline `insta`
snapshot of it. A failing snapshot prints a diff of the generated code, so accept
it with `cargo insta` only after confirming the change is intended. Which target
owns a given macro's snapshot is a convention AGENTS.md sets out.
owns a given macro's snapshot is a convention the [rules](#rules-for-the-test-suite)
below set out.

## Rules for the test suite

Each test sits where its concept's other tests are and states what it checks, so a
reader can find the coverage of a concept in one place:

- Group tests by the CGP concept under test, never by the macro that happens to appear.
- Split a category into finer ones when it grows too large to stay coherent.
- Put each unit test in its own file under the concept directory, self-contained at module scope.
- Open every test file with a brief comment on the behavior it exercises, and link the owning
implementation document.
- Update the implementation document's Tests or Snapshots section in the same change as a test move.
- Run the suite with the commands above, and review every `insta` diff before accepting it.

### Snapshots

A macro's expansion is pinned once, in the target that owns its feature:

- Snapshot a macro only in the concept target that owns its feature; keep the snapshot inline.
- Invoke a macro plainly when it is incidental scaffolding; the owning target already pins its
expansion.

### Adding a failure case

A failure case goes where the failure happens, in one of three places:

- Put an input the macro rejects in `cgp-macro-tests` with `assert_macro_rejects`.
- Put an accepted input whose expansion fails to compile in a `cargo-cgp` UI fixture, filed under its
error class, and catalog the class in the knowledge base's
[error catalog](https://github.com/contextgeneric/cgp-knowledge-base/blob/main/cgp/errors/README.md).
- Put a pin on the exact invalid tokens in the `invalid_expansion` target of `cgp-macro-tests`.
- Check the post-codegen behavior in `cargo-cgp`'s UI suite when a construct change alters an accepted
input or an expansion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
//! 2. add a code comment explaining **why** the output is wrong and **what the
//! correct output should be**;
//! 3. record the limitation in the owning reference document's `## Known issues`
//! section (per cgp-knowledge-base/cgp/AGENTS.md), and link from the test to that document.
//! section (per cgp-knowledge-base/cgp/writing-guide.md), and link from the
//! test to that document.
//!
//! No case is captured at present.
#![allow(dead_code)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! relevant `cgp-macro-core` parser (or a `cgp-macro-lib` entrypoint) returns an
//! error rather than silently accepting the input.
//!
//! See crates/tests/AGENTS.md ("Adding a failure case").
//! See crates/tests/README.md ("Adding a failure case").
#![allow(dead_code)]

pub mod parser_rejections;
4 changes: 2 additions & 2 deletions crates/tests/cgp-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! concept (one target per concept, one file per unit test). This library crate
//! holds the one check that needs a library: `generated_docs`, which denies
//! `missing_docs` so a macro that stops documenting a generated item fails the
//! build. See `../README.md` and `../AGENTS.md` for the organization and the
//! rules for maintaining the suite.
//! build. See `../README.md` for the organization and the rules for
//! maintaining the suite.

pub mod generated_docs;
Loading