Skip to content

Commit 9d85c2e

Browse files
author
claude
committed
recommend: prewarm requests don't bump public toolkit count
The prewarm header was added to bypass per-IP rate limits + optional force-refresh; it did NOT skip recordGeneration, so every prewarm call bumped the public /api/toolkit-count counter. That's fine in principle (the request was really served) but visually wrong: a 100-query prewarm run would jump the homepage display from '400 toolkits built' to '500' in a way that reads as inorganic. Wrap each of the four recordGeneration call sites (L1 / L2 / legacy / L3) with 'if !prewarm'. A valid prewarm token now warms the caches without leaving a trace in toolkit_generations. The per-request log line ('[recommend] L1 quick-match hit' etc.) still fires — ops visibility preserved. Scoped intentionally: the prewarm flag comes from a valid token + the X-Stockyard-Prewarm header, so an unauthenticated scraper can't set this header and have it quietly exempt them from the counter. Token lives in STOCKYARD_PREWARM_TOKEN on Railway. Verification: - go build ./internal/site/...: clean - go vet ./internal/site/...: clean - Existing site tests: pass (no recommend-flow tests exist today; the prewarm path is covered by the integration-style /api/recommend call we'll make from the prewarm script post-deploy) - Secret scan: 0 hits
1 parent 6225749 commit 9d85c2e

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

internal/site/recommend.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,9 @@ func (r *Recommender) HandleRecommend(w http.ResponseWriter, req *http.Request)
411411
result := personalize(cached, businessName)
412412
result.Slug = slug
413413
result.Cached = true
414-
r.recordGeneration(slug, desc, "L1", req.RemoteAddr, len(result.Tools))
414+
if !prewarm {
415+
r.recordGeneration(slug, desc, "L1", req.RemoteAddr, len(result.Tools))
416+
}
415417
log.Printf("[recommend] L1 quick-match hit: %q → %s", normalized, slug)
416418
w.Header().Set("Content-Type", "application/json")
417419
json.NewEncoder(w).Encode(result)
@@ -428,7 +430,9 @@ func (r *Recommender) HandleRecommend(w http.ResponseWriter, req *http.Request)
428430
if cached, ok := r.recCache.Get(normalized); ok {
429431
result := personalize(cached, businessName)
430432
result.Cached = true
431-
r.recordGeneration(result.Slug, desc, "L2", req.RemoteAddr, len(result.Tools))
433+
if !prewarm {
434+
r.recordGeneration(result.Slug, desc, "L2", req.RemoteAddr, len(result.Tools))
435+
}
432436
log.Printf("[recommend] L2 cache hit: %q", normalized)
433437
w.Header().Set("Content-Type", "application/json")
434438
json.NewEncoder(w).Encode(result)
@@ -450,7 +454,9 @@ func (r *Recommender) HandleRecommend(w http.ResponseWriter, req *http.Request)
450454
// Migrate to new cache for future hits
451455
r.recCache.Set(normalized, oldSlug, &result)
452456
result2 := personalize(&result, businessName)
453-
r.recordGeneration(oldSlug, desc, "legacy", req.RemoteAddr, len(result2.Tools))
457+
if !prewarm {
458+
r.recordGeneration(oldSlug, desc, "legacy", req.RemoteAddr, len(result2.Tools))
459+
}
454460
log.Printf("[recommend] legacy cache hit (migrated): %q → %s", normalized, oldSlug)
455461
w.Header().Set("Content-Type", "application/json")
456462
json.NewEncoder(w).Encode(result2)
@@ -610,7 +616,9 @@ func (r *Recommender) HandleRecommend(w http.ResponseWriter, req *http.Request)
610616
genSlug, desc, string(resultJSON))
611617

612618
log.Printf("[recommend] L3 %s: %q → %s (%d tools)", modelUsed, normalized, genSlug, len(result.Tools))
613-
r.recordGeneration(genSlug, desc, "L3", req.RemoteAddr, len(result.Tools))
619+
if !prewarm {
620+
r.recordGeneration(genSlug, desc, "L3", req.RemoteAddr, len(result.Tools))
621+
}
614622

615623
finalResult := personalize(result, businessName)
616624
w.Header().Set("Content-Type", "application/json")

0 commit comments

Comments
 (0)