@ssamurias/dsh-fish 1.1.3 → 1.1.5

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 (71) hide show
  1. package/AGENTS.md +13 -6
  2. package/CHANGELOG.md +137 -0
  3. package/README.md +32 -101
  4. package/RELEASE-NOTES-1.1.4.md +87 -0
  5. package/RELEASE-NOTES-1.1.5.md +49 -0
  6. package/docs//350/256/244/344/272/272/345/210/244/346/215/256.md +4 -1
  7. package/lib/absence.js +1 -1
  8. package/lib/ask.js +3 -3
  9. package/lib/backup.js +44 -1
  10. package/lib/basis.js +85 -0
  11. package/lib/becoming.js +70 -0
  12. package/lib/capture.js +16 -2
  13. package/lib/claim.js +35 -0
  14. package/lib/client/01-state-load.js +21 -0
  15. package/lib/client/04-pressure-lab.js +15 -0
  16. package/lib/client/09-tab-outing.js +88 -1
  17. package/lib/client/11-tab-data-frame.js +59 -0
  18. package/lib/client/12-pressure-chip-mount.js +14 -0
  19. package/lib/client.js +197 -1
  20. package/lib/comment.js +824 -823
  21. package/lib/compactionlog.js +181 -0
  22. package/lib/decisions.js +207 -0
  23. package/lib/derive.js +384 -53
  24. package/lib/dream.js +1 -1
  25. package/lib/firstwake.js +6 -3
  26. package/lib/generator.js +4 -0
  27. package/lib/guard.js +75 -9
  28. package/lib/handoff.js +65 -8
  29. package/lib/health.js +4 -1
  30. package/lib/hear.js +15 -4
  31. package/lib/herpeople.js +29 -15
  32. package/lib/herperson.js +126 -31
  33. package/lib/identity.js +11 -3
  34. package/lib/index.js +17 -2
  35. package/lib/letter.js +1 -1
  36. package/lib/match.js +3 -1
  37. package/lib/memorymap.js +3 -1
  38. package/lib/notice.js +60 -0
  39. package/lib/observe-bili.js +1 -1
  40. package/lib/observe-data.js +16 -0
  41. package/lib/observe-home.js +1 -1
  42. package/lib/observe-http.js +20 -1
  43. package/lib/observe-state.js +22 -3
  44. package/lib/observe-trip.js +1 -1
  45. package/lib/people.js +76 -20
  46. package/lib/peopledoc.js +2 -2
  47. package/lib/person.js +13 -1
  48. package/lib/persona.js +49 -24
  49. package/lib/proactive.js +1 -1
  50. package/lib/pronounask.js +2 -2
  51. package/lib/recall.js +301 -18
  52. package/lib/say.js +76 -6
  53. package/lib/scheduler.js +144 -25
  54. package/lib/schema.js +54 -5
  55. package/lib/selfdoc.js +10 -3
  56. package/lib/session-pressure-analysis.js +24 -4
  57. package/lib/session-pressure-data.js +5 -0
  58. package/lib/session-pressure.js +32 -6
  59. package/lib/settings.js +3 -2
  60. package/lib/sleepfact.js +9 -3
  61. package/lib/speak.js +10 -5
  62. package/lib/stance.js +56 -19
  63. package/lib/store.js +53 -5
  64. package/lib/talkmem.js +151 -21
  65. package/lib/turnexport.js +34 -5
  66. package/lib/turns.js +86 -7
  67. package/lib/types.d.ts +2 -1
  68. package/lib/unclaimed.js +102 -0
  69. package/lib/whatseen.js +17 -0
  70. package/lib/wiki.js +89 -1
  71. 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
