dsh-session-superagent 0.8.51 → 0.8.53

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,48 @@
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.53] - 2026-11-02 —— 按剩余额度自适应降频(别把 5000/小时打光)
6
+
7
+ ### 算一笔账
8
+ core 额度 **5000 次/小时**。客户挂 10 条守、间隔 60s → **600 次/小时**,一天 14,400 次。
9
+ 之前的实现会一直按间隔撞墙,然后每条守开始报 403 —— 客户看到的是"突然全挂"。
10
+
11
+ ### 改了什么
12
+ GitHub 每个响应都带 `X-Ratelimit-Remaining` / `X-Ratelimit-Reset`(本次实测头值:
13
+ `Limit: 5000 / Remaining: 4771 / Reset: 1793608466`)。现在每轮读它们:
14
+
15
+ - 剩余 **< 500** → 把下一轮直接推到额度重置时刻(+1 秒缓冲,最多等 1 小时);
16
+ - 同时在 `lastDeferred` 写明「额度紧张(剩余 N),已推迟到 HH:MM」—— **面板上看得见**,不是静默变慢。
17
+
18
+ ### 测试
19
+ - 新增 2 例:真实头格式解析(含 limit/remaining/resetAt)、throttle 五种边界
20
+ (充足不推迟 / 没头不推迟 / 推到重置 / 超 1 小时封顶 / 重置已过)。合计 **107 例**。
21
+ - **发现一个并发下的偶发红**(约 1/5):并发跑整套时偶有 1 例失败,串行跑 3 次全绿。
22
+ 各个测试文件单独跑 4~5 次都不复现,且四个"没设 `DSH_SUPERAGENT_STATE`"的文件也都不 import `lib/index.js`,
23
+ 所以不是写真实状态导致的。下一轮优先定位(会按文件两两组合来缩小范围)。
24
+
25
+ ## [0.8.52] - 2026-11-02 —— 首轮抖动:多条守不再同一毫秒一起发车
26
+
27
+ ### 学到的一条(知识库第 2 条)
28
+ AWS《Exponential Backoff And Jitter》:固定周期的客户端会同步成 thundering herd,加抖动摊平峰值。
29
+ <https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/>
30
+
31
+ 我们的 `inFlight` 闸是**按 watcher**的,挡不住「N 条守在同一毫秒一起打 gh api」。
32
+
33
+ ### 改了什么
34
+ `jitterMs(entry.id, min(5000, 间隔×10%))` 用 **id 哈希**算偏移(可复现,不是随机数),
35
+ 只延迟**首轮**,之后按 `every` 稳定推进。
36
+
37
+ ### 顺带做了三组实测(都写进知识库)
38
+ | 测什么 | 结果 |
39
+ |---|---|
40
+ | 单条 watcher 满载状态 | **21.8 KB**(seen 500 + history 20 + filteredLog 5),10 条 ≈ 218 KB,有上限 |
41
+ | `gh api` vs `gh api --include` | 稳态几乎同速(~500-650 ms)—— 0.8.51 的 `--include` 没有额外开销 |
42
+ | 抖动分布 | 20 条守 → 17 个不同偏移(用例里要求 >8) |
43
+
44
+ ### 测试
45
+ - 合计 **105 例**;UI 体检 11/11。
46
+
5
47
  ## [0.8.51] - 2026-11-02 —— 条件请求:没变化不计入速率限制
6
48
 
7
49
  ### 学到了什么(新知识库 `docs/kb/` 的第一条)
package/MAINTENANCE.md CHANGED
@@ -9,7 +9,7 @@
9
9
 
10
10
  ## 二、当前状态(2026-11-01)
11
11
 
12
- - [x] 七个工具可用(github / repo_here / watch_start / watch_status / watch_stop / watch_ack / watch_doctor),`node --test` 104/104 通过
12
+ - [x] 七个工具可用(github / repo_here / watch_start / watch_status / watch_stop / watch_ack / watch_doctor),`node --test` 107/107 通过
13
13
  - [x] 自回声抑制(逐条水位)
14
14
  - [x] 仅空闲注入(避免草稿回放)
