Skip to content

feat: insights CLI command with --native and --hook modes #240

@melagiri

Description

@melagiri

Context

Part of the Native Analysis via Claude Code Hooks feature (Phase 12, v4.8.0).
Plan: docs/plans/2026-03-28-native-analysis-hooks.md

Issue 3 of 6 — Core feature. Depends on #238 and #239.

Problem

Users need a way to trigger session analysis from the CLI, supporting both native mode (Claude Code's own LLM) and provider mode (configured API key).

Solution

Command Interface

code-insights insights <session_id> [options]

Options:
  --native         Use calling AI tool (claude -p) instead of configured LLM
  --hook           Read session context from stdin JSON (for hook invocation)
  --source <tool>  Source tool identifier (default: claude-code)
  --force          Re-analyze even if insights already exist
  --quiet, -q      Suppress output

Behavior Matrix

Invocation session_id source Runner Sync
--hook --native stdin JSON NativeRunner syncSingleFile
--hook (no native) stdin JSON ProviderRunner syncSingleFile
<id> --native positional arg NativeRunner none
<id> positional arg ProviderRunner none

Key Mechanisms

  1. syncSingleFile() — targeted sync of one session file (no full provider scan). Guarantees fresh data before analysis when called from hook.

  2. Resume detection — stores session_message_count in analysis_usage (V8 migration). If message count hasn't changed since last analysis, skip. If changed (session was resumed), re-analyze.

  3. Two analysis calls — session analysis + prompt quality, sequential. Same prompts as existing provider path.

  4. Upsert — DELETE existing insights + INSERT new in a transaction. Latest analysis always wins.

Schema V8 Migration

ALTER TABLE analysis_usage ADD COLUMN session_message_count INTEGER;

New/Modified Files

File Change
cli/src/commands/insights.ts NEW — full command implementation
cli/src/commands/sync.ts MODIFY — export syncSingleFile()
cli/src/db/migrate.ts MODIFY — add V8 migration
cli/src/db/schema.ts MODIFY — bump CURRENT_SCHEMA_VERSION to 8
cli/src/index.ts MODIFY — register insights command

Acceptance Criteria

  • code-insights insights <id> analyzes using configured LLM provider
  • code-insights insights <id> --native analyzes using claude -p
  • --hook reads session_id from stdin JSON, runs syncSingleFile() first
  • --force re-analyzes regardless of existing insights
  • --quiet suppresses all output except errors
  • Resume detection: skips if message_count unchanged, re-analyzes if changed
  • V8 migration adds session_message_count column
  • Both analysis + PQ run sequentially, both save to SQLite
  • Summary line printed: [Code Insights] Session analyzed: N insights, PQ X/100
  • Integration tests with mocked runners

Metadata

Metadata

Assignees

No one assigned

    Labels

    analysisLLM analysis featuresfeatureNew functionalityschema-changeSQLite schema changes (high risk)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions