@kenkaiiii/gg-core 5.84.2 → 5.85.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.
package/dist/index.cjs CHANGED
@@ -155,7 +155,8 @@ function getAppPaths() {
155
155
  extensionsDir: import_node_path.default.join(agentDir, "extensions"),
156
156
  agentsDir: import_node_path.default.join(agentDir, "agents"),
157
157
  progressFile: import_node_path.default.join(agentDir, "progress.json"),
158
- progressBackupFile: import_node_path.default.join(agentDir, "progress.backup.json")
158
+ progressBackupFile: import_node_path.default.join(agentDir, "progress.backup.json"),
159
+ trophiesFile: import_node_path.default.join(agentDir, "trophies.json")
159
160
  };
160
161
  }
161
162
 
@@ -417,16 +418,19 @@ var import_gg_ai = require("@kenkaiiii/gg-ai");
417
418
  var MAX_BYTES = 10 * 1024 * 1024;
418
419
  var MIN_HEADROOM_BYTES = 64 * 1024;
419
420
  var fd = null;
420
- var bytesWritten = 0;
421
+ var logPath = null;
422
+ var fileBytes = 0;
423
+ var processBytes = 0;
424
+ var rotationFailed = false;
421
425
  var capped = false;
422
426
  var sessionId = "";
423
427
  var appName = "app";
424
428
  var cleanups = [];
425
429
  var exactSecrets = [];
