dsh-lm-memory 0.17.3 → 0.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -5
- package/cordis.patch.yml +15 -11
- package/docs/project-memory.md +31 -0
- package/docs/task-recall-experiment.md +32 -0
- package/package.json +11 -5
- package/plugin/client.js +41 -14
- package/plugin/lib/confirmed-outcomes.mjs +37 -0
- package/plugin/lib/index.js +236 -118
- package/plugin/lib/project-backends.mjs +56 -0
- package/plugin/lib/project-identity.mjs +30 -0
- package/plugin/lib/project-storage.mjs +58 -0
- package/plugin/lib/project-tool-catalog.json +339 -0
- package/plugin/lib/project-tools.mjs +58 -0
- package/plugin/lib/startup.mjs +50 -0
- package/plugin/lib/task-recall-worker.mjs +31 -0
- package/plugin/lib/task-recall.mjs +128 -0
- package/scripts/migrate-to-personal.mjs +73 -0
- package/server/memory-server.mjs +78 -14
- package/server/viewer-e2e.mjs +77 -64
- package/server/viewer.mjs +1 -1
package/plugin/lib/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { appendQueue } from "../../server/file-lock.mjs";
|
|
2
1
|
// dsh-lm-capture v0.1 — cordis 纯函数插件,零依赖。
|
|
3
2
|
// 监听 workspace/session-stop 与 session/disposed,把会话里「真实用户输入」
|
|
4
3
|
// (source.kind === 'user',排除 runtime-context 等注入)提炼成一条 episode,
|
|
@@ -6,9 +5,13 @@ import { appendQueue } from "../../server/file-lock.mjs";
|
|
|
6
5
|
// 纪律:任何异常都不得抛出宿主进程;只写 stderr;幂等(同一 session 只采一次)。
|
|
7
6
|
import fs from "node:fs";
|
|
8
7
|
import { boundedIndex } from "../../server/recall-utils.mjs";
|
|
8
|
+
import { createTaskRecallHook } from "./task-recall.mjs";
|
|
9
|
+
import { createProjectRouter, registerScopedTools } from './project-tools.mjs';
|
|
10
|
+
import { createProjectBackends } from './project-backends.mjs';
|
|
11
|
+
import { confirmedOutcomes, isOutcomeConfirmation } from './confirmed-outcomes.mjs';
|
|
9
12
|
import { filterTextsForLLM } from "../../server/guards.mjs"; // v0.15 外发前脱敏/剔除(与 server/helper 同源)
|
|
10
13
|
import { createHash } from "node:crypto";
|
|
11
|
-
import {
|
|
14
|
+
import { execFileSync } from "node:child_process";
|
|
12
15
|
import { randomUUID } from "node:crypto";
|
|
13
16
|
import os from "node:os";
|
|
14
17
|
import path from "node:path";
|
|
@@ -17,44 +20,32 @@ import { fileURLToPath } from "node:url";
|
|
|
17
20
|
const libDir = () => path.dirname(fileURLToPath(import.meta.url));
|
|
18
21
|
|
|
19
22
|
let MEM_DIR = process.env.LM_MEMORY_DIR || path.join(os.homedir(), ".lm-memory");
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
let MARKER = path.join(MEM_DIR, "captured-sessions.jsonl");
|
|
24
|
-
const captureIndex = new Map(); // sessionId -> 文本指纹 hash(null = 旧版无指纹记录)
|
|
25
|
-
let captureIndexLoaded = false;
|
|
26
|
-
function resolvePaths() {
|
|
27
|
-
PENDING = path.join(MEM_DIR, "pending-episodes.jsonl");
|
|
28
|
-
MARKER = path.join(MEM_DIR, "captured-sessions.jsonl");
|
|
29
|
-
captureIndex.clear();
|
|
30
|
-
captureIndexLoaded = false; // 目录切换后从新的 captured-sessions.jsonl 重建索引
|
|
23
|
+
let legacyCapture;
|
|
24
|
+
function captureStore(dir) {
|
|
25
|
+
return { dir, pending: path.join(dir, 'pending-episodes.jsonl'), marker: path.join(dir, 'captured-sessions.jsonl'), index: new Map(), confirmed: new Set(), loaded: false };
|
|
31
26
|
}
|
|
32
27
|
|
|
33
28
|
const log = (msg) => { try { console.error(`[lm-capture] ${msg}`); } catch {} };
|
|
34
29
|
|
|
35
30
|
const textHash = (texts) => createHash("sha256").update(texts.join("\u0000")).digest("hex").slice(0, 16);
|
|
36
|
-
function loadCaptureIndex() {
|
|
31
|
+
function loadCaptureIndex(store) {
|
|
37
32
|
try {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
try { const r = JSON.parse(line); if (r?.sessionId) captureIndex.set(r.sessionId, r.hash ?? null); } catch {}
|
|
42
|
-
}
|
|
33
|
+
for (const line of fs.readFileSync(store.marker, 'utf8').split('\n')) {
|
|
34
|
+
if (!line.trim()) continue;
|
|
35
|
+
try { const r = JSON.parse(line); if (r?.sessionId) store.index.set(r.sessionId, r.hash ?? null); } catch {}
|
|
43
36
|
}
|
|
44
37
|
} catch {}
|
|
45
|
-
|
|
38
|
+
store.loaded = true;
|
|
46
39
|
}
|
|
47
40
|
// v0.15:会话指纹幂等(外审实锤「同 session 二次 stop 的后续纠正不被采集」)。
|
|
48
41
|
// 同指纹 = 已采集;指纹变化(会话有新内容)= 允许重新采集,库侧由统一提交入口幂等去重。
|
|
49
|
-
function alreadyCaptured(sessionId, hash) {
|
|
50
|
-
if (!
|
|
51
|
-
|
|
52
|
-
const h = captureIndex.get(sessionId);
|
|
53
|
-
return h === null || h === hash; // 旧版记录(无指纹)保守视为已采集
|
|
42
|
+
function alreadyCaptured(store, sessionId, hash) {
|
|
43
|
+
if (!store.loaded) loadCaptureIndex(store);
|
|
44
|
+
return store.index.get(sessionId) === null || store.index.get(sessionId) === hash;
|
|
54
45
|
}
|
|
55
|
-
function markCaptured(sessionId, hash) {
|
|
56
|
-
|
|
57
|
-
try { fs.appendFileSync(
|
|
46
|
+
function markCaptured(store, sessionId, hash) {
|
|
47
|
+
store.index.set(sessionId, hash);
|
|
48
|
+
try { fs.appendFileSync(store.marker, JSON.stringify({ ts: new Date().toISOString(), sessionId, hash }) + '\n'); } catch {}
|
|
58
49
|
}
|
|
59
50
|
|
|
60
51
|
function extractSessionModel(session) {
|
|
@@ -67,7 +58,7 @@ function extractSessionModel(session) {
|
|
|
67
58
|
return last && last.provider && last.model ? { provider: last.provider, model: last.model } : null;
|
|
68
59
|
} catch { return null; }
|
|
69
60
|
}
|
|
70
|
-
function extractUserTexts(session) {
|
|
61
|
+
function extractUserTexts(session, localConfirmations = false) {
|
|
71
62
|
let events;
|
|
72
63
|
try { events = session.ownEvents(); } catch (e) { log(`ownEvents failed: ${e.message}`); return null; }
|
|
73
64
|
const texts = [];
|
|
@@ -75,8 +66,8 @@ function extractUserTexts(session) {
|
|
|
75
66
|
if (e?.type !== "user/message") continue;
|
|
76
67
|
if (e?.data?.source?.kind !== "user") continue; // 排除 runtime-context / time-context 等注入
|
|
77
68
|
const text = (e.data.content || []).filter((c) => c?.type === "text").map((c) => c.text).join(" ").trim();
|
|
78
|
-
if (text.length >= 2) {
|
|
79
|
-
const stamp = e.timestamp ?? e.ts ?? e.data?.timestamp;
|
|
69
|
+
if (text.length >= 2 && !(localConfirmations && isOutcomeConfirmation(text))) {
|
|
70
|
+
const stamp = e.time ?? e.timestamp ?? e.ts ?? e.data?.timestamp;
|
|
80
71
|
const d = stamp == null ? null : new Date(stamp);
|
|
81
72
|
const date = d && !isNaN(d) ? d.toLocaleDateString("sv") : "unknown";
|
|
82
73
|
texts.push(`[message_date=${date}] ${text}`);
|
|
@@ -85,88 +76,150 @@ function extractUserTexts(session) {
|
|
|
85
76
|
return texts;
|
|
86
77
|
}
|
|
87
78
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
for (const c of candidates) { try { if (fs.existsSync(c)) return c; } catch {} }
|
|
99
|
-
return "node";
|
|
79
|
+
import { resolveNode, spawnViewer, viewerArgs, viewerPort } from "./startup.mjs";
|
|
80
|
+
|
|
81
|
+
// A Host without node:sqlite can still load the plugin and start a compatible external
|
|
82
|
+
// server. Keep queue writes fail-closed when the Host itself cannot acquire the lock.
|
|
83
|
+
let appendQueue;
|
|
84
|
+
try {
|
|
85
|
+
({ appendQueue } = await import("../../server/file-lock.mjs"));
|
|
86
|
+
} catch (error) {
|
|
87
|
+
log(`queue capture unavailable in Host: ${error?.message ?? error}`);
|
|
88
|
+
appendQueue = () => { throw error; };
|
|
100
89
|
}
|
|
101
90
|
|
|
102
91
|
function writeServerShim(config) {
|
|
92
|
+
let tempPath;
|
|
103
93
|
try {
|
|
104
94
|
const realServer = ["../../server/memory-server.mjs", "../../../server/memory-server.mjs"]
|
|
105
95
|
.map((r) => path.join(libDir(), r)).find((p) => fs.existsSync(p));
|
|
106
|
-
if (!realServer) return;
|
|
107
|
-
const binDir = path.
|
|
96
|
+
if (!realServer) { log("server shim skipped: memory-server.mjs not found"); return null; }
|
|
97
|
+
const binDir = path.resolve(MEM_DIR, "bin");
|
|
108
98
|
fs.mkdirSync(binDir, { recursive: true });
|
|
109
99
|
const shimPath = path.join(binDir, "lm-memory-server.mjs");
|
|
110
100
|
const content = [
|
|
111
101
|
"#!/usr/bin/env node",
|
|
112
102
|
"// Auto-generated by dsh-lm-memory plugin — rewritten at every host start. Do not edit.",
|
|
113
|
-
"//
|
|
114
|
-
"// 真实服务器路径由插件按自身安装位置写入,规避缺 DSH_PROFILE_DIR/DSH_PROFILE",
|
|
115
|
-
"// 环境变量时回退 profiles/default 的 spawn ENOENT → MCP 永久断连问题。",
|
|
103
|
+
"// MCP uses this stable launcher; the absolute server path comes from the installed plugin.",
|
|
116
104
|
'import { pathToFileURL } from "node:url";',
|
|
117
105
|
// v0.15.1 系统级禁外发:allowRemote:false 经 shim 传给 server(冲突复核/孤儿回收遵守)
|
|
118
106
|
...(config?.allowRemote === false ? ['process.env.LM_REMOTE_DISABLED = "1"; // 插件 allowRemote:false → 系统级禁外发'] : []),
|
|
119
107
|
`await import(pathToFileURL(${JSON.stringify(realServer)}).href);`,
|
|
120
108
|
"",
|
|
121
109
|
].join("\n");
|
|
122
|
-
|
|
110
|
+
tempPath = shimPath + "." + randomUUID() + ".tmp";
|
|
111
|
+
fs.writeFileSync(tempPath, content, { flag: "wx" });
|
|
112
|
+
fs.renameSync(tempPath, shimPath);
|
|
113
|
+
tempPath = null;
|
|
123
114
|
log("server shim written: " + shimPath);
|
|
124
|
-
|
|
115
|
+
return shimPath;
|
|
116
|
+
} catch (e) {
|
|
117
|
+
log("shim write error: " + (e?.message ?? e));
|
|
118
|
+
return null;
|
|
119
|
+
} finally {
|
|
120
|
+
if (tempPath) { try { fs.rmSync(tempPath, { force: true }); } catch {} }
|
|
121
|
+
}
|
|
125
122
|
}
|
|
126
123
|
|
|
127
124
|
// ===== viewer 旧版自动升级(v0.10)=====
|
|
128
125
|
// 病灶:viewer 是 detached 进程,DSH app 重启杀不掉它;旧 viewer 占着端口且它 spawn 的
|
|
129
126
|
// MCP server 子进程也是旧代码(无 announce)→ 管理页写操作永远不进变更热流。
|
|
130
|
-
//
|
|
131
|
-
//
|
|
127
|
+
// 探测 /api/version 中的版本和启动配置标识;只替换 OS 确认由此安装启动的进程。
|
|
128
|
+
// 无法确认归属的 listener 保持原样,不把其地址发给设置面板。
|
|
132
129
|
const execFileSyncSafe = (cmd, args) => { try { return execFileSync(cmd, args, { encoding: "utf8", timeout: 3000 }).trim(); } catch { return ""; } };
|
|
133
130
|
function pkgVersion() {
|
|
134
131
|
try { return JSON.parse(fs.readFileSync(path.join(libDir(), "../../package.json"), "utf8")).version || "0.0.0"; } catch { return "0.0.0"; }
|
|
135
132
|
}
|
|
136
|
-
function
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
133
|
+
function canReplaceVersion(installed, wanted) {
|
|
134
|
+
if (installed === wanted) return true;
|
|
135
|
+
const [a, preA] = installed.split("-", 2), [b, preB] = wanted.split("-", 2);
|
|
136
|
+
const pa = a.split(".").map(Number), pb = b.split(".").map(Number);
|
|
137
|
+
for (let i = 0; i < 3; i++) { if (pa[i] !== pb[i]) return pa[i] < pb[i]; }
|
|
138
|
+
// Do not guess the ordering of distinct prerelease labels.
|
|
139
|
+
return Boolean(preA && !preB);
|
|
140
140
|
}
|
|
141
|
-
async function
|
|
142
|
-
const url = "http://127.0.0.1:" + port;
|
|
143
|
-
let remote = null;
|
|
141
|
+
async function probeViewer(port) {
|
|
144
142
|
try {
|
|
145
|
-
const r = await fetch(
|
|
146
|
-
if (r.ok)
|
|
143
|
+
const r = await fetch(`http://127.0.0.1:${port}/api/version`, { signal: AbortSignal.timeout(1500), redirect: "error" });
|
|
144
|
+
if (r.ok) {
|
|
145
|
+
const remote = await r.json();
|
|
146
|
+
if (typeof remote?.version === "string" && /^\d+\.\d+\.\d+(?:-[\w.-]+)?$/.test(remote.version)) return remote;
|
|
147
|
+
}
|
|
147
148
|
} catch {}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
function matchesViewer(remote, want) {
|
|
152
|
+
return remote?.version === want.version && remote.startupKey === want.startupKey;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async function ensureFreshViewer(port, node, viewerPath, want) {
|
|
156
|
+
const remote = await probeViewer(port);
|
|
157
|
+
if (matchesViewer(remote, want)) return false;
|
|
158
|
+
const listener = () => execFileSyncSafe("lsof", ["-a", "-ti4TCP:" + port, "-sTCP:LISTEN"])
|
|
159
|
+
.split("\n").map(Number).filter((n) => Number.isSafeInteger(n) && n > 1);
|
|
160
|
+
const listeners = listener();
|
|
161
|
+
if (!remote && !listeners.length) return true;
|
|
162
|
+
const [pid] = listeners;
|
|
163
|
+
// The loopback response is not proof of ownership. Require the real listener and
|
|
164
|
+
// exactly our executable, installed script, port, and managed-launch marker.
|
|
165
|
+
const command = [node, ...viewerArgs(viewerPath, port)].join(" ");
|
|
166
|
+
if (listeners.length === 1 && remote?.pid === pid && typeof remote.startupKey === "string"
|
|
167
|
+
&& canReplaceVersion(remote.version, want.version)
|
|
168
|
+
&& execFileSyncSafe("ps", ["-ww", "-p", String(pid), "-o", "command="]) === command) {
|
|
169
|
+
try { process.kill(pid, "SIGTERM"); log(`stale managed viewer (pid ${pid}) terminated`); } catch {}
|
|
170
|
+
await new Promise((r) => setTimeout(r, 500));
|
|
171
|
+
if (!listener().length && !(await probeViewer(port))) return true;
|
|
155
172
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
173
|
+
log(`viewer startup blocked on ${port}: existing listener is unverified, newer, or could not be stopped`);
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function initializeStartup(ctx, config) {
|
|
178
|
+
const port = viewerPort(config?.viewer);
|
|
179
|
+
if (config?.viewer && !port) log("viewer disabled: expected an integer port from 1 to 65535");
|
|
180
|
+
const memoryDir = path.resolve(MEM_DIR);
|
|
181
|
+
const version = pkgVersion();
|
|
182
|
+
const remoteDisabled = config?.allowRemote === false || process.env.LM_REMOTE_DISABLED === "1";
|
|
183
|
+
const want = { version, startupKey: createHash("sha256").update(JSON.stringify([version, memoryDir, remoteDisabled])).digest("hex") };
|
|
184
|
+
try {
|
|
185
|
+
// DSH 0.2.2: this exact route uses the authenticated owner-only /api bridge,
|
|
186
|
+
// including Desktop's dsh-app://app carrier. No memory or directory is proxied.
|
|
187
|
+
ctx.inject?.(["connection"], (host) => host.connection.fetch?.register({
|
|
188
|
+
path: "/api/lm-memory-viewer",
|
|
189
|
+
methods: ["GET"],
|
|
190
|
+
requestBody: "buffered",
|
|
191
|
+
fetch: async () => {
|
|
192
|
+
const remote = port ? await probeViewer(port) : null;
|
|
193
|
+
const status = !port ? "disabled" : !remote ? "offline" : matchesViewer(remote, want) ? "ready" : "stale";
|
|
194
|
+
return Response.json({ port, status }, { headers: { "Cache-Control": "no-store" } });
|
|
195
|
+
},
|
|
196
|
+
}));
|
|
197
|
+
} catch (error) { log(`viewer address bridge unavailable: ${error?.message ?? error}`); }
|
|
198
|
+
const shimPath = writeServerShim(config);
|
|
199
|
+
const node = resolveNode({ log });
|
|
200
|
+
if (!node) return;
|
|
201
|
+
if (shimPath) {
|
|
202
|
+
// The DSH MCP row waits for this service, including when a shim cannot be written.
|
|
203
|
+
try { ctx.provide?.("lmMemoryStartup", { node, shimPath, memoryDir }); }
|
|
204
|
+
catch (error) { log(`MCP startup registration error: ${error?.message ?? error}`); }
|
|
162
205
|
}
|
|
206
|
+
if (!port) return;
|
|
207
|
+
const viewerPath = ["../../server/viewer.mjs", "../../../server/viewer.mjs"]
|
|
208
|
+
.map((r) => path.join(libDir(), r)).find((p) => fs.existsSync(p));
|
|
209
|
+
if (!viewerPath) { log("viewer skipped: viewer.mjs not found"); return; }
|
|
210
|
+
void ensureFreshViewer(port, node, viewerPath, want).then((shouldStart) => {
|
|
211
|
+
if (shouldStart) spawnViewer(node, viewerPath, port, {
|
|
212
|
+
...process.env, LM_MEMORY_DIR: memoryDir, LM_VIEWER_STARTUP_KEY: want.startupKey,
|
|
213
|
+
...(remoteDisabled ? { LM_REMOTE_DISABLED: "1" } : {}),
|
|
214
|
+
}, log);
|
|
215
|
+
}).catch((error) => log(`viewer ensure error: ${error?.message ?? error}`));
|
|
163
216
|
}
|
|
164
217
|
|
|
165
|
-
function dispatchExtraction(session, texts, config) {
|
|
218
|
+
function dispatchExtraction(session, texts, config, dir) {
|
|
166
219
|
try {
|
|
167
220
|
// v0.15 allowRemote 开关:false 时完全不外呼 LLM(本地模式,只保留会话索引卡),默认 true
|
|
168
221
|
if (config?.extract === false || config?.allowRemote === false || texts.length < 2) return true;
|
|
169
|
-
const jobsDir = path.join(
|
|
222
|
+
const jobsDir = path.join(dir, 'extraction-jobs');
|
|
170
223
|
fs.mkdirSync(jobsDir, { recursive: true });
|
|
171
224
|
// v0.16 文件名带 rev(第七轮外审实锤竞态:文件名固定为 sessionId 时,续聊的新 dispatch 覆盖同一路径,
|
|
172
225
|
// 旧 worker(仍在跑几十秒 LLM)完成时 rmSync 删掉的是【新任务】)——rev 入名 = 每次派发独立文件,互不覆盖。
|
|
@@ -185,7 +238,7 @@ function dispatchExtraction(session, texts, config) {
|
|
|
185
238
|
})); // 写 job 文件(rev 入名,见上)
|
|
186
239
|
// v0.16.2 任务协议(第九轮复核实锤:插件直接 spawn 绕过登记/领取,双 worker 出现在首次执行):
|
|
187
240
|
// 插件只写「待派发通知」,server 消费通知后执行「登记→原子领取→spawn」,与恢复器完全同一入口。
|
|
188
|
-
appendQueue(path.join(
|
|
241
|
+
appendQueue(path.join(dir, 'pending-jobs.jsonl'), { sessionId: session.id, rev, jobFile: path.basename(jobFile) });
|
|
189
242
|
log(`extraction dispatched: ${session.id} (${texts.length} msgs)`);
|
|
190
243
|
return true;
|
|
191
244
|
} catch (e) {
|
|
@@ -194,9 +247,19 @@ function dispatchExtraction(session, texts, config) {
|
|
|
194
247
|
}
|
|
195
248
|
}
|
|
196
249
|
|
|
197
|
-
function capture(session, config) {
|
|
250
|
+
function capture(session, config, store = legacyCapture) {
|
|
251
|
+
if (!store) return;
|
|
198
252
|
try {
|
|
199
|
-
|
|
253
|
+
if (config?.projectMemory === true && config.confirmedOutcomes === true) {
|
|
254
|
+
for (const outcome of confirmedOutcomes(session)) {
|
|
255
|
+
// Stable event identity is deduped by the existing transactional consumer.
|
|
256
|
+
if (store.confirmed.has(outcome.eid)) continue;
|
|
257
|
+
appendQueue(path.join(store.dir, 'pending-memories.jsonl'), outcome);
|
|
258
|
+
if (store.confirmed.size >= 2048) store.confirmed.clear();
|
|
259
|
+
store.confirmed.add(outcome.eid);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const rawTexts = extractUserTexts(session, config?.projectMemory === true && config.confirmedOutcomes === true);
|
|
200
263
|
if (!rawTexts || !session?.id) return;
|
|
201
264
|
// v0.15 外发前防线:脱敏/剔除发生在任何落盘(会话卡首末句、提炼 job)之前——
|
|
202
265
|
// 修复外审实锤「原始文本先写 job/pending,SECRET 检查来不及」。
|
|
@@ -204,7 +267,7 @@ function capture(session, config) {
|
|
|
204
267
|
if (dropped || redacted) log(`filtered before persist: ${dropped} dropped, ${redacted} redacted`);
|
|
205
268
|
if (!texts.length) return;
|
|
206
269
|
const hash = textHash(texts);
|
|
207
|
-
if (alreadyCaptured(session.id, hash)) return;
|
|
270
|
+
if (alreadyCaptured(store, session.id, hash)) return;
|
|
208
271
|
const min = Number.isFinite(config?.minUserMessages) ? config.minUserMessages : 2;
|
|
209
272
|
if (texts.length < min) return;
|
|
210
273
|
const cwd = session.header?.cwd ?? session.meta?.cwd ?? "";
|
|
@@ -219,9 +282,9 @@ function capture(session, config) {
|
|
|
219
282
|
eventDate: date,
|
|
220
283
|
capturedAt: new Date().toISOString(),
|
|
221
284
|
};
|
|
222
|
-
fs.mkdirSync(
|
|
223
|
-
appendQueue(
|
|
224
|
-
if (dispatchExtraction(session, texts, config)) markCaptured(session.id, hash);
|
|
285
|
+
fs.mkdirSync(store.dir, { recursive: true });
|
|
286
|
+
appendQueue(store.pending, rec);
|
|
287
|
+
if (dispatchExtraction(session, texts, config, store.dir)) markCaptured(store, session.id, hash);
|
|
225
288
|
log(`captured ${session.id} (${texts.length} user msgs)`);
|
|
226
289
|
} catch (e) {
|
|
227
290
|
log(`capture error: ${e?.message ?? e}`);
|
|
@@ -235,15 +298,13 @@ function capture(session, config) {
|
|
|
235
298
|
// 注入一条 user 消息(source.kind=lm-memory-refresh,同 time-context 形状),
|
|
236
299
|
// 模型在下一步立刻看到——持续记忆流 + 场景触发(前瞻记忆)的最小实现。
|
|
237
300
|
// 纪律:异常静默不抛宿主;per-session 字节 offset 幂等;无变更零成本(一次 stat)。
|
|
238
|
-
const
|
|
239
|
-
const
|
|
240
|
-
const seenEventIds = new Map(); // sessionId -> Set<eid>(v0.15.2:至少一次投递下跨 offset 重发的同一事件只应用一次)
|
|
301
|
+
const refreshOffsets = new Map(); // legacy sessionId -> consumed offset
|
|
302
|
+
const seenEventIds = new Map();
|
|
241
303
|
const INJECT_OPS = new Set(["remember", "correct", "forget", "pin", "unpin", "wake"]);
|
|
242
304
|
const OP_LABEL = { remember: "新增", correct: "修正", forget: "删除", pin: "置顶", unpin: "取消置顶", wake: "唤醒" };
|
|
243
305
|
const KIND_LABEL = { preference: "偏好", fact: "事实", procedure: "经验", todo: "待办", episode: "会话" };
|
|
244
306
|
|
|
245
|
-
function readNewChanges(offset) {
|
|
246
|
-
const file = changesPath();
|
|
307
|
+
function readNewChanges(file, offset) {
|
|
247
308
|
let st;
|
|
248
309
|
try { st = fs.statSync(file); } catch { return { changes: [], newOffset: offset }; }
|
|
249
310
|
let from = offset;
|
|
@@ -283,7 +344,7 @@ function buildDeltaText(changes) {
|
|
|
283
344
|
return boundedIndex(out, 8192, 40);
|
|
284
345
|
}
|
|
285
346
|
|
|
286
|
-
function refreshHook(config) {
|
|
347
|
+
function refreshHook(config, directory = () => MEM_DIR, offsets = refreshOffsets, seenIds = seenEventIds) {
|
|
287
348
|
return async (payload, next) => {
|
|
288
349
|
const decision = await next();
|
|
289
350
|
try {
|
|
@@ -291,16 +352,19 @@ function refreshHook(config) {
|
|
|
291
352
|
if (config?.refresh === false) return decision;
|
|
292
353
|
const sid = payload?.agent?.session?.id ?? payload?.agent?.id;
|
|
293
354
|
if (!sid) return decision;
|
|
355
|
+
const dir = directory(payload);
|
|
356
|
+
if (!dir) return decision;
|
|
357
|
+
const file = path.join(dir, 'changes.jsonl');
|
|
294
358
|
let st;
|
|
295
|
-
try { st = fs.statSync(
|
|
296
|
-
let offset =
|
|
297
|
-
if (offset === undefined) {
|
|
298
|
-
const { changes, newOffset } = readNewChanges(offset);
|
|
299
|
-
|
|
359
|
+
try { st = fs.statSync(file); } catch { return decision; } // 无热流文件=尚无变更
|
|
360
|
+
let offset = offsets.get(sid);
|
|
361
|
+
if (offset === undefined) { offsets.set(sid, st.size); return decision; }
|
|
362
|
+
const { changes, newOffset } = readNewChanges(file, offset);
|
|
363
|
+
offsets.set(sid, newOffset);
|
|
300
364
|
// v0.15.3:稳定事件身份去重——单次遍历内「检查即登记」,同批重复 eid 与跨 offset 重发都只应用一次
|
|
301
365
|
// (复核实锤:先过滤整批再统一登记,批内两条相同 eid 会漏网注入两次)
|
|
302
|
-
let seen =
|
|
303
|
-
if (!seen) { seen = new Set();
|
|
366
|
+
let seen = seenIds.get(sid);
|
|
367
|
+
if (!seen) { seen = new Set(); seenIds.set(sid, seen); }
|
|
304
368
|
const fresh = [];
|
|
305
369
|
for (const c of changes) {
|
|
306
370
|
if (!c.eid || !seen.has(c.eid)) { fresh.push(c); if (c.eid) seen.add(c.eid); }
|
|
@@ -321,31 +385,83 @@ function refreshHook(config) {
|
|
|
321
385
|
};
|
|
322
386
|
}
|
|
323
387
|
|
|
388
|
+
function registerProjectLifecycle(ctx, router, config, signal) {
|
|
389
|
+
const active = new WeakMap();
|
|
390
|
+
const sessions = new Map();
|
|
391
|
+
const stores = new Map();
|
|
392
|
+
const offsets = new Map(), seenIds = new Map();
|
|
393
|
+
const refresh = refreshHook(config, payload => active.get(payload?.agent?.session)?.dir, offsets, seenIds);
|
|
394
|
+
const automatic = createTaskRecallHook(config, session => !signal.aborted && active.get(session)?.dir);
|
|
395
|
+
const finish = session => {
|
|
396
|
+
const state = active.get(session);
|
|
397
|
+
if (!signal.aborted && state && config.capture !== false) capture(session, config, state.store);
|
|
398
|
+
};
|
|
399
|
+
ctx.on('agent/pre-step', async (payload, next) => {
|
|
400
|
+
const session = payload?.agent?.session;
|
|
401
|
+
if (!signal.aborted && !payload?.signal?.aborted && session && !active.has(session)) {
|
|
402
|
+
const dir = await router.bind(session);
|
|
403
|
+
if (dir && !signal.aborted && !payload?.signal?.aborted) {
|
|
404
|
+
let store = stores.get(dir);
|
|
405
|
+
if (!store) { store = captureStore(dir); stores.set(dir, store); }
|
|
406
|
+
active.set(session, { dir, store });
|
|
407
|
+
sessions.set(session.id, session);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return refresh(payload, automatic ? () => automatic(payload, next) : next);
|
|
411
|
+
});
|
|
412
|
+
ctx.on('workspace/session-stop', ({ sessionId } = {}) => { const session = sessions.get(sessionId); if (session) finish(session); });
|
|
413
|
+
ctx.on('session/disposed', session => {
|
|
414
|
+
finish(session);
|
|
415
|
+
if (sessions.get(session?.id) === session) sessions.delete(session.id);
|
|
416
|
+
offsets.delete(session?.id); seenIds.delete(session?.id);
|
|
417
|
+
active.delete(session);
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function startProjectTools(ctx, config, signal) {
|
|
422
|
+
signal.throwIfAborted();
|
|
423
|
+
const base = path.resolve(config.memoryDir || process.env.LM_MEMORY_DIR || path.join(os.homedir(), '.lm-memory'));
|
|
424
|
+
const command = resolveNode({ log });
|
|
425
|
+
if (!command) return;
|
|
426
|
+
fs.mkdirSync(base, { recursive: true, mode: 0o700 });
|
|
427
|
+
const router = createProjectRouter(base);
|
|
428
|
+
const allowRemote = config.allowRemote === true && process.env.LM_REMOTE_DISABLED !== '1';
|
|
429
|
+
const keyEnv = config.llm?.apiKeyEnv;
|
|
430
|
+
const forwarded = ['HOME', 'USERPROFILE', 'PATH', 'SystemRoot'];
|
|
431
|
+
if (allowRemote && typeof keyEnv === 'string' && /^[A-Za-z_][A-Za-z0-9_]*$/.test(keyEnv)) forwarded.push(keyEnv);
|
|
432
|
+
const env = Object.fromEntries(forwarded.filter(key => typeof process.env[key] === 'string').map(key => [key, process.env[key]]));
|
|
433
|
+
const backends = createProjectBackends({ command, server: path.join(libDir(), '../../server/memory-server.mjs'),
|
|
434
|
+
env: { ...env, LM_EMBED: '0', ...(allowRemote ? {} : { LM_REMOTE_DISABLED: '1' }) } });
|
|
435
|
+
const close = () => { void backends.close(); };
|
|
436
|
+
signal.addEventListener('abort', close, { once: true });
|
|
437
|
+
try {
|
|
438
|
+
const unregister = registerScopedTools(ctx, router, backends, signal);
|
|
439
|
+
if (signal.aborted) { unregister(); return; }
|
|
440
|
+
signal.addEventListener('abort', unregister, { once: true });
|
|
441
|
+
registerProjectLifecycle(ctx, router, { ...config, allowRemote }, signal);
|
|
442
|
+
log('scoped memory tools loaded');
|
|
443
|
+
} catch (error) {
|
|
444
|
+
close();
|
|
445
|
+
throw error;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
324
449
|
export default function apply(ctx, config) {
|
|
325
450
|
try {
|
|
326
|
-
if (config?.
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
const r = await fetch("http://127.0.0.1:" + port + "/api/version", { signal: AbortSignal.timeout(1500) });
|
|
336
|
-
alive = r.ok; // 只有新版 viewer 才有此端点
|
|
337
|
-
} catch {}
|
|
338
|
-
if (alive) { log("viewer already fresh on " + port); return; }
|
|
339
|
-
// 包形态:plugin/lib → ../../server;旧手工部署:plugin/dsh-lm-capture/lib → ../../../server
|
|
340
|
-
const viewerPath = ["../../server/viewer.mjs", "../../../server/viewer.mjs"]
|
|
341
|
-
.map((r) => path.join(libDir(), r)).find((p) => fs.existsSync(p));
|
|
342
|
-
if (viewerPath) {
|
|
343
|
-
const ch = spawn(resolveNode(), [viewerPath, String(port)], { detached: true, stdio: "ignore", env: { ...process.env, LM_MEMORY_DIR: MEM_DIR, ...(config?.allowRemote === false ? { LM_REMOTE_DISABLED: "1" } : {}) } });
|
|
344
|
-
ch.unref();
|
|
345
|
-
log("viewer spawned on " + port);
|
|
346
|
-
}
|
|
347
|
-
}).catch((e) => log("viewer ensure error: " + (e?.message ?? e)));
|
|
451
|
+
if (config?.projectMemory === true) {
|
|
452
|
+
// No legacy startup service, capture, global deltas or Viewer in scoped mode.
|
|
453
|
+
ctx.inject(['tools'], scoped => scoped.effect(() => {
|
|
454
|
+
const lifetime = new AbortController();
|
|
455
|
+
try { startProjectTools(scoped, config, lifetime.signal); }
|
|
456
|
+
catch (error) { log(`scoped memory startup failed: ${error?.message ?? error}`); }
|
|
457
|
+
return () => lifetime.abort();
|
|
458
|
+
}));
|
|
459
|
+
return;
|
|
348
460
|
}
|
|
461
|
+
if (config?.memoryDir) MEM_DIR = String(config.memoryDir);
|
|
462
|
+
if (!legacyCapture || legacyCapture.dir !== MEM_DIR) legacyCapture = captureStore(MEM_DIR);
|
|
463
|
+
try { initializeStartup(ctx, config); }
|
|
464
|
+
catch (error) { log(`startup error: ${error?.message ?? error}`); }
|
|
349
465
|
// 悬浮按钮已移除:设置 → 记忆库 是正式入口(用户反馈悬浮球多余)
|
|
350
466
|
const sessions = new Map();
|
|
351
467
|
const byId = (sessionId) => {
|
|
@@ -372,6 +488,8 @@ export default function apply(ctx, config) {
|
|
|
372
488
|
});
|
|
373
489
|
// lm-refresh:每个 step 前检查记忆变更热流,有新记忆就注入 delta(config.refresh=false 关闭)
|
|
374
490
|
ctx.on("agent/pre-step", refreshHook(config));
|
|
491
|
+
const taskRecallHook = createTaskRecallHook(config, MEM_DIR);
|
|
492
|
+
if (taskRecallHook) ctx.on("agent/pre-step", taskRecallHook);
|
|
375
493
|
// headless/一次性进程:进程退出时同步落盘(桌面常驻进程下由上面的实时事件覆盖)
|
|
376
494
|
if (!globalThis.__lmCaptureExitHooked) {
|
|
377
495
|
globalThis.__lmCaptureExitHooked = true;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
2
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
// Only the Host router chooses the directory. No tool parameter can select a backend.
|
|
6
|
+
export function createProjectBackends({ command, server, env, maxOpen = 8, idleMs = 30_000 }) {
|
|
7
|
+
const entries = new Map();
|
|
8
|
+
let closed = false;
|
|
9
|
+
|
|
10
|
+
function remove(dir, entry) {
|
|
11
|
+
if (entries.get(dir) !== entry) return Promise.resolve();
|
|
12
|
+
entries.delete(dir);
|
|
13
|
+
clearTimeout(entry.timer);
|
|
14
|
+
return entry.client.close().catch(() => {});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async function use(dir, action, signal) {
|
|
18
|
+
if (!path.isAbsolute(dir)) throw Error('Memory storage is not bound');
|
|
19
|
+
signal?.throwIfAborted();
|
|
20
|
+
if (closed) throw Error('Memory backends closed');
|
|
21
|
+
let entry = entries.get(dir);
|
|
22
|
+
if (!entry) {
|
|
23
|
+
if (entries.size >= maxOpen) {
|
|
24
|
+
const idle = [...entries].find(([, value]) => value.users === 0);
|
|
25
|
+
if (!idle) throw Error('Memory backends busy; retry');
|
|
26
|
+
await remove(...idle);
|
|
27
|
+
return use(dir, action, signal);
|
|
28
|
+
}
|
|
29
|
+
const client = new Client({ name: 'lm-memory-project-router', version: '1' });
|
|
30
|
+
const transport = new StdioClientTransport({ command, args: [server], env: { ...env, LM_MEMORY_DIR: dir }, stderr: 'ignore' });
|
|
31
|
+
entry = { client, users: 0, connected: client.connect(transport, { timeout: 8000 }) };
|
|
32
|
+
entries.set(dir, entry);
|
|
33
|
+
const current = entry;
|
|
34
|
+
client.onclose = () => { void remove(dir, current); };
|
|
35
|
+
}
|
|
36
|
+
clearTimeout(entry.timer);
|
|
37
|
+
entry.users++;
|
|
38
|
+
try {
|
|
39
|
+
try { await entry.connected; }
|
|
40
|
+
catch (error) { await remove(dir, entry); throw error; }
|
|
41
|
+
signal?.throwIfAborted();
|
|
42
|
+
return await action(entry.client);
|
|
43
|
+
} finally {
|
|
44
|
+
if (--entry.users === 0 && entries.get(dir) === entry) {
|
|
45
|
+
entry.timer = setTimeout(() => { void remove(dir, entry); }, idleMs);
|
|
46
|
+
entry.timer.unref();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
list: (dir, signal) => use(dir, client => client.listTools(undefined, { signal, timeout: 8000 }), signal),
|
|
53
|
+
call: (dir, name, args, signal) => use(dir, client => client.callTool({ name, arguments: args }, undefined, { signal, timeout: 60_000 }), signal),
|
|
54
|
+
close: async () => { closed = true; await Promise.all([...entries].map(([dir, entry]) => remove(dir, entry))); },
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
import { realpath, stat } from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { promisify } from 'node:util';
|
|
6
|
+
|
|
7
|
+
const git = promisify(execFile);
|
|
8
|
+
|
|
9
|
+
// Resolve the physical checkout root, not a repo name, remote URL or path
|
|
10
|
+
// mentioned by the model. Worktrees and separate clones remain separate.
|
|
11
|
+
export async function resolveProjectIdentity(cwd, { signal } = {}) {
|
|
12
|
+
if (typeof cwd !== 'string' || !path.isAbsolute(cwd) || signal?.aborted) return null;
|
|
13
|
+
try {
|
|
14
|
+
const physical = await realpath(cwd);
|
|
15
|
+
if (!(await stat(physical)).isDirectory()) return null;
|
|
16
|
+
// Ambient Git overrides must not redirect project memory to another checkout.
|
|
17
|
+
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !/^GIT_/i.test(key)));
|
|
18
|
+
const { stdout } = await git('git', ['rev-parse', '--show-toplevel'], {
|
|
19
|
+
cwd: physical, env, signal, timeout: 1200, maxBuffer: 8192, windowsHide: true, encoding: 'utf8',
|
|
20
|
+
});
|
|
21
|
+
const rawRoot = stdout.replace(/\r?\n$/, '');
|
|
22
|
+
if (!path.isAbsolute(rawRoot) || /[\r\n]/.test(rawRoot)) return null;
|
|
23
|
+
const root = await realpath(rawRoot);
|
|
24
|
+
const relative = path.relative(root, physical);
|
|
25
|
+
if (signal?.aborted || relative === '..' || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) return null;
|
|
26
|
+
return { root, id: createHash('sha256').update('lm-memory-project-v1\0').update(root).digest('hex') };
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|