Skip to content

CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息

fix: 修复低额度通知重复发送

Bob112db4ec30da
diff --git a/CodexBar/Services/Notifications/CodexNotificationService.swift b/CodexBar/Services/Notifications/CodexNotificationService.swift
index 8b22013..6b78536 100644
--- a/CodexBar/Services/Notifications/CodexNotificationService.swift
+++ b/CodexBar/Services/Notifications/CodexNotificationService.swift
@@ -382,7 +382,7 @@ final class CodexNotificationService: NSObject {
)
AppLog.notification.notice("阈值已穿越: \(details, privacy: .public)")
- let dedupKey = "low|\(stateKey)|\(Self.epoch(resetsAt))"
+ let dedupKey = lowQuotaDedupKey(stateKey: stateKey, resetsAt: resetsAt)
send(
.lowQuota(
limitTitle: limit.title,
@@ -528,6 +528,29 @@ final class CodexNotificationService: NSObject {
// MARK: - 去重与持久化
+ /// app-server 的重置时间可能在连接重建后出现秒级漂移, 误差不超过容差时复用旧 key
+ private func lowQuotaDedupKey(stateKey: String, resetsAt: Date) -> String {
+ let prefix = "low|\(stateKey)|"
+ let resetEpoch = Self.epoch(resetsAt)
+ let matchesReset: (String) -> Bool = { key in
+ guard key.hasPrefix(prefix),
+ let existingEpoch = Int(key.dropFirst(prefix.count)) else {
+ return false
+ }
+
+ return abs(TimeInterval(existingEpoch) - TimeInterval(resetEpoch)) <= Self.lowQuotaResetTolerance
+ }
+
+ if let existingKey = sentDedupKeys.first(where: matchesReset) {
+ return existingKey
+ }
+ if let existingKey = submittingDedupKeys.first(where: matchesReset) {
+ return existingKey
+ }
+
+ return "\(prefix)\(resetEpoch)"
+ }
+
private func rememberSentDedupKey(_ key: String) {
sentDedupKeys.append(key)
if sentDedupKeys.count > Self.sentKeysLimit {
@@ -705,6 +728,7 @@ final class CodexNotificationService: NSObject {
private static let taskHapticPulseCount = 10
private static let taskHapticPulseInterval = Duration.milliseconds(100)
private static let notificationSubmissionRetryCount = 1
+ private static let lowQuotaResetTolerance: TimeInterval = 60
private static let sentKeysLimit = 300
private static let sentKeysKey = "Notification.sentKeys"
private static let legacyResetDedupKeyPrefix = "reset|"
diff --git a/Docs/DeveloperGuide/notifications.md b/Docs/DeveloperGuide/notifications.md
index 71eef55..deee02c 100644
--- a/Docs/DeveloperGuide/notifications.md
+++ b/Docs/DeveloperGuide/notifications.md
@@ -41,7 +41,11 @@ View 和其他服务不直接创建系统通知。它们发布状态变化,通
通知只在剩余额度从阈值上方跨到阈值或下方时发送。持续停留在低额度区域不会每次刷新都重复通知。
-额度恢复到阈值上方后,下一次向下跨越可以再次通知。
+首次可信快照已经低于阈值时也视为一次向下穿越,用于覆盖 App 在穿越后才启动的情况。
+
+持久化去重 key 保留 app-server 返回的秒级重置时间。相同账户和额度窗口的重置时间与已记录值相差不超过 60 秒时视为同一周期,只有差值超过 60 秒才视为新的重置周期。
+
+额度恢复到阈值上方后会重新启用穿越判定,但同一重置周期仍只通知一次。进入新的重置周期后,下一次向下跨越可以再次通知。
## Reset Credits 到期
diff --git a/Docs/UserGuide/notifications.md b/Docs/UserGuide/notifications.md
index 9f827c3..13650ae 100644
--- a/Docs/UserGuide/notifications.md
+++ b/Docs/UserGuide/notifications.md
@@ -37,6 +37,8 @@
额度预警只在剩余比例从阈值上方跨到阈值或以下时触发,不会在每次 60 秒刷新时重复发送。
+同一额度重置周期内,退出并重新启动 CodexBar 也不会重复发送额度预警。
+
手动重置次数会在过期前 7, 6, 5, 4, 3, 2 和 1 天分别判断是否需要提醒。
## 通知音效