@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/src/lang.js CHANGED
@@ -103,6 +103,17 @@ export const STR = {
103
103
 
104
104
  // --- dream/sleep.js:冲突候选列表 --------------------------------------------
105
105
  similarityReason: { zh: (s) => `相似度 ${s}`, en: (s) => `similarity ${s}` },
106
+ // v0.8.1(issue #170 第 2 步):跨 scope 相似对停车到冲突队列的专属 reason。
107
+ scopeCandidateReason: {
108
+ zh: (s) => `跨作用域相似 ${s}(疑似同一内容落在两个归属下,请裁决归属/去重)`,
109
+ en: (s) => `cross-scope similar ${s} (same content under two scopes — review ownership)`
110
+ },
111
+ // v0.8.1(issue #170 复核项 3):普通 dream 的 LLM 输出跨 scope conflict 时
112
+ // 停车用(相似度未知,带模型给的裁决理由)。
113
+ scopeConflictParkReason: {
114
+ zh: (r) => `跨作用域冲突(归属不同,不自动裁决)${r ? `:${r}` : ""}`,
115
+ en: (r) => `cross-scope conflict (different ownership — not auto-adjudicated)${r ? `: ${r}` : ""}`
116
+ },
106
117
  candidateConflicts: {
107
118
  zh: (p) => `候选冲突:\nid=${p.a.id} | type=${p.a.type} | title=${p.a.title}\n${p.a.content}\n---\nid=${p.b.id} | type=${p.b.type} | title=${p.b.title}\n${p.b.content}\n(相似度 ${p.similarity.toFixed(2)})`,
108
119
  en: (p) => `Candidate conflicts:\nid=${p.a.id} | type=${p.a.type} | title=${p.a.title}\n${p.a.content}\n---\nid=${p.b.id} | type=${p.b.type} | title=${p.b.title}\n${p.b.content}\n(similarity ${p.similarity.toFixed(2)})`
@@ -141,6 +152,18 @@ export const STR = {
141
152
  zh: (name) => `自定义指令 ${name}`,
142
153
  en: (name) => `Custom command ${name}`
143
154
  },
155
+ commandInputHint: {
156
+ zh: "可选:追加给模型的补充说明",
157
+ en: "Optional: extra note to append for the model"
158
+ },
159
+ commandSubmitted: {
160
+ zh: (name) => `已提交指令:${name}`,
161
+ en: (name) => `Submitted command: ${name}`
162
+ },
163
+ commandSubmitFailed: {
164
+ zh: (name) => `指令 ${name} 提交失败,详见日志`,
165
+ en: (name) => `Failed to submit command ${name}; see logs`
166
+ },
144
167
 
145
168
  // --- mirror.js:镜像文件标签(渲染随实例语言;解析两种语言都认) ----------------
146
169
  mirrorLabel: {
package/src/scope.js ADDED
@@ -0,0 +1,118 @@
1
+ // v0.8.0 A1(issue #17):写入端 scope 解析。
2
+ //
3
+ // 身份模型(issue #17 的核心约定):session 只是「载体」,记忆挂在持久化身份
4
+ // 标签上——agent_scope = session header 的 agentPreset(跨会话稳定的 agent 模板
5
+ // 身份),workspace_scope = 会话工作目录(registry 反查的 canonical path 优先,
6
+ // header.cwd 兜底)。session_id 永不作为作用域键。
7
+ //
8
+ // 硬约束:解析失败绝不阻塞写入——任何一步取不到就落 NULL(读侧 NULL = 未标注
9
+ // = 全局可见),任何异常都被吞掉(registry 故障 warn 一次后降级)。
10
+ //
11
+ // v0.8.1(issue #170 复核项 2):scopeEnabled 的闸门从解析器挪到「写标注」处
12
+ // (memory_save 的 auto 分支)。解析器恒返回会话身份(真取不到才是 null)——
13
+ // 读取侧(strictVisibility / memory_get / inject)与显式声明都依赖它:flag 关
14
+ // 只关「自动标注」,不能连读取身份一起关,否则 strictScope 硬墙被静默拆掉
15
+ // (fail-open)。
16
+
17
+ /**
18
+ * @param {object} deps
19
+ * @param {object} [deps.ctx] 宿主上下文(可能带 workspaceRegistry;旧版 dsh 没有)
20
+ * @param {object} [deps.config] 已解析配置(读 scopeEnabled)
21
+ * @param {object} [deps.logger]
22
+ * @returns {(exec: object) => {agent_scope: string|null, workspace_scope: string|null}|null}
23
+ */
24
+ export function createScopeResolver({ ctx, config, logger } = {}) {
25
+ let warned = false;
26
+ const warnOnce = (msg) => {
27
+ if (warned) return;
28
+ warned = true;
29
+ try {
30
+ logger?.warn?.(msg);
31
+ } catch {
32
+ // logger 自身故障同样不能影响写入路径
33
+ }
34
+ };
35
+
36
+ return function resolveWriteScope(exec) {
37
+ let session = null;
38
+ try {
39
+ session = exec?.agent?.session ?? null;
40
+ } catch {
41
+ session = null;
42
+ }
43
+
44
+ // 持久化 header(issue #17 的身份契约):session.header 是创建时冻结的
45
+ // SessionHeader(含 agentPreset/cwd)。⚠️ 不能先读 requestHeader()——那返回
46
+ // EpochHeader(请求级模型路由头,首个请求后即为真值),会把 session.header
47
+ // 遮蔽掉,agentPreset/cwd 双双取空(本地 web 复验 2026-09-13 实测踩中)。
48
+ // 仅当宿主没有 .header 属性时才降级尝试 requestHeader().config(旧形状)。
49
+ let header = null;
50
+ try {
51
+ header = session?.header ?? session?.requestHeader?.()?.config ?? null;
52
+ } catch {
53
+ header = null;
54
+ }
55
+
56
+ let agentScope = null;
57
+ const preset = header?.agentPreset;
58
+ if (typeof preset === "string" && preset.trim()) agentScope = preset.trim();
59
+
60
+ // workspace:registry 反查优先(成员资格 = id 命中 + header canonical cwd
61
+ // 校验过,path 已 realpath 归一),失败回退 header.cwd,再失败留 NULL。
62
+ let workspaceScope = null;
63
+ try {
64
+ const list = ctx?.workspaceRegistry?.list?.();
65
+ const sessionId = session?.id;
66
+ if (Array.isArray(list) && sessionId != null) {
67
+ const hit = list.find(
68
+ (ws) => Array.isArray(ws?.sessionIds) && ws.sessionIds.includes(sessionId)
69
+ );
70
+ if (typeof hit?.path === "string" && hit.path.trim()) workspaceScope = hit.path.trim();
71
+ }
72
+ } catch (e) {
73
+ warnOnce(
74
+ `[dsh-mneme] workspaceRegistry lookup failed, falling back to session header cwd: ${String(e)}`
75
+ );
76
+ }
77
+ if (!workspaceScope) {
78
+ const cwd = header?.cwd;
79
+ if (typeof cwd === "string" && cwd.trim()) workspaceScope = cwd.trim();
80
+ }
81
+
82
+ return { agent_scope: agentScope, workspace_scope: workspaceScope };
83
+ };
84
+ }
85
+
86
+ /**
87
+ * v0.8.1 底座(issue #170):显式 scope 参数(memory_save / memory_update /
88
+ * 面板编辑)的归一化。约定:
89
+ * - 省略(undefined,由调用方判断)= 该维不声明,回落载体自动标注;
90
+ * - "global"(大小写不敏感)/ "*" / 空白 = 显式声明全局 → 存储 NULL
91
+ * (行上的 agent_scope_source='explicit' 承担「显式全局」与「从未标注」
92
+ * 的区分,NULL 本身继续表示全局可见);
93
+ * - 其余非空 trim 字符串 = 收窄到该标签。
94
+ * 脏输入(非字符串)落 NULL(等同声明全局)——与 resolveWriteScope 的「解析
95
+ * 绝不阻塞写入」同一哲学:显式声明是用户意图,形状不对时宁可放宽不报错。
96
+ *
97
+ * @param {unknown} raw 工具/HTTP 参数原值
98
+ * @returns {string|null} 归一化后的标签值(null=显式全局)
99
+ */
100
+ export function normalizeExplicitScope(raw) {
101
+ if (typeof raw !== "string") return null;
102
+ const s = raw.trim();
103
+ if (!s || s === "*" || s.toLowerCase() === "global") return null;
104
+ return s;
105
+ }
106
+
107
+ /**
108
+ * scope 标注的比较键(去重键/检索加权/跨 scope 配对共用口径):非空 trim 字符串
109
+ * 原样返回,其余(NULL/undefined/空串/非字符串)统一为 null(「未标注」)。
110
+ * v0.8.1(issue #170 第 2 步)从 service.js 上提为共享定义——sleep 的跨 scope
111
+ * 候选配对必须与去重键用同一把钥匙,NULL 与已标注才不会错配。
112
+ *
113
+ * @param {unknown} v scope 标注原值
114
+ * @returns {string|null} 比较键(null=未标注)
115
+ */
116
+ export function scopeKeyOf(v) {
117
+ return typeof v === "string" && v.trim() ? v.trim() : null;
118
+ }