@yolk_vat-y/dsh-project-memory 0.6.4 → 0.6.6
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 +111 -1
- package/README.md +35 -7
- package/README.zh-CN.md +34 -7
- package/cordis.patch.yml +6 -1
- package/package.json +6 -4
- package/scripts/bench-edits.mjs +87 -0
- package/skills/project-memory/SKILL.md +72 -0
- package/skills/project-memory/scripts/pm-digest.mjs +138 -0
- package/src/audit.js +6 -0
- package/src/auto-inject.js +72 -24
- package/src/commands/insight-actions.js +1 -1
- package/src/commands/task-actions.js +1 -1
- package/src/commands/tasks.js +1 -1
- package/src/doc-index.js +7 -5
- package/src/doc-pipeline.js +2 -0
- package/src/index.js +25 -11
- package/src/insight-store.js +56 -6
- package/src/lazy.js +5 -3
- package/src/ops.js +226 -36
- package/src/readiness.js +60 -9
- package/src/recall.js +5 -1
- package/src/setup/taskbridge.js +3 -3
- package/src/skill.js +84 -0
- package/src/store.js +123 -28
- package/src/tools/index-doc.js +11 -3
- package/src/tools/index-repo.js +4 -4
- package/src/tools/watch-repo.js +3 -3
- package/src/util/flush.js +62 -0
- package/src/util/fs.js +602 -71
- package/src/util/persist.js +27 -0
- package/src/util/ru.js +201 -0
- package/src/util/search.js +20 -0
- package/src/vector-index.js +4 -0
- package/src/watch.js +48 -17
package/src/store.js
CHANGED
|
@@ -3,6 +3,8 @@ import { mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, unl
|
|
|
3
3
|
import path from 'node:path'
|
|
4
4
|
import { tokenizeRaw, makeSearchText } from './util/search.js'
|
|
5
5
|
import { acquireDirLock, writeJsonAtomic } from './util/fs.js'
|
|
6
|
+
import { debugPersistence } from './util/persist.js'
|
|
7
|
+
import { trackDirty, untrackDirty } from './util/flush.js'
|
|
6
8
|
import { backfillDerivedTriggers } from './readiness.js'
|
|
7
9
|
|
|
8
10
|
const FORMAT_FILE = 'format.json'
|
|
@@ -378,6 +380,22 @@ export class ProjectMemoryStore {
|
|
|
378
380
|
this._removedWatch = new Set()
|
|
379
381
|
/** 上次落盘/加载时的 insight id 集合:原地 splice 的删除也要能被合并逻辑看见。 */
|
|
380
382
|
this._baseInsightIds = new Set()
|
|
383
|
+
/**
|
|
384
|
+
* 本进程**新建**的条目。合并另一进程的磁盘状态时,"磁盘没有但内存有"有两种可能:
|
|
385
|
+
* 我新建的(要补写)或**对方删了**(不能写回)。没有这个集合,后者就会被复活
|
|
386
|
+
* (实测:B 真删、A 下一次注入记账触发 save,已删条目重新出现在磁盘上)。
|
|
387
|
+
*/
|
|
388
|
+
this._createdInsightIds = new Set()
|
|
389
|
+
this._createdTaskIds = new Set()
|
|
390
|
+
/** 上一次 save 是否真的落到盘上(锁竞争/权限失败时为 false)。 */
|
|
391
|
+
this.lastSaveOk = true
|
|
392
|
+
/** `shards/` 的 `.tmp` 清扫节流:NTFS 上 readdir(数千分片) 不便宜,不能每次 save 都扫。 */
|
|
393
|
+
this._lastShardTmpSweep = 0
|
|
394
|
+
/** 自我忽略文件已确认存在:省掉每次 save 的 existsSync(Windows 上每次都要过一次过滤驱动)。 */
|
|
395
|
+
this._selfIgnoreReady = false
|
|
396
|
+
/** store 目录 / shards 目录已确认存在:首次写之后不再每次 save 都 mkdir(9p 上每次 ~4ms)。 */
|
|
397
|
+
this._storeDirReady = false
|
|
398
|
+
this._shardsDirReady = false
|
|
381
399
|
this._lockWarned = false
|
|
382
400
|
this._conflictWarned = new Set()
|
|
383
401
|
/** 锁竞争跳过落盘后的有限次延迟重试;unref,避免拖住进程退出。 */
|
|
@@ -663,7 +681,7 @@ export class ProjectMemoryStore {
|
|
|
663
681
|
this.insights.migratedAt = now
|
|
664
682
|
try {
|
|
665
683
|
mkdirSync(this.dir, { recursive: true })
|
|
666
|
-
writeJsonAtomic(path.join(this.dir, INSIGHTS_FILE), this.insights)
|
|
684
|
+
writeJsonAtomic(path.join(this.dir, INSIGHTS_FILE), this.insights, { ensureDir: false })
|
|
667
685
|
} catch (err) {
|
|
668
686
|
// 没写进 insights.json 就**不能**把旧文件收走:那会真的丢数据。
|
|
669
687
|
console.error(`[dsh-project-memory] insights migration write failed, keeping ${EXPERIENCE_FILE}: ${err.message}`)
|
|
@@ -692,7 +710,10 @@ export class ProjectMemoryStore {
|
|
|
692
710
|
const next = new Set((items || []).map((it) => it?.id).filter(Boolean))
|
|
693
711
|
// 与上次落盘的基线比,而不是与当前数组比:调用方经常先原地 splice 再调用这里。
|
|
694
712
|
for (const id of this._baseInsightIds) if (!next.has(id)) this._deletedInsightIds.add(id)
|
|
695
|
-
for (const id of next)
|
|
713
|
+
for (const id of next) {
|
|
714
|
+
this._deletedInsightIds.delete(id)
|
|
715
|
+
if (!this._baseInsightIds.has(id)) this._createdInsightIds.add(id)
|
|
716
|
+
}
|
|
696
717
|
this.insights.items = items
|
|
697
718
|
this._baseInsightIds = next
|
|
698
719
|
this._dirtyInsights = true
|
|
@@ -708,9 +729,16 @@ export class ProjectMemoryStore {
|
|
|
708
729
|
this._auxVersion++
|
|
709
730
|
}
|
|
710
731
|
|
|
711
|
-
cleanStaleTmp() {
|
|
712
|
-
|
|
732
|
+
cleanStaleTmp({ force = false } = {}) {
|
|
733
|
+
// store 根目录只有几个文件,每次 save 扫一遍很便宜(崩溃遗留的 aux `.tmp` 立刻收走)。
|
|
734
|
+
// `shards/` 才是 O(N):12k 分片时每次单文件更新都 readdir 一遍,在 NTFS 上是实打实的卡顿。
|
|
735
|
+
// 分片里的 .tmp 只影响体积、不影响正确性,60s 扫一次足够;`force` 给测试/诊断用。
|
|
713
736
|
const now = Date.now()
|
|
737
|
+
const scanDirs = [this.dir]
|
|
738
|
+
if (force || now - this._lastShardTmpSweep >= 60_000) {
|
|
739
|
+
this._lastShardTmpSweep = now
|
|
740
|
+
scanDirs.push(path.join(this.dir, SHARDS_DIR))
|
|
741
|
+
}
|
|
714
742
|
for (const dir of scanDirs) {
|
|
715
743
|
let entries
|
|
716
744
|
try {
|
|
@@ -741,11 +769,16 @@ export class ProjectMemoryStore {
|
|
|
741
769
|
* 想把记忆跟着仓库提交:`git add -f .dsh-project-memory`——已跟踪的文件不受忽略规则影响。
|
|
742
770
|
*/
|
|
743
771
|
ensureSelfIgnore() {
|
|
772
|
+
if (this._selfIgnoreReady) return
|
|
744
773
|
if (!existsSafe(this.dir)) return
|
|
745
774
|
const file = path.join(this.dir, GITIGNORE_FILE)
|
|
746
|
-
if (existsSafe(file))
|
|
775
|
+
if (existsSafe(file)) {
|
|
776
|
+
this._selfIgnoreReady = true
|
|
777
|
+
return
|
|
778
|
+
}
|
|
747
779
|
try {
|
|
748
780
|
writeFileSync(file, '# dsh-project-memory: local by default. `git add -f` to commit it.\n*\n')
|
|
781
|
+
this._selfIgnoreReady = true
|
|
749
782
|
} catch {
|
|
750
783
|
// 旁路:写不进去不影响存储本身
|
|
751
784
|
}
|
|
@@ -762,7 +795,12 @@ export class ProjectMemoryStore {
|
|
|
762
795
|
|| this._dirtyWatch
|
|
763
796
|
}
|
|
764
797
|
|
|
765
|
-
|
|
798
|
+
/**
|
|
799
|
+
* @param {{ timeoutMs?: number }} [options] 等锁上限。默认 15s:写路径的正确性靠**同步等待**,
|
|
800
|
+
* 不靠后台重试——用户结束对话就会关 dsh,不会等重试。重试只是长驻进程的兜底。
|
|
801
|
+
* @returns {boolean} 是否真的落盘(无脏数据也算成功)
|
|
802
|
+
*/
|
|
803
|
+
save(options = {}) {
|
|
766
804
|
// 没有脏数据就不落盘。watch 每轮对每个根都无条件 commit → save;照旧执行的话,
|
|
767
805
|
// 末尾的 ID 缓存失效会打在跨实例共享的 store 上,等于每轮清空一次 IDF 复用(v0.3.4 的 20x)。
|
|
768
806
|
// 待压实队列不算"脏数据",但它需要有界推进,所以也走这条落盘路径。
|
|
@@ -776,27 +814,41 @@ export class ProjectMemoryStore {
|
|
|
776
814
|
this._dirtyBinding ||
|
|
777
815
|
this._dirtyWatch ||
|
|
778
816
|
!this._formatWritten
|
|
779
|
-
if (dirty
|
|
817
|
+
if (dirty && !this._storeDirReady) {
|
|
818
|
+
mkdirSync(this.dir, { recursive: true })
|
|
819
|
+
this._storeDirReady = true
|
|
820
|
+
}
|
|
780
821
|
// 崩溃遗留的 *.tmp 无论有没有脏数据都顺手清掉(两次 readdir,自带 try/catch)
|
|
781
822
|
this.cleanStaleTmp()
|
|
782
823
|
// 自我忽略也在无脏数据时执行:老版本建出来的 store 会在下一次 save 时补上。
|
|
783
824
|
this.ensureSelfIgnore()
|
|
784
|
-
if (!dirty)
|
|
785
|
-
|
|
786
|
-
|
|
825
|
+
if (!dirty) {
|
|
826
|
+
this.lastSaveOk = true
|
|
827
|
+
untrackDirty(this)
|
|
828
|
+
return true
|
|
829
|
+
}
|
|
830
|
+
// 跨进程:整段读改写放进目录锁。默认同步等待 15s——写路径的正确性靠"等到锁",
|
|
831
|
+
// 不靠后台重试(用户结束对话就关 dsh)。真拿不到时保持脏状态并登记退出 flush;
|
|
832
|
+
// 默认静默,不向用户/模型抛提示。
|
|
833
|
+
const timeoutMs = Number.isFinite(options.timeoutMs) ? options.timeoutMs : 15000
|
|
834
|
+
const release = acquireDirLock(this.dir, { timeoutMs, ensureDir: false })
|
|
787
835
|
if (!release) {
|
|
836
|
+
this.lastSaveOk = false
|
|
837
|
+
trackDirty(this)
|
|
788
838
|
if (!this._lockWarned) {
|
|
789
839
|
this._lockWarned = true
|
|
790
|
-
|
|
840
|
+
debugPersistence(`[dsh-project-memory] store ${this.dir} is busy (another dsh process is writing); this write is pending.`)
|
|
791
841
|
}
|
|
792
842
|
this._scheduleSaveRetry()
|
|
793
|
-
return
|
|
843
|
+
return false
|
|
794
844
|
}
|
|
845
|
+
let wroteInsights = false
|
|
846
|
+
let wroteTasks = false
|
|
795
847
|
try {
|
|
796
848
|
// 先合并另一进程已落盘的小文档,再写;分片按“加载时 hash”做冲突检测。
|
|
797
849
|
this._mergeAuxFromDisk()
|
|
798
850
|
if (!this._formatWritten) {
|
|
799
|
-
writeJsonAtomic(path.join(this.dir, FORMAT_FILE), { version: 2, layout: 'sharded' })
|
|
851
|
+
writeJsonAtomic(path.join(this.dir, FORMAT_FILE), { version: 2, layout: 'sharded' }, { ensureDir: false })
|
|
800
852
|
this._formatWritten = true
|
|
801
853
|
}
|
|
802
854
|
// 存量压实:≤0.5.10 的分片带着派生字段,而那些字段只在分片被重写时才会从磁盘消失。
|
|
@@ -820,7 +872,8 @@ export class ProjectMemoryStore {
|
|
|
820
872
|
const localAt = Date.parse(this.files[rel].indexedAt || '') || 0
|
|
821
873
|
const diskAt = Date.parse(disk.record?.indexedAt || '') || 0
|
|
822
874
|
if (localAt > diskAt) {
|
|
823
|
-
|
|
875
|
+
this._ensureShardsDir()
|
|
876
|
+
writeJsonAtomic(shardRelPath(this.dir, rel), { relPath: rel, record: this.files[rel], entries: (this.entries[rel] || []).map(withoutPersistedDerived) }, { ensureDir: false })
|
|
824
877
|
this._baseFileSha.set(rel, nextSha)
|
|
825
878
|
continue
|
|
826
879
|
}
|
|
@@ -828,8 +881,8 @@ export class ProjectMemoryStore {
|
|
|
828
881
|
this._warnConflict(rel)
|
|
829
882
|
continue
|
|
830
883
|
}
|
|
831
|
-
|
|
832
|
-
writeJsonAtomic(shardRelPath(this.dir, rel), { relPath: rel, record: this.files[rel], entries: (this.entries[rel] || []).map(withoutPersistedDerived) })
|
|
884
|
+
this._ensureShardsDir()
|
|
885
|
+
writeJsonAtomic(shardRelPath(this.dir, rel), { relPath: rel, record: this.files[rel], entries: (this.entries[rel] || []).map(withoutPersistedDerived) }, { ensureDir: false })
|
|
833
886
|
this._baseFileSha.set(rel, nextSha)
|
|
834
887
|
} else {
|
|
835
888
|
this._removedShards.add(rel)
|
|
@@ -854,29 +907,40 @@ export class ProjectMemoryStore {
|
|
|
854
907
|
this._baseFileSha.delete(rel)
|
|
855
908
|
}
|
|
856
909
|
this._removedShards.clear()
|
|
857
|
-
|
|
910
|
+
wroteInsights = this._dirtyInsights
|
|
911
|
+
wroteTasks = this._dirtyTasks
|
|
912
|
+
if (wroteInsights) {
|
|
858
913
|
// v2 → v3 是纯语义升级(`kind:'experience'` 从"影子副本"变成一等实体),形状不变;
|
|
859
914
|
// 落盘即把版本推上去,老版本读到 v3 也照样按 v2 的字段解析(它只是排除 experience 影子)。
|
|
860
915
|
if (this.insights && this.insights.version !== 3) this.insights.version = 3
|
|
861
|
-
writeJsonAtomic(path.join(this.dir, INSIGHTS_FILE), this.insights)
|
|
916
|
+
writeJsonAtomic(path.join(this.dir, INSIGHTS_FILE), this.insights, { ensureDir: false })
|
|
862
917
|
this._dirtyInsights = false
|
|
863
918
|
}
|
|
864
919
|
if (this._dirtyTasks) {
|
|
865
|
-
writeJsonAtomic(path.join(this.dir, TASKS_FILE), this.tasks)
|
|
920
|
+
writeJsonAtomic(path.join(this.dir, TASKS_FILE), this.tasks, { ensureDir: false })
|
|
866
921
|
this._dirtyTasks = false
|
|
867
922
|
}
|
|
868
923
|
if (this._dirtyBinding) {
|
|
869
|
-
writeJsonAtomic(path.join(this.dir, BINDING_FILE), this.binding)
|
|
924
|
+
writeJsonAtomic(path.join(this.dir, BINDING_FILE), this.binding, { ensureDir: false })
|
|
870
925
|
this._dirtyBinding = false
|
|
871
926
|
}
|
|
872
927
|
if (this._dirtyWatch) {
|
|
873
|
-
writeJsonAtomic(path.join(this.dir, WATCH_FILE), this.watchlist)
|
|
928
|
+
writeJsonAtomic(path.join(this.dir, WATCH_FILE), this.watchlist, { ensureDir: false })
|
|
874
929
|
this._dirtyWatch = false
|
|
875
930
|
}
|
|
876
931
|
} finally {
|
|
877
932
|
release()
|
|
878
933
|
}
|
|
879
934
|
this._saveRetryAttempts = 0
|
|
935
|
+
this.lastSaveOk = true
|
|
936
|
+
untrackDirty(this)
|
|
937
|
+
// 落盘成功才推进基线:失败时保持"本进程新建"的标记,下一次 save 仍要补写。
|
|
938
|
+
if (wroteInsights) {
|
|
939
|
+
this._baseInsightIds = new Set(this.insights.items.map((it) => it?.id).filter(Boolean))
|
|
940
|
+
this._createdInsightIds.clear()
|
|
941
|
+
}
|
|
942
|
+
if (wroteTasks) this._createdTaskIds.clear()
|
|
943
|
+
return true
|
|
880
944
|
}
|
|
881
945
|
|
|
882
946
|
/**
|
|
@@ -910,18 +974,33 @@ export class ProjectMemoryStore {
|
|
|
910
974
|
}
|
|
911
975
|
}
|
|
912
976
|
|
|
977
|
+
/** 首次写分片前建一次 shards/;之后每次 save 都省掉一次 mkdir。 */
|
|
978
|
+
_ensureShardsDir() {
|
|
979
|
+
if (this._shardsDirReady) return
|
|
980
|
+
mkdirSync(path.join(this.dir, SHARDS_DIR), { recursive: true })
|
|
981
|
+
this._shardsDirReady = true
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
/** 退出/卸载路径的同步兜底(见 util/flush.js)。 */
|
|
985
|
+
flushPending(timeoutMs = 1500) {
|
|
986
|
+
return this.save({ timeoutMs })
|
|
987
|
+
}
|
|
988
|
+
|
|
913
989
|
/** 锁竞争后的有限次延迟重试;失败仍保持脏状态,下一次写继续尝试。 */
|
|
914
990
|
_scheduleSaveRetry() {
|
|
915
|
-
|
|
916
|
-
|
|
991
|
+
// 旧实现 3 次 × ~2s ≈ 8.3s,**永远小于** staleMs=10s:一个刚崩溃的跨机锁会让所有重试落空。
|
|
992
|
+
// 同机崩溃锁现在由 pid 判活立即回收(见 util/fs.js),这里的预算只兜跨机 stale 锁。
|
|
993
|
+
const delays = [100, 500, 2000, 6000, 12000]
|
|
994
|
+
if (this._saveRetryTimer || this._saveRetryAttempts >= delays.length) return
|
|
995
|
+
const delay = delays[this._saveRetryAttempts++]
|
|
917
996
|
this._saveRetryTimer = setTimeout(() => {
|
|
918
997
|
this._saveRetryTimer = null
|
|
919
998
|
try {
|
|
920
|
-
this.save()
|
|
999
|
+
this.save({ timeoutMs: 2000 })
|
|
921
1000
|
} catch (err) {
|
|
922
1001
|
console.error(`[dsh-project-memory] deferred save failed: ${err?.message || err}`)
|
|
923
1002
|
}
|
|
924
|
-
},
|
|
1003
|
+
}, delay)
|
|
925
1004
|
this._saveRetryTimer.unref?.()
|
|
926
1005
|
}
|
|
927
1006
|
|
|
@@ -955,7 +1034,12 @@ export class ProjectMemoryStore {
|
|
|
955
1034
|
continue
|
|
956
1035
|
}
|
|
957
1036
|
const prev = byId.get(it.id)
|
|
958
|
-
if (!prev
|
|
1037
|
+
if (!prev) {
|
|
1038
|
+
// 磁盘没有:本进程新建的才补写;否则是**对方删了**,丢掉内存里的旧副本。
|
|
1039
|
+
if (this._createdInsightIds.has(it.id)) byId.set(it.id, it)
|
|
1040
|
+
continue
|
|
1041
|
+
}
|
|
1042
|
+
if (newerIso(it.updatedAt, prev.updatedAt) >= 0) byId.set(it.id, it)
|
|
959
1043
|
}
|
|
960
1044
|
for (const id of this._deletedInsightIds) byId.delete(id)
|
|
961
1045
|
this.insights = { ...this.insights, items: [...byId.values()] }
|
|
@@ -974,8 +1058,16 @@ export class ProjectMemoryStore {
|
|
|
974
1058
|
continue
|
|
975
1059
|
}
|
|
976
1060
|
const prev = byId.get(t.id)
|
|
977
|
-
if (!prev
|
|
1061
|
+
if (!prev) {
|
|
1062
|
+
// 同 insights:磁盘缺失 = 对方删了,只有本进程新建的任务才补写。
|
|
1063
|
+
if (this._createdTaskIds.has(t.id)) byId.set(t.id, t)
|
|
1064
|
+
continue
|
|
1065
|
+
}
|
|
1066
|
+
if (newerIso(t.updatedAt || t.lastActiveAt, prev.updatedAt || prev.lastActiveAt) >= 0) byId.set(t.id, t)
|
|
978
1067
|
}
|
|
1068
|
+
// 本地删除必须从磁盘副本里剔除:`removeTask` 已把它从内存数组移除,上面的循环
|
|
1069
|
+
// 根本看不到它;少了这一行,删任务会在**同一次 save 里**被另一份磁盘快照写回来。
|
|
1070
|
+
for (const id of this._deletedTaskIds) byId.delete(id)
|
|
979
1071
|
this.tasks = [...byId.values()]
|
|
980
1072
|
this._auxVersion++
|
|
981
1073
|
}
|
|
@@ -1178,7 +1270,10 @@ export class ProjectMemoryStore {
|
|
|
1178
1270
|
|
|
1179
1271
|
addTask(task) {
|
|
1180
1272
|
this.tasks.push(task)
|
|
1181
|
-
if (task?.id)
|
|
1273
|
+
if (task?.id) {
|
|
1274
|
+
this._deletedTaskIds.delete(task.id)
|
|
1275
|
+
this._createdTaskIds.add(task.id)
|
|
1276
|
+
}
|
|
1182
1277
|
this._dirtyTasks = true
|
|
1183
1278
|
this._auxVersion++
|
|
1184
1279
|
}
|
package/src/tools/index-doc.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
2
2
|
import path from 'node:path'
|
|
3
|
-
import { assertIndexRoot, assertReadableFile, assertSafeRoot, findProjectRoot, memoryRootFor, sessionMemoryRootOrNull, sha256OfFile, storeKey } from '../util/fs.js'
|
|
3
|
+
import { assertIndexRoot, assertReadableFile, assertSafeRoot, excludedEntryForPath, findProjectRoot, memoryRootFor, sessionMemoryRootOrNull, sha256OfFile, storeKey } from '../util/fs.js'
|
|
4
4
|
import { buildDocEntries } from '../doc-pipeline.js'
|
|
5
5
|
import { docEntriesNeedBackfill } from '../doc-index.js'
|
|
6
6
|
import { ProjectMemoryStore } from '../store.js'
|
|
@@ -27,16 +27,24 @@ export function indexDocTool(ctx, config) {
|
|
|
27
27
|
},
|
|
28
28
|
async execute(args, exec) {
|
|
29
29
|
const filePath = assertReadableFile(args.file_path, config.maxFileSizeMb)
|
|
30
|
+
// roots.exclude 是"这棵子树里的文件一律不进索引":显式 index_doc 也要服从。
|
|
31
|
+
const excluded = excludedEntryForPath(filePath, config)
|
|
32
|
+
if (excluded) {
|
|
33
|
+
return (
|
|
34
|
+
`Not indexed: ${filePath} is inside the subtree excluded by \`roots.exclude\` entry ${JSON.stringify(excluded.raw)}. ` +
|
|
35
|
+
'Remove that entry from `config.roots.exclude` under `- id: project-memory` if this file should be indexed.'
|
|
36
|
+
)
|
|
37
|
+
}
|
|
30
38
|
const explicit = args.root && args.root.trim() ? args.root : null
|
|
31
39
|
let root
|
|
32
40
|
if (explicit) {
|
|
33
41
|
root = path.resolve(explicit)
|
|
34
42
|
assertIndexRoot(root, explicit)
|
|
35
|
-
assertSafeRoot(root, { requested: explicit,
|
|
43
|
+
assertSafeRoot(root, { requested: explicit, config })
|
|
36
44
|
} else {
|
|
37
45
|
// 与懒索引同序:文件自身的项目标记优先,其次才是会话记忆根(安全但无标记的 cwd)。
|
|
38
46
|
const sessionRoot = sessionMemoryRootOrNull(exec, config)
|
|
39
|
-
root = findProjectRoot(filePath, { sessionRoot })
|
|
47
|
+
root = findProjectRoot(filePath, { sessionRoot, config })
|
|
40
48
|
if (!root) {
|
|
41
49
|
return (
|
|
42
50
|
`Not indexed: ${filePath} is not inside a detected project ` +
|
package/src/tools/index-repo.js
CHANGED
|
@@ -17,7 +17,7 @@ export async function indexRepository(ctx, config, root, { reindex = false, allo
|
|
|
17
17
|
// 不存在的 root 连同 .dsh-project-memory 一起 mkdirSync 出来。
|
|
18
18
|
assertIndexRoot(root)
|
|
19
19
|
// 再校验安全:家目录 / 系统目录 / Homebrew 前缀整体索引会吃满内存(issue #5)。
|
|
20
|
-
assertSafeRoot(root, { allowUnsafe })
|
|
20
|
+
assertSafeRoot(root, { allowUnsafe, config })
|
|
21
21
|
const memoryDir = memoryRootFor(root, config.memoryDir)
|
|
22
22
|
const store = new ProjectMemoryStore(memoryDir).load()
|
|
23
23
|
|
|
@@ -41,6 +41,7 @@ export async function indexRepository(ctx, config, root, { reindex = false, allo
|
|
|
41
41
|
const { files, truncated, capped, skipped: nestedRoots, unreadable } = listProjectFiles(root, {
|
|
42
42
|
...scanLimits(config),
|
|
43
43
|
nestedStoreName: memoryDirName(config),
|
|
44
|
+
config,
|
|
44
45
|
})
|
|
45
46
|
for (const filePath of files) {
|
|
46
47
|
const rel = storeKey(relativePath(root, filePath))
|
|
@@ -153,9 +154,8 @@ export function indexRepoTool(ctx, config) {
|
|
|
153
154
|
// 透传原始入参:Windows 风格路径在 POSIX 上会被 resolve 成 <cwd>/D:\...,
|
|
154
155
|
// 报错时要点明这是路径风格问题,而不是“目录被删了”。
|
|
155
156
|
assertIndexRoot(root, args.root)
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
return indexRepository(ctx, config, root, { reindex: Boolean(args.reindex), allowUnsafe })
|
|
157
|
+
assertSafeRoot(root, { requested: args.root, config })
|
|
158
|
+
return indexRepository(ctx, config, root, { reindex: Boolean(args.reindex) })
|
|
159
159
|
},
|
|
160
160
|
})
|
|
161
161
|
}
|
package/src/tools/watch-repo.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
2
2
|
import path from 'node:path'
|
|
3
|
-
import { assertIndexRoot, assertSafeRoot,
|
|
3
|
+
import { assertIndexRoot, assertSafeRoot, memoryRootFor, rootRejection, sessionMemoryRootOrNull } from '../util/fs.js'
|
|
4
4
|
import { ProjectMemoryStore } from '../store.js'
|
|
5
5
|
|
|
6
6
|
export function watchRepoTool(watchManager, config) {
|
|
@@ -31,7 +31,7 @@ export function watchRepoTool(watchManager, config) {
|
|
|
31
31
|
if (args.watch !== false) {
|
|
32
32
|
try {
|
|
33
33
|
assertIndexRoot(root, args.root)
|
|
34
|
-
assertSafeRoot(root, { requested: args.root,
|
|
34
|
+
assertSafeRoot(root, { requested: args.root, config })
|
|
35
35
|
} catch (err) {
|
|
36
36
|
return `Not watching: ${err.message}`
|
|
37
37
|
}
|
|
@@ -58,7 +58,7 @@ export function watchRepoTool(watchManager, config) {
|
|
|
58
58
|
// (这正是要修的场景),而写它会在家目录里造出 .dsh-project-memory。那份
|
|
59
59
|
// watch.json 现在也没有任何启动路径会读取(restorePersisted 只认会话 cwd),
|
|
60
60
|
// 留着即可;会话侧的镜像条目照常摘掉。
|
|
61
|
-
if (!
|
|
61
|
+
if (!rootRejection(root, config)) {
|
|
62
62
|
new ProjectMemoryStore(memoryDir).load().commit((s) => s.removeWatch(root))
|
|
63
63
|
}
|
|
64
64
|
await mirrorSessionWatchlist(false)
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 退出/卸载前的最后一道写盘兜底。
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要:保存失败后的后台重试**不能**当正确性保证——用户结束对话、关掉 dsh 时不会等
|
|
5
|
+
* 20s 的重试。正确性来自两点:① `save()` 在锁竞争时同步等待(见 store.js 的 LOCK_WAIT_MS);
|
|
6
|
+
* ② 万一仍失败,进程正常退出或插件卸载时再同步写一次。
|
|
7
|
+
*
|
|
8
|
+
* 注册的是脏 store(而不是所有 store):只有内存里还有未落盘变更的才需要 flush。
|
|
9
|
+
* `process.on('exit')` 只允许同步代码,`save()` 本身是同步的,所以这里直接调用。
|
|
10
|
+
* 信号导致的异常退出(SIGKILL / 未处理的 SIGINT)不保证走到这里——那是操作系统的边界。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** @type {Set<{ flushPending?: (timeoutMs: number) => boolean }>} */
|
|
14
|
+
const pending = new Set()
|
|
15
|
+
let installed = false
|
|
16
|
+
|
|
17
|
+
function install() {
|
|
18
|
+
if (installed) return
|
|
19
|
+
installed = true
|
|
20
|
+
process.on('exit', () => {
|
|
21
|
+
flushDirtyStores({ timeoutMs: 1500 })
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 标记"这个 store 有未落盘的变更"。
|
|
27
|
+
* @param {{ flushPending?: (timeoutMs: number) => boolean }} store
|
|
28
|
+
*/
|
|
29
|
+
export function trackDirty(store) {
|
|
30
|
+
pending.add(store)
|
|
31
|
+
install()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 落盘成功后取消标记。
|
|
36
|
+
* @param {object} store
|
|
37
|
+
*/
|
|
38
|
+
export function untrackDirty(store) {
|
|
39
|
+
pending.delete(store)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 同步 flush 所有脏 store。退出钩子与插件 disposer 共用;测试可直接调用。
|
|
44
|
+
* @param {{ timeoutMs?: number }} [opts] 每个 store 最多等锁多久(退出路径要短,避免"关不掉")
|
|
45
|
+
* @returns {number} 成功落盘的 store 数
|
|
46
|
+
*/
|
|
47
|
+
export function flushDirtyStores({ timeoutMs = 1500 } = {}) {
|
|
48
|
+
let saved = 0
|
|
49
|
+
for (const store of [...pending]) {
|
|
50
|
+
try {
|
|
51
|
+
if (typeof store.flushPending === 'function' && store.flushPending(timeoutMs)) saved++
|
|
52
|
+
} catch {
|
|
53
|
+
// 退出路径上的失败只能放弃:不抛、不打印,避免把关闭流程变成错误弹窗
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return saved
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 仅供测试:当前还有多少个脏 store 挂着。 */
|
|
60
|
+
export function _pendingDirtyCountForTest() {
|
|
61
|
+
return pending.size
|
|
62
|
+
}
|