@cortexkit/pi-magic-context 0.43.2 → 0.44.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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  log
3
- } from "./index-bfd52y8p.js";
3
+ } from "./index-tfzybajk.js";
4
4
 
5
5
  // ../plugin/src/shared/safe-notification-target.ts
6
6
  var DEFAULT_TITLE_RE = /^(New session - |Child session - )\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  log
3
- } from "./index-bfd52y8p.js";
3
+ } from "./index-tfzybajk.js";
4
4
 
5
5
  // ../plugin/src/shared/write-transaction-timing.ts
6
6
  var SLOW_WRITE_TRANSACTION_THRESHOLD_MS = 1000;
@@ -320,6 +320,15 @@ var buffer = [];
320
320
  var flushTimer = null;
321
321
  var FLUSH_INTERVAL_MS = 500;
322
322
  var BUFFER_SIZE_LIMIT = 50;
323
+ var MAX_BUFFERED_BYTES = 1024 * 1024;
324
+ var bufferedBytes = 0;
325
+ var droppedLines = 0;
326
+ function boundBuffer() {
327
+ while (bufferedBytes > MAX_BUFFERED_BYTES && buffer.length > 0) {
328
+ bufferedBytes -= Buffer.byteLength(buffer.shift() ?? "");
329
+ droppedLines++;
330
+ }
331
+ }
323
332
  var MAX_LOG_FILE_BYTES = 32 * 1024 * 1024;
324
333
  var SIZE_CHECK_INTERVAL_FLUSHES = 64;
325
334
  var activeLogFile = null;
@@ -410,12 +419,12 @@ function flush() {
410
419
  clearTimeout(flushTimer);
411
420
  flushTimer = null;
412
421
  }
413
- if (buffer.length === 0)
422
+ if (buffer.length === 0 && droppedLines === 0)
414
423
  return;
415
- const bufferedData = buffer.join("");
416
- buffer = [];
424
+ const notice = droppedLines ? `[${new Date().toISOString()}] [magic-context][global] logger dropped ${droppedLines} ${droppedLines === 1 ? "line" : "lines"}: buffer exceeded ${MAX_BUFFERED_BYTES} bytes while writes were pending
425
+ ` : "";
417
426
  try {
418
- const data = capLogData(bufferedData);
427
+ const data = capLogData(notice + buffer.join(""));
419
428
  const logFile = getMagicContextLogPath();
420
429
  ensureDir(logFile);
421
430
  let currentSize = getCurrentLogSize(logFile);
@@ -425,6 +434,9 @@ function flush() {
425
434
  currentSize = 0;
426
435
  }
427
436
  fs.appendFileSync(logFile, data, { encoding: "utf8", mode: 384 });
437
+ buffer = [];
438
+ bufferedBytes = 0;
439
+ droppedLines = 0;
428
440
  activeLogFile = logFile;
429
441
  activeLogSize = currentSize + dataSize;
430
442
  flushesSinceSizeCheck++;
@@ -433,6 +445,7 @@ function flush() {
433
445
  activeLogSize = null;
434
446
  flushesSinceSizeCheck = 0;
435
447
  recordSwallowedWrite(error);
448
+ boundBuffer();
436
449
  }
437
450
  }
438
451
  function scheduleFlush() {
@@ -450,8 +463,11 @@ function log(message, data) {
450
463
  const timestamp = new Date().toISOString();
451
464
  const serialized = data === undefined ? "" : data instanceof Error ? ` ${sanitizeDiagnosticText(`${data.message}${data.stack ? `
452
465
  ${data.stack}` : ""}`)}` : ` ${JSON.stringify(sanitizeConfigValue(data))}`;
453
- buffer.push(`[${timestamp}] ${sanitizeDiagnosticText(message)}${serialized}
454
- `);
466
+ const line = `[${timestamp}] ${sanitizeDiagnosticText(message)}${serialized}
467
+ `;
468
+ buffer.push(line);
469
+ bufferedBytes += Buffer.byteLength(line);
470
+ boundBuffer();
455
471
  if (buffer.length >= BUFFER_SIZE_LIMIT) {
456
472
  flush();
457
473
  } else {