/** * @file One-step rotation for AgentHub's append-only runtime logs (server.log, * bootstrap.log, client-build.log, plugin-update.log, cc-update-all.log): a * file past `max` bytes becomes `.1`, replacing the previous one, so * each log costs at most twice `max` however long the install lives. Pure fs, * never throws — a log must never stop what it records. * @author Nguyễn Ngọc Trí Vĩ */ "use strict"; const fs = require("fs"); const DEFAULT_MAX = 5 * 1024 * 1024; function rotateIfLarge(file, max = DEFAULT_MAX) { try { if (fs.statSync(file).size > max) fs.renameSync(file, `${file}.1`); } catch { /* missing file, or not ours to move: keep appending */ } } module.exports = { rotateIfLarge, DEFAULT_MAX }; if (require.main === module) { // Self-check: node scripts/log-rotate.js const os = require("os"); const path = require("path"); const assert = require("assert"); const dir = fs.mkdtempSync(path.join(os.tmpdir(), "log-rotate-")); const f = path.join(dir, "x.log"); fs.writeFileSync(f, "a".repeat(20)); rotateIfLarge(f, 10); assert.ok(!fs.existsSync(f) && fs.statSync(`${f}.1`).size === 20); fs.writeFileSync(f, "b".repeat(5)); rotateIfLarge(f, 10); assert.equal(fs.readFileSync(f, "utf8"), "bbbbb"); rotateIfLarge(path.join(dir, "missing.log"), 10); fs.rmSync(dir, { recursive: true, force: true }); console.log("log-rotate ok"); }