Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

perf(companion): debounce quiet discovery checks

tianma-ifc651f0b31db6
diff --git a/apps/web/src/components/CompanionDiscoveryHub.test.mjs b/apps/web/src/components/CompanionDiscoveryHub.test.mjs
index f7ece139..fce24f02 100644
--- a/apps/web/src/components/CompanionDiscoveryHub.test.mjs
+++ b/apps/web/src/components/CompanionDiscoveryHub.test.mjs
@@ -76,6 +76,9 @@ describe("quiet discovery UI", () => {
test("checks are event-driven, sync-gated and cancel on cleanup", () => {
const source = readFileSync(new URL("./CompanionDiscoveryHub.tsx", import.meta.url), "utf8");
expect(source).not.toContain("setInterval"); expect(source).not.toContain("new Notification");
+ expect(source).toContain("const DISCOVERY_IDLE_DELAY_MS = 3 * 60_000");
+ expect(source).toContain("attemptedSinceWorkspaceChange");
+ expect(source).toContain("scheduleAfterWorkspaceChange");
expect(source).toContain('document.visibilityState !== "visible"'); expect(source).toContain("await assertCompanionChangesSynced(scope)");
expect(source).toContain("stop.abort()"); expect(source).toContain("IntersectionObserver");
});
diff --git a/apps/web/src/components/CompanionDiscoveryHub.tsx b/apps/web/src/components/CompanionDiscoveryHub.tsx
index b039b4d8..355f315e 100644
--- a/apps/web/src/components/CompanionDiscoveryHub.tsx
+++ b/apps/web/src/components/CompanionDiscoveryHub.tsx
@@ -19,6 +19,8 @@ import { assertCompanionChangesSynced } from "@/lib/companion-actions";
import { discoveryFeedKey, discoverySettingsKey, useCompanionDiscoverySettings } from "@/hooks/useCompanionDiscovery";
import { CompanionActionCard } from "./CompanionActionCard";
+const DISCOVERY_IDLE_DELAY_MS = 3 * 60_000;
+
export default function CompanionDiscoveryHub({ scope, onOpenNote, onNotesChanged, onOpenSettings }: {
scope: string; onOpenNote: (id: string, notebookId: string) => void; onNotesChanged: () => Promise<void>; onOpenSettings: () => void;
}) {
@@ -39,16 +41,23 @@ export default function CompanionDiscoveryHub({ scope, onOpenNote, onNotesChange
if (!enabled) return;
let timer: ReturnType<typeof setTimeout> | undefined;
let running = false;
+ let attemptedSinceWorkspaceChange = false;
const stop = new AbortController();
const check = async () => {
- if (running || stop.signal.aborted || document.visibilityState !== "visible" || !navigator.onLine) return;
- if (lastCheckAt.current && Date.now() - Date.parse(lastCheckAt.current) < 86400000) return;
+ if (running || attemptedSinceWorkspaceChange || stop.signal.aborted || document.visibilityState !== "visible" || !navigator.onLine) return;
+ if (lastCheckAt.current && Date.now() - Date.parse(lastCheckAt.current) < 86400000) {
+ attemptedSinceWorkspaceChange = true;
+ return;
+ }
running = true;
try {
await assertCompanionChangesSynced(scope);
if (stop.signal.aborted) return;
const result = await api.checkCompanionDiscoveries(i18n.resolvedLanguage ?? "en-US", stop.signal);
- if (!stop.signal.aborted) client.setQueryData(discoveryFeedKey(scope), result.items);
+ if (!stop.signal.aborted) {
+ attemptedSinceWorkspaceChange = true;
+ client.setQueryData(discoveryFeedKey(scope), result.items);
+ }
} catch { /* Quiet by design; check status is visible inside the panel. */ }
finally {
running = false;
@@ -57,15 +66,21 @@ export default function CompanionDiscoveryHub({ scope, onOpenNote, onNotesChange
};
const schedule = () => {
clearTimeout(timer);
- if (document.visibilityState === "visible" && navigator.onLine) timer = setTimeout(() => void check(), 60_000);
+ if (!attemptedSinceWorkspaceChange && document.visibilityState === "visible" && navigator.onLine) {
+ timer = setTimeout(() => void check(), DISCOVERY_IDLE_DELAY_MS);
+ }
};
- const events = ["keydown", "pointerdown", "input", "online", "edgeever:sync-queue-changed", "edgeever:memo-detail-refreshed"];
- events.forEach(name => window.addEventListener(name, schedule));
+ const activityEvents = ["keydown", "pointerdown", "input", "online"];
+ const workspaceChangeEvents = ["edgeever:sync-queue-changed", "edgeever:memo-detail-refreshed"];
+ const scheduleAfterWorkspaceChange = () => { attemptedSinceWorkspaceChange = false; schedule(); };
+ activityEvents.forEach(name => window.addEventListener(name, schedule));
+ workspaceChangeEvents.forEach(name => window.addEventListener(name, scheduleAfterWorkspaceChange));
document.addEventListener("visibilitychange", schedule);
schedule();
return () => {
clearTimeout(timer); stop.abort();
- events.forEach(name => window.removeEventListener(name, schedule));
+ activityEvents.forEach(name => window.removeEventListener(name, schedule));
+ workspaceChangeEvents.forEach(name => window.removeEventListener(name, scheduleAfterWorkspaceChange));
document.removeEventListener("visibilitychange", schedule);
};
}, [enabled, settings.data?.version, scope, i18n.resolvedLanguage, client]);
@@ -240,4 +255,3 @@ function DiscoveryCard({ item, busy, open, onApply, onDismiss, onOpenNote, onSee
</div>
</article>;
}
-
diff --git a/docs/personality-memory-agent.md b/docs/personality-memory-agent.md
index 7c8100af..ebcea2b2 100644
--- a/docs/personality-memory-agent.md
+++ b/docs/personality-memory-agent.md
@@ -123,7 +123,7 @@ This is a provisional decision based on current scope and architecture, not a cl
### Bounded cost
-- Proactive checks run only while the app is open, visible, online, free of pending sync changes, and idle for one minute. A cross-device limit allows at most one attempt per account per 24 hours; unchanged notes are skipped. This is not an always-running background service.
+- Proactive checks run only while the app is open, visible, online, free of pending sync changes, and inactive for three minutes. A cross-device limit allows at most one attempt per account per 24 hours; unchanged notes are skipped. After one completed check, the client makes no further request in that session until another note or sync change occurs. This is not an always-running background service.
- Each check selects a small set from recent records and keyword searches: at most six note bodies totaling 12,000 characters. One structured generation, at most 1,200 output tokens, cancellation after 60 seconds, and no automatic model retries.
- Full conversations have separate tool-call, context, concurrency, and timeout budgets. Library-wide authorization does not mean putting the entire library into a prompt.
- Selected content is sent to the user's configured default model provider. A check can incur charges without producing a visible suggestion. Keyword retrieval may miss semantic connections, and low-frequency checks do not guarantee timely discovery of every opportunity.
diff --git a/docs/personality-memory-agent.zh-CN.md b/docs/personality-memory-agent.zh-CN.md
index 5512d6c9..04ab9d5b 100644
--- a/docs/personality-memory-agent.zh-CN.md
+++ b/docs/personality-memory-agent.zh-CN.md
@@ -123,7 +123,7 @@ Agent 专用表共七张,位于现有实例数据库中,不另建数据库
### 有界成本
-- 主动检查仅在应用打开、可见、联网、无待同步改动且停止输入一分钟后进行。跨设备共享限频,每账户每 24 小时最多尝试一次,笔记无变化跳过;不是全天候后台服务。
+- 主动检查仅在应用打开、可见、联网、无待同步改动且停止操作 3 分钟后进行。跨设备共享限频,每账户每 24 小时最多尝试一次,笔记无变化跳过;客户端完成一次检查后,若没有新的笔记或同步变化,本轮会话不再重复请求。它不是全天候后台服务。
- 每次从近期记录和关键词查询中选择少量笔记,最多六篇正文、总计 12,000 字符;一次结构化生成,最多 1,200 输出 token、60 秒取消,不自动重试模型请求。
- 完整对话有独立的工具调用、上下文、并发和超时预算;授权全库不等于把全库放进 Prompt。
- 按需选出的内容会发送给用户配置的默认模型服务商。没有生成可展示建议也可能产生费用。关键词召回可能漏掉语义关联,低频检查也不保证及时发现所有机会。
diff --git a/packages/shared/src/i18n/en-US.ts b/packages/shared/src/i18n/en-US.ts
index 2d0db751..6dceffcc 100644
--- a/packages/shared/src/i18n/en-US.ts
+++ b/packages/shared/src/i18n/en-US.ts
@@ -29,7 +29,7 @@ export const enUS = {
lastCheck: "Last check",
neverChecked: "Not checked yet",
nextCheck: "Next check",
- nextCheckEnabled: "After notes change, the app is idle, and at least 24 hours have passed since the last check",
+ nextCheckEnabled: "After notes change, 3 minutes without activity, and at least 24 hours since the last check",
nextCheckDisabled: "Checks begin after Paw mode is enabled",
scope: "Each check sends only a small selection of relevant notes from this account to the default model. It stays quiet without a useful result, and every note change requires your approval.",
latest: "Latest discovery",
diff --git a/packages/shared/src/i18n/zh-CN.ts b/packages/shared/src/i18n/zh-CN.ts
index e9f73241..a6198a59 100644
--- a/packages/shared/src/i18n/zh-CN.ts
+++ b/packages/shared/src/i18n/zh-CN.ts
@@ -29,7 +29,7 @@ export const zhCN = {
lastCheck: "最近检查",
neverChecked: "尚未检查",
nextCheck: "下次检查",
- nextCheckEnabled: "笔记有新变化、应用空闲,且距上次检查至少 24 小时后",
+ nextCheckEnabled: "笔记有新变化、停止操作 3 分钟,且距上次检查至少 24 小时后",
nextCheckDisabled: "启用后才会检查",
scope: "每次只选取当前账户中少量相关笔记交给默认模型。没有有价值的结果时保持安静;任何笔记改动都需要你确认。",
latest: "最近发现",