dsh-session-superagent 0.9.113 → 0.9.115

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,56 @@
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.115] - 2026-11-03 —— `github` 工具失败时也带「下一步」和机器可读的 kind
6
+
7
+ ### 为什么
8
+ 上一版只修了**轮询**路径(x-poll-interval / 永久错误长退避)。而 `github` 工具(任意 REST 调用)
9
+ 失败时仍然只贴原始 stderr 加一行 `[exit code: 1]`:
10
+
11
+ ```
12
+ gh: HTTP 404 Not Found
13
+ [exit code: 1]
14
+ ```
15
+
16
+ 调用它的 agent 得自己猜"是仓库名错了、还是没权限、还是登录失效"。GitHub 官方最佳实践明确要求
17
+ 「do not ignore repeated 4xx/5xx」—— 失败必须带着可执行动作回到调用方。
18
+
19
+ ### 改法
20
+ 抽出 `formatGhCall()`(`lib/gh-format.js`),`github` 工具与轮询共用同一套分类:
21
+
22
+ | 情况 | 现在返回 |
23
+ |---|---|
24
+ | 404 | `…(仓库不存在或没权限:检查 owner/name)` + `[kind: notfound]` |
25
+ | 401 | `…(gh 登录失效:跑 gh auth login…)` + `[kind: auth]` |
26
+ | 304 | `…(没有变化 —— 条件请求命中 304,这不是错误)` + `[kind: notmodified]` |
27
+ | 成功 | 只加 `[exit code: 0]`(不带 kind,保持轻量) |
28
+
29
+ ### 测试
30
+ 新增 1 例:四种情况的返回形状(含「成功时不带 kind」)。合计 **223 例**。
31
+
32
+ ## [0.9.114] - 2026-11-03 —— 按 GitHub 官方最佳实践补两条:x-poll-interval、永久错误等更久
33
+
34
+ ### 学到什么(出处)
35
+ GitHub《Best practices for using the REST API》明确两条我们没做:
36
+
37
+ 1. **`x-poll-interval`**:「If a response includes an `x-poll-interval` header, wait at least that many seconds
38
+ before you poll the same endpoint again.」
39
+ 2. **重复 404**:「Once you have confirmed that your credentials are correct, **wait much longer** before you check
40
+ again… Repeatedly requesting a missing resource wastes your rate limit and can trigger a secondary rate limit.」
41
+
42
+ 出处:<https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api>
43
+
44
+ ### 改了什么
45
+ 1. `parseGhInclude` 解析 `x-poll-interval`(修了一个坑:`Number(null) === 0` 会让"没有这个头"被当成 0);
46
+ 2. 轮询取 `max(配置间隔, x-poll-interval)` 作为下限 —— **成功路径也生效**
47
+ (此前 `nextAt` 只在失败退避时设置,成功后清空,间隔下限等于没用);
48
+ 3. 永久性错误(`notfound` / `moved`)新增 **30 分钟**最低等待(`permanentBackoffMs`),
49
+ 不再按普通指数退避反复撞不存在的仓库。
50
+
51
+ ### 测试
52
+ 新增 2 例:`x-poll-interval` 解析(有/无头)+ 间隔下限逻辑;`permanentBackoffMs` 四种 kind 的取值。
53
+ 合计 **222 例**。
54
+
5
55
  ## [0.9.113] - 2026-11-03 —— 把「不是错误的状态码」理一遍:204 也补上了
6
56
 
7
57
  ### 梳理结果
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.113-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.9.115-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
 
@@ -122,7 +122,7 @@ dsh plugin install /path/to/dsh-session-superagent
122
122
  ## 开发
123
123
 
124
124
  ```sh
125
- node --test # 220 项单测
125
+ node --test # 223 项单测
126
126
  ```
127
127
 
128
128
  ### 工具输出:文本给人和 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.113'
17
+ const CLIENT_VERSION = '0.9.115'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
package/lib/gh-format.js CHANGED
@@ -6,6 +6,28 @@
6
6
  //
7
7
  // 约定:本文件只做「解析 / 归类 / 措辞」,不发请求、不碰状态。
8
8
 
