@restregular/dsh-memory 0.29.0 → 0.30.0

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,16 @@
2
2
 
3
3
  本项目的版本记录。安装 id 与 npm 版本一一对应。
4
4
 
5
+ ## 0.30.0 — 会话作用域绑定会话生命周期(创建自动建 / 归档即冻结 / 恢复即复原)
6
+
7
+ - **问题**:会话作用域此前只在第一次写记忆时才懒创建,且与宿主的会话生命周期完全脱节 —— 会话归档后,它的 `session:<id>` 作用域与记忆仍然"存在",跨作用域检索、面板、`memory_get(id)` 都能碰到;恢复 / 归档也没有任何可见语义。用户想要的是:会话创建时作用域就已存在,归档时连同记忆一起冻结,恢复后原样回来。
8
+ - **创建**:`provider/apply.ts` 订阅宿主 `session/created`(顶层会话;子 Agent 沿用父会话作用域),调用新增的 `ensureSessionScope(sessionId)` —— 只写 `scope-meta`(用途:「会话作用域:仅本会话可见的过程记忆;随会话归档与恢复(系统自动创建)」/ `createdBy: system` / `status: ready`),**不写任何记忆**,幂等、不覆盖用户改过的用途。监听器全程 try/catch:`session/created` 抛错会回滚会话创建,记忆侧失败绝不能影响宿主。启动时 `ctx.inject(['sessions'])` 给已存在的会话补建。
9
+ - **归档 = 冻结(实时判定,不复制状态)**:`LocalMemoryService.setArchivedSessionLookup()` 注入对宿主 `workspaceRegistry.archivedSessionIds` 的**实时**读取(不缓存、不落盘)。`authorize()` 新增 `archivedScopes` 上下文:归档作用域 `discover` 仍允许(目录/面板要能看出「已归档」),`read` / `write` / `delete` 一律 `SCOPE_ARCHIVED`(`requestable=false`,没有审批放行路径)。`search` / `list` / `listSync` / `getByIds` 直接跳过归档作用域 —— 不是过滤结果,而是根本不加载它的 store,所以恢复时无需重建索引。
10
+ - **归档期间不清理**:小时级 housekeeping(过期记录清理 + 容量淘汰)与 dispose 时的清理都跳过归档作用域(新增 `runHousekeeping()` 作为定时器与测试共用的入口);数据文件 `.jsonl` / `.vec` 原样保留。
11
+ - **目录与面板**:`scopeCatalog` 每项新增 `archived?: boolean`,`accessStatus` 新增 `'archived'`;归档作用域的 `records` 仍返回(归档不是权限边界,条数可见),`readable/writable/canRequest*` 恒 false。`memory_scopes` 渲染成「N 条 · 已归档 · 恢复会话后自动恢复读写」;面板「记录 → 作用域」每行显示「已归档」徽标(`/status` 透传 `archived`)。
12
+ - **恢复**:宿主归档集合移除该 id 后,下一次判定就恢复正常 —— `search` / `list` / `getByIds` 立即可用,`memory_get` 按原 id 可取,`authorize` 回到普通策略。没有任何"恢复动作"需要执行。
13
+ - 测试:新增 `source/memory-local/test/session-archive.test.ts`(3 个用例:自动创建与幂等 / 归档冻结与恢复复原 / 冻结期间不清理),覆盖 `search`、`listSync`、`getByIds`、`authorize`、`scopeCatalog`、`save` 拒绝与数据文件保留;全套本地 / 工具 / 注入 / 面板测试通过(`multiprocess.test.ts` 需真起子进程,在受限沙箱里 EPERM,与本改动无关)。
14
+
5
15
  ## 0.29.0 — 作用域用途 / 别名可编辑(用户 + Agent)
6
16
 
7
17
  - **问题**:0.28.0 之后「作用域用途」成了检索范围外清单里唯一的信息来源,但它**只能写不能改**——Agent 想补一句用途只能靠 `memory_save` 的 `scopeDescription`(还要顺带写一条记忆),面板则完全没有编辑入口;别名更只在 `memory_save({ path })` 那一瞬间能设,设错了只能改文件。于是第一次随手写的用途会一直错下去。
package/README.md CHANGED
@@ -95,6 +95,7 @@ JSONL 落盘 · 本机向量检索 · 五个模型工具 · 自动召回 · 硬
95
95
  - **作用域名字带类型前缀**(0.25.10):所有展示名都以 `global:` / `project:` / `session:` / `custom:` 开头 —— `global:全局`、`project:D:\repositories\dsh-memory`、`session:session-1a2b3c4d`、`custom:handbook`。**global 与自定义作用域最容易被漏掉**:它们的 scope 键本身没有前缀(`global` 是个关键字、自定义就是裸名字),前缀必须按类型补。项目 / 自定义作用域有别名时是 `project:<别名>` / `custom:<别名>`,会话有标题时是 `session:<标题>(session-1a2b3c4d)`。
