Skip to content

fix(plugin): cap the daemon start log that grows unbounded - #2608

Open
syzsunshine219 wants to merge 2 commits into
MemTensor:mainfrom
akushonkamen:fix/daemon-start-log-cap
Open

syzsunshine219 wants to merge 2 commits into
MemTensor:mainfrom
akushonkamen:fix/daemon-start-log-cap

Conversation

@syzsunshine219

Copy link
Copy Markdown
Collaborator

Restores the proposal from #2508 as an open PR against main; its former base branch, dev-v2.0.36, is no longer available. This proposal still needs its implementation revised before merging.

Source head: 31d93c64a90185f222f70c21245431e3e62754bc. No new source-code changes are introduced by restoring this proposal. Earlier discussion and evidence remain in #2508; validation claims in the original description refer to the earlier snapshot.

Outstanding review scope

主干daemon-start.log确有直接stdout重定向/追加路径,但旧PR只在安装或启动时截断保留历史,并不能限制长期运行daemon的当前日志增长;不能将其作为#2379运行期无界增长的完整修复直接恢复,应明确范围并补运行期轮转或单独拆成历史保留功能。


Original proposal

Description

daemon-start.log grows without bound for the whole life of an install. Both installers launch the bridge daemon with an uncapped shell redirect, and no truncation or rotation ever runs on it:

  • apps/memos-local-plugin/install.sh — the Hermes bridge daemon: nohup … > "${daemon_log}" 2>&1 & (launch now at install.sh:1389, the log path is logs/daemon-start.log)
  • apps/memos-local-plugin/install.sh:774 — the OpenClaw gateway fallback: nohup … >/tmp/openclaw-memos-gateway.log 2>&1 &
  • apps/memos-local-plugin/install.ps1:884 — Start-Process -RedirectStandardOutput $DaemonLog -RedirectStandardError $DaemonLogErr

Measured in the field (issue #2379): 59.3 MB / 287,895 lines on the oldest install, still growing and actively written. The plugin's rotating file logger never sees this stream — it is a plain process stdout/stderr redirect that bypasses the plugin entirely — so the cap has to live in the installers themselves.

Related Issue (Required): Fixes #2379 (ask 1 — rotate/cap daemon-start.log. Ask 2 — retryable fetch failed / transient=false — is covered by #2468.)

The fix

Add a size cap and route all three launch points through it:

  • rotate_oversized_log (install.sh:483) / Reset-OversizedLog (install.ps1:526) — called before every daemon launch: if the active start log exceeds the cap (default 10 MB, DAEMON_LOG_MAX_BYTES / -MaxBytes 10MB), roll it to a single .1 generation, so the active log + retained generation stay bounded at 2× the cap. An unparsable or unreadable size is a safe no-op (never blocks a daemon start).
  • The Hermes bridge redirect (install.sh:1387), the OpenClaw gateway fallback redirect (install.sh:773), and both Start-Process redirects in install.ps1 (daemon-start.log and daemon-start-err.log, install.ps1:880-881) all go through the cap.
  • Portability: BSD wc -c pads its output with spaces (GNU wc does not); the size is whitespace-stripped before the numeric guard so the check holds on both Linux and macOS — the test suite caught this on a real macOS host and the fix was verified there.

No new dependencies.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • Unit Test
  • Test Script Or Test Steps (please provide)

New regression suite apps/memos-local-plugin/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; BSD-wc space padding handled) plus structural assertions that the Hermes bridge redirect, the OpenClaw gateway fallback redirect, and both install.ps1 Start-Process redirects route through the cap.

Red → green:

  • Red: 9/9 FAILED on the unfixed installers (git stash), then 9/9 PASSED after the fix — npx vitest run tests/unit/install/daemon-start-log-cap.test.ts
  • Related subset npx vitest run tests/unit/install: 74/75 — the 1 failure is a host-load flake (load avg 119): a different DSH fixture test exceeded its 10 s spawnSync timeout on each run, and it passes in isolation on both the clean checkout and with this change (an earlier subset run failed a different test than the later file run, which is what identified it as load flake, not a regression).
  • npm run lint (tsc -p tsconfig.json --noEmit) — clean
  • bash -n install.sh — clean

