dsh-session-superagent 0.9.207 → 0.9.209

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,85 @@
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.209] - 2026-11-05 —— 把「最会丢消息」的那段抽成纯函数并钉上测试
6
+
7
+ ### ① 那段代码恰好是 171 行、0 覆盖里最危险的 40 行
8
+
9
+ T20 的覆盖审计指出 `pollWithItems()` **171 行、0 次调用**。里面最要命的是**唤醒结算**:
10
+
11
+ ```js
12
+ // 会话忙时如果先把 seen 推进、ETag 也提交 ——
13
+ // 下一轮拿到 304 直接 return,那条 PR 评论**永远不会再叫**,而面板还写「运行中」。
14
+ ```
15
+
16
+ 这段以前是内联的闭包,外面调不到、测不了。现在抽成**纯函数** `planWakeSettlement(entry, result)`:
17
+ 给定「这次唤醒成没成」,返回**要写回的字段**(不改入参),调用方只负责落副作用。
18
+
19
+ 关键返回值是两道开关:
20
+
21
+ - `advanceWatermark` —— 只有**真正发出去了**才为 `true`;
22
+ - `commitEtag` —— 同上。失败分支返回 `false`,水位与 ETag 都不动,下一轮重试同一批。
23
+
24
+ ### ② 三条用例钉死行为
25
+
26
+ ```
27
+ 会话忙时不许推进水位(否则下一轮 304,那条评论永远不再叫)
28
+ 真正发出去之后才推进水位,且记账(入队即记账会丢消息)
29
+ 失败的唤醒也要进 history(客户能看到「叫过但没叫醒」)
30
+ ```
31
+
32
+ ### ③ 顺带
33
+
34
+ - 失败分支的「会话忙」不走退避(暂时的,退避只会越拖越久),其余原因才 `bumpFailure`;
35
+ - `planWakeSettlement` 里同时算好 `ackedRecovered`,避免调用方重复算一遍 `silentStreak`。
36
+
37
+ ### 证据
38
+
39
+ ```
40
+ node --test → 363/363(新增 3 条结算用例)
41
+ ui-audit → 11/11(100 分)
42
+ ```
43
+
44
+ ## [0.9.208] - 2026-11-05 —— 覆盖审计顺手挖出一个真 bug:`deferReason` 被拼成 `quota09:30Z`
45
+
46
+ ### ① 真 bug:额度文案拼接错位
47
+
48
+ ```js
49
+ entry.deferReason = 'quota'
50
+ + new Date(Date.now() + throttle).toISOString().slice(11, 16) + 'Z' // → 'quota09:30Z'
51
+ ```
52
+
53
+ `deferReason` 是**机器可读的稳定原因**(面板按它查 `DEFER_LABELS`、打标签、筛选),掺进时间就永远查不到,
54
+ **面板「按暂停原因筛选」永远是空列表**。
55
+
56
+ 改法不是就地改字符串,而是把文案抽成**纯函数** `quotaDeferText()` 并加单测钉住:
57
+ `reason` 必须**恰好**是已知的稳定值、不许含 `:`;时间只能出现在人读的那句话里。
58
+ T20 是把 bug 跑出来的(`actual: 'quota00:01Z'`),现在这条用例进套件了。
59
+
60
+ ### ② 清掉一处空三元
61
+
62
+ `bumpFailure(entry, diag && diag.kind === 'auth' ? baseMs : baseMs)` —— 两个分支同一个值。
63
+ 写它的人本来想区分什么,但没写出来;删掉,行为不变。
64
+
65
+ ### ③ 补两条覆盖(T20 报的「必须补」里最便宜的两条)
66
+
67
+ - **凭证写入校验**:`sshKeyPath` / `ghConfigDir` 必须绝对路径 —— 以前只有**一个 grep 源码**的测试在守
68
+ (源码里有那句字符串就绿),现在真打 HTTP 看 400。
69
+ - **凭证清空仍然合法**(三个字段都传空串)。
70
+
71
+ ### ④ 覆盖审计本身的方法论(记录)
72
+
73
+ 官方 `--experimental-test-coverage` 对 `lib/index.js` **不可信**:5 个测试文件、33 处用
74
+ `import('../lib/index.js?xxx=…')` 重复加载同一模块,报告器只保留其中一个实例 ——
75
+ 同一条命令连跑两次给 24.14% 和 11.03%,只跑单个文件反而报 28.03%(统计上不可能)。
76
+ 真实值是 **59.17% 行 / 67.9% 函数**(V8 原始区间合并)。洞集中在「必须真起进程 / 真等定时器 / 真写盘」那一层。
77
+
78
+ ### 证据
79
+
80
+ ```
81
+ node --test → 360/360(新增:额度文案回归、凭证校验两条)
82
+ ```
83
+
5
84
  ## [0.9.207] - 2026-11-05 —— 路由安全审计剩下的「值得改」:无上限、无形状、无节流
