`packages/extension-core/src/background.ts` is 1511 lines — by far the largest file in the repo. It currently bundles:
- The pure decision function `handleServerHello` (security-critical, well-isolated, ~250 lines)
- The local `declare const chrome` for chrome.* APIs used by the SW
- All `chrome.storage.local` / `chrome.action` / `chrome.alarms` / `chrome.tabs` / `chrome.cookies` / `chrome.webRequest` handlers
- WS connect/reconnect/encrypted-frame routing
- Per-verb `handleXxxRequest` (fetch / read-storage / read-cookies / capture-request-header / read-indexed-db)
- The badge / auto-popup helpers
- `maybeBoot()` lifecycle wiring
Cohesive — most pieces touch the same shared state Maps. A naive split would create circular imports between request-handler modules and the shared-state module.
Reasonable split, if you do it
- Extract `handleServerHello` (already pure) + supporting helpers into `src/handle-server-hello.ts` (~300 lines, cleanly testable today)
- Extract per-verb handlers into `src/handlers/` — `fetch.ts`, `read-storage.ts`, `read-cookies.ts`, `capture-request-header.ts`, `read-indexed-db.ts`. Pass the per-mcpId scope tables as args (don't import them as module-globals)
- Extract badge logic into `src/badge.ts` (~50 lines, self-contained)
- Keep background.ts as the SW entry point: imports, lifecycle, WS plumbing, shared state Maps, small dispatchers
Cost / benefit
- Benefit: smaller files easier to reason about; `handleServerHello` unit-testable without SW boot scaffolding
- Cost: real refactor; touches every test that mocks `chrome.*`; risk of subtle behavior change in SW boot order
Worth doing the next time someone is in this file substantively (adding a new capability handler, refactoring trust storage). Not a standalone change.
`packages/extension-core/src/background.ts` is 1511 lines — by far the largest file in the repo. It currently bundles:
Cohesive — most pieces touch the same shared state Maps. A naive split would create circular imports between request-handler modules and the shared-state module.
Reasonable split, if you do it
Cost / benefit
Worth doing the next time someone is in this file substantively (adding a new capability handler, refactoring trust storage). Not a standalone change.