96
96
  - **参数仍传裸名**(0.26.0):展示名只用于显示。Agent 把 `custom:handbook` 直接粘进作用域参数时会被**自动归一**成 `handbook`(不再是另一条永远匹配不到记录的授权);`custom:` 后面仍必须是合法自定义名,`custom:` / `custom:foo:bar` / `custom:global` 都会被 `SCOPE_INVALID` 拒绝。旧 grants 文件里遗留的 `custom:` 键在读取时也会折回裸键。
97
97
  - **会话作用域的短 id**(0.25.9):会话 id 取**前 16 位**(`session-` + 8 位 uuid)。以前取前 8 位,而真机会话 id 恰好以 `session-` 开头(正好 8 个字符),结果所有会话作用域都显示成同一个名字——等于没给 id。
98
+ - **会话作用域跟随会话生命周期**(0.30.0):会话**创建**时系统自动建立它的作用域(只写用途元数据,不写记忆);会话在宿主里**归档**后,该作用域与其中的记忆**冻结、不再被任何检索命中**(`memory_search` / `memory_list` / `memory_get` / 注入都不会返回,目录与面板会把这一行标成「已归档」,授权判定返回 `SCOPE_ARCHIVED` 且不可申请);会话**恢复**(取消归档)后作用域与记忆**原样恢复**——归档状态实时读宿主的 `workspaceRegistry.archivedSessionIds`,记忆数据一个字节都不动,归档期间过期清理与容量淘汰也会跳过它。子 Agent 沿用父会话的作用域,不单独创建。
98
99
  - **浮窗里能直接看到真实键**(0.25.11):显示名与真实键不一致时(项目别名 → 完整路径、会话短 id → 完整 uuid、自定义名 → 裸键),每行名称下面多一行等宽的完整键;**不做手工截断**,放不下就按容器宽度用 CSS 省略号收尾,窗口宽就多显示几个字符。键行是等宽小字、和勾选框左对齐。记录卡 / 记忆树 / 审批卡的作用域徽标都在 `title` 里给出「显示名 → 真实键」。
99
100
  - **搜索与批量**:顶部可搜索作用域,支持「全选 / 取消全选」;列表最大高度受限,作用域多时在浮窗内滚动。**整张作用域卡片都能点**(0.25.7 起)——点名称、记录数或行内留白等同点勾选框,只有行内的按钮(勾选框 / 绑定 / 可读 / 可写)各管各的,点它们不会再多切换一次。
100
101
  - **浮窗层级**(0.25.8):浮窗 portal 到 `document.body` 渲染,`z-index:900`,不会再被会话里的「回到底部」圆钮(z-index:8)之类浮动件盖住;同时仍低于宿主自己的对话框 / 提示层(1000 起)。
@@ -621,6 +622,7 @@ npm run verify # 构建到 .verify/,不落 packages/
621
622
 
622
623
  ## 更新日志
623
624
 
625
+ - **0.30.0** —— 会话作用域绑定会话生命周期:会话创建时系统自动建立会话作用域(`ensureSessionScope`,只写 `scope-meta`,`createdBy: system`,幂等且不写记忆);会话归档后该作用域与记忆**冻结**——`search` / `list` / `listSync` / `getByIds` 全部跳过,授权统一返回 `SCOPE_ARCHIVED`(不可申请、无审批路径),目录与面板标「已归档」,归档期间小时级过期清理与容量淘汰也跳过它;取消归档后自动复原(实时读宿主 `workspaceRegistry.archivedSessionIds`,不复制、不缓存,数据文件一个字节不动)。`memory_scopes` 目录项新增 `archived` 与 `accessStatus='archived'`;面板作用域行显示「已归档」徽标;`session/created` 监听失败绝不回滚会话创建,启动时给已存在的会话补建。
624
626
  - **0.29.0** —— 作用域用途 / 别名可编辑:`memory_scopes` 新增 `action="edit"`(`scope` + `scopeDescription?` / `scopeName?`,空串分别表示清空用途 / 移除别名,至少给一个,需要该作用域可写,范围外先走 `request` 申请写权限);面板「记录 → 作用域」每行新增「编辑」,用途编辑框只预填手写的那句,默认描述不预填;`describeScope` 的空描述从报错改为**清空**(保留元数据与 generation,目录回退按类型默认描述),新增 `renameScope`(项目 / 自定义作用域可设别名,global / session 拒绝,重名 `SCOPE_NAME_TAKEN`);`/memory/api/status` 新增 `descriptionSource`(custom / default)与别名 `name`,新增 `POST /memory/api/scope-meta`。
625
627
  - **0.28.0** —— 范围外命中只列作用域:`memory_search` 的 `outOfScope` 去掉条目索引(id / score / 标签 / 短摘要),改为只给命中的作用域、各几条、`description`(作用域用途,`scope-meta` 手写优先、没写过按类型补默认描述)与 `requestable`(该域能否走审批);渲染成「名称(条数 · 可申请性):用途」的独立清单,不再重复 `withheld` 说明。面板「记录」页新增「记忆 / 作用域」两个子标签页,作用域页展示名称、用途、条数、待嵌入与最近更新,可一键跳到该域的记忆列表;`/memory/api/status` 每项新增 `description`(无记录但写过用途的作用域也列出)。
