@ssamurias/dsh-fish 1.1.3 → 1.1.4

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.
Files changed (65) hide show
  1. package/AGENTS.md +12 -6
  2. package/CHANGELOG.md +98 -0
  3. package/README.md +28 -3
  4. package/RELEASE-NOTES-1.1.4.md +87 -0
  5. package/docs//350/256/244/344/272/272/345/210/244/346/215/256.md +4 -1
  6. package/lib/absence.js +1 -1
  7. package/lib/ask.js +3 -3
  8. package/lib/backup.js +44 -1
  9. package/lib/basis.js +85 -0
  10. package/lib/capture.js +16 -2
  11. package/lib/claim.js +35 -0
  12. package/lib/client/01-state-load.js +15 -0
  13. package/lib/client/04-pressure-lab.js +15 -0
  14. package/lib/client/09-tab-outing.js +88 -1
  15. package/lib/client/11-tab-data-frame.js +2 -0
  16. package/lib/client/12-pressure-chip-mount.js +14 -0
  17. package/lib/client.js +134 -1
  18. package/lib/comment.js +824 -823
  19. package/lib/compactionlog.js +181 -0
  20. package/lib/derive.js +287 -47
  21. package/lib/dream.js +1 -1
  22. package/lib/firstwake.js +6 -3
  23. package/lib/generator.js +4 -0
  24. package/lib/handoff.js +65 -8
  25. package/lib/health.js +4 -1
  26. package/lib/hear.js +15 -4
  27. package/lib/herpeople.js +28 -14
  28. package/lib/herperson.js +126 -31
  29. package/lib/identity.js +11 -3
  30. package/lib/index.js +17 -2
  31. package/lib/letter.js +1 -1
  32. package/lib/match.js +3 -1
  33. package/lib/memorymap.js +3 -1
  34. package/lib/notice.js +60 -0
  35. package/lib/observe-bili.js +1 -1
  36. package/lib/observe-home.js +1 -1
  37. package/lib/observe-http.js +20 -1
  38. package/lib/observe-state.js +22 -3
  39. package/lib/observe-trip.js +1 -1
  40. package/lib/people.js +60 -13
  41. package/lib/peopledoc.js +1 -1
  42. package/lib/person.js +13 -1
  43. package/lib/persona.js +34 -24
  44. package/lib/proactive.js +1 -1
  45. package/lib/pronounask.js +2 -2
  46. package/lib/recall.js +267 -17
  47. package/lib/say.js +35 -3
  48. package/lib/scheduler.js +107 -22
  49. package/lib/schema.js +54 -5
  50. package/lib/selfdoc.js +10 -3
  51. package/lib/session-pressure-analysis.js +24 -4
  52. package/lib/session-pressure-data.js +5 -0
  53. package/lib/session-pressure.js +32 -6
  54. package/lib/settings.js +3 -2
  55. package/lib/sleepfact.js +9 -3
  56. package/lib/stance.js +56 -19
  57. package/lib/store.js +53 -5
  58. package/lib/talkmem.js +151 -21
  59. package/lib/turnexport.js +34 -5
  60. package/lib/turns.js +46 -6
  61. package/lib/types.d.ts +2 -1
  62. package/lib/unclaimed.js +102 -0
  63. package/lib/whatseen.js +17 -0
  64. package/lib/wiki.js +86 -1
  65. package/package.json +1 -1
