dsh-session-superagent 0.9.5 → 0.9.7

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.9.7] - 2026-11-02 —— 隔离区回收节流:没超额就不必每次唤醒都跑 git
6
+
7
+ ### 学到的(知识库第 56 条)
8
+ 常规清理按**时间**跑,越界清理**立刻**跑 —— 两者混在一起,最坏就是每次业务请求都跑一遍 gc。
9
+
10
+ ### 问题
11
+ `cleanupWorktrees()` 每次唤醒都跑(间隔可短到 30 秒),里面是 `readdir` +
12
+ 每个过期副本 `worktree remove` + `prune` + `branch -D`。本机实测单次 git 调用 0.19s ——
13
+ 而绝大多数时候根本没有过期副本。
14
+
15
+ ### 改法(两条线,别只用一条)
16
+ | 条件 | 动作 |
17
+ |---|---|
18
+ | `count > keepCount` 超额 | **立刻跑**(不跑就会堆,违反数量线) |
19
+ | 距上次 < 10 分钟 | 跳过 |
20
+ | 其它 | 跑(兜底) |
21
+
22
+ 节流状态记在 `entry.lastCleanupAt`,**只在实际执行时更新** ——
23
+ "打算跑"不能算数(同 0.9.5 那条教训)。
24
+
25
+ ### 测试
26
+ - 新增 1 例(5 种情况)。合计 **150 例**。
27
+
28
+ ## [0.9.6] - 2026-11-02 —— 会话忙时不再白开隔离区
29
+
30
+ ### 学到的(知识库第 55 条)
31
+ 把"能不能做"的**便宜判断**放在"做"之前。这里的代价不是网络,而是本地 git:
32
+ 本机实测 `git worktree add` **0.187s**,回收阶段每个过期副本还要 `remove` + `branch -D` 各一次。
33
+
34
+ ### 原来的顺序(浪费)
35
+ `pollOnce` 先 `prepareIsolation()`(回收 + 开新副本)再 `wake()`,
36
+ 而 `wake()` 第一步 `decideWake(agent.status)` —— 会话不是 idle 就直接拒绝。
37
+ 于是"会话忙"这个常见状态,会白白付出一次完整隔离区开销,开出来的副本还没人用。
38
+
39
+ ### 改了什么
40
+ 在 `prepareIsolation()` 之前先判一次;忙则写清「这一轮不准备隔离区,下一轮再试」并返回。
41
+ 省 git 开销,面板原因也更准确(以前只写 `wake failed`)。
42
+
43
+ ### 测试
44
+ - 新增 `test/wake-order.test.mjs`:**源码顺序断言** —— 前置判断必须在 `prepareIsolation` 之前,
45
+ 且文案含"下一轮再试"。(顺序约束没法用行为测试表达。)合计 **149 例**。
46
+
5
47
  ## [0.9.5] - 2026-11-02 —— 修掉 0.9.3 引入的「丢消息」bug
6
48
 
