dsh-metabolism 0.2.2 → 0.3.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.
@@ -0,0 +1,302 @@
1
+ /**
2
+ * dsh-metabolism 骨架:把每次压缩的那一段写成一张「说明卡」,存成人可读的 .md,
3
+ * 同时记进一份总目录 index.json。压缩本身仍然只往上下文里放一行指针,
4
+ * 这张卡是给以后翻查和逐级读取用的,不进上下文。
5
+ *
6
+ * 目录结构(跟茅房并排):
7
+ * 记忆\骨架\index.json 总目录,一行一段
8
+ * 记忆\骨架\L1\<id>.md 每一段一张卡
9
+ *
10
+ * 一条卡里必须有:来源文件、seq 区间、在干什么、仍然有效的要求、已经做到、下一步。
11
+ * 没有 seq 区间的卡不许进目录——不然以后捞到了也没法核对原文。
12
+ *
13
+ * @module dsh-metabolism/骨架
14
+ */
15
+ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
16
+ import { dirname, join } from 'node:path'
17
+ import { guardCard } from './守卫.js'
18
+
19
+ const FRONT = '---'
20
+ const VERSION = 1
21
+
22
+ /** 茅房在 记忆\茅房,骨架放它旁边。 */
23
+ export function skeletonRoot(archiveRoot) {
24
+ return join(dirname(String(archiveRoot)), '骨架')
25
+ }
26
+
27
+ export function readIndex(root) {
28
+ try {
29
+ const parsed = JSON.parse(readFileSync(join(root, 'index.json'), 'utf8'))
30
+ if (parsed !== null && typeof parsed === 'object' && Array.isArray(parsed.entries)) {
31
+ return { version: VERSION, updatedAt: parsed.updatedAt ?? null, entries: parsed.entries }
32
+ }
33
+ } catch {
34
+ // 没有或者坏了,就当空目录重来,不抛
35
+ }
36
+ return { version: VERSION, updatedAt: null, entries: [] }
37
+ }
38
+
39
+ /** 先写临时文件再改名,半截文件不会留在盘上。 */
40
+ function atomicWrite(file, text) {
41
+ const tmp = `${file}.tmp-${process.pid}-${Date.now()}`
42
+ writeFileSync(tmp, text, 'utf8')
43
+ renameSync(tmp, file)
44
+ }
45
+
46
+ function pad(value) {
47
+ return String(value).padStart(2, '0')
48
+ }
49
+
50
+ function stampOf(ms) {
51
+ const d = new Date(ms)
52
+ return {
53
+ iso: `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}+08:00`,
54
+ day: `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`,
55
+ clock: `${pad(d.getHours())}${pad(d.getMinutes())}`,
56
+ }
57
+ }
58
+
59
+ function listOf(value) {
60
+ if (Array.isArray(value)) return value.map((item) => String(item).trim()).filter((item) => item.length > 0)
61
+ if (typeof value !== 'string') return []
62
+ return value
63
+ .split('\n')
64
+ .map((line) => line.replace(/^[-*·\d.]+\s*/, '').trim())
65
+ .filter((line) => line.length > 0)
66
+ }
67
+
68
+ function section(title, lines) {
69
+ const clean = lines.map((line) => String(line ?? '').trim()).filter((line) => line.length > 0)
70
+ const body = clean.length === 0 ? '(这段里没写)' : clean.map((line) => `- ${line}`).join('\n')
71
+ return `**${title}**\n${body}`
72
+ }
73
+
74
+ /**
75
+ * 写一张 L1 卡并更新目录。
76
+ * 失败不抛,返回 { ok:false, error },压缩本身照旧完成。
77
+ */
78
+ export function writeL1(options) {
79
+ try {
80
+ const root = skeletonRoot(options.archiveRoot)
81
+ const l1 = join(root, 'L1')
82
+ mkdirSync(l1, { recursive: true })
83
+ const from = stampOf(options.fromMs)
84
+ const to = stampOf(options.toMs)
85
+ // 编号要能区分段:压缩编号形如 metabolism-mwnfsy9y-3234,取最后一段(3234)当尾码;
86
+ // 老写法取前四个字母会一律变成 meta,同一分钟里几张卡会互相覆盖(2026-11-19 踩过)。
87
+ const rawId = String(options.shortId ?? '').trim()
88
+ const tail = rawId.split(/[^0-9a-zA-Z]+/).filter((part) => part.length > 0).pop() ?? ''
89
+ const short = (tail.length >= 2 ? tail : rawId.replace(/[^0-9a-zA-Z]/g, '')).slice(-6) || 'seg'
90
+ const base = `L1-${from.day.replace(/-/g, '')}-${from.clock}-${short}`
91
+ // 同一分钟同一段重复跑也不覆盖:加 -2、-3
92
+ let id = base
93
+ for (let n = 2; existsSync(join(l1, `${id}.md`)) && n <= 9; n += 1) id = `${base}-${n}`
94
+ const file = join(l1, `${id}.md`)
95
+ const keywords = listOf(options.keywords).slice(0, 8)
96
+ const card = {
97
+ doing: String(options.doing ?? '').trim(),
98
+ requirements: listOf(options.requirements),
99
+ done: listOf(options.done),
100
+ next: String(options.next ?? '').trim(),
101
+ quotes: listOf(options.quotes),
102
+ }
103
+
104
+ // 守卫(设计稿 v3.1 第四节):模型写的四要素落盘前过闸。
105
+ // 没有原文样本(历史回填)就跳过——没对照物,判不了,不硬判。
106
+ const guarded = guardCard(card, options.rawSample, options.guard)
107
+ if (guarded.dropped) {
108
+ return { ok: false, skipped: '高信号缺失过多,退回只写指针', notes: guarded.notes, missing: guarded.missing }
109
+ }
110
+ const safe = guarded.card ?? card
111
+ card.doing = String(safe.doing ?? '').trim()
112
+ card.requirements = listOf(safe.requirements)
113
+ card.done = listOf(safe.done)
114
+ card.next = String(safe.next ?? '').trim()
115
+ card.quotes = listOf(safe.quotes)
116
+
117
+ const lines = [
118
+ FRONT,
119
+ `id: ${id}`,
120
+ `from: ${from.iso}`,
121
+ `to: ${to.iso}`,
122
+ `title: ${String(options.title ?? '').replace(/\n/g, ' ').trim()}`,
123
+ `keywords: [${keywords.join(', ')}]`,
124
+ `ref: ${options.archivePath}#seq ${options.seqFrom}-${options.seqTo}`,
125
+ `seqFrom: ${options.seqFrom}`,
126
+ `seqTo: ${options.seqTo}`,
127
+ `tokens: ${Number(options.tokens) || 0}`,
128
+ `sources: ${JSON.stringify([options.archivePath])}`,
129
+ `compactionId: ${options.compactionId ?? ''}`,
130
+ `useCount: 0`,
131
+ `lastUsedAt: null`,
132
+ FRONT,
133
+ '',
134
+ section('在干什么', [card.doing]),
135
+ '',
136
+ section('仍然有效的要求', card.requirements),
137
+ '',
138
+ section('已经做到', card.done),
139
+ '',
140
+ section('下一步', [card.next]),
141
+ '',
142
+ '**原话**',
143
+ card.quotes.length === 0 ? '(没有摘原话)' : card.quotes.map((q) => `- ${q}`).join('\n'),
144
+ '',
145
+ `原文:${options.archivePath}(seq ${options.seqFrom}–${options.seqTo},${Number(options.tokens) || 0} tokens)`,
146
+ '',
147
+ ]
148
+
149
+ atomicWrite(file, lines.join('\n'))
150
+
151
+ const index = readIndex(root)
152
+ index.entries = index.entries.filter((entry) => entry?.id !== id)
153
+ index.entries.push({
154
+ id,
155
+ layer: 1,
156
+ from: from.iso,
157
+ to: to.iso,
158
+ day: from.day,
159
+ title: String(options.title ?? '').trim(),
160
+ keywords,
161
+ ref: options.archivePath,
162
+ compactionId: String(options.compactionId ?? ''),
163
+ seqFrom: options.seqFrom,
164
+ seqTo: options.seqTo,
165
+ tokens: Number(options.tokens) || 0,
166
+ cold: false,
167
+ useCount: 0,
168
+ lastUsedAt: null,
169
+ })
170
+ index.entries.sort((a, b) => String(b.from).localeCompare(String(a.from)))
171
+ index.updatedAt = new Date().toISOString()
172
+ atomicWrite(join(root, 'index.json'), `${JSON.stringify(index, null, 2)}\n`)
173
+
174
+ return { ok: true, id, file, total: index.entries.length, notes: guarded.notes }
175
+ } catch (error) {
176
+ return { ok: false, error: error?.message ?? String(error) }
177
+ }
178
+ }
179
+
180
+ /** 从一张卡里抠出「下一步」那一行(读不到就空)。 */
181
+ function nextLineOf(file) {
182
+ try {
183
+ const text = readFileSync(file, 'utf8')
184
+ const at = text.indexOf('**下一步**')
185
+ if (at === -1) return ''
186
+ const rest = text.slice(at + '**下一步**'.length)
187
+ const end = rest.indexOf('\n**')
188
+ const block = (end === -1 ? rest : rest.slice(0, end))
189
+ .split('\n')
190
+ .map((line) => line.replace(/^[-*·\d.、]+\s*/, '').trim())
191
+ .filter((line) => line.length > 0 && line !== '(这段里没写)')
192
+ return block[0] ?? ''
193
+ } catch {
194
+ return ''
195
+ }
196
+ }
197
+
198
+ /**
199
+ * 降级:日摘要(L2)、七天标冷、三十天折成一行(L3)。
200
+ * 纯规则,不调模型;dryRun 默认 true,只看不写。
201
+ */
202
+ export function rollup(options) {
203
+ const DAY = 86400000
204
+ const dryRun = options?.dryRun !== false
205
+ const coldAfterDays = Number(options?.coldAfterDays) > 0 ? Number(options.coldAfterDays) : 7
206
+ const foldAfterDays = Number(options?.foldAfterDays) > 0 ? Number(options.foldAfterDays) : 30
207
+ try {
208
+ const root = skeletonRoot(options.archiveRoot)
209
+ const index = readIndex(root)
210
+ const now = Date.now()
211
+ const coldFloor = new Date(now - coldAfterDays * DAY).toISOString().slice(0, 10)
212
+ const foldFloor = new Date(now - foldAfterDays * DAY).toISOString().slice(0, 10)
213
+ const l1 = index.entries.filter((entry) => entry.layer === 1)
214
+
215
+ const byDay = new Map()
216
+ for (const entry of l1) {
217
+ const day = String(entry.day ?? '')
218
+ if (day === '') continue
219
+ if (!byDay.has(day)) byDay.set(day, [])
220
+ byDay.get(day).push(entry)
221
+ }
222
+
223
+ const report = []
224
+ let l2Written = 0
225
+ let coldMarked = 0
226
+ let l3Made = 0
227
+ const l2Dir = join(root, 'L2')
228
+ if (!dryRun) mkdirSync(l2Dir, { recursive: true })
229
+
230
+ for (const [day, list] of [...byDay.entries()].sort()) {
231
+ const rows = list
232
+ .slice()
233
+ .sort((a, b) => String(a.from).localeCompare(String(b.from)))
234
+ .map((entry) => ({
235
+ time: String(entry.from ?? '').slice(11, 16),
236
+ title: String(entry.title ?? '(无标题)'),
237
+ next: dryRun ? '' : nextLineOf(join(root, 'L1', `${entry.id}.md`)),
238
+ }))
239
+ const lines = [`# ${day}`, '', `共 ${rows.length} 段,从 ${rows[0]?.time ?? '?'} 到 ${rows[rows.length - 1]?.time ?? '?'}`, '']
240
+ for (const row of rows) {
241
+ lines.push(`- ${row.time} ${row.title}${row.next.length > 0 ? ` | 下一步:${row.next}` : ''}`)
242
+ }
243
+ lines.push('')
244
+ if (!dryRun) atomicWrite(join(l2Dir, `${day}.md`), lines.join('\n'))
245
+
246
+ const l2Id = `L2-${day.replace(/-/g, '')}`
247
+ index.entries = index.entries.filter((entry) => entry.id !== l2Id)
248
+ index.entries.push({
249
+ id: l2Id,
250
+ layer: 2,
251
+ day,
252
+ from: `${day}T00:00:00+08:00`,
253
+ to: `${day}T23:59:59+08:00`,
254
+ title: `${day}:${rows.length} 段`,
255
+ keywords: [],
256
+ ref: join(l2Dir, `${day}.md`),
257
+ segments: rows.length,
258
+ cold: day < coldFloor,
259
+ useCount: 0,
260
+ lastUsedAt: null,
261
+ })
262
+ l2Written += 1
263
+ report.push(`${day}:${rows.length} 段 → 日摘要`)
264
+
265
+ if (day < coldFloor) {
266
+ for (const entry of list) {
267
+ if (!entry.cold) { entry.cold = true; coldMarked += 1 }
268
+ }
269
+ }
270
+
271
+ if (day < foldFloor) {
272
+ const l3Id = `L3-${day.replace(/-/g, '')}`
273
+ index.entries = index.entries.filter((entry) => entry.id !== l3Id)
274
+ index.entries.push({
275
+ id: l3Id,
276
+ layer: 3,
277
+ day,
278
+ from: `${day}T00:00:00+08:00`,
279
+ to: `${day}T23:59:59+08:00`,
280
+ title: `${day}:${rows.length} 段(${rows[0]?.title ?? ''} 等)`,
281
+ keywords: [],
282
+ ref: join(l2Dir, `${day}.md`),
283
+ cold: true,
284
+ useCount: 0,
285
+ lastUsedAt: null,
286
+ })
287
+ l3Made += 1
288
+ report.push(`${day}:折成一行指针`)
289
+ }
290
+ }
291
+
292
+ if (!dryRun) {
293
+ index.entries.sort((a, b) => String(b.from).localeCompare(String(a.from)))
294
+ index.updatedAt = new Date().toISOString()
295
+ atomicWrite(join(root, 'index.json'), `${JSON.stringify(index, null, 2)}\n`)
296
+ }
297
+
298
+ return { ok: true, dryRun, l2Written, coldMarked, l3Made, report }
299
+ } catch (error) {
300
+ return { ok: false, error: error?.message ?? String(error) }
301
+ }
302
+ }