9
+ /**
10
+ * 把 `gh` 调用的结果整理成给 agent 看的一段文本。
11
+ *
12
+ * 为什么要单独抽出来:`github` 工具原来只贴原始 stderr + `[exit code: N]`,
13
+ * agent 拿到「gh: HTTP 404」还得自己猜该动什么。这里统一补上分类与下一步。
14
+ * 依据:GitHub 官方要求「do not ignore repeated 4xx/5xx」——
15
+ * 失败必须带着可执行的动作回到调用方。
16
+ * 出处:https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api
17
+ */
18
+ export function formatGhCall({ code, text, classify }) {
19
+ const body = String(text || '').trim() || '(empty)'
20
+ const failed = Number(code) !== 0
21
+ const why = failed ? classify(body) : null
22
+ // 304 是「没变化」,不是失败:别让调用方以为出错了
23
+ if (why && why.kind === 'notmodified') {
24
+ return body + String.fromCharCode(10) + why.hint + String.fromCharCode(10) + '[exit code: ' + code + '][kind: notmodified]'
25
+ }
26
+ if (failed) {
27
+ return body + String.fromCharCode(10) + why.hint + String.fromCharCode(10) + '[exit code: ' + code + '][kind: ' + why.kind + ']'
28
+ }
29
+ return body + String.fromCharCode(10) + '[exit code: ' + code + ']'
30
+ }
9
31
  export function classifyGhError(detail) {
10
32
  const text = String(detail || '')
11
33
  // 长 URL 压缩,避免一整条 API 地址占满面板
@@ -86,6 +108,8 @@ export function parseGhInclude(stdout) {
86
108
  limit: numeric('x-ratelimit-limit'),
87
109
  resetAt: resetSeconds ? new Date(resetSeconds * 1000).toISOString() : null,
88
110
  retryAfter: Number.isFinite(Number(retryAfterRaw)) ? Number(retryAfterRaw) : null,
111
+ // GitHub 用这个头告诉你「这个 endpoint 至少隔多久再问」——忽略它会白撞额度
112
+ pollInterval: (() => { const raw = header('x-poll-interval'); return raw && Number.isFinite(Number(raw)) ? Number(raw) : null })(),
89
113
  body,
90
114
  }
91
115
  }
package/lib/index.js CHANGED
@@ -25,7 +25,7 @@ import { redact } from './redact.js'
25
25
  import { decodeBody } from './body.js'
