Skip to content

refactor: extract route modules from server.ts monolith (ARC-2)#1703

Merged
OneStepAt4time merged 8 commits intodevelopfrom
refactor/1695-route-extraction
Apr 12, 2026
Merged

refactor: extract route modules from server.ts monolith (ARC-2)#1703
OneStepAt4time merged 8 commits intodevelopfrom
refactor/1695-route-extraction

Conversation

@OneStepAt4time
Copy link
Copy Markdown
Owner

Summary

Extracts 84+ inline route handlers from the 2,720-line server.ts monolith into 11 focused route module files under src/routes/.

Changes

New files (src/routes/)

File Routes Purpose
context.ts RouteContext interface, guards (requireRole, requireOwnership), helpers
health.ts 8 health, prometheus, alerts, handshake, swarm, channels
auth.ts 6 auth verify, API keys CRUD, SSE token
audit.ts 3 audit log, global metrics, diagnostics
sessions.ts 8 session CRUD, listing, batch delete, session health
session-actions.ts 14 send, read, answer, escape, interrupt, kill, permissions
session-data.ts 12 transcript, summary, screenshot, tools, SSE, CC hooks
events.ts 1 global SSE stream
templates.ts 5 template CRUD
pipelines.ts 4 batch create, pipeline CRUD
index.ts barrel export

Refactored files

  • server.ts: 2,720 → 1,130 lines (58% reduction). Retains app creation, security hooks, auth setup, rate limits, reapers, and graceful shutdown.

Approach

  • Guards and helpers are parameterized (receive service instances as args) instead of closing over module-level variables
  • RouteContext interface provides typed dependency injection to all route modules
  • Zero behavior changes — all routes maintain identical signatures and semantics

Quality gate


  • px tsc --noEmit\ — passes

  • pm run build\ — passes

  • pm test\ — 159 passed, 2 skipped (2,814 tests)

Known gap

Per-route rate limit preHandlers are defined in server.ts but not yet wired to route modules (except templates.ts). This is intentionally deferred to ARC-5 (#1698 — Route middleware extraction).

Aegis version

Developed with: v0.5.0-alpha

Closes #1695

Extract 11 route module files from the 2,720-line server.ts monolith:
- routes/context.ts: RouteContext interface, guards, helpers
- routes/health.ts: health, prometheus, alerts, handshake, swarm
- routes/auth.ts: auth verify, API keys CRUD, SSE token
- routes/audit.ts: audit log, global metrics, diagnostics
- routes/sessions.ts: session CRUD, listing, batch delete
- routes/session-actions.ts: send, read, answer, interrupt, kill, etc.
- routes/session-data.ts: transcript, summary, screenshot, tools, SSE
- routes/events.ts: global SSE stream
- routes/templates.ts: template CRUD
- routes/pipelines.ts: batch create, pipeline CRUD
- routes/index.ts: barrel export

server.ts reduced from 2,720 to 1,130 lines (58% reduction).
Guards and helpers parameterized instead of closing over module vars.

Closes #1695
@OneStepAt4time OneStepAt4time added this to the v0.5.1-alpha milestone Apr 12, 2026
OneStepAt4time and others added 4 commits April 12, 2026 15:28
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…assignment'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
OneStepAt4time and others added 3 commits April 12, 2026 15:31
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
…limit

- health.ts: remove isRateLimited() manual Map-based tracker (memory leak,
  redundant with @fastify/rate-limit config.rateLimit per-route override)
- sessions.ts: restore session create rate limit from 20 to 120 req/min
  to match server.ts RATE_LIMITS.sessionCreate value
All template routes now use config.rateLimit (60 req/min) via
@fastify/rate-limit per-route override, replacing the conditional
preHandler approach that CodeQL flagged as missing rate limiting.
@OneStepAt4time OneStepAt4time merged commit 7b91ac4 into develop Apr 12, 2026
10 checks passed
@OneStepAt4time OneStepAt4time deleted the refactor/1695-route-extraction branch April 12, 2026 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants