cc-pets 1.4.7 → 1.4.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/Sources/CCPets/CCPetsUsage.m +109 -15
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,25 @@
|
|
|
4
4
|
|
|
5
5
|
## [Unreleased]
|
|
6
6
|
|
|
7
|
+
- 修复长期续写同一个会话的用户,面板上 Codex 的「今日 / 最近 7 天」Token 恒为 0。
|
|
8
|
+
`codex resume` 会一直往**建会话那天**的目录里追加,实测有用户近 8 天的全部 `token_count`
|
|
9
|
+
都写在 15 天前的 `sessions/2026/08/06/` 下。此前候选文件按 `YYYY/MM/DD` 目录名剪枝,
|
|
10
|
+
扫到过期目录就整个收手,这个仍在被写的文件一次都没被打开过——而额度百分比走的是另一条
|
|
11
|
+
按 mtime 找文件的路径,照常显示,于是现象是"额度有、Token 是 0",很难往这里想。
|
|
12
|
+
- 窗口之前的日期目录不再收手,改成继续扫、只收 **mtime** 落在窗口内的文件;连续 60 个
|
|
13
|
+
这样的目录都没捞到活跃文件才停,成本上界与历史长度无关(本机 47 个日期目录实测
|
|
14
|
+
`--status` 耗时无变化)。天数上限用完时同样降级成 mtime 过滤而不是 `return`,
|
|
15
|
+
否则上限之外那个正在续写的旧会话又会被切掉一次。
|
|
16
|
+
- 判据是 mtime 而不是文件里的时间戳:回填或伪造的时间戳不该把三个月前的会话拉回今天。
|
|
17
|
+
两条测试互为对照——事件时间戳新但 mtime 老的不计入,目录老但 mtime 新的必须计入。
|
|
18
|
+
- 当前正在增量读取的会话、以及 FSEvents 这一轮点名的并发会话,一律无条件参与聚合。
|
|
19
|
+
内核已经确认它们刚被写过,这个结论比任何按路径名或 mtime 的推断都可靠。此前额度竞选
|
|
20
|
+
落选的会话直接被丢掉,可它们同样带着新的 `token_count`。
|
|
21
|
+
- 月度归档带上 schema 版本号:旧口径归出来的档本身缺数,且一旦落盘就不再重算,
|
|
22
|
+
换了扫描方式也不会自愈,因此版本对不上就整份丢掉重算一次。
|
|
23
|
+
- 顺带修好了额度兜底路径 `LatestQuotaInRecentSessions`:它共用同一份候选列表,此前
|
|
24
|
+
在这类机器上同样看不见活跃会话,只是碰巧没被触发。
|
|
25
|
+
|
|
7
26
|
- 台词可以按宠物分别配置了。`~/.cc-pets/speech.txt` 仍是所有宠物的通用台词,某一只想换
|
|
8
27
|
口吻时在 `~/.cc-pets/speech/<宠物名>.txt` 里**只写想改的小节**,没写的照旧走通用。
|
|
9
28
|
台词编辑器顶部多了「通用 / 当前宠物」的切换,切换宠物时自动跟随。
|
|
@@ -426,38 +426,82 @@ static NSTimeInterval DayDirectoryEnd(NSURL *year, NSURL *month, NSURL *day) {
|
|
|
426
426
|
return date ? date.timeIntervalSince1970 + 86400.0 : 0;
|
|
427
427
|
}
|
|
428
428
|
|
|
429
|
+
// 目录名只说明"会话是哪天建的",不说明"会话哪天还在被写":`codex resume` 会一直往
|
|
430
|
+
// 建会话那天的目录里追加,实测有用户近 8 天的全部 token_count 都写在 15 天前的
|
|
431
|
+
// sessions/2026/08/06/ 里。原实现在这里按目录名直接 return,那台机器上 urls 恒为空,
|
|
432
|
+
// 今日 / 最近 7 天的 Token 于是恒为 0(额度却正常——它走 NewestCodexSessionURL,按 mtime)。
|
|
433
|
+
// 所以窗口之前的目录不再收手,而是继续扫、只收 mtime 落在窗口内的文件。
|
|
434
|
+
// mtime 也是 Claude 侧 AppendRecentTranscriptsInDirectory 用的判据,两边口径就此一致。
|
|
435
|
+
static const NSUInteger ResumeLookbackDays = 60;
|
|
436
|
+
|
|
429
437
|
// earliest 由调用方按当前口径算好:订阅视图是 8 天,API 视图是本月 1 日(上月已归档)或
|
|
430
438
|
// 上月 1 日(还没归档,要扫一次把归档建出来),三者都不会早于 8 天下限。
|
|
431
439
|
static NSArray<NSURL *> *RecentCodexSessionURLs(NSURL *sessionsURL, NSTimeInterval earliest) {
|
|
432
440
|
NSMutableArray<NSURL *> *urls = [NSMutableArray array];
|
|
433
441
|
NSUInteger examinedDays = 0;
|
|
442
|
+
// 连续多少个"整天落在窗口之前、且一个活跃文件都没有"的目录。成本上界与历史长度无关:
|
|
443
|
+
// 续写旧会话的用户在这个计数清零之前就被捞到,闲置多年的历史最多多读 60 次目录项。
|
|
444
|
+
NSUInteger staleDays = 0;
|
|
434
445
|
// 目录个数的上限跟着时间窗口走,再多留几天处理时区边界和异常目录。
|
|
435
446
|
const NSUInteger maximumTokenDays =
|
|
436
447
|
(NSUInteger)ceil((NSDate.date.timeIntervalSince1970 - earliest) / 86400.0) + 3;
|
|
448
|
+
NSArray<NSURLResourceKey> *keys = @[NSURLIsRegularFileKey, NSURLContentModificationDateKey];
|
|
437
449
|
for (NSURL *year in DescendingNumericSubdirectories(sessionsURL)) {
|
|
438
450
|
for (NSURL *month in DescendingNumericSubdirectories(year)) {
|
|
439
451
|
for (NSURL *day in DescendingNumericSubdirectories(month)) {
|
|
440
|
-
//
|
|
441
|
-
//
|
|
452
|
+
// 天数上限用完之后不是收手,而是降级成同一套 mtime 过滤:直接 return 会
|
|
453
|
+
// 把上限之外那个仍在续写的旧会话又一次切掉。目录名解析不出日期(dayEnd 为 0)
|
|
454
|
+
// 时按窗口内处理,交给天数上限收手,不误删。
|
|
442
455
|
NSTimeInterval dayEnd = DayDirectoryEnd(year, month, day);
|
|
443
|
-
|
|
444
|
-
|
|
456
|
+
BOOL beforeWindow = (dayEnd > 0 && dayEnd < earliest) ||
|
|
457
|
+
examinedDays >= maximumTokenDays;
|
|
458
|
+
if (beforeWindow && staleDays >= ResumeLookbackDays) return urls;
|
|
445
459
|
NSArray<NSURL *> *contents = [NSFileManager.defaultManager contentsOfDirectoryAtURL:day
|
|
446
460
|
includingPropertiesForKeys:keys options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
|
|
461
|
+
BOOL matched = NO;
|
|
447
462
|
for (NSURL *url in contents) {
|
|
448
463
|
if (![url.pathExtension.lowercaseString isEqualToString:@"jsonl"]) continue;
|
|
449
|
-
|
|
450
|
-
[
|
|
451
|
-
|
|
464
|
+
NSDictionary *values = [url resourceValuesForKeys:keys error:nil];
|
|
465
|
+
if (![values[NSURLIsRegularFileKey] boolValue]) continue;
|
|
466
|
+
// 窗口内的目录整目录都要(文件本身可能刚建、mtime 还没落后),窗口之前的
|
|
467
|
+
// 目录只认最近被写过的文件——判据是 mtime,不是文件里的时间戳:伪造/回填
|
|
468
|
+
// 的时间戳不该把一份三个月前的会话拉回今天。
|
|
469
|
+
if (beforeWindow) {
|
|
470
|
+
NSDate *modified = values[NSURLContentModificationDateKey];
|
|
471
|
+
if (!modified || modified.timeIntervalSince1970 < earliest) continue;
|
|
472
|
+
}
|
|
473
|
+
[urls addObject:url];
|
|
474
|
+
matched = YES;
|
|
452
475
|
}
|
|
453
|
-
|
|
454
|
-
|
|
476
|
+
if (beforeWindow) staleDays = matched ? 0 : staleDays + 1;
|
|
477
|
+
else examinedDays += 1;
|
|
455
478
|
}
|
|
456
479
|
}
|
|
457
480
|
}
|
|
458
481
|
return urls;
|
|
459
482
|
}
|
|
460
483
|
|
|
484
|
+
// 目录扫描之外再兜一层:当前正在被增量读取的会话、以及 FSEvents 这一轮点名的会话文件,
|
|
485
|
+
// 无条件参与 Token 聚合。内核已经确认它们刚被写过,这个结论比任何按路径名或 mtime 的
|
|
486
|
+
// 推断都可靠。多喂进来的文件不会让数字虚高——窗口过滤在 TokenTotalsInWindow 里按行内
|
|
487
|
+
// 时间戳做,这里只是保证文件有机会被读到。
|
|
488
|
+
static NSArray<NSURL *> *TokenAggregationURLs(NSURL *sessionsURL, NSTimeInterval earliest,
|
|
489
|
+
NSArray<NSURL *> *pinnedURLs) {
|
|
490
|
+
NSMutableArray<NSURL *> *urls = [RecentCodexSessionURLs(sessionsURL, earliest) mutableCopy];
|
|
491
|
+
if (pinnedURLs.count == 0) return urls;
|
|
492
|
+
NSMutableSet<NSString *> *paths = [NSMutableSet setWithCapacity:urls.count];
|
|
493
|
+
for (NSURL *url in urls) [paths addObject:url.path];
|
|
494
|
+
for (NSURL *url in pinnedURLs) {
|
|
495
|
+
if (!url.path.length || [paths containsObject:url.path]) continue;
|
|
496
|
+
// 会话文件可能已经被删/被轮转,PruneTokenCache 拿这份列表当保留集,塞不存在的
|
|
497
|
+
// 路径进去会让缓存条目永远清不掉。
|
|
498
|
+
if (![NSFileManager.defaultManager fileExistsAtPath:url.path]) continue;
|
|
499
|
+
[paths addObject:url.path];
|
|
500
|
+
[urls addObject:url];
|
|
501
|
+
}
|
|
502
|
+
return urls;
|
|
503
|
+
}
|
|
504
|
+
|
|
461
505
|
static NSData *SessionTailData(NSURL *url) {
|
|
462
506
|
NSFileHandle *handle = [NSFileHandle fileHandleForReadingFromURL:url error:nil];
|
|
463
507
|
if (!handle) return nil;
|
|
@@ -1003,6 +1047,22 @@ static NSMutableDictionary *LoadTokenSummaryCache(NSString *path) {
|
|
|
1003
1047
|
return cache;
|
|
1004
1048
|
}
|
|
1005
1049
|
|
|
1050
|
+
// Codex 归档是"上月已经算完"的物化结果,一旦落盘就再也不重算(见 UsageByAddingTokenTotals
|
|
1051
|
+
// 里的 buildsArchive)。旧版本按日期目录名剪枝,长期续写旧会话的机器归出来的档本身就是缺数的,
|
|
1052
|
+
// 光换扫描口径不会自愈。版本号对不上就整份丢掉,让下一轮重新归一次。
|
|
1053
|
+
static NSString *const MonthlyArchiveSchemaKey = @"schema";
|
|
1054
|
+
static const NSInteger MonthlyArchiveSchemaVersion = 2;
|
|
1055
|
+
|
|
1056
|
+
static NSMutableDictionary *LoadMonthlyUsageArchive(NSString *path) {
|
|
1057
|
+
NSMutableDictionary *archive = LoadTokenSummaryCache(path);
|
|
1058
|
+
NSDictionary *schema = [archive[MonthlyArchiveSchemaKey] isKindOfClass:NSDictionary.class]
|
|
1059
|
+
? archive[MonthlyArchiveSchemaKey] : nil;
|
|
1060
|
+
if ([schema[@"version"] integerValue] != MonthlyArchiveSchemaVersion) [archive removeAllObjects];
|
|
1061
|
+
// PruneMonthlyArchive 按 YYYY-MM 的字典序裁剪,"schema" 排在所有月份键之后,不会被裁掉。
|
|
1062
|
+
archive[MonthlyArchiveSchemaKey] = @{@"version": @(MonthlyArchiveSchemaVersion)};
|
|
1063
|
+
return archive;
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1006
1066
|
// NSData 是类簇,可变性判断不能靠 isKindOfClass:二进制 plist 解析出来的**不可变**
|
|
1007
1067
|
// __NSCFData 对 isKindOfClass:NSMutableData 同样返回 YES。据此把只读缓冲区当成可变的
|
|
1008
1068
|
// 原地 appendBytes,就会写到它末尾之外——ASan 抓到的正是这一下:
|
|
@@ -1066,7 +1126,8 @@ static void PruneMonthlyArchive(NSMutableDictionary *archive) {
|
|
|
1066
1126
|
// 收窄到本月,上月的会话文件再也不会被打开;archiveChanged 置位表示这一轮新写了归档,
|
|
1067
1127
|
// 调用方据此落盘。
|
|
1068
1128
|
static NSDictionary *UsageByAddingTokenTotals(NSDictionary *usage, NSURL *sessionsURL,
|
|
1069
|
-
NSMutableDictionary *cache, NSMutableDictionary *archive,
|
|
1129
|
+
NSArray<NSURL *> *pinnedURLs, NSMutableDictionary *cache, NSMutableDictionary *archive,
|
|
1130
|
+
BOOL *archiveChanged) {
|
|
1070
1131
|
if (![usage isKindOfClass:NSDictionary.class]) usage = @{};
|
|
1071
1132
|
BOOL apiMode = ProviderShowsAPIUsage(CodexUsageDisplayModeKey);
|
|
1072
1133
|
NSTimeInterval rollingFloor = RollingTokenWindowFloor();
|
|
@@ -1084,7 +1145,7 @@ static NSDictionary *UsageByAddingTokenTotals(NSDictionary *usage, NSURL *sessio
|
|
|
1084
1145
|
BOOL buildsArchive = apiMode && !archivedDaily;
|
|
1085
1146
|
NSTimeInterval earliest = buildsArchive ? previousMonthStart
|
|
1086
1147
|
: (apiMode ? fmin(monthStart, rollingFloor) : rollingFloor);
|
|
1087
|
-
NSArray<NSURL *> *urls =
|
|
1148
|
+
NSArray<NSURL *> *urls = TokenAggregationURLs(sessionsURL, earliest, pinnedURLs);
|
|
1088
1149
|
NSDictionary *five = [usage[@"fiveHour"] isKindOfClass:NSDictionary.class]
|
|
1089
1150
|
? usage[@"fiveHour"] : nil;
|
|
1090
1151
|
NSDictionary *week = [usage[@"week"] isKindOfClass:NSDictionary.class] ? usage[@"week"] : nil;
|
|
@@ -1162,8 +1223,9 @@ NSDictionary *LatestUsage(void) {
|
|
|
1162
1223
|
NSDictionary *usage = LatestUsageInData(tail, &exhaustedAt)
|
|
1163
1224
|
?: LatestQuotaInRecentSessions(sessions, newest);
|
|
1164
1225
|
// --status 这类一次性调用不带缓存也不写归档:它只需要当下这一份数字。
|
|
1165
|
-
usage = UsageByAddingTokenTotals(usage, sessions, [
|
|
1166
|
-
|
|
1226
|
+
usage = UsageByAddingTokenTotals(usage, sessions, newest ? @[newest] : nil,
|
|
1227
|
+
[NSMutableDictionary dictionary],
|
|
1228
|
+
LoadMonthlyUsageArchive(MonthlyUsageArchivePath(@"codex", sessions.path)), NULL);
|
|
1167
1229
|
return UsageByMarkingExhaustion(usage, exhaustedAt.doubleValue);
|
|
1168
1230
|
}
|
|
1169
1231
|
|
|
@@ -1195,6 +1257,9 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1195
1257
|
@property NSTimeInterval tokenComputedAt;
|
|
1196
1258
|
@property NSTimeInterval exhaustedAt;
|
|
1197
1259
|
@property BOOL forcesTokenAggregation;
|
|
1260
|
+
// 内核已经点名"刚被写过"的会话文件:当前正在增量读取的这个,以及最近几批 FSEvents 报上来的
|
|
1261
|
+
// 并发会话。它们无条件参与 Token 聚合,不受日期目录扫描的取舍影响。
|
|
1262
|
+
@property NSMutableArray<NSURL *> *pinnedSessionURLs;
|
|
1198
1263
|
@end
|
|
1199
1264
|
|
|
1200
1265
|
@implementation CodexUsageReader
|
|
@@ -1205,7 +1270,8 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1205
1270
|
_tokenSummaryCachePath = TokenSummaryCachePath(@"codex", _sessionsURL.path);
|
|
1206
1271
|
_tokenSummaryCache = LoadTokenSummaryCache(_tokenSummaryCachePath);
|
|
1207
1272
|
_monthlyArchivePath = MonthlyUsageArchivePath(@"codex", _sessionsURL.path);
|
|
1208
|
-
_monthlyArchive =
|
|
1273
|
+
_monthlyArchive = LoadMonthlyUsageArchive(_monthlyArchivePath);
|
|
1274
|
+
_pinnedSessionURLs = [NSMutableArray array];
|
|
1209
1275
|
}
|
|
1210
1276
|
return self;
|
|
1211
1277
|
}
|
|
@@ -1230,8 +1296,11 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1230
1296
|
return UsageByMarkingExhaustion(result, self.exhaustedAt);
|
|
1231
1297
|
}
|
|
1232
1298
|
BOOL archiveChanged = NO;
|
|
1299
|
+
// 正在增量读取的会话永远排在钉住列表最前面:它是最不该被漏掉的那一个,而漏掉它
|
|
1300
|
+
// 恰恰是"额度读得出、今日 Token 却是 0"的成因。
|
|
1301
|
+
[self notePinnedSessionURL:self.sessionURL];
|
|
1233
1302
|
NSDictionary *result = UsageByAddingTokenTotals(usage, self.sessionsURL,
|
|
1234
|
-
self.tokenSummaryCache, self.monthlyArchive, &archiveChanged);
|
|
1303
|
+
self.pinnedSessionURLs, self.tokenSummaryCache, self.monthlyArchive, &archiveChanged);
|
|
1235
1304
|
if (archiveChanged) {
|
|
1236
1305
|
PruneMonthlyArchive(self.monthlyArchive);
|
|
1237
1306
|
if (self.persistsCache) SaveTokenSummaryCache(self.monthlyArchive, self.monthlyArchivePath);
|
|
@@ -1248,6 +1317,27 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1248
1317
|
- (void)noteExhaustion:(NSNumber *)timestamp {
|
|
1249
1318
|
if (timestamp.doubleValue > self.exhaustedAt) self.exhaustedAt = timestamp.doubleValue;
|
|
1250
1319
|
}
|
|
1320
|
+
// 最近被写过的会话,最新的排在前面。并发会话不会太多,留 8 个足够覆盖一批 FSEvents;
|
|
1321
|
+
// 更早的那些反正也会被目录扫描按 mtime 捞回来,钉住它们只是白读文件。
|
|
1322
|
+
static const NSUInteger MaximumPinnedSessions = 8;
|
|
1323
|
+
|
|
1324
|
+
- (void)notePinnedSessionURL:(NSURL *)url {
|
|
1325
|
+
if (!url.path.length) return;
|
|
1326
|
+
NSMutableArray<NSURL *> *pinned = self.pinnedSessionURLs;
|
|
1327
|
+
if (!pinned) {
|
|
1328
|
+
pinned = [NSMutableArray array];
|
|
1329
|
+
self.pinnedSessionURLs = pinned;
|
|
1330
|
+
}
|
|
1331
|
+
for (NSUInteger index = 0; index < pinned.count; index++) {
|
|
1332
|
+
if ([pinned[index].path isEqualToString:url.path]) {
|
|
1333
|
+
[pinned removeObjectAtIndex:index];
|
|
1334
|
+
break;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
[pinned insertObject:url atIndex:0];
|
|
1338
|
+
while (pinned.count > MaximumPinnedSessions) [pinned removeLastObject];
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1251
1341
|
- (void)selectSessionURL:(NSURL *)url {
|
|
1252
1342
|
self.sessionURL = url;
|
|
1253
1343
|
self.offset = 0;
|
|
@@ -1256,6 +1346,7 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1256
1346
|
self.usage = nil;
|
|
1257
1347
|
return;
|
|
1258
1348
|
}
|
|
1349
|
+
[self notePinnedSessionURL:url];
|
|
1259
1350
|
NSFileHandle *handle = [NSFileHandle fileHandleForReadingFromURL:url error:nil];
|
|
1260
1351
|
if (!handle) return;
|
|
1261
1352
|
unsigned long long size = [handle seekToEndOfFile];
|
|
@@ -1379,6 +1470,9 @@ static const NSTimeInterval TokenCacheSaveInterval = 60.0;
|
|
|
1379
1470
|
double chosenSampledAt = [self.usage[@"sampledAt"] doubleValue];
|
|
1380
1471
|
for (NSURL *url in urls) {
|
|
1381
1472
|
if (![self isSessionURL:url]) continue;
|
|
1473
|
+
// 额度竞选的输赢与 Token 聚合无关:这一批里每个会话文件都刚被写过,落选的那些
|
|
1474
|
+
// 同样带着新的 token_count。钉住它们,别再让日期目录扫描替内核做一次取舍。
|
|
1475
|
+
[self notePinnedSessionURL:url];
|
|
1382
1476
|
if (self.sessionURL && [url.path isEqualToString:self.sessionURL.path]) {
|
|
1383
1477
|
currentSession = url;
|
|
1384
1478
|
continue;
|