@ssamurias/dsh-fish 1.0.3 → 1.0.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.
- package/AGENTS.md +5 -3
- package/CHANGELOG.md +55 -0
- package/README.md +92 -8
- package/RELEASE-NOTES-1.0.4.md +106 -0
- package/RELEASE-NOTES-1.0.5.md +79 -0
- package/docs/composite-pressure-v0.1.md +23 -0
- package/docs/composite-pressure-v0.2.md +18 -0
- package/docs/pressure-backtest-v0.1.md +19 -0
- package/docs/session-capacity-advice-v0.1.md +17 -0
- package/docs/session-pressure-design-v1.1.md +1173 -0
- package/docs/session-pressure-ui-v0.3.md +28 -0
- package/docs/tool-retention-diagnostics-v2.md +13 -0
- package/lib/appoint.js +154 -25
- package/lib/ask.js +70 -7
- package/lib/autobackup.js +2 -1
- package/lib/backup.js +58 -6
- package/lib/browse.js +191 -19
- package/lib/capture.js +48 -10
- package/lib/client.js +2203 -37
- package/lib/cloudbackup.js +24 -1
- package/lib/clouddrive.js +4 -1
- package/lib/compact.js +28 -11
- package/lib/configstamp.js +25 -0
- package/lib/cue.js +114 -0
- package/lib/derive.js +66 -5
- package/lib/doing.js +11 -2
- package/lib/dreamtable.js +5 -4
- package/lib/drive.js +66 -11
- package/lib/generator.js +1 -1
- package/lib/handoff.js +34 -16
- package/lib/home.js +3 -1
- package/lib/identity.js +23 -4
- package/lib/index.js +6 -1
- package/lib/interest.js +38 -0
- package/lib/look.js +16 -2
- package/lib/note.js +182 -41
- package/lib/observe.js +198 -4
- package/lib/persona.js +37 -15
- package/lib/proactive.js +6 -1
- package/lib/refuse.js +42 -0
- package/lib/restrict.js +8 -2
- package/lib/sametopic.js +94 -0
- package/lib/say.js +36 -7
- package/lib/scheduler.js +445 -24
- package/lib/selfcheck.js +9 -9
- package/lib/selfpronoun.js +7 -2
- package/lib/session-pressure-analysis.js +296 -0
- package/lib/session-pressure-data.js +239 -0
- package/lib/session-pressure-formulas.js +280 -0
- package/lib/session-pressure-load.js +110 -0
- package/lib/session-pressure.js +714 -0
- package/lib/settings.js +4 -1
- package/lib/sleepfact.js +110 -0
- package/lib/stance.js +212 -16
- package/lib/store.js +4 -0
- package/lib/sub.js +9 -7
- package/lib/turns.js +8 -1
- package/lib/use.js +2 -0
- package/lib/video.js +3 -0
- package/lib/watch.js +149 -3
- package/lib/whatseen.js +24 -2
- package/lib/wiki.js +1753 -0
- package/lib/wikiserve.js +148 -0
- package/media/feedback-qr.jpg +0 -0
- package/media/principle-drive.svg +28 -24
- package/media/principle-memory.svg +1 -1
- package/package.json +1 -1
- package/scripts/sync-pressure-formulas.mjs +17 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# 会话压力界面 v0.3
|
|
2
|
+
|
|
3
|
+
打开「记忆 → 会话压力」,默认显示「当前」。四页共享当前会话、历史选择和公式参数。
|
|
4
|
+
|
|
5
|
+
「当前」页顶部提供[实验性容量建议](session-capacity-advice-v0.1.md),以及可展开的占用、趋势和诊断依据。
|
|
6
|
+
|
|
7
|
+
| 原位置或展示 | v0.3 位置 |
|
|
8
|
+
| --- | --- |
|
|
9
|
+
| 首页旧单一负担、历史峰值、100 分警戒线、换房观察 | 退出评分界面。当前页显示 A/B/C 三个独立评分 |
|
|
10
|
+
| 当前概览 | 当前 · 三分、近 20 轮走势、三个子压力、关键诊断、折叠的近期事件 |
|
|
11
|
+
| 工作区会话、历史走势与同轮统计 | 历史 · Session 对比、同轮次比较 |
|
|
12
|
+
| 计算页的 MAE、偏差、预测图与明细 | 历史 · 预测回测 |
|
|
13
|
+
| 上下文、Token、工具残留、轮次采样 | 指标 · 七项原始诊断、上下文与采样、工具结果分析、逐轮记录 |
|
|
14
|
+
| 公式、参数及中间值 | 计算 · 三个子压力和三个独立综合公式,可同时恢复实验默认值 |
|
|
15
|
+
|
|
16
|
+
## 变化归因
|
|
17
|
+
|
|
18
|
+
在「历史 → 变化归因」选择会话、起止轮和 A/B/C。三项子压力与综合分使用当前实验参数。A 直接把各子项分差乘归一化权重。B/C 对三个子压力的六种替换顺序取平均边际贡献,即 Shapley 分解。贡献以完整精度求和,与综合分变化一致,显示值分别保留一位小数。数学贡献描述评分公式中的变化。
|
|
19
|
+
|
|
20
|
+
从回测明细的一条目标预测进入归因,会带入会话、起点、目标轮、公式和预测方法。误差贡献以目标轮实际子压力为起点、该次预测子压力为终点。预测仍只读起点及以前的数据。两组贡献分别描述区间变化与预测误差。
|
|
21
|
+
|
|
22
|
+
## 数据与边界
|
|
23
|
+
|
|
24
|
+
工具结果残留使用 [同请求、同估算口径](tool-retention-diagnostics-v2.md)。模型实报输入单独展示。
|
|
25
|
+
|
|
26
|
+
逐轮历史继续读取原日志前缀与既有轮末采样。只有归因请求需要传送七项端点诊断的统计字段,响应包含单位、来源、范围以及已记录的事件元数据。压缩 Token 变化依赖成对采样。缺失轮次保留断点,端点诊断缺失时会提示。工具和轮次列表使用现有有界样本,逐轮用量补充最近 200 个已分析记录。既有原日志、缓存和旧版后端统计接口继续保留。
|
|
27
|
+
|
|
28
|
+
安装后可在现有历史 Session 上用同一套参数复核端点、事件与数学贡献。
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# 工具结果残留口径
|
|
2
|
+
|
|
3
|
+
工具结果残留和请求总输入都从同一个 DSH 请求边界估算。匹配同轮同请求的首个流式回包事件。历史日志只保存最终回复时,使用写入回复之前的可见上下文。该重建基于宿主日志,提供商侧重写和精确分项 Token 尚不可观察。
|
|
4
|
+
|
|
5
|
+
按 surface 操作维护可见消息。替换、压缩和裁剪移除被替换范围,只有进入消息面的结果参与统计。同轮同一次工具调用的重复结果取最后可见记录。工具结果、其他消息和已记录的请求封装共用 DSH 的内容块和消息结构估算。模型实报输入独立展示。
|
|
6
|
+
|
|
7
|
+
跨轮重复携带逐次请求用同一对估算分子、分母配对。超出 100% 或出现内部不一致的记录标记异常,依赖该数据的 T + E、A/B/C、历史趋势与回测会跳过相应无效读数。日志无法重建时显示缺失。原始 U、A、K、E 继续使用既有统计。
|
|
8
|
+
|
|
9
|
+
缺少可见请求消息或入面记录不完整时,T 未知,模型实报用量继续保留。可重建的请求明确没有工具结果时,残留为零。
|
|
10
|
+
|
|
11
|
+
当前口径标识为 `diagnostics-v3`,分析检查点为 v10。旧检查点按原始日志重建,新轮末快照另写入 `pressure-v0.3`。已有 `diagnostics-v2` 快照原样保留。历史比较、回测及归因使用新口径,公式版本和参数保持原值。
|
|
12
|
+
|
|
13
|
+
`logs/session-pressure/samples.json` 保存现场轮末采样和压缩前后配对读数,路径沿用。手动及自动备份纳入该文件。恢复时合并新旧观测,共用事件键保留本机读数,恢复前原件仍在隔离目录。
|
package/lib/appoint.js
CHANGED
|
@@ -9,6 +9,17 @@
|
|
|
9
9
|
* 2. 到点只提一次(`notifiedAt`),不天天催。
|
|
10
10
|
* 3. 他没来,也不替她圆——prompt 里明说「没来也别替他找理由」。
|
|
11
11
|
* 4. 做没做到由她自己收:做到(done)、取消(cancel,要说为什么)、改期(move)。
|
|
12
|
+
*
|
|
13
|
+
* 2026-11-18 补「定到几点」(issue #149 / 用户 m01744、m01769、m01793):
|
|
14
|
+
* - 时刻**不是闹钟**,是「最早什么时候能提」——到那一刻她一定先看到这条,
|
|
15
|
+
* 做不做、怎么做由她自己定;催不催、说什么都不由这套代码替她决定。
|
|
16
|
+
* - 带时刻的约定不受作息窗口限制(约定优先);两条不同时刻各叫各的,不合并。
|
|
17
|
+
* - 到点那一刻正忙 / 刚叫过,宽限 5 分钟;超出宽限还忙就照叫——约定比闲聊优先。
|
|
18
|
+
* - 走到点这条链时 DSH 没开着,就晚一点叫,prompt 里写清「本来约的是 HH:MM,
|
|
19
|
+
* 那会儿我这边没开着」;不把「他不在」写进事件流(他可能就守在窗口旁)。
|
|
20
|
+
* - 双方都认下才作数:她可以用 ask_owner(kind='appoint') 问一句,答案在
|
|
21
|
+
* `answer` 事件里,这里是读答案那一端;没问过、问过没答、以后再说,各写各的。
|
|
22
|
+
|
|
12
23
|
*
|
|
13
24
|
* 三条硬约束(和 note.js 同一条线):
|
|
14
25
|
* 1. **不审批**。约一件事不是改自己,不设关卡。
|
|
@@ -45,6 +56,25 @@ export const APPOINT_MAX_DAYS = 60
|
|
|
45
56
|
/** list 一次最多列几条。 */
|
|
46
57
|
export const APPOINT_SHOW_MAX = 5
|
|
47
58
|
|
|
59
|
+
/**
|
|
60
|
+
* 到点后宽限多久还算「准点」(2026-11-18,用户 m01744)。
|
|
61
|
+
* 5 分钟内叫她都算准时;超过就明说晚了多少。
|
|
62
|
+
*/
|
|
63
|
+
export const APPT_GRACE_MS = 5 * 60_000
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 两条到点的约定之间至少隔多久(2026-11-18)。
|
|
67
|
+
* 比作息唤醒的 45 分钟短:它不是「该醒了」,是「这件事到点了」。
|
|
68
|
+
*/
|
|
69
|
+
export const APPT_MIN_GAP_MS = 10 * 60_000
|
|
70
|
+
|
|
71
|
+
/** 时刻写成 08:00 这样;24 小时制。 */
|
|
72
|
+
const HHMM = /^([01]\d|2[0-3]):[0-5]\d$/
|
|
73
|
+
|
|
74
|
+
export function isTime(v) {
|
|
75
|
+
return HHMM.test(String(v ?? '').trim())
|
|
76
|
+
}
|
|
77
|
+
|
|
48
78
|
const STATUSES = ['open', 'done', 'cancelled', 'missed']
|
|
49
79
|
|
|
50
80
|
export function appointPath(root = rootDir()) {
|
|
@@ -90,20 +120,50 @@ export function normalizeAppoint(raw) {
|
|
|
90
120
|
const what = String(raw.what ?? '').replace(/\s+/g, ' ').trim()
|
|
91
121
|
if (!what || !isDay(raw.due)) return null
|
|
92
122
|
const status = STATUSES.includes(String(raw.status)) ? String(raw.status) : 'open'
|
|
123
|
+
const time = isTime(raw.time) ? String(raw.time).trim() : null
|
|
124
|
+
const late = Number(raw.lateMinutes)
|
|
93
125
|
return {
|
|
94
126
|
id: String(raw.id ?? '') || newAppointId(),
|
|
95
127
|
due: String(raw.due),
|
|
128
|
+
// 2026-11-18:带时刻的约定。`null` 是老形状(只认日子)。
|
|
129
|
+
time,
|
|
96
130
|
what: clipText(what, APPOINT_TEXT_MAX, ''),
|
|
97
131
|
created: String(raw.created ?? '') || new Date().toISOString(),
|
|
98
132
|
status,
|
|
99
133
|
// 到点提过一次就写时间;改期会清回 null,让她在新日子再想起来一次
|
|
100
134
|
notifiedAt: raw.notifiedAt == null ? null : String(raw.notifiedAt),
|
|
135
|
+
// 实际叫起来时晚了几分钟(超过宽限才写;没晚就是 null)
|
|
136
|
+
lateMinutes: Number.isFinite(late) && late > 0 ? Math.round(late) : null,
|
|
137
|
+
// 他答应过没有:true 答应、false 没问成或问过他还没答、null 还没问过(不猜)
|
|
138
|
+
agreed: raw.agreed === true ? true : raw.agreed === false ? false : null,
|
|
139
|
+
// 她为这条发的那张确认卡(ask 事件的 id),到点用来看他当时怎么答的
|
|
140
|
+
askId: raw.askId == null || String(raw.askId).trim() === '' ? null : String(raw.askId).trim(),
|
|
101
141
|
closedAt: raw.closedAt == null ? null : String(raw.closedAt),
|
|
102
142
|
outcome: ['done', 'cancelled', 'missed'].includes(String(raw.outcome)) ? String(raw.outcome) : null,
|
|
103
143
|
reason: raw.reason == null ? null : clipText(raw.reason, 200, ''),
|
|
104
144
|
}
|
|
105
145
|
}
|
|
106
146
|
|
|
147
|
+
/**
|
|
148
|
+
* 这条约定「到点的那个瞬间」是哪一刻(毫秒)。没有时刻的返回 null。
|
|
149
|
+
* 用本地时间拼:她说的 08:00 是她那边的 08:00,不是 UTC 的。
|
|
150
|
+
*/
|
|
151
|
+
export function momentOf(appt) {
|
|
152
|
+
if (!appt || !isDay(appt.due) || !isTime(appt.time)) return null
|
|
153
|
+
const [y, m, d] = String(appt.due).split('-').map(Number)
|
|
154
|
+
const [hh, mm] = String(appt.time).split(':').map(Number)
|
|
155
|
+
const t = new Date(y, m - 1, d, hh, mm, 0, 0).getTime()
|
|
156
|
+
return Number.isFinite(t) ? t : null
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** 到点后过去了几分钟(没到点或没时刻返回 null)。 */
|
|
160
|
+
export function lateMinutesOf(appt, now = Date.now()) {
|
|
161
|
+
const at = momentOf(appt)
|
|
162
|
+
if (at == null) return null
|
|
163
|
+
const late = Math.floor((Number(now) - at) / 60_000)
|
|
164
|
+
return late < 0 ? null : late
|
|
165
|
+
}
|
|
166
|
+
|
|
107
167
|
/** 读全部预约。文件坏了/没有都只当空,永不抛。 */
|
|
108
168
|
export async function readAppointments(root = rootDir()) {
|
|
109
169
|
const raw = await readJson(appointPath(root), [])
|
|
@@ -118,20 +178,35 @@ export async function writeAppointments(root, list) {
|
|
|
118
178
|
return rows
|
|
119
179
|
}
|
|
120
180
|
|
|
121
|
-
/**
|
|
181
|
+
/** 未关掉的,按日子从近到远;同一天按时刻排。 */
|
|
122
182
|
export function openAppointments(list, { now = Date.now() } = {}) {
|
|
123
183
|
const today = dateKey(new Date(now))
|
|
124
184
|
return (Array.isArray(list) ? list : [])
|
|
125
185
|
.filter((a) => a?.status === 'open')
|
|
126
|
-
.sort((a, b) =>
|
|
127
|
-
|
|
186
|
+
.sort((a, b) =>
|
|
187
|
+
dayNumber(a.due) - dayNumber(b.due)
|
|
188
|
+
|| String(a.time ?? '').localeCompare(String(b.time ?? ''))
|
|
189
|
+
|| String(a.created).localeCompare(String(b.created)))
|
|
190
|
+
.map((a) => ({ ...a, days: daysBetween(today, a.due), at: momentOf(a) }))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 到点了、还没提过的约定(从早到晚)。
|
|
195
|
+
*
|
|
196
|
+
* 2026-11-18(issue #149):带时刻的按「那一刻」算,跟日子无关——
|
|
197
|
+
* 08:00 的约定不会等到晚上 20:00 的作息窗口。老形状(只写日子)
|
|
198
|
+
* 还是只看日子,到当天就算到点。
|
|
199
|
+
*/
|
|
200
|
+
export async function dueAppointments(root = rootDir(), now = Date.now()) {
|
|
201
|
+
const rows = openAppointments(await readAppointments(root), { now })
|
|
202
|
+
.filter((a) => !a.notifiedAt)
|
|
203
|
+
.filter((a) => (a.at != null ? Number(now) >= a.at : a.days <= 0))
|
|
204
|
+
return rows
|
|
128
205
|
}
|
|
129
206
|
|
|
130
207
|
/** 到点、还没提过的那一条。过期了也返回——晚几天也是欠着,不替她抹掉。 */
|
|
131
208
|
export async function dueAppointment(root = rootDir(), now = Date.now()) {
|
|
132
|
-
const
|
|
133
|
-
const rows = openAppointments(await readAppointments(root), { now })
|
|
134
|
-
.filter((a) => !a.notifiedAt && a.days <= 0)
|
|
209
|
+
const rows = await dueAppointments(root, now)
|
|
135
210
|
return rows[0] ?? null
|
|
136
211
|
}
|
|
137
212
|
|
|
@@ -148,12 +223,14 @@ export async function nextAppointment(root = rootDir(), now = Date.now()) {
|
|
|
148
223
|
const mutateAppointments = (root, fn) => mutateJson(appointPath(root), fn)
|
|
149
224
|
|
|
150
225
|
/** 记下「这一条到点提过了」,免得同一个 tick 反复叫。 */
|
|
151
|
-
export async function markNotified(root, id, now = Date.now()) {
|
|
226
|
+
export async function markNotified(root, id, now = Date.now(), { lateMinutes = null } = {}) {
|
|
152
227
|
return mutateAppointments(root, async () => {
|
|
153
228
|
const rows = await readAppointments(root)
|
|
154
229
|
const hit = rows.find((a) => a.id === id)
|
|
155
230
|
if (!hit || hit.status !== 'open' || hit.notifiedAt) return false
|
|
156
231
|
hit.notifiedAt = new Date(now).toISOString()
|
|
232
|
+
const late = Number(lateMinutes)
|
|
233
|
+
if (Number.isFinite(late) && late > 0) hit.lateMinutes = Math.round(late)
|
|
157
234
|
await writeAppointments(root, rows)
|
|
158
235
|
return true
|
|
159
236
|
})
|
|
@@ -161,17 +238,25 @@ export async function markNotified(root, id, now = Date.now()) {
|
|
|
161
238
|
|
|
162
239
|
/**
|
|
163
240
|
* 定一条约定。返回 { ok, id, note, echo }。
|
|
164
|
-
* `due` 只收 YYYY-MM-DD
|
|
241
|
+
* `due` 只收 YYYY-MM-DD;`time` 可给可不给(给了就是「到点」而不是「到日子」);
|
|
242
|
+
* 日子与字数都在这里挡,不信入参。
|
|
243
|
+
*
|
|
244
|
+
* 2026-11-18(issue #149 / 用户 m01769、m01793):
|
|
245
|
+
* 带时刻的约定要「一定让她看见」,所以它不进作息窗口,也不等今天抽的落点;
|
|
246
|
+
* 他答没答应只决定措辞(`askId` 对到那张确认卡),不决定叫不叫。
|
|
165
247
|
*/
|
|
166
|
-
export async function makeAppointment(root = rootDir(), { what, due, now = Date.now() } = {}) {
|
|
248
|
+
export async function makeAppointment(root = rootDir(), { what, due, time, askId, now = Date.now() } = {}) {
|
|
167
249
|
const text = String(what ?? '').replace(/\s+/g, ' ').trim()
|
|
168
250
|
const day = String(due ?? '').trim()
|
|
251
|
+
const at = time == null || String(time).trim() === '' ? null : String(time).trim()
|
|
169
252
|
const today = dateKey(new Date(now))
|
|
170
253
|
if (text.length < 4) return { ok: false, id: '', note: '要做的那件事太短了,说清楚一点。' }
|
|
171
254
|
if (text.length > APPOINT_TEXT_MAX) {
|
|
172
255
|
return { ok: false, id: '', note: `太长,压到 ${APPOINT_TEXT_MAX} 字以内。` }
|
|
173
256
|
}
|
|
174
257
|
if (!isDay(day)) return { ok: false, id: '', note: '日子要写成 2026-11-11 这样。' }
|
|
258
|
+
if (at != null && !isTime(at)) return { ok: false, id: '', note: '时刻要写成 08:00 这样,24 小时制。' }
|
|
259
|
+
if (at != null && day === today) return { ok: false, id: '', note: '带时刻的约定最早只能约到明天。' }
|
|
175
260
|
const gap = daysBetween(today, day)
|
|
176
261
|
if (gap < APPOINT_MIN_DAYS) return { ok: false, id: '', note: '最早只能约到明天。' }
|
|
177
262
|
if (gap > APPOINT_MAX_DAYS) return { ok: false, id: '', note: `最远只能约到 ${APPOINT_MAX_DAYS} 天后。` }
|
|
@@ -180,7 +265,7 @@ export async function makeAppointment(root = rootDir(), { what, due, now = Date.
|
|
|
180
265
|
const rows = await readAppointments(root)
|
|
181
266
|
if (rows.length >= APPOINT_MAX) return { ok: false, id: '', note: '预约簿满了,先关掉几条旧的。' }
|
|
182
267
|
const open = openAppointments(rows, { now })
|
|
183
|
-
const same = open.find((a) => a.due === day && a.what === text)
|
|
268
|
+
const same = open.find((a) => a.due === day && (a.time ?? null) === at && a.what === text)
|
|
184
269
|
if (same) return { ok: true, id: same.id, note: '这件事之前就约好了。', echo: same.what }
|
|
185
270
|
if (open.length >= APPOINT_OPEN_MAX) {
|
|
186
271
|
return { ok: false, id: '', note: `手上已经有两件没做完的约定了,先把它们收掉。` }
|
|
@@ -188,9 +273,11 @@ export async function makeAppointment(root = rootDir(), { what, due, now = Date.
|
|
|
188
273
|
const row = normalizeAppoint({
|
|
189
274
|
id: newAppointId(now),
|
|
190
275
|
due: day,
|
|
276
|
+
time: at,
|
|
191
277
|
what: text,
|
|
192
278
|
created: new Date(now).toISOString(),
|
|
193
279
|
status: 'open',
|
|
280
|
+
askId: askId ?? null,
|
|
194
281
|
})
|
|
195
282
|
rows.push(row)
|
|
196
283
|
await writeAppointments(root, rows)
|
|
@@ -263,7 +350,9 @@ export async function listAppointments(root = rootDir(), { limit = APPOINT_SHOW_
|
|
|
263
350
|
if (!rows.length) return { ok: true, note: '这会儿没有约好的事。', text: '' }
|
|
264
351
|
const lines = rows.map((a) => {
|
|
265
352
|
const when = a.days === 0 ? '今天' : a.days > 0 ? `还有 ${a.days} 天` : `已经过了 ${-a.days} 天`
|
|
266
|
-
|
|
353
|
+
const at = a.time ? ` ${a.time}` : ''
|
|
354
|
+
const late = a.notifiedAt && a.lateMinutes ? `(晚 ${a.lateMinutes} 分钟叫的)` : ''
|
|
355
|
+
return `- ${a.due}${at}(${when}):「${a.what}」${late}`
|
|
267
356
|
})
|
|
268
357
|
return { ok: true, note: '预约簿', text: lines.join('\n') }
|
|
269
358
|
} catch {
|
|
@@ -271,18 +360,50 @@ export async function listAppointments(root = rootDir(), { limit = APPOINT_SHOW_
|
|
|
271
360
|
}
|
|
272
361
|
}
|
|
273
362
|
|
|
363
|
+
/** 把毫秒换成她那边的时间 HH:MM。 */
|
|
364
|
+
function clockOf(ms) {
|
|
365
|
+
const t = new Date(Number(ms))
|
|
366
|
+
return `${String(t.getHours()).padStart(2, '0')}:${String(t.getMinutes()).padStart(2, '0')}`
|
|
367
|
+
}
|
|
368
|
+
|
|
274
369
|
/**
|
|
275
|
-
*
|
|
276
|
-
*
|
|
370
|
+
* 到点那一刻她看到的处境(2026-11-18 改写,用户 m01744 / m01769 / m01793)。
|
|
371
|
+
*
|
|
372
|
+
* 这里只陈述发生过什么:约的是哪天几点、现在几点、他当时怎么答的、这条问没问过。
|
|
373
|
+
* 不替她判断「他不在」——DSH 开着的时候,他可能就守在窗口旁等她先开口;
|
|
374
|
+
* 只有她根本没被叫起来(过点很久,超过半小时),才写清「那会儿没开着」。
|
|
375
|
+
* 最后一句一定是她自己的:做不做、怎么做,她自己定。
|
|
277
376
|
*/
|
|
278
|
-
export function appointPrompt(appt, now = Date.now()) {
|
|
279
|
-
const
|
|
280
|
-
const
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
377
|
+
export function appointPrompt(appt, now = Date.now(), { answer = null } = {}) {
|
|
378
|
+
const due = String(appt?.due ?? '')
|
|
379
|
+
const time = isTime(appt?.time) ? String(appt.time) : ''
|
|
380
|
+
const late = lateMinutesOf(appt, now)
|
|
381
|
+
const hhmm = clockOf(now)
|
|
382
|
+
const head = time
|
|
383
|
+
? `你和{T}约好的事在今天 ${time}(${due}):「${appt?.what}」。`
|
|
384
|
+
: `你跟{T}约好的日子${daysBetween(due, dateKey(new Date(now))) <= 0 ? '就是今天' : `已经过了 ${daysBetween(due, dateKey(new Date(now)))} 天`}(${due}),约的是:「${appt?.what}」。`
|
|
385
|
+
|
|
386
|
+
let whenLine = ''
|
|
387
|
+
if (time && late != null) {
|
|
388
|
+
if (late <= APPT_GRACE_MS / 60_000) whenLine = '到点了。'
|
|
389
|
+
else if (late < 30) whenLine = `到点了,这会儿已经 ${hhmm},晚了 ${late} 分钟。`
|
|
390
|
+
else whenLine = `到点了。本来约的是 ${time},那会儿我这边没开着,现在 ${hhmm}。`
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const verdict = String(answer?.verdict ?? '')
|
|
394
|
+
let deal = ''
|
|
395
|
+
if (verdict === 'approve') deal = '他当时答应了这件事。'
|
|
396
|
+
else if (verdict === 'later') deal = '他当时说「以后再说」——不是答应,也不是拒绝。'
|
|
397
|
+
else if (verdict === 'reject') deal = '他当时说这次不行,这条本该作废;现在它还在。'
|
|
398
|
+
else if (verdict === 'unanswered') deal = '你问过他,一直没人答。'
|
|
399
|
+
else if (verdict === 'pending') deal = '你问过他,他还没答。'
|
|
400
|
+
else if (appt?.askId) deal = '你问过他,他还没答。'
|
|
401
|
+
else if (time) deal = '这条你还没跟{T}对过口——现在先问一句,别自己替他答应。'
|
|
402
|
+
|
|
403
|
+
const tail = time
|
|
404
|
+
? '到点了不等于你要交作业:做不做、怎么做,你自己定。'
|
|
405
|
+
: '现在先说这件事:你自己做到没有,{T}有没有来。没来也别替{T}找理由。'
|
|
406
|
+
return fillOwner([head, whenLine, deal, tail].filter(Boolean).join(''))
|
|
286
407
|
}
|
|
287
408
|
|
|
288
409
|
/** 预约簿的一次操作。没写 action 的按「新约一条」走。 */
|
|
@@ -299,8 +420,11 @@ export function registerAppoint(ctx, deps = {}) {
|
|
|
299
420
|
name: 'her_appoint',
|
|
300
421
|
description:
|
|
301
422
|
'跟{T}约一件具体的事:写清哪一天、要做什么。到那天你醒来会先想起它。' +
|
|
302
|
-
'约的时候给 what 和 due(日子写成 2026-11-11
|
|
303
|
-
'
|
|
423
|
+
'约的时候给 what 和 due(日子写成 2026-11-11);想定到几点就给 time(写成 08:00)——' +
|
|
424
|
+
'带时刻的约定不看作息时间,到点一定让你看见,晚几分钟也会写清晚了多久。' +
|
|
425
|
+
'想让这件事变成两个人都点过头的事,约好之后再用 ask_owner 问一句(kind 填 appoint,subject 写这条的编号);' +
|
|
426
|
+
'不问也会叫,只是到点会写「这条还没对过口」。' +
|
|
427
|
+
'看预约簿给 action:"list";改日子给 action:"move" 和 newDue;做到了给 action:"done";' +
|
|
304
428
|
'不做了给 action:"cancel",并说一句 reason。',
|
|
305
429
|
parameters: toJsonSchema({
|
|
306
430
|
action: {
|
|
@@ -316,6 +440,10 @@ export function registerAppoint(ctx, deps = {}) {
|
|
|
316
440
|
type: 'string',
|
|
317
441
|
description: '哪一天,写成 2026-11-11。最早明天,最远 60 天后。',
|
|
318
442
|
},
|
|
443
|
+
time: {
|
|
444
|
+
type: 'string',
|
|
445
|
+
description: '可选:几点,写成 08:00(24 小时制)。给了就按这一刻叫,不看作息时间;不给就只按日子。',
|
|
446
|
+
},
|
|
319
447
|
id: { type: 'string', description: '预约的编号。手里没有就不用给,给 what 也行。' },
|
|
320
448
|
newDue: { type: 'string', description: '改期时改成哪一天。' },
|
|
321
449
|
reason: { type: 'string', description: '取消时为什么,必填。' },
|
|
@@ -341,7 +469,8 @@ export function registerAppoint(ctx, deps = {}) {
|
|
|
341
469
|
if (act === 'done') return [{ type: 'text', text: out.ok ? '[记下了]' : `[没收掉:${out.note}]` }]
|
|
342
470
|
if (act === 'cancel') return [{ type: 'text', text: out.ok ? '[取消了]' : `[没取消:${out.note}]` }]
|
|
343
471
|
if (!out.ok) return [{ type: 'text', text: `[没约上:${out.note}]` }]
|
|
344
|
-
|
|
472
|
+
const at = String(args?.time ?? '').trim()
|
|
473
|
+
return [{ type: 'text', text: `[约好了:${args?.due ?? ''}${at ? ` ${at}` : ''} ${out.echo ?? ''}]` }]
|
|
345
474
|
},
|
|
346
475
|
},
|
|
347
476
|
async execute(args, exec) {
|
|
@@ -363,7 +492,7 @@ export function registerAppoint(ctx, deps = {}) {
|
|
|
363
492
|
reason: args?.reason,
|
|
364
493
|
})
|
|
365
494
|
}
|
|
366
|
-
return makeAppointment(root, { what: args?.what, due: args?.due })
|
|
495
|
+
return makeAppointment(root, { what: args?.what, due: args?.due, time: args?.time })
|
|
367
496
|
},
|
|
368
497
|
}),
|
|
369
498
|
)
|
package/lib/ask.js
CHANGED
|
@@ -39,14 +39,27 @@ import { askInChat, picked } from './askinchat.js'
|
|
|
39
39
|
import { tsMs } from './schema.js'
|
|
40
40
|
import { logLine } from './log.js'
|
|
41
41
|
import { fillOwner } from './pronoun.js'
|
|
42
|
+
import { closeAppointment, openAppointments, readAppointments } from './appoint.js'
|
|
42
43
|
|
|
43
|
-
/**
|
|
44
|
-
|
|
44
|
+
/**
|
|
45
|
+
* 允许的用途。need-file / need-tool / other / rule 不落实权限;
|
|
46
|
+
* time / rest 会真改东西;appoint 是「为某条约定问一句」(2026-11-18,issue #149)。
|
|
47
|
+
*/
|
|
48
|
+
export const ASK_KINDS = Object.freeze(['need-file', 'need-tool', 'time', 'rest', 'other', 'rule', 'appoint'])
|
|
45
49
|
|
|
46
50
|
/** 每天最多问几次、每周最多问几次(写死,不进配置)。 */
|
|
47
51
|
export const ASK_PER_DAY = 1
|
|
48
52
|
export const ASK_PER_WEEK = 3
|
|
49
53
|
|
|
54
|
+
/**
|
|
55
|
+
* 约定类的确认一天最多问几次(2026-11-18)。
|
|
56
|
+
*
|
|
57
|
+
* 为什么不占上面那 1/3:那是她「替自己争一件事」的额度,被约定挤掉就变成
|
|
58
|
+
* 他答应一个早安、她就少一次为自己说话的机会。这类只是把两个人已经说过的
|
|
59
|
+
* 事记成两个人都点过头,上限和同时能挂的约定数对齐(APPOINT_OPEN_MAX = 2)。
|
|
60
|
+
*/
|
|
61
|
+
export const APPOINT_ASK_PER_DAY = 2
|
|
62
|
+
|
|
50
63
|
/** kind=time 的时段,和 schema.js 存得下的一样。 */
|
|
51
64
|
const HHMM = /^([01]\d|2[0-3]):[0-5]\d$/
|
|
52
65
|
|
|
@@ -77,6 +90,19 @@ export function answersByAsk(events = []) {
|
|
|
77
90
|
return m
|
|
78
91
|
}
|
|
79
92
|
|
|
93
|
+
/**
|
|
94
|
+
* 约定编号 → 为它问过的那张卡(ask 事件)。同一件事问过两次只认最后一次。
|
|
95
|
+
* 2026-11-18:约定到点时用这个看他当时到底怎么答的,不靠猜。
|
|
96
|
+
*/
|
|
97
|
+
export function askForAppoint(events = []) {
|
|
98
|
+
const m = new Map()
|
|
99
|
+
for (const e of asksOf(events)) {
|
|
100
|
+
const id = String(e?.ask?.appointId ?? '')
|
|
101
|
+
if (id) m.set(id, e)
|
|
102
|
+
}
|
|
103
|
+
return m
|
|
104
|
+
}
|
|
105
|
+
|
|
80
106
|
/** 还没答的请求(按时间从早到晚)。 */
|
|
81
107
|
export function pendingAsks(events = [], now = Date.now()) {
|
|
82
108
|
const done = answersByAsk(events)
|
|
@@ -86,9 +112,9 @@ export function pendingAsks(events = [], now = Date.now()) {
|
|
|
86
112
|
.sort((a, b) => tsMs(a) - tsMs(b))
|
|
87
113
|
}
|
|
88
114
|
|
|
89
|
-
/**
|
|
115
|
+
/** 配额:今天问过没有、最近七天问过几次。约定类的确认不算在这里(见 APPOINT_ASK_PER_DAY)。 */
|
|
90
116
|
export function quotaOf(events = [], { now = Date.now(), dk = dateKey() } = {}) {
|
|
91
|
-
const asks = asksOf(events)
|
|
117
|
+
const asks = asksOf(events).filter((e) => String(e?.ask?.kind ?? '') !== 'appoint')
|
|
92
118
|
// 真的事件上 day 是成长第几天(一个整数),不是日期,只能按时间算。
|
|
93
119
|
const today = asks.filter((e) => dateKey(new Date(tsMs(e) || now)) === dk).length
|
|
94
120
|
const week = asks.filter((e) => now - tsMs(e) < 7 * 24 * 3600 * 1000).length
|
|
@@ -102,6 +128,18 @@ export function quotaOf(events = [], { now = Date.now(), dk = dateKey() } = {})
|
|
|
102
128
|
}
|
|
103
129
|
}
|
|
104
130
|
|
|
131
|
+
/** 约定类确认的日配额(不复用上面那 1/3)。 */
|
|
132
|
+
export function appointAskQuotaOf(events = [], { now = Date.now(), dk = dateKey() } = {}) {
|
|
133
|
+
const asks = asksOf(events).filter((e) => String(e?.ask?.kind ?? '') === 'appoint')
|
|
134
|
+
const today = asks.filter((e) => dateKey(new Date(tsMs(e) || now)) === dk).length
|
|
135
|
+
return {
|
|
136
|
+
day: today,
|
|
137
|
+
perDay: APPOINT_ASK_PER_DAY,
|
|
138
|
+
ok: today < APPOINT_ASK_PER_DAY,
|
|
139
|
+
why: today >= APPOINT_ASK_PER_DAY ? `今天已经问过 ${APPOINT_ASK_PER_DAY} 次约定了` : '',
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
105
143
|
/** 她今天请过假吗(rest 被同意后写一条 meta)。 */
|
|
106
144
|
export function restedToday(events = [], dk = dateKey()) {
|
|
107
145
|
return events.some((e) => e?.type === 'meta' && String(e.text ?? '').startsWith(`rest:${dk}`))
|
|
@@ -149,6 +187,12 @@ async function applyVerdict(root, askEv, verdict, { log = () => {} } = {}) {
|
|
|
149
187
|
// 都不重提、不进提示词,只让观测台和后来的她查得到。
|
|
150
188
|
const mark = verdict === 'later' ? 'ask-later' : 'ask-no'
|
|
151
189
|
await appendEvent({ type: 'meta', text: `${mark}:${askEv.id}` }, ctx, root)
|
|
190
|
+
// 约定被驳回就地作废:到点不该再叫她(2026-11-18)。「以后再说」不作废——
|
|
191
|
+
// 他没答应,但这条还挂着,到点会写清「他说以后再说」。
|
|
192
|
+
if (kind === 'appoint' && verdict === 'reject') {
|
|
193
|
+
const id = String(askEv?.ask?.value?.appointId ?? askEv?.ask?.appointId ?? '')
|
|
194
|
+
if (id) await closeAppointment(root, { id, action: 'cancel', reason: '他没答应这件事' })
|
|
195
|
+
}
|
|
152
196
|
return
|
|
153
197
|
}
|
|
154
198
|
if (kind === 'time') {
|
|
@@ -338,7 +382,9 @@ export function registerAsk(ctx, deps = {}) {
|
|
|
338
382
|
name: 'ask_owner',
|
|
339
383
|
description:
|
|
340
384
|
'有事想问那个人同不同意的时候用。只问真的影响你自己的事:今天要不要歇、能不能换个作息、' +
|
|
341
|
-
'
|
|
385
|
+
'能不能要一个文件或一个工具、你想给自己立一条什么规矩。一天最多问一次,一周最多三次(约定确认另算,见下)。' +
|
|
386
|
+
'已经约好一件事、想把「两个人都点过头」记下来时,用 kind:"appoint"、subject 填约定编号——' +
|
|
387
|
+
'这一类不算在那三次里,到点会按他当时的答复写清是答应了、还没答,还是没问成。' +
|
|
342
388
|
'不需要解释你在用哪个工具,也不用道谢。',
|
|
343
389
|
parameters: toJsonSchema({
|
|
344
390
|
question: {
|
|
@@ -350,7 +396,7 @@ export function registerAsk(ctx, deps = {}) {
|
|
|
350
396
|
type: 'string',
|
|
351
397
|
required: true,
|
|
352
398
|
enum: ASK_KINDS,
|
|
353
|
-
description: 'need-file / need-tool / time / rest / rule / other',
|
|
399
|
+
description: 'need-file / need-tool / time / rest / rule / other / appoint(为一条约定问一句)',
|
|
354
400
|
},
|
|
355
401
|
subject: {
|
|
356
402
|
type: 'string',
|
|
@@ -420,7 +466,8 @@ export function registerAsk(ctx, deps = {}) {
|
|
|
420
466
|
}
|
|
421
467
|
|
|
422
468
|
const { events } = await readEvents(root)
|
|
423
|
-
|
|
469
|
+
// 约定类的确认不占「替自己争一件事」的额度:那是另一本账(2026-11-18)。
|
|
470
|
+
const quota = kind === 'appoint' ? { ok: true } : quotaOf(events)
|
|
424
471
|
if (!quota.ok) return { ok: false, note: quota.why }
|
|
425
472
|
|
|
426
473
|
const options = optionsOf(args?.options)
|
|
@@ -439,6 +486,22 @@ export function registerAsk(ctx, deps = {}) {
|
|
|
439
486
|
}
|
|
440
487
|
}
|
|
441
488
|
|
|
489
|
+
// 为一条约定问一句(2026-11-18,issue #149):subject 必须是还没关掉的约定编号。
|
|
490
|
+
let appointId = ''
|
|
491
|
+
if (kind === 'appoint') {
|
|
492
|
+
appointId = String(args?.subject ?? '').trim()
|
|
493
|
+
if (!appointId) {
|
|
494
|
+
return { ok: false, note: fillOwner('kind=appoint 要把约定编号写在 subject 里(her_appoint 约完会给)。') }
|
|
495
|
+
}
|
|
496
|
+
const open = openAppointments(await readAppointments(root))
|
|
497
|
+
if (!open.some((a) => a.id === appointId)) {
|
|
498
|
+
return { ok: false, note: '预约簿里没有这一条还没关掉的约定。' }
|
|
499
|
+
}
|
|
500
|
+
const aq = appointAskQuotaOf(events)
|
|
501
|
+
if (!aq.ok) return { ok: false, note: aq.why }
|
|
502
|
+
value.appointId = appointId
|
|
503
|
+
}
|
|
504
|
+
|
|
442
505
|
const { config } = await loadConfig(root)
|
|
443
506
|
const model = deriveSelf(events, { growthDays: config.growthDays })
|
|
444
507
|
const ev = await appendEvent({
|
package/lib/autobackup.js
CHANGED
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
|
|
42
42
|
import { promises as fs } from 'node:fs'
|
|
43
43
|
import { join } from 'node:path'
|
|
44
|
-
import { collectBackup, bundleBytes, sizeText, BACKUP_FORMAT } from './backup.js'
|
|
44
|
+
import { collectBackup, bundleBytes, sizeText, BACKUP_FORMAT, PRESSURE_OBSERVATIONS } from './backup.js'
|
|
45
45
|
import { rootDir, writeAtomic } from './store.js'
|
|
46
46
|
import { loadConfig, updateConfig } from './settings.js'
|
|
47
47
|
|
|
@@ -78,6 +78,7 @@ export function autoBackupDir(root = rootDir()) {
|
|
|
78
78
|
export const AUTO_SKIP = ['out/covers']
|
|
79
79
|
|
|
80
80
|
const CORE_PATHS = [
|
|
81
|
+
PRESSURE_OBSERVATIONS,
|
|
81
82
|
'events.jsonl', 'config.json', 'diary', 'home.json', 'home/她的身份.md',
|
|
82
83
|
'inbox-state.json', 'inbox.md', 'talk-recent.json', '说明.md',
|
|
83
84
|
'avatar.png', 'avatar.jpg', 'avatar.webp', 'avatar.gif',
|
package/lib/backup.js
CHANGED
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
* format 不是本版本认识的 → 直接拒;schema 比程序新 → 拒(旧程序读新事件会静默丢字段);
|
|
19
19
|
* schema 比程序旧 → 放行,读的时候按旧版兼容(见 `store.readEvents`)。
|
|
20
20
|
*
|
|
21
|
-
* 不搬的东西:`logs
|
|
21
|
+
* 不搬的东西:`logs/`(会话压力观测文件除外)、
|
|
22
22
|
* `backups/`(备份里再套备份,体积会炸)。这两类列在 `excluded` 里,不静默丢;
|
|
23
23
|
* 目录名在任何一层都认(她房间里叫 backups、node_modules 的文件夹也不带),
|
|
24
24
|
* 所以设置页的导出回执要把顶层以外的那几个单独说出来。
|
|
@@ -39,11 +39,12 @@
|
|
|
39
39
|
|
|
40
40
|
import { promises as fs } from 'node:fs'
|
|
41
41
|
import { dirname, extname, join, relative, resolve, sep } from 'node:path'
|
|
42
|
-
import { rootDir } from './store.js'
|
|
42
|
+
import { rootDir, writeAtomic } from './store.js'
|
|
43
43
|
import { EVENT_SCHEMA_VERSION } from './schema.js'
|
|
44
44
|
|
|
45
45
|
/** 备份文件里写的格式标记。改字段结构就换它。 */
|
|
46
46
|
export const BACKUP_FORMAT = 'dsh-fish-backup/1'
|
|
47
|
+
export const PRESSURE_OBSERVATIONS = 'logs/session-pressure/samples.json'
|
|
47
48
|
|
|
48
49
|
/** 不搬的目录(相对状态库根)。 */
|
|
49
50
|
const SKIP_DIRS = new Set(['logs', 'backups', 'dsh-fish-backups', 'node_modules'])
|
|
@@ -252,6 +253,7 @@ const HER_DIRS = new Set(['diary', 'home', 'keepsakes', 'works'])
|
|
|
252
253
|
* 正文 .md 两份都带——自动备份不会被她画的图撑爆。
|
|
253
254
|
*/
|
|
254
255
|
function packRank(rel) {
|
|
256
|
+
if (rel === PRESSURE_OBSERVATIONS) return 0
|
|
255
257
|
if (rel === 'state.json' || rel.toLowerCase().endsWith('.log')) return 3
|
|
256
258
|
const i = rel.indexOf('/')
|
|
257
259
|
if (i < 0) return 0
|
|
@@ -329,7 +331,7 @@ export async function collectBackup(
|
|
|
329
331
|
const nonUtf8 = []
|
|
330
332
|
|
|
331
333
|
// 先只看不读:要装哪些、各多大。读和挑放在后面,挑的顺序不跟 readdir 走。
|
|
332
|
-
const walk = async (dir) => {
|
|
334
|
+
const walk = async (dir, pressureOnly = false) => {
|
|
333
335
|
let names
|
|
334
336
|
try {
|
|
335
337
|
names = await fs.readdir(dir, { withFileTypes: true })
|
|
@@ -339,6 +341,11 @@ export async function collectBackup(
|
|
|
339
341
|
for (const e of names) {
|
|
340
342
|
const full = join(dir, e.name)
|
|
341
343
|
const rel = relative(root, full).split(sep).join('/')
|
|
344
|
+
if (pressureOnly && rel !== PRESSURE_OBSERVATIONS
|
|
345
|
+
&& !(e.isDirectory() && PRESSURE_OBSERVATIONS.startsWith(rel + '/'))) {
|
|
346
|
+
excluded.push(e.isDirectory() ? rel + '/' : rel)
|
|
347
|
+
continue
|
|
348
|
+
}
|
|
342
349
|
if (extra.has(rel) || (include && !include.some((prefix) =>
|
|
343
350
|
rel === prefix || rel.startsWith(prefix + '/') || (e.isDirectory() && prefix.startsWith(rel + '/'))
|
|
344
351
|
))) {
|
|
@@ -348,7 +355,11 @@ export async function collectBackup(
|
|
|
348
355
|
}
|
|
349
356
|
if (e.isDirectory()) {
|
|
350
357
|
if (SKIP_DIRS.has(e.name)) {
|
|
351
|
-
|
|
358
|
+
const sampleDir = join(full, 'session-pressure')
|
|
359
|
+
const hasSamples = rel === 'logs' && (await fs.lstat(sampleDir).catch(() => null))?.isDirectory()
|
|
360
|
+
&& (await fs.lstat(join(sampleDir, 'samples.json')).catch(() => null))?.isFile()
|
|
361
|
+
if (hasSamples) await walk(full, true)
|
|
362
|
+
else excluded.push(`${rel}/`)
|
|
352
363
|
continue
|
|
353
364
|
}
|
|
354
365
|
// 自动备份默认不带附件目录(桥的收件箱、媒体、更新包);手动备份不传这个开关。
|
|
@@ -356,7 +367,7 @@ export async function collectBackup(
|
|
|
356
367
|
excluded.push(`${rel}/`)
|
|
357
368
|
continue
|
|
358
369
|
}
|
|
359
|
-
await walk(full)
|
|
370
|
+
await walk(full, pressureOnly)
|
|
360
371
|
continue
|
|
361
372
|
}
|
|
362
373
|
if (SKIP_FILES.has(rel)) continue
|
|
@@ -377,7 +388,7 @@ export async function collectBackup(
|
|
|
377
388
|
}
|
|
378
389
|
// 自动备份默认不带附件文件:图片 / 电子书 / 压缩包 / 音视频 / 办公文档,
|
|
379
390
|
// 以及 4MB 以上的单文件(有些桥自己起名)。手动备份不传这个开关,口径不变。
|
|
380
|
-
if (skipAttachments && !include && !ALWAYS_INCLUDE.has(rel) && !KEEP_ALWAYS.test(rel)) {
|
|
391
|
+
if (skipAttachments && !include && !ALWAYS_INCLUDE.has(rel) && rel !== PRESSURE_OBSERVATIONS && !KEEP_ALWAYS.test(rel)) {
|
|
381
392
|
if (ATTACHMENT_EXT.has(extname(e.name).toLowerCase())) {
|
|
382
393
|
excluded.push(rel)
|
|
383
394
|
continue
|
|
@@ -613,6 +624,46 @@ export function badTextFiles(bundle) {
|
|
|
613
624
|
return bad.sort(byPath)
|
|
614
625
|
}
|
|
615
626
|
|
|
627
|
+
async function preservePressureObservations(quarantined, root) {
|
|
628
|
+
const read = async path => fs.readFile(path, 'utf8').catch(error => {
|
|
629
|
+
if (error.code !== 'ENOENT') throw error
|
|
630
|
+
return null
|
|
631
|
+
})
|
|
632
|
+
const before = await read(join(quarantined, PRESSURE_OBSERVATIONS))
|
|
633
|
+
if (before == null) return
|
|
634
|
+
const dest = join(root, PRESSURE_OBSERVATIONS)
|
|
635
|
+
const restored = await read(dest)
|
|
636
|
+
if (restored === before) return
|
|
637
|
+
if (restored == null) {
|
|
638
|
+
await writeAtomic(dest, before)
|
|
639
|
+
return
|
|
640
|
+
}
|
|
641
|
+
// 观测文件坏掉不该让整次恢复失败:旧档还在 quarantined 里,恢复出来的新档照常留着。
|
|
642
|
+
let local
|
|
643
|
+
let incoming
|
|
644
|
+
try {
|
|
645
|
+
local = JSON.parse(before)
|
|
646
|
+
incoming = JSON.parse(restored)
|
|
647
|
+
} catch {
|
|
648
|
+
return
|
|
649
|
+
}
|
|
650
|
+
if (!local || typeof local !== 'object' || !incoming || typeof incoming !== 'object') return
|
|
651
|
+
const mergeBySession = (left = {}, right = {}, canonicalize = rows => rows) =>
|
|
652
|
+
Object.fromEntries([...new Set([...Object.keys(left), ...Object.keys(right)])]
|
|
653
|
+
.map(id => [id, { ...canonicalize(left[id]), ...canonicalize(right[id]) }]))
|
|
654
|
+
const bySeq = rows => Object.fromEntries(Object.entries(rows ?? {}).map(([key, row]) =>
|
|
655
|
+
[Number.isSafeInteger(row?.seq) ? row.seq : key, row]))
|
|
656
|
+
const dates = [local.observedSince, incoming.observedSince].filter(Number.isFinite)
|
|
657
|
+
await writeAtomic(dest, JSON.stringify({
|
|
658
|
+
...incoming, ...local,
|
|
659
|
+
...(dates.length ? { observedSince: Math.min(...dates) } : {}),
|
|
660
|
+
records: mergeBySession(incoming.records, local.records, bySeq),
|
|
661
|
+
compactionPairs: mergeBySession(incoming.compactionPairs, local.compactionPairs),
|
|
662
|
+
compactions: { ...incoming.compactions, ...local.compactions },
|
|
663
|
+
rotations: { ...incoming.rotations, ...local.rotations },
|
|
664
|
+
}))
|
|
665
|
+
}
|
|
666
|
+
|
|
616
667
|
/**
|
|
617
668
|
* 恢复。顺序:校验 → 旧档改名 → 建新库 → 写文件。
|
|
618
669
|
* @param {{ skipBad?: boolean }} [opts] skipBad:包里那些「读出来是碎字」的文本已经给人看过、
|
|
@@ -673,6 +724,7 @@ export async function restoreBackup(bundle, root = rootDir(), { now = new Date()
|
|
|
673
724
|
encoding: f.enc === 'base64' ? undefined : 'utf8',
|
|
674
725
|
})
|
|
675
726
|
}
|
|
727
|
+
if (hadOld) await preservePressureObservations(quarantined, root)
|
|
676
728
|
} catch (err) {
|
|
677
729
|
return {
|
|
678
730
|
ok: false,
|