dsh-session-superagent 0.8.97 → 0.8.99

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,52 @@
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.8.99] - 2026-11-02 —— 被每小时上限挡住时,告诉你还要等多久
6
+
7
+ ### 先模拟,再看要不要改(知识库第 48 条)
8
+ 用真实的 `pruneWakeTimes` 跑 6 小时模拟(cap=6/小时):
9
+ ```
10
+ 间隔 30s -> 两次唤醒最大间隔 3480s(58 分钟)
11
+ 间隔 60s -> 3360s(56 分钟)
12
+ 间隔 120s -> 3120s(52 分钟)
13
+ ```
14
+ 滑动窗口下如果 6 次集中在开头,下一次要等近 1 小时。**这不是 bug**(限额就是限额),
15
+ 但客户看到「已攒 N 条」却不知道要等多久,很容易以为卡死。
16
+
17
+ ### 改法:不改限额,只让等待可见
18
+ `nextWaketimeAt(wakeTimes, cap, now)` 算出下一个名额空出的时刻,提示语变成:
19
+ > 每小时最多唤醒 6 次,已攒 3 条待处理(**约 43 分钟后可再唤醒**)
20
+
21
+ 边界:名额没满 → 现在;超过 1 小时的自动剔除;cap 非法按 6 兜底。
22
+
23
+ ### 测试
24
+ - 新增 1 例(4 种情况)。合计 **142 例**。
25
+
26
+ ## [0.8.98] - 2026-11-02 —— 时段外不再空打 API(一夜能省 800+ 次)
27
+
28
+ ### 学到的(知识库第 47 条)
29
+ Renovate 的 `schedule`:**不在时间窗内就不去查更新**,而不是"查完再决定"。
30
+ 通用原则:把"不做"的判断尽量前移。
31
+ <https://docs.renovatebot.com/configuration-options/#schedule>
32
+
33
+ ### 抓到的浪费
34
+ `pollOnce` 里 `activeHours` 的判断原来在**拉完 API 之后**:
35
+ ```js
36
+ const res = await run('gh', ...) // 先打一次 GitHub
37
+ if (!inActiveHours(entry.activeHours)) return // 才决定不叫醒
38
+ ```
39
+ 客户设 `09:00-18:00`,**夜里 14 小时照样每 N 秒打一次**:60 秒间隔 ≈ **一夜 840 次**
40
+ (多数是 304 不计额度,但每次都要起 `gh` 进程 + 一次网络往返),且开门后还要等下一个周期。
41
+
42
+ ### 改了什么
43
+ 1. 时段判断**移到函数最前**(拉数据之前);
44
+ 2. 用 `msUntilActiveWindow()` 算出开门时间,把 `nextAt` 设到那一刻。
45
+
46
+ 用例锁死语义:23:30 → 9.5 小时;07:00 → 2 小时;跨夜 15:00 → 7 小时;非法/空 → 0;最小 1 秒。
47
+
48
+ ### 测试
49
+ - 新增 1 例(8 种情况)。合计 **141 例**。
50
+
5
51
  ## [0.8.97] - 2026-11-02 —— 错误提示要回答"我该做什么"
6
52
 
7
53
  ### 学到的(知识库第 46 条)
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.8.97-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.8.99-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
 
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.8.97'
17
+ const CLIENT_VERSION = '0.8.99'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
package/lib/index.js CHANGED
@@ -26,12 +26,12 @@ import { decodeBody } from './body.js'
26
26
  import {
27
27
  advanceSeen, applyWatcherAction, batchFresh, decideWake, DEFAULT_ACK_LABEL, formatWatchText,
28
28
  TRIAGE_SIGNALS, accessNote, buildCommentBatchQuery, classifyGhError, commentSummaryLine, parseCommentBatch, pickSuggestion, repoStructureNote, countReasons, filterReason, hasAckLabel, jitterMs, parseGhInclude, retryAfterMs, throttleDelayMs, inActiveHours, newestUpdatedAt, nextDelayMs, pushFiltered, normalizeFilters, partitionItems, passesFilters,
29
- mergeFilters, mergePollResult, pruneSeen, pruneWakeTimes, pushHistory, recordReported, staleWorktrees, worktreePlan,
29
+ mergeFilters, mergePollResult, msUntilActiveWindow, nextWaketimeAt, pruneSeen, pruneWakeTimes, pushHistory, recordReported, staleWorktrees, worktreePlan,
30
30
  } from './watch.js'
31
31
 
32
32
  export const name = 'dsh-session-superagent'
33
33
  /** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
34
- export const SERVER_VERSION = '0.8.97'
34
+ export const SERVER_VERSION = '0.8.99'
35
35
  export const inject = ['tools', 'agents', 'webServer']
36
36
 
37
37
  /** 仅供测试:读写状态文件(生产代码不要用)。 */
@@ -728,6 +728,14 @@ async function fetchLastComments(repo, items, maxItems = 3) {
728
728
  }
729
729
 
