Skip to content

Commit fdd8f7f

Browse files
authored
Merge pull request InsightSoftwareConsortium#5986 from hjmjohnson/ai-commit-and-pr-generation
DOC: Add routing to AGENTS.md to minimize task based context, add AI guidance
2 parents 2b7e6d4 + 32cdd4b commit fdd8f7f

File tree

9 files changed

+525
-294
lines changed

9 files changed

+525
-294
lines changed

AGENTS.md

Lines changed: 25 additions & 294 deletions
Original file line numberDiff line numberDiff line change
@@ -1,307 +1,38 @@
11
# ITK AI Agent Guide
22

3-
ITK (Insight Toolkit) is a cross-platform, open-source toolkit for N-dimensional scientific image processing, segmentation, and registration. This guide helps AI agents navigate ITK's unique architecture and development workflows.
3+
The Insight Toolkit (ITK) is a cross-platform, open-source C++ toolkit for
4+
N-dimensional scientific image processing, segmentation, and registration.
5+
Apache 2.0 licensed. Build tool: **Pixi** (wraps CMake + Ninja).
46

5-
## Architecture Overview
7+
## Context to Load on Demand
68

7-
### Modular Structure
8-
ITK uses a **module-based architecture** where each module is a self-contained unit with dependencies. Modules are organized in:
9-
- `Modules/Core/` - Essential classes (Image, Region, Point, Vector, pipeline infrastructure)
10-
- `Modules/Filtering/` - Image processing filters
11-
- `Modules/IO/` - Image, mesh, and transform I/O for various formats (JPEG, PNG, NIFTI, DICOM, etc.)
12-
- `Modules/Registration/` - Image registration algorithms
13-
- `Modules/Segmentation/` - Segmentation algorithms
14-
- `Modules/Numerics/` - Optimization and numerical methods
15-
- `Modules/ThirdParty/` - Vendored dependencies (HDF5, JPEG, TIFF, VNL, Eigen3)
16-
- `Modules/Bridge/` - Bridges to VTK, NumPy
17-
- `Modules/Remote/` - External modules fetched during build
9+
Load only what your task requires:
1810

19-
Each module has an `itk-module.cmake` file declaring dependencies:
20-
```cmake
21-
itk_module(ITKCommon
22-
DEPENDS ITKEigen3 ITKVNL
23-
PRIVATE_DEPENDS ITKDoubleConversion
24-
COMPILE_DEPENDS ITKKWSys
25-
TEST_DEPENDS ITKTestKernel ITKMesh
26-
DESCRIPTION "Core ITK classes..."
27-
)
28-
```
11+
| Task | Read |
12+
|------|------|
13+
| Understanding the codebase layout | [./Documentation/AI/architecture.md](./Documentation/AI/architecture.md) |
14+
| Building or configuring ITK | [./Documentation/AI/building.md](./Documentation/AI/building.md) |
15+
| Writing or running tests | [./Documentation/AI/testing.md](./Documentation/AI/testing.md) |
16+
| Code style, naming conventions | [./Documentation/AI/style.md](./Documentation/AI/style.md) |
17+
| Writing ITK C++ classes, CMake build configuration, and Python wrapping configuration | [./Documentation/AI/conventions.md](./Documentation/AI/conventions.md) |
18+
| Creating a git commit | [./Documentation/AI/git-commits.md](./Documentation/AI/git-commits.md) |
19+
| Opening or updating a pull request | [./Documentation/AI/pull-requests.md](./Documentation/AI/pull-requests.md) |
2920

30-
### Template-Heavy C++ Design
31-
ITK extensively uses **C++ templates** and **generic programming** for:
32-
- Dimension-agnostic code (2D, 3D, nD images)
33-
- Type flexibility (pixel types: unsigned char, float, RGB, etc.)
34-
- Compile-time polymorphism via traits and policy classes
21+
## Critical Pitfalls
3522

