feat(stats): introduce privacy-first gains tracking feature @hansipie#162
feat(stats): introduce privacy-first gains tracking feature @hansipie#162hansipie wants to merge 2 commits intoyoanbernabeu:mainfrom
Conversation
Introduce a privacy-first gains tracking feature that automatically records token usage on every search/trace command and exposes a new `grepai stats` command to visualize savings vs grep-based workflows. All data is stored locally in .grepai/stats.json (NDJSON) and never leaves the machine. - New stats/ package: Entry, Recorder (flock-based append), ReadAll, Summarize, HistoryByDay, cross-platform file locking (Unix/Windows) - New cli/stats.go: `grepai stats [--json] [--history] [--limit N]` with lipgloss human-readable output and JSON mode - cli/search.go + cli/trace.go: fire-and-forget stats recording (100ms timeout goroutine, zero latency impact) - mcp/server.go: stats recording on all MCP tool calls + new grepai_stats MCP tool for agent integration - 13 unit tests, all passing with -race
On Windows, creating symlinks requires elevated privileges or Developer Mode. Skip the test gracefully on Windows; keep t.Fatalf on other platforms.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #162 +/- ##
===========================================
+ Coverage 27.16% 44.27% +17.11%
===========================================
Files 32 61 +29
Lines 3711 10335 +6624
===========================================
+ Hits 1008 4576 +3568
- Misses 2620 5361 +2741
- Partials 83 398 +315 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Thanks for the PR — I’m not a maintainer, but as a contributor/user I’m excited to see this merged. Quick question/idea (feel free to ignore if it’s out of scope): since Totally fine if you’d rather keep this PR focused and tackle any TUI alignment as a follow-up. |
Summary
This PR introduces a privacy-first gains monitoring system for grepai. Every
searchandtracecommand now automatically records token usage metrics locally, and a newgrepai statscommand lets users visualize how much they save compared to a traditional grep-based workflow.Motivation
Users had no visibility into the concrete value grepai brings over grep. This feature answers the question: "How many tokens (and dollars) have I actually saved by using grepai?"
What's new
New
stats/packageEntry— one record per search/trace command (timestamp, command type, output mode, result count, tokens consumed, grep-equivalent tokens)Recorder— appends entries as NDJSON to.grepai/stats.jsonusing a file lock (cross-platform:flockon Unix,LockFileExon Windows)ReadAll/Summarize/HistoryByDay— aggregation helpersGrepEquivalentTokens(n)— estimates what grep would have cost:n × 512 tokens × 3(expansion factor for context)nilfor local providers (Ollama, LM Studio)-race, all greenNew
grepai statscommand (cli/stats.go)--json— structured JSON output for scripting/agents--history— per-day breakdown table--limit N— max days shown (default: 30)Auto-recording in
searchandtraceStats are recorded fire-and-forget in a goroutine with a 100 ms timeout — zero impact on search/trace latency. Output is captured to a string (instead of writing directly to stdout) to enable token counting without stdout hijacking.
MCP integration (
mcp/server.go)Stats are recorded for all MCP tool calls:
grepai_search,grepai_trace_callers,grepai_trace_callees,grepai_trace_graph. A newgrepai_statsMCP tool allows AI agents to query savings programmatically.Example output
Testing
Type of Change
How Has This Been Tested?
Test Configuration:
Checklist
golangci-lint runand fixed any issuesScreenshots (if applicable)
Additional Notes
config_test:
On Windows, creating symlinks requires elevated privileges or Developer
Mode. Skip the test gracefully on Windows; keep t.Fatalf on other platforms.