@restregular/dsh-memory 0.22.3 → 0.23.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/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  本项目的版本记录。安装 id 与 npm 版本一一对应。
4
4
 
5
+ ## 0.23.1 — 默认作用域显示为已绑定;绑定保存后可恢复
6
+
7
+ - **修复「绑定后重开又变成未绑定」的观感与保存范围**:
8
+ - 默认可见范围自带的 **会话 + 项目 + global** 现在由 provider 的 `defaultScopes()` 明确返回,面板把它们显示为「绑定已开」且不可取消(`defaultBound`),只有额外作用域才需要点「绑定」。这解决了「默认的那几个作用域本来就是默认可见,却显示成未绑定」的困惑。
9
+ - 客户端保存时**只提交额外绑定**(`bound && !defaultBound && !session:`),不会把默认作用域或会话作用域写进 `bindings`;服务端仍拒绝会话作用域绑定。
10
+ - `GET /memory/api/visibility` 新增 `defaultScopes` 与每个目录项的 `defaultBound`;`bindings` 仍是显式绑定的额外作用域。旧服务端没有 `defaultScopes` 时客户端按会话 / 项目 / global 兜底。
11
+ - **注意(本次「保存后又变未绑定」的根因)**:0.23.0 安装后,客户端 bundle 已更新(所以能看到绑定按钮),但宿主进程仍是 14:05 启动时加载的旧服务端代码,旧 `POST /visibility` 会忽略 `bindings`,只保存会话可见范围。**升级到 0.23.1 后需要重启一次 DSH**(或让插件管理器真正重载服务端),绑定才会落盘到 `<storeDir>/bindings/<hash>.json`;之后重开面板会从 `GET /visibility` 恢复。
12
+ - 测试:默认 / 额外绑定的服务端载荷、客户端按钮默认锁定、只提交额外绑定、`defaultScopes` 不包含额外绑定。全套通过。
13
+
14
+ ## 0.23.0 — 修正版编辑与记录页筛选;作用域绑定 / 可读 / 可写状态
15
+
16
+ - **编辑记忆 = 新建修正版(不再原地修改)**:
17
+ - 面板 `POST /memory/api/update` 改为调用 provider 的 `reviseRecord()`:新建一条记录、把 `supersedes` 指向旧记录,旧记录内容与元数据保持原样。
18
+ - 内容、类型、标签、重要度、触发词、过期时间都按提交值写入新记录;内容为空 / 超长 / 命中凭据扫描时的行为与 `memory_save` 一致。
19
+ - 返回 `{ record, superseded }`;`/records` 反查 `supersededBy`,旧记录仍可 `memory_get` / 面板历史查看,检索默认隐藏。
20
+ - **记录页「是否被修正」筛选**:默认「隐藏已修正」;可切到「显示全部」「只看已修正」。审计 / 审批跳转过来的目标记录即使被隐藏也会补一张卡并展开。
21
+ - **每个作用域的【绑定 / 可读 / 可写】状态按钮**:
22
+ - **绑定**是工作区级:写入 `<storeDir>/bindings/<hash>.json`;同一工作区里没有显式覆盖的会话,默认范围额外包含已绑定作用域(`resolveReadScopes` / `visibilityPolicy` 同口径)。会话作用域不可绑定。
23
+ - **可读 / 可写**是会话级、独立生效:`read:false` 的作用域不进入读取集合,搜索命中 / `memory_get` 返回 `withheld.reason='read-disabled'` 且不会发起审批;`write:false` 的保存 / 删除直接拒绝并返回 `SCOPE_READ_ONLY`。
24
+ - 状态按钮与 `Scope visibility` 一起提交:`visibility.scopes[{ scope, read?, write? }]` + `bindings: string[]`;只落 `false`,旧文件 / 旧客户端默认都可读可写。
25
+ - **失败回复讲真实原因**:绝对限制下被挡住的读取 / 写入 / 删除不再伪装成「不存在」。
26
+ - 读:`memory_search` / `memory_list` / `memory_get` 返回 `withheld.reason='absolute'`;被挡住的 id 不再进 `missing`,渲染层提示「绝对限制,不会发起审批」及调整方式。
27
+ - 写 / 删:工具返回 `VISIBILITY_ABSOLUTE`(绝对限制)或 `SCOPE_READ_ONLY`(可写开关关闭);普通限制的拒绝 / 超时 / 无法审批保持原有真实原因。
28
+ - **测试**:provider 的 read/write 开关、工作区绑定持久化与默认范围、`reviseRecord` 修正链、`/update` 旧记录保留、记录页筛选、状态按钮与 POST 载荷、绝对限制 `withheld` / 删除错误原因。全套通过(441 tests / 0 fail)。
29
+
5
30
  ## 0.22.3 — 审批卡高度与宿主同口径,矮窗口也给列表留空间
6
31
 
7
32
  - 0.22.2 把详情行锁成 `flex:none` 后,矮窗口里 `max-height: min(--dsh-composer-text-max-height, 46vh)` 那层 `46vh` 会让头部占满高度、条目列表只剩十几像素(437px 高窗口实测 ~18px)。
package/README.md CHANGED
@@ -45,12 +45,13 @@ JSONL 落盘 · 本机向量检索 · 五个模型工具 · 自动召回 · 硬
45
45
  | **自动召回** | 每轮按用户问题召回;新会话第一步注入项目 / global 摘要;发布、部署、工具失败等行为按标签触发定向召回 |
46
46
  | **印象形态** | 每轮只给每条记忆的短摘要;模型判断相关但需要细节时再 `memory_get` 取全文,省 token 也不丢信息 |
47
47
  | **硬性规则** | `kind: rule` 进系统提示常驻段;行首带 8 位 id,注入的是摘要,需要全文时用 `memory_get`;旧会话会收到新增 / 编辑提醒;超预算有明确警告,不静默丢弃 |