36-
Example: `itk::Image<TPixel, VImageDimension>` where both parameters are compile-time constants.
37-
38-
### Pipeline Architecture
39-
ITK uses a **data pipeline** pattern:
40-
- **Process objects** (`itk::ProcessObject`, e.g. most filters) perform computations
41-
- **Data objects** (`itk::DataObject`, e.g., `itk::Image`) store data
42-
- Filters connect via `SetInput()` / `GetOutput()` and execute lazily with `Update()`
43-
- Smart pointers (`itk::SmartPointer<T>`) handle memory management
44-
45-
### Python Wrapping
46-
ITK provides **Python wrappers** via SWIG:
47-
- Wrapping configs in `Wrapping/` directory
48-
- Explicit template instantiations in `wrapping/` subdirectories of modules
49-
- Python package structure mirrors C++ modules
50-
- Snake_case functions available (e.g., `itk.median_image_filter()`)
51-
- Install via `pip install itk` or build with `ITK_WRAP_PYTHON=ON`
52-
53-
## Build System
54-
55-
### CMake Configuration
56-
ITK requires CMake 3.22.1+. Key build options:
57-
58-
**Essential:**
59-
```bash
60-
cmake -B build -S . \
61-
-DCMAKE_BUILD_TYPE=Release \
62-
-DITK_BUILD_DEFAULT_MODULES=ON \
63-
-DBUILD_TESTING=ON \
64-
-DBUILD_EXAMPLES=OFF
65-
```
66-
67-
**Python wrapping:**
68-
```bash
69-
cmake -B build-python -S . \
70-
-DITK_WRAP_PYTHON=ON \
71-
-DITK_WRAP_unsigned_short=ON \
72-
-DITK_WRAP_IMAGE_DIMS="2;3;4"
73-
```
74-
75-
**Module selection:**
76-
- `Module_<ModuleName>=ON` to enable specific modules
77-
- `ITK_BUILD_DEFAULT_MODULES=ON` builds standard modules
78-
- Use `find_package(ITK COMPONENTS ITKCommon ITKIOImageBase)` in external projects
79-
80-
### Building
81-
```bash
82-
# Build via Pixi (recommended for development)
83-
pixi run --as-is build # Build C++ tests
84-
pixi run --as-is build-python # Build Python tests
85-
86-
# Run tests via Pixi (recommended for development)
87-
pixi run --as-is test # C++ tests
88-
pixi run --as-is test-python # Python tests
89-
```
90-
91-
For an interactive shell with ITK environment:
92-
```bash
93-
pixi shell -e cxx # C++ development
94-
pixi shell -e python # Python development
95-
```
96-
97-
### Module Dependencies
98-
ITK automatically resolves module dependencies via CMake. The module DAG is loaded from `itk-module.cmake` files. To find required modules for code:
99-
```bash
100-
python Utilities/Maintenance/WhatModulesITK.py /path/to/ITK/source file1.cxx file2.h
101-
```
102-
103-
## Testing
104-
105-
### CTest Integration
106-
Tests use **CTest** framework:
107-
```bash
108-
cd build
109-
ctest -j8 # Run all tests in parallel
110-
ctest -R ImageFilter # Run tests matching regex
111-
ctest -L REQUIRES_GPU # Run tests with label
112-
ctest --rerun-failed # Rerun only failed tests
113-
```
114-
115-
### Test Organization
116-
Each module has a `test/` directory with:
117-
- **CTest tests**: Defined via `itk_add_test()` macro
118-
- **GTest tests (preferred)**: Modern C++ tests using `creategoogletestdriver()`
119-
- **Baseline images**: Stored via `ExternalData` (downloaded on demand)
120-
121-
Example test definition:
122-
```cmake
123-
itk_add_test(NAME itkImageTest
124-
COMMAND ITKCommonTestDriver itkImageTest
125-
DATA{Input/image.png}
126-
${ITK_TEST_OUTPUT_DIR}/output.png
127-
)
128-
```
129-
130-
### ExternalData System
131-
Large test data is **not stored in Git**. Instead, ITK uses `ExternalData` with content hashes:
132-
- `DATA{path/to/file.png}` references data by hash
133-
- Data downloaded from `https` resources during build
134-
- Test data uploaded separately for new tests
135-
136-
## Development Workflow
137-
138-
### Setup for Development
139-
**First time only:**
140-
```bash
141-
./Utilities/SetupForDevelopment.sh
142-
```
143-
This configures:
144-
- Git hooks (pre-commit, commit-msg)
145-
- clang-format integration (auto-formats C++ on commit)
146-
- KWStyle configuration
147-
- GitHub remote setup
148-
149-
### Code Style
150-
151-
**Automatic C++ formatting:**
152-
ITK enforces `.clang-format` style automatically via pre-commit hook. To format manually:
153-
```bash
154-
Utilities/Maintenance/clang-format.bash --modified # Format modified files
155-
```
156-
157-
**Key style rules:**
158-
- Use `clang-format` 19.1.7 (enforced by pre-commit)
159-
- `constexpr` instead of `#define` for constants
160-
- Smart pointers for all ITK objects: `auto image = ImageType::New();`
161-
- American English spelling
162-
- Doxygen comments with `\` (backslash) style: `\class`, `\brief`
163-
- No `using namespace` in headers
164-
165-
**Naming conventions:**
166-
- Classes: `PascalCase` (e.g., `MedianImageFilter`)
167-
- Variables: `camelCase` or `lowercase` with no underscores
168-
- Member variables: `m_MemberVariable` prefix
169-
- Template parameters: `TInputImage`, `TOutputImage` prefix
170-
- Macros: `ITK_UPPERCASE_MACRO`
171-
172-
### Commit Guidelines
173-
**Required format** (enforced by `kw-commit-msg.py` hook):
174-
```
175-
PREFIX: Brief description (≤78 chars)
176-
177-
Longer explanation if needed. Reference issues or features.
178-
```
179-
180-
Common prefixes: `ENH:`, `BUG:`, `COMP:`, `DOC:`, `STYLE:`, `PERF:`, `WIP:`
181-
182-
### CI/CD
183-
ITK uses:
184-
- **Azure Pipelines** for Linux, Windows, macOS builds (C++ and Python)
185-
- **GitHub Actions** for Pixi builds and Apple Silicon
186-
- **CDash** dashboards at https://open.cdash.org/index.php?project=Insight
187-
188-
## Key Conventions
189-
190-
### ITK-Specific Patterns
191-
192-
**Object creation:**
193-
```cpp
194-
auto filter = FilterType::New(); // Factory method, returns SmartPointer
195-
filter->SetInput(image);
196-
filter->Update(); // Lazy evaluation
197-
auto output = filter->GetOutput();
198-
```
199-
200-
**Image iteration:**
201-
Use **iterators** (not raw buffers) for image traversal:
202-
```cpp
203-
itk::ImageRegionIterator<ImageType> it(image, region);
204-
for (; !it.IsAtEnd(); ++it) {
205-
it.Set(it.Get() * 2);
206-
}
207-
```
208-
209-
**Macros for class boilerplate:**
210-
```cpp
211-
using Self = MyClass;
212-
using Superclass = BaseClass;
213-
using Pointer = SmartPointer<Self>;
214-
itkNewMacro(Self); // Provides New() method
215-
itkTypeMacro(Self, Superclass); // RTTI support
216-
itkSetMacro(Radius, unsigned int); // Generates SetRadius()
217-
itkGetConstMacro(Radius, unsigned int); // Generates GetRadius()
218-
```
219-
220-
### File Organization
221-
Each module follows:
222-
```
223-
ModuleName/
224-
├── itk-module.cmake # Module metadata
225-
├── include/ # Public headers (.h)
226-
│ ├── itkClassName.h
227-
│ └── itkClassName.hxx # Template implementations
228-
├── src/ # Non-template implementations (.cxx)
229-
│ └── itkClassName.cxx # Non-template implementations
230-
├── test/ # Tests
231-
│ ├── CMakeLists.txt
232-
│ └── itkClassNameTest.cxx
233-
└── wrapping/ # Python wrapping (if applicable)
234-
└── itkClassName.wrap
235-
```
236-
237-
**Header structure:**
238-
- `.h` files: Class declarations
239-
- `.hxx` files: Template method implementations (included at end of `.h`)
240-
- `.cxx` files: Non-template implementations compiled into libraries
241-
242-
### Third-Party Code
243-
Third-party libraries in `Modules/ThirdParty/` are **subtrees**, not submodules:
244-
- Updated via `git subtree pull`
245-
- Maintained upstream separately
246-
- Wrapped with ITK CMake logic
247-
248-
## External Module Development
249-
250-
To create a remote module:
251-
1. Create `itk-module.cmake` with dependencies
252-
2. Create `MyModule.remote.cmake` in ITK's `Modules/Remote/`
253-
3. Use `itk_module_impl()` macro in module's CMakeLists.txt
254-
4. Test as external build:
255-
```bash
256-
cmake -B build-external -S path/to/module \
257-
-DITK_DIR=/path/to/ITK-build
258-
```
259-
260-
## Common Pitfalls
261-
262-
1. **Template compilation errors**: ITK's heavy template use causes verbose errors. Focus on the **first error** in the output.
263-
264-
2. **Python wrapping**: Not all C++ types are wrapped. Check `wrapping/` directories for available instantiations. Common wrapped types: `F` (float), `D` (double), `UC` (unsigned char), `US` (unsigned short).
265-
266-
3. **Memory management**: Always use `SmartPointer`. Never `delete` ITK objects manually.
267-
268-
4. **Update() calls**: Filters don't execute until `Update()` is called. Changes to filter parameters after `Update()` require another `Update()`.
269-
270-
5. **Module dependencies**: If code fails to link, check `itk-module.cmake` dependencies. Use `DEPENDS` for public deps, `PRIVATE_DEPENDS` for implementation deps.
23+
1. **Template errors are verbose** — focus on the *first* error only.
24+
2. **Python wrapping is incomplete** — check `wrapping/` dirs for available types before assuming a class is wrapped.
25+
3. **Never `delete` ITK objects** — always use `SmartPointer` (`auto filter = FilterType::New()`).
26+
4. **`Update()` is required** — filters don't execute until called; parameter changes after `Update()` need another call.
27+
5. **Link errors → check `itk-module.cmake`** — missing `DEPENDS` or `PRIVATE_DEPENDS` is the usual cause.
28+
6. **Licensing** — verify AI output does not reproduce third-party code in conflict with Apache 2.0.
27129

27230
## Resources
27331

274-
- Main docs: https://docs.itk.org/
275-
- Discourse forum: https://discourse.itk.org/
32+
- Docs: https://docs.itk.org/
33+
- Contribution guide: https://docs.itk.org/en/latest/contributing/
34+
- Discourse: https://discourse.itk.org/
27635
- Software Guide: https://itk.org/ItkSoftwareGuide.pdf
27736
- Examples: https://examples.itk.org/
27837
- Doxygen API: https://itk.org/Doxygen/html/
279-
- Contribution guide: https://docs.itk.org/en/latest/contributing/
280-
281-
## Quick Reference
282-
283-
**Find class documentation:**
284-
```bash
285-
# Search Doxygen locally after building docs
286-
cmake -DITK_BUILD_DOCUMENTATION=ON ..
287-
```
288-
289-
**Test a single module:**
290-
```bash
291-
ctest -L ITKCommon
292-
```
293-
294-
**Build only specific modules:**
295-
```cmake
296-
set(Module_ITKCommon ON)
297-
set(Module_ITKIOImageBase ON)
298-
set(ITK_BUILD_DEFAULT_MODULES OFF)
299-
```
300-
301-
**Python quick test:**
302-
```python
303-
import itk
304-
image = itk.imread('input.png')
305-
smoothed = itk.median_image_filter(image, radius=2)
306-
itk.imwrite(smoothed, 'output.png')
307-
```
38+
- CDash: https://open.cdash.org/index.php?project=Insight

Documentation/AI/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Documentation/AI — Focused Context Files for AI Agents
2+
3+
This directory contains task-scoped context files loaded on demand by AI agents.
4+
They are the second layer of a two-layer agent guidance system:
5+
6+
```
7+
AGENTS.md ← Layer 1: routing layer (always loaded)
8+
Documentation/AI/*.md ← Layer 2: focused context (loaded on demand)
9+
```
10+
11+
## Design Principle
12+
13+
`AGENTS.md` is intentionally minimal. It contains only:
14+
- A routing table mapping task types to files in this directory
15+
- Non-discoverable critical information every agent needs immediately
16+
(pitfalls, AI contribution policy, resource URLs)
17+
18+
Everything else lives here — detailed enough to be useful, scoped enough to
19+
avoid loading irrelevant context into the agent's working window.
20+
21+
## Existing Files
22+
Add file to table in AGENTS.md for routing:
23+
24+
| File | When to load |
25+
|------|-------------|
26+
| `building.md` | Configuring or building ITK with Pixi or CMake |
27+
| `testing.md` | Writing, running, or converting tests |
28+
29+
## Adding a New Context File
30+
31+
1. **Identify the task boundary.** A file should cover one coherent task an
32+
agent might perform (e.g., "profiling", "Python wrapping", "remote modules").
33+
If it covers two unrelated topics, split it.
34+
35+
2. **Include only non-discoverable content.** Ask: can an agent learn this
36+
by reading the source tree, `pixi.toml`, or CMakeLists.txt? If yes, omit
37+
it. Include only conventions, landmines, and non-obvious workflows that
38+
aren't expressed in code.
39+
40+
3. **Name the file descriptively** using lowercase with hyphens:
41+
`python-wrapping.md`, `remote-modules.md`, `performance-profiling.md`.
42+
43+
4. **Structure the file** with a single H1 title and H2 sections. Keep it
44+
under ~100 lines. If it grows larger, consider splitting.
45+
46+
5. **Register it in `AGENTS.md`** by adding a row to the routing table:
47+
48+
```markdown
49+
| Task description | `Documentation/AI/your-new-file.md` |
50+
```
51+
52+
The task description should match how an agent would naturally describe
53+
what it is trying to do — not a file name or category label.
54+
55+
6. **Keep it current.** Context files that describe workflows or commands
56+
drift as the codebase evolves. Update them when the underlying reality
57+
changes; stale guidance is worse than no guidance.
58+
59+
## What Does Not Belong Here
60+
61+
- Content already documented in `CONTRIBUTING.md`, `GettingStarted.md`,
62+
or upstream docs (link to those instead)
63+
- Step-by-step tutorials (use the ITK Software Guide or examples.itk.org)
64+
- Ephemeral state: active branches, in-progress bugs, who is working on what
65+
- Code snippets that duplicate what is already in the source tree

0 commit comments

Comments
 (0)