cc-pets 1.3.0 → 1.3.1

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 CHANGED
@@ -4,6 +4,21 @@
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ - 修复长期使用后桌宠崩溃、且此后每次启动都在 0.2 秒内再崩(表现为"打不开了"):Claude
8
+ Token 摘要缓存落盘后,下次启动读回时会对同一块缓冲区继续追加新记录,而判断它可不可变
9
+ 用的是 `isKindOfClass:NSMutableData`——`NSData` 是类簇,二进制 plist 解析出来的**不可变**
10
+ `__NSCFData` 对这个判断同样返回 YES。于是只读缓冲区被当成可变的原地 `appendBytes`,
11
+ 每条记录写出界 48 字节(ASan:`heap-buffer-overflow`,`0 bytes after 6640-byte region`,
12
+ 分配点在 `__CFBinaryPlistCreateObjectFiltered`)。越界写破坏的是堆元数据,进程不会当场
13
+ 死,而是在之后任意一次内存分配上倒下,所以三份崩溃报告的落点(`EnumerateLines`、
14
+ `SaveTokenSummaryCache`)和真正的写入点毫无关系,只是先撞上的受害者。
15
+ 触发要同时满足"缓存已落盘、重启后读回、该会话文件之后又变长",因此新装的机器碰不到,
16
+ 攒够会话的老用户则一旦中招每次启动必崩。改为缓存一读回来就把记录换成本进程创建的可变
17
+ 副本,只替换记录本身、其余字段原样保留(Codex 侧条目形状不同,整条重建会抹掉
18
+ `start`/`total`/`segments`/`days`);追加处那个失效的可变性判断一并删除,避免再写回去。
19
+ - `cc-pets --clean` 补上 Token 摘要缓存(`*-token-cache-*.plist`、`*-monthly-usage-*.plist`
20
+ 及落盘中断留下的 `.tmp`)。它同样只是缓存,丢了只意味着重新解析一遍会话文件,原先却漏在
21
+ 清理之外,于是缓存一旦出问题,`clean` 救不回来,只能让用户手动删文件。
7
22
  - 修复面板上的剩余额度来回跳(实测 Claude 5 小时窗口 99% → 94% → 又回 99%):多个会话的
8
23
  status line 各写各的额度快照到同一个文件,而"响应更早、落盘更晚"在并发下完全正常,
9
24
  无条件覆盖会让旧快照盖掉新快照。改为逐窗口按"同一 `resets_at` 内用量只增不减"取舍,
@@ -24,6 +24,22 @@ static BOOL RemoveIfPresent(NSString *path) {
24
24
  return [NSFileManager.defaultManager removeItemAtPath:path error:nil];
25
25
  }
26
26
 
27
+ // Token 摘要缓存按数据源目录分文件(<provider>-token-cache-<hash>.plist、
28
+ // <provider>-monthly-usage-<hash>.plist),同一台机器可以有多份 CODEX_HOME / 配置目录,
29
+ // 份数和 hash 都不固定,因此按名字标记枚举,而不是拼具体路径。
30
+ // 落盘走临时文件替换,中途崩溃会留下同名的 .tmp,一并清掉。
31
+ static NSUInteger RemoveTokenCaches(NSString *supportDirectory) {
32
+ NSArray<NSString *> *entries =
33
+ [NSFileManager.defaultManager contentsOfDirectoryAtPath:supportDirectory error:nil];
34
+ NSUInteger removed = 0;
35
+ for (NSString *entry in entries) {
36
+ if ([entry rangeOfString:@"-token-cache-"].location == NSNotFound &&
37
+ [entry rangeOfString:@"-monthly-usage-"].location == NSNotFound) continue;
38
+ if (RemoveIfPresent([supportDirectory stringByAppendingPathComponent:entry])) removed += 1;
39
+ }
40
+ return removed;
41
+ }
42
+
27
43
  // statusline 写者不依赖桌宠进程存在,所以 RuntimeIsActive() 不能证明额度文件无人使用。
28
44
  // 清理额度缓存前必须拿到同一把锁;锁文件本身刻意长期保留,持锁时 unlink 仍会让后来者
29
45
  // 创建新 inode,导致新旧两把锁同时存在、互斥失效。
@@ -136,6 +152,9 @@ int CleanCCPetsData(BOOL purge) {
136
152
  ClientStateDirectory(), buildCache ?: @""]) {
137
153
  if (RemoveIfPresent(path)) removed += 1;
138
154
  }
155
+ // Token 摘要缓存也是纯缓存(丢了只是重新解析一遍会话文件),原先漏在清理之外,
156
+ // 于是缓存一旦出问题,cc-pets clean 救不回来,只能让用户手动删文件。
157
+ removed += RemoveTokenCaches(supportDirectory);
139
158
  NSString *updateLog = [supportDirectory stringByAppendingPathComponent:@"update.log"];
140
159
  if (RemoveIfPresent(updateLog)) removed += 1;