Re-verified on 2026-10-17 at this branch's HEAD (18f703d): npx vitest run tests/unit/install/daemon-start-log-cap.test.ts → 9 passed (9); lint clean; bash -n clean.

Note: the install.ps1 helper is covered by the structural assertions only — no PowerShell runtime on the test host, so it was not executed.

Checklist

  • I have performed a self-review of my own code | 我已自行检查了自己的代码
  • I have commented my code in hard-to-understand areas | 我已在难以理解的地方对代码进行了注释
  • I have added tests that prove my fix is effective or that my feature works | 我已添加测试以证明我的修复有效或功能正常
  • I have created related documentation issue/PR in MemOS-Docs (if applicable) | 我已在 MemOS-Docs 中创建了相关的文档 issue/PR(如果适用)
  • I have linked the issue to this PR (if applicable) | 我已将 issue 链接到此 PR(如果适用)
  • I have mentioned the person who will review this PR | 我已提及将审查此 PR 的人

AI assistance disclosure

This change was produced with AI assistance (agentic coding session by the account owner): code, tests, and this description were drafted by the AI and reviewed/verified by the account owner, who ran all the test, lint, and syntax checks above locally. No repo requirement specified a disclosure format; this statement is included for transparency.

Reviewer Checklist

akushonkamen and others added 2 commits October 17, 2026 08:14
…#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 MemTensor#2379 (ask 1; ask 2 - retryable fetch failures - is covered by
MemTensor#2468)
@Memtensor-AI Memtensor-AI added area:plugin OpenClaw & Hermes status:in-progress Someone or AI is working on it | 人工或 AI 正在处理 labels Nov 9, 2026
@Memtensor-AI

Copy link
Copy Markdown
Collaborator

🤖 Open Code Review

Target: PR #2608
Task: 0dd4639ac1f5ebe4
Base: main
Head: fix/daemon-start-log-cap
Head SHA: 31d93c64a90185f222f70c21245431e3e62754bc

🔍 OpenCodeReview found 4 issue(s) in this PR.


1. apps/memos-local-plugin/install.ps1 (L553-L554)

The number of bytes actually read is discarded, and the whole $Tail buffer is then written out. If Read returns fewer than $MaxBytes bytes (e.g. the file is truncated concurrently or lives on a slow/network path, or a later refactor changes the seek logic), WriteAllBytes writes the padded buffer, so .1 ends up padded with NUL bytes and the real tail of the log is silently lost. Trim the buffer to the actual read count.

💡 Suggested Change

Before:

                $Tail = New-Object byte[] $MaxBytes
                [void]$Stream.Read($Tail, 0, $MaxBytes)

After:

                $Tail = New-Object byte[] $MaxBytes
                $Read = $Stream.Read($Tail, 0, $MaxBytes)
                if ($Read -lt $MaxBytes) { [Array]::Resize([ref]$Tail, $Read) }

2. apps/memos-local-plugin/install.sh (L497-L498)

Every failure path in this function is silent (wc/tail stderr to /dev/null, || return 0), unlike the PowerShell counterpart which emits a warning. Worst case: rm -f "${log_path}.1" deletes the retained generation and then mv fails (permissions, busy mount), leaving the oversized log untouched with no feedback — exactly the condition this change is meant to bound. Emit a warning so a failed rotation is visible.

💡 Suggested Change

Before:

  rm -f "${log_path}.1"
  mv "${log_path}" "${log_path}.1" 2>/dev/null || return 0

After:

  rm -f "${log_path}.1"
  if ! mv "${log_path}" "${log_path}.1" 2>/dev/null; then
    warn "Could not rotate oversized log: ${log_path}"
    return 0
  fi

3. apps/memos-local-plugin/install.sh (L505)

