bunsane 0.3.0 → 0.3.2
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/.claude/scheduled_tasks.lock +1 -0
- package/CHANGELOG.md +104 -0
- package/CLAUDE.md +20 -0
- package/config/cache.config.ts +35 -1
- package/core/App.ts +24 -1060
- package/core/ArcheType.ts +78 -2110
- package/core/Entity.ts +136 -41
- package/core/RequestContext.ts +85 -36
- package/core/RequestLoaders.ts +89 -31
- package/core/SchedulerManager.ts +13 -13
- package/core/app/bootstrap.ts +133 -0
- package/core/app/cors.ts +94 -0
- package/core/app/graphqlSetup.ts +56 -0
- package/core/app/healthEndpoints.ts +31 -0
- package/core/app/metricsCollector.ts +27 -0
- package/core/app/preparedStatementWarmup.ts +55 -0
- package/core/app/processHandlers.ts +43 -0
- package/core/app/requestRouter.ts +309 -0
- package/core/app/restRegistry.ts +72 -0
- package/core/app/shutdown.ts +97 -0
- package/core/app/studioRouter.ts +83 -0
- package/core/archetype/customTypes.ts +100 -0
- package/core/archetype/decorators.ts +171 -0
- package/core/archetype/fieldResolvers.ts +621 -0
- package/core/archetype/helpers.ts +29 -0
- package/core/archetype/relationLoader.ts +118 -0
- package/core/archetype/schemaBuilder.ts +141 -0
- package/core/archetype/weaver.ts +218 -0
- package/core/archetype/zodSchemaBuilder.ts +527 -0
- package/core/cache/CacheManager.ts +144 -9
- package/core/components/BaseComponent.ts +12 -2
- package/core/middleware/AccessLog.ts +8 -1
- package/database/PreparedStatementCache.ts +17 -16
- package/database/cancellable.ts +22 -0
- package/database/instrumentedDb.ts +141 -0
- package/docs/RFC_APP_REFACTOR.md +248 -0
- package/docs/RFC_REFACTOR_TARGETS.md +251 -0
- package/package.json +1 -1
- package/query/ComponentInclusionNode.ts +5 -5
- package/query/Query.ts +65 -48
- package/service/ServiceRegistry.ts +7 -1
- package/service/index.ts +4 -2
- package/tests/integration/loaders/RequestLoaders.abort.test.ts +82 -0
- package/tests/integration/query/Query.abort.test.ts +66 -0
- package/tests/unit/cache/CacheManager.test.ts +152 -1
- package/tests/unit/database/cancellable.test.ts +81 -0
- package/tests/unit/database/instrumentedDb.test.ts +160 -0
- package/tests/unit/entity/Entity.components.test.ts +73 -0
- package/tests/unit/entity/Entity.drainSideEffects.test.ts +51 -0
- package/tests/unit/entity/Entity.reload.test.ts +63 -0
- package/tests/unit/entity/Entity.requireComponents.test.ts +72 -0
- package/tests/unit/query/Query.emptyString.test.ts +69 -0
- package/tests/unit/query/Query.test.ts +6 -4
- package/tests/unit/scheduler/SchedulerManager.timeBased.test.ts +95 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# RFC: Split `core/App.ts`
|
|
2
|
+
|
|
3
|
+
**Status:** Draft
|
|
4
|
+
**Author:** uray@qyubit.io (drafted with Claude)
|
|
5
|
+
**Branch target:** `refactor/app-split` (off `main`)
|
|
6
|
+
**Date:** 2026-05-09
|
|
7
|
+
**Companion work:** `refactor/archetype-split` (commit `a886b45`) — same playbook applied to `core/ArcheType.ts`.
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
## 1. Summary
|
|
12
|
+
|
|
13
|
+
Split `core/App.ts` (1477 LOC, single God class) into a thin orchestrator (~400 LOC) plus 6–8 focused modules under `core/app/`. Public API surface (`new App(...)`, `app.use()`, `app.setCors()`, `app.start()`, `app.shutdown()`, etc.) remains byte-identical. No behavior change. Each step independently verifiable by the existing test suite.
|
|
14
|
+
|
|
15
|
+
## 2. Motivation
|
|
16
|
+
|
|
17
|
+
`App.ts` is the boot path for every entrypoint in the framework. Today it owns:
|
|
18
|
+
|
|
19
|
+
- HTTP request routing (REST + GraphQL + Studio + health + metrics + OpenAPI/Swagger UI).
|
|
20
|
+
- CORS validation + header injection.
|
|
21
|
+
- GraphQL schema build + Yoga instance creation + plugin pipe + depth/complexity limits.
|
|
22
|
+
- REST endpoint collection from `ServiceRegistry` + OpenAPI spec generation.
|
|
23
|
+
- Lifecycle phase orchestration (`DATABASE_INITIALIZING` → `DATABASE_READY` → `SYSTEM_REGISTERING` → `SYSTEM_READY` → `APPLICATION_READY`).
|
|
24
|
+
- Scheduler bootstrap + scheduled-task registration.
|
|
25
|
+
- RemoteManager bootstrap + remote handler registration.
|
|
26
|
+
- Process signal/error handlers (SIGTERM, SIGINT, unhandledRejection, uncaughtException).
|
|
27
|
+
- Graceful shutdown ordering (HTTP drain → scheduler → remote → cache → DB pool).
|
|
28
|
+
- Prepared-statement cache warm-up.
|
|
29
|
+
- Metrics aggregation (`/metrics`).
|
|
30
|
+
|
|
31
|
+
Concrete pain points observed in the file:
|
|
32
|
+
|
|
33
|
+
| Method | Lines | Concern |
|
|
34
|
+
|---|---|---|
|
|
35
|
+
| `init()` | 151–495 (~345) | Phase switch + phase listener + DB prep + component registration. Multiple nested phases each running 30–80 LOC of business logic. |
|
|
36
|
+
| `handleRequest()` | 663–1090 (~430) | Health, metrics, OpenAPI, Swagger UI, Studio API (4 sub-paths), static assets, REST router, GraphQL fall-through. |
|
|
37
|
+
| `shutdown()` | 1365–1456 (~91) | Reasonable size today but couples directly to 5 subsystems. |
|
|
38
|
+
| `warmUpPreparedStatementCache` | 1183–1237 | Unrelated to anything else `App` does. |
|
|
39
|
+
| Studio API block inside `handleRequest` | 810–917 (~107) | Belongs in `studioEndpoint` module, not request router. |
|
|
40
|
+
|
|
41
|
+
Effects today:
|
|
42
|
+
|
|
43
|
+
1. **Hard to read.** Anyone changing CORS behavior reads through 1500 LOC to find the 80 LOC that matter.
|
|
44
|
+
2. **Hard to test.** Health-endpoint logic, OpenAPI generation, Studio routing all require an `App` instance even though they're pure functions of state.
|
|
45
|
+
3. **Hard to extend.** Adding a new subsystem (e.g. tracing) means another branch in `init()` and another endpoint in `handleRequest`, growing the monolith.
|
|
46
|
+
4. **Coupling risk.** `handleRequest()` already references 8 modules transitively. Moving anything risks an import cycle.
|
|
47
|
+
|
|
48
|
+
## 3. Goals / Non-Goals
|
|
49
|
+
|
|
50
|
+
### Goals
|
|
51
|
+
|
|
52
|
+
- Reduce `App.ts` to ≤500 LOC.
|
|
53
|
+
- Each new module <500 LOC, single concern.
|
|
54
|
+
- Zero change to public API (`App` class methods, `CorsConfig`, `AppConfig` exports).
|
|
55
|
+
- Zero change to phase ordering, shutdown ordering, signal handling, or error handling.
|
|
56
|
+
- All extractions verified by `tsc --noEmit` clean + `bun run test:pglite` green per step.
|
|
57
|
+
- Each step is an independently revertable commit.
|
|
58
|
+
|
|
59
|
+
### Non-Goals
|
|
60
|
+
|
|
61
|
+
- **No DI introduction.** Project rule: singletons + global exports (per `MEMORY.md`). Extracted modules accept `App` (or its state subset) as a parameter; they do not receive an injection container.
|
|
62
|
+
- **No new abstractions** (router DSL, middleware framework, plugin SPI v2, etc.). This is an extraction, not a redesign.
|
|
63
|
+
- **No type-only changes** that would force consumer updates. `App` continues to default-export the same class.
|
|
64
|
+
- **No fix to existing bugs** as part of this refactor. Issues found get filed and fixed in separate PRs.
|
|
65
|
+
|
|
66
|
+
## 4. Proposed Layout
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
core/
|
|
70
|
+
App.ts # ~400 LOC: class skeleton + public API + delegates
|
|
71
|
+
app/
|
|
72
|
+
cors.ts # ~100 LOC: validateOrigin, getCorsHeaders, addCorsHeaders, validateCorsConfig
|
|
73
|
+
bootstrap.ts # ~250 LOC: phase listener body, phase-specific handlers
|
|
74
|
+
graphqlSetup.ts # ~80 LOC: yoga instance build, depth/complexity envvar resolution
|
|
75
|
+
restRegistry.ts # ~120 LOC: REST endpoint collection from services + OpenAPI tagging
|
|
76
|
+
requestRouter.ts # ~250 LOC: handleRequest body, dispatch table, request signal plumbing
|
|
77
|
+
healthEndpoints.ts # ~80 LOC: /health, /health/ready, /health/remote handlers
|
|
78
|
+
studioRouter.ts # ~120 LOC: /studio/api/* sub-router (lifts existing block)
|
|
79
|
+
metricsCollector.ts # ~40 LOC: collectMetrics
|
|
80
|
+
preparedStatementWarmup.ts # ~60 LOC: warmUpPreparedStatementCache
|
|
81
|
+
processHandlers.ts # ~80 LOC: register/unregister SIGTERM/SIGINT/unhandled/uncaught
|
|
82
|
+
shutdown.ts # ~100 LOC: shutdown body + waitForHttpDrain
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Module responsibilities
|
|
86
|
+
|
|
87
|
+
#### `app/cors.ts`
|
|
88
|
+
- `validateOrigin(config, requestOrigin) → string | null`
|
|
89
|
+
- `getCorsHeaders(config, req?) → Record<string, string>`
|
|
90
|
+
- `addCorsHeaders(response, config, req?) → Response`
|
|
91
|
+
- `assertValidCorsConfig(cors)` — throws if `origin === undefined`, etc.
|
|
92
|
+
|
|
93
|
+
`App` keeps `setCors` as a public method but its body becomes `assertValidCorsConfig(cors); this.config.cors = cors;`. CORS state lives on `App.config.cors`; pure functions take it as a parameter.
|
|
94
|
+
|
|
95
|
+
#### `app/bootstrap.ts`
|
|
96
|
+
- `runDatabaseReadyPhase(app)` — wraps `warmUpPreparedStatementCache`.
|
|
97
|
+
- `runSystemReadyPhase(app)` — cache health check, GraphQL setup (delegates to `graphqlSetup`), scheduler init, remote init, REST endpoint collection (delegates to `restRegistry`), final `setPhase(APPLICATION_READY)`.
|
|
98
|
+
- `runApplicationReadyPhase(app)` — `app.start()` outside test env.
|
|
99
|
+
- `createPhaseListener(app)` returns the closure currently inlined inside `init()`.
|
|
100
|
+
|
|
101
|
+
`App.init()` becomes:
|
|
102
|
+
```ts
|
|
103
|
+
async init() {
|
|
104
|
+
this.openAPISpecGenerator = new OpenAPISpecGenerator(...);
|
|
105
|
+
this.registerProcessHandlers();
|
|
106
|
+
validateEnv();
|
|
107
|
+
if (this.cacheConfig) await CacheManager.initialize({ ...defaultCacheConfig, ...this.cacheConfig });
|
|
108
|
+
for (const plugin of this.plugins) plugin.init?.(this);
|
|
109
|
+
this.phaseListener = createPhaseListener(this);
|
|
110
|
+
ApplicationLifecycle.addPhaseListener(this.phaseListener);
|
|
111
|
+
if (currentPhase === DATABASE_INITIALIZING) {
|
|
112
|
+
if (!(await HasValidBaseTable())) await PrepareDatabase();
|
|
113
|
+
else await EnsureDatabaseMigrations();
|
|
114
|
+
ApplicationLifecycle.setPhase(DATABASE_READY);
|
|
115
|
+
await ComponentRegistry.registerAllComponents();
|
|
116
|
+
ApplicationLifecycle.setPhase(SYSTEM_REGISTERING);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### `app/requestRouter.ts`
|
|
122
|
+
- `handleRequest(app, req)` — top-level dispatcher. Pulls path/method, attaches abort signal, runs the dispatch table.
|
|
123
|
+
- Dispatch table lives here as a `Map<string, (app, req, url) => Promise<Response>>` for static paths plus regex for dynamic REST.
|
|
124
|
+
- Calls `healthEndpoints`, `studioRouter`, `metricsCollector`, OpenAPI/docs handlers.
|
|
125
|
+
|
|
126
|
+
This is the largest extraction — and the one with the highest risk because of subtle order-dependence. Mitigation: keep every `if (url.pathname === ...)` branch in the same order in the new module. No reordering.
|
|
127
|
+
|
|
128
|
+
#### `app/healthEndpoints.ts`
|
|
129
|
+
- `handleHealth()`, `handleReady(app)`, `handleRemoteHealth(app)`.
|
|
130
|
+
|
|
131
|
+
Each returns `{ result, httpStatus }` so the router applies CORS uniformly. Today these are inline blocks; they become 8–10 LOC functions.
|
|
132
|
+
|
|
133
|
+
#### `app/studioRouter.ts`
|
|
134
|
+
- `routeStudio(app, url, req, method) → Promise<Response | null>` — returns `null` if not a studio path so the main router falls through.
|
|
135
|
+
- Today: 107 LOC inline in `handleRequest`. Lifted with no change.
|
|
136
|
+
|
|
137
|
+
#### `app/processHandlers.ts`
|
|
138
|
+
- `registerProcessHandlers(app)`, `unregisterProcessHandlers(app)`.
|
|
139
|
+
- Returns and stores handler refs on `app` (same shape as today).
|
|
140
|
+
|
|
141
|
+
#### `app/shutdown.ts`
|
|
142
|
+
- `shutdown(app)` body (HTTP drain → scheduler → remote → cache → DB → lifecycle disposal → handler unregister).
|
|
143
|
+
- `waitForHttpDrain(server, timeoutMs)`.
|
|
144
|
+
|
|
145
|
+
### Cross-cutting decisions
|
|
146
|
+
|
|
147
|
+
- **`App` instance passed by reference.** Extracted functions take `app: App` (or a typed slice) and mutate state via existing setters/fields. We do not refactor private fields into a separate `AppState` type — that's a non-goal change.
|
|
148
|
+
- **No lazy require for these.** Unlike the ArcheType extraction (which had circular type deps with `BaseArcheType`), the App extractions are leaves: they import `App` only as a type. Use `import type { default as App } from "../App"` to avoid runtime cycles.
|
|
149
|
+
- **Logger reuse.** Each module creates its own child logger: `MainLogger.child({ scope: "App.cors" })`. Matches existing `scope: 'app'` conventions in the file.
|
|
150
|
+
- **Error semantics preserved exactly.** The `SYSTEM_READY` failure path (line 451–467) is load-bearing — it sets `isReady=false`, logs fatal, and `process.exit(1)` outside test env (memory: H-K8S-1 / C09). Extraction keeps this exit path intact and tested.
|
|
151
|
+
|
|
152
|
+
## 5. Migration Plan
|
|
153
|
+
|
|
154
|
+
Each step is a separate commit on `refactor/app-split`. Run `tsc --noEmit` and `bun run test:pglite` between steps. Skip a step if its preconditions aren't met after the prior commit.
|
|
155
|
+
|
|
156
|
+
### Step 1 — `cors.ts` (lowest risk, smallest blast)
|
|
157
|
+
- Move `validateOrigin`, `getCorsHeaders`, `addCorsHeaders` to `core/app/cors.ts`.
|
|
158
|
+
- Each takes `app.config.cors` (or a `CorsConfig`) explicitly; no `this`.
|
|
159
|
+
- `App` methods become 1-line delegates.
|
|
160
|
+
- **Verify:** existing CORS tests pass. Check `tests/e2e` for CORS assertions.
|
|
161
|
+
|
|
162
|
+
### Step 2 — `processHandlers.ts`
|
|
163
|
+
- Lift `registerProcessHandlers` / `unregisterProcessHandlers`.
|
|
164
|
+
- **Verify:** signal handler tests if any; otherwise sanity-test by sending SIGINT in a dev run.
|
|
165
|
+
|
|
166
|
+
### Step 3 — `shutdown.ts` + `waitForHttpDrain`
|
|
167
|
+
- Lift the entire `shutdown()` body + helper.
|
|
168
|
+
- `App.shutdown()` becomes `return runShutdown(this)`.
|
|
169
|
+
- **Verify:** shutdown ordering tests (memory: C10, C14 referenced).
|
|
170
|
+
|
|
171
|
+
### Step 4 — `metricsCollector.ts` + `preparedStatementWarmup.ts`
|
|
172
|
+
- Pure leaves. Move and re-import.
|
|
173
|
+
- **Verify:** `/metrics` endpoint test (E2E).
|
|
174
|
+
|
|
175
|
+
### Step 5 — `healthEndpoints.ts`
|
|
176
|
+
- Move `/health`, `/health/ready`, `/health/remote` handlers.
|
|
177
|
+
- Each returns `{ result, httpStatus }`; router wraps in `Response` with CORS.
|
|
178
|
+
- **Verify:** health endpoint tests (`tests/e2e`).
|
|
179
|
+
|
|
180
|
+
### Step 6 — `studioRouter.ts`
|
|
181
|
+
- Lift the entire `if (this.studioEnabled && pathname.startsWith("/studio/api/"))` block.
|
|
182
|
+
- **Verify:** Studio is opt-in (`enableStudio()`) so most tests don't exercise it. Manual smoke test with `STUDIO_ENABLED=true` env.
|
|
183
|
+
|
|
184
|
+
### Step 7 — `graphqlSetup.ts` + `restRegistry.ts`
|
|
185
|
+
- Extract Yoga instance build + GraphQL depth/complexity envvar resolution.
|
|
186
|
+
- Extract REST endpoint collection loop (lines 347–446) including OpenAPI spec generation per endpoint.
|
|
187
|
+
- **Verify:** GraphQL schema tests (`bun run test:graphql`), REST endpoint tests.
|
|
188
|
+
|
|
189
|
+
### Step 8 — `bootstrap.ts`
|
|
190
|
+
- Move the `switch (phase)` body into per-phase functions.
|
|
191
|
+
- `App.init()` shrinks to the skeleton above.
|
|
192
|
+
- **Highest risk step** — this is where lifecycle ordering bugs would surface. Mitigation: do this last, after all leaves are extracted, so any test failure isolates to the orchestrator.
|
|
193
|
+
- **Verify:** full test suite. Pay attention to `tests/integration` (boot-sensitive).
|
|
194
|
+
|
|
195
|
+
### Step 9 — `requestRouter.ts`
|
|
196
|
+
- Move `handleRequest` body. By this point everything it calls is already extracted, so this is largely cut/paste.
|
|
197
|
+
- `App.handleRequest()` becomes `return handleRequest(this, req)`.
|
|
198
|
+
- **Verify:** every E2E test (HTTP path coverage).
|
|
199
|
+
|
|
200
|
+
### Order rationale
|
|
201
|
+
|
|
202
|
+
Leaves first (cors, processHandlers, shutdown, metrics, prepStmtWarmup, healthEndpoints, studioRouter), then composites (graphqlSetup, restRegistry), then orchestrators (bootstrap, requestRouter). This minimizes the number of in-flight extractions when the riskiest steps run.
|
|
203
|
+
|
|
204
|
+
## 6. Verification Strategy
|
|
205
|
+
|
|
206
|
+
For every step:
|
|
207
|
+
|
|
208
|
+
1. `tsc --noEmit` — must show only the 4 pre-existing `gql/index.ts` errors.
|
|
209
|
+
2. `bun run test:pglite` — full suite (currently 770 pass / 0 fail post-archetype-split).
|
|
210
|
+
3. `bun run test:e2e` — covers HTTP routing, CORS, health, OpenAPI.
|
|
211
|
+
4. Manual smoke at the end: `bun examples/<some-app>/index.ts`, hit `/health`, `/openapi.json`, `/graphql`.
|
|
212
|
+
5. **Lifecycle assertion:** before & after Step 8 + Step 9, capture the printed phase log on a clean boot and `diff` them. Phase order must be byte-identical.
|
|
213
|
+
|
|
214
|
+
## 7. Risks
|
|
215
|
+
|
|
216
|
+
| Risk | Severity | Mitigation |
|
|
217
|
+
|---|---|---|
|
|
218
|
+
| Phase listener ordering bug in `bootstrap.ts` extraction | High | Step 8 done last, after every dependency lifted. `diff` the boot log before/after. |
|
|
219
|
+
| Studio path order in `requestRouter` changes a fall-through | Medium | Keep branch order identical; extract as one block, not per-handler. |
|
|
220
|
+
| `handleRequest` abort-signal plumbing breaks | Medium | The signal-combine logic stays in the router (not split). Test with deliberate slow handlers. |
|
|
221
|
+
| Import cycle between `App` and `bootstrap` (or `requestRouter`) | Medium | Use `import type` for `App` in extracted modules. Verified by `tsc --noEmit` per step. |
|
|
222
|
+
| `setRemoteManager(null)` in shutdown is missed | Low | Lifted as part of `shutdown.ts`; verified by remote-shutdown tests. |
|
|
223
|
+
| `process.exit(1)` path on SYSTEM_READY failure removed accidentally | High | Step 8 includes a regression test asserting that `runSystemReadyPhase` rethrows in `NODE_ENV=test`. |
|
|
224
|
+
| Hidden coupling: `composedHandler` set in `start()` but bound to `handleRequest` | Medium | Keep the `bind(this)` site in `App.start()` (not in `requestRouter`). The function `handleRequest` is exported from the module but the bound reference lives on `App`. |
|
|
225
|
+
|
|
226
|
+
## 8. Rollback
|
|
227
|
+
|
|
228
|
+
Each step is a single commit. Roll back with `git revert <sha>`. Because every step preserves the public API, partial rollback (steps 1–6 kept, 7–9 reverted) is also safe.
|
|
229
|
+
|
|
230
|
+
If the whole branch needs to be abandoned: `git checkout main; git branch -D refactor/app-split`. No state outside git.
|
|
231
|
+
|
|
232
|
+
## 9. Out of Scope (Follow-ups)
|
|
233
|
+
|
|
234
|
+
Items observed during analysis but explicitly not addressed here:
|
|
235
|
+
|
|
236
|
+
- **`handleRequest` cyclomatic complexity.** Even after extraction, `requestRouter` has ~15 branches. Could later be a registration-based dispatch (`app.registerRoute(method, path, handler)`) — but that's a feature, not a refactor.
|
|
237
|
+
- **`enforceDocs` warning text.** Hardcoded "Don't use this endpoint until it's properly documented!" in `init()`. Extraction preserves it; cleanup is separate.
|
|
238
|
+
- **Studio API duplication.** Several `studio/api/*` paths repeat the same `parseInt(url.searchParams.get(...))` pattern. After extraction these are obvious to dedupe — but again, separate PR.
|
|
239
|
+
- **Metrics shape.** `/metrics` returns ad-hoc JSON. Prometheus exposition format is a future concern.
|
|
240
|
+
- **Prepared-statement warmup heuristics.** "First 5 components, first 3 for multi-component" is arbitrary. Tunable later.
|
|
241
|
+
|
|
242
|
+
## 10. Decision Required
|
|
243
|
+
|
|
244
|
+
- [ ] Approve scope and 9-step plan.
|
|
245
|
+
- [ ] Approve module names / locations under `core/app/`.
|
|
246
|
+
- [ ] Confirm: no public API change, no behavior change, no DI introduction.
|
|
247
|
+
|
|
248
|
+
Once approved, work proceeds on `refactor/app-split` with one commit per step. Estimated effort: 4–6 focused hours given the test suite already in place.
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
# RFC: Remaining Refactor Targets
|
|
2
|
+
|
|
3
|
+
**Status:** Backlog / planning
|
|
4
|
+
**Author:** uray@qyubit.io (drafted with Claude)
|
|
5
|
+
**Date:** 2026-05-09
|
|
6
|
+
**Prior work:** `core/ArcheType.ts` split — commit `a886b45`, merged to `staging` (`fd75f21`).
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## 1. Purpose
|
|
11
|
+
|
|
12
|
+
After `ArcheType.ts` was split (3064 → 1032 LOC across 8 modules under `core/archetype/`), several other files in `core/` remain large and concern-dense. This RFC enumerates them in priority order so future refactor work has a reference.
|
|
13
|
+
|
|
14
|
+
This is a **planning document**, not an approval-bound RFC. Each target listed here would get its own scoped RFC (like `RFC_APP_REFACTOR.md`) before work starts.
|
|
15
|
+
|
|
16
|
+
## 2. Selection Criteria
|
|
17
|
+
|
|
18
|
+
Files are ordered by combined score of:
|
|
19
|
+
|
|
20
|
+
1. **Size** (LOC) — bigger = harder to read, harder to test.
|
|
21
|
+
2. **Concern density** — number of distinct responsibilities mixed in one class/module.
|
|
22
|
+
3. **Blast radius** — how many tests/consumers depend on the file booting cleanly.
|
|
23
|
+
4. **Refactor ROI** — likelihood that splitting yields independently testable modules without behavior change.
|
|
24
|
+
|
|
25
|
+
Pure "library leaf" files (formatter helpers, fixed schemas) are excluded even when large, because splitting them wouldn't reveal new structure.
|
|
26
|
+
|
|
27
|
+
## 3. Targets
|
|
28
|
+
|
|
29
|
+
### 3.1 `core/App.ts` — 1477 LOC — **highest priority**
|
|
30
|
+
|
|
31
|
+
**Why next:** Most God-class. Mixes:
|
|
32
|
+
|
|
33
|
+
- Application lifecycle (phase orchestration, DB prep, component registration).
|
|
34
|
+
- HTTP server (Bun.serve setup, request routing, signal/disconnect plumbing).
|
|
35
|
+
- CORS (origin validation, header injection, preflight handling).
|
|
36
|
+
- OpenAPI spec generation (per-endpoint registration, Swagger UI HTML).
|
|
37
|
+
- GraphQL setup (Yoga instance, depth/complexity limits, plugin pipe, context factory wrap).
|
|
38
|
+
- REST routing (endpoint collection, dispatch).
|
|
39
|
+
- Plugin pipeline (`addPlugin`, `addYogaPlugin`).
|
|
40
|
+
- Scheduler bootstrap (`SchedulerManager` init, scheduled task registration per service).
|
|
41
|
+
- Health endpoints (`/health`, `/health/ready`, `/health/remote`).
|
|
42
|
+
- Metrics endpoint (`/metrics`).
|
|
43
|
+
- Studio routing (`/studio/api/*` — 107 LOC inline).
|
|
44
|
+
- Remote subsystem bootstrap (RemoteManager init, handler registration).
|
|
45
|
+
- Process signal & error handlers (SIGTERM, SIGINT, unhandledRejection, uncaughtException).
|
|
46
|
+
- Graceful shutdown ordering (HTTP → scheduler → remote → cache → DB).
|
|
47
|
+
- Prepared-statement cache warm-up.
|
|
48
|
+
|
|
49
|
+
`init()` is a giant `switch (phase)` (lines 198–476) where each case runs 30–80 LOC of business logic. `handleRequest()` is ~430 LOC across 8+ branches.
|
|
50
|
+
|
|
51
|
+
**Detailed plan:** see `RFC_APP_REFACTOR.md` (drafted, awaiting approval).
|
|
52
|
+
|
|
53
|
+
**Status:** RFC drafted, not started.
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
### 3.2 `core/Entity.ts` — 1212 LOC
|
|
58
|
+
|
|
59
|
+
**Why next:** `save()` alone likely 300+ LOC. Cache ops inline. Mixes:
|
|
60
|
+
|
|
61
|
+
- Component add/get/remove (in-memory + persisted).
|
|
62
|
+
- DB persistence (insert/update/delete with abort signal + per-component partitioned writes).
|
|
63
|
+
- Cache write-through / write-invalidate strategies (L1 + L2 + pubsub).
|
|
64
|
+
- Hook dispatch (pre-save, post-save, post-delete) via `EntityHookManager`.
|
|
65
|
+
- Pending side-effects queue (`Entity.pendingCacheOps`, `Entity.pendingSideEffects` static drain methods for shutdown).
|
|
66
|
+
- Profile timing (`DB_SAVE_PROFILE`).
|
|
67
|
+
- Abort signal handling (timeout + client disconnect cancellation).
|
|
68
|
+
- Component-ready preflight (`ComponentRegistry.getReadyPromise`).
|
|
69
|
+
- Static finders (`FindById`, etc.).
|
|
70
|
+
|
|
71
|
+
**Proposed split direction:**
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
core/entity/
|
|
75
|
+
saveEntity.ts # save() body — DB writes, abort, profile
|
|
76
|
+
cacheStrategies.ts # write-through, write-invalidate per component
|
|
77
|
+
pendingOps.ts # pendingCacheOps + pendingSideEffects + drain methods
|
|
78
|
+
componentAccess.ts # add/get/remove + in-memory cache
|
|
79
|
+
finders.ts # static FindById, etc.
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Class skeleton + public API stays in `Entity.ts`.
|
|
83
|
+
|
|
84
|
+
**Risks:**
|
|
85
|
+
|
|
86
|
+
- `Entity.save()` is hot-path. Per-step micro-benchmark (save 1000 entities) before/after each extraction.
|
|
87
|
+
- Hook ordering is load-bearing (per `MEMORY.md` H-HOOK-1..3, C13). Don't reorder pre/post-commit phases.
|
|
88
|
+
- The PGlite Bun-SQL ACK race (documented in `CLAUDE.md`) is in this file's blast radius — keep `await entity.save()` semantics byte-identical.
|
|
89
|
+
|
|
90
|
+
**Estimated effort:** larger than App.ts because of perf sensitivity. ~6–8 hours plus benchmark validation.
|
|
91
|
+
|
|
92
|
+
**Status:** Not started.
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
### 3.3 `core/SchedulerManager.ts` — 932 LOC
|
|
97
|
+
|
|
98
|
+
**Why next:** Scheduling logic + distributed lock + hook orchestration in one class.
|
|
99
|
+
|
|
100
|
+
Concerns to disentangle:
|
|
101
|
+
|
|
102
|
+
- Cron expression parsing + schedule evaluation.
|
|
103
|
+
- Task registration & lookup (`registerScheduledTasks`).
|
|
104
|
+
- Per-task execution loop with skip-on-running guard (H-SCHED-1..5 in memory).
|
|
105
|
+
- Distributed lock (`DistributedLock`) acquisition + release semantics.
|
|
106
|
+
- Lifecycle integration (`disposeLifecycleIntegration`, awaiting in-flight tasks on `stop()` per C14).
|
|
107
|
+
- Metrics (`getMetrics`).
|
|
108
|
+
- Error handling per task.
|
|
109
|
+
|
|
110
|
+
**Proposed split direction:**
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
core/scheduler/
|
|
114
|
+
cronEvaluator.ts # cron expression -> next-fire-time
|
|
115
|
+
taskRunner.ts # per-task execute loop + skip-on-running
|
|
116
|
+
lockCoordinator.ts # DistributedLock wiring
|
|
117
|
+
lifecycleHooks.ts # phase-listener + dispose
|
|
118
|
+
metrics.ts # getMetrics
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
`SchedulerManager` keeps singleton + public API.
|
|
122
|
+
|
|
123
|
+
**Risks:**
|
|
124
|
+
|
|
125
|
+
- Concurrency hardening already done in v0.3.0 (H-SCHED-1..5). Refactor must preserve every guard. Property-based tests on the runner would help.
|
|
126
|
+
- Re-entry semantics on `DistributedLock` (memory: `acquired:false` on overlap). Don't change.
|
|
127
|
+
|
|
128
|
+
**Status:** Not started.
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
### 3.4 `core/EntityHookManager.ts` — 921 LOC
|
|
133
|
+
|
|
134
|
+
**Why next:** Hook registry + dispatch + lifecycle in one place.
|
|
135
|
+
|
|
136
|
+
Concerns:
|
|
137
|
+
|
|
138
|
+
- Hook registration (per-component, per-event).
|
|
139
|
+
- Dispatch ordering (pre vs post, sync vs async).
|
|
140
|
+
- Hook chain with timer leak fixes (memory: H-HOOK-2, H-MEM-2).
|
|
141
|
+
- Re-entry / recursion guard.
|
|
142
|
+
- Integration with `Entity.save()` post-commit microtask scheduling.
|
|
143
|
+
|
|
144
|
+
**Proposed split direction:**
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
core/hooks/
|
|
148
|
+
registry.ts # register/lookup
|
|
149
|
+
dispatcher.ts # dispatch loop + ordering
|
|
150
|
+
guards.ts # re-entry guard, timer cleanup
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
`EntityHookManager` keeps public API.
|
|
154
|
+
|
|
155
|
+
**Risks:**
|
|
156
|
+
|
|
157
|
+
- Hook timing fixes (C13, H-HOOK-1..3) are load-bearing. Tests assert specific orderings.
|
|
158
|
+
- Cross-file coupling with `Entity.ts` — coordinate with §3.2 if both run in flight.
|
|
159
|
+
|
|
160
|
+
**Status:** Not started.
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
### 3.5 `core/cache/CacheManager.ts` — 574 LOC
|
|
165
|
+
|
|
166
|
+
**Why next:** L1 (memory) + L2 (Redis) + strategies + pub/sub all-in-one. Already smaller than peers, so lower priority.
|
|
167
|
+
|
|
168
|
+
Concerns:
|
|
169
|
+
|
|
170
|
+
- Provider initialization (memory + Redis).
|
|
171
|
+
- Strategy dispatch (write-through vs write-invalidate).
|
|
172
|
+
- Cross-instance invalidation via Redis pub/sub (`instanceId` loop prevention).
|
|
173
|
+
- Cache stats / health (`ping`, `getStats`).
|
|
174
|
+
- Singleton lifecycle (`initialize` async, `shutdown`).
|
|
175
|
+
|
|
176
|
+
**Proposed split direction:**
|
|
177
|
+
|
|
178
|
+
```
|
|
179
|
+
core/cache/
|
|
180
|
+
CacheManager.ts # singleton + public API (kept)
|
|
181
|
+
strategies/
|
|
182
|
+
writeThrough.ts
|
|
183
|
+
writeInvalidate.ts
|
|
184
|
+
invalidation.ts # pub/sub coordinator
|
|
185
|
+
health.ts # ping + stats
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
**Risks:**
|
|
189
|
+
|
|
190
|
+
- `CacheManager.initialize()` is now async (BREAKING CHANGE per memory, 2026-02-17). Don't regress.
|
|
191
|
+
- Cross-instance loop prevention (`instanceId`) is load-bearing. Test with two instances on same Redis.
|
|
192
|
+
|
|
193
|
+
**Status:** Not started. Lowest priority of the five — defer until at least one peer refactor lands.
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## 4. Cross-Cutting Themes
|
|
198
|
+
|
|
199
|
+
Several patterns recur and would benefit from being decided once before any of these refactors start:
|
|
200
|
+
|
|
201
|
+
### 4.1 Extraction pattern
|
|
202
|
+
|
|
203
|
+
`ArcheType.ts` split established the pattern:
|
|
204
|
+
|
|
205
|
+
- Pure functions in submodules accept the class instance as a parameter (`buildFieldResolvers(archetype)`).
|
|
206
|
+
- Class methods become 1-line delegates via lazy `require()` to break circular type deps.
|
|
207
|
+
- Maps/state stay in the submodule that owns them, exported as `const`.
|
|
208
|
+
- Public API preserved by re-export from the parent file.
|
|
209
|
+
|
|
210
|
+
This pattern works well for ECS-style classes where the class is mostly a data bag with methods. **Re-use it for App, Entity, SchedulerManager, EntityHookManager.** `CacheManager` may want a different shape (provider injection) given its strategy variants.
|
|
211
|
+
|
|
212
|
+
### 4.2 Test infrastructure assumed stable
|
|
213
|
+
|
|
214
|
+
All five targets are exercised by the current 770-test suite (under `bun run test:pglite`). No target requires new test scaffolding before extraction starts; existing tests are sufficient guardrails for behavior preservation.
|
|
215
|
+
|
|
216
|
+
### 4.3 No DI introduction
|
|
217
|
+
|
|
218
|
+
Project rule (per `CLAUDE.md` and `MEMORY.md`): singletons + global exports, no dependency injection container. Extracted modules must respect this — pass `app: App`, `entity: Entity`, etc., not an injection token.
|
|
219
|
+
|
|
220
|
+
### 4.4 No bundled bug fixes
|
|
221
|
+
|
|
222
|
+
If a refactor reveals a latent bug (wrong ordering, missing guard, stale comment claim), file it separately. Refactor PRs must show "no behavior change" by passing the existing test suite unchanged.
|
|
223
|
+
|
|
224
|
+
## 5. Recommended Order
|
|
225
|
+
|
|
226
|
+
1. **`App.ts`** — RFC drafted, ready to start. Highest payoff: every test boots through it.
|
|
227
|
+
2. **`Entity.ts`** — Highest perf sensitivity but biggest readability win. Allocate benchmark time.
|
|
228
|
+
3. **`SchedulerManager.ts`** *or* **`EntityHookManager.ts`** — Either next. They're partially coupled (hooks fire from scheduler-triggered work), so coordinate.
|
|
229
|
+
4. **`CacheManager.ts`** — Last. Smallest of the five, already structured around providers.
|
|
230
|
+
|
|
231
|
+
This ordering minimizes risk because the most heavily-tested file goes first (more guardrails) and the perf-sensitive file goes early-second when there's still energy for benchmarking.
|
|
232
|
+
|
|
233
|
+
## 6. Anti-Goals
|
|
234
|
+
|
|
235
|
+
These are not refactors and should not be bundled:
|
|
236
|
+
|
|
237
|
+
- **Adding new abstractions** (router DSL, plugin SPI v2, hook framework). Out of scope for any of these.
|
|
238
|
+
- **Performance "improvements"** that change semantics. If a refactor reveals an O(n²) loop, file it separately.
|
|
239
|
+
- **API renaming** for "consistency". Public symbols stay byte-identical.
|
|
240
|
+
- **Comment cleanup pass** as a side effect. Touch only comments that are actively wrong after a code move.
|
|
241
|
+
|
|
242
|
+
## 7. Decision
|
|
243
|
+
|
|
244
|
+
This RFC requires no decision. It exists so the next person picking up refactor work has:
|
|
245
|
+
|
|
246
|
+
- A prioritized list.
|
|
247
|
+
- Concern inventory per file.
|
|
248
|
+
- Pre-identified risks per file.
|
|
249
|
+
- Cross-cutting guardrails (extraction pattern, no-DI rule, no bundled fixes).
|
|
250
|
+
|
|
251
|
+
When work starts on any one target, that target gets its own RFC and own branch (per the `RFC_APP_REFACTOR.md` template).
|
package/package.json
CHANGED
|
@@ -606,11 +606,11 @@ export class ComponentInclusionNode extends QueryNode {
|
|
|
606
606
|
condition = result.sql;
|
|
607
607
|
// Note: custom builder is responsible for adding parameters via context.addParam()
|
|
608
608
|
} else {
|
|
609
|
-
// Default filter logic
|
|
610
|
-
//
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
609
|
+
// Default filter logic. Empty-string values are permitted
|
|
610
|
+
// here — `c.data->>'field'` extracts text, so `=`/`!=`/
|
|
611
|
+
// `LIKE` against '' is legitimate. The UUID-cast path
|
|
612
|
+
// below is gated on a regex that empty string cannot
|
|
613
|
+
// match, so unsafe casts never fire.
|
|
614
614
|
|
|
615
615
|
// Check if value looks like a UUID (case-insensitive, with or without hyphens)
|
|
616
616
|
const valueStr = String(filter.value);
|