dsh-session-superagent 0.10.12 → 2.0.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/AGENTS.md +31 -6
- package/CHANGELOG.md +261 -0
- package/MAINTENANCE.md +49 -84
- package/README.md +43 -6
- package/client.js +1301 -115
- package/docs/README.md +1 -0
- package/docs/json-/345/245/221/347/272/246.md +4 -1
- package/docs//350/256/276/350/256/241-/346/250/241/345/235/227/345/214/226/346/210/277/351/227/264.md +91 -0
- package/lib/gh-format.js +202 -1
- package/lib/gitee.js +180 -5
- package/lib/index.js +2367 -270
- package/lib/pr-watch.js +63 -0
- package/lib/rooms/admission-eval.js +189 -0
- package/lib/rooms/data-provider.js +221 -0
- package/lib/rooms/delivery.js +97 -0
- package/lib/rooms/draft.js +112 -0
- package/lib/rooms/execution-assignment.js +241 -0
- package/lib/rooms/executor-host.js +82 -0
- package/lib/rooms/i18n.js +166 -0
- package/lib/rooms/modular-outbox-host.js +326 -0
- package/lib/rooms/modular-runtime.js +222 -0
- package/lib/rooms/outbox.js +338 -0
- package/lib/rooms/projection.js +105 -0
- package/lib/rooms/prompt-config.js +107 -0
- package/lib/rooms/scheduler.js +119 -0
- package/lib/rooms/skill-pack.js +120 -0
- package/lib/rooms/source-registry.js +100 -0
- package/lib/rooms/thread-subscription.js +237 -0
- package/lib/rooms/ticket-projection.js +326 -0
- package/lib/rooms/ticket-state.js +181 -0
- package/lib/rooms/watermark-decision.js +98 -0
- package/lib/rooms/watermark-state.js +268 -0
- package/lib/state-migration.js +29 -16
- package/lib/watch.js +165 -684
- package/package.json +48 -3
- package/skills/dsh-session-superagent-github/SKILL.md +14 -0
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ticket / 执行 / 部署只读投影。
|
|
3
|
+
*
|
|
4
|
+
* 这里只读取共享 DTO,不拥有生命周期状态,也不暴露可回写的内部对象。
|
|
5
|
+
* 未提供的集合与计数是 null;调用方显式给出空数组或空对象时计数才是 0。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const TICKET_STATUSES = ['open', 'assigned', 'waiting', 'ended']
|
|
9
|
+
const ASSIGNMENT_STATUSES = ['assigned', 'waiting', 'ended']
|
|
10
|
+
const OUTBOX_STATUSES = ['pending', 'sending', 'sent', 'failed', 'gone']
|
|
11
|
+
const HEALTH_STATES = ['ready', 'error', 'starting', 'paused']
|
|
12
|
+
|
|
13
|
+
function isRecord(value) {
|
|
14
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function hasOwn(value, key) {
|
|
18
|
+
return isRecord(value) && Object.prototype.hasOwnProperty.call(value, key)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function cloneValue(value) {
|
|
22
|
+
if (Array.isArray(value)) return value.map(cloneValue)
|
|
23
|
+
if (!isRecord(value)) return value
|
|
24
|
+
const copy = {}
|
|
25
|
+
for (const [key, child] of Object.entries(value)) copy[key] = cloneValue(child)
|
|
26
|
+
return copy
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function deepFreeze(value, seen = new WeakSet()) {
|
|
30
|
+
if (!value || typeof value !== 'object' || seen.has(value)) return value
|
|
31
|
+
seen.add(value)
|
|
32
|
+
for (const child of Object.values(value)) deepFreeze(child, seen)
|
|
33
|
+
return Object.freeze(value)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function nullableText(value) {
|
|
37
|
+
return typeof value === 'string' && value ? value : null
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function nullableCount(value) {
|
|
41
|
+
return Number.isSafeInteger(value) && value >= 0 ? value : null
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function unknownCounts(names) {
|
|
45
|
+
return Object.fromEntries(names.map((name) => [name, null]))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function zeroCounts(names) {
|
|
49
|
+
return Object.fromEntries(names.map((name) => [name, 0]))
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function collection(input, key) {
|
|
53
|
+
if (Array.isArray(input)) {
|
|
54
|
+
return key === 'tickets' ? { known: true, items: input } : { known: false, items: [] }
|
|
55
|
+
}
|
|
56
|
+
if (!isRecord(input) || !hasOwn(input, key)) return { known: false, items: [] }
|
|
57
|
+
const value = input[key]
|
|
58
|
+
if (Array.isArray(value)) return { known: true, items: value }
|
|
59
|
+
if (isRecord(value)) return { known: true, items: Object.values(value) }
|
|
60
|
+
return { known: false, items: [] }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** 投影单个 Ticket;history 被复制并冻结,不与原状态共享引用。 */
|
|
64
|
+
export function projectTicketRecord(ticket) {
|
|
65
|
+
const source = isRecord(ticket) ? ticket : {}
|
|
66
|
+
const history = Array.isArray(source.history) ? cloneValue(source.history) : null
|
|
67
|
+
return deepFreeze({
|
|
68
|
+
id: nullableText(source.id),
|
|
69
|
+
threadKey: nullableText(source.threadKey),
|
|
70
|
+
status: TICKET_STATUSES.includes(source.status) ? source.status : null,
|
|
71
|
+
ownerSessionId: nullableText(source.ownerSessionId),
|
|
72
|
+
workdir: nullableText(source.workdir),
|
|
73
|
+
assignmentId: nullableText(source.assignmentId),
|
|
74
|
+
resumeCount: nullableCount(source.resumeCount),
|
|
75
|
+
createdAt: nullableText(source.createdAt),
|
|
76
|
+
updatedAt: nullableText(source.updatedAt),
|
|
77
|
+
endedAt: nullableText(source.endedAt),
|
|
78
|
+
endReason: nullableText(source.endReason),
|
|
79
|
+
history,
|
|
80
|
+
historyCount: history ? history.length : null,
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Ticket records 列表与状态计数。
|
|
86
|
+
* 接受 Ticket[]、{ tickets: Ticket[] } 或 { tickets: Record<id, Ticket> }。
|
|
87
|
+
*/
|
|
88
|
+
export function projectTicketRecords(input) {
|
|
89
|
+
const { known, items } = collection(input, 'tickets')
|
|
90
|
+
if (!known) {
|
|
91
|
+
return deepFreeze({ records: null, total: null, counts: unknownCounts(TICKET_STATUSES) })
|
|
92
|
+
}
|
|
93
|
+
const counts = zeroCounts(TICKET_STATUSES)
|
|
94
|
+
for (const ticket of items) {
|
|
95
|
+
if (isRecord(ticket) && TICKET_STATUSES.includes(ticket.status)) counts[ticket.status] += 1
|
|
96
|
+
}
|
|
97
|
+
return deepFreeze({
|
|
98
|
+
records: items.map(projectTicketRecord),
|
|
99
|
+
total: items.length,
|
|
100
|
+
counts,
|
|
101
|
+
})
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function projectSubscriptionRecord(subscription) {
|
|
105
|
+
const source = isRecord(subscription) ? subscription : {}
|
|
106
|
+
return deepFreeze({
|
|
107
|
+
threadKey: nullableText(source.threadKey),
|
|
108
|
+
ticketId: nullableText(source.ticketId),
|
|
109
|
+
sessionId: nullableText(source.sessionId),
|
|
110
|
+
active: typeof source.active === 'boolean' ? source.active : null,
|
|
111
|
+
createdAt: nullableText(source.createdAt),
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function projectSubscriptions(input) {
|
|
116
|
+
const { known, items } = collection(input, 'subscriptions')
|
|
117
|
+
if (!known) return deepFreeze({ records: null, total: null, active: null, inactive: null })
|
|
118
|
+
const records = items.map(projectSubscriptionRecord)
|
|
119
|
+
const active = records.filter((record) => record.active === true).length
|
|
120
|
+
const inactive = records.filter((record) => record.active === false).length
|
|
121
|
+
return deepFreeze({ records, total: records.length, active, inactive })
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function projectOutboxRecord(message) {
|
|
125
|
+
const source = isRecord(message) ? message : {}
|
|
126
|
+
return deepFreeze({
|
|
127
|
+
id: nullableText(source.id),
|
|
128
|
+
ticketId: nullableText(source.ticketId),
|
|
129
|
+
channel: source.channel === undefined ? null : cloneValue(source.channel),
|
|
130
|
+
body: typeof source.body === 'string' ? source.body : null,
|
|
131
|
+
status: OUTBOX_STATUSES.includes(source.status) ? source.status : null,
|
|
132
|
+
attempts: nullableCount(source.attempts),
|
|
133
|
+
nextAt: nullableText(source.nextAt),
|
|
134
|
+
createdAt: nullableText(source.createdAt),
|
|
135
|
+
updatedAt: nullableText(source.updatedAt),
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function projectOutbox(input) {
|
|
140
|
+
const { known, items } = collection(input, 'outbox')
|
|
141
|
+
if (!known) {
|
|
142
|
+
return deepFreeze({ records: null, total: null, counts: unknownCounts(OUTBOX_STATUSES) })
|
|
143
|
+
}
|
|
144
|
+
const counts = zeroCounts(OUTBOX_STATUSES)
|
|
145
|
+
const records = items.map((message) => {
|
|
146
|
+
const record = projectOutboxRecord(message)
|
|
147
|
+
if (record.status) counts[record.status] += 1
|
|
148
|
+
return record
|
|
149
|
+
})
|
|
150
|
+
return deepFreeze({ records, total: records.length, counts })
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function projectAssignmentRecord(assignment) {
|
|
154
|
+
const source = isRecord(assignment) ? assignment : {}
|
|
155
|
+
return deepFreeze({
|
|
156
|
+
id: nullableText(source.id),
|
|
157
|
+
ticketId: nullableText(source.ticketId),
|
|
158
|
+
sessionId: nullableText(source.sessionId),
|
|
159
|
+
workdir: nullableText(source.workdir),
|
|
160
|
+
status: ASSIGNMENT_STATUSES.includes(source.status) ? source.status : null,
|
|
161
|
+
attempts: nullableCount(source.attempts),
|
|
162
|
+
})
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function projectAssignments(input) {
|
|
166
|
+
const { known, items } = collection(input, 'assignments')
|
|
167
|
+
if (!known) {
|
|
168
|
+
return deepFreeze({ records: null, total: null, counts: unknownCounts(ASSIGNMENT_STATUSES) })
|
|
169
|
+
}
|
|
170
|
+
const counts = zeroCounts(ASSIGNMENT_STATUSES)
|
|
171
|
+
const records = items.map((assignment) => {
|
|
172
|
+
const record = projectAssignmentRecord(assignment)
|
|
173
|
+
if (record.status) counts[record.status] += 1
|
|
174
|
+
return record
|
|
175
|
+
})
|
|
176
|
+
return deepFreeze({ records, total: records.length, counts })
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* records 路由统一投影:顶层 records/total/counts 属于 Tickets,其余集合保留各自命名。
|
|
181
|
+
* 所有集合都遵守“缺省未知为 null、显式空集合计数为 0”。
|
|
182
|
+
*/
|
|
183
|
+
export function projectRecords(input) {
|
|
184
|
+
return deepFreeze({
|
|
185
|
+
...projectTicketRecords(input),
|
|
186
|
+
subscriptions: projectSubscriptions(input),
|
|
187
|
+
assignments: projectAssignments(input),
|
|
188
|
+
outbox: projectOutbox(input),
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function currentTicket(source, tickets) {
|
|
193
|
+
if (hasOwn(source, 'current')) {
|
|
194
|
+
const current = isRecord(source.current) && isRecord(source.current.ticket)
|
|
195
|
+
? source.current.ticket
|
|
196
|
+
: source.current
|
|
197
|
+
return isRecord(current) ? current : null
|
|
198
|
+
}
|
|
199
|
+
if (hasOwn(source, 'currentTicket')) return isRecord(source.currentTicket) ? source.currentTicket : null
|
|
200
|
+
if (hasOwn(source, 'currentTicketId')) {
|
|
201
|
+
if (source.currentTicketId == null) return null
|
|
202
|
+
return tickets.find((ticket) => isRecord(ticket) && ticket.id === source.currentTicketId) || null
|
|
203
|
+
}
|
|
204
|
+
if (hasOwn(source, 'currentAssignmentId')) {
|
|
205
|
+
if (source.currentAssignmentId == null) return null
|
|
206
|
+
return tickets.find((ticket) => isRecord(ticket) && ticket.assignmentId === source.currentAssignmentId) || null
|
|
207
|
+
}
|
|
208
|
+
return tickets.find((ticket) => isRecord(ticket) && ticket.status === 'assigned') || null
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function currentAssignment(source, current, assignments) {
|
|
212
|
+
if (isRecord(source.current) && hasOwn(source.current, 'assignment')) {
|
|
213
|
+
return isRecord(source.current.assignment) ? source.current.assignment : null
|
|
214
|
+
}
|
|
215
|
+
if (hasOwn(source, 'currentAssignment')) return isRecord(source.currentAssignment) ? source.currentAssignment : null
|
|
216
|
+
const id = hasOwn(source, 'currentAssignmentId')
|
|
217
|
+
? source.currentAssignmentId
|
|
218
|
+
: (isRecord(current) ? current.assignmentId : null)
|
|
219
|
+
if (id != null) {
|
|
220
|
+
return assignments.find((assignment) => isRecord(assignment) && assignment.id === id) || null
|
|
221
|
+
}
|
|
222
|
+
if (hasOwn(source, 'currentAssignmentId') || hasOwn(source, 'currentAssignment')) return null
|
|
223
|
+
return assignments.find((assignment) => isRecord(assignment) && assignment.status === 'assigned') || null
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function loadCount(source, known, name, fallback) {
|
|
227
|
+
if (isRecord(source.load) && hasOwn(source.load, name)) return nullableCount(source.load[name])
|
|
228
|
+
return known ? fallback : null
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* 当前执行摘要。未显式指定 current 时,优先取首个 assigned Ticket / Assignment。
|
|
233
|
+
* running 优先使用 load,其次使用 Assignment 计数,最后才由 assigned Ticket 推导。
|
|
234
|
+
*/
|
|
235
|
+
export function projectExecution(input) {
|
|
236
|
+
const source = isRecord(input) ? input : {}
|
|
237
|
+
const tickets = collection(input, 'tickets')
|
|
238
|
+
const assignments = collection(input, 'assignments')
|
|
239
|
+
const records = projectRecords(input)
|
|
240
|
+
const current = currentTicket(source, tickets.items)
|
|
241
|
+
const assignment = currentAssignment(source, current, assignments.items)
|
|
242
|
+
const activeCount = tickets.known ? records.total - records.counts.ended : null
|
|
243
|
+
const runningFallback = assignments.known ? records.assignments.counts.assigned : records.counts.assigned
|
|
244
|
+
return deepFreeze({
|
|
245
|
+
current: current ? projectTicketRecord(current) : null,
|
|
246
|
+
assignment: assignment ? projectAssignmentRecord(assignment) : null,
|
|
247
|
+
total: records.total,
|
|
248
|
+
activeCount,
|
|
249
|
+
counts: cloneValue(records.counts),
|
|
250
|
+
subscriptions: records.subscriptions,
|
|
251
|
+
assignments: records.assignments,
|
|
252
|
+
outbox: records.outbox,
|
|
253
|
+
running: loadCount(source, tickets.known || assignments.known, 'running', runningFallback),
|
|
254
|
+
queued: loadCount(source, tickets.known, 'queued', records.counts.open),
|
|
255
|
+
maxWorkers: isRecord(source.load) && hasOwn(source.load, 'maxWorkers')
|
|
256
|
+
? nullableCount(source.load.maxWorkers)
|
|
257
|
+
: null,
|
|
258
|
+
paused: typeof source.paused === 'boolean' ? source.paused : null,
|
|
259
|
+
})
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function healthCollection(input) {
|
|
263
|
+
if (!isRecord(input) || !hasOwn(input, 'health')) return { known: false, entries: [] }
|
|
264
|
+
if (Array.isArray(input.health)) {
|
|
265
|
+
return {
|
|
266
|
+
known: true,
|
|
267
|
+
entries: input.health.map((entry, index) => [isRecord(entry) && entry.name != null ? String(entry.name) : String(index), entry]),
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (isRecord(input.health)) return { known: true, entries: Object.entries(input.health) }
|
|
271
|
+
return { known: false, entries: [] }
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function projectHealthEntry(name, entry) {
|
|
275
|
+
const source = isRecord(entry) ? entry : {}
|
|
276
|
+
return {
|
|
277
|
+
name: nullableText(name),
|
|
278
|
+
state: HEALTH_STATES.includes(source.state) ? source.state : null,
|
|
279
|
+
code: nullableText(source.code),
|
|
280
|
+
detail: nullableText(source.detail),
|
|
281
|
+
values: source.values === undefined ? null : cloneValue(source.values),
|
|
282
|
+
at: nullableText(source.at),
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** 部署问题与健康摘要;health={} 是明确健康项为空,health 缺省则是未知。 */
|
|
287
|
+
export function projectDeployment(input) {
|
|
288
|
+
const source = isRecord(input) ? input : {}
|
|
289
|
+
const setup = isRecord(source.setup) ? source.setup : {}
|
|
290
|
+
const problem = source.problem !== undefined ? source.problem : setup.problem
|
|
291
|
+
const stateDir = source.stateDir !== undefined
|
|
292
|
+
? source.stateDir
|
|
293
|
+
: (isRecord(setup.machine) ? setup.machine.stateDir : undefined)
|
|
294
|
+
const { known, entries } = healthCollection(source)
|
|
295
|
+
|
|
296
|
+
if (!known) {
|
|
297
|
+
return deepFreeze({
|
|
298
|
+
status: problem ? 'error' : 'unknown',
|
|
299
|
+
stateDir: nullableText(stateDir),
|
|
300
|
+
problem: problem == null ? null : cloneValue(problem),
|
|
301
|
+
health: null,
|
|
302
|
+
counts: unknownCounts(['total', ...HEALTH_STATES, 'unknown']),
|
|
303
|
+
})
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const counts = zeroCounts(['total', ...HEALTH_STATES, 'unknown'])
|
|
307
|
+
counts.total = entries.length
|
|
308
|
+
const health = entries.map(([name, entry]) => {
|
|
309
|
+
const view = projectHealthEntry(name, entry)
|
|
310
|
+
counts[view.state || 'unknown'] += 1
|
|
311
|
+
return view
|
|
312
|
+
})
|
|
313
|
+
const status = problem || counts.error > 0 ? 'error'
|
|
314
|
+
: counts.unknown > 0 ? 'unknown'
|
|
315
|
+
: counts.starting > 0 ? 'starting'
|
|
316
|
+
: counts.paused > 0 ? 'paused'
|
|
317
|
+
: 'ready'
|
|
318
|
+
|
|
319
|
+
return deepFreeze({
|
|
320
|
+
status,
|
|
321
|
+
stateDir: nullableText(stateDir),
|
|
322
|
+
problem: problem == null ? null : cloneValue(problem),
|
|
323
|
+
health,
|
|
324
|
+
counts,
|
|
325
|
+
})
|
|
326
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ticket 生命周期房。
|
|
3
|
+
*
|
|
4
|
+
* 只管理 open → assigned / waiting → ended,以及显式人类重开;不订阅评论、
|
|
5
|
+
* 不启动执行器。状态与历史均为可持久化 JSON,所有输出深冻结,eventId 用于
|
|
6
|
+
* 跨重启识别重复事件;持久化冲突仍由宿主的 commit(next, expected) 裁决。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
const STATUSES = new Set(['open', 'assigned', 'waiting', 'ended'])
|
|
10
|
+
|
|
11
|
+
function isObject(value) {
|
|
12
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function clone(value) {
|
|
16
|
+
if (Array.isArray(value)) return value.map(clone)
|
|
17
|
+
if (!isObject(value)) return value
|
|
18
|
+
return Object.fromEntries(Object.entries(value).map(([key, child]) => [key, clone(child)]))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function deepFreeze(value) {
|
|
22
|
+
if (!value || typeof value !== 'object' || Object.isFrozen(value)) return value
|
|
23
|
+
for (const child of Object.values(value)) deepFreeze(child)
|
|
24
|
+
return Object.freeze(value)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isDeepFrozen(value) {
|
|
28
|
+
if (!value || typeof value !== 'object') return true
|
|
29
|
+
return Object.isFrozen(value) && Object.values(value).every(isDeepFrozen)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function unchanged(ticket) {
|
|
33
|
+
return isDeepFrozen(ticket) ? ticket : deepFreeze(clone(ticket))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function optionsObject(options) {
|
|
37
|
+
if (!isObject(options)) throw new TypeError('options must be an object')
|
|
38
|
+
return options
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function requiredText(value, name) {
|
|
42
|
+
if (typeof value !== 'string' || !value) throw new TypeError(`${name} must be a non-empty string`)
|
|
43
|
+
return value
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function nullableText(value, name) {
|
|
47
|
+
return value === undefined || value === null ? null : requiredText(value, name)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function detailText(value) {
|
|
51
|
+
return value === undefined || value === null ? '' : String(value)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function eventAt(at) {
|
|
55
|
+
return at === undefined ? new Date().toISOString() : requiredText(at, 'at')
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function makeEvent(type, at, detail, eventId) {
|
|
59
|
+
const event = { at, type, detail: detailText(detail) }
|
|
60
|
+
if (eventId !== undefined) event.eventId = requiredText(eventId, 'eventId')
|
|
61
|
+
return event
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function assertTicket(ticket) {
|
|
65
|
+
if (!isObject(ticket)) throw new TypeError('ticket must be an object')
|
|
66
|
+
requiredText(ticket.id, 'ticket.id')
|
|
67
|
+
requiredText(ticket.threadKey, 'ticket.threadKey')
|
|
68
|
+
if (!STATUSES.has(ticket.status)) throw new TypeError(`unknown ticket status: ${ticket.status}`)
|
|
69
|
+
if (!Array.isArray(ticket.history)) throw new TypeError('ticket.history must be an array')
|
|
70
|
+
return ticket
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isDuplicate(ticket, eventId, type) {
|
|
74
|
+
if (eventId === undefined) return false
|
|
75
|
+
const id = requiredText(eventId, 'eventId')
|
|
76
|
+
const previous = ticket.history.find((event) => event && event.eventId === id)
|
|
77
|
+
if (!previous) return false
|
|
78
|
+
if (previous.type !== type) throw new Error(`eventId ${id} was already consumed as ${previous.type}`)
|
|
79
|
+
return true
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function transition(ticket, type, options, changes, detail) {
|
|
83
|
+
const at = eventAt(options.at)
|
|
84
|
+
const event = makeEvent(type, at, detail, options.eventId)
|
|
85
|
+
return deepFreeze({
|
|
86
|
+
...clone(ticket),
|
|
87
|
+
...changes,
|
|
88
|
+
updatedAt: at,
|
|
89
|
+
history: [...clone(ticket.history), event],
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function create(fields = {}) {
|
|
94
|
+
const source = optionsObject(fields)
|
|
95
|
+
const threadKey = requiredText(source.threadKey, 'threadKey')
|
|
96
|
+
const id = source.id === undefined ? `ticket:${threadKey}` : requiredText(source.id, 'id')
|
|
97
|
+
const at = eventAt(source.at)
|
|
98
|
+
return deepFreeze({
|
|
99
|
+
id,
|
|
100
|
+
threadKey,
|
|
101
|
+
status: 'open',
|
|
102
|
+
ownerSessionId: null,
|
|
103
|
+
workdir: null,
|
|
104
|
+
assignmentId: null,
|
|
105
|
+
resumeCount: 0,
|
|
106
|
+
createdAt: at,
|
|
107
|
+
updatedAt: at,
|
|
108
|
+
endedAt: null,
|
|
109
|
+
endReason: null,
|
|
110
|
+
history: [makeEvent('created', at, source.detail, source.eventId)],
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function assign(ticket, options = {}) {
|
|
115
|
+
const state = assertTicket(ticket)
|
|
116
|
+
const source = optionsObject(options)
|
|
117
|
+
const sessionId = requiredText(source.sessionId ?? source.ownerSessionId, 'sessionId')
|
|
118
|
+
const assignmentId = requiredText(source.assignmentId, 'assignmentId')
|
|
119
|
+
const workdir = nullableText(source.workdir, 'workdir')
|
|
120
|
+
if (isDuplicate(state, source.eventId, 'assigned')) return unchanged(state)
|
|
121
|
+
if (state.status === 'ended') throw new Error('cannot assign an ended ticket; human resume is required')
|
|
122
|
+
if (state.status === 'assigned'
|
|
123
|
+
&& state.ownerSessionId === sessionId
|
|
124
|
+
&& state.assignmentId === assignmentId
|
|
125
|
+
&& state.workdir === workdir) return unchanged(state)
|
|
126
|
+
|
|
127
|
+
return transition(state, 'assigned', source, {
|
|
128
|
+
status: 'assigned',
|
|
129
|
+
ownerSessionId: sessionId,
|
|
130
|
+
workdir,
|
|
131
|
+
assignmentId,
|
|
132
|
+
endedAt: null,
|
|
133
|
+
endReason: null,
|
|
134
|
+
}, source.detail)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export function wait(ticket, options = {}) {
|
|
138
|
+
const state = assertTicket(ticket)
|
|
139
|
+
const source = optionsObject(options)
|
|
140
|
+
if (isDuplicate(state, source.eventId, 'waiting')) return unchanged(state)
|
|
141
|
+
if (state.status === 'waiting') return unchanged(state)
|
|
142
|
+
if (state.status !== 'assigned') throw new Error(`cannot wait a ${state.status} ticket; assigned status is required`)
|
|
143
|
+
return transition(state, 'waiting', source, { status: 'waiting' }, source.detail ?? source.reason)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function end(ticket, options = {}) {
|
|
147
|
+
const state = assertTicket(ticket)
|
|
148
|
+
const source = optionsObject(options)
|
|
149
|
+
if (isDuplicate(state, source.eventId, 'ended')) return unchanged(state)
|
|
150
|
+
if (state.status === 'ended') return unchanged(state)
|
|
151
|
+
const endReason = source.reason === undefined || source.reason === null
|
|
152
|
+
? (source.detail === undefined || source.detail === null ? null : String(source.detail))
|
|
153
|
+
: String(source.reason)
|
|
154
|
+
const at = eventAt(source.at)
|
|
155
|
+
return transition(state, 'ended', { ...source, at }, {
|
|
156
|
+
status: 'ended',
|
|
157
|
+
endedAt: at,
|
|
158
|
+
endReason,
|
|
159
|
+
}, source.detail ?? source.reason)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function resume(ticket, options = {}) {
|
|
163
|
+
const state = assertTicket(ticket)
|
|
164
|
+
const source = optionsObject(options)
|
|
165
|
+
if (source.actor !== 'human') throw new Error('resume requires explicit actor: human')
|
|
166
|
+
if (isDuplicate(state, source.eventId, 'resumed')) return unchanged(state)
|
|
167
|
+
if (state.status !== 'ended') throw new Error(`cannot resume a ${state.status} ticket; ended status is required`)
|
|
168
|
+
return transition(state, 'resumed', source, {
|
|
169
|
+
status: 'open',
|
|
170
|
+
ownerSessionId: null,
|
|
171
|
+
workdir: null,
|
|
172
|
+
assignmentId: null,
|
|
173
|
+
resumeCount: (Number(state.resumeCount) || 0) + 1,
|
|
174
|
+
endedAt: null,
|
|
175
|
+
endReason: null,
|
|
176
|
+
}, source.detail)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function history(ticket) {
|
|
180
|
+
return deepFreeze(clone(assertTicket(ticket).history))
|
|
181
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 水位 v2 · 决策房。
|
|
3
|
+
*
|
|
4
|
+
* 输入是当前条目与只读 obs 投影,输出分类与本轮批次。无 I/O、无定时器、
|
|
5
|
+
* 不写 delivered/handled;状态提交只能由交付/处置成功后的状态房完成。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
function isObject(value) {
|
|
9
|
+
return Boolean(value) && typeof value === 'object' && !Array.isArray(value)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function validRecord(record) {
|
|
13
|
+
if (!isObject(record) || !String(record.key || '')) return false
|
|
14
|
+
const cursor = record.cursor
|
|
15
|
+
return (typeof cursor === 'string' && cursor.length > 0)
|
|
16
|
+
|| (typeof cursor === 'number' && Number.isSafeInteger(cursor) && cursor >= 0)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function eventRound(event) {
|
|
20
|
+
return Number(event && event.round) || 0
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function eventTime(event) {
|
|
24
|
+
const parsed = Date.parse(String(event && event.at || ''))
|
|
25
|
+
return Number.isFinite(parsed) ? parsed : 0
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function emptyDecision() {
|
|
29
|
+
return {
|
|
30
|
+
fresh: [], retry: [], waiting: [], unchanged: [], handled: [], exhausted: [], baseline: [], invalid: [],
|
|
31
|
+
candidates: [], listed: [], rest: [], deferred: [], capped: false,
|
|
32
|
+
budget: { used: 0, remaining: 0 },
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function decideWatermark(input = {}) {
|
|
37
|
+
const out = emptyDecision()
|
|
38
|
+
const items = Array.isArray(input.items) ? input.items : []
|
|
39
|
+
const obs = isObject(input.obs) ? input.obs : {}
|
|
40
|
+
const same = typeof input.sameCursor === 'function' ? input.sameCursor : (left, right) => left === right
|
|
41
|
+
const now = Number.isFinite(Number(input.now)) ? Number(input.now) : Date.now()
|
|
42
|
+
const cooldownMs = Math.max(0, Number(input.cooldownMs ?? 10 * 60 * 1000) || 0)
|
|
43
|
+
const maxRetries = Math.max(1, Number(input.maxRetries ?? 2) || 2)
|
|
44
|
+
const batchLimit = Math.max(1, Math.floor(Number(input.batchLimit ?? 5) || 5))
|
|
45
|
+
|
|
46
|
+
for (const record of items) {
|
|
47
|
+
if (!validRecord(record)) { out.invalid.push(record); continue }
|
|
48
|
+
const key = String(record.key)
|
|
49
|
+
const row = isObject(obs[key]) ? obs[key] : null
|
|
50
|
+
if (row && row.legacy) { out.baseline.push(record); continue }
|
|
51
|
+
if (!row) { out.fresh.push(record); continue }
|
|
52
|
+
if (row.undelivered) { out.fresh.push(record); continue }
|
|
53
|
+
|
|
54
|
+
const current = record.cursor
|
|
55
|
+
const delivered = isObject(row.delivered) ? row.delivered : null
|
|
56
|
+
const handled = isObject(row.handled) ? row.handled : null
|
|
57
|
+
|
|
58
|
+
if (Number(row.observedRound) > 0 && same(current, row.cursor)
|
|
59
|
+
&& row.observedRound >= Math.max(eventRound(delivered), eventRound(handled))) {
|
|
60
|
+
out.unchanged.push(record)
|
|
61
|
+
continue
|
|
62
|
+
}
|
|
63
|
+
if (handled && same(current, handled.cursor) && eventRound(handled) >= eventRound(delivered)) {
|
|
64
|
+
out.handled.push(record)
|
|
65
|
+
continue
|
|
66
|
+
}
|
|
67
|
+
if (delivered && same(current, delivered.cursor)) {
|
|
68
|
+
if (handled && eventRound(handled) >= eventRound(delivered)) {
|
|
69
|
+
out.handled.push(record)
|
|
70
|
+
} else if ((Number(row.retries) || 0) >= maxRetries) {
|
|
71
|
+
out.exhausted.push(record)
|
|
72
|
+
} else if (now - eventTime(delivered) < cooldownMs) {
|
|
73
|
+
out.waiting.push(record)
|
|
74
|
+
} else {
|
|
75
|
+
out.retry.push(record)
|
|
76
|
+
}
|
|
77
|
+
continue
|
|
78
|
+
}
|
|
79
|
+
if (!delivered && !handled && same(current, row.cursor)) out.unchanged.push(record)
|
|
80
|
+
else out.fresh.push(record)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
out.candidates = [...out.fresh, ...out.retry]
|
|
84
|
+
const hourAgo = now - 60 * 60 * 1000
|
|
85
|
+
const wakeTimes = (Array.isArray(input.wakeTimes) ? input.wakeTimes : [])
|
|
86
|
+
.map(Number).filter((at) => Number.isFinite(at) && at >= hourAgo && at <= now)
|
|
87
|
+
const limit = Math.max(0, Number(input.maxWakesPerHour ?? 6) || 0)
|
|
88
|
+
out.budget = { used: wakeTimes.length, remaining: Math.max(0, limit - wakeTimes.length) }
|
|
89
|
+
out.capped = wakeTimes.length >= limit
|
|
90
|
+
|
|
91
|
+
if (out.capped) {
|
|
92
|
+
out.deferred = [...out.candidates]
|
|
93
|
+
} else {
|
|
94
|
+
out.listed = out.candidates.slice(0, batchLimit)
|
|
95
|
+
out.rest = out.candidates.slice(batchLimit)
|
|
96
|
+
}
|
|
97
|
+
return out
|
|
98
|
+
}
|