626
628
  - **0.27.0** —— 范围外命中「先索引后审批」:普通限制下 `memory_search` 不再自动弹审批,结果新增 `outOfScope` 索引(作用域聚合 + 条目印象,不含正文),`requestApproval: true` 显式发起审批;`withheld` 新增 `out-of-scope` 原因;`memory_scopes` 目录给没写用途的作用域按类型补默认描述。
package/lib/client.js CHANGED
@@ -4097,6 +4097,8 @@ window.__ModuleLoader__.load({
4097
4097
  records: Number(s.records) || 0,
4098
4098
  pendingEmbeds: Number(s.pendingEmbeds) || 0,
4099
4099
  updatedAt: Number(s.updatedAt) || 0,
4100
+ // 0.30.0:会话归档后作用域保留但读写冻结;恢复会话自动复原。
4101
+ archived: s.archived === true,
4100
4102
  }))
4101
4103
  .filter((row) => needle === ''
4102
4104
  || row.label.toLowerCase().includes(needle)
@@ -4136,6 +4138,12 @@ window.__ModuleLoader__.load({
4136
4138
  h('div', { className: 'scope-row-head' },
4137
4139
  h('span', { className: 'scope-row-name', title: scopeTitle(row.scope, row.label) }, row.label),
4138
4140
  h('span', { className: 'badge' }, `${row.records} 条记忆`),
4141
+ row.archived
4142
+ ? h('span', {
4143
+ className: 'badge',
4144
+ title: '所属会话已归档:作用域与记忆被冻结,取消归档后自动恢复读写',
4145
+ }, '已归档')
4146
+ : null,
4139
4147
  row.pendingEmbeds > 0
4140
4148
  ? h('span', { className: 'pending', title: '待嵌入向量:向量服务就绪后会自动补上' }, `待嵌入 ${row.pendingEmbeds}`)
4141
4149
  : null,
package/lib/panel.js CHANGED
@@ -947,6 +947,7 @@ async function handle(ctx, routePath, req, res) {
947
947
  description: entry.description.trim(),
948
948
  descriptionSource: customDescriptionOf(key) !== '' ? 'custom' : 'default',
949
949
  ...(typeof entry.origin === 'string' ? { origin: entry.origin } : {}),
950
+ ...(entry.archived === true ? { archived: true } : {}),
950
951
  })
951
952
  }
952
953
  let embedServer = null
@@ -36,6 +36,19 @@ export class CordisMemoryService extends Service {
36
36
  return undefined
37
37
  }
38
38
  })
39
+ // 0.30.0:会话作用域跟随宿主会话生命周期。归档状态**实时**读取
40
+ // `workspaceRegistry.archivedSessionIds`,不缓存、不复制 —— 归档即刻不可检索,
41
+ // 恢复即刻复原,不需要任何同步动作。
42
+ this.impl.setArchivedSessionLookup(() => {
43
+ try {
44
+ const registry = ctx.get('workspaceRegistry')
45
+ const ids = registry?.archivedSessionIds
46
+ return Array.isArray(ids) ? ids.filter((id) => typeof id === 'string') : []
47
+ } catch {
48
+ return []
49
+ }
50
+ })
51
+ this.attachSessionLifecycle(ctx)
39
52
  // 0.11.1:同一个目录因为 `D:/x` 与 `D:\x` 写法不同而分叉成两个 scope 的历史数据,
40
53
  // 在装配期合并一次(每进程一次;失败只影响合并,不影响读写)。