15
15
  - [x] 唤醒失败不推进水位
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.51-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.8.53-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
 
@@ -99,7 +99,7 @@ dsh plugin install /path/to/dsh-session-superagent
99
99
  ## 开发
100
100
 
101
101
  ```sh
102
- node --test # 104 项单测
102
+ node --test # 107 项单测
103
103
  ```
104
104
 
105
105
  版本与发布流程见 [UPDATING.md](UPDATING.md),交付门见 [CHECKLIST.md](CHECKLIST.md),接手台账见 [MAINTENANCE.md](MAINTENANCE.md)。
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.51'
17
+ const CLIENT_VERSION = '0.8.53'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
package/lib/index.js CHANGED
@@ -24,13 +24,13 @@ import { redact } from './redact.js'
24
24
  import { decodeBody } from './body.js'
25
25
  import {
26
26
  advanceSeen, applyWatcherAction, batchFresh, decideWake, DEFAULT_ACK_LABEL, formatWatchText,
27
- classifyGhError, filterReason, hasAckLabel, parseGhInclude, inActiveHours, newestUpdatedAt, nextDelayMs, pushFiltered, normalizeFilters, partitionItems, passesFilters,
27
+ classifyGhError, filterReason, hasAckLabel, jitterMs, parseGhInclude, throttleDelayMs, inActiveHours, newestUpdatedAt, nextDelayMs, pushFiltered, normalizeFilters, partitionItems, passesFilters,
28
28
  mergeFilters, mergePollResult, pruneSeen, pruneWakeTimes, pushHistory, recordReported, staleWorktrees, worktreePlan,
29
29
  } from './watch.js'
30
30
 
31
31
  export const name = 'dsh-session-superagent'
32
32
  /** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
33
- export const SERVER_VERSION = '0.8.51'
33
+ export const SERVER_VERSION = '0.8.53'
34
34
  export const inject = ['tools', 'agents', 'webServer']
35
35
 
36
36
  /** 仅供测试:读写状态文件(生产代码不要用)。 */
@@ -451,6 +451,15 @@ async function pollOnce(ctx, entry, baseMs) {
451
451
  return
452
452
  }
453
453
  const parsed = parseGhInclude(res.stdout)