426
- function rotateIfNeeded(filePath) {
430
+ function rotateIfNeeded(filePath, force = false) {
427
431
  try {
428
432
  const st = import_node_fs2.default.statSync(filePath);
429
- if (st.size <= MAX_BYTES - MIN_HEADROOM_BYTES) return;
433
+ if (!force && st.size <= MAX_BYTES - MIN_HEADROOM_BYTES) return;
430
434
  const rotated = `${filePath}.1`;
431
435
  try {
432
436
  import_node_fs2.default.unlinkSync(rotated);
@@ -436,6 +440,32 @@ function rotateIfNeeded(filePath) {
436
440
  } catch {
437
441
  }
438
442
  }
443
+ function rotateOpenLog() {
444
+ if (fd === null || logPath === null) return false;
445
+ const sizeBefore = fileBytes;
446
+ try {
447
+ import_node_fs2.default.closeSync(fd);
448
+ } catch {
449
+ }
450
+ fd = null;
451
+ rotateIfNeeded(logPath, true);
452
+ try {
453
+ fd = import_node_fs2.default.openSync(logPath, "a");
454
+ fileBytes = import_node_fs2.default.fstatSync(fd).size;
455
+ } catch {
456
+ if (fd !== null) {
457
+ try {
458
+ import_node_fs2.default.closeSync(fd);
459
+ } catch {
460
+ }
461
+ }
462
+ fd = null;
463
+ capped = true;
464
+ return false;
465
+ }
466
+ if (fileBytes >= sizeBefore) rotationFailed = true;
467
+ return true;
468
+ }
439
469
  function openLog(filePath, name) {
440
470
  if (fd !== null || capped) return false;
441
471
  appName = name;
@@ -449,24 +479,29 @@ function openLog(filePath, name) {
449
479
  fd = import_node_fs2.default.openSync(filePath, "a");
450
480
  } catch {
451
481
  fd = null;
452
- bytesWritten = 0;
482
+ fileBytes = 0;
453
483
  return false;
454
484
  }
455
485
  try {
456
- bytesWritten = import_node_fs2.default.fstatSync(fd).size;
486
+ fileBytes = import_node_fs2.default.fstatSync(fd).size;
457
487
  } catch {
458
488
  try {
459
489
  import_node_fs2.default.closeSync(fd);
460
490
  } catch {
461
491
  }
462
492
  fd = null;
463
- bytesWritten = 0;
493
+ fileBytes = 0;
464
494
  return false;
465
495
  }
496
+ logPath = filePath;
497
+ processBytes = 0;
498
+ rotationFailed = false;
466
499
  sessionId = (0, import_node_crypto2.randomBytes)(4).toString("hex");
467
500
  try {
468
- if (bytesWritten < MAX_BYTES) {
469
- bytesWritten += import_node_fs2.default.writeSync(fd, "\n");
501
+ if (fileBytes < MAX_BYTES) {
502
+ const written = import_node_fs2.default.writeSync(fd, "\n");
503
+ fileBytes += written;
504
+ processBytes += written;
470
505
  }
471
506
  } catch {
472
507
  }
@@ -494,12 +529,12 @@ function log(level, category, message, data) {
494
529
  }
495
530
  line += "\n";
496
531
  const lineBytes = Buffer.byteLength(line);
497
- if (bytesWritten + lineBytes > MAX_BYTES) {
532
+ if (processBytes + lineBytes > MAX_BYTES) {
498
533
  const capLine = `[${ts}] [sid=${sessionId}] [WARN] [logger] Log cap reached; file logging disabled until restart
499
534
  `;
500
535
  try {
501
- if (bytesWritten + Buffer.byteLength(capLine) <= MAX_BYTES) {
502
- bytesWritten += import_node_fs2.default.writeSync(fd, capLine);
536
+ if (fileBytes + Buffer.byteLength(capLine) <= MAX_BYTES) {
537
+ fileBytes += import_node_fs2.default.writeSync(fd, capLine);
503
538
  }
504
539
  import_node_fs2.default.closeSync(fd);
505
540
  } catch {
@@ -508,8 +543,12 @@ function log(level, category, message, data) {
508
543
  capped = true;
509
544
  return;
510
545
  }
546
+ if (fileBytes + lineBytes > MAX_BYTES && !rotationFailed && !rotateOpenLog()) return;
547
+ if (fd === null) return;
511
548
  try {
512
- bytesWritten += import_node_fs2.default.writeSync(fd, line);
549
+ const written = import_node_fs2.default.writeSync(fd, line);
550
+ fileBytes += written;
551
+ processBytes += written;
513
552
  } catch {
514
553
  }
515
554
  }
@@ -525,7 +564,10 @@ function closeLogger(opts) {
525
564
  }
526
565
  }
527
566
  fd = null;
528
- bytesWritten = 0;
567
+ logPath = null;
568
+ fileBytes = 0;
569
+ processBytes = 0;
570
+ rotationFailed = false;
529
571
  capped = false;
530
572
  exactSecrets = [];
531
573
  for (const unsub of cleanups) unsub();
@@ -2033,9 +2075,20 @@ var AuthStorage = class {
2033
2075
  const msg = err instanceof Error ? err.message : String(err);
2034
2076
  const isAuthFailure = /\((401|400)\)/.test(msg) || /invalid_grant|invalid_token|invalid.*refresh/i.test(msg) || /unauthorized/i.test(msg);
2035
2077
  if (isAuthFailure) {
2036
- delete latest[provider];
2037
- this.data = latest;
2038
- await atomicWriteFile(this.filePath, JSON.stringify(latest, null, 2));
2078
+ const current = await readAuthData(this.filePath);
2079
+ const currentCreds = current[provider];
2080
+ if (currentCreds && currentCreds.refreshToken !== latestCreds.refreshToken) {
2081
+ log(
2082
+ "WARN",
2083
+ "auth",
2084
+ `${provider} token refresh was rejected, but another gg process already saved a newer credential \u2014 adopting it instead of signing out`
2085
+ );
2086
+ this.data = current;
2087
+ return currentCreds;
2088
+ }
2089
+ delete current[provider];
2090
+ this.data = current;
2091
+ await atomicWriteFile(this.filePath, JSON.stringify(current, null, 2));
2039
2092
  throw new NotLoggedInError(provider);
2040
2093
  }
2041
2094
  throw err;