Skip to content

Commit 711a1df

Browse files
committed
feat(scope): 归属显式声明与人工纠偏底座——per-dim 来源列 + save/update scope 参数 + 面板可编辑(issue #170 第 1 步)
- store:三列可空迁移 agent_scope_source/workspace_scope_source/scope_decided_at (ADD COLUMN 无 DEFAULT 存量零重写)+ scope_changes 审计表;update 仅在 patch 显式携带键时改写 scope 列(undefined=不动/null=清空为全局) - scope.js:normalizeExplicitScope——global/*/空白=显式全局,标签 trim 收窄 - service:saveWithDedupe 透传来源;同键显式写入并入时把 auto 行升级 explicit; updateMemory 显式 scope 盖章(explicit + decided_at)并写审计(actor tool/panel) - tools:memory_save/memory_update 增加可选 agent_scope/workspace_scope;显式参数 逐维覆盖载体自动标注且不受 scopeEnabled 门控(显式=直接用户意图);输出 schema 补三个可选键 - api:/update 接纳 agent_scope/workspace_scope(null=放宽全局),actor=panel - 面板:抽屉 scope 行双态(查看含来源徽注 + 显式全局可见/编辑文本框),放宽可见性 需二次确认;六个双语 i18n 键 - 测试 +15(scope-explicit.test.js),全量 1003(1002 过 + 1 既有 skip)
1 parent f779177 commit 711a1df

12 files changed

Lines changed: 968 additions & 32 deletions

File tree

‎dsh-mneme/lib/api.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,19 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
480480
}
481481
patch.tags = body.tags;
482482
}
483+
// v0.8.1 底座(issue #170):显式 scope 修正。null=放宽到全局,
484+
// 字符串=收窄/改标;归一化("global"/"*"/空白→NULL + explicit 章 +
485+
// 审计行)在 service.updateMemory 内做,路由只做形状校验。
486+
if (body.agent_scope !== undefined && body.agent_scope !== null && typeof body.agent_scope !== "string") {
487+
sendJson(res, 400, { error: "invalid-agent-scope" });
488+
return;
489+
}
490+
if (body.workspace_scope !== undefined && body.workspace_scope !== null && typeof body.workspace_scope !== "string") {
491+
sendJson(res, 400, { error: "invalid-workspace-scope" });
492+
return;
493+
}
494+
if (body.agent_scope !== undefined) patch.agent_scope = body.agent_scope;
495+
if (body.workspace_scope !== undefined) patch.workspace_scope = body.workspace_scope;
483496
if (body.archived !== undefined && typeof body.archived !== "boolean") {
484497
sendJson(res, 400, { error: "invalid-archived" });
485498
return;
@@ -501,7 +514,7 @@ export function createApi(ctx, service, settings, commands, embedder, semantic =
501514
...history
502515
].slice(0, 20);
503516
}
504-
if (Object.keys(patch).length) service.update(id, patch);
517+
if (Object.keys(patch).length) service.update(id, patch, { actor: "panel" });
505518
if (body.archived !== undefined) service.setArchived(id, body.archived);
506519
sendJson(res, 200, { memory: service.getById(id) });
507520
});

‎dsh-mneme/lib/client.js‎

Lines changed: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,12 @@ window.__ModuleLoader__.load({
407407
"memory.explorer.scopeBadge": "作用域",
408408
"memory.explorer.detail.agentScope": "Agent 作用域",
409409
"memory.explorer.detail.workspaceScope": "工作区作用域",
410+
"memory.explorer.detail.scopeGlobal": "全局(所有会话可见)",
411+
"memory.explorer.detail.scopeSourceAuto": "自动",
412+
"memory.explorer.detail.scopeSourceExplicit": "显式",
413+
"memory.explorer.detail.scopeEditHint": "留空 = 全局;也可填写具体标签收窄归属",
414+
"memory.explorer.detail.scopeWidenConfirm": "确认放宽可见性?",
415+
"memory.explorer.detail.scopeWidenHint": "放宽后该记忆将对所有会话可见",
410416
"memory.explorer.detail.sensitivity": "敏感度",
411417
"memory.explorer.detail.occurred": "发生时间",
412418
"memory.explorer.detail.edit": "编辑",
@@ -748,6 +754,12 @@ window.__ModuleLoader__.load({
748754
"memory.explorer.scopeBadge": "Scoped",
749755
"memory.explorer.detail.agentScope": "Agent scope",
750756
"memory.explorer.detail.workspaceScope": "Workspace scope",
757+
"memory.explorer.detail.scopeGlobal": "Global (visible to all sessions)",
758+
"memory.explorer.detail.scopeSourceAuto": "auto",
759+
"memory.explorer.detail.scopeSourceExplicit": "explicit",
760+
"memory.explorer.detail.scopeEditHint": "Empty = global; or enter a specific label to narrow the scope",
761+
"memory.explorer.detail.scopeWidenConfirm": "Widen visibility?",
762+
"memory.explorer.detail.scopeWidenHint": "Widening makes this memory visible to all sessions",
751763
"memory.explorer.detail.sensitivity": "Sensitivity",
752764
"memory.explorer.detail.occurred": "Occurred",
753765
"memory.explorer.detail.edit": "Edit",
@@ -3191,11 +3203,30 @@ window.__ModuleLoader__.load({
31913203
// 右侧滑出:全文、来源、质量分、关联实体与手动操作(编辑/归档/删除)。
31923204
// 编辑态本地暂存草稿,保存经 POST /update 落库;后端成功后重渲染镜像,
31933205
// 前端只做本地视图同步。两步删除确认在此完成(红钮武装 → 实心红提交)。
3206+
// v0.8.1 底座(issue #170):scope 行的来源徽注(显式/自动)。存量行无来源
3207+
// 时不加后缀——与 A4 的「未标注零视觉变化」同款克制。
3208+
const scopeSourceSuffix = (t, source) =>
3209+
source === "explicit" ? ` · ${t("memory.explorer.detail.scopeSourceExplicit")}`
3210+
: source === "auto" ? ` · ${t("memory.explorer.detail.scopeSourceAuto")}`
3211+
: "";
3212+
const scopeProvenanceTitle = (t, source, decidedAt) =>
3213+
[
3214+
source === "explicit" ? t("memory.explorer.detail.scopeSourceExplicit")
3215+
: source === "auto" ? t("memory.explorer.detail.scopeSourceAuto") : null,
3216+
decidedAt ?? null
3217+
].filter(Boolean).join(" · ");
3218+
31943219
function MemoryDrawer({ t, memory, conflict, deleting, deleteError, onClose, onDelete, onSaved }) {
31953220
const [editing, setEditing] = useState(false);
31963221
const [title, setTitle] = useState(memory.title || "");
31973222
const [content, setContent] = useState(memory.content || "");
31983223
const [importance, setImportance] = useState(memory.importance || 3);
3224+
// v0.8.1 底座(issue #170):scope 归属的人工修正入口。空串=放宽到全局
3225+
// (发送 null),非空=收窄到该标签;只有真的改动才会随 patch 发送并盖
3226+
// explicit 章——普通编辑不得污染归属来源。
3227+
const [agentScope, setAgentScope] = useState(memory.agent_scope || "");
3228+
const [workspaceScope, setWorkspaceScope] = useState(memory.workspace_scope || "");
3229+
const [confirmWiden, setConfirmWiden] = useState(false);
31993230
const [confirmDelete, setConfirmDelete] = useState(false);
32003231
const [busy, setBusy] = useState(false);
32013232
const [savedTick, setSavedTick] = useState(false);
@@ -3234,7 +3265,25 @@ window.__ModuleLoader__.load({
32343265
const save = () => {
32353266
const t2 = title.trim();
32363267
if (!t2) return;
3237-
postUpdate({ title: t2, content, importance }, {});
3268+
const patch = { title: t2, content, importance };
3269+
const curAgent = memory.agent_scope ?? "";
3270+
const curWorkspace = memory.workspace_scope ?? "";
3271+
const nextAgent = agentScope.trim();
3272+
const nextWorkspace = workspaceScope.trim();
3273+
const agentChanged = nextAgent !== curAgent;
3274+
const workspaceChanged = nextWorkspace !== curWorkspace;
3275+
if (agentChanged) patch.agent_scope = nextAgent || null;
3276+
if (workspaceChanged) patch.workspace_scope = nextWorkspace || null;
3277+
// 放宽可见性(标注→全局)必须显式确认(issue #170 的安全阀):
3278+
// 第一次点保存只弹确认,再次点「确认放宽」才真正落库。
3279+
const widening = (curAgent && agentChanged && !nextAgent)
3280+
|| (curWorkspace && workspaceChanged && !nextWorkspace);
3281+
if (widening && !confirmWiden) {
3282+
setConfirmWiden(true);
3283+
return;
3284+
}
3285+
setConfirmWiden(false);
3286+
postUpdate(patch, {});
32383287
};
32393288

32403289
return h("aside", { className: "mneme-drawer", "aria-label": t("memory.explorer.detail") },
@@ -3278,12 +3327,40 @@ window.__ModuleLoader__.load({
32783327
memory.occurred_at && h(react.Fragment, null,
32793328
h("span", { className: "mneme-dmetakey" }, t("memory.explorer.detail.occurred")),
32803329
h("span", { className: "mneme-dmetaval", title: memory.occurred_at }, formatDateShort(memory.occurred_at))),
3281-
memory.agent_scope && h(react.Fragment, null,
3330+
// v0.8.1 底座:scope 行双态——查看态展示归属 + 来源徽注(显式声明的
3331+
// 「全局」也渲染,不再因 NULL 而隐身);编辑态为文本框(留空=全局)。
3332+
(editing || memory.agent_scope || memory.agent_scope_source === "explicit") && h(react.Fragment, null,
32823333
h("span", { className: "mneme-dmetakey" }, t("memory.explorer.detail.agentScope")),
3283-
h("span", { className: "mneme-dmetaval", title: memory.agent_scope }, memory.agent_scope)),
3284-
memory.workspace_scope && h(react.Fragment, null,
3334+
editing
3335+
? h("input", {
3336+
type: "text",
3337+
className: "mneme-select",
3338+
style: { height: 26, fontSize: 12, justifySelf: "start", width: "100%" },
3339+
value: agentScope,
3340+
placeholder: t("memory.explorer.detail.scopeGlobal"),
3341+
title: t("memory.explorer.detail.scopeEditHint"),
3342+
onChange: (e) => setAgentScope(e.target.value)
3343+
})
3344+
: h("span", {
3345+
className: "mneme-dmetaval",
3346+
title: scopeProvenanceTitle(t, memory.agent_scope_source, memory.scope_decided_at) || memory.agent_scope
3347+
}, (memory.agent_scope ?? t("memory.explorer.detail.scopeGlobal")) + scopeSourceSuffix(t, memory.agent_scope_source))),
3348+
(editing || memory.workspace_scope || memory.workspace_scope_source === "explicit") && h(react.Fragment, null,
32853349
h("span", { className: "mneme-dmetakey" }, t("memory.explorer.detail.workspaceScope")),
3286-
h("span", { className: "mneme-dmetaval", title: memory.workspace_scope }, memory.workspace_scope)),
3350+
editing
3351+
? h("input", {
3352+
type: "text",
3353+
className: "mneme-select",
3354+
style: { height: 26, fontSize: 12, justifySelf: "start", width: "100%" },
3355+
value: workspaceScope,
3356+
placeholder: t("memory.explorer.detail.scopeGlobal"),
3357+
title: t("memory.explorer.detail.scopeEditHint"),
3358+
onChange: (e) => setWorkspaceScope(e.target.value)
3359+
})
3360+
: h("span", {
3361+
className: "mneme-dmetaval",
3362+
title: scopeProvenanceTitle(t, memory.workspace_scope_source, memory.scope_decided_at) || memory.workspace_scope
3363+
}, (memory.workspace_scope ?? t("memory.explorer.detail.scopeGlobal")) + scopeSourceSuffix(t, memory.workspace_scope_source))),
32873364
memory.sensitivity && h(react.Fragment, null,
32883365
h("span", { className: "mneme-dmetakey" }, t("memory.explorer.detail.sensitivity")),
32893366
h("span", { className: "mneme-dmetaval", title: memory.sensitivity }, memory.sensitivity)),
@@ -3316,7 +3393,16 @@ window.__ModuleLoader__.load({
33163393
h("div", { className: "mneme-dactions" },
33173394
editing
33183395
? h(react.Fragment, null,
3319-
h("button", { type: "button", className: "mneme-btn", disabled: busy, onClick: save }, t("memory.explorer.detail.save")),
3396+
confirmWiden
3397+
? h(react.Fragment, null,
3398+
h("span", { style: { fontSize: 12, color: "var(--dsw-alias-state-warn,#b8860b)" } },
3399+
t("memory.explorer.detail.scopeWidenHint")),
3400+
h("button", { type: "button", className: "mneme-btn mneme-btndangerconfirm", disabled: busy, onClick: save },
3401+
t("memory.explorer.detail.scopeWidenConfirm")),
3402+
h("button", { type: "button", className: "mneme-btn", disabled: busy, onClick: () => setConfirmWiden(false) },
3403+
t("memory.explorer.cancel"))
3404+
)
3405+
: h("button", { type: "button", className: "mneme-btn", disabled: busy, onClick: save }, t("memory.explorer.detail.save")),
33203406
h("button", {
33213407
type: "button",
33223408
className: "mneme-btn",
@@ -3326,6 +3412,9 @@ window.__ModuleLoader__.load({
33263412
setTitle(memory.title || "");
33273413
setContent(memory.content || "");
33283414
setImportance(memory.importance || 3);
3415+
setAgentScope(memory.agent_scope || "");
3416+
setWorkspaceScope(memory.workspace_scope || "");
3417+
setConfirmWiden(false);
33293418
}
33303419
}, t("memory.explorer.detail.cancel")),
33313420
savedTick && h("span", { className: "mneme-saved" }, t("memory.explorer.detail.saved")),

‎dsh-mneme/lib/scope.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,24 @@ export function createScopeResolver({ ctx, config, logger } = {}) {
8181
return { agent_scope: agentScope, workspace_scope: workspaceScope };
8282
};
8383
}
84+
85+
/**
86+
* v0.8.1 底座(issue #170):显式 scope 参数(memory_save / memory_update /
87+
* 面板编辑)的归一化。约定:
88+
* - 省略(undefined,由调用方判断)= 该维不声明,回落载体自动标注;
89+
* - "global"(大小写不敏感)/ "*" / 空白 = 显式声明全局 → 存储 NULL
90+
* (行上的 agent_scope_source='explicit' 承担「显式全局」与「从未标注」
91+
* 的区分,NULL 本身继续表示全局可见);
92+
* - 其余非空 trim 字符串 = 收窄到该标签。
93+
* 脏输入(非字符串)落 NULL(等同声明全局)——与 resolveWriteScope 的「解析
94+
* 绝不阻塞写入」同一哲学:显式声明是用户意图,形状不对时宁可放宽不报错。
95+
*
96+
* @param {unknown} raw 工具/HTTP 参数原值
97+
* @returns {string|null} 归一化后的标签值(null=显式全局)
98+
*/
99+
export function normalizeExplicitScope(raw) {
100+
if (typeof raw !== "string") return null;
101+
const s = raw.trim();
102+
if (!s || s === "*" || s.toLowerCase() === "global") return null;
103+
return s;
104+
}

0 commit comments

Comments
 (0)