@yolk_vat-y/dsh-project-memory 0.6.1 → 0.6.2

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.
@@ -8,6 +8,12 @@ import { cfgInsight, GlobalStore, normalizeInsight, defaultGlobalFile, saveInsig
8
8
  import { NO_PROJECT_ROOT_NOTE, projectRootFor } from '../setup/taskbridge.js'
9
9
  import { fencedJson, invocationContext } from './invocation.js'
10
10
 
11
+ /**
12
+ * 记忆列表单页上限。面板每页只请求 30 条(客户端 PAGE_SIZE),远小于它——
13
+ * 封顶只用于挡住畸形/滥用调用,避免单个响应被抓成超大载荷。
14
+ */
15
+ export const INSIGHT_PAGE_MAX = 200
16
+
11
17
  function insSummary(ins, extra = {}) {
12
18
  const s = ins || {}
13
19
  return {
@@ -53,20 +59,72 @@ function findTaskInsight(store, id) {
53
59
  return null
54
60
  }
55
61
 
56
- /** 列出某 scope 的记忆(task 聚合所有任务草稿;project/global 各自池)。 */
57
- export function listInsights(store, gs, scope) {
62
+ /**
63
+ * 时间戳解析:非法/缺失一律 0,让排序键永远是可比较的数字。
64
+ * @param {unknown} value - ISO 时间字符串。
65
+ * @returns {number} 毫秒时间戳;不可解析时 0。
66
+ */
67
+ function tsOf(value) {
68
+ const t = Date.parse(value == null ? '' : String(value))
69
+ return Number.isFinite(t) ? t : 0
70
+ }
71
+
72
+ /**
73
+ * 记忆列表的**全序**比较器:新 → 旧。
74
+ *
75
+ * 分页要求这个比较器给出**确定性全序**,否则相邻两页会重叠或漏条目:
76
+ * 1. `updatedAt` 降序(编辑会把条目顶到最前,符合"刚学到的先看到");
77
+ * 2. `createdAt` 降序(同一批写入的条目按创建时间分先后);
78
+ * 3. `id` 升序兜底 —— 前两级都相同的条目(同毫秒写入)也必须有个稳定次序。
79
+ * @param {{raw: object}} a - 左侧条目。
80
+ * @param {{raw: object}} b - 右侧条目。
81
+ * @returns {number} 排序结果。
82
+ */
83
+ function compareInsightRecency(a, b) {
84
+ const ua = tsOf(a.raw?.updatedAt) || tsOf(a.raw?.createdAt)
85
+ const ub = tsOf(b.raw?.updatedAt) || tsOf(b.raw?.createdAt)
86
+ if (ua !== ub) return ub - ua
87
+ const ca = tsOf(a.raw?.createdAt)
88
+ const cb = tsOf(b.raw?.createdAt)
89
+ if (ca !== cb) return cb - ca
90
+ return String(a.raw?.id ?? '').localeCompare(String(b.raw?.id ?? ''))
91
+ }
92
+
93
+ /**
94
+ * 列出某 scope 的记忆(task 聚合所有任务草稿;project/global 各自池)。
95
+ *
96
+ * 顺序为**新 → 旧**(见 {@link compareInsightRecency})。分页参数可选:
97
+ * 不给 `limit` 时返回全部(保持既有调用方与测试的行为),给了就只返回
98
+ * `[offset, offset+limit)` 这一段,并回报 `total` 与 `nextOffset`。
99
+ * 载荷是纯函数产物,持久化由命令 handler 负责。
100
+ * @param {object} store - 项目 store。
101
+ * @param {object|null} gs - global store。
102
+ * @param {'task'|'project'|'global'} [scope] - 作用域,默认 project。
103
+ * @param {{offset?: number, limit?: number|null}} [opts] - 分页窗口;`limit` 为 null/undefined 表示不分页。
104
+ * @returns {{scope: string, count: number, total: number, offset: number, items: object[], nextOffset: number|null}} 载荷。
105
+ */
106
+ export function listInsights(store, gs, scope, opts = {}) {
58
107
  const s = scope || 'project'
59
- const items = []
108
+ // 先收集「原始条目 + 附加字段」,**排序在摘要之前**:摘要会丢掉 createdAt,
109
+ // 而这个字段是稳定全序的第二级键。
110
+ const collected = []
60
111
  if (s === 'task') {
61
112
  for (const t of store.getTasks() || []) {
62
- for (const i of t.insights || []) items.push(insSummary(i, { taskId: t.id, taskTitle: t.title }))
113
+ for (const i of t.insights || []) collected.push({ raw: i, extra: { taskId: t.id, taskTitle: t.title } })
63
114
  }
64
115
  } else if (s === 'global') {
65
- for (const i of (gs ? gs.items() : [])) items.push(insSummary(i))
116
+ for (const i of (gs ? gs.items() : [])) collected.push({ raw: i, extra: {} })
66
117
  } else {
67
- for (const i of store.insightItems()) items.push(insSummary(i))
118
+ for (const i of store.insightItems()) collected.push({ raw: i, extra: {} })
68
119
  }
69
- return { scope: s, count: items.length, items }
120
+ collected.sort(compareInsightRecency)
121
+
122
+ const total = collected.length
123
+ const limit = typeof opts.limit === 'number' && opts.limit > 0 ? opts.limit : null
124
+ const offset = Number.isFinite(opts.offset) && opts.offset > 0 ? Math.floor(opts.offset) : 0
125
+ const window = limit === null ? collected.slice(offset) : collected.slice(offset, offset + limit)
126
+ const nextOffset = offset + window.length < total ? offset + window.length : null
127
+ return { scope: s, count: total, total, offset, items: window.map((r) => insSummary(r.raw, r.extra)), nextOffset }
70
128
  }
71
129
 
72
130
  /**
@@ -327,8 +385,8 @@ export function editMemoryItem({ store, gs, scope, id, fields, root }) {
327
385
  export function insightCommandDefinition(config, ctx) {
328
386
  return {
329
387
  name: 'insight',
330
- description: '记忆视图动作(面板按钮调用):/tasks insight list project|global|task;/tasks insight <confirm|promote|demote|archive|restore|delete> <scope> <id> [taskId]',
331
- input: { hint: 'list [scope] | <动作> <scope> <id> [taskId]' },
388
+ description: '记忆视图动作(面板按钮调用):/tasks insight list project|global|task [offset] [limit];/tasks insight <confirm|promote|demote|archive|restore|delete> <scope> <id> [taskId]',
389
+ input: { hint: 'list [scope] [offset] [limit] | <动作> <scope> <id> [taskId]' },
332
390
  handler: (invocation) => {
333
391
  try {
334
392
  const { cwd } = invocationContext(invocation)
@@ -340,10 +398,26 @@ export function insightCommandDefinition(config, ctx) {
340
398
  const verb = raw[0]
341
399
  if (verb === 'list') {
342
400
  const scope = raw[1] === 'task' || raw[1] === 'global' || raw[1] === 'project' ? raw[1] : 'project'
401
+ // 分页参数可选:省略即维持"返回全部"的旧行为(老客户端 bundle 仍按此调用)。
402
+ // 显式给 limit 时封顶 INSIGHT_PAGE_MAX,避免一次请求把整个池子拉成超大载荷。
403
+ let offset = 0
404
+ let limit = null
405
+ if (raw[2] !== undefined) {
406
+ const n = Number.parseInt(raw[2], 10)
407
+ if (!Number.isFinite(n) || n < 0) return { kind: 'error', text: '[insight] offset 必须是不小于 0 的整数' }
408
+ offset = n
409
+ }
410
+ if (raw[3] !== undefined) {
411
+ const n = Number.parseInt(raw[3], 10)
412
+ if (!Number.isFinite(n) || n <= 0) return { kind: 'error', text: '[insight] limit 必须是正整数' }
413
+ limit = Math.min(n, INSIGHT_PAGE_MAX)
414
+ }
343
415
  const store = new ProjectMemoryStore(memoryRootFor(root, config.memoryDir)).load()
344
416
  const gs = new GlobalStore(cfgInsight(config).globalFile || defaultGlobalFile()).load()
345
- const payload = listInsights(store, gs, scope)
346
- return { kind: 'success', text: fencedJson(`记忆 ${scope}:${payload.count} 条`, JSON.stringify(payload)) }
417
+ const payload = listInsights(store, gs, scope, { offset, limit })
418
+ const shown = offset + payload.items.length
419
+ const range = limit === null ? '' : `(显示 ${shown}/${payload.total})`
420
+ return { kind: 'success', text: fencedJson(`记忆 ${scope}:${payload.count} 条${range}`, JSON.stringify(payload)) }
347
421
  }
348
422
  if (verb === 'save') {
349
423
  // /insight save <scope> <json>(新建/合并;project|global)
@@ -3,6 +3,7 @@
3
3
  * 「解析 CommandInvocation → 会话与 cwd」和「拼 JSON 围栏载荷」两段代码。
4
4
  * 集中在这里,语义只在一处维护。
5
5
  */
6
+ import { sessionWorkingDir } from '../util/session-dir.js'
6
7
 
7
8
  /**
8
9
  * 从命令调用里解出会话上下文。
@@ -20,7 +21,9 @@ export function invocationContext(invocation) {
20
21
  // 旧写法只在 agent.ctx 不存在时退回:服务在、但 get 返回 undefined 时 session 直接是 undefined,
21
22
  // /task switch|todos|unbind 于是静默跳过 session.append(宿主 todo 同步失效)。
22
23
  const session = fromService || agent?.session || null
23
- const cwd = session?.header?.cwd || agent?.session?.header?.cwd
24
+ // 当前工作目录走 util/session-dir.js:0.2.1+ 由 workingDirectory 投影决定(cd 后会变),
25
+ // 老宿主回退 header.cwd —— 面板/任务桥的"本项目"必须与工具侧同一口径。
26
+ const cwd = sessionWorkingDir(session, { agent })
24
27
  return { agent, sid, session, cwd }
25
28
  }
26
29
 
package/src/index.js CHANGED
@@ -63,6 +63,16 @@ export const Config = Schema.object({
63
63
  insight: Schema.object({
64
64
  dedupOverlap: Schema.number().default(0.7),
65
65
  reinforceBand: Schema.number().default(0.65),
66
+ // 向量带(需 leanvec;未装自动退化成上面的纯词法带)。下面四个键自 0.6.x 起就在
67
+ // cfgInsight(src/insight-store.js)里读取、README 与 types.js 也写了,却一直没进
68
+ // Schema。与 0.5.8 给 autoContext 补声明是同一类问题:未声明键在 schemastery 层会透传,
69
+ // 但走 cordis.patch.yml 时能不能活到代码取决于宿主是否按「not a declared property」校验
70
+ // ——无论哪种,文档里的旋钮与 Schema 已经不一致,等于半真半假。默认值以 cfgInsight 的
71
+ // 兜底值为权威,这里只负责把键暴露出来。
72
+ autoMergeCos: Schema.number().default(0.9),
73
+ autoMergeMargin: Schema.number().default(0.05),
74
+ hintCos: Schema.number().default(0.7),
75
+ maxForgetMatches: Schema.number().default(10),
66
76
  maxProject: Schema.number().default(100),
67
77
  maxGlobalProcedures: Schema.number().default(200),
68
78
  promoteConfidence: Schema.number().default(0.7),