dsh-session-superagent 0.9.1 → 0.9.3

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,57 @@
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.3] - 2026-11-02 —— 同一会话的多条唤醒合并成一条
6
+
7
+ ### 学到的(知识库第 52 条)
8
+ Google SRE《Handling Overload》:过载时要**减少工作**(coalesce/shed),不是更快失败。
9
+ <https://sre.google/sre-book/handling-overload/>
10
+
11
+ ### 实测出来的形态
12
+ 同会话 3 个守、间隔 60 秒:
13
+ ```
14
+ 首轮相位: 1184 / 2145 / 3106 ms
15
+ 相邻两次唤醒间隔: 最小 0.96s / 中位 0.96s / 最大 0.96s
16
+ ```
17
+ 抖动是**一次性**的 —— 之后每个守按自己的 every 推进,相对相位几乎不变,
18
+ 所以**每一轮**都会在约 1 秒内连着收到 3 条注入(5 个守就是 5 条)。
19
+
20
+ ### 为什么不用"最小间隔"
21
+ 先到的守会永远赢,后面的饿死 —— 和"固定窗口让突发者占名额"是同一类错。
22
+
23
+ ### 改法:合并
24
+ 同会话 **3 秒窗口**内的多条唤醒攒成一条:
25
+ ```
26
+ 【主理鱼】这一小段时间内有 3 处变化,合并成一条:
27
+ —— 第 1 处 —— … —— 第 2 处 —— …
28
+ ```
29
+ 单条不加包装;`decideWake`(是否 idle)在真正发送时再判,比原来更新鲜。
30
+
31
+ ### 测试
32
+ - 新增 1 例。合计 **145 例**。
33
+
34
+ ## [0.9.2] - 2026-11-02 —— 会话级唤醒上限:单守上限挡不住"多守叠加"
35
+
36
+ ### 学到的(知识库第 51 条)
37
+ K8s `ResourceQuota`:**单个 Pod 的 limits 不能防止 namespace 被打满**,要在上一层再加配额。
38
+ <https://kubernetes.io/docs/concepts/policy/resource-quotas/>
39
+
40
+ ### 代码里核实出来的漏洞
41
+ `maxWakesPerHour` 记在**每条守**身上(`entry.wakeTimes`):
42
+ ```
43
+ 1 条会话守 5 个仓库 → 实际上限 5 × 6 = 30 次/小时;每次 5 条 → 最多 150 条/小时注进同一会话
44
+ ```
45
+ 而设这个上限的原因正是"别把上下文打爆" —— 多守叠加后它完全没起作用。
46
+
47
+ ### 改了什么
48
+ 新增**会话级上限 `sessionMaxWakes`(默认 12/小时,跨所有守累计)**:
49
+ > 这条会话一小时内已被唤醒 8 次(上限 12),已攒 3 条待处理
50
+
51
+ 三层限额:单次批量 5 / 单守 6 / **会话总量 12**。面板可改,`/state` 回传。
52
+
53
+ ### 测试
54
+ - 新增 1 例(跨守累加、过期不计、多会话隔离、对象/数组入参、坏数据不崩)。合计 **144 例**。
55
+
5
56
  ## [0.9.1] - 2026-11-02 —— 修正「已知限制」:一条是错的,三条缺
6
57
 
7
58
  ### 核对出来的(知识库第 50 条)
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.1-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.9.3-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.9.1'
17
+ const CLIENT_VERSION = '0.9.3'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
@@ -929,6 +929,7 @@ window.__ModuleLoader__.load({
929
929
  },
930
930
  batchLimit: Number(draft.batchLimit) || undefined,
931
931
  maxWakesPerHour: Number(draft.maxWakesPerHour) || undefined,
932
+ sessionMaxWakes: Number(draft.sessionMaxWakes) || undefined,
932
933
  }),
933
934
  })
934
935
  setEditing(null)
