@yolk_vat-y/dsh-project-memory 0.6.3 → 0.6.5
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 +162 -8
- package/README.md +44 -26
- package/README.zh-CN.md +42 -24
- package/client/client.js +272 -138
- package/client/client.js.map +1 -1
- package/cordis.patch.yml +6 -1
- package/package.json +3 -2
- package/scripts/bench-synthetic.mjs +22 -11
- package/src/audit.js +36 -32
- package/src/auto-inject.js +55 -32
- package/src/client/MemoryView.tsx +72 -4
- package/src/client/ShowTaskPanelNode.tsx +8 -5
- package/src/client/TaskCommandNode.tsx +22 -17
- package/src/client/TaskComponents.tsx +18 -3
- package/src/client/TaskPanel.tsx +50 -85
- package/src/client/client.ts +26 -6
- package/src/client/commands.ts +67 -0
- package/src/client/task-ui-store.ts +15 -2
- package/src/commands/insight-actions.js +1 -1
- package/src/commands/task-actions.js +57 -3
- package/src/commands/tasks.js +1 -1
- package/src/index.js +29 -11
- package/src/insight-store.js +200 -78
- package/src/lazy.js +7 -3
- package/src/ops.js +1 -1
- package/src/readiness.js +1 -1
- package/src/recall.js +76 -30
- package/src/setup/taskbridge.js +37 -6
- package/src/store.js +315 -44
- package/src/tools/forget.js +11 -8
- package/src/tools/index-doc.js +14 -8
- package/src/tools/index-repo.js +10 -11
- package/src/tools/lesson-tools.js +25 -35
- package/src/tools/query-memory.js +81 -28
- package/src/tools/remember.js +14 -11
- package/src/tools/stats.js +26 -3
- package/src/tools/task-tools.js +11 -12
- package/src/tools/watch-repo.js +6 -8
- package/src/types.js +9 -4
- package/src/util/fs.js +502 -37
- package/src/util/search.js +6 -27
- package/src/vector-index.js +175 -22
- package/src/watch.js +15 -10
package/src/insight-store.js
CHANGED
|
@@ -10,7 +10,7 @@ import { readFileSync, renameSync } from 'node:fs'
|
|
|
10
10
|
import path from 'node:path'
|
|
11
11
|
import os from 'node:os'
|
|
12
12
|
import { normalizedTokenOverlap, findBestOverlapMatch, insightMatchText } from './similarity.js'
|
|
13
|
-
import { writeJsonAtomic } from './util/fs.js'
|
|
13
|
+
import { acquireDirLock, writeJsonAtomic } from './util/fs.js'
|
|
14
14
|
import { backfillDerivedTriggers } from './readiness.js'
|
|
15
15
|
import { vectorText } from './vector-index.js'
|
|
16
16
|
import { tokenize } from './util/search.js'
|
|
@@ -30,7 +30,7 @@ export const SECRET_PATTERNS = [
|
|
|
30
30
|
]
|
|
31
31
|
|
|
32
32
|
/** 检测疑似密钥/token/私钥形态内容;命中返回第一个命中字段名,否则 null。 */
|
|
33
|
-
export function containsSecretFields(ins, fields = ['title', 'body', 'pattern', 'fix', 'problem', 'solution', 'choice', 'reason']) {
|
|
33
|
+
export function containsSecretFields(ins, fields = ['title', 'body', 'pattern', 'fix', 'problem', 'solution', 'choice', 'reason', 'steps']) {
|
|
34
34
|
for (const f of fields) {
|
|
35
35
|
const v = ins && ins[f]
|
|
36
36
|
if (!v) continue
|
|
@@ -44,6 +44,13 @@ function num(v, d) {
|
|
|
44
44
|
return typeof v === 'number' && Number.isFinite(v) ? v : d
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
/** 两个 ISO 时间戳的先后;缺失/非法一律 0。 */
|
|
48
|
+
function newerIso(a, b) {
|
|
49
|
+
const ta = Date.parse(a || '') || 0
|
|
50
|
+
const tb = Date.parse(b || '') || 0
|
|
51
|
+
return ta - tb
|
|
52
|
+
}
|
|
53
|
+
|
|
47
54
|
/** 合并配置(config.insight 之上补默认)。 */
|
|
48
55
|
export function cfgInsight(config) {
|
|
49
56
|
const c = (config && config.insight) || {}
|
|
@@ -51,11 +58,13 @@ export function cfgInsight(config) {
|
|
|
51
58
|
dedupOverlap: num(c.dedupOverlap, 0.7),
|
|
52
59
|
reinforceBand: num(c.reinforceBand, 0.65),
|
|
53
60
|
// 向量带(需要 leanvec;没装就自动退化成上面的纯词法带)。阈值由真实 store 标定:
|
|
54
|
-
// 47 条 live 条目、1081 对的余弦 max=0.655 / p99=0.531
|
|
61
|
+
// 47 条 live 条目、1081 对的余弦 max=0.655 / p99=0.531;一个本地小模型把同一条知识改写后与
|
|
55
62
|
// 原文的余弦 min=0.688 / p50=0.809。所以:
|
|
56
63
|
// · 0.90 自动覆盖 —— 落在负样本之上 0.245,只会命中逐字级再记录("非常非常相似");
|
|
57
64
|
// · 0.70 提示 —— 在全部 1081 对真实条目上零噪声,而改写后的再记录有 78% 落进来。
|
|
58
65
|
// 0.65 会开始误伤真实条目(1/1081),别往下调:提示是往上下文里加字的。
|
|
66
|
+
// **0 = 关闭**那条带(与仓库其它旋钮一致):`top1 >= 0` 恒真,旧写法把 0 当"总是合并/
|
|
67
|
+
// 总是提示",而用户关掉它的最自然写法恰好就是 0。
|
|
59
68
|
autoMergeCos: num(c.autoMergeCos, 0.9),
|
|
60
69
|
// 自动覆盖还要求"第一名明显领先第二名",否则同一话题撞两条时不知道该覆盖谁。
|
|
61
70
|
autoMergeMargin: num(c.autoMergeMargin, 0.05),
|
|
@@ -97,7 +106,11 @@ export function normalizeInsight(raw, extra = {}) {
|
|
|
97
106
|
id: extra.id || makeInsightId(),
|
|
98
107
|
kind,
|
|
99
108
|
scope: extra.scope || 'project',
|
|
100
|
-
|
|
109
|
+
// 标题推导必须覆盖 `save_lesson` 允许的三种最小输入(title / pattern / choice):少了
|
|
110
|
+
// `choice`,只给 choice 的 decision 会落成 `(untitled)` —— 面板、召回、向量索引三处看到的
|
|
111
|
+
// 都是这个占位符,而唯一能认出它的字段反而不在标题里。顺序与 `vectorText` 的兜底链一致:
|
|
112
|
+
// 写入侧的向量比对文本也走这里(raw → 落盘 title),两边不同源就会在写路径上静默失效。
|
|
113
|
+
title: String(raw.title || raw.pattern || raw.problem || raw.choice || raw.topic || '(untitled)').slice(0, 200),
|
|
101
114
|
source: extra.source || raw.source || 'agent',
|
|
102
115
|
draft: raw.draft === true || extra.draft === true,
|
|
103
116
|
confidence: num(raw.confidence, num(extra.confidence, 0.8)),
|
|
@@ -259,6 +272,9 @@ export function applyUpdate(item, raw, nowIso) {
|
|
|
259
272
|
}
|
|
260
273
|
item.updatedAt = now
|
|
261
274
|
if (item.archived) item.archived = false
|
|
275
|
+
// 显式编辑一条草稿 = 作者确认:不再要求先开面板点“确认”。reflection 产出的 draft
|
|
276
|
+
// 因此可以被 save_lesson 带 id 直接改正文并转正;没有字段变化时也至少完成确认。
|
|
277
|
+
if (item.draft === true) item.draft = false
|
|
262
278
|
return replaced
|
|
263
279
|
}
|
|
264
280
|
|
|
@@ -284,10 +300,17 @@ export function vectorNear(text, items, cosine) {
|
|
|
284
300
|
let top1 = -1
|
|
285
301
|
let top2 = -1
|
|
286
302
|
let item = null
|
|
303
|
+
// 池内分布:给"绝对阈值"做本语料的陈规定标(见 autoMergeTarget)。n ≤ 300,顺路算完。
|
|
304
|
+
let n = 0
|
|
305
|
+
let sum = 0
|
|
306
|
+
let sumSq = 0
|
|
287
307
|
for (const it of items) {
|
|
288
308
|
if (!it || it.archived) continue
|
|
289
309
|
const v = m.get(it.id)
|
|
290
310
|
if (typeof v !== 'number' || !Number.isFinite(v)) continue
|
|
311
|
+
n++
|
|
312
|
+
sum += v
|
|
313
|
+
sumSq += v * v
|
|
291
314
|
if (v > top1) {
|
|
292
315
|
top2 = top1
|
|
293
316
|
top1 = v
|
|
@@ -295,18 +318,47 @@ export function vectorNear(text, items, cosine) {
|
|
|
295
318
|
} else if (v > top2) top2 = v
|
|
296
319
|
}
|
|
297
320
|
if (!item) return null
|
|
298
|
-
|
|
321
|
+
// 分布必须**排除候选自己**:top1 本身就是待检验的那个值,把它算进 μ/σ 会把自己的极端值
|
|
322
|
+
// 摊进方差里(稀疏池里一条 0.92 + 三条 0.2 会把 σ 抬到 0.33,阈值冲上 1 以上,于是永远不合并)。
|
|
323
|
+
// 排除之后 μ/σ 描述的是"池子里随机一条与这段文本有多像"——那才是零假设。
|
|
324
|
+
const restN = n - 1
|
|
325
|
+
const restSum = sum - top1
|
|
326
|
+
const restSumSq = sumSq - top1 * top1
|
|
327
|
+
const mu = restN > 0 ? restSum / restN : 0
|
|
328
|
+
const sigma = restN > 1 ? Math.sqrt(Math.max(0, restSumSq / restN - mu * mu)) : 0
|
|
329
|
+
return { item, top1, top2: top2 < 0 ? 0 : top2, mu, sigma, n }
|
|
299
330
|
}
|
|
300
331
|
|
|
301
|
-
/** 近邻是否够"较高"
|
|
332
|
+
/** 近邻是否够"较高"(提示带);`hintCos <= 0` = 关闭该带。够就返回给调用方回报的形状。 */
|
|
302
333
|
function hintOf(near, cfg) {
|
|
303
|
-
if (!near || near.top1 < cfg.hintCos) return null
|
|
334
|
+
if (!near || !(cfg.hintCos > 0) || near.top1 < cfg.hintCos) return null
|
|
304
335
|
return { id: near.item.id, cos: Number(near.top1.toFixed(3)), title: String(near.item.title || '').slice(0, 40) }
|
|
305
336
|
}
|
|
306
337
|
|
|
307
|
-
/**
|
|
338
|
+
/**
|
|
339
|
+
* 自动覆盖的"池内相对"系数:题面余弦要同时高于 `μ + k·σ`。
|
|
340
|
+
*
|
|
341
|
+
* 为什么需要:0.9 这个绝对阈值只在**当前语料 + 当前模型**下安全。两种漂移会把它推过界,
|
|
342
|
+
* 而这条带是**破坏性写入**(改写既有条目的正文):
|
|
343
|
+
* · 语料模板化——同一句式的大量条目("修复 X 页脚")会把"最近邻余弦"整体抬到 0.9 附近;
|
|
344
|
+
* · 换模型/换表——同一对文本的余弦尺度整体平移。
|
|
345
|
+
* 取 `max(autoMergeCos, μ + k·σ)` 只可能**收紧**、绝不会放宽:当前真实 store(122 条、
|
|
346
|
+
* 7381 对)的余弦 mean=0.281 / sd=0.104 → μ+3σ ≈ 0.59 < 0.9,行为逐字段不变。
|
|
347
|
+
*
|
|
348
|
+
* k 不是调参项而是零假设的分位数:池子 ≤300 时,"随机撞上"的最大余弦 ≈ μ + 2.9σ
|
|
349
|
+
* (n 次抽样的极值分布),取 3 即"显著高于随机"的边界。k 再大就会在密集语料上退化成
|
|
350
|
+
* "永不自动覆盖"(μ+5σ 在 μ≥0.5 时就已越过 1),k 再小则会开始命中噪声。
|
|
351
|
+
*/
|
|
352
|
+
const AUTO_MERGE_SIGMA = 3
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* 自动覆盖判定:够高**且**第一名明显领先第二名(否则不知道该覆盖谁);`autoMergeCos <= 0` = 关闭。
|
|
356
|
+
* 高度门槛取"绝对下限"与"池内 μ+k·σ"的较大者(只收紧不放宽,见 {@link AUTO_MERGE_SIGMA})。
|
|
357
|
+
*/
|
|
308
358
|
function autoMergeTarget(near, cfg) {
|
|
309
|
-
if (!near ||
|
|
359
|
+
if (!near || !(cfg.autoMergeCos > 0)) return null
|
|
360
|
+
const floor = Math.max(cfg.autoMergeCos, near.mu + AUTO_MERGE_SIGMA * near.sigma)
|
|
361
|
+
if (near.top1 < floor) return null
|
|
310
362
|
if (near.top1 - near.top2 < cfg.autoMergeMargin) return null
|
|
311
363
|
return near
|
|
312
364
|
}
|
|
@@ -442,6 +494,9 @@ export function pruneItems(items, max, nowIso) {
|
|
|
442
494
|
|
|
443
495
|
// ---- Global 存储(~/.config/dsh-project-memory/global.json) ----
|
|
444
496
|
|
|
497
|
+
/** 进程内按文件路径复用;整份文档读改写,两个实例会互相覆盖。 */
|
|
498
|
+
const globalStoreCache = new Map()
|
|
499
|
+
|
|
445
500
|
function readJson(file, fallback) {
|
|
446
501
|
let raw
|
|
447
502
|
try {
|
|
@@ -476,14 +531,27 @@ export class GlobalStore {
|
|
|
476
531
|
constructor(file) {
|
|
477
532
|
this.file = file
|
|
478
533
|
this.doc = null
|
|
534
|
+
this._dirty = false
|
|
535
|
+
this._baseIds = new Set()
|
|
536
|
+
this._deletedIds = new Set()
|
|
537
|
+
this._lockWarned = false
|
|
479
538
|
}
|
|
480
539
|
|
|
481
540
|
load() {
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
541
|
+
// 整份文档读改写:同一路径必须共用实例,否则两个工具/会话交错时后写覆盖先写。
|
|
542
|
+
// 与 ProjectMemoryStore 的 storeCache 同语义;这里只按路径缓存(global 文件只有一个)。
|
|
543
|
+
const key = path.resolve(this.file)
|
|
544
|
+
const hot = globalStoreCache.get(key)
|
|
545
|
+
if (hot) {
|
|
546
|
+
this.doc = hot.doc
|
|
547
|
+
this._dirty = hot._dirty
|
|
548
|
+
return hot
|
|
486
549
|
}
|
|
550
|
+
this.doc = normalizeDoc(readJson(this.file, null))
|
|
551
|
+
// 与 project 级一致:global 条目也要补派生 trigger(提示通道用它提升召回;确定性、幂等)。
|
|
552
|
+
if (backfillDerivedTriggers(this.doc)) this.markDirty()
|
|
553
|
+
this._baseIds = new Set(this.doc.items.map((it) => it?.id).filter(Boolean))
|
|
554
|
+
globalStoreCache.set(key, this)
|
|
487
555
|
return this
|
|
488
556
|
}
|
|
489
557
|
|
|
@@ -495,12 +563,42 @@ export class GlobalStore {
|
|
|
495
563
|
this._dirty = true
|
|
496
564
|
}
|
|
497
565
|
|
|
498
|
-
/** 变更提交:fn(this.doc)
|
|
566
|
+
/** 变更提交:fn(this.doc) 后加锁、合并另一进程的磁盘状态、原子落盘。 */
|
|
499
567
|
commit(fn) {
|
|
500
568
|
const res = fn ? fn(this.doc, this) : undefined
|
|
501
|
-
if (this._dirty
|
|
569
|
+
if (!this._dirty && res === undefined) return res
|
|
570
|
+
// 本进程从基线里删掉的 id(removeMemory 直接 splice,没有单独的删除 API)。
|
|
571
|
+
const current = new Set(this.doc.items.map((it) => it?.id).filter(Boolean))
|
|
572
|
+
for (const id of this._baseIds) if (!current.has(id)) this._deletedIds.add(id)
|
|
573
|
+
for (const id of current) this._deletedIds.delete(id)
|
|
574
|
+
const release = acquireDirLock(path.dirname(this.file))
|
|
575
|
+
if (!release) {
|
|
576
|
+
if (!this._lockWarned) {
|
|
577
|
+
this._lockWarned = true
|
|
578
|
+
console.error(`[dsh-project-memory] global store ${this.file} is busy (another dsh process is writing); this save was skipped and will retry on the next write.`)
|
|
579
|
+
}
|
|
580
|
+
return res
|
|
581
|
+
}
|
|
582
|
+
try {
|
|
583
|
+
const disk = normalizeDoc(readJson(this.file, null))
|
|
584
|
+
const byId = new Map()
|
|
585
|
+
for (const it of disk.items) if (it?.id) byId.set(it.id, it)
|
|
586
|
+
for (const it of this.doc.items) {
|
|
587
|
+
if (!it?.id) continue
|
|
588
|
+
if (this._deletedIds.has(it.id)) {
|
|
589
|
+
byId.delete(it.id)
|
|
590
|
+
continue
|
|
591
|
+
}
|
|
592
|
+
const prev = byId.get(it.id)
|
|
593
|
+
if (!prev || newerIso(it.updatedAt, prev.updatedAt) >= 0) byId.set(it.id, it)
|
|
594
|
+
}
|
|
595
|
+
for (const id of this._deletedIds) byId.delete(id)
|
|
596
|
+
this.doc.items = [...byId.values()]
|
|
502
597
|
writeJsonAtomic(this.file, this.doc)
|
|
503
598
|
this._dirty = false
|
|
599
|
+
this._baseIds = new Set(this.doc.items.map((it) => it?.id).filter(Boolean))
|
|
600
|
+
} finally {
|
|
601
|
+
release()
|
|
504
602
|
}
|
|
505
603
|
return res
|
|
506
604
|
}
|
|
@@ -737,17 +835,21 @@ export function saveInsight({ store, globalStore, raw, scope, taskId, cfg, sourc
|
|
|
737
835
|
if (raw && raw.id) {
|
|
738
836
|
const found = findInsightAnywhere({ store, globalStore }, raw.id)
|
|
739
837
|
if (!found) return { ok: false, error: `找不到要更新的条目: ${raw.id}(可能已被删除/提升;用 query_memory 重新确认 id)` }
|
|
838
|
+
const wasDraft = found.item.draft === true
|
|
740
839
|
const replaced = applyUpdate(found.item, raw, now)
|
|
741
840
|
if (found.scope === 'global') globalStore.markDirty()
|
|
742
841
|
else if (found.scope === 'task') store.markTasksDirty()
|
|
743
842
|
else store.replaceInsightItems(store.insightItems())
|
|
843
|
+
const hint = replaced.length
|
|
844
|
+
? `已就地更新(覆盖字段:${replaced.join('/')},旧值在 prev)`
|
|
845
|
+
: wasDraft ? '已确认草稿(draft=false)' : '没有字段变化'
|
|
744
846
|
return {
|
|
745
847
|
ok: true,
|
|
746
848
|
action: 'updated',
|
|
747
849
|
id: raw.id,
|
|
748
850
|
scope: found.scope,
|
|
749
851
|
replaced,
|
|
750
|
-
hint: replaced.length ?
|
|
852
|
+
hint: wasDraft && replaced.length ? `${hint};草稿已确认` : hint,
|
|
751
853
|
}
|
|
752
854
|
}
|
|
753
855
|
const s = scope || 'project'
|
|
@@ -828,77 +930,94 @@ function buildHint(res, pruned, promoted) {
|
|
|
828
930
|
// ---- scope = task ----
|
|
829
931
|
|
|
830
932
|
function saveTaskInsight(store, globalStore, task, raw, cfg, now, source, cosine) {
|
|
831
|
-
//
|
|
832
|
-
|
|
933
|
+
// 反思产出是**待确认草稿**:只落在当前任务,绝不参与 project/global/跨任务的自动合并或
|
|
934
|
+
// 自动提升。否则一条 LLM 生成的 draft 可以覆盖作者手写的 project 级知识,草稿审核也就
|
|
935
|
+
// 失去意义。确认走显式编辑(applyUpdate 会清 draft)或面板的 confirm。
|
|
936
|
+
const draftOnly = raw.draft === true || source === 'reflect'
|
|
833
937
|
const base = { ...raw, source, sourceTaskIds: [task.id] }
|
|
834
938
|
const text = insightMatchText(base)
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
const
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
const { replaced } = mergeInto(pjHit.item, base, cfg, now)
|
|
848
|
-
store.replaceInsightItems(pjItems)
|
|
849
|
-
applyDecay(pjItems, cfg, now)
|
|
850
|
-
pruneItems(pjItems, cfg.maxProject, now)
|
|
851
|
-
const promoted = promoteProjectToGlobal(store, globalStore, cfg, now)
|
|
852
|
-
return { ok: true, action: 'linked-project', id: pjHit.item.id, scope: 'project', replaced, hint: `该知识已在 project 级,累计第 ${(pjHit.item.sourceTaskIds || []).length} 个任务命中${promoted ? ';已自动提升 global' : ''}` }
|
|
853
|
-
}
|
|
854
|
-
// 2) 已有 global 级知识 → 同样只挂命中
|
|
855
|
-
if (globalStore) {
|
|
856
|
-
const glNear = vectorNear(vectorText(base), globalStore.items(), cosine)
|
|
857
|
-
const glTarget = autoMergeTarget(glNear, cfg)
|
|
858
|
-
if (glTarget) {
|
|
859
|
-
const { replaced } = mergeInto(glTarget.item, base, cfg, now)
|
|
860
|
-
globalStore.markDirty()
|
|
861
|
-
return { ok: true, action: 'auto-merged', id: glTarget.item.id, scope: 'global', replaced, cos: Number(glTarget.top1.toFixed(3)), hint: `相似度 ${glTarget.top1.toFixed(3)},已自动覆盖 global 级既有条目${replaced.length ? `(覆盖字段 ${replaced.join('/')})` : ''}` }
|
|
939
|
+
if (!draftOnly) {
|
|
940
|
+
// 1) 已有 project 级知识 → 挂成员/命中,不再在 task 层重复存
|
|
941
|
+
const pjItems = store.insightItems()
|
|
942
|
+
const pjNear = vectorNear(vectorText(base), pjItems, cosine)
|
|
943
|
+
const pjTarget = autoMergeTarget(pjNear, cfg)
|
|
944
|
+
if (pjTarget) {
|
|
945
|
+
const { replaced } = mergeInto(pjTarget.item, base, cfg, now)
|
|
946
|
+
store.replaceInsightItems(pjItems)
|
|
947
|
+
applyDecay(pjItems, cfg, now)
|
|
948
|
+
pruneItems(pjItems, cfg.maxProject, now)
|
|
949
|
+
const promoted = promoteProjectToGlobal(store, globalStore, cfg, now)
|
|
950
|
+
return { ok: true, action: 'auto-merged', id: pjTarget.item.id, scope: 'project', replaced, cos: Number(pjTarget.top1.toFixed(3)), hint: `相似度 ${pjTarget.top1.toFixed(3)},已自动覆盖 project 级既有条目${replaced.length ? `(覆盖字段 ${replaced.join('/')})` : ''}${promoted ? ';已自动提升 global' : ''}` }
|
|
862
951
|
}
|
|
863
|
-
const
|
|
864
|
-
if (
|
|
865
|
-
const { replaced } = mergeInto(
|
|
866
|
-
|
|
867
|
-
|
|
952
|
+
const pjHit = findBestOverlapMatch(text, pjItems)
|
|
953
|
+
if (pjHit && pjHit.score >= cfg.dedupOverlap) {
|
|
954
|
+
const { replaced } = mergeInto(pjHit.item, base, cfg, now)
|
|
955
|
+
store.replaceInsightItems(pjItems)
|
|
956
|
+
applyDecay(pjItems, cfg, now)
|
|
957
|
+
pruneItems(pjItems, cfg.maxProject, now)
|
|
958
|
+
const promoted = promoteProjectToGlobal(store, globalStore, cfg, now)
|
|
959
|
+
return { ok: true, action: 'linked-project', id: pjHit.item.id, scope: 'project', replaced, hint: `该知识已在 project 级,累计第 ${(pjHit.item.sourceTaskIds || []).length} 个任务命中${promoted ? ';已自动提升 global' : ''}` }
|
|
868
960
|
}
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
961
|
+
// 2) 已有 global 级知识 → 同样只挂命中
|
|
962
|
+
if (globalStore) {
|
|
963
|
+
const glNear = vectorNear(vectorText(base), globalStore.items(), cosine)
|
|
964
|
+
const glTarget = autoMergeTarget(glNear, cfg)
|
|
965
|
+
if (glTarget) {
|
|
966
|
+
const { replaced } = mergeInto(glTarget.item, base, cfg, now)
|
|
967
|
+
globalStore.markDirty()
|
|
968
|
+
return { ok: true, action: 'auto-merged', id: glTarget.item.id, scope: 'global', replaced, cos: Number(glTarget.top1.toFixed(3)), hint: `相似度 ${glTarget.top1.toFixed(3)},已自动覆盖 global 级既有条目${replaced.length ? `(覆盖字段 ${replaced.join('/')})` : ''}` }
|
|
969
|
+
}
|
|
970
|
+
const glHit = findBestOverlapMatch(text, globalStore.items())
|
|
971
|
+
if (glHit && glHit.score >= cfg.dedupOverlap) {
|
|
972
|
+
const { replaced } = mergeInto(glHit.item, base, cfg, now)
|
|
973
|
+
globalStore.markDirty()
|
|
974
|
+
return { ok: true, action: 'linked-global', id: glHit.item.id, scope: 'global', replaced, hint: `该知识已在 global 级,累计第 ${(glHit.item.sourceTaskIds || []).length} 个任务命中` }
|
|
975
|
+
}
|
|
881
976
|
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
const
|
|
886
|
-
const
|
|
887
|
-
if (
|
|
977
|
+
// 3) 跨任务池去重合并(同一坑只留一份,sourceTaskIds 累积)。
|
|
978
|
+
// 草稿不是已确认知识:非草稿写入不得把正文并进一条 draft(那会让新知识永远停在待审状态)。
|
|
979
|
+
const pool = allTaskInsights(store).filter((it) => it.draft !== true)
|
|
980
|
+
const poolNear = vectorNear(vectorText(base), pool, cosine)
|
|
981
|
+
const poolTarget = autoMergeTarget(poolNear, cfg)
|
|
982
|
+
if (poolTarget) {
|
|
983
|
+
const { replaced } = mergeInto(poolTarget.item, base, cfg, now)
|
|
888
984
|
store.markTasksDirty()
|
|
889
|
-
const
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
985
|
+
const holder = taskContaining(store, poolTarget.item.id)
|
|
986
|
+
if (holder) {
|
|
987
|
+
const promoted = promoteAllTasksToProject(store, globalStore, cfg, now)
|
|
988
|
+
return { ok: true, action: 'auto-merged', id: poolTarget.item.id, taskId: holder.id, scope: 'task', replaced, cos: Number(poolTarget.top1.toFixed(3)), hint: `相似度 ${poolTarget.top1.toFixed(3)},已自动覆盖任务 ${holder.id} 的既有条目${replaced.length ? `(覆盖字段 ${replaced.join('/')})` : ''}${promoted ? `;自动提升 project ${promoted} 条` : ''}` }
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
const hit = findBestOverlapMatch(text, pool)
|
|
992
|
+
if (hit && hit.score >= cfg.dedupOverlap) {
|
|
993
|
+
const { replaced } = mergeInto(hit.item, base, cfg, now)
|
|
994
|
+
const holder = taskContaining(store, hit.item.id)
|
|
995
|
+
if (holder) {
|
|
996
|
+
store.markTasksDirty()
|
|
997
|
+
const holderId = holder.id
|
|
998
|
+
const promoted = promoteAllTasksToProject(store, globalStore, cfg, now)
|
|
999
|
+
const size = (hit.item.sourceTaskIds || []).length
|
|
1000
|
+
return {
|
|
1001
|
+
ok: true,
|
|
1002
|
+
action: 'merged',
|
|
1003
|
+
id: hit.item.id,
|
|
1004
|
+
taskId: holderId,
|
|
1005
|
+
replaced,
|
|
1006
|
+
hint: `跨任务去重:并入任务 ${holderId} 既有条目(现 ${size} 个任务命中)${replaced.length ? `,覆盖字段 ${replaced.join('/')}` : ''}${promoted ? `;自动提升 project ${promoted} 条` : ''}`,
|
|
1007
|
+
}
|
|
899
1008
|
}
|
|
900
1009
|
}
|
|
901
1010
|
}
|
|
1011
|
+
if (draftOnly) {
|
|
1012
|
+
// 同一任务的重复反思只保留一份草稿;跨任务不合并(各任务的审核上下文独立)。
|
|
1013
|
+
const existingDrafts = (task.insights || []).filter((it) => it && it.draft === true)
|
|
1014
|
+
const hit = findBestOverlapMatch(text, existingDrafts)
|
|
1015
|
+
if (hit && hit.score >= cfg.dedupOverlap) {
|
|
1016
|
+
const { replaced } = mergeInto(hit.item, base, cfg, now)
|
|
1017
|
+
store.markTasksDirty()
|
|
1018
|
+
return { ok: true, action: 'merged', id: hit.item.id, taskId: task.id, replaced, hint: '已合并到本任务既有草稿' }
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
902
1021
|
// 4) 新建于当前任务
|
|
903
1022
|
if (!Array.isArray(task.insights)) task.insights = []
|
|
904
1023
|
const ins = normalizeInsight(raw, { scope: 'task', source, nowIso: now, sourceTaskIds: [task.id], confidence: raw.confidence })
|
|
@@ -921,6 +1040,8 @@ export function promoteAllTasksToProject(store, globalStore, cfg, now) {
|
|
|
921
1040
|
const list = task.insights || []
|
|
922
1041
|
for (const ins of [...list]) {
|
|
923
1042
|
if (ins.scope && ins.scope !== 'task') continue
|
|
1043
|
+
// 草稿必须先被确认;自动提升绕过审核就等于把 reflection 的猜测变成项目资产。
|
|
1044
|
+
if (ins.draft === true) continue
|
|
924
1045
|
const members = new Set(ins.sourceTaskIds || [])
|
|
925
1046
|
if (members.size < 2) continue
|
|
926
1047
|
if (num(ins.confidence, 0) < cfg.promoteConfidence) continue
|
|
@@ -964,6 +1085,7 @@ export function promoteProjectToGlobal(store, globalStore, cfg, now) {
|
|
|
964
1085
|
let moved = 0
|
|
965
1086
|
for (const ins of [...items]) {
|
|
966
1087
|
if (ins.scope && ins.scope !== 'project') continue
|
|
1088
|
+
if (ins.draft === true) continue
|
|
967
1089
|
const members = new Set(ins.sourceTaskIds || [])
|
|
968
1090
|
if (members.size < cfg.globalPromoteTasks) continue
|
|
969
1091
|
const glItems = globalStore.items()
|
package/src/lazy.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
2
|
import { statSync } from 'node:fs'
|
|
3
|
-
import { findProjectRoot,
|
|
3
|
+
import { excludedEntryForPath, findProjectRoot, isGitIgnored, memoryRootFor, relativePath, rootRejection, storeKey } from './util/fs.js'
|
|
4
4
|
import { DROP, OVERSIZE, UNCHANGED, commitFileUpdates, fileKind, planFileIndex, toFileUpdate } from './index-pipeline.js'
|
|
5
5
|
import { ProjectMemoryStore } from './store.js'
|
|
6
6
|
import { onFileObserved } from './enhancer.js'
|
|
@@ -12,11 +12,15 @@ export { findProjectRoot }
|
|
|
12
12
|
export async function indexFile(ctx, config, filePath, watchManager = null, sessionRoot = null) {
|
|
13
13
|
const kind = fileKind(path.extname(filePath).toLowerCase())
|
|
14
14
|
if (!kind) return false
|
|
15
|
+
// roots.exclude 是子树级否定:被排除的文件连根都不推导,直接不落任何记忆。
|
|
16
|
+
if (excludedEntryForPath(filePath, config)) return false
|
|
15
17
|
// 根来源优先级:显式 watch 过的根 → 项目标记 → 会话工作目录(安全时)。
|
|
16
18
|
// 显式登记过的根优先,是为了让"没有标记但被 watch_repo 指定过"的项目也能持续刷新。
|
|
17
19
|
const extraRoots = watchManager ? [...watchManager.roots.keys()] : []
|
|
18
|
-
const root = findProjectRoot(filePath, { extraRoots, sessionRoot })
|
|
19
|
-
if (!root ||
|
|
20
|
+
const root = findProjectRoot(filePath, { extraRoots, sessionRoot, config })
|
|
21
|
+
if (!root || rootRejection(root, config)) return false
|
|
22
|
+
// 被 .gitignore 排除的文件(构建产物 / 内部文档)不进语料;显式 index_doc 不受此限。
|
|
23
|
+
if (isGitIgnored(root, filePath)) return false
|
|
20
24
|
|
|
21
25
|
const memoryDir = memoryRootFor(root, config.memoryDir)
|
|
22
26
|
const store = new ProjectMemoryStore(memoryDir).load()
|
package/src/ops.js
CHANGED
package/src/readiness.js
CHANGED
|
@@ -214,7 +214,7 @@ export function intentText(text) {
|
|
|
214
214
|
.replace(/(?:[A-Za-z0-9_.@-]+\/)+[A-Za-z0-9_.@-]+/g, ' ')
|
|
215
215
|
// 带扩展名的**非 ASCII 文件名**(`示例调研.pptx` / `docs/示例.md`):
|
|
216
216
|
// 只挡 ASCII 路径不够——中文文件名整块留下,"调研""面试"这类子串照样触发 when.intents,
|
|
217
|
-
// 正是引号剥离要防的那类假阳性。ASCII 裸名(如
|
|
217
|
+
// 正是引号剥离要防的那类假阳性。ASCII 裸名(如 notes.md)保持原语义,不在这里动。
|
|
218
218
|
.replace(/\S*[^\x00-\x7F]\S*\.[A-Za-z][A-Za-z0-9]{0,7}\b/g, ' ')
|
|
219
219
|
// 仓库里的裸文件名(README / CHANGELOG …)也是语料,不是意图
|
|
220
220
|
.replace(/\b(?:README|CHANGELOG|LICENSE|AGENTS|CONTRIBUTING|Dockerfile|Makefile)\b/g, ' ')
|