From 8546112abe4b12ee5f4f72de7cadd354aa67e5da Mon Sep 17 00:00:00 2001 From: rcourtman Date: Sun, 21 Dec 2025 23:03:41 +0000 Subject: [PATCH] perf: skip initial patrol if one ran recently When the service restarts, it now checks if a patrol ran within the last hour. If so, it skips the initial patrol to avoid wasting API tokens during development/maintenance when the service is restarted frequently. The scheduled patrol runs (every 6 hours) are not affected. --- internal/ai/patrol.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/ai/patrol.go b/internal/ai/patrol.go index b363c50..911d917 100644 --- a/internal/ai/patrol.go +++ b/internal/ai/patrol.go @@ -641,11 +641,28 @@ func (p *PatrolService) Stop() { // patrolLoop is the main background loop func (p *PatrolService) patrolLoop(ctx context.Context) { - // Run initial quick patrol shortly after startup + // Run initial patrol shortly after startup, but only if one hasn't run recently initialDelay := 30 * time.Second select { case <-time.After(initialDelay): - p.runPatrol(ctx) + // Check if a patrol ran recently (within last hour) to avoid wasting tokens on restarts + runHistory := p.GetRunHistory(1) + + skipInitial := false + if len(runHistory) > 0 { + lastRun := runHistory[0] + timeSinceLastRun := time.Since(lastRun.CompletedAt) + if timeSinceLastRun < 1*time.Hour { + log.Info(). + Dur("time_since_last", timeSinceLastRun). + Msg("AI Patrol: Skipping initial patrol - recent run exists") + skipInitial = true + } + } + + if !skipInitial { + p.runPatrol(ctx) + } case <-p.stopCh: return case <-ctx.Done():