Skip to content

feat: analytics, LLM feedback, session intelligence, and compact output#12

Merged
kehoej merged 11 commits intomainfrom
feat/analytics-feedback-token-optimization
Apr 11, 2026
Merged

feat: analytics, LLM feedback, session intelligence, and compact output#12
kehoej merged 11 commits intomainfrom
feat/analytics-feedback-token-optimization

Conversation

@kehoej
Copy link
Copy Markdown
Owner

@kehoej kehoej commented Apr 11, 2026

Summary

Analytics and token optimization for contextception, implemented in 4 phases:

  • Phase 1 — Usage Analytics: Tracks every analyze, analyze-change, and MCP get_context call in history.sqlite. New contextception gain command shows usage dashboard with top files, blast radius trends, and daily activity. Supports --daily/weekly/monthly, --format json|csv export.

  • Phase 2 — LLM Feedback Loop: New rate_context MCP tool lets LLMs submit structured feedback (usefulness rating, which files were useful/unnecessary/missing/modified). New contextception accuracy command computes must-read precision/recall and likely-modify accuracy from this feedback.

  • Phase 3 — Session Intelligence: Parses Claude Code session JSONL files to find files edited without context being checked. New contextception discover command reports missed opportunities with coverage percentages. New contextception session command shows per-session adoption rates. Cross-references usage_log to detect hook-injected context invisible in session files.

  • Phase 4 — Token Optimization: New --compact flag on analyze and analyze-change produces a text summary at ~60-75% fewer tokens than JSON. Hook-context output now uses the compact formatter by default, providing richer context in fewer tokens.

New commands (4 CLI + 1 MCP)

  • contextception gain — usage analytics dashboard
  • contextception accuracy — recommendation quality metrics
  • contextception discover — find files edited without context
  • contextception session — adoption rates per session
  • rate_context MCP tool — structured LLM feedback

New files

  • internal/analyzer/compact.go — compact text formatter
  • internal/cli/gain.go, accuracy.go, discover.go, session_cmd.go — new commands
  • internal/session/ — Claude Code session JSONL parser
  • Tests for all new code

Key design decisions

  • Extends existing history.sqlite (no new databases)
  • Context analyses tracked separately from change analyses to avoid conflating metrics
  • NULL confidence for analyze_change so AVG() excludes it naturally
  • discover/session cross-reference usage_log for hook-injected context (invisible in session JSONL)
  • Feedback links to context analyses preferentially over change reports

Test plan

  • All 24 packages pass (make check)
  • 0 lint issues
  • Every metric verified against JSON ground truth
  • Edge cases: empty state, --since 0, --limit 0
  • Feedback linkage verified (prefers analyze over analyze_change)
  • Accuracy math manually computed and verified to 4 decimal places
  • Compact output field-by-field comparison against JSON (all fields present)
  • Session date display verified with calendar comparison
  • Discover test file filtering verified with/without --all

kehoej added 11 commits April 11, 2026 06:30
Track every analyze, analyze-change, and MCP get_context call in
history.sqlite with file count, blast radius, confidence, and duration.
New `contextception gain` command shows usage dashboard with summary,
daily/weekly/monthly breakdowns, and JSON/CSV export.

Phase 1 of RTK-inspired analytics: deterministic usage tracking that
answers "is contextception being used and how?"
New `rate_context` MCP tool lets LLMs submit structured feedback after
using get_context: which files were useful, unnecessary, or missing.
New `contextception accuracy` command computes must-read precision/recall
and likely-modify accuracy from this feedback data.

Phase 2 of RTK-inspired analytics: LLM feedback loop for measuring
and improving recommendation quality.
New session parser scans Claude Code JSONL session files to extract
Edit/Write tool calls and contextception MCP usage. Two new commands:

- `contextception discover` finds files edited without get_context
- `contextception session` shows adoption rates across sessions

Phase 3 of RTK-inspired analytics: session intelligence for measuring
contextception adoption and finding missed opportunities.
New compact text formatter produces ~60-75% fewer tokens than JSON
output while preserving all essential context information. Available
on both analyze and analyze-change commands via --compact flag.

Hook-context output now uses the compact formatter by default,
providing richer context (likely-modify, warnings) in fewer tokens.

Phase 4 of RTK-inspired analytics: token optimization for LLM
context efficiency.
The discover command was showing 0% coverage because hook-context
runs as a PreToolUse hook whose output (additionalContext) is not
recorded in Claude Code session JSONL files. Now cross-references
the usage_log table in history.sqlite to detect hook-injected
context alongside explicit MCP get_context calls.
- gain: separate context analyses from change analyses; store NULL
  confidence for analyze-change so AVG excludes it; top files only
  shows files whose context was analyzed, not PR changed files
- gain: deterministic top-files sort (secondary sort by path)
- accuracy: deduplicate likely_modify denominator by distinct usage_id
  so multiple feedbacks on the same analysis don't inflate the count
- feedback: use JSON-quoted path in usage_id lookup to prevent false
  matches (e.g. "db.go" won't match "db_test.go")
- discover: filter test files regardless of context count, not just
  when context is zero
- session: cross-reference usage_log for hook-injected context
  (same fix as discover)
- session: compare calendar dates instead of hours elapsed for
  relative date display
linkage and compact direction display

- GetUsageByPeriod: exclude analyze_change from SUM(file_count) so
  daily/weekly/monthly periods show context-analyzed files, not PR
  changed files
- FilesWithUsage: filter to context analysis tools so discover/session
  don't count analyze_change files as "context requested"
- RecordFeedback: prefer analyze/get_context entries over
  analyze_change when linking feedback to usage_log
- FormatCompact: handle same_package direction and unknown directions
  instead of silently dropping them

Found during rigorous output validation against ground truth.
…utput

Update every documentation file to reflect the 4 new CLI commands
(gain, accuracy, discover, session), the rate_context MCP tool, and
the --compact flag:

- README.md: add analytics section, update CLI/MCP tool lists, add --compact flag
- CLAUDE.md: fix command count to 16, add session/ package to structure
- docs/ARCHITECTURE.md: add rate_context tool, history/session subsystem docs
- docs/mcp-tutorial.md: add rate_context to tool reference
- integrations/README.md: update to nine tools, add rate_context
- integrations/claude-code/CLAUDE.md: add rate_context workflow step
- integrations/codex/agents.md: add rate_context tool docs and workflow step
- AGENTS.md: update tool count, add history/session packages
- CHANGELOG.md: add [Unreleased] section with all Phase 1-4 changes
Deterministic compact output via sorted map keys, SQL injection safety
(LIKE → instr), lazy history store pooling in MCP server, session --since
flag, unused parameter cleanup, and RateContext protocol documentation.
@kehoej kehoej merged commit 5c3134b into main Apr 11, 2026
2 checks passed
@kehoej kehoej deleted the feat/analytics-feedback-token-optimization branch April 12, 2026 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant