dsh-session-superagent 0.9.195 → 0.9.196

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 CHANGED
@@ -2,6 +2,29 @@
2
2
 
3
3
  格式参照 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),版本号遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
4
4
 
5
+ ## [0.9.196] - 2026-11-05 —— watch 审计「值得改」三条:定时器诈尸、字符串暂停、坏数组
6
+
7
+ ### W1 · 首轮的错峰定时器不能取消
8
+ `startWatcher` 为了错峰开了个一次性 `setTimeout`,但**没登记** —— `stopWatcher` 取消不掉它。
9
+ 于是删掉/暂停的守还会「诈尸」轮询一轮。现在登记进 `bootTimers`,stop 与 teardown 都会清。
10
+
11
+ ### W2 · `enabled: "false"` 字符串不会暂停
12
+ `watchers.json` 是**人也会手改**的文件。全仓 11 处 `entry.enabled === false` 都只认真布尔,
13
+ 写成字符串 `"false"` 或数字 `0` 时守照常轮询。统一走 `isEnabled()`:缺省 = 启用,
14
+ `false` / `0` / `"false"` / `"0"` / `"no"` / `"off"` 都算暂停。
15
+
16
+ ### W7 · `partitionItems` 收到非数组会抛
17
+ `items ?? []` 只防 null。手写坏的状态或上游返回对象时 `for-of` 抛 `TypeError`,
18
+ 而 tick 的 `catch` 会把它当成「这一轮失败」→ 该守**永久退避**。非数组一律当空。
19
+
20
+ ### W5 · 暂缓(记录理由)
21
+ 「teardown 只清本实例的定时器」改完会让 routes 注册路径整片挂掉 —— 风险/收益不划算。
22
+ 先按原样全清,等能拆得更小时再做。**这条不进「已修」清单。**
23
+
24
+ ### 证据
25
+ ```
26
+ node --test → 336/336(新增:非数组 partitionItems、enabled 字符串/0 两条)
27
+ ```
5
28
  ## [0.9.195] - 2026-11-05 —— watch 并发审计:4 条必须修(全部真跑出来)
6
29
 
7
30
  ### M1 · 白名单还漏 13 个字段
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  <img src="assets/icon-180.png" width="88" height="88" alt="">
4
4
 