141
160
  if (purge) {
@@ -978,6 +978,29 @@ static NSMutableDictionary *LoadTokenSummaryCache(NSString *path) {
978
978
  return cache;
979
979
  }
980
980
 
981
+ // NSData 是类簇,可变性判断不能靠 isKindOfClass:二进制 plist 解析出来的**不可变**
982
+ // __NSCFData 对 isKindOfClass:NSMutableData 同样返回 YES。据此把只读缓冲区当成可变的
983
+ // 原地 appendBytes,就会写到它末尾之外——ASan 抓到的正是这一下:
984
+ // heap-buffer-overflow, WRITE of size 48, 0 bytes after 6640-byte region
985
+ // 分配点 __CFBinaryPlistCreateObjectFiltered <- LoadTokenSummaryCache
986
+ // 越界写破坏堆之后进程不会当场死,而是在之后任意一次内存操作上倒下,因此崩溃点看起来
987
+ // 和这里毫无关系(实际报告里落在 EnumerateLines 和 SaveTokenSummaryCache 上)。
988
+ //
989
+ // 可靠的做法只有一条:落盘缓存一读回来就把 records 换成本进程创建的可变副本,
990
+ // 之后 ClaudeRecordsForFile 的原地追加才成立。只替换 records,其余字段原样保留——
991
+ // Codex 侧的条目形状不同(start/total/segments/days…),整条重建会把它们抹掉。
992
+ static void MakeCachedRecordsMutable(NSMutableDictionary *cache) {
993
+ for (NSString *path in cache.allKeys) {
994
+ NSDictionary *entry = cache[path];
995
+ if (![entry isKindOfClass:NSDictionary.class]) continue;
996
+ NSData *records = [entry[@"records"] isKindOfClass:NSData.class] ? entry[@"records"] : nil;
997
+ if (!records) continue;
998
+ NSMutableDictionary *updated = [entry mutableCopy];
999
+ updated[@"records"] = [records mutableCopy];
1000
+ cache[path] = updated;
1001
+ }
1002
+ }
1003
+
981
1004
  static void SaveTokenSummaryCache(NSDictionary *cache, NSString *path) {
982
1005
  if (!path.length) return;
983
1006
  NSData *data = [NSPropertyListSerialization dataWithPropertyList:cache
@@ -1518,18 +1541,19 @@ static NSData *ClaudeRecordsForFile(NSURL *url, NSMutableDictionary *cache) {
1518
1541
  unsigned long long size = [attributes[NSFileSize] unsignedLongLongValue];
1519
1542
  NSDictionary *cached = cache[url.path];
1520
1543
  unsigned long long offset = [cached[@"offset"] unsignedLongLongValue];
1521
- // 从落盘缓存读回来的可能是不可变的 NSData,追加前先要一份可变副本。
1544
+ // 这里可以直接原地追加:可变性由 MakeCachedRecordsMutable 在缓存读回时统一保证。
1545
+ // 不要退回成 isKindOfClass:NSMutableData 判断——NSData 是类簇,落盘缓存解析出的
1546
+ // 不可变 __NSCFData 对它同样返回 YES,据此原地 appendBytes 会写出界。详见该函数注释。
1522
1547
  NSMutableData *records = [cached[@"records"] isKindOfClass:NSData.class]
1523
- ? ([cached[@"records"] isKindOfClass:NSMutableData.class]
1524
- ? cached[@"records"] : [cached[@"records"] mutableCopy])
1525
- : nil;
1548
+ ? (NSMutableData *)cached[@"records"] : nil;
1526
1549
  if (!records || offset > size) {
1527
1550
  // 首次读取,或文件被改写导致偏移失效,整篇重来。
1528
1551
  offset = 0;
1529
1552
  records = [NSMutableData data];
1530
1553
  }
1531
1554
  if (cached && offset == size) {
1532
- // 落盘缓存回来的那一份刚被复制成可变,写回去,免得每轮都复制一遍。
1555
+ // 正常情况下 records 就是缓存里那一份,不必回写;只有上面因偏移失效重建过
1556
+ // (文件被清空到 0 字节)才是新对象,这时写回去,免得下一轮又重建一遍。
1533
1557
  if (records != cached[@"records"]) {
1534
1558
  cache[url.path] = @{@"offset": @(offset), @"records": records};
1535
1559
  }
@@ -1645,6 +1669,7 @@ static NSArray<NSDictionary *> *ClaudeDailyTokenSeries(NSArray<NSData *> *record
1645
1669
  _projectsURL = ClaudeProjectsURL();
1646
1670
  _recordCachePath = TokenSummaryCachePath(@"claude", _projectsURL.path);
1647
1671
  _recordCache = LoadTokenSummaryCache(_recordCachePath);
1672
+ MakeCachedRecordsMutable(_recordCache);
1648
1673
  _monthlyArchivePath = MonthlyUsageArchivePath(@"claude", _projectsURL.path);
1649
1674
  _monthlyArchive = LoadTokenSummaryCache(_monthlyArchivePath);
1650
1675
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cc-pets",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "A native macOS desktop pet for Codex CLI and Claude Code",
5
5
  "author": "wangxinyusqn",
6
6
  "license": "MIT",