From c80d351658e953ea1c5b37ff44083b15889619c6 Mon Sep 17 00:00:00 2001 From: autodev Date: Wed, 8 Jul 2026 15:48:27 +0800 Subject: [PATCH 1/3] fix(memos-local-plugin): actually skip evolution pipeline when lightweightMemory is enabled (#2063) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `buildPipelineSubscribers` used to attach reward / L2 / L3 / skill / feedback subscribers unconditionally, so `algorithm.lightweightMemory.enabled: true` only affected `flush()` — every `episode.finalized` still cascaded through the LLM-heavy evolution chain. Startup + periodic recovery compounded the bug: `recoverOpenEpisodesAsSessionEnd` and `recoverDirtyClosedEpisodes` re-emitted `episode.finalized` for legacy episodes that lack `meta.lightweightMemory === true`, producing a large backlog of `skill_generate` / `world_model_generate` / `policy_evolve` calls after every bridge restart. - Gate subscriber attachment in `buildPipelineSubscribers` and return no-op stubs for the runner/handle shape callers depend on. - Treat every orphan / stale / dirty-closed episode as lightweight in the recovery paths of `memory-core.init`, `autoFinalizeStaleTasks`, and `autoRescoreDirtyClosedEpisodes` when the flag is on. - Regression coverage in `tests/unit/pipeline/lightweight-mode.test.ts`. --- apps/memos-local-plugin/core/pipeline/deps.ts | 180 ++++++++++++++++++ .../core/pipeline/memory-core.ts | 133 +++++++++---- .../unit/pipeline/lightweight-mode.test.ts | 180 ++++++++++++++++++ 3 files changed, 456 insertions(+), 37 deletions(-) create mode 100644 apps/memos-local-plugin/tests/unit/pipeline/lightweight-mode.test.ts diff --git a/apps/memos-local-plugin/core/pipeline/deps.ts b/apps/memos-local-plugin/core/pipeline/deps.ts index 7bd23d681..b9b053bf1 100644 --- a/apps/memos-local-plugin/core/pipeline/deps.ts +++ b/apps/memos-local-plugin/core/pipeline/deps.ts @@ -204,6 +204,7 @@ export function buildPipelineSubscribers( session?: PipelineSessionSet, ): PipelineSubscriberSet { const log = deps.log ?? rootLogger.child({ channel: "core.pipeline" }); + const lightweightMode = algorithm.lightweightMemory.enabled; const captureRunner = createCaptureRunner({ tracesRepo: deps.repos.traces, @@ -217,6 +218,32 @@ export function buildPipelineSubscribers( now: deps.now, }); + // Lightweight mode short-circuits the evolution pipeline: reward / + // L2 / L3 / skill / feedback subscribers are NOT attached to their + // upstream buses. This matches the schema comment on + // `algorithm.lightweightMemory.enabled` which promises that only + // summarize + embedding + retrieval filter remain active. Attaching + // the subscribers used to be unconditional (issue #2063) — every + // `episode.finalized` event still cascaded through the LLM-heavy + // evolution chain, defeating the flag. + if (lightweightMode) { + log.info("pipeline.lightweight_mode.enabled", { + skipped: ["reward", "l2", "l3", "skill", "feedback"], + }); + return { + captureRunner, + rewardRunner: lightweightRewardRunnerStub(), + l2: lightweightL2Handle(), + l3: lightweightL3Handle(), + skills: lightweightSkillHandle(), + feedback: lightweightFeedbackHandle(), + subscriptions: { + capture: lightweightCaptureSubscription(), + reward: lightweightRewardSubscription(), + }, + }; + } + const rewardRunner = createRewardRunner({ tracesRepo: deps.repos.traces, episodesRepo: deps.repos.episodes, @@ -406,3 +433,156 @@ export type { SkillEventBus, SkillSubscriberHandle, }; + +// ─── Lightweight-mode subscriber stubs ──────────────────────────────────── +// +// When `algorithm.lightweightMemory.enabled` is `true`, the pipeline skips +// mounting the reward / L2 / L3 / skill / feedback runners entirely (see +// issue #2063). Downstream code (memory-core, orchestrator) still holds +// references to these handles for shape compatibility (`flush()` / +// `shutdown()` walk the whole subscriber set) but every caller that would +// actually invoke a runner is already gated behind the same lightweight +// check — so these stubs must only satisfy the type shape + drain/stop +// lifecycle without producing events or LLM traffic. + +function throwLightweight(feature: string): never { + throw new Error( + `pipeline: ${feature} is unavailable while ` + + `algorithm.lightweightMemory.enabled=true. Every caller must gate ` + + `on the same flag before invoking it.`, + ); +} + +function lightweightRewardRunnerStub(): RewardRunner { + return { + async run() { + return throwLightweight("rewardRunner.run"); + }, + }; +} + +function lightweightRewardSubscription(): RewardSubscription { + return { + submitFeedback() { + throwLightweight("rewardSubscription.submitFeedback"); + }, + async runManually() { + throwLightweight("rewardSubscription.runManually"); + }, + stop() { + /* no-op — nothing was attached */ + }, + async drain() { + /* no-op — no in-flight work in lightweight mode */ + }, + pendingCount() { + return 0; + }, + }; +} + +function lightweightCaptureSubscription(): CaptureSubscription { + return { + stop() { + /* no-op — capture subscriber is not attached in lightweight mode */ + }, + async drain() { + /* no-op — orchestrator calls runLightweight/runLite directly */ + }, + pendingCount() { + return 0; + }, + }; +} + +function lightweightL2Handle(): L2SubscriberHandle { + return { + detach() { + /* no-op */ + }, + async runOnce() { + throwLightweight("l2.runOnce"); + }, + async drain() { + /* no-op */ + }, + }; +} + +function lightweightL3Handle(): L3SubscriberHandle { + return { + detach() { + /* no-op */ + }, + async drain() { + /* no-op */ + }, + async runOnce() { + return throwLightweight("l3.runOnce"); + }, + async adjustFeedback() { + return throwLightweight("l3.adjustFeedback"); + }, + }; +} + +function lightweightSkillHandle(): SkillSubscriberHandle { + return { + dispose() { + /* no-op */ + }, + async runOnce() { + return throwLightweight("skills.runOnce"); + }, + applyFeedback() { + throwLightweight("skills.applyFeedback"); + }, + async lifecycleTick() { + /* no-op */ + }, + async flush() { + /* no-op */ + }, + }; +} + +function lightweightFeedbackHandle(): FeedbackSubscriberHandle { + const signals: FeedbackSubscriberHandle["signals"] = { + recordFailure() { + return null; + }, + recordSuccess() { + /* no-op */ + }, + peek() { + return null; + }, + clear() { + /* no-op */ + }, + stats() { + return { states: 0, totalFailures: 0 }; + }, + }; + return { + recordToolFailure() { + /* no-op — feedback subsystem is off in lightweight mode */ + }, + recordToolSuccess() { + /* no-op */ + }, + async submitUserFeedback() { + return throwLightweight("feedback.submitUserFeedback"); + }, + async runOnce() { + return throwLightweight("feedback.runOnce"); + }, + signals, + async flush() { + /* no-op */ + }, + dispose() { + /* no-op */ + }, + }; +} diff --git a/apps/memos-local-plugin/core/pipeline/memory-core.ts b/apps/memos-local-plugin/core/pipeline/memory-core.ts index 2ae14f053..65424b61b 100644 --- a/apps/memos-local-plugin/core/pipeline/memory-core.ts +++ b/apps/memos-local-plugin/core/pipeline/memory-core.ts @@ -597,6 +597,11 @@ export function createMemoryCore( const nowMs = Date.now(); if (nowMs - lastStaleScan < 30_000) return; lastStaleScan = nowMs; + // Issue #2063: lightweight mode disables reward / L2 / L3 / skill; + // the periodic auto-finalize used to emit `episode.finalized` for + // every stale open topic, which still cascaded into the evolution + // chain in earlier releases. Close them silently instead. + const lightweightMode = handle.algorithm.lightweightMemory.enabled; try { const openEpisodes = handle.repos.episodes .list({ status: "open", limit: 200 }) @@ -615,7 +620,27 @@ export function createMemoryCore( stale.push(ep); } } - if (stale.length > 0) await recoverOpenEpisodesAsSessionEnd(stale); + if (stale.length === 0) return; + if (lightweightMode) { + for (const ep of stale) { + try { + handle.repos.episodes.close(ep.id as EpisodeId, nowMs, ep.rTask ?? undefined); + handle.repos.episodes.updateMeta(ep.id as EpisodeId, { + lightweightMemory: true, + closeReason: "finalized", + recoveredAtStartup: nowMs, + recoveryReason: "lightweight_stale_topic_close", + }); + } catch (err) { + log.debug("stale_topic.lightweight_close_error", { + episodeId: ep.id, + err: err instanceof Error ? err.message : String(err), + }); + } + } + return; + } + await recoverOpenEpisodesAsSessionEnd(stale); } catch (err) { log.debug("stale_topic.scan_error", { err: err instanceof Error ? err.message : String(err), @@ -627,6 +652,11 @@ export function createMemoryCore( const nowMs = Date.now(); if (nowMs - lastDirtyClosedScan < 30_000) return; lastDirtyClosedScan = nowMs; + // Issue #2063: lightweight mode explicitly disables reward — the + // periodic dirty-closed rescan (which would emit + // `episode.finalized` and call `handle.rewardRunner.run(...)`) + // must be a no-op. + if (handle.algorithm.lightweightMemory.enabled) return; try { const allDirty = handle.repos.episodes .list({ status: "closed", limit: 500 }) @@ -926,53 +956,82 @@ export function createMemoryCore( const orphans = handle.repos.episodes.list({ status: "open", limit: 500 }); if (orphans.length > 0) { const nowMs = Date.now(); - const lightweight = orphans.filter((ep) => isLightweightEpisode(ep)); - for (const ep of lightweight) { + // Issue #2063: when `algorithm.lightweightMemory.enabled` is + // true, treat EVERY orphan episode as lightweight — legacy rows + // written before the flag was flipped lack + // `meta.lightweightMemory=true`, and prior versions still + // re-emitted `episode.finalized` on them at startup which + // cascaded into reward / L2 / L3 / skill LLM calls (the + // "backlog of evolution calls after every bridge restart" + // reported by the issue). Closing them silently here matches + // the schema-comment guarantee that the flag disables the + // whole evolution pipeline. + const lightweightMode = handle.algorithm.lightweightMemory.enabled; + const treatAsLightweight = lightweightMode + ? orphans + : orphans.filter((ep) => isLightweightEpisode(ep)); + for (const ep of treatAsLightweight) { handle.repos.episodes.close(ep.id as EpisodeId, nowMs, ep.rTask ?? undefined); handle.repos.episodes.updateMeta(ep.id as EpisodeId, { lightweightMemory: true, closeReason: "finalized", recoveredAtStartup: nowMs, - recoveryReason: "lightweight_startup_close", + recoveryReason: lightweightMode && !isLightweightEpisode(ep) + ? "lightweight_startup_close_backfill" + : "lightweight_startup_close", }); } - const normalOrphans = orphans.filter((ep) => !isLightweightEpisode(ep)); - const stale = normalOrphans.filter( - (ep) => - ep.rTask != null || - (ep.traceIds?.length ?? 0) > 0 || - nowMs - (ep.endedAt ?? ep.startedAt) > STALE_EPISODE_TIMEOUT_MS, - ); - const recent = normalOrphans.filter((ep) => !stale.includes(ep)); - for (const ep of recent) { - handle.repos.episodes.updateMeta(ep.id as EpisodeId, { - topicState: (ep.meta?.topicState as string | undefined) ?? "interrupted", - pauseReason: (ep.meta?.pauseReason as string | undefined) ?? "startup_recovered_open_topic", - recoveredAtStartup: nowMs, - }); + if (lightweightMode) { + // No reward / L2 / L3 / skill work is scheduled when the + // pipeline is lightweight — leave both background queues + // empty so the recovery promise short-circuits below. + staleForBackground = []; + dirtyClosedForBackground = []; + } else { + const normalOrphans = orphans.filter((ep) => !isLightweightEpisode(ep)); + const stale = normalOrphans.filter( + (ep) => + ep.rTask != null || + (ep.traceIds?.length ?? 0) > 0 || + nowMs - (ep.endedAt ?? ep.startedAt) > STALE_EPISODE_TIMEOUT_MS, + ); + const recent = normalOrphans.filter((ep) => !stale.includes(ep)); + for (const ep of recent) { + handle.repos.episodes.updateMeta(ep.id as EpisodeId, { + topicState: (ep.meta?.topicState as string | undefined) ?? "interrupted", + pauseReason: (ep.meta?.pauseReason as string | undefined) ?? "startup_recovered_open_topic", + recoveredAtStartup: nowMs, + }); + } + staleForBackground = stale; } - staleForBackground = stale; } - const nowForDirty = Date.now(); - const allDirty = handle.repos.episodes - .list({ status: "closed", limit: 500 }) - .filter((ep) => !isLightweightEpisode(ep) && episodeRewardIsDirty(ep)); - const dirtyClosed: typeof allDirty = []; - for (const ep of allDirty) { - if (dirtyEpisodeBackoffElapsed(ep, nowForDirty)) { - dirtyClosed.push(ep); - } else { - const dirtyMeta = (ep.meta?.rewardDirty as - | { failedAttempts?: number; lastFailureAt?: number } - | undefined) ?? {}; - log.debug("init.dirty_closed_episodes.skip_backoff", { - episodeId: ep.id, - failedAttempts: dirtyMeta.failedAttempts ?? 0, - lastFailureAt: dirtyMeta.lastFailureAt ?? 0, - }); + if (handle.algorithm.lightweightMemory.enabled) { + // Same story for dirty-closed episodes — never rescore them + // when the pipeline is intentionally light. + dirtyClosedForBackground = []; + } else { + const nowForDirty = Date.now(); + const allDirty = handle.repos.episodes + .list({ status: "closed", limit: 500 }) + .filter((ep) => !isLightweightEpisode(ep) && episodeRewardIsDirty(ep)); + const dirtyClosed: typeof allDirty = []; + for (const ep of allDirty) { + if (dirtyEpisodeBackoffElapsed(ep, nowForDirty)) { + dirtyClosed.push(ep); + } else { + const dirtyMeta = (ep.meta?.rewardDirty as + | { failedAttempts?: number; lastFailureAt?: number } + | undefined) ?? {}; + log.debug("init.dirty_closed_episodes.skip_backoff", { + episodeId: ep.id, + failedAttempts: dirtyMeta.failedAttempts ?? 0, + lastFailureAt: dirtyMeta.lastFailureAt ?? 0, + }); + } } + dirtyClosedForBackground = dirtyClosed; } - dirtyClosedForBackground = dirtyClosed; } catch (err) { log.debug("init.orphan_scan.failed", { err: err instanceof Error ? err.message : String(err), diff --git a/apps/memos-local-plugin/tests/unit/pipeline/lightweight-mode.test.ts b/apps/memos-local-plugin/tests/unit/pipeline/lightweight-mode.test.ts new file mode 100644 index 000000000..ff514da8e --- /dev/null +++ b/apps/memos-local-plugin/tests/unit/pipeline/lightweight-mode.test.ts @@ -0,0 +1,180 @@ +/** + * Regression tests for issue #2063 — `algorithm.lightweightMemory.enabled: + * true` must actually skip the evolution pipeline (reward / L2 / L3 / + * skill / feedback), not just short-circuit inside `flush()`. The bug was + * that `buildPipelineSubscribers` unconditionally attached every runner + * to the buses, so every `capture.done` / `reward.updated` / ... event + * still cascaded through the LLM-heavy evolution chain even with the + * flag on. + * + * These tests lock in the schema-comment contract: + * > When enabled, the runtime skips task/reward/L2/L3/skill evolution + * > and keeps only summarize + embedding + retrieval filter. + */ + +import { afterEach, beforeEach, describe, expect, it } from "vitest"; + +import { + buildPipelineBuses, + buildPipelineSession, + buildPipelineSubscribers, + createPipeline, + extractAlgorithmConfig, + type PipelineDeps, + type PipelineHandle, +} from "../../../core/pipeline/index.js"; +import { DEFAULT_CONFIG } from "../../../core/config/defaults.js"; +import { resolveHome } from "../../../core/config/paths.js"; +import { rootLogger } from "../../../core/logger/index.js"; +import { makeTmpDb, type TmpDbHandle } from "../../helpers/tmp-db.js"; +import { fakeEmbedder } from "../../helpers/fake-embedder.js"; +import type { EpisodeSnapshot } from "../../../core/session/index.js"; + +let dbHandle: TmpDbHandle | null = null; +let pipeline: PipelineHandle | null = null; + +function configWithLightweightMemory(enabled: boolean): typeof DEFAULT_CONFIG { + return { + ...DEFAULT_CONFIG, + algorithm: { + ...DEFAULT_CONFIG.algorithm, + lightweightMemory: { + ...DEFAULT_CONFIG.algorithm.lightweightMemory, + enabled, + }, + }, + }; +} + +function buildDeps( + h: TmpDbHandle, + lightweight: boolean, +): PipelineDeps { + return { + agent: "openclaw", + home: resolveHome("openclaw", "/tmp/memos-lightweight-test"), + config: configWithLightweightMemory(lightweight), + db: h.db, + repos: h.repos, + llm: null, + reflectLlm: null, + embedder: fakeEmbedder({ dimensions: 384 }), + log: rootLogger.child({ channel: "test.lightweight" }), + namespace: { agentKind: "openclaw", profileId: "main" }, + now: () => 1_700_000_000_000, + }; +} + +beforeEach(() => { + dbHandle = makeTmpDb(); + pipeline = null; +}); + +afterEach(async () => { + if (pipeline) { + try { + await pipeline.shutdown("test.cleanup"); + } catch { + /* ignore */ + } + pipeline = null; + } + dbHandle?.cleanup(); + dbHandle = null; +}); + +describe("pipeline/lightweight-mode wiring (issue #2063)", () => { + it("normal mode attaches reward/L2/L3/skill subscribers to their upstream buses", () => { + const buses = buildPipelineBuses(); + const deps = buildDeps(dbHandle!, false); + const algorithm = extractAlgorithmConfig(deps); + const session = buildPipelineSession(deps, buses.session); + buildPipelineSubscribers(deps, buses, algorithm, session); + + // rewardSub listens on captureBus for `capture.done`. + expect(buses.capture.listenerCount("capture.done")).toBeGreaterThan(0); + // L2Sub + SkillSub listen on rewardBus. + expect(buses.reward.listenerCount()).toBeGreaterThan(0); + // L3Sub + SkillSub listen on l2Bus. + expect(buses.l2.listenerCount()).toBeGreaterThan(0); + }); + + it("lightweight mode does NOT attach reward/L2/L3/skill subscribers", () => { + const buses = buildPipelineBuses(); + const deps = buildDeps(dbHandle!, true); + const algorithm = extractAlgorithmConfig(deps); + const session = buildPipelineSession(deps, buses.session); + buildPipelineSubscribers(deps, buses, algorithm, session); + + // The evolution pipeline must be completely off — no listeners on + // the upstream buses that would drive reward / L2 / L3 / skill + // runners. + expect(buses.capture.listenerCount("capture.done")).toBe(0); + expect(buses.reward.listenerCount()).toBe(0); + expect(buses.l2.listenerCount()).toBe(0); + }); + + it("lightweight pipeline exposes no evolution listeners even on session bus recovery", async () => { + // Simulate the bug from the issue: bridge starts up in lightweight + // mode, `recoverOpenEpisodesAsSessionEnd` re-emits + // `episode.finalized` for a legacy episode that was closed before + // lightweight mode was turned on (so `meta.lightweightMemory` is + // missing). No LLM-heavy runners must fire. + pipeline = createPipeline(buildDeps(dbHandle!, true)); + + let rewardScheduled = 0; + let l2Started = 0; + let l3Started = 0; + let skillStarted = 0; + pipeline.buses.reward.onAny(() => { + rewardScheduled++; + }); + pipeline.buses.l2.onAny(() => { + l2Started++; + }); + pipeline.buses.l3.onAny(() => { + l3Started++; + }); + pipeline.buses.skill.onAny(() => { + skillStarted++; + }); + + // Fabricate a plain (non-lightweight) episode snapshot and emit a + // recovery-style finalization exactly as `memory-core.ts` does at + // startup. + const legacyEpisode: EpisodeSnapshot = { + id: "ep_legacy" as never, + sessionId: "se_legacy" as never, + status: "closed", + startedAt: 1_699_000_000_000 as never, + endedAt: 1_699_000_005_000 as never, + rTask: null, + traceIds: [], + meta: {}, + turns: [], + turnCount: 0, + intent: { + kind: "task", + confidence: 0.5, + reason: "test-fixture", + retrieval: { tier1: true, tier2: true, tier3: true }, + signals: [], + }, + }; + pipeline.buses.session.emit({ + kind: "episode.finalized", + episode: legacyEpisode, + closedBy: "finalized", + }); + + await pipeline.flush(); + + // In lightweight mode, no reward / L2 / L3 / skill events must + // fire from a session-bus finalization. This is the exact regression + // called out in issue #2063. + expect(rewardScheduled).toBe(0); + expect(l2Started).toBe(0); + expect(l3Started).toBe(0); + expect(skillStarted).toBe(0); + }); +}); From 98d3bfff9b543f7d42c1244eb73573c84d17c0f8 Mon Sep 17 00:00:00 2001 From: autodev Date: Wed, 8 Jul 2026 16:10:10 +0800 Subject: [PATCH 2/3] fix(memos-local-plugin): address open code review findings on lightweight mode - deps.ts: return throwLightweight(...) in rewardSubscription.runManually and l2.runOnce so control-flow analysis marks subsequent statements unreachable, matching the pattern used by every other throwing stub in the file. - memory-core.ts (autoFinalizeStaleTasks lightweight path): write the lightweight meta BEFORE close() so a failing close() cannot leave an episode without meta.lightweightMemory=true (which would let the periodic non-lightweight rescan pick it up). Also rename the meta fields to closeReason="lightweight_stale" + closedAtMs so observability tooling does not mistake a periodic close for startup recovery. - memory-core.ts (init lightweight orphan close): replace the nested ternary for recoveryReason with an explicit if/else. --- apps/memos-local-plugin/core/pipeline/deps.ts | 4 ++-- .../core/pipeline/memory-core.ts | 21 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/apps/memos-local-plugin/core/pipeline/deps.ts b/apps/memos-local-plugin/core/pipeline/deps.ts index b9b053bf1..a80877815 100644 --- a/apps/memos-local-plugin/core/pipeline/deps.ts +++ b/apps/memos-local-plugin/core/pipeline/deps.ts @@ -467,7 +467,7 @@ function lightweightRewardSubscription(): RewardSubscription { throwLightweight("rewardSubscription.submitFeedback"); }, async runManually() { - throwLightweight("rewardSubscription.runManually"); + return throwLightweight("rewardSubscription.runManually"); }, stop() { /* no-op — nothing was attached */ @@ -501,7 +501,7 @@ function lightweightL2Handle(): L2SubscriberHandle { /* no-op */ }, async runOnce() { - throwLightweight("l2.runOnce"); + return throwLightweight("l2.runOnce"); }, async drain() { /* no-op */ diff --git a/apps/memos-local-plugin/core/pipeline/memory-core.ts b/apps/memos-local-plugin/core/pipeline/memory-core.ts index 65424b61b..3e01b576c 100644 --- a/apps/memos-local-plugin/core/pipeline/memory-core.ts +++ b/apps/memos-local-plugin/core/pipeline/memory-core.ts @@ -624,13 +624,18 @@ export function createMemoryCore( if (lightweightMode) { for (const ep of stale) { try { - handle.repos.episodes.close(ep.id as EpisodeId, nowMs, ep.rTask ?? undefined); + // Write the lightweight flag BEFORE close() so that even if + // close() throws mid-way, `isLightweightEpisode()` still + // returns true and the periodic non-lightweight rescan paths + // (autoRescoreDirtyClosedEpisodes) skip this episode instead + // of feeding it into the reward / L2 / L3 pipeline. handle.repos.episodes.updateMeta(ep.id as EpisodeId, { lightweightMemory: true, - closeReason: "finalized", - recoveredAtStartup: nowMs, + closeReason: "lightweight_stale", + closedAtMs: nowMs, recoveryReason: "lightweight_stale_topic_close", }); + handle.repos.episodes.close(ep.id as EpisodeId, nowMs, ep.rTask ?? undefined); } catch (err) { log.debug("stale_topic.lightweight_close_error", { episodeId: ep.id, @@ -971,14 +976,18 @@ export function createMemoryCore( ? orphans : orphans.filter((ep) => isLightweightEpisode(ep)); for (const ep of treatAsLightweight) { + let recoveryReason: string; + if (lightweightMode && !isLightweightEpisode(ep)) { + recoveryReason = "lightweight_startup_close_backfill"; + } else { + recoveryReason = "lightweight_startup_close"; + } handle.repos.episodes.close(ep.id as EpisodeId, nowMs, ep.rTask ?? undefined); handle.repos.episodes.updateMeta(ep.id as EpisodeId, { lightweightMemory: true, closeReason: "finalized", recoveredAtStartup: nowMs, - recoveryReason: lightweightMode && !isLightweightEpisode(ep) - ? "lightweight_startup_close_backfill" - : "lightweight_startup_close", + recoveryReason, }); } if (lightweightMode) { From b9d4a305a2e132f25988016aa2210bd6ec4cea7d Mon Sep 17 00:00:00 2001 From: autodev Date: Wed, 8 Jul 2026 16:21:00 +0800 Subject: [PATCH 3/3] fix(memos-local-plugin): address round-2 open code review on init lightweight close MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three issues in the init() lightweight-orphan-close loop: 1. Ordering: close() was called BEFORE updateMeta(). If close() succeeded but updateMeta() threw, the episode ended up closed without meta.lightweightMemory=true, so autoRescoreDirtyClosedEpisodes would later pick it up and feed it into the reward/L2/L3 pipeline. Now the meta write happens first — matching the periodic stale-topic path. 2. Error isolation: the loop had no per-episode try/catch, so a single DB lock / constraint violation would abort the whole loop and be swallowed by the outer scan-level try/catch. Now each episode has its own try/catch that emits init.lightweight_close_error at debug. 3. Backfill semantics: legacy rows being backfilled were tagged closeReason="finalized" even though they were never actually finalized. They now get closeReason="lightweight_backfill", matching the existing recoveryReason="lightweight_startup_close_backfill" distinction so downstream analytics can tell the two apart. --- .../core/pipeline/memory-core.ts | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/apps/memos-local-plugin/core/pipeline/memory-core.ts b/apps/memos-local-plugin/core/pipeline/memory-core.ts index 3e01b576c..594256d05 100644 --- a/apps/memos-local-plugin/core/pipeline/memory-core.ts +++ b/apps/memos-local-plugin/core/pipeline/memory-core.ts @@ -976,19 +976,40 @@ export function createMemoryCore( ? orphans : orphans.filter((ep) => isLightweightEpisode(ep)); for (const ep of treatAsLightweight) { + const isBackfill = lightweightMode && !isLightweightEpisode(ep); let recoveryReason: string; - if (lightweightMode && !isLightweightEpisode(ep)) { + if (isBackfill) { recoveryReason = "lightweight_startup_close_backfill"; } else { recoveryReason = "lightweight_startup_close"; } - handle.repos.episodes.close(ep.id as EpisodeId, nowMs, ep.rTask ?? undefined); - handle.repos.episodes.updateMeta(ep.id as EpisodeId, { - lightweightMemory: true, - closeReason: "finalized", - recoveredAtStartup: nowMs, - recoveryReason, - }); + try { + // Write the lightweight flag BEFORE close() so that even if + // close() throws, `isLightweightEpisode()` still returns true + // for this row and the periodic non-lightweight rescan paths + // (autoRescoreDirtyClosedEpisodes) skip it instead of feeding + // it into the reward / L2 / L3 pipeline. Mirrors the ordering + // used by the periodic stale-topic close above. + handle.repos.episodes.updateMeta(ep.id as EpisodeId, { + lightweightMemory: true, + // Legacy rows being backfilled were never actually + // finalized through the evolution pipeline; tag them with + // a distinct closeReason so analytics can distinguish a + // real finalize from a lightweight backfill. + closeReason: isBackfill ? "lightweight_backfill" : "finalized", + recoveredAtStartup: nowMs, + recoveryReason, + }); + handle.repos.episodes.close(ep.id as EpisodeId, nowMs, ep.rTask ?? undefined); + } catch (err) { + // Isolate per-episode failures so a single DB lock / + // constraint violation cannot abort the whole startup + // orphan-close loop. Mirrors the periodic stale-topic path. + log.debug("init.lightweight_close_error", { + episodeId: ep.id, + err: err instanceof Error ? err.message : String(err), + }); + } } if (lightweightMode) { // No reward / L2 / L3 / skill work is scheduled when the