From 18f703d8f36c902e84bf14335acc55862ffa78f0 Mon Sep 17 00:00:00 2001 From: akushonkamen Date: Sat, 17 Oct 2026 08:14:56 +0800 Subject: [PATCH 1/2] fix(plugin): cap the daemon start log that grows unbounded (#2379) install.sh launched the bridge daemon with a plain nohup redirect to logs/daemon-start.log (and the OpenClaw gateway fallback to /tmp/openclaw-memos-gateway.log) with no size cap or rotation anywhere, so the file grew for the whole life of the install - 59 MB / 287,895 lines measured in the field. The plugin's rotating file logger never sees this stream, so the cap has to live in the installers. Add rotate_oversized_log (install.sh) / Reset-OversizedLog (install.ps1): before every daemon launch, roll an over-cap start log to a single .1 generation, bounding the pair at 2x the 10 MB default cap. An unparsable or unreadable size is a safe no-op; BSD wc's space-padded output is stripped so the numeric guard holds on both Linux and macOS. Tested: new tests/unit/install/daemon-start-log-cap.test.ts (9 tests): rotation behavior extracted verbatim from install.sh (over-cap rolls to .1; at-cap, missing, and unreadable files are no-ops; exactly one generation kept) plus structural checks that the Hermes bridge redirect, the OpenClaw gateway fallback redirect, and both install.ps1 Start-Process redirects route through the cap. Red: 9/9 failed on the unfixed installers; green: 9/9 passed after the fix. tests/unit/install subset: 74/75 - the one failure is a load flake on this host (load avg 119): a different DSH fixture test hangs past its 10s spawn timeout on each run and passes in isolation on both the clean checkout and with this change. npm run lint (tsc --noEmit): clean. The install.ps1 helper is covered by structural assertions only (no PowerShell runtime on this host to execute it). Fixes #2379 (ask 1; ask 2 - retryable fetch failures - is covered by #2468) --- apps/memos-local-plugin/install.ps1 | 25 +++ apps/memos-local-plugin/install.sh | 27 +++ .../unit/install/daemon-start-log-cap.test.ts | 169 ++++++++++++++++++ 3 files changed, 221 insertions(+) create mode 100644 apps/memos-local-plugin/tests/unit/install/daemon-start-log-cap.test.ts diff --git a/apps/memos-local-plugin/install.ps1 b/apps/memos-local-plugin/install.ps1 index 184997b30..e91cb9275 100644 --- a/apps/memos-local-plugin/install.ps1 +++ b/apps/memos-local-plugin/install.ps1 @@ -523,6 +523,28 @@ function Write-RuntimeHomeMarker { Move-Item -Path $TempFile -Destination $MarkerFile -Force } +function Reset-OversizedLog { + # The daemon's stdout/stderr redirects bypass the plugin's rotating file + # logger, so without a cap the start log grows for the whole life of the + # install (59 MB measured in the field — MemTensor/MemOS#2379). Call + # before every daemon launch: roll an over-cap log to a single `.1` + # generation so the pair stays bounded at 2x the cap. + param([string]$Path, [long]$MaxBytes = 10MB) + if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { return } + try { + $length = (Get-Item -LiteralPath $Path).Length + } catch { return } + if ($length -le $MaxBytes) { return } + $rotated = "$Path.1" + if (Test-Path -LiteralPath $rotated) { + Remove-Item -LiteralPath $rotated -Force -ErrorAction SilentlyContinue + } + try { + Move-Item -LiteralPath $Path -Destination $rotated -Force -ErrorAction Stop + New-Item -ItemType File -Path $Path -Force | Out-Null + } catch { } +} + function Wait-ForViewer { param([int]$Port, [int]$Timeout = 60) $Url = "http://127.0.0.1:$Port/" @@ -854,6 +876,9 @@ memory: if ($NodeBin -and (Test-Path $BridgeEntry) -and ($BridgeEntry.EndsWith(".cjs") -or (Test-Path $TsxBin))) { $DaemonLog = Join-Path $Prefix "logs\daemon-start.log" $DaemonLogErr = Join-Path $Prefix "logs\daemon-start-err.log" + New-Item -ItemType Directory -Path (Join-Path $Prefix "logs") -Force | Out-Null + Reset-OversizedLog -Path $DaemonLog + Reset-OversizedLog -Path $DaemonLogErr if ($BridgeEntry.EndsWith(".cjs")) { $DaemonArgs = "`"$BridgeEntry`" --agent=hermes --daemon --home=`"$HomeDir`"" Start-Process -FilePath $NodeBin -ArgumentList $DaemonArgs -WindowStyle Hidden -RedirectStandardOutput $DaemonLog -RedirectStandardError $DaemonLogErr diff --git a/apps/memos-local-plugin/install.sh b/apps/memos-local-plugin/install.sh index eb30bff68..c4b317788 100755 --- a/apps/memos-local-plugin/install.sh +++ b/apps/memos-local-plugin/install.sh @@ -472,6 +472,31 @@ ensure_runtime_home() { fi } +# ─── Daemon log cap — bound the daemon's stdout/stderr redirect ─────────── +# The bridge daemon's start log is a plain shell redirect, so the rotating +# file logger inside the plugin never sees it. Without a cap the file grows +# for the whole life of the install (59 MB / 287,895 lines measured in the +# field — https://github.com/MemTensor/MemOS/issues/2379). Call this before +# every daemon launch: if the active log exceeds the cap, roll it to a +# single `.1` generation so the pair stays bounded at 2× the cap. +DAEMON_LOG_MAX_BYTES=$((10 * 1024 * 1024)) +rotate_oversized_log() { + local log_path="$1" + local max_bytes="${2:-${DAEMON_LOG_MAX_BYTES}}" + [[ -f "${log_path}" ]] || return 0 + local size + # BSD wc pads its output with spaces; GNU wc does not. Strip whitespace + # so the numeric guard holds on both (an unparsable size is a no-op). + size="$(wc -c <"${log_path}" 2>/dev/null || true)" + size="${size//[[:space:]]/}" + [[ "${size}" =~ ^[0-9]+$ ]] || return 0 + (( size <= max_bytes )) && return 0 + rm -f "${log_path}.1" + mv "${log_path}" "${log_path}.1" 2>/dev/null || return 0 + : >"${log_path}" + return 0 +} + # ─── Wait for viewer — spin until HTTP endpoint actually responds ───────── wait_for_viewer() { local port="$1" @@ -745,6 +770,7 @@ NODE fi warn "Memory Viewer did not respond after service start; trying foreground gateway mode." + rotate_oversized_log /tmp/openclaw-memos-gateway.log nohup "${oc_bin}" gateway >/tmp/openclaw-memos-gateway.log 2>&1 & sleep 2 if wait_for_viewer "${OPENCLAW_PORT}"; then @@ -1358,6 +1384,7 @@ CFGEOF if [[ -n "${node_bin}" && -x "${node_bin}" && -f "${bridge_entry}" && ( "${bridge_entry}" == *.cjs || -f "${tsx_bin}" ) ]]; then local daemon_log="${prefix}/logs/daemon-start.log" mkdir -p "${prefix}/logs" + rotate_oversized_log "${daemon_log}" # Launch bridge in --daemon mode (pure HTTP, no stdio). # The process stays alive to serve the Memory Viewer. if [[ "${bridge_entry}" == *.cjs ]]; then diff --git a/apps/memos-local-plugin/tests/unit/install/daemon-start-log-cap.test.ts b/apps/memos-local-plugin/tests/unit/install/daemon-start-log-cap.test.ts new file mode 100644 index 000000000..8913c3c27 --- /dev/null +++ b/apps/memos-local-plugin/tests/unit/install/daemon-start-log-cap.test.ts @@ -0,0 +1,169 @@ +/** + * Daemon start-log cap tests (issue #2379). + * + * The daemon's stdout/stderr is a plain shell redirect (install.sh) or + * Start-Process redirection (install.ps1), so the plugin's rotating file + * logger never sees it. The installers must cap the file themselves before + * every daemon launch. + * + * Verified here without side effects on the host: + * 1. `rotate_oversized_log` (extracted verbatim from install.sh) rolls an + * over-cap log to a single `.1` generation and leaves small, missing, + * and unreadable files untouched. + * 2. Every daemon launch site routes through the cap: the Hermes bridge + * redirect and the OpenClaw gateway fallback redirect in install.sh, + * and both Start-Process redirects in install.ps1. + */ + +import { describe, expect, it } from "vitest"; +import path from "node:path"; +import { spawnSync } from "node:child_process"; +import { chmodSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; + +const REPO_ROOT = path.resolve(__dirname, "..", "..", ".."); +const INSTALL_SH = path.join(REPO_ROOT, "install.sh"); +const INSTALL_PS1 = path.join(REPO_ROOT, "install.ps1"); + +/** Extract the `rotate_oversized_log` function body verbatim from install.sh. */ +function extractRotateFunction(): string { + const script = readFileSync(INSTALL_SH, "utf8"); + const start = script.indexOf("rotate_oversized_log() {"); + if (start < 0) throw new Error("rotate_oversized_log() not found in install.sh"); + const end = script.indexOf("\n}\n", start); + if (end < 0) throw new Error("rotate_oversized_log() has no closing brace"); + return script.slice(start, end + 3); +} + +interface RunResult { + code: number; + stdout: string; + stderr: string; +} + +/** One isolated temp dir + a runner that executes the extracted function. */ +function makeSandbox(): { root: string; run(scriptBody: string): RunResult } { + const root = mkdtempSync(path.join(tmpdir(), "memos-daemon-log-cap-")); + return { + root, + run(scriptBody: string): RunResult { + const r = spawnSync("bash", ["-c", `${extractRotateFunction()}\n${scriptBody}`], { + cwd: root, + encoding: "utf8", + timeout: 10_000, + }); + return { code: r.status ?? -1, stdout: r.stdout, stderr: r.stderr }; + }, + }; +} + +function makeLog(dir: string, name: string, bytes: number): string { + const file = path.join(dir, name); + writeFileSync(file, "x".repeat(bytes), "utf8"); + return file; +} + +describe("install.sh — rotate_oversized_log behavior", () => { + it("rolls an over-cap log to .1 and starts a fresh active file", () => { + const sandbox = makeSandbox(); + const log = makeLog(sandbox.root, "daemon-start.log", 64); + try { + const r = sandbox.run(`rotate_oversized_log "${log}" 32`); + expect(r.code).toBe(0); + expect(readFileSync(`${log}.1`, "utf8")).toHaveLength(64); + expect(readFileSync(log, "utf8")).toHaveLength(0); + } finally { + rmSync(sandbox.root, { recursive: true, force: true }); + } + }); + + it("leaves a log at or under the cap untouched", () => { + const sandbox = makeSandbox(); + const log = makeLog(sandbox.root, "daemon-start.log", 32); + try { + const r = sandbox.run(`rotate_oversized_log "${log}" 32`); + expect(r.code).toBe(0); + expect(readFileSync(log, "utf8")).toHaveLength(32); + expect(() => readFileSync(`${log}.1`, "utf8")).toThrow(); + } finally { + rmSync(sandbox.root, { recursive: true, force: true }); + } + }); + + it("is a no-op for a missing log", () => { + const sandbox = makeSandbox(); + try { + const log = path.join(sandbox.root, "absent.log"); + const r = sandbox.run(`rotate_oversized_log "${log}" 32`); + expect(r.code).toBe(0); + expect(r.stderr).toBe(""); + } finally { + rmSync(sandbox.root, { recursive: true, force: true }); + } + }); + + it("keeps exactly one rotated generation across repeated launches", () => { + const sandbox = makeSandbox(); + try { + const log = makeLog(sandbox.root, "daemon-start.log", 64); + sandbox.run(`rotate_oversized_log "${log}" 32`); + // The fresh active file grows past the cap again before the next launch. + makeLog(sandbox.root, "daemon-start.log", 48); + sandbox.run(`rotate_oversized_log "${log}" 32`); + expect(readFileSync(`${log}.1`, "utf8")).toHaveLength(48); + expect(readFileSync(log, "utf8")).toHaveLength(0); + } finally { + rmSync(sandbox.root, { recursive: true, force: true }); + } + }); + + it("survives an unreadable log without failing the launch path", () => { + const sandbox = makeSandbox(); + const log = makeLog(sandbox.root, "daemon-start.log", 64); + try { + chmodSync(log, 0o000); + const r = sandbox.run(`rotate_oversized_log "${log}" 32`); + expect(r.code).toBe(0); + } finally { + chmodSync(log, 0o644); + rmSync(sandbox.root, { recursive: true, force: true }); + } + }); + + it("uses a 10 MB script default cap", () => { + const script = readFileSync(INSTALL_SH, "utf8"); + expect(script).toContain("DAEMON_LOG_MAX_BYTES=$((10 * 1024 * 1024))"); + }); +}); + +describe("daemon launch sites route through the cap", () => { + it("caps daemon-start.log before the Hermes bridge redirect in install.sh", () => { + const script = readFileSync(INSTALL_SH, "utf8"); + const rotation = script.indexOf('rotate_oversized_log "${daemon_log}"'); + const launch = script.indexOf( + 'nohup "${node_bin}" "${bridge_entry}" --agent=hermes --daemon', + ); + const logDecl = script.indexOf('local daemon_log="${prefix}/logs/daemon-start.log"'); + expect(logDecl).toBeGreaterThan(0); + expect(rotation).toBeGreaterThan(logDecl); + expect(launch).toBeGreaterThan(rotation); + }); + + it("caps the OpenClaw gateway fallback log in install.sh", () => { + const script = readFileSync(INSTALL_SH, "utf8"); + const launch = script.indexOf('nohup "${oc_bin}" gateway >/tmp/openclaw-memos-gateway.log'); + expect(launch).toBeGreaterThan(0); + const before = script.slice(Math.max(0, launch - 200), launch); + expect(before).toContain("rotate_oversized_log /tmp/openclaw-memos-gateway.log"); + }); + + it("caps both daemon start redirects in install.ps1", () => { + const script = readFileSync(INSTALL_PS1, "utf8"); + expect(script).toContain("function Reset-OversizedLog"); + const firstLaunch = script.indexOf("Start-Process -FilePath $NodeBin"); + expect(firstLaunch).toBeGreaterThan(0); + const before = script.slice(0, firstLaunch); + expect(before).toContain("Reset-OversizedLog -Path $DaemonLog"); + expect(before).toContain("Reset-OversizedLog -Path $DaemonLogErr"); + }); +}); From 31d93c64a90185f222f70c21245431e3e62754bc Mon Sep 17 00:00:00 2001 From: akushonkamen Date: Sun, 18 Oct 2026 13:50:11 +0800 Subject: [PATCH 2/2] fix(plugin): trim rotated daemon log to cap, warn on rotation failure, add boundary tests --- apps/memos-local-plugin/install.ps1 | 28 +++- apps/memos-local-plugin/install.sh | 17 ++- .../unit/install/daemon-start-log-cap.test.ts | 6 +- .../tests/unit/install/install-ps1.test.ts | 90 ++++++++++++- .../tests/unit/install/install-sh.test.ts | 120 ++++++++++++++++++ 5 files changed, 249 insertions(+), 12 deletions(-) diff --git a/apps/memos-local-plugin/install.ps1 b/apps/memos-local-plugin/install.ps1 index e91cb9275..e563a7525 100644 --- a/apps/memos-local-plugin/install.ps1 +++ b/apps/memos-local-plugin/install.ps1 @@ -525,10 +525,13 @@ function Write-RuntimeHomeMarker { function Reset-OversizedLog { # The daemon's stdout/stderr redirects bypass the plugin's rotating file - # logger, so without a cap the start log grows for the whole life of the - # install (59 MB measured in the field — MemTensor/MemOS#2379). Call - # before every daemon launch: roll an over-cap log to a single `.1` - # generation so the pair stays bounded at 2x the cap. + # logger, so without a cap the start log grows for the whole life of a + # daemon session (59 MB measured in the field — MemTensor/MemOS#2379). + # The launch redirect truncates the active log anyway, so this only + # bounds retained history at install time: a running daemon can still + # grow the active log without limit between installs. Call before every + # daemon launch: keep only the tail (up to the cap) of an over-cap log + # as a single `.1` generation. param([string]$Path, [long]$MaxBytes = 10MB) if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { return } try { @@ -541,8 +544,23 @@ function Reset-OversizedLog { } try { Move-Item -LiteralPath $Path -Destination $rotated -Force -ErrorAction Stop + # Keep only the tail of the rolled generation so `.1` itself stays + # bounded (the pre-rotation file can be arbitrarily larger than the cap). + if ((Get-Item -LiteralPath $rotated).Length -gt $MaxBytes) { + $Stream = [IO.File]::Open($rotated, 'Open', 'Read') + try { + $Stream.Seek(-$MaxBytes, 'End') | Out-Null + $Tail = New-Object byte[] $MaxBytes + [void]$Stream.Read($Tail, 0, $MaxBytes) + } finally { + $Stream.Close() + } + [IO.File]::WriteAllBytes($rotated, $Tail) + } New-Item -ItemType File -Path $Path -Force | Out-Null - } catch { } + } catch { + Write-Warn "Could not rotate oversized daemon log: $Path" + } } function Wait-ForViewer { diff --git a/apps/memos-local-plugin/install.sh b/apps/memos-local-plugin/install.sh index c4b317788..430bf741f 100755 --- a/apps/memos-local-plugin/install.sh +++ b/apps/memos-local-plugin/install.sh @@ -475,10 +475,13 @@ ensure_runtime_home() { # ─── Daemon log cap — bound the daemon's stdout/stderr redirect ─────────── # The bridge daemon's start log is a plain shell redirect, so the rotating # file logger inside the plugin never sees it. Without a cap the file grows -# for the whole life of the install (59 MB / 287,895 lines measured in the -# field — https://github.com/MemTensor/MemOS/issues/2379). Call this before -# every daemon launch: if the active log exceeds the cap, roll it to a -# single `.1` generation so the pair stays bounded at 2× the cap. +# whole life of a daemon session (59 MB / 287,895 lines measured in the +# field — https://github.com/MemTensor/MemOS/issues/2379). The launch +# redirect truncates the active log anyway, so this only bounds retained +# history at install time: a running daemon can still grow the active log +# without limit between installs. Call this before every daemon launch: +# if the active log exceeds the cap, keep only the tail (up to the cap) +# as a single `.1` generation. DAEMON_LOG_MAX_BYTES=$((10 * 1024 * 1024)) rotate_oversized_log() { local log_path="$1" @@ -493,6 +496,12 @@ rotate_oversized_log() { (( size <= max_bytes )) && return 0 rm -f "${log_path}.1" mv "${log_path}" "${log_path}.1" 2>/dev/null || return 0 + # Keep only the tail of the rolled generation so `.1` itself stays + # bounded (the pre-rotation file can be arbitrarily larger than the cap). + if [[ "${size}" -gt "${max_bytes}" ]]; then + tail -c "${max_bytes}" "${log_path}.1" >"${log_path}.1.tmp" 2>/dev/null \ + && mv -f "${log_path}.1.tmp" "${log_path}.1" || rm -f "${log_path}.1.tmp" + fi : >"${log_path}" return 0 } diff --git a/apps/memos-local-plugin/tests/unit/install/daemon-start-log-cap.test.ts b/apps/memos-local-plugin/tests/unit/install/daemon-start-log-cap.test.ts index 8913c3c27..c0b1c84ee 100644 --- a/apps/memos-local-plugin/tests/unit/install/daemon-start-log-cap.test.ts +++ b/apps/memos-local-plugin/tests/unit/install/daemon-start-log-cap.test.ts @@ -70,7 +70,9 @@ describe("install.sh — rotate_oversized_log behavior", () => { try { const r = sandbox.run(`rotate_oversized_log "${log}" 32`); expect(r.code).toBe(0); - expect(readFileSync(`${log}.1`, "utf8")).toHaveLength(64); + // The rolled generation keeps only the tail, bounded at the cap — + // the pre-rotation file can be arbitrarily larger than the cap. + expect(readFileSync(`${log}.1`, "utf8")).toHaveLength(32); expect(readFileSync(log, "utf8")).toHaveLength(0); } finally { rmSync(sandbox.root, { recursive: true, force: true }); @@ -110,7 +112,7 @@ describe("install.sh — rotate_oversized_log behavior", () => { // The fresh active file grows past the cap again before the next launch. makeLog(sandbox.root, "daemon-start.log", 48); sandbox.run(`rotate_oversized_log "${log}" 32`); - expect(readFileSync(`${log}.1`, "utf8")).toHaveLength(48); + expect(readFileSync(`${log}.1`, "utf8")).toHaveLength(32); expect(readFileSync(log, "utf8")).toHaveLength(0); } finally { rmSync(sandbox.root, { recursive: true, force: true }); diff --git a/apps/memos-local-plugin/tests/unit/install/install-ps1.test.ts b/apps/memos-local-plugin/tests/unit/install/install-ps1.test.ts index 166a5b8a4..bb74f9fc0 100644 --- a/apps/memos-local-plugin/tests/unit/install/install-ps1.test.ts +++ b/apps/memos-local-plugin/tests/unit/install/install-ps1.test.ts @@ -1,4 +1,10 @@ -import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { + existsSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs"; import { spawnSync } from "node:child_process"; import { tmpdir } from "node:os"; import path from "node:path"; @@ -186,3 +192,85 @@ describe("install.ps1 — OpenClaw config patch", () => { } }); }); + +const PWSH_AVAILABLE = (() => { + const probe = spawnSync( + process.platform === "win32" ? "pwsh" : "pwsh", + ["-NoProfile", "-Command", "$PSVersionTable.PSVersion.ToString()"], + { encoding: "utf8", timeout: 30_000 }, + ); + return probe.status === 0; +})(); + +function extractPsFunction(script: string, name: string, nextName: string): string { + const start = script.indexOf(`function ${name}`); + const end = script.indexOf(`function ${nextName}`, start); + if (start < 0 || end < 0) throw new Error(`${name} function not found`); + return script.slice(start, end); +} + +function runResetOversizedLog(harness: string): { + code: number; + stdout: string; + stderr: string; +} { + const script = readFileSync(SCRIPT, "utf8"); + const writeWarn = script.match(/function Write-Warn.*?\n/)?.[0] ?? ""; + const resetLog = extractPsFunction(script, "Reset-OversizedLog", "Wait-ForViewer"); + const r = spawnSync( + "pwsh", + ["-NoProfile", "-NonInteractive", "-Command", `${writeWarn}\n${resetLog}\n${harness}`], + { encoding: "utf8", timeout: 60_000 }, + ); + return { code: r.status ?? -1, stdout: r.stdout, stderr: r.stderr }; +} + +describe.skipIf(!PWSH_AVAILABLE)("install.ps1 — Reset-OversizedLog", () => { + it("does nothing when the log file is absent", () => { + const tempDir = mkdtempSync(path.join(tmpdir(), "memos-reset-log-")); + try { + const logPath = path.join(tempDir, "daemon.log"); + const r = runResetOversizedLog(`Reset-OversizedLog -Path "${logPath}"`); + expect(r.code).toBe(0); + expect(existsSync(logPath)).toBe(false); + expect(existsSync(`${logPath}.1`)).toBe(false); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("does not rotate when the size exactly equals the cap", () => { + const tempDir = mkdtempSync(path.join(tmpdir(), "memos-reset-log-")); + try { + const logPath = path.join(tempDir, "daemon.log"); + writeFileSync(logPath, Buffer.alloc(2048, "a"), "utf8"); + const r = runResetOversizedLog( + `Reset-OversizedLog -Path "${logPath}" -MaxBytes 2048`, + ); + expect(r.code).toBe(0); + expect(readFileSync(logPath, "utf8")).toHaveLength(2048); + expect(existsSync(`${logPath}.1`)).toBe(false); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } + }); + + it("rotates an over-cap log, trims .1 to the cap, and removes a stale .1", () => { + const tempDir = mkdtempSync(path.join(tmpdir(), "memos-reset-log-")); + try { + const logPath = path.join(tempDir, "daemon.log"); + writeFileSync(logPath, Buffer.alloc(6000, "a"), "utf8"); + writeFileSync(`${logPath}.1`, "stale-generation", "utf8"); + const r = runResetOversizedLog( + `Reset-OversizedLog -Path "${logPath}" -MaxBytes 1024`, + ); + expect(r.code).toBe(0); + // Active log truncated, ready for the launch redirect. + expect(readFileSync(logPath, "utf8")).toHaveLength(0); + // `.1` keeps only the tail, bounded at the cap (not the full 6000 B). + expect(readFileSync(`${logPath}.1`, "utf8")).toHaveLength(1024); + } finally { + rmSync(tempDir, { recursive: true, force: true }); + } + }); +}); diff --git a/apps/memos-local-plugin/tests/unit/install/install-sh.test.ts b/apps/memos-local-plugin/tests/unit/install/install-sh.test.ts index 943345fdd..74d754642 100644 --- a/apps/memos-local-plugin/tests/unit/install/install-sh.test.ts +++ b/apps/memos-local-plugin/tests/unit/install/install-sh.test.ts @@ -696,3 +696,123 @@ exit 90 expect(pkg.files).not.toContain("tests"); }); }); + +function extractShellFunction(script: string, name: string): string { + const start = script.indexOf(`${name}() {`); + if (start < 0) throw new Error(`${name} function not found`); + const end = script.indexOf("\n}", start); + if (end < 0) throw new Error(`${name} function end not found`); + return script.slice(start, end + 2); +} + +function runRotate(body: string): { code: number; stdout: string; stderr: string } { + const script = readFileSync(SCRIPT, "utf8"); + const rotate = extractShellFunction(script, "rotate_oversized_log"); + const cap = script.match(/^DAEMON_LOG_MAX_BYTES=\$\(\(.*\)\)$/m)?.[0] ?? ""; + const r = spawnSync( + "bash", + ["-c", `${cap}\n${rotate}\n${body}`], + { encoding: "utf8", timeout: 10_000 }, + ); + return { code: r.status ?? -1, stdout: r.stdout, stderr: r.stderr }; +} + +describe("install.sh — rotate_oversized_log", () => { + it("does nothing when the log file is absent", () => { + const root = mkdtempSync(path.join(tmpdir(), "memos-rotate-")); + try { + const r = runRotate(`rotate_oversized_log "${root}/missing.log"`); + expect(r.code).toBe(0); + expect(existsSync(`${root}/missing.log`)).toBe(false); + expect(existsSync(`${root}/missing.log.1`)).toBe(false); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); + + it("does not rotate when the size exactly equals the cap", () => { + const root = mkdtempSync(path.join(tmpdir(), "memos-rotate-")); + try { + const log = path.join(root, "daemon.log"); + writeFileSync(log, Buffer.alloc(2048, "a"), "utf8"); + const r = runRotate(`rotate_oversized_log "${log}" 2048`); + expect(r.code).toBe(0); + expect(readFileSync(log, "utf8")).toHaveLength(2048); + expect(existsSync(`${log}.1`)).toBe(false); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); + + it("rotates an over-cap log and trims the .1 generation to the cap", () => { + const root = mkdtempSync(path.join(tmpdir(), "memos-rotate-")); + try { + const log = path.join(root, "daemon.log"); + const oversized = Buffer.alloc(6000, "a"); + writeFileSync(log, oversized, "utf8"); + const r = runRotate(`rotate_oversized_log "${log}" 1024`); + expect(r.code).toBe(0); + // Active log truncated, ready for the launch redirect. + expect(readFileSync(log, "utf8")).toHaveLength(0); + // `.1` keeps only the tail, bounded at the cap (not the full 6000 B). + expect(readFileSync(`${log}.1`, "utf8")).toHaveLength(1024); + expect(existsSync(`${log}.1.tmp`)).toBe(false); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); + + it("is a no-op when wc output is not numeric", () => { + const root = mkdtempSync(path.join(tmpdir(), "memos-rotate-")); + try { + const bin = path.join(root, "bin"); + mkdirSync(bin, { recursive: true }); + writeExecutable( + path.join(bin, "wc"), + `#!/usr/bin/env bash\necho not-a-number\n`, + ); + const log = path.join(root, "daemon.log"); + writeFileSync(log, Buffer.alloc(6000, "a"), "utf8"); + // The real function is sourced from the script; only `wc` is replaced, + // so the numeric guard (not the size) decides the outcome. + const script = readFileSync(SCRIPT, "utf8"); + const rotate = extractShellFunction(script, "rotate_oversized_log"); + const cap = script.match(/^DAEMON_LOG_MAX_BYTES=\$\(\(.*\)\)$/m)?.[0] ?? ""; + const r = spawnSync( + "bash", + ["-c", `${cap}\n${rotate}\nrotate_oversized_log "${log}" 1024`], + { + encoding: "utf8", + env: { ...process.env, PATH: `${bin}:${process.env.PATH ?? ""}` }, + timeout: 10_000, + }, + ); + expect(r.status).toBe(0); + expect(readFileSync(log, "utf8")).toHaveLength(6000); + expect(existsSync(`${log}.1`)).toBe(false); + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); + + it("keeps the active log when mv fails after removing the old .1", () => { + const root = mkdtempSync(path.join(tmpdir(), "memos-rotate-")); + try { + const dir = path.join(root, "logs"); + mkdirSync(dir, { recursive: true }); + const log = path.join(dir, "daemon.log"); + writeFileSync(log, Buffer.alloc(6000, "a"), "utf8"); + // Read-only directory makes mv (a rename) fail after `.1` was removed. + chmodSync(dir, 0o555); + try { + const r = runRotate(`rotate_oversized_log "${log}" 1024`); + expect(r.code).toBe(0); + expect(existsSync(`${log}.1`)).toBe(false); + } finally { + chmodSync(dir, 0o755); + } + } finally { + rmSync(root, { recursive: true, force: true }); + } + }); +});