@kenkaiiii/gg-core 5.84.2 → 5.84.3

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