bunsane 0.3.1 → 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.
Files changed (41) hide show
  1. package/.claude/scheduled_tasks.lock +1 -0
  2. package/CHANGELOG.md +52 -0
  3. package/config/cache.config.ts +35 -1
  4. package/core/App.ts +24 -1064
  5. package/core/ArcheType.ts +78 -2110
  6. package/core/Entity.ts +10 -33
  7. package/core/RequestContext.ts +85 -36
  8. package/core/RequestLoaders.ts +89 -31
  9. package/core/app/bootstrap.ts +133 -0
  10. package/core/app/cors.ts +94 -0
  11. package/core/app/graphqlSetup.ts +56 -0
  12. package/core/app/healthEndpoints.ts +31 -0
  13. package/core/app/metricsCollector.ts +27 -0
  14. package/core/app/preparedStatementWarmup.ts +55 -0
  15. package/core/app/processHandlers.ts +43 -0
  16. package/core/app/requestRouter.ts +309 -0
  17. package/core/app/restRegistry.ts +72 -0
  18. package/core/app/shutdown.ts +97 -0
  19. package/core/app/studioRouter.ts +83 -0
  20. package/core/archetype/customTypes.ts +100 -0
  21. package/core/archetype/decorators.ts +171 -0
  22. package/core/archetype/fieldResolvers.ts +621 -0
  23. package/core/archetype/helpers.ts +29 -0
  24. package/core/archetype/relationLoader.ts +118 -0
  25. package/core/archetype/schemaBuilder.ts +141 -0
  26. package/core/archetype/weaver.ts +218 -0
  27. package/core/archetype/zodSchemaBuilder.ts +527 -0
  28. package/core/cache/CacheManager.ts +126 -9
  29. package/core/middleware/AccessLog.ts +8 -1
  30. package/database/PreparedStatementCache.ts +12 -3
  31. package/database/cancellable.ts +22 -0
  32. package/database/instrumentedDb.ts +141 -0
  33. package/docs/RFC_APP_REFACTOR.md +248 -0
  34. package/docs/RFC_REFACTOR_TARGETS.md +251 -0
  35. package/package.json +1 -1
  36. package/query/Query.ts +53 -20
  37. package/tests/integration/loaders/RequestLoaders.abort.test.ts +82 -0
  38. package/tests/integration/query/Query.abort.test.ts +66 -0
  39. package/tests/unit/cache/CacheManager.test.ts +132 -1
  40. package/tests/unit/database/cancellable.test.ts +81 -0
  41. package/tests/unit/database/instrumentedDb.test.ts +160 -0
@@ -15,6 +15,16 @@ interface InvalidationMessage {
15
15
  pattern?: string;
16
16
  }
17
17
 
18
+ /**
19
+ * Sentinel value written to the cache to record "known absent" lookups.
20
+ * String literal (not object) so it round-trips cleanly through
21
+ * JSON.stringify in RedisCache + CompressionUtils. Callers must treat it
22
+ * as a cache hit but propagate a `null`/`[]` upstream.
23
+ */
24
+ export const COMPONENT_TOMBSTONE = '__TOMBSTONE__' as const;
25
+ export const RELATION_TOMBSTONE = '__TOMBSTONE__' as const;
26
+ export type ComponentCacheValue = ComponentData | typeof COMPONENT_TOMBSTONE;
27
+
18
28
  /**
19
29
  * High-level cache operations manager
20
30
  * Singleton that provides entity and component caching methods
@@ -321,16 +331,18 @@ export class CacheManager {
321
331
  }
322
332
 
323
333
  /**
324
- * Get components by entity and type from cache (for DataLoader integration)
334
+ * Get components by entity and type from cache (for DataLoader integration).
335
+ * Returns COMPONENT_TOMBSTONE for keys whose absence was previously
336
+ * recorded; callers must treat this as a hit and propagate null upstream.
325
337
  */