41
54
  try {
@@ -47,6 +60,52 @@ export class CordisMemoryService extends Service {
47
60
  this.attachRerank(ctx, config)
48
61
  }
49
62
 
63
+ /**
64
+ * 0.30.0:把「会话作用域」接到宿主的会话生命周期上。
65
+ *
66
+ * - **创建**:`session/created` 时系统自动建立会话作用域(只写用途元数据,不写记忆);
67
+ * - **归档 / 恢复**:不写任何东西 —— 检索与授权实时读
68
+ * `workspaceRegistry.archivedSessionIds`,归档即不可检索,恢复即复原;
69
+ * - 启动补建:`sessions` 服务就绪后,给已存在的会话补上作用域。
70
+ *
71
+ * 监听器绝不能抛:`session/created` 的异常会回滚会话创建(宿主契约)。
72
+ */
73
+ attachSessionLifecycle(ctx ) {
74
+ try {
75
+ ctx.on('session/created', (session ) => {
76
+ try {
77
+ const id = liveSessionIdOf(session)
78
+ if (id !== undefined) this.impl.ensureSessionScope(id)
79
+ } catch {
80
+ // 记忆侧失败不能回滚会话创建。
81
+ }
82
+ })
83
+ } catch {
84
+ // 老宿主没有该事件:退化为懒创建(第一次写记忆时才建作用域)。
85
+ }
86
+ try {
87
+ ctx.inject(['sessions'], (child) => {
88
+ try {
89
+ const sessions = child.get('sessions')
90
+ const list = sessions?.list?.()
91
+ if (!Array.isArray(list)) return
92
+ for (const session of list) {
93
+ try {
94
+ const id = liveSessionIdOf(session)
95
+ if (id !== undefined) this.impl.ensureSessionScope(id)
96
+ } catch {
97
+ // 单个会话失败不影响其它会话。
98
+ }
99
+ }
100
+ } catch {
101
+ // 服务形状不符:跳过补建,后续 session/created 仍会建。
102
+ }
103
+ })
104
+ } catch {
105
+ // 老宿主 / 测试环境没有 inject:只靠 session/created。
106
+ }
107
+ }
108
+
50
109
  /**
51
110
  * Wire the rerank stage to ctx.llm when both it and a model are configured.
52
111
  * Uses the documented one-shot shape: GenerateOptions + RequestUserInput,
@@ -444,6 +503,22 @@ export function apply(ctx , config = {}) {
444
503
  }
445
504
  ctx.plugin(ScopedMemoryService)
446
505
  }
506
+
507
+ /**
508
+ * 从宿主的 session 对象取出「需要建立作用域」的会话 id(0.30.0)。
509
+ *
510
+ * 只给顶层会话建:子 Agent 沿用父会话的记忆作用域(见 `resolveCurrentScopes`),
511
+ * 单独建只会制造一批空的作用域。形状兼容 `session.id` 与 `session.header.id`。
512
+ */
513
+ function liveSessionIdOf(session ) {
514
+ const value = session
515
+ const raw = typeof value?.id === 'string' && value.id !== ''
516
+ ? value.id
517
+ : value?.header?.id
518
+ if (typeof raw !== 'string' || raw === '') return undefined
519
+ if (value?.header?.origin === 'subagent') return undefined
520
+ return raw
521
+ }
447
522
 
448
523
 
449
524
  //# sourceURL=source/memory-local/src/apply.ts
@@ -37,6 +37,8 @@
37
37
 
38
38
 
39
39
 
40
+                                   
41
+
40
42
 
41
43
 
42
44
 
@@ -64,6 +66,11 @@
64
66
 
65
67
                        
66
68
 
69
+
70
+                          
71
+                                   
72
+
73
+
67
74
 
68
75
 
69
76
  function outsideDecision(level ) {
@@ -76,6 +83,12 @@ export function authorize(request , context )
76
83
  if (!context.enabled[context.originOf(request.scope)]) {
77
84
  return { allowed: false, reason: 'SCOPE_DISABLED', requestable: false }
78
85
  }
86
+ // 归档是宿主生命周期事实,不是可见域策略:没有审批放行路径,也**不允许**申请。
87
+ // discover 仍然允许(目录/面板要能看出「这个作用域已归档」),正文一律不可读。
88
+ if (context.archivedScopes?.has(request.scope) === true) {
89
+ if (request.action === 'discover') return { allowed: true, source: 'directory' }
90
+ return { allowed: false, reason: 'SCOPE_ARCHIVED', requestable: false }
91
+ }
79
92
  if (request.action === 'discover') {
80
93
  return { allowed: true, source: 'directory' }
81
94
  }
@@ -575,9 +575,14 @@ export function resolveConfig(config = {}) {
575
575
 
576
576
 
577
577
 
578
-
578
+
579
579
 
580
580
 
581
+                                  
582
+                    
583
+
584
+
585
+
581
586
                        
582
587
                                      
583
588
 
@@ -736,6 +741,11 @@ export class LocalMemoryService {
736
741
  * 宿主侧 apply.ts 注入;旧宿主 / 测试里没有时退化为只看当前会话。
737
742
  */
738
743
  sessionLookup
744
+ /**
745
+ * 宿主归档会话查询(0.30.0):`workspaceRegistry.archivedSessionIds` 的惰性读取。
746
+ * 宿主侧 apply.ts 注入;没有时按「没有归档」处理(纯本地 / 测试环境)。
747
+ */
748
+ archivedLookup
739
749
  /** 注入半边的审计收集器(可选;没有注入半边时保持 null)。 */
740
750
  injectAuditSink
741
751
 
@@ -918,9 +928,12 @@ export class LocalMemoryService {
918
928
  const visibleFilter = input.agent !== undefined ? this.visibilityFilterFor(input.agent) : undefined
919
929
  const seen = new Set ()
920
930
  const stores = []
931
+ const archivedScopes = this.archivedSessionScopes()
921
932
  for (const scope of wanted) {
922
933
  if (seen.has(scope)) continue
923
934
  seen.add(scope)
935
+ // 0.30.0:归档会话作用域在归档期间不参与枚举 / 注入。
936
+ if (archivedScopes.has(scope)) continue
924
937
  const s = this.store.peek(scope)
925
938
  if (s) stores.push(s)
926
939
  }
@@ -1340,6 +1353,19 @@ export class LocalMemoryService {
1340
1353
  void this.ensureEmbedServer(60_000).catch(() => { /* panel shows the outcome */ })
1341
1354
  }
1342
1355
 
1356
+ /**
1357
+ * 过期清理 + 容量淘汰(小时定时器与测试共用)。
1358
+ *
1359
+ * 0.30.0:归档会话作用域在归档期间**冻结** —— 过期与容量都不碰它们,
1360
+ * 恢复会话后记忆原样回来;恢复后下一次 housekeeping 再按原规则清理。
1361
+ */
1362
+ runHousekeeping() {
1363
+ const frozen = (scope ) => this.isArchivedSessionScope(scope)
1364
+ const purged = this.store.purgeExpired(Date.now(), frozen)
1365
+ const evicted = this.store.enforceLimit(this.cfg.maxRecordsPerScope, Date.now(), frozen)
1366
+ return { purged, evicted }
1367
+ }
1368
+
1343
1369
  /**
1344
1370
  * Hourly expired-record purge + capacity eviction. M4 housekeeping; the
1345
1371
  * timer is unref'd so it never holds the process open, and cleared by
@@ -1349,8 +1375,7 @@ export class LocalMemoryService {
1349
1375
 
1350
1376
  startMaintenance() {
1351
1377
  this.maintenanceTimer = setInterval(() => {
1352
- this.store.purgeExpired()
1353
- this.store.enforceLimit(this.cfg.maxRecordsPerScope)
1378
+ this.runHousekeeping()
1354
1379
  this.store.scheduleFlush(0)
1355
1380
  }, 3_600_000)
1356
1381
  this.maintenanceTimer.unref?.()
@@ -1533,11 +1558,77 @@ export class LocalMemoryService {
1533
1558
  this.sessionLookup = lookup
1534
1559
  }
1535
1560
 
1561
+ /**
1562
+ * 宿主注入归档会话查询(0.30.0)。每次判定都实时读取,不缓存:
1563
+ * 归档/恢复是宿主 `workspaceRegistry` 的持久状态,读它才能保证
1564
+ * 「归档后检索不到、恢复后立刻能检索」与宿主的会话生命周期一致。
1565
+ * 服务未就绪 / 老宿主拿不到时按「没有归档」处理。
1566
+ */
1567
+ setArchivedSessionLookup(lookup ) {
1568
+ this.archivedLookup = lookup
1569
+ }
1570
+
1571
+ /** 宿主当前归档的会话 id 集合(实时读取;容错为空集合)。 */
1572
+ archivedSessionIds() {
1573
+ const out = new Set ()
1574
+ if (this.archivedLookup === undefined) return out
1575
+ try {
1576
+ const ids = this.archivedLookup()
1577
+ if (Array.isArray(ids)) {
1578
+ for (const id of ids) {
1579
+ if (typeof id === 'string' && id !== '') out.add(id)
1580
+ }
1581
+ }
1582
+ } catch {
1583
+ // 宿主服务未就绪:当作没有归档,绝不能因为生命周期查询失败而让检索挂掉。
1584
+ }
1585
+ return out
1586
+ }
1587
+
1588
+ /** 已归档的**会话作用域键**集合(`session:<id>`)。 */
1589
+ archivedSessionScopes() {
1590
+ const out = new Set ()
1591
+ for (const id of this.archivedSessionIds()) out.add(`session:${id}`)
1592
+ return out
1593
+ }
1594
+
1595
+ /** 这个作用域是否是已归档会话的作用域(只有 session: 键会命中)。 */
1596
+ isArchivedSessionScope(scope ) {
1597
+ if (typeof scope !== 'string' || !scope.startsWith('session:')) return false
1598
+ return this.archivedSessionIds().has(scope.slice('session:'.length))
1599
+ }
1600
+
1601
+ /**
1602
+ * 会话创建时**系统自动建立**会话作用域(0.30.0)。
1603
+ *
1604
+ * 只写 scope-meta(用途 + 创建者 system),不写任何记忆记录;用户 / Agent
1605
+ * 之后的第一条 `memory_save` 仍走原来的懒创建路径。已有元数据(用户改过用途、
1606
+ * 或之前保存过)时不覆盖。会话作用域类型被关闭时不动。
1607
+ *
1608
+ * @returns 是否新建了元数据。
1609
+ */
1610
+ ensureSessionScope(sessionId ) {
1611
+ const id = typeof sessionId === 'string' ? sessionId.trim() : ''
1612
+ if (id === '' || !this.cfg.sessionEnabled) return false
1613
+ const scope = `session:${id}`
1614
+ if (this.meta.get(scope) !== undefined) return false
1615
+ this.meta.put(scope, {
1616
+ description: '会话作用域:仅本会话可见的过程记忆;随会话归档与恢复(系统自动创建)。',
1617
+ createdBy: 'system',
1618
+ status: 'ready',
1619
+ })
1620
+ return true
1621
+ }
1622
+
1536
1623
  /** 只保留当前配置允许的 scope(覆盖集合里若含被禁用的 scope,静默剔除)。 */
1537
1624
  filterEnabledRules(rules ) {
1538
1625
  const out = []
1626
+ // 已归档会话的作用域不在「可见域」里:用户归档前勾选过也一并剔除,
1627
+ // 恢复会话后规则会自动重新生效(规则本身没被改写)。
1628
+ const archived = this.archivedSessionScopes()
1539
1629
  for (const raw of rules) {
1540
1630
  const scope = this.canonicalScope(raw.scope)
1631
+ if (archived.has(scope)) continue
1541
1632
  if (out.some((rule) => rule.scope === scope)) continue
1542
1633
  switch (scopeKind(scope)) {
1543
1634
  case 'global': if (!this.cfg.globalEnabled) continue; break
@@ -1669,7 +1760,9 @@ export class LocalMemoryService {
1669
1760
  /** 没有显式覆盖时的默认可见域范围(基础三路 + 工作区绑定的额外作用域)。 */
1670
1761
  defaultReadScopes(agent ) {
1671
1762
  const out = this.baseDefaultScopes(agent)
1763
+ const archived = this.archivedSessionScopes()
1672
1764
  const push = (scope ) => {
1765
+ if (archived.has(scope)) return
1673
1766
  if (out.includes(scope)) return
1674
1767
  out.push(scope)
1675
1768
  }
@@ -1756,6 +1849,7 @@ export class LocalMemoryService {
1756
1849
  },
1757
1850
  originOf: (scope) => scopeKind(scope),
1758
1851
  grants: this.validGrants(agent),
1852
+ archivedScopes: this.archivedSessionScopes(),
1759
1853
  }
1760
1854
  }
1761
1855
 
@@ -2361,6 +2455,8 @@ export class LocalMemoryService {
2361
2455
  const decision = this.authorize(agent, { scope, action: 'write' })
2362
2456
  if (decision.allowed) return
2363
2457
  switch (decision.reason) {
2458
+ case 'SCOPE_ARCHIVED':
2459
+ throw memoryError('SCOPE_ARCHIVED', `作用域 ${scope} 所属的会话已归档:作用域与记忆被冻结,恢复会话后即可继续读写。`)
2364
2460
  case 'WRITE_DISABLED':
2365
2461
  throw memoryError('SCOPE_READ_ONLY', `作用域 ${scope} 在本会话记忆范围里被标记为「不可写」(可读/可写按钮),本次写入/删除没有执行。如需写入,请让用户打开该作用域的「可写」状态。`)
2366
2462
  case 'SCOPE_DISABLED':
@@ -2636,9 +2732,13 @@ export class LocalMemoryService {
2636
2732
  : [this.defaultProjectScope, 'global']).map((scope) => this.canonicalScope(scope))
2637
2733
  const stores = []
2638
2734
  const seen = new Set ()
2735
+ const archivedScopes = this.archivedSessionScopes()
2639
2736
  for (const scope of wanted) {
2640
2737
  if (seen.has(scope)) continue // 按 scope key 去重(也顺手消掉 defaultScope==='global' 时的重复 push)
2641
2738
  seen.add(scope)
2739
+ // 0.30.0:归档会话作用域在归档期间**完全不参与检索** —— 连词法/向量候选都不进,
2740
+ // 这样恢复会话后不需要重建任何索引,记忆原样回来。
2741
+ if (archivedScopes.has(scope)) continue
2642
2742
  const store = this.store.peek(scope)
2643
2743
  if (!store) continue
2644
2744
  stores.push({ store, scope, isGlobal: scope === 'global' })
@@ -2908,9 +3008,11 @@ export class LocalMemoryService {
2908
3008
  : [this.defaultProjectScope]).map((scope) => this.canonicalScope(scope))
2909
3009
  const seen = new Set ()
2910
3010
  const stores = []
3011
+ const archivedScopes = this.archivedSessionScopes()
2911
3012
  for (const scope of wanted) {
2912
3013
  if (seen.has(scope)) continue
2913
3014
  seen.add(scope)
3015
+ if (archivedScopes.has(scope)) continue
2914
3016
  const store = this.store.peek(scope)
2915
3017
  if (store) stores.push(store)
2916
3018
  }
@@ -2937,11 +3039,13 @@ export class LocalMemoryService {
2937
3039
  getByIds(ids ) {
2938
3040
  this.discoverScopes()
2939
3041
  const scopes = this.store.allScopes()
3042
+ const archivedScopes = this.archivedSessionScopes()
2940
3043
  const out = []
2941
3044
  const seen = new Set ()
2942
3045
  for (const token of ids) {
2943
3046
  if (typeof token !== 'string' || token.length === 0) continue
2944
3047
  for (const s of scopes) {
3048
+ if (archivedScopes.has(s.scope)) continue
2945
3049
  for (const rec of s.records.values()) {
2946
3050
  if (seen.has(rec.id)) continue
2947
3051
  if (rec.id === token || rec.id.startsWith(token)) {
@@ -3266,8 +3370,9 @@ export class LocalMemoryService {
3266
3370
  * 漏掉别的进程(上一次会话、另一个 agent)写下的作用域。
3267
3371
  * 排序键是"最近活跃"(`updatedAt`):面板据此只给最近活跃的前 N 个会话 scope 查标题。
3268
3372
  */
3269
- scopeStats() {
3373
+ scopeStats() {
3270
3374
  this.discoverScopes()
3375
+ const archived = this.archivedSessionScopes()
3271
3376
  return this.store.allScopes()
3272
3377
  .map((s) => ({
3273
3378
  scope: s.scope,
@@ -3275,6 +3380,7 @@ export class LocalMemoryService {
3275
3380
  records: s.records.size,
3276
3381
  pendingEmbeds: s.embedPending.size,
3277
3382
  updatedAt: [...s.records.values()].reduce((max, r) => Math.max(max, r.updatedAt), 0),
3383
+ ...(archived.has(s.scope) ? { archived: true } : {}),
3278
3384
  }))
3279
3385
  .sort((a, b) => b.updatedAt - a.updatedAt || b.records - a.records || a.scope.localeCompare(b.scope))
3280
3386
  }
@@ -3323,6 +3429,7 @@ export class LocalMemoryService {
3323
3429
 
3324
3430
  const out = []
3325
3431
  const evaluate = this.accessEvaluator(agent)
3432
+ const archivedScopes = this.archivedSessionScopes()
3326
3433
  for (const scope of keys) {
3327
3434
  // 目录发现本身也是一条授权判定:作用域类型被关闭时连名字/用途都不出现。
3328
3435
  if (!evaluate({ scope, action: 'discover' }).allowed) continue
@@ -3333,28 +3440,33 @@ export class LocalMemoryService {
3333
3440
  // 别的会话的 session scope 默认不进目录:它们没有用途、也不该被 Agent 主动申请,
3334
3441
  // 全列出来只会把目录变成历史会话列表。有元数据(被人描述过)则保留。
3335
3442
  if (!inScope && meta === undefined && scopeKind(scope) === 'session') continue
3443
+ // 0.30.0:归档会话的作用域保留在目录里,但读写/申请全部冻结 —— 这不是权限
3444
+ // 边界,而是会话生命周期:恢复会话后自动复原,不需要任何额外操作。
3445
+ const archived = archivedScopes.has(scope)
3336
3446
  const readDecision = evaluate({ scope, action: 'read' })
3337
3447
  const writeDecision = evaluate({ scope, action: 'write' })
3338
- const readable = readDecision.allowed
3339
- const writable = writeDecision.allowed
3448
+ const readable = !archived && readDecision.allowed
3449
+ const writable = !archived && writeDecision.allowed
3340
3450
  // 只有“整个作用域不在范围内”(SCOPE_NOT_IN_RANGE)才可申请;absolute 与
3341
- // 用户显式关掉的 read/write 都不会 requestable。
3342
- const canRequestRead = !readable && readDecision.requestable
3343
- const canRequestWrite = !writable && writeDecision.requestable
3344
- const accessStatus = !inScope
3345
- // 范围外但已有会话授权:如实显示“可读 / 可写”,而不是继续标 restricted。
3346
- ? readable && writable
3347
- ? 'read-write'
3348
- : readable
3349
- ? 'read-only'
3350
- : 'restricted'
3351
- : policy.source === 'default'
3352
- ? 'default'
3353
- : readable && writable
3451
+ // 用户显式关掉的 read/write 都不会 requestable;归档也不可申请。
3452
+ const canRequestRead = !archived && !readable && readDecision.requestable
3453
+ const canRequestWrite = !archived && !writable && writeDecision.requestable
3454
+ const accessStatus = archived
3455
+ ? 'archived'
3456
+ : !inScope
3457
+ // 范围外但已有会话授权:如实显示“可读 / 可写”,而不是继续标 restricted。
3458
+ ? readable && writable
3354
3459
  ? 'read-write'
3355
3460
  : readable
3356
3461
  ? 'read-only'
3357
3462
  : 'restricted'
3463
+ : policy.source === 'default'
3464
+ ? 'default'
3465
+ : readable && writable
3466
+ ? 'read-write'
3467
+ : readable
3468
+ ? 'read-only'
3469
+ : 'restricted'
3358
3470
  const name = meta?.name ?? this.names.nameOf(scope)
3359
3471
  const initialRecords = policy.level === 'absolute' ? undefined : this.initialRecordsOf(scope)
3360
3472
  // 0.27.0:scope-meta 没写用途时按作用域类型给默认描述,
@@ -3367,7 +3479,7 @@ export class LocalMemoryService {
3367
3479
  ...(name !== undefined ? { name } : {}),
3368
3480
  ...(description !== '' ? { description } : {}),
3369
3481
  origin: meta?.origin ?? scopeKind(scope),
3370
- ...(inScope ? { records: stat?.records ?? 0 } : {}),
3482
+ ...(inScope || archived ? { records: stat?.records ?? 0 } : {}),
3371
3483
  inScope,
3372
3484
  readable,
3373
3485
  writable,
@@ -3375,6 +3487,7 @@ export class LocalMemoryService {
3375
3487
  canRequestWrite,
3376
3488
  accessStatus,
3377
3489
  updatedAt: stat?.updatedAt ?? meta?.updatedAt ?? 0,
3490
+ ...(archived ? { archived: true } : {}),
3378
3491
  ...(initialRecords !== undefined ? { initialRecords } : {}),
3379
3492
  })
3380
3493
  }
@@ -3531,8 +3644,10 @@ export class LocalMemoryService {
3531
3644
  process.removeListener('exit', this.exitHook)
3532
3645
  this.exitHook = null
3533
3646
  }
3534
- this.store.purgeExpired()
3535
- this.store.enforceLimit(this.cfg.maxRecordsPerScope)
3647
+ // 归档会话作用域在归档期间冻结:dispose 时也不做过期清理 / 容量淘汰。
3648
+ const frozen = (scope ) => this.isArchivedSessionScope(scope)
3649
+ this.store.purgeExpired(undefined, frozen)
3650
+ this.store.enforceLimit(this.cfg.maxRecordsPerScope, undefined, frozen)
3536
3651
  this.store.close()
3537
3652
  }
3538
3653
 
@@ -849,10 +849,16 @@ export class MemoryStore {
849
849
  return ids
850
850
  }
851
851
 
852
- /** Remove every expired record across scopes; returns removed ids. */
853
- purgeExpired(now = Date.now()) {
852
+ /**
853
+ * Remove every expired record across scopes; returns removed ids.
854
+ *
855
+ * `skip` 让调用方按作用域排除(0.30.0:已归档会话作用域在归档期间冻结,
856
+ * 过期清理不碰它们 —— 恢复会话后记忆仍在)。
857
+ */
858
+ purgeExpired(now = Date.now(), skip ) {
854
859
  const removed = []
855
860
  for (const s of this.scopes.values()) {
861
+ if (skip?.(s.scope)) continue
856
862
  for (const rec of [...s.records.values()]) {
857
863
  if (rec.expiredAt !== undefined && rec.expiredAt < now) {
858
864
  s.remove(rec.id)
@@ -866,10 +872,13 @@ export class MemoryStore {
866
872
  /**
867
873
  * Evict lowest-ranked records until each scope is within its cap. Ranking:
868
874
  * recency (30d half-life) × importance — keep what retrieval would favor.
875
+ *
876
+ * `skip` 与 purgeExpired 同口径:冻结的作用域不参与容量淘汰。
869
877
  */
870
- enforceLimit(maxPerScope , now = Date.now()) {
878
+ enforceLimit(maxPerScope , now = Date.now(), skip ) {
871
879
  const removed = []
872
880
  for (const s of this.scopes.values()) {
881
+ if (skip?.(s.scope)) continue
873
882
  if (s.records.size <= maxPerScope) continue
874
883
  const ranked = [...s.records.values()]
875
884
  .map((r) => ({
@@ -135,8 +135,10 @@ import { canonicalWorkspacePath, defaultScopeDescription, normalizeScopeInput, r
135
135
 
136
136
 
137
137
 
138
-
138
+
139
139
 
140
+                              
141
+
140
142
                 
141
143
 
142
144
 
@@ -187,7 +189,7 @@ import { canonicalWorkspacePath, defaultScopeDescription, normalizeScopeInput, r
187
189
                  
188
190
 
189
191
                          
190
-
192
+
191
193
                                
192
194
 
193
195
 
@@ -1378,7 +1380,7 @@ export function memoryForgetTool(opts )
1378
1380
 
1379
1381
 
1380
1382
                                 
1381
-
1383
+
1382
1384
 
1383
1385
                               
1384
1386
                       
@@ -206,7 +206,10 @@ export function renderScopesResult(value , label )
206
206
  : '(未填写用途)'
207
207
  const meta = []
208
208
  if (item.records !== undefined) meta.push(`${item.records} 条`)
209
- if (item.inScope) {
209
+ if (item.archived === true) {
210
+ // 0.30.0:归档不是权限拒绝,而是会话生命周期 —— 数据仍在,恢复会话即复原。
211
+ meta.push('已归档 · 恢复会话后自动恢复读写')
212
+ } else if (item.inScope) {
210
213
  meta.push(item.accessStatus === 'default'
211
214
  ? '范围内'
212
215
  : item.accessStatus === 'read-only'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@restregular/dsh-memory",
3
- "version": "0.29.0",
3
+ "version": "0.30.0",
4
4
  "description": "DeepSeek Harness 记忆系统(单包单入口):JSONL 存储 + 本地向量检索(BGE-M3)+ 五个模型工具 + 自动召回注入 + 管理面板。",
5
5
  "icon": "./icon.svg",
6
6
  "type": "module",
@@ -47,11 +47,6 @@
47
47
  "optional": true
48
48
  }
49
49
  },
50
- "repository": {
51
- "type": "git",
52
- "url": "git+https://github.com/RestRegular/dsh-memory",
53
- "directory": "packages/dsh-memory"
54
- },
55
50
  "publishConfig": {
56
51
  "access": "public"
57
52
  },