@@ -0,0 +1,181 @@
1
+ // @ts-check
2
+ /**
3
+ * compactionlog.js —— 她这条会话被宿主压缩时,往事件流留一条能查的账
4
+ *
5
+ * 为什么要有(1.1.4,外部报告 B1 轻量版):
6
+ * 宿主的自动压缩与 `/compact` 默认就在链上(`@deepseek-ai/dsh-base` 挂了
7
+ * `dsh-compaction-basic` 与 `dsh-command-compact`),可是**压过没有**这件事
8
+ * 在插件这边完全看不出来——事件流里一条记录都没有,只能去解宿主的会话文件。
9
+ * 于是「她要紧的记忆是不是被折过」这个问题,谁也答不上来。
10
+ *
11
+ * 为什么只记一条账、不贴补丁:
12
+ * 《清单》原文要的是「压缩前自动抽增量补丁、压缩后贴回提示词」。那等于让
13
+ * 系统替她总结一段话塞进她眼前,与「她读到的判断只能是她自己的原话或凭据」
14
+ * 这条底线冲突。这里只写 `meta`——系统账,不进判断、不进身份段,
15
+ * `her_recall` 也不放行(`lib/recall.js` 的 `SKIP_TYPES` 只认几种白名单 meta)。
16
+ *
17
+ * 数字从哪来:`compaction/summary` 的载荷里有 `shadowedSeqs`(被遮蔽的条数)
18
+ * 与 `shadowedTokenCount`(估算释放的 token)。`compaction/end` 自己没有数量,
19
+ * 所以先在 summary 上记下,等 end 落定(没带 error)再写一条。
20
+ *
21
+ * 幂等:同一次压缩按 `compactionId` 只写一条。本进程用 Set 挡重复投递,
22
+ * 跨重启靠事件流里已有的 `compaction:<id>` 前缀。
23
+ */
24
+
25
+ import { join } from 'node:path'
26
+ import { appendEvent, readEvents, readJson, rootDir } from './store.js'
27
+ import { envelopeOf } from './derive.js'
28
+ import { loadConfig } from './settings.js'
29
+ import { readChannels } from './channels.js'
30
+
31
+ /** 这条账的文本前缀。`her_recall` 不放行它,只作审计用。 */
32
+ export const COMPACTION_MARK = 'compaction:'
33
+
34
+ /** 未落定的压缩最多记几个:压缩很稀,这个上限只防异常重放撑爆内存。 */
35
+ const PENDING_MAX = 64
36
+
37
+ /**
38
+ * 这条会话是不是她的(自己那间房,或 channels.json 里登记过的群房)。
39
+ * 别人在这台机器上跟别的 agent 说话时也会走到这个监听,不该记进她的账。
40
+ *
41
+ * @param {string} root
42
+ * @param {string} sessionId
43
+ */
44
+ async function isHerRoom(root, sessionId) {
45
+ const sid = String(sessionId ?? '').trim()
46
+ if (!sid) return false
47
+ try {
48
+ const saved = await readJson(join(root, 'home.json'), null)
49
+ if (String(saved?.sessionId ?? '') === sid) return true
50
+ } catch {
51
+ /* 读不到就继续看群房 */
52
+ }
53
+ const rooms = await readChannels(root).catch(() => [])
54
+ return rooms.some((room) => String(room?.sessionId ?? '') === sid)
55
+ }
56
+
57
+ /**
58
+ * 一条压缩记录写成一句人话。她读不到,但翻日志的人要看得懂。
59
+ *
60
+ * @param {string} marker
61
+ * @param {{count?: number, tokens?: number, provider?: string, model?: string}} [info]
62
+ */
63
+ export function compactionLine(marker, info = {}) {
64
+ const parts = []
65
+ const count = Number(info.count) || 0
66
+ const tokens = Number(info.tokens) || 0
67
+ if (count > 0) parts.push(`收起 ${count} 条历史`)
68
+ if (tokens > 0) parts.push(`估算释放 ${tokens} token`)
69
+ const route = [info.provider, info.model].filter(Boolean).join('/')
70
+ const detail = parts.length ? parts.join('、') : '这一次没留摘要记录(可能是裁剪那一档)'
71
+ return `${String(marker).trimEnd()} 宿主压缩了一次:${detail}${route ? `(${route})` : ''}`
72
+ }
73
+
74
+ /**
75
+ * 挂上压缩记账。只监听、只写 `meta`,不改任何派生。
76
+ *
77
+ * @param {any} ctx
78
+ * @param {string} [root]
79
+ */
80
+ export function registerCompactionLog(ctx, root = rootDir()) {
81
+ /** @type {Map<string, {count: number, tokens: number, provider: string, model: string}>} */
82
+ const pending = new Map()
83
+ /** 本进程已经处理过的:重试投递不再读盘。 */
84
+ const written = new Set()
85
+ /**
86
+ * 正在写的那几个。`compaction/end` 被重投时两条 `settle` 会同时跑,
87
+ * 只靠 `written` 挡不住——它要等读完盘才填。所以在第一个 await 之前就占位。
88
+ */
89
+ const busy = new Set()
90
+ let disposed = false
91
+
92
+ /** @param {any} event */
93
+ function remember(event) {
94
+ const compactionId = String(event?.data?.compactionId ?? '').trim()
95
+ if (!compactionId) return
96
+ const seqs = Array.isArray(event?.data?.shadowedSeqs) ? event.data.shadowedSeqs.length : 0
97
+ const tokens = Number(event?.data?.shadowedTokenCount)
98
+ pending.set(compactionId, {
99
+ count: seqs,
100
+ tokens: Number.isFinite(tokens) && tokens > 0 ? Math.round(tokens) : 0,
101
+ provider: String(event?.data?.provider ?? ''),
102
+ model: String(event?.data?.model ?? ''),
103
+ })
104
+ if (pending.size > PENDING_MAX) pending.delete(/** @type {string} */ (pending.keys().next().value))
105
+ }
106
+
107
+ /**
108
+ * 把一条压缩落成账。调用方已经同步占好了 `busy`,这里只管写。
109
+ *
110
+ * @param {any} session
111
+ * @param {string} compactionId
112
+ * @param {{count: number, tokens: number, provider: string, model: string}} info
113
+ */
114
+ async function writeOne(session, compactionId, info) {
115
+ const sessionId = String(session?.header?.id ?? '').trim()
116
+ if (!(await isHerRoom(root, sessionId))) {
117
+ written.add(compactionId)
118
+ return
119
+ }
120
+ const { events } = await readEvents(root)
121
+ const marker = `${COMPACTION_MARK}${compactionId}`
122
+ if (events.some((ev) => ev?.type === 'meta' && String(ev?.text ?? '').startsWith(marker))) {
123
+ written.add(compactionId)
124
+ return
125
+ }
126
+ const { config } = await loadConfig(root)
127
+ const stamp = envelopeOf(events, { growthDays: config?.growthDays })
128
+ await appendEvent(
129
+ { type: 'meta', text: compactionLine(marker, info), ...(sessionId ? { sessionId } : {}) },
130
+ { ...stamp, source: 'system' },
131
+ root,
132
+ )
133
+ written.add(compactionId)
134
+ }
135
+
136
+ /**
137
+ * `compaction/end` 到了:先同步占位,再异步写。
138
+ * 占位必须在第一个 `await` 之前——重投的两条事件是同一个 tick 里分发下来的,
139
+ * 等到读完盘再判断就已经写了两条。
140
+ *
141
+ * @param {any} session
142
+ * @param {any} event
143
+ */
144
+ function settle(session, event) {
145
+ const compactionId = String(event?.data?.compactionId ?? '').trim()
146
+ if (!compactionId) return
147
+ const info = pending.get(compactionId) ?? { count: 0, tokens: 0, provider: '', model: '' }
148
+ pending.delete(compactionId)
149
+ // 失败的那次不记账:`compaction/end` 带 error 说明这一压没落成。
150
+ if (event?.data?.error) return
151
+ if (written.has(compactionId) || busy.has(compactionId)) return
152
+ busy.add(compactionId)
153
+ void writeOne(session, compactionId, info)
154
+ .catch(() => { /* 写不进去只影响这一条账,下一拍重投还有机会 */ })
155
+ .finally(() => busy.delete(compactionId))
156
+ }
157
+
158
+ const off = ctx?.on?.('session/event', (/** @type {any} */ session, /** @type {any} */ event) => {
159
+ if (disposed) return
160
+ const type = String(event?.type ?? '')
161
+ if (type === 'compaction/summary') {
162
+ try { remember(event) } catch { /* 记不下就不记,压缩本身不等我们 */ }
163
+ return
164
+ }
165
+ if (type === 'compaction/end') {
166
+ try {
167
+ settle(session, event)
168
+ } catch {
169
+ /* 写不进去只影响这一条账 */
170
+ }
171
+ }
172
+ })
173
+
174
+ return {
175
+ dispose: () => {
176
+ disposed = true
177
+ pending.clear()
178
+ off?.()
179
+ },
180
+ }
181
+ }