326
- public async getComponents(keys: Array<{ entityId: string; typeId: string }>): Promise<(ComponentData | null)[]> {
338
+ public async getComponents(keys: Array<{ entityId: string; typeId: string }>): Promise<(ComponentCacheValue | null)[]> {
327
339
  if (!this.config.enabled || !this.config.component?.enabled) {
328
340
  return keys.map(() => null);
329
341
  }
330
342
 
331
343
  try {
332
344
  const cacheKeys = keys.map(k => `component:${k.entityId}:${k.typeId}`);
333
- const results = await this.provider.getMany<ComponentData>(cacheKeys);
345
+ const results = await this.provider.getMany<ComponentCacheValue>(cacheKeys);
334
346
  return results;
335
347
  } catch (error) {
336
348
  logger.error({ scope: 'cache', component: 'CacheManager', msg: 'Error getting components from cache', error });
@@ -339,26 +351,131 @@ export class CacheManager {
339
351
  }
340
352
 
341
353
  /**
342
- * Set components in cache with write-through strategy (for DataLoader integration)
354
+ * Set components in cache with write-through strategy (for DataLoader integration).
355
+ *
356
+ * When `requestedKeys` is supplied and `component.negativeCacheEnabled` is
357
+ * true, tombstones are written for any requested key not present in
358
+ * `components` (within the same setMany call — single round-trip).
343
359
  */
344
- public async setComponentsWriteThrough(components: ComponentData[], ttl?: number): Promise<void> {
360
+ public async setComponentsWriteThrough(
361
+ components: ComponentData[],
362
+ ttlOrRequested?: number | Array<{ entityId: string; typeId: string }>,
363
+ ttlIfRequested?: number,
364
+ ): Promise<void> {
345
365
  if (!this.config.enabled || !this.config.component?.enabled) {
346
366
  return;
347
367
  }
348
368
 
369
+ // Backward-compatible overload: (components, ttl?) or (components, requestedKeys, ttl?)
370
+ const requestedKeys = Array.isArray(ttlOrRequested) ? ttlOrRequested : undefined;
371
+ const ttl = Array.isArray(ttlOrRequested) ? ttlIfRequested : ttlOrRequested;
372
+
349
373
  try {
350
- const effectiveTTL = ttl ?? this.config.component?.ttl;
351
- const entries = components.map(comp => ({
374
+ const componentTTL = ttl ?? this.config.component.ttl;
375
+ const entries: Array<{ key: string; value: ComponentCacheValue; ttl: number }> = components.map(comp => ({
352
376
  key: `component:${comp.entityId}:${comp.typeId}`,
353
377
  value: comp,
354
- ttl: effectiveTTL
378
+ ttl: componentTTL,
355
379
  }));
356
- await this.provider.setMany(entries);
380
+
381
+ const negativeEnabled = this.config.component.negativeCacheEnabled === true;
382
+ if (negativeEnabled && requestedKeys && requestedKeys.length > 0) {
383
+ const found = new Set(components.map(c => `${c.entityId}-${c.typeId}`));
384
+ const tombstoneTTL = this.config.component.negativeCacheTtl
385
+ ?? Math.min(componentTTL, 60_000);
386
+ for (const k of requestedKeys) {
387
+ const dedupeKey = `${k.entityId}-${k.typeId}`;
388
+ if (!found.has(dedupeKey)) {
389
+ entries.push({
390
+ key: `component:${k.entityId}:${k.typeId}`,
391
+ value: COMPONENT_TOMBSTONE,
392
+ ttl: tombstoneTTL,
393
+ });
394
+ }
395
+ }
396
+ }
397
+
398
+ if (entries.length > 0) {
399
+ await this.provider.setMany(entries);
400
+ }
357
401
  } catch (error) {
358
402
  logger.error({ scope: 'cache', component: 'CacheManager', msg: 'Error setting components in cache', error });
359
403
  }
360
404
  }
361
405
 
406
+ // Relation negative-cache methods
407
+
408
+ /**
409
+ * Build the cache key for a relation tombstone. Null byte separator
410
+ * prevents collision when relationField contains hyphens or colons.
411
+ */
412
+ private static relationCacheKey(entityId: string, relationField: string, relatedType: string, foreignKey?: string): string {
413
+ const fk = foreignKey ?? '';
414
+ return `relation:${entityId}\x00${relationField}\x00${relatedType}\x00${fk}`;
415
+ }
416
+
417
+ /**
418
+ * Bulk-check relation tombstones. Returns true at index i when the
419
+ * relation at keys[i] was previously recorded as empty.
420
+ */
421
+ public async getRelationsEmpty(
422
+ keys: Array<{ entityId: string; relationField: string; relatedType: string; foreignKey?: string }>,
423
+ ): Promise<boolean[]> {
424
+ if (!this.config.enabled || !this.config.relation?.negativeCacheEnabled) {
425
+ return keys.map(() => false);
426
+ }
427
+ try {
428
+ const cacheKeys = keys.map(k => CacheManager.relationCacheKey(k.entityId, k.relationField, k.relatedType, k.foreignKey));
429
+ const values = await this.provider.getMany<string>(cacheKeys);
430
+ return values.map(v => v === RELATION_TOMBSTONE);
431
+ } catch (error) {
432
+ logger.error({ scope: 'cache', component: 'CacheManager', msg: 'Error getting relation tombstones', error });
433
+ return keys.map(() => false);
434
+ }
435
+ }
436
+
437
+ /**
438
+ * Record relation tombstones for keys whose query returned []. TTL
439
+ * defaults to relation.negativeCacheTtl (60s).
440
+ */
441
+ public async setRelationsEmpty(
442
+ keys: Array<{ entityId: string; relationField: string; relatedType: string; foreignKey?: string }>,
443
+ ttl?: number,
444
+ ): Promise<void> {
445
+ if (!this.config.enabled || !this.config.relation?.negativeCacheEnabled || keys.length === 0) {
446
+ return;
447
+ }
448
+ try {
449
+ const effectiveTTL = ttl ?? this.config.relation.negativeCacheTtl ?? 60_000;
450
+ const entries = keys.map(k => ({
451
+ key: CacheManager.relationCacheKey(k.entityId, k.relationField, k.relatedType, k.foreignKey),
452
+ value: RELATION_TOMBSTONE,
453
+ ttl: effectiveTTL,
454
+ }));
455
+ await this.provider.setMany(entries);
456
+ } catch (error) {
457
+ logger.error({ scope: 'cache', component: 'CacheManager', msg: 'Error setting relation tombstones', error });
458
+ }
459
+ }
460
+
461
+ /**
462
+ * Drop a relation tombstone. Call when a target component is created
463
+ * that may newly satisfy the relation. Pub/sub invalidation is wired
464
+ * identically to component invalidation.
465
+ */
466
+ public async invalidateRelation(entityId: string, relationField: string, relatedType: string, foreignKey?: string): Promise<void> {
467
+ if (!this.config.enabled || !this.config.relation?.negativeCacheEnabled) {
468
+ return;
469
+ }
470
+ try {
471
+ const key = CacheManager.relationCacheKey(entityId, relationField, relatedType, foreignKey);
472
+ await this.provider.delete(key);
473
+ await this.publishInvalidation('key', [key]);
474
+ } catch (error) {
475
+ logger.error({ scope: 'cache', component: 'CacheManager', msg: 'Error invalidating relation tombstone', error });
476
+ }
477
+ }
478
+
362
479
  // Generic cache methods
363
480
 
364
481
  /**
@@ -1,6 +1,7 @@
1
1
  import type { Middleware } from '../Middleware';
2
2
  import { logger as MainLogger } from '../Logger';
3
3
  import { getRequestId } from './RequestId';
4
+ import type { RequestStats } from '../RequestContext';
4
5
 
5
6
  const logger = MainLogger.child({ scope: 'HTTP' });
6
7
 
@@ -37,7 +38,8 @@ export function accessLog(options: AccessLogOptions = {}): Middleware {
37
38
  }
38
39
 
39
40
  const duration = Math.round(performance.now() - start);
40
- const logData = {
41
+ const stats = (req as any).__bunsaneStats as RequestStats | undefined;
42
+ const logData: Record<string, any> = {
41
43
  requestId: getRequestId(),
42
44
  method: req.method,
43
45
  path: url.pathname,
@@ -45,6 +47,11 @@ export function accessLog(options: AccessLogOptions = {}): Middleware {
45
47
  duration,
46
48
  msg: `${req.method} ${url.pathname} ${response.status} ${duration}ms`,
47
49
  };
50
+ if (stats) {
51
+ logData.operationName = stats.operationName;
52
+ logData.dataLoaderCalls = stats.dataLoaderCalls;
53
+ logData.dbQueryCount = stats.dbQueryCount;
54
+ }
48
55
 
49
56
  if (response.status >= 500) {
50
57
  logger.error(logData);
@@ -1,4 +1,5 @@
1
1
  import { logger } from "../core/Logger";
2
+ import { timedUnsafe, type PerRequestCounters } from "./instrumentedDb";
2
3
 
3
4
  export interface CacheEntry {
4
5
  sql: string;
@@ -108,15 +109,23 @@ export class PreparedStatementCache {
108
109
  }
109
110
 
110
111
  /**
111
- * Execute a prepared statement with parameters
112
+ * Execute a prepared statement with parameters. Routes through
113
+ * `timedUnsafe` so the call is timed and (when a signal is supplied)
114
+ * cancellable via Bun's `Query.cancel()` on abort.
112
115
  */
113
- public async execute(statement: any, params: any[], db: any): Promise<any[]> {
116
+ public async execute(
117
+ statement: any,
118
+ params: any[],
119
+ db: any,
120
+ signal?: AbortSignal,
121
+ perRequest?: PerRequestCounters,
122
+ ): Promise<any[]> {
114
123
  // Empty-string params are legitimate for text-field filters
115
124
  // (`c.data->>'field' = ''`). UUID-typed params never reach this
116
125
  // point empty — callers (Query.findById etc.) guard at entry. PG
117
126
  // emits a clear error at execution time if a UUID cast meets an
118
127
  // empty string.
119
- return await db.unsafe(statement.sql, params);
128
+ return await timedUnsafe<any[]>(db, statement.sql, params, signal, perRequest);
120
129
  }
121
130
 
122
131
  /**
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Wraps a Bun SQL Query so an AbortSignal can cancel the in-flight query
3
+ * via the underlying `query.cancel()` method. When the signal fires the
4
+ * server-side query receives a cancel request, the awaited promise rejects,
5
+ * any enclosing transaction triggers ROLLBACK, and the pooled backend
6
+ * connection is released. Without this, a wall-clock timeout leaks the
7
+ * backend into `idle in transaction` under pgbouncer transaction-mode.
8
+ */
9
+ export async function runWithSignal<T>(q: any, signal?: AbortSignal): Promise<T> {
10
+ if (!signal) return await q;
11
+ if (signal.aborted) {
12
+ try { q.cancel?.(); } catch { /* ignore */ }
13
+ throw signal.reason ?? new Error('Query aborted');
14
+ }
15
+ const onAbort = () => { try { q.cancel?.(); } catch { /* ignore */ } };
16
+ signal.addEventListener('abort', onAbort, { once: true });
17
+ try {
18
+ return await q;
19
+ } finally {
20
+ signal.removeEventListener('abort', onAbort);
21
+ }
22
+ }
@@ -0,0 +1,141 @@
1
+ import type { SQL } from "bun";
2
+ import { logger as MainLogger } from "../core/Logger";
3
+ import { runWithSignal } from "./cancellable";
4
+
5
+ const logger = MainLogger.child({ scope: "db" });
6
+
7
+ const SLOW_MS = parseInt(process.env.BUNSANE_DB_SLOW_MS ?? '500', 10);
8
+
9
+ export type DataLoaderKind = 'entity' | 'component' | 'relation';
10
+
11
+ interface DbStatsInternal {
12
+ totalCount: number;
13
+ totalMs: number;
14
+ maxMs: number;
15
+ slowCount: number;
16
+ abortedCount: number;
17
+ inFlight: number;
18
+ inFlightMax: number;
19
+ dataLoaderCalls: { entity: number; component: number; relation: number };
20
+ }
21
+
22
+ const stats: DbStatsInternal = {
23
+ totalCount: 0,
24
+ totalMs: 0,
25
+ maxMs: 0,
26
+ slowCount: 0,
27
+ abortedCount: 0,
28
+ inFlight: 0,
29
+ inFlightMax: 0,
30
+ dataLoaderCalls: { entity: 0, component: 0, relation: 0 },
31
+ };
32
+
33
+ /**
34
+ * Per-request counter incremented when current request context is reachable
35
+ * via the (request as any).__bunsaneStats pointer. We accept that as a
36
+ * parameter from the call site so this module stays free of GraphQL imports.
37
+ */
38
+ export interface PerRequestCounters {
39
+ dbQueryCount: number;
40
+ }
41
+
42
+ /**
43
+ * Execute `db.unsafe(sql, params)` with optional AbortSignal cancellation
44
+ * and roundtrip telemetry. On abort the in-flight query is cancelled via
45
+ * `Query.cancel()`. Total ms is recorded into module-level stats; calls
46
+ * over `BUNSANE_DB_SLOW_MS` increment slowCount and emit a warn log.
47
+ */
48
+ export async function timedUnsafe<T = any>(
49
+ db: SQL,
50
+ sql: string,
51
+ params: any[],
52
+ signal?: AbortSignal,
53
+ perRequest?: PerRequestCounters,
54
+ ): Promise<T> {
55
+ const t0 = performance.now();
56
+ stats.inFlight++;
57
+ if (stats.inFlight > stats.inFlightMax) stats.inFlightMax = stats.inFlight;
58
+ if (perRequest) perRequest.dbQueryCount++;
59
+ let aborted = false;
60
+ try {
61
+ const q = (db as any).unsafe(sql, params);
62
+ return await runWithSignal<T>(q, signal);
63
+ } catch (err) {
64
+ if ((err as Error)?.name === 'AbortError' || signal?.aborted) {
65
+ aborted = true;
66
+ stats.abortedCount++;
67
+ }
68
+ throw err;
69
+ } finally {
70
+ const dt = performance.now() - t0;
71
+ stats.inFlight--;
72
+ stats.totalCount++;
73
+ stats.totalMs += dt;
74
+ if (dt > stats.maxMs) stats.maxMs = dt;
75
+ if (SLOW_MS > 0 && dt > SLOW_MS && !aborted) {
76
+ stats.slowCount++;
77
+ logger.warn(
78
+ {
79
+ durationMs: Math.round(dt),
80
+ thresholdMs: SLOW_MS,
81
+ sqlSnippet: sql.length > 200 ? sql.slice(0, 200) + '…' : sql,
82
+ msg: 'Slow DB call',
83
+ },
84
+ 'Slow DB call',
85
+ );
86
+ }
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Increment the per-kind DataLoader counter. Called from inside DataLoader
92
+ * batch functions so /metrics + access log can attribute load patterns.
93
+ *
94
+ * `perRequest` is loosely typed because RequestContext's `RequestStats`
95
+ * (defined in core/RequestContext.ts) extends `PerRequestCounters` with
96
+ * extra fields like `dataLoaderCalls`. We accept either shape here without
97
+ * importing the higher-level type (which would create a cycle).
98
+ */
99
+ export function incrementDataLoaderCall(
100
+ kind: DataLoaderKind,
101
+ perRequest?: PerRequestCounters | { dataLoaderCalls?: { entity: number; component: number; relation: number } },
102
+ ): void {
103
+ stats.dataLoaderCalls[kind]++;
104
+ const dlc = (perRequest as any)?.dataLoaderCalls;
105
+ if (dlc) dlc[kind]++;
106
+ }
107
+
108
+ /**
109
+ * Snapshot of accumulated DB stats for the /metrics endpoint.
110
+ */
111
+ export function getDbStats() {
112
+ const avgMs = stats.totalCount > 0 ? stats.totalMs / stats.totalCount : 0;
113
+ return {
114
+ totalCount: stats.totalCount,
115
+ totalMs: Math.round(stats.totalMs),
116
+ maxMs: Math.round(stats.maxMs),
117
+ avgMs: Number(avgMs.toFixed(2)),
118
+ slowCount: stats.slowCount,
119
+ abortedCount: stats.abortedCount,
120
+ inFlight: stats.inFlight,
121
+ inFlightMax: stats.inFlightMax,
122
+ slowThresholdMs: SLOW_MS,
123
+ dataLoaderCalls: { ...stats.dataLoaderCalls },
124
+ };
125
+ }
126
+
127
+ /**
128
+ * Reset counters. Intended for tests only.
129
+ */
130
+ export function resetDbStats(): void {
131
+ stats.totalCount = 0;
132
+ stats.totalMs = 0;
133
+ stats.maxMs = 0;
134
+ stats.slowCount = 0;
135
+ stats.abortedCount = 0;
136
+ stats.inFlight = 0;
137
+ stats.inFlightMax = 0;
138
+ stats.dataLoaderCalls.entity = 0;
139
+ stats.dataLoaderCalls.component = 0;
140
+ stats.dataLoaderCalls.relation = 0;
141
+ }
@@ -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.