@@ -1020,6 +1021,7 @@ window.__ModuleLoader__.load({
1020
1021
  workdir: watcher.workdir || '',
1021
1022
  batchLimit: watcher.batchLimit ? String(watcher.batchLimit) : '',
1022
1023
  maxWakesPerHour: watcher.maxWakesPerHour ? String(watcher.maxWakesPerHour) : '',
1024
+ sessionMaxWakes: watcher.sessionMaxWakes ? String(watcher.sessionMaxWakes) : '',
1023
1025
  isolateKeepDays: watcher.isolateKeepDays ? String(watcher.isolateKeepDays) : '',
1024
1026
  maxIsolated: watcher.maxIsolated ? String(watcher.maxIsolated) : '',
1025
1027
  priorityReopened: (watcher.priority && watcher.priority.reopened) !== false,
@@ -1074,6 +1076,7 @@ window.__ModuleLoader__.load({
1074
1076
  h('label', { className: 'dt-switch-row' }, h(Toggle, { checked: !!draft.onlyAssigned, onChange: (v) => setDraft({ ...draft, onlyAssigned: v }), label: '只看已指派' }), '只处理已指派给人的条目'),
1075
1077
  field('单次最多唤醒条数', h(Input, { value: draft.batchLimit || '', placeholder: '5(1-20)', onChange: (e) => setDraft({ ...draft, batchLimit: e.target.value }) })),
1076
1078
  field('每小时最多唤醒次数', h(Input, { value: draft.maxWakesPerHour || '', placeholder: '6(1-60)', onChange: (e) => setDraft({ ...draft, maxWakesPerHour: e.target.value }) })),
1079
+ field('这条会话每小时最多被唤醒', h(Input, { value: draft.sessionMaxWakes || '', placeholder: '12(1-60)', onChange: (e) => setDraft({ ...draft, sessionMaxWakes: e.target.value }) }), '跨所有守累计:守 5 个仓库时,光有单守上限挡不住叠加'),
1077
1080
  field('优先级(一次只处理 N 条时先挑谁)', h('div', { className: 'dt-actions' },
1078
1081
  h('label', { className: 'dt-switch-row' }, h(Toggle, { checked: draft.priorityReopened !== false, onChange: (v) => setDraft({ ...draft, priorityReopened: v }), label: '重开优先' }), '被重开的排最前'),
1079
1082
  h('label', { className: 'dt-switch-row' }, h(Toggle, { checked: draft.priorityComments !== false, onChange: (v) => setDraft({ ...draft, priorityComments: v }), label: '评论优先' }), '有人在等回应的靠前'),
package/lib/index.js CHANGED
@@ -26,13 +26,15 @@ 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, msUntilActiveWindow, nextWaketimeAt, pruneSeen, pruneWakeTimes, pushHistory, recordReported, staleWorktrees, worktreePlan,
29
+ mergeFilters, mergePollResult, mergeWakeTexts, msUntilActiveWindow, nextWaketimeAt, pruneSeen, sessionWakesLastHour, 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.9.1'
34
+ export const SERVER_VERSION = '0.9.3'
35
35
  export const inject = ['tools', 'agents', 'webServer']
36
+ /** 一条会话每小时最多被唤醒几次(跨所有守累计)。单守上限挡不住多守叠加。 */
37
+ const DEFAULT_SESSION_WAKES = 12
36
38
 
37
39
  /** 仅供测试:读写状态文件(生产代码不要用)。 */
38
40
  export const __test = { readState, writeState, hardenSecretsFile }
@@ -450,6 +452,34 @@ function defineTool(options) {
450
452
  * `Agent.followup()` queues an ordinary turn AND wakes the driver; the durable
451
453
  * inbox alone only parks the message (the client shows it as a pending draft).
452
454
  */
455
+
456
+ /**
457
+ * 同会话几秒内的多条唤醒合并成一条再注入。
458
+ *
459
+ * 实测:同会话 3 个守、间隔 60 秒时,首轮抖动只把相位拉开 ≤5 秒 —— 之后每一轮都一样,
460
+ * 于是三次唤醒落在约 1 秒内,会话连着收到 3 条注入(5 个守就是 5 条)。
461
+ * 不做硬间隔,是因为"先到的守永远赢"会把后面的饿死;**合并**既不丢也不饿死。
462
+ */
463
+ const WAKE_WINDOW_MS = 3000
464
+ const wakeBuffers = new Map()
465
+
466
+ function wakeMerged(ctx, sessionId, text) {
467
+ const now = Date.now()
468
+ const buf = wakeBuffers.get(sessionId) || { parts: [], timer: null, openedAt: now }
469
+ buf.parts.push(text)
470
+ if (buf.timer) return { ok: true, merged: true, pending: buf.parts.length }
471
+ buf.timer = setTimeout(() => {
472
+ wakeBuffers.delete(sessionId)
473
+ try {
474
+ const merged = mergeWakeTexts(buf.parts)
475
+ if (merged) wake(ctx, sessionId, merged)
476
+ } catch { /* 合并失败也不能让唤醒消失:下面这次单独发 */ }
477
+ }, WAKE_WINDOW_MS)
478
+ if (buf.timer && typeof buf.timer.unref === 'function') buf.timer.unref()
479
+ wakeBuffers.set(sessionId, buf)
480
+ return { ok: true, merged: true, pending: buf.parts.length }
481
+ }
482
+
453
483
  function wake(ctx, sessionId, text) {
454
484
  const agents = agentsService(ctx)
455
485
  const agent = agents && typeof agents.get === 'function' ? agents.get(sessionId) : undefined
@@ -837,6 +867,14 @@ async function pollOnce(ctx, entry, baseMs) {
837
867
  return
838
868
  }
839
869
 
870
+ // 会话级上限(跨守累计):单守上限 × 守数量 = 实际频率,光有单守上限挡不住
871
+ const sessionCap = Math.max(1, Number(entry.sessionMaxWakes) || DEFAULT_SESSION_WAKES)
872
+ const sessionUsed = sessionWakesLastHour(Object.values(readState().watchers || {}), entry.sessionId)
873
+ if (sessionUsed >= sessionCap) {
874
+ entry.lastDeferred = '这条会话一小时内已被唤醒 ' + sessionUsed + ' 次(上限 ' + sessionCap + '),已攒 ' + fresh.length + ' 条待处理'
875
+ return
876
+ }
877
+
840
878
  // 每小时唤醒上限:宁可攒着,也不要连续叫醒把上下文打爆
841
879
  const cap = Math.max(1, Number(entry.maxWakesPerHour) || 6)
842
880
  entry.wakeTimes = pruneWakeTimes(entry.wakeTimes)
@@ -878,7 +916,7 @@ async function pollOnce(ctx, entry, baseMs) {
878
916
  if (isolation && isolation.dir) entry.lastWorktree = isolation.dir
879
917
  // 批次前几条补「最后是谁在说话」(最多 3 条,失败静默)
880
918
  await fetchLastComments(entry.repo, listed)
881
- const result = wake(ctx, entry.sessionId, formatWatchText(entry.repo, listed, {
919
+ const wakeText = formatWatchText(entry.repo, listed, {
882
920
  more: rest.length,
883
921
  restNumbers: rest.map((item) => item.number),
884
922
  isolation,
@@ -896,7 +934,8 @@ async function pollOnce(ctx, entry, baseMs) {
896
934
  changedTotal: fresh.length,
897
935
  batchLimit: listed.length,
898
936
  skippedReasons: entry.filterCounts || {},
899
- }))
937
+ })
938
+ const result = wakeMerged(ctx, entry.sessionId, wakeText)
900
939
  entry.lastWake = result.ok ? new Date().toISOString() : `failed: ${result.reason}`
901
940
  entry.lastWakeCount = fresh.length
902
941
  entry.lastBatch = listed.map((item) => item.number)
@@ -1093,6 +1132,7 @@ export function apply(ctx) {
1093
1132
  filters: w.filters ?? null,
1094
1133
  batchLimit: w.batchLimit ?? 5,
1095
1134
  maxWakesPerHour: w.maxWakesPerHour ?? 6,
1135
+ sessionMaxWakes: w.sessionMaxWakes ?? 12,
1096
1136
  consecutiveFailures: w.consecutiveFailures ?? 0,
1097
1137
  nextAt: w.nextAt ?? null,
1098
1138
  lastDeferred: w.lastDeferred ?? null,
@@ -1257,6 +1297,7 @@ export function apply(ctx) {
1257
1297
  entry.filters = mergeFilters(entry.filters, body.filters)
1258
1298
  entry.batchLimit = Math.max(1, Math.min(20, Number(body.batchLimit) || entry.batchLimit || 5))
1259
1299
  entry.maxWakesPerHour = Math.max(1, Math.min(60, Number(body.maxWakesPerHour) || entry.maxWakesPerHour || 6))
1300
+ if (body.sessionMaxWakes != null) entry.sessionMaxWakes = Math.max(1, Math.min(60, Number(body.sessionMaxWakes) || DEFAULT_SESSION_WAKES))
1260
1301
  if (typeof body.ackLabel === 'string' && body.ackLabel.trim()) entry.ackLabel = body.ackLabel.trim().slice(0, 60)
1261
1302
  // 优先级开关:客户可以关掉某几类"加权",只按 updated 顺序处理
1262
1303
  if (body.priority && typeof body.priority === 'object') {
package/lib/watch.js CHANGED
@@ -539,3 +539,42 @@ export function nextWaketimeAt(wakeTimes, cap, now = Date.now()) {
539
539
  const freeing = times[times.length - n]
540
540
  return freeing + 60 * 60 * 1000
541
541
  }
542
+
543
+ /**
544
+ * 同一条会话在过去一小时被唤醒了几次(跨所有守)。
545
+ *
546
+ * 为什么需要:`maxWakesPerHour` 是**按守**算的。一条会话守 5 个仓库时,
547
+ * 上限实际是 5 × 6 = 30 次/小时 —— 这根本没保护到"别把会话的上下文打爆",
548
+ * 而那正是当初设这个上限的原因。
549
+ *
550
+ * 类比:只给单个 Pod 设 CPU limit,并不能防止整个 namespace 被打满,所以还要 ResourceQuota。
551
+ * 出处:https://kubernetes.io/docs/concepts/policy/resource-quotas/
552
+ */
553
+ export function sessionWakesLastHour(entries, sessionId, now = Date.now()) {
554
+ if (!sessionId) return 0
555
+ const list = Array.isArray(entries) ? entries : Object.values(entries || {})
556
+ let total = 0
557
+ for (const entry of list) {
558
+ if (!entry || entry.sessionId !== sessionId) continue
559
+ total += pruneWakeTimes(entry.wakeTimes, now).length
560
+ }
561
+ return total
562
+ }
563
+
564
+ /**
565
+ * 把同一会话在几秒内产生的多条唤醒合并成一条。
566
+ *
567
+ * 实测(本轮):同会话 3 个守、间隔都是 60 秒时,首轮抖动只把相位拉开 ≤5 秒,
568
+ * **之后每一轮都一样** —— 三次唤醒落在 ~1 秒内,会话会连着收到 3 条注入。
569
+ * 5 个守就是 5 条。这正是"上下文被瞬间打爆"的真实形态。
570
+ *
571
+ * 合并而不是"设个间隔挡掉":硬间隔会让先到的守永远赢,后面的守被饿死。
572
+ */
573
+ export function mergeWakeTexts(parts) {
574
+ const list = (Array.isArray(parts) ? parts : []).filter((t) => typeof t === 'string' && t.trim())
575
+ if (list.length === 0) return ''
576
+ if (list.length === 1) return list[0]
577
+ const head = '【主理鱼】这一小段时间内有 ' + list.length + ' 处变化,合并成一条:'
578
+ const body = list.map((text, i) => String.fromCharCode(10, 10) + '—— 第 ' + (i + 1) + ' 处 ——' + String.fromCharCode(10) + text).join('')
579
+ return head + body
580
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.9.1",
3
+ "version": "0.9.3",
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",