7
49
  ### 学到的(知识库第 54 条)
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.5-green.svg)](https://github.com/MYCF711/dsh-session-superagent/releases)
6
+ [![Version](https://img.shields.io/badge/version-0.9.7-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.5'
17
+ const CLIENT_VERSION = '0.9.7'
18
18
  let BASE = '/session-superagent'
19
19
  /**
20
20
  * 挑「最新的那个服务端实例」。
package/lib/index.js CHANGED
@@ -26,15 +26,18 @@ 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
+ shouldRunCleanup,
29
30
  mergeFilters, mergePollResult, mergeWakeTexts, msUntilActiveWindow, nextWaketimeAt, pruneSeen, sessionWakesLastHour, pruneWakeTimes, pushHistory, recordReported, staleWorktrees, worktreePlan,
30
31
  } from './watch.js'
31
32
 
32
33
  export const name = 'dsh-session-superagent'
33
34
  /** 服务端版本:随 package.json 同步(meta 测试守着)。客户端据此挑「最新的那个实例」。 */
34
- export const SERVER_VERSION = '0.9.5'
35
+ export const SERVER_VERSION = '0.9.7'
35
36
  export const inject = ['tools', 'agents', 'webServer']
36
37
  /** 一条会话每小时最多被唤醒几次(跨所有守累计)。单守上限挡不住多守叠加。 */
37
38
  const DEFAULT_SESSION_WAKES = 12
39
+ /** 隔离区回收的最小间隔:没有超额时不必每次唤醒都跑 git。 */
40
+ const CLEANUP_INTERVAL_MS = 10 * 60 * 1000
38
41
 
39
42
  /** 仅供测试:读写状态文件(生产代码不要用)。 */
40
43
  export const __test = { readState, writeState, hardenSecretsFile, wakeMerged }
@@ -334,13 +337,23 @@ function bumpFailure(entry, baseMs) {
334
337
  * 回收过期的隔离区:每次唤醒都会开一个新 worktree,不回收就会一直涨。
335
338
  * 只删 .dsh-zhuli/ 下超过 keepDays 天没动过的目录,删不掉就留着(不阻断唤醒)。
336
339
  */
337
- async function cleanupWorktrees(workdir, keepDays = 3, keepCount = 5) {
340
+ async function cleanupWorktrees(workdir, keepDays = 3, keepCount = 5, options = {}) {
338
341
  if (!workdir) return 0
339
342
  const BS = String.fromCharCode(92)
340
343
  const base = String(workdir).split(BS).join('/').replace(/\/+$/, '')
341
344
  const root = base + '/.dsh-zhuli'
342
345
  let names = []
343
346
  try { names = readdirSync(root) } catch { return 0 }
347
+
348
+ // 节流:没有超额就不必每次都跑 git(remove / prune / branch -D 都是进程级开销)
349
+ const decision = shouldRunCleanup({
350
+ count: names.length,
351
+ keepCount,
352
+ lastRunAt: Number(options.lastRunAt) || 0,
353
+ intervalMs: CLEANUP_INTERVAL_MS,
354
+ })
355
+ if (!decision.run) return 0
356
+ if (typeof options.onRun === 'function') options.onRun(decision.reason)
344
357
  const entries = []
345
358
  for (const name of names) {
346
359
  try { entries.push({ name, mtimeMs: statSync(root + '/' + name).mtimeMs }) } catch {}
@@ -376,7 +389,10 @@ async function prepareIsolation(entry) {
376
389
  const plan = worktreePlan({ workdir: entry.workdir, repo: entry.repo, sessionTag: entry.sessionId })
377
390
  try {
378
391
  // 先回收过期隔离区,再开新的
379
- await cleanupWorktrees(entry.workdir, Number(entry.isolateKeepDays) || 3, Number(entry.maxIsolated) || 5)
392
+ await cleanupWorktrees(entry.workdir, Number(entry.isolateKeepDays) || 3, Number(entry.maxIsolated) || 5, {
393
+ lastRunAt: Number(entry.lastCleanupAt) || 0,
394
+ onRun: () => { entry.lastCleanupAt = Date.now() },
395
+ })
380
396
  const res = await run('git', ['-C', entry.workdir, 'worktree', 'add', plan.dir, '-b', plan.branch], { timeoutMs: 45000 })
381
397
  if (res.code !== 0) {
382
398
  return { error: redact([res.stderr, res.stdout].filter(Boolean).join(' ')).slice(0, 200) }
@@ -921,6 +937,17 @@ async function pollOnce(ctx, entry, baseMs) {
921
937
  return
922
938
  }
923
939
 
940
+ // 会话忙时**不要**先开隔离区:`git worktree add`(本机实测 0.19s,大仓库更久)
941
+ // 加上回收过期副本的若干次 git 调用,全都是白发 —— 因为 decideWake 之后一定会拒绝。
942
+ {
943
+ const agents = agentsService(ctx)
944
+ const agent = agents && typeof agents.get === 'function' ? agents.get(entry.sessionId) : undefined
945
+ const pre = decideWake(agent && agent.status)
946
+ if (!pre.ok) {
947
+ entry.lastDeferred = '会话正忙(' + String(pre.reason || '') + '),这一轮不准备隔离区,下一轮再试'
948
+ return
949
+ }
950
+ }
924
951
  const isolation = await prepareIsolation(entry)
925
952
  if (isolation && isolation.dir) entry.lastWorktree = isolation.dir
926
953
  // 批次前几条补「最后是谁在说话」(最多 3 条,失败静默)
package/lib/watch.js CHANGED
@@ -601,3 +601,23 @@ export function mergeWakeTexts(parts) {
601
601
  const body = trimmed.map((text, i) => String.fromCharCode(10, 10) + '—— 第 ' + (i + 1) + ' 处 ——' + String.fromCharCode(10) + text).join('')
602
602
  return head + body
603
603
  }
604
+
605
+ /**
606
+ * 该不该现在回收隔离区。
607
+ *
608
+ * 现状:每次唤醒都跑一遍回收 —— 里面是 `git worktree remove` / `branch -D` / `prune`
609
+ * (本机实测 `worktree add` 0.19s,remove 同级;5 个过期副本就是 1 秒级),
610
+ * 而绝大多数时候根本没有过期副本。
611
+ *
612
+ * 节流规则:
613
+ * - 数量**超上限**时必须立刻回收(不能等,否则会堆);
614
+ * - 没超额时,距上次回收不足 `intervalMs`(默认 10 分钟)就跳过。
615
+ */
616
+ export function shouldRunCleanup({ count = 0, keepCount = 5, lastRunAt = 0, now = Date.now(), intervalMs = 10 * 60 * 1000 } = {}) {
617
+ const total = Number(count) || 0
618
+ const cap = Math.max(1, Number(keepCount) || 5)
619
+ if (total > cap) return { run: true, reason: 'over-cap' }
620
+ const last = Number(lastRunAt) || 0
621
+ if (last > 0 && now - last < Number(intervalMs)) return { run: false, reason: 'throttled' }
622
+ return { run: true, reason: 'interval' }
623
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-superagent",
3
- "version": "0.9.5",
3
+ "version": "0.9.7",
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",