From 5c18da829241659547ab805c60cdf75bc67ed606 Mon Sep 17 00:00:00 2001 From: akushonkamen Date: Thu, 8 Oct 2026 23:47:55 +0800 Subject: [PATCH] fix(plugin): initialize file logging from the ESM bridge entry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #2399 ## Summary - pass `initLogging: true` from the ESM entry (`bridge.mts`) to `bootstrapMemoryCoreFull`, aligning it with the legacy CJS entry (`bridge.cts`), which already passes the flag - add an integration regression asserting that a bridge started from `dist/bridge.mjs` creates the `config.logging` file sinks (`logs/memos.log`, `logs/error.log`) under the runtime home ## Why `bootstrapMemoryCoreFull` only initializes the global logger when `options.initLogging` is set — the option's documented contract is that standalone bridge entries own their stdio and must opt in, while embedded hosts leave it false. `bridge.cts` passes the flag; `bridge.mts` never did. Because `bridge_client.py` resolves the entry as `dist/bridge.mjs` > `dist/bridge.cjs` > `bridge.mts` > `bridge.cts`, every bridge launched from a current build took the ESM path and skipped `initLogger` entirely: `config.logging` (timezone, level, channels, and the file sinks under `/logs`) was silently inert — no file sink was ever created and nothing warned about it. ## Validation - new integration case in `apps/memos-local-plugin/tests/integration/bridge-esm-entry.test.ts`: red on unfixed `main` (no `logs/memos.log` after startup), green after the fix - build-level check (`node dist/bridge.mjs --agent=hermes --no-viewer --home=`): before the fix `/logs` was never created; after the fix it contains all six sinks (`memos.log`, `error.log`, `audit.log`, `events.jsonl`, `llm.jsonl`, `perf.jsonl`) - `npx vitest run tests/unit/bridge-status.test.ts` — 3/3 - `npm run test:unit` — 181 files, 1589 passed, 2 skipped - `npm run lint` (`tsc -p tsconfig.json --noEmit`) — clean The embedded-host path (`adapters/deepseek-harness`, which intentionally passes `initLogging: false`) is untouched. --- apps/memos-local-plugin/bridge.mts | 5 ++ .../integration/bridge-esm-entry.test.ts | 54 ++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/apps/memos-local-plugin/bridge.mts b/apps/memos-local-plugin/bridge.mts index 75f7e7c37..d62e35656 100644 --- a/apps/memos-local-plugin/bridge.mts +++ b/apps/memos-local-plugin/bridge.mts @@ -264,6 +264,11 @@ async function main(): Promise { pkgVersion, hostLlmBridge: args.daemon ? null : lazyHostLlmBridge, home: resolvedHome, + // Standalone bridge owns its stdio — initialize the logger from + // config.logging (timezone, level, channels, file sinks). Without this the + // logger stays on the bootstrap console default (tz pinned to "UTC"), which + // makes logging.timezone inert in the daemon. + initLogging: true, }); const telemetry = new Telemetry( diff --git a/apps/memos-local-plugin/tests/integration/bridge-esm-entry.test.ts b/apps/memos-local-plugin/tests/integration/bridge-esm-entry.test.ts index 6ca2a66c2..73fbdebae 100644 --- a/apps/memos-local-plugin/tests/integration/bridge-esm-entry.test.ts +++ b/apps/memos-local-plugin/tests/integration/bridge-esm-entry.test.ts @@ -41,13 +41,19 @@ interface SpawnResult { function spawnBridge( args: readonly string[], - options: { home: string; timeoutMs: number }, + options: { + home: string; + timeoutMs: number; + /** Extra environment overrides (e.g. MEMOS_HOME). */ + env?: Record; + }, ): Promise { return new Promise((resolve, reject) => { const proc = spawn(process.execPath, [BRIDGE_MJS, ...args], { env: { ...process.env, HOME: options.home, + ...options.env, }, stdio: ["pipe", "pipe", "pipe"], }); @@ -128,3 +134,49 @@ describe("Issue #1736 — bridge ESM entry boots without CJS 'exports' error", ( } }, 20_000); }); + +describe("Issue #2399 — ESM bridge entry initializes file logging sinks", () => { + it("dist/bridge.mjs startup creates the config.logging file sinks under /logs", async () => { + // The build step is a precondition for this test (same contract as the + // #1736 case above): skip silently when the artifact is missing. + if (!fs.existsSync(BRIDGE_MJS)) { + return; + } + + const home = fs.mkdtempSync(path.join(os.tmpdir(), "memos-2399-")); + try { + // Pass the runtime home explicitly and pin it through both channels — + // MEMOS_HOME is what clobbers any developer shell export, and it + // outranks the --home flag in resolveHome's precedence (see + // core/config/paths.ts) — so the assertion target is deterministic: + // resolveHome("hermes", home).root === home, hence + // home.logsDir === /logs. + const result = await spawnBridge( + ["--agent=hermes", "--no-viewer", `--home=${home}`], + { home, timeoutMs: 8_000, env: { MEMOS_HOME: home } }, + ); + + // Environment tolerance, mirroring the #1736 test above: a missing + // better-sqlite3 native binding kills the bridge in a clean sandbox + // and is unrelated to this issue. + const combined = `${result.stdout}\n${result.stderr}`; + if (combined.includes("Could not locate the bindings file")) { + return; + } + + // The standalone ESM entry owns its stdio and must therefore bootstrap + // the global logger from config.logging — the legacy CJS entry + // (bridge.cts) already passes `initLogging: true`, but bridge.mts never + // did. Because the transports open their files eagerly during + // initLogger (before any downstream work), the sinks are a + // deterministic post-fix marker: no timing sensitivity. + expect( + fs.existsSync(path.join(home, "logs", "memos.log")), + `expected a file sink at ${path.join(home, "logs", "memos.log")} after startup; stderr:\n${result.stderr}`, + ).toBe(true); + expect(fs.existsSync(path.join(home, "logs", "error.log"))).toBe(true); + } finally { + fs.rmSync(home, { recursive: true, force: true }); + } + }, 20_000); +});