This truncation is the only mutation in the helper that is not guarded (wc and tail/mv all use || true / || return 0). install.sh runs under set -euo pipefail, and at this point mv has already succeeded, so the interesting failure modes (ENOSPC while creating the new inode, path raced into a directory, mount remounted read-only) are exactly the ones where mv succeeded — and a failed : > redirection then aborts the whole install with an obscure cannot create error instead of leaving the daemon log alone. The PowerShell counterpart treats this as best-effort (try/catch → warning), so the two installers behave differently. Suggest swallowing the failure here as well.

💡 Suggested Change

Before:

  : >"${log_path}"

After:

  : >"${log_path}" 2>/dev/null || true

4. apps/memos-local-plugin/install.ps1 (L558)

WriteAllBytes truncates the destination before writing, so it rewrites the freshly rolled .1 in place. If the write fails part-way (disk full, antivirus/indexer lock, long path ACL) the retained generation is left truncated/partial, and the catch below only emits a warning — the previous log content is gone. The shell implementation avoids this by writing ${log_path}.1.tmp and then renaming it over .1; mirroring that here keeps .1 either fully old or fully new.

💡 Suggested Change

Before:

            [IO.File]::WriteAllBytes($rotated, $Tail)

After:

            [IO.File]::WriteAllBytes("$rotated.tmp", $Tail)
            Move-Item -LiteralPath "$rotated.tmp" -Destination $rotated -Force -ErrorAction Stop

Generated by cloud-assistant via Open Code Review.

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

⚠️ Automated Test Results: ENV ISSUE

The test environment encountered an issue that requires manual attention.

Details: Executor error: Node dependency preparation failed for memos_local_plugin: Command failed with exit code 1: npm ci --include=dev --no-audit --no-fund
stdout:
stderr: npm warn deprecated boolean@3.2.0: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
npm error code 1
npm error path /data/test-workspaces/0dd4639ac1f5ebe4/repo/apps/memos-local-plugin/node_modules/onnxruntime-node
npm error command failed
npm error command sh -c node ./script/install
npm error /data/test-workspaces/0dd4639ac1f5ebe4/repo/apps/memos-local-plugin/node_modules/onnxruntime-node/script/install-utils.js:57
npm error reject(new Error(Failed to download build list. HTTP status code = ${statusCode}));
npm error ^
npm error
npm error Error: Failed to download build list. HTTP status code = 302
npm error at ClientRequest. (/data/test-workspaces/0dd4639ac1f5ebe4/repo/apps/memos-local-plugin/node_modules/onnxruntime-node/script/install-utils.js:57:18)
npm error at Object.onceWrapper (node:events:634:26)
npm error at ClientRequest.emit (node:events:519:28)
npm error at ClientRequest.emit (node:domain:489:12)
npm error at HTTPParser.parserOnIncomingClient (node:_http_client:780:27)
npm error at HTTPParser.parserOnHeadersComplete (node:_http_common:125:17)
npm error at TLSSocket.socketOnData (node:_http_client:615:22)
npm error at TLSSocket.emit (node:events:519:28)
npm error at TLSSocket.emit (node:domain:489:12)
npm error at addChunk (node:internal/streams/readable:561:12)
npm error
npm error Node.js v22.22.2
npm error A complete log of this run can be found in: /data/test-workspaces/_shared/npm-downloads/_logs/2026-11-09T17_39_30_348Z-debug-0.log
Branch: fix/daemon-start-log-cap

Tested source: 31d93c64a90185f222f70c21245431e3e62754bc
Tested base: 3a16e5fe3048a209f722806f4123fa2e533588fd
Tested commit: 9a132a5cb722d2184277b714ca5df14299e20af1
These results apply only to this immutable source/base snapshot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:plugin OpenClaw & Hermes status:in-progress Someone or AI is working on it | 人工或 AI 正在处理

Projects

None yet

Development

Successfully merging this pull request may close these issues.

memos-local-plugin daemon log grows unbounded (no rotation); most of its volume is unretried http.exception errors

4 participants