@yolk_vat-y/dsh-project-memory 0.6.11 → 0.7.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 +104 -120
- package/README.md +14 -105
- package/README.zh-CN.md +14 -105
- package/client/client.js +228 -95
- package/client/client.js.map +1 -1
- package/package.json +2 -2
- package/scripts/file-peak.mjs +1 -1
- package/scripts/tool-schema-size.mjs +1 -1
- package/skills/project-memory/SKILL.md +1 -1
- package/skills/project-memory/scripts/pm-digest.mjs +20 -6
- package/src/audit.js +10 -2
- package/src/auto-inject.js +124 -38
- package/src/client/MemoryView.tsx +119 -40
- package/src/client/TaskCommandNode.tsx +6 -4
- package/src/client/TaskComponents.tsx +13 -16
- package/src/client/TaskPanel.tsx +41 -15
- package/src/client/client.ts +4 -7
- package/src/client/paging.js +1 -14
- package/src/client/settings-card.tsx +9 -0
- package/src/client/task-data-store.ts +57 -7
- package/src/client/task-hooks.ts +0 -2
- package/src/client/task-scope.js +16 -1
- package/src/client/task-ui-store.ts +1 -1
- package/src/commands/insight-actions.js +91 -94
- package/src/commands/task-actions.js +7 -5
- package/src/commands/tasks.js +34 -10
- package/src/doc-index.js +3 -1
- package/src/enhancer.js +0 -12
- package/src/index-failures.js +1 -1
- package/src/index-pipeline.js +11 -2
- package/src/index.js +60 -14
- package/src/inject-visible.js +282 -0
- package/src/insight-store.js +133 -68
- package/src/lazy.js +29 -23
- package/src/llm-route.js +1 -1
- package/src/llm.js +1 -8
- package/src/ops.js +18 -5
- package/src/parsers/pdfjs-parser.js +3 -33
- package/src/project-memory-service.js +195 -0
- package/src/project-profile.js +0 -4
- package/src/setup/taskbridge.js +38 -6
- package/src/store.js +639 -147
- package/src/symbols.js +2 -4
- package/src/tools/context.js +14 -0
- package/src/tools/forget.js +7 -17
- package/src/tools/index-doc.js +62 -21
- package/src/tools/index-repo.js +17 -15
- package/src/tools/lesson-tools.js +10 -13
- package/src/tools/output.js +33 -0
- package/src/tools/query-memory.js +7 -12
- package/src/tools/remember.js +8 -18
- package/src/tools/stats.js +4 -9
- package/src/tools/task-tools.js +23 -22
- package/src/tools/watch-repo.js +16 -15
- package/src/util/flush.js +1 -1
- package/src/util/fs.js +135 -20
- package/src/util/insight-fields.js +22 -0
- package/src/util/message-source.js +20 -0
- package/src/util/relative-time.js +24 -0
- package/src/util/search.js +0 -1
- package/src/util/task-view.js +8 -15
- package/src/util/value.js +16 -0
- package/src/vector-index.js +2 -2
- package/src/watch.js +25 -8
- package/client/client.d.ts +0 -27
- package/client/client.d.ts.map +0 -1
- package/client/style.css +0 -395
package/src/enhancer.js
CHANGED
|
@@ -73,18 +73,6 @@ export function initTypeScript(config) {
|
|
|
73
73
|
return null
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
export function getTypeScript() {
|
|
77
|
-
return ts
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function hasTypeScript() {
|
|
81
|
-
return ts !== null
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function getTsPath() {
|
|
85
|
-
return tsPath
|
|
86
|
-
}
|
|
87
|
-
|
|
88
76
|
export function getTsVersion() {
|
|
89
77
|
return tsVersion
|
|
90
78
|
}
|
package/src/index-failures.js
CHANGED
package/src/index-pipeline.js
CHANGED
|
@@ -113,13 +113,16 @@ export function toFileUpdate(rel, plan, record) {
|
|
|
113
113
|
* 不再重建 doc↔symbol 链接:链接是读取期解算的派生关系(见 src/link.js),
|
|
114
114
|
* 所以这里也没有了那个「中间批次跳过链接、最后一批统一做」的 `link` 参数。
|
|
115
115
|
*
|
|
116
|
-
* @param {{updates: object[], unseen?: Set<string>|null, root?: string|null
|
|
116
|
+
* @param {{updates: object[], unseen?: Set<string>|null, root?: string|null,
|
|
117
|
+
* isExcludedRel?: ((rel: string) => boolean)|null}} args
|
|
117
118
|
* `root` 给出时,显式索引过、且文件仍在盘上的条目不会被 unseen 清理删掉:它们本来就不在
|
|
118
119
|
* 扫描清单里(.gitignore 排除),"没扫到"不等于"被删了"。
|
|
120
|
+
* `isExcludedRel` 必须先于 explicit 豁免生效:roots.exclude 是"这棵子树一律不进索引",
|
|
121
|
+
* 显式 index_doc 过的文件也不能因为 explicit 标记而在排除后继续可检索。
|
|
119
122
|
* @returns {{stale: string[], removed: number}} stale 是 CAS 失败(并发改动)的 rel,
|
|
120
123
|
* 调用方应让它们保持「未落快照」状态,下一轮重试。
|
|
121
124
|
*/
|
|
122
|
-
export function commitFileUpdates(store, { updates, unseen = null, root = null }) {
|
|
125
|
+
export function commitFileUpdates(store, { updates, unseen = null, root = null, isExcludedRel = null }) {
|
|
123
126
|
const stale = []
|
|
124
127
|
let removed = 0
|
|
125
128
|
store.commit((s) => {
|
|
@@ -133,6 +136,12 @@ export function commitFileUpdates(store, { updates, unseen = null, root = null }
|
|
|
133
136
|
if (unseen) {
|
|
134
137
|
for (const rel of Object.keys(s.files)) {
|
|
135
138
|
if (unseen.has(rel)) continue
|
|
139
|
+
// 排除规则优先于 explicit:被 roots.exclude 命中的文件即使显式索引过也必须摘掉。
|
|
140
|
+
if (isExcludedRel && isExcludedRel(rel)) {
|
|
141
|
+
s.removeFile(rel)
|
|
142
|
+
removed++
|
|
143
|
+
continue
|
|
144
|
+
}
|
|
136
145
|
if (root && s.files[rel]?.explicit && existsSync(path.join(root, rel))) continue
|
|
137
146
|
s.removeFile(rel)
|
|
138
147
|
removed++
|
package/src/index.js
CHANGED
|
@@ -14,6 +14,7 @@ import { setupTaskbridge } from './setup/taskbridge.js'
|
|
|
14
14
|
import { listTasksTool, selectTaskTool, archiveTaskTool, showTaskPanelTool } from './tools/task-tools.js'
|
|
15
15
|
import { lessonTool } from './tools/lesson-tools.js'
|
|
16
16
|
import { installAutoInject } from './auto-inject.js'
|
|
17
|
+
import { installProjectMemoryService } from './project-memory-service.js'
|
|
17
18
|
import { rememberRoute } from './llm-route.js'
|
|
18
19
|
import { workflowCommandDefinition } from './commands/workflow.js'
|
|
19
20
|
import { setupSkill } from './skill.js'
|
|
@@ -28,7 +29,7 @@ export const Config = Schema.object({
|
|
|
28
29
|
chunkChars: Schema.number().default(3000),
|
|
29
30
|
maxChunksPerFile: Schema.number().default(40),
|
|
30
31
|
// 单文件内存闸门,不是磁盘设置:索引一个文件要先整个物化,峰值随文件大小线性走。
|
|
31
|
-
// 实测边际系数(峰值 RSS / 磁盘 MB,见 scripts/file-peak.mjs
|
|
32
|
+
// 实测边际系数(峰值 RSS / 磁盘 MB,见 scripts/file-peak.mjs):
|
|
32
33
|
// 典型源码 ~10×、符号密集代码 ~22×、文本型 PDF ~20×;10 把单文件增量压在 ~+300 MB。
|
|
33
34
|
maxFileSizeMb: Schema.number().default(10),
|
|
34
35
|
maxOutputChars: Schema.number().default(8000),
|
|
@@ -160,6 +161,58 @@ export const Config = Schema.object({
|
|
|
160
161
|
}).default({}),
|
|
161
162
|
})
|
|
162
163
|
|
|
164
|
+
/**
|
|
165
|
+
* 12 个工具的唯一定义处:apply 与 schema 预算测试共用,避免测试里手抄一份列表
|
|
166
|
+
*(新增第 13 个工具不会因为漏抄而绕过预算)。
|
|
167
|
+
* @param {object} config 插件配置
|
|
168
|
+
* @param {object} ctx 宿主上下文(仅部分工具构造时需要)
|
|
169
|
+
* @param {object} watchManager watch 管理器
|
|
170
|
+
* @returns {object[]} defineTool 的产物
|
|
171
|
+
*/
|
|
172
|
+
export function toolDefinitions(config, ctx, watchManager) {
|
|
173
|
+
return [
|
|
174
|
+
listTasksTool(config),
|
|
175
|
+
selectTaskTool(config, { llm: ctx?.llm, ctx }),
|
|
176
|
+
archiveTaskTool(config, { llm: ctx?.llm, ctx }),
|
|
177
|
+
showTaskPanelTool(config),
|
|
178
|
+
indexDocTool(ctx, config),
|
|
179
|
+
indexRepoTool(ctx, config),
|
|
180
|
+
queryMemoryTool(ctx, config),
|
|
181
|
+
rememberTool(config),
|
|
182
|
+
forgetTool(config),
|
|
183
|
+
lessonTool(config),
|
|
184
|
+
watchRepoTool(watchManager, config),
|
|
185
|
+
statsTool(config),
|
|
186
|
+
]
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 逐个注册工具,单个失败只丢那一个。
|
|
191
|
+
*
|
|
192
|
+
* 触发这次改动的是真事故:`dsh-memory-tiers` 也注册了 `memory_stats`,宿主 `ctx.tools.register`
|
|
193
|
+
* 遇到重名直接抛错,而这里原本是裸循环——一个工具名被别的插件占掉,整条 fiber 就 failed,
|
|
194
|
+
* 12 个工具连同客户端面板、`/tasks` 一起消失,用户看到的是"插件装了但什么都没生效"。
|
|
195
|
+
* 重名不是本插件的 bug,代价不该是全部归零;失败必须点名打日志,否则只是换成"少了个工具"
|
|
196
|
+
* 的静默。其它注册错误(schema 不合法等)同样吞掉并照常登记其余工具——那种 bug 归测试管,
|
|
197
|
+
* 线上仍应尽量保留可用面。
|
|
198
|
+
*
|
|
199
|
+
* @param {object} ctx 宿主上下文(读取 `ctx.tools.register`)
|
|
200
|
+
* @param {object[]} defs `toolDefinitions()` 的产物
|
|
201
|
+
* @returns {string[]} 注册失败的工具名,供调用方/测试核对
|
|
202
|
+
*/
|
|
203
|
+
export function registerTools(ctx, defs) {
|
|
204
|
+
const failed = []
|
|
205
|
+
for (const def of defs) {
|
|
206
|
+
try {
|
|
207
|
+
ctx.tools.register(def)
|
|
208
|
+
} catch (err) {
|
|
209
|
+
failed.push(def.name)
|
|
210
|
+
console.error(`[dsh-project-memory] tool "${def.name}" was not registered: ${err?.message ?? err}`)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return failed
|
|
214
|
+
}
|
|
215
|
+
|
|
163
216
|
export function apply(ctx, config) {
|
|
164
217
|
if (config.enableTypeScript !== false) {
|
|
165
218
|
initTypeScript(config)
|
|
@@ -187,7 +240,7 @@ export function apply(ctx, config) {
|
|
|
187
240
|
}
|
|
188
241
|
|
|
189
242
|
// 设置页保存(宿主 volatile 更新)后:只有 watchInterval 会影响运行中的定时器,
|
|
190
|
-
// 其余 volatile 字段都在各自的读取点当场生效(llmQueryExpansion 每次查询读、
|
|
243
|
+
// 其余 volatile 字段都在各自的读取点当场生效(llmQueryExpansion 每次查询读、autoIndexOnFirstUse 只认启动值)。
|
|
191
244
|
// paths 为 `[[]]` 时表示整份 Config 都可能变了 → changedConfigKeys 返回 null,按"全部重读"处理。
|
|
192
245
|
ctx.on('loader/volatile-update', (paths) => {
|
|
193
246
|
const changed = changedConfigKeys(paths)
|
|
@@ -214,10 +267,7 @@ export function apply(ctx, config) {
|
|
|
214
267
|
ctx.on('session/disposed', (session) => {
|
|
215
268
|
watchManager.setSessionActive(session?.id ?? 'unknown', false)
|
|
216
269
|
})
|
|
217
|
-
ctx
|
|
218
|
-
ctx.tools.register(selectTaskTool(config, { llm: ctx.llm, ctx }))
|
|
219
|
-
ctx.tools.register(archiveTaskTool(config, { llm: ctx.llm, ctx }))
|
|
220
|
-
ctx.tools.register(showTaskPanelTool(config))
|
|
270
|
+
registerTools(ctx, toolDefinitions(config, ctx, watchManager))
|
|
221
271
|
|
|
222
272
|
// /tasks:唯一的用户命令(宿主命令注册后必然出现在 `/` 菜单且无法隐藏,所以只留一条);
|
|
223
273
|
// 切换/归档/审核等子动词由面板按钮经 remote.commands.execute 驱动。
|
|
@@ -229,14 +279,10 @@ export function apply(ctx, config) {
|
|
|
229
279
|
console.error(`[dsh-project-memory] /tasks registration skipped: ${err.message}`)
|
|
230
280
|
}
|
|
231
281
|
|
|
232
|
-
|
|
233
|
-
ctx.
|
|
234
|
-
ctx.
|
|
235
|
-
ctx
|
|
236
|
-
ctx.tools.register(forgetTool(config))
|
|
237
|
-
ctx.tools.register(lessonTool(config))
|
|
238
|
-
ctx.tools.register(watchRepoTool(watchManager, config))
|
|
239
|
-
ctx.tools.register(statsTool(config))
|
|
282
|
+
|
|
283
|
+
// 原生服务 seam(D3-1):宿主/其他插件可通过 `ctx.get('projectMemory')` 消费召回与写入。
|
|
284
|
+
// 老宿主没有 `ctx.provide` 时 install 返回 null 并安全跳过,不影响其余注册与 apply 的返回值。
|
|
285
|
+
installProjectMemoryService(ctx, config)
|
|
240
286
|
|
|
241
287
|
// 注册 agent/pre-step 监听,向每步请求追加记忆消息(默认开;异常/无会话 cwd → 交回默认决策)。
|
|
242
288
|
installAutoInject(ctx, config)
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 注入对账的可见性索引:以 session.surface 为真相源,进程缓存只做 fallback。
|
|
3
|
+
* 事件驱动更新;compaction 只标 dirty,下一次 ensure 时全量重建。
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createHash } from 'node:crypto'
|
|
7
|
+
import { isOwnInjection } from './util/message-source.js'
|
|
8
|
+
|
|
9
|
+
const ITEM_LINE_RE = /^\s*-\s+\[(ins_[^\]\s]+)\].*$/gm
|
|
10
|
+
// 一条消息最多带回多少条 provenance:默认 maxItemsPerSession=60,写 40 会漏记尾部,
|
|
11
|
+
// 重启/压缩重建后已注入条目会再发一遍。上限只防手写/损坏的异常长数组。
|
|
12
|
+
const MAX_PROVENANCE_ITEMS = 1024
|
|
13
|
+
|
|
14
|
+
function surfaceOf(session) {
|
|
15
|
+
const surface = session && session.surface
|
|
16
|
+
if (!surface || !Array.isArray(surface.nodes) || typeof session.eventAt !== 'function') return null
|
|
17
|
+
return surface
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function createVisibleState() {
|
|
21
|
+
return {
|
|
22
|
+
ready: false,
|
|
23
|
+
dirty: true,
|
|
24
|
+
surfaceGen: null,
|
|
25
|
+
genSupported: false,
|
|
26
|
+
nodeCount: 0,
|
|
27
|
+
firstSeq: null,
|
|
28
|
+
// 没有 replaceGeneration 的旧宿主:等长替换/中间替换无法靠长度或首节点发现,
|
|
29
|
+
// 每 N 次 ensure 做一次全量重建兜底(30k 节点约 9ms,摊到 32 步可忽略)。
|
|
30
|
+
fallbackScans: 0,
|
|
31
|
+
degraded: false,
|
|
32
|
+
items: new Map(), // id -> hash | null(null = 已知可见但 hash 未知)
|
|
33
|
+
// 旧消息(无 provenance)按“整行文本 hash”记账:正文/结论改了就不再算可见,
|
|
34
|
+
// 避免以前 `hash=null` 对任何 hash 都可见、编辑后永远不重发。
|
|
35
|
+
legacy: new Map(), // id -> Set(lineHash)
|
|
36
|
+
legacyBlocks: new Set(), // 旧 procedure 的整块文本指纹(正文没有 id)
|
|
37
|
+
blocks: new Set(),
|
|
38
|
+
// 只在真正扫到 surface/事件时登记;用于区分 pending 的乐观块。
|
|
39
|
+
confirmedBlocks: new Set(),
|
|
40
|
+
// 任务卡(entry)的稳定指纹:条目都被跳过时,卡本身不该在下一步/重启后再发一次。
|
|
41
|
+
entries: new Set(),
|
|
42
|
+
// 注入后消息还没被宿主 append 时的乐观登记;下一次 ensureVisible 若 surface 里找不到就撤销。
|
|
43
|
+
pending: [],
|
|
44
|
+
rollback: null,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function isAvailable(session) {
|
|
49
|
+
return surfaceOf(session) !== null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** 旧消息行指纹:同一行文本(去掉首尾空白)才算同一条可见注入。 */
|
|
53
|
+
export function lineHash(text) {
|
|
54
|
+
return createHash('sha256').update(String(text).trim()).digest('hex').slice(0, 12)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function parseItemLines(text) {
|
|
58
|
+
const out = []
|
|
59
|
+
for (const match of String(text || '').matchAll(ITEM_LINE_RE)) out.push({ id: match[1], hash: lineHash(match[0]) })
|
|
60
|
+
return out
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function mergeProvenance(state, memory) {
|
|
64
|
+
if (!memory || typeof memory !== 'object') return
|
|
65
|
+
if (typeof memory.b === 'string' && memory.b) {
|
|
66
|
+
state.blocks.add(memory.b)
|
|
67
|
+
state.confirmedBlocks.add(memory.b)
|
|
68
|
+
}
|
|
69
|
+
if (typeof memory.e === 'string' && memory.e) state.entries.add(memory.e)
|
|
70
|
+
if (!Array.isArray(memory.i)) return
|
|
71
|
+
let count = 0
|
|
72
|
+
for (const pair of memory.i) {
|
|
73
|
+
if (!Array.isArray(pair) || typeof pair[0] !== 'string') continue
|
|
74
|
+
state.items.set(pair[0], typeof pair[1] === 'string' ? pair[1] : null)
|
|
75
|
+
if (++count >= MAX_PROVENANCE_ITEMS) break
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function recordMessage(state, message) {
|
|
80
|
+
const memory = message?.source?.memory
|
|
81
|
+
if (memory && memory.v === 1) {
|
|
82
|
+
mergeProvenance(state, memory)
|
|
83
|
+
return
|
|
84
|
+
}
|
|
85
|
+
// 正文回退只对**本插件自己的**旧消息生效。压缩摘要、session-reference 的 recall、用户粘贴的
|
|
86
|
+
// 记忆块、子代理转述都可能包含 `- [ins_x] ...` 行;把它们当已注入会永久压制这些条目
|
|
87
|
+
//(hash=null → 任何 hash 都算可见),编辑后也不再重发。
|
|
88
|
+
if (!isOwnInjection(message?.source)) return
|
|
89
|
+
const text = Array.isArray(message.content)
|
|
90
|
+
? message.content.map((b) => (b && b.type === 'text' && typeof b.text === 'string' ? b.text : '')).join('\n')
|
|
91
|
+
: ''
|
|
92
|
+
for (const { id, hash } of parseItemLines(text)) {
|
|
93
|
+
let seen = state.legacy.get(id)
|
|
94
|
+
if (!seen) {
|
|
95
|
+
seen = new Set()
|
|
96
|
+
state.legacy.set(id, seen)
|
|
97
|
+
}
|
|
98
|
+
seen.add(hash)
|
|
99
|
+
}
|
|
100
|
+
// 旧 procedure 正文是 `【标题】\n1. …`,没有 id:整块 hash 是唯一可对账的指纹。
|
|
101
|
+
for (const match of text.matchAll(/【[^】]+】[\s\S]*?(?=\n【|$)/g)) {
|
|
102
|
+
state.legacyBlocks.add(lineHash(match[0]))
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function scanNodes(session, state, fromIndex = 0) {
|
|
107
|
+
const surface = surfaceOf(session)
|
|
108
|
+
if (!surface) return
|
|
109
|
+
for (const seq of surface.nodes.slice(fromIndex)) {
|
|
110
|
+
const event = session.eventAt(seq)
|
|
111
|
+
if (!event || event.type !== 'user/message') continue
|
|
112
|
+
recordMessage(state, event.data)
|
|
113
|
+
}
|
|
114
|
+
if (surface.nodes.length) state.firstSeq = surface.nodes[0]
|
|
115
|
+
state.nodeCount = surface.nodes.length
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 乐观登记的结算:宿主可能在 waterfall 之后、append 之前中止(abort / prepareRequest 失败),
|
|
120
|
+
* 消息没进 surface。此时不能把这些条目当成“已在上下文里”,否则整个会话都不再补发。
|
|
121
|
+
* 只撤销没有在 surface 扫描结果里出现的 pending;出现了就转正。
|
|
122
|
+
*/
|
|
123
|
+
function settlePending(state) {
|
|
124
|
+
if (!state.pending.length) return
|
|
125
|
+
const dropped = []
|
|
126
|
+
for (const pending of state.pending) {
|
|
127
|
+
if (state.confirmedBlocks.has(pending.block)) continue
|
|
128
|
+
state.blocks.delete(pending.block)
|
|
129
|
+
for (const [id, hash] of pending.items) {
|
|
130
|
+
if (state.items.get(id) === hash) state.items.delete(id)
|
|
131
|
+
}
|
|
132
|
+
dropped.push(pending)
|
|
133
|
+
}
|
|
134
|
+
state.pending = []
|
|
135
|
+
if (dropped.length) {
|
|
136
|
+
state.rollback = {
|
|
137
|
+
ids: dropped.flatMap((p) => p.ids),
|
|
138
|
+
chars: dropped.reduce((n, p) => n + p.chars, 0),
|
|
139
|
+
step: Math.max(...dropped.map((p) => p.step ?? -1)),
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 取走“上一步的注入没进 surface”的账,调用方回滚额度/命中/条目冷却。 */
|
|
145
|
+
export function takeRollback(state) {
|
|
146
|
+
if (!state) return null
|
|
147
|
+
const rollback = state.rollback
|
|
148
|
+
state.rollback = null
|
|
149
|
+
return rollback || null
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** 常驻任务卡是否仍可见(条目全被跳过时,卡本身不该重复发)。 */
|
|
153
|
+
export function isEntryVisible(state, entryHash) {
|
|
154
|
+
return Boolean(state && entryHash && state.entries.has(entryHash))
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** 旧 procedure 的整块文本是否仍可见(正文无 id,只能按整块 hash 对账)。 */
|
|
158
|
+
export function isLegacyBodyVisible(state, body) {
|
|
159
|
+
return Boolean(state && body && state.legacyBlocks.has(lineHash(body)))
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function ensureVisible(session, state) {
|
|
163
|
+
const surface = surfaceOf(session)
|
|
164
|
+
if (!surface || state.degraded) return false
|
|
165
|
+
const gen = Number.isFinite(surface.replaceGeneration) ? surface.replaceGeneration : null
|
|
166
|
+
state.genSupported = gen !== null
|
|
167
|
+
if (!state.genSupported) {
|
|
168
|
+
state.fallbackScans += 1
|
|
169
|
+
if (state.fallbackScans >= 32) {
|
|
170
|
+
state.fallbackScans = 0
|
|
171
|
+
state.dirty = true
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (!state.ready || state.dirty || (state.genSupported && gen !== state.surfaceGen)
|
|
175
|
+
|| (!state.genSupported && (surface.nodes.length < state.nodeCount
|
|
176
|
+
|| (state.firstSeq !== null && surface.nodes[0] !== state.firstSeq)))) {
|
|
177
|
+
state.items.clear()
|
|
178
|
+
state.legacy.clear()
|
|
179
|
+
state.legacyBlocks.clear()
|
|
180
|
+
state.blocks.clear()
|
|
181
|
+
state.confirmedBlocks.clear()
|
|
182
|
+
state.entries.clear()
|
|
183
|
+
state.firstSeq = null
|
|
184
|
+
state.nodeCount = 0
|
|
185
|
+
state.dirty = false
|
|
186
|
+
state.ready = true
|
|
187
|
+
state.surfaceGen = gen
|
|
188
|
+
try {
|
|
189
|
+
scanNodes(session, state)
|
|
190
|
+
} catch {
|
|
191
|
+
state.degraded = true
|
|
192
|
+
state.items.clear()
|
|
193
|
+
state.legacy.clear()
|
|
194
|
+
state.legacyBlocks.clear()
|
|
195
|
+
state.blocks.clear()
|
|
196
|
+
state.confirmedBlocks.clear()
|
|
197
|
+
state.entries.clear()
|
|
198
|
+
return false
|
|
199
|
+
}
|
|
200
|
+
settlePending(state)
|
|
201
|
+
return true
|
|
202
|
+
}
|
|
203
|
+
// 事件流可能漏掉(宿主版本差异):只补扫尾部追加的节点。
|
|
204
|
+
if (surface.nodes.length > state.nodeCount) {
|
|
205
|
+
try {
|
|
206
|
+
scanNodes(session, state, state.nodeCount)
|
|
207
|
+
} catch {
|
|
208
|
+
state.degraded = true
|
|
209
|
+
state.items.clear()
|
|
210
|
+
state.legacy.clear()
|
|
211
|
+
state.legacyBlocks.clear()
|
|
212
|
+
state.blocks.clear()
|
|
213
|
+
state.confirmedBlocks.clear()
|
|
214
|
+
state.entries.clear()
|
|
215
|
+
return false
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
settlePending(state)
|
|
219
|
+
return true
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function onSessionEvent(state, event) {
|
|
223
|
+
if (!state || !event) return
|
|
224
|
+
if (typeof event.type === 'string' && event.type.startsWith('compaction/')) {
|
|
225
|
+
state.dirty = true
|
|
226
|
+
return
|
|
227
|
+
}
|
|
228
|
+
if (event.type !== 'user/message') return
|
|
229
|
+
recordMessage(state, event.data)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 注入后立即登记(消息可能还没 commit 到 surface);下一步发现 surface 里没有就撤销。
|
|
234
|
+
* 注意这里**不能**走 mergeProvenance:它把块标成“已确认”,而这条消息此刻还没进 surface。
|
|
235
|
+
*/
|
|
236
|
+
export function recordInjected(state, message, meta = {}) {
|
|
237
|
+
const memory = message?.source?.memory
|
|
238
|
+
if (memory && memory.v === 1) {
|
|
239
|
+
const block = typeof memory.b === 'string' ? memory.b : ''
|
|
240
|
+
if (block) state.blocks.add(block)
|
|
241
|
+
const items = []
|
|
242
|
+
for (const pair of Array.isArray(memory.i) ? memory.i : []) {
|
|
243
|
+
if (!Array.isArray(pair) || typeof pair[0] !== 'string') continue
|
|
244
|
+
const hash = typeof pair[1] === 'string' ? pair[1] : null
|
|
245
|
+
state.items.set(pair[0], hash)
|
|
246
|
+
items.push([pair[0], hash])
|
|
247
|
+
}
|
|
248
|
+
state.pending.push({ block, items, chars: Number(meta.chars) || 0, step: Number.isFinite(meta.step) ? meta.step : null, ids: (meta.reasons || []).map((r) => r && r.id).filter(Boolean) })
|
|
249
|
+
return
|
|
250
|
+
}
|
|
251
|
+
recordMessage(state, message)
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* @param {object} state 可见性索引
|
|
256
|
+
* @param {string} id 条目 id
|
|
257
|
+
* @param {string} hash 当前条目正文指纹
|
|
258
|
+
* @param {string[]} [legacyHashes] 旧消息可能留下的整行指纹(见 lineHash)
|
|
259
|
+
*/
|
|
260
|
+
export function isItemVisible(state, id, hash, legacyHashes = []) {
|
|
261
|
+
if (!state) return false
|
|
262
|
+
if (state.items.has(id)) {
|
|
263
|
+
const seen = state.items.get(id)
|
|
264
|
+
return seen === null || seen === hash
|
|
265
|
+
}
|
|
266
|
+
const lines = state.legacy.get(id)
|
|
267
|
+
return Boolean(lines && legacyHashes.some((h) => lines.has(h)))
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function isBlockVisible(state, block) {
|
|
271
|
+
return Boolean(state && block && state.blocks.has(block))
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function provenanceOf(block, reasons, max = MAX_PROVENANCE_ITEMS, entry = null) {
|
|
275
|
+
const items = []
|
|
276
|
+
for (const reason of reasons || []) {
|
|
277
|
+
if (!reason || !reason.id || !reason.hash) continue
|
|
278
|
+
items.push([reason.id, reason.hash])
|
|
279
|
+
if (items.length >= max) break
|
|
280
|
+
}
|
|
281
|
+
return { v: 1, b: String(block || ''), i: items, ...(entry ? { e: String(entry) } : {}) }
|
|
282
|
+
}
|