+ }
@@ -0,0 +1,207 @@
1
+ // @ts-check
2
+ /**
3
+ * decisions.js —— 判决账:这一轮为什么醒、回没回、叫没叫模型
4
+ *
5
+ * 为什么单独一份文件(2026-11-29,用户 m00311 批准):
6
+ * 她的记忆里只留下「结果」——说了什么、写了什么。可出问题时最常问的是
7
+ * **「这一轮为什么没醒 / 为什么没回」**。这句话在旧账里查不到:主动回合的
8
+ * skip 只写日志,日志轮转掉就没了;人类回合根本不记「谁把她叫醒」。
9
+ *
10
+ * 三条边界,先写在最前面:
11
+ * 1. **只记不用**。这份账不参与判醒、不参与回不回、不进她的提示词。
12
+ * 它只是给人看的行车记录仪。要拿它调参数,得先另开一轮讨论。
13
+ * 2. **不记原文**。不存他说的话、不存她说的话、不存会话 id。
14
+ * 只存时间、类型、醒/回、稳定原因码和一句人话原因。
15
+ * 3. **只追加**。一行一条,坏了就跳过那一行;不从这份账反推任何状态。
16
+ *
17
+ * 文件位置:`<root>/decisions.jsonl`。和 events.jsonl 分开,避免把
18
+ * 「系统为什么这么做」混进「她经历过什么」。
19
+ */
20
+
21
+ import { promises as fs } from 'node:fs'
22
+ import { dirname, join } from 'node:path'
23
+ import { rootDir } from './store.js'
24
+
25
+ /** 判决类型:人类开口、接着上一轮、自己醒的一轮。 */
26
+ export const DECISION_KINDS = ['human', 'continuation', 'proactive']
27
+
28
+ /** 稳定原因码 → 数据页上的人话。改文案不改码;码是聚合与排查用的。 */
29
+ export const DECISION_LABELS = {
30
+ human: '他说话了',
31
+ continuation: '接着上一轮',
32
+ off: '主动性关着',
33
+ window: '不在醒着的时段',
34
+ away: '她不在家',
35
+ rest: '今天请了假',
36
+ cap: '今天的额度用完了',
37
+ cap_silent: '醒过太多次,都没落地',
38
+ retry: '上次没送出去,还在等',
39
+ silent_wait: '上次醒来没开口,还在等',
40
+ self_said: '她自己刚开过口',
41
+ talked: '刚说过话',
42
+ no_material: '桌上没有可说的东西',
43
+ no_new: '这半天没有新东西',
44
+ busy: '她正忙',
45
+ hot: '你还在说话',
46
+ deliver_fail: '这一拍没送进去',
47
+ silent: '醒着,但选择不说',
48
+ turn: '醒着,开口了',
49
+ tool: '醒着,做了件事但没说话',
50
+ other: '别的原因',
51
+ }
52
+
53
+ /**
54
+ * 把一句中文原因压成稳定码。
55
+ *
56
+ * 为什么不直接存中文:中文文案会改,聚合会跟着散。码不动,文案随便改。
57
+ * 匹配用「包含」而不是全等:`proactiveState` 的原因常带括号补充。
58
+ *
59
+ * @param {string} why
60
+ * @returns {string}
61
+ */
62
+ export function decisionCodeOf(why) {
63
+ const w = String(why ?? '')
64
+ if (!w) return 'other'
65
+ if (w.includes('他说话了')) return 'human'
66
+ if (w.includes('接着上一轮')) return 'continuation'
67
+ if (w.includes('主动性关着') || w.includes('主动说话是关着的')) return 'off'
68
+ if (w.includes('不在她醒着的时段')) return 'window'
69
+ if (w.includes('她不在家')) return 'away'
70
+ if (w.includes('请了假')) return 'rest'
71
+ if (w.includes('醒过太多次')) return 'cap_silent'
72
+ if (w.includes('到上限') || w.includes('额度')) return 'cap'
73
+ if (w.includes('没送出去')) return 'retry'
74
+ if (w.includes('没开口')) return 'silent_wait'
75
+ if (w.includes('刚开过口')) return 'self_said'
76
+ if (w.includes('刚说过话')) return 'talked'
77
+ if (w.includes('桌上没有')) return 'no_material'
78
+ if (w.includes('没有新东西')) return 'no_new'
79
+ if (w.includes('她正忙') || w.includes('别的插件说她在忙')) return 'busy'
80
+ if (w.includes('你还在说话')) return 'hot'
81
+ if (w.includes('deliver failed') || w.includes('投递没有落成')) return 'deliver_fail'
82
+ return 'other'
83
+ }
84
+
85
+ /** 判决账文件。 */
86
+ export function decisionsFile(root = rootDir()) {
87
+ return join(root, 'decisions.jsonl')
88
+ }
89
+
90
+ /**
91
+ * 往判决账追加一行。
92
+ *
93
+ * 复制 `store.js` 的 appendLine 口径(不导出私有函数):文件末字节不是换行时
94
+ * 先补一个,再写这一行;只追加,不改已有内容。写失败不抛——判决账写不进去
95
+ * 不能连累她说话。
96
+ *
97
+ * @param {string} file
98
+ * @param {string} line
99
+ */
100
+ async function appendLine(file, line) {
101
+ await fs.mkdir(dirname(file), { recursive: true })
102
+ const fh = await fs.open(file, 'a+')
103
+ try {
104
+ const { size } = await fh.stat()
105
+ let glued = false
106
+ if (size > 0) {
107
+ const last = Buffer.alloc(1)
108
+ const { bytesRead } = await fh.read(last, 0, 1, size - 1)
109
+ glued = bytesRead === 1 && last[0] !== 0x0a
110
+ }
111
+ await fh.appendFile((glued ? '\n' : '') + line + '\n', 'utf8')
112
+ } finally {
113
+ await fh.close()
114
+ }
115
+ }
116
+
117
+ /**
118
+ * 记一笔判决。
119
+ *
120
+ * @param {string} root
121
+ * @param {{kind:string,code?:string,why?:string,woke?:boolean,replied?:boolean,model?:boolean,source?:string}} d
122
+ * @returns {Promise<boolean>} 写没写进去
123
+ */
124
+ export async function recordDecision(root, d = /** @type {any} */ ({})) {
125
+ try {
126
+ const why = String(d.why ?? '')
127
+ const code = String(d.code ?? decisionCodeOf(why))
128
+ const kind = DECISION_KINDS.includes(d.kind) ? d.kind : 'other'
129
+ const row = {
130
+ v: 1,
131
+ at: new Date().toISOString(),
132
+ kind,
133
+ code,
134
+ why: why.slice(0, 200),
135
+ woke: d.woke === true,
136
+ replied: d.replied === true,
137
+ model: d.model === true,
138
+ source: String(d.source ?? ''),
139
+ }
140
+ await appendLine(decisionsFile(root), JSON.stringify(row))
141
+ return true
142
+ } catch {
143
+ return false
144
+ }
145
+ }
146
+
147
+ /**
148
+ * 读判决账。坏行跳过,不抛。
149
+ *
150
+ * @param {string} root
151
+ * @param {{days?:number,limit?:number}} [o]
152
+ * @returns {Promise<{decisions:object[],total:number,days:number}>}
153
+ */
154
+ export async function readDecisions(root, o = {}) {
155
+ const days = Math.min(90, Math.max(1, Number(o.days) || 7))
156
+ const limit = Math.min(500, Math.max(1, Number(o.limit) || 500))
157
+ const cutoff = Date.now() - days * 24 * 3600_000
158
+ let raw = ''
159
+ try {
160
+ raw = await fs.readFile(decisionsFile(root), 'utf8')
161
+ } catch {
162
+ return { decisions: [], total: 0, days }
163
+ }
164
+ const all = []
165
+ for (const line of raw.split(/\r?\n/)) {
166
+ const s = line.trim()
167
+ if (!s) continue
168
+ try {
169
+ const d = JSON.parse(s)
170
+ if (!d || typeof d !== 'object') continue
171
+ const t = Date.parse(String(d.at ?? ''))
172
+ if (!Number.isFinite(t) || t < cutoff) continue
173
+ all.push(d)
174
+ } catch {
175
+ // 半行/坏行:跳过。判决账是旁证,不值得为一行坏数据把整页弄挂。
176
+ }
177
+ }
178
+ // 时间倒序,最近的在最前;limit 作用在倒序之后。
179
+ all.sort((a, b) => (Date.parse(String(b.at)) || 0) - (Date.parse(String(a.at)) || 0))
180
+ return { decisions: all.slice(0, limit), total: all.length, days }
181
+ }
182
+
183
+ /**
184
+ * 按原因码聚合。只算数量、醒/回次数,不算分数、不排序成「谁更重要」。
185
+ *
186
+ * @param {object[]} decisions
187
+ * @returns {{code:string,label:string,count:number,woke:number,replied:number}[]}
188
+ */
189
+ export function summarizeDecisions(decisions = []) {
190
+ /** @type {Map<string,{code:string,label:string,count:number,woke:number,replied:number}>} */
191
+ const m = new Map()
192
+ for (const d of decisions) {
193
+ const code = String(d?.code ?? 'other')
194
+ const cur = m.get(code) ?? {
195
+ code,
196
+ label: DECISION_LABELS[code] ?? code,
197
+ count: 0,
198
+ woke: 0,
199
+ replied: 0,
200
+ }
201
+ cur.count += 1
202
+ if (d?.woke === true) cur.woke += 1
203
+ if (d?.replied === true) cur.replied += 1
204
+ m.set(code, cur)
205
+ }
206
+ return [...m.values()].sort((a, b) => b.count - a.count || a.code.localeCompare(b.code))
207
+ }