---
phase: 01-daemon-decomposition
plan: 02
subsystem: infra
tags: [daemon, refactoring, module-extraction, channels, agents, routing]

# Dependency graph
requires:
  - phase: 01-01
    provides: "models.ts extraction reducing daemon/index.ts baseline; memory.ts and tools.ts also pre-extracted"
provides:
  - "src/daemon/routing.ts — message router with agent cache, sandbox wiring, audio transcription"
  - "src/daemon/agents.ts — agent config registry, agent router, sandbox manager initialization"
  - "src/daemon/channels.ts — all channel adapter registration (Telegram, Discord, Slack, WhatsApp, WebChat, cron, webhooks, Gmail)"
affects: [01-daemon-decomposition, 02-config-overlays]

# Tech tracking
tech-stack:
  added: []
  patterns: ["deps-interface factory pattern for module extraction", "lifecycle shutdown wiring in extracted modules"]

key-files:
  created:
    - src/daemon/routing.ts
    - src/daemon/agents.ts
    - src/daemon/channels.ts
  modified:
    - src/daemon/index.ts

key-decisions:
  - "Used deps-interface pattern (ChannelsDeps, AgentsDeps) for extracted factories — explicit dependency injection"
  - "Returned cronScheduler from registerChannels so startDaemon can still wire cron tools in Tier 1 registration"
  - "Accepted pre-existing memory.ts and tools.ts extractions from Plan 01 overflow (already integrated into index.ts)"

patterns-established:
  - "Deps-interface factory: each extracted module takes a typed deps object and returns a typed result"
  - "Lifecycle wiring stays in the module that owns the resource (agents.ts registers sandbox shutdown)"

# Metrics
duration: ~25min
completed: 2026-02-10
---

# Phase 1 Plan 2: Channel/Agent/Routing Extraction Summary

**Extracted message routing (240 lines), agent config (48 lines), and channel registration (102 lines) from daemon/index.ts into dedicated modules, reducing it from ~459 to 385 lines**

## Performance

- **Duration:** ~25 min (across two sessions)
- **Started:** 2026-02-10T04:08:12Z
- **Completed:** 2026-02-10T04:34:17Z
- **Tasks:** 3
- **Files modified:** 4 (3 created, 1 modified)

## Accomplishments
- `createMessageRouter` (~240 lines) extracted to `routing.ts` — agent cache, sandbox wiring, audio transcription, command handling
- `initAgents` (~48 lines) extracted to `agents.ts` — AgentConfigRegistry, AgentRouter, SandboxManager with lifecycle shutdown
- `registerChannels` (~102 lines) extracted to `channels.ts` — all 8 adapter types (Telegram, Discord, Slack, WhatsApp, WebChat, CronScheduler, WebhookHandler, GmailWatcher)
- daemon/index.ts reduced from ~459 lines (post-Plan 01) to 385 lines; from 1087 baseline to 385 (65% reduction cumulative)
- All 1077 tests pass, zero regressions

## Task Commits

Each task was committed atomically:

1. **Task 1: Extract message routing into routing.ts** — `08f5b6b` (feat)
2. **Task 2: Extract agent config into agents.ts** — `efceb38` (feat)
3. **Task 3: Extract channel adapter registration into channels.ts** — `35a0061` (feat)

## Files Created/Modified
- `src/daemon/routing.ts` — Message router factory with agent cache, sandbox wiring, audio transcription, reset/compact/usage command handling (239 lines)
- `src/daemon/agents.ts` — Agent config registry loading, agent router creation, sandbox manager init with lifecycle shutdown (48 lines)
- `src/daemon/channels.ts` — All channel adapter registration: Telegram, Discord, Slack, WhatsApp, WebChat, CronScheduler, WebhookHandler, GmailWatcher (102 lines)
- `src/daemon/index.ts` — Reduced to infrastructure wiring only; imports and calls the three new modules (385 lines)

## Decisions Made
- Used deps-interface factory pattern (`ChannelsDeps`, `AgentsDeps`) for explicit dependency injection rather than passing individual arguments — keeps call sites clean and extensible
- `registerChannels` returns `cronScheduler` (and `webhookHandler`, `gmailWatcher`) so `startDaemon` can still wire cron tools in Tier 1 tool registration
- Accepted pre-existing `memory.ts` and `tools.ts` extractions from Plan 01 session overflow — they were already integrated into index.ts and working

## Deviations from Plan

### Auto-fixed Issues

**1. [Rule 3 - Blocking] Accepted pre-existing memory.ts and tools.ts extractions**
- **Found during:** Task 1 (initial analysis)
- **Issue:** Previous Plan 01 session extracted `src/daemon/memory.ts` and `src/daemon/tools.ts` beyond its scope; both were already integrated into index.ts imports but untracked in git
- **Fix:** Accepted as-is — code was working and tested. These files were committed by the Plan 01 continuation agent (`fb1199a` for tools.ts, `00f8f74` for memory.ts`)
- **Files affected:** src/daemon/memory.ts, src/daemon/tools.ts (not our commits)
- **Verification:** All 1077 tests pass with these extractions in place

---

**Total deviations:** 1 auto-accepted (blocking — pre-existing code)
**Impact on plan:** No scope creep. Pre-existing extractions were already working and simply acknowledged.

## Issues Encountered
None — all three tasks executed cleanly.

## User Setup Required
None — no external service configuration required.

## Next Phase Readiness
- daemon/index.ts now contains only infrastructure wiring (385 lines): session store, hooks, skills, gateway, signal handlers, service startup, heartbeat monitor
- Plan 01-03 (remaining extractions: skills, gateway, lifecycle/infra) can proceed — depends on the output of Plans 01 and 02
- All daemon modules use consistent deps-interface factory pattern

---
*Phase: 01-daemon-decomposition*
*Completed: 2026-02-10*
