Skip to content

Viewer Code Audit

Inventory of all viewer-related code, cross-package dependencies, and placement recommendation.

File Inventory

Server Code (11 files, 3,124 lines)

All in packages/sourcevision/src/cli/server/:

FileLinesPurposeSourcevision-specific?
start.ts184HTTP server bootstrap, file watchers, route dispatchNo — wires sv + rex + hench routes
types.ts57ServerContext, RouteHandler, json/error helpersNo — generic server utilities
routes-sourcevision.ts166/api/sv/* endpoints (manifest, inventory, imports, zones, components, context)Yes
routes-rex.ts999/api/rex/* endpoints (PRD CRUD, dashboard, analyze, proposals, bulk ops)No — rex domain
routes-hench.ts141/api/hench/* endpoints (run list, run detail)No — hench domain
routes-validation.ts556/api/rex/validate, /api/rex/dependency-graphNo — rex domain
routes-token-usage.ts471/api/token/* endpoints (usage analytics)No — cross-cutting
routes-data.ts140/data/* file serving + live-reload status pollingPartially — reads .sourcevision/ JSON files
routes-static.ts116Serves viewer HTML + PNG assetsNo — generic static file serving
websocket.ts274RFC 6455 implementation (no deps), broadcasts sv/rex/hench changesNo — generic infrastructure
index.ts20Re-exports startServerNo

Only 1 of 11 server files is sourcevision-specific (routes-sourcevision.ts).

Viewer Client Code (57 files, 11,435 lines TypeScript)

All in packages/sourcevision/src/viewer/:

Core (5 files)

FilePurposeSV-specific?
main.tsPreact app entry, view routingNo — orchestrates all views
types.tsLoadedData, ViewId, DetailItem typesPartially — LoadedData types reference SV schema
loader.tsData loading (server vs static mode), pollingPartially — loads .sourcevision/ data
schema-compat.tsSchema version migrationYes
utils.tsUtility functionsPartially — uses SV Zones type

Views (14 files)

FileDomainSV-specific?
views/overview.tsDashboard summaryPartially — shows SV + rex data
views/graph.tsImport dependency graph (D3)Yes
views/zones.tsArchitectural zone mapYes
views/files.tsFile inventory listingYes
views/routes.tsRoute detection/displayYes
views/architecture.tsArchitecture analysisYes
views/problems.tsAnalysis findings/problemsYes
views/suggestions.tsAnalysis suggestionsYes
views/prd.tsPRD tree (CRUD)No — rex domain
views/rex-dashboard.tsRex dashboard (stats, progress)No — rex domain
views/analysis.tsRex analysis logNo — rex domain
views/validation.tsValidation + dependency graphNo — rex domain
views/token-usage.tsToken usage analyticsNo — cross-cutting
views/hench-runs.tsAgent run historyNo — hench domain

7 of 14 views are sourcevision-specific, 5 are rex, 1 is hench, 1 is cross-cutting.

Components (24 files)

FileDomain
components/sidebar.tsShared — nav for all views, imports SV Manifest/Zones types
components/detail-panel.tsSV-specific — file/zone detail display
components/guide.tsShared
components/theme-toggle.tsShared
components/search-filter.tsShared
components/logos.tsShared
components/rex-task-link.tsRex domain — task status badges, context menu for status change
components/faq.tsShared
components/constants.tsShared
components/prd-tree/ (10 files)Rex domain — full PRD tree management UI
components/data-display/ (6 files)Shared/SV — zone-map is SV-specific, rest are generic
graph/physics.tsSV-specific — D3 force simulation
graph/renderer.tsSV-specific — graph rendering

Styles (25 CSS files, 6,721 lines)

All in src/viewer/styles/. Mix of generic layout/theming and domain-specific views:

  • Generic (10): base, tokens, layout, forms, cards, tables, utils, responsive, a11y, branding
  • SV-specific (4): graph, zone-map, overview (mixed), components
  • Rex domain (3): prd-tree, rex-dashboard, task-link
  • Hench domain (1): hench-runs
  • Cross-cutting (4): analysis, token-usage, validation, detail
  • Structural (1): index (imports all others)
  • Other (2): routes, faq

Static Assets (3 files)

  • index.html — root HTML template
  • lightmode_logo.png, darkmode_logo.png — SV logos

Test Files (17 files)

DirectoryFilesDomain
tests/unit/server/7Server routes, websocket, type consistency
tests/unit/viewer/10UI components and views

Build & Dev Files (2 files)

FilePurpose
build.jsesbuild bundler (inlines JS+CSS into single HTML)
dev.jsDev server (tsc watcher + esbuild watcher + serve)

Root Orchestration (1 file)

FilePurpose
web.jsndx web command — spawns sourcevision serve, PID management, port config

Cross-Package Dependencies

Compile-Time Dependencies

The viewer and server code have zero compile-time imports of rex or hench packages.

Sourcevision schema imports (the only compile-time coupling):

  • src/schema/v1.ts — Types: Manifest, Inventory, Imports, Zones, Components, FileEntry, Zone, Finding, RouteTreeNode, RouteExportKind, ComponentUsageEdge
  • src/schema/validate.ts — Validation functions for loading data
  • src/schema/data-files.ts — Constants: DATA_FILES, ALL_DATA_FILES, SUPPLEMENTARY_FILES

These are used by:

  • Server: start.ts, routes-data.ts, routes-sourcevision.ts (3 files)
  • Viewer: main.ts, loader.ts, types.ts, schema-compat.ts, utils.ts, sidebar.ts, zone-map.ts, and 8 view files (13 files total)

Runtime Dependencies (No Imports — File/Process Based)

Rex integration (routes-rex.ts):

  • Reads/writes .rex/prd.json directly via fs
  • Reads .rex/execution-log.jsonl for analysis log
  • Reads .rex/pending-proposals.json for proposal workflow
  • Spawns rex analyze as a subprocess via execFile()
  • Duplicates Rex types locally (Priority, ItemLevel, LEVEL_HIERARCHY, PRIORITY_ORDER) with @see references

Hench integration (routes-hench.ts):

  • Reads .hench/runs/*.json directory via fs
  • No subprocess calls, purely read-only

Token usage (routes-token-usage.ts):

  • Reads .hench/runs/*.json for token analytics
  • Cross-cutting: aggregates data from hench runs

Dependency Summary

              ┌─────────────────────────────────────────────┐
              │  packages/sourcevision/src/cli/server/       │
              │  (HTTP server + all route handlers)          │
              ├─────────────────────────────────────────────┤
              │                                             │
  compile ──► │  src/schema/{v1, validate, data-files}.ts   │
              │                                             │
              │  .sourcevision/*.json  ◄── fs.readFile      │
   runtime ──►│  .rex/prd.json         ◄── fs.read/write   │
              │  .hench/runs/*.json    ◄── fs.readdir       │
              │  rex CLI               ◄── child_process    │
              │                                             │
              └─────────────────────────────────────────────┘

Classification

Sourcevision-Specific Code

Files that genuinely belong to the sourcevision package — they display or serve sourcevision analysis results:

  • Server: routes-sourcevision.ts (1 file, 166 lines)
  • Views: graph, zones, files, routes, architecture, problems, suggestions (7 files)
  • Components: detail-panel, graph/physics, graph/renderer, data-display/zone-map (4 files)
  • Core: schema-compat (1 file)
  • Styles: graph, zone-map (2 files)

Total SV-specific: ~15 files

N-DX Dashboard Code (Should Live at Orchestration Layer)

Files that serve or display rex, hench, or cross-cutting data:

  • Server: start, types, routes-rex, routes-hench, routes-validation, routes-token-usage, routes-data, routes-static, websocket, index (10 files, ~2,958 lines)
  • Views: prd, rex-dashboard, analysis, validation, token-usage, hench-runs (6 files)
  • Components: rex-task-link, prd-tree/* (11 files)
  • Core: main (app shell), loader (data fetching), types (shared types)
  • Styles: prd-tree, rex-dashboard, task-link, hench-runs, analysis, token-usage, validation (7 files)
  • Build: build.js, dev.js

Total n-dx-level: ~40 files

Shared/Generic Code

Files that are framework infrastructure, used by both SV-specific and n-dx views:

  • Components: sidebar, guide, theme-toggle, search-filter, logos, faq, constants, most of data-display/* (8+ files)
  • Styles: base, tokens, layout, forms, cards, tables, utils, responsive, a11y, branding (10 files)
  • Assets: index.html, logos

Decision: Where Should This Code Live?

Recommendation: packages/web

Create a new packages/web package to house the unified dashboard.

Rationale:

  1. The dashboard is not a sourcevision viewer. 10 of 11 server files serve non-sourcevision data. 7 of 14 views are not sourcevision-specific. The server watches and serves .rex/ and .hench/ data with the same priority as .sourcevision/ data.

  2. Sourcevision should remain independently installable. When someone installs just sourcevision (e.g., npx sourcevision analyze), they shouldn't get rex PRD management UI, hench run viewers, and token analytics bundled in.

  3. The root web.js already treats the viewer as orchestration-level. It lives at the monorepo root and delegates to sourcevision serve — acknowledging this is an n-dx concern, not a sourcevision concern.

  4. The existing MCP factory pattern supports this. Rex and sourcevision both export createXxxMcpServer() factories. The web package can import these to mount MCP over HTTP alongside the REST API routes.

  5. Clean dependency direction. packages/web depends on packages/sourcevision (schema types + MCP factory), packages/rex (MCP factory), and reads hench data from filesystem. No circular dependencies.

Alternative Considered: Monorepo Root

Placing web code at the monorepo root (alongside web.js, cli.js, config.js) was considered but rejected:

  • Root-level code is currently thin orchestration scripts (50-300 lines each)
  • The viewer is a substantial codebase (~100 files, ~21K lines)
  • A proper package provides its own package.json, tsconfig, test setup, and build pipeline
  • Root-level doesn't support pnpm build / pnpm test per-package workflow

Migration Approach (For Subsequent Tasks)

  1. Create packages/web with its own package.json, tsconfig, build.js
  2. Move all server code (src/cli/server/*) → packages/web/src/server/
  3. Move all viewer code (src/viewer/*) → packages/web/src/viewer/
  4. Move tests (tests/unit/server/*, tests/unit/viewer/*) → packages/web/tests/
  5. Add dependencies: @n-dx/sourcevision (for schema types), @n-dx/rex (for MCP factory)
  6. Copy the 3 schema files (v1.ts, validate.ts, data-files.ts) or re-export from sourcevision's public API
  7. Update web.js to delegate to packages/web instead of sourcevision serve
  8. Remove viewer/server code from sourcevision, keep sourcevision's serve command as a thin wrapper or remove it
  9. Update build pipeline and CI

Released under the Elastic License 2.0.