diff --git a/backend/ai-core/internal/background/thinker.go b/backend/ai-core/internal/background/thinker.go index 2a2ca3c..b0de429 100644 --- a/backend/ai-core/internal/background/thinker.go +++ b/backend/ai-core/internal/background/thinker.go @@ -1080,8 +1080,8 @@ func (t *Thinker) HasPendingThoughts() bool { // 强制执行最小间隔,防止并发调用或 bug 导致 LLM 配额被快速消耗。 func (t *Thinker) performThink(triggerReason string) { t.muLock() - defer t.muUnlock() gapSinceLast := time.Since(t.lastThinkTimeAtomic()) + t.muUnlock() minGap := t.minThinkGap if minGap <= 0 { minGap = 5 * time.Second // 默认最小间隔 5 秒 @@ -1101,15 +1101,14 @@ func (t *Thinker) performThink(triggerReason string) { ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) defer cancel() t.muLock() - defer t.muUnlock() t.thinkCancel = cancel - + t.muUnlock() // 0. 让步于前台——如果用户最近有活动(非post_chat),跳过本次思考。 if triggerReason != "post_chat" { t.muLock() - defer t.muUnlock() sinceLastUser := time.Since(t.lastUserTime()) + t.muUnlock() if sinceLastUser < 10*time.Second { log.Printf("[后台思考] 用户 %v 前有活动,让步于前台回复,跳过思考", sinceLastUser.Round(time.Second)) @@ -1122,8 +1121,8 @@ func (t *Thinker) performThink(triggerReason string) { // 0. 让步于前台——如果用户最近有活动(非post_chat),跳过本次思考。 if triggerReason != "post_chat" { t.muLock() - defer t.muUnlock() sinceLastUser := time.Since(t.lastUserTime()) + t.muUnlock() if sinceLastUser < 10*time.Second { log.Printf("[后台思考] 用户 %v 前有活动,让步于前台回复,跳过思考", sinceLastUser.Round(time.Second))