Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/memos-local-plugin/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,46 @@ 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 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 {
$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
# 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 {
Write-Warn "Could not rotate oversized daemon log: $Path"
}
}

function Wait-ForViewer {
param([int]$Port, [int]$Timeout = 60)
$Url = "http://127.0.0.1:$Port/"
Expand Down Expand Up @@ -854,6 +894,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
Expand Down
36 changes: 36 additions & 0 deletions apps/memos-local-plugin/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,40 @@ 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
# 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"
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
# 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
}

# ─── Wait for viewer — spin until HTTP endpoint actually responds ─────────
wait_for_viewer() {
local port="$1"
Expand Down Expand Up @@ -745,6 +779,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
Expand Down Expand Up @@ -1358,6 +1393,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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/**
* 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);
// 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 });
}
});

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(32);
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");
});
});
90 changes: 89 additions & 1 deletion apps/memos-local-plugin/tests/unit/install/install-ps1.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 });
}
});
});
Loading
Loading