48
- | **会话可见域** | 输入框旁的「记忆范围」按钮设置本会话可读的作用域 + 限制等级(绝对 / 普通 / 无限制);Agent 及其子 Agent 都按它检索、注入与 `memory_get` |
48
+ | **会话可见域** | 输入框旁的「记忆范围」按钮设置本会话可读的作用域 + 限制等级(绝对 / 普通 / 无限制);每个作用域还有**绑定 / 可读 / 可写**状态按钮——绑定到当前工作区后新会话默认可见,可读 / 可写独立影响工具读写;Agent 及其子 Agent 都按它检索、注入与 `memory_get` |
49
+ | **修正版编辑** | 面板里编辑一条记忆 = 新建一条修正版并通过 `supersedes` 指向旧记录,旧记录不原地修改;记录页「是否被修正」筛选默认隐藏旧版本,可切到全部 / 只看已修正 |
49
50
  | **范围外审批** | 普通限制下,工具命中可见域外的记忆时会在**对应会话的输入框上方**出现审批卡(样式与系统权限审批一致):展示工具、Agent 说明、查询关键词与范围外记忆摘要,支持逐条勾选 / 全选 / 取消全选(「允许选中的 N 条」或整单「允许一次」)或「拒绝」;点记忆 ID 直接跳到面板「记录」页并展开该条 |
50
51
  | **上下文去重** | `memory_search` / `memory_list` 返回过的条目记进会话上下文状态;同一版本再次命中只回记忆 ID(`memory_search` 的裸 ID 与带摘要条目之间空一行),并提示用 `memory_get` 取全文(内容更新后重新给摘要) |
51
52
  | **状态记忆** | `kind: state` 默认 7 天 TTL;注入带「状态,使用前验证」;过期后默认不进检索 / 摘要 / 召回,但 `memory_get` 与面板仍可取 |
52
53
  | **流程索引** | `kind: procedure` 的目录进系统提示:按当前工作区 + 触发词(工具名 / 关键词)列 `id · 触发词 → 标题`,需要时再用 `memory_get` 取全文 |
53
- | **可视化管理** | 记录浏览 / 搜索 / 新建 / 编辑 / 删除;**记录页可主动做语义检索**(与 `memory_search` 同一套排序);记忆树展示作用域、类型与修正链;设置改完即存;导入导出 |
54
+ | **可视化管理** | 记录浏览 / 搜索 / 新建 / 编辑 / 删除(编辑生成修正版);**记录页可主动做语义检索**(与 `memory_search` 同一套排序),并有「是否被修正」筛选;记忆树展示作用域、类型与修正链;设置改完即存;导入导出 |
54
55
  | **一键向量后端** | 面板里下载 llama.cpp 运行时与内置模型,按 CPU / 内存 / 显卡推荐;换模型自动重建向量 |
55
56
  | **跨进程安全** | 每个作用域一把写锁,锁内重新读盘再合并;多个 DSH 实例或 MCP server 共用一个 store 不丢数据 |
56
57
 
@@ -87,20 +88,21 @@ JSONL 落盘 · 本机向量检索 · 五个模型工具 · 自动召回 · 硬
87
88
 
88
89
  输入框工具行右侧有「记忆范围」按钮。**打开会话时就会读取这个会话的设置**,按钮上的数字是当前生效的作用域数量,边框样式表示限制等级(绝对限制 = 橙黄实线 / 普通限制 = 蓝色实线 / 无限制 = 灰色虚线;`title` 里写明「已设置 / 使用默认 / 继承父会话 + 数量 + 等级」)。点开的浮窗使用毛玻璃背景,按当前会话列出可选作用域(当前会话 / 当前项目 / global / 已有自定义作用域):
89
90
 
90
- - **默认范围**:没有显式设置时(新会话)按默认设置生效 —— **当前会话 + 当前项目 + global** 三个作用域 + **普通限制**;面板按同一份默认范围预选,不会出现空面板。
91
+ - **默认范围**:没有显式设置时(新会话)按默认设置生效 —— **当前会话 + 当前项目 + global + 已绑定的作用域** + **普通限制**;面板按同一份默认范围预选,不会出现空面板。
91
92
  - **搜索与批量**:顶部可搜索作用域,支持「全选 / 取消全选」;列表最大高度受限,作用域多时在浮窗内滚动。
93
+ - **状态按钮(0.23.0 / 0.23.1)**:作用域列表每行有【绑定】、【可读】|【可写】。**默认范围自带的会话 + 项目 + global 显示为已绑定且不可取消**;**绑定**按钮用于把其它作用域额外并入当前工作区的默认范围(没有显式覆盖的新会话可见);**可读 / 可写**是会话级,独立控制该作用域的读取与写入(关掉可读 → 搜索 / 读取跳过并说明原因;关掉可写 → 保存 / 删除被拒绝并说明原因)。
92
94
  - **限制等级**:
93
- - **绝对限制**:可见域外的记忆一律查不到(`memory_get` 按 id 取也取不到,`memory_search` / `memory_list` 不返回、也不告诉模型"被挡住");范围外也写不进去,删除范围外记录按"不存在"处理。
95
+ - **绝对限制**:可见域外的读取 / 写入 / 删除一律不执行;命中被挡住时会明确告诉 Agent 是「绝对限制」以及怎么调整(不再假装记忆不存在)。
94
96
  - **普通限制**(默认):可见域内正常返回;命中范围外记忆时,工具会暂停并在**该会话**的输入框上方显示审批卡,展示 Agent 的查询关键词与范围外记忆摘要;读审批可以逐条勾选,只允许其中一条或多条。用户拒绝 / 5 分钟未回答时,工具结果里会明确写出真实原因(「用户拒绝了本次访问」/「审批超时」),模型不会误以为记忆不存在。范围外写入 / 删除同样走审批。
95
97
  - **无限制**:不启用范围白名单,可以读写任意作用域(选择仍会保存,供下次切换)。
96
98
  - **不再按记忆类型收窄**:0.19.1 起只按作用域限制;旧配置里的 `kinds` 会被忽略,同一作用域下的事实 / 规则 / 流程等都可读。
