Skip to content

refactor: move analysis DB helpers from server to CLI (Option A deduplication) #248

@melagiri

Description

@melagiri

Context

PR #247 (insights CLI command) introduced ~170 lines of inline duplicate DB helper functions in cli/src/commands/insights.ts because the CLI cannot import from server (circular dependency: server → CLI). This refactor moves the canonical versions from server/src/llm/ to cli/src/analysis/, making the server files thin re-exports.

This follows the exact pattern of PR #238, which moved 9 prompt modules from server/src/llm/ to cli/src/analysis/.

TA-Approved Plan

TA has already reviewed and approved this plan. Dev must follow these specifications exactly.


Files Moving

server/src/llm/analysis-usage-db.tscli/src/analysis/analysis-usage-db.ts

server/src/llm/analysis-db.tscli/src/analysis/analysis-db.ts


After the Move

  1. Server re-exports: Both server files become thin re-exports via @code-insights/cli/analysis/*
  2. Delete inline duplicates: Remove ~170 lines from cli/src/commands/insights.ts (lines 36–212)
  3. Replace with imports: Add imports from ../analysis/analysis-db.js and ../analysis/analysis-usage-db.js
  4. CLI package.json: Add two new exports:
    • "./analysis/analysis-db": "./dist/analysis/analysis-db.js"
    • "./analysis/analysis-usage-db": "./dist/analysis/analysis-usage-db.js"

Key Divergences to Converge

Behavior Server version CLI version Resolution
saveInsightsToDb early return No if (insights.length === 0) return Keep CLI guard
decisions ?? [] / learnings ?? [] No ?? Has ?? [] Keep CLI ?? []
saveFacetsToDb null guards No ?? null Has ?? null Keep CLI guards
DeleteOptions.includeOnlyTypes Yes No Keep — server uses it
SaveAnalysisUsageData.session_message_count No Yes (inline only) Add to interface
ANALYSIS_VERSION in saveFacetsToDb Parameter Hardcoded const Parameter with default

Scope Tags

  • NOT VISUAL (no UI changes)
  • NOT API_CHANGE (server re-exports preserve all public APIs)
  • NOT NEW_DEPS
  • Tier 1 verification only: pnpm build + cd cli && pnpm test + cd server && pnpm test

TDD Requirement

Tests are required. Write or update tests in cli/src/analysis/__tests__/ for:

  • saveInsightsToDb — happy path, empty input early return
  • deleteSessionInsights — excludeTypes, includeOnlyTypes, excludeIds
  • saveFacetsToDb — with and without optional fields (null guards)
  • saveAnalysisUsage — ON CONFLICT upsert behavior, session_message_count write
  • getSessionAnalysisUsage — returns empty array for unknown session
  • convertToInsightRows — decisions/learnings with missing confidence (boundary)
  • convertPQToInsightRow — category normalization at write time

File Pointers

File Role
server/src/llm/analysis-db.ts Source (canonical DB helpers)
server/src/llm/analysis-usage-db.ts Source (canonical usage helpers)
cli/src/commands/insights.ts Contains ~170 lines of inline duplicates to delete
cli/src/analysis/ Target directory (existing PR #238 modules live here)
cli/package.json Needs 2 new exports
cli/src/analysis/__tests__/ TDD test target

Verification

# From repo root
pnpm build           # Zero errors required

# Per-package tests
cd cli && pnpm test   # All tests pass
cd server && pnpm test # All tests pass

No PR until all three pass.


References

Metadata

Metadata

Assignees

No one assigned

    Labels

    infrastructureBuild, CI, tooling, refactoring

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions