Skip to content

Split server.ts into route modules with a typed service container#110

Closed
SuuBro wants to merge 21 commits intomasterfrom
goal/split-serv-04e6c40c
Closed

Split server.ts into route modules with a typed service container#110
SuuBro wants to merge 21 commits intomasterfrom
goal/split-serv-04e6c40c

Conversation

@SuuBro
Copy link
Copy Markdown
Owner

@SuuBro SuuBro commented Mar 29, 2026

Summary

Splits the 3,207-line server.ts monolith into 19 domain-grouped route modules with a typed AppContext service container.

Changes

  • src/server/app-context.tsAppContext interface (typed service container replacing 21 loose parameters)
  • src/server/routes/index.ts — Router dispatching to 19 route modules
  • src/server/routes/*.ts — health, sessions, goals, teams, gates, tasks, roles, tools, aigw, models, preferences, personalities, workflows, auth, skills, staff, mcp, git, preview
  • src/server/routes/utils.ts — Shared utilities (readBody, json, hasTransitiveDep)
  • src/server/services/github-service.ts — PR/git caching helpers extracted
  • src/server/server.ts — Reduced from 3,207 to 363 lines
  • AGENTS.md — Updated with new architecture guidance

Acceptance Criteria

  • AppContext interface is the sole parameter to route handlers
  • All 50+ routes extracted into domain-grouped modules
  • server.ts under 400 lines (363)
  • npm run check passes
  • All E2E tests pass (323 unit, 315 E2E)
  • No as any casts for service wiring

SuuBro and others added 21 commits March 29, 2026 08:25
Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
…lities, workflows, skills, preview

Extract 8 route modules from server.ts handleApiRoute():
- health.ts: /api/health, /api/setup-status, /api/shutdown + isSetupComplete()
- auth.ts: /api/ca-cert, /api/connection-info, /api/oauth/*
- preferences.ts: /api/preferences, /api/project-config, /api/config/cwd
- models.ts: /api/models
- personalities.ts: /api/personalities CRUD
- workflows.ts: /api/workflows CRUD + clone + delete
- skills.ts: /api/slash-skills, /api/slash-skills/details
- preview.ts: /api/preview GET/POST

Each module exports handle(ctx, url, req, res) => Promise<boolean>.
All use AppContext from app-context.ts and json/readBody from utils.ts.

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
…els, personalities, workflows, skills, preview)

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
… and github-service

Extract route handlers from server.ts handleApiRoute() into dedicated modules:
- routes/teams.ts: /api/goals/:id/team/* and /api/goals/:id/swarm/* (start, spawn, dismiss, steer, abort, prompt, agents, complete, teardown)
- routes/gates.ts: /api/goals/:id/gates/* (list, detail, signal, signals, content, workflow-context, active verifications)
- routes/aigw.ts: /api/aigw/*, /api/custom-providers/*, /api/provider-keys/*
- routes/staff.ts: /api/staff/* (CRUD, wake, sessions)
- routes/mcp.ts: /api/mcp-servers/*, /api/internal/mcp-call
- routes/git.ts: /api/sessions/:id/git-*, /api/goals/:id/pr-*
- services/github-service.ts: PR/git cache helpers (getViewerIsAdmin, getCachedPrStatus, execGit, execGitSafe, bustPrCache)

Each module exports handle(ctx, url, req, res) => Promise<boolean>.
All use AppContext from app-context.ts and readBody/json from routes/utils.ts.

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
…, git) + github-service

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
…, tools

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
… tools)

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
- Import AppContext/GatewayConfig from app-context.ts, re-export for compat
- Build AppContext in createGateway() with all services
- Replace handleApiRoute(21 params) with routeApiRequest(ctx, url, req, res)
- Delete handleApiRoute, git helpers, readBody, hasTransitiveDep, isSetupComplete
- Remove (sessionManager as any).bgProcessManager cast
- Add missing draft/bg-process/file-content/prompt-sections routes to sessions.ts
- server.ts: 363 lines (down from 3207). All 315 E2E tests pass.

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
- Add /api/config prefix to router for /api/config/cwd endpoint
- Add GET /api/pr-status-cache endpoint to git.ts route module
- Add GET /api/goals/:id/cost/breakdown and /cost endpoints to goals.ts
- Add GET /api/tasks/:id/cost endpoint to tasks.ts
- Replace local execGit/execGitSafe in goals.ts with imports from github-service
- Rename _fetchPrStatus to fetchPrStatus in github-service.ts
- Fix unused import warnings in aigw.ts, mcp.ts, preview.ts

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
…to 'totals')

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
- Replace 'Edit server.ts handleApiRoute()' with route module guidance
- Add 'Server route architecture' section documenting AppContext, routes/, and github-service
- Update goal and tool doc references from server.ts to specific route modules

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
…ons and GET /api/goals

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
…use readBody in mcp.ts

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
…o gates.ts

Co-Authored-By: Bobbit (Claude Opus 4 6 (anthropic)) <noreply@bobbit.dev>
@SuuBro SuuBro force-pushed the goal/split-serv-04e6c40c branch from 8974cfb to 4e4347a Compare March 29, 2026 19:53
@SuuBro SuuBro closed this Apr 2, 2026
@SuuBro SuuBro deleted the goal/split-serv-04e6c40c branch April 9, 2026 12:29
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.

1 participant