Skip to content

Commit 1a8c85d

Browse files
fix: negative token savings caused by passthrough pollution and wrong avg (#3)
- Stop tracking passthrough commands (0/0 tokens is noise) - Skip tracking when input_tokens == 0 (no output captured) - Use weighted avg savings: SUM(saved)/SUM(input)*100 instead of AVG(pct) Fixes #2
1 parent 7a6a5d1 commit 1a8c85d

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

internal/engine/pipeline.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,30 @@ func (p *Pipeline) Run(command string, args []string) int {
8181
fmt.Fprint(os.Stderr, result.Stderr)
8282
}
8383

84-
// Track
85-
originalCmd := command + " " + strings.Join(fullArgs, " ")
86-
snipCmd := command + " " + strings.Join(finalArgs, " ")
84+
// Track (skip if no input — nothing meaningful to measure)
8785
inputTokens := utils.EstimateTokens(result.Stdout)
88-
outputTokens := utils.EstimateTokens(filtered)
89-
if err := timed.Track(originalCmd, snipCmd, inputTokens, outputTokens); err != nil {
90-
fmt.Fprintf(os.Stderr, "snip: tracking error: %v\n", err)
86+
if inputTokens > 0 {
87+
originalCmd := command + " " + strings.Join(fullArgs, " ")
88+
snipCmd := command + " " + strings.Join(finalArgs, " ")
89+
outputTokens := utils.EstimateTokens(filtered)
90+
if err := timed.Track(originalCmd, snipCmd, inputTokens, outputTokens); err != nil {
91+
fmt.Fprintf(os.Stderr, "snip: tracking error: %v\n", err)
92+
}
9193
}
9294

9395
return result.ExitCode
9496
}
9597

9698
// Passthrough runs a command directly without filtering.
99+
// Passthrough commands are not tracked because the output goes directly
100+
// to stdout — snip never captures it, so token counts would be 0/0.
97101
func (p *Pipeline) Passthrough(command string, args []string) int {
98-
timed := tracking.Start(p.Tracker)
99-
100102
code, err := Passthrough(command, args)
101103
if err != nil {
102104
fmt.Fprintf(os.Stderr, "snip: %v\n", err)
103105
return 1
104106
}
105107

106-
cmd := command + " " + strings.Join(args, " ")
107-
if err := timed.TrackPassthrough(cmd, 0); err != nil {
108-
fmt.Fprintf(os.Stderr, "snip: tracking error: %v\n", err)
109-
}
110-
111108
return code
112109
}
113110

internal/tracking/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const summarySQL = `
2525
SELECT
2626
COUNT(*) as total_commands,
2727
COALESCE(SUM(saved_tokens), 0) as total_saved,
28-
COALESCE(AVG(savings_pct), 0) as avg_savings,
28+
COALESCE(SUM(saved_tokens) * 100.0 / NULLIF(SUM(input_tokens), 0), 0) as avg_savings,
2929
COALESCE(SUM(exec_time_ms), 0) as total_time_ms
3030
FROM commands;
3131
`
@@ -37,7 +37,7 @@ SELECT
3737
SUM(input_tokens) as input_tokens,
3838
SUM(output_tokens) as output_tokens,
3939
SUM(saved_tokens) as saved_tokens,
40-
AVG(savings_pct) as avg_savings
40+
COALESCE(SUM(saved_tokens) * 100.0 / NULLIF(SUM(input_tokens), 0), 0) as avg_savings
4141
FROM commands
4242
WHERE timestamp >= datetime('now', ? || ' days')
4343
GROUP BY date(timestamp)

0 commit comments

Comments
 (0)