fix: guard memory sync errors
This commit is contained in:
@@ -47,6 +47,7 @@
|
|||||||
- Sub-agents: normalize announce delivery origin + queue bucketing by accountId to keep multi-account routing stable. (#1061, #1058) — thanks @adam91holt.
|
- Sub-agents: normalize announce delivery origin + queue bucketing by accountId to keep multi-account routing stable. (#1061, #1058) — thanks @adam91holt.
|
||||||
- Sessions: include deliveryContext in sessions.list and reuse normalized delivery routing for announce/restart fallbacks. (#1058)
|
- Sessions: include deliveryContext in sessions.list and reuse normalized delivery routing for announce/restart fallbacks. (#1058)
|
||||||
- Sessions: propagate deliveryContext into last-route updates to keep account/channel routing stable. (#1058)
|
- Sessions: propagate deliveryContext into last-route updates to keep account/channel routing stable. (#1058)
|
||||||
|
- Memory: prevent unhandled rejections when watch/interval sync fails. (#1076) — thanks @roshanasingh4.
|
||||||
- Gateway: honor explicit delivery targets without implicit accountId fallback; preserve lastAccountId for implicit routing.
|
- Gateway: honor explicit delivery targets without implicit accountId fallback; preserve lastAccountId for implicit routing.
|
||||||
- Gateway: avoid reusing last-to/accountId when the requested channel differs; sync deliveryContext with last route fields.
|
- Gateway: avoid reusing last-to/accountId when the requested channel differs; sync deliveryContext with last route fields.
|
||||||
- Repo: fix oxlint config filename and move ignore pattern into config. (#1064) — thanks @connorshea.
|
- Repo: fix oxlint config filename and move ignore pattern into config. (#1064) — thanks @connorshea.
|
||||||
|
|||||||
92
src/memory/manager.sync-errors-do-not-crash.test.ts
Normal file
92
src/memory/manager.sync-errors-do-not-crash.test.ts
Normal file
@@ -0,0 +1,92 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import { getMemorySearchManager, type MemoryIndexManager } from "./index.js";
|
||||||
|
|
||||||
|
vi.mock("chokidar", () => ({
|
||||||
|
default: {
|
||||||
|
watch: vi.fn(() => ({
|
||||||
|
on: vi.fn(),
|
||||||
|
close: vi.fn(async () => undefined),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("./embeddings.js", () => {
|
||||||
|
return {
|
||||||
|
createEmbeddingProvider: async () => ({
|
||||||
|
requestedProvider: "openai",
|
||||||
|
provider: {
|
||||||
|
id: "mock",
|
||||||
|
model: "mock-embed",
|
||||||
|
embedQuery: async () => [0, 0, 0],
|
||||||
|
embedBatch: async () => {
|
||||||
|
throw new Error("openai embeddings failed: 429 insufficient_quota");
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("memory manager sync failures", () => {
|
||||||
|
let workspaceDir: string;
|
||||||
|
let indexPath: string;
|
||||||
|
let manager: MemoryIndexManager | null = null;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdbot-mem-"));
|
||||||
|
indexPath = path.join(workspaceDir, "index.sqlite");
|
||||||
|
await fs.mkdir(path.join(workspaceDir, "memory"));
|
||||||
|
await fs.writeFile(path.join(workspaceDir, "MEMORY.md"), "Hello");
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
if (manager) {
|
||||||
|
await manager.close();
|
||||||
|
manager = null;
|
||||||
|
}
|
||||||
|
await fs.rm(workspaceDir, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not raise unhandledRejection when watch-triggered sync fails", async () => {
|
||||||
|
const unhandled: unknown[] = [];
|
||||||
|
const handler = (reason: unknown) => {
|
||||||
|
unhandled.push(reason);
|
||||||
|
};
|
||||||
|
process.on("unhandledRejection", handler);
|
||||||
|
|
||||||
|
const cfg = {
|
||||||
|
agents: {
|
||||||
|
defaults: {
|
||||||
|
workspace: workspaceDir,
|
||||||
|
memorySearch: {
|
||||||
|
provider: "openai",
|
||||||
|
model: "mock-embed",
|
||||||
|
store: { path: indexPath },
|
||||||
|
sync: { watch: true, watchDebounceMs: 1, onSessionStart: false, onSearch: false },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
list: [{ id: "main", default: true }],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = await getMemorySearchManager({ cfg, agentId: "main" });
|
||||||
|
expect(result.manager).not.toBeNull();
|
||||||
|
if (!result.manager) throw new Error("manager missing");
|
||||||
|
manager = result.manager;
|
||||||
|
|
||||||
|
// Call the internal scheduler directly; it uses fire-and-forget sync.
|
||||||
|
(manager as unknown as { scheduleWatchSync: () => void }).scheduleWatchSync();
|
||||||
|
|
||||||
|
await vi.runAllTimersAsync();
|
||||||
|
await Promise.resolve();
|
||||||
|
|
||||||
|
process.off("unhandledRejection", handler);
|
||||||
|
expect(unhandled).toHaveLength(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,6 +8,7 @@ import { resolveAgentDir, resolveAgentWorkspaceDir } from "../agents/agent-scope
|
|||||||
import type { ResolvedMemorySearchConfig } from "../agents/memory-search.js";
|
import type { ResolvedMemorySearchConfig } from "../agents/memory-search.js";
|
||||||
import { resolveMemorySearchConfig } from "../agents/memory-search.js";
|
import { resolveMemorySearchConfig } from "../agents/memory-search.js";
|
||||||
import type { ClawdbotConfig } from "../config/config.js";
|
import type { ClawdbotConfig } from "../config/config.js";
|
||||||
|
import { createSubsystemLogger } from "../logging.js";
|
||||||
import { resolveUserPath, truncateUtf16Safe } from "../utils.js";
|
import { resolveUserPath, truncateUtf16Safe } from "../utils.js";
|
||||||
import {
|
import {
|
||||||
createEmbeddingProvider,
|
createEmbeddingProvider,
|
||||||
@@ -46,6 +47,8 @@ type MemoryIndexMeta = {
|
|||||||
const META_KEY = "memory_index_meta_v1";
|
const META_KEY = "memory_index_meta_v1";
|
||||||
const SNIPPET_MAX_CHARS = 700;
|
const SNIPPET_MAX_CHARS = 700;
|
||||||
|
|
||||||
|
const log = createSubsystemLogger("memory");
|
||||||
|
|
||||||
const INDEX_CACHE = new Map<string, MemoryIndexManager>();
|
const INDEX_CACHE = new Map<string, MemoryIndexManager>();
|
||||||
|
|
||||||
export class MemoryIndexManager {
|
export class MemoryIndexManager {
|
||||||
@@ -314,7 +317,9 @@ export class MemoryIndexManager {
|
|||||||
if (!minutes || minutes <= 0 || this.intervalTimer) return;
|
if (!minutes || minutes <= 0 || this.intervalTimer) return;
|
||||||
const ms = minutes * 60 * 1000;
|
const ms = minutes * 60 * 1000;
|
||||||
this.intervalTimer = setInterval(() => {
|
this.intervalTimer = setInterval(() => {
|
||||||
void this.sync({ reason: "interval" });
|
void this.sync({ reason: "interval" }).catch((err) => {
|
||||||
|
log.warn(`memory sync failed (interval): ${String(err)}`);
|
||||||
|
});
|
||||||
}, ms);
|
}, ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -323,7 +328,9 @@ export class MemoryIndexManager {
|
|||||||
if (this.watchTimer) clearTimeout(this.watchTimer);
|
if (this.watchTimer) clearTimeout(this.watchTimer);
|
||||||
this.watchTimer = setTimeout(() => {
|
this.watchTimer = setTimeout(() => {
|
||||||
this.watchTimer = null;
|
this.watchTimer = null;
|
||||||
void this.sync({ reason: "watch" });
|
void this.sync({ reason: "watch" }).catch((err) => {
|
||||||
|
log.warn(`memory sync failed (watch): ${String(err)}`);
|
||||||
|
});
|
||||||
}, this.settings.sync.watchDebounceMs);
|
}, this.settings.sync.watchDebounceMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user