@mrciphersmith/keryx 0.4.28 → 0.4.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +4916 -3655
- package/dist/core.js +91 -28
- package/dist/keryx-general-worker-entry.js +413 -240
- package/package.json +1 -1
- package/src/gdskills/bundled/install-manifest.json +3 -0
- package/src/gdskills/bundled/rules/core/api-contracts.mdc +21 -136
- package/src/gdskills/bundled/rules/core/async-patterns.mdc +18 -171
- package/src/gdskills/bundled/rules/core/clean-architecture.mdc +40 -150
- package/src/gdskills/bundled/rules/core/cli-interface-design.mdc +63 -194
- package/src/gdskills/bundled/rules/core/code-style-patterns.mdc +36 -48
- package/src/gdskills/bundled/rules/core/concurrency-patterns.mdc +32 -0
- package/src/gdskills/bundled/rules/core/database-patterns.mdc +26 -148
- package/src/gdskills/bundled/rules/core/error-handling.mdc +57 -109
- package/src/gdskills/bundled/rules/core/frontend-assistant.mdc +14 -29
- package/src/gdskills/bundled/rules/core/layout-rendering.mdc +51 -0
- package/src/gdskills/bundled/rules/core/mobx-store-template.mdc +69 -511
- package/src/gdskills/bundled/rules/core/nestjs-dto.mdc +18 -36
- package/src/gdskills/bundled/rules/core/playwright-testing.mdc +35 -308
- package/src/gdskills/bundled/rules/core/security-baseline.mdc +18 -140
- package/src/gdskills/bundled/rules/core/skill-authoring.mdc +190 -0
- package/src/gdskills/bundled/rules/core/skills-storage-workflow.mdc +5 -0
- package/src/gdskills/bundled/rules/core/solid-principles.mdc +70 -121
- package/src/gdskills/bundled/rules/core/storybook-guidelines.mdc +20 -22
- package/src/gdskills/bundled/rules/core/tdd-workflow.mdc +37 -74
- package/src/gdskills/bundled/rules/core/ui-anti-slop.mdc +48 -37
- package/src/gdskills/bundled/skills/orchestration/task-implementer/SKILL.md +2 -2
- package/src/gdskills/bundled/skills/quality/deprecation-path/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/quality/tests-creator/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-architecture/SKILL.md +25 -61
- package/src/gdskills/bundled/skills/review/review-backend/SKILL.md +15 -69
- package/src/gdskills/bundled/skills/review/review-clean-code/SKILL.md +84 -418
- package/src/gdskills/bundled/skills/review/review-core-boundaries/SKILL.md +2 -2
- package/src/gdskills/bundled/skills/review/review-degradation/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-flow-graph/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-frontend/SKILL.md +17 -311
- package/src/gdskills/bundled/skills/review/review-frontend-conventions/SKILL.md +8 -22
- package/src/gdskills/bundled/skills/review/review-highload/SKILL.md +84 -228
- package/src/gdskills/bundled/skills/review/review-layout/SKILL.md +22 -28
- package/src/gdskills/bundled/skills/review/review-logic/SKILL.md +9 -10
- package/src/gdskills/bundled/skills/review/review-performance/SKILL.md +16 -38
- package/src/gdskills/bundled/skills/review/review-premise/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-regression/SKILL.md +1 -1
- package/src/gdskills/bundled/skills/review/review-security-code/SKILL.md +8 -8
- package/src/gdskills/bundled/skills/review/review-style/SKILL.md +49 -88
- package/src/gdskills/bundled/skills/review/review-testing-practices/SKILL.md +22 -40
- package/src/gdskills/bundled/skills/review/review-ui-ux/SKILL.md +42 -82
- package/src/gdskills/bundled/stacks/angular/rules/coding-style.mdc +15 -57
- package/src/gdskills/bundled/stacks/angular/rules/patterns.mdc +16 -60
- package/src/gdskills/bundled/stacks/angular/rules/security.mdc +11 -49
- package/src/gdskills/bundled/stacks/angular/rules/testing.mdc +13 -52
- package/src/gdskills/bundled/stacks/c-cpp/rules/coding-style.mdc +33 -57
- package/src/gdskills/bundled/stacks/c-cpp/rules/patterns.mdc +35 -62
- package/src/gdskills/bundled/stacks/c-cpp/rules/security.mdc +35 -62
- package/src/gdskills/bundled/stacks/c-cpp/rules/testing.mdc +34 -54
- package/src/gdskills/bundled/stacks/ci-github-gitlab/rules/patterns.mdc +27 -56
- package/src/gdskills/bundled/stacks/ci-github-gitlab/rules/security.mdc +51 -119
- package/src/gdskills/bundled/stacks/csharp-dotnet/rules/coding-style.mdc +42 -74
- package/src/gdskills/bundled/stacks/csharp-dotnet/rules/patterns.mdc +45 -83
- package/src/gdskills/bundled/stacks/csharp-dotnet/rules/security.mdc +34 -59
- package/src/gdskills/bundled/stacks/csharp-dotnet/rules/testing.mdc +33 -62
- package/src/gdskills/bundled/stacks/django/rules/coding-style.mdc +15 -56
- package/src/gdskills/bundled/stacks/django/rules/patterns.mdc +16 -68
- package/src/gdskills/bundled/stacks/django/rules/security.mdc +13 -71
- package/src/gdskills/bundled/stacks/django/rules/testing.mdc +16 -62
- package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/coding-style.mdc +30 -53
- package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/patterns.mdc +20 -56
- package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/security.mdc +52 -122
- package/src/gdskills/bundled/stacks/docker-k8s-terraform/rules/testing.mdc +23 -42
- package/src/gdskills/bundled/stacks/fastapi/rules/coding-style.mdc +13 -47
- package/src/gdskills/bundled/stacks/fastapi/rules/patterns.mdc +18 -81
- package/src/gdskills/bundled/stacks/fastapi/rules/security.mdc +13 -75
- package/src/gdskills/bundled/stacks/fastapi/rules/testing.mdc +14 -58
- package/src/gdskills/bundled/stacks/flutter-dart/rules/coding-style.mdc +36 -74
- package/src/gdskills/bundled/stacks/flutter-dart/rules/patterns.mdc +30 -66
- package/src/gdskills/bundled/stacks/flutter-dart/rules/security.mdc +33 -67
- package/src/gdskills/bundled/stacks/flutter-dart/rules/testing.mdc +41 -73
- package/src/gdskills/bundled/stacks/go/rules/coding-style.mdc +36 -57
- package/src/gdskills/bundled/stacks/go/rules/patterns.mdc +27 -42
- package/src/gdskills/bundled/stacks/go/rules/security.mdc +26 -43
- package/src/gdskills/bundled/stacks/go/rules/testing.mdc +23 -41
- package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/coding-style.mdc +21 -40
- package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/patterns.mdc +26 -43
- package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/security.mdc +25 -37
- package/src/gdskills/bundled/stacks/java-kotlin-spring/rules/testing.mdc +34 -50
- package/src/gdskills/bundled/stacks/kotlin-android/rules/coding-style.mdc +28 -65
- package/src/gdskills/bundled/stacks/kotlin-android/rules/patterns.mdc +34 -78
- package/src/gdskills/bundled/stacks/kotlin-android/rules/security.mdc +35 -68
- package/src/gdskills/bundled/stacks/kotlin-android/rules/testing.mdc +34 -68
- package/src/gdskills/bundled/stacks/mobx/rules/coding-style.mdc +18 -64
- package/src/gdskills/bundled/stacks/mobx/rules/patterns.mdc +13 -66
- package/src/gdskills/bundled/stacks/mobx/rules/security.mdc +8 -35
- package/src/gdskills/bundled/stacks/mobx/rules/testing.mdc +10 -39
- package/src/gdskills/bundled/stacks/nestjs/rules/coding-style.mdc +32 -45
- package/src/gdskills/bundled/stacks/nestjs/rules/patterns.mdc +40 -60
- package/src/gdskills/bundled/stacks/nestjs/rules/security.mdc +30 -50
- package/src/gdskills/bundled/stacks/nestjs/rules/testing.mdc +27 -47
- package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/coding-style.mdc +14 -48
- package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/patterns.mdc +15 -67
- package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/security.mdc +12 -50
- package/src/gdskills/bundled/stacks/nextjs-nuxt/rules/testing.mdc +11 -43
- package/src/gdskills/bundled/stacks/php-laravel/rules/coding-style.mdc +17 -58
- package/src/gdskills/bundled/stacks/php-laravel/rules/patterns.mdc +15 -56
- package/src/gdskills/bundled/stacks/php-laravel/rules/security.mdc +13 -50
- package/src/gdskills/bundled/stacks/php-laravel/rules/testing.mdc +14 -55
- package/src/gdskills/bundled/stacks/python/rules/coding-style.mdc +13 -41
- package/src/gdskills/bundled/stacks/python/rules/patterns.mdc +20 -61
- package/src/gdskills/bundled/stacks/python/rules/security.mdc +15 -57
- package/src/gdskills/bundled/stacks/python/rules/testing.mdc +16 -50
- package/src/gdskills/bundled/stacks/react/rules/coding-style.mdc +14 -40
- package/src/gdskills/bundled/stacks/react/rules/patterns.mdc +15 -55
- package/src/gdskills/bundled/stacks/react/rules/security.mdc +13 -49
- package/src/gdskills/bundled/stacks/react/rules/testing.mdc +10 -37
- package/src/gdskills/bundled/stacks/ruby-rails/rules/coding-style.mdc +15 -48
- package/src/gdskills/bundled/stacks/ruby-rails/rules/patterns.mdc +18 -67
- package/src/gdskills/bundled/stacks/ruby-rails/rules/security.mdc +11 -60
- package/src/gdskills/bundled/stacks/ruby-rails/rules/testing.mdc +14 -65
- package/src/gdskills/bundled/stacks/rust/rules/coding-style.mdc +39 -65
- package/src/gdskills/bundled/stacks/rust/rules/patterns.mdc +33 -60
- package/src/gdskills/bundled/stacks/rust/rules/security.mdc +35 -57
- package/src/gdskills/bundled/stacks/rust/rules/testing.mdc +29 -52
- package/src/gdskills/bundled/stacks/sql-db/rules/coding-style.mdc +12 -48
- package/src/gdskills/bundled/stacks/sql-db/rules/patterns.mdc +22 -113
- package/src/gdskills/bundled/stacks/sql-db/rules/security.mdc +11 -56
- package/src/gdskills/bundled/stacks/sql-db/rules/testing.mdc +14 -59
- package/src/gdskills/bundled/stacks/swift-ios/rules/coding-style.mdc +29 -65
- package/src/gdskills/bundled/stacks/swift-ios/rules/patterns.mdc +41 -88
- package/src/gdskills/bundled/stacks/swift-ios/rules/security.mdc +28 -53
- package/src/gdskills/bundled/stacks/swift-ios/rules/testing.mdc +32 -65
- package/src/gdskills/bundled/stacks/vue/rules/coding-style.mdc +17 -52
- package/src/gdskills/bundled/stacks/vue/rules/patterns.mdc +17 -63
- package/src/gdskills/bundled/stacks/vue/rules/security.mdc +11 -41
- package/src/gdskills/bundled/stacks/vue/rules/testing.mdc +16 -48
|
@@ -1,163 +1,41 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: "Database
|
|
2
|
+
description: "Database standard shared by code writers and reviewers, with the severities reviewers use: no query per item on a request path, indexes with the query, migrations compatible with the code still running, transactions for multi-step writes, no SELECT * to clients, soft-delete discipline. Use when writing or reviewing queries, migrations or ORM models."
|
|
3
3
|
alwaysApply: false
|
|
4
4
|
stack_requires: "sql"
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
# Database Patterns
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
Prevent N+1 query explosions, missing indexes, and migration-induced outages. Enforce safe, observable, backward-compatible database changes.
|
|
11
|
-
|
|
12
|
-
## When To Apply
|
|
13
|
-
Apply when writing ORM queries, creating migrations, designing schema changes, or reviewing data access code.
|
|
14
|
-
|
|
15
|
-
---
|
|
9
|
+
Canon applies: the PostgreSQL documentation on locking, `ALTER TABLE` and `CREATE INDEX CONCURRENTLY`, and the ORM's documentation. This rule holds only this project's decisions.
|
|
16
10
|
|
|
17
11
|
## Rules
|
|
18
12
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const orders = await Order.findAll({ include: [{ model: User }] });
|
|
32
|
-
for (const order of orders) {
|
|
33
|
-
console.log(order.user.name); // no additional queries
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// GOOD alternative — batch with IN clause
|
|
37
|
-
const userIds = orders.map(o => o.userId);
|
|
38
|
-
const users = await User.findAll({ where: { id: userIds } });
|
|
39
|
-
const userMap = new Map(users.map(u => [u.id, u]));
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
**Detection rule:** Any `findById`, `findOne`, or `findByPk` inside a loop MUST be refactored.
|
|
43
|
-
|
|
44
|
-
### Rule 2: Index Before Query
|
|
45
|
-
Every WHERE, JOIN, ORDER BY column that does not already have an index MUST have a migration adding one before the feature ships.
|
|
46
|
-
|
|
47
|
-
```sql
|
|
48
|
-
-- Migration checklist before deploy:
|
|
49
|
-
-- 1. Identify all new WHERE clauses in the feature
|
|
50
|
-
-- 2. Check EXPLAIN ANALYZE on representative data volume
|
|
51
|
-
-- 3. Add indexes for any full-table scans
|
|
52
|
-
|
|
53
|
-
CREATE INDEX CONCURRENTLY idx_orders_user_id ON orders(user_id);
|
|
54
|
-
-- CONCURRENTLY avoids table lock in production
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
**Index requirements:**
|
|
58
|
-
- Foreign keys: always indexed
|
|
59
|
-
- Columns used in WHERE with high cardinality: indexed
|
|
60
|
-
- Composite indexes: column order matters (most selective first)
|
|
61
|
-
- Unique constraints: also serve as indexes — declare explicitly
|
|
62
|
-
|
|
63
|
-
### Rule 3: Backward-Compatible Migrations
|
|
64
|
-
|
|
65
|
-
Migrations run before code deploys in rolling deployments. The new schema MUST be compatible with both old and new application code simultaneously.
|
|
66
|
-
|
|
67
|
-
**Safe migration sequence:**
|
|
68
|
-
|
|
69
|
-
| Change | Safe? | Safe Method |
|
|
70
|
-
|---|---|---|
|
|
71
|
-
| Add nullable column | Yes | Single migration |
|
|
72
|
-
| Add NOT NULL column | No | 3-step: add nullable → backfill → add constraint |
|
|
73
|
-
| Rename column | No | 3-step: add new → copy data → remove old |
|
|
74
|
-
| Remove column | No | 2-step: stop reading in code → migration to drop |
|
|
75
|
-
| Add index | Yes (use CONCURRENTLY) | Single migration |
|
|
76
|
-
| Drop index | Yes | Single migration |
|
|
77
|
-
| Change column type | No | 3-step: add new → migrate data → remove old |
|
|
78
|
-
|
|
79
|
-
**3-Step pattern for breaking changes:**
|
|
80
|
-
```sql
|
|
81
|
-
-- Step 1 (Deploy A): Add new column as nullable
|
|
82
|
-
ALTER TABLE users ADD COLUMN email_verified_at TIMESTAMP;
|
|
83
|
-
|
|
84
|
-
-- Deploy new code that writes to both old and new column
|
|
85
|
-
|
|
86
|
-
-- Step 2 (Deploy B): Backfill + add NOT NULL constraint
|
|
87
|
-
UPDATE users SET email_verified_at = created_at WHERE email_verified_at IS NULL;
|
|
88
|
-
ALTER TABLE users ALTER COLUMN email_verified_at SET NOT NULL;
|
|
89
|
-
|
|
90
|
-
-- Deploy new code that only reads new column
|
|
91
|
-
|
|
92
|
-
-- Step 3 (Deploy C): Remove old column (if applicable)
|
|
93
|
-
```
|
|
94
|
-
|
|
95
|
-
### Rule 4: Transactions for Multi-Step Mutations
|
|
96
|
-
Any operation that writes to multiple tables or performs multiple writes to the same table MUST use a transaction.
|
|
97
|
-
|
|
98
|
-
```typescript
|
|
99
|
-
// BAD — partial failure leaves data inconsistent
|
|
100
|
-
await userRepo.save(user);
|
|
101
|
-
await walletRepo.create({ userId: user.id, balance: 0 }); // if this fails, orphaned user
|
|
102
|
-
|
|
103
|
-
// GOOD — atomic operation
|
|
104
|
-
await db.transaction(async (trx) => {
|
|
105
|
-
await userRepo.save(user, { transaction: trx });
|
|
106
|
-
await walletRepo.create({ userId: user.id, balance: 0 }, { transaction: trx });
|
|
107
|
-
});
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### Rule 5: Never SELECT * in Application Code
|
|
111
|
-
Always select specific columns to avoid:
|
|
112
|
-
- Accidentally loading large TEXT/BLOB columns
|
|
113
|
-
- Breaking when new columns are added to the schema
|
|
114
|
-
- Leaking sensitive fields (passwords, tokens)
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
// BAD
|
|
118
|
-
const users = await User.findAll(); // loads ALL columns
|
|
119
|
-
|
|
120
|
-
// GOOD
|
|
121
|
-
const users = await User.findAll({ attributes: ['id', 'name', 'email'] });
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
### Rule 6: Soft Deletes Need Index on deleted_at
|
|
125
|
-
If using soft deletes (`deleted_at IS NULL` filter), that filter is on every query. Index it:
|
|
126
|
-
|
|
127
|
-
```sql
|
|
128
|
-
CREATE INDEX idx_users_deleted_at ON users(deleted_at) WHERE deleted_at IS NULL;
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
---
|
|
132
|
-
|
|
133
|
-
## Migration Review Checklist
|
|
134
|
-
|
|
135
|
-
Before merging any migration:
|
|
136
|
-
- [ ] `CONCURRENTLY` used for new indexes on existing tables
|
|
137
|
-
- [ ] NOT NULL columns have a default or backfill in the same migration
|
|
138
|
-
- [ ] No column renames (use 3-step instead)
|
|
139
|
-
- [ ] `EXPLAIN ANALYZE` run on production-scale data for new queries
|
|
140
|
-
- [ ] Rollback plan documented (reverse migration or migration with inverse operation)
|
|
141
|
-
- [ ] Migration is idempotent (safe to run twice: use `IF NOT EXISTS`, `IF EXISTS`)
|
|
142
|
-
|
|
143
|
-
---
|
|
144
|
-
|
|
145
|
-
## Iron Laws
|
|
146
|
-
|
|
147
|
-
**IRON LAW 1: A query inside a loop is an N+1. Fix it before merging.**
|
|
148
|
-
**IRON LAW 2: A NOT NULL column without a backfill migration will fail in production on deploy.**
|
|
149
|
-
**IRON LAW 3: Multi-table writes without a transaction are a data integrity bug.**
|
|
13
|
+
1. **No query per item** over a collection whose size the caller or the data decides: batch with `IN`, join or eager-load.
|
|
14
|
+
2. **An index ships with the query.** A new `WHERE`, `JOIN` or `ORDER BY` column on a large table gets its index in the same change; foreign keys are indexed.
|
|
15
|
+
3. **Migrations run before the rollout, so the schema works with the old and the new code at once:**
|
|
16
|
+
- a nullable column or a new table: one migration;
|
|
17
|
+
- a `NOT NULL` column: one migration with a constant `DEFAULT`; without one, add nullable, backfill, then add the constraint;
|
|
18
|
+
- rename or type change: add the new column, copy and write both, drop the old one later;
|
|
19
|
+
- dropping a column: stop reading it first, drop in a later release;
|
|
20
|
+
- an index on an existing table: `CREATE INDEX CONCURRENTLY`, outside a transaction; a table created in the same migration may use a plain index;
|
|
21
|
+
- every migration is re-runnable (`IF [NOT] EXISTS`) and has a down.
|
|
22
|
+
4. **Writes that must change together run in one transaction** (several tables, or several rows of one).
|
|
23
|
+
5. **Select the columns you need.** A raw row or `SELECT *` never goes to a client.
|
|
24
|
+
6. **Soft-deleted entities** are deleted with the soft delete; a raw query on them adds `deleted_at IS NULL`.
|
|
150
25
|
|
|
151
26
|
---
|
|
152
27
|
|
|
153
|
-
##
|
|
28
|
+
## Review severities (shared with reviewers)
|
|
154
29
|
|
|
155
|
-
|
|
156
|
-
|---|---|
|
|
157
|
-
| "The table is small, N+1 is fine for now" | Tables grow — N+1 that's "fine now" causes outages at 100k rows |
|
|
158
|
-
| "I'll add the index after it's in production if it's slow" | Adding an index without CONCURRENTLY locks the table in production |
|
|
159
|
-
| "The migration is simple, I don't need a rollback plan" | Every migration that fails in production requires a rollback — having no plan means downtime |
|
|
160
|
-
| "NULL is fine, I'll add NOT NULL later" | Nullable columns become load-bearing nullability over time — enforce constraints early |
|
|
161
|
-
| "SELECT * is easier during development" | SELECT * loaded to production is a query-time performance and security risk |
|
|
30
|
+
`review-backend`, `review-performance` and `review-highload` rate these patterns by this table. Change a row here, never in a reviewer.
|
|
162
31
|
|
|
163
|
-
|
|
32
|
+
| Signal | A finding when | Default | Not a finding |
|
|
33
|
+
|---|---|---|---|
|
|
34
|
+
| Query per item in a loop (Rule 1) | the loop size is not fixed small | `major` when a frequency or hot path is named with the loop size; else `minor` | a fixed small list, or a one-off or startup path |
|
|
35
|
+
| New filter or sort column with no index (Rule 2) | the table is large or the query hot, named | `major`; else `minor`; `info` when the schema is not in the payload (say that reading the schema would settle it) | a small table |
|
|
36
|
+
| `NOT NULL` column with no default and no backfill (Rule 3) | the table can hold rows, or old code inserts into it | `major`; `blocker` only where wrong values or truncation are shown | a constant `DEFAULT` |
|
|
37
|
+
| Rename, type change or drop in one step (Rule 3) | code still running reads the old column | `major` | — |
|
|
38
|
+
| Non-concurrent index on an existing table (Rule 3) | the table is large or busy | `major`; else `minor` | a table created in the same migration |
|
|
39
|
+
| Writes that must change together without a transaction (Rule 4) | a named failure leaves a partial write | `major`; `blocker` where the partial write corrupts persisted state | — |
|
|
40
|
+
| Raw row or `SELECT *` returned to a client (Rule 5) | always | `major` when it exposes a column the contract does not have; else `minor` | — |
|
|
41
|
+
| Hard delete of a soft-deleted entity, raw query without `deleted_at IS NULL` (Rule 6) | always | `major` | — |
|
|
@@ -1,153 +1,101 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: "Error handling
|
|
2
|
+
description: "Error handling standard shared by code writers and reviewers, with the severities reviewers use: failure is observable, carries context, keeps its cause, and is never signalled by a value a caller can mistake for success. Use when implementing functions that can fail, calling external APIs, handling async operations, or reviewing that code."
|
|
3
3
|
alwaysApply: false
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Error Handling
|
|
7
7
|
|
|
8
8
|
## Purpose
|
|
9
|
-
|
|
9
|
+
One standard for writing and reviewing failure paths. It describes how this codebase already handles errors (typed and contextual throws, `cause` chains, no silent catches), so a writer following it and a reviewer checking it reach the same answer.
|
|
10
10
|
|
|
11
11
|
## When To Apply
|
|
12
|
-
Apply whenever
|
|
13
|
-
|
|
14
|
-
---
|
|
15
|
-
|
|
16
|
-
## Core Principle: Errors Are Values
|
|
17
|
-
|
|
18
|
-
Recoverable errors are data, not exceptions. Use the **Result pattern** to make failure explicit in the type signature:
|
|
19
|
-
|
|
20
|
-
```typescript
|
|
21
|
-
type Result<T, E = Error> =
|
|
22
|
-
| { ok: true; value: T }
|
|
23
|
-
| { ok: false; error: E };
|
|
24
|
-
|
|
25
|
-
function ok<T>(value: T): Result<T, never> {
|
|
26
|
-
return { ok: true, value };
|
|
27
|
-
}
|
|
28
|
-
function err<E>(error: E): Result<never, E> {
|
|
29
|
-
return { ok: false, error };
|
|
30
|
-
}
|
|
31
|
-
```
|
|
12
|
+
Apply whenever code interacts with external systems, parses input, performs I/O, or runs an operation that can fail, and when reviewing such code.
|
|
32
13
|
|
|
33
14
|
---
|
|
34
15
|
|
|
35
16
|
## Rules
|
|
36
17
|
|
|
37
|
-
### Rule 1:
|
|
38
|
-
|
|
18
|
+
### Rule 1: Signal failure explicitly
|
|
19
|
+
A function that can fail signals the failure in a form the caller cannot mistake for success:
|
|
20
|
+
- **Throw** an error. This is the default in this codebase.
|
|
21
|
+
- **Return a discriminated result** (`{ ok: true, value } | { ok: false, error }` or a tagged union) when callers are expected to branch on an anticipated failure, such as a parse or a lookup. This is allowed, not required.
|
|
39
22
|
|
|
40
|
-
|
|
41
|
-
// BAD — caller has no idea this can fail from the type alone
|
|
42
|
-
async function fetchUser(id: string): Promise<User> {
|
|
43
|
-
const res = await http.get(`/users/${id}`);
|
|
44
|
-
return res.data; // throws if 404
|
|
45
|
-
}
|
|
23
|
+
Never signal failure with `null`, `undefined`, `false`, `-1` or an empty collection when a caller could read that value as a valid result.
|
|
46
24
|
|
|
47
|
-
|
|
48
|
-
async function fetchUser(id: string): Promise<Result<User, NotFoundError | NetworkError>> {
|
|
49
|
-
const res = await http.get(`/users/${id}`).catch(e => null);
|
|
50
|
-
if (!res) return err(new NetworkError('fetch failed'));
|
|
51
|
-
if (res.status === 404) return err(new NotFoundError(id));
|
|
52
|
-
return ok(res.data);
|
|
53
|
-
}
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
### Rule 2: Typed Domain Errors
|
|
57
|
-
Do not throw or return generic `Error`. Create typed error classes per domain:
|
|
25
|
+
A language whose norm differs (Rust's `Result`, Go's `(T, error)`) follows its norm; its stack rule states it and overrides this rule for that language.
|
|
58
26
|
|
|
59
27
|
```typescript
|
|
60
|
-
// BAD
|
|
61
|
-
|
|
28
|
+
// BAD — "not found" and "empty" are the same value to the caller
|
|
29
|
+
function readEntries(path: string): Entry[] {
|
|
30
|
+
try { return parse(readFileSync(path, "utf8")); } catch { return []; }
|
|
31
|
+
}
|
|
62
32
|
|
|
63
|
-
// GOOD
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
super(`${resource} with id "${id}" not found`);
|
|
68
|
-
}
|
|
33
|
+
// GOOD — the failure is visible and says what failed
|
|
34
|
+
function readEntries(path: string): Entry[] {
|
|
35
|
+
try { return parse(readFileSync(path, "utf8")); }
|
|
36
|
+
catch (error) { throw new Error(`cannot read entries from ${path}`, { cause: error }); }
|
|
69
37
|
}
|
|
70
38
|
```
|
|
71
39
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
- `ValidationError(field, message)`
|
|
75
|
-
- `UnauthorizedError(action)`
|
|
76
|
-
- `ConflictError(resource, detail)`
|
|
77
|
-
- `ExternalServiceError(service, cause)`
|
|
78
|
-
|
|
79
|
-
### Rule 3: No Silent Failures
|
|
80
|
-
Never swallow errors without action. Every `catch` block must either:
|
|
81
|
-
- Re-throw (or return `err(...)`)
|
|
82
|
-
- Log with full context AND take fallback action
|
|
83
|
-
- Explicitly mark the result as degraded
|
|
40
|
+
### Rule 2: The message names what failed and for which input
|
|
41
|
+
An error message identifies the operation and the input it failed on: `cannot read entries from ${path}`, not `something went wrong`.
|
|
84
42
|
|
|
85
|
-
|
|
86
|
-
// BAD — silent failure
|
|
87
|
-
try {
|
|
88
|
-
await sendNotification(user);
|
|
89
|
-
} catch (_) {}
|
|
90
|
-
|
|
91
|
-
// GOOD — explicit, logged, handled
|
|
92
|
-
try {
|
|
93
|
-
await sendNotification(user);
|
|
94
|
-
} catch (e) {
|
|
95
|
-
logger.warn('notification failed', { userId: user.id, error: e });
|
|
96
|
-
// explicit decision: continue without notification
|
|
97
|
-
}
|
|
98
|
-
```
|
|
43
|
+
Use a typed error class (`class XError extends Error`) when a caller needs to tell this failure apart from others, or when it crosses a module boundary that handles it. A plain `Error` with a contextual message is fine inside a module.
|
|
99
44
|
|
|
100
|
-
### Rule
|
|
101
|
-
|
|
45
|
+
### Rule 3: No silent failures
|
|
46
|
+
Every `catch` either:
|
|
47
|
+
- rethrows, or returns the failure per Rule 1;
|
|
48
|
+
- takes a fallback that is correct for the caller, and says why in a comment;
|
|
49
|
+
- marks the result as degraded where the caller can see it.
|
|
102
50
|
|
|
103
|
-
|
|
104
|
-
When catching and re-throwing, preserve the original error as `cause`:
|
|
51
|
+
An empty `catch`, or one that only logs, is a defect when the caller then proceeds as if the operation succeeded.
|
|
105
52
|
|
|
106
53
|
```typescript
|
|
107
|
-
// BAD —
|
|
108
|
-
|
|
54
|
+
// BAD — the write failed, the caller is told it succeeded
|
|
55
|
+
try { await writeState(next); } catch {}
|
|
56
|
+
return { saved: true };
|
|
109
57
|
|
|
110
|
-
// GOOD —
|
|
111
|
-
|
|
58
|
+
// GOOD — a deliberate, explained fallback
|
|
59
|
+
try { await removeTempFile(path); }
|
|
60
|
+
catch { /* best effort: a leftover temp file is swept by the next run */ }
|
|
112
61
|
```
|
|
113
62
|
|
|
114
|
-
### Rule
|
|
115
|
-
|
|
63
|
+
### Rule 4: No unhandled promise rejections
|
|
64
|
+
Every promise is awaited inside a `try/catch` or has a `.catch()`; a fire-and-forget call handles its own rejection (`@typescript-eslint/no-floating-promises` enforces it).
|
|
116
65
|
|
|
117
|
-
|
|
66
|
+
### Rule 5: Keep the cause
|
|
67
|
+
When catching and rethrowing, pass the original as `cause`. Do not both log and rethrow the same error in one `catch`: whoever handles it logs it.
|
|
118
68
|
|
|
119
|
-
|
|
69
|
+
```typescript
|
|
70
|
+
// BAD — the original stack is lost
|
|
71
|
+
catch (e) { throw new Error("database failed"); }
|
|
120
72
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
└─ Return Result<T, DomainError> ← convert to typed error here
|
|
124
|
-
└─ Domain / use-case layer
|
|
125
|
-
└─ Return Result<T, E> up the stack
|
|
126
|
-
└─ Controller / handler
|
|
127
|
-
└─ Map to HTTP status / user message here
|
|
73
|
+
// GOOD
|
|
74
|
+
catch (e) { throw new DatabaseError("write failed", { cause: e }); }
|
|
128
75
|
```
|
|
129
76
|
|
|
130
|
-
|
|
77
|
+
### Rule 6: Assertions guard invariants, not recoverable input
|
|
78
|
+
Do not throw from an `assert` on input a caller can legitimately send. Validate it and fail per Rule 1.
|
|
131
79
|
|
|
132
80
|
---
|
|
133
81
|
|
|
134
82
|
## Iron Laws
|
|
135
83
|
|
|
136
|
-
**IRON LAW 1: A
|
|
137
|
-
**IRON LAW 2:
|
|
138
|
-
**IRON LAW 3:
|
|
84
|
+
**IRON LAW 1: A catch that hides a failure from a caller who then proceeds is forbidden.**
|
|
85
|
+
**IRON LAW 2: A failure is never signalled by a value a caller can read as success.**
|
|
86
|
+
**IRON LAW 3: A rethrow keeps the original error as `cause`.**
|
|
139
87
|
|
|
140
|
-
|
|
88
|
+
## Review severities (shared with reviewers)
|
|
141
89
|
|
|
142
|
-
|
|
90
|
+
`review-clean-code`, `review-logic`, `review-backend` and `review-frontend` rate failure paths by this table and keep only framework-specific rows of their own (a NestJS exception type, a MobX store's `try/catch/finally`). Change a row here, never in a reviewer.
|
|
143
91
|
|
|
144
|
-
|
|
|
145
|
-
|
|
146
|
-
|
|
|
147
|
-
|
|
|
148
|
-
|
|
|
149
|
-
|
|
|
150
|
-
|
|
|
151
|
-
|
|
|
92
|
+
| Signal | A finding when | Default | Not a finding |
|
|
93
|
+
|---|---|---|---|
|
|
94
|
+
| `catch` that hides a failure (Rule 3) | the caller then proceeds as if it succeeded | `major` — `blocker` only if the caller then persists or returns wrong data | a correct fallback with a comment giving the reason |
|
|
95
|
+
| Failure returned as `null` / `false` / `-1` / `[]` (Rule 1) | a caller can read it as a valid result | `major` with that caller named, else `minor` | a type that makes absence explicit and is checked |
|
|
96
|
+
| Rethrow without `cause`; message without operation or input (Rules 2, 5) | at a module boundary / any new message | `minor` | a throw that is not a `Result`; a plain contextual `Error` inside a module |
|
|
97
|
+
| Log and rethrow in one `catch` (Rule 5) | always | `minor` | — |
|
|
98
|
+
| Unhandled promise rejection (Rule 4) | it reaches the process | `blocker` (crash) | a rejection a framework's exception layer turns into an error response: rate that response instead |
|
|
99
|
+
| `catch (err: any)` in TypeScript | always | `minor` | `catch (err: unknown)` |
|
|
152
100
|
|
|
153
|
-
|
|
101
|
+
---
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
description: "Frontend delivery
|
|
2
|
+
description: "Frontend delivery rule for TypeScript, React, MobX, Ant Design and Tailwind CSS: the stack, which rule holds which standard, and the answer format. Use when implementing frontend features."
|
|
3
3
|
alwaysApply: false
|
|
4
4
|
globs:
|
|
5
5
|
- "src/**/*.ts"
|
|
@@ -9,33 +9,18 @@ stack_requires: "react,mobx"
|
|
|
9
9
|
|
|
10
10
|
# Frontend Assistant Guidelines
|
|
11
11
|
|
|
12
|
-
##
|
|
13
|
-
Produce clean, maintainable, production-ready frontend code aligned with project architecture.
|
|
12
|
+
## Stack
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
Apply when implementing features in the frontend project (TypeScript/React/MobX/Ant Design/Tailwind CSS).
|
|
14
|
+
TypeScript (strict), React, MobX, Ant Design for components, Tailwind CSS for layout and utilities, Axios for HTTP. Add no third-party dependency the stack already covers.
|
|
17
15
|
|
|
18
|
-
##
|
|
19
|
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
##
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
- Prefer explicit typing.
|
|
30
|
-
- Keep business logic in stores/services, not in view components.
|
|
31
|
-
|
|
32
|
-
## Technical Rules
|
|
33
|
-
- Use strict TypeScript types; avoid `any`.
|
|
34
|
-
- Follow `core/mobx-store-template.mdc` for MobX store patterns.
|
|
35
|
-
- Use Ant Design for components and Tailwind for layout/utilities.
|
|
36
|
-
- Avoid unnecessary third-party dependencies.
|
|
37
|
-
|
|
38
|
-
## Output Contract
|
|
39
|
-
- Final solutions must be Markdown.
|
|
40
|
-
- Code must be in fenced code blocks with language labels.
|
|
41
|
-
- Response language must match user query language.
|
|
16
|
+
## Where the standard is
|
|
17
|
+
|
|
18
|
+
- Components, layers, state placement, accessibility and TypeScript: `code-style-patterns.mdc`.
|
|
19
|
+
- Stores: `mobx-store-template.mdc`.
|
|
20
|
+
- Stories: `storybook-guidelines.mdc`.
|
|
21
|
+
|
|
22
|
+
Each holds the severities reviewers use, so code written to these rules is what the reviewer checks.
|
|
23
|
+
|
|
24
|
+
## Answer format
|
|
25
|
+
|
|
26
|
+
Code goes in fenced blocks with a language label; answer in the language of the question. Keep the change to what was asked.
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
---
|
|
2
|
+
description: "Rendered-layout standard shared by code writers and reviewers, with the severities reviewers use: a layout claim is measured in a real layout engine, 320px is always a supported width, nothing clips or scrolls sideways at a supported width, identical data renders identical geometry in every locale, and a mirrored component mirrors entirely. Use when writing or reviewing flex/grid sizing, overflow, logical properties, RTL, or containers of translated text."
|
|
3
|
+
alwaysApply: false
|
|
4
|
+
globs:
|
|
5
|
+
- "**/*.tsx"
|
|
6
|
+
- "**/*.jsx"
|
|
7
|
+
- "**/*.vue"
|
|
8
|
+
- "**/*.svelte"
|
|
9
|
+
- "**/*.css"
|
|
10
|
+
- "**/*.scss"
|
|
11
|
+
- "**/*.html"
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
# Layout Rendering
|
|
15
|
+
|
|
16
|
+
Canon applies: the CSS box model, Flexbox and Grid sizing (intrinsic and extrinsic sizing),
|
|
17
|
+
CSS Logical Properties, and WCAG 2.2 1.4.10 Reflow with its two-dimensional-content exception.
|
|
18
|
+
This rule holds only this project's decisions for rendered geometry. Taste and the UI tell
|
|
19
|
+
catalog are `ui-anti-slop.mdc`.
|
|
20
|
+
|
|
21
|
+
## Rules
|
|
22
|
+
|
|
23
|
+
1. **A layout claim is measured in a real layout engine.** The project's real-CSS tier
|
|
24
|
+
(Storybook, Playwright, a component-visual suite) or a probe that mounts the component and
|
|
25
|
+
reads computed CSS off the node. A DOM stub (happy-dom, jsdom) loads no stylesheet, so a
|
|
26
|
+
class assertion or a `getBoundingClientRect` there measures nothing. Measurements supplied
|
|
27
|
+
with the change count when they name the command and the viewport.
|
|
28
|
+
2. **320px is always a supported width.** Other supported widths come from the project's own
|
|
29
|
+
list: `DESIGN.md`, the brief, the job context, or the hosts the product ships in. At every
|
|
30
|
+
supported width nothing is clipped and the page does not scroll sideways.
|
|
31
|
+
3. **Identical data renders identical geometry in every locale.** A box sized by its own
|
|
32
|
+
labels is measured with the longest real translation in the catalog, not the English one.
|
|
33
|
+
4. **A component that mirrors under RTL mirrors entirely.** Logical properties flip; a physical
|
|
34
|
+
gradient, shadow or transform in the same component is made logical or flipped explicitly.
|
|
35
|
+
Surfaces that do not mirror by platform practice (media controls and their seek bar, numbers,
|
|
36
|
+
code, phone numbers) keep `dir="ltr"` with the reason in a comment.
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Review severities (shared with reviewers)
|
|
41
|
+
|
|
42
|
+
`review-layout` rates these by this table, and `review-ui-ux` defers overflow and reflow to it.
|
|
43
|
+
Change a row here, never in a reviewer. A row counts only when the change touches the element.
|
|
44
|
+
|
|
45
|
+
| Signal | A finding when | Default | Not a finding |
|
|
46
|
+
|---|---|---|---|
|
|
47
|
+
| Element renders at 0px while its label shows (Rule 1) | at a supported width, measured | `major` | a width no supported host reaches (`info`) |
|
|
48
|
+
| Identical data renders different geometry per locale (Rule 3) | measured with the longest real translation in the catalog | `major` | — |
|
|
49
|
+
| Content clipped, or the page scrolls sideways (Rule 2) | at a supported width, 320px included, measured | `major` | content that needs two dimensions to read (data table, map, code block) scrolling inside its own region |
|
|
50
|
+
| Cosmetic offset (a few px off centre), or a physical gradient, shadow or transform that does not flip under RTL (Rule 4) | measured: a few px, or one direction named per locale | `minor` | a surface that does not mirror by platform practice, with its reason read |
|
|
51
|
+
| Any layout claim with no measurement (Rule 1) | always | `info` | — |
|