Skip to content

Commit 287d3ef

Browse files
author
Aqsa Aqeel
committed
fix: code review feedback
1 parent 928620f commit 287d3ef

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

cmd/cli/createMsg.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,11 @@ func generateMessage(ctx context.Context, provider llm.Provider, changes string,
356356
func generateMessageWithCache(ctx context.Context, provider llm.Provider, store *store.StoreMethods, providerType types.LLMProvider, changes string, opts *types.GenerationOptions) (string, error) {
357357
startTime := time.Now()
358358

359+
// Determine if this is a first attempt (cache check eligible)
360+
isFirstAttempt := opts == nil || opts.Attempt <= 1
361+
359362
// Check cache first (only for first attempt to avoid caching regenerations)
360-
if opts == nil || opts.Attempt <= 1 {
363+
if isFirstAttempt {
361364
if cachedEntry, found := store.GetCachedMessage(providerType, changes, opts); found {
362365
pterm.Info.Printf("Using cached commit message (saved $%.4f)\n", cachedEntry.Cost)
363366

@@ -369,6 +372,7 @@ func generateMessageWithCache(ctx context.Context, provider llm.Provider, store
369372
TokensUsed: 0, // No tokens used for cached result
370373
Cost: 0, // No cost for cached result
371374
CacheHit: true,
375+
CacheChecked: true,
372376
Timestamp: time.Now().UTC().Format(time.RFC3339),
373377
}
374378

@@ -398,6 +402,7 @@ func generateMessageWithCache(ctx context.Context, provider llm.Provider, store
398402
TokensUsed: inputTokens + outputTokens,
399403
Cost: cost,
400404
CacheHit: false,
405+
CacheChecked: isFirstAttempt, // Only first attempts check cache
401406
Timestamp: time.Now().UTC().Format(time.RFC3339),
402407
}
403408

@@ -416,7 +421,7 @@ func generateMessageWithCache(ctx context.Context, provider llm.Provider, store
416421
}
417422

418423
// Cache the result (only for first attempt)
419-
if opts == nil || opts.Attempt <= 1 {
424+
if isFirstAttempt {
420425
// Store in cache
421426
if cacheErr := store.SetCachedMessage(providerType, changes, opts, message, cost, nil); cacheErr != nil {
422427
// Log cache error but don't fail the generation

internal/usage/usage.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ func (sm *StatsManager) RecordGeneration(event *types.GenerationEvent) error {
7171
sm.stats.FirstUse = now
7272
}
7373

74-
// Update cache stats
75-
if event.CacheHit {
76-
sm.stats.CacheHits++
77-
} else {
78-
sm.stats.CacheMisses++
74+
// Update cache stats (only when cache was actually checked)
75+
if event.CacheChecked {
76+
if event.CacheHit {
77+
sm.stats.CacheHits++
78+
} else {
79+
sm.stats.CacheMisses++
80+
}
7981
}
8082

8183
// Update average generation time

pkg/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ type GenerationEvent struct {
179179
TokensUsed int `json:"tokens_used"`
180180
Cost float64 `json:"cost"`
181181
CacheHit bool `json:"cache_hit"`
182+
CacheChecked bool `json:"cache_checked"`
182183
Timestamp string `json:"timestamp"`
183184
ErrorMessage string `json:"error_message,omitempty"`
184185
}

0 commit comments

Comments
 (0)