97
99
  - **审批卡(0.21.0 / 0.22.x)**:注册在会话内的 `conversation.input.dock`,**只在发起审批的那个会话里显示**,切到别的会话看不到;视觉与系统自带的权限审批弹窗一致(警告色 strip、卡片、右对齐动作行,同一批主题 token)。按动作分为「记忆读取 / 写入 / 删除审批」。读审批默认全部勾选:可逐条取消、用「全选 / 取消全选」批量操作,**点条目任意位置即可切换选中**,再点「允许选中的 N 条」,工具只放行勾选的 id、其余写入 `withheld.reason='partial'`;全选时等价于原来的「允许一次」。strip 右侧可**折叠 / 展开**详情,折叠后仍保留「拒绝 / 允许」动作。写 / 删审批是单条,隐藏勾选框。**0.22.0 起**卡片展示工具调用的 `description`(普通限制下工具层强制模型提供),点记忆 ID 会打开记忆面板的「记录」标签页、选中并滚动到该条、自动展开;**0.22.2 起**查询与说明固定显示在条目列表上方,条目再多也不会被列表挤没(两行锁 `flex:none`,只有列表滚动);**0.22.3 起**审批卡高度只受 composer 文本区上限约束(与宿主审批卡同口径),矮窗口下列表也有空间。拒绝、超时或无法发起审批都会把原因告诉模型。Enter = 允许、Esc = 拒绝(焦点在卡片内时)。
98
100
  - 子 Agent 自动继承父会话 / 根会话的设置;在子 Agent 自己的会话里再设置,只影响那一个会话。
99
101
  - 保存后,下一次 Agent 步骤会通过注入的上下文消息收到「可见域已更新」通知(包含可见范围与限制等级),不需要重启会话。
100
- - 「恢复默认」清除覆盖,回到默认设置(会话 + 项目 + global + 普通限制)。
102
+ - 「恢复默认」清除覆盖,回到默认设置(会话 + 项目 + global + 已绑定的作用域 + 普通限制)。
101
103
  - 覆盖按会话落盘在 `<storeDir>/visibility/<session>.json`,浏览器刷新 / 插件重载后仍在;0.18 的旧文件按「绝对限制」兼容读取。
102
104
  - 浮窗与审批卡的半透明背景、边框、文字颜色全部走宿主主题 token;勾选框选中态、保存按钮、限制等级选中态使用 `--dsw-alias-state-business-primary` + `--dsw-alias-label-primary-foreground`,深色模式下不会出现白底白勾 / 白底白字;按钮三档边框用 `state-warn-primary`(橙黄)/ `state-business-primary`(蓝)/ 虚线灰;阴影已收敛为小范围投影,滚动列表高度收紧到 `min(220px, 32vh)`。
103
- - **可见范围 = 可操作范围(0.20.0 起)**:`memory_search` / `memory_list` / `memory_get` 与 `memory_save` / `memory_forget` 共用同一份可见域;面板管理页仍是用户本人的操作,不受会话语义限制。
105
+ - **可见范围 = 可操作范围(0.20.0 起,0.23.0 拆分读写)**:`memory_search` / `memory_list` / `memory_get` 与 `memory_save` / `memory_forget` 共用同一份可见域;0.23.0 起每个作用域的「可读 / 可写」状态独立生效,失败原因会写进工具回复。面板管理页仍是用户本人的操作,不受会话语义限制。
104
106
 
105
107
  ### 向量服务
106
108
 