730
730
  async function pollOnce(ctx, entry, baseMs) {
731
+ // 活跃时段要**在拉 API 之前**判断:以前放在后面,夜里照样每 N 秒打一次 GitHub,纯浪费。
732
+ // 不在时段内就把下一次轮询安排到"开门那一刻",攒着的条目不会多等一个周期。
733
+ if (entry.activeHours && !inActiveHours(entry.activeHours)) {
734
+ const wait = msUntilActiveWindow(entry.activeHours)
735
+ entry.nextAt = new Date(Date.now() + wait).toISOString()
736
+ entry.lastDeferred = `不在活跃时段(${entry.activeHours}),${Math.round(wait / 60000)} 分钟后开始`
737
+ return
738
+ }
731
739
  // 条件请求:带上上次的 ETag,没变化时 GitHub 回 304,且**不计入**速率限制
732
740
  const args = ['api', '--include', `repos/${entry.repo}/issues?state=all&sort=updated&direction=desc&per_page=20`]
733
741
  if (entry.etag) args.push('-H', 'If-None-Match: ' + entry.etag)
@@ -828,18 +836,14 @@ async function pollOnce(ctx, entry, baseMs) {
828
836
  return
829
837
  }
830
838
 
831
- // 活跃时段:夜里不叫醒,攒着(对标 Renovate 的 schedule)
832
- if (entry.activeHours && !inActiveHours(entry.activeHours)) {
833
- entry.lastDeferred = `不在活跃时段(${entry.activeHours}),已攒 ${fresh.length} 条`
834
- return
835
- }
836
-
837
839
  // 每小时唤醒上限:宁可攒着,也不要连续叫醒把上下文打爆
838
840
  const cap = Math.max(1, Number(entry.maxWakesPerHour) || 6)
839
841
  entry.wakeTimes = pruneWakeTimes(entry.wakeTimes)
840
842
  if (entry.wakeTimes.length >= cap) {
841
843
  // 只记原因,绝不动 seen/cursor —— 否则「攒着」等于「丢掉」
842
- entry.lastDeferred = `每小时最多唤醒 ${cap} 次,已攒 ${fresh.length} 条待处理`
844
+ const freeAt = nextWaketimeAt(entry.wakeTimes, cap)
845
+ const waitMin = Math.max(1, Math.round((freeAt - Date.now()) / 60000))
846
+ entry.lastDeferred = `每小时最多唤醒 ${cap} 次,已攒 ${fresh.length} 条待处理(约 ${waitMin} 分钟后可再唤醒)`
843
847
  return
844
848
  }
845
849
 
package/lib/watch.js CHANGED
@@ -484,3 +484,40 @@ export {
484
484
  stateReasonNote,
485
485
  throttleDelayMs,
486
486
  } from './gh-format.js'
487
+
488
+ /**
489
+ * 距离「活跃时段开门」还有多久(毫秒);已在时段内返回 0。
490
+ *
491
+ * 为什么要它:以前 activeHours 的判断在**拉完 API 之后**才做 ——
492
+ * 夜里 8 小时照常每 N 秒打一次 GitHub,纯属白跑。现在提前到最前面,
493
+ * 并把下一次轮询安排在"开门那一刻",既不浪费请求,也不会让攒着的条目干等一个周期。
494
+ */
495
+ export function msUntilActiveWindow(spec, now = new Date()) {
496
+ if (!spec || typeof spec !== 'string') return 0
497
+ const m = spec.match(/^(\d{1,2}):(\d{2})\s*-\s*(\d{1,2}):(\d{2})$/)
498
+ if (!m) return 0
499
+ if (inActiveHours(spec, now)) return 0
500
+ const startMin = (Number(m[1]) * 60 + Number(m[2])) % (24 * 60)
501
+ const curMin = now.getHours() * 60 + now.getMinutes()
502
+ const curSec = now.getSeconds()
503
+ let diffMin = startMin - curMin
504
+ if (diffMin <= 0) diffMin += 24 * 60
505
+ // 精确到分钟的开头,再减掉当前秒数
506
+ return Math.max(1000, diffMin * 60 * 1000 - curSec * 1000)
507
+ }
508
+
509
+ /**
510
+ * 被「每小时上限」挡住时,下一个名额什么时候空出来。
511
+ *
512
+ * 为什么要它:cap=6 时最坏要等近 1 小时(实测:滑动窗口下 6 次集中在开头几分钟,
513
+ * 之后要等最早那次过期)。这段时间里面板只说「已攒 N 条」,客户不知道要等多久,
514
+ * 容易以为卡死了。算出具体时刻,就能写「约 43 分钟后可再唤醒」。
515
+ */
516
+ export function nextWaketimeAt(wakeTimes, cap, now = Date.now()) {
517
+ const times = pruneWakeTimes(wakeTimes, now).sort((a, b) => a - b)
518
+ const n = Math.max(1, Number(cap) || 6)
519
+ if (times.length < n) return now
520
+ // 需要等最早的那 n 个里第一个过期:第 (times.length - n) 个之后的那次过期就空出一个名额
521
+ const freeing = times[times.length - n]
522
+ return freeing + 60 * 60 * 1000
523
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.8.97",
3
+ "version": "0.8.99",
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",