cc-pets 1.1.31 → 1.2.0

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.
@@ -843,13 +843,16 @@ static NSString *TokenWindowIdentity(NSDictionary *usage) {
843
843
 
844
844
  // MARK: - Claude Code 本地 Token 统计
845
845
  //
846
- // 数据源是 ~/.claude/projects/<编码后的 cwd>/<session-uuid>.jsonl。每条 type=assistant
847
- // 的行带 timestamp 和 message.usage,是**单次请求的增量**,与 Codex 的会话累计值不同,
848
- // 因此必须逐条落窗口累加,不能取末行。
846
+ // 数据源是 ~/.claude/projects/<编码后的 cwd>/<session-uuid>.jsonl,以及同级的
847
+ // <session-uuid>/subagents/agent-*.jsonl(子 agent 的转录,见 RecentClaudeTranscriptURLs)。
848
+ // 每条 type=assistant 的行带 timestamp 和 message.usage,是**单次请求的增量**,
849
+ // 与 Codex 的会话累计值不同,因此必须逐条落窗口累加,不能取末行。
849
850
  //
850
851
  // 关键差异:Claude 的 --resume / fork / compact 会把旧行原样复制进新文件,本机实测
851
852
  // 5575 行里有 692 行重复(12%)。不去重的话 7 天用量会虚高一成以上,所以按
852
- // message.id + requestId 做全局去重(两者缺失时回退 uuid)。
853
+ // message.id + requestId 做全局去重(两者缺失时回退 uuid)。同一次请求还会被拆成多行、
854
+ // 各带一份递增的 usage 快照,所以去重是**逐字段取最大值合并**而不是丢弃后续行,
855
+ // 详见 ClaudeTokenTotals。
853
856
 
854
857
  typedef struct {
855
858
  double timestamp;
@@ -925,28 +928,63 @@ static NSURL *ClaudeProjectsURL(void) {
925
928
  [DefaultClaudeConfigDirectory() stringByAppendingPathComponent:@"projects"]];
926
929
  }
927
930
 
931
+ // 收集目录内 mtime 落在窗口内的 .jsonl;顺带把子目录交给调用方(传 nil 表示不关心),
932
+ // 省一次 readdir。
933
+ static void AppendRecentTranscriptsInDirectory(NSURL *directory, NSTimeInterval earliest,
934
+ NSMutableArray<NSURL *> *urls, NSMutableArray<NSURL *> *outSubdirectories) {
935
+ NSArray<NSURLResourceKey> *keys = @[NSURLContentModificationDateKey, NSURLIsRegularFileKey,
936
+ NSURLIsDirectoryKey];
937
+ NSArray<NSURL *> *contents = [NSFileManager.defaultManager contentsOfDirectoryAtURL:directory
938
+ includingPropertiesForKeys:keys options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
939
+ for (NSURL *url in contents) {
940
+ NSDictionary *values = [url resourceValuesForKeys:keys error:nil];
941
+ if ([values[NSURLIsDirectoryKey] boolValue]) {
942
+ [outSubdirectories addObject:url];
943
+ continue;
944
+ }
945
+ if (![url.pathExtension.lowercaseString isEqualToString:@"jsonl"]) continue;
946
+ if (![values[NSURLIsRegularFileKey] boolValue]) continue;
947
+ NSDate *modified = values[NSURLContentModificationDateKey];
948
+ if (modified && modified.timeIntervalSince1970 < earliest) continue;
949
+ [urls addObject:url];
950
+ }
951
+ }
952
+
928
953
  // 只看 mtime 落在窗口内的转录文件。本机 76 个文件里近 7 天只有 44 个,目录遍历约 3ms。
954
+ //
955
+ // 子 agent(Task/Explore)的转录不写进主会话文件,而是单独落在
956
+ // <project>/<session-uuid>/subagents/agent-*.jsonl,只扫项目目录一层的话它们一条都进不来
957
+ // ——本机主会话文件里 isSidechain 的行数为 0,漏掉的就是全部子 agent 用量(历史上单个
958
+ // 文件到过 269 万 token)。
959
+ //
960
+ // 下钻哪些会话由主会话文件 <session-uuid>.jsonl 的 mtime 决定:Task 的调用和结果都会写进
961
+ // 主会话文件,主文件不在窗口内,它的子 agent 也不可能在。projects/ 目录只增不减(本机
962
+ // 99 个会话里近 8 天只有 27 个),无条件下钻等于给每个历史会话都白跑一次 readdir,而且
963
+ // 多数子目录压根不是 subagents(本机 13 个里 7 个是 tool-results/memory)。
964
+ // 不能改用 <session-uuid>/ 目录自身的 mtime 剪枝:目录 mtime 只在直接子项增删时更新,
965
+ // 往已有 agent-*.jsonl 追加写不会动它,正在跑的长任务会被剪掉。
929
966
  static NSArray<NSURL *> *RecentClaudeTranscriptURLs(NSURL *projectsURL) {
930
967
  NSTimeInterval earliest = EarliestTokenWindowStart();
931
- NSArray<NSURLResourceKey> *keys = @[NSURLIsDirectoryKey];
932
968
  NSArray<NSURL *> *projects = [NSFileManager.defaultManager contentsOfDirectoryAtURL:projectsURL
933
- includingPropertiesForKeys:keys options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
969
+ includingPropertiesForKeys:@[NSURLIsDirectoryKey]
970
+ options:NSDirectoryEnumerationSkipsHiddenFiles error:nil];
934
971
  NSMutableArray<NSURL *> *urls = [NSMutableArray array];
935
- NSArray<NSURLResourceKey> *fileKeys = @[NSURLContentModificationDateKey, NSURLIsRegularFileKey];
936
972
  for (NSURL *project in projects) {
937
973
  NSNumber *isDirectory = nil;
938
974
  if (![project getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil]) continue;
939
975
  if (!isDirectory.boolValue) continue;
940
- NSArray<NSURL *> *contents = [NSFileManager.defaultManager contentsOfDirectoryAtURL:project
941
- includingPropertiesForKeys:fileKeys options:NSDirectoryEnumerationSkipsHiddenFiles
942
- error:nil];
943
- for (NSURL *url in contents) {
944
- if (![url.pathExtension.lowercaseString isEqualToString:@"jsonl"]) continue;
945
- NSDictionary *values = [url resourceValuesForKeys:fileKeys error:nil];
946
- if (![values[NSURLIsRegularFileKey] boolValue]) continue;
947
- NSDate *modified = values[NSURLContentModificationDateKey];
948
- if (modified && modified.timeIntervalSince1970 < earliest) continue;
949
- [urls addObject:url];
976
+ NSMutableArray<NSURL *> *sessions = [NSMutableArray array];
977
+ NSUInteger firstIndex = urls.count;
978
+ AppendRecentTranscriptsInDirectory(project, earliest, urls, sessions);
979
+ NSMutableSet<NSString *> *activeSessions = [NSMutableSet set];
980
+ for (NSUInteger index = firstIndex; index < urls.count; index++) {
981
+ [activeSessions addObject:urls[index].URLByDeletingPathExtension.lastPathComponent];
982
+ }
983
+ for (NSURL *session in sessions) {
984
+ if (![activeSessions containsObject:session.lastPathComponent]) continue;
985
+ AppendRecentTranscriptsInDirectory(
986
+ [session URLByAppendingPathComponent:@"subagents" isDirectory:YES],
987
+ earliest, urls, nil);
950
988
  }
951
989
  }
952
990
  return urls;
@@ -991,10 +1029,15 @@ static NSData *ClaudeRecordsForFile(NSURL *url, NSMutableDictionary *cache) {
991
1029
  return records;
992
1030
  }
993
1031
 
1032
+ // 同一次请求会被拆成多行写入(thinking / text / tool_use 各一行),每行都带一份 usage
1033
+ // 快照,而这些快照是**递增的**:本机实测有 14 组末行的 output_tokens 远大于首行
1034
+ // (6→235、3→1870),首行取到的是流式过程中的中间值。因此同 identity 的多行必须逐字段
1035
+ // 取最大值合并,只保留第一条会少算——全量口径下少算 16060 个 output token。
1036
+ // 取 max 而不是"取最后一行":fork/resume 会把旧行复制进新文件,跨文件的行序没有保证。
994
1037
  static NSDictionary *ClaudeTokenTotals(NSArray<NSData *> *recordSets, NSTimeInterval start,
995
1038
  NSTimeInterval end) {
996
- NSMutableSet<NSNumber *> *seen = [NSMutableSet set];
997
- unsigned long long input = 0, output = 0, cacheRead = 0, cacheWrite = 0;
1039
+ NSMutableDictionary<NSNumber *, NSNumber *> *slots = [NSMutableDictionary dictionary];
1040
+ NSMutableData *merged = [NSMutableData data];
998
1041
  for (NSData *data in recordSets) {
999
1042
  const ClaudeTokenRecord *records = data.bytes;
1000
1043
  NSUInteger count = data.length / sizeof(ClaudeTokenRecord);
@@ -1002,14 +1045,29 @@ static NSDictionary *ClaudeTokenTotals(NSArray<NSData *> *recordSets, NSTimeInte
1002
1045
  const ClaudeTokenRecord *record = &records[index];
1003
1046
  if (record->timestamp < start || record->timestamp >= end) continue;
1004
1047
  NSNumber *identity = @(record->identity);
1005
- if ([seen containsObject:identity]) continue;
1006
- [seen addObject:identity];
1007
- input += record->input;
1008
- output += record->output;
1009
- cacheRead += record->cacheRead;
1010
- cacheWrite += record->cacheWrite;
1048
+ NSNumber *slot = slots[identity];
1049
+ if (!slot) {
1050
+ slots[identity] = @(merged.length / sizeof(ClaudeTokenRecord));
1051
+ [merged appendBytes:record length:sizeof(*record)];
1052
+ continue;
1053
+ }
1054
+ // append 可能搬动缓冲区,基址每次重新取。
1055
+ ClaudeTokenRecord *target =
1056
+ (ClaudeTokenRecord *)merged.mutableBytes + slot.unsignedIntegerValue;
1057
+ target->input = MAX(target->input, record->input);
1058
+ target->output = MAX(target->output, record->output);
1059
+ target->cacheRead = MAX(target->cacheRead, record->cacheRead);
1060
+ target->cacheWrite = MAX(target->cacheWrite, record->cacheWrite);
1011
1061
  }
1012
1062
  }
1063
+ unsigned long long input = 0, output = 0, cacheRead = 0, cacheWrite = 0;
1064
+ const ClaudeTokenRecord *unique = merged.bytes;
1065
+ for (NSUInteger index = 0; index < merged.length / sizeof(ClaudeTokenRecord); index++) {
1066
+ input += unique[index].input;
1067
+ output += unique[index].output;
1068
+ cacheRead += unique[index].cacheRead;
1069
+ cacheWrite += unique[index].cacheWrite;
1070
+ }
1013
1071
  // 不输出 reasoning_output_tokens:Claude 的思考 token 已经计入 output_tokens,
1014
1072
  // 单列一个恒为 0 的字段会让人误以为没统计。
1015
1073
  return @{
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cc-pets",
3
- "version": "1.1.31",
3
+ "version": "1.2.0",
4
4
  "description": "A native macOS desktop pet for Codex CLI and Claude Code",
5
5
  "author": "wangxinyusqn",
6
6
  "license": "MIT",