@restregular/dsh-memory 0.28.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 +20 -0
- package/README.md +13 -3
- package/lib/client.js +130 -26
- package/lib/inject/policy.js +2 -0
- package/lib/panel.js +1345 -1284
- package/lib/provider/apply.js +81 -1
- package/lib/provider/authorize.js +13 -0
- package/lib/provider/index.js +170 -26
- package/lib/provider/store.js +12 -3
- package/lib/tools/index.js +114 -7
- package/lib/tools/render.js +20 -2
- package/package.json +1 -6
package/lib/provider/apply.js
CHANGED
|
@@ -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,
|
|
@@ -201,11 +260,16 @@ export class CordisMemoryService extends Service {
|
|
|
201
260
|
return this.impl.scopeMeta(scope)
|
|
202
261
|
}
|
|
203
262
|
|
|
204
|
-
/** 写入 /
|
|
263
|
+
/** 写入 / 更新 / 清空作用域用途(不创建记录)。 */
|
|
205
264
|
describeScope(scope , description , by ) {
|
|
206
265
|
return this.impl.describeScope(scope, description, by)
|
|
207
266
|
}
|
|
208
267
|
|
|
268
|
+
/** 设置 / 移除作用域别名(只影响显示与引用,不改变作用域键)。 */
|
|
269
|
+
renameScope(scope , name ) {
|
|
270
|
+
return this.impl.renameScope(scope, name)
|
|
271
|
+
}
|
|
272
|
+
|
|
209
273
|
/** 作用域目录:名字 / 用途 / 当前可读可写状态(工具 memory_scopes 与面板共用)。 */
|
|
210
274
|
scopeCatalog(agent ) {
|
|
211
275
|
return this.impl.scopeCatalog(agent)
|
|
@@ -439,6 +503,22 @@ export function apply(ctx , config = {}) {
|
|
|
439
503
|
}
|
|
440
504
|
ctx.plugin(ScopedMemoryService)
|
|
441
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
|
+
}
|
|
442
522
|
|
|
443
523
|
|
|
444
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
|
}
|
package/lib/provider/index.js
CHANGED
|
@@ -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.
|
|
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)) {
|
|
@@ -3189,21 +3293,48 @@ export class LocalMemoryService {
|
|
|
3189
3293
|
}
|
|
3190
3294
|
|
|
3191
3295
|
/**
|
|
3192
|
-
* 写入 /
|
|
3193
|
-
* memory_save 的 scopeDescription
|
|
3296
|
+
* 写入 / 更新 / 清空作用域用途。**不会**创建记录,也不触发初始化;给面板、
|
|
3297
|
+
* `memory_scopes` 的 edit 动作与 memory_save 的 scopeDescription 用。
|
|
3298
|
+
*
|
|
3299
|
+
* 0.29.0:空白描述 = **清空**(保留元数据与 generation —— 删元数据会让按
|
|
3300
|
+
* generation 绑定的会话授权失效,所以清空只把 description 写成空串)。
|
|
3301
|
+
* 清空后目录按类型补默认描述,与"从没写过"的展示一致。
|
|
3194
3302
|
*/
|
|
3195
3303
|
describeScope(scope , description , by ) {
|
|
3196
3304
|
const key = this.canonicalScope(scope)
|
|
3197
3305
|
this.assertScopeAllowed(key)
|
|
3198
3306
|
this.assertCustomScopeBudget(key)
|
|
3199
3307
|
const text = typeof description === 'string' ? description.replace(/\s+/g, ' ').trim() : ''
|
|
3200
|
-
if (text === '') throw memoryError('SCOPE_DESCRIPTION_EMPTY', '作用域用途不能为空')
|
|
3201
3308
|
if (text.length > MAX_SCOPE_DESCRIPTION_CHARS) {
|
|
3202
3309
|
throw memoryError('SCOPE_DESCRIPTION_TOO_LONG', `作用域用途最多 ${MAX_SCOPE_DESCRIPTION_CHARS} 字`)
|
|
3203
3310
|
}
|
|
3204
3311
|
return this.meta.put(key, { description: text, createdBy: by, status: 'ready' })
|
|
3205
3312
|
}
|
|
3206
3313
|
|
|
3314
|
+
/**
|
|
3315
|
+
* 设置 / 移除作用域别名(0.29.0)。别名只影响**显示与引用**,不改变作用域键:
|
|
3316
|
+
* 项目作用域的身份永远是 `project:<绝对路径>`,自定义作用域永远是名字本身。
|
|
3317
|
+
*
|
|
3318
|
+
* - 非空名字 → `ScopeNames.claim`(统一校验、去重;重名报 SCOPE_NAME_TAKEN);
|
|
3319
|
+
* - 空串 / undefined → 移除别名,显示名回退到键本身(项目路径 / 自定义名);
|
|
3320
|
+
* - global 与 session 不支持别名:它们的显示名跟随类型与会话 id,改名只会让
|
|
3321
|
+
* 同一个域出现两种叫法,没有收益。
|
|
3322
|
+
*/
|
|
3323
|
+
renameScope(scope , name ) {
|
|
3324
|
+
const key = this.canonicalScope(scope)
|
|
3325
|
+
this.assertScopeAllowed(key)
|
|
3326
|
+
const kind = scopeKind(key)
|
|
3327
|
+
if (kind === 'global' || kind === 'session') {
|
|
3328
|
+
throw memoryError('SCOPE_NAME_UNSUPPORTED', 'global 与 session 作用域不支持别名:它们的显示名跟随类型与会话 id(项目 / 自定义作用域可以设别名)')
|
|
3329
|
+
}
|
|
3330
|
+
const text = typeof name === 'string' ? name.replace(/\s+/g, ' ').trim() : ''
|
|
3331
|
+
if (text === '') {
|
|
3332
|
+
this.names.release(key)
|
|
3333
|
+
return { scope: key }
|
|
3334
|
+
}
|
|
3335
|
+
return { scope: key, name: this.names.claim(key, text) }
|
|
3336
|
+
}
|
|
3337
|
+
|
|
3207
3338
|
/** 名字 → 作用域键:读路径允许直接用别名找作用域。 */
|
|
3208
3339
|
scopeKeyForName(name ) {
|
|
3209
3340
|
return this.names.scopeOf(name)
|
|
@@ -3239,8 +3370,9 @@ export class LocalMemoryService {
|
|
|
3239
3370
|
* 漏掉别的进程(上一次会话、另一个 agent)写下的作用域。
|
|
3240
3371
|
* 排序键是"最近活跃"(`updatedAt`):面板据此只给最近活跃的前 N 个会话 scope 查标题。
|
|
3241
3372
|
*/
|
|
3242
|
-
scopeStats()
|
|
3373
|
+
scopeStats() {
|
|
3243
3374
|
this.discoverScopes()
|
|
3375
|
+
const archived = this.archivedSessionScopes()
|
|
3244
3376
|
return this.store.allScopes()
|
|
3245
3377
|
.map((s) => ({
|
|
3246
3378
|
scope: s.scope,
|
|
@@ -3248,6 +3380,7 @@ export class LocalMemoryService {
|
|
|
3248
3380
|
records: s.records.size,
|
|
3249
3381
|
pendingEmbeds: s.embedPending.size,
|
|
3250
3382
|
updatedAt: [...s.records.values()].reduce((max, r) => Math.max(max, r.updatedAt), 0),
|
|
3383
|
+
...(archived.has(s.scope) ? { archived: true } : {}),
|
|
3251
3384
|
}))
|
|
3252
3385
|
.sort((a, b) => b.updatedAt - a.updatedAt || b.records - a.records || a.scope.localeCompare(b.scope))
|
|
3253
3386
|
}
|
|
@@ -3296,6 +3429,7 @@ export class LocalMemoryService {
|
|
|
3296
3429
|
|
|
3297
3430
|
const out = []
|
|
3298
3431
|
const evaluate = this.accessEvaluator(agent)
|
|
3432
|
+
const archivedScopes = this.archivedSessionScopes()
|
|
3299
3433
|
for (const scope of keys) {
|
|
3300
3434
|
// 目录发现本身也是一条授权判定:作用域类型被关闭时连名字/用途都不出现。
|
|
3301
3435
|
if (!evaluate({ scope, action: 'discover' }).allowed) continue
|
|
@@ -3306,39 +3440,46 @@ export class LocalMemoryService {
|
|
|
3306
3440
|
// 别的会话的 session scope 默认不进目录:它们没有用途、也不该被 Agent 主动申请,
|
|
3307
3441
|
// 全列出来只会把目录变成历史会话列表。有元数据(被人描述过)则保留。
|
|
3308
3442
|
if (!inScope && meta === undefined && scopeKind(scope) === 'session') continue
|
|
3443
|
+
// 0.30.0:归档会话的作用域保留在目录里,但读写/申请全部冻结 —— 这不是权限
|
|
3444
|
+
// 边界,而是会话生命周期:恢复会话后自动复原,不需要任何额外操作。
|
|
3445
|
+
const archived = archivedScopes.has(scope)
|
|
3309
3446
|
const readDecision = evaluate({ scope, action: 'read' })
|
|
3310
3447
|
const writeDecision = evaluate({ scope, action: 'write' })
|
|
3311
|
-
const readable = readDecision.allowed
|
|
3312
|
-
const writable = writeDecision.allowed
|
|
3448
|
+
const readable = !archived && readDecision.allowed
|
|
3449
|
+
const writable = !archived && writeDecision.allowed
|
|
3313
3450
|
// 只有“整个作用域不在范围内”(SCOPE_NOT_IN_RANGE)才可申请;absolute 与
|
|
3314
|
-
// 用户显式关掉的 read/write 都不会 requestable
|
|
3315
|
-
const canRequestRead = !readable && readDecision.requestable
|
|
3316
|
-
const canRequestWrite = !writable && writeDecision.requestable
|
|
3317
|
-
const accessStatus =
|
|
3318
|
-
|
|
3319
|
-
|
|
3320
|
-
|
|
3321
|
-
|
|
3322
|
-
? 'read-only'
|
|
3323
|
-
: 'restricted'
|
|
3324
|
-
: policy.source === 'default'
|
|
3325
|
-
? 'default'
|
|
3326
|
-
: 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
|
|
3327
3459
|
? 'read-write'
|
|
3328
3460
|
: readable
|
|
3329
3461
|
? 'read-only'
|
|
3330
3462
|
: 'restricted'
|
|
3463
|
+
: policy.source === 'default'
|
|
3464
|
+
? 'default'
|
|
3465
|
+
: readable && writable
|
|
3466
|
+
? 'read-write'
|
|
3467
|
+
: readable
|
|
3468
|
+
? 'read-only'
|
|
3469
|
+
: 'restricted'
|
|
3331
3470
|
const name = meta?.name ?? this.names.nameOf(scope)
|
|
3332
3471
|
const initialRecords = policy.level === 'absolute' ? undefined : this.initialRecordsOf(scope)
|
|
3333
3472
|
// 0.27.0:scope-meta 没写用途时按作用域类型给默认描述,
|
|
3334
3473
|
// 让目录里每一条都自带「这个域是干什么的」。
|
|
3335
|
-
|
|
3474
|
+
// 0.29.0:被显式清空(描述为空串)与从没写过同样处理,都回退到默认描述。
|
|
3475
|
+
const written = typeof meta?.description === 'string' ? meta.description.trim() : ''
|
|
3476
|
+
const description = written !== '' ? written : defaultScopeDescription(scope)
|
|
3336
3477
|
out.push({
|
|
3337
3478
|
scope,
|
|
3338
3479
|
...(name !== undefined ? { name } : {}),
|
|
3339
3480
|
...(description !== '' ? { description } : {}),
|
|
3340
3481
|
origin: meta?.origin ?? scopeKind(scope),
|
|
3341
|
-
...(inScope ? { records: stat?.records ?? 0 } : {}),
|
|
3482
|
+
...(inScope || archived ? { records: stat?.records ?? 0 } : {}),
|
|
3342
3483
|
inScope,
|
|
3343
3484
|
readable,
|
|
3344
3485
|
writable,
|
|
@@ -3346,6 +3487,7 @@ export class LocalMemoryService {
|
|
|
3346
3487
|
canRequestWrite,
|
|
3347
3488
|
accessStatus,
|
|
3348
3489
|
updatedAt: stat?.updatedAt ?? meta?.updatedAt ?? 0,
|
|
3490
|
+
...(archived ? { archived: true } : {}),
|
|
3349
3491
|
...(initialRecords !== undefined ? { initialRecords } : {}),
|
|
3350
3492
|
})
|
|
3351
3493
|
}
|
|
@@ -3502,8 +3644,10 @@ export class LocalMemoryService {
|
|
|
3502
3644
|
process.removeListener('exit', this.exitHook)
|
|
3503
3645
|
this.exitHook = null
|
|
3504
3646
|
}
|
|
3505
|
-
|
|
3506
|
-
this.
|
|
3647
|
+
// 归档会话作用域在归档期间冻结:dispose 时也不做过期清理 / 容量淘汰。
|
|
3648
|
+
const frozen = (scope ) => this.isArchivedSessionScope(scope)
|
|
3649
|
+
this.store.purgeExpired(undefined, frozen)
|
|
3650
|
+
this.store.enforceLimit(this.cfg.maxRecordsPerScope, undefined, frozen)
|
|
3507
3651
|
this.store.close()
|
|
3508
3652
|
}
|
|
3509
3653
|
|
package/lib/provider/store.js
CHANGED
|
@@ -849,10 +849,16 @@ export class MemoryStore {
|
|
|
849
849
|
return ids
|
|
850
850
|
}
|
|
851
851
|
|
|
852
|
-
/**
|
|
853
|
-
|
|
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) => ({
|