26
26
  import {
27
27
  advanceSeen, applyWatcherAction, batchFresh, decideWake, DEFAULT_ACK_LABEL, formatWatchText,
28
- TRIAGE_SIGNALS, accessNote, buildCommentBatchQuery, classifyGhError, commentSummaryLine, parseCommentBatch, pickSuggestion, repoStructureNote, countReasons, filterReason, hasAckLabel, jitterMs, parseGhInclude, retryAfterMs, throttleDelayMs, inActiveHours, newestUpdatedAt, nextDelayMs, pushAbsorbed, pushFiltered, normalizeFilters, partitionItems, passesFilters,
28
+ TRIAGE_SIGNALS, accessNote, buildCommentBatchQuery, classifyGhError, commentSummaryLine, parseCommentBatch, pickSuggestion, repoStructureNote, countReasons, filterReason, formatGhCall, hasAckLabel, jitterMs, parseGhInclude, retryAfterMs, throttleDelayMs, inActiveHours, newestUpdatedAt, nextDelayMs, permanentBackoffMs, pushAbsorbed, pushFiltered, normalizeFilters, partitionItems, passesFilters,
29
29
  shouldRunCleanup,
30
30
  bumpDailyWakes,
31
31
  busierThanUsual,
@@ -40,7 +40,7 @@ import {
40
40
 
41
41
  export const name = 'dsh-session-superagent'
42
42
  /** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
43
- export const SERVER_VERSION = '0.9.113'
43
+ export const SERVER_VERSION = '0.9.115'
44
44
  export const inject = ['tools', 'agents', 'webServer']
45
45
  /** 一条会话每小时最多被唤醒几次(跨所有守累计)。单守上限挡不住多守叠加。 */
46
46
  // 本进程启动时刻:用来区分「刚重启还没轮到」与「定时器真的丢了」
@@ -365,9 +365,15 @@ function noteEvent(entry, kind, text) {
365
365
  } catch { /* 观测失败不能影响主流程 */ }
366
366
  }
367
367
 
368
+ // 实际间隔:自己的配置与 GitHub 要求的 x-poll-interval 取大者
369
+ function effectiveIntervalMs(entry, baseMs) {
370
+ const floor = (Number(entry.minIntervalSec) || 0) * 1000
371
+ return Math.max(Number(baseMs) || 0, floor)
372
+ }
373
+
368
374
  function bumpFailure(entry, baseMs) {
369
375
  entry.consecutiveFailures = (Number(entry.consecutiveFailures) || 0) + 1
370
- entry.nextAt = new Date(Date.now() + nextDelayMs(entry, baseMs || 60000)).toISOString()
376
+ entry.nextAt = new Date(Date.now() + nextDelayMs(entry, effectiveIntervalMs(entry, baseMs || 60000))).toISOString()
371
377
  }
372
378
 
373
379
  /**
@@ -888,9 +894,21 @@ async function pollOnce(ctx, entry, baseMs) {
888
894
  const classified = classifyGhError(detail)
889
895
  setEntryError(entry, (classified.short + classified.hint).slice(0, 300))
890
896
  bumpFailure(entry, baseMs)
897
+ // 永久性错误(404 / 仓库改名)不要按普通退避重试:至少等半小时
898
+ const permanent = permanentBackoffMs(classified.kind, baseMs)
899
+ if (permanent > 0) {
900
+ entry.nextAt = new Date(Date.now() + permanent).toISOString()
901
+ entry.deferReason = classified.kind === 'moved' ? 'moved' : 'notfound'
902
+ }
891
903
  return
892
904
  }
893
905
  const parsed = parseGhInclude(res.stdout)
906
+ // GitHub 的 x-poll-interval 是硬下限:比我们的间隔大就照它等,别硬撞
907
+ if (parsed.pollInterval && parsed.pollInterval * 1000 > baseMs) {
908
+ entry.minIntervalSec = parsed.pollInterval
909
+ } else if (entry.minIntervalSec && (!parsed.pollInterval || parsed.pollInterval * 1000 <= baseMs)) {
910
+ entry.minIntervalSec = null
911
+ }
894
912
  // 额度自适应:剩余低于 500 时不再按固定间隔撞墙,直接把下一轮推到额度重置(最多 1 小时)
895
913
  const throttle = throttleDelayMs({ remaining: parsed.remaining, resetAt: parsed.resetAt })
896
914
  if (throttle > 0) {
@@ -1170,6 +1188,9 @@ export function apply(ctx) {
1170
1188
  inFlight.add(entry.id)
1171
1189
  try {
1172
1190
  await pollOnce(ctx, entry, every)
1191
+ // GitHub 要求的间隔比我们的长:把下一轮推到它之后(成功路径也生效)
1192
+ const floor = (Number(entry.minIntervalSec) || 0) * 1000
1193
+ if (floor > every) entry.nextAt = new Date(Date.now() + floor).toISOString()
1173
1194
  } catch (error) {
1174
1195
  // 未预期的异常(比如宿主上下文/服务不可用)也必须退避:
1175
1196
  // 之前只写 lastError,于是坏掉的 watcher 会按 intervalSec 无限重试(真机踩到过)。
@@ -2053,8 +2074,9 @@ const UPDATE_CACHE_MS = 10 * 60 * 1000
2053
2074
  const res = await run('gh', ['api', '-X', method, args.path, '--header', 'Accept: application/vnd.github+json'], {
2054
2075
  input: write && args.body ? args.body : undefined, env: ghEnv(),
2055
2076
  })
2056
- const text = redact([res.stdout, res.stderr].filter(Boolean).join('\n').trim())
2057
- return `${text || '(empty)'}\n[exit code: ${res.code}]`
2077
+ const text = redact([res.stdout, res.stderr].filter(Boolean).join(String.fromCharCode(10)).trim())
2078
+ // 失败要带「下一步」:只贴 gh 的原始报错,调用方还得自己猜
2079
+ return formatGhCall({ code: res.code, text, classify: classifyGhError })
2058
2080
  },
2059
2081
  }))
2060
2082
 
package/lib/watch.js CHANGED
@@ -312,6 +312,18 @@ export function batchFresh(fresh, limit = 3, priority) {
312
312
  }
313
313
 
314
314
  /** 连续失败指数退避:30s → 60 → 120 → … 上限 30 分钟;成功即复位。 */
315
+ /**
316
+ * 永久性错误(仓库不存在 / 已改名)要等多久再试。
317
+ *
318
+ * 依据 GitHub 官方最佳实践:确认凭据没问题后,「wait much longer before you check again」——
319
+ * 反复请求不存在的资源既浪费额度,还可能触发 secondary rate limit。
320
+ * 出处:https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api
321
+ */
322
+ export function permanentBackoffMs(kind, baseMs = 60000) {
323
+ const PERMANENT = new Set(['notfound', 'moved'])
324
+ if (!PERMANENT.has(kind)) return 0
325
+ return Math.max(30 * 60 * 1000, Number(baseMs) || 0)
326
+ }
315
327
  export function nextDelayMs(entry, baseMs) {
316
328
  const failures = Number((entry && entry.consecutiveFailures) || 0)
317
329
  if (failures <= 0) return baseMs
@@ -525,6 +537,7 @@ export {
525
537
  classifyGhError,
526
538
  commentSummaryLine,
527
539
  countReasons,
540
+ formatGhCall,
528
541
  jitterMs,
529
542
  linkedPrNote,
530
543
  parseCommentBatch,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.9.113",
3
+ "version": "0.9.115",
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",