dsh-hive-runner 0.1.0 → 0.6.1
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 +66 -0
- package/LICENSE +21 -0
- package/README.md +274 -10
- package/README.zh-CN.md +206 -0
- package/SPEC.md +212 -0
- package/cordis.patch.yml +80 -2
- package/docs/DESIGN.md +181 -0
- package/index.js +182 -64
- package/lib/commands.js +280 -0
- package/lib/config.js +264 -0
- package/lib/discover.js +96 -0
- package/lib/hooks.js +92 -0
- package/lib/http.js +120 -0
- package/lib/limits.js +184 -0
- package/lib/queue.js +156 -0
- package/lib/round.js +380 -0
- package/lib/shell.js +72 -0
- package/lib/supervisor.js +574 -0
- package/package.json +45 -10
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HiveSupervisor — the resident, repository-scoped scheduler.
|
|
3
|
+
*
|
|
4
|
+
* It is a plain state machine with injected effects (`now`, `listReady`,
|
|
5
|
+
* `runCard`, `limits`, `discover`) so it can be unit-tested without DSH, while
|
|
6
|
+
* the plugin wires it to `ctx.shell` (command layer), `ctx.subagents` (card
|
|
7
|
+
* workers) and a timer owned by Cordis' effect scope.
|
|
8
|
+
*
|
|
9
|
+
* Concurrency policy (user setting, finite by default):
|
|
10
|
+
* - per role, at most `roleConcurrency[role]` card workers are **live** in one
|
|
11
|
+
* repository at the same time; the package default is
|
|
12
|
+
* `{ coder: 5, tester: 5 }` and `defaultRoleConcurrency` (default 5) caps
|
|
13
|
+
* roles not listed;
|
|
14
|
+
* - each repository can persist its own limits in `<repo>/.hive/limits.json`
|
|
15
|
+
* (`{ role: n }`), read at startup and hot-reloaded on mtime change; the
|
|
16
|
+
* config map is the initial/fallback value;
|
|
17
|
+
* - queued cards do **not** occupy a slot: a worker that finishes a card
|
|
18
|
+
* immediately picks the next queued card of the same role;
|
|
19
|
+
* - 0 pauses that role in that repository;
|
|
20
|
+
* - `maxConcurrentRepos` still caps how many repositories may drain at once;
|
|
21
|
+
* - real subagent capacity is DSH's `subagent.maxActiveSubagents`; when it is
|
|
22
|
+
* exhausted the work item stays queued and is retried, never dropped.
|
|
23
|
+
*
|
|
24
|
+
* One repository is always serial: at most one drain in flight.
|
|
25
|
+
*/
|
|
26
|
+
import {
|
|
27
|
+
DEFAULT_DEFAULT_ROLE_CONCURRENCY,
|
|
28
|
+
DEFAULT_ROLE_CONCURRENCY,
|
|
29
|
+
normalizeRepoKey,
|
|
30
|
+
} from './config.js';
|
|
31
|
+
import { RepoLimits, validateRoleLimits } from './limits.js';
|
|
32
|
+
import { drainQueue } from './queue.js';
|
|
33
|
+
|
|
34
|
+
/** Per-repository lifecycle states exposed by `op=status`. */
|
|
35
|
+
export const STATES = Object.freeze({
|
|
36
|
+
/** Waiting for the next scheduled attempt. */
|
|
37
|
+
IDLE: 'idle',
|
|
38
|
+
/** A drain is currently running. */
|
|
39
|
+
RUNNING: 'running',
|
|
40
|
+
/** Waiting longer: no ready cards, or the last drain threw. */
|
|
41
|
+
BACKOFF: 'backoff',
|
|
42
|
+
/** Skipped by every tick until an operator resumes it. */
|
|
43
|
+
PAUSED: 'paused',
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/** Raised when pause/resume/limits names a repository the supervisor does not track. */
|
|
47
|
+
export class UnknownRepoError extends Error {
|
|
48
|
+
constructor(repo) {
|
|
49
|
+
super('unknown repository ' + JSON.stringify(repo));
|
|
50
|
+
this.name = 'UnknownRepoError';
|
|
51
|
+
this.code = 'hive/unknown-repo';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Deferrable errors are environmental (no live parent Agent, no subagent
|
|
57
|
+
* provider): they set `lastError` and back off, but do not count towards the
|
|
58
|
+
* auto-pause failure budget.
|
|
59
|
+
*/
|
|
60
|
+
export function isDeferrableError(error) {
|
|
61
|
+
if (error === null || typeof error !== 'object') return false;
|
|
62
|
+
if (error.deferrable === true) return true;
|
|
63
|
+
return typeof error.code === 'string' && error.code.startsWith('hive/no-');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function messageOf(error) {
|
|
67
|
+
if (error instanceof Error) return error.message;
|
|
68
|
+
return String(error);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function iso(timestamp) {
|
|
72
|
+
return typeof timestamp === 'number' && Number.isFinite(timestamp) ? new Date(timestamp).toISOString() : null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function emptyBacklog() {
|
|
76
|
+
return { queued: 0, inFlight: 0, completed: 0 };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function copyRoles(roles) {
|
|
80
|
+
const copy = {};
|
|
81
|
+
for (const [role, value] of Object.entries(roles ?? {})) {
|
|
82
|
+
copy[role] = { limit: value.limit, live: value.live, queued: value.queued };
|
|
83
|
+
}
|
|
84
|
+
return copy;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export class HiveSupervisor {
|
|
88
|
+
/**
|
|
89
|
+
* @param {object} [options]
|
|
90
|
+
* @param {string[]} [options.repos] absolute repository paths
|
|
91
|
+
* @param {string[]} [options.paused] repository paths paused at startup
|
|
92
|
+
* @param {number} [options.intervalMs] tick period (default 30s)
|
|
93
|
+
* @param {number} [options.idleBackoffMs] no-work backoff (default 120s)
|
|
94
|
+
* @param {number} [options.maxConcurrentRepos] cross-repository concurrency (default 2)
|
|
95
|
+
* @param {number} [options.maxConsecutiveFailures] auto-pause threshold (default 3)
|
|
96
|
+
* @param {Record<string, number>} [options.roleConcurrency] global role -> live-worker cap
|
|
97
|
+
* @param {Record<string, Record<string, number>>} [options.repoRoleConcurrency] config per-repo overrides
|
|
98
|
+
* @param {number} [options.defaultRoleConcurrency] fallback cap for unlisted roles
|
|
99
|
+
* @param {RepoLimits} [options.limits] persistent limits store (defaults to one built from the config)
|
|
100
|
+
* @param {number} [options.capacityRetryMs] capacity-queue retry delay (default 2s)
|
|
101
|
+
* @param {boolean} [options.autoDiscover]
|
|
102
|
+
* @param {number} [options.discoverIntervalMs]
|
|
103
|
+
* @param {() => number} [options.now]
|
|
104
|
+
* @param {(repo: string, options: object) => Promise<object[]>} [options.listReady]
|
|
105
|
+
* @param {(request: object) => Promise<object>} [options.runCard]
|
|
106
|
+
* @param {(repo: string) => void} [options.preflight] round-start environment check (merge-on-commit hook)
|
|
107
|
+
* @param {(repo: string) => Promise<string|{mode: string}>} [options.readMode] Hive mode reader (free/freeze)
|
|
108
|
+
* @param {() => Promise<string[]>} [options.discover]
|
|
109
|
+
* @param {Console} [options.logger]
|
|
110
|
+
* @param {boolean} [options.debug]
|
|
111
|
+
*/
|
|
112
|
+
constructor(options = {}) {
|
|
113
|
+
this.intervalMs = options.intervalMs ?? 30_000;
|
|
114
|
+
this.idleBackoffMs = options.idleBackoffMs ?? 120_000;
|
|
115
|
+
this.maxConcurrentRepos = options.maxConcurrentRepos ?? 2;
|
|
116
|
+
this.maxConsecutiveFailures = options.maxConsecutiveFailures ?? 3;
|
|
117
|
+
this.roleConfig = {
|
|
118
|
+
roleConcurrency: options.roleConcurrency ?? { ...DEFAULT_ROLE_CONCURRENCY },
|
|
119
|
+
repoRoleConcurrency: options.repoRoleConcurrency ?? {},
|
|
120
|
+
defaultRoleConcurrency: options.defaultRoleConcurrency ?? DEFAULT_DEFAULT_ROLE_CONCURRENCY,
|
|
121
|
+
};
|
|
122
|
+
this.limits = options.limits ?? new RepoLimits({
|
|
123
|
+
roleConcurrency: this.roleConfig.roleConcurrency,
|
|
124
|
+
repoRoleConcurrency: this.roleConfig.repoRoleConcurrency,
|
|
125
|
+
defaultRoleConcurrency: this.roleConfig.defaultRoleConcurrency,
|
|
126
|
+
});
|
|
127
|
+
this.capacityRetryMs = options.capacityRetryMs ?? 2_000;
|
|
128
|
+
this.autoDiscover = options.autoDiscover === true;
|
|
129
|
+
this.discoverIntervalMs = options.discoverIntervalMs ?? 10 * 60_000;
|
|
130
|
+
this.now = typeof options.now === 'function' ? options.now : () => Date.now();
|
|
131
|
+
this.listReady = options.listReady;
|
|
132
|
+
this.runCard = options.runCard;
|
|
133
|
+
this.preflight = typeof options.preflight === 'function' ? options.preflight : undefined;
|
|
134
|
+
this.readMode = typeof options.readMode === 'function' ? options.readMode : undefined;
|
|
135
|
+
this.discover = options.discover;
|
|
136
|
+
this.logger = options.logger === undefined ? console : options.logger;
|
|
137
|
+
this.debug = options.debug === true;
|
|
138
|
+
|
|
139
|
+
/** @type {Map<string, object>} */
|
|
140
|
+
this.repos = new Map();
|
|
141
|
+
/** Repository paths paused by config; applied when they are first registered. */
|
|
142
|
+
this.configPaused = new Set((options.paused ?? []).map((repo) => normalizeRepoKey(repo)));
|
|
143
|
+
for (const repo of options.repos ?? []) this.addRepo(repo);
|
|
144
|
+
|
|
145
|
+
this.started = false;
|
|
146
|
+
this.timer = null;
|
|
147
|
+
this.abortController = null;
|
|
148
|
+
this.lastTickAt = null;
|
|
149
|
+
this.nextDiscoverAt = 0;
|
|
150
|
+
this.lastDiscoverError = null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Whether the periodic timer is currently armed. */
|
|
154
|
+
isRunning() {
|
|
155
|
+
return this.started;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#seedRoles(repo) {
|
|
159
|
+
const roles = {};
|
|
160
|
+
for (const role of this.limits.rolesFor(repo)) {
|
|
161
|
+
roles[role] = { limit: this.limits.limitFor(repo, role), live: 0, queued: 0 };
|
|
162
|
+
}
|
|
163
|
+
return roles;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
#resetLiveCounts(entry) {
|
|
167
|
+
entry.roles = this.#seedRoles(entry.repo);
|
|
168
|
+
entry.backlog = emptyBacklog();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Register a repository (idempotent). Config-paused paths start paused.
|
|
173
|
+
* @param {string} repo absolute path
|
|
174
|
+
* @returns {object} the repository status snapshot
|
|
175
|
+
*/
|
|
176
|
+
addRepo(repo) {
|
|
177
|
+
const key = normalizeRepoKey(repo);
|
|
178
|
+
const existing = this.repos.get(key);
|
|
179
|
+
if (existing !== undefined) return this.#snapshot(existing);
|
|
180
|
+
const entry = {
|
|
181
|
+
repo: key,
|
|
182
|
+
state: STATES.IDLE,
|
|
183
|
+
paused: false,
|
|
184
|
+
pausedBy: null,
|
|
185
|
+
pausedReason: null,
|
|
186
|
+
inFlight: false,
|
|
187
|
+
lastRoundAt: null,
|
|
188
|
+
lastError: null,
|
|
189
|
+
lastCheckAt: null,
|
|
190
|
+
nextAttemptAt: 0,
|
|
191
|
+
consecutiveFailures: 0,
|
|
192
|
+
lastCards: 0,
|
|
193
|
+
lastResults: 0,
|
|
194
|
+
queue: [],
|
|
195
|
+
backlog: emptyBacklog(),
|
|
196
|
+
roles: this.#seedRoles(key),
|
|
197
|
+
limitsError: this.limits.errorFor(key),
|
|
198
|
+
environmentError: null,
|
|
199
|
+
mode: null,
|
|
200
|
+
frozenSkipped: 0,
|
|
201
|
+
lastSkipReason: null,
|
|
202
|
+
};
|
|
203
|
+
if (this.configPaused.has(key)) {
|
|
204
|
+
entry.paused = true;
|
|
205
|
+
entry.pausedBy = 'config';
|
|
206
|
+
entry.pausedReason = 'paused by config (remove it from config.paused and restart, or POST ?op=resume)';
|
|
207
|
+
entry.state = STATES.PAUSED;
|
|
208
|
+
}
|
|
209
|
+
this.repos.set(key, entry);
|
|
210
|
+
if (this.debug) this.#log('info', 'registered repository', key);
|
|
211
|
+
return this.#snapshot(entry);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** @param {string} repo */
|
|
215
|
+
hasRepo(repo) {
|
|
216
|
+
try {
|
|
217
|
+
return this.repos.has(normalizeRepoKey(repo));
|
|
218
|
+
} catch {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Persist new role limits for one repository and apply them immediately.
|
|
225
|
+
* The limits file merges with the current persisted entries; the config map
|
|
226
|
+
* remains the fallback for roles not written.
|
|
227
|
+
* @param {string} repo
|
|
228
|
+
* @param {Record<string, number>} roleLimits
|
|
229
|
+
* @returns {object} the repository status snapshot
|
|
230
|
+
*/
|
|
231
|
+
setRoleLimits(repo, roleLimits) {
|
|
232
|
+
const entry = this.#entry(repo);
|
|
233
|
+
validateRoleLimits(roleLimits);
|
|
234
|
+
this.limits.write(entry.repo, roleLimits);
|
|
235
|
+
this.limits.refresh(entry.repo);
|
|
236
|
+
entry.limitsError = this.limits.errorFor(entry.repo);
|
|
237
|
+
const names = new Set([...this.limits.rolesFor(entry.repo), ...Object.keys(entry.roles)]);
|
|
238
|
+
const next = {};
|
|
239
|
+
for (const role of names) {
|
|
240
|
+
next[role] = {
|
|
241
|
+
limit: this.limits.limitFor(entry.repo, role),
|
|
242
|
+
live: entry.roles[role]?.live ?? 0,
|
|
243
|
+
queued: entry.roles[role]?.queued ?? 0,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
entry.roles = next;
|
|
247
|
+
this.#log('info', 'role limits updated for ' + entry.repo);
|
|
248
|
+
return this.#snapshot(entry);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Read the current effective role limits for one repository. */
|
|
252
|
+
roleLimits(repo) {
|
|
253
|
+
const entry = this.#entry(repo);
|
|
254
|
+
this.limits.refresh(entry.repo);
|
|
255
|
+
return copyRoles(this.#seedRoles(entry.repo));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Pause one repository. Auto-paused and config-paused repositories are both
|
|
260
|
+
* resumed through {@link resume} (the failure budget resets).
|
|
261
|
+
*/
|
|
262
|
+
pause(repo, reason = 'paused manually') {
|
|
263
|
+
const entry = this.#entry(repo);
|
|
264
|
+
entry.paused = true;
|
|
265
|
+
entry.pausedBy = 'manual';
|
|
266
|
+
entry.pausedReason = reason;
|
|
267
|
+
entry.state = STATES.PAUSED;
|
|
268
|
+
this.#log('info', 'paused repository', entry.repo);
|
|
269
|
+
return this.#snapshot(entry);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Resume one repository and clear its failure budget / error. */
|
|
273
|
+
resume(repo) {
|
|
274
|
+
const entry = this.#entry(repo);
|
|
275
|
+
entry.paused = false;
|
|
276
|
+
entry.pausedBy = null;
|
|
277
|
+
entry.pausedReason = null;
|
|
278
|
+
entry.consecutiveFailures = 0;
|
|
279
|
+
entry.lastError = null;
|
|
280
|
+
entry.state = STATES.IDLE;
|
|
281
|
+
entry.nextAttemptAt = this.now();
|
|
282
|
+
this.#log('info', 'resumed repository', entry.repo);
|
|
283
|
+
return this.#snapshot(entry);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Arm the timer; the first tick runs immediately (asynchronously). */
|
|
287
|
+
start() {
|
|
288
|
+
if (this.started) return;
|
|
289
|
+
this.started = true;
|
|
290
|
+
this.abortController = new AbortController();
|
|
291
|
+
void this.tick().catch((error) => this.#log('warn', 'tick failed', messageOf(error)));
|
|
292
|
+
this.timer = setInterval(() => {
|
|
293
|
+
void this.tick().catch((error) => this.#log('warn', 'tick failed', messageOf(error)));
|
|
294
|
+
}, this.intervalMs);
|
|
295
|
+
this.timer.unref?.();
|
|
296
|
+
if (this.debug) this.#log('info', 'supervisor started', this.intervalMs + 'ms');
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** Disarm the timer and abort in-flight drains. */
|
|
300
|
+
stop() {
|
|
301
|
+
this.started = false;
|
|
302
|
+
if (this.timer !== null) {
|
|
303
|
+
clearInterval(this.timer);
|
|
304
|
+
this.timer = null;
|
|
305
|
+
}
|
|
306
|
+
this.abortController?.abort();
|
|
307
|
+
this.abortController = null;
|
|
308
|
+
if (this.debug) this.#log('info', 'supervisor stopped');
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* One scheduling pass: discover if due, then start up to
|
|
313
|
+
* `maxConcurrentRepos` due repositories and await their drains.
|
|
314
|
+
* Never rejects; per-repository failures become status fields.
|
|
315
|
+
* @param {number} [at] epoch ms (tests may pin the clock)
|
|
316
|
+
* @returns {Promise<Array<{ repo: string, state: string }>>}
|
|
317
|
+
*/
|
|
318
|
+
async tick(at = this.now()) {
|
|
319
|
+
this.lastTickAt = at;
|
|
320
|
+
if (this.autoDiscover && typeof this.discover === 'function' && at >= this.nextDiscoverAt) {
|
|
321
|
+
await this.#refreshDiscovery(at);
|
|
322
|
+
}
|
|
323
|
+
const due = [...this.repos.values()]
|
|
324
|
+
.filter((entry) => !entry.paused && !entry.inFlight && entry.nextAttemptAt <= at)
|
|
325
|
+
.sort((a, b) => a.nextAttemptAt - b.nextAttemptAt);
|
|
326
|
+
|
|
327
|
+
const running = [];
|
|
328
|
+
for (const entry of due) {
|
|
329
|
+
if (running.length >= this.maxConcurrentRepos) break;
|
|
330
|
+
running.push(this.#runEntry(entry));
|
|
331
|
+
}
|
|
332
|
+
const settled = await Promise.allSettled(running);
|
|
333
|
+
const outcomes = [];
|
|
334
|
+
for (const item of settled) {
|
|
335
|
+
if (item.status === 'fulfilled') outcomes.push(item.value);
|
|
336
|
+
else this.#log('warn', 'round bookkeeping failed', messageOf(item.reason));
|
|
337
|
+
}
|
|
338
|
+
return outcomes;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** Serializable supervisor + per-repository status. */
|
|
342
|
+
status() {
|
|
343
|
+
return {
|
|
344
|
+
started: this.started,
|
|
345
|
+
intervalMs: this.intervalMs,
|
|
346
|
+
idleBackoffMs: this.idleBackoffMs,
|
|
347
|
+
maxConcurrentRepos: this.maxConcurrentRepos,
|
|
348
|
+
maxConsecutiveFailures: this.maxConsecutiveFailures,
|
|
349
|
+
roleConcurrency: { ...this.roleConfig.roleConcurrency },
|
|
350
|
+
repoRoleConcurrency: { ...this.roleConfig.repoRoleConcurrency },
|
|
351
|
+
defaultRoleConcurrency: this.roleConfig.defaultRoleConcurrency,
|
|
352
|
+
capacityRetryMs: this.capacityRetryMs,
|
|
353
|
+
lastTickAt: iso(this.lastTickAt),
|
|
354
|
+
lastDiscoverError: this.lastDiscoverError,
|
|
355
|
+
repos: [...this.repos.values()]
|
|
356
|
+
.map((entry) => this.#snapshot(entry))
|
|
357
|
+
.sort((a, b) => a.repo.localeCompare(b.repo)),
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
async #refreshDiscovery(at) {
|
|
362
|
+
this.nextDiscoverAt = at + this.discoverIntervalMs;
|
|
363
|
+
try {
|
|
364
|
+
const found = await this.discover();
|
|
365
|
+
let added = 0;
|
|
366
|
+
for (const repo of found ?? []) {
|
|
367
|
+
const key = normalizeRepoKey(repo);
|
|
368
|
+
if (!this.repos.has(key)) {
|
|
369
|
+
this.addRepo(key);
|
|
370
|
+
added += 1;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
this.lastDiscoverError = null;
|
|
374
|
+
if (added > 0) this.#log('info', 'auto-discovered repositories', added);
|
|
375
|
+
} catch (error) {
|
|
376
|
+
this.lastDiscoverError = messageOf(error);
|
|
377
|
+
this.#log('warn', 'auto-discovery failed', this.lastDiscoverError);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
async #runEntry(entry) {
|
|
382
|
+
entry.inFlight = true;
|
|
383
|
+
entry.state = STATES.RUNNING;
|
|
384
|
+
const signal = this.abortController?.signal;
|
|
385
|
+
try {
|
|
386
|
+
if (entry.paused) return { repo: entry.repo, state: entry.state };
|
|
387
|
+
if (typeof this.listReady !== 'function') throw new Error('supervisor has no listReady implementation');
|
|
388
|
+
if (typeof this.runCard !== 'function') throw new Error('supervisor has no runCard implementation');
|
|
389
|
+
|
|
390
|
+
// Round-start environment check (merge-on-commit pre-commit hook). A
|
|
391
|
+
// missing hook is a deferrable environment error, never a card failure.
|
|
392
|
+
if (typeof this.preflight === 'function') this.preflight(entry.repo);
|
|
393
|
+
|
|
394
|
+
// Hot-reload <repo>/.hive/limits.json at the start of every drain.
|
|
395
|
+
this.limits.refresh(entry.repo);
|
|
396
|
+
entry.limitsError = this.limits.errorFor(entry.repo);
|
|
397
|
+
|
|
398
|
+
// Read the Hive mode first: in freeze mode only fix-tagged cards run.
|
|
399
|
+
// Non-fix cards are skipped (left on the board), never marked failed.
|
|
400
|
+
if (typeof this.readMode === 'function') {
|
|
401
|
+
const read = await this.readMode(entry.repo);
|
|
402
|
+
entry.mode = typeof read === 'string' ? read : read?.mode ?? 'free';
|
|
403
|
+
} else {
|
|
404
|
+
entry.mode = 'free';
|
|
405
|
+
}
|
|
406
|
+
const frozen = entry.mode === 'freeze';
|
|
407
|
+
let frozenSkipped = 0;
|
|
408
|
+
if (frozen) {
|
|
409
|
+
entry.queue = entry.queue.filter((item) => {
|
|
410
|
+
const keep = Array.isArray(item?.card?.tags) && item.card.tags.includes('fix');
|
|
411
|
+
if (!keep) frozenSkipped += 1;
|
|
412
|
+
return keep;
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const cards = await this.listReady(entry.repo, { signal });
|
|
417
|
+
entry.lastCheckAt = this.now();
|
|
418
|
+
if (entry.paused) return { repo: entry.repo, state: entry.state };
|
|
419
|
+
const queuedIds = new Set(entry.queue.map((item) => item?.card?.id));
|
|
420
|
+
for (const card of Array.isArray(cards) ? cards : []) {
|
|
421
|
+
if (typeof card?.id !== 'string' || queuedIds.has(card.id)) continue;
|
|
422
|
+
if (frozen && !(Array.isArray(card.tags) && card.tags.includes('fix'))) {
|
|
423
|
+
frozenSkipped += 1;
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
entry.queue.push({ role: card.role, card, claimed: false });
|
|
427
|
+
queuedIds.add(card.id);
|
|
428
|
+
}
|
|
429
|
+
entry.frozenSkipped = frozenSkipped;
|
|
430
|
+
entry.lastSkipReason = frozen && frozenSkipped > 0
|
|
431
|
+
? 'mode=freeze: skipped ' + frozenSkipped + ' non-fix card(s); only fix-tagged cards are dispatched'
|
|
432
|
+
: null;
|
|
433
|
+
if (entry.queue.length === 0) {
|
|
434
|
+
entry.state = STATES.BACKOFF;
|
|
435
|
+
entry.lastError = null;
|
|
436
|
+
this.#resetLiveCounts(entry);
|
|
437
|
+
entry.nextAttemptAt = this.now() + this.idleBackoffMs;
|
|
438
|
+
return { repo: entry.repo, state: entry.state, cards: 0 };
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const queueRoles = entry.queue.map((item) => item.role);
|
|
442
|
+
const roleNames = this.limits.rolesFor(entry.repo, queueRoles);
|
|
443
|
+
const outcome = await drainQueue({
|
|
444
|
+
queue: entry.queue,
|
|
445
|
+
roleNames,
|
|
446
|
+
limitFor: (role) => this.limits.limitFor(entry.repo, role),
|
|
447
|
+
capacityRetryMs: this.capacityRetryMs,
|
|
448
|
+
signal,
|
|
449
|
+
sleep: (ms) => this.#sleep(ms, signal),
|
|
450
|
+
runCard: (item) => this.runCard({
|
|
451
|
+
repo: entry.repo,
|
|
452
|
+
card: item.card,
|
|
453
|
+
claimed: item.claimed === true,
|
|
454
|
+
signal,
|
|
455
|
+
}),
|
|
456
|
+
onState: (state) => {
|
|
457
|
+
entry.roles = copyRoles(state.roles);
|
|
458
|
+
entry.backlog = { queued: state.queued, inFlight: state.live, completed: state.completed };
|
|
459
|
+
},
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
entry.queue = outcome.queue;
|
|
463
|
+
entry.roles = copyRoles(outcome.state.roles);
|
|
464
|
+
entry.backlog = {
|
|
465
|
+
queued: outcome.state.queued,
|
|
466
|
+
inFlight: outcome.state.live,
|
|
467
|
+
completed: outcome.state.completed,
|
|
468
|
+
};
|
|
469
|
+
if (signal?.aborted) return { repo: entry.repo, state: entry.state, aborted: true };
|
|
470
|
+
if (outcome.error !== undefined) throw outcome.error;
|
|
471
|
+
|
|
472
|
+
entry.lastRoundAt = this.now();
|
|
473
|
+
entry.lastCards = outcome.results.length;
|
|
474
|
+
entry.lastResults = outcome.results.length;
|
|
475
|
+
entry.consecutiveFailures = 0;
|
|
476
|
+
entry.lastError = null;
|
|
477
|
+
entry.environmentError = null;
|
|
478
|
+
if (!entry.paused) {
|
|
479
|
+
entry.state = STATES.IDLE;
|
|
480
|
+
entry.nextAttemptAt = this.now() + this.intervalMs;
|
|
481
|
+
}
|
|
482
|
+
return { repo: entry.repo, state: entry.state, cards: outcome.results.length, results: outcome.results };
|
|
483
|
+
} catch (error) {
|
|
484
|
+
if (signal?.aborted) return { repo: entry.repo, state: entry.state, aborted: true };
|
|
485
|
+
entry.lastError = messageOf(error);
|
|
486
|
+
entry.backlog.queued = entry.queue.length;
|
|
487
|
+
entry.environmentError = isDeferrableError(error) ? entry.lastError : null;
|
|
488
|
+
if (isDeferrableError(error)) {
|
|
489
|
+
// Environmental: retry after the idle backoff without burning the
|
|
490
|
+
// failure budget (opening a session later must not be "3 failures").
|
|
491
|
+
entry.state = entry.paused ? STATES.PAUSED : STATES.BACKOFF;
|
|
492
|
+
entry.nextAttemptAt = this.now() + this.idleBackoffMs;
|
|
493
|
+
this.#log('warn', 'deferred round for ' + entry.repo + ': ' + entry.lastError);
|
|
494
|
+
} else {
|
|
495
|
+
entry.consecutiveFailures += 1;
|
|
496
|
+
entry.lastRoundAt = this.now();
|
|
497
|
+
if (entry.consecutiveFailures >= this.maxConsecutiveFailures) {
|
|
498
|
+
entry.paused = true;
|
|
499
|
+
entry.pausedBy = 'auto';
|
|
500
|
+
entry.pausedReason = 'auto-paused after ' + entry.consecutiveFailures + ' consecutive round failures: ' + entry.lastError;
|
|
501
|
+
entry.state = STATES.PAUSED;
|
|
502
|
+
this.#log('warn', entry.pausedReason);
|
|
503
|
+
} else {
|
|
504
|
+
entry.state = STATES.BACKOFF;
|
|
505
|
+
entry.nextAttemptAt = this.now() + this.#failureBackoff(entry.consecutiveFailures);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
return { repo: entry.repo, state: entry.state, error: entry.lastError };
|
|
509
|
+
} finally {
|
|
510
|
+
entry.inFlight = false;
|
|
511
|
+
if (entry.paused) entry.state = STATES.PAUSED;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** Abort-aware sleep used between capacity retries. */
|
|
516
|
+
async #sleep(ms, signal) {
|
|
517
|
+
if (signal?.aborted) return;
|
|
518
|
+
await new Promise((resolve) => {
|
|
519
|
+
const timer = setTimeout(done, ms);
|
|
520
|
+
timer.unref?.();
|
|
521
|
+
function done() {
|
|
522
|
+
clearTimeout(timer);
|
|
523
|
+
signal?.removeEventListener('abort', done);
|
|
524
|
+
resolve();
|
|
525
|
+
}
|
|
526
|
+
signal?.addEventListener('abort', done, { once: true });
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/** Exponential failure backoff, capped at the idle backoff. */
|
|
531
|
+
#failureBackoff(failures) {
|
|
532
|
+
return Math.min(this.idleBackoffMs, this.intervalMs * 2 ** failures);
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
#entry(repo) {
|
|
536
|
+
const key = normalizeRepoKey(repo);
|
|
537
|
+
const entry = this.repos.get(key);
|
|
538
|
+
if (entry === undefined) throw new UnknownRepoError(key);
|
|
539
|
+
return entry;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
#snapshot(entry) {
|
|
543
|
+
return {
|
|
544
|
+
repo: entry.repo,
|
|
545
|
+
state: entry.state,
|
|
546
|
+
lastRoundAt: iso(entry.lastRoundAt),
|
|
547
|
+
lastError: entry.lastError,
|
|
548
|
+
inFlight: entry.inFlight,
|
|
549
|
+
paused: entry.paused,
|
|
550
|
+
pausedBy: entry.pausedBy,
|
|
551
|
+
pausedReason: entry.pausedReason,
|
|
552
|
+
lastCheckAt: iso(entry.lastCheckAt),
|
|
553
|
+
nextAttemptAt: iso(entry.nextAttemptAt === 0 ? null : entry.nextAttemptAt),
|
|
554
|
+
consecutiveFailures: entry.consecutiveFailures,
|
|
555
|
+
lastCards: entry.lastCards,
|
|
556
|
+
lastResults: entry.lastResults,
|
|
557
|
+
backlog: { ...entry.backlog },
|
|
558
|
+
roles: copyRoles(entry.roles),
|
|
559
|
+
limitsError: entry.limitsError ?? null,
|
|
560
|
+
environmentError: entry.environmentError ?? null,
|
|
561
|
+
mode: entry.mode ?? null,
|
|
562
|
+
frozenSkipped: entry.frozenSkipped ?? 0,
|
|
563
|
+
lastSkipReason: entry.lastSkipReason ?? null,
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
#log(level, ...args) {
|
|
568
|
+
if (this.logger === false || this.logger === null) return;
|
|
569
|
+
const write = this.logger?.[level];
|
|
570
|
+
if (typeof write === 'function') write.call(this.logger, '[hive-runner]', ...args);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
export default HiveSupervisor;
|
package/package.json
CHANGED
|
@@ -1,27 +1,62 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-hive-runner",
|
|
3
|
-
"version": "0.1
|
|
4
|
-
"description": "DSH host plugin for Hive:
|
|
3
|
+
"version": "0.6.1",
|
|
4
|
+
"description": "DSH host plugin for Hive: a resident supervisor that runs one DSH subagent per ready task card, with per-role worker limits, pause/resume and Hive tools.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./package.json": "./package.json"
|
|
10
|
+
},
|
|
7
11
|
"files": [
|
|
8
12
|
"index.js",
|
|
13
|
+
"lib",
|
|
14
|
+
"docs",
|
|
9
15
|
"cordis.patch.yml",
|
|
10
|
-
"
|
|
16
|
+
"SPEC.md",
|
|
17
|
+
"README.md",
|
|
18
|
+
"README.zh-CN.md",
|
|
19
|
+
"CHANGELOG.md",
|
|
20
|
+
"LICENSE"
|
|
11
21
|
],
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=22"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"test": "node --test test/*.test.js",
|
|
27
|
+
"check": "node --check index.js && ls lib/*.js | xargs -n 1 node --check"
|
|
28
|
+
},
|
|
12
29
|
"peerDependencies": {
|
|
13
30
|
"@deepseek-ai/dsh-tools": "*"
|
|
14
31
|
},
|
|
15
|
-
"dsh": {
|
|
16
|
-
"bundle": {
|
|
17
|
-
"patch": "./cordis.patch.yml"
|
|
18
|
-
}
|
|
19
|
-
},
|
|
20
32
|
"keywords": [
|
|
33
|
+
"dsh",
|
|
21
34
|
"dsh-plugin",
|
|
35
|
+
"deepseek-harness",
|
|
22
36
|
"hive",
|
|
23
37
|
"agents",
|
|
24
|
-
"swarm"
|
|
38
|
+
"swarm",
|
|
39
|
+
"supervisor",
|
|
40
|
+
"subagent",
|
|
41
|
+
"task-queue",
|
|
42
|
+
"multi-agent"
|
|
25
43
|
],
|
|
26
|
-
"license": "MIT"
|
|
44
|
+
"license": "MIT",
|
|
45
|
+
"author": "Tommy <thf85@qq.com>",
|
|
46
|
+
"repository": {
|
|
47
|
+
"type": "git",
|
|
48
|
+
"url": "git+https://github.com/d86e/dsh-hive-runner.git"
|
|
49
|
+
},
|
|
50
|
+
"bugs": {
|
|
51
|
+
"url": "https://github.com/d86e/dsh-hive-runner/issues"
|
|
52
|
+
},
|
|
53
|
+
"homepage": "https://github.com/d86e/dsh-hive-runner#readme",
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
},
|
|
57
|
+
"dsh": {
|
|
58
|
+
"bundle": {
|
|
59
|
+
"patch": "./cordis.patch.yml"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
27
62
|
}
|