@modusensus/dsh-mneme 0.7.32 → 0.8.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 +44 -20
- package/lib/api.js +100 -12
- package/lib/client.js +276 -4
- package/lib/commands.js +30 -3
- package/lib/config.js +21 -0
- package/lib/dream/sleep.js +43 -12
- package/lib/dream.js +43 -2
- package/lib/inject.js +39 -5
- package/lib/lang.js +23 -0
- package/lib/scope.js +118 -0
- package/lib/service.js +374 -62
- package/lib/settings.js +7 -0
- package/lib/store.js +245 -7
- package/lib/tools.js +138 -17
- package/lib/version-check.js +122 -0
- package/package.json +1 -1
- package/src/api.js +100 -12
- package/src/commands.js +30 -3
- package/src/config.js +21 -0
- package/src/dream/sleep.js +43 -12
- package/src/dream.js +43 -2
- package/src/inject.js +39 -5
- package/src/lang.js +23 -0
- package/src/scope.js +118 -0
- package/src/service.js +374 -62
- package/src/settings.js +7 -0
- package/src/store.js +245 -7
- package/src/tools.js +138 -17
- package/src/version-check.js +122 -0
- package/test/api.test.js +90 -4
- package/test/client.test.js +58 -0
- package/test/commands.test.js +40 -3
- package/test/conflict-freeze.test.js +15 -5
- package/test/inject.test.js +93 -0
- package/test/scope-candidates.test.js +239 -0
- package/test/scope-explicit.test.js +452 -0
- package/test/scope-retrieval.test.js +197 -0
- package/test/scope-storage.test.js +255 -0
- package/test/scope-strict.test.js +231 -0
- package/test/service.test.js +76 -0
- package/test/version-check.test.js +152 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// 版本自检(issue #174 后续,2026-09-15 拍板):独立插件不把「用户在用哪个
|
|
2
|
+
// 版本」托付给插件市场——市场收录有 ~26h 年龄闸门 + 30min 缓存,且安装时
|
|
3
|
+
// 精确指定过版本的 profile 会被 pnpm 钉子挡住(update 尊重既有 semver 范围,
|
|
4
|
+
// 永不跨 minor/major,#174 的报障者正是这样用着 0.7.32 的行为模型来报 bug)。
|
|
5
|
+
//
|
|
6
|
+
// 本模块只做三件事:
|
|
7
|
+
// 1. 向 npm registry 查 @modusensus/dsh-mneme 的 latest dist-tag(带 TTL
|
|
8
|
+
// 缓存 + 超时 + 全失败静默——离线/被墙/registry 异常都返回 null,调用方
|
|
9
|
+
// 据 null 渲染「未知」,前端不打扰);
|
|
10
|
+
// 2. 与运行版本做纯函数比对(classify);
|
|
11
|
+
// 3. 对外暴露 registry URL 白名单校验(validateRegistryUrl)——发请求前
|
|
12
|
+
// 强制 https + 精确 host,构造上排除本机与内网地址。
|
|
13
|
+
import { readFileSync } from "node:fs";
|
|
14
|
+
|
|
15
|
+
// 运行版本:src/ 与 lib/ 都在插件根下一层,相对 import.meta.url 解析一致;
|
|
16
|
+
// 读取失败(打包/受限环境)降级 "unknown",横幅随之静默。
|
|
17
|
+
const PACKAGE_VERSION = (() => {
|
|
18
|
+
try {
|
|
19
|
+
return JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).version ?? "unknown";
|
|
20
|
+
} catch {
|
|
21
|
+
return "unknown";
|
|
22
|
+
}
|
|
23
|
+
})();
|
|
24
|
+
|
|
25
|
+
// registry 查询地址是模块常量而非用户输入;validateRegistryUrl 仍作为发请求
|
|
26
|
+
// 前的强制闸(构造上即排除非 https 与非白名单 host,双保险且可单测)。
|
|
27
|
+
const REGISTRY_URL = "https://registry.npmjs.org/@modusensus%2Fdsh-mneme/latest";
|
|
28
|
+
const REGISTRY_HOST = "registry.npmjs.org";
|
|
29
|
+
// 市场收录本身有 ~1 天年龄闸门,1h 的 registry 缓存远低于该粒度,足够新鲜。
|
|
30
|
+
const CACHE_TTL_MS = 60 * 60 * 1000;
|
|
31
|
+
const FETCH_TIMEOUT_MS = 5000;
|
|
32
|
+
|
|
33
|
+
// 精确 host 白名单:协议必须 https,且 host 逐字等于 registry.npmjs.org。
|
|
34
|
+
// 这一条即构造性排除本机回环、私有网段与保留地址——不做任何
|
|
35
|
+
// 「看起来像公网」的宽松判断,将来换源必须显式改这里的常量。
|
|
36
|
+
export function validateRegistryUrl(candidate) {
|
|
37
|
+
let url;
|
|
38
|
+
try {
|
|
39
|
+
url = new URL(candidate);
|
|
40
|
+
} catch {
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return url.protocol === "https:" && url.hostname === REGISTRY_HOST;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 解析 `x.y.z` / `x.y.z-后缀` 为可比较结构;解析失败返回 null。 */
|
|
47
|
+
function parseVersion(text) {
|
|
48
|
+
if (typeof text !== "string") return null;
|
|
49
|
+
const match = text.trim().match(/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z.-]+))?$/);
|
|
50
|
+
if (!match) return null;
|
|
51
|
+
return {
|
|
52
|
+
major: Number(match[1]),
|
|
53
|
+
minor: Number(match[2]),
|
|
54
|
+
patch: Number(match[3]),
|
|
55
|
+
// 有 prerelease 后缀的同号版本视为更旧(0.9.0-rc.1 < 0.9.0)。
|
|
56
|
+
prerelease: match[4] !== undefined
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 三元组语义化比较:a<b → -1,a>b → 1,无法比较 → null(调用方归入 unknown)。
|
|
62
|
+
* 仅比较数字部分;数字相等时,带 prerelease 的一方更旧(0.9.0-rc.1 < 0.9.0),
|
|
63
|
+
* 两侧都带则视为同版——横幅只关心「是否落后于 latest」,不追求完整 semver 序。
|
|
64
|
+
*/
|
|
65
|
+
export function compareVersions(a, b) {
|
|
66
|
+
const va = parseVersion(a);
|
|
67
|
+
const vb = parseVersion(b);
|
|
68
|
+
if (!va || !vb) return null;
|
|
69
|
+
for (const key of ["major", "minor", "patch"]) {
|
|
70
|
+
if (va[key] !== vb[key]) return va[key] < vb[key] ? -1 : 1;
|
|
71
|
+
}
|
|
72
|
+
if (va.prerelease !== vb.prerelease) return va.prerelease ? -1 : 1;
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 运行版本 vs registry latest 的展示归类。latest 为 null(查询失败/离线)
|
|
78
|
+
* 或任一侧解析失败时归 unknown——前端对 unknown 完全静默。
|
|
79
|
+
*/
|
|
80
|
+
export function classify(version, latest) {
|
|
81
|
+
const cmp = compareVersions(version, latest);
|
|
82
|
+
if (cmp === null) return "unknown";
|
|
83
|
+
if (cmp < 0) return "outdated";
|
|
84
|
+
if (cmp > 0) return "ahead";
|
|
85
|
+
return "up-to-date";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 模块级缓存:面板可能多处/多次拉取,TTL 内一律复用,不重复打 registry。
|
|
89
|
+
// at 用 -Infinity 作「从未拉取」哨兵:任何时钟下 now-at 都是正无穷,首轮
|
|
90
|
+
// 必然真实请求(测试的假时钟从 0 起也不会被误判成新鲜缓存)。
|
|
91
|
+
let cache = { at: -Infinity, latest: null };
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 查询 registry latest。TTL 内直接命中缓存;任何失败(网络/超时/非 2xx/
|
|
95
|
+
* 响应形状不对/URL 校验不过)都静默返回 null——版本提示是锦上添花,绝不
|
|
96
|
+
* 让它成为新的故障面。失败不写缓存:TTL 起点保持旧值,下次调用立刻重试。
|
|
97
|
+
*/
|
|
98
|
+
export async function fetchLatestVersion({ fetchImpl = typeof fetch === "function" ? fetch : () => Promise.reject(new Error("no fetch")), now = Date.now } = {}) {
|
|
99
|
+
if (now() - cache.at < CACHE_TTL_MS) return cache.latest;
|
|
100
|
+
if (!validateRegistryUrl(REGISTRY_URL)) return null;
|
|
101
|
+
let manifest;
|
|
102
|
+
try {
|
|
103
|
+
const response = await fetchImpl(REGISTRY_URL, {
|
|
104
|
+
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS),
|
|
105
|
+
headers: { accept: "application/json" }
|
|
106
|
+
});
|
|
107
|
+
if (!response.ok) return null;
|
|
108
|
+
manifest = await response.json();
|
|
109
|
+
} catch {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
const latest = typeof manifest?.version === "string" && manifest.version ? manifest.version : null;
|
|
113
|
+
cache = { at: now(), latest };
|
|
114
|
+
return latest;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** 仅供测试:清空模块级缓存与时间戳。 */
|
|
118
|
+
export function resetCacheForTest() {
|
|
119
|
+
cache = { at: -Infinity, latest: null };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export { PACKAGE_VERSION, REGISTRY_URL, CACHE_TTL_MS };
|
package/test/api.test.js
CHANGED
|
@@ -649,11 +649,12 @@ test("GET /api/dsh-mneme/features returns empty overrides and effective config d
|
|
|
649
649
|
// dreamMaxTokens、睡眠路由 sleepProvider/sleepModel、PR1 新增的
|
|
650
650
|
// recallFusion/signalTransparency、issue #109 新增的实体抽取路由
|
|
651
651
|
// entityExtractionProvider/entityExtractionModel/entityExtractionReasoning、
|
|
652
|
-
// issue #127 新增的 summarize 五键、issue #126 新增的 sleepActionSet
|
|
653
|
-
// issue #125
|
|
652
|
+
// issue #127 新增的 summarize 五键、issue #126 新增的 sleepActionSet,
|
|
653
|
+
// 以及 issue #125 新增的候选集三键、issue #17(v0.8.0 A1/A3)新增的
|
|
654
|
+
// scopeEnabled/strictScope),未覆盖时取 bundle 配置的解析默认值;
|
|
654
655
|
// dreamProvider/dreamModel 无 schema 默认值(Config({}) 解析为 undefined),
|
|
655
|
-
// 不编造给前端 →
|
|
656
|
-
assert.equal(Object.keys(data.effective).length,
|
|
656
|
+
// 不编造给前端 → 53 - 2 = 51
|
|
657
|
+
assert.equal(Object.keys(data.effective).length, 51);
|
|
657
658
|
assert.equal(data.effective.dreamSkipInvalid, true);
|
|
658
659
|
assert.equal(data.effective.allowCrossTypeMerge, false);
|
|
659
660
|
assert.equal(data.effective.dreamMinIntervalMinutes, 0);
|
|
@@ -1068,6 +1069,91 @@ test("GET /api/dsh-mneme/dream-status returns runs and pending conflict ids", as
|
|
|
1068
1069
|
assert.deepEqual(afterData.pendingMemoryIds, []);
|
|
1069
1070
|
});
|
|
1070
1071
|
|
|
1072
|
+
// v0.8.0 冲突集中处理:GET /conflicts 队列联表 + POST /conflicts/resolve 人工确认。
|
|
1073
|
+
test("GET /api/dsh-mneme/conflicts joins both sides of unresolved conflicts", async () => {
|
|
1074
|
+
const { routes, service } = setup();
|
|
1075
|
+
const a = service.saveWithDedupe({ type: "history", title: "界面暗色偏好", content: "用户当前使用暗色界面主题", importance: 3 }).memory;
|
|
1076
|
+
const b = service.saveWithDedupe({ type: "history", title: "界面亮色切换", content: "用户界面亮色切换计划", importance: 3 }).memory;
|
|
1077
|
+
service.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "主题偏好前后矛盾" });
|
|
1078
|
+
|
|
1079
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/conflicts");
|
|
1080
|
+
const res = new FakeRes();
|
|
1081
|
+
await route.handler(req("/api/dsh-mneme/conflicts"), res);
|
|
1082
|
+
assert.equal(res.statusCode, 200);
|
|
1083
|
+
const data = JSON.parse(res.body);
|
|
1084
|
+
assert.equal(data.items.length, 1);
|
|
1085
|
+
assert.equal(data.total, 1);
|
|
1086
|
+
assert.equal(data.items[0].reason, "主题偏好前后矛盾");
|
|
1087
|
+
// saveConflictPending 会按 id 字典序归一 A/B 侧位——断言集合而非位置。
|
|
1088
|
+
assert.deepEqual(
|
|
1089
|
+
[data.items[0].memory_a.title, data.items[0].memory_b.title].sort(),
|
|
1090
|
+
["界面亮色切换", "界面暗色偏好"]
|
|
1091
|
+
);
|
|
1092
|
+
assert.equal(data.items[0].memory_a.archived, false);
|
|
1093
|
+
assert.equal(data.items[0].memory_b.archived, false);
|
|
1094
|
+
// 幽灵侧容忍缺失
|
|
1095
|
+
service.saveConflictPending({ memory_a: a.id, memory_b: "ghost-id", reason: "对方已删" });
|
|
1096
|
+
const res2 = new FakeRes();
|
|
1097
|
+
await route.handler(req("/api/dsh-mneme/conflicts"), res2);
|
|
1098
|
+
const data2 = JSON.parse(res2.body);
|
|
1099
|
+
assert.equal(data2.items.length, 2);
|
|
1100
|
+
assert.equal(data2.items.find((x) => x.reason === "对方已删").memory_b.missing, true);
|
|
1101
|
+
});
|
|
1102
|
+
|
|
1103
|
+
test("POST /api/dsh-mneme/conflicts/resolve stamps and applies the disposition", async () => {
|
|
1104
|
+
const { store, routes, service } = setup();
|
|
1105
|
+
const a = service.saveWithDedupe({ type: "history", title: "界面暗色偏好", content: "用户当前使用暗色界面主题", importance: 3 }).memory;
|
|
1106
|
+
const b = service.saveWithDedupe({ type: "history", title: "界面亮色切换", content: "用户界面亮色切换计划", importance: 3 }).memory;
|
|
1107
|
+
const pending = service.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "主题偏好前后矛盾" });
|
|
1108
|
+
|
|
1109
|
+
// store 会把 pair 按 id 字典序归一——从队列行反查「界面亮色切换」所在侧再选。
|
|
1110
|
+
const queueRoute = routes.find((r) => r.path === "/api/dsh-mneme/conflicts");
|
|
1111
|
+
const queueRes = new FakeRes();
|
|
1112
|
+
await queueRoute.handler(req("/api/dsh-mneme/conflicts"), queueRes);
|
|
1113
|
+
const queued = JSON.parse(queueRes.body).items.find((x) => x.id === pending.id);
|
|
1114
|
+
const keepSide = queued.memory_a.id === b.id ? "a" : "b";
|
|
1115
|
+
// loser 恒为 a(keeper 恒为「界面亮色切换」= b),与 A/B 侧位字母无关
|
|
1116
|
+
const loserId = a.id;
|
|
1117
|
+
|
|
1118
|
+
const route = routes.find((r) => r.path === "/api/dsh-mneme/conflicts/resolve");
|
|
1119
|
+
// 保留「界面亮色切换」所在侧(apply 默认 true)→ 该侧保留,另一侧归档
|
|
1120
|
+
const res = new FakeRes();
|
|
1121
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", { id: pending.id, winner: keepSide }), res);
|
|
1122
|
+
assert.equal(res.statusCode, 200);
|
|
1123
|
+
const data = JSON.parse(res.body);
|
|
1124
|
+
assert.equal(data.ok, true);
|
|
1125
|
+
assert.equal(data.conflict.resolved_winner, b.id);
|
|
1126
|
+
assert.equal(data.conflict.disposition.ok, true);
|
|
1127
|
+
assert.equal(store.getById(b.id).archived, false, "winner kept");
|
|
1128
|
+
assert.equal(store.getById(loserId).archived, true, "loser archived by apply");
|
|
1129
|
+
|
|
1130
|
+
// 处理过的 id 再 resolve → 幂等重盖(仍是 200;队列视图只列未解决行,
|
|
1131
|
+
// 正常 UI 不会再发这个请求,但接口层保持幂等)
|
|
1132
|
+
const res2 = new FakeRes();
|
|
1133
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", { id: pending.id, winner: "a" }), res2);
|
|
1134
|
+
assert.equal(res2.statusCode, 200);
|
|
1135
|
+
assert.equal(JSON.parse(res2.body).ok, true);
|
|
1136
|
+
|
|
1137
|
+
// 未知 id → 404;缺 id → 400
|
|
1138
|
+
const res3 = new FakeRes();
|
|
1139
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", { id: "nope", winner: "a" }), res3);
|
|
1140
|
+
assert.equal(res3.statusCode, 404);
|
|
1141
|
+
const res4 = new FakeRes();
|
|
1142
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", {}), res4);
|
|
1143
|
+
assert.equal(res4.statusCode, 400);
|
|
1144
|
+
|
|
1145
|
+
// apply:false 只盖章不处置
|
|
1146
|
+
const c = service.saveWithDedupe({ type: "decision", title: "丙", content: "内容丙" }).memory;
|
|
1147
|
+
const d = service.saveWithDedupe({ type: "decision", title: "丁", content: "内容丁" }).memory;
|
|
1148
|
+
const p2 = service.saveConflictPending({ memory_a: c.id, memory_b: d.id, reason: "r2" });
|
|
1149
|
+
const res5 = new FakeRes();
|
|
1150
|
+
await route.handler(req("/api/dsh-mneme/conflicts/resolve", "POST", { id: p2.id, apply: false }), res5);
|
|
1151
|
+
assert.equal(res5.statusCode, 200);
|
|
1152
|
+
assert.equal(JSON.parse(res5.body).conflict.disposition, null);
|
|
1153
|
+
assert.equal(store.getById(c.id).archived, false);
|
|
1154
|
+
assert.equal(store.getById(d.id).archived, false);
|
|
1155
|
+
});
|
|
1156
|
+
|
|
1071
1157
|
test("parseHumanEdits is the pure core of readHumanEdits (CRLF tolerant)", () => {
|
|
1072
1158
|
const digest = "0".repeat(64);
|
|
1073
1159
|
const text = [
|
package/test/client.test.js
CHANGED
|
@@ -676,4 +676,62 @@ test("vector status card surfaces the configured/degraded split", () => {
|
|
|
676
676
|
clientSource.includes('t("memory.status.vectorDegradedHint")'),
|
|
677
677
|
"the degraded state must render its own caption (0 of {m} indexed)"
|
|
678
678
|
);
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
// v0.8.0 A4(issue #17):作用域隔离的面板面——设置卡新组(scopeEnabled /
|
|
682
|
+
// strictScope 开关)+ 记忆条目的 scope provenance 展示。i18n 键必须中英双语
|
|
683
|
+
// 齐全;徽章/详情行沿用 heat 徽章的「字段缺省即不渲染」模式,前端不感知开关。
|
|
684
|
+
/**
|
|
685
|
+
* A4(issue #17)面板面回归:作用域隔离的设置卡分组、十个 i18n 键的中英双语
|
|
686
|
+
* 齐备(沿用 occurrences ≥ 2 模式),以及 ScopeBadge 组件与三处接线(cards
|
|
687
|
+
* 脚部 / timeline 行 / 详情抽屉 dmeta)的真实存在性——只做源码断言,不挂
|
|
688
|
+
* React 渲染环境,与文件内其他 clientSource 断言同一风格。
|
|
689
|
+
*/
|
|
690
|
+
test("scope isolation ships panel switches and provenance surfaces (A4)", () => {
|
|
691
|
+
assert.ok(
|
|
692
|
+
/key: "group\.scope", items: \["scopeEnabled", "strictScope"\]/.test(clientSource),
|
|
693
|
+
"FEATURE_GROUPS must carry a dedicated scope group"
|
|
694
|
+
);
|
|
695
|
+
for (const key of [
|
|
696
|
+
"memory.features.group.scope",
|
|
697
|
+
"memory.features.scopeEnabled",
|
|
698
|
+
"memory.features.scopeEnabled.hint",
|
|
699
|
+
"memory.features.strictScope",
|
|
700
|
+
"memory.features.strictScope.hint",
|
|
701
|
+
"memory.explorer.scopeBadge",
|
|
702
|
+
"memory.explorer.detail.agentScope",
|
|
703
|
+
"memory.explorer.detail.workspaceScope",
|
|
704
|
+
"memory.explorer.detail.sensitivity",
|
|
705
|
+
"memory.explorer.detail.occurred"
|
|
706
|
+
]) {
|
|
707
|
+
const occurrences = clientSource.split(`"${key}"`).length - 1;
|
|
708
|
+
assert.ok(occurrences >= 2, `i18n key ${key} must exist in both zh and en (got ${occurrences})`);
|
|
709
|
+
}
|
|
710
|
+
assert.ok(/const ScopeBadge = \(\{ m, t \}\)/.test(clientSource), "ScopeBadge component must exist");
|
|
711
|
+
assert.ok(clientSource.includes("h(ScopeBadge, { m, t })"), "ScopeBadge must be wired into browse views");
|
|
712
|
+
assert.ok(clientSource.includes('t("memory.explorer.detail.agentScope")'), "drawer must render agent scope row");
|
|
713
|
+
assert.ok(clientSource.includes('t("memory.explorer.detail.occurred")'), "drawer must render occurred row");
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
// v0.8.0 冲突集中处理:状态页的待确认冲突队列(reason + 双方内容对比 +
|
|
717
|
+
// 保留 A/B/仅标记动作),resolveConflictPending 的第一个前端出口。双语齐备。
|
|
718
|
+
test("conflict queue ships a central review surface on the status tab", () => {
|
|
719
|
+
for (const key of [
|
|
720
|
+
"memory.status.conflictQueue.reason",
|
|
721
|
+
"memory.status.conflictQueue.sideA",
|
|
722
|
+
"memory.status.conflictQueue.sideB",
|
|
723
|
+
"memory.status.conflictQueue.keepA",
|
|
724
|
+
"memory.status.conflictQueue.keepB",
|
|
725
|
+
"memory.status.conflictQueue.markReviewed",
|
|
726
|
+
"memory.status.conflictQueue.applyHint",
|
|
727
|
+
"memory.status.conflictQueue.missing",
|
|
728
|
+
"memory.status.conflictQueue.refresh"
|
|
729
|
+
]) {
|
|
730
|
+
const occurrences = clientSource.split(`"${key}"`).length - 1;
|
|
731
|
+
assert.ok(occurrences >= 2, `i18n key ${key} must exist in both zh and en (got ${occurrences})`);
|
|
732
|
+
}
|
|
733
|
+
assert.ok(/function ConflictsQueue\(\{ t \}\)/.test(clientSource), "ConflictsQueue component must exist");
|
|
734
|
+
assert.ok(clientSource.includes('"/api/dsh-mneme/conflicts"'), "queue must fetch the conflicts endpoint");
|
|
735
|
+
assert.ok(clientSource.includes('"/api/dsh-mneme/conflicts/resolve"'), "queue must call the resolve endpoint");
|
|
736
|
+
assert.ok(clientSource.includes("h(ConflictsQueue, { t })"), "status tab must render the queue");
|
|
679
737
|
});
|
package/test/commands.test.js
CHANGED
|
@@ -40,14 +40,51 @@ test("add registers live; remove unregisters live", () => {
|
|
|
40
40
|
store.close();
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
test("handler
|
|
43
|
+
test("handler submits the instruction to the agent and returns a receipt", async () => {
|
|
44
44
|
const { store, manager, registered } = setup();
|
|
45
45
|
manager.add({ name: "fmt", description: "格式化", instruction: "请按项目的 lint 规则格式化当前文件" });
|
|
46
46
|
const def = registered.get("fmt");
|
|
47
47
|
assert.ok(def, "definition registered");
|
|
48
|
-
|
|
48
|
+
assert.equal(def.input.hint, "可选:追加给模型的补充说明");
|
|
49
|
+
const submitted = [];
|
|
50
|
+
const agent = { followup: (message) => submitted.push(message) };
|
|
51
|
+
const result = await def.handler({ agent, rawInput: "", signal: null });
|
|
49
52
|
assert.equal(result.kind, "success");
|
|
50
|
-
assert.equal(result.text, "
|
|
53
|
+
assert.equal(result.text, "已提交指令:fmt");
|
|
54
|
+
assert.equal(submitted.length, 1);
|
|
55
|
+
assert.equal(submitted[0].role, "user");
|
|
56
|
+
assert.deepEqual(submitted[0].content, [{ type: "text", text: "请按项目的 lint 规则格式化当前文件" }]);
|
|
57
|
+
assert.deepEqual(submitted[0].source, { kind: "user" });
|
|
58
|
+
store.close();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("rawInput suffix is appended for the model", async () => {
|
|
62
|
+
const { store, manager, registered } = setup();
|
|
63
|
+
manager.add({ name: "mneme", instruction: "以「小墨」的口吻自我介绍" });
|
|
64
|
+
const def = registered.get("mneme");
|
|
65
|
+
const submitted = [];
|
|
66
|
+
await def.handler({ agent: { followup: (m) => submitted.push(m) }, rawInput: " 顺便列出归档数量 " });
|
|
67
|
+
assert.equal(submitted[0].content[0].text, "以「小墨」的口吻自我介绍\n\n顺便列出归档数量");
|
|
68
|
+
store.close();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test("handler without a receiving agent falls back to surfacing the instruction", () => {
|
|
72
|
+
const { store, manager, registered } = setup();
|
|
73
|
+
manager.add({ name: "fmt", instruction: "legacy text" });
|
|
74
|
+
const def = registered.get("fmt");
|
|
75
|
+
const result = def.handler({ rawInput: "" });
|
|
76
|
+
assert.equal(result.kind, "success");
|
|
77
|
+
assert.equal(result.text, "legacy text");
|
|
78
|
+
store.close();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("handler returns an error result when the agent rejects the submission", () => {
|
|
82
|
+
const { store, manager, registered } = setup();
|
|
83
|
+
manager.add({ name: "boom", instruction: "x" });
|
|
84
|
+
const def = registered.get("boom");
|
|
85
|
+
const result = def.handler({ agent: { followup: () => { throw new Error("nope"); } } });
|
|
86
|
+
assert.equal(result.kind, "error");
|
|
87
|
+
assert.match(result.text, /boom/);
|
|
51
88
|
store.close();
|
|
52
89
|
});
|
|
53
90
|
|
|
@@ -111,13 +111,23 @@ test("store: countConflictPending 只统计未决行", () => {
|
|
|
111
111
|
store.close();
|
|
112
112
|
});
|
|
113
113
|
|
|
114
|
-
test("store:
|
|
114
|
+
test("store: 已解决的同一对在内容变化后才可再次 pending(issue #170 复核项 4)", async () => {
|
|
115
115
|
const store = openStore();
|
|
116
|
-
const
|
|
116
|
+
const a = store.save({ type: "project", title: "对A", content: "内容A" });
|
|
117
|
+
const b = store.save({ type: "project", title: "对B", content: "内容B" });
|
|
118
|
+
const p1 = store.saveConflictPending({ memory_a: a.id, memory_b: b.id, reason: "r" });
|
|
117
119
|
store.resolveConflictPending(p1.id, { winner: "a" });
|
|
118
|
-
//
|
|
119
|
-
const
|
|
120
|
-
assert.
|
|
120
|
+
// 内容未变 → 已裁决对不重新入队:「保留双方」是有效决定,不是待办。
|
|
121
|
+
const suppressed = store.saveConflictPending({ memory_a: b.id, memory_b: a.id, reason: "again" });
|
|
122
|
+
assert.equal(suppressed, undefined, "未变化的已裁决对被抑制");
|
|
123
|
+
assert.equal(store.countConflictPending(), 0);
|
|
124
|
+
// 任一侧被改过(updated_at 晚于 resolved_at)→ 重新入队产生新 pending。
|
|
125
|
+
// 留真实时间间隔:updated_at/resolved_at 毫秒精度,同毫秒算未变。
|
|
126
|
+
await new Promise((resolve) => setTimeout(resolve, 5));
|
|
127
|
+
store.update(a.id, { content: "内容A(改)" });
|
|
128
|
+
const p2 = store.saveConflictPending({ memory_a: b.id, memory_b: a.id, reason: "changed" });
|
|
129
|
+
assert.ok(p2, "内容变化后的对重新入队");
|
|
130
|
+
assert.notEqual(p2.id, p1.id, "新 pending 是新行");
|
|
121
131
|
assert.equal(p2.resolved_at, undefined);
|
|
122
132
|
assert.equal(store.countConflictPending(), 1);
|
|
123
133
|
store.close();
|
package/test/inject.test.js
CHANGED
|
@@ -154,3 +154,96 @@ test("Issue #129: 扁平的 data.content 形状仍被兼容", () => {
|
|
|
154
154
|
assert.ok(text.includes("问题"), "query present");
|
|
155
155
|
assert.ok(text.includes("扁平形状的回答"), "flat content shape still supported");
|
|
156
156
|
});
|
|
157
|
+
|
|
158
|
+
// ---- issue #162: 注入边界花括号转义(v0.7.4 #40 修复被 v0.7.11 误删后恢复) ----
|
|
159
|
+
|
|
160
|
+
test("issue #162: {{...}} in memory content is escaped at the injection boundary", () => {
|
|
161
|
+
const { contexts, service } = setup();
|
|
162
|
+
service.saveWithDedupe({ type: "preference", title: "模板记忆", content: "{{hl|highlight}} 和 {{挖空}} {{关键词}}", importance: 5 });
|
|
163
|
+
const text = contexts[0].text({});
|
|
164
|
+
assert.ok(text.includes("模板记忆"), "memory still rendered by title");
|
|
165
|
+
assert.ok(text.includes("{\\{hl|highlight}\\}"), "ASCII template braces escaped");
|
|
166
|
+
assert.ok(text.includes("{\\{挖空}\\}"), "CJK template braces escaped");
|
|
167
|
+
assert.ok(!text.includes("{{"), "no raw {{ survives into the prompt");
|
|
168
|
+
assert.ok(!text.includes("}}"), "no raw }} survives into the prompt");
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("issue #162: odd brace runs like {{{a}}} are escaped too", () => {
|
|
172
|
+
const { contexts, service } = setup();
|
|
173
|
+
service.saveWithDedupe({ type: "preference", title: "三层模板", content: "{{{a}}} 和 }}} 结尾", importance: 5 });
|
|
174
|
+
const text = contexts[0].text({});
|
|
175
|
+
assert.ok(!text.includes("{{"), "no raw {{ for odd brace runs");
|
|
176
|
+
assert.ok(!text.includes("}}"), "no raw }} for odd brace runs");
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
test("issue #162: {{...}} in user profile/rules is escaped", () => {
|
|
180
|
+
const { contexts, settings } = setup();
|
|
181
|
+
settings.setProfile("我叫{{名字}},前端开发者");
|
|
182
|
+
settings.setRules(["回答时用 {{hl|term}}", "以 }} 结尾的规则"]);
|
|
183
|
+
const settingsCtx = contexts.find((c) => c.name === "user-settings");
|
|
184
|
+
const text = settingsCtx.text({});
|
|
185
|
+
assert.ok(text.includes("{\\{名字}\\}"), "profile braces escaped");
|
|
186
|
+
assert.ok(text.includes("{\\{hl|term}\\}"), "rule braces escaped");
|
|
187
|
+
assert.ok(!text.includes("{{"), "no raw {{ in user-settings block");
|
|
188
|
+
assert.ok(!text.includes("}}"), "no raw }} in user-settings block");
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("issue #162: hot-context rounds with {{...}} are escaped", () => {
|
|
192
|
+
const { contexts } = setup();
|
|
193
|
+
const text = contexts[0].text({
|
|
194
|
+
agent: {
|
|
195
|
+
session: {
|
|
196
|
+
id: "s1",
|
|
197
|
+
events: [
|
|
198
|
+
{ type: "user/message", data: { source: { kind: "user" }, content: ["用 {{hl|你好}} 测试"] } },
|
|
199
|
+
{ type: "assistant/message", data: { source: { kind: "assistant" }, content: ["返回 {{答案}}"] } }
|
|
200
|
+
]
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
assert.ok(text.includes("[短期上下文]"), "hot context rendered");
|
|
205
|
+
assert.ok(!text.includes("{{"), "no raw {{ in hot context");
|
|
206
|
+
assert.ok(!text.includes("}}"), "no raw }} in hot context");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("issue #162: reasoning parts are excluded from hot memory (docker --format case)", () => {
|
|
210
|
+
const { contexts } = setup();
|
|
211
|
+
// issue #162 的触发源:assistant 的 reasoning part 是 { type: "reasoning", text }
|
|
212
|
+
// 结构,内容里出现 docker --format "{{.Server.Version}}" 时,若被注入 hot memory
|
|
213
|
+
// 会让 DSH interpolate() 扫到非法变量名 throw → 整轮永久失败。textOf 必须跳过它。
|
|
214
|
+
const text = contexts[0].text({
|
|
215
|
+
agent: {
|
|
216
|
+
session: {
|
|
217
|
+
id: "s1",
|
|
218
|
+
events: [
|
|
219
|
+
{ type: "user/message", data: { source: { kind: "user" }, content: ["检查 docker 版本"] } },
|
|
220
|
+
{
|
|
221
|
+
type: "assistant/message",
|
|
222
|
+
data: {
|
|
223
|
+
source: { kind: "assistant" },
|
|
224
|
+
content: [
|
|
225
|
+
{ type: "reasoning", text: "运行 docker version --format \"{{.Server.Version}}\" 查看版本" },
|
|
226
|
+
{ type: "text", text: "已检查,Docker 版本 27.1.1。" }
|
|
227
|
+
]
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
]
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
// reasoning 不应进入 hot memory
|
|
235
|
+
assert.ok(!text.includes("Server.Version"), "reasoning text excluded from hot memory");
|
|
236
|
+
// 正文保留
|
|
237
|
+
assert.ok(text.includes("27.1.1"), "assistant text part still injected");
|
|
238
|
+
// 整个注入块没有任何原始 {{ (双保险:即使将来 reasoning 被引入,也会被转义)
|
|
239
|
+
assert.ok(!text.includes("{{"), "no raw {{ anywhere in injected context");
|
|
240
|
+
assert.ok(!text.includes("}}"), "no raw }} anywhere in injected context");
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("issue #162: escapePromptVariables=false passes {{...}} through verbatim", () => {
|
|
244
|
+
const { contexts, service } = setup({ escapePromptVariables: false });
|
|
245
|
+
service.saveWithDedupe({ type: "preference", title: "模板", content: "{{挖空}} 与 {{hl|term}}", importance: 5 });
|
|
246
|
+
const text = contexts[0].text({});
|
|
247
|
+
assert.ok(text.includes("{{挖空}}"), "raw braces preserved when escaping disabled");
|
|
248
|
+
assert.ok(text.includes("{{hl|term}}"), "raw ASCII braces preserved when escaping disabled");
|
|
249
|
+
});
|