|
| 1 | +# Copilot Instructions - Protobuf Schema Repository |
| 2 | + |
| 3 | +## Project Overview |
| 4 | + |
| 5 | +Enterprise-grade Protocol Buffer schemas for multi-tenant microservices using [Buf](https://buf.build) for validation, linting, breaking change detection, and multi-language code generation. This is a **schema-only** repository - no application code, only `.proto` definitions with strict compliance and documentation standards. |
| 6 | + |
| 7 | +## Critical Architecture Patterns |
| 8 | + |
| 9 | +## Development Workflows |
| 10 | + |
| 11 | +### Essential Buf Commands |
| 12 | + |
| 13 | +```bash |
| 14 | +buf lint # MUST pass before commits |
| 15 | +buf format -w # Auto-format all protos |
| 16 | +buf breaking --against '.git#branch=main' # Check compatibility (PRs only) |
| 17 | +buf generate # Generate code for all languages |
| 18 | +buf generate --path proto/idp/ # Generate specific domain |
| 19 | +``` |
| 20 | + |
| 21 | +### Critical Validation Rules |
| 22 | + |
| 23 | +Use `buf/validate/validate.proto` annotations extensively: |
| 24 | + |
| 25 | +- Required fields: `[(buf.validate.field).string.min_len = 1]` |
| 26 | +- Email validation: `[(buf.validate.field).string.email = true]` |
| 27 | +- Patterns: `[(buf.validate.field).string.pattern = "^[a-z0-9-]+$"]` |
| 28 | +- Enums: `[(buf.validate.field).enum.defined_only = true]` |
| 29 | + |
| 30 | +See `docs/VALIDATION.md` for comprehensive examples. |
| 31 | + |
| 32 | +### Breaking Change Policy |
| 33 | + |
| 34 | +- **NEVER break v1 APIs** - no field removal, type changes, or renumbering |
| 35 | +- Reserved fields for deprecation: `reserved 5, 8 to 10; reserved "old_field";` |
| 36 | +- Use `[deprecated = true]` annotation instead of removing fields |
| 37 | +- Create v2 package for breaking changes |
| 38 | + |
| 39 | +### CI/CD Pipeline (`buf.yml`) |
| 40 | + |
| 41 | +Uses `bufbuild/buf-action@v1` which automatically: |
| 42 | + |
| 43 | +- Lints and formats on every push/PR |
| 44 | +- Checks breaking changes on PRs (compares to base branch) |
| 45 | +- Generates multi-language clients (Go, Python, Java, TypeScript, C#) |
| 46 | +- Creates per-domain client artifacts |
| 47 | +- Publishes docs to GitHub Pages |
| 48 | + |
| 49 | +## Project-Specific Conventions |
| 50 | + |
| 51 | +### Import Paths |
| 52 | + |
| 53 | +**Module-relative only** (no `proto/` prefix): |
| 54 | + |
| 55 | +```protobuf |
| 56 | +import "core/v1/common.proto"; // ✅ Correct |
| 57 | +import "proto/core/v1/common.proto"; // ❌ Wrong |
| 58 | +``` |
| 59 | + |
| 60 | +### Package Naming |
| 61 | + |
| 62 | +```protobuf |
| 63 | +package geniustechspace.users.v1; // Format: geniustechspace.domain.version |
| 64 | +``` |
| 65 | + |
| 66 | +### Field Numbering Rules |
| 67 | + |
| 68 | +- Start at 1 (field 0 is invalid in proto3) |
| 69 | +- tenant_id always field 1 or 2 (field 2 if entity_id is field 1) |
| 70 | +- NEVER reuse field numbers - use `reserved` instead |
| 71 | + |
| 72 | +### Go Package Options |
| 73 | + |
| 74 | +```protobuf |
| 75 | +option go_package = "github.com/geniustechspace/protobuf/gen/go/users/v1;usersv1"; |
| 76 | +``` |
| 77 | + |
| 78 | +Last segment after `;` MUST match package name with version suffix (e.g., `usersv1`). |
| 79 | + |
| 80 | +### Documentation Requirements (CRITICAL) |
| 81 | + |
| 82 | +Every proto file MUST include (see `docs/PROTO_DOCUMENTATION_STANDARD.md`): |
| 83 | + |
| 84 | +**File Header:** |
| 85 | + |
| 86 | +```protobuf |
| 87 | +// DOMAIN: [domain name] |
| 88 | +// COMPLIANCE: SOC 2 CC6.1, GDPR Article 5, ISO 27001 A.9.2 |
| 89 | +// SECURITY: Authentication required, tenant isolation enforced |
| 90 | +// PII: [Yes/No] - [description] |
| 91 | +``` |
| 92 | + |
| 93 | +**Field-Level:** |
| 94 | + |
| 95 | +```protobuf |
| 96 | +// Email address. REQUIRED. |
| 97 | +// PII: Yes - GDPR Article 4(1) personal identifier |
| 98 | +// ENCRYPTION: Required at rest |
| 99 | +// VALIDATION: RFC 5322 email format |
| 100 | +string email = 3 [(buf.validate.field).string.email = true]; |
| 101 | +``` |
| 102 | + |
| 103 | +**Service RPCs:** |
| 104 | + |
| 105 | +```protobuf |
| 106 | +// CreateUser creates new user account. |
| 107 | +// AUTHENTICATION: Required - valid bearer token |
| 108 | +// AUTHORIZATION: Requires 'users:create' permission |
| 109 | +// COMPLIANCE: SOC 2 CC6.1 (User provisioning) |
| 110 | +// RATE LIMIT: 100/min per tenant |
| 111 | +rpc CreateUser(CreateUserRequest) returns (CreateUserResponse); |
| 112 | +``` |
| 113 | + |
| 114 | +## Common Patterns |
| 115 | + |
| 116 | +### Pagination (Use core.v1) |
| 117 | + |
| 118 | +```protobuf |
| 119 | +import "core/v1/common.proto"; |
| 120 | +
|
| 121 | +message ListUsersRequest { |
| 122 | + string tenant_id = 1; |
| 123 | + core.v1.PaginationRequest pagination = 2; |
| 124 | +} |
| 125 | +
|
| 126 | +message ListUsersResponse { |
| 127 | + repeated User users = 1; |
| 128 | + core.v1.PaginationResponse pagination = 2; |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### Domain Events Pattern |
| 133 | + |
| 134 | +```protobuf |
| 135 | +// UserCreated emitted when user account is successfully created. |
| 136 | +// EVENT: Publish to event bus for downstream consumers |
| 137 | +// COMPLIANCE: Audit trail requirement |
| 138 | +message UserCreated { |
| 139 | + core.v1.Metadata metadata = 1; // Event ID and timestamp |
| 140 | + string tenant_id = 2; |
| 141 | + string user_id = 3; |
| 142 | + string email = 4; // Safe for event - no password |
| 143 | + string created_by = 5; |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +### Error Handling |
| 148 | + |
| 149 | +Use standard gRPC status codes. For detailed errors, include context in metadata, not custom error types. |
| 150 | + |
| 151 | +## Key Files Reference |
| 152 | + |
| 153 | +- `buf.yaml` - Buf config with linting rules (STANDARD + UNARY_RPC) |
| 154 | +- `buf.gen.yaml` - Code generation config for 5 languages + protovalidate |
| 155 | +- `CONTRIBUTING.md` - PR process, field numbering, deprecation patterns |
| 156 | +- `docs/VALIDATION.md` - Protovalidate examples and patterns |
| 157 | +- `docs/PROTO_DOCUMENTATION_STANDARD.md` - Compliance documentation requirements |
| 158 | +- `QUICK_START.md` - Installation, generation, usage examples |
| 159 | + |
| 160 | +## Testing Changes |
| 161 | + |
| 162 | +Before committing: |
| 163 | + |
| 164 | +1. `buf lint` must pass (zero errors) |
| 165 | +2. `buf format -w` to auto-format |
| 166 | +3. `buf breaking --against '.git#branch=main'` on PRs (should pass unless creating v2) |
| 167 | +4. `buf generate` verify generation works for all languages |
| 168 | +5. Check `gen/go/` compiles: `cd gen/go && go mod init test && go build ./...` |
| 169 | + |
| 170 | +## Common Gotchas |
| 171 | + |
| 172 | +1. **Don't modify `deprecated/` directory** - it's frozen legacy code |
| 173 | +2. **Check if domain exists** - Use `proto/idp/` for auth, not deprecated `proto/auth/` |
| 174 | +3. **Empty directories** - `proto/idp/authn/v1/`, `proto/core/[subdomain]/` may be empty; check README location |
| 175 | +4. **Import paths** - Always module-relative, never absolute with `proto/` prefix |
| 176 | +5. **Tenant isolation** - Every request MUST have tenant_id; every service MUST enforce it |
| 177 | +6. **Validation is NOT optional** - All user input fields need protovalidate annotations |
| 178 | +7. **Compliance annotations** - Required for all PII fields and sensitive operations |
| 179 | + |
| 180 | +## When Adding New Domains |
| 181 | + |
| 182 | +1. Create directory: `proto/newdomain/v1/` |
| 183 | +2. Create modular files: `messages.proto`, `service.proto`, `events.proto`, `enums.proto` |
| 184 | +3. Add domain README explaining purpose and patterns |
| 185 | +4. Include Metadata in all entities |
| 186 | +5. Include tenant_id in all requests |
| 187 | +6. Add protovalidate annotations |
| 188 | +7. Document compliance requirements |
| 189 | +8. Update main README.md with domain description |
| 190 | +9. Test: `buf lint && buf generate --path proto/newdomain/` |
| 191 | + |
| 192 | +## Additional Resources |
| 193 | + |
| 194 | +- Buf docs: https://buf.build/docs |
| 195 | +- Protovalidate: https://github.com/bufbuild/protovalidate |
| 196 | +- gRPC style guide: https://grpc.io/docs/guides/ |
| 197 | +- Domain-driven design applied to this repo: Check modular IDP structure in `proto/idp/` |
0 commit comments