@yolk_vat-y/dsh-project-memory 0.5.14 → 0.5.15

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.
@@ -19,9 +19,9 @@ export function listTasksTool(config) {
19
19
  return defineTool({
20
20
  name: 'list_tasks',
21
21
  description:
22
- 'List all task records in this project (archived included, marked). Call this first in a new session or before continuing work, to see what tasks exist and pick one with select_task.',
22
+ '列出本项目的任务记录(含归档,带标记)。新会话或续接工作前先看它,再用 select_task 选一个。',
23
23
  parameters: {
24
- root: { type: 'string', description: 'Project root. Defaults to current working directory.' },
24
+ root: { type: 'string', description: '项目根目录,默认当前工作目录。' },
25
25
  },
26
26
  output: { schema: { type: 'string' }, render: (_a, v) => [{ type: 'text', text: v }] },
27
27
  async execute(args, exec) {
@@ -44,11 +44,12 @@ export function selectTaskTool(config, host) {
44
44
  return defineTool({
45
45
  name: 'select_task',
46
46
  description:
47
- 'Bind the current session to a task record so its todo list and read files sync into it. When starting NEW work, call select_task(title="任务名") FIRST, then maintain your plan with todo_write — you choose the task title (short, no need to restate the user message). taskId: exact id from list_tasks (auto-unarchives; pass title too to rename). title: exact match only; multiple matches return candidates; no match creates a new task. Returns a task card (title, steps, files) to rebuild the session todo from. Call list_tasks first in a new session.',
47
+ '把会话绑定到一个任务:此后 todo 清单与读过的文件都同步进该任务。开始新工作前先调它(title 自取,短的就行,不用复述用户原话),再用 todo_write 维护计划。' +
48
+ 'taskId:list_tasks 里的精确 id(会自动解归档;同时给 title 则改名)。title:完全匹配,多个同名返回候选,无匹配则新建。返回任务卡(title/steps/files)用于重建会话清单。',
48
49
  parameters: {
49
50
  taskId: { type: 'string', description: 'Exact task id, e.g. tsk_ab12cd34_...' },
50
51
  title: { type: 'string', description: 'Exact task title; creates a new task when absent' },
51
- root: { type: 'string', description: 'Project root. Defaults to current working directory.' },
52
+ root: { type: 'string', description: '项目根目录,默认当前工作目录。' },
52
53
  },
53
54
  output: { schema: { type: 'string' }, render: (_a, v) => [{ type: 'text', text: v }] },
54
55
  async execute(args, exec) {
@@ -168,7 +169,7 @@ export function archiveTaskTool(config, host) {
168
169
  description: 'Archive a task: hide from default views, exclude from capacity, stop syncing. Restore by selecting it again.',
169
170
  parameters: {
170
171
  taskId: { type: 'string', required: true, description: 'Task id to archive (from list_tasks / select_task)' },
171
- root: { type: 'string', description: 'Project root. Defaults to current working directory.' },
172
+ root: { type: 'string', description: '项目根目录,默认当前工作目录。' },
172
173
  },
173
174
  output: { schema: { type: 'string' }, render: (_a, v) => [{ type: 'text', text: v }] },
174
175
  async execute(args, exec) {
@@ -14,7 +14,7 @@ export function watchRepoTool(watchManager, config) {
14
14
  root: {
15
15
  type: 'string',
16
16
  required: true,
17
- description: 'Absolute path to the project root to watch.',
17
+ description: '要监听的项目根目录(绝对路径)。',
18
18
  },
19
19
  watch: {
20
20
  type: 'boolean',
@@ -0,0 +1,32 @@
1
+ // entry id 的「路径前缀」编码:**必须是单射**(不同路径 → 不同前缀)。
2
+ //
3
+ // 为什么值得单独一个模块:同一个规则曾在三处各写一份(doc-pipeline 的 relativeId、
4
+ // symbols.js 的 buildSymbol、enhancer.js 的 applyEnhancedSymbols),三份都是
5
+ // `replace(/[\\/:\s]/g, '_')`。于是 `a b.md` 与 `a_b.md`(以及 `a/b.md`、`a:b.md`、
6
+ // `a\b.md`)得到同一个前缀,`${prefix}#0` 就撞成同一个 id。
7
+ //
8
+ // 撞 id 不是「不好看」,它会让检索**静默少结果**:`rankEntriesStreaming` 按 id 合并
9
+ // 多查询命中(src/util/search.js),两条不同文件的条目会被合成一条、低分的那条被丢掉。
10
+ //
11
+ // 编码规则:
12
+ // - `/` → `_`(最常见的那种分隔符,保持 id 可读:`src/foo.js` → `src_foo.js`);
13
+ // - 其余风险字符(`\` `:` 各类空白 `_` `%` `#`)→ `%` + 该字符码点的十六进制。
14
+ //
15
+ // 单射理由:输出里的 `%` 只可能来自转义(源里的 `%` 一定被转成 `%25`),而每个风险字符
16
+ // 的转义串互不相同、也不同于 `_`(`/` 专属);其余字符原样输出,且原样字符里不可能出现
17
+ // 上面这些风险字符。所以解码唯一。
18
+ //
19
+ // 注意「所有分隔符都变成 `_`」是**不够**的:那正是旧实现的错(`a b.md` 与 `a/b.md` 仍然
20
+ // 同 id)。每种分隔符必须有各自的输出。
21
+ //
22
+ // 后缀(`#<chunkIndex>` / `#<line>`)只由整数构成,因此前缀单射 ⇒ 完整 id 单射。
23
+
24
+ const RISKY = /[\\/:\s_%#]/g
25
+
26
+ /** 把相对路径编码成 entry id 的路径前缀(单射、基本可读)。 */
27
+ export function entryIdPrefix(relPath) {
28
+ return String(relPath).replace(RISKY, (ch) => {
29
+ if (ch === '/') return '_'
30
+ return `%${ch.codePointAt(0).toString(16).toUpperCase().padStart(2, '0')}`
31
+ })
32
+ }
package/src/util/fs.js CHANGED
@@ -155,12 +155,19 @@ export function scanLimits(config) {
155
155
  }
156
156
 
157
157
  /**
158
- * 遍历目录,返回 `{ files, truncated }`。
158
+ * 遍历目录,返回 `{ files, truncated, capped, skipped, unreadable }`。
159
159
  *
160
160
  * 与旧版的两点区别都是 OOM 的直接修复:
161
161
  * - **有上限**:`maxFiles` / `maxDepth` 封顶,超限只做截断标记,不再一路吃内存;
162
162
  * - `truncated` 让调用方区分「这棵树扫完了」和「只扫了一部分」——后者绝不能拿
163
163
  * 本轮未见到的文件去删旧条目(见 index-repo/watch 的 unseen 用法)。
164
+ *
165
+ * `truncated` 现在有**两个来源**,必须分开报告:
166
+ * - `capped`:撞到 `maxFiles` / `maxDepth`;
167
+ * - `unreadable`:某个目录 `readdirSync` 失败(EACCES/EPERM…)。
168
+ * 旧实现把读失败当"这个目录不存在"直接 `continue` —— 于是那棵子树里的文件既不在 `seen` 里、
169
+ * 又不置 `truncated`,调用方就会把它们当"已删除"清掉(派生索引静默丢失,且每次索引都白清一遍)。
170
+ * 读不到 ≠ 不存在:置 `truncated` 是最保守的处置。
164
171
  */
165
172
  /**
166
173
  * 一个目录是否**自带 store**(即它自己已经是一个被索引过的项目根)。
@@ -218,7 +225,9 @@ export function walkDir(root, opts = {}) {
218
225
  const nestedStoreName = opts.nestedStoreName || null
219
226
  const out = []
220
227
  const skipped = []
228
+ const unreadable = []
221
229
  const stack = [[root, 0]]
230
+ let capped = false
222
231
  let truncated = false
223
232
  let hitFileCap = false
224
233
  while (stack.length) {
@@ -227,6 +236,10 @@ export function walkDir(root, opts = {}) {
227
236
  try {
228
237
  entries = readdirSync(dir, { withFileTypes: true })
229
238
  } catch {
239
+ // 读不到 ≠ 不存在。旧实现直接 continue,于是这片子树被当成"已删除"(调用方拿
240
+ // seen 去清 unseen),而且失败是静默的。置 truncated 让调用方跳过清理。
241
+ unreadable.push(dir)
242
+ truncated = true
230
243
  continue
231
244
  }
232
245
  for (const entry of entries) {
@@ -240,12 +253,14 @@ export function walkDir(root, opts = {}) {
240
253
  }
241
254
  if (depth + 1 > maxDepth) {
242
255
  truncated = true
256
+ capped = true
243
257
  continue
244
258
  }
245
259
  stack.push([full, depth + 1])
246
260
  } else {
247
261
  if (out.length >= maxFiles) {
248
262
  truncated = true
263
+ capped = true
249
264
  hitFileCap = true
250
265
  break
251
266
  }
@@ -256,7 +271,8 @@ export function walkDir(root, opts = {}) {
256
271
  }
257
272
  out.sort()
258
273
  skipped.sort()
259
- return { files: out, truncated, skipped }
274
+ unreadable.sort()
275
+ return { files: out, truncated, capped, skipped, unreadable }
260
276
  }
261
277
 
262
278
  export function isSupportedDoc(ext) {
@@ -0,0 +1,40 @@
1
+ /**
2
+ * 路径 token 抽取(纯函数、零依赖,readiness 与 ops 共用)。
3
+ *
4
+ * 为什么会有这个模块:同一套规则原先在 `readiness.js`(抽人类消息/工具参数里的路径)与
5
+ * `ops.js`(抽 shell 命令的写目标)里各抄了一份,而 `ops.js` 那份的注释写着
6
+ * 「与 readiness.extractPaths 同规则」—— 其实不是:它只抄了「带扩展名的路径」,
7
+ * 漏掉了「点开头的裸文件名」(`.gitignore` / `.npmrc` / `.env`)。
8
+ *
9
+ * 后果不是文案问题:`ops.targets` 正是 `when.writes` 的匹配面,于是
10
+ * `rm -f .gitignore` 在 ops 侧抽不出写目标(实测 targets=[]),一条明确写了
11
+ * `writes: ['.gitignore']` 的教训被静默漏召 —— 注释还在替这份漂移担保。
12
+ * 收敛到这里,只保留一个事实,两个调用方拿到的是同一份结果。
13
+ *
14
+ * 规则(两类 token,先「带扩展名」后「点开头」,各自按出现顺序去重):
15
+ * 1. 带扩展名的路径:`src/util/fs.js`、`README.md`、`.env.bak`(点算普通字符);
16
+ * 2. 点开头的裸文件名:只认 token 起始处(行首 / 空白 / 引号 / 左括号之后)的 `.xxx`
17
+ * —— `src/.env` 这类中缀点文件名没有扩展名可依,与普通点号无法区分,不在规则内;
18
+ * 调用方若已知道完整路径,应直接并入,而不是指望从文本里猜。
19
+ *
20
+ * 只导出函数、不导出正则:全局正则一旦被 `.test()`/`.exec()` 用过就带 `lastIndex` 状态,
21
+ * 共享出去等于把「顺序敏感」的坑一起共享。`matchAll` 内部克隆正则,这里无状态。
22
+ * @module dsh-project-memory/util/path-token
23
+ */
24
+
25
+ const PATH_TOKEN = /(?:[A-Za-z0-9_.@-]+\/)*[A-Za-z0-9_.@-]+\.[A-Za-z][A-Za-z0-9]{0,7}\b/g
26
+ const DOTFILE_TOKEN = /(?:^|[\s"'`(])(\.[A-Za-z0-9_-]{2,})/g
27
+
28
+ /**
29
+ * 文本 → 路径 token(去重、顺序确定、无副作用)。
30
+ * @param {unknown} text - 工具调用参数、人类消息或 shell 命令。
31
+ * @returns {string[]} 去重后的路径 token;空/缺失输入返回 `[]`。
32
+ */
33
+ export function extractPathTokens(text) {
34
+ const s = String(text || '')
35
+ if (!s) return []
36
+ const out = new Set()
37
+ for (const m of s.matchAll(PATH_TOKEN)) out.add(m[0])
38
+ for (const m of s.matchAll(DOTFILE_TOKEN)) out.add(m[1])
39
+ return [...out]
40
+ }
package/src/watch.js CHANGED
@@ -13,6 +13,8 @@ export class WatchManager {
13
13
  this.timer = null
14
14
  this._polling = false
15
15
  this._truncationWarned = new Set()
16
+ /** 已就「读不到的目录」告警过的根:同上,只提示一次。 */
17
+ this._unreadableWarned = new Set()
16
18
  /** 已就「跳过了嵌套项目根」告警过的根:同上,只提示一次。 */
17
19
  this._nestedStoreWarned = new Set()
18
20
  }
@@ -165,7 +167,7 @@ export class WatchManager {
165
167
  // 本轮用的 store:命中缓存即 O(1),被逐出则重新读盘。整个 pollRoot 期间共用同一个实例。
166
168
  const store = this.storeFor(root)
167
169
 
168
- const { files, truncated, skipped: nestedRoots } = walkDir(root, {
170
+ const { files, truncated, capped, skipped: nestedRoots, unreadable } = walkDir(root, {
169
171
  ...scanLimits(this.config),
170
172
  nestedStoreName: memoryDirName(this.config),
171
173
  })
@@ -178,7 +180,16 @@ export class WatchManager {
178
180
  'their content is refreshed by their own root, and any duplicate copy previously indexed here is being removed.',
179
181
  )
180
182
  }
181
- if (truncated && !this._truncationWarned.has(root)) {
183
+ if (unreadable.length && !this._unreadableWarned.has(root)) {
184
+ // 读不到 ≠ 已删除:本轮跳过快照清理(见下方 unseen),既有条目原样保留。只提示一次。
185
+ this._unreadableWarned.add(root)
186
+ const rels = unreadable.map((p) => storeKey(relativePath(root, p)))
187
+ console.error(
188
+ `[dsh-project-memory] watch of ${root} could not read ${unreadable.length} director${unreadable.length === 1 ? 'y' : 'ies'} ` +
189
+ `(${rels.join(', ')}); they are skipped and nothing under them is treated as deleted.`,
190
+ )
191
+ }
192
+ if (capped && !this._truncationWarned.has(root)) {
182
193
  // 只提示一次:这是一条**永久**的降级说明,不是每轮都要刷屏的故障。
183
194
  this._truncationWarned.add(root)
184
195
  const { maxFiles, maxDepth } = scanLimits(this.config)
@@ -237,7 +248,8 @@ export class WatchManager {
237
248
 
238
249
  // 单事务写盘;CAS 失败的条目本轮不落快照,下一轮自然重试。
239
250
  // 截断时**不传 unseen**:没扫到的文件不等于被删了,否则一份被上限截掉的树每轮都会
240
- // 把自己的记忆删掉一半(先删再下轮重新索引,纯粹的抖动)。
251
+ // 把自己的记忆删掉一半(先删再下轮重新索引,纯粹的抖动)。`truncated` 同样覆盖
252
+ // "有目录读不到"(EACCES)这一路。
241
253
  const { stale, removed } = commitFileUpdates(store, {
242
254
  updates,
243
255
  unseen: truncated ? null : seen,