GFramework/AGENTS.md
GeWuYou ba4ee24ce7 docs(guidelines): 更新 AGENTS.md 和 CLAUDE.md 文档内容
- 重写 AGENTS.md 为 AI 代理和贡献者提供完整的编码行为准则
- 新增详细的注释规则,包括 XML 文档、内联注释和架构级注释要求
- 完善代码风格指南,涵盖命名约定、格式化和 C# 最佳实践
- 扩展测试要求,明确覆盖率、组织结构和执行期望
- 添加安全规则和文档规范,确保代码质量和安全性
- 重构 CLAUDE.md 为 AI 代理提供项目理解指导
- 更新模块依赖图和架构模式说明,澄清各层职责
- 补充源码生成器、测试框架和文档结构的详细说明
- 优化项目概述和设计意图描述,提升 AI 理解准确性
2026-03-21 11:36:39 +08:00

8.3 KiB

AGENTS.md

This document is the single source of truth for coding behavior in this repository.

All AI agents and contributors must follow these rules when writing, reviewing, or modifying code in GFramework.

Commenting Rules (MUST)

All generated or modified code MUST include clear and meaningful comments where required by the rules below.

XML Documentation (Required)

  • All public, protected, and internal types and members MUST include XML documentation comments (///).
  • Use <summary>, <param>, <returns>, <exception>, and <remarks> where applicable.
  • Comments must explain intent, contract, and usage constraints instead of restating syntax.
  • If a member participates in lifecycle, threading, registration, or disposal behavior, document that behavior explicitly.

Inline Comments

  • Add inline comments for:
    • Non-trivial logic
    • Concurrency or threading behavior
    • Performance-sensitive paths
    • Workarounds, compatibility constraints, or edge cases
    • Registration order, lifecycle sequencing, or generated code assumptions
  • Avoid obvious comments such as // increment i.

Architecture-Level Comments

  • Core framework components such as Architecture, Module, System, Context, Registry, Service Module, and Lifecycle types MUST include high-level explanations of:
    • Responsibilities
    • Lifecycle
    • Interaction with other components
    • Why the abstraction exists
    • When to use it instead of alternatives

Source Generator Comments

  • Generated logic and generator pipelines MUST explain:
    • What is generated
    • Why it is generated
    • The semantic assumptions the generator relies on
    • Any diagnostics or fallback behavior

Complex Logic Requirement

  • Methods with non-trivial logic MUST document:
    • The core idea
    • Key decisions
    • Edge case handling, if any

Quality Rules

  • Comments MUST NOT be trivial, redundant, or misleading.
  • Prefer explaining why and when, not just what.
  • Code should remain understandable without requiring external context.
  • Prefer slightly more explanation over too little for framework code.

Enforcement

  • Missing required documentation is a coding standards violation.
  • Code that does not meet the documentation rules is considered incomplete.

Code Style

Language and Project Settings

  • Follow the repository defaults:
    • ImplicitUsings disabled
    • Nullable enabled
    • GenerateDocumentationFile enabled for shipped libraries
    • LangVersion is generally preview in the main libraries and abstractions
  • Do not rely on implicit imports. Declare every required using explicitly.
  • Write null-safe code that respects nullable annotations instead of suppressing warnings by default.

Naming and Structure

  • Use the namespace pattern GFramework.{Module}.{Feature} with PascalCase segments.
  • Follow standard C# naming:
    • Types, methods, properties, events, and constants: PascalCase
    • Interfaces: I prefix
    • Parameters and locals: camelCase
    • Private fields: _camelCase
  • Keep abstractions projects free of implementation details and engine-specific dependencies.
  • Preserve existing module boundaries. Do not introduce new cross-module dependencies without clear architectural need.

Formatting

  • Use 4 spaces for indentation. Do not use tabs.
  • Use Allman braces.
  • Keep using directives at the top of the file and sort them consistently.
  • Separate logical blocks with blank lines when it improves readability.
  • Prefer one primary type per file unless the surrounding project already uses a different local pattern.
  • Keep line length readable. Around 120 characters is the preferred upper bound.

C# Conventions

  • Prefer explicit, readable code over clever shorthand in framework internals.
  • Match existing async patterns and naming conventions (Async suffix for asynchronous methods).
  • Avoid hidden side effects in property getters, constructors, and registration helpers.
  • Preserve deterministic behavior in registries, lifecycle orchestration, and generated outputs.
  • When adding analyzers or suppressions, keep them minimal and justify them in code comments if the reason is not obvious.

Analyzer and Validation Expectations

  • The repository uses Meziantou.Analyzer; treat analyzer feedback as part of the coding standard.
  • Naming must remain compatible with scripts/validate-csharp-naming.sh.

Testing Requirements

Required Coverage

  • Every non-trivial feature, bug fix, or behavior change MUST include tests or an explicit justification for why a test is not practical.
  • Public API changes must be covered by unit or integration tests.
  • Regression fixes should include a test that fails before the fix and passes after it.

Test Organization

  • Mirror the source structure in test projects whenever practical.
  • Reuse existing architecture test infrastructure when relevant:
    • ArchitectureTestsBase<T>
    • SyncTestArchitecture
    • AsyncTestArchitecture
  • Keep tests focused on observable behavior, not implementation trivia.

Source Generator Tests

  • Source generator changes MUST be covered by generator tests.
  • Preserve snapshot-based verification patterns already used in the repository.
  • When generator behavior changes intentionally, update snapshots together with the implementation.

Validation Commands

Use the smallest command set that proves the change, then expand if the change is cross-cutting.

# Build the full solution
dotnet build GFramework.sln -c Release

# Run all tests
dotnet test GFramework.sln -c Release

# Run a single test project
dotnet test GFramework.Core.Tests -c Release
dotnet test GFramework.Game.Tests -c Release
dotnet test GFramework.SourceGenerators.Tests -c Release
dotnet test GFramework.Ecs.Arch.Tests -c Release

# Run a single NUnit test or test group
dotnet test GFramework.Core.Tests -c Release --filter "FullyQualifiedName~CommandExecutorTests.Execute"

# Validate naming rules used by CI
bash scripts/validate-csharp-naming.sh

Test Execution Expectations

  • Run targeted tests for the code you changed whenever possible.
  • Run broader solution-level validation for changes that touch shared abstractions, lifecycle behavior, source generators, or dependency wiring.
  • Do not claim completion if required tests were skipped; state what was not run and why.

Security Rules

  • Validate external or user-controlled input before it reaches file system, serialization, reflection, code generation, or process boundaries.
  • Do not build command strings, file paths, type names, or generated code from untrusted input without strict validation or allow-listing.
  • Avoid logging secrets, tokens, credentials, or machine-specific sensitive data.
  • Keep source generators deterministic and free of hidden environment or network dependencies.
  • Prefer least-privilege behavior for file, process, and environment access.
  • Do not introduce unsafe deserialization, broad reflection-based activation, or dynamic code execution unless it is explicitly required and tightly constrained.
  • When adding caching, pooling, or shared mutable state, document thread-safety assumptions and failure modes.
  • Minimize new package dependencies. Add them only when necessary and keep scope narrow.

Documentation Rules

Code Documentation

  • Any change to public API, lifecycle semantics, module behavior, or extension points MUST update the related XML docs.
  • If a framework abstraction changes meaning or intended usage, update the explanatory comments in code as part of the same change.

Repository Documentation

  • Update the relevant README.md or docs/ page when behavior, setup steps, architecture guidance, or user-facing examples change.
  • The main documentation site lives under docs/, with Chinese content under docs/zh-CN/.
  • Keep code samples, package names, and command examples aligned with the current repository state.
  • Prefer documenting behavior and design intent, not only API surface.

Documentation Preview

When documentation changes need local preview, use:

cd docs && bun install && bun run dev

Review Standard

Before considering work complete, confirm:

  • Required comments and XML docs are present
  • Code follows repository style and naming rules
  • Relevant tests were added or updated
  • Sensitive or unsafe behavior was not introduced
  • User-facing documentation is updated when needed