5
5
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
6
- [![Version](https://img.shields.io/badge/version-0.9.195-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.9.196-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
7
7
  [![npm](https://img.shields.io/npm/v/dsh-session-superagent.svg)](https://www.npmjs.com/package/dsh-session-superagent)
8
8
  [![dsh](https://img.shields.io/badge/DeepSeek%20Harness-plugin-orange.svg)](https://github.com/topics/dsh-plugin)
9
9
 
@@ -144,7 +144,7 @@ Gitee 私有仓库要在「设置 → 凭证」填私人令牌;公开仓库不
144
144
  ## 开发
145
145
 
146
146
  ```sh
147
- node --test # 334 项单测
147
+ node --test # 336 项单测
148
148
  ```
149
149
 
150
150
  ### 工具输出:文本给人和 agent,JSON 给程序
package/client.js CHANGED
@@ -14,7 +14,7 @@ window.__ModuleLoader__.load({
14
14
  const { useCallback, useEffect, useRef, useState } = React
15
15
 
16
16
  // 客户端版本探针:每次模块加载写一次,用来确认浏览器里跑的是哪一版
17
- const CLIENT_VERSION = '0.9.195'
17
+ const CLIENT_VERSION = '0.9.196'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
package/lib/index.js CHANGED
@@ -42,7 +42,7 @@ import {
42
42
 
43
43
  export const name = 'dsh-session-superagent'
44
44
  /** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
45
- export const SERVER_VERSION = '0.9.195'
45
+ export const SERVER_VERSION = '0.9.196'
46
46
  export const inject = ['tools', 'agents', 'webServer']
47
47
  /** 一条会话每小时最多被唤醒几次(跨所有守累计)。单守上限挡不住多守叠加。 */
48
48
  // 本进程启动时刻:用来区分「刚重启还没轮到」与「定时器真的丢了」
@@ -161,8 +161,23 @@ function normalizeAckLabel(entry) {
161
161
  return entry
162
162
  }
163
163
 
164
+ /**
165
+ * 这条守是不是启用着。
166
+ *
167
+ * `watchers.json` 是**人也会手改**的文件(真机踩过:`enabled: "false"` 字符串让守照常轮询)。
168
+ * 所以不能只判 `=== false`:字符串 "false"、数字 0、"0" 都算暂停,缺省才算启用。
169
+ */
170
+ function isEnabled(entry) {
171
+ if (!entry) return false
172
+ const value = entry.enabled
173
+ if (value === undefined || value === null) return true
174
+ if (value === false || value === 0) return false
175
+ const text = String(value).trim().toLowerCase()
176
+ return !(text === 'false' || text === '0' || text === 'no' || text === 'off')
177
+ }
178
+
164
179
  function normalizePaused(entry) {
165
- if (!entry || entry.enabled !== false) return entry
180
+ if (!entry || isEnabled(entry)) return entry
166
181
  if (entry.lastError || entry.lastErrorAt || entry.consecutiveFailures) {
167
182
  entry.lastError = null
168
183
  entry.lastErrorAt = null
@@ -1318,7 +1333,7 @@ async function pollWithItems(ctx, entry, baseMs, parsed) {
1318
1333
  const { listed, rest } = splitBatch(fresh, Number(entry.batchLimit) || 3, entry.priorityOrder || entry.priority)
1319
1334
  // 暂停/删除可能发生在这一轮轮询期间:以盘上为准
1320
1335
  const liveEntry = (readState().watchers || {})[entry.id]
1321
- if (!liveEntry || liveEntry.enabled === false) {
1336
+ if (!liveEntry || !isEnabled(liveEntry)) {
1322
1337
  entry.lastDeferred = '本轮期间被暂停或删除,跳过唤醒'
1323
1338
  return
1324
1339
  }
@@ -1458,6 +1473,10 @@ export function apply(ctx) {
1458
1473
  return
1459
1474
  }
1460
1475
  const timers = sharedTimers
1476
+ // 一次性定时器(首轮错峰):**每实例一份** —— teardown 只清自己的,不碰别人的(审核 W5)
1477
+ const bootTimers = new Map()
1478
+
1479
+
1461
1480
  // 并发闸的唯一事实源:tick 和 HTTP「立刻」拿到的是两个不同的 entry 对象,
1462
1481
  // 靠 entry.polling 标记永远对不上(审核专员 probe-c 实证:同一条被唤醒 3 次)。
1463
1482
  // 跨模块实例共享:旧实例还没跑完的那轮,新实例不能同时再跑一遍。
@@ -1505,7 +1524,7 @@ export function apply(ctx) {
1505
1524
  const tick = async () => {
1506
1525
  // A pause (or delete) can land between two ticks; never poll a watcher
1507
1526
  // the operator has just switched off.
1508
- if (entry.enabled === false) return
1527
+ if (!isEnabled(entry)) return
1509
1528
  // 连续失败后的退避窗口:到点之前一律不碰 API
1510
1529
  if (entry.nextAt && Date.now() < Date.parse(entry.nextAt)) return
1511
1530
  // 同一个 watcher 不允许两轮并跑(手动「立刻」+ 定时器撞一起会开两个隔离区)
@@ -1528,12 +1547,16 @@ export function apply(ctx) {
1528
1547
  }
1529
1548
  // unref:宿主本来不会退出,但测试进程不该被这个定时器吊住
1530
1549
  timers.set(entry.id, setInterval(tick, every).unref?.())
1550
+
1531
1551
  // DSH_SUPERAGENT_NO_POLL=1 只登记不轮询(路由层单测用,避免真去请求 GitHub)
1532
1552
  if (process.env.DSH_SUPERAGENT_NO_POLL === '1') return
1533
1553
  // 首轮按确定性抖动错峰(0~5 秒),之后按 every 稳定推进
1534
1554
  if (firstDelay > 0) {
1535
- const boot = setTimeout(() => { tick() }, firstDelay)
1555
+ // 这个一次性定时器**也要能取消**:以前只 unref 不登记,stop 之后它还会 fire 一次
1556
+ // (审核 W1)—— 删掉/暂停的守会「诈尸」轮询一轮。
1557
+ const boot = setTimeout(() => { bootTimers.delete(entry.id); tick() }, firstDelay)
1536
1558
  boot.unref?.()
1559
+ bootTimers.set(entry.id, boot)
1537
1560
  } else {
1538
1561
  tick()
1539
1562
  }
@@ -1542,6 +1565,9 @@ export function apply(ctx) {
1542
1565
  // Must run before the entry leaves the state file: a live tick would
1543
1566
  // otherwise `persistEntry` the just-deleted watcher straight back.
1544
1567
  const stopWatcher = (id) => {
1568
+ // 首轮的错峰定时器也要取消,否则删掉的守还会 tick 一次(W1)
1569
+ const pending = bootTimers.get(id)
1570
+ if (pending) { clearTimeout(pending); bootTimers.delete(id) }
1545
1571
  const timer = timers.get(id)
1546
1572
  if (timer === undefined) return false
1547
1573
  clearInterval(timer)
@@ -1597,8 +1623,12 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1597
1623
  if (!httpServer || typeof httpServer.register !== 'function') return
1598
1624
  for (const dispose of activeRoutes) { try { dispose() } catch {} }
1599
1625
  activeRoutes.length = 0
1626
+ // W5(旧实例晚到 dispose 会掐死新实例)**暂缓**:改成「只清自己的」会让 routes 注册路径整片挂掉,
1627
+ // 风险/收益不划算。先按原样全清 —— 等能拆得更小时再做。
1600
1628
  for (const handle of timers.values()) { try { clearInterval(handle) } catch {} }
1601
1629
  timers.clear()
1630
+ for (const handle of bootTimers.values()) { try { clearTimeout(handle) } catch { /* 已停 */ } }
1631
+ bootTimers.clear()
1602
1632
  const routes = []
1603
1633
  // 宿主热重载时旧实例的路由可能还没 dispose;一条重复会让整个 try 崩掉,
1604
1634
  // 面板和所有接口一起 404(2026-11-02 真机踩到 webserver: duplicate exact route)。
@@ -1645,7 +1675,7 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1645
1675
  provider: w.provider === 'gitee' ? 'gitee' : 'github',
1646
1676
  sessionId: w.sessionId,
1647
1677
  intervalSec: w.intervalSec,
1648
- enabled: w.enabled !== false,
1678
+ enabled: isEnabled(w),
1649
1679
  cursor: w.cursor ?? null,
1650
1680
  lastPoll: w.lastPoll ?? null,
1651
1681
  lastWake: w.lastWake ?? null,
@@ -1791,7 +1821,7 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1791
1821
  if (action === 'poll') {
1792
1822
  const entry = state.watchers && state.watchers[id]
1793
1823
  if (!entry) return json(res, 404, { error: 'unknown watcher: ' + id })
1794
- if (entry.enabled === false) return json(res, 409, { error: 'watcher is paused' })
1824
+ if (!isEnabled(entry)) return json(res, 409, { error: 'watcher is paused' })
1795
1825
  if (inFlight.has(id)) return json(res, 409, { error: 'already polling', id })
1796
1826
  const every = Math.max(30, Number(entry.intervalSec) || 60) * 1000
1797
1827
  inFlight.add(id)
@@ -1806,9 +1836,10 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1806
1836
  // 立墓碑:否则一个持有旧快照的写入会把这条又"复活"
1807
1837
  state.deleted = { ...(state.deleted || {}), [id]: new Date().toISOString() }
1808
1838
  }
1809
- if (result.deleted || result.enabled === false) stopWatcher(id)
1839
+ if (result.deleted || !isEnabled(result)) stopWatcher(id)
1810
1840
  writeState(state)
1811
- if (result.enabled === true) startWatcher(state.watchers[id])
1841
+ // 只有「显式启用」才重启轮询:result 可能是部分字段,缺 enabled 时不该把守拉起来
1842
+ if ((result.enabled === true || String(result.enabled) === 'true') && state.watchers[id]) startWatcher(state.watchers[id])
1812
1843
  return json(res, 200, result)
1813
1844
  }
1814
1845
  // 认三种写法:`owner/name` / `gitee:owner/name` / `https://gitee.com/owner/name`。
@@ -1879,7 +1910,7 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1879
1910
  entry.consecutiveFailures = 0
1880
1911
  entry.nextAt = null
1881
1912
  // 没传 enabled 就保持原值:只改间隔/标签不该把暂停的 watcher 悄悄恢复
1882
- entry.enabled = body.enabled === undefined ? (entry.enabled !== false) : body.enabled !== false
1913
+ entry.enabled = body.enabled === undefined ? isEnabled(entry) : isEnabled({ enabled: body.enabled })
1883
1914
  // 暂停/恢复时清掉「上一次的错误」:暂停后不再轮询,旧错误会一直挂在面板上变成假异常
1884
1915
  if (body.enabled === false || body.enabled === true) {
1885
1916
  entry.lastError = null
@@ -1976,7 +2007,7 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
1976
2007
  lines.push('值守: ' + entries.length + ' 条')
1977
2008
  for (const entry of entries) {
1978
2009
  lines.push('- ' + entry.repo
1979
- + ' | ' + (entry.enabled === false ? '已暂停' : '启用')
2010
+ + ' | ' + (isEnabled(entry) ? '启用' : '已暂停')
1980
2011
  + ' | 间隔 ' + entry.intervalSec + 's'
1981
2012
  + ' | 会话 ' + String(entry.sessionId || '').slice(0, 20)
1982
2013
  + ' | 上次轮询 ' + String(entry.lastPoll || '-')
@@ -2461,7 +2492,7 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
2461
2492
  }
2462
2493
 
2463
2494
  // 5. 轮询停滞:定时器丢了或进程被挂起时,面板上一切「正常」,其实早就不看了
2464
- if (entry.enabled !== false && entry.lastPoll) {
2495
+ if (isEnabled(entry) && entry.lastPoll) {
2465
2496
  const intervalMs = (Number(entry.intervalSec) || 60) * 1000
2466
2497
  const staleMs = Math.max(intervalMs * 3, 5 * 60 * 1000)
2467
2498
  const age = Date.now() - Date.parse(entry.lastPoll)
@@ -2480,7 +2511,7 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
2480
2511
 
2481
2512
  // 6. 最近一次状态
2482
2513
  const last = entry.lastError ? `错误:${entry.lastError}` : entry.lastDeferred ? `等待:${entry.lastDeferred}` : '正常'
2483
- lines.push(`INFO ${entry.enabled === false ? '已暂停' : '运行中'} · 上次轮询 ${entry.lastPoll || '无'} · ${last}`)
2514
+ lines.push(`INFO ${isEnabled(entry) ? '运行中' : '已暂停'} · 上次轮询 ${entry.lastPoll || '无'} · ${last}`)
2484
2515
  }
2485
2516
  // format=json 时从同一份文本行解析出结构 —— 单一来源,不会出现两套逻辑走偏
2486
2517
  if (args && args.format === 'json') {
package/lib/watch.js CHANGED
@@ -27,6 +27,9 @@ import { repoBadges } from './gh-format.js'
27
27
  */
28
28
  export function partitionItems(items, state = {}, options = {}) {
29
29
  const { now = Date.now(), cooldownMs = 0 } = options
30
+ // `items ?? []` 只防 null:手写坏的状态或上游返回对象时,for-of 会抛 TypeError,
31
+ // 而 tick 的 catch 会把它当成「这一轮失败」→ 该守永久退避。非数组一律当空。
32
+ if (!Array.isArray(items)) return { fresh: [], absorbed: [] }
30
33
  const seen = state.seen ?? {}
31
34
  const reported = state.lastReported ?? {}
32
35
  const floor = state.cursor ? Date.parse(state.cursor) : 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.9.195",
3
+ "version": "0.9.196",
4
4
  "description": "Session-level superagent for DeepSeek Harness: watch a GitHub repository and wake a session when something needs doing. Credentials never reach the model.",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",