package/lib/client.js CHANGED
@@ -530,6 +530,16 @@ window.__ModuleLoader__.load({
530
530
  '.mem-scope-label{flex:1;min-width:0;padding:0;border:none;background:transparent;color:var(--dsw-alias-label-primary);font-size:12.5px;text-align:left;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;cursor:pointer}',
531
531
  '.mem-scope-label:hover{color:var(--dsw-alias-brand-primary);border:none}',
532
532
  '.mem-scope-row .cnt{flex:none;color:var(--dsw-alias-label-secondary);font-size:10.5px}',
533
+ // 0.23.0:每个作用域的状态按钮行 —— 绑定 | 可读 | 可写。
534
+ '.mem-scope-state-row{display:flex;align-items:center;gap:4px;padding-left:26px}',
535
+ '.mem-scope-state{padding:1px 7px;border:1px solid var(--dsw-alias-border-l1);border-radius:6px;background:transparent;color:var(--dsw-alias-label-secondary);font-size:10.5px;line-height:1.65;cursor:pointer;transition:border-color .12s ease,background .12s ease,color .12s ease}',
536
+ '.mem-scope-state:hover:not(:disabled){border-color:color-mix(in srgb,var(--dsw-alias-brand-primary) 45%,var(--dsw-alias-border-l1));color:var(--dsw-alias-label-primary)}',
537
+ '.mem-scope-state.on{border-color:color-mix(in srgb,var(--dsw-alias-state-business-primary) 58%,var(--dsw-alias-border-l1));background:color-mix(in srgb,var(--dsw-alias-state-business-primary) 14%,transparent);color:var(--dsw-alias-state-business-primary)}',
538
+ '.mem-scope-state.bind.on{border-style:dashed}',
539
+ '.mem-scope-state.bind.locked{opacity:.82;cursor:default}',
540
+ '.mem-scope-state:disabled{opacity:.42;cursor:default}',
541
+ '.mem-scope-state:focus-visible{outline:1px solid var(--dsw-focus-ring-color,var(--dsw-alias-state-business-primary));outline-offset:1px}',
542
+ '.mem-scope-sep{width:1px;height:12px;margin:0 1px;background:var(--dsw-alias-border-l1);flex:none}',
533
543
  '.mem-scope-foot{display:flex;align-items:center;gap:8px}',
534
544
  '.mem-scope-foot .spacer{flex:1}',
535
545
  '.mem-scope-reset{padding:6px 13px;border:1px solid var(--dsw-alias-border-l1);border-radius:9px;background:transparent;color:var(--dsw-alias-label-secondary);font-size:12px;cursor:pointer;transition:border-color .12s ease,color .12s ease}',
@@ -1049,9 +1059,10 @@ window.__ModuleLoader__.load({
1049
1059
  : null,
1050
1060
  ),
1051
1061
  h('div', { className: 'acts' },
1052
- h('button', { disabled: busy, onClick: save }, busy ? '保存中…' : '保存'),
1062
+ h('button', { disabled: busy, onClick: save }, busy ? '保存中…' : '保存修正版'),
1053
1063
  h('button', { disabled: busy, onClick: cancelEdit }, '取消'),
1054
1064
  ),
1065
+ h('div', { className: 'hint' }, '保存会新建一条修正版(supersedes 当前记录),旧记录保留;记录页默认隐藏已被修正的旧版本。'),
1055
1066
  )
1056
1067
  : h('div', null,
1057
1068
  h('div', { className: 'content' }, rec.content),
@@ -3413,6 +3424,9 @@ window.__ModuleLoader__.load({
3413
3424
  const [installRequested, setInstallRequested] = React.useState(false)
3414
3425
  const [dangerAsk, setDangerAsk] = React.useState('')
3415
3426
  const [scopeFilter, setScopeFilter] = React.useState('')
3427
+ // 0.23.0:记录页「是否被修正」筛选。默认隐藏已被 supersede 的旧记录,
3428
+ // 但历史仍可切到「显示全部 / 只看已修正」查看。
3429
+ const [supersededFilter, setSupersededFilter] = React.useState('hide')
3416
3430
  const [q, setQ] = React.useState('')
3417
3431
  // 语义检索:默认关(记录页原本是"关键词过滤");开启后搜索走 /search,
3418
3432
  // 由 provider 用向量 + 词法同一套排序,列表按相关度排。
@@ -3688,7 +3702,7 @@ window.__ModuleLoader__.load({
3688
3702
  notice = h('div', { className: 'hint err' }, `安装失败:${install.error || install.message}`)
3689
3703
  }
3690
3704
 
3691
- const filtering = q.trim() !== '' || scopeFilter !== ''
3705
+ const filtering = q.trim() !== '' || scopeFilter !== '' || supersededFilter !== 'hide'
3692
3706
 
3693
3707
  // 三个标签的内容分开构建:记录(概览) / 数据(列表) / 设置。
3694
3708
  const statsCard = h('div', { className: 'stats' },
@@ -3807,9 +3821,21 @@ window.__ModuleLoader__.load({
3807
3821
  disabled: loading,
3808
3822
  onChange: (next) => { setScopeFilter(next); load(q, next) },
3809
3823
  }),
3824
+ // 0.23.0:是否被修正。默认隐藏旧版本;要看历史关系时切到另外两档。
3825
+ h(Dropdown, {
3826
+ items: [
3827
+ { value: 'hide', label: '隐藏已修正' },
3828
+ { value: 'all', label: '显示全部' },
3829
+ { value: 'only', label: '只看已修正' },
3830
+ ],
3831
+ value: supersededFilter,
3832
+ disabled: loading,
3833
+ title: '是否被修正:默认隐藏已被新版本取代的旧记录',
3834
+ onChange: setSupersededFilter,
3835
+ }),
3810
3836
  h('button', { onClick: () => setCreating((v) => !v), disabled: loading }, creating ? '收起' : '新建'),
3811
3837
  h('button', { onClick: () => load(q, scopeFilter), disabled: loading }, loading ? '刷新中…' : '刷新'),
3812
- filtering ? h('button', { className: 'linkbtn', onClick: () => { setQ(''); setScopeFilter(''); setSearchInfo(null); load('', '', semantic) } }, '清空筛选') : null,
3838
+ filtering ? h('button', { className: 'linkbtn', onClick: () => { setQ(''); setScopeFilter(''); setSupersededFilter('hide'); setSearchInfo(null); load('', '', semantic) } }, '清空筛选') : null,
3813
3839
  )
3814
3840
  // 语义检索结果的出处:真跑了向量,还是因为服务/向量缺失退回了词法 —— 明说,
3815
3841
  // 不把词法结果冒充成"语义找到的"。
@@ -3848,11 +3874,18 @@ window.__ModuleLoader__.load({
3848
3874
  })
3849
3875
  : null
3850
3876
  const emptyText = searchInfo ? '没有语义相关的记忆' : filtering ? '没有匹配的记忆' : '还没有记忆'
3851
- // 从审计跳过来的目标记录如果不在当前加载结果里,先把它补到列表最前面,
3877
+ // 0.23.0:「是否被修正」筛选。默认隐藏已被 supersede 的旧记录,
3878
+ // 历史核对时可切到「显示全部 / 只看已修正」。
3879
+ const supersededRecords = supersededFilter === 'all'
3880
+ ? records
3881
+ : supersededFilter === 'only'
3882
+ ? records.filter((rec) => Boolean(rec.supersededBy))
3883
+ : records.filter((rec) => !rec.supersededBy)
3884
+ // 从审计跳过来的目标记录如果不在当前筛选结果里,先把它补到列表最前面,
3852
3885
  // 否则跳转后列表里根本没有这张卡,focus 自然也无法展开。
3853
- const displayRecords = focusRecordData !== null && !records.some((rec) => rec.id === focusRecordData.id)
3854
- ? [focusRecordData, ...records]
3855
- : records
3886
+ const displayRecords = focusRecordData !== null && !supersededRecords.some((rec) => rec.id === focusRecordData.id)
3887
+ ? [focusRecordData, ...supersededRecords]
3888
+ : supersededRecords
3856
3889
  const listBody = !loading && !error && displayRecords.length === 0
3857
3890
  ? h('div', { className: 'empty' },
3858
3891
  emptyText,
@@ -4514,8 +4547,14 @@ window.__ModuleLoader__.load({
4514
4547
  * - 不再按记忆类型收窄,只选作用域。
4515
4548
  *
4516
4549
  * 读写走 /memory/api/visibility:
4517
- * GET ?session=<id> → { override, effective, inherited, defaults, scopes }
4518
- * POST { session, visibility: { scopes: [{ scope }], level } | null } → 同上
4550
+ * GET ?session=<id> → { override, effective, inherited, defaults, workspace, bindings, scopes }
4551
+ * POST { session, visibility: { scopes: [{ scope, read?, write? }], level } | null,
4552
+ * bindings: string[] } → 同上
4553
+ *
4554
+ * 0.23.0:
4555
+ * - 每个作用域有「绑定 / 可读 / 可写」状态按钮;绑定是工作区级(新会话默认可见),
4556
+ * 可读 / 可写是会话级,直接影响工具读到 / 写到该作用域时的结果与失败原因;
4557
+ * - 保存时 read/write 只在关掉时提交 false,绑定整体提交。
4519
4558
  */
4520
4559
  function MemoryVisibilityButton({ sessionId } = {}) {
4521
4560
  const [open, setOpen] = React.useState(false)
@@ -4525,6 +4564,9 @@ window.__ModuleLoader__.load({
4525
4564
  const [data, setData] = React.useState(null)
4526
4565
  const [level, setLevel] = React.useState('normal')
4527
4566
  const [entries, setEntries] = React.useState({})
4567
+ const [perms, setPerms] = React.useState({})
4568
+ const [bound, setBound] = React.useState({})
4569
+ const [defaultBound, setDefaultBound] = React.useState({})
4528
4570
  const [catalog, setCatalog] = React.useState([])
4529
4571
  const [search, setSearch] = React.useState('')
4530
4572
  const [anchor, setAnchor] = React.useState(null)
@@ -4591,8 +4633,42 @@ window.__ModuleLoader__.load({
4591
4633
  }
4592
4634
  const nextEntries = {}
4593
4635
  for (const item of nextCatalog) nextEntries[item.scope] = selected.has(item.scope)
4636
+ // 0.23.0:每个作用域的 read / write 状态 + 工作区绑定。
4637
+ const ruleByScope = new Map()
4638
+ for (const rule of selectedRules) {
4639
+ const scope = typeof rule === 'string' ? rule : rule && typeof rule.scope === 'string' ? rule.scope : ''
4640
+ if (scope !== '' && !ruleByScope.has(scope)) ruleByScope.set(scope, rule)
4641
+ }
4642
+ const boundSet = new Set(
4643
+ (Array.isArray(value.bindings) ? value.bindings : [])
4644
+ .filter((scope) => typeof scope === 'string' && scope !== ''),
4645
+ )
4646
+ // 默认基础范围(会话 + 项目 + global)天然包含在默认范围里:展示为已绑定、
4647
+ // 不可取消;只有额外绑定才进 bound 状态、才会随保存提交。
4648
+ const defaultSet = new Set(
4649
+ (Array.isArray(value.defaultScopes)
4650
+ ? value.defaultScopes
4651
+ : (Array.isArray(value.defaults) ? value.defaults : []).filter((scope) => typeof scope === 'string'
4652
+ && (scope === 'global' || scope.startsWith('session:') || scope.startsWith('project:'))))
4653
+ .filter((scope) => typeof scope === 'string' && scope !== ''),
4654
+ )
4655
+ const nextPerms = {}
4656
+ const nextBound = {}
4657
+ const nextDefaultBound = {}
4658
+ for (const item of nextCatalog) {
4659
+ const rule = ruleByScope.get(item.scope)
4660
+ nextPerms[item.scope] = {
4661
+ read: rule ? rule.read !== false : item.read !== false,
4662
+ write: rule ? rule.write !== false : item.write !== false,
4663
+ }
4664
+ nextBound[item.scope] = boundSet.has(item.scope)
4665
+ nextDefaultBound[item.scope] = defaultSet.has(item.scope) || item.defaultBound === true
4666
+ }
4594
4667
  setCatalog(nextCatalog)
4595
4668
  setEntries(nextEntries)
4669
+ setPerms(nextPerms)
4670
+ setBound(nextBound)
4671
+ setDefaultBound(nextDefaultBound)
4596
4672
  }
4597
4673
 
4598
4674
  const load = () => {
@@ -4629,6 +4705,19 @@ window.__ModuleLoader__.load({
4629
4705
  setEntries((prev) => ({ ...prev, [scope]: !prev[scope] }))
4630
4706
  }
4631
4707
 
4708
+ /** 切换某个作用域的可读 / 可写状态(会话级)。 */
4709
+ const togglePerm = (scope, key) => {
4710
+ setPerms((prev) => {
4711
+ const current = prev[scope] ?? { read: true, write: true }
4712
+ return { ...prev, [scope]: { ...current, [key]: !current[key] } }
4713
+ })
4714
+ }
4715
+
4716
+ /** 切换某个作用域是否绑定到当前工作区(工作区级,新会话默认可见)。 */
4717
+ const toggleBound = (scope) => {
4718
+ setBound((prev) => ({ ...prev, [scope]: !prev[scope] }))
4719
+ }
4720
+
4632
4721
  const selectAll = () => {
4633
4722
  setEntries((prev) => {
4634
4723
  const next = { ...prev }
@@ -4648,7 +4737,11 @@ window.__ModuleLoader__.load({
4648
4737
  const save = (config) => {
4649
4738
  setBusy(true)
4650
4739
  setError('')
4651
- api('visibility', { session: sessionId, visibility: config })
4740
+ // 只提交**额外**绑定:默认范围自带的会话 / 项目 / global 不需要也不允许写入。
4741
+ const bindings = catalog
4742
+ .filter((item) => bound[item.scope] && !defaultBound[item.scope] && !String(item.scope).startsWith('session:'))
4743
+ .map((item) => item.scope)
4744
+ api('visibility', { session: sessionId, visibility: config, bindings })
4652
4745
  .then((value) => {
4653
4746
  applyPayload(value)
4654
4747
  setOpen(false)
@@ -4658,7 +4751,11 @@ window.__ModuleLoader__.load({
4658
4751
  }
4659
4752
 
4660
4753
  const saveDraft = () => {
4661
- const scopes = catalog.filter((item) => entries[item.scope]).map((item) => ({ scope: item.scope }))
4754
+ const scopes = catalog.filter((item) => entries[item.scope]).map((item) => ({
4755
+ scope: item.scope,
4756
+ read: perms[item.scope]?.read !== false,
4757
+ write: perms[item.scope]?.write !== false,
4758
+ }))
4662
4759
  if (scopes.length === 0) { setError('至少选择一个可见范围'); return }
4663
4760
  save({ scopes, level })
4664
4761
  }
@@ -4779,6 +4876,42 @@ window.__ModuleLoader__.load({
4779
4876
  }, item.label || item.scope),
4780
4877
  h('span', { className: 'cnt' }, `${item.records ?? 0}`),
4781
4878
  ),
4879
+ // 0.23.0:状态按钮 —— 绑定(工作区级) / 可读 | 可写(会话级)。
4880
+ h('div', { className: 'mem-scope-state-row' },
4881
+ h('button', {
4882
+ type: 'button',
4883
+ className: `mem-scope-state bind${defaultBound[item.scope] || bound[item.scope] ? ' on' : ''}${defaultBound[item.scope] ? ' locked' : ''}`,
4884
+ 'aria-pressed': Boolean(defaultBound[item.scope] || bound[item.scope]),
4885
+ disabled: Boolean(defaultBound[item.scope]),
4886
+ title: defaultBound[item.scope]
4887
+ ? '默认可见范围已包含(会话 + 项目 + global 默认绑定),无需再绑定'
4888
+ : (bound[item.scope]
4889
+ ? '已绑定到当前工作区:本工作区新会话的默认可见范围会包含它'
4890
+ : '绑定到当前工作区:本工作区新会话的默认可见范围会包含它'),
4891
+ onClick: () => { if (!defaultBound[item.scope]) toggleBound(item.scope) },
4892
+ }, '绑定'),
4893
+ h('span', { className: 'mem-scope-sep', 'aria-hidden': 'true' }),
4894
+ h('button', {
4895
+ type: 'button',
4896
+ className: `mem-scope-state read${perms[item.scope]?.read !== false ? ' on' : ''}`,
4897
+ 'aria-pressed': perms[item.scope]?.read !== false,
4898
+ disabled: !entries[item.scope],
4899
+ title: entries[item.scope]
4900
+ ? (perms[item.scope]?.read !== false ? '可读:搜索 / 读取会命中该作用域' : '不可读:搜索 / 读取会跳过该作用域')
4901
+ : '先勾选该作用域再调整可读 / 可写',
4902
+ onClick: () => togglePerm(item.scope, 'read'),
4903
+ }, '可读'),
4904
+ h('button', {
4905
+ type: 'button',
4906
+ className: `mem-scope-state write${perms[item.scope]?.write !== false ? ' on' : ''}`,
4907
+ 'aria-pressed': perms[item.scope]?.write !== false,
4908
+ disabled: !entries[item.scope],
4909
+ title: entries[item.scope]
4910
+ ? (perms[item.scope]?.write !== false ? '可写:保存 / 删除会作用到该作用域' : '不可写:保存 / 删除会被拒绝并告知原因')
4911
+ : '先勾选该作用域再调整可读 / 可写',
4912
+ onClick: () => togglePerm(item.scope, 'write'),
4913
+ }, '可写'),
4914
+ ),
4782
4915
  )),
4783
4916
  ),
4784
4917
  error ? h('div', { className: 'mem-scope-err' }, error) : null,
package/lib/panel.js CHANGED
@@ -11,7 +11,8 @@
11
11
  * GET /status 各 scope 记录数 + 待嵌入数 + 展示标签
12
12
  * GET /records ?scope=&q= 枚举/过滤记录
13
13
  * GET /search ?scope=&q=&topK= 语义检索(向量 + 词法,provider 排序)
14
- * POST /update {id, content?, kind?, importance?, tags?, supersededBy?}
14
+ * POST /update {id, content?, kind?, importance?, tags?, triggers?, expiresAt?/ttlMs?}
15
+ * → 新建一条修正版(supersedes=id),旧记录保留(0.23.0)
15
16
  * POST /delete {id}
16
17
  * scope 惰性加载是同步的(store.peek(),**读路径不创建**),无需 ready 门。内容编辑会作废旧
17
18
  * 向量并加入 embedPending,由 provider 的回填队列重新嵌入。
@@ -505,7 +506,7 @@ function visibilityPayload(ctx, memory, sessionId) {
505
506
 
506
507
  let refs
507
508
  try { if (session !== undefined) refs = resolveCurrentScopes({ session }) } catch { refs = undefined }
508
- // 没有显式覆盖时,面板仍预选"当前默认读取集合"(会话 + 项目 + global),
509
+ // 没有显式覆盖时,面板仍预选"当前默认读取集合"(会话 + 项目 + global + 工作区绑定),
509
510
  // 让用户看到 Agent 现在实际能看到什么;保存后才变成显式覆盖。
510
511
  let defaults = []
511
512
  try {
@@ -513,6 +514,22 @@ function visibilityPayload(ctx, memory, sessionId) {
513
514
  if (Array.isArray(resolved)) defaults = resolved
514
515
  } catch { /* 降级为空 */ }
515
516
 
517
+ // 0.23.0:工作区级绑定 + 每个作用域的 read / write 状态。
518
+ let workspace = null
519
+ let bindings = []
520
+ try {
521
+ if (typeof memory.projectKeyFor === 'function') workspace = memory.projectKeyFor(agentLike)
522
+ if (typeof memory.workspaceBindings === 'function') {
523
+ const list = memory.workspaceBindings(agentLike)
524
+ if (Array.isArray(list)) bindings = list
525
+ }
526
+ } catch { /* 绑定是增强;拿不到就按"没有绑定"处理 */ }
527
+ const bound = new Set(bindings)
528
+ const rules = new Map()
529
+ for (const rule of (Array.isArray(effective?.scopes) ? effective.scopes : [])) {
530
+ if (rule && typeof rule.scope === 'string' && !rules.has(rule.scope)) rules.set(rule.scope, rule)
531
+ }
532
+
516
533
  const stats = typeof memory.scopeStats === 'function' ? memory.scopeStats() : []
517
534
  const byScope = new Map()
518
535
  for (const item of Array.isArray(stats) ? stats : []) {
@@ -526,14 +543,38 @@ function visibilityPayload(ctx, memory, sessionId) {
526
543
  }
527
544
  const sessionScope = refs?.session ?? defaults.find((scope) => scope.startsWith('session:'))
528
545
  const projectScope = refs?.project ?? defaults.find((scope) => scope.startsWith('project:'))
546
+ // 默认范围的**基础集合** = 会话 + 项目 + global。它们天然包含在默认范围里,
547
+ // 面板按「已绑定」展示且不可取消;工作区绑定只负责额外并入其它作用域。
548
+ let defaultScopes = []
549
+ try {
550
+ const list = typeof memory.defaultScopes === 'function' ? memory.defaultScopes(agentLike) : undefined
551
+ if (Array.isArray(list)) defaultScopes = list.filter((scope) => typeof scope === 'string' && scope !== '')
552
+ } catch { /* 旧 provider:退回下面的本地面包 */ }
553
+ if (defaultScopes.length === 0) {
554
+ defaultScopes = [sessionScope, projectScope, 'global'].filter((scope) => typeof scope === 'string' && scope !== '')
555
+ }
556
+ const defaultBound = new Set(defaultScopes)
529
557
  ensure(sessionScope)
530
558
  ensure(projectScope)
531
559
  ensure('global')
532
560
  for (const scope of defaults) ensure(scope)
561
+ for (const scope of bindings) ensure(scope)
533
562
  const rank = (scope) => scope === sessionScope ? 0 : scope === projectScope ? 1 : scope === 'global' ? 2 : 3
534
563
  const scopes = [...byScope.values()]
535
564
  .sort((a, b) => rank(a.scope) - rank(b.scope) || String(a.scope).localeCompare(String(b.scope)))
536
- .map((item) => ({ ...item, label: item.name ?? scopeLabel(ctx, item.scope) }))
565
+ .map((item) => {
566
+ const rule = rules.get(item.scope)
567
+ return {
568
+ ...item,
569
+ label: item.name ?? scopeLabel(ctx, item.scope),
570
+ read: rule === undefined ? true : rule.read !== false,
571
+ write: rule === undefined ? true : rule.write !== false,
572
+ // bound = 显式绑定或默认范围自带;defaultBound 告诉客户端这一项不能取消。
573
+ bound: bound.has(item.scope) || defaultBound.has(item.scope),
574
+ defaultBound: defaultBound.has(item.scope),
575
+ bindable: !String(item.scope).startsWith('session:') && !defaultBound.has(item.scope),
576
+ }
577
+ })
537
578
  return {
538
579
  sessionId,
539
580
  override: own,
@@ -541,6 +582,9 @@ function visibilityPayload(ctx, memory, sessionId) {
541
582
  inherited,
542
583
  source,
543
584
  defaults,
585
+ defaultScopes,
586
+ workspace,
587
+ bindings: [...bound],
544
588
  scopes,
545
589
  }
546
590
  }
@@ -633,7 +677,7 @@ async function handle(ctx, routePath, req, res) {
633
677
  if (sessionId === '') throw new ApiError(400, 'SESSION_REQUIRED', 'session 不能为空')
634
678
  let next = null
635
679
  const rawConfig = body?.visibility
636
- // 新协议:{ visibility: { scopes: [{ scope }], level } } | null
680
+ // 新协议:{ visibility: { scopes: [{ scope, read?, write? }], level } } | null
637
681
  // 兼容 0.19.0 的 { scopes: [{ scope, kinds? }] } 与 0.18 的 { scopes: string[] }:
638
682
  // 0.19.1 起不再限制记忆类型,提交里的 kinds 一律忽略。
639
683
  const rawScopes = rawConfig !== null && rawConfig !== undefined && typeof rawConfig === 'object'
@@ -654,12 +698,43 @@ async function handle(ctx, routePath, req, res) {
654
698
  throw new ApiError(400, 'SCOPE_INVALID', `可见范围必须是具体作用域键: ${JSON.stringify(scopeRaw)}`)
655
699
  }
656
700
  if (normalized.scope === undefined || scopes.some((item) => item.scope === normalized.scope)) continue
657
- scopes.push({ scope: normalized.scope })
701
+ // 0.23.0:read / write 状态。省略 = 可读可写;只有显式 false 才落盘。
702
+ const read = typeof raw === 'object' && raw !== null && raw.read === false
703
+ const write = typeof raw === 'object' && raw !== null && raw.write === false
704
+ scopes.push({
705
+ scope: normalized.scope,
706
+ ...(read ? { read: false } : {}),
707
+ ...(write ? { write: false } : {}),
708
+ })
658
709
  }
659
710
  const levelRaw = rawConfig !== null && rawConfig !== undefined && typeof rawConfig === 'object' ? rawConfig.level : 'absolute'
660
711
  const level = levelRaw === 'normal' || levelRaw === 'none' ? levelRaw : 'absolute'
661
712
  next = { scopes, level }
662
713
  }
714
+ // 工作区级绑定:省略 = 不改;数组 = 整体替换。与可见范围同一个保存动作提交。
715
+ const rawBindings = body?.bindings
716
+ if (rawBindings !== undefined) {
717
+ if (!Array.isArray(rawBindings)) throw new ApiError(400, 'SCOPE_INVALID', 'bindings 必须是字符串数组')
718
+ if (typeof memory.setWorkspaceBindings !== 'function') {
719
+ throw new ApiError(501, 'NOT_SUPPORTED', 'provider 未提供工作区绑定接口(请升级 @restregular/dsh-memory)')
720
+ }
721
+ const bindings = []
722
+ for (const raw of rawBindings) {
723
+ if (typeof raw !== 'string' || raw.trim() === '') throw new ApiError(400, 'SCOPE_INVALID', '绑定项必须是非空字符串')
724
+ const normalized = normalizeScopeInput(raw)
725
+ if (!normalized.ok) throw new ApiError(400, normalized.code, normalized.message)
726
+ if (normalized.kind === 'all' || normalized.kind === 'current-project' || normalized.kind === 'current-session' || normalized.scope === undefined) {
727
+ throw new ApiError(400, 'SCOPE_INVALID', `绑定必须是具体作用域键: ${JSON.stringify(raw)}`)
728
+ }
729
+ if (normalized.scope.startsWith('session:')) {
730
+ throw new ApiError(400, 'SCOPE_INVALID', '会话作用域不能绑定到工作区')
731
+ }
732
+ if (!bindings.includes(normalized.scope)) bindings.push(normalized.scope)
733
+ }
734
+ let session
735
+ try { session = ctx.get('sessions')?.get?.(sessionId) } catch { session = undefined }
736
+ memory.setWorkspaceBindings({ session: session !== undefined ? session : { id: sessionId } }, bindings)
737
+ }
663
738
  memory.setSessionVisibility(sessionId, next)
664
739
  writeJson(res, { ok: true, value: visibilityPayload(ctx, memory, sessionId) })
665
740
  return
@@ -944,6 +1019,28 @@ async function handle(ctx, routePath, req, res) {
944
1019
  if (method === 'POST' && path === '/update') {
945
1020
  const body = await readJsonBody(req)
946
1021
  const id = String(body.id ?? '')
1022
+ // 0.23.0:编辑 = 新建一条修正版,旧记录不原地修改。旧 provider 没有
1023
+ // reviseRecord 时才退回原来的原地编辑,避免版本错配直接报错。
1024
+ if (typeof memory.reviseRecord === 'function') {
1025
+ const input = { id }
1026
+ if (body.content !== undefined) input.content = String(body.content)
1027
+ if (body.kind !== undefined) input.kind = String(body.kind)
1028
+ if (body.importance !== undefined) input.importance = Number(body.importance)
1029
+ if (body.tags !== undefined) input.tags = Array.isArray(body.tags) ? body.tags.map(String) : []
1030
+ if (body.triggers !== undefined) input.triggers = Array.isArray(body.triggers) ? body.triggers.map(String) : []
1031
+ if (body.expiredAt === null) input.expiresAt = null
1032
+ else if (typeof body.expiresAt === 'number') input.expiresAt = body.expiresAt
1033
+ else if (typeof body.ttlMs === 'number' && body.ttlMs > 0) input.ttlMs = body.ttlMs
1034
+ const { record, superseded } = await memory.reviseRecord(input)
1035
+ const scopeStore = store.peek(record.scope) ?? { embedPending: new Map(), vectors: new Map() }
1036
+ let supersededBy = null
1037
+ for (const other of scopeStore.records.values()) {
1038
+ if (other.supersedes === record.id) { supersededBy = other.id; break }
1039
+ }
1040
+ console.log(`[dsh-memory-panel] 修正记忆 ${superseded} -> ${record.id}`)
1041
+ writeJson(res, { ok: true, value: { record: wireRecord(record, scopeStore, supersededBy), superseded } })
1042
+ return
1043
+ }
947
1044
  let target = null
948
1045
  for (const s of store.allScopes()) {
949
1046
  const rec = s.records.get(id)
@@ -151,6 +151,11 @@ export class CordisMemoryService extends Service {
151
151
  return this.impl.createRecord(input)
152
152
  }
153
153
 
154
+ /** 面板编辑:新建一条修正版并通过 supersedes 指向旧记录(0.23.0)。 */
155
+ reviseRecord(input ) {
156
+ return this.impl.reviseRecord(input)
157
+ }
158
+
154
159
  /** 面板导出:纯文本 + 元数据,不含向量。 */
155
160
  exportAll() {
156
161
  return this.impl.exportAll()
@@ -253,6 +258,36 @@ export class CordisMemoryService extends Service {
253
258
  return this.impl.isScopeOperable(agent, scope)
254
259
  }
255
260
 
261
+ /** 某个作用域是否可读(0.23.0 的 read 开关)。 */
262
+ isScopeReadable(agent , scope ) {
263
+ return this.impl.isScopeReadable(agent, scope)
264
+ }
265
+
266
+ /** 某个作用域是否可写(0.23.0 的 write 开关)。 */
267
+ isScopeWritable(agent , scope ) {
268
+ return this.impl.isScopeWritable(agent, scope)
269
+ }
270
+
271
+ /** 当前工作区 key(= 会话 cwd 解析出的 project scope)。 */
272
+ workspaceKeyFor(agent ) {
273
+ return this.impl.workspaceKeyFor(agent)
274
+ }
275
+
276
+ /** 面板:当前工作区绑定的作用域。 */
277
+ workspaceBindings(agent ) {
278
+ return this.impl.workspaceBindings(agent)
279
+ }
280
+
281
+ /** 面板:默认范围的基础集合(会话 + 项目 + global),不含工作区绑定。 */
282
+ defaultScopes(agent ) {
283
+ return this.impl.defaultScopes(agent)
284
+ }
285
+
286
+ /** 面板:设置当前工作区绑定的作用域。 */
287
+ setWorkspaceBindings(agent , scopes ) {
288
+ return this.impl.setWorkspaceBindings(agent, scopes)
289
+ }
290
+
256
291
  /** 某个 scope 下允许的记忆类型(undefined = 不限制)。 */
257
292
  visibleKinds(agent , scope ) {
258
293
  return this.impl.visibleKinds(agent, scope)