6
85
 
7
86
  ### ① 守数量没有任何上限(HTTP 路径)
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.207-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.9.209-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 # 358 项单测
147
+ node --test # 363 项单测
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.207'
17
+ const CLIENT_VERSION = '0.9.209'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
package/lib/gh-format.js CHANGED
@@ -577,3 +577,22 @@ export function linkedPrNote(linked) {
577
577
  return '已有 PR #' + first.number + (stateText || draftText || blocked ? '(' + stateText + draftText + blocked + ')' : '')
578
578
  + (list.length > 1 ? ' 等 ' + list.length + ' 个' : '')
579
579
  }
580
+
581
+ /**
582
+ * 额度推迟的文案(审核 T20 的回归点)。
583
+ *
584
+ * 以前这段是内联拼的,拼错过一次:
585
+ * `deferReason = 'quota' + new Date(...).toISOString().slice(11, 16) + 'Z'` → `quota09:30Z`
586
+ * `deferReason` 是**机器可读的稳定原因**(面板按它筛选、打标签),掺进时间就永远查不到。
587
+ * 抽成纯函数 + 单测钉住:reason 必须**恰好**是下面这些之一,时间只能出现在人读的那句话里。
588
+ */
589
+ export const DEFER_REASONS = ['ratelimit', 'quota', 'truncated', 'busy', 'window', 'observe', 'backoff']
590
+
591
+ export function quotaDeferText(remaining, resumeInMs, now = Date.now()) {
592
+ const at = new Date(now + Math.max(0, Number(resumeInMs) || 0))
593
+ const hhmm = at.toISOString().slice(11, 16)
594
+ return {
595
+ reason: 'quota',
596
+ text: '额度紧张(剩余 ' + String(remaining) + '),已推迟到 ' + hhmm + ' UTC',
597
+ }
598
+ }
package/lib/index.js CHANGED
@@ -23,9 +23,10 @@ import { zstdDecompressSync } from 'node:zlib'
23
23
 
24
24
  import { redact } from './redact.js'
25
25
  import { decodeBody, truncateUtf8 } from './body.js'
26
+ import { quotaDeferText } from './gh-format.js'
26
27
  import { listGiteeItems, fetchGiteeLastComment, addGiteeLabel, ensureGiteeLabel, giteeWhoami, giteeRepo, mapGiteeRepoAccess, DEFAULT_ACK_LABEL_GITEE, giteeLabels, giteeHasLabel, isValidGiteeLabel, suggestGiteeLabel } from './gitee.js'
