@laraveladmin/dsh-stay-awake 0.2.13 → 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.
- package/CHANGELOG.md +59 -0
- package/README.en.md +35 -11
- package/README.md +35 -11
- package/client.js +181 -30
- package/docs/REQUIREMENTS.md +35 -11
- package/docs/VERIFICATION.md +60 -0
- package/index.js +107 -1
- package/lib/auto-resume.js +95 -0
- package/lib/config.js +35 -0
- package/lib/probe.js +50 -0
- package/lib/resume.js +173 -0
- package/lib/server.js +22 -8
- package/lib/sound.js +19 -4
- package/lib/state.js +2 -1
- package/locale/en.json +1 -1
- package/locale/zh-Hant.json +6 -0
- package/locale/zh.json +1 -1
- package/market/index.json +2 -2
- package/package.json +2 -2
package/docs/REQUIREMENTS.md
CHANGED
|
@@ -257,9 +257,25 @@ wanted = config.enabled && hasWork
|
|
|
257
257
|
| FR-11.2 | 从 Git 安装的用户更新到新的固定 commit;从本地目录(`link:`)安装的用户 `git pull` 即更新,没有版本概念,也不会出现更新提示 |
|
|
258
258
|
| FR-11.3 | Discover 索引源随每次发布更新;**首选 GitHub raw 地址**(推送即生效),jsDelivr 作为备用并在发布流程中主动 purge(无版本 URL 有 CDN 缓存,不 purge 会长期读到旧索引) |
|
|
259
259
|
| FR-11.4 | 更新不得影响用户配置:`cordis.patch.yml` 的 config 与 `state.json` 保留;当前版本在弹层底部显示 |
|
|
260
|
-
| FR-11.5 |
|
|
260
|
+
| FR-11.5 | 插件**不联网检查更新**,更新检测完全交给 DSH;唯一的网络动作是 FR-12 的 TCP 可达性探测,与更新检查无关 |
|
|
261
261
|
| FR-11.6 | 发布流程固定为:改版本 → 重建索引 → 全量测试 → 提交 → 双远端推送 → `npm publish` → purge CDN → 校验(见 `docs/RELEASING.md`) |
|
|
262
262
|
|
|
263
|
+
### FR-12 断网自动重试
|
|
264
|
+
|
|
265
|
+
目标:合盖休眠导致的那一轮断网失败,在用户回到电脑、网络恢复后自动续跑,不需要用户手动点「重试」。
|
|
266
|
+
|
|
267
|
+
| 序号 | 规则 |
|
|
268
|
+
|---|---|
|
|
269
|
+
| FR-12.1 | 判定复用 DSH 自身的可续跑语义:本轮以 `turn/end reason.kind = error` 结束,错误码属于网络类集合(默认 `TRANSPORT`、`TIMEOUT`,可由 `autoResumeCodes` 扩展),且本轮存在被接纳的人类输入(`user/message`,`source.kind = user` 且内容非空)与一次失败的模型请求(`assistant/attempt` 之后没有成功的 `assistant/message`) |
|
|
270
|
+
| FR-12.2 | 不触发的情形:用户主动中断(`aborted`)、等待审批(`blocked`)、`interrupted`、`max-tokens`、限流/服务端错误(默认码集之外的错误码) |
|
|
271
|
+
| FR-12.3 | 只处理普通会话:`header.parentSession` 存在或 `header.origin = subagent` 的子代理会话不参与,由父会话负责 |
|
|
272
|
+
| FR-12.4 | 触发前必须由 **TCP 探测**确认网络可达:间隔 `autoResumeProbeIntervalMs`(默认 15s),目标 `autoResumeProbeHost:Port`(默认 `api.deepseek.com:443`),只做三次握手,不发送任何数据、不读取会话内容 |
|
|
273
|
+
| FR-12.5 | 重试方式等价于界面「重试」按钮:调用宿主 `sessionController.prompt`,`content: []`(产生 `retry` 来源的空消息),`mode: queue`;宿主返回 `session/agent-busy` 时视为不可续跑并清除等待 |
|
|
274
|
+
| FR-12.6 | 预算:每个会话每次故障最多 `autoResumeMaxAttempts`(默认 3)次,`autoResumeWindowMinutes`(默认 60)分钟内有效;用户自己发消息或该轮成功完成会重置预算 |
|
|
275
|
+
| FR-12.7 | 失败受理、宿主缺少 `sessionController`、探测持续失败都只记日志并在弹层状态区提示,不影响保活与提示音 |
|
|
276
|
+
| FR-12.8 | 弹层提供「断网自动重试」开关(默认开,写入 `state.json`);关闭立即清空等待队列;宿主重启不保留待重试队列 |
|
|
277
|
+
| FR-12.9 | 状态可见:`/api/stay-awake` 回包附带 `autoResume` 与 `resume { state, retries, lastSentAt, failure }`,客户端在弹层开关下方显示「等待网络恢复… / 正在自动重试… / 自动重试未成功」,并复用底部状态行提示「已自动重试」;协议版本升到 3 |
|
|
278
|
+
|
|
263
279
|
### FR-10 任务结束提示音
|
|
264
280
|
|
|
265
281
|
目标:用户离开电脑时,任务结束能"听见",不必回来看屏幕。
|
|
@@ -275,20 +291,23 @@ wanted = config.enabled && hasWork
|
|
|
275
291
|
| FR-10.7 | **宿主端播放**(不依赖浏览器自动播放策略,DSH 窗口最小化/后台也能响):macOS `afplay`、Windows `System.Media.SystemSounds`、Linux `paplay`(不可用时静默降级) |
|
|
276
292
|
| FR-10.8 | 播放必须异步、不阻塞事件循环;子进程 `unref`,5 秒未结束则忽略;同一秒内最多播放一次 |
|
|
277
293
|
| FR-10.9 | 播放失败(无播放器、无音频文件、静音)只记日志,不弹错、不影响其它功能 |
|
|
278
|
-
| FR-10.10 |
|
|
279
|
-
| FR-10.11 |
|
|
294
|
+
| FR-10.10 | 支持试听:弹层提供「完成 / 失败 / 提醒」三个小按钮,分别调用宿主播放对应音效,用于确认设备与音量 |
|
|
295
|
+
| FR-10.11 | 弹层只放一个「提示音」总开关(覆盖完成/失败/提醒三种);音效文件、音量、最短时长、是否只响用户任务、是否提醒提问走 `cordis.patch.yml` |
|
|
280
296
|
| FR-10.12 | `sound` 开关与 `enabled`/`scope` 一样持久化到状态文件,重启后保持 |
|
|
281
297
|
|
|
282
298
|
| FR-10.13 | 插件总开关 `enabled=false` 时不响(用户明确关掉了整套防护) |
|
|
283
299
|
| FR-10.14 | 「试听」不受 `sound` 开关与 `enabled` 限制,任何时候都能验证设备与音量 |
|
|
300
|
+
| FR-10.15 | **需要你回答时响一声**:监听 waterfall 事件 `user-questions/request`(`ask_user_question`)与 `approval/request`(工具审批),旁路调用 `next()` 不干扰原流程,播放第三种音效(`Ping` / `SystemSounds.Question` / `message.oga`);这是唯一在回合未结束时也会响的提示音 |
|
|
301
|
+
| FR-10.16 | 提醒音受 `sound` 总开关与 `soundOnQuestion`(默认 true)控制,不受 `soundOnlyUserTasks` 影响(自主轮次提问也要人回答);1 秒节流防连响;播放失败只记日志,绝不打断问答 |
|
|
302
|
+
| FR-10.17 | 试听请求带 `kind ∈ {success, error, question}`,宿主据此播放对应音效;未知 kind 返回 400 |
|
|
284
303
|
|
|
285
304
|
平台默认音效:
|
|
286
305
|
|
|
287
|
-
| 平台 | 完成音 | 失败音 | 说明 |
|
|
288
|
-
|
|
289
|
-
| macOS | `/System/Library/Sounds/Glass.aiff` | `/System/Library/Sounds/Basso.aiff` | 播放器 `afplay`,支持 `-v` 音量 |
|
|
290
|
-
| Windows | `SystemSounds.Asterisk` | `SystemSounds.Hand` | PowerShell 调用,无音量参数 |
|
|
291
|
-
| Linux | `/usr/share/sounds/freedesktop/stereo/complete.oga` | `dialog-error.oga` | `paplay`;文件缺失则静默 |
|
|
306
|
+
| 平台 | 完成音 | 失败音 | 提醒音 | 说明 |
|
|
307
|
+
|---|---|---|---|---|
|
|
308
|
+
| macOS | `/System/Library/Sounds/Glass.aiff` | `/System/Library/Sounds/Basso.aiff` | `/System/Library/Sounds/Ping.aiff` | 播放器 `afplay`,支持 `-v` 音量 |
|
|
309
|
+
| Windows | `SystemSounds.Asterisk` | `SystemSounds.Hand` | `SystemSounds.Question` | PowerShell 调用,无音量参数 |
|
|
310
|
+
| Linux | `/usr/share/sounds/freedesktop/stereo/complete.oga` | `dialog-error.oga` | `message.oga` | `paplay`;文件缺失则静默 |
|
|
292
311
|
|
|
293
312
|
**边界说明**:系统静音、耳机未佩戴、音频设备切走时用户听不到,插件无法感知;DSH 跑在远程主机时声音在远端播放(本插件只作用于运行 Host 的机器)。
|
|
294
313
|
|
|
@@ -405,7 +424,7 @@ Idle ──wanted──▶ Holding ──!wanted──▶ Releasing ──超时
|
|
|
405
424
|
| NFR-1 | 零第三方运行时依赖,零 native 模块 |
|
|
406
425
|
| NFR-2 | 空闲开销:事件驱动 + 60s 对账,无高频轮询;稳态 CPU 占用可忽略,常驻内存增量 < 5MB |
|
|
407
426
|
| NFR-3 | 加载开销 < 50ms;acquire 延迟 < 500ms(Windows PowerShell 冷启动例外) |
|
|
408
|
-
| NFR-4 |
|
|
427
|
+
| NFR-4 | 隐私:不读取会话内容/提示词/文件内容;唯一本地写入是插件自己的状态文件;唯一外发动作是开启「断网自动重试」后对配置主机的 TCP 连接探测(只建连、不发数据,可关闭)。插件不联网检查更新 |
|
|
409
428
|
| NFR-5 | 兼容:不声明 `engines.dsh`;缺 `agents`/`jobs` 等服务时按可用性降级,不得阻塞启动 |
|
|
410
429
|
| NFR-6 | 可测:判定与状态机为纯逻辑,可注入时钟与 spawn;平台命令生成可单测 |
|
|
411
430
|
| NFR-7 | 安全:不修改系统电源计划,不申请提权,不写系统目录 |
|
|
@@ -453,6 +472,10 @@ Idle ──wanted──▶ Holding ──!wanted──▶ Releasing ──超时
|
|
|
453
472
|
| AC-18 | 提示音开启 | `run_in_background` 的 30 秒任务跑完 | 响一次,且不早于后台进程结束 |
|
|
454
473
|
| AC-19 | 提示音开启,`soundOnlyUserTasks` | 自主轮次(goal)结束 | 不响 |
|
|
455
474
|
| AC-20 | 无播放器/静音环境 | 任务结束 | 无异常抛出,插件其余功能正常 |
|
|
475
|
+
| AC-21 | 断网自动重试开启 | 任务中合盖休眠 → 唤醒、网络恢复 | 15–30s 内该轮自动续跑(对话中出现一条 retry 用户消息),无需手动点重试 |
|
|
476
|
+
| AC-22 | 断网自动重试开启 | 网络始终未恢复 | 不重试;弹层状态显示「等待网络恢复…」;恢复后才重试 |
|
|
477
|
+
| AC-23 | 断网自动重试开启 | 用户主动停止回合 → 关闭网络再恢复 | 不触发自动重试 |
|
|
478
|
+
| AC-24 | 断网自动重试开启 | 连续 3 次重试仍失败 | 停止,弹层显示「自动重试未成功」,不再尝试 |
|
|
456
479
|
|
|
457
480
|
平台归属:**AC-1 ~ AC-14 在 macOS 上由作者逐条真机验收**;Windows / Linux 先以单元测试与命令生成验证,真机验收由使用者或 CI 补充(见 §14.3)。
|
|
458
481
|
|
|
@@ -534,7 +557,8 @@ pgrep -fl "caffeinate -i -w"
|
|
|
534
557
|
| FR-1.3 后台任务 | ✅ 实现,逻辑测试覆盖 | `ctx.jobs` 事件 + `jobs.list(sessionId)` 对账;真机需用户在 agent idle 时跑后台任务观察 |
|
|
535
558
|
| FR-1.4 计划任务 | ❌ 未实现 | 仍为 P2 |
|
|
536
559
|
| FR-11 更新与发布运维 | ✅ 流程落地 | 更新由 DSH 插件管理器负责(registry 安装可一键更新、失败回滚);索引首选 GitHub raw,jsDelivr 发布后 purge;发布清单见 docs/RELEASING.md |
|
|
537
|
-
| FR-
|
|
560
|
+
| FR-12 断网自动重试 | ✅ 实现 | TCP 探测后调用宿主 `sessionController.prompt` 空内容 retry;失败码集合、探测目标、预算均可配置;真实合盖断网场景需用户在真机验收(见 VERIFICATION) |
|
|
561
|
+
| FR-10 提示音 | ✅ 实现 | 工作段结束(完成/失败)+ 需要你回答(提问/审批)三类音效、最短时长、仅用户任务、宿主端播放、弹层总开关 + 三种试听、提醒音配置;开关持久化 |
|
|
538
562
|
| FR-2 释放判定 | ✅ 实现并真机验证 | 防抖释放、竞争重算、禁用立即释放 |
|
|
539
563
|
| FR-3 三平台后端 | macOS ✅ 真机;Windows / Linux ✅ 实现 + 单测 | Windows/Linux 电源效果未真机验证 |
|
|
540
564
|
| FR-4.2 `scope` 热切换 | ✅ 实现 | 弹层切换时先起新断言再停旧的;改 `cordis.patch.yml` 的 scope 仍需要插件重载 |
|
|
@@ -542,7 +566,7 @@ pgrep -fl "caffeinate -i -w"
|
|
|
542
566
|
| FR-6 配置 | ✅ 除 `batteryPolicy` / `minBatteryPercent` | 两项未实现,也未暴露,避免"配置了却不生效" |
|
|
543
567
|
| FR-6.2 状态文件 | ✅ 实现 | `~/.dsh/plugin-data/dsh-stay-awake/state.json`,只存弹层里的 enabled/scope/sound;原子写(tmp+rename),损坏或非法值回落默认并记 warning |
|
|
544
568
|
| FR-7 可观测 | 🟡 部分 | 有内存环形日志与 `/api/stay-awake` 状态;无日志导出 |
|
|
545
|
-
| FR-8 UI | ✅ 可交互指示器 +
|
|
569
|
+
| FR-8 UI | ✅ 可交互指示器 + 设置弹层(简中/繁中/英文) | 点击状态栏「守夜」打开 `shell.overlay` 弹层:总开关、允许屏幕休眠、提示音(含完成/失败/提醒三种试听)、断网自动重试、临时保持(30 分钟 / 2 小时 / 取消)、状态详情、源码链接;面板与指示器共享单例状态,一个页面只有一个轮询 |
|
|
546
570
|
| FR-9 生命周期 | ✅ 实现并真机验证 | 禁用即释放、重新启用即重取、宿主 SIGKILL 不残留 |
|
|
547
571
|
|
|
548
572
|
**配置优先级**:`cordis.patch.yml` 提供默认值,弹层改过的 `enabled` / `scope` 以状态文件为准;其余参数(防抖、兜底时长、对账间隔等)只能改 patch。
|
package/docs/VERIFICATION.md
CHANGED
|
@@ -151,6 +151,66 @@
|
|
|
151
151
|
中英字典各 45 键、键集合一致、无空值、英文无 CJK 残留。
|
|
152
152
|
- 未验证:动画观感、面板是否确实零跳动、省略号在窄面板下的表现,需宿主重启后目视确认(本会话无浏览器控制权)。
|
|
153
153
|
|
|
154
|
+
## 0.2.14 反馈浮层(2026-12-03)
|
|
155
|
+
|
|
156
|
+
- **0.2.13 的遗留缺陷**:把底部说明文字原地替换成反馈文案后,高度稳定了,但宽度会跳——说明文字是面板里
|
|
157
|
+
最宽的一行(`width: max-content` 由它决定),被换成短文案后弹层立刻收窄,2.5 秒后又撑回去。
|
|
158
|
+
- **改法**:说明文字不再被替换或卸载(继续撑宽度),反馈改为 `.dsa-status-note` 绝对定位浮层(`inset: 0`,
|
|
159
|
+
不参与布局、`pointer-events: none`)。切换时说明 `opacity 1→0`(140ms),浮层 `opacity 0→1` +
|
|
160
|
+
`translateY(2px→0)`(160ms),2.5 秒后反向;`prefers-reduced-motion` 下取消过渡。
|
|
161
|
+
结果:弹层外框宽高全程不变,只有行内文字交叉淡入淡出。
|
|
162
|
+
- **真机目视确认(用户,2026-12-03)**:重启宿主后反复点「试听」与三个开关,反馈在原位切换、弹层边缘不动,
|
|
163
|
+
确认「没问题了」。
|
|
164
|
+
- **追加修复:飞行中不再置灰整块面板**。用户反馈点「提示音」开关后「临时保持」有时会闪:`act()` 在一次
|
|
165
|
+
请求期间把 `busy` 置真,面板所有按钮与开关同步落到 `:disabled`(`opacity: .5`),本地路由几十毫秒返回,
|
|
166
|
+
于是置灰只闪一两帧。现在删除 `busy` 这个概念,请求进行中不再改变任何控件的禁用态,只更新状态与底部反馈;
|
|
167
|
+
`unreachable`、`!enabled` 这类语义禁用保留。
|
|
168
|
+
- 自动化:81 项单测/接线测试全绿(纯前端改动,无新增用例)。
|
|
169
|
+
|
|
170
|
+
## 0.3.0 断网自动重试(2026-12-03)
|
|
171
|
+
|
|
172
|
+
### 实现依据(读的是当前安装的 DSH 源码,不是猜的)
|
|
173
|
+
|
|
174
|
+
- `dsh-api-session-controller/src/index.ts`:`prompt()` 对 `content.length === 0` 构造
|
|
175
|
+
`source.kind = 'retry'` 的用户消息;`commands.ts` 的空内容分支要求
|
|
176
|
+
`continuation.stateOf(session).ending !== null`、`mode === 'queue'`、inbox 无待处理人类消息,
|
|
177
|
+
否则抛 `session/agent-busy`。这就是界面「重试」按钮的后端语义。
|
|
178
|
+
- `dsh-api-session-controller/src/continuation.ts`:可续跑投影的三个条件(人类输入被接纳、
|
|
179
|
+
`assistant/attempt` 未产出成功消息、`turn/end` 错误码属于
|
|
180
|
+
`RATE_LIMIT/SERVER/TIMEOUT/TRANSPORT/EMPTY_RESPONSE/STREAM_CLOSED`)。本插件 `lib/resume.js`
|
|
181
|
+
按同样条件从 `session/event` 本地折叠,不依赖投影服务。
|
|
182
|
+
- `dsh-llm-retry`:DSH 自带 5 次退避重试覆盖 `TRANSPORT/TIMEOUT`;本功能接手「5 次仍失败」之后。
|
|
183
|
+
|
|
184
|
+
### 本轮验证过的
|
|
185
|
+
|
|
186
|
+
- 真机 TCP 探测:`probeTcp({ host: 'api.deepseek.com', port: 443 })` 返回 `true`(19ms);
|
|
187
|
+
不存在的域名与本地关闭端口均返回 `false`,说明成功/失败两条路径都真的跑通。
|
|
188
|
+
- 自动化测试 81 → **109 项全绿**,新增 28 项覆盖:失败判定状态机(成功轮、`aborted`/`blocked`/
|
|
189
|
+
`interrupted`/`max-tokens`、非网络错误码、无人类输入)、retry 消息计入「已接纳」、每故障 3 次上限、
|
|
190
|
+
用户消息重置预算、60 分钟窗口过期、拒绝后清除、`probeTcp` 三种结果、调度器(探测通过才重试、
|
|
191
|
+
探测失败不重试、拒绝态、开关关闭不探测、关闭清队列)、提问/审批提醒(两个 waterfall 事件各响一次且
|
|
192
|
+
`next()` 照常继续、声音开关关闭时不响)、三种试听音效(成功/失败/提醒的 `kind` 回包,未知 kind 400)、
|
|
193
|
+
macOS/Windows/Linux 三平台提醒音命令映射与自定义文件覆盖。
|
|
194
|
+
- 配置校验:默认值(开、`TRANSPORT/TIMEOUT`、`api.deepseek.com:443`、15s、60min、3 次)与非法值
|
|
195
|
+
钳制/回退均有测试;`state.json` 新增 `autoResume` 的读写测试。
|
|
196
|
+
- 宿主协议 3:回包 `autoResume` / `resume` 字段与 `set-auto-resume` 动作有测试;客户端字典
|
|
197
|
+
简中/繁中/英文各 54 键、键集合一致、无空值、英文无 CJK 残留;全部文件 `node --check` 通过。
|
|
198
|
+
- 提醒音依据的是宿主 Event 合同(`user-questions/request`、`approval/request` 均为 waterfall),
|
|
199
|
+
监听器旁路调用 `next()`,由接线测试证明不吞请求。
|
|
200
|
+
- 繁体中文通过 `ctx.locale.addLanguage({ id: 'zh-Hant', label: '繁體中文', fallback: 'zh' })` 贡献;
|
|
201
|
+
该接口在 DSH 客户端 locale 服务合同里存在(`addLanguage`:回退目标必须已注册、链必须终结于英文)。
|
|
202
|
+
|
|
203
|
+
### 未验证(需真机与真实故障)
|
|
204
|
+
|
|
205
|
+
| 项 | 原因 | 何时可验 |
|
|
206
|
+
|---|---|---|
|
|
207
|
+
| AC-21 合盖断网 → 唤醒恢复后自动续跑 | 需要真实合盖断网 + 宿主重启加载新代码;本会话无法模拟(也无法在本进程内重启宿主) | 用户重启 DSH 后合盖实测;观察对话中出现 retry 消息、弹层显示「已自动重试」 |
|
|
208
|
+
| AC-22 网络长期不恢复 | 同上 | 断开 Wi-Fi 后观察「等待网络恢复…」 |
|
|
209
|
+
| AC-24 连续 3 次失败后停止 | 需要稳定的失败环境 | 断网状态下等三次调度 |
|
|
210
|
+
| `sessionController.prompt` 从插件调用 | 只在真实宿主里才有该服务;已在源码层面确认服务合同与空内容分支,但未真机调用 | 用户实测 AC-21 时一并验证 |
|
|
211
|
+
| 提问/审批提醒音实际听感 | 需要宿主重启加载新代码并真实触发一次 `ask_user_question` 或审批;本进程内无法重启宿主 | 用户重启后提问一次即可确认 |
|
|
212
|
+
| 繁体中文在 DSH 语言列表中出现并可选中 | `addLanguage` 注册成功后由 DSH 的「语言」设置行渲染,本会话无浏览器控制权 | 重启后在 设置 → 语言 中查看「繁體中文」 |
|
|
213
|
+
|
|
154
214
|
## 未验证(明确记录,不代替验证)
|
|
155
215
|
|
|
156
216
|
| ID / 项 | 原因 | 何时可验 |
|
package/index.js
CHANGED
|
@@ -12,14 +12,17 @@
|
|
|
12
12
|
* the machine awake forever.
|
|
13
13
|
*/
|
|
14
14
|
|
|
15
|
+
import { randomUUID } from 'node:crypto'
|
|
15
16
|
import { readFileSync } from 'node:fs'
|
|
16
17
|
|
|
17
18
|
import { createActivity } from './lib/activity.js'
|
|
19
|
+
import { createAutoResume } from './lib/auto-resume.js'
|
|
18
20
|
import { applyState, normalizeConfig } from './lib/config.js'
|
|
19
21
|
import { createController } from './lib/controller.js'
|
|
20
22
|
import { createLog } from './lib/log.js'
|
|
21
23
|
import { backendName, verifyCommand } from './lib/platform.js'
|
|
22
24
|
import { createPower } from './lib/power.js'
|
|
25
|
+
import { createResumeTracker } from './lib/resume.js'
|
|
23
26
|
import { createStatusHandler } from './lib/server.js'
|
|
24
27
|
import { createSound } from './lib/sound.js'
|
|
25
28
|
import { defaultStatePath, loadState, saveState } from './lib/state.js'
|
|
@@ -65,7 +68,12 @@ export function start(ctx, rawConfig, deps = {}) {
|
|
|
65
68
|
|
|
66
69
|
const persist = () => {
|
|
67
70
|
if (deps.statePath === false) return
|
|
68
|
-
const result = saveState(statePath, {
|
|
71
|
+
const result = saveState(statePath, {
|
|
72
|
+
enabled: config.enabled,
|
|
73
|
+
scope: config.scope,
|
|
74
|
+
sound: config.sound,
|
|
75
|
+
autoResume: config.autoResume,
|
|
76
|
+
})
|
|
69
77
|
if (!result.ok) log.warn(`stay-awake: could not persist settings: ${result.error}`)
|
|
70
78
|
}
|
|
71
79
|
|
|
@@ -106,6 +114,62 @@ export function start(ctx, rawConfig, deps = {}) {
|
|
|
106
114
|
}
|
|
107
115
|
}
|
|
108
116
|
|
|
117
|
+
// ---- automatic retry of a turn that died with the network ----
|
|
118
|
+
const resumeTracker = createResumeTracker({
|
|
119
|
+
codes: config.autoResumeCodes,
|
|
120
|
+
maxAttempts: config.autoResumeMaxAttempts,
|
|
121
|
+
windowMs: config.autoResumeWindowMinutes * 60_000,
|
|
122
|
+
now: clockNow,
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Press DSH's own retry door: an empty prompt becomes a `retry`-source user
|
|
127
|
+
* message and re-runs the failed step, exactly like the Retry button. The
|
|
128
|
+
* host refuses it with `session/agent-busy` when the turn is no longer
|
|
129
|
+
* retryable, so a stale decision fails closed.
|
|
130
|
+
*/
|
|
131
|
+
async function retryTurn(sessionId) {
|
|
132
|
+
const sessionController = ctx.get('sessionController')
|
|
133
|
+
if (sessionController === undefined || sessionController === null
|
|
134
|
+
|| typeof sessionController.prompt !== 'function') {
|
|
135
|
+
return { ok: false, reason: 'session-controller-unavailable' }
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
await sessionController.prompt({
|
|
139
|
+
requestId: randomUUID(),
|
|
140
|
+
sessionId,
|
|
141
|
+
mode: 'queue',
|
|
142
|
+
content: [],
|
|
143
|
+
}, AbortSignal.timeout(20_000))
|
|
144
|
+
return { ok: true }
|
|
145
|
+
} catch (error) {
|
|
146
|
+
return { ok: false, reason: describe(error) }
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const autoResume = createAutoResume({
|
|
151
|
+
tracker: resumeTracker,
|
|
152
|
+
config,
|
|
153
|
+
log,
|
|
154
|
+
retry: retryTurn,
|
|
155
|
+
now: clockNow,
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
// ---- "your turn": the agent is waiting for an answer or an approval ----
|
|
159
|
+
/**
|
|
160
|
+
* These two are waterfall events, so the listener asks for a sound and then
|
|
161
|
+
* hands the request back untouched. Ringing is best effort: a missing player
|
|
162
|
+
* or a closed sound switch must never affect the question itself.
|
|
163
|
+
*/
|
|
164
|
+
const ringQuestion = () => {
|
|
165
|
+
try {
|
|
166
|
+
if (!config.enabled || !config.sound || config.soundOnQuestion !== true) return
|
|
167
|
+
sound.play('question')
|
|
168
|
+
} catch (error) {
|
|
169
|
+
log.warn(`stay-awake: question sound failed: ${describe(error)}`)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
109
173
|
const sync = () => {
|
|
110
174
|
const working = activity.hasWork(config)
|
|
111
175
|
if (working && !wasWorking) {
|
|
@@ -149,6 +213,11 @@ export function start(ctx, rawConfig, deps = {}) {
|
|
|
149
213
|
sync()
|
|
150
214
|
}),
|
|
151
215
|
ctx.on('session/event', (session, event) => {
|
|
216
|
+
// Subagent sessions are not resumed automatically: their parent owns
|
|
217
|
+
// the failure and retrying a child out of band would fork the work.
|
|
218
|
+
if (session?.header?.parentSession === undefined && session?.header?.origin !== 'subagent') {
|
|
219
|
+
resumeTracker.observe(session.id, event)
|
|
220
|
+
}
|
|
152
221
|
if (event?.type === 'user/message') {
|
|
153
222
|
pendingUserSound = true
|
|
154
223
|
return
|
|
@@ -178,6 +247,30 @@ export function start(ctx, rawConfig, deps = {}) {
|
|
|
178
247
|
}
|
|
179
248
|
}, 'stay-awake: activity sources')
|
|
180
249
|
|
|
250
|
+
ctx.effect(() => {
|
|
251
|
+
const listen = (name) => {
|
|
252
|
+
try {
|
|
253
|
+
return ctx.on(name, (_payload, next) => {
|
|
254
|
+
ringQuestion()
|
|
255
|
+
return typeof next === 'function' ? next() : undefined
|
|
256
|
+
})
|
|
257
|
+
} catch (error) {
|
|
258
|
+
log.warn(`stay-awake: could not subscribe to ${name}: ${describe(error)}`)
|
|
259
|
+
return () => {}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
const disposers = [listen('user-questions/request'), listen('approval/request')]
|
|
263
|
+
return () => {
|
|
264
|
+
for (const dispose of disposers) {
|
|
265
|
+
try {
|
|
266
|
+
dispose()
|
|
267
|
+
} catch {
|
|
268
|
+
// Unloading is best effort.
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}, 'stay-awake: question sound')
|
|
273
|
+
|
|
181
274
|
const jobs = ctx.get('jobs')
|
|
182
275
|
if (jobs !== undefined && jobs !== null && config.includeBackgroundJobs) {
|
|
183
276
|
ctx.effect(() => {
|
|
@@ -237,6 +330,18 @@ export function start(ctx, rawConfig, deps = {}) {
|
|
|
237
330
|
sync()
|
|
238
331
|
}
|
|
239
332
|
|
|
333
|
+
// The runner schedules its own probes (default every 15 s) and only acts when
|
|
334
|
+
// the tracker says a turn is retryable; the tick itself is cheap.
|
|
335
|
+
ctx.effect(() => {
|
|
336
|
+
const timer = setInterval(() => {
|
|
337
|
+
Promise.resolve(autoResume.tick()).catch((error) => {
|
|
338
|
+
log.warn(`stay-awake: auto-resume tick failed: ${describe(error)}`)
|
|
339
|
+
})
|
|
340
|
+
}, 5_000)
|
|
341
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
342
|
+
return () => clearInterval(timer)
|
|
343
|
+
}, 'stay-awake: auto-resume')
|
|
344
|
+
|
|
240
345
|
if (config.reconcileSeconds > 0) {
|
|
241
346
|
ctx.effect(() => {
|
|
242
347
|
const timer = setInterval(reconcile, config.reconcileSeconds * 1000)
|
|
@@ -255,6 +360,7 @@ export function start(ctx, rawConfig, deps = {}) {
|
|
|
255
360
|
version: VERSION,
|
|
256
361
|
backend,
|
|
257
362
|
verify: verifyCommand(platform),
|
|
363
|
+
autoResume,
|
|
258
364
|
onChange: persist,
|
|
259
365
|
})
|
|
260
366
|
scope.effect(() => {
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The automatic retry runner.
|
|
3
|
+
*
|
|
4
|
+
* One detached tick loop (wired in `index.js`) asks the tracker whether a turn
|
|
5
|
+
* is retryable, waits until the probe says the network is actually back, then
|
|
6
|
+
* presses DSH's own retry door once. Nothing here talks to the model: the retry
|
|
7
|
+
* is an empty `retry`-source user message, exactly like the Retry button.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { probeTcp } from './probe.js'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {object} options
|
|
14
|
+
* @param {ReturnType<import('./resume.js').createResumeTracker>} options.tracker
|
|
15
|
+
* @param {Record<string, any>} options.config live normalized config (mutated by the popover)
|
|
16
|
+
* @param {{ warn: (message: string) => void, info?: (message: string) => void }} options.log
|
|
17
|
+
* @param {(options: { host: string, port: number, timeoutMs: number }) => Promise<boolean>} [options.probe]
|
|
18
|
+
* @param {(sessionId: string) => Promise<{ ok: true } | { ok: false, reason: string }>} options.retry
|
|
19
|
+
* @param {() => number} [options.now]
|
|
20
|
+
*/
|
|
21
|
+
export function createAutoResume({ tracker, config, log, probe = probeTcp, retry, now = Date.now }) {
|
|
22
|
+
let inFlight = false
|
|
23
|
+
let lastProbeAt = 0
|
|
24
|
+
/** @type {'idle' | 'waiting' | 'retrying' | 'failed'} */
|
|
25
|
+
let state = 'idle'
|
|
26
|
+
let failure = null
|
|
27
|
+
|
|
28
|
+
function view() {
|
|
29
|
+
return {
|
|
30
|
+
enabled: config.autoResume === true,
|
|
31
|
+
state: config.autoResume === true ? state : 'idle',
|
|
32
|
+
failure: state === 'failed' ? failure : null,
|
|
33
|
+
retries: tracker.stats().sent,
|
|
34
|
+
lastSentAt: tracker.stats().lastSentAt,
|
|
35
|
+
probeHost: config.autoResumeProbeHost,
|
|
36
|
+
probePort: config.autoResumeProbePort,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function setEnabled(enabled) {
|
|
41
|
+
if (enabled !== true) {
|
|
42
|
+
state = 'idle'
|
|
43
|
+
failure = null
|
|
44
|
+
tracker.clear()
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function tick() {
|
|
49
|
+
if (config.autoResume !== true) {
|
|
50
|
+
state = 'idle'
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
if (tracker.pending().length === 0) {
|
|
54
|
+
if (state !== 'retrying') state = 'idle'
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
state = 'waiting'
|
|
58
|
+
if (inFlight) return
|
|
59
|
+
const at = now()
|
|
60
|
+
if (at - lastProbeAt < config.autoResumeProbeIntervalMs) return
|
|
61
|
+
lastProbeAt = at
|
|
62
|
+
inFlight = true
|
|
63
|
+
try {
|
|
64
|
+
const reachable = await probe({
|
|
65
|
+
host: config.autoResumeProbeHost,
|
|
66
|
+
port: config.autoResumeProbePort,
|
|
67
|
+
timeoutMs: 4_000,
|
|
68
|
+
})
|
|
69
|
+
if (!reachable) return
|
|
70
|
+
// Re-read after the probe: the user may have taken over meanwhile.
|
|
71
|
+
const [next] = tracker.pending()
|
|
72
|
+
if (next === undefined) return
|
|
73
|
+
tracker.recordSend(next.sessionId)
|
|
74
|
+
state = 'retrying'
|
|
75
|
+
const result = await retry(next.sessionId)
|
|
76
|
+
if (result?.ok === true) {
|
|
77
|
+
state = 'idle'
|
|
78
|
+
log.info?.(`stay-awake: retried session ${next.sessionId} after the network came back`)
|
|
79
|
+
} else {
|
|
80
|
+
failure = result?.reason ?? 'retry failed'
|
|
81
|
+
tracker.recordRejected(next.sessionId, failure)
|
|
82
|
+
state = 'failed'
|
|
83
|
+
log.warn(`stay-awake: automatic retry was rejected for ${next.sessionId}: ${failure}`)
|
|
84
|
+
}
|
|
85
|
+
} catch (error) {
|
|
86
|
+
failure = error instanceof Error ? error.message : String(error)
|
|
87
|
+
state = 'failed'
|
|
88
|
+
log.warn(`stay-awake: automatic retry failed: ${failure}`)
|
|
89
|
+
} finally {
|
|
90
|
+
inFlight = false
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return { tick, view, setEnabled }
|
|
95
|
+
}
|
package/lib/config.js
CHANGED
|
@@ -24,7 +24,18 @@ export const DEFAULTS = Object.freeze({
|
|
|
24
24
|
soundOnlyUserTasks: true,
|
|
25
25
|
successSound: '',
|
|
26
26
|
errorSound: '',
|
|
27
|
+
questionSound: '',
|
|
28
|
+
soundOnQuestion: true,
|
|
27
29
|
soundVolume: 1,
|
|
30
|
+
// "Retry the turn that died with the network": on by default, but it only
|
|
31
|
+
// fires for a lost connection, never for a user abort or an approval wait.
|
|
32
|
+
autoResume: true,
|
|
33
|
+
autoResumeCodes: Object.freeze(['TRANSPORT', 'TIMEOUT']),
|
|
34
|
+
autoResumeProbeHost: 'api.deepseek.com',
|
|
35
|
+
autoResumeProbePort: 443,
|
|
36
|
+
autoResumeProbeIntervalMs: 15_000,
|
|
37
|
+
autoResumeWindowMinutes: 60,
|
|
38
|
+
autoResumeMaxAttempts: 3,
|
|
28
39
|
})
|
|
29
40
|
|
|
30
41
|
const SCOPES = new Set(['system', 'display'])
|
|
@@ -40,6 +51,7 @@ export function applyState(config, state) {
|
|
|
40
51
|
if (typeof state?.enabled === 'boolean') next.enabled = state.enabled
|
|
41
52
|
if (SCOPES.has(state?.scope)) next.scope = state.scope
|
|
42
53
|
if (typeof state?.sound === 'boolean') next.sound = state.sound
|
|
54
|
+
if (typeof state?.autoResume === 'boolean') next.autoResume = state.autoResume
|
|
43
55
|
return next
|
|
44
56
|
}
|
|
45
57
|
|
|
@@ -62,6 +74,14 @@ function asOptionalPath(value) {
|
|
|
62
74
|
return typeof value === 'string' ? value.trim() : ''
|
|
63
75
|
}
|
|
64
76
|
|
|
77
|
+
function asCodes(value, fallback) {
|
|
78
|
+
if (!Array.isArray(value)) return [...fallback]
|
|
79
|
+
const codes = value
|
|
80
|
+
.filter((entry) => typeof entry === 'string' && entry.trim() !== '')
|
|
81
|
+
.map((entry) => entry.trim().toUpperCase())
|
|
82
|
+
return codes.length > 0 ? codes : [...fallback]
|
|
83
|
+
}
|
|
84
|
+
|
|
65
85
|
/**
|
|
66
86
|
* @param {Record<string, unknown> | undefined} raw
|
|
67
87
|
* @returns {{ config: typeof DEFAULTS, warnings: string[] }}
|
|
@@ -86,11 +106,26 @@ export function normalizeConfig(raw) {
|
|
|
86
106
|
soundOnlyUserTasks: asBoolean(input.soundOnlyUserTasks, DEFAULTS.soundOnlyUserTasks),
|
|
87
107
|
successSound: asOptionalPath(input.successSound),
|
|
88
108
|
errorSound: asOptionalPath(input.errorSound),
|
|
109
|
+
questionSound: asOptionalPath(input.questionSound),
|
|
110
|
+
soundOnQuestion: asBoolean(input.soundOnQuestion, DEFAULTS.soundOnQuestion),
|
|
89
111
|
soundVolume: asNumber(input.soundVolume, DEFAULTS.soundVolume, { min: 0, max: 3 }),
|
|
112
|
+
autoResume: asBoolean(input.autoResume, DEFAULTS.autoResume),
|
|
113
|
+
autoResumeCodes: asCodes(input.autoResumeCodes, DEFAULTS.autoResumeCodes),
|
|
114
|
+
autoResumeProbeHost: asPath(input.autoResumeProbeHost, DEFAULTS.autoResumeProbeHost),
|
|
115
|
+
autoResumeProbePort: asNumber(input.autoResumeProbePort, DEFAULTS.autoResumeProbePort, { min: 1, max: 65_535 }),
|
|
116
|
+
autoResumeProbeIntervalMs: asNumber(input.autoResumeProbeIntervalMs, DEFAULTS.autoResumeProbeIntervalMs, { min: 1_000, max: 300_000 }),
|
|
117
|
+
autoResumeWindowMinutes: asNumber(input.autoResumeWindowMinutes, DEFAULTS.autoResumeWindowMinutes, { min: 1, max: 24 * 60 }),
|
|
118
|
+
autoResumeMaxAttempts: asNumber(input.autoResumeMaxAttempts, DEFAULTS.autoResumeMaxAttempts, { min: 1, max: 10 }),
|
|
90
119
|
}
|
|
91
120
|
|
|
92
121
|
for (const key of Object.keys(config)) {
|
|
93
122
|
if (input[key] === undefined) continue
|
|
123
|
+
if (Array.isArray(config[key])) {
|
|
124
|
+
if (!Array.isArray(input[key]) || input[key].join('\u0000') !== config[key].join('\u0000')) {
|
|
125
|
+
warnings.push(`config: "${key}" has an invalid value (${JSON.stringify(input[key])}); using ${JSON.stringify(config[key])}`)
|
|
126
|
+
}
|
|
127
|
+
continue
|
|
128
|
+
}
|
|
94
129
|
if (config[key] !== input[key]) {
|
|
95
130
|
warnings.push(`config: "${key}" has an invalid value (${JSON.stringify(input[key])}); using ${JSON.stringify(config[key])}`)
|
|
96
131
|
}
|
package/lib/probe.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reachability probe used before an automatic retry.
|
|
3
|
+
*
|
|
4
|
+
* A TCP handshake is the cheapest question that actually answers "can this
|
|
5
|
+
* machine reach the provider again": it sends no request, no headers and no
|
|
6
|
+
* prompt data, and it fails fast while the Wi-Fi is still coming up. DNS alone
|
|
7
|
+
* would pass on a cached answer, and an HTTP request would look like traffic
|
|
8
|
+
* the user never asked for.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { createConnection } from 'node:net'
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {object} options
|
|
15
|
+
* @param {string} options.host
|
|
16
|
+
* @param {number} options.port
|
|
17
|
+
* @param {number} [options.timeoutMs]
|
|
18
|
+
* @param {typeof createConnection} [options.createConnection] test seam
|
|
19
|
+
* @returns {Promise<boolean>} true when the TCP handshake completed
|
|
20
|
+
*/
|
|
21
|
+
export function probeTcp({ host, port, timeoutMs = 4_000, createConnection: connect = createConnection }) {
|
|
22
|
+
return new Promise((resolve) => {
|
|
23
|
+
let settled = false
|
|
24
|
+
/** @type {import('node:net').Socket | undefined} */
|
|
25
|
+
let socket
|
|
26
|
+
|
|
27
|
+
const finish = (reachable) => {
|
|
28
|
+
if (settled) return
|
|
29
|
+
settled = true
|
|
30
|
+
try {
|
|
31
|
+
socket?.destroy()
|
|
32
|
+
} catch {
|
|
33
|
+
// A socket already torn down by the failure itself is not an error.
|
|
34
|
+
}
|
|
35
|
+
resolve(reachable)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
try {
|
|
39
|
+
socket = connect({ host, port })
|
|
40
|
+
} catch {
|
|
41
|
+
resolve(false)
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
socket.once('connect', () => finish(true))
|
|
46
|
+
socket.once('timeout', () => finish(false))
|
|
47
|
+
socket.once('error', () => finish(false))
|
|
48
|
+
if (typeof socket.setTimeout === 'function') socket.setTimeout(timeoutMs)
|
|
49
|
+
})
|
|
50
|
+
}
|