@cortexkit/pi-magic-context 0.41.0 → 0.41.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.
@@ -314,6 +314,11 @@ var buffer = [];
314
314
  var flushTimer = null;
315
315
  var FLUSH_INTERVAL_MS = 500;
316
316
  var BUFFER_SIZE_LIMIT = 50;
317
+ var MAX_LOG_FILE_BYTES = 32 * 1024 * 1024;
318
+ var SIZE_CHECK_INTERVAL_FLUSHES = 64;
319
+ var activeLogFile = null;
320
+ var activeLogSize = null;
321
+ var flushesSinceSizeCheck = 0;
317
322
  var swallowedWriteCount = 0;
318
323
  var lastErrorMessage = null;
319
324
  var lastErrorTime = null;
@@ -327,6 +332,73 @@ function recordSwallowedWrite(error) {
327
332
  function ensureDir(filePath) {
328
333
  fs.mkdirSync(path2.dirname(filePath), { recursive: true });
329
334
  }
335
+ function isMissingFile(error) {
336
+ return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
337
+ }
338
+ function getCurrentLogSize(logFile) {
339
+ if (activeLogFile === logFile && activeLogSize !== null && flushesSinceSizeCheck < SIZE_CHECK_INTERVAL_FLUSHES) {
340
+ return activeLogSize;
341
+ }
342
+ try {
343
+ const stat = fs.statSync(logFile);
344
+ if (!stat.isFile()) {
345
+ throw new Error(`Magic Context log path is not a regular file: ${logFile}`);
346
+ }
347
+ fs.chmodSync(logFile, 384);
348
+ activeLogFile = logFile;
349
+ activeLogSize = stat.size;
350
+ flushesSinceSizeCheck = 0;
351
+ return stat.size;
352
+ } catch (error) {
353
+ if (!isMissingFile(error))
354
+ throw error;
355
+ activeLogFile = logFile;
356
+ activeLogSize = 0;
357
+ flushesSinceSizeCheck = 0;
358
+ return 0;
359
+ }
360
+ }
361
+ function capLogData(data) {
362
+ if (Buffer.byteLength(data) <= MAX_LOG_FILE_BYTES)
363
+ return data;
364
+ let bounded = Buffer.from(data).subarray(0, MAX_LOG_FILE_BYTES).toString("utf8");
365
+ while (Buffer.byteLength(bounded) > MAX_LOG_FILE_BYTES) {
366
+ bounded = bounded.slice(0, -1);
367
+ }
368
+ return bounded;
369
+ }
370
+ function writeBoundedPredecessor(logFile, predecessorPath, size) {
371
+ const predecessorFd = fs.openSync(predecessorPath, "w", 384);
372
+ try {
373
+ fs.fchmodSync(predecessorFd, 384);
374
+ const bytesToCopy = Math.min(size, MAX_LOG_FILE_BYTES);
375
+ const sourceFd = fs.openSync(logFile, "r");
376
+ try {
377
+ const chunk = Buffer.allocUnsafe(Math.min(64 * 1024, bytesToCopy));
378
+ let remaining = bytesToCopy;
379
+ let position = Math.max(0, size - bytesToCopy);
380
+ while (remaining > 0) {
381
+ const bytesRead = fs.readSync(sourceFd, chunk, 0, Math.min(chunk.length, remaining), position);
382
+ if (bytesRead === 0)
383
+ break;
384
+ fs.writeSync(predecessorFd, chunk, 0, bytesRead);
385
+ remaining -= bytesRead;
386
+ position += bytesRead;
387
+ }
388
+ } finally {
389
+ fs.closeSync(sourceFd);
390
+ }
391
+ } finally {
392
+ fs.closeSync(predecessorFd);
393
+ }
394
+ }
395
+ function rotateLogFile(logFile, size) {
396
+ const predecessorPath = `${logFile}.1`;
397
+ writeBoundedPredecessor(logFile, predecessorPath, size);
398
+ fs.truncateSync(logFile, 0);
399
+ activeLogSize = 0;
400
+ flushesSinceSizeCheck = 0;
401
+ }
330
402
  function flush() {
331
403
  if (flushTimer) {
332
404
  clearTimeout(flushTimer);
@@ -334,13 +406,26 @@ function flush() {
334
406
  }
335
407
  if (buffer.length === 0)
336
408
  return;
337
- const data = buffer.join("");
409
+ const bufferedData = buffer.join("");
338
410
  buffer = [];
339
411
  try {
412
+ const data = capLogData(bufferedData);
340
413
  const logFile = getMagicContextLogPath();
341
414
  ensureDir(logFile);
342
- fs.appendFileSync(logFile, data);
415
+ let currentSize = getCurrentLogSize(logFile);
416
+ const dataSize = Buffer.byteLength(data);
417
+ if (currentSize > 0 && currentSize + dataSize > MAX_LOG_FILE_BYTES) {
418
+ rotateLogFile(logFile, currentSize);
419
+ currentSize = 0;
420
+ }
421
+ fs.appendFileSync(logFile, data, { encoding: "utf8", mode: 384 });
422
+ activeLogFile = logFile;
423
+ activeLogSize = currentSize + dataSize;
424
+ flushesSinceSizeCheck++;
343
425
  } catch (error) {
426
+ activeLogFile = null;
427
+ activeLogSize = null;
428
+ flushesSinceSizeCheck = 0;
344
429
  recordSwallowedWrite(error);
345
430
  }
346
431
  }