27
28
  import {
28
- advanceSeen, applyWatcherAction, batchFresh, decideWake, DEFAULT_ACK_LABEL, formatWatchText,
29
+ planWakeSettlement, advanceSeen, applyWatcherAction, batchFresh, decideWake, DEFAULT_ACK_LABEL, formatWatchText,
29
30
  parseRepoSpec, markGiteeReopened,
30
31
  TRIAGE_SIGNALS, accessNote, wakeCriteria, sessionMissingVerdict, buildCommentBatchQuery, classifyGhError, commentSummaryLine, parseCommentBatch, pickSuggestion, repoStructureNote, countReasons, filterReason, formatGhCall, diagnoseEmptyBody, diagnoseBadBody, isAuthBroken, hasAckLabel, jitterMs, parseGhInclude, retryAfterMs, throttleDelayMs, inActiveHours, newestUpdatedAt, nextDelayMs, enterActiveHoursDefer, recordOtherError, permanentBackoffMs, RETRY_BUDGET_ATTEMPTS, RETRY_SLOW_CAP_MINUTES, pushAbsorbed, pushFiltered, normalizeFilters, partitionItems, passesFilters,
31
32
  shouldRunCleanup,
@@ -42,7 +43,7 @@ import {
42
43
 
43
44
  export const name = 'dsh-session-superagent'
44
45
  /** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
45
- export const SERVER_VERSION = '0.9.207'
46
+ export const SERVER_VERSION = '0.9.209'
46
47
  export const inject = ['tools', 'agents', 'webServer']
47
48
  /** 一条会话每小时最多被唤醒几次(跨所有守累计)。单守上限挡不住多守叠加。 */
48
49
  // 本进程启动时刻:用来区分「刚重启还没轮到」与「定时器真的丢了」
@@ -1181,9 +1182,11 @@ async function pollOnce(ctx, entry, baseMs) {
1181
1182
  const throttle = throttleDelayMs({ remaining: parsed.remaining, resetAt: parsed.resetAt })
1182
1183
  if (throttle > 0) {
1183
1184
  entry.nextAt = new Date(Date.now() + throttle).toISOString()
1184
- entry.lastDeferred = '额度紧张(剩余 ' + parsed.remaining + '),已推迟到 '
1185
- entry.deferReason = 'quota'
1186
- + new Date(Date.now() + throttle).toISOString().slice(11, 16) + 'Z'
1185
+ // 审核 T20:以前 deferReason 被拼成 'quota09:30Z' —— 面板按 reason 筛选永远空。
1186
+ // 文案抽成纯函数(quotaDeferText)并有单测钉住:reason 只有稳定原因,时间只在人读的那句里。
1187
+ const quotaText = quotaDeferText(parsed.remaining, throttle)
1188
+ entry.lastDeferred = quotaText.text
1189
+ entry.deferReason = quotaText.reason
1187
1190
  entry.lastError = null
1188
1191
  entry.needsReauth = false
1189
1192
  return
@@ -1238,7 +1241,7 @@ async function pollWithItems(ctx, entry, baseMs, parsed) {
1238
1241
  // 兜底不再默认「可能是代理干扰」:那会把客户往网络上游带。
1239
1242
  // 现在只有确诊是网关页才说代理;其余情况指向 watch_doctor 与本仓库 issue。
1240
1243
  setEntryError(entry, (diag ? diag.hint : '(返回内容不是合法 JSON:先跑 watch_doctor;确认不是凭证问题就带上这段提 issue)') + ' —— 已按退避自动重试')
1241
- bumpFailure(entry, diag && diag.kind === 'auth' ? baseMs : baseMs)
1244
+ bumpFailure(entry, baseMs)
1242
1245
  if (diag && diag.kind === 'auth') {
1243
1246
  const wait = permanentBackoffMs('auth', baseMs)
1244
1247
  if (wait > 0) entry.nextAt = new Date(Date.now() + wait).toISOString()
@@ -1438,48 +1441,39 @@ async function pollWithItems(ctx, entry, baseMs, parsed) {
1438
1441
  // 之前是入队即记账 —— 会话忙时 decideWake 会拒绝,次数却已经记了、seen 也已经推进,
1439
1442
  // 于是那几条既不会重试(seen 已推进)又白白占掉名额。这是会丢消息的 bug。
1440
1443
  wakeMerged(ctx, entry.sessionId, wakeText, (result) => {
1441
- entry.lastWake = result.ok ? new Date().toISOString() : 'failed: ' + String(result.reason || '')
1442
- entry.history = pushHistory(entry.history, {
1443
- at: new Date().toISOString(),
1444
- numbers: entry.lastBatch,
1444
+ // 审核 T20:结算逻辑抽成纯函数 planWakeSettlement()(有单测),这里只负责落副作用。
1445
+ const settled = planWakeSettlement(entry, {
1445
1446
  ok: result.ok,
1446
- reason: result.ok ? null : String(result.reason || '').slice(0, 120),
1447
+ reason: result.reason,
1448
+ fresh,
1449
+ firstSync,
1450
+ baseMs,
1447
1451
  })
1448
- if (result.ok) {
1449
- // 上一次唤醒之后有没有 ack?没有就累计"叫醒了没回音"
1450
- entry.silentWakes = silentStreak({
1451
- previousWakeAt: entry.prevWakeAt,
1452
- lastAckedAt: entry.lastAcked && entry.lastAcked.at,
1453
- previousStreak: entry.silentWakes,
1454
- })
1455
- if (entry.silentWakes === 0 && entry.prevWakeAt) noteEvent(entry, 'acked', '上一次唤醒有回音(ack 出现过了)')
1456
- entry.prevWakeAt = new Date().toISOString()
1457
- entry.wakeTimes.push(Date.now())
1458
- entry.dailyWakes = bumpDailyWakes(entry.dailyWakes)
1459
- entry.lastDeferred = null
1460
- entry.deferReason = null
1461
- noteEvent(entry, firstSync ? 'baseline' : 'wake',
1462
- (firstSync ? '首次同步(存量 ' : '唤醒了 ') + (entry.lastBatch || []).length + ' 条' + (firstSync ? ')' : '') +
1463
- ':' + (entry.lastBatch || []).map((n) => '#' + n).join(' '))
1464
- entry.seen = advanceSeen(entry.seen, fresh)
1465
- entry.lastReported = recordReported(entry, fresh)
1466
- entry.cursor = newestUpdatedAt(fresh)
1467
- // 真正发出去了:这一轮被完整消费,ETag 才可以前移。
1468
- // 若在失败分支前移,下一轮会拿到 304 而不再重算 fresh —— 与上面「保留水位」的意图相悖。
1452
+ entry.lastWake = settled.lastWake
1453
+ entry.lastWakeCount = settled.lastWakeCount
1454
+ entry.lastBatch = settled.lastBatch
1455
+ entry.history = settled.history
1456
+ if (settled.error) setEntryError(entry, settled.error)
1457
+ if (settled.advanceWatermark) {
1458
+ entry.silentWakes = settled.silentWakes
1459
+ if (settled.ackedRecovered && entry.prevWakeAt) noteEvent(entry, 'acked', '上一次唤醒有回音(ack 出现过了)')
1460
+ entry.prevWakeAt = settled.prevWakeAt
1461
+ entry.wakeTimes = settled.wakeTimes
1462
+ entry.dailyWakes = settled.dailyWakes
1463
+ entry.lastDeferred = settled.lastDeferred
1464
+ entry.deferReason = settled.deferReason
1465
+ noteEvent(entry, settled.eventKind, settled.eventText)
1466
+ entry.seen = settled.seen
1467
+ entry.lastReported = settled.lastReported
1468
+ entry.cursor = settled.cursor
1469
1469
  commitEtag()
1470
1470
  } else {
1471
1471
  // 保留水位,下一轮重试这次事件
1472
- setEntryError(entry, 'wake failed: ' + String(result.reason || ''))
1473
1472
  noteEvent(entry, 'wake-failed', entry.lastError)
1474
- // 会话正忙是暂时的:下一轮直接重试;退避反而会越拖越久
1475
- entry.deferReason = 'busy'
1476
- if (/busy|running|idle/i.test(String(result.reason || ''))) {
1477
- entry.lastDeferred = '会话正忙,下一轮重试'
1478
- } else {
1479
- bumpFailure(entry, baseMs)
1480
- }
1481
- }
1482
- persistEntry(entry)
1473
+ entry.deferReason = settled.deferReason
1474
+ if (settled.lastDeferred) entry.lastDeferred = settled.lastDeferred
1475
+ if (settled.bumpBackoff) bumpFailure(entry, baseMs)
1476
+ } persistEntry(entry)
1483
1477
  })
1484
1478
  }
1485
1479
 
package/lib/watch.js CHANGED
@@ -1174,3 +1174,73 @@ export function markGiteeReopened(items, lastStates = {}) {
1174
1174
  for (const key of Object.keys(next)) if (!alive.has(key)) delete next[key]
1175
1175
  return next
1176
1176
  }
1177
+
1178
+ /**
1179
+ * 一次唤醒的**结算**:成功才推进水位、失败就保留水位。
1180
+ *
1181
+ * 审核 T20:这段原来是内联在 `pollWithItems()` 里的(171 行、0 覆盖)—— 它恰好是
1182
+ * **最会丢消息**的那段:会话忙时如果先把 `seen` 推进、ETag 也提交,下一轮拿到 304 直接 return,
1183
+ * 那条 PR 评论**永远不会再叫**,而面板还写「运行中」。
1184
+ *
1185
+ * 抽成纯函数:给定 entry 与结果,返回**要写回的字段**(不改入参)。调用方 `Object.assign` 即可。
1186
+ */
1187
+ export function planWakeSettlement(entry, { ok, reason, fresh = [], firstSync = false, baseMs = 60000, now = Date.now() } = {}) {
1188
+ const at = new Date(now).toISOString()
1189
+ const numbers = (fresh || []).map((item) => item && item.number).filter(Boolean)
1190
+ const history = pushHistory(entry.history, {
1191
+ at,
1192
+ numbers,
1193
+ ok: Boolean(ok),
1194
+ reason: ok ? null : String(reason || '').slice(0, 120),
1195
+ })
1196
+ const lastWake = ok ? at : 'failed: ' + String(reason || '')
1197
+
1198
+ if (!ok) {
1199
+ // 保留水位:下一轮重试这一批。**不许**在这里碰 seen / cursor / etag。
1200
+ const busy = /busy|running|idle/i.test(String(reason || ''))
1201
+ return {
1202
+ lastWake,
1203
+ history,
1204
+ lastBatch: numbers,
1205
+ lastWakeCount: (fresh || []).length,
1206
+ commitEtag: false,
1207
+ advanceWatermark: false,
1208
+ deferReason: 'busy',
1209
+ lastDeferred: busy ? '会话正忙,下一轮重试' : (entry.lastDeferred || null),
1210
+ bumpBackoff: !busy,
1211
+ baseMs,
1212
+ error: 'wake failed: ' + String(reason || ''),
1213
+ }
1214
+ }
1215
+
1216
+ return {
1217
+ lastWake,
1218
+ history,
1219
+ lastBatch: numbers,
1220
+ lastWakeCount: (fresh || []).length,
1221
+ // 真正发出去了才允许:水位前移 + ETag 前移
1222
+ commitEtag: true,
1223
+ advanceWatermark: true,
1224
+ seen: advanceSeen(entry.seen, fresh || []),
1225
+ lastReported: recordReported(entry, fresh || []),
1226
+ cursor: newestUpdatedAt(fresh || []),
1227
+ silentWakes: silentStreak({
1228
+ previousWakeAt: entry.prevWakeAt,
1229
+ lastAckedAt: entry.lastAcked && entry.lastAcked.at,
1230
+ previousStreak: entry.silentWakes,
1231
+ }),
1232
+ prevWakeAt: at,
1233
+ wakeTimes: [...(entry.wakeTimes || []), now],
1234
+ dailyWakes: bumpDailyWakes(entry.dailyWakes, at),
1235
+ lastDeferred: null,
1236
+ deferReason: null,
1237
+ ackedRecovered: Boolean(entry.prevWakeAt && silentStreak({
1238
+ previousWakeAt: entry.prevWakeAt,
1239
+ lastAckedAt: entry.lastAcked && entry.lastAcked.at,
1240
+ previousStreak: entry.silentWakes,
1241
+ }) === 0),
1242
+ eventKind: firstSync ? 'baseline' : 'wake',
1243
+ eventText: (firstSync ? '首次同步(存量 ' : '唤醒了 ') + numbers.length + ' 条' + (firstSync ? ')' : '') + ':' + numbers.map((n) => '#' + n).join(' '),
1244
+ error: null,
1245
+ }
1246
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.9.207",
3
+ "version": "0.9.209",
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",