@restregular/dsh-memory 0.31.0 → 0.33.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 +24 -0
- package/README.md +7 -4
- package/lib/client.js +310 -49
- package/lib/inject/apply.js +21 -4
- package/lib/inject/inject.js +3 -3
- package/lib/panel.js +273 -2
- package/lib/provider/apply.js +100 -0
- package/lib/provider/approval-audit.js +9 -1
- package/lib/provider/blocks.js +279 -0
- package/lib/provider/index.js +629 -29
- package/lib/provider/scope-archive.js +229 -0
- package/lib/tools/apply.js +6 -1
- package/lib/tools/index.js +217 -1
- package/lib/tools/render.js +50 -2
- package/locale/en.json +1 -1
- package/locale/zh.json +1 -1
- package/package.json +1 -1
package/lib/provider/index.js
CHANGED
|
@@ -37,8 +37,15 @@ import {
|
|
|
37
37
|
|
|
38
38
|
} from './session-visibility.js'
|
|
39
39
|
import { WorkspaceBindingsStore } from './workspace-bindings.js'
|
|
40
|
+
import {
|
|
41
|
+
MemoryBlocksStore,
|
|
42
|
+
blockMatchesRecord,
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
} from './blocks.js'
|
|
46
|
+
import { ScopeArchiveStore, } from './scope-archive.js'
|
|
40
47
|
import { SessionGrantsStore, } from './grants.js'
|
|
41
|
-
import { ApprovalAuditStore,
|
|
48
|
+
import { ApprovalAuditStore, } from './approval-audit.js'
|
|
42
49
|
import {
|
|
43
50
|
authorize as authorizeAccess,
|
|
44
51
|
|
|
@@ -583,6 +590,18 @@ export function resolveConfig(config = {}) {
|
|
|
583
590
|
|
|
584
591
|
|
|
585
592
|
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
|
|
604
|
+
|
|
586
605
|
|
|
587
606
|
|
|
588
607
|
|
|
@@ -619,9 +638,10 @@ export function resolveConfig(config = {}) {
|
|
|
619
638
|
|
|
620
639
|
|
|
621
640
|
|
|
622
|
-
|
|
641
|
+
|
|
642
|
+
|
|
623
643
|
|
|
624
|
-
|
|
644
|
+
|
|
625
645
|
|
|
626
646
|
|
|
627
647
|
|
|
@@ -640,7 +660,7 @@ export function resolveConfig(config = {}) {
|
|
|
640
660
|
|
|
641
661
|
|
|
642
662
|
|
|
643
|
-
|
|
663
|
+
|
|
644
664
|
|
|
645
665
|
|
|
646
666
|
|
|
@@ -683,6 +703,75 @@ export function resolveConfig(config = {}) {
|
|
|
683
703
|
|
|
684
704
|
|
|
685
705
|
|
|
706
|
+
/**
|
|
707
|
+
* 把审批答复归一成对象形态(0.32.0)。
|
|
708
|
+
*
|
|
709
|
+
* `requestOutOfScope` 是可以被测试 / 外部实现替换的注入点,历史上允许直接返回
|
|
710
|
+
* 裸字符串(`'allowed-once'` / `'rejected'` / `'timeout'`)。新代码一律按对象读取,
|
|
711
|
+
* 但为了让老实现继续工作,这里统一折一次。
|
|
712
|
+
*/
|
|
713
|
+
export function normalizeApprovalAnswer(raw ) {
|
|
714
|
+
if (typeof raw === 'string') {
|
|
715
|
+
const outcome = raw === 'allowed-once' || raw === 'rejected' || raw === 'timeout'
|
|
716
|
+
? raw
|
|
717
|
+
: 'rejected'
|
|
718
|
+
return { outcome }
|
|
719
|
+
}
|
|
720
|
+
if (raw !== null && typeof raw === 'object') {
|
|
721
|
+
const item = raw
|
|
722
|
+
const outcome = item.outcome === 'allowed-once' || item.outcome === 'rejected' || item.outcome === 'timeout'
|
|
723
|
+
? item.outcome
|
|
724
|
+
: 'rejected'
|
|
725
|
+
const ids = Array.isArray(item.allowedIds)
|
|
726
|
+
? [...new Set(item.allowedIds.map(String).filter((id) => id !== ''))]
|
|
727
|
+
: undefined
|
|
728
|
+
return ids !== undefined ? { outcome, allowedIds: ids } : { outcome }
|
|
729
|
+
}
|
|
730
|
+
return { outcome: 'rejected' }
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/** 屏蔽的目标:一个作用域,或一条记录(完整 id / 前缀)。 */
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
|
|
738
|
+
|
|
739
|
+
|
|
740
|
+
/** Agent 发起的一次屏蔽请求(0.32.0)。 */
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
/** 屏蔽结果。`outcome='blocked'` 表示这些目标现在确实处于屏蔽状态。 */
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
|
|
759
|
+
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
/** 一条屏蔽的对外视图(面板 / 工具)。 */
|
|
763
|
+
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
|
|
774
|
+
|
|
686
775
|
|
|
687
776
|
|
|
688
777
|
|
|
@@ -690,9 +779,16 @@ export function resolveConfig(config = {}) {
|
|
|
690
779
|
|
|
691
780
|
/** 普通限制下一次审批最多等多久;超时按拒绝处理(工具不会无限挂住)。 */
|
|
692
781
|
const VISIBILITY_APPROVAL_TTL_MS = 5 * 60_000
|
|
782
|
+
|
|
693
783
|
/** 审批浮层一次最多展示多少条范围外命中(工具侧仍会返回全部允许的命中)。 */
|
|
694
784
|
const VISIBILITY_APPROVAL_MAX_ITEMS = 20
|
|
695
785
|
|
|
786
|
+
/**
|
|
787
|
+
* 归档对账的节流窗口(0.32.0)。宿主工作区清单每次目录/统计查询前对一次账,
|
|
788
|
+
* 5 秒窗口足够覆盖连续的面板轮询,又不会把 `workspaceRegistry.list()` 读成热路径。
|
|
789
|
+
*/
|
|
790
|
+
const ARCHIVE_RECONCILE_INTERVAL_MS = 5_000
|
|
791
|
+
|
|
696
792
|
/**
|
|
697
793
|
* 没有显式覆盖时的默认限制等级(0.20.0 起)。
|
|
698
794
|
* 默认范围 = 当前会话 + 当前项目 + global;范围外命中走一次审批。
|
|
@@ -727,6 +823,10 @@ export class LocalMemoryService {
|
|
|
727
823
|
bindings
|
|
728
824
|
/** 会话级授权(Agent 申请、用户批准);独立于 visibility 策略。 */
|
|
729
825
|
grants
|
|
826
|
+
/** 屏蔽(0.32.0):Agent 主动藏起无关记忆;会话级 + 工作区级两层。 */
|
|
827
|
+
blocks
|
|
828
|
+
/** 项目作用域归档(0.32.0):绑定工作区,工作区删除/重加自动归档/恢复。 */
|
|
829
|
+
archive
|
|
730
830
|
/** 权限申请 / 授权生命周期审计(approvals.jsonl,不含正文)。 */
|
|
731
831
|
approvalAudit
|
|
732
832
|
/** 可见域变更通知:下一次 pre-step 注入给 Agent 后删除(按会话)。 */
|
|
@@ -746,6 +846,13 @@ export class LocalMemoryService {
|
|
|
746
846
|
* 宿主侧 apply.ts 注入;没有时按「没有归档」处理(纯本地 / 测试环境)。
|
|
747
847
|
*/
|
|
748
848
|
archivedLookup
|
|
849
|
+
/**
|
|
850
|
+
* 宿主工作区存在性查询(0.32.0):`workspaceRegistry.resolveByPath` / `list()`
|
|
851
|
+
* 的惰性读取。返回 undefined = 宿主服务不可用,此时**不改动**归档状态。
|
|
852
|
+
*/
|
|
853
|
+
workspaceExists
|
|
854
|
+
/** 上一次归档对账的时间(节流用)。 */
|
|
855
|
+
archiveReconciledAt = 0
|
|
749
856
|
/** 注入半边的审计收集器(可选;没有注入半边时保持 null)。 */
|
|
750
857
|
injectAuditSink
|
|
751
858
|
|
|
@@ -763,6 +870,8 @@ export class LocalMemoryService {
|
|
|
763
870
|
this.visibility = new SessionVisibilityStore({ root: this.cfg.storeDir })
|
|
764
871
|
this.bindings = new WorkspaceBindingsStore({ root: this.cfg.storeDir })
|
|
765
872
|
this.grants = new SessionGrantsStore({ root: this.cfg.storeDir })
|
|
873
|
+
this.blocks = new MemoryBlocksStore({ root: this.cfg.storeDir })
|
|
874
|
+
this.archive = new ScopeArchiveStore({ root: this.cfg.storeDir })
|
|
766
875
|
this.approvalAudit = new ApprovalAuditStore({ root: this.cfg.storeDir })
|
|
767
876
|
this.rerankBudget = new DailyBudget(this.cfg.llm.dailyBudgetTokens)
|
|
768
877
|
this.store = new MemoryStore(this.cfg.storeDir, { onWarn: (m) => console.warn(`[dsh-memory-local] ${m}`) })
|
|
@@ -922,17 +1031,24 @@ export class LocalMemoryService {
|
|
|
922
1031
|
*
|
|
923
1032
|
* 默认排除已被 supersede 的旧记录;需要历史/审计时显式 includeSuperseded。
|
|
924
1033
|
*/
|
|
925
|
-
listSync(input
|
|
1034
|
+
listSync(input = {}) {
|
|
926
1035
|
const wanted = (input.scopes ?? []).map((scope) => this.canonicalScope(scope))
|
|
927
1036
|
// 注入侧必须按可见域的类型白名单过滤;工具侧不传 agent(它需要范围外命中来发起审批)。
|
|
928
1037
|
const visibleFilter = input.agent !== undefined ? this.visibilityFilterFor(input.agent) : undefined
|
|
1038
|
+
// 0.32.0:被屏蔽的作用域 / 记录不参与枚举与注入(规则段、过程索引、标签召回同一条路径)。
|
|
1039
|
+
const blockedFilter = this.blockFilter({
|
|
1040
|
+
...(input.agent !== undefined ? { agent: input.agent } : {}),
|
|
1041
|
+
...(input.sessionId !== undefined ? { sessionId: input.sessionId } : {}),
|
|
1042
|
+
scopes: wanted,
|
|
1043
|
+
})
|
|
929
1044
|
const seen = new Set ()
|
|
930
1045
|
const stores = []
|
|
931
|
-
const archivedScopes = this.
|
|
1046
|
+
const archivedScopes = this.archivedScopeKeys()
|
|
932
1047
|
for (const scope of wanted) {
|
|
933
1048
|
if (seen.has(scope)) continue
|
|
934
1049
|
seen.add(scope)
|
|
935
1050
|
// 0.30.0:归档会话作用域在归档期间不参与枚举 / 注入。
|
|
1051
|
+
// 0.32.0:项目作用域归档后同理(工作区被删除时自动标 dormant,仍然不参与)。
|
|
936
1052
|
if (archivedScopes.has(scope)) continue
|
|
937
1053
|
const s = this.store.peek(scope)
|
|
938
1054
|
if (s) stores.push(s)
|
|
@@ -947,6 +1063,7 @@ export class LocalMemoryService {
|
|
|
947
1063
|
if (!input.includeExpired && rec.expiredAt !== undefined && rec.expiredAt < now) continue
|
|
948
1064
|
if (input.kind !== undefined && rec.kind !== input.kind) continue
|
|
949
1065
|
if (visibleFilter !== undefined && !visibleFilter(rec.scope, rec.kind)) continue
|
|
1066
|
+
if (blockedFilter !== undefined && blockedFilter({ id: rec.id, scope: rec.scope })) continue
|
|
950
1067
|
out.push(rec)
|
|
951
1068
|
}
|
|
952
1069
|
}
|
|
@@ -1360,7 +1477,7 @@ export class LocalMemoryService {
|
|
|
1360
1477
|
* 恢复会话后记忆原样回来;恢复后下一次 housekeeping 再按原规则清理。
|
|
1361
1478
|
*/
|
|
1362
1479
|
runHousekeeping() {
|
|
1363
|
-
const frozen = (scope ) => this.
|
|
1480
|
+
const frozen = (scope ) => this.isArchivedScope(scope)
|
|
1364
1481
|
const purged = this.store.purgeExpired(Date.now(), frozen)
|
|
1365
1482
|
const evicted = this.store.enforceLimit(this.cfg.maxRecordsPerScope, Date.now(), frozen)
|
|
1366
1483
|
return { purged, evicted }
|
|
@@ -1592,12 +1709,101 @@ export class LocalMemoryService {
|
|
|
1592
1709
|
return out
|
|
1593
1710
|
}
|
|
1594
1711
|
|
|
1712
|
+
/**
|
|
1713
|
+
* 所有**已归档作用域键**(0.32.0 起含项目作用域)。
|
|
1714
|
+
*
|
|
1715
|
+
* 会话作用域由宿主生命周期驱动(0.30.0),项目作用域由本服务持久化
|
|
1716
|
+
* (`archive/project.json`)。检索 / 枚举 / 统计统一问这一个集合,
|
|
1717
|
+
* 所以"归档后立刻搜不到、恢复后立刻回来"对两者都成立。
|
|
1718
|
+
*/
|
|
1719
|
+
archivedScopeKeys() {
|
|
1720
|
+
const out = this.archivedSessionScopes()
|
|
1721
|
+
// dormant 也算归档:工作区暂时被删掉,记忆仍然保留、仍然不参与检索。
|
|
1722
|
+
for (const entry of this.archive.list()) out.add(this.canonicalScope(entry.scope))
|
|
1723
|
+
return out
|
|
1724
|
+
}
|
|
1725
|
+
|
|
1595
1726
|
/** 这个作用域是否是已归档会话的作用域(只有 session: 键会命中)。 */
|
|
1596
1727
|
isArchivedSessionScope(scope ) {
|
|
1597
1728
|
if (typeof scope !== 'string' || !scope.startsWith('session:')) return false
|
|
1598
1729
|
return this.archivedSessionIds().has(scope.slice('session:'.length))
|
|
1599
1730
|
}
|
|
1600
1731
|
|
|
1732
|
+
/** 这个作用域是否已归档(会话作用域或项目作用域)。 */
|
|
1733
|
+
isArchivedScope(scope ) {
|
|
1734
|
+
if (typeof scope !== 'string' || scope === '') return false
|
|
1735
|
+
if (this.isArchivedSessionScope(scope)) return true
|
|
1736
|
+
const key = this.canonicalScope(scope)
|
|
1737
|
+
return this.archive.has(key)
|
|
1738
|
+
}
|
|
1739
|
+
|
|
1740
|
+
/**
|
|
1741
|
+
* 归档一个项目作用域(0.32.0)。只有 `project:` 键可归档 —— global 与自定义
|
|
1742
|
+
* 作用域的生命周期不属于某个工作区,归档它们只会变成"悄悄藏起来"。
|
|
1743
|
+
*/
|
|
1744
|
+
archiveScope(scope , by , note ) {
|
|
1745
|
+
const key = this.canonicalScope(scope)
|
|
1746
|
+
if (!key.startsWith('project:')) {
|
|
1747
|
+
throw memoryError(
|
|
1748
|
+
'SCOPE_NOT_ARCHIVABLE',
|
|
1749
|
+
'只有会话作用域(随会话生命周期)与项目作用域(绑定工作区)可以归档;global 与自定义作用域不支持归档',
|
|
1750
|
+
)
|
|
1751
|
+
}
|
|
1752
|
+
const entry = this.archive.archive(key, by, note)
|
|
1753
|
+
if (entry === undefined) throw memoryError('SCOPE_NOT_ARCHIVABLE', `无法归档作用域 ${key}`)
|
|
1754
|
+
return entry
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
/** 解除项目作用域归档;返回是否真的解除了一条。 */
|
|
1758
|
+
restoreScope(scope ) {
|
|
1759
|
+
const key = this.canonicalScope(scope)
|
|
1760
|
+
return this.archive.restore(key)
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
/** 已归档的项目作用域清单(面板 / 目录用)。 */
|
|
1764
|
+
archivedProjectScopes() {
|
|
1765
|
+
return this.archive.list()
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
/**
|
|
1769
|
+
* 工作区存在性对账(0.32.0):工作区从侧边栏删除时把归档标记为 dormant,
|
|
1770
|
+
* 重新加入时**自动解除归档**。`exists` 由宿主服务提供(`workspaceRegistry`),
|
|
1771
|
+
* 拿不到时保持现状。
|
|
1772
|
+
*/
|
|
1773
|
+
reconcileArchivedScopes(exists ) {
|
|
1774
|
+
return this.archive.reconcile(exists)
|
|
1775
|
+
}
|
|
1776
|
+
|
|
1777
|
+
/**
|
|
1778
|
+
* 宿主工作区存在性查询(0.32.0)。返回 undefined = 宿主服务不可用
|
|
1779
|
+
* (这时**不能**改动任何归档状态)。
|
|
1780
|
+
*/
|
|
1781
|
+
setWorkspaceExistsLookup(lookup ) {
|
|
1782
|
+
this.workspaceExists = lookup
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
/**
|
|
1786
|
+
* 按宿主工作区清单对账归档状态(节流:同一个 5 秒窗口只跑一次)。
|
|
1787
|
+
*
|
|
1788
|
+
* 为什么不监听事件:宿主的 `workspace/delete` / `workspace/create` 是 UI 层
|
|
1789
|
+
* emit,不保证在服务层出现;而 `workspaceRegistry.list()` 是持久状态,读它
|
|
1790
|
+
* 才是唯一可靠的来源。
|
|
1791
|
+
*
|
|
1792
|
+
* @param force 跳过 5 秒节流(测试与用户手动「刷新」用)。
|
|
1793
|
+
* @returns 本次被自动解除归档的作用域(第一次调用时可能为空)。
|
|
1794
|
+
*/
|
|
1795
|
+
reconcileArchivedScopesNow(force = false) {
|
|
1796
|
+
const now = Date.now()
|
|
1797
|
+
if (!force && now - this.archiveReconciledAt < ARCHIVE_RECONCILE_INTERVAL_MS) return []
|
|
1798
|
+
this.archiveReconciledAt = now
|
|
1799
|
+
if (this.workspaceExists === undefined) return []
|
|
1800
|
+
return this.archive.reconcile((scope) => {
|
|
1801
|
+
const present = this.workspaceExists?.(scope)
|
|
1802
|
+
// undefined = 宿主不可用:把"未知"当作"还在",避免误删用户的归档。
|
|
1803
|
+
return present !== false
|
|
1804
|
+
})
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1601
1807
|
/**
|
|
1602
1808
|
* 会话创建时**系统自动建立**会话作用域(0.30.0)。
|
|
1603
1809
|
*
|
|
@@ -1625,7 +1831,7 @@ export class LocalMemoryService {
|
|
|
1625
1831
|
const out = []
|
|
1626
1832
|
// 已归档会话的作用域不在「可见域」里:用户归档前勾选过也一并剔除,
|
|
1627
1833
|
// 恢复会话后规则会自动重新生效(规则本身没被改写)。
|
|
1628
|
-
const archived = this.
|
|
1834
|
+
const archived = this.archivedScopeKeys()
|
|
1629
1835
|
for (const raw of rules) {
|
|
1630
1836
|
const scope = this.canonicalScope(raw.scope)
|
|
1631
1837
|
if (archived.has(scope)) continue
|
|
@@ -1691,13 +1897,14 @@ export class LocalMemoryService {
|
|
|
1691
1897
|
* 沿 parentSession 链收集 grant(当前会话在前、父会话在后)。
|
|
1692
1898
|
* 子 Agent 与父会话是同一个任务,应该继承父会话获批的权限。
|
|
1693
1899
|
*/
|
|
1694
|
-
|
|
1695
|
-
|
|
1900
|
+
/** 会话本身 + 沿 parentSession 链的祖先(当前会话在前)。 */
|
|
1901
|
+
sessionChain(sessionId ) {
|
|
1902
|
+
const out = []
|
|
1696
1903
|
const seen = new Set ()
|
|
1697
1904
|
let id = sessionId
|
|
1698
1905
|
while (id !== '' && !seen.has(id)) {
|
|
1699
1906
|
seen.add(id)
|
|
1700
|
-
out.push(
|
|
1907
|
+
out.push(id)
|
|
1701
1908
|
let parent
|
|
1702
1909
|
try {
|
|
1703
1910
|
const session = this.sessionLookup?.(id)
|
|
@@ -1710,6 +1917,12 @@ export class LocalMemoryService {
|
|
|
1710
1917
|
return out
|
|
1711
1918
|
}
|
|
1712
1919
|
|
|
1920
|
+
resolveGrantChain(sessionId ) {
|
|
1921
|
+
const out = []
|
|
1922
|
+
for (const id of this.sessionChain(sessionId)) out.push(...this.grants.list(id))
|
|
1923
|
+
return out
|
|
1924
|
+
}
|
|
1925
|
+
|
|
1713
1926
|
/**
|
|
1714
1927
|
* 当前 agent 的有效 grant:作用域仍启用、作用域代次一致(同名重建不复用旧授权)。
|
|
1715
1928
|
* `grants.list()` 已过滤撤销 / 过期;这里再按 provider 的 canonicalScope 统一键。
|
|
@@ -1760,7 +1973,7 @@ export class LocalMemoryService {
|
|
|
1760
1973
|
/** 没有显式覆盖时的默认可见域范围(基础三路 + 工作区绑定的额外作用域)。 */
|
|
1761
1974
|
defaultReadScopes(agent ) {
|
|
1762
1975
|
const out = this.baseDefaultScopes(agent)
|
|
1763
|
-
const archived = this.
|
|
1976
|
+
const archived = this.archivedScopeKeys()
|
|
1764
1977
|
const push = (scope ) => {
|
|
1765
1978
|
if (archived.has(scope)) return
|
|
1766
1979
|
if (out.includes(scope)) return
|
|
@@ -1849,7 +2062,7 @@ export class LocalMemoryService {
|
|
|
1849
2062
|
},
|
|
1850
2063
|
originOf: (scope) => scopeKind(scope),
|
|
1851
2064
|
grants: this.validGrants(agent),
|
|
1852
|
-
archivedScopes: this.
|
|
2065
|
+
archivedScopes: this.archivedScopeKeys(),
|
|
1853
2066
|
}
|
|
1854
2067
|
}
|
|
1855
2068
|
|
|
@@ -2057,8 +2270,8 @@ export class LocalMemoryService {
|
|
|
2057
2270
|
|
|
2058
2271
|
|
|
2059
2272
|
|
|
2060
|
-
|
|
2061
|
-
|
|
2273
|
+
|
|
2274
|
+
|
|
2062
2275
|
|
|
2063
2276
|
|
|
2064
2277
|
,
|
|
@@ -2210,6 +2423,344 @@ export class LocalMemoryService {
|
|
|
2210
2423
|
return true
|
|
2211
2424
|
}
|
|
2212
2425
|
|
|
2426
|
+
// ---------------------------------------------------------------- 屏蔽(0.32.0)
|
|
2427
|
+
|
|
2428
|
+
/** 当前 agent 的会话链(自身 + 祖先),用于合并会话级屏蔽。 */
|
|
2429
|
+
blockSessionIds(agent ) {
|
|
2430
|
+
const sessionId = this.rawSessionIdOf(agent)
|
|
2431
|
+
if (sessionId === undefined) return []
|
|
2432
|
+
return this.sessionChain(sessionId)
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
/** 当前 agent 的工作区键(= 项目作用域键);拿不到会话时用兜底项目键。 */
|
|
2436
|
+
blockWorkspaceKey(agent ) {
|
|
2437
|
+
return this.workspaceKeyFor(agent)
|
|
2438
|
+
}
|
|
2439
|
+
|
|
2440
|
+
/**
|
|
2441
|
+
* 当前 agent 生效的**全部**屏蔽:会话级(含父链)+ 工作区级。
|
|
2442
|
+
*
|
|
2443
|
+
* 工作区级屏蔽对所有会话生效(含历史与将来),会话级只对当前会话链生效。
|
|
2444
|
+
* 结果按层级排:工作区级在前(它更"重")。
|
|
2445
|
+
*/
|
|
2446
|
+
blocksFor(agent ) {
|
|
2447
|
+
const out = []
|
|
2448
|
+
const seen = new Set ()
|
|
2449
|
+
const push = (block ) => {
|
|
2450
|
+
const key = block.scope !== undefined
|
|
2451
|
+
? `${block.level}\0scope\0${block.scope}`
|
|
2452
|
+
: `${block.level}\0record\0${block.recordId ?? block.prefix ?? ''}`
|
|
2453
|
+
if (seen.has(key)) return
|
|
2454
|
+
seen.add(key)
|
|
2455
|
+
out.push(block)
|
|
2456
|
+
}
|
|
2457
|
+
const workspace = this.blockWorkspaceKey(agent)
|
|
2458
|
+
if (workspace !== '') {
|
|
2459
|
+
for (const block of this.blocks.list('workspace', workspace)) push(block)
|
|
2460
|
+
}
|
|
2461
|
+
for (const sessionId of this.blockSessionIds(agent)) {
|
|
2462
|
+
for (const block of this.blocks.list('session', sessionId)) push(block)
|
|
2463
|
+
}
|
|
2464
|
+
return out
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
/** 这个作用域当前是否被屏蔽(会话级或工作区级)。 */
|
|
2468
|
+
isScopeBlocked(agent , scope ) {
|
|
2469
|
+
const key = this.canonicalScope(scope)
|
|
2470
|
+
return this.blocksFor(agent).some((block) => block.scope === key)
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
/** 这条记录当前是否被屏蔽(作用域级屏蔽也算)。 */
|
|
2474
|
+
isRecordBlocked(agent , record ) {
|
|
2475
|
+
const scope = this.canonicalScope(record.scope)
|
|
2476
|
+
return this.blocksFor(agent).some((block) => blockMatchesRecord(block, { id: record.id, scope }))
|
|
2477
|
+
}
|
|
2478
|
+
|
|
2479
|
+
/**
|
|
2480
|
+
* 批量过滤用的屏蔽判定(undefined = 没有屏蔽,调用方跳过过滤)。
|
|
2481
|
+
*
|
|
2482
|
+
* 与 `visibilityFilterFor` 一样:一次解析、多次判定。调用方可以给 `agent`
|
|
2483
|
+
* (工具 / 面板),也可以只给 `sessionId` + `scopes`(注入链路拿不到 agent
|
|
2484
|
+
* 对象,但有会话 id 与本次召回的作用域集合,工作区级屏蔽由其中的 project:
|
|
2485
|
+
* 键确定)。
|
|
2486
|
+
*/
|
|
2487
|
+
blockFilter(input ) {
|
|
2488
|
+
const blocks = []
|
|
2489
|
+
if (input.agent !== undefined) {
|
|
2490
|
+
blocks.push(...this.blocksFor(input.agent))
|
|
2491
|
+
} else {
|
|
2492
|
+
const sessionIds = typeof input.sessionId === 'string' && input.sessionId !== ''
|
|
2493
|
+
? this.sessionChain(input.sessionId)
|
|
2494
|
+
: []
|
|
2495
|
+
for (const sessionId of sessionIds) blocks.push(...this.blocks.list('session', sessionId))
|
|
2496
|
+
for (const scope of input.scopes ?? []) {
|
|
2497
|
+
const key = this.canonicalScope(scope)
|
|
2498
|
+
if (!key.startsWith('project:')) continue
|
|
2499
|
+
blocks.push(...this.blocks.list('workspace', key))
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
if (blocks.length === 0) return undefined
|
|
2503
|
+
const scopeBlocked = new Set ()
|
|
2504
|
+
const recordIds = new Set ()
|
|
2505
|
+
const prefixes = []
|
|
2506
|
+
for (const block of blocks) {
|
|
2507
|
+
if (block.scope !== undefined) scopeBlocked.add(this.canonicalScope(block.scope))
|
|
2508
|
+
else if (block.recordId !== undefined) recordIds.add(block.recordId)
|
|
2509
|
+
else if (block.prefix !== undefined) prefixes.push(block.prefix)
|
|
2510
|
+
}
|
|
2511
|
+
return (record) => {
|
|
2512
|
+
if (scopeBlocked.has(this.canonicalScope(record.scope))) return true
|
|
2513
|
+
if (recordIds.has(record.id)) return true
|
|
2514
|
+
return prefixes.some((prefix) => record.id.startsWith(prefix))
|
|
2515
|
+
}
|
|
2516
|
+
}
|
|
2517
|
+
|
|
2518
|
+
/** 面板 / 诊断:当前 agent 生效的屏蔽清单。 */
|
|
2519
|
+
listBlocks(agent ) {
|
|
2520
|
+
return this.blocksFor(agent).map((block) => ({ ...block }))
|
|
2521
|
+
}
|
|
2522
|
+
|
|
2523
|
+
/** 面板:某会话自己的会话级屏蔽(不含父链、不含工作区级)。 */
|
|
2524
|
+
sessionBlocks(sessionId ) {
|
|
2525
|
+
if (typeof sessionId !== 'string' || sessionId.trim() === '') return []
|
|
2526
|
+
return this.blocks.list('session', sessionId.trim()).map((block) => ({ ...block }))
|
|
2527
|
+
}
|
|
2528
|
+
|
|
2529
|
+
/** 面板:某工作区的工作区级屏蔽。 */
|
|
2530
|
+
workspaceBlocks(workspace ) {
|
|
2531
|
+
if (typeof workspace !== 'string' || workspace.trim() === '') return []
|
|
2532
|
+
// 工作区键就是项目作用域键,必须走同一套规范化:`project:D:/x` 与
|
|
2533
|
+
// `project:D:\x` 是同一个工作区(Windows 上尤其容易踩),否则面板会查到空。
|
|
2534
|
+
const key = this.canonicalScope(workspace.trim())
|
|
2535
|
+
return this.blocks.list('workspace', key).map((block) => ({ ...block }))
|
|
2536
|
+
}
|
|
2537
|
+
|
|
2538
|
+
/**
|
|
2539
|
+
* 解除一条屏蔽(面板 / 工具共用)。
|
|
2540
|
+
*
|
|
2541
|
+
* `level` 省略时先找会话级、再找工作区级 —— 用户点「解除」时不该被迫知道
|
|
2542
|
+
* 这条屏蔽当初是哪一层写下的。
|
|
2543
|
+
*
|
|
2544
|
+
* 面板可以显式给 `key`(会话 id 或 `project:<路径>`),这时不再从 agent 反推:
|
|
2545
|
+
* 面板是按「用户当前正在看的那一行」解除的,那个作用域不一定属于当前会话。
|
|
2546
|
+
*/
|
|
2547
|
+
unblock(
|
|
2548
|
+
agent ,
|
|
2549
|
+
match ,
|
|
2550
|
+
) {
|
|
2551
|
+
const out = []
|
|
2552
|
+
const levels = match.level !== undefined ? [match.level] : ['session', 'workspace']
|
|
2553
|
+
const explicit = typeof match.key === 'string' && match.key.trim() !== '' ? match.key.trim() : undefined
|
|
2554
|
+
for (const level of levels) {
|
|
2555
|
+
const keys = explicit !== undefined
|
|
2556
|
+
// 工作区级的 key 是项目作用域键,与 workspaceBlocks 一样要先规范化。
|
|
2557
|
+
? [level === 'workspace' ? this.canonicalScope(explicit) : explicit]
|
|
2558
|
+
: level === 'session' ? this.blockSessionIds(agent) : [this.blockWorkspaceKey(agent)]
|
|
2559
|
+
for (const key of keys) {
|
|
2560
|
+
if (key === '') continue
|
|
2561
|
+
const before = this.blocks.list(level, key)
|
|
2562
|
+
if (before.length === 0) continue
|
|
2563
|
+
// match.scope 也要过同一套规范化:面板传的是 `project:D:/x`,落盘的是
|
|
2564
|
+
// `project:D:\x`,不折一次就永远匹配不上(表现为「点了解除没反应」)。
|
|
2565
|
+
const canonicalMatch = match.scope !== undefined
|
|
2566
|
+
? { ...match, scope: this.canonicalScope(match.scope) }
|
|
2567
|
+
: match
|
|
2568
|
+
const after = this.blocks.remove(level, key, canonicalMatch)
|
|
2569
|
+
const removed = before.length - after.length
|
|
2570
|
+
if (removed > 0) out.push({ level, removed })
|
|
2571
|
+
}
|
|
2572
|
+
}
|
|
2573
|
+
return out
|
|
2574
|
+
}
|
|
2575
|
+
|
|
2576
|
+
/** 清空某一层级的全部屏蔽。 */
|
|
2577
|
+
clearBlocks(agent , level ) {
|
|
2578
|
+
const keys = level === 'session' ? this.blockSessionIds(agent) : [this.blockWorkspaceKey(agent)]
|
|
2579
|
+
let removed = 0
|
|
2580
|
+
for (const key of keys) {
|
|
2581
|
+
if (key === '') continue
|
|
2582
|
+
removed += this.blocks.list(level, key).length
|
|
2583
|
+
this.blocks.clear(level, key)
|
|
2584
|
+
}
|
|
2585
|
+
return removed
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
/**
|
|
2589
|
+
* Agent 请求屏蔽(0.32.0)。
|
|
2590
|
+
*
|
|
2591
|
+
* 审批矩阵(用户口径,不能改):
|
|
2592
|
+
* - 按**记录 id / 前缀**屏蔽 → 不弹审批(只是让这条记录不再打扰当前任务);
|
|
2593
|
+
* - 按**作用域**屏蔽 → 弹审批(会把整个域藏起来,影响面大);
|
|
2594
|
+
* - 目标是**规则**(kind='rule')→ 一律弹审批,**即使按 id 屏蔽**;
|
|
2595
|
+
* - **会话级** → 只影响本会话,不弹审批;
|
|
2596
|
+
* - **工作区级** → 影响该工作区所有会话(含历史与将来),一律弹审批。
|
|
2597
|
+
*/
|
|
2598
|
+
async requestBlock(agent , input ) {
|
|
2599
|
+
const level = input.level === 'workspace' ? 'workspace' : 'session'
|
|
2600
|
+
const sessionId = this.rawSessionIdOf(agent)
|
|
2601
|
+
const workspace = this.blockWorkspaceKey(agent)
|
|
2602
|
+
if (sessionId === undefined && level === 'session') {
|
|
2603
|
+
return { outcome: 'unavailable', blocks: [], records: [] }
|
|
2604
|
+
}
|
|
2605
|
+
if (workspace === '' && level === 'workspace') {
|
|
2606
|
+
return { outcome: 'unavailable', blocks: [], records: [] }
|
|
2607
|
+
}
|
|
2608
|
+
|
|
2609
|
+
// 目标解析:scope 直接规范化;id / 前缀先解析出真实记录(屏蔽规则要知道 kind)。
|
|
2610
|
+
const scopeTarget = typeof input.scope === 'string' && input.scope.trim() !== ''
|
|
2611
|
+
? this.canonicalScope(input.scope.trim())
|
|
2612
|
+
: undefined
|
|
2613
|
+
const idTarget = typeof input.id === 'string' && input.id.trim() !== '' ? input.id.trim() : undefined
|
|
2614
|
+
if (scopeTarget === undefined && idTarget === undefined) {
|
|
2615
|
+
return { outcome: 'unavailable', blocks: [], records: [] }
|
|
2616
|
+
}
|
|
2617
|
+
const records = []
|
|
2618
|
+
if (idTarget !== undefined) {
|
|
2619
|
+
for (const record of this.getByIds([idTarget])) {
|
|
2620
|
+
records.push({ id: record.id, scope: record.scope, kind: record.kind })
|
|
2621
|
+
}
|
|
2622
|
+
if (records.length === 0) {
|
|
2623
|
+
// 解析不到也允许记一条前缀屏蔽:目标可能是被归档/暂时读不到的记录,
|
|
2624
|
+
// 记下来后等它出现时自动生效,比直接报错更贴近"别再打扰我"的意图。
|
|
2625
|
+
records.push({ id: idTarget, scope: scopeTarget ?? '', kind: '' })
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
const containsRule = records.some((record) => record.kind === 'rule')
|
|
2629
|
+
|| (scopeTarget !== undefined && this.scopeHasRule(scopeTarget))
|
|
2630
|
+
|
|
2631
|
+
// 审批判定(用户口径,命中任一条就弹审批):
|
|
2632
|
+
// - 按**作用域**屏蔽 → 审批(整个域被藏起来,影响面大);
|
|
2633
|
+
// - **工作区级** → 审批(影响该工作区所有会话,含历史与将来);
|
|
2634
|
+
// - 目标是**规则**(kind='rule')→ 审批,即使按 id、即使是会话级 ——
|
|
2635
|
+
// 重要规则不能被静默丢弃。
|
|
2636
|
+
// 只有「会话级 + 按记录 id / 前缀 + 不是规则」这一种不审批:影响面只有本会话的
|
|
2637
|
+
// 一条记录,模型自己就能撤销,弹卡只会打断工作流。
|
|
2638
|
+
const needsApproval = scopeTarget !== undefined || level === 'workspace' || containsRule
|
|
2639
|
+
if (needsApproval) {
|
|
2640
|
+
const items = scopeTarget !== undefined
|
|
2641
|
+
? [{
|
|
2642
|
+
id: scopeTarget,
|
|
2643
|
+
scope: scopeTarget,
|
|
2644
|
+
kind: 'scope',
|
|
2645
|
+
content: this.scopeBlockSummary(scopeTarget),
|
|
2646
|
+
tags: [],
|
|
2647
|
+
importance: 1,
|
|
2648
|
+
updatedAt: Date.now(),
|
|
2649
|
+
reason: 'scope',
|
|
2650
|
+
}]
|
|
2651
|
+
: records.map((record) => ({
|
|
2652
|
+
id: record.id,
|
|
2653
|
+
scope: record.scope,
|
|
2654
|
+
kind: record.kind,
|
|
2655
|
+
content: this.blockRecordSummary(record.id),
|
|
2656
|
+
tags: [],
|
|
2657
|
+
importance: 1,
|
|
2658
|
+
updatedAt: Date.now(),
|
|
2659
|
+
reason: 'scope' ,
|
|
2660
|
+
}))
|
|
2661
|
+
const answer = normalizeApprovalAnswer(await this.requestOutOfScope(agent, {
|
|
2662
|
+
tool: input.tool ?? 'memory_block',
|
|
2663
|
+
action: 'write',
|
|
2664
|
+
query: scopeTarget ?? idTarget ?? '',
|
|
2665
|
+
...(input.description !== undefined ? { description: input.description } : {}),
|
|
2666
|
+
mode: 'block',
|
|
2667
|
+
items,
|
|
2668
|
+
...(input.signal !== undefined ? { signal: input.signal } : {}),
|
|
2669
|
+
}))
|
|
2670
|
+
if (answer.outcome !== 'allowed-once') {
|
|
2671
|
+
return { outcome: answer.outcome === 'timeout' ? 'timeout' : 'rejected', blocks: [], records, containsRule }
|
|
2672
|
+
}
|
|
2673
|
+
// 用户可能只勾了其中几条:只写被允许的那些。
|
|
2674
|
+
const allowed = answer.allowedIds !== undefined ? new Set(answer.allowedIds) : undefined
|
|
2675
|
+
if (allowed !== undefined) {
|
|
2676
|
+
if (scopeTarget !== undefined && !allowed.has(scopeTarget)) {
|
|
2677
|
+
return { outcome: 'rejected', blocks: [], records, containsRule }
|
|
2678
|
+
}
|
|
2679
|
+
if (allowed.size === 0) return { outcome: 'rejected', blocks: [], records, containsRule }
|
|
2680
|
+
}
|
|
2681
|
+
}
|
|
2682
|
+
|
|
2683
|
+
const now = Date.now()
|
|
2684
|
+
const createdBy = sessionId ?? 'panel'
|
|
2685
|
+
const written = []
|
|
2686
|
+
const write = (block ) => {
|
|
2687
|
+
const full = {
|
|
2688
|
+
id: `blk-${now.toString(36)}-${written.length + 1}`,
|
|
2689
|
+
level,
|
|
2690
|
+
createdAt: now,
|
|
2691
|
+
createdBy,
|
|
2692
|
+
...(input.reason !== undefined && input.reason.trim() !== '' ? { reason: input.reason.trim().slice(0, 500) } : {}),
|
|
2693
|
+
...block,
|
|
2694
|
+
}
|
|
2695
|
+
const key = level === 'session' ? sessionId : workspace
|
|
2696
|
+
const list = this.blocks.add(level, key, full)
|
|
2697
|
+
const stored = list.find((item) => item.scope === full.scope
|
|
2698
|
+
&& item.recordId === full.recordId
|
|
2699
|
+
&& item.prefix === full.prefix) ?? full
|
|
2700
|
+
written.push(stored)
|
|
2701
|
+
}
|
|
2702
|
+
if (scopeTarget !== undefined) write({ scope: scopeTarget })
|
|
2703
|
+
for (const record of records) {
|
|
2704
|
+
if (record.kind === '' && record.id === idTarget) {
|
|
2705
|
+
// 没解析到记录:只记前缀,等它出现时自动生效。
|
|
2706
|
+
write({ prefix: record.id })
|
|
2707
|
+
continue
|
|
2708
|
+
}
|
|
2709
|
+
// 记录级屏蔽写精确 id:解析到了就用 recordId,解析不到才退回 prefix
|
|
2710
|
+
//(前缀屏蔽会连带着命中同前缀的其它记录,精确 id 不会)。
|
|
2711
|
+
write({
|
|
2712
|
+
recordId: record.id,
|
|
2713
|
+
...(record.kind !== '' ? { recordKind: record.kind } : {}),
|
|
2714
|
+
})
|
|
2715
|
+
}
|
|
2716
|
+
return { outcome: 'blocked', blocks: written, records, containsRule }
|
|
2717
|
+
}
|
|
2718
|
+
|
|
2719
|
+
/** 某作用域里有没有规则(按作用域屏蔽时要提前告知用户"这会藏起规则")。 */
|
|
2720
|
+
scopeHasRule(scope ) {
|
|
2721
|
+
try {
|
|
2722
|
+
const store = this.store.peek(this.canonicalScope(scope))
|
|
2723
|
+
if (store === undefined) return false
|
|
2724
|
+
for (const record of store.records.values()) {
|
|
2725
|
+
if (record.kind === 'rule') return true
|
|
2726
|
+
}
|
|
2727
|
+
} catch { /* 读不到就当没有 */ }
|
|
2728
|
+
return false
|
|
2729
|
+
}
|
|
2730
|
+
|
|
2731
|
+
/** 审批卡上「按作用域屏蔽」那一行的正文。 */
|
|
2732
|
+
scopeBlockSummary(scope ) {
|
|
2733
|
+
const meta = this.meta.get(scope)
|
|
2734
|
+
const description = typeof meta?.description === 'string' && meta.description.trim() !== ''
|
|
2735
|
+
? meta.description.trim()
|
|
2736
|
+
: defaultScopeDescription(scope)
|
|
2737
|
+
let count = 0
|
|
2738
|
+
let rules = 0
|
|
2739
|
+
try {
|
|
2740
|
+
const store = this.store.peek(this.canonicalScope(scope))
|
|
2741
|
+
if (store !== undefined) {
|
|
2742
|
+
for (const record of store.records.values()) {
|
|
2743
|
+
count += 1
|
|
2744
|
+
if (record.kind === 'rule') rules += 1
|
|
2745
|
+
}
|
|
2746
|
+
}
|
|
2747
|
+
} catch { /* 统计失败就不写条数 */ }
|
|
2748
|
+
const parts = [`作用域 ${scope}`]
|
|
2749
|
+
if (description !== '') parts.push(description)
|
|
2750
|
+
if (count > 0) parts.push(`共 ${count} 条记忆${rules > 0 ? `(含 ${rules} 条规则)` : ''}`)
|
|
2751
|
+
parts.push('屏蔽后本会话检索 / 注入 / 目录都不再包含它,随时可以解除。')
|
|
2752
|
+
return parts.join('|')
|
|
2753
|
+
}
|
|
2754
|
+
|
|
2755
|
+
/** 审批卡上「按记录屏蔽」那一行的正文。 */
|
|
2756
|
+
blockRecordSummary(id ) {
|
|
2757
|
+
for (const record of this.getByIds([id])) {
|
|
2758
|
+
const content = record.content.length > 240 ? `${record.content.slice(0, 240)}…` : record.content
|
|
2759
|
+
return `${record.id.slice(0, 8)}|${record.kind}|${record.scope}|${content}`
|
|
2760
|
+
}
|
|
2761
|
+
return `${id}(暂时读不到正文;屏蔽会在它出现后生效)`
|
|
2762
|
+
}
|
|
2763
|
+
|
|
2213
2764
|
/**
|
|
2214
2765
|
* Agent 主动申请范围外作用域的会话权限(0.24.0 / D3)。
|
|
2215
2766
|
*
|
|
@@ -2456,7 +3007,7 @@ export class LocalMemoryService {
|
|
|
2456
3007
|
if (decision.allowed) return
|
|
2457
3008
|
switch (decision.reason) {
|
|
2458
3009
|
case 'SCOPE_ARCHIVED':
|
|
2459
|
-
throw memoryError('SCOPE_ARCHIVED', `作用域 ${scope}
|
|
3010
|
+
throw memoryError('SCOPE_ARCHIVED', `作用域 ${scope} 已归档:作用域与记忆被冻结,恢复后即可继续读写。会话作用域恢复会话即复原;项目作用域在面板里解除归档即复原。`)
|
|
2460
3011
|
case 'WRITE_DISABLED':
|
|
2461
3012
|
throw memoryError('SCOPE_READ_ONLY', `作用域 ${scope} 在本会话记忆范围里被标记为「不可写」(可读/可写按钮),本次写入/删除没有执行。如需写入,请让用户打开该作用域的「可写」状态。`)
|
|
2462
3013
|
case 'SCOPE_DISABLED':
|
|
@@ -2491,6 +3042,14 @@ export class LocalMemoryService {
|
|
|
2491
3042
|
}
|
|
2492
3043
|
// 自定义 scope 的**新建**受 scopes.custom.max 限制;写已存在的 scope 不受限。
|
|
2493
3044
|
this.assertCustomScopeBudget(scope)
|
|
3045
|
+
// 0.32.0:被屏蔽的作用域不再接受 Agent 写入 —— 写进去的记忆自己看不见,
|
|
3046
|
+
// 等于静默丢失。面板(不带 agent)不受此限:用户本人始终能往里写。
|
|
3047
|
+
if (saveAgent !== undefined && this.isScopeBlocked(saveAgent, scope)) {
|
|
3048
|
+
throw memoryError(
|
|
3049
|
+
'SCOPE_BLOCKED',
|
|
3050
|
+
`作用域 ${scope} 当前被屏蔽(检索与注入里看不到它),本次写入没有执行。如确需写入,请先解除屏蔽(面板「记录 → 作用域」里可以一键解除)。`,
|
|
3051
|
+
)
|
|
3052
|
+
}
|
|
2494
3053
|
let content = input.content?.trim()
|
|
2495
3054
|
if (!content) throw memoryError('CONTENT_EMPTY', 'content must be a non-empty string')
|
|
2496
3055
|
if (content.length > this.cfg.maxRecordChars) {
|
|
@@ -2709,7 +3268,7 @@ export class LocalMemoryService {
|
|
|
2709
3268
|
// 面板的语义检索复用这条流水线,但它是"看一眼"的浏览:不写访问计数,也不在
|
|
2710
3269
|
// 没有任何向量时白跑一次嵌入请求。这两个开关只给面板的 searchForPanel 用,
|
|
2711
3270
|
// 工具 / 注入调用不会传。
|
|
2712
|
-
const internal = query
|
|
3271
|
+
const internal = query
|
|
2713
3272
|
const allowVector = internal.vector !== false && this.cfg.vector.enabled && this.cfg.vector.configured
|
|
2714
3273
|
const includeSuperseded = internal.includeSuperseded === true
|
|
2715
3274
|
const includeExpired = internal.includeExpired === true
|
|
@@ -2722,6 +3281,15 @@ export class LocalMemoryService {
|
|
|
2722
3281
|
return allowed === undefined || allowed.length === 0 || allowed.includes(kind)
|
|
2723
3282
|
}
|
|
2724
3283
|
|
|
3284
|
+
// 0.32.0:屏蔽。检索侧按调用方给的 agent 解析(工具 / 面板),注入侧退化为
|
|
3285
|
+
// 「会话链 + 本次召回的作用域集合」—— 屏蔽的作用域连候选都不进,恢复屏蔽后
|
|
3286
|
+
// 不需要重建任何索引。
|
|
3287
|
+
const blockedFilter = this.blockFilter({
|
|
3288
|
+
...(internal.agent !== undefined ? { agent: internal.agent } : {}),
|
|
3289
|
+
...(internal.sessionId !== undefined ? { sessionId: internal.sessionId } : {}),
|
|
3290
|
+
...(queryScopes !== undefined ? { scopes: queryScopes } : query.scope !== undefined ? { scopes: [query.scope] } : {}),
|
|
3291
|
+
})
|
|
3292
|
+
|
|
2725
3293
|
// 候选 scope:显式 pin(单值)优先;否则用调用方给的合并集合(query.scopes,
|
|
2726
3294
|
// 工具 / 注入传"会话 + 项目 + global");两者都没有才退回旧的 project + global。
|
|
2727
3295
|
// `peek` 而不是 `scope`:读路径不创建,不存在的 scope 直接跳过。
|
|
@@ -2732,7 +3300,7 @@ export class LocalMemoryService {
|
|
|
2732
3300
|
: [this.defaultProjectScope, 'global']).map((scope) => this.canonicalScope(scope))
|
|
2733
3301
|
const stores = []
|
|
2734
3302
|
const seen = new Set ()
|
|
2735
|
-
const archivedScopes = this.
|
|
3303
|
+
const archivedScopes = this.archivedScopeKeys()
|
|
2736
3304
|
for (const scope of wanted) {
|
|
2737
3305
|
if (seen.has(scope)) continue // 按 scope key 去重(也顺手消掉 defaultScope==='global' 时的重复 push)
|
|
2738
3306
|
seen.add(scope)
|
|
@@ -2769,6 +3337,7 @@ export class LocalMemoryService {
|
|
|
2769
3337
|
if (!includeExpired && rec.expiredAt && rec.expiredAt < now) continue
|
|
2770
3338
|
if (!includeSuperseded && retired.has(rec.id)) continue
|
|
2771
3339
|
if (kindFilter !== undefined && !kindFilter(rec.scope, rec.kind)) continue
|
|
3340
|
+
if (blockedFilter !== undefined && blockedFilter({ id: rec.id, scope: rec.scope })) continue
|
|
2772
3341
|
const lex = lexicalScore(queryTokens, tokenSets[i] , stats)
|
|
2773
3342
|
if (lex <= 0) continue
|
|
2774
3343
|
// Weak-only overlap (no shared word/bigram) must clear the "real hit"
|
|
@@ -2808,6 +3377,7 @@ export class LocalMemoryService {
|
|
|
2808
3377
|
if (!rec) continue
|
|
2809
3378
|
if (!includeExpired && rec.expiredAt !== undefined && rec.expiredAt < now) continue
|
|
2810
3379
|
if (kindFilter !== undefined && !kindFilter(rec.scope, rec.kind)) continue
|
|
3380
|
+
if (blockedFilter !== undefined && blockedFilter({ id: rec.id, scope: rec.scope })) continue
|
|
2811
3381
|
if (!(rec.scope === 'global' && sim < this.cfg.globalThreshold)) {
|
|
2812
3382
|
candidates.set(id, { record: rec, recallScore: sim, isGlobal: rec.scope === 'global' })
|
|
2813
3383
|
}
|
|
@@ -2996,9 +3566,15 @@ export class LocalMemoryService {
|
|
|
2996
3566
|
* 一律走 `peek`,所以列一个不存在的 scope 只会得到空数组,不会造出文件。
|
|
2997
3567
|
*/
|
|
2998
3568
|
async list(input = {}) {
|
|
2999
|
-
const internal = input
|
|
3569
|
+
const internal = input
|
|
3000
3570
|
// 注入侧(摘要)传入 agent 时按可见域的类型白名单过滤;工具侧不传,以便处理范围外审批。
|
|
3001
3571
|
const visibleFilter = internal.agent !== undefined ? this.visibilityFilterFor(internal.agent) : undefined
|
|
3572
|
+
// 0.32.0:屏蔽(工具侧传 agent,注入侧传 sessionId + scopes)。
|
|
3573
|
+
const blockedFilter = this.blockFilter({
|
|
3574
|
+
...(internal.agent !== undefined ? { agent: internal.agent } : {}),
|
|
3575
|
+
...(internal.sessionId !== undefined ? { sessionId: internal.sessionId } : {}),
|
|
3576
|
+
...(input.scope !== undefined ? { scopes: [input.scope] } : input.scopes !== undefined ? { scopes: input.scopes } : {}),
|
|
3577
|
+
})
|
|
3002
3578
|
// 显式空集合 = 无可见范围(见 search 的同口径说明)。
|
|
3003
3579
|
if (input.scope === undefined && input.scopes !== undefined && input.scopes.length === 0) return []
|
|
3004
3580
|
const wanted = (input.scope !== undefined
|
|
@@ -3008,7 +3584,7 @@ export class LocalMemoryService {
|
|
|
3008
3584
|
: [this.defaultProjectScope]).map((scope) => this.canonicalScope(scope))
|
|
3009
3585
|
const seen = new Set ()
|
|
3010
3586
|
const stores = []
|
|
3011
|
-
const archivedScopes = this.
|
|
3587
|
+
const archivedScopes = this.archivedScopeKeys()
|
|
3012
3588
|
for (const scope of wanted) {
|
|
3013
3589
|
if (seen.has(scope)) continue
|
|
3014
3590
|
seen.add(scope)
|
|
@@ -3028,6 +3604,7 @@ export class LocalMemoryService {
|
|
|
3028
3604
|
}
|
|
3029
3605
|
if (input.kind) records = records.filter((r) => r.kind === input.kind)
|
|
3030
3606
|
if (visibleFilter !== undefined) records = records.filter((r) => visibleFilter(r.scope, r.kind))
|
|
3607
|
+
if (blockedFilter !== undefined) records = records.filter((r) => !blockedFilter({ id: r.id, scope: r.scope }))
|
|
3031
3608
|
records.sort((a, b) => b.updatedAt - a.updatedAt)
|
|
3032
3609
|
return records.slice(0, input.limit ?? 50)
|
|
3033
3610
|
}
|
|
@@ -3036,10 +3613,14 @@ export class LocalMemoryService {
|
|
|
3036
3613
|
* 按 id 批量取记录(memory_get)。接受完整 id,也接受摘要里给出的前 8 位前缀
|
|
3037
3614
|
* (前缀命中多条时全部返回,由调用方/模型自行判断)。取全文不需要再检索。
|
|
3038
3615
|
*/
|
|
3039
|
-
getByIds(ids ) {
|
|
3616
|
+
getByIds(ids , agent ) {
|
|
3040
3617
|
this.discoverScopes()
|
|
3041
3618
|
const scopes = this.store.allScopes()
|
|
3042
|
-
|
|
3619
|
+
// 0.32.0:归档包含项目作用域(工作区被移除),不只是会话作用域。
|
|
3620
|
+
const archivedScopes = this.archivedScopeKeys()
|
|
3621
|
+
// 0.32.0:被屏蔽的记录不再按 id 取回(工具层还会单独给出 WITHHELD 说明,
|
|
3622
|
+
// 让模型知道"这条被我自己屏蔽了",而不是当成记录不存在)。
|
|
3623
|
+
const blockedFilter = agent !== undefined ? this.blockFilter({ agent }) : undefined
|
|
3043
3624
|
const out = []
|
|
3044
3625
|
const seen = new Set ()
|
|
3045
3626
|
for (const token of ids) {
|
|
@@ -3049,6 +3630,7 @@ export class LocalMemoryService {
|
|
|
3049
3630
|
for (const rec of s.records.values()) {
|
|
3050
3631
|
if (seen.has(rec.id)) continue
|
|
3051
3632
|
if (rec.id === token || rec.id.startsWith(token)) {
|
|
3633
|
+
if (blockedFilter !== undefined && blockedFilter({ id: rec.id, scope: rec.scope })) continue
|
|
3052
3634
|
seen.add(rec.id)
|
|
3053
3635
|
out.push(rec)
|
|
3054
3636
|
}
|
|
@@ -3372,7 +3954,9 @@ export class LocalMemoryService {
|
|
|
3372
3954
|
*/
|
|
3373
3955
|
scopeStats() {
|
|
3374
3956
|
this.discoverScopes()
|
|
3375
|
-
|
|
3957
|
+
// 0.32.0:面板每次拉清单前对一次账(节流),工作区被删 / 重加立刻反映到归档状态。
|
|
3958
|
+
try { this.reconcileArchivedScopesNow() } catch { /* 对账失败不影响统计 */ }
|
|
3959
|
+
const archived = this.archivedScopeKeys()
|
|
3376
3960
|
return this.store.allScopes()
|
|
3377
3961
|
.map((s) => ({
|
|
3378
3962
|
scope: s.scope,
|
|
@@ -3399,12 +3983,13 @@ export class LocalMemoryService {
|
|
|
3399
3983
|
* 作用域初始化记录(`initial: true`)的目录视图。只给 normal / none 用;
|
|
3400
3984
|
* absolute 由调用方跳过。正文截断到 800 字,每个作用域最多 3 条。
|
|
3401
3985
|
*/
|
|
3402
|
-
initialRecordsOf(scope ) {
|
|
3986
|
+
initialRecordsOf(scope , blocked ) {
|
|
3403
3987
|
const store = this.store.peek(scope)
|
|
3404
3988
|
if (store === undefined) return undefined
|
|
3405
3989
|
const out = []
|
|
3406
3990
|
for (const record of store.records.values()) {
|
|
3407
3991
|
if ((record ).initial !== true) continue
|
|
3992
|
+
if (blocked !== undefined && blocked({ id: record.id, scope: record.scope })) continue
|
|
3408
3993
|
const content = record.content.trim()
|
|
3409
3994
|
out.push({
|
|
3410
3995
|
id: record.id,
|
|
@@ -3419,6 +4004,7 @@ export class LocalMemoryService {
|
|
|
3419
4004
|
|
|
3420
4005
|
scopeCatalog(agent ) {
|
|
3421
4006
|
const policy = this.visibilityPolicy(agent)
|
|
4007
|
+
try { this.reconcileArchivedScopesNow() } catch { /* 对账失败不影响目录 */ }
|
|
3422
4008
|
const stats = new Map(this.scopeStats().map((item) => [this.canonicalScope(item.scope), item]))
|
|
3423
4009
|
const metas = new Map(this.meta.list().map((item) => [this.canonicalScope(item.scope), item]))
|
|
3424
4010
|
const keys = new Set ()
|
|
@@ -3429,7 +4015,17 @@ export class LocalMemoryService {
|
|
|
3429
4015
|
|
|
3430
4016
|
const out = []
|
|
3431
4017
|
const evaluate = this.accessEvaluator(agent)
|
|
3432
|
-
const archivedScopes = this.
|
|
4018
|
+
const archivedScopes = this.archivedScopeKeys()
|
|
4019
|
+
// 0.32.0:屏蔽在目录里如实标出(blocked + 层级),而不是把作用域整个藏掉 ——
|
|
4020
|
+
// 模型需要看到"这个域是我自己屏蔽的,可以解除",用户也需要在面板里找回它。
|
|
4021
|
+
const blocks = this.blocksFor(agent)
|
|
4022
|
+
const blockedScopes = new Map ()
|
|
4023
|
+
for (const block of blocks) {
|
|
4024
|
+
if (block.scope === undefined) continue
|
|
4025
|
+
const key = this.canonicalScope(block.scope)
|
|
4026
|
+
// 工作区级比会话级"更重":同一作用域两层都屏蔽时显示 workspace。
|
|
4027
|
+
if (blockedScopes.get(key) !== 'workspace') blockedScopes.set(key, block.level)
|
|
4028
|
+
}
|
|
3433
4029
|
for (const scope of keys) {
|
|
3434
4030
|
// 目录发现本身也是一条授权判定:作用域类型被关闭时连名字/用途都不出现。
|
|
3435
4031
|
if (!evaluate({ scope, action: 'discover' }).allowed) continue
|
|
@@ -3443,6 +4039,7 @@ export class LocalMemoryService {
|
|
|
3443
4039
|
// 0.30.0:归档会话的作用域保留在目录里,但读写/申请全部冻结 —— 这不是权限
|
|
3444
4040
|
// 边界,而是会话生命周期:恢复会话后自动复原,不需要任何额外操作。
|
|
3445
4041
|
const archived = archivedScopes.has(scope)
|
|
4042
|
+
const blockedLevel = blockedScopes.get(scope)
|
|
3446
4043
|
const readDecision = evaluate({ scope, action: 'read' })
|
|
3447
4044
|
const writeDecision = evaluate({ scope, action: 'write' })
|
|
3448
4045
|
const readable = !archived && readDecision.allowed
|
|
@@ -3468,7 +4065,9 @@ export class LocalMemoryService {
|
|
|
3468
4065
|
? 'read-only'
|
|
3469
4066
|
: 'restricted'
|
|
3470
4067
|
const name = meta?.name ?? this.names.nameOf(scope)
|
|
3471
|
-
const initialRecords = policy.level === 'absolute'
|
|
4068
|
+
const initialRecords = policy.level === 'absolute'
|
|
4069
|
+
? undefined
|
|
4070
|
+
: this.initialRecordsOf(scope, this.blockFilter({ agent }))
|
|
3472
4071
|
// 0.27.0:scope-meta 没写用途时按作用域类型给默认描述,
|
|
3473
4072
|
// 让目录里每一条都自带「这个域是干什么的」。
|
|
3474
4073
|
// 0.29.0:被显式清空(描述为空串)与从没写过同样处理,都回退到默认描述。
|
|
@@ -3487,7 +4086,8 @@ export class LocalMemoryService {
|
|
|
3487
4086
|
canRequestWrite,
|
|
3488
4087
|
accessStatus,
|
|
3489
4088
|
updatedAt: stat?.updatedAt ?? meta?.updatedAt ?? 0,
|
|
3490
|
-
...(archived ? { archived: true } : {}),
|
|
4089
|
+
...(archived ? { archived: true, archivedKind: this.isArchivedSessionScope(scope) ? 'session' : 'project' } : {}),
|
|
4090
|
+
...(blockedLevel !== undefined ? { blocked: true, blockedLevel } : {}),
|
|
3491
4091
|
...(initialRecords !== undefined ? { initialRecords } : {}),
|
|
3492
4092
|
})
|
|
3493
4093
|
}
|
|
@@ -3644,8 +4244,8 @@ export class LocalMemoryService {
|
|
|
3644
4244
|
process.removeListener('exit', this.exitHook)
|
|
3645
4245
|
this.exitHook = null
|
|
3646
4246
|
}
|
|
3647
|
-
//
|
|
3648
|
-
const frozen = (scope ) => this.
|
|
4247
|
+
// 归档作用域在归档期间冻结:dispose 时也不做过期清理 / 容量淘汰。
|
|
4248
|
+
const frozen = (scope ) => this.isArchivedScope(scope)
|
|
3649
4249
|
this.store.purgeExpired(undefined, frozen)
|
|
3650
4250
|
this.store.enforceLimit(this.cfg.maxRecordsPerScope, undefined, frozen)
|
|
3651
4251
|
this.store.close()
|