454
+ // 额度自适应:剩余低于 500 时不再按固定间隔撞墙,直接把下一轮推到额度重置(最多 1 小时)
455
+ const throttle = throttleDelayMs({ remaining: parsed.remaining, resetAt: parsed.resetAt })
456
+ if (throttle > 0) {
457
+ entry.nextAt = new Date(Date.now() + throttle).toISOString()
458
+ entry.lastDeferred = '额度紧张(剩余 ' + parsed.remaining + '),已推迟到 '
459
+ + new Date(Date.now() + throttle).toISOString().slice(11, 16) + 'Z'
460
+ entry.lastError = null
461
+ return
462
+ }
454
463
  if (parsed.status === 304) {
455
464
  // 没变化:不解析、不算失败、不重置退避,只记一笔
456
465
  entry.lastError = null
@@ -601,6 +610,8 @@ export function apply(ctx) {
601
610
  const startWatcher = (entry) => {
602
611
  if (timers.has(entry.id)) return
603
612
  const every = Math.max(30, Number(entry.intervalSec) || 60) * 1000
613
+ // 首轮错峰:多条守同间隔时不要整点一起发车(之后按 every 稳定推进,不累积漂移)
614
+ const firstDelay = jitterMs(entry.id, Math.min(5000, every * 0.1))
604
615
 
605
616
  const tick = async () => {
606
617
  // A pause (or delete) can land between two ticks; never poll a watcher
@@ -627,7 +638,13 @@ export function apply(ctx) {
627
638
  timers.set(entry.id, setInterval(tick, every).unref?.())
628
639
  // DSH_SUPERAGENT_NO_POLL=1 只登记不轮询(路由层单测用,避免真去请求 GitHub)
629
640
  if (process.env.DSH_SUPERAGENT_NO_POLL === '1') return
630
- tick()
641
+ // 首轮按确定性抖动错峰(0~5 秒),之后按 every 稳定推进
642
+ if (firstDelay > 0) {
643
+ const boot = setTimeout(() => { tick() }, firstDelay)
644
+ boot.unref?.()
645
+ } else {
646
+ tick()
647
+ }
631
648
  }
632
649
 
633
650
  // Must run before the entry leaves the state file: a live tick would
package/lib/watch.js CHANGED
@@ -436,12 +436,62 @@ export function parseGhInclude(stdout) {
436
436
  const head = idx < 0 ? text : text.slice(0, idx)
437
437
  const body = idx < 0 ? text.trim() : text.slice(idx + blank.length)
438
438
  const lines = head.split(LF)
439
- // 状态行形如 `HTTP/2.0 200 OK`
440
439
  const statusParts = String(lines[0] || '').split(' ')
441
440
  const status = Number(statusParts[1]) || 0
442
- let etag = null
443
- for (const line of lines) {
444
- if (line.toLowerCase().startsWith('etag:')) etag = line.slice(5).trim()
441
+ const header = (name) => {
442
+ const needle = name.toLowerCase() + ':'
443
+ for (const line of lines) {
444
+ if (line.toLowerCase().startsWith(needle)) return line.slice(needle.length).trim()
445
+ }
446
+ return null
445
447
  }
446
- return { status, etag, body }
448
+ const numeric = (name) => {
449
+ const raw = header(name)
450
+ const value = Number(raw)
451
+ return Number.isFinite(value) ? value : null
452
+ }
453
+ const resetSeconds = numeric('x-ratelimit-reset')
454
+ return {
455
+ status,
456
+ etag: header('etag'),
457
+ remaining: numeric('x-ratelimit-remaining'),
458
+ limit: numeric('x-ratelimit-limit'),
459
+ resetAt: resetSeconds ? new Date(resetSeconds * 1000).toISOString() : null,
460
+ body,
461
+ }
462
+ }
463
+
464
+ /**
465
+ * 额度自适应:GitHub 主额度快用完时,不要继续按固定间隔撞墙。
466
+ *
467
+ * 依据:GitHub 每次响应都带 `X-RateLimit-Remaining` / `X-RateLimit-Reset`
468
+ * (实测 `gh api --include` 的输出里就是这几个头)。
469
+ * 剩余低于阈值时,把下一次轮询推到额度重置时间(最多等 1 小时),而不是继续发请求。
470
+ */
471
+ export function throttleDelayMs({ remaining, resetAt, now = Date.now(), threshold = 500, capMs = 3600 * 1000 } = {}) {
472
+ if (remaining === null || remaining === undefined) return 0
473
+ if (Number(remaining) >= Number(threshold)) return 0
474
+ const reset = Date.parse(resetAt || '')
475
+ if (!Number.isFinite(reset)) return Math.min(capMs, 5 * 60 * 1000)
476
+ const wait = reset - now + 1000
477
+ if (wait <= 0) return 0
478
+ return Math.min(capMs, wait)
479
+ }
480
+
481
+ /**
482
+ * 去同步抖动:给每个 watcher 一个**确定性的**起始偏移,避免 N 条守在同一条时间线上一起发车。
483
+ *
484
+ * 为什么不用随机数:随机数每次启动都不同,调试时对不上;用 id 的哈希则「同一条守永远同一个偏移」,
485
+ * 既错峰又可复现。范围固定在 [0, windowMs)。
486
+ *
487
+ * 依据:AWS Architecture Blog 的《Exponential Backoff And Jitter》——同步发车会让
488
+ * 下游在整点承压,加入抖动后峰值被摊平。(https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/)
489
+ */
490
+ export function jitterMs(key, windowMs) {
491
+ const width = Math.max(0, Math.floor(Number(windowMs) || 0))
492
+ if (width === 0) return 0
493
+ const text = String(key || '')
494
+ let hash = 0
495
+ for (let i = 0; i < text.length; i += 1) hash = (hash * 31 + text.charCodeAt(i)) % 1000003
496
+ return hash % width
447
497
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.8.51",
3
+ "version": "0.8.53",
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",