dsh-session-superagent 0.9.230 → 0.9.232
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 +7298 -7236
- package/README.md +1 -1
- package/client.js +1 -1
- package/lib/index.js +1 -1
- package/lib/watch.js +1193 -1192
- package/package.json +1 -1
package/lib/watch.js
CHANGED
|
@@ -1,203 +1,206 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure watcher logic — no I/O, no timers, no Cordis.
|
|
3
|
-
*
|
|
4
|
-
* Kept separate so the behaviour that matters (what counts as news, when a wake
|
|
5
|
-
* is allowed, what the wake text says) is unit-testable without a DSH host.
|
|
6
|
-
*/
|
|
7
|
-
import { repoBadges } from './gh-format.js'
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Split items into "report now" and "absorb silently".
|
|
11
|
-
*
|
|
12
|
-
* An item's `updated_at` moves for two different reasons:
|
|
13
|
-
*
|
|
14
|
-
* 1. Genuinely new activity → report it.
|
|
15
|
-
* 2. Our own follow-up actions (comment, close, label) on an item we just
|
|
16
|
-
* handled → the item is newer than its watermark, but it is not news.
|
|
17
|
-
*
|
|
18
|
-
* A pure cooldown is not enough: it only *delays* the second kind, and once the
|
|
19
|
-
* window expires the item resurfaces. So a bump that falls inside the cooldown
|
|
20
|
-
* is **absorbed** — its watermark is advanced without a wake — and never comes
|
|
21
|
-
* back. Only bumps that arrive after the window are reported.
|
|
22
|
-
*
|
|
23
|
-
* @param {Array<{number:number, updated_at:string}>} items
|
|
24
|
-
* @param {{seen?: Record<string,string>, cursor?: string|null, lastReported?: Record<string,number>}} state
|
|
25
|
-
* @param {{now?: number, cooldownMs?: number}} [options]
|
|
26
|
-
* @returns {{fresh: Array<object>, absorbed: Array<object>}}
|
|
27
|
-
*/
|
|
28
|
-
export function partitionItems(items, state = {}, options = {}) {
|
|
29
|
-
const { now = Date.now(), cooldownMs = 0 } = options
|
|
30
|
-
// `items ?? []` 只防 null:手写坏的状态或上游返回对象时,for-of 会抛 TypeError,
|
|
31
|
-
// 而 tick 的 catch 会把它当成「这一轮失败」→ 该守永久退避。非数组一律当空。
|
|
32
|
-
if (!Array.isArray(items)) return { fresh: [], absorbed: [] }
|
|
33
|
-
const seen = state.seen ?? {}
|
|
34
|
-
const reported = state.lastReported ?? {}
|
|
35
|
-
const floor = state.cursor ? Date.parse(state.cursor) : 0
|
|
36
|
-
const fresh = []
|
|
37
|
-
const absorbed = []
|
|
38
|
-
for (const item of items ?? []) {
|
|
39
|
-
const key = String(item.number)
|
|
40
|
-
const prev = seen[key]
|
|
41
|
-
const baseline = prev ? Date.parse(prev) : floor
|
|
42
|
-
if (!(Date.parse(item.updated_at) > baseline)) continue
|
|
43
|
-
const handledAt = reported[key]
|
|
44
|
-
if (cooldownMs > 0 && handledAt && now - handledAt < cooldownMs) absorbed.push(item)
|
|
45
|
-
else fresh.push(item)
|
|
46
|
-
}
|
|
47
|
-
return { fresh, absorbed }
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Backwards-compatible view: only the items that warrant a wake.
|
|
52
|
-
* @param {Array<object>} items
|
|
53
|
-
* @param {object} state
|
|
54
|
-
* @param {{now?: number, cooldownMs?: number}} [options]
|
|
55
|
-
* @returns {Array<object>}
|
|
56
|
-
*/
|
|
57
|
-
export function splitFresh(items, state = {}, options = {}) {
|
|
58
|
-
return partitionItems(items, state, options).fresh
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/** Remember when each item was reported, so the cooldown window can start. */
|
|
62
|
-
export function recordReported(state, fresh, now = Date.now()) {
|
|
63
|
-
const next = { ...(state.lastReported ?? {}) }
|
|
64
|
-
for (const item of fresh ?? []) next[String(item.number)] = now
|
|
65
|
-
return next
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* May we wake now?
|
|
70
|
-
*
|
|
71
|
-
* A followup spliced while the agent is driving a turn lands in the client's
|
|
72
|
-
* pending-input strip; the client can later restore already-sent input as a
|
|
73
|
-
* draft and re-submit it. Waiting for `idle` costs at most one poll interval.
|
|
74
|
-
*
|
|
75
|
-
* @param {string|undefined} status agent status (`idle` | `running`)
|
|
76
|
-
* @returns {{ok: true} | {ok: false, reason: string}}
|
|
77
|
-
*/
|
|
78
|
-
export function decideWake(status) {
|
|
79
|
-
if (!status) return { ok: true }
|
|
80
|
-
if (status !== 'idle') return { ok: false, reason: `agent busy (${status})` }
|
|
81
|
-
return { ok: true }
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** Advance per-item watermarks after a successful wake. */
|
|
85
|
-
export function advanceSeen(seen, fresh) {
|
|
86
|
-
const next = { ...(seen ?? {}) }
|
|
87
|
-
for (const item of fresh ?? []) next[String(item.number)] = item.updated_at
|
|
88
|
-
return next
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Build the wake text. Stable shape: one line per item, then the rule reminder. */
|
|
92
|
-
/**
|
|
93
|
-
* 唤醒消息 = 任务书。竞品的触发都带足上下文(类型、状态、作者、标签、是否草稿),
|
|
94
|
-
* 否则 agent 醒来还得自己查一遍。这里一次给全。
|
|
95
|
-
*/
|
|
96
|
-
/**
|
|
97
|
-
* 唤醒消息里的「完成标准」三条。
|
|
98
|
-
*
|
|
99
|
-
* 单独抽出来是因为它曾经写错过一次:`criteria` 里引用了不存在的 `iso`(实际变量叫 `isolation`),
|
|
100
|
-
* 只在**会话空闲、真的走到发消息**那一步才爆 `ReferenceError: iso is not defined` ——
|
|
101
|
-
* 有守但一直是 304/会话忙的话,永远看不到。抽成纯函数后,它就能被用例直接覆盖。
|
|
102
|
-
*/
|
|
103
|
-
export function wakeCriteria({ isolation } = {}) {
|
|
104
|
-
const iso = isolation || {}
|
|
105
|
-
const where = iso.dir
|
|
106
|
-
? '回写到哪:推分支并开 PR,然后在 issue 里留言 + watch_ack 打标签'
|
|
107
|
-
: '回写到哪:在 issue 里留言 + watch_ack 打标签'
|
|
108
|
-
return [
|
|
109
|
-
'改了什么:文件清单 + 一句话为什么(别只写"已修复")',
|
|
110
|
-
'怎么验证的:跑过的测试/命令 + 结果;跑不了就写清为什么跑不了',
|
|
111
|
-
where,
|
|
112
|
-
]
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function formatWatchText(repo, fresh, options = {}) {
|
|
116
|
-
const list = fresh ?? []
|
|
117
|
-
const more = Number(options.more) || 0
|
|
118
|
-
const lines = list.map((item) => {
|
|
119
|
-
const kind = item.pull_request ? 'PR' : 'issue'
|
|
120
|
-
const draft = item.draft ? ' 草稿' : ''
|
|
121
|
-
const who = (item.user && item.user.login) || '?'
|
|
122
|
-
const labels = labelNames(item)
|
|
123
|
-
const tail = [
|
|
124
|
-
`(${kind}${draft})`,
|
|
125
|
-
labels.length > 0 ? `标签: ${labels.join(', ')}` : null,
|
|
126
|
-
(item.assignees ?? []).length > 0 ? `指派: ${item.assignees.map((a) => '@' + a.login).join(' ')}` : null,
|
|
127
|
-
item.html_url,
|
|
128
|
-
(stateReasonNote(item) && String(item.state_reason || '') === 'reopened') ? '⚠ ' + stateReasonNote(item) : null,
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
item.
|
|
137
|
-
? '
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
//
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
.split(
|
|
152
|
-
.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
: ''
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
const
|
|
199
|
-
|
|
200
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Pure watcher logic — no I/O, no timers, no Cordis.
|
|
3
|
+
*
|
|
4
|
+
* Kept separate so the behaviour that matters (what counts as news, when a wake
|
|
5
|
+
* is allowed, what the wake text says) is unit-testable without a DSH host.
|
|
6
|
+
*/
|
|
7
|
+
import { repoBadges } from './gh-format.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Split items into "report now" and "absorb silently".
|
|
11
|
+
*
|
|
12
|
+
* An item's `updated_at` moves for two different reasons:
|
|
13
|
+
*
|
|
14
|
+
* 1. Genuinely new activity → report it.
|
|
15
|
+
* 2. Our own follow-up actions (comment, close, label) on an item we just
|
|
16
|
+
* handled → the item is newer than its watermark, but it is not news.
|
|
17
|
+
*
|
|
18
|
+
* A pure cooldown is not enough: it only *delays* the second kind, and once the
|
|
19
|
+
* window expires the item resurfaces. So a bump that falls inside the cooldown
|
|
20
|
+
* is **absorbed** — its watermark is advanced without a wake — and never comes
|
|
21
|
+
* back. Only bumps that arrive after the window are reported.
|
|
22
|
+
*
|
|
23
|
+
* @param {Array<{number:number, updated_at:string}>} items
|
|
24
|
+
* @param {{seen?: Record<string,string>, cursor?: string|null, lastReported?: Record<string,number>}} state
|
|
25
|
+
* @param {{now?: number, cooldownMs?: number}} [options]
|
|
26
|
+
* @returns {{fresh: Array<object>, absorbed: Array<object>}}
|
|
27
|
+
*/
|
|
28
|
+
export function partitionItems(items, state = {}, options = {}) {
|
|
29
|
+
const { now = Date.now(), cooldownMs = 0 } = options
|
|
30
|
+
// `items ?? []` 只防 null:手写坏的状态或上游返回对象时,for-of 会抛 TypeError,
|
|
31
|
+
// 而 tick 的 catch 会把它当成「这一轮失败」→ 该守永久退避。非数组一律当空。
|
|
32
|
+
if (!Array.isArray(items)) return { fresh: [], absorbed: [] }
|
|
33
|
+
const seen = state.seen ?? {}
|
|
34
|
+
const reported = state.lastReported ?? {}
|
|
35
|
+
const floor = state.cursor ? Date.parse(state.cursor) : 0
|
|
36
|
+
const fresh = []
|
|
37
|
+
const absorbed = []
|
|
38
|
+
for (const item of items ?? []) {
|
|
39
|
+
const key = String(item.number)
|
|
40
|
+
const prev = seen[key]
|
|
41
|
+
const baseline = prev ? Date.parse(prev) : floor
|
|
42
|
+
if (!(Date.parse(item.updated_at) > baseline)) continue
|
|
43
|
+
const handledAt = reported[key]
|
|
44
|
+
if (cooldownMs > 0 && handledAt && now - handledAt < cooldownMs) absorbed.push(item)
|
|
45
|
+
else fresh.push(item)
|
|
46
|
+
}
|
|
47
|
+
return { fresh, absorbed }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Backwards-compatible view: only the items that warrant a wake.
|
|
52
|
+
* @param {Array<object>} items
|
|
53
|
+
* @param {object} state
|
|
54
|
+
* @param {{now?: number, cooldownMs?: number}} [options]
|
|
55
|
+
* @returns {Array<object>}
|
|
56
|
+
*/
|
|
57
|
+
export function splitFresh(items, state = {}, options = {}) {
|
|
58
|
+
return partitionItems(items, state, options).fresh
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Remember when each item was reported, so the cooldown window can start. */
|
|
62
|
+
export function recordReported(state, fresh, now = Date.now()) {
|
|
63
|
+
const next = { ...(state.lastReported ?? {}) }
|
|
64
|
+
for (const item of fresh ?? []) next[String(item.number)] = now
|
|
65
|
+
return next
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* May we wake now?
|
|
70
|
+
*
|
|
71
|
+
* A followup spliced while the agent is driving a turn lands in the client's
|
|
72
|
+
* pending-input strip; the client can later restore already-sent input as a
|
|
73
|
+
* draft and re-submit it. Waiting for `idle` costs at most one poll interval.
|
|
74
|
+
*
|
|
75
|
+
* @param {string|undefined} status agent status (`idle` | `running`)
|
|
76
|
+
* @returns {{ok: true} | {ok: false, reason: string}}
|
|
77
|
+
*/
|
|
78
|
+
export function decideWake(status) {
|
|
79
|
+
if (!status) return { ok: true }
|
|
80
|
+
if (status !== 'idle') return { ok: false, reason: `agent busy (${status})` }
|
|
81
|
+
return { ok: true }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** Advance per-item watermarks after a successful wake. */
|
|
85
|
+
export function advanceSeen(seen, fresh) {
|
|
86
|
+
const next = { ...(seen ?? {}) }
|
|
87
|
+
for (const item of fresh ?? []) next[String(item.number)] = item.updated_at
|
|
88
|
+
return next
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Build the wake text. Stable shape: one line per item, then the rule reminder. */
|
|
92
|
+
/**
|
|
93
|
+
* 唤醒消息 = 任务书。竞品的触发都带足上下文(类型、状态、作者、标签、是否草稿),
|
|
94
|
+
* 否则 agent 醒来还得自己查一遍。这里一次给全。
|
|
95
|
+
*/
|
|
96
|
+
/**
|
|
97
|
+
* 唤醒消息里的「完成标准」三条。
|
|
98
|
+
*
|
|
99
|
+
* 单独抽出来是因为它曾经写错过一次:`criteria` 里引用了不存在的 `iso`(实际变量叫 `isolation`),
|
|
100
|
+
* 只在**会话空闲、真的走到发消息**那一步才爆 `ReferenceError: iso is not defined` ——
|
|
101
|
+
* 有守但一直是 304/会话忙的话,永远看不到。抽成纯函数后,它就能被用例直接覆盖。
|
|
102
|
+
*/
|
|
103
|
+
export function wakeCriteria({ isolation } = {}) {
|
|
104
|
+
const iso = isolation || {}
|
|
105
|
+
const where = iso.dir
|
|
106
|
+
? '回写到哪:推分支并开 PR,然后在 issue 里留言 + watch_ack 打标签'
|
|
107
|
+
: '回写到哪:在 issue 里留言 + watch_ack 打标签'
|
|
108
|
+
return [
|
|
109
|
+
'改了什么:文件清单 + 一句话为什么(别只写"已修复")',
|
|
110
|
+
'怎么验证的:跑过的测试/命令 + 结果;跑不了就写清为什么跑不了',
|
|
111
|
+
where,
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function formatWatchText(repo, fresh, options = {}) {
|
|
116
|
+
const list = fresh ?? []
|
|
117
|
+
const more = Number(options.more) || 0
|
|
118
|
+
const lines = list.map((item) => {
|
|
119
|
+
const kind = item.pull_request ? 'PR' : 'issue'
|
|
120
|
+
const draft = item.draft ? ' 草稿' : ''
|
|
121
|
+
const who = (item.user && item.user.login) || '?'
|
|
122
|
+
const labels = labelNames(item)
|
|
123
|
+
const tail = [
|
|
124
|
+
`(${kind}${draft})`,
|
|
125
|
+
labels.length > 0 ? `标签: ${labels.join(', ')}` : null,
|
|
126
|
+
(item.assignees ?? []).length > 0 ? `指派: ${item.assignees.map((a) => '@' + a.login).join(' ')}` : null,
|
|
127
|
+
item.html_url,
|
|
128
|
+
(stateReasonNote(item) && String(item.state_reason || '') === 'reopened') ? '⚠ ' + stateReasonNote(item) : null,
|
|
129
|
+
// T31 曾建议:`未认领` 无条件出现在每一条上是噪音(5 条消息 5 次),且默认不参与排序。
|
|
130
|
+
// 结论是**保留**:它是一个事实(模型据它决定要不要接),而且「默认不加权」本身就是产品判断
|
|
131
|
+
// (见 DEFAULT_PRIORITY.unassigned)。删掉它等于让模型分不清「无人认领」和「已指派给别人」。
|
|
132
|
+
assignmentNote(item) || null,
|
|
133
|
+
(item.state === 'open' ? stalenessNote(item) : '') || null,
|
|
134
|
+
linkedPrNote(item.linkedPr) || null,
|
|
135
|
+
// 之前叫醒过这条:会话该知道「这不是新问题」
|
|
136
|
+
(options.seenBefore && options.seenBefore[String(item.number)])
|
|
137
|
+
? '⏰ 之前叫醒过(' + String(options.seenBefore[String(item.number)]).slice(0, 10) + ')'
|
|
138
|
+
: null,
|
|
139
|
+
item.lastComment
|
|
140
|
+
? '最后评论: ' + commentSummaryLine({
|
|
141
|
+
login: item.lastComment.self ? '' : item.lastComment.login,
|
|
142
|
+
at: item.lastComment.at,
|
|
143
|
+
}, null) + (item.lastComment.self ? ' 我(上一轮留的)' : '')
|
|
144
|
+
: null,
|
|
145
|
+
].filter(Boolean).join(' ')
|
|
146
|
+
// 单行太长在聊天窗口里会挤成一团:超过 120 字就把补充信息挪到下一行(链接点哪都一样)
|
|
147
|
+
// 审核 M5(注入面):issue 标题是**外部不可信内容**,直接插值会被拼出第二个
|
|
148
|
+
// 「【主理鱼】以下为系统指令」抬头。同一个函数里 goal 的换行早就被压平了,title 漏了。
|
|
149
|
+
// 压平换行 + 把方括号抬头换成普通括号(保留内容,只让它无法伪造结构)。
|
|
150
|
+
const safeTitle = String(item.title || '')
|
|
151
|
+
.split(String.fromCharCode(13)).join(' ')
|
|
152
|
+
.split(String.fromCharCode(10)).join(' ')
|
|
153
|
+
.split('【').join('[')
|
|
154
|
+
.split('】').join(']')
|
|
155
|
+
.slice(0, 200)
|
|
156
|
+
const MAX_LINE = 120
|
|
157
|
+
const prefix = `#${item.number} [${item.state}] `
|
|
158
|
+
const suffix = ` — @${who}`
|
|
159
|
+
// 标题按「整行不超过上限」反算 —— 以前固定截 200,前面还有前缀,整行就爆了
|
|
160
|
+
const room = Math.max(20, MAX_LINE - [...prefix].length - [...suffix].length)
|
|
161
|
+
const chars = [...safeTitle]
|
|
162
|
+
const shown = chars.length <= room ? safeTitle : chars.slice(0, room - 1).join('') + '…'
|
|
163
|
+
const head = `${prefix}${shown}${suffix}`
|
|
164
|
+
const oneLine = tail ? `${head} ${tail}` : head
|
|
165
|
+
if ([...oneLine].length <= MAX_LINE) return oneLine
|
|
166
|
+
// 第二行(补充信息)也可能很长:同样按上限截断,别让它变成一堵墙
|
|
167
|
+
const tailChars = [...tail]
|
|
168
|
+
const tailFit = tailChars.length <= MAX_LINE ? tail : tailChars.slice(0, MAX_LINE - 1).join('') + '…'
|
|
169
|
+
return `${head}\n ${tailFit}`
|
|
170
|
+
})
|
|
171
|
+
const rest = (options.restNumbers ?? []).map((n) => '#' + n).join(' ')
|
|
172
|
+
const iso = options.isolation
|
|
173
|
+
const isoLine = iso && iso.dir
|
|
174
|
+
? `\n【隔离工作区】${iso.dir}(分支 ${iso.branch})—— 只在这里改,推这个分支开 PR,别碰主工作区;用完 git worktree remove 掉。`
|
|
175
|
+
: iso && iso.error
|
|
176
|
+
? `\n【隔离失败】${iso.error};这次请谨慎,改完别留下未提交内容。`
|
|
177
|
+
: ''
|
|
178
|
+
const goalLine = options.goal
|
|
179
|
+
? `${String.fromCharCode(10)}本条目主理目标:${String(options.goal).split(String.fromCharCode(13)).join(' ').split(String.fromCharCode(10)).join(' ').slice(0, 200)}`
|
|
180
|
+
: ''
|
|
181
|
+
// 身份说明:插件调 gh 用的是它绑定的账号,会话里跑的 git push 用的是会话自己的环境。
|
|
182
|
+
// 不说明的话,gh 评论挂智能体名下、git 提交挂操作者名下 —— 一次改动两个作者。
|
|
183
|
+
//
|
|
184
|
+
// 注意:**这里不写路径**(PR #7 原本把 GH_CONFIG_DIR=<绝对路径> 塞进来)。唤醒消息会进模型上下文、
|
|
185
|
+
// 进对话记录、可能被后续总结引用;带路径就等于把本机用户目录一并带出去。
|
|
186
|
+
// 会话只要知道「该问用户」就够了 —— 用户本来就知道自己的路径。
|
|
187
|
+
const identityLine = options.identityBound
|
|
188
|
+
? String.fromCharCode(10) + '【提交身份】本插件的 gh 已绑定单独的智能体账号。你在会话里执行 git / git push 时如需一致身份,'
|
|
189
|
+
+ '请向用户索取该账号的 gh 配置目录并设 GH_CONFIG_DIR;否则提交会落到操作者账号上。'
|
|
190
|
+
: ''
|
|
191
|
+
const moreLine = more > 0
|
|
192
|
+
? `\n(另有 ${more} 条:${rest},处理完这批再看)`
|
|
193
|
+
: ''
|
|
194
|
+
// 「为什么是这几条」:只给结果不给依据,会话/客户都没法判断要不要调过滤条件。
|
|
195
|
+
// 依据:GitHub 的通知带 reason(为什么通知你)、Slack 摘要会说明分组依据 —— 同一个道理。
|
|
196
|
+
const whyLine = (() => {
|
|
197
|
+
const parts = []
|
|
198
|
+
const changed = Number(options.changedTotal)
|
|
199
|
+
const count = list.length
|
|
200
|
+
// 只有一条变化时,解释"为什么是这几条"就是噪音:本来就只有它
|
|
201
|
+
const onlyOne = count <= 1 && (!Number.isFinite(changed) || changed <= 1)
|
|
202
|
+
if (!onlyOne) {
|
|
203
|
+
if (Number.isFinite(changed) && changed > 0) parts.push('本轮共 ' + changed + ' 条变化')
|
|
201
204
|
if (options.batchLimit) {
|
|
202
205
|
// 审核 T31 M3:真实排序是 priorityScore(重开 +100 / 评论 +10 / 标签 +5),不是「最近更新」。
|
|
203
206
|
// 写「按最近更新」等于骗人:客户会以为越新越靠前,实测 5 分钟前的落进「另有」。
|
|
@@ -205,998 +208,996 @@ export function formatWatchText(repo, fresh, options = {}) {
|
|
|
205
208
|
parts.push(priorityOn
|
|
206
209
|
? '按优先级取前 ' + options.batchLimit + ' 条(重开的排最前)'
|
|
207
210
|
: '按优先级取前 ' + options.batchLimit + ' 条')
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
const skipped = options.skippedReasons && typeof options.skippedReasons === 'object'
|
|
211
|
-
? Object.entries(options.skippedReasons).filter(([, n]) => Number(n) > 0).slice(0, 3)
|
|
212
|
-
: []
|
|
213
|
-
if (skipped.length > 0) {
|
|
214
|
-
// 只说「前三个原因」会让人以为只有这些;把总数也报出来
|
|
215
|
-
const total = Object.values(options.skippedReasons).reduce((sum, n) => sum + (Number(n) || 0), 0)
|
|
216
|
-
const more = Object.keys(options.skippedReasons).length > skipped.length ? ' 等' : ''
|
|
217
|
-
parts.push('本轮跳过 ' + total + ' 条(' + skipped.map(([reason, n]) => reason + ' × ' + n).join('、') + more + ')')
|
|
218
|
-
}
|
|
219
|
-
return parts.length > 0 ? String.fromCharCode(10) + '【为什么是这几条】' + parts.join(';') : ''
|
|
220
|
-
})()
|
|
221
|
-
// 完成标准要「可观测」:只说"做好它"等于没说。
|
|
222
|
-
// 依据:Given-When-Then 的核心是把验收写成可观察的结果(martinfowler.com/bliki/GivenWhenThen.html)。
|
|
223
|
-
const accessLine = options.access ? String.fromCharCode(10) + '【权限提示】' + options.access : ''
|
|
224
|
-
const criteria = Array.isArray(options.criteria) ? options.criteria.filter(Boolean).slice(0, 4) : []
|
|
225
|
-
const criteriaLine = criteria.length > 0
|
|
226
|
-
? String.fromCharCode(10) + '【完成标准】' + String.fromCharCode(10)
|
|
227
|
-
+ criteria.map((line, i) => (i + 1) + '. ' + line).join(String.fromCharCode(10))
|
|
228
|
-
: ''
|
|
229
|
-
return (
|
|
230
|
-
(options.firstSync
|
|
231
|
-
? `【主理鱼】${repo} 首次同步:仓库里现有 ${lines.length} 条待处理(接上之前就在的,不是刚刚发生):`
|
|
232
|
-
: `【主理鱼】${repo} 有 ${lines.length} 条新动态:`) + String.fromCharCode(10) +
|
|
233
|
-
lines.join('\n') +
|
|
234
|
-
whyLine +
|
|
235
|
-
moreLine +
|
|
236
|
-
isoLine +
|
|
237
|
-
goalLine +
|
|
238
|
-
identityLine +
|
|
239
|
-
accessLine +
|
|
240
|
-
criteriaLine +
|
|
241
|
-
'\n\n
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
const
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
*
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
if (f.
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
*
|
|
341
|
-
*
|
|
342
|
-
*
|
|
343
|
-
*
|
|
344
|
-
*
|
|
345
|
-
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
348
|
-
*
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
export
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
if (on.
|
|
359
|
-
|
|
360
|
-
if (on.
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
const
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
.map((
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
*
|
|
381
|
-
*
|
|
382
|
-
*
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
*
|
|
388
|
-
*
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
*
|
|
394
|
-
*
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
entry.
|
|
406
|
-
|
|
407
|
-
entry.
|
|
408
|
-
|
|
409
|
-
entry.
|
|
410
|
-
entry
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
*
|
|
422
|
-
*
|
|
423
|
-
*
|
|
424
|
-
*
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
export const
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
const
|
|
433
|
-
|
|
434
|
-
const
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
*
|
|
463
|
-
*
|
|
464
|
-
*
|
|
465
|
-
*
|
|
466
|
-
* @
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
//
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
export
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
.
|
|
517
|
-
.
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
const
|
|
529
|
-
const
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
id
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
*
|
|
548
|
-
*
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
const
|
|
554
|
-
const
|
|
555
|
-
const
|
|
556
|
-
const
|
|
557
|
-
const
|
|
558
|
-
const
|
|
559
|
-
const
|
|
560
|
-
const
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
*
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
if (!
|
|
577
|
-
const
|
|
578
|
-
|
|
579
|
-
const
|
|
580
|
-
const
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
'
|
|
599
|
-
|
|
600
|
-
'
|
|
601
|
-
//
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
//
|
|
605
|
-
|
|
606
|
-
//
|
|
607
|
-
//
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
'
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
//
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
const
|
|
649
|
-
if (
|
|
650
|
-
|
|
651
|
-
if ('
|
|
652
|
-
if ('
|
|
653
|
-
if ('
|
|
654
|
-
if ('
|
|
655
|
-
if ('
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
*
|
|
663
|
-
*
|
|
664
|
-
*
|
|
665
|
-
*
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
*
|
|
701
|
-
*
|
|
702
|
-
*
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
if (!
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
const
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
*
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
*
|
|
738
|
-
*
|
|
739
|
-
*
|
|
740
|
-
*
|
|
741
|
-
*
|
|
742
|
-
*
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
const
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
*
|
|
761
|
-
*
|
|
762
|
-
*
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
const
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
//
|
|
794
|
-
|
|
795
|
-
//
|
|
796
|
-
|
|
797
|
-
//
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
*
|
|
819
|
-
*
|
|
820
|
-
*
|
|
821
|
-
*
|
|
822
|
-
*
|
|
823
|
-
*
|
|
824
|
-
*
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
const
|
|
831
|
-
if (
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
*
|
|
839
|
-
*
|
|
840
|
-
*
|
|
841
|
-
*
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
const
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
at,
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
last.
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
const
|
|
877
|
-
if (
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
*
|
|
888
|
-
*
|
|
889
|
-
*
|
|
890
|
-
*
|
|
891
|
-
*
|
|
892
|
-
*
|
|
893
|
-
*
|
|
894
|
-
*
|
|
895
|
-
*
|
|
896
|
-
*
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
const
|
|
903
|
-
if (
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
*
|
|
911
|
-
*
|
|
912
|
-
*
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
group.
|
|
923
|
-
if (watcher.
|
|
924
|
-
|
|
925
|
-
if (watcher.
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
*
|
|
940
|
-
*
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
export
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
*
|
|
953
|
-
*
|
|
954
|
-
*
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
const
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
*
|
|
967
|
-
*
|
|
968
|
-
*
|
|
969
|
-
*
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
if (!
|
|
976
|
-
const
|
|
977
|
-
|
|
978
|
-
const
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
.
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
.
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
if (
|
|
992
|
-
else if (
|
|
993
|
-
else if (
|
|
994
|
-
else
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
*
|
|
1003
|
-
*
|
|
1004
|
-
*
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
const
|
|
1010
|
-
|
|
1011
|
-
const
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
*
|
|
1020
|
-
*
|
|
1021
|
-
*
|
|
1022
|
-
*
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
const
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
const
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
*
|
|
1054
|
-
*
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
*
|
|
1068
|
-
*
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
const
|
|
1074
|
-
const
|
|
1075
|
-
const
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
cap,
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
*
|
|
1113
|
-
*
|
|
1114
|
-
*
|
|
1115
|
-
*
|
|
1116
|
-
*
|
|
1117
|
-
*
|
|
1118
|
-
*
|
|
1119
|
-
|
|
1120
|
-
*
|
|
1121
|
-
|
|
1122
|
-
export
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
}
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
*
|
|
1136
|
-
*
|
|
1137
|
-
*
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
const
|
|
1165
|
-
if (
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
*
|
|
1180
|
-
*
|
|
1181
|
-
*
|
|
1182
|
-
*
|
|
1183
|
-
*
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
return next
|
|
1199
|
-
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const skipped = options.skippedReasons && typeof options.skippedReasons === 'object'
|
|
214
|
+
? Object.entries(options.skippedReasons).filter(([, n]) => Number(n) > 0).slice(0, 3)
|
|
215
|
+
: []
|
|
216
|
+
if (skipped.length > 0) {
|
|
217
|
+
// 只说「前三个原因」会让人以为只有这些;把总数也报出来
|
|
218
|
+
const total = Object.values(options.skippedReasons).reduce((sum, n) => sum + (Number(n) || 0), 0)
|
|
219
|
+
const more = Object.keys(options.skippedReasons).length > skipped.length ? ' 等' : ''
|
|
220
|
+
parts.push('本轮跳过 ' + total + ' 条(' + skipped.map(([reason, n]) => reason + ' × ' + n).join('、') + more + ')')
|
|
221
|
+
}
|
|
222
|
+
return parts.length > 0 ? String.fromCharCode(10) + '【为什么是这几条】' + parts.join(';') : ''
|
|
223
|
+
})()
|
|
224
|
+
// 完成标准要「可观测」:只说"做好它"等于没说。
|
|
225
|
+
// 依据:Given-When-Then 的核心是把验收写成可观察的结果(martinfowler.com/bliki/GivenWhenThen.html)。
|
|
226
|
+
const accessLine = options.access ? String.fromCharCode(10) + '【权限提示】' + options.access : ''
|
|
227
|
+
const criteria = Array.isArray(options.criteria) ? options.criteria.filter(Boolean).slice(0, 4) : []
|
|
228
|
+
const criteriaLine = criteria.length > 0
|
|
229
|
+
? String.fromCharCode(10) + '【完成标准】' + String.fromCharCode(10)
|
|
230
|
+
+ criteria.map((line, i) => (i + 1) + '. ' + line).join(String.fromCharCode(10))
|
|
231
|
+
: ''
|
|
232
|
+
return (
|
|
233
|
+
(options.firstSync
|
|
234
|
+
? `【主理鱼】${repo} 首次同步:仓库里现有 ${lines.length} 条待处理(接上之前就在的,不是刚刚发生):`
|
|
235
|
+
: `【主理鱼】${repo} 有 ${lines.length} 条新动态:`) + String.fromCharCode(10) +
|
|
236
|
+
lines.join('\n') +
|
|
237
|
+
whyLine +
|
|
238
|
+
moreLine +
|
|
239
|
+
isoLine +
|
|
240
|
+
goalLine +
|
|
241
|
+
identityLine +
|
|
242
|
+
accessLine +
|
|
243
|
+
criteriaLine +
|
|
244
|
+
'\n\n接下来:先读仓库 AGENTS.md 定「修 / 回 / 待定」;没有测试结果不算完成;' +
|
|
245
|
+
(options.provider === 'gitee'
|
|
246
|
+
// Gitee 没有 gh CLI:回写走网页/API,别让会话去跑 gh 命令
|
|
247
|
+
? '做完一条就写回 Gitee(网页或 API 都行)+ watch_ack 打标签,不要扩大范围。'
|
|
248
|
+
: '做完一条就写回 GitHub + watch_ack 打标签,不要扩大范围。')
|
|
249
|
+
)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// ── 竞品蒸馏出来的四道闸(见 docs/competitive-analysis.md) ────────────────
|
|
253
|
+
|
|
254
|
+
export const DEFAULT_FILTERS = {
|
|
255
|
+
labels: [],
|
|
256
|
+
ignoreLabels: [],
|
|
257
|
+
skipBots: true,
|
|
258
|
+
skipDrafts: true,
|
|
259
|
+
skipClosed: true,
|
|
260
|
+
onlyAssigned: false,
|
|
261
|
+
assignee: null,
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** 宽容地把用户传进来的 filters 规范化,缺字段走默认。 */
|
|
265
|
+
export function normalizeFilters(raw) {
|
|
266
|
+
const f = raw && typeof raw === 'object' ? raw : {}
|
|
267
|
+
const list = (v) => (Array.isArray(v) ? v.map(String).map((s) => s.trim()).filter(Boolean) : [])
|
|
268
|
+
return {
|
|
269
|
+
labels: list(f.labels),
|
|
270
|
+
ignoreLabels: list(f.ignoreLabels),
|
|
271
|
+
skipBots: f.skipBots !== false,
|
|
272
|
+
skipDrafts: f.skipDrafts !== false,
|
|
273
|
+
skipClosed: f.skipClosed !== false,
|
|
274
|
+
onlyAssigned: f.onlyAssigned === true,
|
|
275
|
+
assignee: typeof f.assignee === 'string' && f.assignee.trim() ? f.assignee.trim() : null,
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const labelNames = (item) => (item.labels ?? []).map((l) => (typeof l === 'string' ? l : l && l.name)).filter(Boolean)
|
|
280
|
+
const isBot = (item) => {
|
|
281
|
+
const user = item.user || {}
|
|
282
|
+
return user.type === 'Bot' || /\[bot\]$/i.test(String(user.login || ''))
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* 触发要有许可:不是仓库里任何风吹草动都叫醒会话。
|
|
287
|
+
* 返回 false 的条目直接不进候选集,也不会污染 seen 水位。
|
|
288
|
+
*/
|
|
289
|
+
/**
|
|
290
|
+
* 为什么这条被过滤掉?返回 null 表示通过,否则返回一个可读的原因码。
|
|
291
|
+
* 拆出原因是为了面板能展示「最近被过滤了什么、因为哪条规则」——
|
|
292
|
+
* 否则「不在干活」和「在过滤」在界面上长得一样。
|
|
293
|
+
*/
|
|
294
|
+
export function filterReason(item, filters = DEFAULT_FILTERS) {
|
|
295
|
+
const f = { ...DEFAULT_FILTERS, ...(filters || {}) }
|
|
296
|
+
if (f.skipClosed && item.state && item.state !== 'open') return '已关闭'
|
|
297
|
+
if (f.skipDrafts && item.draft) return '草稿 PR'
|
|
298
|
+
if (f.skipBots && isBot(item)) return '机器人'
|
|
299
|
+
|
|
300
|
+
const names = labelNames(item).map((n) => String(n).toLowerCase())
|
|
301
|
+
const lower = (arr) => (arr || []).map((n) => String(n).toLowerCase())
|
|
302
|
+
const ignored = lower(f.ignoreLabels).find((n) => names.includes(n))
|
|
303
|
+
if (ignored) return '排除标签: ' + ignored
|
|
304
|
+
if (f.labels.length > 0 && !lower(f.labels).some((n) => names.includes(n))) {
|
|
305
|
+
return '不在标签白名单: ' + f.labels.join(',')
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (f.onlyAssigned) {
|
|
309
|
+
const assignees = (item.assignees ?? []).map((a) => String((a && a.login) || a).toLowerCase())
|
|
310
|
+
if (f.assignee) return assignees.includes(f.assignee.toLowerCase()) ? null : '未指派给 @' + f.assignee
|
|
311
|
+
return assignees.length > 0 ? null : '没有指派给人'
|
|
312
|
+
}
|
|
313
|
+
return null
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function passesFilters(item, filters = DEFAULT_FILTERS) {
|
|
317
|
+
return filterReason(item, filters) === null
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** 环形缓冲:最近被过滤的条目(默认留 5 条),够解释「为什么没动」就行。 */
|
|
321
|
+
export function pushFiltered(log, record, max = 5) {
|
|
322
|
+
const list = Array.isArray(log) ? log.slice() : []
|
|
323
|
+
list.push(record)
|
|
324
|
+
return list.slice(-Math.max(1, max))
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** 单次唤醒最多带几条;其余留到下一轮,别让一次唤醒把上下文撑爆。 */
|
|
328
|
+
/** 记一条「被冷却期吸收」:只有记下来,客户问「这条为什么没叫醒我」才答得准。 */
|
|
329
|
+
// 吸收日志只用于回答「这条最近为什么没叫醒我」,20 条足够覆盖最近几轮;
|
|
330
|
+
// 50 条时状态文件会多几十 KB(实测 20 守 × 30 天从 204 KB 降到约 170 KB)。
|
|
331
|
+
export function pushAbsorbed(log, items, at, max = 20) {
|
|
332
|
+
const list = Array.isArray(log) ? log.slice() : []
|
|
333
|
+
for (const item of items ?? []) {
|
|
334
|
+
list.push({ at, number: String(item.number), title: String(item.title || '').slice(0, 80) })
|
|
335
|
+
}
|
|
336
|
+
return list.slice(-Math.max(1, max))
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* 优先级:一次只能处理 N 条时,该先处理哪 N 条。
|
|
342
|
+
*
|
|
343
|
+
* 依据:GitHub 的 issue 列表支持 `sort=created|updated|comments`(服务端排序),
|
|
344
|
+
* 但它排的是"哪条更热",我们要的是"哪条更该现在处理"——两者不同,所以在本地排。
|
|
345
|
+
* 出处:https://docs.github.com/en/rest/issues/issues#list-repository-issues
|
|
346
|
+
*
|
|
347
|
+
* 权重(简单、可解释、可测):
|
|
348
|
+
* 被重开 100 —— 之前判过"不做"被推翻,最该看
|
|
349
|
+
* 有人评论 10 —— 说明在等回应
|
|
350
|
+
* 命中我们的标签 5
|
|
351
|
+
* 其余按 GitHub 的 updated 顺序(稳定排序保持原相对次序)
|
|
352
|
+
*/
|
|
353
|
+
export const DEFAULT_PRIORITY = { reopened: true, comments: true, labels: true, unassigned: false }
|
|
354
|
+
|
|
355
|
+
export function priorityScore(item, options) {
|
|
356
|
+
if (!item || typeof item !== 'object') return 0
|
|
357
|
+
const on = { ...DEFAULT_PRIORITY, ...(options || {}) }
|
|
358
|
+
let score = 0
|
|
359
|
+
if (on.reopened !== false && String(item.state_reason || '') === 'reopened') score += 100
|
|
360
|
+
if (on.comments !== false && Number(item.comments) > 0) score += 10
|
|
361
|
+
if (on.labels !== false && Array.isArray(item.labels) && item.labels.length > 0) score += 5
|
|
362
|
+
// 默认关闭:这是产品判断而非规则,客户想用再开
|
|
363
|
+
if (on.unassigned === true && assignmentNote(item) !== '') score += 3
|
|
364
|
+
if (on.stale === true && stalenessNote(item) !== '') score += 2
|
|
365
|
+
return score
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export function batchFresh(fresh, limit = 3, priority) {
|
|
369
|
+
const list = Array.isArray(fresh) ? fresh.slice() : []
|
|
370
|
+
const n = Math.max(1, Number(limit) || 3)
|
|
371
|
+
// 稳定排序:分数相同就保持 GitHub 给的 updated 顺序
|
|
372
|
+
const ranked = list
|
|
373
|
+
.map((item, index) => ({ item, index, score: priorityScore(item, priority) }))
|
|
374
|
+
.sort((a, b) => (b.score - a.score) || (a.index - b.index))
|
|
375
|
+
.map((entry) => entry.item)
|
|
376
|
+
return ranked.slice(0, n)
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** 连续失败指数退避:30s → 60 → 120 → … 上限 30 分钟;成功即复位。 */
|
|
380
|
+
/**
|
|
381
|
+
* 永久性错误(仓库不存在 / 已改名)要等多久再试。
|
|
382
|
+
*
|
|
383
|
+
* 依据 GitHub 官方最佳实践:确认凭据没问题后,「wait much longer before you check again」——
|
|
384
|
+
* 反复请求不存在的资源既浪费额度,还可能触发 secondary rate limit。
|
|
385
|
+
* 出处:https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api
|
|
386
|
+
*/
|
|
387
|
+
/**
|
|
388
|
+
* 进入「不在活跃时段」的等待:顺带把上一次的错误清掉。
|
|
389
|
+
*
|
|
390
|
+
* 为什么:时段外根本不轮询,旧的 lastError 会一直挂着,面板上就变成一条永不消停的「异常」。
|
|
391
|
+
* 依据同 paused-means-no-new-facts:不再更新的状态不该继续代表「现在」。
|
|
392
|
+
*/
|
|
393
|
+
/**
|
|
394
|
+
* 未归类的错误:把原始措辞留一份样本(最多 3 条)到事件流。
|
|
395
|
+
*
|
|
396
|
+
* 为什么:分类器只认我们见过的措辞。没见过的如果只显示「没归类的错误」,
|
|
397
|
+
* 下次还是收集不到 —— 让它在事件流里留个样本,下一轮才能补规则。
|
|
398
|
+
*/
|
|
399
|
+
export function recordOtherError(entry, detail) {
|
|
400
|
+
const text = String(detail || '').trim().slice(0, 120)
|
|
401
|
+
if (!text) return entry
|
|
402
|
+
entry.events = pushEvent(entry.events, { kind: 'other', text })
|
|
403
|
+
return entry
|
|
404
|
+
}
|
|
405
|
+
export function enterActiveHoursDefer(entry, waitMs, hours, at = new Date()) {
|
|
406
|
+
entry.nextAt = new Date(at.getTime() + Number(waitMs || 0)).toISOString()
|
|
407
|
+
entry.lastDeferred = `不在活跃时段(${hours}),${Math.round(Number(waitMs || 0) / 60000)} 分钟后开始`
|
|
408
|
+
entry.deferReason = 'active-hours'
|
|
409
|
+
// 旧错误不是「现在」的事实
|
|
410
|
+
entry.lastError = null
|
|
411
|
+
entry.lastErrorAt = null
|
|
412
|
+
entry.consecutiveFailures = 0
|
|
413
|
+
return entry
|
|
414
|
+
}
|
|
415
|
+
export function permanentBackoffMs(kind, baseMs = 60000) {
|
|
416
|
+
// auth:凭证失效不会自己好 —— 每 60 秒重试既无用,也可能被风控当成异常
|
|
417
|
+
const PERMANENT = new Set(['notfound', 'moved', 'auth'])
|
|
418
|
+
if (!PERMANENT.has(kind)) return 0
|
|
419
|
+
return Math.max(30 * 60 * 1000, Number(baseMs) || 0)
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* 失败后的等待时间。
|
|
423
|
+
*
|
|
424
|
+
* 前几次按指数退避(上限 30 分钟);**连续失败到第 6 次就不再密集重试**,改成 2 小时一次 ——
|
|
425
|
+
* 思路来自 Google SRE《Handling Overload》的「per-request retry budget」:
|
|
426
|
+
* 重试应当有预算,用完就不能再无限次立刻重来。
|
|
427
|
+
* 出处:https://sre.google/sre-book/handling-overload/
|
|
428
|
+
*/
|
|
429
|
+
export const RETRY_BUDGET_ATTEMPTS = 6
|
|
430
|
+
// 预算外的等待上限(分钟):面板上的「已放慢到 X 小时档」从这里算,不再写死文案
|
|
431
|
+
export const RETRY_SLOW_CAP_MINUTES = 120
|
|
432
|
+
export function nextDelayMs(entry, baseMs) {
|
|
433
|
+
const failures = Number((entry && entry.consecutiveFailures) || 0)
|
|
434
|
+
if (failures <= 0) return baseMs
|
|
435
|
+
const overBudget = failures >= RETRY_BUDGET_ATTEMPTS
|
|
436
|
+
// 预算内:最多 60 倍(默认 60s 间隔 → 1 小时);超出预算:最多 120 倍(→ 2 小时)
|
|
437
|
+
const factor = Math.min(2 ** failures, overBudget ? 120 : 60)
|
|
438
|
+
const cap = overBudget ? RETRY_SLOW_CAP_MINUTES * 60 * 1000 : 30 * 60 * 1000
|
|
439
|
+
return Math.min(Math.max(baseMs * factor, baseMs), cap)
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** 每小时唤醒上限:把过去一小时的唤醒时间戳剪掉再判断。 */
|
|
443
|
+
export function pruneWakeTimes(times, now = Date.now()) {
|
|
444
|
+
const floor = now - 60 * 60 * 1000
|
|
445
|
+
// 手写坏的值("abc" / {} / 0)以前会让这里抛 TypeError:①该守被 tick 的 catch 抓走进永久退避,
|
|
446
|
+
// ②/state 整条 500(面板全灭)。非数组一律当空处理 —— 坏数据的代价不该是「整个面板挂掉」。
|
|
447
|
+
const list = Array.isArray(times) ? times : []
|
|
448
|
+
return list.map(Number).filter((t) => Number.isFinite(t) && t >= floor)
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
/** Highest `updated_at` among fresh items, used as the falling-back global cursor. */
|
|
453
|
+
export function newestUpdatedAt(fresh) {
|
|
454
|
+
let best = null
|
|
455
|
+
for (const item of fresh ?? []) {
|
|
456
|
+
const at = item.updated_at
|
|
457
|
+
if (!best || Date.parse(at) > Date.parse(best)) best = at
|
|
458
|
+
}
|
|
459
|
+
return best
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Pause / resume / delete one watcher, as a pure state transition.
|
|
464
|
+
*
|
|
465
|
+
* The host route and the tests share this so the three actions cannot drift:
|
|
466
|
+
* the route only has to persist the returned state and reconcile its timers.
|
|
467
|
+
* @param state - mutable `{ watchers }` record read from disk.
|
|
468
|
+
* @param request - `{ action, id }`; id is `repo#sessionId`.
|
|
469
|
+
* @returns `{ ok: true, action, id, enabled?, deleted? }` or `{ ok: false, error }`.
|
|
470
|
+
*/
|
|
471
|
+
export function applyWatcherAction(state, { action, id } = {}) {
|
|
472
|
+
const watchers = (state && state.watchers) || {}
|
|
473
|
+
// 审核 T19:`id = "__proto__"` 会命中 Object.prototype —— 随后 `entry.enabled = false`
|
|
474
|
+
// 写到**原型**上,进程级生效(所有缺省 enabled 的守、同进程其它插件一起受影响),
|
|
475
|
+
// 而盘上一个字没改。只认自有属性,且 id 必须是字符串。
|
|
476
|
+
if (typeof id !== 'string' || !id || !Object.prototype.hasOwnProperty.call(watchers, id)) {
|
|
477
|
+
return { ok: false, error: `unknown watcher: ${id || '(missing id)'}` }
|
|
478
|
+
}
|
|
479
|
+
const entry = watchers[id]
|
|
480
|
+
if (action === 'delete') {
|
|
481
|
+
delete watchers[id]
|
|
482
|
+
return { ok: true, action, id, deleted: true }
|
|
483
|
+
}
|
|
484
|
+
if (action === 'pause') {
|
|
485
|
+
entry.enabled = false
|
|
486
|
+
return { ok: true, action, id, enabled: false }
|
|
487
|
+
}
|
|
488
|
+
if (action === 'resume') {
|
|
489
|
+
entry.enabled = true
|
|
490
|
+
// A stale error would make the satellite look broken on the first frame
|
|
491
|
+
// after the operator turns it back on; the next poll repopulates it.
|
|
492
|
+
entry.lastError = null
|
|
493
|
+
return { ok: true, action, id, enabled: true }
|
|
494
|
+
}
|
|
495
|
+
return { ok: false, error: 'action must be pause | resume | delete' }
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// ── DeepSeek 论文蒸馏:记忆要遗忘 + 幂等要留在外部系统 ──────────────────
|
|
499
|
+
|
|
500
|
+
/** 处理完打在这个 issue/PR 上的标签,轮询时直接跳过 —— 换机器也不会重报。 */
|
|
501
|
+
export const DEFAULT_ACK_LABEL = '主理鱼:已处理'
|
|
502
|
+
|
|
503
|
+
export function hasAckLabel(item, label = DEFAULT_ACK_LABEL) {
|
|
504
|
+
const want = String(label).toLowerCase()
|
|
505
|
+
return labelNames(item).some((n) => String(n).toLowerCase() === want)
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* `seen` / `lastReported` 会随条目无限增长(DeepSeek-OCR 那篇把这件事叫记忆遗忘)。
|
|
510
|
+
* 超过 maxAgeMs 或超过 maxEntries 的旧记录直接丢掉 —— 丢掉的代价只是「老 issue 可能被重报一次」。
|
|
511
|
+
*/
|
|
512
|
+
export function pruneSeen(seen, options = {}) {
|
|
513
|
+
// 500 条 × 每条约 60 字节 × 20 条守 → 状态文件会到 200 KB 以上;
|
|
514
|
+
// 去重只需要覆盖「最近几页条目」,200 条足够(列表查询一次最多 100 条/页)。
|
|
515
|
+
const { maxAgeMs = 30 * 24 * 3600 * 1000, maxEntries = 200, now = Date.now() } = options
|
|
516
|
+
const entries = Object.entries(seen ?? {})
|
|
517
|
+
.map(([k, v]) => [k, Date.parse(v)])
|
|
518
|
+
.filter(([, t]) => Number.isFinite(t) && now - t <= maxAgeMs)
|
|
519
|
+
.sort((a, b) => b[1] - a[1])
|
|
520
|
+
.slice(0, maxEntries)
|
|
521
|
+
return Object.fromEntries(entries.map(([k, t]) => [k, new Date(t).toISOString()]))
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* 隔离工作区命名(对应 DeepSeek DSec 的沙箱思路):一次唤醒一个一次性 worktree,
|
|
526
|
+
* 让 agent 在副本里改,主工作区不受污染。纯函数,方便测试。
|
|
527
|
+
*/
|
|
528
|
+
export function worktreePlan({ workdir, repo, sessionTag, stamp = Date.now() } = {}) {
|
|
529
|
+
const safeRepo = String(repo || 'repo').replace(/[^A-Za-z0-9._-]+/g, '-')
|
|
530
|
+
const safeTag = String(sessionTag || 'session').replace(/^session-/, '').replace(/[^A-Za-z0-9._-]+/g, '').slice(0, 8) || 'session'
|
|
531
|
+
const id = `${safeRepo}-${safeTag}-${Number(stamp).toString(36)}`
|
|
532
|
+
const root = String(workdir || '.').replace(/[\\/]+$/, '')
|
|
533
|
+
return {
|
|
534
|
+
id,
|
|
535
|
+
dir: `${root}/.dsh-zhuli/${id}`,
|
|
536
|
+
branch: `zhuli/${id}`,
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/**
|
|
541
|
+
* 隔离区(worktree)会随每次唤醒累积。返回超过 keepMs 没动过的那些,交给调用方回收。
|
|
542
|
+
* 纯函数,单测覆盖。
|
|
543
|
+
*/
|
|
544
|
+
/**
|
|
545
|
+
* 该回收的隔离区:**按年龄** + **按数量** 两条线(原来只有年龄)。
|
|
546
|
+
*
|
|
547
|
+
* 为什么补数量:年龄线管不住"堆" —— 上限 6 次/小时 × 保留 3 天 = 最坏 432 个副本。
|
|
548
|
+
* 实测一个 worktree ≈ 该仓库工作树的大小(我们的仓库 1.3 MB,一个 3 MB 文件的测试仓库开出 2.9 MB),
|
|
549
|
+
* 按 10 MB 估就是 4 GB 级占用。
|
|
550
|
+
*
|
|
551
|
+
* `keepCount`:最近 N 个无论多老都留着(正在用的那个不能被删)。
|
|
552
|
+
*/
|
|
553
|
+
export function staleWorktrees(entries, options = {}) {
|
|
554
|
+
const { now = Date.now(), keepMs = 3 * 24 * 3600 * 1000, keepCount = 5 } = options
|
|
555
|
+
const list = (entries ?? []).filter((entry) => entry && entry.name && Number.isFinite(Number(entry.mtimeMs)))
|
|
556
|
+
const byAge = list.filter((entry) => now - Number(entry.mtimeMs) > keepMs)
|
|
557
|
+
const cap = Math.max(0, Number(keepCount) || 0)
|
|
558
|
+
const newest = list.slice().sort((a, b) => Number(b.mtimeMs) - Number(a.mtimeMs)).slice(0, cap)
|
|
559
|
+
const keep = new Set(newest.map((entry) => entry.name))
|
|
560
|
+
const byCount = list.filter((entry) => !keep.has(entry.name))
|
|
561
|
+
const out = []
|
|
562
|
+
const seen = new Set()
|
|
563
|
+
for (const entry of [...byAge, ...byCount]) {
|
|
564
|
+
if (seen.has(entry.name)) continue
|
|
565
|
+
seen.add(entry.name)
|
|
566
|
+
out.push(entry)
|
|
567
|
+
}
|
|
568
|
+
return out
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* 活跃时段控制(对标 Renovate 的 schedule):只在人和 agent 都清醒的时段唤醒,
|
|
573
|
+
* 夜里攒着。spec 形如 "09:00-23:00",支持跨夜 "22:00-06:00";空值或格式不对 = 一直活跃。
|
|
574
|
+
* 用本地时间判断。
|
|
575
|
+
*/
|
|
576
|
+
export function inActiveHours(spec, now = new Date()) {
|
|
577
|
+
if (!spec || typeof spec !== 'string') return true
|
|
578
|
+
const m = spec.match(/^(\d{1,2}):(\d{2})\s*-\s*(\d{1,2}):(\d{2})$/)
|
|
579
|
+
if (!m) return true
|
|
580
|
+
const toMin = (h, mi) => (Number(h) * 60 + Number(mi)) % (24 * 60)
|
|
581
|
+
const start = toMin(m[1], m[2])
|
|
582
|
+
const end = toMin(m[3], m[4])
|
|
583
|
+
const cur = now.getHours() * 60 + now.getMinutes()
|
|
584
|
+
if (start === end) return true
|
|
585
|
+
return start < end ? cur >= start && cur < end : cur >= start || cur < end
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* 唤醒历史(面板要能看见「上一轮干了什么」)。只保留最近 max 条。
|
|
590
|
+
*/
|
|
591
|
+
export function pushHistory(history, record, max = 20) {
|
|
592
|
+
const list = Array.isArray(history) ? history.slice() : []
|
|
593
|
+
list.push(record)
|
|
594
|
+
return list.slice(-Math.max(1, max))
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/** 只允许从内存 entry 写回的轮询结果字段。配置类字段(enabled/filters/…)以盘上为准。 */
|
|
598
|
+
export const POLL_RESULT_FIELDS = [
|
|
599
|
+
'lastPoll', 'lastError', 'nextAt', 'consecutiveFailures', 'seen', 'cursor', 'lastReported',
|
|
600
|
+
'lastWake', 'lastWakeCount', 'lastBatch', 'lastAbsorbed', 'wakeTimes', 'lastDeferred',
|
|
601
|
+
'history', 'lastWorktree', 'lastAcked', 'lastTrigger',
|
|
602
|
+
// 过滤记录也是轮询结果:漏了它面板上的「最近被过滤」永远是空的
|
|
603
|
+
'filteredLog',
|
|
604
|
+
// lastReopened / lastStates 是「这一轮判出重新打开」的产物:漏了它们,
|
|
605
|
+
// /state 永远回 null,面板上的 ⚠ 与 problems 也永远不出现(对 GitHub 同样成立)。
|
|
606
|
+
'lastReopened', 'lastStates',
|
|
607
|
+
// 审核(0.9.194)又数出 13 个:**都是轮询结果,却全被写盘丢掉**。
|
|
608
|
+
// 最关键的是 etag —— 丢了它,条件请求永远带不上,每轮都在全量拉;
|
|
609
|
+
// 其次是 needsReauth / lastErrorAt —— doctor 的「凭证失效」分支永远不会触发。
|
|
610
|
+
// 教训:白名单是**手维护的**,新增字段时必须同时加进来,否则「算出来了但没存」。
|
|
611
|
+
'etag', 'needsReauth', 'lastErrorAt', 'deferReason', 'silentWakes', 'prevWakeAt',
|
|
612
|
+
'dailyWakes', 'absorbedLog', 'filterCounts', 'repoAccess', 'sessionMissingSince',
|
|
613
|
+
'minIntervalSec', 'lastCleanupAt',
|
|
614
|
+
]
|
|
615
|
+
|
|
616
|
+
/**
|
|
617
|
+
* 把一次轮询的内存结果合并进盘上记录:配置类字段永远以盘上为准。
|
|
618
|
+
* 这条规则是审核找出来的 —— tick 收尾曾把「暂停」写回成运行中。
|
|
619
|
+
*/
|
|
620
|
+
export function mergePollResult(stored, polled) {
|
|
621
|
+
const merged = { ...(stored || {}) }
|
|
622
|
+
for (const key of POLL_RESULT_FIELDS) {
|
|
623
|
+
if (polled && polled[key] !== undefined) merged[key] = polled[key]
|
|
624
|
+
}
|
|
625
|
+
// 水位只许前进:`seen` / `lastPoll` 是「处理到哪」的标记,不是普通字段。
|
|
626
|
+
// 真机事故(0.9.194 审核 M2):`watch_ack` 把 seen[42] 推到 05:00,
|
|
627
|
+
// 并发 tick 手里还是 04:59 的快照,收尾整份覆盖 → 水位后退 → 冷却期一过同一条又被叫醒。
|
|
628
|
+
if (stored && polled) {
|
|
629
|
+
if (stored.seen && polled.seen) {
|
|
630
|
+
const seen = { ...polled.seen }
|
|
631
|
+
for (const [key, at] of Object.entries(stored.seen)) {
|
|
632
|
+
const prev = seen[key]
|
|
633
|
+
if (!prev || Date.parse(at) > Date.parse(prev)) seen[key] = at
|
|
634
|
+
}
|
|
635
|
+
merged.seen = seen
|
|
636
|
+
}
|
|
637
|
+
if (stored.lastPoll && merged.lastPoll && Date.parse(stored.lastPoll) > Date.parse(merged.lastPoll)) {
|
|
638
|
+
merged.lastPoll = stored.lastPoll
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
return merged
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* 部分更新过滤器:只用 patch 里「出现过的键」覆盖,没出现的保持原值。
|
|
646
|
+
* 踩过的坑:GUI 只发 labels/onlyAssigned,服务端整对象重建会把 ignoreLabels / skip* / assignee 全抹成默认。
|
|
647
|
+
*/
|
|
648
|
+
export function mergeFilters(base, patch) {
|
|
649
|
+
const current = normalizeFilters(base)
|
|
650
|
+
if (!patch || typeof patch !== 'object') return current
|
|
651
|
+
const next = { ...current }
|
|
652
|
+
if ('labels' in patch) next.labels = normalizeFilters({ labels: patch.labels }).labels
|
|
653
|
+
if ('ignoreLabels' in patch) next.ignoreLabels = normalizeFilters({ ignoreLabels: patch.ignoreLabels }).ignoreLabels
|
|
654
|
+
if ('skipBots' in patch) next.skipBots = patch.skipBots !== false
|
|
655
|
+
if ('skipDrafts' in patch) next.skipDrafts = patch.skipDrafts !== false
|
|
656
|
+
if ('skipClosed' in patch) next.skipClosed = patch.skipClosed !== false
|
|
657
|
+
if ('onlyAssigned' in patch) next.onlyAssigned = patch.onlyAssigned === true
|
|
658
|
+
if ('assignee' in patch) next.assignee = normalizeFilters({ assignee: patch.assignee }).assignee
|
|
659
|
+
return next
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* 把 gh 失败时的原始文本归类成「一句话原因 + 该怎么做」。
|
|
664
|
+
*
|
|
665
|
+
* 真机上出现过 `Get "https://api.github.com/...": EOF` 这种直接怼给用户看的原始报错 ——
|
|
666
|
+
* 它既长又吓人,其实是**网络抖动**,自动退避重试就会好。这里把它变成人话。
|
|
667
|
+
*
|
|
668
|
+
* @returns {{ kind: string, short: string, hint: string }}
|
|
669
|
+
*/
|
|
670
|
+
// 拆分后:GitHub 交互层在 gh-format.js,这里转出以保持既有调用方不变
|
|
671
|
+
import {
|
|
672
|
+
assignmentNote, commentSummaryLine, linkedPrNote, stalenessNote, stateReasonNote,
|
|
673
|
+
} from './gh-format.js'
|
|
674
|
+
export {
|
|
675
|
+
TRIAGE_SIGNALS,
|
|
676
|
+
accessNote,
|
|
677
|
+
assignmentNote,
|
|
678
|
+
buildCommentBatchQuery,
|
|
679
|
+
classifyGhError,
|
|
680
|
+
commentSummaryLine,
|
|
681
|
+
countReasons,
|
|
682
|
+
isAuthBroken,
|
|
683
|
+
diagnoseEmptyBody,
|
|
684
|
+
diagnoseBadBody,
|
|
685
|
+
formatGhCall,
|
|
686
|
+
jitterMs,
|
|
687
|
+
linkedPrNote,
|
|
688
|
+
parseCommentBatch,
|
|
689
|
+
parseGhInclude,
|
|
690
|
+
pickLinkedPullRequests,
|
|
691
|
+
pickSuggestion,
|
|
692
|
+
repoBadges,
|
|
693
|
+
repoStructureNote,
|
|
694
|
+
retryAfterMs,
|
|
695
|
+
stalenessNote,
|
|
696
|
+
stateReasonNote,
|
|
697
|
+
throttleDelayMs,
|
|
698
|
+
} from './gh-format.js'
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* 距离「活跃时段开门」还有多久(毫秒);已在时段内返回 0。
|
|
702
|
+
*
|
|
703
|
+
* 为什么要它:以前 activeHours 的判断在**拉完 API 之后**才做 ——
|
|
704
|
+
* 夜里 8 小时照常每 N 秒打一次 GitHub,纯属白跑。现在提前到最前面,
|
|
705
|
+
* 并把下一次轮询安排在"开门那一刻",既不浪费请求,也不会让攒着的条目干等一个周期。
|
|
706
|
+
*/
|
|
707
|
+
export function msUntilActiveWindow(spec, now = new Date()) {
|
|
708
|
+
if (!spec || typeof spec !== 'string') return 0
|
|
709
|
+
const m = spec.match(/^(\d{1,2}):(\d{2})\s*-\s*(\d{1,2}):(\d{2})$/)
|
|
710
|
+
if (!m) return 0
|
|
711
|
+
if (inActiveHours(spec, now)) return 0
|
|
712
|
+
const startMin = (Number(m[1]) * 60 + Number(m[2])) % (24 * 60)
|
|
713
|
+
const curMin = now.getHours() * 60 + now.getMinutes()
|
|
714
|
+
const curSec = now.getSeconds()
|
|
715
|
+
let diffMin = startMin - curMin
|
|
716
|
+
if (diffMin <= 0) diffMin += 24 * 60
|
|
717
|
+
// 精确到分钟的开头,再减掉当前秒数
|
|
718
|
+
return Math.max(1000, diffMin * 60 * 1000 - curSec * 1000)
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
/**
|
|
722
|
+
* 被「每小时上限」挡住时,下一个名额什么时候空出来。
|
|
723
|
+
*
|
|
724
|
+
* 为什么要它:cap=6 时最坏要等近 1 小时(实测:滑动窗口下 6 次集中在开头几分钟,
|
|
725
|
+
* 之后要等最早那次过期)。这段时间里面板只说「已攒 N 条」,客户不知道要等多久,
|
|
726
|
+
* 容易以为卡死了。算出具体时刻,就能写「约 43 分钟后可再唤醒」。
|
|
727
|
+
*/
|
|
728
|
+
export function nextWaketimeAt(wakeTimes, cap, now = Date.now()) {
|
|
729
|
+
const times = pruneWakeTimes(wakeTimes, now).sort((a, b) => a - b)
|
|
730
|
+
const n = Math.max(1, Number(cap) || 6)
|
|
731
|
+
if (times.length < n) return now
|
|
732
|
+
// 需要等最早的那 n 个里第一个过期:第 (times.length - n) 个之后的那次过期就空出一个名额
|
|
733
|
+
const freeing = times[times.length - n]
|
|
734
|
+
return freeing + 60 * 60 * 1000
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* 同一条会话在过去一小时被唤醒了几次(跨所有守)。
|
|
739
|
+
*
|
|
740
|
+
* 为什么需要:`maxWakesPerHour` 是**按守**算的。一条会话守 5 个仓库时,
|
|
741
|
+
* 上限实际是 5 × 6 = 30 次/小时 —— 这根本没保护到"别把会话的上下文打爆",
|
|
742
|
+
* 而那正是当初设这个上限的原因。
|
|
743
|
+
*
|
|
744
|
+
* 类比:只给单个 Pod 设 CPU limit,并不能防止整个 namespace 被打满,所以还要 ResourceQuota。
|
|
745
|
+
* 出处:https://kubernetes.io/docs/concepts/policy/resource-quotas/
|
|
746
|
+
*/
|
|
747
|
+
export function sessionWakesLastHour(entries, sessionId, now = Date.now()) {
|
|
748
|
+
if (!sessionId) return 0
|
|
749
|
+
const list = Array.isArray(entries) ? entries : Object.values(entries || {})
|
|
750
|
+
let total = 0
|
|
751
|
+
for (const entry of list) {
|
|
752
|
+
if (!entry || entry.sessionId !== sessionId) continue
|
|
753
|
+
total += pruneWakeTimes(entry.wakeTimes, now).length
|
|
754
|
+
}
|
|
755
|
+
return total
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
/**
|
|
759
|
+
* 把同一会话在几秒内产生的多条唤醒合并成一条。
|
|
760
|
+
*
|
|
761
|
+
* 实测(本轮):同会话 3 个守、间隔都是 60 秒时,首轮抖动只把相位拉开 ≤5 秒,
|
|
762
|
+
* **之后每一轮都一样** —— 三次唤醒落在 ~1 秒内,会话会连着收到 3 条注入。
|
|
763
|
+
* 5 个守就是 5 条。这正是"上下文被瞬间打爆"的真实形态。
|
|
764
|
+
*
|
|
765
|
+
* 合并而不是"设个间隔挡掉":硬间隔会让先到的守永远赢,后面的守被饿死。
|
|
766
|
+
*/
|
|
767
|
+
export function mergeWakeTexts(parts) {
|
|
768
|
+
const list = (Array.isArray(parts) ? parts : []).filter((t) => typeof t === 'string' && t.trim())
|
|
769
|
+
if (list.length === 0) return ''
|
|
770
|
+
if (list.length === 1) return list[0]
|
|
771
|
+
// 每条唤醒都带着同一段【完成标准】和目标行 —— 合并后重复 3 遍纯属噪音(实测占约 40% 长度)。
|
|
772
|
+
// 只保留第一份;内容不同的(不同仓库的完成标准确实不同)则各自保留。
|
|
773
|
+
const seenCriteria = new Set()
|
|
774
|
+
const seenGoal = new Set()
|
|
775
|
+
const trimmed = list.map((text) => {
|
|
776
|
+
let out = text
|
|
777
|
+
const criteriaAt = out.indexOf('【完成标准】')
|
|
778
|
+
if (criteriaAt >= 0) {
|
|
779
|
+
const block = out.slice(criteriaAt)
|
|
780
|
+
if (seenCriteria.has(block)) out = out.slice(0, criteriaAt).replace(/[\s]+$/, '')
|
|
781
|
+
else seenCriteria.add(block)
|
|
782
|
+
}
|
|
783
|
+
out = out
|
|
784
|
+
.split(String.fromCharCode(10))
|
|
785
|
+
.filter((line) => {
|
|
786
|
+
if (!line.startsWith('本条目主理目标:')) return true
|
|
787
|
+
if (seenGoal.has(line)) return false
|
|
788
|
+
seenGoal.add(line)
|
|
789
|
+
return true
|
|
790
|
+
})
|
|
791
|
+
.join(String.fromCharCode(10))
|
|
792
|
+
return out
|
|
793
|
+
})
|
|
794
|
+
// 「接下来」那一整段是每次唤醒都一样的收尾(三条动作)。合并后重复 N 遍纯噪音:
|
|
795
|
+
// 它就是同一份指令,客户读一遍就够。依据:SRE 的「Every page should be actionable」,
|
|
796
|
+
// 但 actionable 不等于把同一份指令印 N 次。
|
|
797
|
+
const TAIL = '接下来:'
|
|
798
|
+
// 审核 T31 M2:先把「接下来」整段抽出来,再做去重 ——
|
|
799
|
+
// 以前去重会从【完成标准】一口气切到末尾,把「接下来」也切掉,
|
|
800
|
+
// 于是「合并多条」这个最需要指令的场景反而一条指令都没有。
|
|
801
|
+
const tailBlock = (() => {
|
|
802
|
+
for (const text of trimmed) {
|
|
803
|
+
const at = text.indexOf(TAIL)
|
|
804
|
+
if (at >= 0) return text.slice(at).replace(/[\s]+$/, '')
|
|
805
|
+
}
|
|
806
|
+
return ''
|
|
807
|
+
})()
|
|
808
|
+
const stripped = trimmed.map((text) => {
|
|
809
|
+
const at = text.indexOf(TAIL)
|
|
810
|
+
return at < 0 ? text : text.slice(0, at).replace(/[\s]+$/, '')
|
|
811
|
+
})
|
|
812
|
+
|
|
813
|
+
const head = '【主理鱼】这一小段时间内有 ' + list.length + ' 处变化,合并成一条:'
|
|
814
|
+
const body = stripped.map((text, i) => String.fromCharCode(10, 10) + '—— 第 ' + (i + 1) + ' 处 ——' + String.fromCharCode(10) + text).join('')
|
|
815
|
+
return head + body + (tailBlock ? String.fromCharCode(10, 10) + tailBlock : '')
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
/**
|
|
819
|
+
* 该不该现在回收隔离区。
|
|
820
|
+
*
|
|
821
|
+
* 现状:每次唤醒都跑一遍回收 —— 里面是 `git worktree remove` / `branch -D` / `prune`
|
|
822
|
+
* (本机实测 `worktree add` 0.19s,remove 同级;5 个过期副本就是 1 秒级),
|
|
823
|
+
* 而绝大多数时候根本没有过期副本。
|
|
824
|
+
*
|
|
825
|
+
* 节流规则:
|
|
826
|
+
* - 数量**超上限**时必须立刻回收(不能等,否则会堆);
|
|
827
|
+
* - 没超额时,距上次回收不足 `intervalMs`(默认 10 分钟)就跳过。
|
|
828
|
+
*/
|
|
829
|
+
export function shouldRunCleanup({ count = 0, keepCount = 5, lastRunAt = 0, now = Date.now(), intervalMs = 10 * 60 * 1000 } = {}) {
|
|
830
|
+
const total = Number(count) || 0
|
|
831
|
+
const cap = Math.max(1, Number(keepCount) || 5)
|
|
832
|
+
if (total > cap) return { run: true, reason: 'over-cap' }
|
|
833
|
+
const last = Number(lastRunAt) || 0
|
|
834
|
+
if (last > 0 && now - last < Number(intervalMs)) return { run: false, reason: 'throttled' }
|
|
835
|
+
return { run: true, reason: 'interval' }
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* 迷你时间线:最近 N 条事件(唤醒 / 攒着 / 限流 / 回收 / 报错 / ack)。
|
|
840
|
+
*
|
|
841
|
+
* 为什么不用 `history`:那个只记"成功唤醒过哪几条",回答不了"为什么没叫醒"。
|
|
842
|
+
* 事件流按 OTel 的语义组织:一条事件 = 时间 + 类型 + 一句人话 + 可选属性,
|
|
843
|
+
* 有界(只留最近 N 条),所以即使跑一整夜也不会把状态文件撑大。
|
|
844
|
+
* 出处:https://opentelemetry.io/docs/concepts/signals/logs/
|
|
845
|
+
*/
|
|
846
|
+
export function pushEvent(events, event, max = 20) {
|
|
847
|
+
const list = Array.isArray(events) ? events.slice() : []
|
|
848
|
+
const limit = Math.max(1, Number(max) || 20)
|
|
849
|
+
if (!event || typeof event !== 'object' || !event.kind) return list.slice(-limit)
|
|
850
|
+
const at = event.at || new Date().toISOString()
|
|
851
|
+
const next = {
|
|
852
|
+
at,
|
|
853
|
+
// since:首次发生时间。合并时**不更新**,这样"这事从什么时候开始"仍然查得到。
|
|
854
|
+
since: at,
|
|
855
|
+
kind: String(event.kind).slice(0, 24),
|
|
856
|
+
text: String(event.text || '').slice(0, 160),
|
|
857
|
+
}
|
|
858
|
+
// 连续同一条事件合并成一条带次数 —— 否则"撞上限"这种每轮都一样的记录会刷满 20 条,
|
|
859
|
+
// 把真正有用的一次性事件挤掉(噪声淹没信号是这个界面最不该犯的错)。
|
|
860
|
+
const last = list[list.length - 1]
|
|
861
|
+
if (last && last.kind === next.kind && last.text === next.text) {
|
|
862
|
+
last.at = next.at
|
|
863
|
+
last.count = (Number(last.count) || 1) + 1
|
|
864
|
+
if (!last.since) last.since = last.at // 老数据补一个,免得显示成"持续 0 分钟"
|
|
865
|
+
return list.slice(-limit)
|
|
866
|
+
}
|
|
867
|
+
list.push(next)
|
|
868
|
+
return list.slice(-limit)
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* "持续了多久":合并事件时 at 是最新、since 是首次,两者之差就是这个状态持续的时间。
|
|
873
|
+
* 面板上写「×10 · 持续 42 分钟」比只写「×10」有用得多 —— 前者能回答"卡了多久"。
|
|
874
|
+
*/
|
|
875
|
+
export function durationText(sinceIso, untilIso) {
|
|
876
|
+
const from = Date.parse(String(sinceIso || ''))
|
|
877
|
+
const to = Date.parse(String(untilIso || ''))
|
|
878
|
+
if (!Number.isFinite(from) || !Number.isFinite(to) || to < from) return ''
|
|
879
|
+
const minutes = Math.floor((to - from) / 60000)
|
|
880
|
+
if (minutes < 1) return '不到 1 分钟'
|
|
881
|
+
if (minutes < 60) return minutes + ' 分钟'
|
|
882
|
+
const hours = Math.floor(minutes / 60)
|
|
883
|
+
const rest = minutes % 60
|
|
884
|
+
return rest > 0 ? hours + ' 小时 ' + rest + ' 分' : hours + ' 小时'
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
/**
|
|
888
|
+
* "叫醒了但没回音"的连续次数。
|
|
889
|
+
*
|
|
890
|
+
* 为什么值得统计:唤醒只保证**消息送到了**,不保证**有人动手**。
|
|
891
|
+
* 客户最容易遇到的情况恰恰是"守在工作、消息也进去了,但会话没处理" ——
|
|
892
|
+
* 因为仓库的 AGENTS.md 没给规则、或者任务书里的完成标准被忽略。
|
|
893
|
+
* 这类问题在现有指标里完全看不到(wake 计数还在涨)。
|
|
894
|
+
*
|
|
895
|
+
* 判据:上一次唤醒之后有没有出现过 ack:
|
|
896
|
+
* - 有 ack → 连续次数清零;
|
|
897
|
+
* - 没有 → 次数 +1。
|
|
898
|
+
* 依据(ack 语义):RabbitMQ 的 consumer acknowledgement —— 投递成功不代表处理完成,
|
|
899
|
+
* 必须由消费者显式 ack 才能确认。(https://www.rabbitmq.com/docs/confirms)
|
|
900
|
+
*/
|
|
901
|
+
export function silentStreak({ previousWakeAt, lastAckedAt, previousStreak = 0 } = {}) {
|
|
902
|
+
const streak = Math.max(0, Number(previousStreak) || 0)
|
|
903
|
+
const wakeAt = Date.parse(String(previousWakeAt || ''))
|
|
904
|
+
if (!Number.isFinite(wakeAt)) return streak // 还没唤醒过:保持原状
|
|
905
|
+
const ackAt = Date.parse(String(lastAckedAt || ''))
|
|
906
|
+
if (Number.isFinite(ackAt) && ackAt >= wakeAt) return 0
|
|
907
|
+
return streak + 1
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
/**
|
|
911
|
+
* 按仓库汇总:守 5 个仓库时,平铺的列表看不出"哪个健康、哪个一直跳过"。
|
|
912
|
+
*
|
|
913
|
+
* 这是渐进披露(progressive disclosure)在数据层的落实:
|
|
914
|
+
* 先给每仓库一行摘要,细节留在下面的明细里。
|
|
915
|
+
* 出处:https://www.nngroup.com/articles/progressive-disclosure/
|
|
916
|
+
*/
|
|
917
|
+
export function summarizeByRepo(watchers) {
|
|
918
|
+
const list = Array.isArray(watchers) ? watchers.filter((w) => w && w.repo) : []
|
|
919
|
+
const groups = new Map()
|
|
920
|
+
for (const watcher of list) {
|
|
921
|
+
const repo = String(watcher.repo)
|
|
922
|
+
const group = groups.get(repo) || { repo, count: 0, lastWake: null, lastPoll: null, silentWakes: 0, errors: 0, enabled: 0, badges: [] }
|
|
923
|
+
group.count += 1
|
|
924
|
+
if (watcher.enabled !== false) group.enabled += 1
|
|
925
|
+
if (watcher.lastWake && (!group.lastWake || String(watcher.lastWake) > String(group.lastWake))) group.lastWake = watcher.lastWake
|
|
926
|
+
if (watcher.lastPoll && (!group.lastPoll || String(watcher.lastPoll) > String(group.lastPoll))) group.lastPoll = watcher.lastPoll
|
|
927
|
+
group.silentWakes = Math.max(group.silentWakes, Number(watcher.silentWakes) || 0)
|
|
928
|
+
if (watcher.lastError) group.errors += 1
|
|
929
|
+
for (const badge of repoBadges(watcher.repoAccess) || []) {
|
|
930
|
+
if (!group.badges.some((b) => b.text === badge.text)) group.badges.push(badge)
|
|
931
|
+
}
|
|
932
|
+
groups.set(repo, group)
|
|
933
|
+
}
|
|
934
|
+
// 有问题的排前面:错误 > 未回音 >= 2 > 其余;同类按仓库名
|
|
935
|
+
const weight = (g) => (g.errors > 0 ? 0 : g.silentWakes >= 2 ? 1 : 2)
|
|
936
|
+
return [...groups.values()].sort((a, b) => (weight(a) - weight(b)) || a.repo.localeCompare(b.repo))
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
/**
|
|
940
|
+
* 有几条守正被"频率上限"挡着。
|
|
941
|
+
*
|
|
942
|
+
* 之前是面板去匹配提示文案(`lastDeferred.includes('上限')`)—— 文案一改统计就废。
|
|
943
|
+
* 现在服务端在暂停时写一个明确的 `deferReason`,这里只按字段统计。
|
|
944
|
+
*/
|
|
945
|
+
export const CAP_REASONS = ['cap-session', 'cap-watcher']
|
|
946
|
+
|
|
947
|
+
export function countCapped(watchers) {
|
|
948
|
+
const list = Array.isArray(watchers) ? watchers : Object.values(watchers || {})
|
|
949
|
+
return list.filter((w) => w && CAP_REASONS.includes(w.deferReason)).length
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/**
|
|
953
|
+
* 把一批新条目拆成「这次唤醒的」和「剩下的」。
|
|
954
|
+
*
|
|
955
|
+
* 为什么单独成一个函数:剩余绝不能写成 `fresh.slice(listed.length)` ——
|
|
956
|
+
* `batchFresh` 会按优先级重排,切片会把已经选中的条目又算进剩余,
|
|
957
|
+
* 于是唤醒消息里会出现「#77 …(另有 1 条:#77)」这种重复(首次演练脚本抓到过)。
|
|
958
|
+
*/
|
|
959
|
+
export function splitBatch(fresh, limit = 3, priority) {
|
|
960
|
+
const listed = batchFresh(fresh, limit, priority)
|
|
961
|
+
const picked = new Set(listed.map((item) => item.number))
|
|
962
|
+
const rest = (Array.isArray(fresh) ? fresh : []).filter((item) => !picked.has(item.number))
|
|
963
|
+
return { listed, rest }
|
|
964
|
+
}
|
|
965
|
+
|
|
966
|
+
/**
|
|
967
|
+
* 单条条目的追踪:它到底经历了什么(被过滤 / 被冷却吸收 / 上报过 / 还在等)。
|
|
968
|
+
*
|
|
969
|
+
* 客户会问"这条 issue 为什么没叫醒我"。这个问题以前只能靠三处分散的数据
|
|
970
|
+
* (seen 水位、filteredLog、history)自己在脑子里拼 —— 这里拼好并返回结构化结果。
|
|
971
|
+
* 类比 SQL 的 EXPLAIN:不改变行为,只解释"为什么是这个结果"。
|
|
972
|
+
* 出处:https://www.postgresql.org/docs/current/sql-explain.html
|
|
973
|
+
*/
|
|
974
|
+
export function itemTrace(watcher, number) {
|
|
975
|
+
// 注意 String(null) === 'null' —— 先判断入参本身,别让 null 变成一个合法编号
|
|
976
|
+
if (!watcher || number === null || number === undefined || number === '') return null
|
|
977
|
+
const target = String(number).trim()
|
|
978
|
+
if (!target) return null
|
|
979
|
+
const seen = (watcher.seen && watcher.seen[target]) || null
|
|
980
|
+
const reported = (watcher.lastReported && watcher.lastReported[target]) || null
|
|
981
|
+
const filtered = (Array.isArray(watcher.filteredLog) ? watcher.filteredLog : [])
|
|
982
|
+
.filter((entry) => String(entry.number) === target)
|
|
983
|
+
.map((entry) => ({ at: entry.at, reason: entry.reason, title: entry.title }))
|
|
984
|
+
const absorbed = (Array.isArray(watcher.absorbedLog) ? watcher.absorbedLog : [])
|
|
985
|
+
.filter((entry) => String(entry.number) === target)
|
|
986
|
+
.map((entry) => ({ at: entry.at, title: entry.title }))
|
|
987
|
+
const wakes = (Array.isArray(watcher.history) ? watcher.history : [])
|
|
988
|
+
.filter((entry) => Array.isArray(entry.numbers) && entry.numbers.map(String).includes(target))
|
|
989
|
+
.map((entry) => ({ at: entry.at, ok: Boolean(entry.ok), reason: entry.reason || null }))
|
|
990
|
+
|
|
991
|
+
let verdict
|
|
992
|
+
if (filtered.length > 0 && !seen && !reported) verdict = '被过滤掉了:' + (filtered[filtered.length - 1].reason || '未知原因')
|
|
993
|
+
else if (wakes.some((w) => w.ok)) verdict = '已经唤醒过 ' + wakes.filter((w) => w.ok).length + ' 次'
|
|
994
|
+
else if (absorbed.length > 0) verdict = '被冷却期吸收了(' + (absorbed[absorbed.length - 1].at || '未知时间') + ')—— 那一批刚唤醒过,等下一轮'
|
|
995
|
+
else if (seen) verdict = '已记入水位但没唤醒(仅观察,或上报前就已存在)'
|
|
996
|
+
else if (wakes.length > 0) verdict = '尝试唤醒过 ' + wakes.length + ' 次但都没成功'
|
|
997
|
+
else verdict = '还没轮到它(可能还没被轮询到,或在批外)'
|
|
998
|
+
|
|
999
|
+
return { number: target, repo: watcher.repo || null, seen, reported, filtered, absorbed, wakes, verdict }
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* 用满了该调到多少?给一个具体数字,而不是让客户自己算。
|
|
1004
|
+
*
|
|
1005
|
+
* 依据:离线模拟(3 仓库 / 每仓库每小时 10 条 / 会话上限 12)显示
|
|
1006
|
+
* 一小时只能处理 12 条、剩 18 条排队 —— 也就是"想全部处理,需要约 30 次/小时"。
|
|
1007
|
+
* 所以规则取「翻倍 + 封顶 60」,既给足余量,也不至于一次跳到不合理的大数。
|
|
1008
|
+
*/
|
|
1009
|
+
export function suggestSessionCap({ used = 0, cap = 12, capped = 0, max = 60 } = {}) {
|
|
1010
|
+
const u = Math.max(0, Number(used) || 0)
|
|
1011
|
+
const c = Math.max(1, Number(cap) || 12)
|
|
1012
|
+
const blocked = Math.max(0, Number(capped) || 0)
|
|
1013
|
+
if (u < c) return null // 还没用满,不需要建议
|
|
1014
|
+
const doubled = c * 2
|
|
1015
|
+
const wanted = blocked > 0 ? doubled : c + Math.ceil(c / 2)
|
|
1016
|
+
return Math.min(Math.max(1, Number(max) || 60), wanted)
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
/**
|
|
1020
|
+
* 按天累计唤醒次数,只保留最近 N 天(默认 7)。
|
|
1021
|
+
*
|
|
1022
|
+
* 为什么需要:上限建议目前只看"当前这一小时",是瞬时值 ——
|
|
1023
|
+
* 偶尔一次积压就建议翻倍,客户可能被误导。留一份极小的历史(每天一个数,几十字节),
|
|
1024
|
+
* 面板就能显示"过去 7 天共 N 次",让判断有上下文。
|
|
1025
|
+
* 用 map 而不是数组:同一天多次唤醒只加同一条,天然去重。
|
|
1026
|
+
*/
|
|
1027
|
+
export function bumpDailyWakes(daily, atIso = new Date().toISOString(), keepDays = 7) {
|
|
1028
|
+
const day = String(atIso).slice(0, 10)
|
|
1029
|
+
const map = (daily && typeof daily === 'object' && !Array.isArray(daily)) ? { ...daily } : {}
|
|
1030
|
+
if (!/^\d{4}-\d{2}-\d{2}$/.test(day)) return map
|
|
1031
|
+
map[day] = (Number(map[day]) || 0) + 1
|
|
1032
|
+
const days = Object.keys(map).sort().slice(-Math.max(1, Number(keepDays) || 7))
|
|
1033
|
+
const out = {}
|
|
1034
|
+
for (const key of days) out[key] = map[key]
|
|
1035
|
+
return out
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/** 最近几天的总量与日均(给面板显示用;没有数据就返回 null)。 */
|
|
1039
|
+
export function dailySummary(daily, todayIso = new Date().toISOString()) {
|
|
1040
|
+
const map = (daily && typeof daily === 'object' && !Array.isArray(daily)) ? daily : {}
|
|
1041
|
+
const values = Object.values(map).map(Number).filter((n) => Number.isFinite(n) && n >= 0)
|
|
1042
|
+
if (values.length === 0) return null
|
|
1043
|
+
const total = values.reduce((a, b) => a + b, 0)
|
|
1044
|
+
const day = String(todayIso).slice(0, 10)
|
|
1045
|
+
return {
|
|
1046
|
+
days: values.length,
|
|
1047
|
+
total,
|
|
1048
|
+
avg: Math.round((total / values.length) * 10) / 10,
|
|
1049
|
+
today: Number(map[day]) || 0,
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
/**
|
|
1054
|
+
* 「今天明显比平时忙」:只在有足够历史、且高出很多倍时才提示。
|
|
1055
|
+
*
|
|
1056
|
+
* 阈值故意保守(默认 2 倍 + 至少 3 天数据)——
|
|
1057
|
+
* 拿历史去影响判断,最怕的是频繁误报;宁可少提示,也不要制造噪音。
|
|
1058
|
+
*/
|
|
1059
|
+
export function busierThanUsual(summary, { factor = 2, minDays = 3 } = {}) {
|
|
1060
|
+
if (!summary || !Number.isFinite(Number(summary.days)) || summary.days < minDays) return false
|
|
1061
|
+
const avg = Number(summary.avg) || 0
|
|
1062
|
+
const today = Number(summary.today) || 0
|
|
1063
|
+
if (avg <= 0) return false // 历史为 0 时没有可比基线
|
|
1064
|
+
return today >= avg * Math.max(1.5, Number(factor) || 2)
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
/**
|
|
1068
|
+
* `watch_status` 的结构化视图:同一份数据,既能渲染成人读的文本,也能给 agent 解析。
|
|
1069
|
+
*
|
|
1070
|
+
* 为什么要有它:工具输出原来是拼接的字符串,agent 只能靠字符串匹配 —— 一改文案就崩。
|
|
1071
|
+
* 现在纯函数产出结构,文本只是它的一种渲染(两边永远一致)。
|
|
1072
|
+
*/
|
|
1073
|
+
export function statusView(entries, options = {}) {
|
|
1074
|
+
const list = (Array.isArray(entries) ? entries : Object.values(entries || {})).filter(Boolean)
|
|
1075
|
+
const sessionId = options.sessionId || (list[0] && list[0].sessionId) || null
|
|
1076
|
+
const capped = countCapped(list)
|
|
1077
|
+
const used = sessionId ? sessionWakesLastHour(list, sessionId) : 0
|
|
1078
|
+
const cap = Number((list[0] && list[0].sessionMaxWakes)) || 12
|
|
1079
|
+
return {
|
|
1080
|
+
// 消费方靠它判断字段语义有没有变;改字段时这个数一起加
|
|
1081
|
+
schemaVersion: 1,
|
|
1082
|
+
session: {
|
|
1083
|
+
id: sessionId,
|
|
1084
|
+
used,
|
|
1085
|
+
cap,
|
|
1086
|
+
capped,
|
|
1087
|
+
suggest: list.length === 0 ? null : suggestSessionCap({ used, cap, capped }),
|
|
1088
|
+
},
|
|
1089
|
+
watchers: list.map((w) => ({
|
|
1090
|
+
repo: w.repo || null,
|
|
1091
|
+
// 两家仓库同名时(owner/name 在 GitHub 与 Gitee 都存在),只有 provider 能区分
|
|
1092
|
+
provider: w.provider === 'gitee' ? 'gitee' : 'github',
|
|
1093
|
+
sessionId: w.sessionId || null,
|
|
1094
|
+
enabled: w.enabled !== false,
|
|
1095
|
+
intervalSec: Number(w.intervalSec) || 60,
|
|
1096
|
+
observe: w.observe === true,
|
|
1097
|
+
isolated: w.isolated === true,
|
|
1098
|
+
activeHours: w.activeHours || null,
|
|
1099
|
+
labels: (w.filters && Array.isArray(w.filters.labels)) ? w.filters.labels : [],
|
|
1100
|
+
onlyAssigned: Boolean(w.filters && w.filters.onlyAssigned),
|
|
1101
|
+
deferReason: w.deferReason || null,
|
|
1102
|
+
lastDeferred: w.lastDeferred || null,
|
|
1103
|
+
silentWakes: Number(w.silentWakes) || 0,
|
|
1104
|
+
cursor: w.cursor || null,
|
|
1105
|
+
lastPoll: w.lastPoll || null,
|
|
1106
|
+
lastWake: w.lastWake || null,
|
|
1107
|
+
lastError: w.lastError || null,
|
|
1108
|
+
})),
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
/**
|
|
1113
|
+
* 「找不到会话」到底是**被删了**,还是**宿主刚启动还没加载**?
|
|
1114
|
+
*
|
|
1115
|
+
* 真机现场(2026-11-04 重启后第一轮轮询):
|
|
1116
|
+
* lastError: 会话已不在:请删除或重建这条主理鱼
|
|
1117
|
+
* 而那个会话明明还开着 —— 宿主刚启动时 agents.get() 会短暂返回 undefined。
|
|
1118
|
+
* 让客户看到"请删除或重建"这种不可逆建议,比什么都不说更糟。
|
|
1119
|
+
*
|
|
1120
|
+
* 判据:先给一段宽限期(默认 10 分钟,约 5 个轮询周期)。
|
|
1121
|
+
* 宽限期内只写 lastDeferred(不记失败、不退避);过了宽限期才认定真的没了。
|
|
1122
|
+
*/
|
|
1123
|
+
export const SESSION_MISSING_GRACE_MS = 10 * 60 * 1000
|
|
1124
|
+
|
|
1125
|
+
export function sessionMissingVerdict({ missingSince, now = Date.now(), graceMs = SESSION_MISSING_GRACE_MS } = {}) {
|
|
1126
|
+
const since = Date.parse(String(missingSince || ''))
|
|
1127
|
+
if (!Number.isFinite(since)) {
|
|
1128
|
+
return { grace: true, startedAt: new Date(now).toISOString(), waitMin: Math.ceil(graceMs / 60000), justNow: true }
|
|
1129
|
+
}
|
|
1130
|
+
const left = graceMs - (now - since)
|
|
1131
|
+
if (left > 0) return { grace: true, startedAt: new Date(since).toISOString(), waitMin: Math.max(1, Math.ceil(left / 60000)) }
|
|
1132
|
+
return { grace: false, waitedMin: Math.max(1, Math.round((now - since) / 60000)) }
|
|
1133
|
+
}
|
|
1134
|
+
|
|
1135
|
+
/**
|
|
1136
|
+
* 解析仓库写法:`owner/name`、`github:owner/name`、`gitee:owner/name`,
|
|
1137
|
+
* 以及 `https://github.com/owner/name`、`https://gitee.com/owner/name`(可带 .git / 结尾斜杠)。
|
|
1138
|
+
*
|
|
1139
|
+
* 为什么要认 URL:客户是从浏览器地址栏复制的,让他手动删 `https://gitee.com/` 是没必要的摩擦。
|
|
1140
|
+
* 认不出来就返回 ok:false + 具体原因,别在轮询阶段才 404。
|
|
1141
|
+
*/
|
|
1142
|
+
export function parseRepoSpec(input) {
|
|
1143
|
+
const raw = String(input || '').trim()
|
|
1144
|
+
if (!raw) return { ok: false, error: 'repo 不能为空' }
|
|
1145
|
+
let provider = 'github'
|
|
1146
|
+
let rest = raw
|
|
1147
|
+
|
|
1148
|
+
const url = raw.match(/^https?:\/\/([^/]+)\/(.+)$/i)
|
|
1149
|
+
if (url) {
|
|
1150
|
+
const host = url[1].toLowerCase()
|
|
1151
|
+
rest = url[2]
|
|
1152
|
+
if (host === 'gitee.com' || host.endsWith('.gitee.com')) provider = 'gitee'
|
|
1153
|
+
else if (host === 'github.com' || host.endsWith('.github.com')) provider = 'github'
|
|
1154
|
+
else return { ok: false, error: '只支持 github.com 与 gitee.com 的地址' }
|
|
1155
|
+
} else {
|
|
1156
|
+
const prefixed = raw.match(/^(github|gitee)\s*[::]\s*(.+)$/i)
|
|
1157
|
+
if (prefixed) {
|
|
1158
|
+
provider = prefixed[1].toLowerCase()
|
|
1159
|
+
rest = prefixed[2]
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
rest = rest.replace(/\.git$/i, '').replace(/\/+$/, '')
|
|
1164
|
+
// URL 里可能还带着 /issues 之类的尾巴:只取前两段
|
|
1165
|
+
const parts = rest.split('/').filter(Boolean)
|
|
1166
|
+
if (parts.length < 2) return { ok: false, error: '要写成 owner/name' }
|
|
1167
|
+
const [owner, name] = parts
|
|
1168
|
+
if (!/^[A-Za-z0-9_.-]+$/.test(owner) || !/^[A-Za-z0-9_.-]+$/.test(name)) {
|
|
1169
|
+
return { ok: false, error: 'owner/name 只能包含字母、数字、下划线、点、短横线' }
|
|
1170
|
+
}
|
|
1171
|
+
// 审核 T19:`.` 和 `..` 能过上面那条字符类(点也算合法字符),于是 `'../..'` 会被当成 owner/name ——
|
|
1172
|
+
// 它会一路拼进 URL 与磁盘路径。路径段不许是这两个。
|
|
1173
|
+
if (owner === '.' || owner === '..' || name === '.' || name === '..') {
|
|
1174
|
+
return { ok: false, error: 'owner/name 不能是 . 或 ..' }
|
|
1175
|
+
}
|
|
1176
|
+
return { ok: true, provider, repo: owner + '/' + name, spec: provider === 'github' ? owner + '/' + name : 'gitee:' + owner + '/' + name }
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
/**
|
|
1180
|
+
* Gitee 没有 `state_reason` 字段 —— 「重新打开」这个信号在它上面永远不出现。
|
|
1181
|
+
*
|
|
1182
|
+
* 但「上一轮是 closed、这一轮是 open」本身就是 reopen 的定义。这里用上一轮记下的状态自己判,
|
|
1183
|
+
* 判出来写回 `state_reason = 'reopened'`,下游的 ⚠ 标记与 +100 加权不用改一行。
|
|
1184
|
+
*
|
|
1185
|
+
* 只在 Gitee 上调用:GitHub 有原生字段,覆盖它等于篡改事实。
|
|
1186
|
+
* 返回新的状态表(调用方存进 entry,键是编号字符串,值只存 open/closed)。
|
|
1187
|
+
*/
|
|
1188
|
+
export function markGiteeReopened(items, lastStates = {}) {
|
|
1189
|
+
const list = items || []
|
|
1190
|
+
for (const item of list) {
|
|
1191
|
+
const prev = lastStates[String(item.number)]
|
|
1192
|
+
if (!item.state_reason && prev === 'closed' && item.state === 'open') item.state_reason = 'reopened'
|
|
1193
|
+
}
|
|
1194
|
+
const next = { ...lastStates }
|
|
1195
|
+
for (const item of list) next[String(item.number)] = item.state
|
|
1196
|
+
// 只留当前这一页见过的:翻页之外的旧状态没有价值,留着就是内存泄漏
|
|
1197
|
+
const alive = new Set(list.map((item) => String(item.number)))
|
|
1198
|
+
for (const key of Object.keys(next)) if (!alive.has(key)) delete next[key]
|
|
1199
|
+
return next
|
|
1200
|
+
}
|
|
1200
1201
|
|
|
1201
1202
|
/**
|
|
1202
1203
|
* 一次唤醒的**结算**:成功才推进水位、失败就保留水位。
|