dsh-session-superagent 0.8.98 → 0.9.0
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 +43 -0
- package/README.md +1 -1
- package/client.js +4 -1
- package/lib/index.js +12 -6
- package/lib/watch.js +40 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,49 @@
|
|
|
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.0] - 2026-11-02 —— 隔离区加"数量线":最坏 432 个副本 → 最多 5 个
|
|
6
|
+
|
|
7
|
+
### 学到的(知识库第 49 条)
|
|
8
|
+
`git worktree prune` 只清"目录已不在的管理记录",**不管副本数量**。
|
|
9
|
+
清理策略要 **时间线 + 数量线** 双保险,只靠一条会被极端情况绕过。
|
|
10
|
+
|
|
11
|
+
### 实测与推演
|
|
12
|
+
| 场景 | 数字 |
|
|
13
|
+
|---|---|
|
|
14
|
+
| 合成仓库(3 MB 文件) | 每个 worktree **2.9 MB**(≈ 工作树大小) |
|
|
15
|
+
| 6 次/小时 × 保留 3 天 | 最坏 **432 个副本** |
|
|
16
|
+
| 按 10 MB/副本估 | ≈ **4 GB** |
|
|
17
|
+
|
|
18
|
+
### 改了什么
|
|
19
|
+
`staleWorktrees` 加数量线:按 mtime 倒序只留最近 `keepCount`(默认 5)个,其余回收;
|
|
20
|
+
两条线结果去重,**最近 N 个无论多老都留着**(正在用的那个不能删)。
|
|
21
|
+
|
|
22
|
+
配置 `entry.maxIsolated`(1–50,默认 5),面板「隔离」区可改,`/state` 回传。
|
|
23
|
+
|
|
24
|
+
### 测试
|
|
25
|
+
- 新增 1 例(4 种情况)。合计 **143 例**。
|
|
26
|
+
|
|
27
|
+
## [0.8.99] - 2026-11-02 —— 被每小时上限挡住时,告诉你还要等多久
|
|
28
|
+
|
|
29
|
+
### 先模拟,再看要不要改(知识库第 48 条)
|
|
30
|
+
用真实的 `pruneWakeTimes` 跑 6 小时模拟(cap=6/小时):
|
|
31
|
+
```
|
|
32
|
+
间隔 30s -> 两次唤醒最大间隔 3480s(58 分钟)
|
|
33
|
+
间隔 60s -> 3360s(56 分钟)
|
|
34
|
+
间隔 120s -> 3120s(52 分钟)
|
|
35
|
+
```
|
|
36
|
+
滑动窗口下如果 6 次集中在开头,下一次要等近 1 小时。**这不是 bug**(限额就是限额),
|
|
37
|
+
但客户看到「已攒 N 条」却不知道要等多久,很容易以为卡死。
|
|
38
|
+
|
|
39
|
+
### 改法:不改限额,只让等待可见
|
|
40
|
+
`nextWaketimeAt(wakeTimes, cap, now)` 算出下一个名额空出的时刻,提示语变成:
|
|
41
|
+
> 每小时最多唤醒 6 次,已攒 3 条待处理(**约 43 分钟后可再唤醒**)
|
|
42
|
+
|
|
43
|
+
边界:名额没满 → 现在;超过 1 小时的自动剔除;cap 非法按 6 兜底。
|
|
44
|
+
|
|
45
|
+
### 测试
|
|
46
|
+
- 新增 1 例(4 种情况)。合计 **142 例**。
|
|
47
|
+
|
|
5
48
|
## [0.8.98] - 2026-11-02 —— 时段外不再空打 API(一夜能省 800+ 次)
|
|
6
49
|
|
|
7
50
|
### 学到的(知识库第 47 条)
|
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)
|
|
6
|
-
[](https://github.com/MYCF711/dsh-session-superagent/releases)
|
|
7
7
|
[](https://www.npmjs.com/package/dsh-session-superagent)
|
|
8
8
|
[](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.
|
|
17
|
+
const CLIENT_VERSION = '0.9.0'
|
|
18
18
|
let BASE = '/session-superagent'
|
|
19
19
|
/**
|
|
20
20
|
* 挑「最新的那个服务端实例」。
|
|
@@ -919,6 +919,7 @@ window.__ModuleLoader__.load({
|
|
|
919
919
|
workdir: draft.workdir || '',
|
|
920
920
|
observe: !!draft.observe,
|
|
921
921
|
isolateKeepDays: Number(draft.isolateKeepDays) || undefined,
|
|
922
|
+
maxIsolated: Number(draft.maxIsolated) || undefined,
|
|
922
923
|
priority: {
|
|
923
924
|
reopened: draft.priorityReopened !== false,
|
|
924
925
|
comments: draft.priorityComments !== false,
|
|
@@ -1020,6 +1021,7 @@ window.__ModuleLoader__.load({
|
|
|
1020
1021
|
batchLimit: watcher.batchLimit ? String(watcher.batchLimit) : '',
|
|
1021
1022
|
maxWakesPerHour: watcher.maxWakesPerHour ? String(watcher.maxWakesPerHour) : '',
|
|
1022
1023
|
isolateKeepDays: watcher.isolateKeepDays ? String(watcher.isolateKeepDays) : '',
|
|
1024
|
+
maxIsolated: watcher.maxIsolated ? String(watcher.maxIsolated) : '',
|
|
1023
1025
|
priorityReopened: (watcher.priority && watcher.priority.reopened) !== false,
|
|
1024
1026
|
priorityComments: (watcher.priority && watcher.priority.comments) !== false,
|
|
1025
1027
|
priorityLabels: (watcher.priority && watcher.priority.labels) !== false,
|
|
@@ -1082,6 +1084,7 @@ window.__ModuleLoader__.load({
|
|
|
1082
1084
|
h('label', { className: 'dt-switch-row' }, h(Toggle, { checked: !!draft.isolated, onChange: (v) => setDraft({ ...draft, isolated: v }), label: '隔离执行' }), '隔离执行(每次唤醒开一次性 worktree)'),
|
|
1083
1085
|
draft.isolated ? field('隔离工作目录', h(Input, { value: draft.workdir || '', placeholder: '绝对路径,留空 = 当前工作目录', onChange: (e) => setDraft({ ...draft, workdir: e.target.value }) })) : null,
|
|
1084
1086
|
draft.isolated ? field('隔离区保留天数', h(Input, { value: draft.isolateKeepDays || '', placeholder: '3(1-30)', onChange: (e) => setDraft({ ...draft, isolateKeepDays: e.target.value }) }), '每次唤醒前回收这个天数之前的一次性工作区;留空用默认 3 天') : null,
|
|
1087
|
+
draft.isolated ? field('最多保留几个隔离区', h(Input, { value: draft.maxIsolated || '', placeholder: '5(1-50)', onChange: (e) => setDraft({ ...draft, maxIsolated: e.target.value }) }), '天数之外的第二道线:超出就回收最旧的(不影响正在用的那个)') : null,
|
|
1085
1088
|
h('div', { className: 'dt-actions' },
|
|
1086
1089
|
h(Btn, { size: 'sm', variant: 'primary', onClick: () => saveEdit(watcher) }, '保存'),
|
|
1087
1090
|
h(Btn, { size: 'sm', onClick: () => setEditing(null) }, '放弃')))
|
package/lib/index.js
CHANGED
|
@@ -26,12 +26,12 @@ 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, pruneSeen, pruneWakeTimes, pushHistory, recordReported, staleWorktrees, worktreePlan,
|
|
29
|
+
mergeFilters, mergePollResult, msUntilActiveWindow, nextWaketimeAt, pruneSeen, 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.
|
|
34
|
+
export const SERVER_VERSION = '0.9.0'
|
|
35
35
|
export const inject = ['tools', 'agents', 'webServer']
|
|
36
36
|
|
|
37
37
|
/** 仅供测试:读写状态文件(生产代码不要用)。 */
|
|
@@ -332,7 +332,7 @@ function bumpFailure(entry, baseMs) {
|
|
|
332
332
|
* 回收过期的隔离区:每次唤醒都会开一个新 worktree,不回收就会一直涨。
|
|
333
333
|
* 只删 .dsh-zhuli/ 下超过 keepDays 天没动过的目录,删不掉就留着(不阻断唤醒)。
|
|
334
334
|
*/
|
|
335
|
-
async function cleanupWorktrees(workdir, keepDays = 3) {
|
|
335
|
+
async function cleanupWorktrees(workdir, keepDays = 3, keepCount = 5) {
|
|
336
336
|
if (!workdir) return 0
|
|
337
337
|
const BS = String.fromCharCode(92)
|
|
338
338
|
const base = String(workdir).split(BS).join('/').replace(/\/+$/, '')
|
|
@@ -343,7 +343,8 @@ async function cleanupWorktrees(workdir, keepDays = 3) {
|
|
|
343
343
|
for (const name of names) {
|
|
344
344
|
try { entries.push({ name, mtimeMs: statSync(root + '/' + name).mtimeMs }) } catch {}
|
|
345
345
|
}
|
|
346
|
-
|
|
346
|
+
// 数量线:最近 N 个留着(默认 5),其余回收 —— 年龄线管不住"堆"
|
|
347
|
+
const stale = staleWorktrees(entries, { keepMs: keepDays * 24 * 3600 * 1000, keepCount: Number(keepCount) || 5 })
|
|
347
348
|
let removed = 0
|
|
348
349
|
for (const item of stale) {
|
|
349
350
|
const target = root + '/' + item.name
|
|
@@ -373,7 +374,7 @@ async function prepareIsolation(entry) {
|
|
|
373
374
|
const plan = worktreePlan({ workdir: entry.workdir, repo: entry.repo, sessionTag: entry.sessionId })
|
|
374
375
|
try {
|
|
375
376
|
// 先回收过期隔离区,再开新的
|
|
376
|
-
await cleanupWorktrees(entry.workdir, Number(entry.isolateKeepDays) || 3)
|
|
377
|
+
await cleanupWorktrees(entry.workdir, Number(entry.isolateKeepDays) || 3, Number(entry.maxIsolated) || 5)
|
|
377
378
|
const res = await run('git', ['-C', entry.workdir, 'worktree', 'add', plan.dir, '-b', plan.branch], { timeoutMs: 45000 })
|
|
378
379
|
if (res.code !== 0) {
|
|
379
380
|
return { error: redact([res.stderr, res.stdout].filter(Boolean).join(' ')).slice(0, 200) }
|
|
@@ -841,7 +842,9 @@ async function pollOnce(ctx, entry, baseMs) {
|
|
|
841
842
|
entry.wakeTimes = pruneWakeTimes(entry.wakeTimes)
|
|
842
843
|
if (entry.wakeTimes.length >= cap) {
|
|
843
844
|
// 只记原因,绝不动 seen/cursor —— 否则「攒着」等于「丢掉」
|
|
844
|
-
|
|
845
|
+
const freeAt = nextWaketimeAt(entry.wakeTimes, cap)
|
|
846
|
+
const waitMin = Math.max(1, Math.round((freeAt - Date.now()) / 60000))
|
|
847
|
+
entry.lastDeferred = `每小时最多唤醒 ${cap} 次,已攒 ${fresh.length} 条待处理(约 ${waitMin} 分钟后可再唤醒)`
|
|
845
848
|
return
|
|
846
849
|
}
|
|
847
850
|
|
|
@@ -1104,6 +1107,7 @@ export function apply(ctx) {
|
|
|
1104
1107
|
lastWorktree: w.lastWorktree ?? null,
|
|
1105
1108
|
activeHours: w.activeHours ?? null,
|
|
1106
1109
|
isolateKeepDays: w.isolateKeepDays ?? 3,
|
|
1110
|
+
maxIsolated: w.maxIsolated ?? 5,
|
|
1107
1111
|
history: (w.history ?? []).slice(-5),
|
|
1108
1112
|
note: w.goal ?? '',
|
|
1109
1113
|
title: sessionTitle(w.sessionId),
|
|
@@ -1269,6 +1273,8 @@ export function apply(ctx) {
|
|
|
1269
1273
|
entry.observe = body.observe === undefined ? entry.observe === true : body.observe === true
|
|
1270
1274
|
entry.isolated = body.isolated === undefined ? entry.isolated === true : body.isolated === true
|
|
1271
1275
|
if (body.isolateKeepDays != null) entry.isolateKeepDays = Math.max(1, Math.min(30, Number(body.isolateKeepDays) || 3))
|
|
1276
|
+
// 同时最多留几个隔离区(越界回收最旧的)—— 1~50
|
|
1277
|
+
if (body.maxIsolated != null) entry.maxIsolated = Math.max(1, Math.min(50, Number(body.maxIsolated) || 5))
|
|
1272
1278
|
if (typeof body.activeHours === 'string') entry.activeHours = body.activeHours.trim().slice(0, 20) || null
|
|
1273
1279
|
if (typeof body.workdir === 'string' && body.workdir.trim()) {
|
|
1274
1280
|
const workdir = body.workdir.trim()
|
package/lib/watch.js
CHANGED
|
@@ -377,13 +377,31 @@ export function worktreePlan({ workdir, repo, sessionTag, stamp = Date.now() } =
|
|
|
377
377
|
* 隔离区(worktree)会随每次唤醒累积。返回超过 keepMs 没动过的那些,交给调用方回收。
|
|
378
378
|
* 纯函数,单测覆盖。
|
|
379
379
|
*/
|
|
380
|
+
/**
|
|
381
|
+
* 该回收的隔离区:**按年龄** + **按数量** 两条线(原来只有年龄)。
|
|
382
|
+
*
|
|
383
|
+
* 为什么补数量:年龄线管不住"堆" —— 上限 6 次/小时 × 保留 3 天 = 最坏 432 个副本。
|
|
384
|
+
* 实测一个 worktree ≈ 该仓库工作树的大小(我们的仓库 1.3 MB,一个 3 MB 文件的测试仓库开出 2.9 MB),
|
|
385
|
+
* 按 10 MB 估就是 4 GB 级占用。
|
|
386
|
+
*
|
|
387
|
+
* `keepCount`:最近 N 个无论多老都留着(正在用的那个不能被删)。
|
|
388
|
+
*/
|
|
380
389
|
export function staleWorktrees(entries, options = {}) {
|
|
381
|
-
const { now = Date.now(), keepMs = 3 * 24 * 3600 * 1000 } = options
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
390
|
+
const { now = Date.now(), keepMs = 3 * 24 * 3600 * 1000, keepCount = 5 } = options
|
|
391
|
+
const list = (entries ?? []).filter((entry) => entry && entry.name && Number.isFinite(Number(entry.mtimeMs)))
|
|
392
|
+
const byAge = list.filter((entry) => now - Number(entry.mtimeMs) > keepMs)
|
|
393
|
+
const cap = Math.max(0, Number(keepCount) || 0)
|
|
394
|
+
const newest = list.slice().sort((a, b) => Number(b.mtimeMs) - Number(a.mtimeMs)).slice(0, cap)
|
|
395
|
+
const keep = new Set(newest.map((entry) => entry.name))
|
|
396
|
+
const byCount = list.filter((entry) => !keep.has(entry.name))
|
|
397
|
+
const out = []
|
|
398
|
+
const seen = new Set()
|
|
399
|
+
for (const entry of [...byAge, ...byCount]) {
|
|
400
|
+
if (seen.has(entry.name)) continue
|
|
401
|
+
seen.add(entry.name)
|
|
402
|
+
out.push(entry)
|
|
403
|
+
}
|
|
404
|
+
return out
|
|
387
405
|
}
|
|
388
406
|
|
|
389
407
|
/**
|
|
@@ -505,3 +523,19 @@ export function msUntilActiveWindow(spec, now = new Date()) {
|
|
|
505
523
|
// 精确到分钟的开头,再减掉当前秒数
|
|
506
524
|
return Math.max(1000, diffMin * 60 * 1000 - curSec * 1000)
|
|
507
525
|
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* 被「每小时上限」挡住时,下一个名额什么时候空出来。
|
|
529
|
+
*
|
|
530
|
+
* 为什么要它:cap=6 时最坏要等近 1 小时(实测:滑动窗口下 6 次集中在开头几分钟,
|
|
531
|
+
* 之后要等最早那次过期)。这段时间里面板只说「已攒 N 条」,客户不知道要等多久,
|
|
532
|
+
* 容易以为卡死了。算出具体时刻,就能写「约 43 分钟后可再唤醒」。
|
|
533
|
+
*/
|
|
534
|
+
export function nextWaketimeAt(wakeTimes, cap, now = Date.now()) {
|
|
535
|
+
const times = pruneWakeTimes(wakeTimes, now).sort((a, b) => a - b)
|
|
536
|
+
const n = Math.max(1, Number(cap) || 6)
|
|
537
|
+
if (times.length < n) return now
|
|
538
|
+
// 需要等最早的那 n 个里第一个过期:第 (times.length - n) 个之后的那次过期就空出一个名额
|
|
539
|
+
const freeing = times[times.length - n]
|
|
540
|
+
return freeing + 60 * 60 * 1000
|
|
541
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-session-superagent",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
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",
|